source: tags/arb-6.0/GDE/CLUSTALW/pairalign.c

Last change on this file was 1754, checked in by westram, 21 years ago

updated to version 1.83

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.5 KB
Line 
1/* Change int h to int gh everywhere  DES June 1994 */
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <math.h>
6#include "clustalw.h"
7
8#define MIN(a,b) ((a)<(b)?(a):(b))
9#define MAX(a,b) ((a)>(b)?(a):(b))
10
11#define gap(k)  ((k) <= 0 ? 0 : g + gh * (k))
12#define tbgap(k)  ((k) <= 0 ? 0 : tb + gh * (k))
13#define tegap(k)  ((k) <= 0 ? 0 : te + gh * (k))
14
15/*
16 *      Prototypes
17 */
18static void add(sint v);
19static sint calc_score(sint iat, sint jat, sint v1, sint v2);
20static float tracepath(sint tsb1,sint tsb2);
21static void forward_pass(char *ia, char *ib, sint n, sint m);
22static void reverse_pass(char *ia, char *ib);
23static sint diff(sint A, sint B, sint M, sint N, sint tb, sint te);
24static void del(sint k);
25
26/*
27 *   Global variables
28 */
29#ifdef MAC
30#define pwint   short
31#else
32#define pwint   int
33#endif
34static sint             int_scale;
35
36extern double   **tmat;
37extern float    pw_go_penalty;
38extern float    pw_ge_penalty;
39extern float    transition_weight;
40extern sint     nseqs;
41extern sint     max_aa;
42extern sint     gap_pos1,gap_pos2;
43extern sint     max_aln_length;
44extern sint     *seqlen_array;
45extern sint     debug;
46extern sint     mat_avscore;
47extern short    blosum30mt[],pam350mt[],idmat[],pw_usermat[],pw_userdnamat[];
48extern short    clustalvdnamt[],swgapdnamt[];
49extern short    gon250mt[];
50extern short    def_dna_xref[],def_aa_xref[],pw_dna_xref[],pw_aa_xref[];
51extern Boolean  dnaflag;
52extern char     **seq_array;
53extern char     *amino_acid_codes;
54extern char     pw_mtrxname[];
55extern char     pw_dnamtrxname[];
56
57static float    mm_score;
58static sint     print_ptr,last_print;
59static sint     *displ;
60static pwint    *HH, *DD, *RR, *SS;
61static sint     g, gh;
62static sint     seq1, seq2;
63static sint     matrix[NUMRES][NUMRES];
64static pwint    maxscore;
65static sint     sb1, sb2, se1, se2;
66
67
68sint pairalign(sint istart, sint iend, sint jstart, sint jend)
69{
70  short  *mat_xref;
71  static sint    si, sj, i;
72  static sint    n,m,len1,len2;
73  static sint    maxres;
74  static short    *matptr;
75  static char   c;
76  static float gscale,ghscale;
77
78  displ = (sint *)ckalloc((2*max_aln_length+1) * sizeof(sint));
79  HH = (pwint *)ckalloc((max_aln_length) * sizeof(pwint));
80  DD = (pwint *)ckalloc((max_aln_length) * sizeof(pwint));
81  RR = (pwint *)ckalloc((max_aln_length) * sizeof(pwint));
82  SS = (pwint *)ckalloc((max_aln_length) * sizeof(pwint));
83               
84#ifdef MAC
85  int_scale = 10;
86#else
87  int_scale = 100;
88#endif
89  gscale=ghscale=1.0;
90  if (dnaflag)
91    {
92      if (debug>1) fprintf(stdout,"matrix %s\n",pw_dnamtrxname);
93      if (strcmp(pw_dnamtrxname, "iub") == 0)
94        { 
95          matptr = swgapdnamt;
96          mat_xref = def_dna_xref;
97        }
98      else if (strcmp(pw_dnamtrxname, "clustalw") == 0)
99        { 
100          matptr = clustalvdnamt;
101          mat_xref = def_dna_xref;
102          gscale=0.6667;
103          ghscale=0.751;
104        }
105      else
106        {
107          matptr = pw_userdnamat;
108          mat_xref = pw_dna_xref;
109        }
110      maxres = get_matrix(matptr, mat_xref, matrix, TRUE, int_scale);
111      if (maxres == 0) return((sint)-1);
112
113      matrix[0][4]=transition_weight*matrix[0][0];
114      matrix[4][0]=transition_weight*matrix[0][0];
115      matrix[2][11]=transition_weight*matrix[0][0];
116      matrix[11][2]=transition_weight*matrix[0][0];
117      matrix[2][12]=transition_weight*matrix[0][0];
118      matrix[12][2]=transition_weight*matrix[0][0];
119    }
120  else
121    {
122      if (debug>1) fprintf(stdout,"matrix %s\n",pw_mtrxname);
123      if (strcmp(pw_mtrxname, "blosum") == 0)
124        {
125          matptr = blosum30mt;
126          mat_xref = def_aa_xref;
127        }
128      else if (strcmp(pw_mtrxname, "pam") == 0)
129        {
130          matptr = pam350mt;
131          mat_xref = def_aa_xref;
132        }
133      else if (strcmp(pw_mtrxname, "gonnet") == 0)
134        {
135          matptr = gon250mt;
136          int_scale /= 10;
137          mat_xref = def_aa_xref;
138        }
139      else if (strcmp(pw_mtrxname, "id") == 0)
140        {
141          matptr = idmat;
142          mat_xref = def_aa_xref;
143        }
144      else
145        {
146          matptr = pw_usermat;
147          mat_xref = pw_aa_xref;
148        }
149
150      maxres = get_matrix(matptr, mat_xref, matrix, TRUE, int_scale);
151      if (maxres == 0) return((sint)-1);
152    }
153
154
155  for (si=MAX(0,istart);si<nseqs && si<iend;si++)
156    {
157      n = seqlen_array[si+1];
158      len1 = 0;
159      for (i=1;i<=n;i++) {
160        c = seq_array[si+1][i];
161        if ((c!=gap_pos1) && (c != gap_pos2)) len1++;
162      }
163
164      for (sj=MAX(si+1,jstart+1);sj<nseqs && sj<jend;sj++)
165        {
166          m = seqlen_array[sj+1];
167          if(n==0 || m==0) {
168            tmat[si+1][sj+1]=1.0;
169            tmat[sj+1][si+1]=1.0;
170            continue;
171          }
172          len2 = 0;
173          for (i=1;i<=m;i++) {
174            c = seq_array[sj+1][i];
175            if ((c!=gap_pos1) && (c != gap_pos2)) len2++;
176          }
177
178          if (dnaflag) {
179            g = 2 * (float)pw_go_penalty * int_scale*gscale;
180            gh = pw_ge_penalty * int_scale*ghscale;
181          }
182          else {
183            if (mat_avscore <= 0)
184              g = 2 * (float)(pw_go_penalty + log((double)(MIN(n,m))))*int_scale;
185            else
186              g = 2 * mat_avscore * (float)(pw_go_penalty +
187                                            log((double)(MIN(n,m))))*gscale;
188            gh = pw_ge_penalty * int_scale;
189          }
190
191          if (debug>1) fprintf(stdout,"go %d ge %d\n",(pint)g,(pint)gh);
192
193          /*
194            align the sequences
195          */
196          seq1 = si+1;
197        seq2 = sj+1;
198
199        forward_pass(&seq_array[seq1][0], &seq_array[seq2][0],
200           n, m);
201
202        reverse_pass(&seq_array[seq1][0], &seq_array[seq2][0]);
203
204        last_print = 0;
205        print_ptr = 1;
206/*
207        sb1 = sb2 = 1;
208        se1 = n-1;
209        se2 = m-1;
210*/
211
212/* use Myers and Miller to align two sequences */
213
214        maxscore = diff(sb1-1, sb2-1, se1-sb1+1, se2-sb2+1, 
215        (sint)0, (sint)0);
216 
217/* calculate percentage residue identity */
218
219        mm_score = tracepath(sb1,sb2);
220
221                if(len1==0 || len2==0) mm_score=0;
222                else
223                        mm_score /= (float)MIN(len1,len2);
224
225        tmat[si+1][sj+1] = ((float)100.0 - mm_score)/(float)100.0;
226        tmat[sj+1][si+1] = ((float)100.0 - mm_score)/(float)100.0;
227
228if (debug>1)
229{
230        fprintf(stdout,"Sequences (%d:%d) Aligned. Score: %d CompScore:  %d\n",
231                           (pint)si+1,(pint)sj+1, 
232                           (pint)mm_score, 
233                           (pint)maxscore/(MIN(len1,len2)*100));
234}
235else
236{
237        info("Sequences (%d:%d) Aligned. Score:  %d",
238                                      (pint)si+1,(pint)sj+1, 
239                                      (pint)mm_score);
240}
241
242   }
243  }
244   displ=ckfree((void *)displ);
245   HH=ckfree((void *)HH);
246   DD=ckfree((void *)DD);
247   RR=ckfree((void *)RR);
248   SS=ckfree((void *)SS);
249
250
251  return((sint)1);
252}
253
254static void add(sint v)
255{
256
257        if(last_print<0) {
258                displ[print_ptr-1] = v;
259                displ[print_ptr++] = last_print;
260        }
261        else
262                last_print = displ[print_ptr++] = v;
263}
264
265static sint calc_score(sint iat,sint jat,sint v1,sint v2)
266{
267        sint ipos,jpos;
268                sint ret;
269
270        ipos = v1 + iat;
271        jpos = v2 + jat;
272
273        ret=matrix[(int)seq_array[seq1][ipos]][(int)seq_array[seq2][jpos]];
274
275        return(ret);
276}
277
278
279static float tracepath(sint tsb1,sint tsb2)
280{
281        char c1,c2;
282    sint  i1,i2,r;
283    sint i,k,pos,to_do;
284        sint count;
285        float score;
286        char s1[600], s2[600];
287
288        to_do=print_ptr-1;
289        i1 = tsb1;
290        i2 = tsb2;
291
292        pos = 0;
293        count = 0;
294        for(i=1;i<=to_do;++i) {
295
296          if (debug>1) fprintf(stdout,"%d ",(pint)displ[i]);
297          if(displ[i]==0) {
298            c1 = seq_array[seq1][i1];
299            c2 = seq_array[seq2][i2];
300           
301            if (debug>0)
302              {
303                if (c1>max_aa) s1[pos] = '-';
304                else s1[pos]=amino_acid_codes[c1];
305                if (c2>max_aa) s2[pos] = '-';
306                else s2[pos]=amino_acid_codes[c2];
307              }
308           
309            if ((c1!=gap_pos1) && (c1 != gap_pos2) &&
310                (c1 == c2)) count++;
311            ++i1;
312            ++i2;
313            ++pos;
314          }
315          else {
316            if((k=displ[i])>0) {
317             
318              if (debug>0)
319                for (r=0;r<k;r++)
320                  {
321                    s1[pos+r]='-';
322                    if (seq_array[seq2][i2+r]>max_aa) s2[pos+r] = '-';
323                    else s2[pos+r]=amino_acid_codes[seq_array[seq2][i2+r]];
324                  }
325             
326              i2 += k;
327              pos += k;
328            }
329            else {
330             
331              if (debug>0)
332                for (r=0;r<(-k);r++)
333                  {
334                    s2[pos+r]='-';
335                    if (seq_array[seq1][i1+r]>max_aa) s1[pos+r] = '-';
336                    else s1[pos+r]=amino_acid_codes[seq_array[seq1][i1+r]];
337                  }
338             
339              i1 -= k;
340              pos -= k;
341            }
342          }
343        }
344        if (debug>0) fprintf(stdout,"\n");
345        if (debug>0) 
346          {
347            for (i=0;i<pos;i++) fprintf(stdout,"%c",s1[i]);
348            fprintf(stdout,"\n");
349            for (i=0;i<pos;i++) fprintf(stdout,"%c",s2[i]);
350            fprintf(stdout,"\n");
351          }
352        /*
353          if (count <= 0) count = 1;
354        */
355        score = 100.0 * (float)count;
356        return(score);
357}
358
359
360static void forward_pass(char *ia, char *ib, sint n, sint m)
361{
362
363  sint i,j;
364  pwint f,hh,p,t;
365
366  maxscore = 0;
367  se1 = se2 = 0;
368  for (i=0;i<=m;i++)
369    {
370       HH[i] = 0;
371       DD[i] = -g;
372    }
373
374  for (i=1;i<=n;i++)
375     {
376        hh = p = 0;
377                f = -g;
378
379        for (j=1;j<=m;j++)
380           {
381
382              f -= gh; 
383              t = hh - g - gh;
384              if (f<t) f = t;
385
386              DD[j] -= gh;
387              t = HH[j] - g - gh;
388              if (DD[j]<t) DD[j] = t;
389
390              hh = p + matrix[(int)ia[i]][(int)ib[j]];
391              if (hh<f) hh = f;
392              if (hh<DD[j]) hh = DD[j];
393              if (hh<0) hh = 0;
394
395              p = HH[j];
396              HH[j] = hh;
397
398              if (hh > maxscore)
399                {
400                   maxscore = hh;
401                   se1 = i;
402                   se2 = j;
403                }
404           }
405     }
406
407}
408
409
410static void reverse_pass(char *ia, char *ib)
411{
412
413  sint i,j;
414  pwint f,hh,p,t;
415  pwint cost;
416
417  cost = 0;
418  sb1 = sb2 = 1;
419  for (i=se2;i>0;i--)
420    {
421       HH[i] = -1;
422       DD[i] = -1;
423    }
424
425  for (i=se1;i>0;i--)
426     {
427        hh = f = -1;
428        if (i == se1) p = 0;
429        else p = -1;
430
431        for (j=se2;j>0;j--)
432           {
433
434              f -= gh; 
435              t = hh - g - gh;
436              if (f<t) f = t;
437
438              DD[j] -= gh;
439              t = HH[j] - g - gh;
440              if (DD[j]<t) DD[j] = t;
441
442              hh = p + matrix[(int)ia[i]][(int)ib[j]];
443              if (hh<f) hh = f;
444              if (hh<DD[j]) hh = DD[j];
445
446              p = HH[j];
447              HH[j] = hh;
448
449              if (hh > cost)
450                {
451                   cost = hh;
452                   sb1 = i;
453                   sb2 = j;
454                   if (cost >= maxscore) break;
455                }
456           }
457        if (cost >= maxscore) break;
458     }
459
460}
461
462static int diff(sint A,sint B,sint M,sint N,sint tb,sint te)
463{
464  sint type;
465  sint midi,midj,i,j;
466  int midh;
467  static pwint f, hh, e, s, t;
468 
469  if(N<=0)  {
470    if(M>0) {
471      del(M);
472    }
473   
474    return(-(int)tbgap(M));
475  }
476 
477  if(M<=1) {
478    if(M<=0) {
479      add(N);
480      return(-(int)tbgap(N));
481    }
482   
483    midh = -(tb+gh) - tegap(N);
484    hh = -(te+gh) - tbgap(N);
485    if (hh>midh) midh = hh;
486    midj = 0;
487    for(j=1;j<=N;j++) {
488      hh = calc_score(1,j,A,B)
489        - tegap(N-j) - tbgap(j-1);
490      if(hh>midh) {
491        midh = hh;
492        midj = j;
493      }
494    }
495   
496    if(midj==0) {
497      del(1);
498      add(N);
499    }
500    else {
501      if(midj>1)
502        add(midj-1);
503      displ[print_ptr++] = last_print = 0;
504      if(midj<N)
505        add(N-midj);
506    }
507    return midh;
508  }
509 
510/* Divide: Find optimum midpoint (midi,midj) of cost midh */
511 
512  midi = M / 2;
513  HH[0] = 0.0;
514  t = -tb;
515  for(j=1;j<=N;j++) {
516    HH[j] = t = t-gh;
517    DD[j] = t-g;
518  }
519 
520  t = -tb;
521  for(i=1;i<=midi;i++) {
522    s=HH[0];
523    HH[0] = hh = t = t-gh;
524    f = t-g;
525    for(j=1;j<=N;j++) {
526      if ((hh=hh-g-gh) > (f=f-gh)) f=hh;
527      if ((hh=HH[j]-g-gh) > (e=DD[j]-gh)) e=hh;
528      hh = s + calc_score(i,j,A,B);
529      if (f>hh) hh = f;
530      if (e>hh) hh = e;
531     
532      s = HH[j];
533      HH[j] = hh;
534      DD[j] = e;
535    }
536  }
537 
538  DD[0]=HH[0];
539 
540  RR[N]=0;
541  t = -te;
542  for(j=N-1;j>=0;j--) {
543    RR[j] = t = t-gh;
544    SS[j] = t-g;
545  }
546 
547  t = -te;
548  for(i=M-1;i>=midi;i--) {
549    s = RR[N];
550    RR[N] = hh = t = t-gh;
551    f = t-g;
552   
553    for(j=N-1;j>=0;j--) {
554     
555      if ((hh=hh-g-gh) > (f=f-gh)) f=hh;
556      if ((hh=RR[j]-g-gh) > (e=SS[j]-gh)) e=hh;
557      hh = s + calc_score(i+1,j+1,A,B);
558      if (f>hh) hh = f;
559      if (e>hh) hh = e;
560     
561      s = RR[j];
562      RR[j] = hh;
563      SS[j] = e;
564     
565    }
566  }
567 
568  SS[N]=RR[N];
569 
570  midh=HH[0]+RR[0];
571  midj=0;
572  type=1;
573  for(j=0;j<=N;j++) {
574    hh = HH[j] + RR[j];
575    if(hh>=midh)
576      if(hh>midh || (HH[j]!=DD[j] && RR[j]==SS[j])) {
577        midh=hh;
578        midj=j;
579      }
580  }
581 
582  for(j=N;j>=0;j--) {
583    hh = DD[j] + SS[j] + g;
584    if(hh>midh) {
585      midh=hh;
586      midj=j;
587      type=2;
588    }
589  }
590 
591  /* Conquer recursively around midpoint  */
592 
593 
594  if(type==1) {             /* Type 1 gaps  */
595    diff(A,B,midi,midj,tb,g);
596    diff(A+midi,B+midj,M-midi,N-midj,g,te);
597  }
598  else {
599    diff(A,B,midi-1,midj,tb,0.0);
600    del(2);
601    diff(A+midi+1,B+midj,M-midi-1,N-midj,0.0,te);
602  }
603 
604  return midh;       /* Return the score of the best alignment */
605}
606
607static void del(sint k)
608{
609  if(last_print<0)
610    last_print = displ[print_ptr-1] -= k;
611  else
612    last_print = displ[print_ptr++] = -(k);
613}
614
615
Note: See TracBrowser for help on using the repository browser.