Bsoft  2.1.0
Bernard's software package
seq_analysis.cpp
Go to the documentation of this file.
1 
9 #include "seq_analysis.h"
10 #include "seq_util.h"
11 #include "ps_sequence.h"
12 #include "moving_average.h"
13 #include "linked_list.h"
14 #include "utilities.h"
15 
16 // Declaration of global variables
17 extern int verbose; // Level of output to the screen
18 
19 // Structure for sorting residues by information content
20 struct ResInfo { char res[4]; double info; } ;
21 
22 
23 double log_2(double a)
24 {
25  if ( a <= 0 ) return 0;
26  else return log(a)/log(2.0L);
27 }
28 
36 long seq_limit(Bmolgroup* molgroup, Bstring& refseq)
37 {
38  long i, n(0);
39  Bmolecule* mol;
40 
41  for ( mol = molgroup->mol; mol; mol = mol->next )
42  if ( mol->id.contains(refseq) ) break;
43 
44  if ( !mol ) {
45  cerr << "Sequence " << refseq << " not found!" << endl;
46  return molgroup->maxlen;
47  }
48 
49  for ( i=0; i<mol->seq.length(); ++i ) {
50  if ( mol->seq[i] == '-' ) {
51  molgroup->seqflag[i] = 0;
52  } else {
53  molgroup->seqflag[i] = 1;
54  n++;
55  }
56  }
57 
58  return n;
59 }
60 
74 {
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;
78  Bmolecule* mol1;
79  Bmolecule* mol2;
80 
81  if ( verbose & VERB_LABEL )
82  cout << "Aligned identity analysis:" << endl;
83 
84  if ( verbose & VERB_PROCESS )
85  cout << "Seq1\tSeq2\tIdentity\tnID\tOverlap\tName1\tName2" << endl;
86 
87  for ( mol1 = molgroup->mol; mol1; mol1 = mol1->next ) nmol++;
88 
89  // Initialize an image structure to hold the results
90  Matrix mat(nmol, nmol);
91 
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 ) {
94  n++;
95  nid = overlap = 0;
96  maxlen = mol1->seq.length();
97  if ( maxlen > mol2->seq.length() ) maxlen = mol2->seq.length();
98  for ( k=0; k<maxlen; k++ ) {
99  if ( molgroup->seqflag[k] > 0 &&
100  mol1->seq[k] != '-' &&
101  mol2->seq[k] != '-' ) {
102  overlap++;
103  if ( mol1->seq[k] == mol2->seq[k] ) nid++;
104  }
105  }
106  idsum += nid;
107  idssum += nid*nid;
108  overlapsum += overlap;
109  overlapssum += overlap*overlap;
110 // k = i*nmol + j;
111 // if ( overlap ) pimg->set(k, nid*1.0/overlap);
112  if ( overlap ) mat[i][j] = mat[j][i] = nid*1.0/overlap;
113  if ( verbose & VERB_PROCESS )
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;
117  }
118  }
119 
120  idavg = idsum*1.0/n;
121  overlapavg = overlapsum*1.0/n;
122 
123  if ( verbose & VERB_LABEL ) {
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;
128  }
129 
130  return mat;
131 }
132 
152 Matrix seq_aligned_similarity(Bmolgroup* molgroup, double threshold, Bresidue_matrix* simat)
153 {
154  long i, j, k, thelen, overlap, nmol(0);
155  long ii, jj;
156  double simsum(0), percentsim(0);
157  Bmolecule *mol1, *mol2;
158 
159  long nres = simat->n;
160  Bstring code1 = simat->c;
161  float* sim_mat = simat->m;
162 
163  for ( mol1 = molgroup->mol; mol1; mol1 = mol1->next ) nmol++;
164 
165  if ( verbose & VERB_LABEL ) {
166  cout << "Aligned similarity analysis:" << endl;
167  cout << "Similar residue threshold: " << threshold << endl;
168  cout << "Number of sequences: " << nmol << endl << endl;
169  }
170 
171  if ( verbose & VERB_PROCESS )
172  cout << "Seq1\tSeq2\tSimilarity\tFraction\tOverlap\tName1\tName2" << endl;
173 
174  // Initialize an image structure to hold the results
175  Matrix mat(nmol, nmol);
176 
177  for ( i=1, mol1 = molgroup->mol->next; mol1; i++, mol1 = mol1->next ) {
178 // seq1 = mol1->seq.c_str();
179  for ( j=0, mol2 = molgroup->mol; mol2 != mol1; j++, mol2 = mol2->next ) {
180 // seq2 = mol2->seq.c_str();
181 // thelen = strlen(seq1);
182 // if ( thelen > strlen(seq2) ) thelen = strlen(seq2);
183  thelen = mol1->seq.length();
184  if ( thelen > mol2->seq.length() ) thelen = mol2->seq.length();
185  simsum = percentsim = 0.0;
186  overlap = 0;
187  if ( verbose & VERB_DEBUG )
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] != '-' ) ) {
192 // ii = find_char_index(code1, mol1->seq[k]);
193 // jj = find_char_index(code1, mol2->seq[k]);
194  ii = code1.index(mol1->seq[k]);
195  jj = code1.index(mol2->seq[k]);
196  if ( ii > -1 && jj > -1 ) {
197  simsum += sim_mat[ii*nres+jj];
198  if ( sim_mat[ii*nres+jj] >= threshold )
199  percentsim += 1;
200  overlap++;
201  }
202  }
203  if ( verbose & VERB_DEBUG )
204  cout << k << tab << simsum << tab << overlap << endl;
205  }
206 // if ( overlap ) {
207 // pimg->set(i*nmol+j, simsum*1.0/overlap);
208 // percentsim /= overlap;
209 // } else pimg->set(i*nmol+j, 0);
210  if ( overlap ) {
211  mat[i][j] = simsum*1.0/overlap;
212  percentsim /= overlap;
213  } else mat[i][j] = 0;
214  mat[j][i] = mat[i][j];
215  if ( verbose & VERB_PROCESS )
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;
219  }
220  }
221  cout << endl;
222 
223  return mat;
224 }
225 
233 long seq_select(Bmolgroup* molgroup, long minlen, long maxlen)
234 {
235  long i, n(0), len;
236  Bmolecule* mol;
237 
238  if ( verbose )
239  cout << "Selecting sequences of length " << minlen << " - " << maxlen << endl;
240 
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 ) {
245  mol->sel = 1;
246  n++;
247  } else {
248  mol->sel = 0;
249  }
250  }
251 
252  if ( verbose )
253  cout << "Selected: " << n << endl << endl;
254 
255  return n;
256 }
257 
266 long seq_select(Bmolgroup* molgroup, Matrix mat, long ref, double cutoff)
267 {
268  if ( ref > mat.rows() ) return mat.rows();
269 
270  long i(ref-1), j, n(0);
271  Bmolecule* mol;
272 
273  for ( j = 1, mol = molgroup->mol; j < ref && mol; ++j ) ;
274 
275  if ( verbose ) {
276  cout << "Selecting based on sequence " << ref << ":" << endl;
277  cout << "Sequence ID: " << mol->id << endl;
278  cout << "Cutoff: " << cutoff << endl << endl;
279  }
280 
281  for ( j = 0, mol = molgroup->mol; j < mat.rows() && mol; mol = mol->next, ++j )
282  if ( i != j && mat[i][j] < cutoff ) {
283  mol->sel = 0;
284  } else {
285  mol->sel = 1;
286  n++;
287  }
288 
289  if ( verbose )
290  cout << "Selected: " << n << endl << endl;
291 
292  return n;
293 }
294 /*
295 long seq_select(Bmolgroup* molgroup, Matrix mat, long ref, double cutoff)
296  {
297  if ( ref > mat.rows() ) return -1;
298 
299  long i(ref-1), j;
300  Bmolecule* mol, *pmol;
301 
302  for ( j = 1, mol = molgroup->mol; j < ref && mol; ++j ) ;
303 
304  if ( verbose ) {
305  cout << "Selecting based on sequence " << ref << ":" << endl;
306  cout << "Sequence ID: " << mol->id << endl;
307  cout << "Cutoff: " << cutoff << endl << endl;
308  }
309 
310  // cout << mat << endl;
311 
312  for ( j = 0, mol = molgroup->mol; j < mat.rows() && mol; mol = mol->next, ++j )
313  if ( i != j && mat[i][j] < cutoff ) mol->sel = 0;
314  else mol->sel = 1;
315 
316  for ( j = 0, mol = molgroup->mol; mol; mol = mol->next ) if ( mol->sel ) ++j;
317 
318  // cout << "reference = " << i << endl;
319  // cout << "selected = " << j << endl;
320 
321  for ( j = 0; j < mat.rows(); ) {
322  if ( i != j && mat[i][j] < cutoff ) {
323  mat = mat.delete_row_column(j);
324  if ( i > j ) i--;
325  } else j++;
326  }
327 
328  for ( mol = pmol = molgroup->mol; mol; ) {
329  if ( mol->sel < 1 ) {
330  if ( mol == molgroup->mol ) {
331  molgroup->mol = pmol = mol->next;
332  molecule_kill(mol);
333  mol = pmol;
334  } else {
335  pmol->next = mol->next;
336  molecule_kill(mol);
337  mol = pmol->next;
338  }
339  } else {
340  pmol = mol;
341  mol = mol->next;
342  }
343  }
344 
345  for ( j = 0, mol = molgroup->mol; mol; mol = mol->next ) ++j;
346 
347  if ( verbose ) {
348  cout << "Number of sequences retained: " << j << endl << endl;
349  cout << "Number of sequences retained: " << mat.rows() << endl << endl;
350  }
351 
352  return mat.rows();
353  }
354 */
355 
362 long seq_delete(Bmolgroup* molgroup, Matrix mat)
363 {
364  long i;
365  Bmolecule* mol, *pmol;
366 
367  for ( i = 0, mol = molgroup->mol; mol; mol = mol->next )
368  if ( mol->sel == 0 ) ++i;
369 
370  if ( verbose )
371  cout << "Deleting " << i << " sequences" << endl << endl;
372 
373 // cout << mat << endl;
374 
375  if ( mat.rows() ) {
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 )
380  if ( del[i] ) mat = mat.delete_row_column(i);
381  }
382 
383  for ( mol = pmol = molgroup->mol; mol; ) {
384  if ( mol->sel < 1 ) {
385  if ( mol == molgroup->mol ) {
386  molgroup->mol = pmol = mol->next;
387  molecule_kill(mol);
388  mol = pmol;
389  } else {
390  pmol->next = mol->next;
391  molecule_kill(mol);
392  mol = pmol->next;
393  }
394  } else {
395  pmol = mol;
396  mol = mol->next;
397  }
398  }
399 
400  for ( i = 0, mol = molgroup->mol; mol; mol = mol->next ) ++i;
401 
402  if ( verbose ) {
403  cout << "Number of sequences retained: " << i << endl << endl;
404  cout << "Number of sequences retained: " << mat.rows() << endl << endl;
405  }
406 
407  return mat.rows();
408 }
409 
423 {
424  int notdone, n;
425  long i, j, x, g;
426  Bmolecule *mol;
427 // char *seq, *aptr, *aptr2;
428  char *aptr, *aptr2;
429  char reslist[3];
430 
431  long len(3*molgroup->maxlen);
432  char* profseq = new char[len];
433  char* prof = new char[100000];
434 
435  if ( verbose & VERB_LABEL )
436  cout << "Profile:" << endl;
437 
438  for ( i=0, aptr=profseq; i<molgroup->maxlen; i++, aptr+=3 ) {
439  notdone = 1;
440  for ( j=0; j<3; j++ ) reslist[j] = ' ';
441  for ( mol = molgroup->mol; mol && notdone; mol = mol->next ) {
442 // seq = mol->seq.c_str();
443  if ( mol->seq[i] == '-' ) {
444  memset(aptr, '-', 3);
445  notdone = 0;
446  } else {
447  for ( j=0; j<3 && reslist[j] != ' ' && mol->seq[i] != reslist[j]; j++ ) ;
448  if ( j<3 ) reslist[j] = mol->seq[i];
449  else {
450  memset(aptr, 'x', 3);
451  notdone = 0;
452  }
453  }
454  if ( notdone ) memcpy(aptr, reslist, 3);
455  }
456  }
457 
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++;
461  else {
462  if ( aptr2 != prof ) *aptr2++ = '-';
463  if ( g + x > 0 ) {
464  if ( g == 0 ) {
465  snprintf(aptr2, len, "x(%ld)%n", x, &n);
466  } else {
467  snprintf(aptr2, len, "x(%ld,%ld)%n", x, x+g, &n);
468  }
469  aptr2 += n;
470  *aptr2++ = '-';
471  }
472  if ( aptr[1] == ' ' ) {
473  *aptr2++ = *aptr;
474  } else {
475  *aptr2++ = '[';
476  for ( j=0; j<3 && aptr[j] != ' '; j++ ) *aptr2++ = aptr[j];
477  *aptr2++ = ']';
478  }
479  g = x = 0;
480  }
481  }
482  *aptr2 = 0;
483 
484  if ( verbose )
485  cout << prof << endl;
486 
487  char* profile = new char[aptr2-prof+1];
488  memcpy(profile, prof, aptr2-prof);
489  profile[aptr2-prof] = 0;
490 
491  delete[] profseq;
492  delete[] prof;
493 
494  return profile;
495 }
496 
497 static int resinfo_compare(const void *x,const void *y)
498 {
499  ResInfo* ri1 = (ResInfo *) x;
500  ResInfo* ri2 = (ResInfo *) y;
501 
502  if ( ri1->info < ri2->info ) return 1;
503  else return -1;
504 }
505 
527 int seq_aligned_information(Bmolgroup* molgroup, int window, Bstring& psfile)
528 {
529  long i, j, k, ii, n, nres;
530  double fgap;
531 
532  Bstring temp;
534  Bresidue_type* rt_curr = NULL;
535  for ( nres = 0, rt_curr = rt; rt_curr; rt_curr = rt_curr->next ) nres++;
536 // Bstring code1(nres, ' ');
537  Bstring code1(' ', nres);
538  for ( i = 0, rt_curr = rt; rt_curr; rt_curr = rt_curr->next, i++ )
539  code1[i] = rt_curr->c;
540 
541  kill_list((char *) rt, sizeof(Bresidue_type));
542 
543  if ( verbose & VERB_DEBUG )
544  cout << "DEBUG seq_aligned_information: code=" << code1 << endl;
545 
546  long nmol(0);
547  Bmolecule* mol = molgroup->mol;
548  for ( nmol = 0, mol = molgroup->mol; mol; mol = mol->next ) nmol++;
549 
550  long npos(0);
551  for ( i=0; i<molgroup->maxlen; i++ ) npos += molgroup->seqflag[i];
552 
553  long igap = code1.index('-');
554  double* fsum = new double[npos];
555  double* info = new double[npos];
556  double* freq = new double[nres*npos];
557  ResInfo* ri = new ResInfo[nres];
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;
561  seq[npos] = 0;
562  for ( i=0; i<nres*npos; i++ ) freq[i] = pattern[i] = 0;
563 
564  if ( verbose & VERB_LABEL ) {
565  cout << "Information content analysis:" << endl;
566  cout << "Sequences: " << nmol << endl;
567  cout << "Alignment positions: " << npos << endl;
568  cout << "Moving average window: " << window << endl;
569  }
570 
571  // Calculating the frequency of each residue type occurring at each
572  // position in the alignment
573  for ( mol = molgroup->mol; mol; mol = mol->next ) {
574  j = k = 0;
575  while ( j < mol->seq.length() ) {
576  if ( molgroup->seqflag[j] ) {
577 // ii = find_char_index(code1, mol->seq[j]);
578  ii = code1.index(mol->seq[j]);
579  if ( ii > -1 ) freq[nres*k+ii] += 1;
580  k++;
581  }
582  j++;
583  }
584  }
585 
586  // Calculating information content for a position in the alignment
587  k = 0;
588  mol = molgroup->mol;
589  for ( j=0; j<molgroup->maxlen; j++ ) {
590  if ( molgroup->seqflag[j] ) {
591  memset(ri, 0, nres*sizeof(ResInfo));
592  fgap = 0;
593  for ( i=0; i<nres; i++ ) {
594  if ( i == igap )
595  fgap += freq[nres*k+i];
596  else
597  fsum[k] += freq[nres*k+i];
598  }
599  info[k] = 0;
600  if ( fsum[k] > 0 ) {
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;
605  }
606  if ( fsum[k] > 20 )
607  info[k] += log(20.0/fsum[k])/log(2.0);
608  }
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;
614  }
615  seq[k] = mol->seq[j];
616  k++;
617  }
618  }
619 
620  // Calculate a moving average
621  double* movavg = moving_average(npos, info, window);
622 
623  // Print out the list of results
624  if ( verbose & VERB_LABEL )
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];
630  cout << endl;
631  }
632 
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++;
636  if ( n < 1 ) {
637  cout << "x";
638  } else {
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 << "]";
643  }
644  }
645  cout << endl;
646 
647  Complex<float>* per = seq_frequency_analysis(npos, 16, 4, 4, info);
648 
649  Complex<float>* per_avg = moving_average_complex(npos, per, window);
650 
651  Bstring title("Information");
652  if ( psfile.length() )
653  ps_seq_info(psfile, title, nres, npos, movavg, fsum, per_avg,
654  freq, pattern);
655 
656  delete[] fsum;
657  delete[] movavg;
658  delete[] ri;
659  delete[] seq;
660  delete[] freq;
661  delete[] pattern;
662  delete[] info;
663  delete[] per;
664  delete[] per_avg;
665 
666  return 0;
667 }
668 
682  int window, double threshold, Bstring& hphobfile, Bstring& psfile)
683 {
684  long i, j, nres;
685  Bresidue_type* rt = get_residue_properties(hphobfile);
686  Bresidue_type* rt_curr = NULL;
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;
691  code1[nres] = 0;
692 
693  if ( verbose & VERB_LABEL )
694  cout << "Average aligned hydrophobicity:" << endl;
695 
696  double* nseq = new double[molgroup->maxlen];
697  double* avgHP = new double[molgroup->maxlen];
698  int* HPseg = new int[molgroup->maxlen];
699  Bmolecule* mol;
700 
701  for ( i=0; i<molgroup->maxlen; i++ ) nseq[i] = avgHP[i] = HPseg[i] = 0;
702 
703  for ( mol = molgroup->mol; mol; mol = mol->next ) {
704  for ( j=0; j<mol->seq.length(); j++ ) {
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; // Note: Negative for scales of hydrophilicity
709  nseq[j]++;
710  }
711  }
712  }
713  }
714 
715  delete[] code1;
716  kill_list((char *) rt, sizeof(Bresidue_type));
717 
718  for ( j=0; j<molgroup->maxlen; j++ )
719  if ( nseq[j] > 0 ) avgHP[j] /= nseq[j];
720 
721  if ( verbose & VERB_PROCESS )
722  cout << "nRes\tavgHP\tintHP\tnSeq\tRes" << endl;
723 
724  double sum(0);
725  i = 0;
726  mol = molgroup->mol;
727  for ( j=0; j<molgroup->maxlen; j++ ) {
728  if ( molgroup->seqflag[j] ) {
729  i++;
730  sum += avgHP[j];
731  cout << i << tab << avgHP[j] << tab << sum << tab << nseq[j] << tab << mol->seq[j] << endl;
732  }
733  }
734 
735  if ( verbose & VERB_PROCESS )
736  cout << endl << "Segment\tStart\tEnd\tLength\tsumHP" << endl;
737 
738  int k, m, sum_flag(0), start[1000], end[1000], newstart, newend, nmol(0);
739  double max, sumHP[1000];
740 
741  for ( nmol = 0, mol = molgroup->mol; mol; mol = mol->next ) nmol++;
742 
743  if ( threshold < 0.5 ) threshold = 0.5;
744  if ( threshold > 1 ) threshold = 1;
745  threshold *= nmol;
746  i = 0;
747  sumHP[0] = 0;
748  for ( j=1; j<molgroup->maxlen; j++ ) {
749  if ( molgroup->seqflag[j] ) {
750  if ( nseq[j] >= threshold && nseq[j-1] < threshold ) {
751  if ( sumHP[i] > 20 ) i++;
752  start[i] = j;
753  sum_flag = 1;
754  sumHP[i] = 0;
755  }
756  if ( sum_flag && nseq[j] < threshold ) {
757  end[i] = j - 1;
758  sum_flag = 0;
759  newstart = start[i];
760  newend = end[i];
761  max = sumHP[i];
762  for ( k=start[i]; k<end[i]; k++ ) {
763  sum = 0;
764  for ( m=k; m<=end[i]; m++ ) {
765  sum += avgHP[m];
766  if ( max < sum ) {
767  max = sum;
768  newstart = k;
769  newend = m;
770  }
771  }
772  }
773  start[i] = newstart;
774  end[i] = newend;
775  sumHP[i] = max;
776  if ( max > 20 ) {
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;
780  }
781  }
782  if ( sum_flag ) sumHP[i] += avgHP[j];
783  }
784  }
785  cout << endl;
786 
787  // Calculate a moving average
788  double* movavg = moving_average(molgroup->maxlen, avgHP, window);
789 
790  Complex<float>* per = seq_frequency_analysis(molgroup->maxlen, 16, 4, 4, avgHP);
791 
792  Complex<float>* per_avg = moving_average_complex(molgroup->maxlen, per, window);
793 
794  Bstring title("Hydrophobicity");
795  if ( psfile.length() )
796  ps_seq_hydrophob(psfile, title, molgroup->maxlen, movavg, HPseg, nseq, per_avg);
797 
798  delete[] nseq;
799  delete[] avgHP;
800  delete[] HPseg;
801  delete[] movavg;
802  delete[] per;
803  delete[] per_avg;
804 
805  return 0;
806 }
807 
820 // Fourier analysis of a vector
821 Complex<float>* seq_frequency_analysis(int length, int win, int start, int end, double* data)
822 {
823  if ( start > end ) swap(start, end);
824  if ( start < 0 ) start = 0;
825  if ( end > win - 1 ) end = win - 1;
826 
827  if ( verbose )
828  cout << "Frequency analysis:" << endl;
829 
830  if ( verbose & VERB_RESULT ) {
831  cout << "Window: " << win << endl;
832  cout << "Start - end: " << start << " - " << end << endl;
833  }
834 
835  int j, k, l;
836  int half_win = win/2;
837  double real, imag, amp, phi, dphi;
838 
839  Complex<float>* transform_data = new Complex<float>[length*(end-start+1)];
840 
841  for ( j=0; j<length-win; j++ ) {
842  cout << j+half_win;
843  for ( k=start; k<=end; k++ ) {
844  real = 0;
845  imag = 0;
846  for ( l=0; l<win; l++ ) {
847  phi = MIN2PI*k*l/win;
848  real += data[l+j]*cos(phi);
849  imag += data[l+j]*sin(phi);
850  }
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;
855  dphi = angle_set_negPI_to_PI(dphi);
856  transform_data[length*(k-start)+j+half_win] = Complex<float>(amp*cos(dphi), amp*sin(dphi));
857  if ( verbose & VERB_RESULT )
858  cout << tab << amp << tab << dphi*180/M_PI;
859  }
860  if ( verbose & VERB_RESULT )
861  cout << endl;
862  }
863  cout << endl;
864 
865  return transform_data;
866 }
867 
868 vector<double> seq_aligned_weight(Bmolgroup* molgroup)
869 {
870  char m1, m2, igap = '-';
871  long i, k, l, nseq, overlap, identity;
872 
873  Bmolecule *mol1, *mol2;
874 
875  for ( nseq=0, mol1 = molgroup->mol; mol1; mol1 = mol1->next ) nseq++;
876 
877  vector<double> weight(nseq*nseq, 0);
878 
879  // Weighting is based on the difference between identity and overlap
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++ ) {
884  m1 = mol1->seq[i];
885  m2 = mol2->seq[i];
886  if ( m1 != igap && m2 != igap ) {
887  overlap++;
888  if ( m1 == m2 ) identity++;
889  }
890  }
891  weight[k*nseq+l] = (overlap - identity)*1.0/molgroup->maxlen;
892  if ( verbose & VERB_DEBUG )
893  cout << "k=" << k << " l=" << l << " weight=" << weight[k*nseq+l] << endl;
894  }
895  }
896 
897  return weight;
898 }
899 
934  Bstring& refseqid, double cutoff, Bstring& simfile)
935 {
936  long h, i, j, k, l;
937  long refseqnum, length(molgroup->maxlen), number(0);
938 
939  Bmolecule *molref, *mol1, *mol2;
940  for ( number=0, mol1 = molgroup->mol; mol1; mol1 = mol1->next ) number++;
941  for ( refseqnum=0, molref = molgroup->mol; molref; molref = molref->next, ++refseqnum )
942  if ( molref->id.contains(refseqid) ) break;
943 
944  if ( !molref ) {
945  cerr << "Sequence " << refseqid << " not found!" << endl;
946  return Matrix();
947  }
948 
949  Bstring refseq(molref->seq);
950  vector<char> selseq(length*number, 0);
951 
952  if ( verbose & VERB_DEBUG )
953  cout << "DEBUG seq_correlated_mutation: length=" << length << " number=" << number << endl;
954 
955  int m1, m2, ir, ntot;
956  int total = number*(number-1)/2; // Only lower triangle
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);
962 
963  Bresidue_matrix* simat = get_residue_matrix(simfile);
964 
965  int nres = simat->n;
966  Bstring code1 = simat->c;
967  float* sim_mat = simat->m;
968 
969  int igap = code1.index('-');
970 
971  if ( verbose & VERB_DEBUG )
972  cout << "DEBUG seq_correlated_mutation: total=" << total << endl;
973 
974  vector<double> weight = seq_aligned_weight(molgroup);
975 
976  for ( i=ir=0; i<length; i++ ) {
977  if ( molgroup->seqflag[i] ) {
978  sum = ssum = 0;
979  m1 = code1.index(molref->seq[i]);
980  for ( k=1, mol1 = molgroup->mol->next; k<number; k++, mol1 = mol1->next ) {
981  m1 = code1.index(mol1->seq[i]);
982  for ( l=0, mol2 = molgroup->mol; l<k; l++, mol2 = mol2->next ) {
983  m2 = code1.index(mol2->seq[i]);
984  h = (ir*number + k)*number + l;
985  // Exclude all gaps
986  if ( m1 != igap && m2 != igap ) {
987  score[h] = sim_mat[m2*nres + m1];
988  sum += score[h];
989  ssum += score[h]*score[h];
990  num[ir]++;
991  } else score[h] = -999;
992 // if ( m1 == m2 ) score[h] = -999;
993  }
994  }
995  avg[ir] = std[ir] = 0;
996  if ( total ) {
997  avg[ir] = sum/total;
998  std[ir] = sqrt(ssum/total - avg[ir]*avg[ir]);
999  }
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];
1003  if ( verbose & VERB_DEBUG )
1004  cout << "i=" << ir << " avg=" << avg[ir] << " std=" << std[ir] << " num=" << num[ir] << endl;
1005  ir++;
1006  }
1007  }
1008  length = ir;
1009 
1010  if ( verbose & VERB_PROCESS ) {
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;
1015  }
1016 
1017  Matrix mat(length, length);
1018 
1019  ir = 0;
1020  sigma2 = 0;
1021  for ( i=0; i<length; i++ ) {
1022  for ( j=0; j<=i; j++ ) {
1023  ntot = 0;
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]);
1033  ntot++;
1034  }
1035  }
1036  }
1037 
1038  mat[i][j] /= total*std[i]*std[j];
1039  }
1040  }
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]; // Sum of squared correlations
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;
1047  if ( verbose & VERB_PROCESS ) {
1048  for ( k=0; k<number; k++ )
1049  cout << selseq[j*number+k];
1050  cout << endl;
1051  for ( k=0; k<number; k++ )
1052  cout << selseq[i*number+k];
1053  cout << endl;
1054  }
1055  ir++;
1056  }
1057  mat[j][i] = mat[i][j];
1058  }
1059  }
1060  if ( verbose & VERB_PROCESS ) {
1061  cout << endl << "Correlations reported: " << ir << endl;
1062  cout << "Maximum off-diagonal coefficient: " << maxCC << endl << endl;
1063  }
1064 
1065  residue_matrix_kill(simat);
1066 
1067  return mat;
1068 }
1069 
seq_frequency_analysis
Complex< float > * seq_frequency_analysis(int length, int win, int start, int end, double *data)
Fourier transforms a vector for frequency analysis.
Definition: seq_analysis.cpp:821
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Bmolgroup::seqflag
int * seqflag
Definition: rwmolecule.h:188
ResInfo::info
double info
Definition: seq_analysis.cpp:20
seq_aligned_identity
Matrix seq_aligned_identity(Bmolgroup *molgroup)
Calculates the pairwise identities between aligned sequences.
Definition: seq_analysis.cpp:73
seq_aligned_weight
vector< double > seq_aligned_weight(Bmolgroup *molgroup)
Definition: seq_analysis.cpp:868
moving_average_complex
Complex< float > * moving_average_complex(long number, Complex< float > *x, long window)
Calculates a moving average over an array of complex data.
Definition: moving_average.cpp:75
kill_list
long kill_list(char *list, unsigned long size)
Frees all the items in a linked list.
Definition: linked_list.cpp:214
Complex
Definition: Complex.h:23
Bstring::index
long index(const char c)
Definition: Bstring.h:102
Bresidue_type::c
char c
Definition: rwresprop.h:24
Bstring
Definition: Bstring.h:29
Bresidue_matrix::c
Bstring c
Definition: rwresprop.h:46
seq_aligned_profile
char * seq_aligned_profile(Bmolgroup *molgroup)
Generates a PROSITE format profile from an aligned set of sequences.
Definition: seq_analysis.cpp:422
Bresidue_type::next
Bresidue_type * next
Definition: rwresprop.h:23
ps_seq_hydrophob
int ps_seq_hydrophob(Bstring &filename, Bstring &title, int length, double *Hphob, int *HPseg, double *nseq, Complex< float > *per)
Generates a postscript plot from a hydrophobicity analysis of a protein sequence alignment.
Definition: ps_sequence.cpp:194
Bmolgroup::mol
Bmolecule * mol
Definition: rwmolecule.h:189
seq_limit
long seq_limit(Bmolgroup *molgroup, Bstring &refseq)
Limits the selection to the reference sequence in an aligned set.
Definition: seq_analysis.cpp:36
seq_aligned_hydrophobicity
int seq_aligned_hydrophobicity(Bmolgroup *molgroup, int window, double threshold, Bstring &hphobfile, Bstring &psfile)
Calculates the average hydrophobicity at every position in an alignment.
Definition: seq_analysis.cpp:681
length
double length(vector< T > v)
Definition: Vector3.h:530
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
get_residue_properties
Bresidue_type * get_residue_properties(Bstring &filename)
Reading residue properties from parameter files.
Definition: rwresprop.cpp:99
ResInfo::res
char res[4]
Definition: seq_analysis.cpp:20
angle_set_negPI_to_PI
double angle_set_negPI_to_PI(double angle)
Returns an angle between -PI and PI.
Definition: utilities.cpp:558
Bmolecule::seq
Bstring seq
Definition: rwmolecule.h:114
seq_select
long seq_select(Bmolgroup *molgroup, long minlen, long maxlen)
Selects sequences within a range of lengths.
Definition: seq_analysis.cpp:233
moving_average
double * moving_average(long number, double *x, long window)
Calculates a moving average over an array of data.
Definition: moving_average.cpp:28
Matrix::rows
long rows()
Definition: Matrix.h:153
Matrix::delete_row_column
Matrix delete_row_column(long rc)
Definition: Matrix.h:173
VERB_RESULT
#define VERB_RESULT
Definition: utilities.h:109
Bmolecule::next
Bmolecule * next
Definition: rwmolecule.h:108
Bmolecule
Definition: rwmolecule.h:107
MIN2PI
#define MIN2PI
Definition: utilities.h:75
seq_correlated_mutation
Matrix seq_correlated_mutation(Bmolgroup *molgroup, Bstring &refseqid, double cutoff, Bstring &simfile)
Correlated mutation analysis of an alignment.
Definition: seq_analysis.cpp:933
Bresidue_type
Definition: rwresprop.h:22
Bmolgroup::maxlen
long maxlen
Definition: rwmolecule.h:180
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
seq_analysis.h
Header file for sequence analysis functions.
ps_sequence.h
Header file for postscript output for sequence analysis functions.
Bstring::length
long length() const
Definition: Bstring.cpp:263
molecule_kill
int molecule_kill(Bmolecule *mol)
Destroys a molecule.
Definition: rwmolecule.cpp:417
seq_aligned_information
int seq_aligned_information(Bmolgroup *molgroup, int window, Bstring &psfile)
Calculates the sequence logo representation for an alignment.
Definition: seq_analysis.cpp:527
ps_seq_info
int ps_seq_info(Bstring &filename, Bstring &title, int nres, int length, double *info, double *nseq, Complex< float > *per, double *freq, char *pattern)
Generates a postscript plot from an information content analysis of a protein sequence alignment.
Definition: ps_sequence.cpp:33
log_2
double log_2(double a)
Definition: seq_analysis.cpp:23
seq_util.h
Header file for sequence utilities.
ResInfo
Definition: seq_analysis.cpp:20
moving_average.h
Header file moving average calculations.
linked_list.h
Header file for generalized linked list functions.
residue_matrix_kill
int residue_matrix_kill(Bresidue_matrix *resmat)
Deallocating a residue matrix.
Definition: rwresprop.cpp:208
verbose
int verbose
Bmolgroup
Definition: rwmolecule.h:170
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
Bresidue_type::hphob
float hphob
Definition: rwresprop.h:31
Bmolecule::sel
int sel
Definition: rwmolecule.h:120
Matrix
Definition: Matrix.h:22
seq_delete
long seq_delete(Bmolgroup *molgroup, Matrix mat)
Deletes non-selected sequences and corresponding elelments of a comparison matrix.
Definition: seq_analysis.cpp:362
Bresidue_matrix
Definition: rwresprop.h:44
Bresidue_matrix::n
int n
Definition: rwresprop.h:45
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:110
Bresidue_matrix::m
float * m
Definition: rwresprop.h:47
get_residue_matrix
Bresidue_matrix * get_residue_matrix(Bstring &filename)
Reading a residue matrix from a file.
Definition: rwresprop.cpp:164
Bmolecule::id
Bstring id
Definition: rwmolecule.h:110
TWOPI
#define TWOPI
Definition: utilities.h:72
seq_aligned_similarity
Matrix seq_aligned_similarity(Bmolgroup *molgroup, double threshold, Bresidue_matrix *simat)
Calculates the pairwise similarities between aligned sequences.
Definition: seq_analysis.cpp:152