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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
#include <BALL/FORMAT/molFileFactory.h>
#include <BALL/FORMAT/genericMolFile.h>
#include <BALL/FORMAT/dockResultFile.h>
#include <BALL/FORMAT/commandlineParser.h>
#include <BALL/KERNEL/molecule.h>
#include <BALL/KERNEL/bond.h>
#include <BALL/KERNEL/PTE.h>
#include <BALL/STRUCTURE/UCK.h>
#include <set>
#include <map>
#include "version.h"
using namespace BALL;
using namespace std;
bool isUnique(const Molecule* mol, map<String, pair<int, String> >& mol_hashs, bool unique_topologies)
{
String hash;
if (!unique_topologies)
{
Conformation::generateHash(mol, hash);
}
else
{
UCK uck(*mol, false);
hash = uck.getUCK();
}
map<String, pair<int, String> >::iterator it = mol_hashs.find(hash);
if (it == mol_hashs.end())
{
mol_hashs.insert(make_pair(hash, make_pair(1, mol->getName())));
return true;
}
else
{
it->second.first++;
return false;
}
}
// returns the number of non-unique conformations
int printNonUniqueConformations(map < String, pair < int, String > > & mol_hashs)
{
int no = 0;
for (map < String, pair < int, String > > ::iterator it = mol_hashs.begin(); it != mol_hashs.end(); it++)
{
if (it->second.first > 1)
{
no++;
Log.level(10) <<" Conformation "<<it->first(0, 10);
if (it->second.second != "")
{
Log.level(10)<<", "<<it->second.second;
}
Log.level(10)<<" occurred "<<it->second.first<<" times."<<endl;
}
}
return no;
}
/* check whether the given Molecule really contains only one molecule */
void checkBonds(const Atom* atom, set<const Atom*>& visited_atoms)
{
if (visited_atoms.find(atom) != visited_atoms.end())
{
return;
}
visited_atoms.insert(atom);
for (Atom::BondConstIterator b_it = atom->beginBond(); +b_it; b_it++)
{
checkBonds(b_it->getPartner(*atom), visited_atoms);
}
}
// Return true if molecule passed all tests, else return false.
bool checkMolecule(Molecule* mol, int molecule_no, map<String, pair<int, String> >& mol_hashs, bool unique_topologies, bool no_unique_conf_check)
{
bool bond_length_ok = 1;
bool elements_ok = 1;
bool one_molecule = 1;
bool has_atoms = 1;
bool has_3D_coordinates = 1;
bool has_hydrogens = 0;
bool partial_charges_ok = 1;
Size no_atoms = 0;
bool all_x_zero = 1; // are all x-coordinates in the file set to zero?
bool all_y_zero = 1;
bool all_z_zero = 1;
if (!no_unique_conf_check || unique_topologies) // check uniqueness only if desired
{
if (!isUnique(mol, mol_hashs, unique_topologies)) return false;
}
for (AtomConstIterator it = mol->beginAtom(); +it; it++, no_atoms++)
{
if (bond_length_ok) // skip if such errors have already been detected for this molecule
{
for (Atom::BondConstIterator b_it = it->beginBond(); +b_it; b_it++)
{
double length = b_it->getLength();
if (length < 0.7 || length > 2.5)
{
bond_length_ok = false;
}
}
}
if (elements_ok)
{
if (it->getElement().getName() == BALL_ELEMENT_NAME_DEFAULT)
{
elements_ok = false;
}
}
if (!has_hydrogens)
{
if (it->getElement().getSymbol() == "H")
{
has_hydrogens = 1;
}
}
const TVector3<float>& pos = it->getPosition();
if (all_x_zero)
{
if (fabs(pos[0]) > 0.001) all_x_zero = 0;
}
if (all_y_zero)
{
if (fabs(pos[1]) > 0.001) all_y_zero = 0;
}
if (all_z_zero)
{
if (fabs(pos[2]) > 0.001) all_z_zero = 0;
}
if (fabs(it->getCharge()) > 5)
{
partial_charges_ok = 0;
}
}
if (no_atoms == 0)
{
has_atoms = false;
Log.level(10)<<" "<<"Error for molecule "<<molecule_no<<":"<<" molecule has no atoms!"<<endl;
}
else
{
set<const Atom*> visited_atoms;
checkBonds(&*mol->beginAtom(), visited_atoms);
if (visited_atoms.size() != no_atoms)
{
one_molecule = false;
Log.level(10)<<" "<<"Error for molecule "<<molecule_no<<":"<<" more than one molecule or bonds are missing !"<<endl;
}
}
if (all_x_zero || all_y_zero || all_z_zero)
{
has_3D_coordinates = false;
Log.level(10)<<" "<<"Error for molecule "<<molecule_no<<":"<<" no 3D coordinates!"<<endl;
}
if (!elements_ok)
{
Log.level(10)<<" "<<"Error for molecule "<<molecule_no<<":"<<" atoms without element detected!"<<endl;
}
if (!bond_length_ok)
{
Log.level(10)<<" "<<"Error for molecule "<<molecule_no<<":"<<" senseless bond-length detected!"<<endl;
}
if (!has_hydrogens)
{
Log.level(10)<<" "<<"Error for molecule "<<molecule_no<<":"<<" no hydrogens in molecule! We need protonated molecules as input for docking!"<<endl;
}
if (!partial_charges_ok)
{
Log.level(10)<<" "<<"Error for molecule "<<molecule_no<<":"<<" atoms with nonsense partial charges detected!"<<endl;
}
return has_atoms && has_hydrogens && has_3D_coordinates && one_molecule && bond_length_ok && elements_ok && partial_charges_ok;
}
int main(int argc, char* argv[])
{
CommandlineParser par("LigCheck", "check molecules for errors", VERSION, String(__DATE__), "Checks and evaluations");
par.registerMandatoryInputFile("i", "input molecule file");
par.registerMandatoryOutputFile("o", "output file");
par.registerOptionalDoubleParameter("ef", "error fraction; print error if fraction of invalid mols is larger", 0.5);
par.registerFlag("ri", "remove invalid molecules.", true);
par.registerFlag("ut", "check for unique topologies");
par.registerFlag("nc", "no not check for unique conformations");
par.registerFlag("rm", "remove input file when finished");
String man = "This tool checks all molecules of the given input file for errors. Supported formats are mol2, sdf or drf (DockResultFile, xml-based).\n\nThe following checks are done for each molecule:\n\n\
* bond-lengths may not be completely senseless (i.e. <0.7 or >2.5 Angstroem)\n\
* each 'molecule' in the input file may only contain one actual molecule, i.e. there may be no unconnected atoms or fragments.\n\
* each atom must have a valid assigned element\n\
* the molecule must be protonated (since this is necessary for docking/(re-)scoring).\n\
* 3D coordinates must be present (instead of 2D coordinates; also necessary for docking/(re-)scoring)\n\
* partial charges may not contain completely senseless values (>5 or <-5).\n\
* each conformation should appear only once within the given file, otherwise it is rejected and not written to the output file. However, if option '-ut' is used, molecules will instead be checked for unique topologies.\n\nIf option '-ri' is used, only those molecules that pass all those tests are written to the output file. If this option is not used, all molecules are written to output containing a property 'score_ligcheck' with a value of 1 if the molecule passed all tests or with a value of 0 if it did not pass them.";
par.setToolManual(man);
par.setSupportedFormats("i",MolFileFactory::getSupportedFormats());
par.setSupportedFormats("o",MolFileFactory::getSupportedFormats());
par.parse(argc, argv);
GenericMolFile* input = MolFileFactory::open(par.get("i"));
GenericMolFile* output = 0;
if (argc > 2) output = MolFileFactory::open(par.get("o"), ios::out, input);
int no_errors = 0;
bool remove_invalid = 0;
bool unique_topologies = 0;
bool no_unique_conf_check = 0;
if (par.has("ri")) remove_invalid = 1;
if (par.has("ut")) unique_topologies = 1;
if (par.has("nc")) no_unique_conf_check = 1;
DockResultFile* drf_output = dynamic_cast<DockResultFile*>(output);
if (drf_output)
{
String dummy = "0";
Result::Method method = Result::getMethod(6);
drf_output->setOutputParameters(method, "score_ligcheck", dummy);
}
// map the hash-string of each molecule that has been read already to the number of its occurences and its name
map<String, pair<int, String> > mol_hashs;
int molecule_no = 1;
for (Molecule* mol = 0; molecule_no == 1 || mol; molecule_no++)
{
try
{
mol = input->read();
}
catch(BALL::Exception::GeneralException e)
{
no_errors++;
}
if (!mol) break;
bool ok = checkMolecule(mol, molecule_no, mol_hashs, unique_topologies, no_unique_conf_check);
if (ok)
{
if (output)
{
if (!remove_invalid) mol->setProperty("score_ligcheck", 1); // for Result-section entry
*output << *mol;
}
}
else if (output && !remove_invalid)
{
mol->setProperty("score_ligcheck", 0); // for Result-section entry
*output << *mol;
no_errors++;
}
else no_errors++;
delete mol;
}
molecule_no--;
int no_non_unique = printNonUniqueConformations(mol_hashs);
int status = 0;
no_errors -= no_non_unique;
String ef = par.get("ef");
double error_fraction = 1;
if (ef != CommandlineParser::NOT_FOUND)
{
error_fraction = ef.toDouble();
}
if (no_non_unique > 0)
{
status = 1;
Log.level(20)<<endl<<no_non_unique<<" molecules occurred multiple times within the input file.";
if (output && remove_invalid)
{
Log.level(20)<<" Each of those molecules was only written once to the output file !";
}
Log.level(20)<<endl;
}
if (no_errors > 0)
{
if (no_errors == molecule_no)
{
status = 1;
Log.error()<<endl<<"All molecules are invalid!!";
if (output && remove_invalid)
{
Log.error()<<" Thus no molecules have been written to output!";
}
Log.error()<<endl;
}
else if (no_errors > molecule_no*error_fraction)
{
status = 1;
Log.error()<<endl<<no_errors<<" molecules contained errors";
if (output && remove_invalid)
{
Log.error()<<" and were not written to output file!";
}
Log.error()<<endl;
}
else
{
status = 0;
Log.level(20)<<endl<<no_errors<<" molecules contained errors";
if (output && remove_invalid)
{
Log.level(20)<<" and were not written to output file!";
}
Log.level(20)<<endl;
}
}
if (no_non_unique == 0 && no_errors == 0)
{
Log.level(20)<<endl<<"No errors detected.";
if (output && !drf_output) Log.level(20)<<"All molecules have been written to output file";
Log.level(20)<<endl;
}
delete input;
delete output;
if (par.has("rm"))
{
File::remove(par.get("i"));
}
return status;
}
|