forked from pylelab/USalign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TMalign.h
3641 lines (3298 loc) · 125 KB
/
TMalign.h
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
/* Functions for the core TMalign algorithm, including the entry function
* TMalign_main */
#ifndef TMalign_h
#define TMalign_h 1
#include "param_set.h"
#include "NW.h"
#include "Kabsch.h"
#include "NWalign.h"
// 1, collect those residues with dis<d;
// 2, calculate TMscore
int score_fun8( double **xa, double **ya, int n_ali, double d, int i_ali[],
double *score1, int score_sum_method, const double Lnorm,
const double score_d8, const double d0)
{
double score_sum=0, di;
double d_tmp=d*d;
double d02=d0*d0;
double score_d8_cut = score_d8*score_d8;
int i, n_cut, inc=0;
while(1)
{
n_cut=0;
score_sum=0;
for(i=0; i<n_ali; i++)
{
di = dist(xa[i], ya[i]);
if(di<d_tmp)
{
i_ali[n_cut]=i;
n_cut++;
}
if(score_sum_method==8)
{
if(di<=score_d8_cut) score_sum += 1/(1+di/d02);
}
else score_sum += 1/(1+di/d02);
}
//there are not enough feasible pairs, relieve the threshold
if(n_cut<3 && n_ali>3)
{
inc++;
double dinc=(d+inc*0.5);
d_tmp = dinc * dinc;
}
else break;
}
*score1=score_sum/Lnorm;
return n_cut;
}
int score_fun8_standard(double **xa, double **ya, int n_ali, double d,
int i_ali[], double *score1, int score_sum_method,
double score_d8, double d0)
{
double score_sum = 0, di;
double d_tmp = d*d;
double d02 = d0*d0;
double score_d8_cut = score_d8*score_d8;
int i, n_cut, inc = 0;
while (1)
{
n_cut = 0;
score_sum = 0;
for (i = 0; i<n_ali; i++)
{
di = dist(xa[i], ya[i]);
if (di<d_tmp)
{
i_ali[n_cut] = i;
n_cut++;
}
if (score_sum_method == 8)
{
if (di <= score_d8_cut) score_sum += 1 / (1 + di / d02);
}
else
{
score_sum += 1 / (1 + di / d02);
}
}
//there are not enough feasible pairs, relieve the threshold
if (n_cut<3 && n_ali>3)
{
inc++;
double dinc = (d + inc*0.5);
d_tmp = dinc * dinc;
}
else break;
}
*score1 = score_sum / n_ali;
return n_cut;
}
double TMscore8_search(double **r1, double **r2, double **xtm, double **ytm,
double **xt, int Lali, double t0[3], double u0[3][3], int simplify_step,
int score_sum_method, double *Rcomm, double local_d0_search, double Lnorm,
double score_d8, double d0)
{
int i, m;
double score_max, score, rmsd;
const int kmax=Lali;
int k_ali[kmax], ka, k;
double t[3];
double u[3][3];
double d;
//iterative parameters
int n_it=20; //maximum number of iterations
int n_init_max=6; //maximum number of different fragment length
int L_ini[n_init_max]; //fragment lengths, Lali, Lali/2, Lali/4 ... 4
int L_ini_min=4;
if(Lali<L_ini_min) L_ini_min=Lali;
int n_init=0, i_init;
for(i=0; i<n_init_max-1; i++)
{
n_init++;
L_ini[i]=(int) (Lali/pow(2.0, (double) i));
if(L_ini[i]<=L_ini_min)
{
L_ini[i]=L_ini_min;
break;
}
}
if(i==n_init_max-1)
{
n_init++;
L_ini[i]=L_ini_min;
}
score_max=-1;
//find the maximum score starting from local structures superposition
int i_ali[kmax], n_cut;
int L_frag; //fragment length
int iL_max; //maximum starting position for the fragment
for(i_init=0; i_init<n_init; i_init++)
{
L_frag=L_ini[i_init];
iL_max=Lali-L_frag;
i=0;
while(1)
{
//extract the fragment starting from position i
ka=0;
for(k=0; k<L_frag; k++)
{
int kk=k+i;
r1[k][0]=xtm[kk][0];
r1[k][1]=xtm[kk][1];
r1[k][2]=xtm[kk][2];
r2[k][0]=ytm[kk][0];
r2[k][1]=ytm[kk][1];
r2[k][2]=ytm[kk][2];
k_ali[ka]=kk;
ka++;
}
//extract rotation matrix based on the fragment
Kabsch(r1, r2, L_frag, 1, &rmsd, t, u);
if (simplify_step != 1)
*Rcomm = 0;
do_rotation(xtm, xt, Lali, t, u);
//get subsegment of this fragment
d = local_d0_search - 1;
n_cut=score_fun8(xt, ytm, Lali, d, i_ali, &score,
score_sum_method, Lnorm, score_d8, d0);
if(score>score_max)
{
score_max=score;
//save the rotation matrix
for(k=0; k<3; k++)
{
t0[k]=t[k];
u0[k][0]=u[k][0];
u0[k][1]=u[k][1];
u0[k][2]=u[k][2];
}
}
//try to extend the alignment iteratively
d = local_d0_search + 1;
for(int it=0; it<n_it; it++)
{
ka=0;
for(k=0; k<n_cut; k++)
{
m=i_ali[k];
r1[k][0]=xtm[m][0];
r1[k][1]=xtm[m][1];
r1[k][2]=xtm[m][2];
r2[k][0]=ytm[m][0];
r2[k][1]=ytm[m][1];
r2[k][2]=ytm[m][2];
k_ali[ka]=m;
ka++;
}
//extract rotation matrix based on the fragment
Kabsch(r1, r2, n_cut, 1, &rmsd, t, u);
do_rotation(xtm, xt, Lali, t, u);
n_cut=score_fun8(xt, ytm, Lali, d, i_ali, &score,
score_sum_method, Lnorm, score_d8, d0);
if(score>score_max)
{
score_max=score;
//save the rotation matrix
for(k=0; k<3; k++)
{
t0[k]=t[k];
u0[k][0]=u[k][0];
u0[k][1]=u[k][1];
u0[k][2]=u[k][2];
}
}
//check if it converges
if(n_cut==ka)
{
for(k=0; k<n_cut; k++)
{
if(i_ali[k]!=k_ali[k]) break;
}
if(k==n_cut) break;
}
} //for iteration
if(i<iL_max)
{
i=i+simplify_step; //shift the fragment
if(i>iL_max) i=iL_max; //do this to use the last missed fragment
}
else if(i>=iL_max) break;
}//while(1)
//end of one fragment
}//for(i_init
return score_max;
}
double TMscore8_search_standard( double **r1, double **r2,
double **xtm, double **ytm, double **xt, int Lali,
double t0[3], double u0[3][3], int simplify_step, int score_sum_method,
double *Rcomm, double local_d0_search, double score_d8, double d0)
{
int i, m;
double score_max, score, rmsd;
const int kmax = Lali;
int k_ali[kmax], ka, k;
double t[3];
double u[3][3];
double d;
//iterative parameters
int n_it = 20; //maximum number of iterations
int n_init_max = 6; //maximum number of different fragment length
int L_ini[n_init_max]; //fragment lengths, Lali, Lali/2, Lali/4 ... 4
int L_ini_min = 4;
if (Lali<L_ini_min) L_ini_min = Lali;
int n_init = 0, i_init;
for (i = 0; i<n_init_max - 1; i++)
{
n_init++;
L_ini[i] = (int)(Lali / pow(2.0, (double)i));
if (L_ini[i] <= L_ini_min)
{
L_ini[i] = L_ini_min;
break;
}
}
if (i == n_init_max - 1)
{
n_init++;
L_ini[i] = L_ini_min;
}
score_max = -1;
//find the maximum score starting from local structures superposition
int i_ali[kmax], n_cut;
int L_frag; //fragment length
int iL_max; //maximum starting position for the fragment
for (i_init = 0; i_init<n_init; i_init++)
{
L_frag = L_ini[i_init];
iL_max = Lali - L_frag;
i = 0;
while (1)
{
//extract the fragment starting from position i
ka = 0;
for (k = 0; k<L_frag; k++)
{
int kk = k + i;
r1[k][0] = xtm[kk][0];
r1[k][1] = xtm[kk][1];
r1[k][2] = xtm[kk][2];
r2[k][0] = ytm[kk][0];
r2[k][1] = ytm[kk][1];
r2[k][2] = ytm[kk][2];
k_ali[ka] = kk;
ka++;
}
//extract rotation matrix based on the fragment
Kabsch(r1, r2, L_frag, 1, &rmsd, t, u);
if (simplify_step != 1)
*Rcomm = 0;
do_rotation(xtm, xt, Lali, t, u);
//get subsegment of this fragment
d = local_d0_search - 1;
n_cut = score_fun8_standard(xt, ytm, Lali, d, i_ali, &score,
score_sum_method, score_d8, d0);
if (score>score_max)
{
score_max = score;
//save the rotation matrix
for (k = 0; k<3; k++)
{
t0[k] = t[k];
u0[k][0] = u[k][0];
u0[k][1] = u[k][1];
u0[k][2] = u[k][2];
}
}
//try to extend the alignment iteratively
d = local_d0_search + 1;
for (int it = 0; it<n_it; it++)
{
ka = 0;
for (k = 0; k<n_cut; k++)
{
m = i_ali[k];
r1[k][0] = xtm[m][0];
r1[k][1] = xtm[m][1];
r1[k][2] = xtm[m][2];
r2[k][0] = ytm[m][0];
r2[k][1] = ytm[m][1];
r2[k][2] = ytm[m][2];
k_ali[ka] = m;
ka++;
}
//extract rotation matrix based on the fragment
Kabsch(r1, r2, n_cut, 1, &rmsd, t, u);
do_rotation(xtm, xt, Lali, t, u);
n_cut = score_fun8_standard(xt, ytm, Lali, d, i_ali, &score,
score_sum_method, score_d8, d0);
if (score>score_max)
{
score_max = score;
//save the rotation matrix
for (k = 0; k<3; k++)
{
t0[k] = t[k];
u0[k][0] = u[k][0];
u0[k][1] = u[k][1];
u0[k][2] = u[k][2];
}
}
//check if it converges
if (n_cut == ka)
{
for (k = 0; k<n_cut; k++)
{
if (i_ali[k] != k_ali[k]) break;
}
if (k == n_cut) break;
}
} //for iteration
if (i<iL_max)
{
i = i + simplify_step; //shift the fragment
if (i>iL_max) i = iL_max; //do this to use the last missed fragment
}
else if (i >= iL_max) break;
}//while(1)
//end of one fragment
}//for(i_init
return score_max;
}
//Comprehensive TMscore search engine
// input: two vector sets: x, y
// an alignment invmap0[] between x and y
// simplify_step: 1 or 40 or other integers
// score_sum_method: 0 for score over all pairs
// 8 for socre over the pairs with dist<score_d8
// output: the best rotaion matrix t, u that results in highest TMscore
double detailed_search(double **r1, double **r2, double **xtm, double **ytm,
double **xt, double **x, double **y, int xlen, int ylen,
int invmap0[], double t[3], double u[3][3], int simplify_step,
int score_sum_method, double local_d0_search, double Lnorm,
double score_d8, double d0)
{
//x is model, y is template, try to superpose onto y
int i, j, k;
double tmscore;
double rmsd;
k=0;
for(i=0; i<ylen; i++)
{
j=invmap0[i];
if(j>=0) //aligned
{
xtm[k][0]=x[j][0];
xtm[k][1]=x[j][1];
xtm[k][2]=x[j][2];
ytm[k][0]=y[i][0];
ytm[k][1]=y[i][1];
ytm[k][2]=y[i][2];
k++;
}
}
//detailed search 40-->1
tmscore = TMscore8_search(r1, r2, xtm, ytm, xt, k, t, u, simplify_step,
score_sum_method, &rmsd, local_d0_search, Lnorm, score_d8, d0);
return tmscore;
}
double detailed_search_standard( double **r1, double **r2,
double **xtm, double **ytm, double **xt, double **x, double **y,
int xlen, int ylen, int invmap0[], double t[3], double u[3][3],
int simplify_step, int score_sum_method, double local_d0_search,
const bool& bNormalize, double Lnorm, double score_d8, double d0)
{
//x is model, y is template, try to superpose onto y
int i, j, k;
double tmscore;
double rmsd;
k=0;
for(i=0; i<ylen; i++)
{
j=invmap0[i];
if(j>=0) //aligned
{
xtm[k][0]=x[j][0];
xtm[k][1]=x[j][1];
xtm[k][2]=x[j][2];
ytm[k][0]=y[i][0];
ytm[k][1]=y[i][1];
ytm[k][2]=y[i][2];
k++;
}
}
//detailed search 40-->1
tmscore = TMscore8_search_standard( r1, r2, xtm, ytm, xt, k, t, u,
simplify_step, score_sum_method, &rmsd, local_d0_search, score_d8, d0);
if (bNormalize)// "-i", to use standard_TMscore, then bNormalize=true, else bNormalize=false;
tmscore = tmscore * k / Lnorm;
return tmscore;
}
//compute the score quickly in three iterations
double get_score_fast( double **r1, double **r2, double **xtm, double **ytm,
double **x, double **y, int xlen, int ylen, int invmap[],
double d0, double d0_search, double t[3], double u[3][3])
{
double rms, tmscore, tmscore1, tmscore2;
int i, j, k;
k=0;
for(j=0; j<ylen; j++)
{
i=invmap[j];
if(i>=0)
{
r1[k][0]=x[i][0];
r1[k][1]=x[i][1];
r1[k][2]=x[i][2];
r2[k][0]=y[j][0];
r2[k][1]=y[j][1];
r2[k][2]=y[j][2];
xtm[k][0]=x[i][0];
xtm[k][1]=x[i][1];
xtm[k][2]=x[i][2];
ytm[k][0]=y[j][0];
ytm[k][1]=y[j][1];
ytm[k][2]=y[j][2];
k++;
}
else if(i!=-1) PrintErrorAndQuit("Wrong map!\n");
}
Kabsch(r1, r2, k, 1, &rms, t, u);
//evaluate score
double di;
const int len=k;
double dis[len];
double d00=d0_search;
double d002=d00*d00;
double d02=d0*d0;
int n_ali=k;
double xrot[3];
tmscore=0;
for(k=0; k<n_ali; k++)
{
transform(t, u, &xtm[k][0], xrot);
di=dist(xrot, &ytm[k][0]);
dis[k]=di;
tmscore += 1/(1+di/d02);
}
//second iteration
double d002t=d002;
vector<double> dis_vec(dis, dis+n_ali);
sort(dis_vec.begin(), dis_vec.end());
if (d002t<dis_vec[2]) d002t=dis_vec[2];
dis_vec.clear();
while(1)
{
j=0;
for(k=0; k<n_ali; k++)
{
if(dis[k]<=d002t)
{
r1[j][0]=xtm[k][0];
r1[j][1]=xtm[k][1];
r1[j][2]=xtm[k][2];
r2[j][0]=ytm[k][0];
r2[j][1]=ytm[k][1];
r2[j][2]=ytm[k][2];
j++;
}
}
//there are not enough feasible pairs, relieve the threshold
if(j<3 && n_ali>3) d002t += 0.5;
else break;
}
if(n_ali!=j)
{
Kabsch(r1, r2, j, 1, &rms, t, u);
tmscore1=0;
for(k=0; k<n_ali; k++)
{
transform(t, u, &xtm[k][0], xrot);
di=dist(xrot, &ytm[k][0]);
dis[k]=di;
tmscore1 += 1/(1+di/d02);
}
//third iteration
d002t=d002+1;
vector<double> dis_vec(dis, dis+n_ali);
sort(dis_vec.begin(), dis_vec.end());
if (d002t<dis_vec[2]) d002t=dis_vec[2];
dis_vec.clear();
while(1)
{
j=0;
for(k=0; k<n_ali; k++)
{
if(dis[k]<=d002t)
{
r1[j][0]=xtm[k][0];
r1[j][1]=xtm[k][1];
r1[j][2]=xtm[k][2];
r2[j][0]=ytm[k][0];
r2[j][1]=ytm[k][1];
r2[j][2]=ytm[k][2];
j++;
}
}
//there are not enough feasible pairs, relieve the threshold
if(j<3 && n_ali>3) d002t += 0.5;
else break;
}
//evaluate the score
Kabsch(r1, r2, j, 1, &rms, t, u);
tmscore2=0;
for(k=0; k<n_ali; k++)
{
transform(t, u, &xtm[k][0], xrot);
di=dist(xrot, &ytm[k][0]);
tmscore2 += 1/(1+di/d02);
}
}
else
{
tmscore1=tmscore;
tmscore2=tmscore;
}
if(tmscore1>=tmscore) tmscore=tmscore1;
if(tmscore2>=tmscore) tmscore=tmscore2;
return tmscore; // no need to normalize this score because it will not be used for latter scoring
}
//perform gapless threading to find the best initial alignment
//input: x, y, xlen, ylen
//output: y2x0 stores the best alignment: e.g.,
//y2x0[j]=i means:
//the jth element in y is aligned to the ith element in x if i>=0
//the jth element in y is aligned to a gap in x if i==-1
double get_initial(double **r1, double **r2, double **xtm, double **ytm,
double **x, double **y, int xlen, int ylen, int *y2x,
double d0, double d0_search, const bool fast_opt,
double t[3], double u[3][3])
{
int min_len=getmin(xlen, ylen);
if(min_len<3) PrintErrorAndQuit("Sequence is too short <3!\n");
int min_ali= min_len/2; //minimum size of considered fragment
if(min_ali<=5) min_ali=5;
int n1, n2;
n1 = -ylen+min_ali;
n2 = xlen-min_ali;
int i, j, k, k_best;
double tmscore, tmscore_max=-1;
k_best=n1;
for(k=n1; k<=n2; k+=(fast_opt)?5:1)
{
//get the map
for(j=0; j<ylen; j++)
{
i=j+k;
if(i>=0 && i<xlen) y2x[j]=i;
else y2x[j]=-1;
}
//evaluate the map quickly in three iterations
//this is not real tmscore, it is used to evaluate the goodness of the initial alignment
tmscore=get_score_fast(r1, r2, xtm, ytm,
x, y, xlen, ylen, y2x, d0,d0_search, t, u);
if(tmscore>=tmscore_max)
{
tmscore_max=tmscore;
k_best=k;
}
}
//extract the best map
k=k_best;
for(j=0; j<ylen; j++)
{
i=j+k;
if(i>=0 && i<xlen) y2x[j]=i;
else y2x[j]=-1;
}
return tmscore_max;
}
void smooth(int *sec, int len)
{
int i, j;
//smooth single --x-- => -----
for (i=2; i<len-2; i++)
{
if(sec[i]==2 || sec[i]==4)
{
j=sec[i];
if (sec[i-2]!=j && sec[i-1]!=j && sec[i+1]!=j && sec[i+2]!=j)
sec[i]=1;
}
}
// smooth double
// --xx-- => ------
for (i=0; i<len-5; i++)
{
//helix
if (sec[i]!=2 && sec[i+1]!=2 && sec[i+2]==2 && sec[i+3]==2 &&
sec[i+4]!=2 && sec[i+5]!= 2)
{
sec[i+2]=1;
sec[i+3]=1;
}
//beta
if (sec[i]!=4 && sec[i+1]!=4 && sec[i+2]==4 && sec[i+3]==4 &&
sec[i+4]!=4 && sec[i+5]!= 4)
{
sec[i+2]=1;
sec[i+3]=1;
}
}
//smooth connect
for (i=0; i<len-2; i++)
{
if (sec[i]==2 && sec[i+1]!=2 && sec[i+2]==2) sec[i+1]=2;
else if(sec[i]==4 && sec[i+1]!=4 && sec[i+2]==4) sec[i+1]=4;
}
}
char sec_str(double dis13, double dis14, double dis15,
double dis24, double dis25, double dis35)
{
char s='C';
double delta=2.1;
if (fabs(dis15-6.37)<delta && fabs(dis14-5.18)<delta &&
fabs(dis25-5.18)<delta && fabs(dis13-5.45)<delta &&
fabs(dis24-5.45)<delta && fabs(dis35-5.45)<delta)
{
s='H'; //helix
return s;
}
delta=1.42;
if (fabs(dis15-13 )<delta && fabs(dis14-10.4)<delta &&
fabs(dis25-10.4)<delta && fabs(dis13-6.1 )<delta &&
fabs(dis24-6.1 )<delta && fabs(dis35-6.1 )<delta)
{
s='E'; //strand
return s;
}
if (dis15 < 8) s='T'; //turn
return s;
}
/* secondary structure assignment for protein:
* 1->coil, 2->helix, 3->turn, 4->strand */
void make_sec(double **x, int len, char *sec)
{
int j1, j2, j3, j4, j5;
double d13, d14, d15, d24, d25, d35;
for(int i=0; i<len; i++)
{
sec[i]='C';
j1=i-2;
j2=i-1;
j3=i;
j4=i+1;
j5=i+2;
if(j1>=0 && j5<len)
{
d13=sqrt(dist(x[j1], x[j3]));
d14=sqrt(dist(x[j1], x[j4]));
d15=sqrt(dist(x[j1], x[j5]));
d24=sqrt(dist(x[j2], x[j4]));
d25=sqrt(dist(x[j2], x[j5]));
d35=sqrt(dist(x[j3], x[j5]));
sec[i]=sec_str(d13, d14, d15, d24, d25, d35);
}
}
sec[len]=0;
}
/* a c d b: a paired to b, c paired to d */
bool overlap(const int a1,const int b1,const int c1,const int d1,
const int a2,const int b2,const int c2,const int d2)
{
return (a2>=a1&&a2<=c1)||(c2>=a1&&c2<=c1)||
(d2>=a1&&d2<=c1)||(b2>=a1&&b2<=c1)||
(a2>=d1&&a2<=b1)||(c2>=d1&&c2<=b1)||
(d2>=d1&&d2<=b1)||(b2>=d1&&b2<=b1);
}
/* find base pairing stacks in RNA*/
void sec_str(int len,char *seq, const vector<vector<bool> >&bp,
int a, int b,int &c, int &d)
{
int i;
for (i=0;i<len;i++)
{
if (a+i<len-3 && b-i>0)
{
if (a+i<b-i && bp[a+i][b-i]) continue;
break;
}
}
c=a+i-1;d=b-i+1;
}
/* secondary structure assignment for RNA:
* 1->unpair, 2->paired with upstream, 3->paired with downstream */
void make_sec(char *seq, double **x, int len, char *sec,const string atom_opt)
{
int ii,jj,i,j;
float lb=12.5; // lower bound for " C3'"
float ub=15.0; // upper bound for " C3'"
if (atom_opt==" C4'") {lb=14.0;ub=16.0;}
else if(atom_opt==" C5'") {lb=16.0;ub=18.0;}
else if(atom_opt==" O3'") {lb=13.5;ub=16.5;}
else if(atom_opt==" O5'") {lb=15.5;ub=18.5;}
else if(atom_opt==" P ") {lb=16.5;ub=21.0;}
float dis;
vector<bool> bp_tmp(len,false);
vector<vector<bool> > bp(len,bp_tmp);
bp_tmp.clear();
for (i=0; i<len; i++)
{
sec[i]='.';
for (j=i+1; j<len; j++)
{
if (((seq[i]=='u'||seq[i]=='t')&&(seq[j]=='a' ))||
((seq[i]=='a' )&&(seq[j]=='u'||seq[j]=='t'))||
((seq[i]=='g' )&&(seq[j]=='c'||seq[j]=='u'))||
((seq[i]=='c'||seq[i]=='u')&&(seq[j]=='g' )))
{
dis=sqrt(dist(x[i], x[j]));
bp[j][i]=bp[i][j]=(dis>lb && dis<ub);
}
}
}
// From 5' to 3': A0_var C0_var D0_var B0_var: A0_var paired to B0_var, C0_var paired to D0_var
vector<int> A0_var,B0_var,C0_var,D0_var;
for (i=0; i<len-2; i++)
{
for (j=i+3; j<len; j++)
{
if (!bp[i][j]) continue;
if (i>0 && j+1<len && bp[i-1][j+1]) continue;
if (!bp[i+1][j-1]) continue;
sec_str(len,seq, bp, i,j,ii,jj);
if (jj<i || j<ii)
{
ii=i;
jj=j;
}
A0_var.push_back(i);
B0_var.push_back(j);
C0_var.push_back(ii);
D0_var.push_back(jj);
}
}
//int sign;
for (i=0;i<A0_var.size();i++)
{
/*
sign=0;
if(C0_var[i]-A0_var[i]<=1)
{
for(j=0;j<A0_var.size();j++)
{
if(i==j) continue;
if((A0_var[j]>=A0_var[i]&&A0_var[j]<=C0_var[i])||
(C0_var[j]>=A0_var[i]&&C0_var[j]<=C0_var[i])||
(D0_var[j]>=A0_var[i]&&D0_var[j]<=C0_var[i])||
(B0_var[j]>=A0_var[i]&&B0_var[j]<=C0_var[i])||
(A0_var[j]>=D0_var[i]&&A0_var[j]<=B0_var[i])||
(C0_var[j]>=D0_var[i]&&C0_var[j]<=B0_var[i])||
(D0_var[j]>=D0_var[i]&&D0_var[j]<=B0_var[i])||
(B0_var[j]>=D0_var[i]&&B0_var[j]<=B0_var[i]))
{
sign=-1;
break;
}
}
}
if(sign!=0) continue;
*/
for (j=0;;j++)
{
if(A0_var[i]+j>C0_var[i]) break;
sec[A0_var[i]+j]='<';
sec[D0_var[i]+j]='>';
}
}
sec[len]=0;
/* clean up */
A0_var.clear();
B0_var.clear();
C0_var.clear();
D0_var.clear();
bp.clear();
}
//get initial alignment from secondary structure alignment
//input: x, y, xlen, ylen
//output: y2x stores the best alignment: e.g.,
//y2x[j]=i means:
//the jth element in y is aligned to the ith element in x if i>=0
//the jth element in y is aligned to a gap in x if i==-1
void get_initial_ss(bool **path, double **val,
const char *secx, const char *secy, int xlen, int ylen, int *y2x)
{
double gap_open=-1.0;
NWDP_TM(path, val, secx, secy, xlen, ylen, gap_open, y2x);
}
// get_initial5 in TMalign fortran, get_initial_local in TMalign c by yangji
//get initial alignment of local structure superposition
//input: x, y, xlen, ylen
//output: y2x stores the best alignment: e.g.,
//y2x[j]=i means:
//the jth element in y is aligned to the ith element in x if i>=0
//the jth element in y is aligned to a gap in x if i==-1
bool get_initial5( double **r1, double **r2, double **xtm, double **ytm,
bool **path, double **val,
double **x, double **y, int xlen, int ylen, int *y2x,
double d0, double d0_search, const bool fast_opt, const double D0_MIN)
{
double GL, rmsd;
double t[3];
double u[3][3];
double d01 = d0 + 1.5;
if (d01 < D0_MIN) d01 = D0_MIN;
double d02 = d01*d01;
double GLmax = 0;
int aL = getmin(xlen, ylen);
int *invmap = new int[ylen + 1];
// jump on sequence1-------------->
int n_jump1 = 0;
if (xlen > 250)
n_jump1 = 45;
else if (xlen > 200)
n_jump1 = 35;
else if (xlen > 150)
n_jump1 = 25;
else
n_jump1 = 15;
if (n_jump1 > (xlen / 3))
n_jump1 = xlen / 3;
// jump on sequence2-------------->
int n_jump2 = 0;
if (ylen > 250)
n_jump2 = 45;
else if (ylen > 200)
n_jump2 = 35;
else if (ylen > 150)
n_jump2 = 25;
else
n_jump2 = 15;
if (n_jump2 > (ylen / 3))
n_jump2 = ylen / 3;
// fragment to superimpose-------------->
int n_frag[2] = { 20, 100 };
if (n_frag[0] > (aL / 3))
n_frag[0] = aL / 3;
if (n_frag[1] > (aL / 2))
n_frag[1] = aL / 2;
// start superimpose search-------------->
if (fast_opt)
{
n_jump1*=5;
n_jump2*=5;
}
bool flag = false;
for (int i_frag = 0; i_frag < 2; i_frag++)