forked from pemsley/coot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrankshaft.cc
1851 lines (1539 loc) · 61.3 KB
/
crankshaft.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/crankshaft.cc
*
* Copyright 2017 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 <algorithm> // for sort
#include <iomanip>
#include "crankshaft.hh"
#include "geometry/main-chain.hh"
#include "coot-utils/coot-coord-utils.hh"
// for refinement (put refinement in new file?)
#include "simple-restraint.hh"
#include "coot-utils/coot-map-utils.hh"
std::ostream &
coot::operator<<(std::ostream &s, const coot::crankshaft::scored_triple_angle_set_t &as) {
s << "minus-log-prob: " << as.minus_log_prob << " from angles";
for (std::size_t i=0; i<as.angles.size(); i++)
s << std::setw(9) << clipper::Util::rad2d(as.angles[i]) << " ";
return s;
}
std::ostream &
coot::operator<<(std::ostream &s, const coot::crankshaft::scored_nmer_angle_set_t &as) {
s << "minus_log_prob: " << std::setw(8) << as.minus_log_prob
<< " combi-score: " << std::right << std::setprecision(3) << std::fixed << as.combi_score
<< " from angles";
for (std::size_t i=0; i<as.angles.size(); i++)
s << std::setw(9) << clipper::Util::rad2d(as.angles[i]) << " ";
return s;
}
coot::crankshaft_set::crankshaft_set(mmdb::Residue *res_0,
mmdb::Residue *res_1,
mmdb::Residue *res_2,
mmdb::Residue *res_3) {
if (! res_0) throw(std::runtime_error("Null residue 0"));
if (! res_1) throw(std::runtime_error("Null residue 1"));
if (! res_2) throw(std::runtime_error("Null residue 2"));
if (! res_3) throw(std::runtime_error("Null residue 3"));
v.resize(8, 0);
mmdb::Atom **residue_atoms = 0;
int n_residue_atoms;
ca_1 = 0; // initally unset
ca_2 = 0;
mmdb::Residue *residue_p = res_0;
residue_p->GetAtomTable(residue_atoms, n_residue_atoms);
for (int iat=0; iat<n_residue_atoms; iat++) {
mmdb::Atom *at = residue_atoms[iat];
std::string at_name = at->name;
if (at_name == " C ") { // PDBv3 fixme
v[0] = at;
break;
}
}
residue_p = res_1;
residue_p->GetAtomTable(residue_atoms, n_residue_atoms);
for (int iat=0; iat<n_residue_atoms; iat++) {
mmdb::Atom *at = residue_atoms[iat];
std::string at_name = at->name;
if (at_name == " N ") { // PDBv3 fixme
v[1] = at;
}
if (at_name == " C ") { // PDBv3 fixme
v[2] = at;
}
if (at_name == " O ") { // PDBv3 fixme
v[3] = at;
}
if (at_name == " CA ") { // PDBv3 fixme
ca_1 = at;
}
}
residue_atoms = 0;
n_residue_atoms = 0;
residue_p = res_2;
residue_p->GetAtomTable(residue_atoms, n_residue_atoms);
for (int iat=0; iat<n_residue_atoms; iat++) {
mmdb::Atom *at = residue_atoms[iat];
std::string at_name = at->name;
if (at_name == " N ") { // PDBv3 fixme
v[4] = at;
}
if (at_name == " H ") { // PDBv3 fixme
v[5] = at;
}
if (at_name == " C ") { // PDBv3 fixme
v[6] = at;
}
if (at_name == " CA ") { // PDBv3 fixme
ca_2 = at;
}
}
residue_atoms = 0;
n_residue_atoms = 0;
residue_p = res_3;
residue_p->GetAtomTable(residue_atoms, n_residue_atoms);
for (int iat=0; iat<n_residue_atoms; iat++) {
mmdb::Atom *at = residue_atoms[iat];
std::string at_name = at->name;
if (at_name == " N ") { // PDBv3 fixme
v[7] = at;
}
}
if (! ca_1) throw(std::runtime_error("missing ca_1"));
if (! ca_2) throw(std::runtime_error("missing ca_2"));
if (false) {
std::cout << "in crankshaft_set constructor for res_1 " << residue_spec_t(res_1)
<< " ca_1 was set to " << ca_1 << std::endl;
std::cout << "in crankshaft_set constructor for res_1 " << residue_spec_t(res_1)
<< " ca_2 was set to " << ca_2 << std::endl;
}
// check that we have all the atoms - except the HN
int n_atoms = 0;
for (std::size_t i=0; i<v.size(); i++) {
if (v[i])
n_atoms++;
else
if (i==5)
n_atoms++;
}
if (n_atoms == 8) {
// happy path
make_trans_from_non_pro_cis_if_needed();
} else {
throw(std::runtime_error("missing a mainchain atom"));
}
}
// move the atoms of of res_1 and res_2 in mol
// Find if this is cis and move the N, (H), C and O to make it more like a trans
// N (and H) will move most of course.
// C and O will move a bit in the direction of new N position
//
void
coot::crankshaft_set::make_trans_from_non_pro_cis_if_needed() {
if (ca_1) {
if (ca_2) {
const std::string res_name_2 = ca_2->GetResName();
if (res_name_2 != "PRO") {
if (is_cis()) {
mmdb::Atom *N_at = v[4];
if (N_at) {
clipper::Coord_orth N_pos = co(N_at);
clipper::Coord_orth C_pos = co(v[2]);
clipper::Coord_orth O_pos = co(v[3]);
clipper::Coord_orth p_ca_1 = co(ca_1);
clipper::Coord_orth p_ca_2 = co(ca_2);
clipper::Coord_orth dir = p_ca_2 - p_ca_1;
clipper::Coord_orth N_pos_new = util::rotate_around_vector(dir, N_pos, p_ca_1, M_PI);
update_position(N_at, N_pos_new);
// Move the N and C closer together (as needed) along the N-C vector.
// Ideal distance 1.33A
clipper::Coord_orth n_c_vec = C_pos - N_pos_new;
clipper::Coord_orth n_c_unit(n_c_vec.unit());
double bl = sqrt(n_c_vec.lengthsq());
double bl_delta = bl - 1.33;
// std::cout << " cis N-C correction bl_delta " << bl_delta << std::endl;
clipper::Coord_orth pos_corr = bl_delta * 0.5 * n_c_unit;
N_pos_new += pos_corr;
C_pos -= pos_corr;
O_pos -= pos_corr;
update_position(N_at, N_pos_new);
update_position(v[2], C_pos);
update_position(v[3], O_pos);
if (v[5]) { // H, null probably
clipper::Coord_orth H_pos = co(v[5]);
clipper::Coord_orth at_pos_new = util::rotate_around_vector(dir, H_pos, p_ca_1, M_PI);
update_position(v[5], at_pos_new);
}
}
}
}
}
}
}
bool
coot::crankshaft_set::is_cis() const {
bool cis = false;
if (ca_1) {
if (ca_2) {
clipper::Coord_orth N_pos = co(v[4]);
clipper::Coord_orth C_pos = co(v[2]);
clipper::Coord_orth p_ca_1 = co(ca_1);
clipper::Coord_orth p_ca_2 = co(ca_2);
clipper::Coord_orth dir = p_ca_2 - p_ca_1;
double tors = clipper::Coord_orth::torsion(p_ca_2, N_pos, C_pos, p_ca_1);
if (false)
std::cout << " debug:: torsion for ca_1 " << atom_spec_t(ca_1) << " "
<< clipper::Util::rad2d(tors) << " degrees" << std::endl;
if (std::abs(tors) < M_PI_2)
cis = true;
}
}
// std::cout << " debug:: is cis for ca_1 " << atom_spec_t(ca_1) << " " << cis << std::endl;
return cis;
}
std::vector<mmdb::Residue *>
coot::nmer_crankshaft_set::residues() const {
std::vector<mmdb::Residue *> v;
for (std::size_t i=0; i<size(); i++) {
mmdb::Atom *at_1 = cs_vec[i].ca_1;
mmdb::Atom *at_2 = cs_vec[i].ca_2;
mmdb::Residue *r_1 = at_1->residue;
mmdb::Residue *r_2 = at_2->residue;
if (std::find(v.begin(), v.end(), r_1) == v.end()) v.push_back(r_1);
if (std::find(v.begin(), v.end(), r_2) == v.end()) v.push_back(r_2);
}
return v;
}
// n_samples = 60 default arg
std::vector<std::pair<float, float> >
coot::crankshaft::spin_search(const coot::residue_spec_t &spec,
const zo::rama_table_set &zorts,
int n_samples) const {
std::vector<std::pair<float, float> > v;
std::pair<mmdb::Residue *, mmdb::Residue *> rs = util::get_this_and_next_residues(spec, mol);
if (rs.first) {
if (rs.second) {
mmdb::Residue *res_1 = rs.first;
mmdb::Residue *res_2 = rs.second;
mmdb::Atom *ca_1 = get_atom(res_1, " CA ");
mmdb::Atom *ca_2 = get_atom(res_2, " CA ");
if (ca_1 && ca_2) {
// when we crankshaft the C, O and N of res_1(n) and res_2(n+1),
// we need to know the C of residue n-1 and the N of residue n+2
// because the phi,psi of n and n+1 depend on them.
mmdb::Residue *res_0 = util::get_previous_residue(spec, mol);
if (res_0) {
// res_3 is needed for phi,psi of res_2
residue_spec_t spec_2(res_2);
mmdb::Residue *res_3 = util::get_following_residue(spec_2, mol);
if (res_3) {
std::string rt1 = "ALL!nP"; // hack
std::string rt2 = "ALL!nP";
float div = 1/float(n_samples);
crankshaft_set cs(res_0, res_1, res_2, res_3);
for (int i=0; i<n_samples; i++) {
float a = float(i) * div * 2*M_PI;
std::pair<phi_psi_t, phi_psi_t> ppp = cs.phi_psis(a);
std::pair<float,float> pr = probability_of_spin_orientation(ppp, rt1, rt2, zorts);
if (false)
std::cout << "score: " << spec << " " << i << " "
<< pr.first << " " << pr.second << std::endl;
}
}
}
} else {
std::cout << "missing mainchain atom(s) for " << spec << std::endl;
}
} else {
std::cout << "missing second residue for " << spec << std::endl;
}
} else {
std::cout << "missing first residue: " << spec << std::endl;
}
return v;
}
void
coot::crankshaft_set::move_the_atoms(float ang) {
// move atom indices 2 3 4 5 (if not null)
if (v.size() > 5) {
int indices[] = { 2, 3, 4, 5};
clipper::Coord_orth p_ca_1 = co(ca_1);
clipper::Coord_orth p_ca_2 = co(ca_2);
clipper::Coord_orth dir = p_ca_2 - p_ca_1;
for (std::size_t i=0; i<4; i++) {
mmdb::Atom *at = v[indices[i]];
if (at) {
clipper::Coord_orth at_pos = co(v[indices[i]]);
clipper::Coord_orth at_pos_new = util::rotate_around_vector(dir, at_pos, p_ca_1, ang);
at->x = at_pos_new.x();
at->y = at_pos_new.y();
at->z = at_pos_new.z();
}
}
}
}
// can throw a runtime_error
//
// we need a rama_table_set to get the residue types
// (maybe that function should be a static of a rama_table_set)
//
coot::triple_crankshaft_set::triple_crankshaft_set(const residue_spec_t &spec_first_residue,
const zo::rama_table_set &zorts,
mmdb::Manager *mol) {
std::pair<mmdb::Residue *, mmdb::Residue *> rs =
util::get_this_and_next_residues(spec_first_residue, mol);
if (rs.first) {
if (rs.second) {
mmdb::Residue *res_1 = rs.first;
mmdb::Residue *res_2 = rs.second;
// when we crankshaft the C, O and N of res_1(n) and res_2(n+1),
// we need to know the C of residue n-1 and the N of residue n+2
// because the phi,psi of n and n+1 depend on them.
mmdb::Residue *res_0 = util::get_previous_residue(spec_first_residue, mol);
if (res_0) {
// res_3 is needed for phi,psi of res_2
residue_spec_t spec_2(res_2);
mmdb::Residue *res_3 = util::get_following_residue(spec_2, mol);
if (res_3) {
residue_spec_t spec_3(res_3);
mmdb::Residue *res_4 = util::get_following_residue(spec_3, mol);
if (res_4) {
residue_spec_t spec_4(res_4);
mmdb::Residue *res_5 = util::get_following_residue(spec_4, mol);
if (res_5) {
std::string this_residue_name = res_1->GetResName();
std::string next_residue_name = res_2->GetResName();
std::string res_name_3 = res_3->GetResName();
std::string res_name_4 = res_4->GetResName();
std::string res_name_5 = res_5->GetResName();
// I think that get_residue_type() can be a static
std::string rt1 = zorts.get_residue_type(this_residue_name, next_residue_name);
std::string rt2 = zorts.get_residue_type(next_residue_name, res_name_3);
std::string rt3 = zorts.get_residue_type(res_name_3, res_name_4);
std::string rt4 = zorts.get_residue_type(res_name_4, res_name_5);
residue_types.resize(5); // the type of 0th residue not relevant
residue_types[1] = rt1;
residue_types[2] = rt2;
residue_types[3] = rt3;
residue_types[4] = rt4;
if (false)
std::cout << "making cs 0 from "
<< residue_spec_t(res_0) << " "
<< residue_spec_t(res_1) << " "
<< residue_spec_t(res_2) << " "
<< residue_spec_t(res_3) << " "
<< std::endl;
if (false)
std::cout << "making cs 1 from "
<< residue_spec_t(res_1) << " "
<< residue_spec_t(res_2) << " "
<< residue_spec_t(res_3) << " "
<< residue_spec_t(res_4) << " "
<< std::endl;
if (false)
std::cout << "making cs 2 from "
<< residue_spec_t(res_2) << " "
<< residue_spec_t(res_3) << " "
<< residue_spec_t(res_4) << " "
<< residue_spec_t(res_5) << " "
<< std::endl;
cs[0] = crankshaft_set(res_0, res_1, res_2, res_3);
cs[1] = crankshaft_set(res_1, res_2, res_3, res_4);
cs[2] = crankshaft_set(res_2, res_3, res_4, res_5);
// debug
// mol->WritePDBASCII("uncis.pdb");
}
}
}
}
}
}
}
coot::nmer_crankshaft_set::nmer_crankshaft_set(const residue_spec_t &spec_mid_residue,
unsigned int n_peptides,
const zo::rama_table_set &zorts,
mmdb::Manager *mol) {
#ifdef HAVE_CXX11
// we first need to find what is the first residue (the N-terminal residue of the
// first rotating peptide). res_0 is the residue before the first residue.
// This will not do the right thing if there are insertion codes. In that case
// more cleverness is required.
int first_from_mid_delta = std::round((n_peptides-1)/2);
residue_spec_t spec_first_residue(spec_mid_residue.chain_id,
spec_mid_residue.res_no - first_from_mid_delta);
// if n_peptides is 1: then we will need to fill cs[0] with:
// res_0: residue before the first spec
// res_1: this residue (which has ca_1) of which we move the C,O
// res_2: next residue (which has ca_2) or which we move the N
// res_3: needed for phi,psi of res_2
// set mess if we fail to find residues of a peptide cranshaft set
//
std::string mess; // if this get set, throw an exception.
mmdb::Residue *residue_p = util::get_residue(spec_first_residue, mol);
if (residue_p) {
// when we crankshaft the C, O and N of res_1(n) and res_2(n+1),
// we need to know the C of residue n-1 and the N of residue n+2
// because the phi,psi of n and n+1 depend on them.
mmdb::Residue *res_0 = util::get_previous_residue(spec_first_residue, mol);
if (res_0) {
// a base_residue_spec is res_1 of a crankshaft set (not res_0)
//
residue_spec_t current_base_residue_spec = spec_first_residue;
mmdb::Residue *current_base_residue = residue_p;
// maybe it's silly to have "off-by-one" residue type index?
residue_types.resize(n_peptides+1);
for (std::size_t i_pep=0; i_pep<n_peptides; i_pep++) {
mmdb::Residue *pep_res_0 = util::get_previous_residue (current_base_residue_spec, mol);
mmdb::Residue *pep_res_2 = util::get_following_residue(current_base_residue_spec, mol);
if (pep_res_2) {
residue_spec_t current_res_2_spec(pep_res_2);
mmdb::Residue *pep_res_3 = util::get_following_residue(current_res_2_spec, mol);
if (pep_res_3) {
// this can throw a runtime_error
crankshaft_set cs(pep_res_0, current_base_residue, pep_res_2, pep_res_3);
cs_vec.push_back(cs);
// maybe it's silly to have "off-by-one" residue type index?
std::string rn_1 = current_base_residue->GetResName();
std::string rn_2 = pep_res_2->GetResName();
std::string rt = zorts.get_residue_type(rn_1, rn_2);
residue_types[i_pep+1] = rt;
// setup for next
current_base_residue = pep_res_2;
current_base_residue_spec = residue_spec_t(pep_res_2);
} else {
mess = "Fail to get residue 3 for peptide index ";
mess += util::int_to_string(i_pep);
break;
}
} else {
mess = "Fail to get residue 2 for peptide index ";
mess += util::int_to_string(i_pep);
break;
}
}
} else {
mess = "Fail to get residue 0 for starting residue ";
}
}
if (! mess.empty()) throw std::runtime_error(mess);
#endif // HAVE_CXX11
}
void
coot::triple_crankshaft_set::move_the_atoms(float best_angles[]) {
std::cout << "move the atoms with peptide rotations "
<< clipper::Util::rad2d(best_angles[0]) << " "
<< clipper::Util::rad2d(best_angles[1]) << " "
<< clipper::Util::rad2d(best_angles[2]) << " "
<< std::endl;
for (unsigned int i=0; i<3; i++)
cs[i].move_the_atoms(best_angles[i]);
}
//
// not const because we can change the atoms of mol if apply_best_angles_flag is set
void
coot::crankshaft::triple_spin_search(const residue_spec_t &spec_first_residue,
const zo::rama_table_set &zorts,
bool apply_best_angles_flag,
int n_samples) {
// 0.1-2-3-4.5 (. for ref, - is spin)
float div = 1.0/float(n_samples);
try {
triple_crankshaft_set tcs(spec_first_residue, zorts, mol);
float best_angles[3];
best_angles[0] = -10; best_angles[1] = -10; best_angles[2] = -10;
float best_prob = 0;
for (int i=0; i<n_samples; i++) {
float a0 = float(i) * div * 2*M_PI;
phi_psi_t pp_1 = tcs.phi_psi(0, a0);
float pr_1 = zorts.value(pp_1, tcs.residue_type(1));
for (int j=0; j<n_samples; j++) {
float a1 = float(j) * div * 2*M_PI;
phi_psi_t pp_2 = tcs.phi_psi(1, a1);
float pr_2 = zorts.value(pp_2, tcs.residue_type(1));
for (int k=0; k<n_samples; k++) {
float a2 = float(k) * div * 2*M_PI;
// the last peptides gives us the probabilities for 2 phi,psi pairs
std::pair<phi_psi_t, phi_psi_t> pp_3 = tcs.phi_psis_last(a2);
std::pair<float,float> pr_3_pair =
probability_of_spin_orientation(pp_3, tcs.residue_type(3), tcs.residue_type(4), zorts);
float log_prob = pr_1 + pr_2 + pr_3_pair.first + pr_3_pair.second;
if (log_prob > best_prob) {
best_prob = log_prob;
best_angles[0] = a0;
best_angles[1] = a1;
best_angles[2] = a2;
}
}
}
}
std::cout << "INFO:: best log prob: " << best_prob << " angles: "
<< clipper::Util::rad2d(best_angles[0]) << " "
<< clipper::Util::rad2d(best_angles[1]) << " "
<< clipper::Util::rad2d(best_angles[2]) << " "
<< std::endl;
if (apply_best_angles_flag)
tcs.move_the_atoms(best_angles);
}
catch (const std::runtime_error &rte) {
std::cout << "WARNING:: " << rte.what() << std::endl;
}
}
// not const because we can change the atoms of mol if apply_best_angles_flag is set
//
std::vector<coot::crankshaft::scored_triple_angle_set_t>
coot::crankshaft::find_maxima_from_triples(const residue_spec_t &spec_first_residue,
const zo::rama_table_set &zorts,
unsigned int n_samples) {
// n_samples = 1; // hack for testing
// 0.1-2-3-4.5 (. for ref, - is spin)
std::vector<scored_triple_angle_set_t> results;
try {
triple_crankshaft_set tcs(spec_first_residue, zorts, mol);
float div = 1.0/float(n_samples);
for (std::size_t i_sample=0; i_sample<n_samples; i_sample++) {
// make these random numbers 0 -> 2pi
float start_angles[] = { 1.0, 2.0, 3.0 }; // in radians
for (std::size_t i=0; i<3; i++)
start_angles[i] = 2 * M_PI * float(coot::util::random())/float(RAND_MAX);
// testing wild solutions
// start_angles[0] = clipper::Util::d2rad(264.849);
// start_angles[1] = clipper::Util::d2rad(122.957);
// start_angles[2] = clipper::Util::d2rad(300.472);
//
// start_angles[0] = clipper::Util::d2rad();
// start_angles[1] = clipper::Util::d2rad();
// start_angles[2] = clipper::Util::d2rad();
//
// start_angles[0] = clipper::Util::d2rad(351.391);
// start_angles[1] = clipper::Util::d2rad(287.775);
// start_angles[2] = clipper::Util::d2rad(49.7706);
scored_triple_angle_set_t as = run_optimizer(start_angles, tcs, zorts);
if (as.filled()) {
if (false)
std::cout << "sample " << i_sample << " " << as.minus_log_prob << " from angles "
<< std::setw(9) << clipper::Util::rad2d(as.angles[0]) << " "
<< std::setw(9) << clipper::Util::rad2d(as.angles[1]) << " "
<< std::setw(9) << clipper::Util::rad2d(as.angles[2]) << std::endl;
// add it if not already something similar
//
bool add_it = true;
if (false) { // what's close?
float closest_delta = 999.9;
for (std::size_t i=0; i<results.size(); i++) {
float diff = results[i].angles[0] - as.angles[0];
float this_delta_2 = diff * diff;
diff = results[i].angles[1] - as.angles[1];
this_delta_2 += diff * diff;
diff = results[i].angles[2] - as.angles[2];
this_delta_2 += diff * diff;
float this_delta = sqrt(this_delta_2);
if (this_delta < closest_delta) {
closest_delta = this_delta;
}
}
std::cout << "closest-delta: " << closest_delta << std::endl;
}
for (std::size_t i=0; i<results.size(); i++) {
if (results[i].is_close(as)) {
add_it = false;
break;
}
}
if (add_it) {
if (false)
std::cout << "as: " << as << " from starting angles "
<< clipper::Util::rad2d(start_angles[0]) << " "
<< clipper::Util::rad2d(start_angles[1]) << " "
<< clipper::Util::rad2d(start_angles[2]) << " "
<< std::endl;
results.push_back(as);
} else {
// std::cout << "we already have that " << std::endl;
}
}
}
std::cout << "From " << n_samples << " samples, found " << results.size()
<< " unique solutions" << std::endl;
}
catch (const std::runtime_error &rte) {
std::cout << "WARNING:: " << rte.what() << std::endl;
}
std::sort(results.begin(), results.end());
if (false) {
std::ofstream f("solutions");
for (std::size_t i=0; i<results.size(); i++) {
f << " value "
<< results[i].minus_log_prob << " at (degrees) "
<< clipper::Util::rad2d(results[i].angles[0]) << " "
<< clipper::Util::rad2d(results[i].angles[1]) << " "
<< clipper::Util::rad2d(results[i].angles[2]) << "\n";
}
}
return results;
}
// not const because we can change the atoms of mol if apply_best_angles_flag is set
//
// "scored" because the probability has been calculated (not used in refinement)
//
// only allow solutions that are within log_prob_filter_n_sigma sds of the top solution
// i.e. 2.0 will allow more solutions than 1.0.
//
std::vector<coot::crankshaft::scored_nmer_angle_set_t>
coot::crankshaft::find_maxima(const residue_spec_t &spec_mid_residue,
unsigned int n_peptides, // the length of the nmer
const zo::rama_table_set &zorts,
float log_prob_filter_n_sigma,
unsigned int n_samples) {
// n_samples = 1; // hack for testing
// 0.1-2-3-4.5 (. for ref, - is spin)
std::vector<scored_nmer_angle_set_t> results;
#ifdef HAVE_CXX_THREAD
try {
// if the atoms are not there, this can throw a std::runtime_error.
nmer_crankshaft_set cs(spec_mid_residue, n_peptides, zorts, mol);
if (cs.size() == 0) return results;
float div = 1.0/float(n_samples);
unsigned int n_threads = coot::get_max_number_of_threads();
if (n_threads > 0) {
// first split the samples into batches for each thread
std::size_t current_thread = 0;
std::vector<std::vector<std::size_t> > samples_for_thread(n_threads);
for (std::size_t i_sample=0; i_sample<n_samples; i_sample++) {
samples_for_thread[current_thread].push_back(i_sample);
++current_thread;
if (current_thread == n_threads)
current_thread = 0;
}
results.resize(n_samples);
std::vector<std::thread> threads;
std::vector<scored_nmer_angle_set_t> sas_vec(n_threads);
for (std::size_t i_thread=0; i_thread<n_threads; i_thread++) {
if (samples_for_thread[i_thread].size() > 0) {
threads.push_back(std::thread(run_optimizer_in_thread,
std::cref(samples_for_thread[i_thread]),
std::cref(cs), std::cref(zorts), &results));
}
}
for (unsigned int i_thread=0; i_thread<threads.size(); i_thread++)
threads.at(i_thread).join();
}
}
catch (const std::runtime_error &rte) {
std::cout << "WARNING:: " << rte.what() << std::endl;
}
if (results.size() > 0) {
// first remove failed solutions (no angles)
// std::cout << "before null-eraser: " << results.size() << std::endl;
results.erase(std::remove_if(results.begin(), results.end(), null_eraser), results.end());
// std::cout << "after null-eraser: " << results.size() << std::endl;
// next remove solutions that are similar to each other
// pre-sort so that std::unique does the adjacent values comparison correctly
std::sort(results.begin(), results.end());
// std::cout << "before similar-eraser: " << results.size() << std::endl;
results.erase(std::unique(results.begin(), results.end(), scored_solution_comparer), results.end());
// std::cout << "after similar-eraser: " << results.size() << std::endl;
std::sort(results.begin(), results.end()); // resort after std::unique
// now filter out those with "low" log_prob values
//
std::vector<float> v(results.size());
for (std::size_t i=0; i<results.size(); i++)
v[i] = results[i].minus_log_prob;
float top_score = results[0].minus_log_prob; // highest (most negative)
// delete those results that are "a long way" below the top_score
// or are below the mean.
util::stats_data sd(v);
float at_least = top_score + log_prob_filter_n_sigma*sd.sd;
float mean = sd.mean;
// std::cout << "before log_prob filter we have " << results.size() << " solutions" << std::endl;
results.erase(std::remove_if(results.begin(), results.end(), eraser(at_least, mean)), results.end());
// std::cout << "after log_prob filter we have " << results.size() << " solutions" << std::endl;
if (false) {
std::ofstream f("solutions");
for (std::size_t i=0; i<results.size(); i++) {
f << " value "
<< results[i].minus_log_prob << " at (degrees) ";
for (std::size_t iang=0; iang<results[i].angles.size(); iang++)
f << clipper::Util::rad2d(results[i].angles[iang]) << " ";
f << "\n";
}
}
}
#endif // HAVE_CXX_THREAD
return results;
}
// The function and the derivatives need to be turned upside down at some
// stage, because actually, we want to maximize, not minimize.
//
// static
double
coot::crankshaft::optimize_a_triple::f(const gsl_vector *v, void *params) {
// convert from spin angles (in v) to phi,psi and then log probability
// extract the table refs
const triple_set_param_holder_t *param_holder = reinterpret_cast<triple_set_param_holder_t *> (params);
const zo::rama_table_set &zorts = param_holder->zorts;
if (false)
std::cout << "in f() with tcs has residue types "
<< param_holder->tcs.residue_type(1) << " "
<< param_holder->tcs.residue_type(2) << std::endl;
float log_prob_sum = 0;
for (unsigned int i=0; i<3; i++) { // i is cs index, not residue_type index
phi_psi_t pp = param_holder->tcs[i].phi_psi(gsl_vector_get(v, i));
float pr = param_holder->tcs.log_prob(pp, i+1, zorts); // index i+1 is for residue type
log_prob_sum += pr;
}
if (false)
std::cout << "f() with angles "
<< gsl_vector_get(v, 0) << " " << gsl_vector_get(v, 1) << " " << gsl_vector_get(v, 2) << " "
<< " returns " << log_prob_sum << std::endl;
return -log_prob_sum; // we want maxima
}
/* The gradient of f, df/da */
void
coot::crankshaft::optimize_a_triple::df(const gsl_vector *v,
void *params,
gsl_vector *df_vec) {
// convert from spin angles (in v) to phi,psi and then log probability
// When it comes to refinement, moving the atoms:
//
// L = log_prob_sum
// a is peptide rotation angle
//
// dL/da = dphi/da * dL/dphi + dpsi/da * dL/dpsi
//
// zorts df gives us dL/dphi and dL/dpsi
//
// dphi/da = dphi/dx * dx/da + dphi/dy * dy/da + dphi/dz * dz/da
//
// torsion restraint derivatives give us dphi/d{x,y,z}
//
// what is dx/da?
//
// dx/da = rcos(a), dy/da = -rsin(a), dz/da = 0 in the coordinate system
// of the peptide - where CA-CA is along (0,0,1).
// X,Y plane orientation is arbitrary (use the position of the N atom?)
// You will need to work out r.
// You will need to rotate this into the world coordinates frame.
bool do_numerical = true; // debugging
// extract the table refs
const triple_set_param_holder_t *param_holder = reinterpret_cast<triple_set_param_holder_t *> (params);
const zo::rama_table_set &zorts = param_holder->zorts;
for (unsigned int i=0; i<3; i++) {
phi_psi_t pp = param_holder->tcs[i].phi_psi(gsl_vector_get(v, i));
const std::string &rt = param_holder->tcs.residue_type(i+1);
// std::cout << "debug:: in df() for peptide index " << i << " rt " << rt << std::endl;
std::pair<mmdb::realtype,mmdb::realtype> grads = zorts.df(pp, rt);
if (false)
std::cout << "debug:: in df() analyticals for peptide index " << i
<< " pp " << pp.phi << " " << pp.psi
<< " gradients " << grads.first << " " << grads.second << std::endl;
if (do_numerical) {
float delta = 0.002;
float current_val = gsl_vector_get(v, i);
phi_psi_t pp_1 = param_holder->tcs[i].phi_psi(current_val + delta);
phi_psi_t pp_2 = param_holder->tcs[i].phi_psi(current_val - delta);
float pr_1 = param_holder->tcs.log_prob(pp_1, i+1, zorts);
float pr_2 = param_holder->tcs.log_prob(pp_2, i+1, zorts);
float numerical_grad = - (pr_1 - pr_2) / (2 * delta);
if (false)
std::cout << "debug:: in df() for peptide index " << i
<< " angles "
<< clipper::Util::rad2d(gsl_vector_get(v, i)) << " "
<< " pp " << pp.phi << " " << pp.psi
<< " numerical gradient from " << pr_1 << " - " << pr_2 << " / 2 * " << delta
<< " = " << numerical_grad << std::endl;
gsl_vector_set(df_vec, i, numerical_grad);
}
}
}
/* Compute both f and df together. */
void
coot::crankshaft::optimize_a_triple::fdf(const gsl_vector *x, void *params,
double *f_in, gsl_vector *df_in) {
*f_in = f(x, params);
df(x, params, df_in);
}
// static
double
coot::crankshaft::optimize_an_nmer::f(const gsl_vector *v, void *params) {
// convert from spin angles (in v) to phi,psi and then log probability
// extract the table refs
const nmer_set_param_holder_t *param_holder = reinterpret_cast<nmer_set_param_holder_t *> (params);
const zo::rama_table_set &zorts = param_holder->zorts;
// debug zorts
// debug nmer_crankshaft_set &cs
float log_prob_sum = 0;
for (unsigned int i=0; i<param_holder->cs.size(); i++) { // i is cs index, not residue_type index
phi_psi_t pp = param_holder->cs[i].phi_psi(gsl_vector_get(v, i));
float pr = param_holder->cs.log_prob(pp, i+1, zorts); // index i+1 is for residue type
log_prob_sum += pr;
}
return -log_prob_sum; // we want maxima
}
/* The gradient of f, df/da */
void
coot::crankshaft::optimize_an_nmer::df(const gsl_vector *v,
void *params,
gsl_vector *df_vec) {
// convert from spin angles (in v) to phi,psi and then log probability
// using numerical gradients! :-/
// extract the table refs
const nmer_set_param_holder_t *param_holder = reinterpret_cast<nmer_set_param_holder_t *> (params);
const zo::rama_table_set &zorts = param_holder->zorts;
for (unsigned int i=0; i<param_holder->cs.size(); i++) {
if (true) {
float delta = 0.002;
float current_val = gsl_vector_get(v, i);
phi_psi_t pp_1 = param_holder->cs[i].phi_psi(current_val + delta);
phi_psi_t pp_2 = param_holder->cs[i].phi_psi(current_val - delta);
float pr_1 = param_holder->cs.log_prob(pp_1, i+1, zorts);
float pr_2 = param_holder->cs.log_prob(pp_2, i+1, zorts);
float numerical_grad = - (pr_1 - pr_2) / (2 * delta);
gsl_vector_set(df_vec, i, numerical_grad);
}
}
}
/* Compute both f and df together. */
void
coot::crankshaft::optimize_an_nmer::fdf(const gsl_vector *x, void *params,
double *f_in, gsl_vector *df_in) {
*f_in = f(x, params);
df(x, params, df_in);
}
coot::crankshaft::scored_triple_angle_set_t
coot::crankshaft::run_optimizer(float start_angles[],
const coot::triple_crankshaft_set &tcs,
const zo::rama_table_set &zorts) {
size_t iter = 0;
int status;
triple_set_param_holder_t param_holder(zorts, tcs);
gsl_vector *x;
gsl_multimin_function_fdf my_func;
my_func.n = 3;
my_func.f = &optimize_a_triple::f;
my_func.df = &optimize_a_triple::df;
my_func.fdf = &optimize_a_triple::fdf;
my_func.params = reinterpret_cast<void *> (¶m_holder);
x = gsl_vector_alloc(3);
gsl_vector_set(x, 0, start_angles[0]); // refactor
gsl_vector_set(x, 1, start_angles[1]);
gsl_vector_set(x, 2, start_angles[2]);
const gsl_multimin_fdfminimizer_type *T = gsl_multimin_fdfminimizer_conjugate_pr;
gsl_multimin_fdfminimizer *s = gsl_multimin_fdfminimizer_alloc (T, 3);
// small moves Ellie, small moves...
gsl_multimin_fdfminimizer_set(s, &my_func, x, 0.01, 1.0); // within 1 degree is good enough
do {
iter++;
status = gsl_multimin_fdfminimizer_iterate (s);
if (status)
break;