Skip to content

Commit

Permalink
Merge pull request #2608 from dkoes/obrmsfix
Browse files Browse the repository at this point in the history
obrms improvements
  • Loading branch information
ghutchis committed Oct 20, 2023
2 parents 7d6fd4c + 60461a5 commit f9efcc9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
19 changes: 19 additions & 0 deletions test/testbabel.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

from subprocess import CalledProcessError, Popen, PIPE, check_output, STDOUT

INF = float("inf")

def run_exec(*args):
"""Run one of OpenBabel's executables
Expand Down Expand Up @@ -521,5 +523,22 @@ def testXYZph(self):
# mol2 displays element twice
self.assertEqual(output.count('H'), 12)

def testOBRMS(self):
'''Sanity checks for obrms'''
sdffile = self.getTestFile('testsym_2Dtests.sdf')
output, err = run_exec( "obrms -t 10 %s %s"%(sdffile,sdffile))
# all rmsds should be zero
rmsds = [float(line.split()[-1]) for line in output.split('\n') if line]
for rmsd in rmsds:
self.assertEqual(rmsd, 0, "RMSD not zero between identical structures")
output, err = run_exec( "obrms -t 10 -f %s %s" % (sdffile,sdffile))
#first zero, second nonzero, last inf
rmsds = [float(line.split()[-1]) for line in output.split('\n') if line]
self.assertEqual(rmsds[0],0)
self.assertEqual(rmsds[1],2.73807)
self.assertEqual(rmsds[-1],INF)



if __name__ == "__main__":
unittest.main()
20 changes: 15 additions & 5 deletions tools/obrms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,13 @@ class Matcher
};

public:
Matcher(OBMol& mol) : ref(&mol)
Matcher(OBMol& mol, int timeout) : ref(&mol)
{
query = std::shared_ptr<OBQuery>(CompileMoleculeQuery(&mol));
mapper = std::shared_ptr<OBIsomorphismMapper>(OBIsomorphismMapper::GetInstance(query.get()));
if(timeout > 0) {
mapper->SetTimeout(timeout);
}
}


Expand Down Expand Up @@ -221,6 +224,7 @@ int main(int argc, char **argv)
string fileRef;
string fileTest;
string fileOut;
int timeout = 0;

const char *helpmsg =
"obrms: Computes the heavy-atom RMSD of identical compound structures.\n"
Expand All @@ -231,19 +235,21 @@ int main(int argc, char **argv)
"\t -m, --minimize compute minimum RMSD\n"
"\t -x, --cross compute all n^2 RMSDs between molecules of reference file\n"
"\t -s, --separate separate reference file into constituent molecules and report best RMSD\n"
"\t -t, --timeout give up on matching after specified number of seconds\n"
"\t -h, --help help message\n";
struct option long_options[] = {
{"firstonly", no_argument, nullptr, 'f'},
{"minimize", no_argument, nullptr, 'm'},
{"cross", no_argument, nullptr, 'x'},
{"separate", no_argument, nullptr, 's'},
{"timeout", required_argument, nullptr, 't'},
{"out", required_argument, nullptr, 'o'},
{"help", no_argument, nullptr, 'h'},
{nullptr, 0, nullptr, 0}
};
int option_index = 0;
int c = 0;
while ((c = getopt_long(argc, argv, "hfmxso:", long_options, &option_index) ) != -1) {
while ((c = getopt_long(argc, argv, "hfmxst:o:", long_options, &option_index) ) != -1) {
switch(c) {
case 'o':
fileOut = optarg;
Expand All @@ -260,6 +266,9 @@ int main(int argc, char **argv)
case 's':
separate = true;
break;
case 't':
timeout = atoi(optarg);
break;
case 'h':
cout << helpmsg;
exit(0);
Expand Down Expand Up @@ -325,7 +334,7 @@ int main(int argc, char **argv)

for(unsigned i = 0, n = refmols.size() ; i < n; i++) {
OBMol& ref = refmols[i];
Matcher matcher(ref);
Matcher matcher(ref, timeout);
cout << ref.GetTitle();
for(unsigned j = 0; j < n; j++) {
OBMol& moltest = refmols[j];
Expand All @@ -337,6 +346,8 @@ int main(int argc, char **argv)

} else {

OBConversion testconv(fileTest);

//check comparison file
while (refconv.Read(&molref))
{
Expand All @@ -350,11 +361,10 @@ int main(int argc, char **argv)
vector<Matcher> matchers;
for(unsigned i = 0, n = refmols.size(); i < n; i++) {
processMol(refmols[i]);
Matcher matcher(refmols[i]); // create the matcher
Matcher matcher(refmols[i], timeout); // create the matcher
matchers.push_back(matcher);
}

OBConversion testconv(fileTest);
OBMol moltest;
while (testconv.Read(&moltest))
{
Expand Down

0 comments on commit f9efcc9

Please sign in to comment.