Skip to content

Commit

Permalink
#1619 Import/export of variant monomers from IDT (#2198)
Browse files Browse the repository at this point in the history
Co-authored-by: Aliakasndr Dziarkach <Aliakasndr.Dziarkach@gmail.com>
  • Loading branch information
AliaksandrDziarkach and Aliakasndr Dziarkach authored Aug 14, 2024
1 parent 5eb60ce commit 179c0bc
Show file tree
Hide file tree
Showing 39 changed files with 175,111 additions and 369 deletions.
6 changes: 6 additions & 0 deletions api/c/indigo/indigo.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ CEXPORT int indigoLoadSmartsFromBuffer(const char* buffer, int size);
CEXPORT int indigoLoadMonomerLibrary(int source);
CEXPORT int indigoLoadMonomerLibraryFromString(const char* string);
CEXPORT int indigoLoadMonomerLibraryFromFile(const char* filename);
CEXPORT int indigoLoadMonomerLibraryFromBuffer(const char* buffer, int size);

CEXPORT int indigoLoadKetDocument(int source);
CEXPORT int indigoLoadKetDocumentFromString(const char* string);
CEXPORT int indigoLoadKetDocumentFromFile(const char* filename);
CEXPORT int indigoLoadKetDocumentFromBuffer(const char* buffer, int size);

CEXPORT int indigoLoadSequence(int source, const char* seq_type, int library);
CEXPORT int indigoLoadSequenceFromString(const char* string, const char* seq_type, int library);
Expand Down
8 changes: 8 additions & 0 deletions api/c/indigo/src/indigo_macros.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ WRAPPER_LOAD_FROM_STRING(indigoLoadReactionSmarts)
WRAPPER_LOAD_FROM_FILE(indigoLoadReactionSmarts)
WRAPPER_LOAD_FROM_BUFFER(indigoLoadReactionSmarts)

WRAPPER_LOAD_FROM_STRING(indigoLoadMonomerLibrary)
WRAPPER_LOAD_FROM_FILE(indigoLoadMonomerLibrary)
WRAPPER_LOAD_FROM_BUFFER(indigoLoadMonomerLibrary)

WRAPPER_LOAD_FROM_STRING(indigoLoadKetDocument)
WRAPPER_LOAD_FROM_FILE(indigoLoadKetDocument)
WRAPPER_LOAD_FROM_BUFFER(indigoLoadKetDocument)

CEXPORT int indigoSaveMolfileToFile(int molecule, const char* filename)
{
int f = indigoWriteFile(filename);
Expand Down
37 changes: 10 additions & 27 deletions api/c/indigo/src/indigo_molecule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "molecule/canonical_smiles_saver.h"
#include "molecule/elements.h"
#include "molecule/hybridization.h"
#include "molecule/ket_document_json_loader.h"
#include "molecule/molecule_auto_loader.h"
#include "molecule/molecule_automorphism_search.h"
#include "molecule/molecule_fingerprint.h"
Expand Down Expand Up @@ -543,36 +544,18 @@ CEXPORT int indigoLoadMonomerLibrary(int source)
INDIGO_END(-1);
}

CEXPORT int indigoLoadMonomerLibraryFromString(const char* string)
CEXPORT int indigoLoadKetDocument(int source)
{
INDIGO_BEGIN
{
int source = indigoReadString(string);
int result;

if (source <= 0)
return -1;

result = indigoLoadMonomerLibrary(source);
indigoFree(source);
return result;
}
INDIGO_END(-1);
}

CEXPORT int indigoLoadMonomerLibraryFromFile(const char* filename)
{
INDIGO_BEGIN
{
int source = indigoReadFile(filename);
int result;

if (source < 0)
return -1;

result = indigoLoadMonomerLibrary(source);
indigoFree(source);
return result;
IndigoObject& obj = self.getObject(source);
auto& scanner = IndigoScanner::get(obj);
std::string json_str;
scanner.readAll(json_str);
std::unique_ptr<IndigoKetDocument> docptr = std::make_unique<IndigoKetDocument>();
KetDocumentJsonLoader loader{};
loader.parseJson(json_str, docptr->get());
return self.addObject(docptr.release());
}
INDIGO_END(-1);
}
Expand Down
17 changes: 17 additions & 0 deletions api/c/indigo/src/indigo_savers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "base_cpp/scanner.h"
#include "molecule/canonical_smiles_saver.h"
#include "molecule/cml_saver.h"
#include "molecule/ket_document_json_saver.h"
#include "molecule/molecule_cdxml_saver.h"
#include "molecule/molecule_json_saver.h"
#include "molecule/molfile_loader.h"
Expand Down Expand Up @@ -639,6 +640,14 @@ CEXPORT int indigoSaveIdt(int item, int output, int library)
out.flush();
return 1;
}
else if (IndigoKetDocument::is(obj))
{
IndigoObject& lib_obj = self.getObject(library);
SequenceSaver saver(out, IndigoMonomerLibrary::get(lib_obj));
saver.saveKetDocument(static_cast<IndigoKetDocument&>(obj).get(), SequenceSaver::SeqFormat::IDT);
out.flush();
return 1;
}
throw IndigoError("indigoSaveIdt(): expected molecule, got %s", obj.debugInfo());
}
INDIGO_END(-1);
Expand Down Expand Up @@ -688,6 +697,14 @@ CEXPORT int indigoSaveJson(int item, int output)
out.flush();
return 1;
}
else if (IndigoKetDocument::is(obj))
{
KetDocumentJsonSaver js(out);
js.pretty_json = self.json_saving_pretty;
js.saveKetDocument(static_cast<IndigoKetDocument&>(obj).get());
out.flush();
return 1;
}
throw IndigoError("indigoSaveJson(): expected molecule or reaction, got %s", obj.debugInfo());
}
INDIGO_END(-1);
Expand Down
30 changes: 30 additions & 0 deletions api/c/tests/unit/tests/formats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

#include "common.h"

#include <algorithm>
#include <fstream>

using namespace std;
using namespace indigo;

Expand Down Expand Up @@ -282,3 +285,30 @@ TEST_F(IndigoApiFormatsTest, noFile)
},
Exception);
}

TEST_F(IndigoApiFormatsTest, idt_to_ket)
{
int obj = -1;
int library = indigoLoadMonomerLibraryFromFile(dataPath("molecules/basic/monomer_library.ket").c_str());
obj = indigoLoadIdtFromString("ARAS", library);
indigoSetOptionBool("json-saving-pretty", true);
const char* res = indigoJson(obj);
// printf("res=%s", res);
std::ifstream is(dataPath("molecules/basic/idt_mixed_std.ket"), std::ios::binary | std::ios::ate);
auto size = is.tellg();
std::string str(size, '\0'); // construct string to stream size
is.seekg(0);
is.read(&str[0], size);
str.erase(std::remove(str.begin(), str.end(), '\r'), str.end());
ASSERT_STREQ(res, str.c_str());
}

TEST_F(IndigoApiFormatsTest, ket_to_idt)
{
int obj = -1;
int library = indigoLoadMonomerLibraryFromFile(dataPath("molecules/basic/monomer_library.ket").c_str());
obj = indigoLoadKetDocumentFromFile(dataPath("molecules/basic/idt_mixed_std.ket").c_str());
const char* res = indigoIdt(obj, library);
// printf("res=%s", res);
ASSERT_STREQ("ARAS", res);
}
12 changes: 12 additions & 0 deletions api/dotnet/src/Indigo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,12 @@ public IndigoObject loadMonomerLibrary(string str)
return new IndigoObject(this, checkResult(IndigoLib.indigoLoadMonomerLibraryFromString(str)));
}

public IndigoObject loadKetDocument(string str)
{
setSessionID();
return new IndigoObject(this, checkResult(IndigoLib.indigoLoadKetDocumentFromString(str)));
}

public IndigoObject loadSequence(string str, string seq_type, IndigoObject library)
{
setSessionID();
Expand Down Expand Up @@ -490,6 +496,12 @@ public IndigoObject loadMonomerLibraryFromFile(string path)
return new IndigoObject(this, checkResult(IndigoLib.indigoLoadMonomerLibraryFromFile(path)));
}

public IndigoObject loadKetDocumentFromFile(string path)
{
setSessionID();
return new IndigoObject(this, checkResult(IndigoLib.indigoLoadKetDocumentFromFile(path)));
}

public IndigoObject loadSequenceFromFile(string path, string seq_type, IndigoObject library)
{
setSessionID();
Expand Down
6 changes: 6 additions & 0 deletions api/dotnet/src/IndigoLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ public unsafe class IndigoLib
[DllImport("indigo"), SuppressUnmanagedCodeSecurity]
public static extern int indigoLoadMonomerLibraryFromString(string str);

[DllImport("indigo"), SuppressUnmanagedCodeSecurity]
public static extern int indigoLoadKetDocumentFromString(string str);

[DllImport("indigo"), SuppressUnmanagedCodeSecurity]
public static extern int indigoLoadSequenceFromString(string str, string seq_type, int library);

Expand All @@ -146,6 +149,9 @@ public unsafe class IndigoLib
[DllImport("indigo"), SuppressUnmanagedCodeSecurity]
public static extern int indigoLoadMonomerLibraryFromFile(string filename);

[DllImport("indigo"), SuppressUnmanagedCodeSecurity]
public static extern int indigoLoadKetDocumentFromFile(string filename);

[DllImport("indigo"), SuppressUnmanagedCodeSecurity]
public static extern int indigoLoadSequenceFromFile(string filename, string seq_type, int library);

Expand Down
10 changes: 10 additions & 0 deletions api/java/indigo/src/main/java/com/epam/indigo/Indigo.java
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,16 @@ public IndigoObject loadMonomerLibraryFromFile(String path) {
return new IndigoObject(this, checkResult(this, lib.indigoLoadMonomerLibraryFromFile(path)));
}

public IndigoObject loadKetDocument(String str) {
setSessionID();
return new IndigoObject(this, checkResult(this, lib.indigoLoadKetDocumentFromString(str)));
}

public IndigoObject loadKetDocumentFromFile(String path) {
setSessionID();
return new IndigoObject(this, checkResult(this, lib.indigoLoadKetDocumentFromFile(path)));
}

public IndigoObject loadSequence(String str, String seq_type, IndigoObject library) {
setSessionID();
return new IndigoObject(this, checkResult(this, lib.indigoLoadSequenceFromString(str, seq_type, library.self)));
Expand Down
6 changes: 6 additions & 0 deletions api/java/indigo/src/main/java/com/epam/indigo/IndigoLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ public interface IndigoLib extends Library {

int indigoLoadMonomerLibraryFromFile(String filename);

int indigoLoadKetDocument(int source);

int indigoLoadKetDocumentFromString(String str);

int indigoLoadKetDocumentFromFile(String filename);

int indigoLoadSequence(int source, String seq_type, int library);

int indigoLoadSequenceFromString(String str, String seq_type, int library);
Expand Down
40 changes: 40 additions & 0 deletions api/python/indigo/indigo/indigo.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,46 @@ def loadMonomerLibraryFromFile(self, filename):
),
)

def loadKetDocument(self, string):
"""Loads ket document from ket string
Args:
string (str): ket
Returns:
IndigoObject: loaded ket document
Raises:
IndigoException: Exception if structure format is incorrect
"""

return IndigoObject(
self,
IndigoLib.checkResult(
self._lib().indigoLoadKetDocumentFromString(string.encode())
),
)

def loadKetDocumentFromFile(self, filename):
"""Loads ket document from from file in ket format
Args:
string (str): full path to the file with ket
Returns:
IndigoObject: loaded ket document
Raises:
IndigoException: Exception if structure format is incorrect
"""

return IndigoObject(
self,
IndigoLib.checkResult(
self._lib().indigoLoadKetDocumentFromFile(filename.encode())
),
)

def loadSequence(self, string, seq_type, library):
"""Loads molecule from DNA/RNA/PEPTIDE sequence string
Expand Down
4 changes: 4 additions & 0 deletions api/python/indigo/indigo/indigo_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ def __init__(self) -> None:
IndigoLib.lib.indigoLoadMonomerLibraryFromString.argtypes = [c_char_p]
IndigoLib.lib.indigoLoadMonomerLibraryFromFile.restype = c_int
IndigoLib.lib.indigoLoadMonomerLibraryFromFile.argtypes = [c_char_p]
IndigoLib.lib.indigoLoadKetDocumentFromString.restype = c_int
IndigoLib.lib.indigoLoadKetDocumentFromString.argtypes = [c_char_p]
IndigoLib.lib.indigoLoadKetDocumentFromFile.restype = c_int
IndigoLib.lib.indigoLoadKetDocumentFromFile.argtypes = [c_char_p]
IndigoLib.lib.indigoLoadSequenceFromString.restype = c_int
IndigoLib.lib.indigoLoadSequenceFromString.argtypes = [
c_char_p,
Expand Down
1 change: 1 addition & 0 deletions api/tests/integration/ref/formats/idt_to_ket.py.out
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ idt_i2moera_sp_32moera.ket:SUCCEED
idt_i2moera_t.ket:SUCCEED
idt_many_molecules.ket:SUCCEED
idt_mixed.ket:SUCCEED
idt_mixed_custom.ket:SUCCEED
idt_mixed_std.ket:SUCCEED
idt_mod_phosphates.ket:SUCCEED
idt_modifications.ket:SUCCEED
Expand Down
2 changes: 2 additions & 0 deletions api/tests/integration/ref/formats/ket_to_idt.py.out
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ idt_i2moera_sp_32moera.ket:SUCCEED
idt_i2moera_t.ket:SUCCEED
idt_many_molecules.ket:SUCCEED
idt_mixed.ket:SUCCEED
idt_mixed_custom.ket:SUCCEED
idt_mixed_std.ket:SUCCEED
idt_mod_phosphates.ket:SUCCEED
idt_modifications.ket:SUCCEED
idt_more_than_80_chars.ket:SUCCEED
Expand Down
1 change: 1 addition & 0 deletions api/tests/integration/tests/formats/idt_to_ket.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def find_diff(a, b):
"idt_unresolved_many": "/unr0/A/unr1/C/unr2/ACTG/unr3/G/unr4/",
"idt_unsplit": "/5UNSPLIT/A",
"idt_mixed_std": "ARAS",
"idt_mixed_custom": "(N1:10203050)(N1)N",
}

lib = indigo.loadMonomerLibraryFromFile(
Expand Down
4 changes: 3 additions & 1 deletion api/tests/integration/tests/formats/ket_to_idt.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ def find_diff(a, b):
"idt_52moera_sp_i2moera_sp": "/52MOErA/*/i2MOErA/*",
"idt_unsplit": "/5UNSPLIT/A",
"idt_more_than_80_chars": "/52MOErA//i2MOErA//i2MOErA//i2MOErA//i2MOErA//i2MOErA//i2MOErA//i2MOErA//i2MOErA//i2MOErA//i2MOErA//i2MOErA//3Phos/",
"idt_mixed_std": "ARAS",
"idt_mixed_custom": "(N1:10203050)(N1)N",
}

for filename in sorted(idt_data.keys()):
mol = indigo.loadMoleculeFromFile(os.path.join(ref, filename + ".ket"))
mol = indigo.loadKetDocumentFromFile(os.path.join(ref, filename + ".ket"))
idt = mol.idt(lib)
idt_ref = idt_data[filename]
if idt_ref == idt:
Expand Down
1 change: 0 additions & 1 deletion api/tests/integration/tests/formats/ref/idt_3phos.ket
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"$ref": "monomer0"
}
],
"connections": [],
"templates": [
{
"$ref": "monomerTemplate-P___Phosphate"
Expand Down
1 change: 0 additions & 1 deletion api/tests/integration/tests/formats/ref/idt_5phos.ket
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"$ref": "monomer0"
}
],
"connections": [],
"templates": [
{
"$ref": "monomerTemplate-P___Phosphate"
Expand Down
Loading

0 comments on commit 179c0bc

Please sign in to comment.