-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtmolecule.cpp
1822 lines (1605 loc) · 66 KB
/
tmolecule.cpp
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
#include "tmolecule.h"
std::list<TElectr> TMolecule::Electros;
std::list<TLennard> TMolecule::Lennards;
std::list<THBond> TMolecule::HBonds;
std::list<TSpring> TMolecule::Springs;
std::list<TBasePair> TMolecule::BasePairs;
std::vector<std::pair<
std::list<TElectr>::const_iterator, std::list<TElectr>::const_iterator> > TMolecule::IteratorsPARALLEL;
#ifndef SEQUENTIAL
pthread_mutex_t TMolecule::mutexPARALLEL = PTHREAD_MUTEX_INITIALIZER;
double TMolecule::dElectrEnergyPARALLEL;
#endif /*SEQUENTIAL*/
//---------------------------------------------------------------------------------------------------
TMolecule::TMolecule(const TMolecule& _moMol)
{
/********************************************************************************
* This copy constructor must be defined due to std::list<> expectations
* but SHOULD NEVER BE USED unless an empty molecule is about to be copied.
* (it would cause invalidation of all Bonds, Angles, Dihedrals... since
* they all contain pointers to Atoms[] elements)
********************************************************************************/
if(!(_moMol.bEmpty()))
{
std::cerr << "Fatal Internal Error: forbidden copying of nonempty molecule!" << std::endl;
assert(0);
}
}
//---------------------------------------------------------------------------------------------------
int TMolecule::iReadAtoms(std::istream& _isIn)
{
int iRetVal = 0;
std::string sLine;
while(std::getline(_isIn, sLine)) // Read single line from input PDB
{
TAtom atTemp;
if(atTemp.bAssignTag(sLine))
{ // It fits as a description of atom
Atoms.push_back(atTemp);
++iRetVal;
}
else if(sLine.length() >= 3 && sLine.substr(0,3) == "TER") break;
}
return iRetVal;
}
//---------------------------------------------------------------------------------------------------
bool TMolecule::bMakeIntraBonds(std::map<std::string, int>& AtomContentMap,
const std::vector<std::pair<std::string,std::string> >& ResSequence)
{// A piece of code not to be fucked with unless necessary.
bool bRetVal = true;
if(TConsts::TALKATIVE > 0) std::clog << "Building intraresidual bonds..." << std::endl;
for(unsigned int j = 0; j < ResSequence.size(); ++j)
{
std::ifstream ifsIn((TConsts::BONDSDIR+ResSequence[j].second+".bnd").c_str());
if(ifsIn != NULL)
{
std::string sBondLine;
while(std::getline(ifsIn, sBondLine))
{
std::istringstream issIn(sBondLine);
std::string sAtom1Temp, sAtom2Temp;
double dKrTemp, dREqTemp;
issIn >> sAtom1Temp >> sAtom2Temp >> dKrTemp >> dREqTemp;
Textutils::bCorrectAtomName(sAtom1Temp);
Textutils::bCorrectAtomName(sAtom2Temp);
if(AtomContentMap.find(ResSequence[j].first+sAtom1Temp) != AtomContentMap.end() &&
AtomContentMap.find(ResSequence[j].first+sAtom2Temp) != AtomContentMap.end())
{
Bonds.push_back(TBond(Atoms[AtomContentMap[ResSequence[j].first+sAtom1Temp]],
Atoms[AtomContentMap[ResSequence[j].first+sAtom2Temp]],
BondParam(dKrTemp, dREqTemp) ));
if(TConsts::TALKATIVE > 2)
std::clog << "Bond added: " << sAtom1Temp << " " << sAtom2Temp
<< " in residue: " << ResSequence[j].first << std::endl;
}
else
{
std::cerr << "Warning: Bond expected, but some of the atoms are missing: " << sAtom1Temp << " " << sAtom2Temp
<< ", residue No. " << ResSequence[j].first << std::endl;
bRetVal = false; // Unknown atom name in *.bnd file.
}
}
}
else
{
std::cerr << "Error: Cannot read file: " << (TConsts::BONDSDIR+ResSequence[j].second+".bnd") << std::endl;
bRetVal = false;
break; // Unknown residue is a good reason to break.
}
}
return bRetVal;
}
//---------------------------------------------------------------------------------------------------
bool TMolecule::bMakeInterBonds(std::map<std::string, int>& AtomContentMap,
const std::vector<std::pair<std::string,std::string> >& ResSequence)
{
bool bRetVal = true;
if(TConsts::TALKATIVE > 0) std::clog << "Building interresidual bonds..." << std::endl;
for(unsigned int j = 0; j < ResSequence.size()-1; ++j)
{
if(AtomContentMap.find(ResSequence[j].first+"O3'") != AtomContentMap.end() &&
AtomContentMap.find(ResSequence[j+1].first+"P") != AtomContentMap.end())
{
Bonds.push_back(TBond(Atoms[AtomContentMap[ResSequence[j].first+"O3'"]],
Atoms[AtomContentMap[ResSequence[j+1].first+"P"]],
TConsts::GeneralBondsData[TConsts::AtomTypeNumbers[Atoms[AtomContentMap[ResSequence[j].first+"O3'"]].sGetType()]]\
[TConsts::AtomTypeNumbers[Atoms[AtomContentMap[ResSequence[j+1].first+"P"]].sGetType()]]));
if(TConsts::TALKATIVE > 2)
std::clog << "Interresidual bond added: "
<< Atoms[AtomContentMap[ResSequence[j].first+"O3'"]].sGetPDB_tag().substr(0,27) << " --- "
<< Atoms[AtomContentMap[ResSequence[j+1].first+"P"]].sGetPDB_tag().substr(0,27)
<< std::endl;
}
else
{
std::cerr << "Error: Cannot link residues: " << ResSequence[j].first << " and "
<< ResSequence[j+1].first << std::endl;
bRetVal = false;
}
}
return bRetVal;
}
//---------------------------------------------------------------------------------------------------
bool TMolecule::bMakeIntraAngles(std::map<std::string, int>& AtomContentMap,
const std::vector<std::pair<std::string,std::string> >& ResSequence)
{
bool bRetVal = true;
if(TConsts::TALKATIVE > 0) std::clog << "Building intraresidual angles..." << std::endl;
for(unsigned int j = 0; j < ResSequence.size(); ++j)
{
std::ifstream in((TConsts::ANGLEDIR+ResSequence[j].second+".angl").c_str());
if(in != NULL)
{
std::string sAnglLine;
while(std::getline(in, sAnglLine))
{
std::istringstream issIn(sAnglLine);
std::string sAtom1Temp, sAtom2Temp, sAtom3Temp;
double dKTheta, dThetaEq;
issIn >> sAtom1Temp >> sAtom2Temp >> sAtom3Temp >> dKTheta >> dThetaEq;
Textutils::bCorrectAtomName(sAtom1Temp);
Textutils::bCorrectAtomName(sAtom2Temp);
Textutils::bCorrectAtomName(sAtom3Temp);
if(AtomContentMap.find(ResSequence[j].first+sAtom1Temp) != AtomContentMap.end() &&
AtomContentMap.find(ResSequence[j].first+sAtom2Temp) != AtomContentMap.end() &&
AtomContentMap.find(ResSequence[j].first+sAtom3Temp) != AtomContentMap.end() )
{
Angles.push_back(TAngle(Atoms[AtomContentMap[ResSequence[j].first+sAtom1Temp]],
Atoms[AtomContentMap[ResSequence[j].first+sAtom2Temp]],
Atoms[AtomContentMap[ResSequence[j].first+sAtom3Temp]],
AngleParam(dKTheta, dThetaEq) ));
if(TConsts::TALKATIVE > 2)
std::clog << "Angle added: " << sAtom1Temp << " " << sAtom2Temp << " " << sAtom3Temp << " in res: "
<< ResSequence[j].first << std::endl;
}
else
{
std::cerr << "Warning: Angle expected, but some of the atoms are missing: " << sAtom1Temp << " " << sAtom2Temp << " " << sAtom3Temp
<< ", residue No. " << ResSequence[j].first << std::endl;
bRetVal = false; // Unknown atom name in *.angl file.
}
}
}
else
{
std::cerr << "Error: Cannot read file: " << (TConsts::ANGLEDIR+ResSequence[j].second+".angl") << std::endl;
bRetVal = false;
break; // Unknown residue is a good reason to break.
}
}
return bRetVal;
}
//---------------------------------------------------------------------------------------------------
bool TMolecule::bMakeInterAngles(std::map<std::string, int>& AtomContentMap,
const std::vector<std::pair<std::string,std::string> >& ResSequence)
{
bool bRetVal = true;
if(TConsts::TALKATIVE > 0) std::clog << "Building interresidual angles..." << std::endl;
TConsts::iAnglesInit();
for(std::list<TBond>::iterator I = Bonds.begin(); I != Bonds.end(); ++I)
{
if(I->patAtom1->sGetResIdent() != I->patAtom2->sGetResIdent()) // Interresidual bond - a good source of interresidual angles.
for(std::list<TBond>::iterator J = Bonds.begin(); J != Bonds.end(); ++J)
{
bAddAngle(*I, *J);
}
}
return bRetVal;
}
//---------------------------------------------------------------------------------------------------
bool TMolecule::bAddAngle(const TBond& _boBond1, const TBond& _boBond2)
{
bool bRetVal = true;
if((_boBond1.patAtom1 == _boBond2.patAtom1 && _boBond1.patAtom2 == _boBond2.patAtom2) ||
(_boBond1.patAtom2 == _boBond2.patAtom1 && _boBond1.patAtom1 == _boBond2.patAtom2)) bRetVal = false; //case:same bonds
else
{
TAtom *patAng1 = NULL, *patAng2 = NULL, *patAng3 = NULL;
if(_boBond1.patAtom1 == _boBond2.patAtom1)
{
patAng1 = _boBond1.patAtom2;
patAng2 = _boBond1.patAtom1;
patAng3 = _boBond2.patAtom2;
}
else if(_boBond1.patAtom1 == _boBond2.patAtom2)
{
patAng1 = _boBond1.patAtom2;
patAng2 = _boBond1.patAtom1;
patAng3 = _boBond2.patAtom1;
}
else if(_boBond1.patAtom2 == _boBond2.patAtom1)
{
patAng1 = _boBond1.patAtom1;
patAng2 = _boBond1.patAtom2;
patAng3 = _boBond2.patAtom2;
}
else if(_boBond1.patAtom2 == _boBond2.patAtom2)
{
patAng1 = _boBond1.patAtom1;
patAng2 = _boBond1.patAtom2;
patAng3 = _boBond2.patAtom1;
}
if(patAng1 != NULL && patAng2 != NULL && patAng3 != NULL)
{
bRetVal = true;
AngleParam apTemp = TConsts::GeneralAnglesData[TConsts::AtomTypeNumbers[patAng1->sGetType()]]\
[TConsts::AtomTypeNumbers[patAng2->sGetType()]]\
[TConsts::AtomTypeNumbers[patAng3->sGetType()]];
Angles.push_back(TAngle(patAng1, patAng2, patAng3, apTemp));
if(TConsts::TALKATIVE > 2)
{
std::clog << "Interresidual angle added: "
<< patAng1->sGetName() << " (res " << patAng1->sGetResIdent() << ") --- "
<< patAng2->sGetName() << " (res " << patAng2->sGetResIdent() << ") --- "
<< patAng3->sGetName() << " (res " << patAng3->sGetResIdent() << ")" << std::endl;
}
}
else bRetVal = false;
}
return bRetVal;
}
//---------------------------------------------------------------------------------------------------
bool TMolecule::bMakeIntraDihedrals(std::map<std::string, int>& AtomContentMap,
const std::vector<std::pair<std::string,std::string> >& ResSequence)
{
bool bRetVal = true;
if(TConsts::TALKATIVE > 0) std::clog << "Building intraresidual dihedrals..." << std::endl;
/*************************************************************************************
* Now follows the formation of 'intraresidual' dihedrals.
************************************************************************************/
for(unsigned int j = 0; j < ResSequence.size(); ++j)
{
std::ifstream ifsIn((TConsts::DIHEDDIR+ResSequence[j].second+".dihd").c_str());
if(ifsIn != NULL)
{
std::string sDihLine;
while(std::getline(ifsIn, sDihLine))
{
std::istringstream issIn(sDihLine);
std::string sAtom1Temp, sAtom2Temp, sAtom3Temp, sAtom4Temp;
int iDiv; double dKI, dPhase, dPN;
issIn >> sAtom1Temp >> sAtom2Temp >> sAtom3Temp >> sAtom4Temp >> iDiv >> dKI >> dPhase >> dPN;
Textutils::bCorrectAtomName(sAtom1Temp);
Textutils::bCorrectAtomName(sAtom2Temp);
Textutils::bCorrectAtomName(sAtom3Temp);
Textutils::bCorrectAtomName(sAtom4Temp);
if(AtomContentMap.find(ResSequence[j].first+sAtom1Temp) != AtomContentMap.end() &&
AtomContentMap.find(ResSequence[j].first+sAtom2Temp) != AtomContentMap.end() &&
AtomContentMap.find(ResSequence[j].first+sAtom3Temp) != AtomContentMap.end() &&
AtomContentMap.find(ResSequence[j].first+sAtom4Temp) != AtomContentMap.end() )
{
Dihedrals.push_back(TDihedral(Atoms[AtomContentMap[ResSequence[j].first+sAtom1Temp]],
Atoms[AtomContentMap[ResSequence[j].first+sAtom2Temp]],
Atoms[AtomContentMap[ResSequence[j].first+sAtom3Temp]],
Atoms[AtomContentMap[ResSequence[j].first+sAtom4Temp]],
DihedralParam(iDiv, dKI, dPhase, dPN) ));
if(TConsts::TALKATIVE > 2)
std::clog << "Dihedral added: " << sAtom1Temp << " " << sAtom2Temp << " " << sAtom3Temp << " "
<< sAtom4Temp << " in res No. " << ResSequence[j].first << std::endl;
}
else
{
std::cerr << "Warning: Dihedral expected, but some of the atoms are missing: " << sAtom1Temp << " " << sAtom2Temp << " " << sAtom3Temp
<< " " << sAtom4Temp << ", residue No. " << ResSequence[j].first << std::endl;
bRetVal = false; // Unknown atom name in *.dihd file.
}
}
}
else
{
std::cerr << "Error: cannot read file: " << (TConsts::DIHEDDIR+ResSequence[j].second+".dihd") << std::endl;
bRetVal = false;
break; // Unknown residue is a good reason to break.
}
}
return bRetVal;
}
//---------------------------------------------------------------------------------------------------
bool TMolecule::bMakeInterDihedrals(std::map<std::string, int>& AtomContentMap,
const std::vector<std::pair<std::string,std::string> >& ResSequence)
{
bool bRetVal = true;
if(TConsts::TALKATIVE > 0) std::clog << "Building interresidual dihedrals..." << std::endl;
TConsts::iDihedralsInit();
for(std::list<TBond>::iterator K = Bonds.begin(); K != Bonds.end(); ++K)
{
if(K->patAtom1->sGetResIdent() != K->patAtom2->sGetResIdent()) // Interresidual bond - a good source of interresidual dihedrals.
{
for(std::list<TAngle>::iterator L = Angles.begin(); L != Angles.end(); ++L)
{
if(L->bContains(*K))
{
for(std::list<TAngle>::iterator M = Angles.begin(); M != Angles.end(); ++M)
if(!(M->bContains(*K))) bAddDihedral(*L, *M);
for(std::list<TAngle>::iterator M = Angles.begin(); M != L; ++M)
if(M->bContains(*K)) bAddDihedral(*L, *M);
}
}
}
}
return bRetVal;
}
//---------------------------------------------------------------------------------------------------
bool TMolecule::bAddDihedral(const TAngle& _anAngle1, const TAngle& _anAngle2)
{
bool bRetVal = true;
if((_anAngle1.patAtom1 == _anAngle2.patAtom1 && _anAngle1.patAtom2 == _anAngle2.patAtom2 && _anAngle1.patAtom3 == _anAngle2.patAtom3) ||
(_anAngle1.patAtom1 == _anAngle2.patAtom3 && _anAngle1.patAtom2 == _anAngle2.patAtom2 && _anAngle1.patAtom3 == _anAngle2.patAtom1))
bRetVal = false; //the same angle
else
{
TAtom *patDih1 = NULL, *patDih2 = NULL,
*patDih3 = NULL, *patDih4 = NULL;
if( _anAngle1.patAtom1 == _anAngle2.patAtom2 && _anAngle1.patAtom2 == _anAngle2.patAtom1)
{
patDih1 = _anAngle2.patAtom3,
patDih2 = _anAngle2.patAtom2,
patDih3 = _anAngle2.patAtom1,
patDih4 = _anAngle1.patAtom3;
}
else if(_anAngle1.patAtom1 == _anAngle2.patAtom2 && _anAngle1.patAtom2 == _anAngle2.patAtom3)
{
patDih1 = _anAngle2.patAtom1,
patDih2 = _anAngle2.patAtom2,
patDih3 = _anAngle2.patAtom3,
patDih4 = _anAngle1.patAtom3;
}
else if(_anAngle1.patAtom2 == _anAngle2.patAtom1 && _anAngle1.patAtom3 == _anAngle2.patAtom2)
{
patDih1 = _anAngle1.patAtom1,
patDih2 = _anAngle2.patAtom1,
patDih3 = _anAngle2.patAtom2,
patDih4 = _anAngle2.patAtom3;
}
else if(_anAngle1.patAtom2 == _anAngle2.patAtom3 && _anAngle1.patAtom3 == _anAngle2.patAtom2)
{
patDih1 = _anAngle1.patAtom1,
patDih2 = _anAngle2.patAtom3,
patDih3 = _anAngle2.patAtom2,
patDih4 = _anAngle2.patAtom1;
}
if(patDih1 != NULL && patDih2 != NULL && patDih3 != NULL && patDih4 != NULL)
{
if(TConsts::TALKATIVE > 2)
std::clog << "Interresidual dihedral added: "
<< patDih1->sGetName() << " (res " << patDih1->sGetResIdent() << ") --- "
<< patDih2->sGetName() << " (res " << patDih2->sGetResIdent() << ") --- "
<< patDih3->sGetName() << " (res " << patDih3->sGetResIdent() << ") --- "
<< patDih4->sGetName() << " (res " << patDih4->sGetResIdent() << ")" << std::endl;
for(std::list<DihedralParam>::iterator I = TConsts::GeneralDihedralsData[TConsts::AtomTypeNumbers[patDih1->sGetType()]]\
[TConsts::AtomTypeNumbers[patDih2->sGetType()]]\
[TConsts::AtomTypeNumbers[patDih3->sGetType()]]\
[TConsts::AtomTypeNumbers[patDih4->sGetType()]].begin();
I != TConsts::GeneralDihedralsData[TConsts::AtomTypeNumbers[patDih1->sGetType()]]\
[TConsts::AtomTypeNumbers[patDih2->sGetType()]]\
[TConsts::AtomTypeNumbers[patDih3->sGetType()]]\
[TConsts::AtomTypeNumbers[patDih4->sGetType()]].end();
++I )
{
I->dPN = fabs(I->dPN); // If multiple fourier coeffs appear some of them are negative.
Dihedrals.push_back(TDihedral(patDih1, patDih2, patDih3, patDih4, *I));
}
}
else bRetVal = false;
}
return bRetVal;
}
//---------------------------------------------------------------------------------------------------
bool TMolecule::bMakeIntraImpropers(std::map<std::string, int>& AtomContentMap,
const std::vector<std::pair<std::string,std::string> >& ResSequence)
{
bool bRetVal = true;
if(TConsts::TALKATIVE > 0) std::clog << "Building intraresidual impropers..." << std::endl;
/*************************************************************************************
* Now follows the formation of 'intraresidual' impropers.
************************************************************************************/
for(unsigned int j = 0; j < ResSequence.size(); ++j)
{
std::ifstream ifsIn((TConsts::IMPRODIR+ResSequence[j].second+".impr").c_str());
if(ifsIn != NULL)
{
std::string sImprLine;
while(std::getline(ifsIn, sImprLine))
{
std::istringstream issIn(sImprLine);
std::string sAtom1Temp, sAtom2Temp, sAtom3Temp, sAtom4Temp;
double dKI, dPhase, dPN;
issIn >> sAtom1Temp >> sAtom2Temp >> sAtom3Temp >> sAtom4Temp >> dKI >> dPhase >> dPN;
Textutils::bCorrectAtomName(sAtom1Temp);
Textutils::bCorrectAtomName(sAtom2Temp);
Textutils::bCorrectAtomName(sAtom3Temp);
Textutils::bCorrectAtomName(sAtom4Temp);
if(AtomContentMap.find(ResSequence[j].first+sAtom1Temp) != AtomContentMap.end() &&
AtomContentMap.find(ResSequence[j].first+sAtom2Temp) != AtomContentMap.end() &&
AtomContentMap.find(ResSequence[j].first+sAtom3Temp) != AtomContentMap.end() &&
AtomContentMap.find(ResSequence[j].first+sAtom4Temp) != AtomContentMap.end() )
{
Impropers.push_back(TImproper(Atoms[AtomContentMap[ResSequence[j].first+sAtom1Temp]],
Atoms[AtomContentMap[ResSequence[j].first+sAtom2Temp]],
Atoms[AtomContentMap[ResSequence[j].first+sAtom3Temp]],
Atoms[AtomContentMap[ResSequence[j].first+sAtom4Temp]],
ImproperParam(dKI, dPhase, dPN) ));
if(TConsts::TALKATIVE > 2)
std::clog << "Improper added: " << sAtom1Temp << " " << sAtom2Temp << " " << sAtom3Temp << " "
<< sAtom4Temp << " in res No. " << ResSequence[j].first << std::endl;
}
else
{
std::cerr << "Warning: Improper expected, but some of the atoms are missing: " << sAtom1Temp << " " << sAtom2Temp << " " << sAtom3Temp
<< " " << sAtom4Temp << ", residue No. " << ResSequence[j].first << std::endl;
bRetVal = false; // Unknown atom name in *.impr file.
}
}
}
else
{
std::cerr << "Warning: no impropers for residue " << ResSequence[j].second << " have been predefined." << std::endl;
// Unknown residue is a NOT a reason to break, since impropers are NOT mandatory.
}
}
return bRetVal;
}
//---------------------------------------------------------------------------------------------------
bool TMolecule::bMakePosRestraints()
{
for(unsigned int i = 0; i < Atoms.size(); ++i)
{
if(Atoms[i].dPosRestr > 0.)
{
double dX0, dY0, dZ0;
Atoms[i].GetCoords(dX0, dY0, dZ0);
PosRestrs.push_back(TPosRestr(Atoms[i], dX0, dY0, dZ0, Atoms[i].dPosRestr));
}
}
return true;
}
//---------------------------------------------------------------------------------------------------
bool TMolecule::bMakeElectrosAndLennardsWithin()
{
bool bRetVal = true;
for(unsigned int i = 0; i < vvContacts.size(); ++i)
for(unsigned int j = i+1; j < vvContacts[i].size(); ++j)
{
if(vvContacts[i][j] >= 3)
{
TElectr elToAdd = elMakeElectrPair(Atoms[i], Atoms[j]);
TLennard ljToAdd = ljMakeLJPair( Atoms[i], Atoms[j]);
if(vvContacts[i][j] == 3)
{
elToAdd.Scale14();
ljToAdd.Scale14();
}
if(TConsts::ELECTR)
Electros.push_back(elToAdd);
// Now follows magic ass-factor of 5: (the same one appears in bMakeElectrosAndLennardsExtern()).
// It gives us some margin when seeking Lennard-Jones-bound atom pairs.
if(TConsts::VANDERWAALS)
if(ljToAdd.dLength_SQR() < TConsts::CUTOFF_SQR + 5.)
Lennards.push_back(ljToAdd);
}
}
return bRetVal;
}
//---------------------------------------------------------------------------------------------------
bool TMolecule::bMakeHBondsWithin()
{
bool bRetVal = true;
for(unsigned int i = 0; i < vvContacts.size(); ++i) // Possible hydrogen donors.
for(unsigned int j = 0; j < vvContacts[i].size(); ++j) // Possible hydrogen acceptors.
{
if(Textutils::bIsHydrogenDonor(Atoms[i].sGetType()) &&
Textutils::bIsHydrogenAcceptor(Atoms[j].sGetType()) &&
vvContacts[i][j] == 4)
{
for(unsigned int k = 0; k < vvContacts[i].size(); ++k) // Possible hydrogen 'parents', i.e. P-D...A
{
if(vvContacts[i][k] == 1)
{
THBond hbTemp(Atoms[k], Atoms[i], Atoms[j]);
if(hbTemp.dCalcEnergy() < THBond::dThreshold) // Energy threshold for a hydrogen bond.
{
HBonds.push_back(hbTemp);
if(TConsts::TALKATIVE > 1)
{
std::clog << "Intrachain hydrogen bond detected: "
<< Atoms[i].sGetPDB_tag().substr(0,27) << " - "
<< Atoms[j].sGetPDB_tag().substr(0,27) << std::endl;
}
break;
}
}
}
}
}
return bRetVal;
}
//---------------------------------------------------------------------------------------------------
bool TMolecule::bMakeElectrosAndLennardsExtern(TMolecule& _molMol1, TMolecule& _molMol2)
{
bool bRetVal = true;
for(unsigned int i = 0; i < _molMol1.Atoms.size(); ++i)
for(unsigned int j = 0; j < _molMol2.Atoms.size(); ++j)
{
if(TConsts::ELECTR)
TMolecule::Electros.push_back(elMakeElectrPair(_molMol1.Atoms[i], _molMol2.Atoms[j]));
// Now magic constant of 10 follows: (its another occurrence is in bMakeElectrosAndLennardsWithin()).
// It gives us some margin when defining Lennard-Jones-bound atom pairs.
if(TConsts::VANDERWAALS)
if(dDistance_SQR(_molMol1.Atoms[i], _molMol2.Atoms[j]) < TConsts::CUTOFF_SQR + 10.)
TMolecule::Lennards.push_back(ljMakeLJPair(_molMol1.Atoms[i], _molMol2.Atoms[j]));
}
return bRetVal;
}
//---------------------------------------------------------------------------------------------------
bool TMolecule::bMakeHBondsExtern(TMolecule& _molMol1, TMolecule& _molMol2)
{
bool bRetVal = true;
for(unsigned int i = 0; i < _molMol1.Atoms.size(); ++i)
for(unsigned int j = 0; j < _molMol2.Atoms.size(); ++j)
{
TAtom *patDonor = NULL,
*patAcceptor = NULL;
TMolecule* pmolDonor = NULL;
if( Textutils::bIsHydrogenDonor(_molMol1.Atoms[i].sGetType()))
{
patDonor = &(_molMol1.Atoms[i]);
pmolDonor = &_molMol1;
patAcceptor = &(_molMol2.Atoms[j]);
}
else if(Textutils::bIsHydrogenDonor(_molMol2.Atoms[j].sGetType()))
{
patDonor = &(_molMol2.Atoms[j]);
pmolDonor = &_molMol2;
patAcceptor = &(_molMol1.Atoms[i]);
}
if(patAcceptor != NULL)
if(!(Textutils::bIsHydrogenAcceptor(patAcceptor->sGetType()))) patAcceptor = NULL;
// "People always told me: be careful what you do":
if(patDonor != NULL && patAcceptor != NULL && pmolDonor != NULL && patDonor != patAcceptor)
{
// Now find the hydrogen's ``parent'' atom:
for(std::list<TBond>::iterator K = pmolDonor->Bonds.begin(); K != pmolDonor->Bonds.end(); ++K)
{
TAtom *patParent = NULL;
if( K->patAtom1 == patDonor) patParent = K->patAtom2;
else if(K->patAtom2 == patDonor) patParent = K->patAtom1;
if(patParent != NULL)
{
THBond hbTemp(patParent, patDonor, patAcceptor);
if(hbTemp.dCalcEnergy() < THBond::dThreshold) // Criterion for an H-bond to exist.
{
TMolecule::HBonds.push_back(hbTemp);
if(TConsts::TALKATIVE > 2)
{
std::clog << "Interchain hydrogen bond detected: "
<< patDonor ->sGetPDB_tag().substr(0,27) << " - "
<< patAcceptor->sGetPDB_tag().substr(0,27) << std::endl;
}
break;
}
}
}
}
}
return bRetVal;
}
//---------------------------------------------------------------------------------------------------
bool TMolecule::bMakeSprings(TMolecule& _molMol1, TMolecule& _molMol2)
{
bool bRetVal = true;
for(unsigned int i = 0; i < _molMol1.Atoms.size(); ++i)
for(unsigned int j = 0; j < _molMol2.Atoms.size(); ++j)
{
TAtom *pat1 = &(_molMol1.Atoms[i]),
*pat2 = &(_molMol2.Atoms[j]);
if(pat1 == pat2) continue;
for(unsigned int k = 0; k < TConsts::DistanceRestraintsDescriptors.size(); ++k)
{
if(TConsts::DistanceRestraintsDescriptors[k].bAtomFits(pat1) &&
TConsts::DistanceRestraintsDescriptors[k].bAtomFits(pat2) &&
!TConsts::DistanceRestraintsDescriptors[k].bIsUsed)
{
Springs.push_back(TSpring(pat1, pat2, TConsts::DistanceRestraintsDescriptors[k].bp));
TConsts::DistanceRestraintsDescriptors[k].bIsUsed = true;
break;
}
}
}
return bRetVal;
}
//---------------------------------------------------------------------------------------------------
bool TMolecule::bMakeBPRestraints(TMolecule& _molMol1, TMolecule& _molMol2)
{
for(unsigned int k = 0u; k < TConsts::BPRestraintsDescriptors.size(); ++k)
{
if(TConsts::BPRestraintsDescriptors[k].bIsUsed) continue;
std::vector<TAtom*> AtomPtrs;
bool bSthAdded1 = false;
for(unsigned int i = 0u; i < _molMol1.Atoms.size(); ++i)
{
if(TConsts::BPRestraintsDescriptors[k].bAtomFits(&_molMol1.Atoms[i]) &&
Textutils::bIsInBase(_molMol1.Atoms[i].sGetName(), _molMol1.Atoms[i].sGetResName()))
{
AtomPtrs.push_back(&_molMol1.Atoms[i]);
bSthAdded1 = true;
}
}
if(!bSthAdded1) continue;
if(&_molMol1 != &_molMol2) // Nonidentical molecules
{
for(unsigned int i = 0u; i < _molMol2.Atoms.size(); ++i)
{
if(TConsts::BPRestraintsDescriptors[k].bAtomFits(&_molMol2.Atoms[i]) &&
Textutils::bIsInBase(_molMol2.Atoms[i].sGetName(), _molMol2.Atoms[i].sGetResName()) &&
AtomPtrs.front()->sGetResIdent() != _molMol2.Atoms[i].sGetResIdent())
{
AtomPtrs.push_back(&_molMol2.Atoms[i]);
}
}
}
bool bTwoDifferent = false;
for(unsigned int i = 0u; i < AtomPtrs.size(); ++i)
{
if(AtomPtrs.front()->sGetResIdent() != AtomPtrs[i]->sGetResIdent())
{
bTwoDifferent = true;
break;
}
}
if(bTwoDifferent)
{
TMolecule::BasePairs.push_back(TBasePair(AtomPtrs));
TConsts::BPRestraintsDescriptors[k].bIsUsed = true;
}
}
return true;
}
//---------------------------------------------------------------------------------------------------
bool TMolecule::bMakeBasePairs()
{
std::vector<std::vector<THBond*> > PossibleSS; // Future candidates for secondary struct.
for(std::list<THBond>::iterator I = HBonds.begin(); I != HBonds.end(); ++I)
{
bool bAdded = false;
for(unsigned int j = 0; j < PossibleSS.size(); ++j)
{
std::string sRes1Ident = PossibleSS[j][0]->patDonor->sGetResIdent(),
sRes2Ident = PossibleSS[j][0]->patAcceptor->sGetResIdent(),
sCurrRes1Ident = I->patDonor->sGetResIdent(),
sCurrRes2Ident = I->patAcceptor->sGetResIdent();
if((sRes1Ident == sCurrRes1Ident && sRes2Ident == sCurrRes2Ident) ||
(sRes1Ident == sCurrRes2Ident && sRes2Ident == sCurrRes1Ident))
{
if(sCurrRes1Ident != sCurrRes2Ident)
{
PossibleSS[j].push_back(&(*I));
bAdded = true;
}
else std::cerr << "Internal Warning: attempt to build a trivial base pair." << std::endl;
}
}
if(!bAdded) PossibleSS.push_back(std::vector<THBond*>(1, &(*I)));
}
for(unsigned int j = 0; j < PossibleSS.size(); ++j)
{
if(PossibleSS[j].size() > 1)
{
TBasePair bpToAdd(PossibleSS[j][0], PossibleSS[j][1], (PossibleSS[j].size() > 2 ? PossibleSS[j][2] : NULL));
if(bpToAdd.dCalcEnergy() < TBasePair::dThreshold)
{
BasePairs.push_back(bpToAdd);
if(TConsts::TALKATIVE > 1)
std::clog << "H-bonded base pair detected: "
<< (PossibleSS[j][0]->patDonor->sGetResName() + PossibleSS[j][0]->patDonor->sGetResIdent())
<< "--- "
<< (PossibleSS[j][0]->patAcceptor->sGetResName() + PossibleSS[j][0]->patAcceptor->sGetResIdent())
<< std::endl;
}
}
}
return true;
}
//---------------------------------------------------------------------------------------------------
bool TMolecule::bBuildContacts(std::map<std::string, int>& AtomContentMap) const
{
bool bRetVal = true;
/******************************************************************************************
* Matrix vvContacts describes relation between pair of atoms (i,j):
* 0: i == j
* 1: i and j are separated by one bond
* 2: i and j separated by two bonds;
* 3: i and j separated by 3 bonds (so called '1-4 interaction');
* 4: i and j are separated by >= 4 bonds, interacting normally.
*****************************************************************************************
* If either two cases apply (as 1 and 3 for vicinal carbons in cyclobutane)
* then the smaller value is taken (1 in the example of cyclobutane).
*****************************************************************************************/
vvContacts = std::vector<std::vector<char> >(Atoms.size(), std::vector<char>(Atoms.size(), 4));
for(std::list<TDihedral>::const_iterator I = Dihedrals.begin(); I != Dihedrals.end(); ++I)
{
if(AtomContentMap.find(I->patAtom1->sGetResIdent()+I->patAtom1->sGetName()) == AtomContentMap.end() ||
AtomContentMap.find(I->patAtom4->sGetResIdent()+I->patAtom4->sGetName()) == AtomContentMap.end() )
{
bRetVal = false;
std::cerr << "Internal error: dihedral "
<< I->patAtom1->sGetName() << "(" << I->patAtom1->sGetResIdent() << ") -- "
<< I->patAtom2->sGetName() << "(" << I->patAtom2->sGetResIdent() << ") -- "
<< I->patAtom3->sGetName() << "(" << I->patAtom3->sGetResIdent() << ") -- "
<< I->patAtom4->sGetName() << "(" << I->patAtom4->sGetResIdent() << ") contains unknown atom(s)." << std::endl;
}
else
{
vvContacts[AtomContentMap[I->patAtom1->sGetResIdent()+I->patAtom1->sGetName()]]\
[AtomContentMap[I->patAtom4->sGetResIdent()+I->patAtom4->sGetName()]] =
vvContacts[AtomContentMap[I->patAtom4->sGetResIdent()+I->patAtom4->sGetName()]]\
[AtomContentMap[I->patAtom1->sGetResIdent()+I->patAtom1->sGetName()]] = 3;
}
}
for(std::list<TAngle>::const_iterator I = Angles.begin(); I != Angles.end(); ++I)
{
if(AtomContentMap.find(I->patAtom1->sGetResIdent()+I->patAtom1->sGetName()) == AtomContentMap.end() ||
AtomContentMap.find(I->patAtom3->sGetResIdent()+I->patAtom3->sGetName()) == AtomContentMap.end() )
{
bRetVal = false;
std::cerr << "Internal error: angle "
<< I->patAtom1->sGetName() << "(" << I->patAtom1->sGetResIdent() << ") -- "
<< I->patAtom2->sGetName() << "(" << I->patAtom2->sGetResIdent() << ") -- "
<< I->patAtom3->sGetName() << "(" << I->patAtom3->sGetResIdent() << ") contains unknown atom(s)." << std::endl;
}
else
{
vvContacts[AtomContentMap[I->patAtom1->sGetResIdent()+I->patAtom1->sGetName()]]\
[AtomContentMap[I->patAtom3->sGetResIdent()+I->patAtom3->sGetName()]] =
vvContacts[AtomContentMap[I->patAtom3->sGetResIdent()+I->patAtom3->sGetName()]]\
[AtomContentMap[I->patAtom1->sGetResIdent()+I->patAtom1->sGetName()]] = 2;
}
}
for(std::list<TBond>::const_iterator I = Bonds.begin(); I != Bonds.end(); ++I)
{
if(AtomContentMap.find(I->patAtom1->sGetResIdent()+I->patAtom1->sGetName()) == AtomContentMap.end() ||
AtomContentMap.find(I->patAtom2->sGetResIdent()+I->patAtom2->sGetName()) == AtomContentMap.end() )
{
bRetVal = false;
std::cerr << "Internal error: bond "
<< I->patAtom1->sGetName() << "(" << I->patAtom1->sGetResIdent() << ") -- "
<< I->patAtom2->sGetName() << "(" << I->patAtom2->sGetResIdent() << ") contains unknown atom(s)." << std::endl;
}
else
{
vvContacts[AtomContentMap[I->patAtom1->sGetResIdent()+I->patAtom1->sGetName()]]\
[AtomContentMap[I->patAtom2->sGetResIdent()+I->patAtom2->sGetName()]] =
vvContacts[AtomContentMap[I->patAtom2->sGetResIdent()+I->patAtom2->sGetName()]]\
[AtomContentMap[I->patAtom1->sGetResIdent()+I->patAtom1->sGetName()]] = 1;
}
}
for(unsigned int i = 0; i < vvContacts.size(); ++i)
vvContacts[i][i] = 0;
return bRetVal;
}
//---------------------------------------------------------------------------------------------------
void TMolecule::ClearContacts() const
{// Release some memory.
vvContacts.clear();
}
//---------------------------------------------------------------------------------------------------
bool TMolecule::bRecognizeResidues()
{
bool bRetVal = true;
for(unsigned int i = 0; i < Atoms.size(); ++i)
if (!(Atoms[i].bRecognizeResidue()))
bRetVal = false;
return bRetVal;
}
//---------------------------------------------------------------------------------------------------
bool TMolecule::bRecognizeNames()
{// Recognize all atoms' names given the sPDB_tag.
for(unsigned int i = 0; i < Atoms.size(); ++i)
Atoms[i].bRecognizeName();
return true;
}
//---------------------------------------------------------------------------------------------------
bool TMolecule::bRecognizeAtoms()
{
std::vector<TAtom> OnlyGoodAtoms;
bool bRetVal = true;
for(unsigned int i = 0; i < Atoms.size(); ++i)
if (Atoms[i].bRecognize())
{
OnlyGoodAtoms.push_back(Atoms[i]);
}
else
{
bRetVal = false;
std::cerr << "Error: recognition of atom " << Atoms[i].sGetPDB_tag().substr(0,27) << "... failed. Atom erased." << std::endl;
}
Atoms = OnlyGoodAtoms;
return bRetVal;
}
//---------------------------------------------------------------------------------------------------
void TMolecule::CalcBornRadii()
{// At development stage - use at your own risk
for(unsigned int i = 0; i < Atoms.size(); ++i)
Atoms[i].dBornRadius = 1./Atoms[i].dGetvdWRadius();
for(unsigned int i = 0; i < Atoms.size(); ++i)
for(unsigned int j = i; j < Atoms.size(); ++j)
{
double dAj = Atoms[j].dGetvdWRadius(), // van der Waals radius of j-th atom.
dLen_SQR = dDistance_SQR(Atoms[i], Atoms[j]), // Square of distance between atoms i and j.
dLen = sqrt(dLen_SQR), // Distance itsef from i to j.
dVal; // The correction being introduced.
if(Atoms[i].dGetvdWRadius() + dAj > dLen) // Overlap of spheres - some correction is needed (factor of 1/2).
dVal = dAj/(4.*(dLen_SQR-dAj*dAj)) - 1./(8.*dLen)*log((dLen-dAj)/(dLen+dAj));
else // No overlap - ordinary formulae apply.
dVal = dAj/(2.*(dLen_SQR-dAj*dAj)) - 1./(4.*dLen)*log((dLen-dAj)/(dLen+dAj));
Atoms[i].dBornRadius -= dVal;
Atoms[j].dBornRadius -= dVal;
}
for(unsigned int i = 0; i < Atoms.size(); ++i)
if(TConsts::bIsNan(Atoms[i].dBornRadius) || Atoms[i].dBornRadius == 0.)
Atoms[i].dBornRadius = Atoms[i].dGetvdWRadius();
else Atoms[i].dBornRadius = 1./Atoms[i].dBornRadius;
}
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
bool TMolecule::bRead(std::istream& _isIn)
{
if(!iReadAtoms(_isIn))
return false;
if(!bRecognizeResidues())
{
std::cerr << "Error: unknown residue(s). Molecule discarded." << std::endl;
Atoms.clear();
return false;
}
bRecognizeNames();
bCorrectTerminalResidueNames();
bRecognizeAtoms();
bAddMissingAtoms();
CorrectAtomNumbers();
/******************************************************************************************
* Now the AtomContentMap is created to simplify the search for pointers to given atoms.
* The index of this map is a std::string of the form: <residuenumber>+<atomname>.
* <residuenumber> is given by TAtom's method sGetResIdent().
* The insertion code is included in <residuenumber> if applicable.
*****************************************************************************************/
std::map<std::string, int> AtomContentMap;
/******************************************************************************************
* The following vector of pairs stores the residue identity (i.e. chain letter +
* res. number) and the residue name. Will be of use when creating bonds, angles,
* impopers and dihedrals.
*
* Both references to ResSequence and AtomContentMap are passed to appropriate functions
* (bMakeBonds, bMakeAngles, etc.)
*****************************************************************************************/
std::vector<std::pair<std::string,std::string> > ResSequence;
if(!(Atoms.empty())) ResSequence.push_back(std::make_pair(Atoms[0].sGetResIdent(),Atoms[0].sGetResName()));
for(unsigned int i = 0; i < Atoms.size(); ++i)
{
if(AtomContentMap.find(Atoms[i].sGetResIdent()+Atoms[i].sGetName()) == AtomContentMap.end())
AtomContentMap[Atoms[i].sGetResIdent()+Atoms[i].sGetName()] = i;
else std::cerr << "Error: coincidence of atom names: in residue " << Atoms[i].sGetResIdent()
<< " name " << Atoms[i].sGetName() << " is ambiguous." << std::endl;
if(Atoms[i].sGetResIdent() != ResSequence.back().first)
ResSequence.push_back(std::make_pair(Atoms[i].sGetResIdent(), Atoms[i].sGetResName()));
}
std::clog << "Number of atoms: " << Atoms.size() << std::endl
<< "Number of residues: " << ResSequence.size() << std::endl;
bool bRetIntraBonds = bMakeIntraBonds( AtomContentMap, ResSequence),
bRetIntraAngles = bMakeIntraAngles( AtomContentMap, ResSequence),
bRetIntraDihedrals = bMakeIntraDihedrals(AtomContentMap, ResSequence),
bRetIntraImpropers = bMakeIntraImpropers(AtomContentMap, ResSequence),
bRetInterBonds = bMakeInterBonds( AtomContentMap, ResSequence),