-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathALFA.cpp
More file actions
2546 lines (2236 loc) · 95.8 KB
/
Copy pathALFA.cpp
File metadata and controls
2546 lines (2236 loc) · 95.8 KB
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* ALFA.cpp
*
* 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.
*
*/
/*
*
* v5.0 - June/2016.
* Improved library of torsionals (TorLib2 - Guba et al. 2016. JCIM
* Library generator mode added for multi-MOL2 inputs
* Bug fixing (nasty segfaults, etc.)
*
* v4.3 - Published version 2014. Centro de Biologia Molecular Severo Ochoa.
* Improved Empirical rules.
*
* v4.0 - Initial OpenBabel port
*/
#include <fstream>
#include <iomanip>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <openbabel/mol.h>
#include <openbabel/ring.h>
#include <openbabel/obconversion.h>
#include <openbabel/typer.h>
#include <openbabel/rand.h>
#include <openbabel/forcefield.h>
#include <openbabel/atom.h>
#include <openbabel/parsmart.h>
#include <openbabel/obiter.h>
#include <openbabel/conformersearch.h>
#include <openbabel/math/align.h>
#include <openbabel/canon.h>
#include "Constants.h"
#include "Rules.h"
#include "RotatableBond.h"
#include "TorsionRule.h"
#include "USR_lib.h"
using namespace OpenBabel;
using namespace std;
struct conformer_struct {
unsigned long long conf;
unsigned long long id;
//vector<unsigned int> id;
double coord[MAX_ATOM];
double energy;
double rmsd;
double moments[NUM_MOMENTS];
double meanDist;
};typedef struct conformer_struct tConformer;
//! Functions headers
void getParameters(int piArgc, char **piArgv);
void help(void);
unsigned long long string2ulonglong(string piInput);
vector < OBMol > readInputMolecule(string piInputFile, vector<unsigned int>& poFileID2molID, vector<unsigned int>& poMolID2FileID);
void getFileVsMolRelationIDs(OBMol& piMol, vector<unsigned int>& poFileID2molID, vector<unsigned int>& poMolID2fileID);
OBMol readMolecule(string piInputFile);
void loadTorsionRules(vector<TorsionRule> *torsionRulesList);
string simplifyWhitesAndTabs(char * inStr);
TorsionRule readTorsionRule(string piLine);
string simplifyWhitesAndTabs(char * inStr);
vector<RotatableBond> getRotatableBonds(OBMol& piMol, vector<TorsionRule> piTorsionRules);
void addOriginalAngles(OBMol& piMol, vector<RotatableBond>& piRotatableBonds);
// void cleanEquivalentAngles(OBMol& piMol, vector<RotatableBond>& piRotatableBonds);
void cleanEquivalentAngles(OBMol& piMol, vector<RotatableBond>& piRotatableBonds);
vector<unsigned int> getAmberTypes(OBMol& piMol);
vector< vector<unsigned int> > getMinimalDistances(OBMol& piMol);
vector< vector<unsigned int> > getTestAtoms4vdw(OBMol& piMol, const vector< vector<unsigned int> >& piMinimalDistances, vector<RotatableBond> rotBonds);
vector<unsigned long long> getSizeByIndexPosition(vector<RotatableBond>& piRotatableBonds);
list<tConformer> generateConformers(OBMol& piMol, vector<RotatableBond>& piRotatableBonds, const vector< vector<unsigned int> >& piMinimalDistances, const vector< vector<unsigned int> >& piTestAtoms4vdw, const vector<unsigned int>& piAmberTypes);
unsigned int stericClashFilter(const vector<unsigned int>& piAtomIdxs, const double* piCoordinates, const vector<unsigned int>& piAtomAmberTypes, const vector< vector<unsigned int> >& piTestAtoms4vdw, vector< vector<float> >& poDistances);
double getVDWEnergy(const vector<unsigned int>& piAtomIdxs, const double* piCoordinates, const vector<unsigned int>& piAtomAmberTypes, const vector< vector<unsigned int> >& piTestAtoms4vdw, const vector<vector<unsigned int> >& piMinimalDistances,vector< vector<float> > & piDistances);
bool isRigid(OBMol& piMol, vector<RotatableBond>& piRotatableBonds);
vector<unsigned int> getNextConformerMCSA(const vector<unsigned int>& piLastConformer, vector<RotatableBond>& piRotatableBonds);
vector<unsigned int> getIDFromIdConformer(unsigned long long piIdConformer, vector<RotatableBond>& piRotatableBonds);
vector<unsigned int> getNextConformer(const vector<unsigned int>& piLastConformer, vector<RotatableBond>& piRotatableBonds);
unsigned long long getConformerIndex(vector<unsigned int> piConformer);
double getScore(const vector<unsigned int>& piAtomIdxs, OBMol& piMol, double* piCoordinates, const vector< vector<unsigned int> >& piMinimalDistances,const vector<unsigned int>& piAtomAmberTypes, const vector< vector<unsigned int> >& piTestAtoms4vdw);
list< tConformer > getRefinedConformerList(vector < list < tConformer > > piConfs, vector < OBMol > piMol, const vector< vector<unsigned int> >& piMinimalDistances,const vector<unsigned int>& piAtomAmberTypes, const vector< vector<unsigned int> >& piTestAtoms4vdw);
int getSillasBotes(OBMol& piMol, int& poNumSillas);
void writeMol2Output(OBMol& piMol, list<tConformer>& piConformers);
string ulonglong2string(unsigned long long piInput);
string double2string(double piInput);
void writePDBOutput(OBMol& piMol, list<tConformer>& piConformers);
void writeXMLOutput(list<tConformer>& piConformers, vector< vector<unsigned int> > piAmberTypes, vector< vector<RotatableBond> > piRotatableBonds);
list< tConformer > getLibraryConformerList(list < tConformer > piConfs, OBMol piMol, const vector< vector<unsigned int> >& piMinimalDistances,const vector<unsigned int>& piAtomAmberTypes, const vector< vector<unsigned int> >& piTestAtoms4vdw);
//! Command line parameters
string addRulesFile;
double cutOff;
unsigned int howManySelect;
string inputFile;
unsigned long long maxCombinations;
string outputFile;
string outputType;
string referenceFile;
string useRulesFile;
unsigned int clustering;
unsigned int EnergySelection;
unsigned int outMini;
unsigned long long minSteps;
string minMethod;
double minEconv;
unsigned long long useInputInRMSD;
//! Useful global variables
double bestGeneratedRMSD = 1000.0;
double bestGeneratedRMSDEnergy = 1000.0;
double bestSelectedRMSD = 1000.0;
double bestSelectedRMSDEnergy = 1000.0;
vector<unsigned int> fileID2molID;
unsigned int generatedConformers = 0;
vector<unsigned int> molID2fileID;
unsigned long long possibleConformers = 1; // Because we have the input conformer
OBMol referenceMolecule;
vector<unsigned long long> sizeByIndexPosition;
unsigned int numConfig = 0;
double dist_cutoff = 0.80f;
int main(int argc, char** argv){
//! Variables
vector<unsigned int> amberTypesTMP;
vector< vector<unsigned int> > amberTypes;
vector< list<tConformer> > conformers;
list<tConformer> conformersRefined;
vector< vector<unsigned int> > minimalDistances;
vector < OBMol > molecule;
OBMol moleculeTMP1;
OBMol moleculeTMP2;
vector<RotatableBond> rotatableBondsTMP;
vector< vector<RotatableBond> > rotatableBonds;
string smiles;
vector< vector<unsigned int> > testAtoms4vdw;
OBStopwatch timer;
vector<TorsionRule> torsionRules;
srand( time(NULL) );
//! Start timer
timer.Start();
//! Get command line parameters
getParameters(argc, argv);
cout << endl << endl << LOGO1 ;
cout << endl << LOGO2 ;
cout << endl << LOGO3 ;
cout << endl << LOGO4 ;
cout << endl << LOGO5 ;
cout << endl << LOGO6 ;
cout << endl << LOGO7 << endl << endl;
cout << "2012-2016 Universidad de Alcala. Alvaro Cortes and Federico Gago" << endl;
cout << "2008-2012 Centro de Biologia Molecular Severo Ochoa. Javier Klett and Ruben Gil" << endl;
cout << "- Version: " << VERSION << endl << endl;
cout << "- Parameters" << endl;
cout << "\taddRulesFile: " << addRulesFile << endl;
cout << "\tcutOff: " << cutOff << endl;
cout << "\thowManySelect: " << howManySelect << endl;
cout << "\tinputFile: " << inputFile << endl;
cout << "\tmaxCombinations: " << maxCombinations << endl;
cout << "\toutputFile: " << outputFile << endl;
cout << "\toutputType: " << outputType << endl;
cout << "\tuseRulesFile: " << useRulesFile << endl << endl;
//! Read molecule
molecule = readInputMolecule(inputFile, fileID2molID, molID2fileID);
cout << "- Total input molecules: " << molecule.size() << endl << endl;
//! Test reference file
if (referenceFile != "") {
referenceMolecule = readMolecule(referenceFile);
moleculeTMP1 = molecule.at(0);
moleculeTMP2 = referenceMolecule;
moleculeTMP1.DeleteHydrogens();
moleculeTMP2.DeleteHydrogens();
if (moleculeTMP1.NumAtoms() != moleculeTMP2.NumAtoms()) {
cout << "ERROR: Different number of heavy-atoms in the input and reference molecule." << endl;
cout << "#Atoms Input: " << moleculeTMP1.NumAtoms() << " #Atoms Ref: " << moleculeTMP2.NumAtoms() << endl;
exit(-1);
}
}
//! Load torsion rules
loadTorsionRules(&torsionRules);
#ifdef _DEBUG
cout << "Number of loaded torsion rules: " << torsionRules.size() << endl;
#endif
// if(clustering != ""){
if(clustering == 1)
{
for(unsigned int k=0;k<molecule.size();k++)
{
numConfig++;
cout << "Processing input molecule " << numConfig << endl;
//! Assign rotatable bonds
rotatableBondsTMP = getRotatableBonds(molecule.at(k), torsionRules);
rotatableBonds.push_back(rotatableBondsTMP);
if( rotatableBondsTMP.size() >= 12)
dist_cutoff = 0.95f;
else if( rotatableBondsTMP.size() == 11)
dist_cutoff = 0.90f;
else if( rotatableBondsTMP.size() == 10)
dist_cutoff = 0.90f;
else if( rotatableBondsTMP.size() == 9)
dist_cutoff = 0.85f;
else if( rotatableBondsTMP.size() == 8)
dist_cutoff = 0.85f;
else if( rotatableBondsTMP.size() == 7)
dist_cutoff = 0.80f;
else if( rotatableBondsTMP.size() <= 6)
dist_cutoff = 0.75f;
else
dist_cutoff = 0.85f;
//! Add original angles
if (useInputInRMSD == 1) addOriginalAngles(molecule.at(k), rotatableBonds.at(k));
//! Clean equivalent angles
cleanEquivalentAngles(molecule.at(k), rotatableBonds.at(k));
//! Assign AMBER types
amberTypesTMP = getAmberTypes(molecule.at(k));
amberTypes.push_back(amberTypesTMP);
//! Get minimal distances
minimalDistances = getMinimalDistances(molecule.at(k));
//! Get test atoms for vdw
testAtoms4vdw = getTestAtoms4vdw(molecule.at(k), minimalDistances, rotatableBonds.at(k));
//! Get size by index position
sizeByIndexPosition = getSizeByIndexPosition(rotatableBonds.at(k));
vector<unsigned int> atomIdxs(molecule.at(k).NumAtoms(), 0);
unsigned int i = 0;
for( OBMolAtomIter atomsIter(molecule.at(k)); atomsIter; ++atomsIter){
atomIdxs.at(i) = atomsIter->GetIdx();
i++;
}
//! Get conformer list for current input molecule
conformers.push_back(generateConformers(molecule.at(k), rotatableBonds.at(k), minimalDistances, testAtoms4vdw, amberTypes.at(k)));
}
conformersRefined = getRefinedConformerList(conformers, molecule, minimalDistances, amberTypes.at(0), testAtoms4vdw); // be aware that we are passing amberTypes.at(0) this may leed to uncorrect vdw ene if amberTypes changes for different input configurations.
//getRefinedConformerList(conformers, molecule, minimalDistances, amberTypes.at(0), testAtoms4vdw); // be aware that we are passing amberTypes.at(0) this may leed to uncorrect vdw ene if amberTypes changes for different input configurations.
}else{
for(unsigned int k=0;k<molecule.size();k++)
{
numConfig++;
cout << "****** Processing input molecule " << numConfig << " ******" << endl;
//! Assign rotatable bonds
rotatableBondsTMP = getRotatableBonds(molecule.at(k), torsionRules);
rotatableBonds.push_back(rotatableBondsTMP);
//! Add original angles
if (useInputInRMSD == 1) {
addOriginalAngles(molecule.at(k), rotatableBonds.at(k));
}
//! Clean equivalent angles
cleanEquivalentAngles(molecule.at(k), rotatableBonds.at(k));
//! Assign AMBER types
amberTypesTMP = getAmberTypes(molecule.at(k));
amberTypes.push_back(amberTypesTMP);
//! Get minimal distances
minimalDistances = getMinimalDistances(molecule.at(k));
//! Get test atoms for vdw
testAtoms4vdw = getTestAtoms4vdw(molecule.at(k), minimalDistances,rotatableBonds.at(k));
//! Get size by index position
sizeByIndexPosition = getSizeByIndexPosition(rotatableBonds.at(k));
vector<unsigned int> atomIdxs(molecule.at(k).NumAtoms(), 0);
unsigned int i = 0;
for( OBMolAtomIter atomsIter(molecule.at(k)); atomsIter; ++atomsIter){
atomIdxs.at(i) = atomsIter->GetIdx();
i++;
}
if( rotatableBondsTMP.size() >= 12)
dist_cutoff = 0.95f;
else if( rotatableBondsTMP.size() == 11)
dist_cutoff = 0.90f;
else if( rotatableBondsTMP.size() == 10)
dist_cutoff = 0.90f;
else if( rotatableBondsTMP.size() == 9)
dist_cutoff = 0.85f;
else if( rotatableBondsTMP.size() == 8)
dist_cutoff = 0.85f;
else if( rotatableBondsTMP.size() == 7)
dist_cutoff = 0.80f;
else if( rotatableBondsTMP.size() <= 6)
dist_cutoff = 0.75f;
else
dist_cutoff = 0.85f;
// if( rotatableBonds.at(k).size() > 7)
// {
// cout << "Rotable bonds larger than 7. Skipping" << endl;
// list<tConformer> empty;
// conformers.push_back( empty );
// continue;
// }
conformers.push_back(generateConformers(molecule[k], rotatableBonds.at(k), minimalDistances, testAtoms4vdw, amberTypes.at(k)));
conformersRefined = getLibraryConformerList(conformers.at(k), molecule[k], minimalDistances, amberTypes.at(k), testAtoms4vdw);
cout << "- Number of atoms: " << molecule.at(k).NumAtoms() << endl;
cout << "- Rotatable bonds: " << rotatableBonds.at(k).size() << endl << endl;
unsigned int index = 0;
for (vector<RotatableBond>::iterator itRotBonds = rotatableBonds.at(k).begin(); itRotBonds != rotatableBonds.at(k).end(); itRotBonds++) {
index++;
cout << "\t" << index << " (" << itRotBonds->getType() << "); atoms: ";
cout << molID2fileID[itRotBonds->getAtoms()[0]->GetIdx()] << " ";
cout << molID2fileID[itRotBonds->getAtoms()[1]->GetIdx()] << " ";
cout << molID2fileID[itRotBonds->getAtoms()[2]->GetIdx()] << " ";
cout << molID2fileID[itRotBonds->getAtoms()[3]->GetIdx()];
cout << "; angles:";
for (unsigned int i = 0; i < itRotBonds->getAngles().size(); i++) {
cout << " " << itRotBonds->getAngles()[i];
}
cout << endl;
}
cout << endl;
cout << "- Possible conformers: " << possibleConformers << endl;
cout << "- Generated conformers: " << generatedConformers << endl;
cout << "- Accepted conformers: " << conformersRefined.size() << endl << endl;
writeMol2Output(molecule.at(k), conformersRefined);
}
}
//! Write outupt
if(clustering != 0){
if (outputType == "mol2" || outputType == "mol2-xml") {
writeMol2Output(molecule.at(0), conformersRefined);
}
if (outputType == "pdb"){
writePDBOutput(molecule.at(0), conformersRefined);
}
if (outputType == "xml" || outputType == "mol2-xml") {
writeXMLOutput(conformersRefined, amberTypes, rotatableBonds);
}
/* }else{
if (outputType == "mol2" || outputType == "mol2-xml") {
writeMol2Output(molecule.at(0), conformers.at(0));
}
if (outputType == "pdb"){
writePDBOutput(molecule.at(0), conformers.at(0));
}
if (outputType == "xml" || outputType == "mol2-xml") {
writeXMLOutput(conformers.at(0), amberTypes, rotatableBonds);
}*/
}
double seconds = timer.Elapsed();
// cout << "- Number of atoms: " << molecule.at(0).NumAtoms() << endl;
// cout << "- Rotatable bonds: " << rotatableBonds.at(0).size() << endl << endl;
// unsigned int index = 0;
// for (vector<RotatableBond>::iterator itRotBonds = rotatableBonds.at(0).begin(); itRotBonds != rotatableBonds.at(0).end(); itRotBonds++) {
// index++;
// cout << "\t" << index << " (" << itRotBonds->getType() << "); atoms: ";
// cout << molID2fileID[itRotBonds->getAtoms()[0]->GetIdx()] << " ";
// cout << molID2fileID[itRotBonds->getAtoms()[1]->GetIdx()] << " ";
// cout << molID2fileID[itRotBonds->getAtoms()[2]->GetIdx()] << " ";
// cout << molID2fileID[itRotBonds->getAtoms()[3]->GetIdx()];
// cout << "; angles:";
// for (unsigned int i = 0; i < itRotBonds->getAngles().size(); i++) {
// cout << " " << itRotBonds->getAngles()[i];
// }
// cout << endl;
// }
// cout << endl;
// cout << "- Possible conformers: " << possibleConformers << endl;
// cout << "- Generated conformers: " << generatedConformers << endl;
if (referenceFile != "") cout << "- Best generated RMSD: " << bestGeneratedRMSD << " (energy = " << bestGeneratedRMSDEnergy << ")" << endl;
if(clustering != 0){
cout << "- Accepted conformers: " << conformersRefined.size() << endl;
// }else{
// cout << "- Accepted conformers: " << conformers.at(0).size() << endl;
}
if (referenceFile != "") cout << "- Best selected RMSD: " << bestSelectedRMSD << " (energy = " << bestSelectedRMSDEnergy << ")" << endl;
//cout << "- Time per conformer: " << (seconds / generatedConformers) * 1000 << " milliseconds" << endl;
cout << "- Total time: " << seconds << " seconds" << endl << endl;
exit(0);
}
void getParameters(int piArgc, char **piArgv) {
int i;
if (piArgc == 1)help();
//! Setting up default values
howManySelect = 20;
cutOff = 0.0;
maxCombinations = 300000;
addRulesFile = "";
referenceFile = "";
useRulesFile = "";
referenceFile = "";
outputType = "mol2";
useInputInRMSD = 1;
outMini = 0;
clustering = 1;
EnergySelection = 0;
minSteps = 1000;
minMethod = "conjugate";
minEconv = 1.0e-6;
for(i = 1; i < piArgc; i++){
//! Getting input parameters
if (strncmp(piArgv[i],"-help",5)==0){
help();
}else if (strncmp(piArgv[i],"-inputFile",10) == 0){
inputFile = piArgv[i+1];
//printf("INPUTFILE >> %i %s\n",i,inputFile.c_str());
}else if (strncmp(piArgv[i],"-outputFile",11) == 0){
outputFile = piArgv[i+1];
//printf("OUTPUTFILE >> %i %s\n",i,outputFile.c_str());
}else if (strncmp(piArgv[i],"-howManySelect",14) == 0){
howManySelect = (unsigned int) atoi(piArgv[i+1]);
//printf("HOWMANYSELECT >> %i %i\n",i, howManySelect);
}else if (strncmp(piArgv[i],"-cutOff", 7) == 0){
//printf("CUTOFF\n");
cutOff = atof(piArgv[i+1]);
}else if (strncmp(piArgv[i],"-maxCombinations",16) == 0){
//printf("MAXCOMBINATIONS\n");
maxCombinations = string2ulonglong(piArgv[i+1]);
}else if (strncmp(piArgv[i],"-useInputInRMSD",15) == 0){
//printf("USEINPUTINRMSD\n");
useInputInRMSD = (unsigned int)atof(piArgv[i+1]);
}else if (strncmp(piArgv[i],"-outputType",11) == 0){
//printf("OUTPUTTYPE\n");
outputType = piArgv[i+1];
}else if (strncmp(piArgv[i],"-addRulesFile",13) == 0){
addRulesFile = piArgv[i+1];
//printf("ADDRULESFILE\n");
}else if (strncmp(piArgv[i],"-referenceFile",14) == 0){
//printf("REFERENCEFILE\n");
referenceFile = piArgv[i+1];
}else if (strncmp(piArgv[i],"-useRulesFile",13) == 0){
//printf("USERULESFILE\n");
useRulesFile = piArgv[i+1];
}else if (strncmp(piArgv[i],"-energySelection",11) == 0){
EnergySelection = 1;
}else if (strncmp(piArgv[i],"-clustering",11) == 0){
clustering = 1;
}else if(strncmp(piArgv[i],"-minimize",8)==0){
outMini = 1;
}else if(strncmp(piArgv[i],"-minMethod",8)==0){
minMethod = piArgv[i+1];
}else if(strncmp(piArgv[i],"-minSteps",8)==0){
minSteps = string2ulonglong(piArgv[i+1]);
}else if(strncmp(piArgv[i],"-h",2)==0){
help();
}
}
if ( EnergySelection == 1){
clustering = 0;
}
return;
}
void help(void){
fprintf(stderr, "\nALFA - %s \n\n", VERSION.c_str());
fprintf(stderr, "Another program to generate conformers based on rules\n");
fprintf(stderr, "2012 - 2016 Universidad de Alcala. Alvaro Cortes <alvarocortesc@gmail.com> and Federico Gago.\n");
fprintf(stderr, "2008 - 2012 Centro de Biologia Molecular Severo Ochoa. Javier Klett and Ruben Gil.\n\n");
fprintf(stderr, "Complete parameter list:\n");
fprintf(stderr, " -addRulesFile : File containing torsion rules in order to add they (replacing if needed) to the default torsion rules\n");
fprintf(stderr, " -cutOff : Cut off for the energies in the final list (allows values only of minEnergy + cutOff). -- DEFAULT: 0.0 (no cutOff)\n");
fprintf(stderr, " -howManySelect : Maximum number of selected conformers. -- DEFAULT: 100\n");
fprintf(stderr, " -inputFile : Name of the mol2 input file\n");
fprintf(stderr, " -maxCombinations : Maximum number of generated conformers. -- DEFAULT: 300000\n");
fprintf(stderr, " -outputFile : Name of the output file (without extension)\n");
fprintf(stderr, " -outputType : Type of the output file. -- DEFAULT: mol2\n");
fprintf(stderr, " -referenceFile : Name of the reference file in order to perform RMSD calculations\n");
fprintf(stderr, " -useInputInRMSD : Say if input structure must be taken into account for RMSD calculations. Be careful, if you activate this option then ALFA rules\n");
fprintf(stderr, " can include the input molecule angles. Allowed values: 0 (no), 1 (yes). -- DEFAULT: 1\n");
fprintf(stderr, " -useRulesFile : File containing torsion rules in order to use they (not taking into account default torsion rules\n");
fprintf(stderr, " -clustering : conformers are chosen by similirity criteria -- DEFAULT\n");
fprintf(stderr, " -energySelection : conformers are chosen by minimum energy criteria\n");
fprintf(stderr, " -minimize : Minimize output conformations\n");
fprintf(stderr, " -minMethod : Allowed values: conjugate / steepest -- DEFAULT conjugate\n");
fprintf(stderr, " -minSteps : Number of steeps in minimization-- DEFAULT 1000\n");
fprintf(stderr, " -minEconv : Energy convergence criteria.-- DEFAULT 1e-6\n");
exit(1);
}
unsigned long long string2ulonglong(string piInput) {
//if (!isNumber(piInput)) {
//throw VSDBException(piInput + " is not a number");
//}
char* pEnd;
return strtoull(piInput.c_str(), &pEnd, 10);
}
vector < OBMol > readInputMolecule(string piInputFile, vector<unsigned int>& poFileID2molID, vector<unsigned int>& poMolID2FileID) {
vector < OBMol > mol;
OBMol * molTMP;
ifstream ifs(piInputFile.c_str());
if(!ifs)
{
cout << "Error. Cannot open input file. Aborting..." << endl;
exit(-1);
}
OBConversion conv;
OBFormat* inFormat = conv.FormatFromExt(piInputFile.c_str());
conv.SetInFormat(inFormat);
#ifdef _DEBUG
cout << "Calculating conformers for: " << piInputFile << endl;
#endif
if ( strcmp(inFormat->GetID(),"sy2") == 0 ) {
#ifdef _DEBUG
cout << "MOL2 file" << endl;
#endif
conv.SetInFormat("MOL2");
for( molTMP = new OBMol ; conv.Read(molTMP,&ifs); molTMP = new OBMol ){
molTMP->FindRingAtomsAndBonds();
OBAromaticTyper aromTyper;
aromTyper.AssignAromaticFlags(*molTMP);
OBAtomTyper atomTyper;
atomTyper.AssignHyb(*molTMP);
mol.push_back(*molTMP);
}
} else if (strcmp(inFormat->GetID(),"ent") == 0) {
#ifdef _DEBUG
cout << "PDB file" << endl;
#endif
conv.SetInFormat("PDB");
for( molTMP = new OBMol ; conv.Read(molTMP,&ifs); molTMP = new OBMol ){
molTMP->ConnectTheDots();
molTMP->FindRingAtomsAndBonds();
molTMP->PerceiveBondOrders();
OBAromaticTyper aromTyper;
aromTyper.AssignAromaticFlags(*molTMP);
OBAtomTyper atomTyper;
atomTyper.AssignHyb(*molTMP);
mol.push_back(*molTMP);
}
} else {
cout << "ERROR: unknow type for file " << piInputFile << endl;
exit(-1);
}
ifs.close();
#ifdef _DEBUG
cout << "Num. Input conformations " << mol.size() << endl;
cout << "Num. Atoms " << mol[0].NumAtoms() << endl;
#endif
getFileVsMolRelationIDs(mol[0], poFileID2molID, poMolID2FileID);
return mol;
}
void getFileVsMolRelationIDs(OBMol& piMol, vector<unsigned int>& poFileID2molID, vector<unsigned int>& poMolID2fileID) {
poFileID2molID.reserve(piMol.NumAtoms() + 1);
poFileID2molID.assign(0, piMol.NumAtoms() + 1);
poMolID2fileID.reserve(piMol.NumAtoms() + 1);
poMolID2fileID.assign(0, piMol.NumAtoms() + 1);
OBMolAtomIter atom(piMol);
unsigned int index;
for(index = 1; atom; index++, ++atom ){
poFileID2molID[index] = atom->GetIdx();
poMolID2fileID[atom->GetIdx()] = index;
}
}
OBMol readMolecule(string piInputFile) {
OBMol mol;
ifstream ifs(piInputFile.c_str());
OBConversion conv;
OBFormat* inFormat = conv.FormatFromExt(piInputFile.c_str());
conv.SetInFormat(inFormat);
#ifdef _DEBUG
cout << "Referece file: " << piInputFile << endl;
#endif
if (strcmp(inFormat->GetID(),"sy2") == 0) {
#ifdef _DEBUG
cout << "MOL2 file" << endl;
#endif
conv.SetInFormat("MOL2");
conv.Read(&mol,&ifs);
mol.FindRingAtomsAndBonds();
OBAromaticTyper aromTyper;
aromTyper.AssignAromaticFlags(mol);
OBAtomTyper atomTyper;
atomTyper.AssignHyb(mol);
} else if (strcmp(inFormat->GetID(),"ent") == 0) {
#ifdef _DEBUG
cout << "PDB file" << endl;
#endif
conv.SetInFormat("PDB");
conv.Read(&mol,&ifs);
mol.ConnectTheDots();
mol.FindRingAtomsAndBonds();
mol.PerceiveBondOrders();
OBAromaticTyper aromTyper;
aromTyper.AssignAromaticFlags(mol);
OBAtomTyper atomTyper;
atomTyper.AssignHyb(mol);
} else {
cout << "ERROR: unknow type for file " << piInputFile << endl;
exit(-1);
}
ifs.close();
return mol;
}
void loadTorsionRules(vector<TorsionRule> *torsionRulesList) {
ifstream ifs;
string line;
if (useRulesFile != "") {
ifs.open(useRulesFile.c_str());
while (getline(ifs, line)) {
line = simplifyWhitesAndTabs((char *)line.c_str());
if ((line.find_first_of("#") == 0) || (line.length() == 0)) {
continue; // Commentary or blank line
}
readTorsionRule(line);
torsionRulesList->push_back(readTorsionRule(line));
}
ifs.close();
} else {
torsionRulesList->reserve(NDTR);
for (unsigned int i = 0; i < NDTR; i++) {
list<string> stLineDTR;
torsionRulesList->push_back(readTorsionRule( simplifyWhitesAndTabs((char *)DTR[i].c_str())));
}
}
if (addRulesFile != "") {
ifs.open(addRulesFile.c_str());
while (getline(ifs, line)) {
line = simplifyWhitesAndTabs((char *)line.c_str());
if ((line.find_first_of("#") == 0) || (line.length() == 0)) {
continue;
}
torsionRulesList->push_back(readTorsionRule(line));
}
ifs.close();
}
return;
}
string simplifyWhitesAndTabs(char * inStr){
string outStr;
char * pch;
pch = strtok (inStr," \t");
while (pch != NULL){
outStr.append(pch);
outStr.append(" ");
pch = strtok (NULL, " \t");
}
return outStr;
}
list<string> separateByWhites(char * inStr){
list<string> outStr;
char * pch;
pch = strtok (inStr," \t");
while (pch != NULL){
outStr.push_back(pch);
pch = strtok (NULL, " \t");
}
return outStr;
}
TorsionRule readTorsionRule(string piLine) {
list<string> stLine;
stLine = separateByWhites((char *)piLine.c_str());
list<string>::iterator itList;
unsigned int index;
string name;
string pattern;
int point;
vector<unsigned int> points;
points.reserve(4);
float angle;
vector<double> angles;
for (itList = stLine.begin(), index = 1; itList != stLine.end(); itList++, index++) {
switch (index) {
case 1: name = *itList; break;
case 2: pattern = *itList; break;
case 3:case 4:case 5:case 6:
sscanf(itList->c_str(), "%d", &point );
points.push_back(point);
break;
default:
sscanf(itList->c_str(), "%f", &angle );
angles.push_back((double)angle);
break;
}
}
TorsionRule torsionRule(name, pattern, points, angles);
return torsionRule;
}
vector<RotatableBond> getRotatableBonds(OBMol& piMol, vector<TorsionRule> piTorsionRules) {
vector<RotatableBond> rotatableBonds;
//! Take rotatable bonds
for( OBMolBondIter iterRotBonds(piMol); iterRotBonds; ++iterRotBonds ){
if(iterRotBonds->IsRotor()){
vector<double> angles;
vector<OBAtom*> atoms;
OBAtom *atom1, *atom2, *atom3, *atom4;
//! main atoms
atom2 = iterRotBonds->GetBeginAtom();
atom3 = iterRotBonds->GetEndAtom();
//! random references
for( OBAtomAtomIter it(atom2); it; ++it ){
if(it->GetIdx() != atom2->GetIdx()){ atom1 = &(*it); break;}
}
for( OBAtomAtomIter it(atom3); it; ++it ){
if(it->GetIdx() != atom3->GetIdx() && it->GetIdx() != atom2->GetIdx()){ atom4 = &(*it); break; }
}
atoms.push_back(atom1); atoms.push_back(atom2); atoms.push_back(atom3); atoms.push_back(atom4);
RotatableBond rotatableBond("UNKNOWN", atoms, angles);
rotatableBonds.push_back(rotatableBond);
}
}
//! Assign types, references and angles
for (unsigned int i = 0; i < piTorsionRules.size(); i++) {
TorsionRule torsionRule = piTorsionRules.at(i);
OBSmartsPattern smartPatter;
smartPatter.Init(torsionRule.getPattern().c_str());
smartPatter.Match(piMol);
if(smartPatter.NumMatches() == 0) continue;
vector<vector<int> > maplist;
maplist = smartPatter.GetMapList();
for (unsigned int j = 0; j < rotatableBonds.size(); j++) {
vector<vector<int> >::iterator i;
for (i = maplist.begin();i != maplist.end();++i){
vector<OBAtom*> points;
for (unsigned int k = 0; k < 4; k++) points.push_back(piMol.GetAtom(i->at(k)));
if ((rotatableBonds.at(j).getAtoms().at(1)->GetIdx() == points.at(1)->GetIdx()) && (rotatableBonds.at(j).getAtoms().at(2)->GetIdx() == points.at(2)->GetIdx())) {
rotatableBonds.at(j).updateAtom(0, points.at(0));
rotatableBonds.at(j).updateAtom(3, points.at(3));
rotatableBonds.at(j).setType(torsionRule.getName());
rotatableBonds.at(j).setAngles(torsionRule.getAngles());
break;
}else if ((rotatableBonds.at(j).getAtoms().at(1)->GetIdx() == points.at(2)->GetIdx()) && (rotatableBonds.at(j).getAtoms().at(2)->GetIdx() == points.at(1)->GetIdx())) {
rotatableBonds.at(j).updateAtom(0, points.at(3));
rotatableBonds.at(j).updateAtom(3, points.at(0));
rotatableBonds.at(j).setType(torsionRule.getName());
rotatableBonds.at(j).setAngles(torsionRule.getAngles());
break;
}
}
}
}
//! Clear not rotatable bonds (torsionals without angles)
vector<RotatableBond> tmpRotatableBonds = rotatableBonds;
rotatableBonds.clear();
for (unsigned int i = 0; i < tmpRotatableBonds.size(); i++) {
if (tmpRotatableBonds[i].getAngles().size() != 0) {
rotatableBonds.push_back(tmpRotatableBonds[i]);
}
}
#ifdef _DEBUG_ANG
for (unsigned int h = 0; h < rotatableBonds.size(); h++){
cout << rotatableBonds.at(h).getType() << " --> ";
cout << rotatableBonds.at(h).getAtoms().at(0)->GetIdx() << " ";
cout << rotatableBonds.at(h).getAtoms().at(1)->GetIdx() << " ";
cout << rotatableBonds.at(h).getAtoms().at(2)->GetIdx() << " ";
cout << rotatableBonds.at(h).getAtoms().at(3)->GetIdx() << endl;
for (unsigned int g = 0; g < rotatableBonds.at(h).getAngles().size(); g++){
cout << rotatableBonds.at(h).getAngles().at(g) << " ";
}
cout << endl;
}
#endif
return rotatableBonds;
}
void addOriginalAngles(OBMol& piMol, vector<RotatableBond>& piRotatableBonds) {
for (unsigned int i = 0; i < piRotatableBonds.size(); i++) {
double originalAngle = piMol.GetTorsion(piRotatableBonds[i].getAtoms()[0], piRotatableBonds[i].getAtoms()[1], piRotatableBonds[i].getAtoms()[2], piRotatableBonds[i].getAtoms()[3]) ;
unsigned int howManyAngles = piRotatableBonds[i].getAngles().size();
for (int j = (howManyAngles - 1); j >= 0; j--) {
int diffAngle = (int) rint(piRotatableBonds[i].getAngles()[j] - originalAngle);
if (diffAngle > 180) {
diffAngle = 360 - diffAngle;
}
if (abs(diffAngle) < 30) { //! Remove similar angle to the original
piRotatableBonds[i].removeAngle(j);
}
}
piRotatableBonds[i].addAngle(rint(originalAngle)); //! Add original angle
#ifdef _DEBUG_ANG
cout << (int)originalAngle << " " ;
#endif
}
#ifdef _DEBUG_ANG
cout << "<--ANGLES IN ORIG. MOL." << endl;
#endif
}
void cleanEquivalentAngles(OBMol& piMol, vector<RotatableBond>& piRotatableBonds) {
OBAlign *rmsd = new OBAlign(false,true);
OBMol referenceMol_1 = piMol;
OBMol referenceMol_2 = piMol;
double DELETED_VALUE = 1000;
double *origCoord = new double[piMol.NumAtoms() * 3];
double *origCoordTmp = piMol.GetCoordinates(); //! Save the original coordinates
for(unsigned int i = 0; i < piMol.NumAtoms() * 3; i = i+3){
origCoord[i ] = origCoordTmp[i ];
origCoord[i+1] = origCoordTmp[i+1];
origCoord[i+2] = origCoordTmp[i+2];
}
for (unsigned int i = 0; i < piRotatableBonds.size(); i++) {
vector<double> newAngles;
vector<double> angles = piRotatableBonds[i].getAngles();
vector<OBAtom*> atoms = piRotatableBonds[i].getAtoms();
#ifdef _DEBUG_ANG
cout << piRotatableBonds[i].getType() << " Atms: ";
cout << " " << piRotatableBonds.at(0).getAtoms().at(0)->GetIdx();
cout << " " << piRotatableBonds.at(0).getAtoms().at(1)->GetIdx();
cout << " " << piRotatableBonds.at(0).getAtoms().at(2)->GetIdx();
cout << " " << piRotatableBonds.at(0).getAtoms().at(3)->GetIdx() << endl;
#endif
for (unsigned int j = 0; j < angles.size() - 1; j++) {
if (angles[j] == DELETED_VALUE) continue;
referenceMol_1.SetCoordinates(origCoord);
OBAtom *a_1 = referenceMol_1.GetAtom(piRotatableBonds.at(0).getAtoms().at(0)->GetIdx());
OBAtom *b_1 = referenceMol_1.GetAtom(piRotatableBonds.at(0).getAtoms().at(1)->GetIdx());
OBAtom *c_1 = referenceMol_1.GetAtom(piRotatableBonds.at(0).getAtoms().at(2)->GetIdx());
OBAtom *d_1 = referenceMol_1.GetAtom(piRotatableBonds.at(0).getAtoms().at(3)->GetIdx());
referenceMol_1.SetTorsion(a_1,b_1,c_1,d_1, angles[j] * DEG_TO_RAD);
#ifdef _DEBUG_ANG
cout <<j << " " <<angles[j] << "\tRef_1 "<< referenceMol_1.GetTorsion(a_1, b_1, c_1, d_1) << endl;
#endif
for (unsigned int k = j + 1; k < angles.size(); k++) {
if (angles[k] == DELETED_VALUE) continue;
referenceMol_2.SetCoordinates(origCoord);
OBAtom *a_2 = referenceMol_2.GetAtom(piRotatableBonds.at(0).getAtoms().at(0)->GetIdx());
OBAtom *b_2 = referenceMol_2.GetAtom(piRotatableBonds.at(0).getAtoms().at(1)->GetIdx());
OBAtom *c_2 = referenceMol_2.GetAtom(piRotatableBonds.at(0).getAtoms().at(2)->GetIdx());
OBAtom *d_2 = referenceMol_2.GetAtom(piRotatableBonds.at(0).getAtoms().at(3)->GetIdx());
referenceMol_2.SetTorsion(a_2,b_2,c_2,d_2, angles[k] * DEG_TO_RAD);
#ifdef _DEBUG_ANG
cout << k<< " " <<angles[k] << "\tRef_2 " << referenceMol_2.GetTorsion(a_2, b_2, c_2, d_2) << endl;
#endif
rmsd->SetRefMol(referenceMol_1);
rmsd->SetTargetMol(referenceMol_2);
rmsd->Align();
rmsd->UpdateCoords(&referenceMol_2);
if (rmsd->GetRMSD () < DELTA) {
#ifdef _DEBUG_ANG
cout << "DELETED " << rmsd->GetRMSD() << angles[k] << endl;
#endif
angles[k] = DELETED_VALUE;
}
}
}
for (unsigned int j = 0; j < angles.size(); j++) {
if (angles[j] != DELETED_VALUE) newAngles.push_back(angles[j]);
}
piRotatableBonds[i].setAngles(newAngles);
}
piMol.SetCoordinates(origCoord); //! Restore original coordinates, may be not needed
return;
}
vector<unsigned int> getAmberTypes(OBMol& piMol) {
vector<unsigned int> result;
result.assign(piMol.NumAtoms() + 1, 0);
for( OBMolAtomIter atomsIter(piMol); atomsIter; ++atomsIter ){
if( atomsIter->IsHydrogen() ){
#ifdef _DEBUG_AMBTYPES
cout << "Atom " << atomsIter->GetIdx() << " is Hyd? " << atomsIter->IsHydrogen() << endl;
#endif
continue;
}
if (atomsIter->GetAtomicNum() == 6) { // carbon
if (atomsIter->GetHyb() <= 2) { // sp2 o sp1
#ifdef _DEBUG_AMBTYPES
cout << "Atom " << atomsIter->GetIdx() << " is Carbon sp2 ó sp1. AtomicNum = " << atomsIter->GetAtomicNum() << " Hybridation = " << atomsIter->GetHyb()<< endl;
#endif
result[atomsIter->GetIdx()] = 2;
} else if (atomsIter->GetHyb() >= 3) { //sp3
#ifdef _DEBUG_AMBTYPES
cout << "Atom " << atomsIter->GetIdx() << " is Carbon sp3. AtomicNum = " << atomsIter->GetAtomicNum() << " Hybridation = " << atomsIter->GetHyb()<< endl;
#endif
result[atomsIter->GetIdx()] = 1;
}
#ifdef _DEBUG_AMBTYPES
cout << "Vecinos de carbonos -Z " ;
#endif
//! Assing nprotonate state and nheteroatoms
bool nprotonate = false;
unsigned int nheteroatoms = 0;
for (OBAtomAtomIter neighborAtomsIter( *atomsIter ); neighborAtomsIter; ++neighborAtomsIter ) {
#ifdef _DEBUG_AMBTYPES
cout << neighborAtomsIter->GetIdx() << " - " ;
#endif
if(neighborAtomsIter->IsCarbon() || neighborAtomsIter->IsHydrogen() || neighborAtomsIter->IsPhosphorus() ) continue;
if (neighborAtomsIter->IsNitrogen()) {
if (neighborAtomsIter->GetValence() > 3) {
nprotonate = true;
} else {
nheteroatoms++;
}
} else {
nheteroatoms++;
}
}
#ifdef _DEBUG_AMBTYPES
cout << endl;
#endif
//! Assign H type
unsigned int hType = 8; //! Generic
if (atomsIter->GetHyb() <= 2) {
if (nheteroatoms == 0) {
hType = 9;
} else if (nheteroatoms == 1) {
hType = 20;
} else if (nheteroatoms == 2) {
hType = 21;
}
} else if (atomsIter->GetHyb() >= 3) {
if (nprotonate) {
hType = 22;
} else if (nheteroatoms == 0) {
hType = 8;
} else if (nheteroatoms == 1) {
hType = 17;
} else if (nheteroatoms == 2) {
hType = 18;
} else if (nheteroatoms == 3) {
hType = 19;
}
}
for (OBAtomAtomIter neighborHAtomsIter( *atomsIter ); neighborHAtomsIter; ++neighborHAtomsIter ) {
if (!neighborHAtomsIter->IsHydrogen()) continue;
result[ neighborHAtomsIter->GetIdx()] = hType;
#ifdef _DEBUG_AMBTYPES