Expose the property pickle options to SWIG (#7448)

* support pickle params in SWIG wrappers
csharp tests need to be tested

* make that work with csharp too
This commit is contained in:
Greg Landrum
2024-05-24 07:37:06 +02:00
committed by GitHub
parent b5041c3c51
commit 18eb648d56
3 changed files with 108 additions and 20 deletions

View File

@@ -167,19 +167,20 @@
}
}
}
public byte[] ToByteArray() {
UChar_Vect vec = null;
try {
vec = toUCharVect();
byte[] res = new byte[vec.Count];
vec.CopyTo(res);
return res;
} finally {
if (vec != null) {
vec.Dispose();
}
}
}
public
byte[] ToByteArray(int propertyFlags = -1) {
UChar_Vect vec = null;
try {
vec = toUCharVect(propertyFlags);
byte[] res = new byte[vec.Count];
vec.CopyTo(res);
return res;
} finally {
if (vec != null) {
vec.Dispose();
}
}
}
%}
%include <GraphMol/ROMol.h>
@@ -187,7 +188,14 @@
%include <GraphMol/Substruct/SubstructMatch.h>
%ignore RDKit::MolPickler;
#ifdef SWIGJAVA
%include "enums.swg"
%javaconst(1);
#endif
%include <GraphMol/MolPickler.h>
#ifdef SWIGJAVA
%javaconst(0);
#endif
@@ -250,6 +258,19 @@ void setAllowNontetrahedralChirality(bool);
%csmethodmodifiers RDKit::ROMol::toUCharVect "private";
#endif
%{
/* From MolPickler.h */
void setDefaultPickleProperties(unsigned int propertyFlags) {
RDKit::MolPickler::setDefaultPickleProperties(propertyFlags);
}
unsigned int getDefaultPickleProperties() {
return RDKit::MolPickler::getDefaultPickleProperties();
}
%}
void setDefaultPickleProperties(unsigned int propertyFlags);
unsigned int getDefaultPickleProperties();
%extend RDKit::ROMol {
std::string getProp(const std::string key){
std::string res;
@@ -583,10 +604,13 @@ void setAllowNontetrahedralChirality(bool);
return new std::vector<RDKit::Bond*>(bondNbrs.begin(), bondNbrs.end());
}
/* From MolPickler.h */
std::vector<int> ToBinary(){
std::vector<int> ToBinary(int propertyFlags=-1){
std::string sres;
RDKit::MolPickler::pickleMol(*($self),sres);
if(propertyFlags>=0) {
RDKit::MolPickler::pickleMol(*($self),sres,propertyFlags);
} else {
RDKit::MolPickler::pickleMol(*($self),sres);
}
std::vector<int> res(sres.length());
std::copy(sres.begin(),sres.end(),res.begin());
return res;
@@ -605,16 +629,24 @@ void setAllowNontetrahedralChirality(bool);
return RDKit::ROMOL_SPTR(res);
}
#ifdef SWIGJAVA
const std::string toByteArray() {
const std::string toByteArray(int propertyFlags=-1) {
std::string sres;
RDKit::MolPickler::pickleMol(*($self), sres);
if(propertyFlags>=0) {
RDKit::MolPickler::pickleMol(*($self),sres,propertyFlags);
} else {
RDKit::MolPickler::pickleMol(*($self),sres);
}
return sres;
}
#endif
#ifdef SWIGCSHARP
const std::vector<unsigned char> toUCharVect() {
const std::vector<unsigned char> toUCharVect(int propertyFlags=-1) {
std::string sres;
RDKit::MolPickler::pickleMol(*($self), sres);
if(propertyFlags>=0) {
RDKit::MolPickler::pickleMol(*($self),sres,propertyFlags);
} else {
RDKit::MolPickler::pickleMol(*($self),sres);
}
const std::vector<unsigned char> vec(sres.begin(), sres.end());
return vec;
}

View File

@@ -32,5 +32,32 @@ namespace RdkitTests
File.Delete(pklFileName);
}
}
[Fact]
public void TestPickleOptions()
{
string smi = "c1ccccc1[C@](F)(Cl)Br";
ROMol mol = RWMol.MolFromSmiles(smi);
mol.setProp("_MolFileChiralFlag", "1");
{
byte[] pkl = mol.ToByteArray();
ROMol mol2 = ROMol.FromByteArray(pkl);
Assert.False(mol2.hasProp("_MolFileChiralFlag"));
}
{
byte[] pkl = mol.ToByteArray((int)PropertyPickleOptions.AllProps);
ROMol mol2 = ROMol.FromByteArray(pkl);
Assert.True(mol2.hasProp("_MolFileChiralFlag"));
}
{
uint val = RDKFuncs.getDefaultPickleProperties();
RDKFuncs.setDefaultPickleProperties((int)PropertyPickleOptions.AllProps);
byte[] pkl = mol.ToByteArray();
RDKFuncs.setDefaultPickleProperties(val);
ROMol mol2 = ROMol.FromByteArray(pkl);
Assert.True(mol2.hasProp("_MolFileChiralFlag"));
}
}
}
}

View File

@@ -176,6 +176,35 @@ public class PicklingTests extends GraphMolTest {
}
}
@Test
public void testPickleProperties() {
ROMol mol = RWMol.MolFromSmiles("c1ccccc1[C@](F)(Cl)Br");
mol.setProp("foo", "bar");
mol.setProp("_MolFileChiralFlag", "1");
{
Int_Vect pkl = mol.ToBinary();
ROMol mol2 = ROMol.MolFromBinary(pkl);
assertFalse(mol2.hasProp("_MolFileChiralFlag"));
assertFalse(mol2.hasProp("foo"));
}
{
Int_Vect pkl = mol.ToBinary(PropertyPickleOptions.AllProps.swigValue());
ROMol mol2 = ROMol.MolFromBinary(pkl);
assertTrue(mol2.hasProp("_MolFileChiralFlag"));
assertTrue(mol2.hasProp("foo"));
}
{
long val = RDKFuncs.getDefaultPickleProperties();
RDKFuncs.setDefaultPickleProperties(PropertyPickleOptions.AllProps.swigValue());
Int_Vect pkl = mol.ToBinary();
RDKFuncs.setDefaultPickleProperties(val);
ROMol mol2 = ROMol.MolFromBinary(pkl);
assertTrue(mol2.hasProp("_MolFileChiralFlag"));
assertTrue(mol2.hasProp("foo"));
}
}
public static void main(String args[]) {
org.junit.runner.JUnitCore.main("org.RDKit.PicklingTests");
}