Skip to content

Commit

Permalink
#2472 - Support for three letter sequences (#2590)
Browse files Browse the repository at this point in the history
  • Loading branch information
AliaksandrDziarkach authored Oct 31, 2024
1 parent 3c4fbcd commit 2596f46
Show file tree
Hide file tree
Showing 21 changed files with 5,659 additions and 6 deletions.
4 changes: 4 additions & 0 deletions api/c/indigo/indigo.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ CEXPORT int indigoSaveSequence(int molecule, int output, int library);
CEXPORT int indigoSaveSequenceToFile(int molecule, const char* filename, int library);
CEXPORT const char* indigoSequence(int molecule, int library);

CEXPORT int indigoSaveSequence3Letter(int molecule, int output, int library);
CEXPORT int indigoSaveSequence3LetterToFile(int molecule, const char* filename, int library);
CEXPORT const char* indigoSequence3Letter(int molecule, int library);

CEXPORT int indigoSaveFasta(int molecule, int output, int library);
CEXPORT int indigoSaveFastaToFile(int molecule, const char* filename, int library);
CEXPORT const char* indigoFasta(int molecule, int library);
Expand Down
30 changes: 30 additions & 0 deletions api/c/indigo/src/indigo_macros.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ CEXPORT int indigoSaveSequenceToFile(int item, const char* filename, int library
return res;
}

CEXPORT int indigoSaveSequence3LetterToFile(int item, const char* filename, int library)
{
int f = indigoWriteFile(filename);
int res;

if (f == -1)
return -1;

res = indigoSaveSequence3Letter(item, f, library);

indigoFree(f);
return res;
}

CEXPORT int indigoSaveFastaToFile(int item, const char* filename, int library)
{
int f = indigoWriteFile(filename);
Expand Down Expand Up @@ -222,6 +236,22 @@ CEXPORT const char* indigoSequence(int molecule, int library)
return res;
}

CEXPORT const char* indigoSequence3Letter(int molecule, int library)
{
int b = indigoWriteBuffer();
const char* res;

if (b == -1)
return 0;

if (indigoSaveSequence3Letter(molecule, b, library) == -1)
return 0;

res = indigoToString(b);
indigoFree(b);
return res;
}

CEXPORT const char* indigoFasta(int molecule, int library)
{
int b = indigoWriteBuffer();
Expand Down
19 changes: 19 additions & 0 deletions api/c/indigo/src/indigo_savers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,25 @@ CEXPORT int indigoSaveSequence(int item, int output, int library)
INDIGO_END(-1);
}

CEXPORT int indigoSaveSequence3Letter(int item, int output, int library)
{
INDIGO_BEGIN
{
IndigoObject& obj = self.getObject(item);
Output& out = IndigoOutput::get(self.getObject(output));
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::Sequence3);
out.flush();
return 1;
}
throw IndigoError("indigoSaveSequence(): expected molecule, got %s", obj.debugInfo());
}
INDIGO_END(-1);
}

CEXPORT int indigoSaveFasta(int item, int output, int library)
{
INDIGO_BEGIN
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 @@ -182,6 +182,9 @@ public unsafe class IndigoLib
[DllImport("indigo"), SuppressUnmanagedCodeSecurity]
public static extern int indigoSaveSequenceToFile(int molecule, int output, int library);

[DllImport("indigo"), SuppressUnmanagedCodeSecurity]
public static extern int indigoSaveSequence3LetterToFile(int molecule, int output, int library);

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

Expand All @@ -191,6 +194,9 @@ public unsafe class IndigoLib
[DllImport("indigo"), SuppressUnmanagedCodeSecurity]
public static extern byte* indigoSequence(int molecule, int library);

[DllImport("indigo"), SuppressUnmanagedCodeSecurity]
public static extern byte* indigoSequence3Letter(int molecule, int library);

[DllImport("indigo"), SuppressUnmanagedCodeSecurity]
public static extern byte* indigoFasta(int molecule, int library);

Expand Down
14 changes: 14 additions & 0 deletions api/dotnet/src/IndigoObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ public string sequence(IndigoObject library)
return dispatcher.checkResult(IndigoLib.indigoSequence(self, library.self));
}

public string sequence3Letter(IndigoObject library)
{
dispatcher.setSessionID();
return dispatcher.checkResult(IndigoLib.indigoSequence3Letter(self, library.self));
}

public string fasta(IndigoObject library)
{
dispatcher.setSessionID();
Expand All @@ -118,6 +124,14 @@ public void saveSequenceToFile(string filename, IndigoObject library)
dispatcher.checkResult(IndigoLib.indigoFree(s));
}

public void saveSequence3LetterToFile(string filename, IndigoObject library)
{
dispatcher.setSessionID();
int s = dispatcher.checkResult(IndigoLib.indigoWriteFile(filename));
dispatcher.checkResult(IndigoLib.indigoSaveSequence3LetterToFile(self, s, library.self));
dispatcher.checkResult(IndigoLib.indigoFree(s));
}

public string cml()
{
dispatcher.setSessionID();
Expand Down
4 changes: 4 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 @@ -163,6 +163,8 @@ public interface IndigoLib extends Library {

int indigoSaveSequenceToFile(int molecule, String filename, int library);

int indigoSaveSequence3LetterToFile(int molecule, String filename, int library);

int indigoSaveFastaToFile(int molecule, String filename);

int indigoSaveMolfileToFile(int molecule, String filename);
Expand All @@ -171,6 +173,8 @@ public interface IndigoLib extends Library {

Pointer indigoSequence(int molecule, int library);

Pointer indigoSequence3Letter(int molecule, int library);

Pointer indigoFasta(int molecule, int library);

Pointer indigoIdt(int molecule, int library);
Expand Down
10 changes: 10 additions & 0 deletions api/java/indigo/src/main/java/com/epam/indigo/IndigoObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ public String sequence(IndigoObject library) {
return Indigo.checkResultString(this, lib.indigoSequence(self, library.self));
}

public String sequence3Letter(IndigoObject library) {
dispatcher.setSessionID();
return Indigo.checkResultString(this, lib.indigoSequence3Letter(self, library.self));
}

public String fasta(IndigoObject library) {
dispatcher.setSessionID();
return Indigo.checkResultString(this, lib.indigoFasta(self, library.self));
Expand Down Expand Up @@ -112,6 +117,11 @@ public void saveSequenceToFile(String filename, IndigoObject library) {
Indigo.checkResult(this, lib.indigoSaveSequenceToFile(self, filename, library.self));
}

public void saveSequence3LetterToFile(String filename, IndigoObject library) {
dispatcher.setSessionID();
Indigo.checkResult(this, lib.indigoSaveSequence3LetterToFile(self, filename, library.self));
}

public void saveFastaToFile(String filename) {
dispatcher.setSessionID();
Indigo.checkResult(this, lib.indigoSaveFastaToFile(self, filename));
Expand Down
8 changes: 8 additions & 0 deletions api/python/indigo/indigo/indigo_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,14 @@ def __init__(self) -> None:
c_char_p,
c_int,
]
IndigoLib.lib.indigoSequence3Letter.restype = c_char_p
IndigoLib.lib.indigoSequence3Letter.argtypes = [c_int, c_int]
IndigoLib.lib.indigoSaveSequence3LetterToFile.restype = c_int
IndigoLib.lib.indigoSaveSequence3LetterToFile.argtypes = [
c_int,
c_char_p,
c_int,
]
IndigoLib.lib.indigoFasta.restype = c_char_p
IndigoLib.lib.indigoFasta.argtypes = [c_int, c_int]
IndigoLib.lib.indigoSaveFastaToFile.restype = c_int
Expand Down
27 changes: 27 additions & 0 deletions api/python/indigo/indigo/indigo_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -3434,6 +3434,33 @@ def sequence(self, library):
self._lib().indigoSequence(self.id, library.id)
)

def saveSequence3Letter(self, filename, library):
"""Saves macromolecule to monomers 3 letter sequence file
Args:
filename (str): full file path to the output file
Returns:
int: 1 if file is saved successfully
"""

return IndigoLib.checkResult(
self._lib().indigoSaveSequence3LetterToFile(
self.id, filename.encode(), library.id
)
)

def sequence3Letter(self, library):
"""Molecule or reaction method returns monomer 3 letter sequence for the structure
Returns:
str: sequence string
"""

return IndigoLib.checkResultString(
self._lib().indigoSequence3Letter(self.id, library.id)
)

def saveFasta(self, filename, library):
"""Saves macromolecule to FASTA file
Expand Down
2 changes: 2 additions & 0 deletions api/tests/integration/ref/formats/ket_to_seq3letter.py.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*** KET to 3 LETTER SEQUENCE ***
peptides_3letter : SUCCEED
5 changes: 5 additions & 0 deletions api/tests/integration/ref/formats/seq3letter_to_ket.py.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*** 3 LETTER SEQUENCE to KET ***
peptides_3letter : SUCCEED
Test 'ALA': got expected error 'Given string cannot be interpreted as a valid three letter sequence because of incorrect formatting.'
Test 'Al a': got expected error 'Given string cannot be interpreted as a valid three letter sequence because of incorrect formatting.'
Test 'ala': got expected error 'Given string cannot be interpreted as a valid three letter sequence because of incorrect formatting.'
45 changes: 45 additions & 0 deletions api/tests/integration/tests/formats/ket_to_seq3letter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import difflib
import os
import sys


def find_diff(a, b):
return "\n".join(difflib.unified_diff(a.splitlines(), b.splitlines()))


sys.path.append(
os.path.normpath(
os.path.join(os.path.abspath(__file__), "..", "..", "..", "common")
)
)
from env_indigo import Indigo, joinPathPy # noqa

indigo = Indigo()
indigo.setOption("ignore-stereochemistry-errors", True)

print("*** KET to 3 LETTER SEQUENCE ***")

refp = joinPathPy("ref/", __file__)

files = [
"peptides_3letter",
]

lib = indigo.loadMonomerLibraryFromFile(
os.path.join(refp, "monomer_library.ket")
)

files.sort()
for filename in files:
doc = indigo.loadKetDocumentFromFile(os.path.join(refp, filename + ".ket"))
with open(os.path.join(refp, filename) + ".seq3", "w") as file:
file.write(doc.sequence3Letter(lib))
with open(os.path.join(refp, filename) + ".seq3", "r") as file:
seq_ref = file.read()
seq = doc.sequence3Letter(lib)
diff = find_diff(seq_ref, seq)
if not diff:
print(filename + " : SUCCEED")
else:
print(filename + " : FAILED")
print(diff)
Loading

0 comments on commit 2596f46

Please sign in to comment.