-
Notifications
You must be signed in to change notification settings - Fork 0
/
DisCovER.cpp
2839 lines (2575 loc) · 106 KB
/
DisCovER.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* DisCovER.cpp
*
* Created on:
* Author: Sutanu Bhattacharya
* @Version: 1.0 (Nov 7, 2020)
*
* Purpose: Distance- and orientation-based protein threading
*
* References: (1) S. Wu, Y. Zhang. MUSTER: Improving protein sequence profile-profile alignments by using multiple sources of structure information. Proteins: Structure, Function, and Bioinformatics 2008
* (2) S Ovchinnikov et al. Protein Structure Determination using Metagenome sequence data. (2017) Science. 355(6322):294–8.
* (3) D Bhattacharya et al. UniCon3D: de novo protein structure prediction using united-residue conformational search via stepwise, probabilistic sampling. (2016) Bioinformatics.
* (4) D Buchan, D Jones. EigenTHREADER: analogous protein fold recognition by efficient contact map threading. (2017) Bioinformatics.
* (5) Y. Zhang, http://zhanglab.ccmb.med.umich.edu/NW-align
*
*
* Inputs:
* 1. seq.fasta
* 2. seq.spd33 (output of SPIDER3) for query SS, SA, psi, phi
* 3. library for templates SS, SA, psi, phi, PDB, MTX, DEP and template list
* 4. seq.prf (sequence profile) for query protein
* 5. seq.mtx (query mtx profile)
* 6. query distance map (query.rr), orientation angles (omega.txt, theta.txt, phi.txt)
*
* Outputs:
* 1. top1.fasta : query-template (TOP1) alignment in MODELLER format
*
*/
#include <unistd.h>
#include <string>
#include <ctime>
#include <omp.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <vector>
#include <limits.h>
#include <cstring>
#include <cmath>
#include <map>
#include <iterator>
#include <set>
#include <algorithm>
#include <functional>
#include<bits/stdc++.h> //for upper and system call
#include <ctype.h>
#define VERSION "V1.0"
using namespace std;
struct OPTS {
std::string libdir; /* Template library directory */
std::string list; /* list of template IDs (file) */
std::string target; /*query*/
std::string tardir; /* query directory */
std::string qdist; /*directory containing predicted distance of the query */
std::string modeller;/*path to MODELLER*/
int nTop;/*number of TOP templates selceted by pure threadng */
float idcut; /* sequence identity cutoff to remove homologs */
};
struct ALIGN1_RESULT {
std::string alignment;//query-template alignment
int seq_number;
std::string template_name;
double score_align;
int idt;
double ialg;
double lena;
double iii;
};
// for CMO
typedef vector<vector<int> > mtx_int;
typedef vector<vector<double> > mtx_double;
typedef vector<int> vec_int;
typedef vector<double> vec_double;
typedef vector<char> vec_char;
typedef vector<bool> vec_bool;
typedef vector<string> vec_string;
struct ALIGN2_CMO {
std::string templ; //template
std::string alignmentQ; //query-template alignment getting from CMO
std::string alignmentT;
int aln_len; //alignment length
float score; //contact score+gap score+(profile score)
float zscore_thread; //zscore from pure threading
};
bool GetOpts(int argc, char *argv[], OPTS &opts);
void PrintOpts(const OPTS &opts);
void PrintCap(const OPTS &opts);
string read_fasta(string file);
int get8to3ss(string ssParm);
void read_query_ss_sa_angles(string file, string x, int mysec[], float mysa[], float mypsi[], float myphi[]);
float getSolAcc(char aa, string sa);
void read_mtx(string file, double mtx[][21]);
void read_prf(string file, double mtx[][21]);
void read_template_ss_sa_angles(string dssp, int ss[], float sa[], float psi[], float phi[]);
ALIGN1_RESULT align1(int seq_num, string template_name, string x, string y, double mymtx[][21], double umtxb[][21], int mysec[],
float mysa[], float mypsi[], float myphi[], int iysec[], float iysa[], float iypsi[], float iyphi[],
double myprf[][21], double prflib[][21], float p[]);
int getResNum(char a);
void read_DEP_profile(string dep_prf, double dep[][21], string seq);
void Blosum62Matrix(int imut[][24]);
double NeedlemanWunsch(string f1, string f2, int gap_open,int gap_extn);
void smooth_ss(int seq_length,int ss[]);
string trim(const string& str);
//for CMO
double exp_fast(double x){
x = 1 + x/1024;
x *= x; x *= x; x *= x; x *= x;
x *= x; x *= x; x *= x; x *= x;
x *= x; x *= x;
return x;
}
double gaussian(double mean, double stdev, double x){return exp_fast(-pow((x - mean),2)/(2*(pow(stdev,2))));}
vec_int align(vec_double &gap_a, vec_double &gap_b, double &gap_e, mtx_double &sco_mtx, mtx_double &p_sco_mtx);
double Falign(double *sco_mtx, int rows, int cols);
void mod_gap(vec_double &gap, vec_char &ss, double &gap_ss_w)
{
for(int i = 0; i < ss.size()-1; i++){
if((ss[i] == 'H' && ss[i+1] == 'H') || (ss[i] == 'E' && ss[i+1] == 'E')){gap[i] *= gap_ss_w;}
}
}
double sepw(double sep){if(sep <= 4){return 0.50;}else if(sep == 5){return 0.75;}else{return log10(1+sep);}}
struct point3d {
double x;
double y;
double z;
};
struct pdbInfo {
int res_num;
point3d ca;
point3d cb;
point3d n;
};
struct dist_bin_5_to_20{
mtx_double m5,m5_5,m6,m6_5,m7,m7_5,m8,m8_5,m9,m9_5,m10,m10_5,m11,m11_5,m12,m12_5,m13,m13_5,m14,m14_5;
mtx_double m15,m15_5,m16,m16_5,m17,m17_5,m18,m18_5,m19,m19_5,m20,m20_5;
};
struct dist_bin_6_to_14_2{
mtx_double m6,m8,m10,m12,m14;
};
struct dist_bin_5_to_15_1{
mtx_double m5,m6,m7,m8,m9,m10,m11,m12,m13,m14,m15;
};
struct dihedral_angles{
mtx_double a_15,a_30,a_45,a_60,a_75,a_90,a_105,a_120,a_135,a_150,a_165,a_180,a_195,a_210,a_225,a_240,a_255,a_270,a_285,a_300,a_315,a_330,a_345,a_360;
};
struct planar_angles{
mtx_double a_15,a_30,a_45,a_60,a_75,a_90,a_105,a_120,a_135,a_150,a_165,a_180;
};
vec_int load_data (string file, int sep_cutoff, mtx_double &mtx, vec_int &vec_div, vec_int &vec, mtx_int &vec_i, mtx_double &prf, int size, vec_char &ss,dist_bin_5_to_20 &rr, double n_bins);
void ini_prf_SCO(mtx_double &P_SCO, double &prf_w, mtx_double &prf_a, vec_char &aa_a, mtx_double &prf_b, vec_char &aa_b);
void ini_SCO(double sep_x, double sep_y, mtx_double &SCO,vec_int &vec_a_div,vec_int &vec_b_div,vec_int &vec_a,vec_int &vec_b,mtx_int &vec_a_i,mtx_int &vec_b_i,mtx_double &mtx_a,mtx_double &mtx_b,dist_bin_5_to_20 query_mtx,dist_bin_5_to_20 temp_mtx,dihedral_angles omega,dihedral_angles theta,planar_angles phi,map<int,pdbInfo> &all_atomsT);
vec_int mod_SCO(double do_it, vec_double &gap_a, vec_double &gap_b, double gap_e, mtx_double &SCO, mtx_double &P_SCO,vec_int &vec_a_div,vec_int &vec_b_div,vec_int &vec_a, vec_int &vec_b,mtx_int &vec_a_i, mtx_int &vec_b_i,mtx_double &mtx_a, mtx_double &mtx_b,dist_bin_5_to_20 query_mtx,dist_bin_5_to_20 temp_mtx,dihedral_angles omega,dihedral_angles theta,planar_angles phi,map<int,pdbInfo> &all_atomsT);
void chk (vec_double &gap_a, vec_double &gap_b, double &gap_e_w, double& con_sco,double& gap_sco,double& prf_sco,vec_int& vec_a_div,mtx_int& vec_a_i,mtx_double& mtx_a,mtx_double& mtx_b,vec_int& a2b,mtx_double &P_SCO,dist_bin_5_to_20 query_mtx,dist_bin_5_to_20 temp_mtx,dihedral_angles omega,dihedral_angles theta,planar_angles phi,map<int,pdbInfo> &all_atomsT);
ALIGN2_CMO cmo(string templ, string qdist, string libdir, float zscore_thread, string target, string seqA, int lenQ, vec_int m2n_a, mtx_double mtx_a, vec_int vec_a,vec_int vec_a_div,mtx_int vec_a_i,mtx_double prf_a, vec_char ss_a, dist_bin_5_to_20 query_mtx,dihedral_angles omega_all_bins, dihedral_angles theta_all_bins, planar_angles phi_all_bins);
void ini_prf_SCO(mtx_double &P_SCO, double &prf_w, mtx_double &prf_a, vec_char &aa_a, mtx_double &prf_b, vec_char &aa_b);
//MODELLER
void same_ali(string model);
void generateModeller_SS_CM_Script(float t);
void generateBasicModellerScript(string temp);
void run_modeller(string top1_temp,string modeller,string tempDIR);
/*inter-residue orientation */
const double PI = std::atan(1.0)*4;
const double RAD2DEG = 180/PI;
const double DEG2RAD = PI/180;
map<int,pdbInfo> pdb2coord(string file);
point3d getDifference(point3d &p1, point3d &p2);
double getDistance(point3d &p1, point3d &p2);
double getDotProduct(point3d &p1, point3d &p2);
point3d getCrossProduct(point3d &p1, point3d &p2);
double getAngle(point3d &p1, point3d &p2, point3d &p3);
double getDihedral(point3d &p1, point3d &p2, point3d &p3, point3d &p4);
void read_orientation_query(string file,dihedral_angles &a,int lenQ);
void read_orientation_phi_query(string file,planar_angles &phi,int lenQ);
/** main() */
int main(int argc, char *argv[]) {
OPTS opts = { "", "", "", "", "", "", 50, 0.30 }; //default seq id cutoff 0.30
if (!GetOpts(argc, argv, opts)) {
PrintOpts(opts);
return 1;
}
PrintCap(opts);
int seq_num = 1; //track number of templates processed
float idcut = opts.idcut; // sequence identity cutoff
int nTop = opts.nTop; //number of TOP templates selected by pure threadng
string libdir = opts.libdir;
string MTX = libdir + "MTX/";
string SEQ = libdir + "seq/";
string DEP = libdir + "DEP/";
string dssp = libdir + "dssp/";
string PDB = libdir + "PDB/";
string qdist = opts.qdist;
// read the query sequence
string x = "";
x = read_fasta("seq.fasta");
int x_len = x.length();
// Read query SS, SA, psi, phi from .spd33 file
int mysec [x_len];
std::fill_n(mysec, x_len, 3); // default 3
float mysa [x_len];
std::fill_n(mysa, x_len, 0);
float mypsi [x_len];
std::fill_n(mypsi, x_len, 0);
float myphi [x_len];
std::fill_n(myphi, x_len, 0);
read_query_ss_sa_angles("seq.spd33",x,mysec, mysa, mypsi, myphi);
smooth_ss(x_len,mysec);
//read query MTX file
double myprf_mtx [x_len][21];
memset(myprf_mtx, 0, x_len * 21 * sizeof(double));
read_mtx("seq.mtx", myprf_mtx);
//read query frequency profile
double myprf [x_len][21];
memset(myprf, 0, x_len * 21 * sizeof(double));
read_prf("seq.prf", myprf);
/* read template IDs */
std::vector<std::string> listB;
ifstream in(opts.list.c_str());
if(!in) {
printf("Error: cannot open template list file '%s'\n", opts.list.c_str());
return 1;
}
string name = "";
while (std::getline(in, name)) {
if(name.size() > 0) {
listB.push_back(name);
}
}
in.close();
//weights 0,g0,ge,ss,sa,shift,hydro,phi,0,1,depthLib,psi
float weight[12] = {0, 7.01, 0.55, 0.66, 1.6, -0.99, 0.31, 0.19, 0, 1.0, 0.39, 0.19}; //weights, p[0] is place holder
//read PDBs one by one and calculate alignments
ofstream fw; // open a file in write mode.
fw.open("align.dat");
// calculate alignment score for each PDB
ofstream fw_rst;
fw_rst.open("rst.dat");
map<string,ALIGN1_RESULT> template_align1;//store return values of align1 method for each template
for (unsigned i = 0; i < listB.size(); i++) { // for schedule(dynamic) will access PDBs one by one as opposed to parallel
string templatePDB = listB[i]; // make sure whether all libraries has this template
string dep_file = DEP + templatePDB + ".dep";
string dssp_fil = dssp + templatePDB + ".dssp";
string seq_fil = SEQ + templatePDB + ".seq";
string mtx_file = MTX + templatePDB + ".mtx";
string y = read_fasta(seq_fil);
int seq_length = y.length();
//exclude templates with seq id > id cutoff to the query protein
string f1 = x;
string f2 = y;
transform(f1.begin(), f1.end(), f1.begin(), ::toupper);
transform(f2.begin(), f2.end(), f2.begin(), ::toupper);
if (idcut<1.0){
double identity = NeedlemanWunsch(f2,f1,-11,-1); //gap_open=-11,gap_extn=-1;
if (identity > idcut){continue;}
}
//read template MTX file
double umtxb [seq_length][21];
memset(umtxb, 0, seq_length * 21 * sizeof(double));
read_mtx(mtx_file, umtxb);
//read template DEP file
double dep_prof [seq_length][21];
memset(dep_prof, 0, seq_length * 21 * sizeof(double));
read_DEP_profile(dep_file, dep_prof, y);
//Read template SS, SA, psi, phi from .dssp file
int libsec [seq_length];
std::fill_n(libsec, seq_length, 3);
float libsa [seq_length];
std::fill_n(libsa, seq_length, 0);
float libpsi [seq_length];
std::fill_n(libpsi, seq_length, 0);
float libphi [seq_length];
std::fill_n(libphi, seq_length, 0);
read_template_ss_sa_angles(dssp_fil, libsec, libsa, libpsi, libphi);
smooth_ss(seq_length,libsec); //smooth SS according to EigenTHREADER
//calculate alignment score and query-template alignment
ALIGN1_RESULT result;
result.template_name = "", result.alignment = "";
result.seq_number = 0, result.score_align = 0, result.idt = 0, result.ialg = 0, result.lena = 0, result.iii = 0;
result = align1(seq_num, templatePDB, x, y, myprf_mtx, umtxb, mysec, mysa, mypsi, myphi, libsec, libsa, libpsi, libphi, myprf, dep_prof, weight);
template_align1[result.template_name] = result;
seq_num += 1;
}
//calculate z-score for each template
//instead of seq_num-1 because 0th index will be ignored
double v1 [seq_num];
std::fill_n(v1, seq_num, 0);
double v2 [seq_num];
std::fill_n(v2, seq_num, 0);
double sequence_id [seq_num];
std::fill_n(sequence_id, seq_num, 0);
double z1 [seq_num];
std::fill_n(z1, seq_num, 0);
double z2 [seq_num];
std::fill_n(z2, seq_num, 0);
string ppa_alignments = "", tempName = "";
string p [seq_num];
map<string,ALIGN1_RESULT>::iterator itr; //iterate map
int i = 0, k;
for (itr = template_align1.begin(); itr != template_align1.end(); ++itr) {
tempName = itr->first;
ALIGN1_RESULT align1_out = itr->second;
i += 1;
p[i] = tempName; //template
v1[i] = align1_out.score_align; //raw score
v2[i] = 0.000; //reverse raw score
sequence_id[i] = align1_out.idt; //seq id
z1[i] = v1[i] / align1_out.lena; //full alignment
z2[i] = v1[i] / align1_out.iii; //partial alignment
//save alignment and score details in align.dat and rst.dat files respectively
ppa_alignments = align1_out.alignment;
size_t found = ppa_alignments.find(" "); //find space between structure and query alignments
fw << ">P1;" + tempName + "\n";
fw << "structureX:" + tempName + " :1 : : : : t1 : t2 : t3 :\n";
for (k = 0; k < found; k++){
fw << ppa_alignments[k];
}
fw << "*\n";
fw << ">P1;query\nsequence:query:1 : : : : q1 : q2 : q3 :\n";
for (k = found+1; k < ppa_alignments.length(); k++){ //ignoring the space
fw << ppa_alignments[k];
}
fw << "*\n\n";
fw_rst << std::to_string(i) + "\t" + tempName + "\t" + std::to_string(v1[i]) + "\t" + std::to_string(v2[i]) + "\t" +
std::to_string(sequence_id[i]) + "\t" + std::to_string(align1_out.lena) + "\t" +
std::to_string(align1_out.iii) + "\n";
}
fw.close();
fw_rst.close();
int N_hit = i;
float z1_a = 0.0, z1_a2 = 0.0, z2_a = 0.0, z2_a2 = 0.0;
for(i = 1; i<N_hit+1; i++){
z1_a += z1[i];
z1_a2 += pow(z1[i], 2);
z2_a += z2[i];
z2_a2 += pow(z2[i], 2);
}
z1_a /= float(N_hit);
z1_a2 /= float(N_hit);
float z1_sd = sqrt(z1_a2 - pow(z1_a, 2));
map<string,float> z1_zscore;
map<string,int> TT1;
for(i = 1; i<N_hit+1; i++){
z1_zscore[p[i]] = float (z1_a - z1[i]) / z1_sd;
TT1[p[i]] = i;
}
//sort map in descending order
// Declaring the type of Predicate that accepts 2 pairs and return a bool
typedef std::function<bool(std::pair<std::string, float>, std::pair<std::string, float>)> Comparator;
// Defining a lambda function to compare two pairs. It will compare two pairs using second field
Comparator compFunctor =
[](std::pair<std::string, float> elem1 ,std::pair<std::string, float> elem2)
{
return elem1.second > elem2.second;
};
//sort z1_zscore in descending order
std::set<std::pair<std::string, float>, Comparator> z1_zscore_keys(z1_zscore.begin(), z1_zscore.end(), compFunctor);
//sort z2_zscore in descending order
z2_a /= float(N_hit);
z2_a2 /= float(N_hit);
float z2_sd = sqrt(z2_a2 - pow(z2_a, 2));
map<string,float> z2_zscore;
map<string,int> TT2;
for(i = 1; i<N_hit+1; i++){
z2_zscore[p[i]] = float (z2_a - z2[i]) / z2_sd;
TT2[p[i]] = i;
}
std::set<std::pair<std::string, float>, Comparator> z2_zscore_keys(z2_zscore.begin(), z2_zscore.end(), compFunctor);
string index1, index2;
for (std::pair<std::string, float> element : z1_zscore_keys){
index1 = element.first;
break;
}
for (std::pair<std::string, float> element : z2_zscore_keys){
index2 = element.first;
break;
}
int score_flag=1; //always first scheme unless the following:
if( (sequence_id[TT1[index1]]) + 0.01 <= sequence_id[TT2[index2]] ){
score_flag=2;
} else if ( (sequence_id[TT2[index2]]) + 0.01 <= sequence_id[TT1[index1]] ) {
score_flag=1;
}
//cout<<"check...\n";
//read query rr and orientation angles
int lowest_bin=5, highest_bin=20;
double bin_length=0.5, n_bins=31;
mtx_double mtx_a; vec_int vec_a; vec_int vec_a_div; mtx_int vec_a_i; mtx_double prf_a; vec_char ss_a;
dist_bin_5_to_20 query_mtx;
//dist_bin_6_to_14_2 query_mtx;
//dist_bin_5_to_15_1 query_mtx;
int sep_cutoff = 5;
//cout<<"read query.rr ";
vec_int m2n_a = load_data("query.rr",sep_cutoff,mtx_a,vec_a_div,vec_a,vec_a_i,prf_a,x_len,ss_a,query_mtx,n_bins);
//cout << "done..\nread omega ";
dihedral_angles om;
read_orientation_query("omega.txt",om,x_len);
//cout << "done..\n";
dihedral_angles th;
read_orientation_query("theta.txt",th,x_len);
planar_angles ph;
read_orientation_phi_query("phi.txt",ph,x_len);
//cout << "theta and phi are done..\n";
//find best-fit templates
int i_t = 0;
float zscore_value=0.0;
string top_template_name="";
string top1_template="";
map<string,ALIGN2_CMO> template_align2;
int shrink_pool = nTop;
for(i = 1; i<shrink_pool+1; i++){
ALIGN2_CMO result2;
if(score_flag == 0) {
continue;
} else if (score_flag == 1) {
int index_set = 0;
for (std::pair<std::string, float> element : z1_zscore_keys){
top_template_name = element.first;
if(i==1){top1_template = top_template_name;}
zscore_value = element.second;
if (index_set == i-1){
//cout << top_template_name << "\n";
result2 = cmo(top_template_name,qdist,libdir,zscore_value,opts.target,x ,x_len,m2n_a,mtx_a,vec_a,vec_a_div,vec_a_i,prf_a,ss_a,query_mtx,om,th,ph);
break;
}
index_set += 1;
}
} else {
int index_set = 0;
for (std::pair<std::string, float> element : z2_zscore_keys){
top_template_name = element.first;
if(i==1){top1_template = top_template_name;}
zscore_value = element.second;
if (index_set == i-1){
//cout << top_template_name << "\n";
result2 = cmo(top_template_name,qdist,libdir,zscore_value,opts.target,x ,x_len,m2n_a,mtx_a,vec_a,vec_a_div,vec_a_i,prf_a,ss_a,query_mtx,om,th,ph);
break;
}
index_set += 1;
}
}
template_align2[result2.templ] = result2;
}
template_align1.clear(); //clear map
z1_zscore.clear();
z2_zscore.clear();
TT1.clear();
TT2.clear();
// rank templates based on z_contact_score
tempName = "";
//calculate z-score for each template using contact score and alignment length
double v_cmo[shrink_pool+1];
std::fill_n(v_cmo, shrink_pool+1, 0);
double z_cmo[shrink_pool+1];
std::fill_n(z_cmo, shrink_pool+1, 0);
double z_thread_sc[shrink_pool+1];
std::fill_n(z_thread_sc, shrink_pool+1, 0);
map<string,ALIGN2_CMO>::iterator itr2; //iterate map
ALIGN2_CMO align2_out;
int i_cmo = 0;
string p_cmo[shrink_pool+1];
for (itr2 = template_align2.begin(); itr2 != template_align2.end(); ++itr2) {
tempName = itr2->first;
align2_out = itr2->second;
if(align2_out.aln_len <= 0){continue;}
i_cmo += 1;
p_cmo[i_cmo] = tempName;
v_cmo[i_cmo] = align2_out.score; //contact score
z_cmo[i_cmo] = v_cmo[i_cmo] / float(align2_out.aln_len);
z_thread_sc[i_cmo] = align2_out.zscore_thread; //z_score from pure threading
}
float z_a_cmo = 0.0, z_a2_cmo = 0.0;
for(i_cmo = 1; i_cmo<shrink_pool+1; i_cmo++){
z_a_cmo += z_cmo[i_cmo];
z_a2_cmo += pow(z_cmo[i_cmo], 2);
}
z_a_cmo /= float(shrink_pool);
z_a2_cmo /= float(shrink_pool);
float z_sd_cmo = sqrt(z_a2_cmo - pow(z_a_cmo, 2));
map<string,float> z_zscore_cmo;
for(i_cmo = 1; i_cmo<shrink_pool+1; i_cmo++){
z_zscore_cmo[p_cmo[i_cmo]] = float(z_cmo[i_cmo] - z_a_cmo) / z_sd_cmo;
}
//sort map in descending order
string top1_templ_CMO = "";
//combine z_score = w1*z_score + w2*z_contact_score
map<string,float> combine_zscore;
for(i_cmo = 1; i_cmo<shrink_pool+1; i_cmo++){
combine_zscore[p_cmo[i_cmo]] = float(1 * z_thread_sc[i_cmo] + 1.0 * z_zscore_cmo[p_cmo[i_cmo]]);
}
//sort map in descending order
std::set<std::pair<std::string, float>, Comparator> sorted_z_zscore_cmo(combine_zscore.begin(), combine_zscore.end(), compFunctor);
//print score for each template
cout << "#Template\tZ_final\n";
for (std::pair<std::string, float> element : sorted_z_zscore_cmo){
top1_templ_CMO = element.first;
float z_cmo = element.second;
align2_out = template_align2[top1_templ_CMO];
//template\t zscore_thread\t contact score\t alignemnt length\t z_score_cmo
cout << top1_templ_CMO << "\t\t" << z_cmo << "\n";
}
// extract query-template alignment for the TOP1 template
for (std::pair<std::string, float> element : sorted_z_zscore_cmo){
top1_templ_CMO = element.first;
align2_out = template_align2[top1_templ_CMO];
// extract alignment between query and TOP1 template
ofstream fw_aln;
fw_aln.open("top1.fasta");
ofstream fw_aln2;
fw_aln2.open("top1_aln.fasta");
string alignmentQ = align2_out.alignmentQ;
string alignmentT = align2_out.alignmentT;
fw_aln << ">P1;query\nsequence:query:1 : : : : q1 : q2 : q3 :\n";
fw_aln2 << ">"+opts.target+"\n";
for(k=0; k<alignmentQ.length(); k++){
fw_aln << alignmentQ[k];
fw_aln2 << alignmentQ[k];
}
fw_aln << "*\n";
fw_aln2 << "\n";
fw_aln << ">P1;" + top1_templ_CMO + "\n";
fw_aln2 << ">" + top1_templ_CMO + "\n";
fw_aln << "structureX:" + top1_templ_CMO + " :1 : : : : t1 : t2 : t3 :\n";
for(k=0; k<alignmentT.length(); k++){
fw_aln << alignmentT[k];
fw_aln2 << alignmentT[k];
}
fw_aln << "*\n";
fw_aln2.close();
fw_aln.close();
break;
}
template_align2.clear();
z_zscore_cmo.clear();
combine_zscore.clear();
cout << "\n#DisCovER is done. First-ranked query-template alignment is available in 'top1.fasta' file.\n";
/*run MODELLER if MODELLER path is given*/
string modeller = opts.modeller;
if(modeller != ""){
//cout << "#Running Modeller ...\n";
//run_modeller(top1_templ_CMO, modeller, PDB);
}
//system("rm con*.rr");
//system("rm *.dat");
system("mv top1_aln.fasta top1.fasta");
//finish date/time
time_t timer;
time(&timer);
struct tm* tm_info = localtime(&timer);
char buf[100];
strftime(buf, 26, "%Y:%m:%d / %H:%M:%S", tm_info);
printf("# %s\n", std::string(80, '-').c_str());
printf("# %20s : %s\n", "end date/time", buf);
printf("# %s\n", std::string(80, '-').c_str());
return 0;
}
/** check whether a file exists */
bool fexists(const std::string& filename) {
std::ifstream ifile(filename.c_str());
return (bool)ifile;
}
/* Smooth DSSP secondary structure definitions */
void smooth_ss(int seq_length,int ss[]){
for(int i=2; i<seq_length-1; i++){
if(ss[i-1] == ss[i+1]){ss[i] = ss[i-1];}
}
}
/** extract contacts from PDB */
struct AtomRecord
{
char chainId; /* Chain identifier */
int resNum; /* Residue sequence number */
double x, y, z; /* Orthogonal coordinates for X, Y, Z */
};
double eucl_dist(AtomRecord A, AtomRecord B){
double x = A.x - B.x;
double y = A.y - B.y;
double z = A.z - B.z;
return sqrt(x * x + y * y + z * z);
}
void pdb2con(string file){
string line;
ifstream in(file);
//ofstream fw_5,fw_6,fw_7,fw_8,fw_9,fw_10,fw_11,fw_12,fw_13,fw_14,fw_15;
AtomRecord atom_rec;
map<int,AtomRecord> all_atoms;
ofstream fw;
fw.open("template.rr");
//fw_5.open("con5T.rr");fw_7.open("con7T.rr");fw_9.open("con9T.rr");fw_11.open("con11T.rr");fw_13.open("con13T.rr");fw_15.open("con15T.rr");
//fw_6.open("con6T.rr");fw_8.open("con8T.rr");fw_10.open("con10T.rr");fw_12.open("con12T.rr");fw_14.open("con14T.rr");
while(getline(in,line)){
if (trim(line.substr(0,5).c_str()) == "ATOM" && trim(line.substr(12,4).c_str()) == "CA"){
atom_rec.chainId = line[21];
atom_rec.resNum = stoi(trim(line.substr(22,4).c_str()));
atom_rec.x = stof(trim(line.substr(30,8).c_str()));
atom_rec.y = stof(trim(line.substr(38,8).c_str()));
atom_rec.z = stof(trim(line.substr(46,8).c_str()));
}
all_atoms[atom_rec.resNum] = atom_rec;
}
in.close();
int nRes = all_atoms.size();
for (int i = 1; i <= nRes-5; i++){
for (int j = i+5; j <= nRes; j++){
if(all_atoms[i].chainId == all_atoms[j].chainId){
double dist = eucl_dist(all_atoms[i],all_atoms[j]);
if (dist <= 5.0){fw << i << " " << j << " 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n";continue;}
if (dist <= 5.5){fw << i << " " << j << " 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n";continue;}
if (dist <= 6.0){fw << i << " " << j << " 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n";continue;}
if (dist <= 6.5){fw << i << " " << j << " 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n";continue;}
if (dist <= 7.0){fw << i << " " << j << " 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n";continue;}
if (dist <= 7.5){fw << i << " " << j << " 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n";continue;}
if (dist <= 8.0){fw << i << " " << j << " 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n";continue;}
if (dist <= 8.5){fw << i << " " << j << " 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n";continue;}
if (dist <= 9.0){fw << i << " " << j << " 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n";continue;}
if (dist <= 9.5){fw << i << " " << j << " 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n";continue;}
if (dist <= 10.0){fw << i << " " << j << " 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n";continue;}
if (dist <= 10.5){fw << i << " " << j << " 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n";continue;}
if (dist <= 11.0){fw << i << " " << j << " 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n";continue;}
if (dist <= 11.5){fw << i << " " << j << " 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n";continue;}
if (dist <= 12.0){fw << i << " " << j << " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n";continue;}
if (dist <= 12.5){fw << i << " " << j << " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n";continue;}
if (dist <= 13.0){fw << i << " " << j << " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n";continue;}
if (dist <= 13.5){fw << i << " " << j << " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n";continue;}
if (dist <= 14.0){fw << i << " " << j << " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1\n";continue;}
if (dist <= 14.5){fw << i << " " << j << " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1\n";continue;}
if (dist <= 15.0){fw << i << " " << j << " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1\n";continue;}
if (dist <= 15.5){fw << i << " " << j << " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1\n";continue;}
if (dist <= 16.0){fw << i << " " << j << " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1\n";continue;}
if (dist <= 16.5){fw << i << " " << j << " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1\n";continue;}
if (dist <= 17.0){fw << i << " " << j << " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1\n";continue;}
if (dist <= 17.5){fw << i << " " << j << " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1\n";continue;}
if (dist <= 18.0){fw << i << " " << j << " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1\n";continue;}
if (dist <= 18.5){fw << i << " " << j << " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1\n";continue;}
if (dist <= 19.0){fw << i << " " << j << " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1\n";continue;}
if (dist <= 19.5){fw << i << " " << j << " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1\n";continue;}
if (dist <= 20.0){fw << i << " " << j << " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1\n";continue;}
/*
if (dist <= 6.0){fw_6 << i << " " << j << " 0 6 " << dist << "\n";}
if (dist <= 7.0){fw_7 << i << " " << j << " 0 7 " << dist << "\n";}
if (dist <= 8.0){fw_8 << i << " " << j << " 0 8 " << dist << "\n";}
if (dist <= 9.0){fw_9 << i << " " << j << " 0 9 " << dist << "\n";}
if (dist <= 10.0){fw_10 << i << " " << j << " 0 10 " << dist << "\n";}
if (dist <= 11.0){fw_11 << i << " " << j << " 0 11 " << dist << "\n";}
if (dist <= 12.0){fw_12 << i << " " << j << " 0 12 " << dist << "\n";}
if (dist <= 13.0){fw_13 << i << " " << j << " 0 13 " << dist << "\n";}
if (dist <= 14.0){fw_14 << i << " " << j << " 0 14 " << dist << "\n";}
if (dist <= 15.0){fw_15 << i << " " << j << " 0 15 " << dist << "\n";}
*/
}
}
}
/*
fw_6.close();
fw_8.close();
fw_10.close();
fw_12.close();
fw_14.close();
*/
fw.close();
all_atoms.clear();
}
//Contact Map Overlap
ALIGN2_CMO cmo(string templ, string qdist, string libdir, float zscore_thread, string target, string x, int lenQ, vec_int m2n_a, mtx_double mtx_a, vec_int vec_a,vec_int vec_a_div,mtx_int vec_a_i,mtx_double prf_a, vec_char ss_a, dist_bin_5_to_20 query_mtx,dihedral_angles omega_all_bins, dihedral_angles theta_all_bins, planar_angles phi_all_bins){
string SEQ = libdir + "seq/";
string DEP = libdir + "DEP/";
string dssp = libdir + "dssp/";
string PDB = libdir + "PDB/";
string distT = PDB + templ + ".pdb";
pdb2con(distT);//create con6T.rr,con8T.rr,con10T.rr,con12T.rr,con14T.rr
string seqB = SEQ + templ + ".seq"; //template sequence
string y = read_fasta(seqB);
int lenT = y.length(); //eliminating leading *
map<int,pdbInfo> all_atomsT;
all_atomsT = pdb2coord(distT);
//query profile
double prfA [lenQ][21];
memset(prfA, 0, lenQ * 21 * sizeof(double));
read_prf("seq.prf", prfA);
//template profile
string dep_file = DEP + templ + ".dep";
double prfB [lenT][21];
memset(prfB, 0, lenT * 21 * sizeof(double));
read_DEP_profile(dep_file, prfB, y);
double gap_open = -1;
double gap_ext = -0.01;
int sep_cutoff = 6;
int iter = 20;
bool use_gap_ss = true;
double gap_ss_w = 2;
bool use_prf = true;
double prf_w = 1;
double gap_ext_w = fabs(gap_ext)/fabs(gap_open);
int size_a = mtx_a.size();
prf_a.resize(lenQ,vector<double>(20,0));
for(int i=1;i<lenQ;i++){
for (int k = 1; k <= 20; k++) {
prf_a[i-1][k-1] = prfA[i][k];
}
}
ss_a.resize(lenQ,'X');
// Read query SS, SA, psi, phi from .spd33 file
int mysec [lenQ]; std::fill_n(mysec, lenQ, 3); // default 3
float mysa [lenQ]; std::fill_n(mysa, lenQ, 0);
float mypsi [lenQ]; std::fill_n(mypsi, lenQ, 0);
float myphi [lenQ]; std::fill_n(myphi, lenQ, 0);
read_query_ss_sa_angles("seq.spd33",x,mysec, mysa, mypsi, myphi);
smooth_ss(lenQ,mysec);
for(int i=1;i<lenQ;i++){
if (mysec[i] == 1){ss_a[i-1] = 'H';}
else if(mysec[i] == 2){ss_a[i-1] = 'E';}
else{ss_a[i-1] = 'C';}
}
// if use_gap_ss on, modify the gap penalities
vec_double gap_a(size_a,gap_open);if(use_gap_ss == true){mod_gap(gap_a,ss_a,gap_ss_w);}
// load data from contact map B
mtx_double mtx_b; vec_int vec_b; vec_int vec_b_div; mtx_int vec_b_i; mtx_double prf_b; vec_char ss_b;
dist_bin_5_to_20 template_mtx;
//cout << "\ntemplate.rr";
vec_int m2n_b = load_data("template.rr",sep_cutoff,mtx_b,vec_b_div,vec_b,vec_b_i,prf_b,lenT,ss_b,template_mtx,31);
int size_b = mtx_b.size();
prf_b.resize(lenT,vector<double>(20,0));
for(int i=1;i<lenT;i++){
for (int k = 1; k <= 20; k++) {
prf_b[i-1][k-1] = prfB[i][k];
}
}
ss_b.resize(lenT,'X');
string dssp_fil = dssp + templ + ".dssp";
int libsec [lenT]; std::fill_n(libsec, lenT, 3);
float libsa [lenT]; std::fill_n(libsa, lenT, 0);
float libpsi [lenT]; std::fill_n(libpsi, lenT, 0);
float libphi [lenT]; std::fill_n(libphi, lenT, 0);
read_template_ss_sa_angles(dssp_fil, libsec, libsa, libpsi, libphi);
smooth_ss(lenT,libsec);
for(int i=1;i<lenT;i++){
if (libsec[i] == 1){ss_b[i-1] = 'H';}
else if(libsec[i] == 2){ss_b[i-1] = 'E';}
else{ss_b[i-1] = 'C';}
}
// if use_gap_ss on, modify the gap penalities
vec_double gap_b(size_b,gap_open);if(use_gap_ss == true){mod_gap(gap_b,ss_b,gap_ss_w);}
// if use_prf on, initialize profile SCO matrix
vec_char aa_a;vec_char aa_b;
aa_a.resize(lenQ,'X');
for(int i=1;i<lenQ;i++){
aa_a[i] = x[i];
}
aa_b.resize(lenT,'X');
for(int i=1;i<lenT;i++){
aa_b[i] = y[i];
}
mtx_double P_SCO;if(use_prf == true){ini_prf_SCO(P_SCO,prf_w,prf_a,aa_a,prf_b,aa_b);}
//cout << "\nstart wlignmnet";
// STARTING ALIGNMENT!!!
// keeping track of the BEST alignment
int max_sep_x = 0;
int max_sep_y = 0;
int max_g_e = 0;
double con_max = -1;
double gap_max = 0;
double prf_max = 0;
vec_int a2b_max;
// try different sep (sequence seperation difference) penalities
vec_double sep_x_steps {0,1,2}; // (constant, linear, quadratic)
for(int sx = 0; sx < sep_x_steps.size(); sx++){
double sep_x = sep_x_steps[sx];
//try different scaling factors for sep penalities
vec_double sep_y_steps {1,2,4,8,16,32};
for(int sy = 0; sy < sep_y_steps.size(); sy++){
double sep_y = sep_y_steps[sy];
// Get initial score matrix
mtx_double C_SCO(size_a,vector<double>(size_b,0));
//cout << "\ncall INIT "<<sep_x<<" "<<sep_y;
//cout << "\ncheck....";
ini_SCO(sep_x,sep_y,C_SCO,vec_a_div,vec_b_div,vec_a,vec_b,vec_a_i,vec_b_i,mtx_a,mtx_b,query_mtx,template_mtx,omega_all_bins,theta_all_bins,phi_all_bins,all_atomsT);
//cout << "\nINIT done..";
// try different gap_ext penalities!
vec_double gap_e_steps {5,10,100,1000};
for(int g_e = 0; g_e < gap_e_steps.size(); g_e++){
//cout << "\ncall INIT "<<sep_x<<" "<<sep_y<<" "<<gap_e_steps[g_e];
double gap_e = 1/gap_e_steps[g_e];
// restart SCO matrix
mtx_double SCO = C_SCO;
// get alignment (a2b mapping) after X iterations
vec_int a2b = mod_SCO(iter,gap_a,gap_b,gap_e,SCO,P_SCO,vec_a_div,vec_b_div,vec_a,vec_b,vec_a_i,vec_b_i,mtx_a,mtx_b,query_mtx,template_mtx,omega_all_bins,theta_all_bins,phi_all_bins,all_atomsT);
// compute number of contacts/gaps made
double con_sco = 0;
double gap_sco = 0;
double prf_sco = 0;
chk(gap_a,gap_b,gap_ext_w,con_sco,gap_sco,prf_sco,vec_a_div,vec_a_i,mtx_a,mtx_b,a2b,P_SCO,query_mtx,template_mtx,omega_all_bins,theta_all_bins,phi_all_bins,all_atomsT);
// save if BEST!
//cout << "if best ";
if(con_sco+gap_sco+prf_sco > con_max+gap_max+prf_max){
max_sep_x = sep_x;
max_sep_y = sep_y;
max_g_e = g_e;
con_max = con_sco;
gap_max = gap_sco;
prf_max = prf_sco;
a2b_max = a2b;
}
//cout << "save ";
}
}
}
//cout << "\nReport the BEST score ";
// Report the BEST score
//cout << "\ncheck ..";
ALIGN2_CMO result;
//cout << "\na2b_max";
int aln_len = 0;for(int ai = 0; ai < size_a; ai++){int bi = a2b_max[ai];if(bi != -1){aln_len++;}}
float score = (float) (con_max+gap_max+prf_max); //con_max+gap_max+prf_max
//cout << "\nscore: "<<score;
result.aln_len = aln_len;
result.score = score;
result.zscore_thread = zscore_thread;
result.alignmentQ = "";
result.alignmentT = "";
result.templ = templ;
//cout << "\nalignment..";
// generate alignment
string seqQ = x;
string seqT = y;
string target_seq_dash = "";
string template_seq_dash = "";
int j, i, k;
int prevI = 0; int prevJ = 0;
for(int a = 0; a < size_a; a++){
int b = a2b_max[a];
if(b != -1){
i = m2n_a[a]; j = m2n_b[b];
if (prevI<(i-1)){
for(int k1 = prevI+1; k1<i; k1++){
target_seq_dash += seqQ[k1];
template_seq_dash += "-";
}
prevI = i;
}
if (prevJ<(j-1)){
for(int k1 = prevJ+1; k1<j; k1++){
template_seq_dash += seqT[k1];
target_seq_dash += "-";
}
prevJ = j;
}
template_seq_dash += seqT[j];
target_seq_dash += seqQ[i];
prevI = i;
prevJ = j;
}
}
if (j<seqT.length()){
for (k = j+1; k < seqT.length(); k++){
template_seq_dash += seqT[k];
target_seq_dash += "-";
}
}
if (i<seqQ.length()){
for (k = i+1; k < seqQ.length(); k++){
template_seq_dash += "-";
target_seq_dash += seqQ[k];