Skip to content

Commit

Permalink
#1619 Import/export of variant monomers from IDT
Browse files Browse the repository at this point in the history
Add import/export of standard variant ID monomers.
Add UTs
  • Loading branch information
AliaksandrDziarkach committed Aug 7, 2024
1 parent 4927ba5 commit b9738c5
Show file tree
Hide file tree
Showing 28 changed files with 173,483 additions and 332 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
8 changes: 8 additions & 0 deletions api/c/indigo/src/indigo_savers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,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
28 changes: 28 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,8 @@

#include "common.h"

#include <fstream>

using namespace std;
using namespace indigo;

Expand Down Expand Up @@ -282,3 +284,29 @@ 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);
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);
}
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/ket_to_idt.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_std.ket:SUCCEED
idt_mod_phosphates.ket:SUCCEED
idt_modifications.ket:SUCCEED
idt_more_than_80_chars.ket:SUCCEED
Expand Down
3 changes: 2 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,11 @@ 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",
}

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
Loading

0 comments on commit b9738c5

Please sign in to comment.