Skip to content

Commit

Permalink
cdx export #941 (#1012)
Browse files Browse the repository at this point in the history
* rgroups (#946)

Co-authored-by: Roman Porozhnetov <roman_porozhnetov@epam.com>

* CDX import: Reaction arrows disappear when opening a file #943 (#948)

* marvin cdx fix

* clang fix

Co-authored-by: Roman Porozhnetov <roman_porozhnetov@epam.com>

* bond order fix

* clang fix

* test fix

* cdx export

* clang fixes

* cdx python binding

* clang fixes

* api fixes

* cdx export

* indigo py fix

* cdx

* cdx

* cdx fixes

* clang fix

* PR comments

* clang fix

* pr fix

* pr fix

* b64cdx java&dotnet fix

* b64cdx java&dotnet fix

* b64cdx java&dotnet fix

* diff fix

* test fix

* clang fix

* mol_to_cdx.out deleted

* clang fix

* tests fix

* tests fix

* tests fix

* tests fix

* tests fix

* tests fix

* tests fix

* tests fix

* fix dir

* fix dir

* fix dir

---------

Co-authored-by: Roman Porozhnetov <roman_porozhnetov@epam.com>
  • Loading branch information
even1024 and even1024 authored Jan 30, 2023
1 parent 9760b99 commit a7d0a74
Show file tree
Hide file tree
Showing 105 changed files with 58,392 additions and 1,148 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/indigo-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ jobs:
lfs: false
fetch-depth: 500
- name: Git fetch tags
run: git fetch --tags -f
run: |
git config --global --add safe.directory /__w/Indigo/Indigo
git fetch --tags -f
- name: Check
run: |
python3 -m pip install -r api/python/requirements_dev.txt
Expand Down
2 changes: 2 additions & 0 deletions api/c/indigo/indigo.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ CEXPORT int indigoSaveJson(int item, int output);
CEXPORT int indigoSaveCml(int object, int output);
CEXPORT int indigoSaveCmlToFile(int object, const char* filename);
CEXPORT const char* indigoCml(int object);
CEXPORT const char* indigoCdxBase64(int object);

// accepts molecules and reactions
CEXPORT int indigoSaveCdxml(int object, int output);
Expand Down Expand Up @@ -1021,6 +1022,7 @@ CEXPORT int indigoExpandAbbreviations(int molecule);
/* Other */

CEXPORT const char* indigoToString(int handle);
CEXPORT const char* indigoToBase64String(int handle);
CEXPORT int indigoToBuffer(int handle, char** buf, int* size);

/* Reaction products enumeration */
Expand Down
2 changes: 2 additions & 0 deletions api/c/indigo/src/indigo_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ class DLLEXPORT IndigoObject
virtual MonomersProperties& getMonomersProperties();
virtual void copyProperties(PropertiesMap&);

void toBase64String(Array<char>& str);

private:
IndigoObject(const IndigoObject&);
};
Expand Down
14 changes: 14 additions & 0 deletions api/c/indigo/src/indigo_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,20 @@ CEXPORT const char* indigoToString(int handle)
INDIGO_END(0);
}

CEXPORT const char* indigoToBase64String(int handle)
{
INDIGO_BEGIN
{
IndigoObject& obj = self.getObject(handle);

auto& tmp = self.getThreadTmpData();
obj.toBase64String(tmp.string);
tmp.string.push(0);
return tmp.string.ptr();
}
INDIGO_END(0);
}

CEXPORT int indigoToBuffer(int handle, char** buf, int* size)
{
INDIGO_BEGIN
Expand Down
16 changes: 16 additions & 0 deletions api/c/indigo/src/indigo_macros.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,22 @@ CEXPORT const char* indigoMolfile(int molecule)
return res;
}

CEXPORT const char* indigoCdxBase64(int object)
{
int b = indigoWriteBuffer();
const char* res;

if (b == -1)
return 0;

if (indigoSaveCdx(object, b) == -1)
return 0;

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

CEXPORT const char* indigoCml(int molecule)
{
int b = indigoWriteBuffer();
Expand Down
16 changes: 12 additions & 4 deletions api/c/indigo/src/indigo_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
* limitations under the License.
***************************************************************************/

#include "indigo_internal.h"

#include <map>

#include "base_cpp/output.h"
#include "base_cpp/properties_map.h"
#include "indigo_internal.h"
#include "reaction/reaction.h"

#include <../cppcodec/cppcodec/base64_default_rfc4648.hpp>
#include <map>

using IndigoObjectTypesMap = std::map<int, const char* const>;
class IndigoObjectTypes : public IndigoObjectTypesMap, public NonCopyable
{
Expand Down Expand Up @@ -161,6 +161,14 @@ const char* IndigoObject::debugInfo() const
return getTypeName();
}

void IndigoObject::toBase64String(Array<char>& str)
{
Array<char> temp;
toString(temp);
auto encoded = base64::encode(temp.ptr(), temp.size());
str.copy(encoded.c_str(), encoded.size());
}

void IndigoObject::toString(Array<char>& str)
{
throw IndigoError("can not convert %s to string", debugInfo());
Expand Down
3 changes: 3 additions & 0 deletions api/dotnet/src/IndigoLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ public unsafe class IndigoLib
[DllImport("indigo"), SuppressUnmanagedCodeSecurity]
public static extern byte* indigoCdxml(int molecule);

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

[DllImport("indigo"), SuppressUnmanagedCodeSecurity]
public static extern int indigoSaveMDLCT(int item, int output);

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

public string b64cdx()
{
dispatcher.setSessionID();
return dispatcher.checkResult(IndigoLib.indigoCdxBase64(self));
}

public void saveCdxml(string filename)
{
dispatcher.setSessionID();
Expand Down
2 changes: 2 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 @@ -140,6 +140,8 @@ public interface IndigoLib extends Library {

Pointer indigoCdxml(int object);

Pointer indigoCdxBase64(int object);

int indigoSaveMDLCT(int item, int output);

int indigoLoadReaction(int source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ public String cdxml() {
return Indigo.checkResultString(this, lib.indigoCdxml(self));
}

public String b64cdx() {
dispatcher.setSessionID();
return Indigo.checkResultString(this, lib.indigoCdxBase64(self));
}

public void saveCdxml(String filename) {
dispatcher.setSessionID();
Indigo.checkResult(this, lib.indigoSaveCdxmlToFile(self, filename));
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 @@ -254,8 +254,12 @@ def __init__(self) -> None:
IndigoLib.lib.indigoCml.argtypes = [c_int]
IndigoLib.lib.indigoSaveCdxmlToFile.restype = c_int
IndigoLib.lib.indigoSaveCdxmlToFile.argtypes = [c_int, c_char_p]
IndigoLib.lib.indigoSaveCdxToFile.restype = c_int
IndigoLib.lib.indigoSaveCdxToFile.argtypes = [c_int, c_char_p]
IndigoLib.lib.indigoCdxml.restype = c_char_p
IndigoLib.lib.indigoCdxml.argtypes = [c_int]
IndigoLib.lib.indigoCdxBase64.restype = c_char_p
IndigoLib.lib.indigoCdxBase64.argtypes = [c_int]
IndigoLib.lib.indigoJson.restype = c_char_p
IndigoLib.lib.indigoJson.argtypes = [c_int]
IndigoLib.lib.indigoSaveMDLCT.restype = c_int
Expand Down
25 changes: 25 additions & 0 deletions api/python/indigo/indigo/indigo_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,20 @@ def saveCdxml(self, filename):
self._lib().indigoSaveCdxmlToFile(self.id, filename.encode())
)

def saveCdx(self, filename):
"""Molecule method saves the structure into a CDX file
Args:
filename (str): full path to the output file
Returns:
int: 1 if the file is saved successfully
"""

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

def cdxml(self):
"""Molecule method returns the structure as a string in CDXML format
Expand All @@ -305,6 +319,17 @@ def cdxml(self):

return IndigoLib.checkResultString(self._lib().indigoCdxml(self.id))

def b64cdx(self):
"""Molecule method returns the structure as a string in CDX base64 encoded format
Returns:
str: base64 encoded CDX string
"""

return IndigoLib.checkResultString(
self._lib().indigoCdxBase64(self.id)
)

def json(self):
"""Structure method returns the structure as a string in KET format
Expand Down
5 changes: 5 additions & 0 deletions api/tests/integration/ref/formats/cdx_export.py.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*** Mol to CDX ***
enhanced_stereo1:success
enhanced_stereo2:success
enhanced_stereo3:success
stereo_either-0020:success
54 changes: 54 additions & 0 deletions api/tests/integration/tests/formats/cdx_export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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 * # noqa

indigo = Indigo()

print("*** Mol to CDX ***")

root = joinPathPy("molecules/", __file__)
files = [
"stereo_either-0020",
"enhanced_stereo1",
"enhanced_stereo2",
"enhanced_stereo3",
]

ref_path = joinPathPy("ref/", __file__)
files.sort()

for filename in files:
try:
mol = indigo.loadMoleculeFromFile(
os.path.join(root, filename + ".mol")
)
resb64 = mol.b64cdx()
# with open(os.path.join(ref_path, filename + ".cdxml"), 'w') as file:
# data = file.write(mol.cdxml())
with open(os.path.join(ref_path, filename + ".b64cdx"), "r") as file:
refb64 = file.read()
print(filename + (":success" if refb64 == resb64 else ":failed"))
except IndigoException as e:
print(getIndigoExceptionText(e))
print("*** Try as Query ***")
mol = indigo.loadQueryMoleculeFromFile(
os.path.join(root, filename + ".mol")
)
resb64 = mol.b64cdx()
# with open(os.path.join(ref_path, filename + ".cdxml"), 'w') as file:
# data = file.write(mol.cdxml())
with open(os.path.join(ref_path, filename + ".b64cdx"), "r") as file:
refb64 = file.read()
print(filename + (":success" if refb64 == resb64 else ":failed"))
Loading

0 comments on commit a7d0a74

Please sign in to comment.