forked from pemsley/coot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathng.cc
2434 lines (2026 loc) · 110 KB
/
ng.cc
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
/*
* ideal/ng.cc
*
* Copyright 2019 by Medical Research Council
* Author: Paul Emsley
*
* This file is part of Coot
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* 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.
*
* You should have received a copies of the GNU General Public License and
* the GNU Lesser General Public License along with this program; if not,
* write to the Free Software Foundation, Inc., 51 Franklin Street,
* Fifth Floor, Boston, MA, 02110-1301, USA.
* See http://www.gnu.org/licenses/
*/
#include <iomanip>
#include "compat/coot-sysdep.h"
#include "simple-restraint.hh"
#include "coot-utils/contacts-by-bricks.hh"
#include "coot-utils/stack-and-pair.hh"
int
coot::restraints_container_t::make_restraints_ng(int imol,
const coot::protein_geometry &geom,
coot::restraint_usage_Flags flags_in,
bool do_residue_internal_torsions,
bool do_trans_peptide_restraints,
float rama_plot_target_weight,
bool do_rama_plot_restraints,
bool do_auto_helix_restraints,
bool do_auto_strand_restraints,
bool do_auto_h_bond_restraints,
coot::pseudo_restraint_bond_type sec_struct_pseudo_bonds,
bool do_link_restraints,
bool do_flank_restraints) {
// std::cout << ".................. make_restraints_ng() --- start --- n_atoms: " << n_atoms << std::endl;
// debug_sets();
bool console_output_for_restraints_generation_timings = false;
if (! thread_pool_p) {
std::cout << "ERROR:: " << __FUNCTION__ << " --- thread pool was not set! ---------"
<< std::endl;
std::cout << "ERROR:: make_restraints_ng() stops before it starts" << std::endl;
return -1;
}
auto tp_0 = std::chrono::high_resolution_clock::now();
restraints_usage_flag = flags_in;
bool just_chirals = false;
if (restraints_usage_flag == coot::CHIRAL_VOLUMES) just_chirals = true;
rama_plot_weight = rama_plot_target_weight;
if (n_atoms > 0) {
mark_OXT(geom);
make_monomer_restraints(imol, geom, do_residue_internal_torsions);
auto tp_1 = std::chrono::high_resolution_clock::now();
// This should be a set, not a vector, i.e.
// std::map<mmdb::Residue *, std::set<mmdb::Residue *> > residue_link_set_map;
// Fix that in the lab.
std::map<mmdb::Residue *, std::vector<mmdb::Residue *> > residue_link_vector_map;
// This should be a trivial class (that also contains the link type)
// not a pair (you can keep the variable name though)
#if 0
// Heres's a clue: We should add them both ways when they are made,
// so it's cleaner to check if they are there later on in the code (say,
// rama code)
//
class linked_residue_pair {
public:
mmdb::Residue *r1;
mmdb::Residue *r2;
std::string link_type;
linked_residue_pair(mmdb::Residue *r1_in, mmdb::Residue *r2_in) : r1(r1_in), r2(r2_in) {}
linked_residue_pair(mmdb::Residue *r1_in, mmdb::Residue *r2_in, const std::string &s) : r1(r1_in), r2(r2_in), link_type(s) {}
bool match_p(mmdb::Residue *test_pair_1, mmdb::Residue *test_pair_2) const {
if (r1 == test_pair_1)
if (r2 == test_pair_2)
return true;
return false;
}
// this class will be used in a std::map, so it needs operator==() and
// operator<()
bool operator==(const linked_residue_pair &lrp) {
return match_p(lrp.r1, lrp.r2);
}
bool operator<(const linked_residue_pair &lrp) {
return (lrp.r1 < r1);
}
};
#endif
std::set<std::pair<mmdb::Residue *, mmdb::Residue *> > residue_pair_link_set;
// make_link_restraints_ng makes flanking residues restraints also
if (residues_vec.size() > 0) // always true?
make_link_restraints_ng(geom,
do_rama_plot_restraints, do_trans_peptide_restraints,
&residue_link_vector_map, // fill this
&residue_pair_link_set); // fill this
auto tp_2 = std::chrono::high_resolution_clock::now();
auto tp_3 = std::chrono::high_resolution_clock::now();
if (! just_chirals)
raic.init(restraints_vec);
auto tp_4 = std::chrono::high_resolution_clock::now();
// the non-bonded contact atoms at the end of the atoms array
// don't have (forward) neighbours, so we don't want to
// calculate NBC restraints for them (and don't use
// them when splitting non-bonded contacts into range sets).
//
if (! just_chirals)
non_bonded_contacts_atom_indices.resize(n_atoms_limit_for_nbc);
// the non-threaded version has a different limit on the
// non_bonded_contacts_atom_indices (so, out of range if you use it?)
if (! thread_pool_p) {
std::cout << "ERROR:: ------- " << __FUNCTION__ << " - thread pool was not set! ---------"
<< std::endl;
// and yet we continue... that's bad news.
std::cout << "ERROR:: Bad things will now happen" << std::endl;
}
if (! just_chirals)
make_non_bonded_contact_restraints_using_threads_ng(imol, geom);
auto tp_5 = std::chrono::high_resolution_clock::now();
if (do_rama_plot_restraints)
make_rama_plot_restraints_ng(residue_link_vector_map, residue_pair_link_set, geom);
if (console_output_for_restraints_generation_timings) {
auto d10 = std::chrono::duration_cast<std::chrono::milliseconds>(tp_1 - tp_0).count();
auto d21 = std::chrono::duration_cast<std::chrono::milliseconds>(tp_2 - tp_1).count();
auto d32 = std::chrono::duration_cast<std::chrono::milliseconds>(tp_3 - tp_2).count();
auto d43 = std::chrono::duration_cast<std::chrono::milliseconds>(tp_4 - tp_3).count();
auto d54 = std::chrono::duration_cast<std::chrono::milliseconds>(tp_5 - tp_4).count();
std::cout << "------------------ timings: for make_restraints_ng(): monomers: "
<< d10 << " links: " << d21 << " flank: " << d32 << " raic: " << d43 << " nbc: " << d54
<< " milliseconds " << std::endl;
}
// probably not needed because plane restraints don't come in a bunch (I don't know why)
// disperse_plane_restraints();
if (! just_chirals) {
if (residues_vec.size() > 1)
if (sec_struct_pseudo_bonds == coot::HELIX_PSEUDO_BONDS)
make_helix_pseudo_bond_restraints();
if (residues_vec.size() > 1)
if (sec_struct_pseudo_bonds == coot::STRAND_PSEUDO_BONDS)
make_strand_pseudo_bond_restraints();
if (residues_vec.size() > 1)
if (do_auto_helix_restraints)
make_helix_pseudo_bond_restraints_from_res_vec_auto();
if (residues_vec.size() > 1)
if (do_auto_h_bond_restraints)
make_h_bond_restraints_from_res_vec_auto(geom, imol);
if (residues_vec.size() > 1)
make_base_pairing_and_stacking_restraints_ng(imol, geom);
}
make_df_restraints_indices();
make_distortion_electron_density_ranges();
// Now we don't do this here - we do it after all the restraints have been added -
// called from graphics-info-modelling.cc
//
// std::cout << "DEBUG:: make-restraints(): analysis of bad geometry in input model" << std::endl;
// analyze_for_bad_restraints(); // bonds and non-bonded.
// info(); - are the NBCs correct?
}
for (unsigned int i=0; i<restraints_vec.size(); i++)
restraints_vec[i].restraint_index = i;
return size();
}
void
coot::restraints_container_t::make_rama_plot_restraints_ng(const std::map<mmdb::Residue *, std::vector<mmdb::Residue *> > &residue_link_vector_map,
const std::set<std::pair<mmdb::Residue *, mmdb::Residue *> > &residue_pair_link_set,
const protein_geometry &geom) {
bool debug = false;
// std::cout << "############################ debug ######################### " << std::endl;
// std::cout << "################# make_rama_plot_restraints_ng() ########### " << std::endl;
std::map<mmdb::Residue *, std::vector<mmdb::Residue *> >::const_iterator it_map;
if (debug) {
for (it_map=residue_link_vector_map.begin(); it_map != residue_link_vector_map.end(); ++it_map) {
mmdb::Residue *key = it_map->first;
std::cout << "key " << residue_spec_t(key) << " link vector map: " << std::endl;
const std::vector<mmdb::Residue *> &v = it_map->second;
for (unsigned int i=0; i<v.size(); i++) {
std::cout << " " << i << " " << residue_spec_t(v[i]) << std::endl;
}
}
std::set<std::pair<mmdb::Residue *, mmdb::Residue *> >::const_iterator it_set;
for (it_set=residue_pair_link_set.begin(); it_set!=residue_pair_link_set.end(); ++it_set) {
mmdb::Residue *r_1 = it_set->first;
mmdb::Residue *r_2 = it_set->second;
std::cout << " paired: " << residue_spec_t(r_1) << " " << residue_spec_t(r_2) << std::endl;
}
}
// of course residues_vec is sorted by now
int n_vec_rama_residues = residues_vec.size() -1; // the residue at the end will not have the atoms
// for the next residue
if (residues_vec.size() > 2) {
for (int i=1; i<n_vec_rama_residues; i++) {
mmdb::Residue *residue_prev_p = residues_vec[i-1].second;
mmdb::Residue *residue_this_p = residues_vec[i ].second;
mmdb::Residue *residue_next_p = residues_vec[i+1].second;
bool f1 = residues_vec[i-1].first; // fixed flags
bool f2 = residues_vec[i ].first;
bool f3 = residues_vec[i+1].first;
if (f1 && f2 && f3)
continue;
// So there are some moving atoms if we are here
int index_p = residue_prev_p->index;
int index_t = residue_this_p->index;
int index_n = residue_next_p->index;
if (false)
std::cout << "residues "
<< residue_spec_t(residue_prev_p) << " "
<< residue_spec_t(residue_this_p) << " "
<< residue_spec_t(residue_next_p) << " "
<< "residue indices: " << index_p << " " << index_t << " " << index_n << std::endl;
if ((index_t-index_p) == 1) {
if ((index_n-index_t) == 1) {
// OK so the residues are next to each other in the chain.
// Are they protein?
std::string rn_1(residue_prev_p->GetResName());
std::string rn_2(residue_this_p->GetResName());
std::string rn_3(residue_next_p->GetResName());
if (util::is_standard_amino_acid_name(rn_1)) {
if (util::is_standard_amino_acid_name(rn_2)) {
if (util::is_standard_amino_acid_name(rn_3)) {
// rama restraints should not be added to CIS, PCIS. Only TRANS and PTRANS
// were they linked by peptide bonds?
mmdb::Residue *key = residue_this_p;
it_map = residue_link_vector_map.find(key);
if (it_map != residue_link_vector_map.end()) {
const std::vector<mmdb::Residue *> &v = it_map->second;
std::vector<mmdb::Residue *>::const_iterator it_vec_prev;
std::vector<mmdb::Residue *>::const_iterator it_vec_next;
it_vec_prev = std::find(v.begin(), v.end(), residue_prev_p);
it_vec_next = std::find(v.begin(), v.end(), residue_next_p);
if (it_vec_prev != v.end()) {
if (it_vec_next != v.end()) {
// OK, they were linked by peptide bonds
std::string link_type = find_peptide_link_type_ng(residue_prev_p, residue_this_p, geom);
if (link_type == "TRANS" || link_type == "PTRANS") {
rama_triple_t triple(residue_prev_p, residue_this_p, residue_next_p, link_type, f1, f2, f3);
add_rama(triple, geom);
}
}
}
}
}
}
}
}
}
}
}
}
void
coot::restraints_container_t::make_base_pairing_and_stacking_restraints_ng(int imol, const coot::protein_geometry &geom) {
bool console_output_for_restraints_generation_timings = false;
auto tp_6 = std::chrono::high_resolution_clock::now();
stack_and_pair sp(mol, residues_vec);
std::vector<stack_and_pair::stacked_planes_info_t> stacked_residues = sp.stacked_residues(mol);
// using spec indirection - this is not fast
extra_restraints_t extra_restraints;
for (std::size_t i=0; i<stacked_residues.size(); i++) {
parallel_planes_t ppr(residue_spec_t(stacked_residues[i].res_1),
residue_spec_t(stacked_residues[i].res_2),
stacked_residues[i].atom_names_1,
stacked_residues[i].atom_names_2,
"", "");
extra_restraints.parallel_plane_restraints.push_back(ppr);
}
auto tp_7 = std::chrono::high_resolution_clock::now();
// base pairs:
bool all_atoms_are_moving_flag = false; // can be true in the future.
std::vector<stack_and_pair::paired_residues_info_t> pr =
sp.paired_residues(mol, residues_vec, all_atoms_are_moving_flag, geom, imol);
auto tp_8 = std::chrono::high_resolution_clock::now();
unsigned int n_base_pairing_bonds = 0;
for (std::size_t i=0; i<pr.size(); i++) {
for (std::size_t j=0; j<pr[i].atom_pair_vec.size(); j++) {
mmdb::Atom *at_1 = pr[i].atom_pair_vec[j].first;
mmdb::Atom *at_2 = pr[i].atom_pair_vec[j].second;
int index_1 = -1;
int index_2 = -1;
at_1->GetUDData(udd_atom_index_handle, index_1);
at_2->GetUDData(udd_atom_index_handle, index_2);
std::vector<bool> fixed_flags = make_fixed_flags(index_1, index_2);
if (!fixed_flags[0] || !fixed_flags[1]) {
// This should be hydrogen bond type (whatever that means)
// the target distance depends on the pair and should be calculated in
// paired_residues and made part of the atom "pair" (-> tuple/class)
//
// typical values: 2.92, 2.83, 2.87
add(BOND_RESTRAINT, index_1, index_2, fixed_flags, 2.88, 0.08, 1.2);
n_base_pairing_bonds++;
}
}
}
if (console_output_for_restraints_generation_timings)
std::cout << "INFO:: Made " << n_base_pairing_bonds << " base pairing Hydrogen bonds"
<< std::endl;
auto tp_9 = std::chrono::high_resolution_clock::now();
if (extra_restraints.has_restraints())
add_extra_restraints(imol, "from make_base_pairing_and_stacking_restraints_ng()", extra_restraints, geom);
if (console_output_for_restraints_generation_timings) {
auto tp_10 = std::chrono::high_resolution_clock::now();
auto d76 = std::chrono::duration_cast<std::chrono::milliseconds>(tp_7 - tp_6).count();
auto d87 = std::chrono::duration_cast<std::chrono::milliseconds>(tp_8 - tp_7).count();
auto d98 = std::chrono::duration_cast<std::chrono::milliseconds>(tp_9 - tp_8).count();
auto d109 = std::chrono::duration_cast<std::chrono::milliseconds>(tp_10 - tp_9).count();
std::cout << "------------------ timings: for make_restraints_ng(): stacking and pairing: "
<< d76 << " " << d87 << " " << d98 << " " << d109 << " ms" << std::endl;
}
}
// residue_link_count_map should be residue_link_info_map - the data of the map should be
// a vector of linked residues (we can get the index of those residues if needed)
// but more importantly perhaps, filter them out of the residues near the A and C residues
// of our (say) A-B-C residue triplet.
//
void
coot::restraints_container_t::make_flanking_atoms_restraints_ng(const coot::protein_geometry &geom,
std::map<mmdb::Residue *, std::vector<mmdb::Residue *> > *residue_link_vector_map_p,
std::set<std::pair<mmdb::Residue *, mmdb::Residue *> > *residue_pair_link_set_p,
bool do_rama_plot_restraints,
bool do_trans_peptide_restraints) {
bool debug = false;
link_restraints_counts flank_restraints("flank");
// residue 20 is being refined, we need to make flanking link restraints to residues 19 and 21 using
// fixed_neighbours_set
//
// residues_vec contains residue 20
//
// fixed_neighbours_set are the (fixed) neighbours of residue 20.
//
// mol contains residue 20 and the residues around it (made by the function that
// creates the restraints object)
if (debug) { // debugging
std::cout << "########## make_flanking_atoms_restraints_ng() residue_link_count_map size "
<< residue_link_vector_map_p->size() << std::endl;
std::map<mmdb::Residue *, std::vector<mmdb::Residue *> >::const_iterator it;
for (it=residue_link_vector_map_p->begin();
it!=residue_link_vector_map_p->end(); it++) {
std::cout << " residue_link_vector_map key: "
<< residue_spec_t(it->first) << " " << std::endl;
}
}
if (debug) {
std::cout << "####### make_flanking_atoms_restraints_ng() debugging fixed_neighbours_set() " << std::endl;
std::map<mmdb::Residue *, std::set<mmdb::Residue *> >::const_iterator it;
for (it=fixed_neighbours_set.begin(); it!=fixed_neighbours_set.end(); it++) {
mmdb::Residue *residue_p = it->first;
std::cout << "\n\n...fixed-neighbour for residue " << residue_spec_t(residue_p) << " index " << residue_p->index << std::endl;
const std::set<mmdb::Residue *> &s = it->second;
std::set<mmdb::Residue *>::const_iterator its;
for (its=s.begin(); its!=s.end(); its++) {
mmdb::Residue *neighb = *its;
std::cout << " neighb: " << residue_spec_t(neighb) << " " << neighb << " index " << neighb->index << std::endl;
}
}
std::cout << "####### done make_flanking_atoms_restraints_ng() debugging fixed_neighbours_set() " << std::endl;
}
int idx_reference_index_handle = mol->GetUDDHandle(mmdb::UDR_RESIDUE, "index from reference residue");
std::map<mmdb::Residue *, std::set<mmdb::Residue *> >::const_iterator it;
for (it=fixed_neighbours_set.begin(); it!=fixed_neighbours_set.end(); it++) {
mmdb::Residue *residue_p = it->first;
unsigned int n_link_for_residue = 0;
std::map<mmdb::Residue *, std::vector<mmdb::Residue *> >::const_iterator itm = residue_link_vector_map_p->find(residue_p);
if (itm != residue_link_vector_map_p->end()) {
n_link_for_residue = itm->second.size();
}
// was it polymer-linked at both ends? (we've only made _polymer_ links so far)
//
if (n_link_for_residue < 2) {
int index_for_residue;
residue_p->GetUDData(idx_reference_index_handle, index_for_residue);
const std::set<mmdb::Residue *> &s = it->second;
std::set<mmdb::Residue *>::const_iterator its;
for (its=s.begin(); its!=s.end(); its++) {
mmdb::Residue *neighb = *its;
// Let's say that we refine residue 21,22,23.
// Residue 21 needs to be flanking-linked to residue 20.
// fixed_neighbours_set can contain residue
// 22 as a neighbour of 21. We don't want to link 22 to 21 here.
// because we have done it in the polymer function. Let's
// see if we can find 21-22 here in residue_link_vector_map.
//
// If we are refining just one residue, there will be nothing in the
// residue_link_vector_map, so only skip this one (continue)
// if we can find the key and the neighb is in the vector (set) of
// residues to whicih this residue is already linked.
if (neighb->chain != residue_p->chain) continue;
// this kills links for insertion codes, but also kills links
// across gaps (this is the feature that we want).
//
int rn_1 = neighb->GetSeqNum();
int rn_2 = residue_p->GetSeqNum();
int rn_delta = abs(rn_2 - rn_1);
if (rn_delta != 1) {
std::string ins_1(residue_p->GetInsCode());
std::string ins_2(neighb->GetInsCode());
if (ins_1.empty())
if (ins_2.empty())
continue;
}
std::map<mmdb::Residue *, std::vector<mmdb::Residue *> >::const_iterator itm;
itm = residue_link_vector_map_p->find(residue_p);
if (itm != residue_link_vector_map_p->end())
if (std::find(itm->second.begin(), itm->second.end(), neighb) != itm->second.end())
continue;
int index_delta = neighb->index - residue_p->index;
if (index_delta == -1 || index_delta == 1) {
// std::cout << " fixed neigb: " << residue_spec_t(*its) << std::endl;
std::pair<bool, mmdb::Residue *> pair_1(true, neighb);
std::pair<bool, mmdb::Residue *> pair_2(false, residue_p);
// if this is a link onto the N of this residue, then we need
// to call try_make_peptide_link_ng with the arguments the other way
// around
//
if (index_delta == 1) std::swap(pair_1, pair_2);
if (debug)
std::cout << " making flanking-peptide link: "
<< residue_spec_t(pair_1.second) << " fixed: " << pair_1.first << " "
<< residue_spec_t(pair_2.second) << " fixed: " << pair_2.first << " "
<< std::endl;
std::pair<bool, link_restraints_counts> link_result =
try_make_peptide_link_ng(geom,
pair_1,
pair_2,
do_trans_peptide_restraints);
if (! link_result.first)
link_result = try_make_phosphodiester_link_ng(geom, pair_1, pair_2);
if (link_result.first) {
flank_restraints.add(link_result.second);
(*residue_link_vector_map_p)[residue_p].push_back(neighb);
(*residue_link_vector_map_p)[neighb ].push_back(residue_p);
std::pair<mmdb::Residue *, mmdb::Residue *> p1(residue_p, neighb);
std::pair<mmdb::Residue *, mmdb::Residue *> p2(neighb, residue_p);
residue_pair_link_set_p->insert(p1);
residue_pair_link_set_p->insert(p2);
}
}
}
}
}
// flank_restraints.report();
}
// static
void
coot::restraints_container_t::make_non_bonded_contact_restraints_workpackage_ng(int ithread,
int imol,
const coot::protein_geometry &geom,
const std::vector<std::set<int> > &bonded_atom_indices,
const reduced_angle_info_container_t &raic,
const std::vector<std::set<unsigned int> > &vcontacts,
std::pair<unsigned int, unsigned int> atom_index_range_pair,
const std::set<int> &fixed_atom_indices,
const std::vector<std::string> &energy_type_for_atom,
bool extended_atom_mode,
mmdb::PPAtom atom,
const std::vector<bool> &atom_is_metal,
const std::vector<bool> &atom_is_hydrogen,
const std::vector<bool> &H_atom_parent_atom_is_donor_vec,
const std::vector<bool> &atom_is_acceptor_vec,
std::vector<std::set<int> > *non_bonded_contacts_atom_indices_p,
std::vector<simple_restraint> *nbc_restraints_fragment_p,
std::atomic<unsigned int> &done_count) {
if (false) {
get_print_lock();
std::cout << "H_atom_parent_atom_is_donor_vec vector size " << H_atom_parent_atom_is_donor_vec.size() << std::endl;
for (unsigned int i=0; i<H_atom_parent_atom_is_donor_vec.size(); i++)
std::cout << "H_atom_parent_atom_is_donor_vec " << i << " " << H_atom_parent_atom_is_donor_vec[i] << std::endl;
release_print_lock();
}
// make_fixed_flags() will have to be done in place using fixed_atom_indices
// think about is_in_same ring without a cache. Hmm.
// pointer to reference
std::vector<std::set<int> > &non_bonded_contacts_atom_indices = *non_bonded_contacts_atom_indices_p;
std::map<std::string, std::pair<bool, std::vector<std::list<std::string> > > > residue_ring_map_cache;
// bool extended_atom_mode = false; // turn this on if there are no Hydrogen atoms in the model
// extended_atom_mode = ! model_has_hydrogen_atoms;
// there is no need to modify a pointer. It can return a value
//
auto tweak_neighbours_for_rn_diff_is_1 = [] (const std::vector<bool> &fixed_atom_flags,
const std::string &atom_name_1,
const std::string &atom_name_2,
const int &res_no_1,
const int &res_no_2,
bool second_is_pro,
double *dist_min_p) {
double &dist_min(*dist_min_p);
bool strange_exception = false;
if (fixed_atom_flags.size()) {
if (fixed_atom_flags[0] || fixed_atom_flags[1]) {
if (atom_name_1 == " O ")
if (atom_name_2 == " CA ")
strange_exception = true;
if (atom_name_1 == " CA ")
if (atom_name_2 == " O ")
strange_exception = true;
if (atom_name_1 == " N ")
if (atom_name_2 == " CB ")
strange_exception = true;
if (atom_name_1 == " CB ")
if (atom_name_2 == " N ")
strange_exception = true;
if (atom_name_1 == " C ")
if (atom_name_2 == " CB ")
strange_exception = true;
}
}
if (strange_exception)
dist_min = 2.7;
// Strange that these are not marked as 1-4 related. Fix here...
// HA-CA-N-C can be down to ~2.4A.
// HA-CA-C-N can be down to ~2.41A.
if (res_no_2 > res_no_1) {
if (atom_name_1 == " C ") {
if (atom_name_2 == " HA " || atom_name_2 == "HA2" || atom_name_2 == " HA3") {
strange_exception = true;
dist_min = 2.4;
}
}
if (atom_name_1 == " HA " || atom_name_1 == "HA2" || atom_name_1 == " HA3") {
if (atom_name_2 == " N ") {
strange_exception = true;
dist_min = 2.41;
}
}
// Another 1-4:
// N-terminal hydrogens should not have NBCs to C of next residue
if (atom_name_2 == " C ") {
if (atom_name_1 == " H1 " || atom_name_1 == " H2 " || atom_name_1 == " H3 ") {
dist_min = 2.15;
}
}
if (atom_name_1 == " N ") {
if (atom_name_2 == " H ") {
strange_exception = true;
dist_min = 2.4;
}
}
// CA(n) - CD(n+1) for XXX-PRO needs to be reduced by 0.8 or so
if (second_is_pro) {
if (atom_name_1 == " CA ") {
if (atom_name_2 == " CD ") {
dist_min = 3.1;
}
}
}
} else {
if (atom_name_1 == " HA " || atom_name_1 == "HA2" || atom_name_1 == " HA3") {
if (atom_name_2 == " C ") {
strange_exception = true;
dist_min = 2.4;
}
}
if (atom_name_1 == " N ") {
if (atom_name_2 == " HA " || atom_name_2 == "HA2" || atom_name_2 == " HA3") {
strange_exception = true;
dist_min = 2.41;
}
}
if (atom_name_2 == " N ") {
if (atom_name_1 == " H ") {
strange_exception = true;
dist_min = 2.4;
}
}
}
};
auto tweak_neighbours_for_rn_diff_is_2 = [] (const std::vector<bool> &fixed_atom_flags,
const std::string &atom_name_1,
const std::string &atom_name_2,
double *dist_min_p) {
double &dist_min(*dist_min_p);
bool strange_exception = false;
if (fixed_atom_flags.size()) {
if (fixed_atom_flags[0] || fixed_atom_flags[1]) {
if (atom_name_1 == " C ")
if (atom_name_2 == " N ")
strange_exception = true;
if (atom_name_1 == " N ")
if (atom_name_2 == " C ")
strange_exception = true; // 3.1 would be enough
if (strange_exception)
dist_min = 2.7;
}
}
return dist_min;
};
// note to self: this is the multi-threaded version
auto debug_print = [energy_type_for_atom] (const std::string &remark,
int i, int j, mmdb::Atom *at_1, mmdb::Atom *at_2,
const std::vector<bool> &fixed_atom_flags, double dist_min) {
get_print_lock();
std::cout << "Adding NBC: " << remark << " " << std::setw(4) << i << " " << std::setw(4) << j << " "
<< atom_spec_t(at_1) << " " << atom_spec_t(at_2) << " types: "
<< std::setw(4) << energy_type_for_atom[i] << " "
<< std::setw(4) << energy_type_for_atom[j]
<< " fixed-flags: " << fixed_atom_flags[0] << " " << fixed_atom_flags[1]
<< " dist-min: " << dist_min << "\n";
release_print_lock();
};
for (unsigned int i=atom_index_range_pair.first; i<atom_index_range_pair.second; i++) {
mmdb::Atom *at_1 = atom[i];
if (! at_1) {
std::cout << "ERROR:: make_non_bonded_contact_restraints_workpackage_ng()"
<< " null atom at index " << i << " in range " << atom_index_range_pair.first
<< " " << atom_index_range_pair.second << " with n_atoms (bonded_atom_indices size()) "
<< bonded_atom_indices.size() << std::endl;
continue;
}
if (at_1->isTer()) continue;
const std::set<unsigned int> &n_set = vcontacts[i];
std::string alt_conf_1(at_1->altLoc);
// std::cout << "base atom: " << atom_spec_t(at_1) << std::endl;
// std::cout << "Here with i " << i << " which has " << n_set.size() << " neighbours " << std::endl;
std::set<unsigned int>::const_iterator it;
for (it=n_set.begin(); it!=n_set.end(); ++it) {
const unsigned int &j = *it;
if (j < i) /* only add NBC one way round */
continue;
if (bonded_atom_indices[i].find(j) != bonded_atom_indices[i].end())
continue;
// for updating non-bonded contacts
if (non_bonded_contacts_atom_indices[i].find(j) != non_bonded_contacts_atom_indices[i].end())
continue;
mmdb::Atom *at_2 = atom[j];
std::string alt_conf_2(at_2->altLoc);
{
bool at_1_is_fixed_flag = false;
bool at_2_is_fixed_flag = false;
if (fixed_atom_indices.find(i) != fixed_atom_indices.end()) at_1_is_fixed_flag = true;
if (fixed_atom_indices.find(j) != fixed_atom_indices.end()) at_2_is_fixed_flag = true;
if (false)
std::cout << "make_non_bonded_contact_restraints_workpackage_ng considering "
<< atom_spec_t(at_1) << " " << atom_spec_t(at_2) << " : "
<< fixed_atom_indices.size() << " fixed flags: " << at_1_is_fixed_flag << " " << at_2_is_fixed_flag
<< std::endl;
}
if (!alt_conf_1.empty()) // alt confs don't see each other
if (!alt_conf_2.empty())
if (alt_conf_1 != alt_conf_2)
continue;
if (fixed_atom_indices.find(i) != fixed_atom_indices.end())
if (fixed_atom_indices.find(j) != fixed_atom_indices.end())
continue;
std::string res_name_1 = at_1->GetResName();
std::string res_name_2 = at_2->GetResName();
int res_no_1 = at_1->GetSeqNum();
int res_no_2 = at_2->GetSeqNum();
bool second_is_pro = false;
if (res_name_2 == "PRO") second_is_pro = true; // residues are sorted and j > i
if (res_name_2 == "HYP") second_is_pro = true;
std::string element_1 = at_1->element;
std::string element_2 = at_2->element;
const std::string &type_1 = energy_type_for_atom[i];
const std::string &type_2 = energy_type_for_atom[j];
// std::vector<bool> fixed_atom_flags = make_fixed_flags(i, j);
std::vector<bool> fixed_atom_flags(2, false);
if (fixed_atom_indices.find(i) != fixed_atom_indices.end()) fixed_atom_flags[0] = true;
if (fixed_atom_indices.find(j) != fixed_atom_indices.end()) fixed_atom_flags[1] = true;
double dist_min = 3.4;
bool in_same_residue_flag = (at_1->residue == at_2->residue);
bool in_same_ring_flag = true;
// part of this test is not needed.
if (at_2->residue != at_1->residue) {
in_same_ring_flag = false;
in_same_residue_flag = false;
}
std::string atom_name_1(at_1->GetAtomName());
std::string atom_name_2(at_2->GetAtomName());
if (in_same_ring_flag)
in_same_ring_flag = is_in_same_ring(imol, at_2->residue, residue_ring_map_cache,
atom_name_1, atom_name_2, geom);
// this doesn't check 1-4 over a moving->non-moving peptide link (see comment above function)
// because the non-moving atom doesn't have angle restraints.
//
// 20191122-PE It should do now because we have bond restraints in a raic (and other fixes).
//
bool is_1_4_related = raic.is_1_4(i, j, fixed_atom_flags);
if (false) { // debug 1-4s
get_print_lock();
std::cout << "make_non_bonded_contact_restraints_workpackage_ng() here with at_1 " << atom_spec_t(at_1)
<< " at_2 " << atom_spec_t(at_2) << " is_1_4_related: " << is_1_4_related << std::endl;
release_print_lock();
}
bool mc_atoms_tandem = false;
bool mc_CC_atoms_tandem = false;
if (! is_1_4_related) {
// hacky case for C in a helix. Also N.
// (because fixed atoms don't have angle restraints - so raic.is_1_4_related()
// will not work)
if (atom_name_1 == " C ")
if (atom_name_2 == " C ")
if (at_2->residue->index - at_1->residue->index == 1) {
mc_atoms_tandem = true;
mc_CC_atoms_tandem = true;
}
if (atom_name_1 == " N ")
if (atom_name_2 == " N ")
if (at_2->residue->index - at_1->residue->index == -1)
mc_atoms_tandem = true;
// down-weight CA-CA in cis-peptide bonds
if (atom_name_1 == " CA ")
if (atom_name_2 == " CA ")
if (true) // cis-peptide test
mc_atoms_tandem = true;
}
if (mc_atoms_tandem)
is_1_4_related = true;
if (is_1_4_related) {
if (mc_atoms_tandem) dist_min = 2.99;
if (in_same_ring_flag) {
dist_min = 2.64; // was 2.7 but c.f. guanine ring distances
} else {
if (mc_CC_atoms_tandem) {
dist_min = 3.05; // in a helix, more elsewhere.
} else {
dist_min = 2.82; // N-N, but captures all others, including H-H
}
}
if (atom_is_hydrogen[i] && atom_is_hydrogen[j]) {
dist_min = 2.42; // shold depend on energy type
} else {
if (atom_is_hydrogen[i])
dist_min -= 0.6; // was 0.4
if (atom_is_hydrogen[j])
dist_min -= 0.6; // was 0.4
}
} else {
// not 1-4 related
std::pair<bool, double> nbc_dist = geom.get_nbc_dist_v2(type_1, type_2,
element_1, element_2,
atom_is_metal[i],
atom_is_metal[j],
extended_atom_mode,
in_same_residue_flag,
in_same_ring_flag);
if (nbc_dist.first) {
// In a helix O(n) is close to C(n+1), we should allow it.
//
bool is_O_C_1_5_related = check_for_O_C_1_5_relation(at_1, at_2);
if (is_O_C_1_5_related) {
dist_min = 2.84;
} else {
dist_min = nbc_dist.second;
if (false) debug_print("0", i, j, at_1, at_2, fixed_atom_flags, dist_min);
// Perhaps we don't have angle restraints to both atoms because one
// of the atoms is fixed (and thus miss that these have a 1-4 relationship).
// e.g. O(n) [moving] -> CA(n+1) [fixed]
//
// (this test will fail on insertion codes)
//
int rn_diff = abs(res_no_2 - res_no_1);
if (rn_diff == 1) {
// tweak dist_min
tweak_neighbours_for_rn_diff_is_1(fixed_atom_flags, atom_name_1, atom_name_2, res_no_1, res_no_2,
second_is_pro, &dist_min);
}
// this should be signed - not both ways, I think.
if (rn_diff == 2) {
// tweak dist_min
tweak_neighbours_for_rn_diff_is_2(fixed_atom_flags, atom_name_1, atom_name_2, &dist_min);
}
}
} else {
// missing NBC distance so use this fallback. short/standard value
dist_min = 2.8;
}
}
if (false) debug_print("A", i, j, at_1, at_2, fixed_atom_flags, dist_min);
bool is_H_non_bonded_contact = false;
if (atom_is_hydrogen[i]) {
is_H_non_bonded_contact = true;
if (H_atom_parent_atom_is_donor_vec[i])
if (atom_is_acceptor_vec[j])
dist_min -= 0.7;
}
if (false) debug_print("B", i, j, at_1, at_2, fixed_atom_flags, dist_min);
if (atom_is_hydrogen[j]) {
is_H_non_bonded_contact = true;
if (H_atom_parent_atom_is_donor_vec[j])
if (atom_is_acceptor_vec[i])
dist_min -= 0.7;
}
if (false) debug_print("C", i, j, at_1, at_2, fixed_atom_flags, dist_min);
// if they are both hydrogens 1.2 + 1.2, the min dist for molprobity should be 2.1 <smiley face>
if (atom_is_hydrogen[i])
if (atom_is_hydrogen[j])
dist_min -= 0.2; // aim for 2.2 so that 2.1 is a bit energetically unfavourable
if (false) debug_print("D", i, j, at_1, at_2, fixed_atom_flags, dist_min);
non_bonded_contacts_atom_indices[i].insert(j);
simple_restraint::nbc_function_t nbcf = simple_restraint::LENNARD_JONES;
// simple_restraint::nbc_function_t nbcf = simple_restraint::HARMONIC;
simple_restraint r(NON_BONDED_CONTACT_RESTRAINT,
nbcf, i, j,
is_H_non_bonded_contact,
fixed_atom_flags, dist_min);
nbc_restraints_fragment_p->push_back(r);
if (false) debug_print("-end-", i, j, at_1, at_2, fixed_atom_flags, dist_min);
}
}
done_count += 1; // atomic
}
void
coot::restraints_container_t::make_non_bonded_contact_restraints_using_threads_ng(int imol,
const coot::protein_geometry &geom) {
bool console_output_for_restraints_generation_timings = false;
auto tp_0 = std::chrono::high_resolution_clock::now();
std::vector<std::string> energy_type_for_atom(n_atoms);
std::vector<bool> H_atom_parent_atom_is_donor_vec(n_atoms, false);
std::vector<bool> atom_is_acceptor_vec(n_atoms, false);
#if 0
// bonded_atom_indices is constructed as bonds and angles are added (before calling add()).
for (int i=0; i<n_atoms; i++) {
const std::set<int> &s = bonded_atom_indices[i];
std::cout << i << " : ";
for (std::set<int>::const_iterator it=s.begin(); it!=s.end(); it++)
std::cout << *it << " ";
std::cout << "\n";
}
#endif
// needs timing test - might be slow (it isn't)
for (int i=0; i<n_atoms; i++) {
mmdb::Atom *at = atom[i];
if (! at->isTer()) {
std::string et = get_type_energy(imol, at, geom);
energy_type_for_atom[i] = et;
if (H_parent_atom_is_donor(at))
H_atom_parent_atom_is_donor_vec[i] = true;
if (is_acceptor(et, geom))
atom_is_acceptor_vec[i] = true;
}
}
auto tp_1 = std::chrono::high_resolution_clock::now();