25 if ( a <= 0 )
return 0;
26 else return log(a)/log(2.0L);
41 for ( mol = molgroup->
mol; mol; mol = mol->
next )
45 cerr <<
"Sequence " << refseq <<
" not found!" << endl;
50 if ( mol->
seq[i] ==
'-' ) {
75 long i, j, k, n(0), nid, overlap, nmol(0), maxlen;
76 long idsum(0), idssum(0), overlapsum(0), overlapssum(0);
77 double idavg, overlapavg;
82 cout <<
"Aligned identity analysis:" << endl;
85 cout <<
"Seq1\tSeq2\tIdentity\tnID\tOverlap\tName1\tName2" << endl;
87 for ( mol1 = molgroup->
mol; mol1; mol1 = mol1->
next ) nmol++;
92 for ( i=1, mol1 = molgroup->
mol->
next; mol1; i++, mol1 = mol1->
next ) {
93 for ( j=0, mol2 = molgroup->
mol; mol2 != mol1 && j<i; j++, mol2 = mol2->next ) {
98 for ( k=0; k<maxlen; k++ ) {
99 if ( molgroup->
seqflag[k] > 0 &&
100 mol1->
seq[k] !=
'-' &&
101 mol2->
seq[k] !=
'-' ) {
103 if ( mol1->
seq[k] == mol2->
seq[k] ) nid++;
108 overlapsum += overlap;
109 overlapssum += overlap*overlap;
112 if ( overlap ) mat[i][j] = mat[j][i] = nid*1.0/overlap;
114 cout << i+1 <<
tab << j+1 <<
tab << fixed << setprecision(3) << setw(8) <<
115 mat[i][j] <<
tab << nid <<
tab << overlap <<
tab <<
116 mol1->
id <<
tab << mol2->
id << endl;
121 overlapavg = overlapsum*1.0/n;
124 cout <<
"Average identical residues: " << idavg <<
" (" <<
125 sqrt(idssum*1.0/n - idavg*idavg) <<
")" << endl;
126 cout <<
"Average overlap: " << overlapavg <<
" (" <<
127 sqrt(overlapssum*1.0/n - overlapavg*overlapavg) <<
")" << endl << endl;
154 long i, j, k, thelen, overlap, nmol(0);
156 double simsum(0), percentsim(0);
159 long nres = simat->
n;
161 float* sim_mat = simat->
m;
163 for ( mol1 = molgroup->
mol; mol1; mol1 = mol1->
next ) nmol++;
166 cout <<
"Aligned similarity analysis:" << endl;
167 cout <<
"Similar residue threshold: " << threshold << endl;
168 cout <<
"Number of sequences: " << nmol << endl << endl;
172 cout <<
"Seq1\tSeq2\tSimilarity\tFraction\tOverlap\tName1\tName2" << endl;
177 for ( i=1, mol1 = molgroup->
mol->
next; mol1; i++, mol1 = mol1->
next ) {
179 for ( j=0, mol2 = molgroup->
mol; mol2 != mol1; j++, mol2 = mol2->
next ) {
185 simsum = percentsim = 0.0;
188 cout <<
"i=" << i <<
" j=" << j <<
" thelen=" << thelen << endl;
189 for ( k=0; k<thelen; k++ ) {
190 if ( molgroup->
seqflag[k] > 0 &&
191 ( mol1->
seq[k] !=
'-' ) && ( mol2->
seq[k] !=
'-' ) ) {
196 if ( ii > -1 && jj > -1 ) {
197 simsum += sim_mat[ii*nres+jj];
198 if ( sim_mat[ii*nres+jj] >= threshold )
204 cout << k <<
tab << simsum <<
tab << overlap << endl;
211 mat[i][j] = simsum*1.0/overlap;
212 percentsim /= overlap;
213 }
else mat[i][j] = 0;
214 mat[j][i] = mat[i][j];
216 cout << i+1 <<
tab << j+1 <<
tab << fixed << setprecision(3) << setw(8) <<
217 mat[i][j] <<
tab << setw(8) << percentsim <<
tab << overlap <<
tab <<
218 mol1->
id <<
tab << mol2->
id << endl;
239 cout <<
"Selecting sequences of length " << minlen <<
" - " << maxlen << endl;
241 for ( mol = molgroup->
mol; mol; mol = mol->
next ) {
242 for ( i=len=0; i<mol->
seq.
length(); ++i )
243 if ( mol->
seq[i] !=
'-' ) len++;
244 if ( len >= minlen && len <=maxlen ) {
253 cout <<
"Selected: " << n << endl << endl;
268 if ( ref > mat.
rows() )
return mat.
rows();
270 long i(ref-1), j, n(0);
273 for ( j = 1, mol = molgroup->
mol; j < ref && mol; ++j ) ;
276 cout <<
"Selecting based on sequence " << ref <<
":" << endl;
277 cout <<
"Sequence ID: " << mol->
id << endl;
278 cout <<
"Cutoff: " << cutoff << endl << endl;
281 for ( j = 0, mol = molgroup->
mol; j < mat.
rows() && mol; mol = mol->
next, ++j )
282 if ( i != j && mat[i][j] < cutoff ) {
290 cout <<
"Selected: " << n << endl << endl;
367 for ( i = 0, mol = molgroup->
mol; mol; mol = mol->
next )
368 if ( mol->
sel == 0 ) ++i;
371 cout <<
"Deleting " << i <<
" sequences" << endl << endl;
376 vector<long> del(mat.
rows(), 0);
377 for ( i = 0, mol = molgroup->
mol; i < mat.
rows() && mol; mol = mol->
next, ++i )
378 if ( mol->
sel == 0 ) del[i] = 1;
379 for ( i = mat.
rows() - 1; i >= 0; --i )
383 for ( mol = pmol = molgroup->
mol; mol; ) {
384 if ( mol->
sel < 1 ) {
385 if ( mol == molgroup->
mol ) {
386 molgroup->
mol = pmol = mol->
next;
400 for ( i = 0, mol = molgroup->
mol; mol; mol = mol->
next ) ++i;
403 cout <<
"Number of sequences retained: " << i << endl << endl;
404 cout <<
"Number of sequences retained: " << mat.
rows() << endl << endl;
431 long len(3*molgroup->
maxlen);
432 char* profseq =
new char[len];
433 char* prof =
new char[100000];
436 cout <<
"Profile:" << endl;
438 for ( i=0, aptr=profseq; i<molgroup->
maxlen; i++, aptr+=3 ) {
440 for ( j=0; j<3; j++ ) reslist[j] =
' ';
441 for ( mol = molgroup->
mol; mol && notdone; mol = mol->
next ) {
443 if ( mol->
seq[i] ==
'-' ) {
444 memset(aptr,
'-', 3);
447 for ( j=0; j<3 && reslist[j] !=
' ' && mol->
seq[i] != reslist[j]; j++ ) ;
448 if ( j<3 ) reslist[j] = mol->
seq[i];
450 memset(aptr,
'x', 3);
454 if ( notdone ) memcpy(aptr, reslist, 3);
458 for ( i=g=x=0, aptr=profseq, aptr2=prof; i<molgroup->
maxlen; i++, aptr+=3 ) {
459 if ( *aptr ==
'-' ) g++;
460 else if ( *aptr ==
'x' ) x++;
462 if ( aptr2 != prof ) *aptr2++ =
'-';
465 snprintf(aptr2, len,
"x(%ld)%n", x, &n);
467 snprintf(aptr2, len,
"x(%ld,%ld)%n", x, x+g, &n);
472 if ( aptr[1] ==
' ' ) {
476 for ( j=0; j<3 && aptr[j] !=
' '; j++ ) *aptr2++ = aptr[j];
485 cout << prof << endl;
487 char* profile =
new char[aptr2-prof+1];
488 memcpy(profile, prof, aptr2-prof);
489 profile[aptr2-prof] = 0;
497 static int resinfo_compare(
const void *x,
const void *y)
502 if ( ri1->
info < ri2->
info )
return 1;
529 long i, j, k, ii, n, nres;
535 for ( nres = 0, rt_curr = rt; rt_curr; rt_curr = rt_curr->
next ) nres++;
538 for ( i = 0, rt_curr = rt; rt_curr; rt_curr = rt_curr->
next, i++ )
539 code1[i] = rt_curr->
c;
544 cout <<
"DEBUG seq_aligned_information: code=" << code1 << endl;
548 for ( nmol = 0, mol = molgroup->
mol; mol; mol = mol->
next ) nmol++;
551 for ( i=0; i<molgroup->
maxlen; i++ ) npos += molgroup->
seqflag[i];
553 long igap = code1.
index(
'-');
554 double* fsum =
new double[npos];
555 double* info =
new double[npos];
556 double* freq =
new double[nres*npos];
558 char* pattern =
new char[nres*npos];
559 char* seq =
new char[npos + 1];
560 for ( i=0; i<npos; i++ ) fsum[i] = info[i] = seq[i] = 0;
562 for ( i=0; i<nres*npos; i++ ) freq[i] = pattern[i] = 0;
565 cout <<
"Information content analysis:" << endl;
566 cout <<
"Sequences: " << nmol << endl;
567 cout <<
"Alignment positions: " << npos << endl;
568 cout <<
"Moving average window: " << window << endl;
573 for ( mol = molgroup->
mol; mol; mol = mol->
next ) {
575 while ( j < mol->seq.length() ) {
579 if ( ii > -1 ) freq[nres*k+ii] += 1;
589 for ( j=0; j<molgroup->
maxlen; j++ ) {
591 memset(ri, 0, nres*
sizeof(
ResInfo));
593 for ( i=0; i<nres; i++ ) {
595 fgap += freq[nres*k+i];
597 fsum[k] += freq[nres*k+i];
601 for ( i=0; i<nres; i++ )
if ( i != igap ) {
602 ri[i].
res[0] = code1[i];
603 ri[i].
info = freq[nres*k+i]*
log_2(freq[nres*k+i])/fsum[k];
604 info[k] += ri[i].
info;
607 info[k] += log(20.0/fsum[k])/log(2.0);
609 qsort((
void *) ri, nres,
sizeof(
ResInfo),
610 (
int (*)(
const void *,
const void *)) resinfo_compare);
611 for ( i=0; i<nres; i++ ) {
612 pattern[nres*k+i] = ri[i].res[0];
613 freq[nres*k+i] = ri[i].info;
615 seq[k] = mol->
seq[j];
625 cout <<
" Pos\tBits\tMovAvg\tnSeq\tPattern" << endl;
626 for ( k=0; k<npos; k++ ) {
627 cout << k+1 <<
tab << info[k] <<
tab << movavg[k] <<
tab << fsum[k] <<
tab;
628 for ( i=0; i<nres; i++ )
if ( freq[nres*k+i] > 0.1 )
629 cout << pattern[nres*k+i];
633 for ( k=0; k<npos; k++ ) {
634 if ( k > 0 ) cout <<
"-";
635 for ( i=n=0; i<nres; i++ )
if ( freq[nres*k+i] > 0.1 ) n++;
639 if ( n > 1 ) cout <<
"[";
640 for ( i=0; i<nres; i++ )
if ( freq[nres*k+i] > 0.1 )
641 cout << pattern[nres*k+i];
642 if ( n > 1 ) cout <<
"]";
653 ps_seq_info(psfile, title, nres, npos, movavg, fsum, per_avg,
682 int window,
double threshold,
Bstring& hphobfile,
Bstring& psfile)
687 for ( nres = 0, rt_curr = rt; rt_curr; rt_curr = rt_curr->
next ) nres++;
688 char* code1 =
new char[nres+1];
689 for ( i = 0, rt_curr = rt; rt_curr; rt_curr = rt_curr->
next, i++ )
690 code1[i] = rt_curr->
c;
694 cout <<
"Average aligned hydrophobicity:" << endl;
696 double* nseq =
new double[molgroup->
maxlen];
697 double* avgHP =
new double[molgroup->
maxlen];
698 int* HPseg =
new int[molgroup->
maxlen];
701 for ( i=0; i<molgroup->
maxlen; i++ ) nseq[i] = avgHP[i] = HPseg[i] = 0;
703 for ( mol = molgroup->
mol; mol; mol = mol->
next ) {
705 if ( mol->
seq[j] !=
'-' ) {
706 for ( rt_curr = rt; rt_curr && mol->
seq[j] != rt_curr->
c; rt_curr = rt_curr->
next ) ;
707 if ( mol->
seq[j] == rt_curr->
c ) {
708 avgHP[j] += -rt_curr->
hphob;
718 for ( j=0; j<molgroup->
maxlen; j++ )
719 if ( nseq[j] > 0 ) avgHP[j] /= nseq[j];
722 cout <<
"nRes\tavgHP\tintHP\tnSeq\tRes" << endl;
727 for ( j=0; j<molgroup->
maxlen; j++ ) {
731 cout << i <<
tab << avgHP[j] <<
tab << sum <<
tab << nseq[j] <<
tab << mol->
seq[j] << endl;
736 cout << endl <<
"Segment\tStart\tEnd\tLength\tsumHP" << endl;
738 int k, m, sum_flag(0), start[1000], end[1000], newstart, newend, nmol(0);
739 double max, sumHP[1000];
741 for ( nmol = 0, mol = molgroup->
mol; mol; mol = mol->
next ) nmol++;
743 if ( threshold < 0.5 ) threshold = 0.5;
744 if ( threshold > 1 ) threshold = 1;
748 for ( j=1; j<molgroup->
maxlen; j++ ) {
750 if ( nseq[j] >= threshold && nseq[j-1] < threshold ) {
751 if ( sumHP[i] > 20 ) i++;
756 if ( sum_flag && nseq[j] < threshold ) {
762 for ( k=start[i]; k<end[i]; k++ ) {
764 for ( m=k; m<=end[i]; m++ ) {
777 for ( k=start[i]; k<=end[i]; k++ ) HPseg[k] = 1;
778 cout << i+1 <<
tab << start[i]+1 <<
tab << end[i]+1 <<
tab <<
779 end[i] - start[i] + 1 <<
tab << sumHP[i] << endl;
782 if ( sum_flag ) sumHP[i] += avgHP[j];
794 Bstring title(
"Hydrophobicity");
823 if ( start > end ) swap(start, end);
824 if ( start < 0 ) start = 0;
825 if ( end > win - 1 ) end = win - 1;
828 cout <<
"Frequency analysis:" << endl;
831 cout <<
"Window: " << win << endl;
832 cout <<
"Start - end: " << start <<
" - " << end << endl;
836 int half_win = win/2;
837 double real, imag, amp, phi, dphi;
841 for ( j=0; j<
length-win; j++ ) {
843 for ( k=start; k<=end; k++ ) {
846 for ( l=0; l<win; l++ ) {
848 real += data[l+j]*cos(phi);
849 imag += data[l+j]*sin(phi);
851 amp = sqrt(real*real + imag*imag);
852 phi = atan2(imag, real);
853 if ( amp < 0.001 ) phi = 0;
854 dphi = phi -
TWOPI*j*k/win;
865 return transform_data;
870 char m1, m2, igap =
'-';
871 long i, k, l, nseq, overlap, identity;
875 for ( nseq=0, mol1 = molgroup->
mol; mol1; mol1 = mol1->
next ) nseq++;
877 vector<double> weight(nseq*nseq, 0);
880 for ( k=1, mol1 = molgroup->
mol->
next; k<nseq; k++, mol1 = mol1->next ) {
881 for ( l=0, mol2 = molgroup->
mol; l<k; l++, mol2 = mol2->next ) {
882 overlap = identity = 0;
883 for ( i=0; i<molgroup->
maxlen; i++ ) {
886 if ( m1 != igap && m2 != igap ) {
888 if ( m1 == m2 ) identity++;
891 weight[k*nseq+l] = (overlap - identity)*1.0/molgroup->
maxlen;
893 cout <<
"k=" << k <<
" l=" << l <<
" weight=" << weight[k*nseq+l] << endl;
940 for ( number=0, mol1 = molgroup->
mol; mol1; mol1 = mol1->
next ) number++;
941 for ( refseqnum=0, molref = molgroup->
mol; molref; molref = molref->
next, ++refseqnum )
945 cerr <<
"Sequence " << refseqid <<
" not found!" << endl;
950 vector<char> selseq(
length*number, 0);
953 cout <<
"DEBUG seq_correlated_mutation: length=" <<
length <<
" number=" << number << endl;
955 int m1, m2, ir, ntot;
956 int total = number*(number-1)/2;
957 double sum, ssum, sigma2, maxCC(-1e30);
958 vector<double> avg(
length, 0);
959 vector<double> std(
length, 0);
960 vector<double> num(
length, 0);
961 vector<double> score(
length*number*number, 0);
967 float* sim_mat = simat->
m;
969 int igap = code1.
index(
'-');
972 cout <<
"DEBUG seq_correlated_mutation: total=" << total << endl;
976 for ( i=ir=0; i<
length; i++ ) {
980 for ( k=1, mol1 = molgroup->
mol->
next; k<number; k++, mol1 = mol1->next ) {
982 for ( l=0, mol2 = molgroup->
mol; l<k; l++, mol2 = mol2->next ) {
984 h = (ir*number + k)*number + l;
986 if ( m1 != igap && m2 != igap ) {
987 score[h] = sim_mat[m2*nres + m1];
989 ssum += score[h]*score[h];
991 }
else score[h] = -999;
995 avg[ir] = std[ir] = 0;
998 std[ir] = sqrt(ssum/total - avg[ir]*avg[ir]);
1000 refseq[ir] = refseq[i];
1001 for ( j=0, mol1 = molgroup->
mol; j<number; j++, mol1 = mol1->next )
1002 selseq[ir*number+j] = mol1->
seq[i];
1004 cout <<
"i=" << ir <<
" avg=" << avg[ir] <<
" std=" << std[ir] <<
" num=" << num[ir] << endl;
1011 cout <<
"Correlated mutation analysis:" << endl;
1012 cout <<
"Matrix size: " <<
length <<
" x " <<
length << endl;
1013 cout <<
"Reference sequence: " << molref->
id <<
" (" << refseqnum + 1 <<
")" << endl << endl;
1014 cout << endl <<
"Res1\tNum1\tRes2\tNum2\tTotal\tCorr" << endl;
1021 for ( i=0; i<
length; i++ ) {
1022 for ( j=0; j<=i; j++ ) {
1024 if ( num[i] > 2 && num[j] > 2 ) {
1025 if ( std[i] && std[j] ) {
1026 for ( k=1; k<number; k++ ) {
1027 for ( l=0; l<k; l++ ) {
1028 m1 = (i*number + k)*number + l;
1029 m2 = (j*number + k)*number + l;
1030 if ( score[m1] > -100 && score[m2] > -100 ) {
1031 mat[i][j] += weight[k*number+l]*
1032 (score[m1] - avg[i])*(score[m2] - avg[j]);
1038 mat[i][j] /= total*std[i]*std[j];
1041 if ( i == j ) mat[i][j] = 1;
1042 else if ( maxCC < mat[i][j] ) maxCC = mat[i][j];
1043 sigma2 += mat[i][j] * mat[i][j];
1044 if ( mat[i][j] > cutoff && i != j ) {
1045 cout << refseq[j] <<
tab << j+1 <<
tab <<
1046 refseq[i] <<
tab << i+1 <<
tab << ntot <<
tab << mat[i][j] << endl;
1048 for ( k=0; k<number; k++ )
1049 cout << selseq[j*number+k];
1051 for ( k=0; k<number; k++ )
1052 cout << selseq[i*number+k];
1057 mat[j][i] = mat[i][j];
1061 cout << endl <<
"Correlations reported: " << ir << endl;
1062 cout <<
"Maximum off-diagonal coefficient: " << maxCC << endl << endl;