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 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
#include <BALL/DOCKING/COMMON/conformation.h>
#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 <map>
#include <set>
#include "version.h"
using namespace BALL;
using namespace std;
void sortMolecules(vector<String>& names, string& output_file, Size& best_k, string& e_property, double& score_cutoff, double& score_cuton)
{
multimap<double, Molecule*> compounds;
for (Size file = 0; file < names.size(); file++)
{
GenericMolFile* input = MolFileFactory::open(names[file]);
int mol_no = 0;
for (Molecule* mol = input->read(); mol; mol = input->read(), mol_no++)
{
if (!mol->hasProperty(e_property))
{
Log.level(10) << "Compound " << mol->getName() << " in file " << names[file] << " has no score property. Skipping this compound." << endl;
delete mol;
continue;
}
double score = ((String)mol->getProperty(e_property).toString()).toFloat();
if (score > score_cutoff || score < score_cuton)
{
delete mol;
continue;
}
if ((compounds.size() < best_k || score < compounds.rbegin()->first))
{
compounds.insert(make_pair(score, mol));
if (compounds.size() > best_k)
{
delete compounds.rbegin()->second;
multimap<double, Molecule*>::iterator it = compounds.end();
it--;
compounds.erase(it);
}
}
else
{
delete mol;
}
Log.level(20) << "\r" << names[file] << " : " << mol_no+1 << flush;
}
Log.level(20) << endl;
Log.flush();
input->close();
delete input;
}
if (compounds.size() < best_k)
{
Log.level(20) << "found " << compounds.size() << " compounds matching the given criteria." << endl;
}
GenericMolFile* output = MolFileFactory::open(output_file, ios::out, "mol2.gz");
for (multimap < double, Molecule* > ::iterator it = compounds.begin();
it!=compounds.end(); it++)
{
*output << *it->second;
delete it->second;
}
output->close();
delete output;
}
void mergeDRFiles(vector<String>& names, string& output_file, Size& best_k, string& e_property, double& score_cutoff, double& score_cuton)
{
DockResultFile* output = new DockResultFile(output_file, ios::out);
bool sort_by_scores = 1;
if (e_property == "") sort_by_scores = 0;
vector<Result*> new_results;
/// First of all, copy Result data
map<Result::Method, Result*> result_map;
for (Size file = 0; file < names.size(); file++)
{
DockResultFile* input = new DockResultFile(names[file]);
const vector<Result*>* results = input->getResults();
for (Size i = 0; i < results->size(); i++)
{
map<Result::Method, Result*>::iterator it = result_map.find((*results)[i]->getMethod());
if (it == result_map.end())
{
Result* result_copy = new Result(*(*results)[i]);
if (!sort_by_scores) output->addResult(result_copy);
else new_results.push_back(result_copy);
result_map.insert(make_pair(result_copy->getMethod(), result_copy));
}
else
{
*it->second += *(*results)[i];
}
}
input->close();
delete input;
}
if (e_property != "")
{
e_property = "score_"+new_results.back()->getMethodString();
}
/// If no sorting is desired, iterate over all input-files and write each input-molecules to output-file
if (!sort_by_scores)
{
output->disableAutomaticResultCreation();
for (Size file = 0; file < names.size(); file++)
{
GenericMolFile* input = MolFileFactory::open(names[file]);
int mol_no = 0;
for (Molecule* mol = input->read(); mol; mol = input->read(), mol_no++)
{
*output << *mol;
delete mol;
Log.level(20) << "\r" << names[file] << " : " << mol_no+1;
Log.flush();
}
Log.level(20)<<endl;
Log.flush();
input->close();
delete input;
}
}
/// If sorting is desired, iterate over all input-files and save each input-molecules to a map.
/// Then write all FlexibleMolecules in this map to the output file and adapt the Result objects.
else
{
multimap < double, FlexibleMolecule* > compounds; // map containing score and conformation-ID
set < String > IDs; // IDs of the base-conformations
for (Size file = 0; file < names.size(); file++)
{
DockResultFile* input = new DockResultFile(names[file]);
int mol_no = 0;
for (Molecule* mol = input->read(); mol; mol = input->read(), mol_no++)
{
if (!mol->hasProperty(e_property))
{
Log.level(10) << "Compound " << mol->getName() << " in file " << names[file] << " has no score property. Skipping this compound." << endl;
for (Size i = 0; i < new_results.size(); i++)
{
new_results[i]->erase(input->getCurrentLigand());
}
delete mol;
continue;
}
double score = ((String)mol->getProperty(e_property).toString()).toFloat();
if (score > score_cutoff || score < score_cuton)
{
for (Size i = 0; i < new_results.size(); i++)
{
new_results[i]->erase(input->getCurrentLigand());
}
delete mol;
continue;
}
if ((compounds.size() < best_k || score < compounds.rbegin()->first))
{
FlexibleMolecule* flexmol_copy = new FlexibleMolecule(*input->getCurrentLigand());
compounds.insert(make_pair(score, flexmol_copy));
IDs.insert(flexmol_copy->getId());
if (compounds.size() > best_k)
{
for (Size i = 0; i < new_results.size(); i++)
{
new_results[i]->erase(compounds.rbegin()->second);
}
IDs.erase(compounds.rbegin()->second->getId());
delete compounds.rbegin()->second;
multimap<double, FlexibleMolecule*>::iterator it = compounds.end();
it--;
compounds.erase(it);
}
}
else
{
for (Size i = 0; i < new_results.size(); i++)
{
new_results[i]->erase(input->getCurrentLigand());
}
delete mol;
}
Log.level(20) <<"\r"<<names[file]<<" : "<<mol_no+1<<flush;
}
Log.level(20)<<endl;
input->close();
delete input;
}
if (compounds.size() < best_k)
{
Log.level(20)<<"found "<<compounds.size()<<" compounds matching the given criteria."<<endl;
}
list<String> score_list;
for (multimap < double, FlexibleMolecule* > ::iterator it = compounds.begin();
it!=compounds.end(); it++)
{
output->writeLigand(it->second);
score_list.push_back(it->second->getId());
delete it->second;
}
// Remove those ligands from results for which no final result is available (e.g. due to missing atom parameters)
vector < String > import_IDs = *new_results[0]->getInputConformations();
for (Size i = 0; i < import_IDs.size(); i++)
{
if (IDs.find(import_IDs[i]) == IDs.end())
{
vector<vector<Result::ResultData> > data_list;
for (Size k = 0; k < new_results.size(); k++)
{
if (k == 0)
{
data_list.push_back(*new_results[k]->get(import_IDs[i]));
}
vector<vector<Result::ResultData> > new_data_list;
for (Size j = 0; j < data_list.size(); j++)
{
for (Size l = 0; l < data_list[j].size(); l++)
{
String ID = data_list[j][l].getLigandConformationId();
new_data_list.push_back(*new_results[k]->get(ID));
new_results[k]->erase(ID);
}
}
data_list = new_data_list;
}
}
}
for (Size i = 0; i < new_results.size(); i++)
{
list<String> new_list;
new_results[i]->sort(score_list, new_list);
score_list = new_list;
}
output->writeResults(new_results);
}
output->close();
delete output;
}
int main(int argc, char* argv[])
{
CommandlineParser parpars("DockResultMerger", "merge docking output files", VERSION, String(__DATE__), "Convert, combine and store");
parpars.registerMandatoryInputFile("i", "input files");
parpars.registerMandatoryOutputFile("o", "output file");
parpars.registerOptionalStringParameter("score", "score property name", "score");
parpars.registerOptionalDoubleParameter("min", "minimal score value", -30.0);
parpars.registerOptionalDoubleParameter("max", "maximal score value", 0.0);
parpars.registerOptionalIntegerParameter("k", "number of output molecules", 20);
parpars.registerFlag("rm", "remove input files after merging");
parpars.setSupportedFormats("i","mol2,sdf,drf");
parpars.setSupportedFormats("o","mol2,sdf,drf");
String manual = "This tool merges and sorts molecule files as generated by docking or rescoring.\n\nYou need to specify the property-tag name of the scores according to which the molecules should be sorted. Optionally you can filter those compounds that were assigned a score above and/or below specified thresholds. If desired, you can furthermore choose to have only the compounds with the k best scores written to the output file.\n\n Output of DockResultMerger is one molecule containing the molecules found in input-files (that matched all filter criteria, if any), sorted ascendingly according to their scores.";
parpars.setToolManual(manual);
parpars.parse(argc, argv);
string e_property="score";
double energy_cutoff = 1e100;
double energy_cuton = -1e100;
Size best_k = 100000000;
String output = parpars.get("o");
String s = parpars.get("score");
if (s != CommandlineParser::NOT_FOUND) e_property = s;
s = parpars.get("min");
if (s != CommandlineParser::NOT_FOUND) energy_cuton = s.toDouble();
s = parpars.get("max");
if (s != CommandlineParser::NOT_FOUND) energy_cutoff = s.toDouble();
s = parpars.get("k");
if (s != CommandlineParser::NOT_FOUND) best_k = s.toInt();
const list<String>& n = parpars.getList("i");
vector<String> names;
for (list<String>::const_iterator it = n.begin(); it != n.end(); it++)
{
names.push_back(*it);
}
bool drf_merge = 0;
if (output.hasSuffix(".drf"))
{
for (Size i = 0; i < names.size(); i++)
{
if (names[i].hasSuffix(".drf")) drf_merge = 1;
else if (drf_merge)
{
Log.error()<<"[Error:] Using drf and non-drf files together as input is not supported."<<endl;
return 1;
}
}
}
if (drf_merge)
{
Log.error()<<"[Error:] Using DockingFiles (*.drf) is not possible since this version of DockResultMerger has been compiled without QtXML support."<<endl;
return 1;
}
if (!drf_merge)
{
sortMolecules(names, output, best_k, e_property, energy_cutoff, energy_cuton);
}
else
{
mergeDRFiles(names, output, best_k, e_property, energy_cutoff, energy_cuton);
}
if (parpars.has("rm"))
{
for (Size i = 0; i < names.size(); i++)
{
File::remove(names[i]);
}
}
return 0;
}
|