1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
/**********************************************************************
obrms = Returns the rms between two chemically identical structures
Derived from obfit.
*
This file is part of the Open Babel project.
For more information, see <http://openbabel.org/>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
***********************************************************************/
/*
Require a fixed molecule and a set of molecules to compare against.
example of command line:
obrms ref.sdf test.sdf
*/
// used to set import/export for Cygwin DLLs
#ifdef WIN32
#define USING_OBDLL
#endif
#include <openbabel/babelconfig.h>
#include <openbabel/mol.h>
#include <openbabel/parsmart.h>
#include <openbabel/obconversion.h>
#include <openbabel/query.h>
#include <openbabel/isomorphism.h>
#ifndef _MSC_VER
#include <unistd.h>
#endif
#include <cassert>
#include <sstream>
using namespace std;
using namespace OpenBabel;
class AtomDistanceSorter
{
vector3 ref;
public:
AtomDistanceSorter(OBAtom *r) :
ref(r->GetVector())
{
}
bool operator()(OBAtom *l, OBAtom *r) const
{
double ld = ref.distSq(l->GetVector());
double rd = ref.distSq(r->GetVector());
return ld < rd;
}
};
/* class to perform graph matching between two molecules.
* Is initialized with the reference molecule.
* Will figure out the atom correspondences and compute the rmsd between the
* ref mol and a provided test mol.
*/
class Matcher
{
const OBMol& ref;
OBQuery *query;
OBIsomorphismMapper *mapper;
class MapRMSDFunctor: public OBIsomorphismMapper::Functor
{
private:
const OBMol& ref;
const OBMol& test;
double bestRMSD;
public:
MapRMSDFunctor(const OBMol& r, const OBMol& t) :
ref(r), test(t), bestRMSD(HUGE_VAL)
{
}
bool operator()(OBIsomorphismMapper::Mapping &map)
{
double rmsd = 0;
for (unsigned i = 0, n = map.size(); i < n; i++)
{
//obmol indices are 1-indexed while the mapper is zero indexed
const OBAtom *ratom = ref.GetAtom(map[i].first + 1);
const OBAtom *tatom = test.GetAtom(map[i].second + 1);
assert(ratom && tatom);
vector3 rvec = ratom->GetVector();
vector3 tvec = tatom->GetVector();
rmsd += rvec.distSq(tvec);
}
rmsd /= map.size();
rmsd = sqrt(rmsd);
if (rmsd < bestRMSD)
bestRMSD = rmsd;
// check all possible mappings
return false;
}
double getRMSD() const
{
return bestRMSD;
}
};
public:
Matcher(OBMol& mol) :
ref(mol), query(NULL), mapper(NULL)
{
query = CompileMoleculeQuery(&mol);
mapper = OBIsomorphismMapper::GetInstance(query);
}
~Matcher()
{
if (query)
delete query;
if (mapper)
delete mapper;
}
//computes a correspondence between the ref mol and test (exhaustively)
//and returns the rmsd; returns infinity if unmatchable
double computeRMSD(OBMol& test)
{
MapRMSDFunctor funct(ref, test);
mapper->MapGeneric(funct, &test);
return funct.getRMSD();
}
};
//preprocess molecule into a standardized state for heavy atom rmsd computation
static void processMol(OBMol& mol)
{
//isomorphismmapper wants isomorphic atoms to have the same aromatic and ring state,
//but these proporties aren't reliable enough to be trusted in evaluating molecules
//should be considered the same based solely on connectivity
mol.DeleteHydrogens(); //heavy atom rmsd
for(OBAtomIterator aitr = mol.BeginAtoms(); aitr != mol.EndAtoms(); aitr++)
{
OBAtom *a = *aitr;
a->UnsetAromatic();
a->SetInRing();
}
for(OBBondIterator bitr = mol.BeginBonds(); bitr != mol.EndBonds(); bitr++)
{
OBBond *b = *bitr;
b->UnsetAromatic();
b->SetBondOrder(1);
b->SetInRing();
}
//avoid recomputations
mol.SetHybridizationPerceived();
mol.SetRingAtomsAndBondsPerceived();
mol.SetAromaticPerceived();
}
///////////////////////////////////////////////////////////////////////////////
//! \brief compute rms between chemically identical molecules
int main(int argc, char **argv)
{
bool firstOnly = false;
if (argc != 3 && argc != 4)
{
cerr << "Usage: " << argv[0]
<< " [-firstonly] <reference structure(s)> <comparison structure(s)>\n";
cerr << "Computes the heavy-atom RMSD of identical compound structures.\n";
cerr << "Structures in multi-structure files are compared one-by-one unless -firstonly\n"
<< "is passed, in which case only the first structure in the reference file is used.\n";
exit(-1);
}
char *fileRef = argv[1];
char *fileTest = argv[2];
if (argc == 4)
{
//if iterate is passed as first command, try to match structures in first file to strucutres in second
if (strcmp("-firstonly", argv[1]) != 0)
{
cerr << "Usage: " << argv[0]
<< " [-firstonly] <reference structure(s)> <comparison structure(s)>\n";
exit(-1);
}
fileRef = argv[2];
fileTest = argv[3];
firstOnly = true;
}
//open mols
OBConversion refconv;
OBFormat *refFormat = refconv.FormatFromExt(fileRef);
if (!refFormat || !refconv.SetInFormat(refFormat)
|| !refconv.SetOutFormat("SMI"))
{
cerr << "Cannot read reference molecule format!" << endl;
exit(-1);
}
OBConversion testconv;
OBFormat *testFormat = testconv.FormatFromExt(fileTest);
if (!testFormat || !testconv.SetInAndOutFormats(testFormat, testFormat))
{
cerr << "Cannot read reference molecule format!" << endl;
exit(-1);
}
//read reference
ifstream ifsref;
OBMol molref;
ifsref.open(fileRef);
if (!ifsref)
{
cerr << "Cannot read fixed molecule file: " << fileRef << endl;
exit(-1);
}
//check comparison file
ifstream ifstest;
ifstest.open(fileTest);
if (!ifstest)
{
cerr << "Cannot read file: " << fileTest << endl;
exit(-1);
}
while (refconv.Read(&molref, &ifsref))
{
processMol(molref);
Matcher matcher(molref);// create the matcher
OBMol moltest;
while (testconv.Read(&moltest, &ifstest))
{
if (moltest.Empty())
break;
processMol(moltest);
double rmsd = matcher.computeRMSD(moltest);
cout << "RMSD " << moltest.GetTitle() << " " << rmsd << "\n";
if (!firstOnly)
{
break;
}
}
}
return (0);
}
|