| Bsoft 2.0.5 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 // Internal function prototype
23 double* seq_aligned_weight(Bmolgroup* molgroup);
24
25 double log_2(double a)
26 {
27 if ( a <= 0 ) return 0;
28 else return log(a)/log(2.0L);
29 }
30
43 Bimage* seq_aligned_identity(Bmolgroup* molgroup)
44 {
45 long i, j, k, n(0), nid, overlap, nmol(0), maxlen;
46 long idsum(0), idssum(0), overlapsum(0), overlapssum(0);
47 double idavg, overlapavg;
48 Bmolecule* mol1;
49 Bmolecule* mol2;
50
51 if ( verbose & VERB_LABEL ) {
52 cout << "Aligned identity analysis:" << endl;
53 cout << "Seq1\tSeq2\tIdentity\tnID\tOverlap\tName1\tName2" << endl;
54 }
55
56 for ( mol1 = molgroup->mol; mol1; mol1 = mol1->next ) nmol++;
57
58 // Initialize an image structure to hold the results
59 Bimage* pimg = new Bimage(Float, TSimple, nmol, nmol, 1, 1);
60
61 for ( i=1, mol1 = molgroup->mol->next; mol1; i++, mol1 = mol1->next ) {
62 for ( j=0, mol2 = molgroup->mol; mol2 != mol1 && j<i; j++, mol2 = mol2->next ) {
63 n++;
64 nid = overlap = 0;
65 maxlen = mol1->seq.length();
66 if ( maxlen > mol2->seq.length() ) maxlen = mol2->seq.length();
67 for ( k=0; k<maxlen; k++ ) {
68 if ( molgroup->seqflag[k] > 0 &&
69 mol1->seq[k] != '-' &&
70 mol2->seq[k] != '-' ) {
71 overlap++;
72 if ( mol1->seq[k] == mol2->seq[k] ) nid++;
73 }
74 }
75 idsum += nid;
76 idssum += nid*nid;
77 overlapsum += overlap;
78 overlapssum += overlap*overlap;
79 k = i*nmol + j;
80 if ( overlap ) pimg->set(k, nid*1.0/overlap);
81 cout << i+1 << tab << j+1 << tab << fixed << setprecision(3) << setw(8) <<
82 (*pimg)[k] << tab << nid << tab << overlap << tab <<
83 mol1->id << tab << mol2->id << endl;
84 }
85 }
86
87 idavg = idsum*1.0/n;
88 overlapavg = overlapsum*1.0/n;
89
90 if ( verbose & VERB_LABEL ) {
91 cout << "Average identical residues: " << idavg << " (" <<
92 sqrt(idssum*1.0/n - idavg*idavg) << ")" << endl;
93 cout << "Average overlap: " << overlapavg << " (" <<
94 sqrt(overlapssum*1.0/n - overlapavg*overlapavg) << ")" << endl << endl;
95 }
96
97 return pimg;
98 }
99
119 Bimage* seq_aligned_similarity(Bmolgroup* molgroup, double threshold, Bresidue_matrix* simat)
120 {
121 long i, j, k, thelen, overlap, nmol(0);
122 long ii, jj;
123 double simsum(0), percentsim(0);
124 Bmolecule *mol1, *mol2;
125
126 long nres = simat->n;
127 Bstring code1 = simat->c;
128 float* sim_mat = simat->m;
129
130 for ( mol1 = molgroup->mol; mol1; mol1 = mol1->next ) nmol++;
131
132 if ( verbose & VERB_LABEL ) {
133 cout << "Aligned similarity analysis:" << endl;
134 cout << "Similar residue threshold: " << threshold << endl;
135 cout << "Number of sequences: " << nmol << endl << endl;
136 cout << "Seq1\tSeq2\tSimilarity\tFraction\tOverlap\tName1\tName2" << endl;
137 }
138
139 // Initialize an image structure to hold the results
140 Bimage* pimg = new Bimage(Float, TSimple, nmol, nmol, 1, 1);
141
142 for ( i=1, mol1 = molgroup->mol->next; mol1; i++, mol1 = mol1->next ) {
143 // seq1 = mol1->seq.c_str();
144 for ( j=0, mol2 = molgroup->mol; mol2 != mol1; j++, mol2 = mol2->next ) {
145 // seq2 = mol2->seq.c_str();
146 // thelen = strlen(seq1);
147 // if ( thelen > strlen(seq2) ) thelen = strlen(seq2);
148 thelen = mol1->seq.length();
149 if ( thelen > mol2->seq.length() ) thelen = mol2->seq.length();
150 simsum = percentsim = 0.0;
151 overlap = 0;
152 if ( verbose & VERB_DEBUG )
153 cout << "i=" << i << " j=" << j << " thelen=" << thelen << endl;
154 for ( k=0; k<thelen; k++ ) {
155 if ( molgroup->seqflag[k] > 0 &&
156 ( mol1->seq[k] != '-' ) && ( mol2->seq[k] != '-' ) ) {
157 // ii = find_char_index(code1, mol1->seq[k]);
158 // jj = find_char_index(code1, mol2->seq[k]);
159 ii = code1.index(mol1->seq[k]);
160 jj = code1.index(mol2->seq[k]);
161 if ( ii > -1 && jj > -1 ) {
162 simsum += sim_mat[ii*nres+jj];
163 if ( sim_mat[ii*nres+jj] >= threshold )
164 percentsim += 1;
165 overlap++;
166 }
167 }
168 if ( verbose & VERB_DEBUG )
169 cout << k << tab << simsum << tab << overlap << endl;
170 }
171 if ( overlap ) {
172 pimg->set(i*nmol+j, simsum*1.0/overlap);
173 percentsim /= overlap;
174 } else pimg->set(i*nmol+j, 0);
175 cout << i+1 << tab << j+1 << tab << fixed << setprecision(3) << setw(8) <<
176 (*pimg)[i*nmol+j] << tab << setw(8) << percentsim << tab << overlap << tab <<
177 mol1->id << tab << mol2->id << endl;
178 }
179 }
180 cout << endl;
181
182 return pimg;
183 }
184
197 char* seq_aligned_profile(Bmolgroup* molgroup)
198 {
199 int notdone, n;
200 long i, j, x, g;
201 Bmolecule *mol;
202 // char *seq, *aptr, *aptr2;
203 char *aptr, *aptr2;
204 char reslist[3];
205
206 long len(3*molgroup->maxlen);
207 char* profseq = new char[len];
208 char* prof = new char[100000];
209
210 if ( verbose & VERB_LABEL )
211 cout << "Profile:" << endl;
212
213 for ( i=0, aptr=profseq; i<molgroup->maxlen; i++, aptr+=3 ) {
214 notdone = 1;
215 for ( j=0; j<3; j++ ) reslist[j] = ' ';
216 for ( mol = molgroup->mol; mol && notdone; mol = mol->next ) {
217 // seq = mol->seq.c_str();
218 if ( mol->seq[i] == '-' ) {
219 memset(aptr, '-', 3);
220 notdone = 0;
221 } else {
222 for ( j=0; j<3 && reslist[j] != ' ' && mol->seq[i] != reslist[j]; j++ ) ;
223 if ( j<3 ) reslist[j] = mol->seq[i];
224 else {
225 memset(aptr, 'x', 3);
226 notdone = 0;
227 }
228 }
229 if ( notdone ) memcpy(aptr, reslist, 3);
230 }
231 }
232
233 for ( i=g=x=0, aptr=profseq, aptr2=prof; i<molgroup->maxlen; i++, aptr+=3 ) {
234 if ( *aptr == '-' ) g++;
235 else if ( *aptr == 'x' ) x++;
236 else {
237 if ( aptr2 != prof ) *aptr2++ = '-';
238 if ( g + x > 0 ) {
239 if ( g == 0 ) {
240 snprintf(aptr2, len, "x(%ld)%n", x, &n);
241 } else {
242 snprintf(aptr2, len, "x(%ld,%ld)%n", x, x+g, &n);
243 }
244 aptr2 += n;
245 *aptr2++ = '-';
246 }
247 if ( aptr[1] == ' ' ) {
248 *aptr2++ = *aptr;
249 } else {
250 *aptr2++ = '[';
251 for ( j=0; j<3 && aptr[j] != ' '; j++ ) *aptr2++ = aptr[j];
252 *aptr2++ = ']';
253 }
254 g = x = 0;
255 }
256 }
257 *aptr2 = 0;
258
259 if ( verbose )
260 cout << prof << endl;
261
262 char* profile = new char[aptr2-prof+1];
263 memcpy(profile, prof, aptr2-prof);
264 profile[aptr2-prof] = 0;
265
266 delete[] profseq;
267 delete[] prof;
268
269 return profile;
270 }
271
272 static int resinfo_compare(const void *x,const void *y)
273 {
274 ResInfo* ri1 = (ResInfo *) x;
275 ResInfo* ri2 = (ResInfo *) y;
276
277 if ( ri1->info < ri2->info ) return 1;
278 else return -1;
279 }
280
302 int seq_aligned_information(Bmolgroup* molgroup, int window, Bstring& psfile)
303 {
304 long i, j, k, ii, n, nres;
305 double fgap;
306
307 Bstring temp;
308 Bresidue_type* rt = get_residue_properties(temp);
309 Bresidue_type* rt_curr = NULL;
310 for ( nres = 0, rt_curr = rt; rt_curr; rt_curr = rt_curr->next ) nres++;
311 // Bstring code1(nres, ' ');
312 Bstring code1(' ', nres);
313 for ( i = 0, rt_curr = rt; rt_curr; rt_curr = rt_curr->next, i++ )
314 code1[i] = rt_curr->c;
315
316 kill_list((char *) rt, sizeof(Bresidue_type));
317
318 if ( verbose & VERB_DEBUG )
319 cout << "DEBUG seq_aligned_information: code=" << code1 << endl;
320
321 long nmol(0);
322 Bmolecule* mol = molgroup->mol;
323 for ( nmol = 0, mol = molgroup->mol; mol; mol = mol->next ) nmol++;
324
325 long npos(0);
326 for ( i=0; i<molgroup->maxlen; i++ ) npos += molgroup->seqflag[i];
327
328 long igap = code1.index('-');
329 double* fsum = new double[npos];
330 double* info = new double[npos];
331 double* freq = new double[nres*npos];
332 ResInfo* ri = new ResInfo[nres];
333 char* pattern = new char[nres*npos];
334 char* seq = new char[npos + 1];
335 for ( i=0; i<npos; i++ ) fsum[i] = info[i] = seq[i] = 0;
336 seq[npos] = 0;
337 for ( i=0; i<nres*npos; i++ ) freq[i] = pattern[i] = 0;
338
339 if ( verbose & VERB_LABEL ) {
340 cout << "Information content analysis:" << endl;
341 cout << "Sequences: " << nmol << endl;
342 cout << "Alignment positions: " << npos << endl;
343 cout << "Moving average window: " << window << endl;
344 }
345
346 // Calculating the frequency of each residue type occurring at each
347 // position in the alignment
348 for ( mol = molgroup->mol; mol; mol = mol->next ) {
349 j = k = 0;
350 while ( j < mol->seq.length() ) {
351 if ( molgroup->seqflag[j] ) {
352 // ii = find_char_index(code1, mol->seq[j]);
353 ii = code1.index(mol->seq[j]);
354 if ( ii > -1 ) freq[nres*k+ii] += 1;
355 k++;
356 }
357 j++;
358 }
359 }
360
361 // Calculating information content for a position in the alignment
362 k = 0;
363 mol = molgroup->mol;
364 for ( j=0; j<molgroup->maxlen; j++ ) {
365 if ( molgroup->seqflag[j] ) {
366 memset(ri, 0, nres*sizeof(ResInfo));
367 fgap = 0;
368 for ( i=0; i<nres; i++ ) {
369 if ( i == igap )
370 fgap += freq[nres*k+i];
371 else
372 fsum[k] += freq[nres*k+i];
373 }
374 info[k] = 0;
375 if ( fsum[k] > 0 ) {
376 for ( i=0; i<nres; i++ ) if ( i != igap ) {
377 ri[i].res[0] = code1[i];
378 ri[i].info = freq[nres*k+i]*log_2(freq[nres*k+i])/fsum[k];
379 info[k] += ri[i].info;
380 }
381 if ( fsum[k] > 20 )
382 info[k] += log(20.0/fsum[k])/log(2.0);
383 }
384 qsort((void *) ri, nres, sizeof(ResInfo),
385 (int (*)(const void *, const void *)) resinfo_compare);
386 for ( i=0; i<nres; i++ ) {
387 pattern[nres*k+i] = ri[i].res[0];
388 freq[nres*k+i] = ri[i].info;
389 }
390 seq[k] = mol->seq[j];
391 k++;
392 }
393 }
394
395 // Calculate a moving average
396 double* movavg = moving_average(npos, info, window);
397
398 // Print out the list of results
399 if ( verbose & VERB_LABEL )
400 cout << " Pos\tBits\tMovAvg\tnSeq\tPattern" << endl;
401 for ( k=0; k<npos; k++ ) {
402 cout << k+1 << tab << info[k] << tab << movavg[k] << tab << fsum[k] << tab;
403 for ( i=0; i<nres; i++ ) if ( freq[nres*k+i] > 0.1 )
404 cout << pattern[nres*k+i];
405 cout << endl;
406 }
407
408 for ( k=0; k<npos; k++ ) {
409 if ( k > 0 ) cout << "-";
410 for ( i=n=0; i<nres; i++ ) if ( freq[nres*k+i] > 0.1 ) n++;
411 if ( n < 1 ) {
412 cout << "x";
413 } else {
414 if ( n > 1 ) cout << "[";
415 for ( i=0; i<nres; i++ ) if ( freq[nres*k+i] > 0.1 )
416 cout << pattern[nres*k+i];
417 if ( n > 1 ) cout << "]";
418 }
419 }
420 cout << endl;
421
422 Complex<float>* per = seq_frequency_analysis(npos, 16, 4, 4, info);
423
424 Complex<float>* per_avg = moving_average_complex(npos, per, window);
425
426 Bstring title("Information");
427 if ( psfile.length() )
428 ps_seq_info(psfile, title, nres, npos, movavg, fsum, per_avg,
429 freq, pattern);
430
431 delete[] fsum;
432 delete[] movavg;
433 delete[] ri;
434 delete[] seq;
435 delete[] freq;
436 delete[] pattern;
437 delete[] info;
438 delete[] per;
439 delete[] per_avg;
440
441 return 0;
442 }
443
456 int seq_aligned_hydrophobicity(Bmolgroup* molgroup,
457 int window, double threshold, Bstring& hphobfile, Bstring& psfile)
458 {
459 long i, j, nres;
460 Bresidue_type* rt = get_residue_properties(hphobfile);
461 Bresidue_type* rt_curr = NULL;
462 for ( nres = 0, rt_curr = rt; rt_curr; rt_curr = rt_curr->next ) nres++;
463 char* code1 = new char[nres+1];
464 for ( i = 0, rt_curr = rt; rt_curr; rt_curr = rt_curr->next, i++ )
465 code1[i] = rt_curr->c;
466 code1[nres] = 0;
467
468 if ( verbose & VERB_LABEL )
469 cout << "Average aligned hydrophobicity:" << endl;
470
471 double* nseq = new double[molgroup->maxlen];
472 double* avgHP = new double[molgroup->maxlen];
473 int* HPseg = new int[molgroup->maxlen];
474 Bmolecule* mol;
475
476 for ( i=0; i<molgroup->maxlen; i++ ) nseq[i] = avgHP[i] = HPseg[i] = 0;
477
478 for ( mol = molgroup->mol; mol; mol = mol->next ) {
479 for ( j=0; j<mol->seq.length(); j++ ) {
480 if ( mol->seq[j] != '-' ) {
481 for ( rt_curr = rt; rt_curr && mol->seq[j] != rt_curr->c; rt_curr = rt_curr->next ) ;
482 if ( mol->seq[j] == rt_curr->c ) {
483 avgHP[j] += -rt_curr->hphob; // Note: Negative for scales of hydrophilicity
484 nseq[j]++;
485 }
486 }
487 }
488 }
489
490 delete[] code1;
491 kill_list((char *) rt, sizeof(Bresidue_type));
492
493 for ( j=0; j<molgroup->maxlen; j++ )
494 if ( nseq[j] > 0 ) avgHP[j] /= nseq[j];
495
496 if ( verbose & VERB_PROCESS )
497 cout << "nRes\tavgHP\tintHP\tnSeq\tRes" << endl;
498
499 double sum(0);
500 i = 0;
501 mol = molgroup->mol;
502 for ( j=0; j<molgroup->maxlen; j++ ) {
503 if ( molgroup->seqflag[j] ) {
504 i++;
505 sum += avgHP[j];
506 cout << i << tab << avgHP[j] << tab << sum << tab << nseq[j] << tab << mol->seq[j] << endl;
507 }
508 }
509
510 if ( verbose & VERB_PROCESS )
511 cout << endl << "Segment\tStart\tEnd\tLength\tsumHP" << endl;
512
513 int k, m, sum_flag(0), start[1000], end[1000], newstart, newend, nmol(0);
514 double max, sumHP[1000];
515
516 for ( nmol = 0, mol = molgroup->mol; mol; mol = mol->next ) nmol++;
517
518 if ( threshold < 0.5 ) threshold = 0.5;
519 if ( threshold > 1 ) threshold = 1;
520 threshold *= nmol;
521 i = 0;
522 sumHP[0] = 0;
523 for ( j=1; j<molgroup->maxlen; j++ ) {
524 if ( molgroup->seqflag[j] ) {
525 if ( nseq[j] >= threshold && nseq[j-1] < threshold ) {
526 if ( sumHP[i] > 20 ) i++;
527 start[i] = j;
528 sum_flag = 1;
529 sumHP[i] = 0;
530 }
531 if ( sum_flag && nseq[j] < threshold ) {
532 end[i] = j - 1;
533 sum_flag = 0;
534 newstart = start[i];
535 newend = end[i];
536 max = sumHP[i];
537 for ( k=start[i]; k<end[i]; k++ ) {
538 sum = 0;
539 for ( m=k; m<=end[i]; m++ ) {
540 sum += avgHP[m];
541 if ( max < sum ) {
542 max = sum;
543 newstart = k;
544 newend = m;
545 }
546 }
547 }
548 start[i] = newstart;
549 end[i] = newend;
550 sumHP[i] = max;
551 if ( max > 20 ) {
552 for ( k=start[i]; k<=end[i]; k++ ) HPseg[k] = 1;
553 cout << i+1 << tab << start[i]+1 << tab << end[i]+1 << tab <<
554 end[i] - start[i] + 1 << tab << sumHP[i] << endl;
555 }
556 }
557 if ( sum_flag ) sumHP[i] += avgHP[j];
558 }
559 }
560 cout << endl;
561
562 // Calculate a moving average
563 double* movavg = moving_average(molgroup->maxlen, avgHP, window);
564
565 Complex<float>* per = seq_frequency_analysis(molgroup->maxlen, 16, 4, 4, avgHP);
566
567 Complex<float>* per_avg = moving_average_complex(molgroup->maxlen, per, window);
568
569 Bstring title("Hydrophobicity");
570 if ( psfile.length() )
571 ps_seq_hydrophob(psfile, title, molgroup->maxlen, movavg, HPseg, nseq, per_avg);
572
573 delete[] nseq;
574 delete[] avgHP;
575 delete[] HPseg;
576 delete[] movavg;
577 delete[] per;
578 delete[] per_avg;
579
580 return 0;
581 }
582
595 // Fourier analysis of a vector
596 Complex<float>* seq_frequency_analysis(int length, int win, int start, int end, double* data)
597 {
598 if ( start > end ) swap(start, end);
599 if ( start < 0 ) start = 0;
600 if ( end > win - 1 ) end = win - 1;
601
602 if ( verbose )
603 cout << "Frequency analysis:" << endl;
604
605 if ( verbose & VERB_RESULT ) {
606 cout << "Window: " << win << endl;
607 cout << "Start - end: " << start << " - " << end << endl;
608 }
609
610 int j, k, l;
611 int half_win = win/2;
612 double real, imag, amp, phi, dphi;
613
614 Complex<float>* transform_data = new Complex<float>[length*(end-start+1)];
615
616 for ( j=0; j<length-win; j++ ) {
617 cout << j+half_win;
618 for ( k=start; k<=end; k++ ) {
619 real = 0;
620 imag = 0;
621 for ( l=0; l<win; l++ ) {
622 phi = MIN2PI*k*l/win;
623 real += data[l+j]*cos(phi);
624 imag += data[l+j]*sin(phi);
625 }
626 amp = sqrt(real*real + imag*imag);
627 phi = atan2(imag, real);
628 if ( amp < 0.001 ) phi = 0;
629 dphi = phi - TWOPI*j*k/win;
630 dphi = angle_set_negPI_to_PI(dphi);
631 transform_data[length*(k-start)+j+half_win] = Complex<float>(amp*cos(dphi), amp*sin(dphi));
632 if ( verbose & VERB_RESULT )
633 cout << tab << amp << tab << dphi*180/M_PI;
634 }
635 if ( verbose & VERB_RESULT )
636 cout << endl;
637 }
638 cout << endl;
639
640 return transform_data;
641 }
642
676 Bimage* seq_correlated_mutation(Bmolgroup* molgroup,
677 int refseqnum, double cutoff, Bstring& simfile)
678 {
679 int h, i, j, k, l, length = molgroup->maxlen, number(0);
680 char* refseq = new char[length];
681
682 Bmolecule *molref, *mol1, *mol2;
683 for ( number=0, mol1 = molgroup->mol; mol1; mol1 = mol1->next ) number++;
684 for ( i=0, molref = molgroup->mol; i < refseqnum; molref = molref->next ) i++;
685
686 memcpy(refseq, molref->seq.c_str(), length);
687 char* selseq = new char[length*number];
688 for ( i=0; i<length*number; i++ ) selseq[i] = 0;
689
690 if ( verbose & VERB_DEBUG )
691 cout << "DEBUG seq_correlated_mutation: length=" << length << " number=" << number << endl;
692
693 int m1, m2, ir, ntot;
694 int total = number*(number-1)/2; // Only lower triangle
695 double sum, ssum, sigma2, maxCC = -1e30;
696 double* avg = new double[length];
697 double* std = new double[length];
698 double* num = new double[length];
699 double* score = new double[length*number*number];
700 for ( i=0; i<length; i++ ) avg[i] = std[i] = num[i] = 0;
701 for ( i=0; i<length*number*number; i++ ) score[i] = 0;
702
703 Bresidue_matrix* simat = get_residue_matrix(simfile);
704
705 int nres = simat->n;
706 Bstring code1 = simat->c;
707 float* sim_mat = simat->m;
708
709 // int igap = find_char_index(code1, '-');
710 int igap = code1.index('-');
711
712 if ( verbose & VERB_DEBUG )
713 cout << "DEBUG seq_correlated_mutation: total=" << total << endl;
714
715 double* weight = seq_aligned_weight(molgroup);
716
717 // Initialize an image structure to hold the results
718 Bimage* pimg = new Bimage(Float, TSimple, length, length, 1, 1);
719
720 ir = 0;
721 for ( i=0; i<length; i++ ) {
722 if ( molgroup->seqflag[i] ) {
723 sum = ssum = 0;
724 // m1 = find_char_index(code1, molref->seq[i]);
725 m1 = code1.index(molref->seq[i]);
726 for ( k=1, mol1 = molgroup->mol->next; k<number; k++, mol1 = mol1->next ) {
727 // m1 = find_char_index(code1, mol1->seq[i]);
728 m1 = code1.index(mol1->seq[i]);
729 for ( l=0, mol2 = molgroup->mol; l<k; l++, mol2 = mol2->next ) {
730 // m2 = find_char_index(code1, mol2->seq[i]);
731 m2 = code1.index(mol2->seq[i]);
732 h = (ir*number + k)*number + l;
733 // Exclude all gaps
734 if ( m1 != igap && m2 != igap ) {
735 score[h] = sim_mat[m2*nres + m1];
736 sum += score[h];
737 ssum += score[h]*score[h];
738 num[ir]++;
739 } else score[h] = -999;
740 // if ( m1 == m2 ) score[h] = -999;
741 }
742 }
743 avg[ir] = std[ir] = 0;
744 if ( total ) {
745 avg[ir] = sum/total;
746 std[ir] = sqrt(ssum/total - avg[ir]*avg[ir]);
747 }
748 refseq[ir] = refseq[i];
749 for ( j=0, mol1 = molgroup->mol; j<number; j++, mol1 = mol1->next )
750 selseq[ir*number+j] = mol1->seq[i];
751 if ( verbose & VERB_DEBUG )
752 cout << "i=" << ir << " avg=" << avg[ir] << " std=" << std[ir] << " num=" << num[ir] << endl;
753 ir++;
754 }
755 }
756 length = ir;
757
758 if ( verbose & VERB_PROCESS ) {
759 cout << "Correlated mutation analysis:" << endl;
760 cout << "Matrix size: " << length << " x " << length << endl;
761 cout << "Reference sequence: " << molref->id << " (" << refseqnum + 1 << ")" << endl << endl;
762 cout << endl << "Res1\tNum1\tRes2\tNum2\tTotal\tCorr" << endl;
763 }
764
765 ir = 0;
766 sigma2 = 0;
767 for ( i=0; i<length; i++ ) {
768 for ( j=0; j<=i; j++ ) {
769 h = i*length + j;
770 pimg->set(h, 0);
771 ntot = 0;
772 if ( num[i] > 2 && num[j] > 2 ) {
773 if ( std[i] && std[j] ) {
774 for ( k=1; k<number; k++ ) {
775 for ( l=0; l<k; l++ ) {
776 m1 = (i*number + k)*number + l;
777 m2 = (j*number + k)*number + l;
778 if ( score[m1] > -100 && score[m2] > -100 ) {
779 pimg->set(h, (*pimg)[h] + weight[k*number+l]*
780 (score[m1] - avg[i])*(score[m2] - avg[j]));
781 ntot++;
782 }
783 }
784 }
785 pimg->set(h, (*pimg)[h] / total*std[i]*std[j]);
786 }
787 }
788 if ( i == j ) pimg->set(h, 1);
789 else if ( maxCC < (*pimg)[h] ) maxCC = (*pimg)[h];
790 sigma2 += (*pimg)[h]*(*pimg)[h]; // Sum of squared correlations
791 if ( (*pimg)[h] > cutoff && i != j ) {
792 cout << refseq[j] << tab << j+1 << tab <<
793 refseq[i] << tab << i+1 << tab << ntot << tab << (*pimg)[h] << endl;
794 if ( verbose & VERB_PROCESS ) {
795 for ( k=0; k<number; k++ )
796 cout << selseq[j*number+k];
797 cout << endl;
798 for ( k=0; k<number; k++ )
799 cout << selseq[i*number+k];
800 cout << endl;
801 }
802 ir++;
803 }
804 pimg->set(j*length+i, (*pimg)[h]);
805 }
806 }
807 if ( verbose & VERB_PROCESS ) {
808 cout << endl << "Correlations reported: " << ir << endl;
809 cout << "Maximum off-diagonal coefficient: " << maxCC << endl << endl;
810 }
811
812 residue_matrix_kill(simat);
813
814 delete[] avg;
815 delete[] std;
816 delete[] num;
817 delete[] score;
818 delete[] weight;
819 delete[] selseq;
820 delete[] refseq;
821
822 return pimg;
823 }
824
825 double* seq_aligned_weight(Bmolgroup* molgroup)
826 {
827 char m1, m2, igap = '-';
828 long i, k, l, nseq, overlap, identity;
829
830 Bmolecule *mol1, *mol2;
831
832 for ( nseq=0, mol1 = molgroup->mol; mol1; mol1 = mol1->next ) nseq++;
833
834 double* weight = new double[nseq*nseq];
835 for ( i=0; i<nseq*nseq; i++ ) weight[i] = 0;
836
837 // Weighting is based on the difference between identity and overlap
838 for ( k=1, mol1 = molgroup->mol->next; k<nseq; k++, mol1 = mol1->next ) {
839 for ( l=0, mol2 = molgroup->mol; l<k; l++, mol2 = mol2->next ) {
840 overlap = identity = 0;
841 for ( i=0; i<molgroup->maxlen; i++ ) {
842 m1 = mol1->seq[i];
843 m2 = mol2->seq[i];
844 if ( m1 != igap && m2 != igap ) {
845 overlap++;
846 if ( m1 == m2 ) identity++;
847 }
848 }
849 weight[k*nseq+l] = (overlap - identity)*1.0/molgroup->maxlen;
850 if ( verbose & VERB_DEBUG )
851 cout << "k=" << k << " l=" << l << " weight=" << weight[k*nseq+l] << endl;
852 }
853 }
854
855 return weight;
856 }
857
858
Bmolecule::id
Bstring id
Definition: rwmolecule.h:110
Bmolgroup::mol
Bmolecule * mol
Definition: rwmolecule.h:189
Bresidue_type::hphob
float hphob
Definition: rwresprop.h:31
seq_util.h
Header file for sequence utilities.
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
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:302
seq_correlated_mutation
Bimage * seq_correlated_mutation(Bmolgroup *molgroup, int refseqnum, double cutoff, Bstring &simfile)
Correlated mutation analysis of an alignment.
Definition: seq_analysis.cpp:676
moving_average.h
Header file moving average calculations.
seq_aligned_profile
char * seq_aligned_profile(Bmolgroup *molgroup)
Generates a PROSITE format profile from an aligned set of sequences.
Definition: seq_analysis.cpp:197
utilities.h
Header file for general utilities.
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
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
Bmolgroup::seqflag
int * seqflag
Definition: rwmolecule.h:188
angle_set_negPI_to_PI
double angle_set_negPI_to_PI(double angle)
Returns an angle between -PI and PI.
Definition: utilities.cpp:594
ps_sequence.h
Header file for postscript output for sequence analysis functions.
Bresidue_matrix::m
float * m
Definition: rwresprop.h:47
kill_list
long kill_list(char *list, unsigned long size)
Frees all the items in a linked list.
Definition: linked_list.cpp:214
Bresidue_type::c
char c
Definition: rwresprop.h:24
Bstring::length
long length() const
Definition: Bstring.cpp:263
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
TSimple
Definition: Bimage.h:58
Float
Definition: utilities.h:157
get_residue_matrix
Bresidue_matrix * get_residue_matrix(Bstring &filename)
Reading a residue matrix from a file.
Definition: rwresprop.cpp:164
verbose
int verbose
Bimage
General image parameter class.
Definition: Bimage.h:163
ResInfo::res
char res[4]
Definition: seq_analysis.cpp:20
Bmolecule::seq
Bstring seq
Definition: rwmolecule.h:114
M_PI
#define M_PI
Definition: utilities.h:59
linked_list.h
Header file for generalized linked list functions.
Bmolgroup
Definition: rwmolecule.h:170
seq_aligned_similarity
Bimage * seq_aligned_similarity(Bmolgroup *molgroup, double threshold, Bresidue_matrix *simat)
Calculates the pairwise similarities between aligned sequences.
Definition: seq_analysis.cpp:119
log_2
double log_2(double a)
Definition: seq_analysis.cpp:25
ResInfo
Definition: seq_analysis.cpp:20
residue_matrix_kill
int residue_matrix_kill(Bresidue_matrix *resmat)
Deallocating a residue matrix.
Definition: rwresprop.cpp:208
seq_aligned_weight
double * seq_aligned_weight(Bmolgroup *molgroup)
Definition: seq_analysis.cpp:825
length
double length(vector< T > v)
Definition: Vector3.h:528
Bmolecule::next
Bmolecule * next
Definition: rwmolecule.h:108
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
Bmolgroup::maxlen
long maxlen
Definition: rwmolecule.h:180
Bresidue_type
Definition: rwresprop.h:22
seq_analysis.h
Header file for sequence analysis functions.
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
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:596
Bresidue_matrix
Definition: rwresprop.h:44
Bresidue_matrix::n
int n
Definition: rwresprop.h:45
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:456
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Bmolecule
Definition: rwmolecule.h:107
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1096
Bresidue_matrix::c
Bstring c
Definition: rwresprop.h:46
VERB_RESULT
#define VERB_RESULT
Definition: utilities.h:105
seq_aligned_identity
Bimage * seq_aligned_identity(Bmolgroup *molgroup)
Calculates the pairwise identities between aligned sequences.
Definition: seq_analysis.cpp:43
Complex
Definition: Complex.h:23
MIN2PI
#define MIN2PI
Definition: utilities.h:71
Bstring
Definition: Bstring.h:29
get_residue_properties
Bresidue_type * get_residue_properties(Bstring &filename)
Reading residue properties from parameter files.
Definition: rwresprop.cpp:99
Bstring::index
long index(const char c)
Definition: Bstring.h:101
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:106
ResInfo::info
double info
Definition: seq_analysis.cpp:20
TWOPI
#define TWOPI
Definition: utilities.h:68
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:21
Bresidue_type::next
Bresidue_type * next
Definition: rwresprop.h:23