source: branches/stable/GDE/CLUSTALW/showpair.c

Last change on this file was 176, checked in by jobb, 23 years ago

* empty log message *

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.6 KB
Line 
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <math.h>
5#include "clustalw.h"   
6
7static void make_p_ptrs(sint *tptr, sint *pl, sint naseq, sint l);
8static void make_n_ptrs(sint *tptr, sint *pl, sint naseq, sint len);
9static void put_frag(sint fs, sint v1, sint v2, sint flen);
10static sint frag_rel_pos(sint a1, sint b1, sint a2, sint b2);
11static void des_quick_sort(sint *array1, sint *array2, sint array_size);
12static void pair_align(sint seq_no, sint l1, sint l2);
13
14
15/*
16*       Prototypes
17*/
18
19/*
20*        Global variables
21*/
22extern sint *seqlen_array;
23extern char **seq_array;
24extern sint  dna_ktup, dna_window, dna_wind_gap, dna_signif; /* params for DNA */
25extern sint prot_ktup,prot_window,prot_wind_gap,prot_signif; /* params for prots */
26extern sint     nseqs;
27extern Boolean  dnaflag;
28extern double   **tmat;
29extern sint     max_aa;
30extern sint  max_aln_length;
31
32static sint     next;
33static sint     curr_frag,maxsf,vatend;
34static sint     **accum;
35static sint     *diag_index;
36static char     *slopes;
37
38sint ktup,window,wind_gap,signif;                     /* Pairwise aln. params */
39sint *displ;
40sint *zza, *zzb, *zzc, *zzd;
41
42extern Boolean percent;
43
44
45static void make_p_ptrs(sint *tptr,sint *pl,sint naseq,sint l)
46{
47        static sint a[10];
48        sint i,j,limit,code,flag;
49        char residue;
50       
51        for (i=1;i<=ktup;i++)
52           a[i] = (sint) pow((double)(max_aa+1),(double)(i-1));
53
54        limit = (sint) pow((double)(max_aa+1),(double)ktup);
55        for(i=1;i<=limit;++i)
56                pl[i]=0;
57        for(i=1;i<=l;++i)
58                tptr[i]=0;
59       
60        for(i=1;i<=(l-ktup+1);++i) {
61                code=0;
62                flag=FALSE;
63                for(j=1;j<=ktup;++j) {
64                        residue = seq_array[naseq][i+j-1];
65                        if((residue<0) || (residue > max_aa)){
66                                flag=TRUE;
67                                break;
68                        }
69                        code += ((residue) * a[j]);
70                }
71                if(flag)
72                        continue;
73                ++code;
74                if(pl[code]!=0)
75                        tptr[i]=pl[code];
76                pl[code]=i;
77        }
78}
79
80
81static void make_n_ptrs(sint *tptr,sint *pl,sint naseq,sint len)
82{
83        static sint pot[]={ 0, 1, 4, 16, 64, 256, 1024, 4096 };
84        sint i,j,limit,code,flag;
85        char residue;
86       
87        limit = (sint) pow((double)4,(double)ktup);
88       
89        for(i=1;i<=limit;++i)
90                pl[i]=0;
91        for(i=1;i<=len;++i)
92                tptr[i]=0;
93       
94        for(i=1;i<=len-ktup+1;++i) {
95                code=0;
96                flag=FALSE;
97                for(j=1;j<=ktup;++j) {
98                        residue = seq_array[naseq][i+j-1];
99                        if((residue<0) || (residue>4)){
100                                flag=TRUE;
101                                break;
102                        }
103                        code += ((residue) * pot[j]);  /* DES */
104                }
105                if(flag)
106                        continue;
107                ++code;
108                if(pl[code]!=0)
109                        tptr[i]=pl[code];
110                pl[code]=i;
111        }
112}
113
114
115static void put_frag(sint fs,sint v1,sint v2,sint flen)
116{
117        sint end;
118        accum[0][curr_frag]=fs;
119        accum[1][curr_frag]=v1;
120        accum[2][curr_frag]=v2;
121        accum[3][curr_frag]=flen;
122       
123        if(!maxsf) {
124                maxsf=1;
125                accum[4][curr_frag]=0;
126                return;
127        }
128       
129        if(fs >= accum[0][maxsf]) {
130                accum[4][curr_frag]=maxsf;
131                maxsf=curr_frag;
132                return;
133        }
134        else {
135                next=maxsf;
136                while(TRUE) {
137                        end=next;
138                        next=accum[4][next];
139                        if(fs>=accum[0][next])
140                                break;
141                }
142                accum[4][curr_frag]=next;
143                accum[4][end]=curr_frag;
144        }
145}
146
147
148static sint frag_rel_pos(sint a1,sint b1,sint a2,sint b2)
149{
150        sint ret;
151       
152        ret=FALSE;
153        if(a1-b1==a2-b2) {
154                if(a2<a1)
155                        ret=TRUE;
156        }
157        else {
158                if(a2+ktup-1<a1 && b2+ktup-1<b1)
159                        ret=TRUE;
160        }
161        return ret;
162}
163
164
165static void des_quick_sort(sint *array1, sint *array2, sint array_size)
166/*  */
167/* Quicksort routine, adapted from chapter 4, page 115 of software tools */
168/* by Kernighan and Plauger, (1986) */
169/* Sort the elements of array1 and sort the */
170/* elements of array2 accordingly */
171/*  */
172{
173        sint temp1, temp2;
174        sint p, pivlin;
175        sint i, j;
176        sint lst[50], ust[50];       /* the maximum no. of elements must be*/
177                                                                /* < log(base2) of 50 */
178
179        lst[1] = 1;
180        ust[1] = array_size-1;
181        p = 1;
182
183        while(p > 0) {
184                if(lst[p] >= ust[p])
185                        p--;
186                else {
187                        i = lst[p] - 1;
188                        j = ust[p];
189                        pivlin = array1[j];
190                        while(i < j) {
191                                for(i=i+1; array1[i] < pivlin; i++)
192                                        ;
193                                for(j=j-1; j > i; j--)
194                                        if(array1[j] <= pivlin) break;
195                                if(i < j) {
196                                        temp1     = array1[i];
197                                        array1[i] = array1[j];
198                                        array1[j] = temp1;
199                                       
200                                        temp2     = array2[i];
201                                        array2[i] = array2[j];
202                                        array2[j] = temp2;
203                                }
204                        }
205                       
206                        j = ust[p];
207
208                        temp1     = array1[i];
209                        array1[i] = array1[j];
210                        array1[j] = temp1;
211
212                        temp2     = array2[i];
213                        array2[i] = array2[j];
214                        array2[j] = temp2;
215
216                        if(i-lst[p] < ust[p] - i) {
217                                lst[p+1] = lst[p];
218                                ust[p+1] = i - 1;
219                                lst[p]   = i + 1;
220                        }
221                        else {
222                                lst[p+1] = i + 1;
223                                ust[p+1] = ust[p];
224                                ust[p]   = i - 1;
225                        }
226                        p = p + 1;
227                }
228        }
229        return;
230
231}
232
233
234
235
236
237static void pair_align(sint seq_no,sint l1,sint l2)
238{
239        sint pot[8],i,j,l,m,flag,limit,pos,tl1,vn1,vn2,flen,osptr,fs;
240        sint tv1,tv2,encrypt,subt1,subt2,rmndr;
241        char residue;
242       
243        if(dnaflag) {
244                for(i=1;i<=ktup;++i)
245                        pot[i] = (sint) pow((double)4,(double)(i-1));
246                limit = (sint) pow((double)4,(double)ktup);
247        }
248        else {
249                for (i=1;i<=ktup;i++)
250                        pot[i] = (sint) pow((double)(max_aa+1),(double)(i-1));
251                limit = (sint) pow((double)(max_aa+1),(double)ktup);
252        }
253       
254        tl1 = (l1+l2)-1;
255       
256        for(i=1;i<=tl1;++i) {
257                slopes[i]=displ[i]=0;
258                diag_index[i] = i;
259        }
260       
261
262/* increment diagonal score for each k_tuple match */
263
264        for(i=1;i<=limit;++i) {
265                vn1=zzc[i];
266                while(TRUE) {
267                        if(!vn1) break;
268                        vn2=zzd[i];
269                        while(vn2 != 0) {
270                                osptr=vn1-vn2+l2;
271                                ++displ[osptr];
272                                vn2=zzb[vn2];
273                        }
274                        vn1=zza[vn1];
275                }
276        }
277
278/* choose the top SIGNIF diagonals */
279
280        des_quick_sort(displ, diag_index, tl1);
281
282        j = tl1 - signif + 1;
283        if(j < 1) j = 1;
284 
285/* flag all diagonals within WINDOW of a top diagonal */
286
287        for(i=tl1; i>=j; i--) 
288                if(displ[i] > 0) {
289                        pos = diag_index[i];
290                        l = (1  >pos-window) ? 1   : pos-window;
291                        m = (tl1<pos+window) ? tl1 : pos+window;
292                        for(; l <= m; l++) 
293                                slopes[l] = 1;
294                }
295
296        for(i=1; i<=tl1; i++)  displ[i] = 0;
297
298       
299        curr_frag=maxsf=0;
300       
301        for(i=1;i<=(l1-ktup+1);++i) {
302                encrypt=flag=0;
303                for(j=1;j<=ktup;++j) {
304                        residue = seq_array[seq_no][i+j-1];
305                        if((residue<0) || (residue>max_aa)) {
306                                flag=TRUE;
307                                break;
308                        }
309                        encrypt += ((residue)*pot[j]);
310                }
311                if(flag) continue;
312                ++encrypt;
313       
314                vn2=zzd[encrypt];
315       
316                flag=FALSE;
317                while(TRUE) {
318                        if(!vn2) {
319                                flag=TRUE;
320                                break;
321                        }
322                        osptr=i-vn2+l2;
323                        if(slopes[osptr]!=1) {
324                                vn2=zzb[vn2];
325                                continue;
326                        }
327                        flen=0;
328                        fs=ktup;
329                        next=maxsf;             
330               
331                /*
332                * A-loop
333                */
334               
335                        while(TRUE) {
336                                if(!next) {
337                                        ++curr_frag;
338                                        if(curr_frag>=2*max_aln_length) {
339                                                info("(Partial alignment)");
340                                                vatend=1;
341                                                return;
342                                        }
343                                        displ[osptr]=curr_frag;
344                                        put_frag(fs,i,vn2,flen);
345                                }
346                                else {
347                                        tv1=accum[1][next];
348                                        tv2=accum[2][next];
349                                        if(frag_rel_pos(i,vn2,tv1,tv2)) {
350                                                if(i-vn2==accum[1][next]-accum[2][next]) {
351                                                        if(i>accum[1][next]+(ktup-1))
352                                                                fs=accum[0][next]+ktup;
353                                                        else {
354                                                                rmndr=i-accum[1][next];
355                                                                fs=accum[0][next]+rmndr;
356                                                        }
357                                                        flen=next;
358                                                        next=0;
359                                                        continue;
360                                                }
361                                                else {
362                                                        if(displ[osptr]==0)
363                                                                subt1=ktup;
364                                                        else {
365                                                                if(i>accum[1][displ[osptr]]+(ktup-1))
366                                                                        subt1=accum[0][displ[osptr]]+ktup;
367                                                                else {
368                                                                        rmndr=i-accum[1][displ[osptr]];
369                                                                        subt1=accum[0][displ[osptr]]+rmndr;
370                                                                }
371                                                        }
372                                                        subt2=accum[0][next]-wind_gap+ktup;
373                                                        if(subt2>subt1) {
374                                                                flen=next;
375                                                                fs=subt2;
376                                                        }
377                                                        else {
378                                                                flen=displ[osptr];
379                                                                fs=subt1;
380                                                        }
381                                                        next=0;
382                                                        continue;
383                                                }
384                                        }
385                                        else {
386                                                next=accum[4][next];
387                                                continue;
388                                        }
389                                }
390                                break;
391                        }
392                /*
393                * End of Aloop
394                */
395               
396                        vn2=zzb[vn2];
397                }
398        }
399        vatend=0;
400}               
401
402void show_pair(sint istart, sint iend, sint jstart, sint jend)
403{
404        sint i,j,dsr;
405        double calc_score;
406       
407        accum = (sint **)ckalloc( 5*sizeof (sint *) );
408        for (i=0;i<5;i++)
409                accum[i] = (sint *) ckalloc((2*max_aln_length+1) * sizeof (sint) );
410
411        displ      = (sint *) ckalloc( (2*max_aln_length +1) * sizeof (sint) );
412        slopes     = (char *)ckalloc( (2*max_aln_length +1) * sizeof (char));
413        diag_index = (sint *) ckalloc( (2*max_aln_length +1) * sizeof (sint) );
414
415        zza = (sint *)ckalloc( (max_aln_length+1) * sizeof (sint) );
416        zzb = (sint *)ckalloc( (max_aln_length+1) * sizeof (sint) );
417
418        zzc = (sint *)ckalloc( (max_aln_length+1) * sizeof (sint) );
419        zzd = (sint *)ckalloc( (max_aln_length+1) * sizeof (sint) );
420
421        if(dnaflag) {
422                ktup     = dna_ktup;
423                window   = dna_window;
424                signif   = dna_signif;
425                wind_gap = dna_wind_gap;
426        }
427        else {
428                ktup     = prot_ktup;
429                window   = prot_window;
430                signif   = prot_signif;
431                wind_gap = prot_wind_gap;
432        }
433
434        fprintf(stdout,"\n\n");
435       
436        for(i=istart+1;i<=iend;++i) {
437                if(dnaflag)
438                        make_n_ptrs(zza,zzc,i,seqlen_array[i]);
439                else
440                        make_p_ptrs(zza,zzc,i,seqlen_array[i]);
441                for(j=jstart+2;j<=jend;++j) {
442                        if(dnaflag)
443                                make_n_ptrs(zzb,zzd,j,seqlen_array[j]);
444                        else
445                                make_p_ptrs(zzb,zzd,j,seqlen_array[j]);
446                        pair_align(i,seqlen_array[i],seqlen_array[j]);
447                        if(!maxsf)
448                                calc_score=0.0;
449                        else {
450                                calc_score=(double)accum[0][maxsf];
451                                if(percent) {
452                                        dsr=(seqlen_array[i]<seqlen_array[j]) ?
453                                                        seqlen_array[i] : seqlen_array[j];
454                                calc_score = (calc_score/(double)dsr) * 100.0;
455                                }
456                        }
457/*
458                        tmat[i][j]=calc_score;
459                        tmat[j][i]=calc_score;
460*/
461
462                        tmat[i][j] = (100.0 - calc_score)/100.0;
463                        tmat[j][i] = (100.0 - calc_score)/100.0;
464                        if(calc_score>0.1) 
465                                info("Sequences (%d:%d) Aligned. Score: %lg",
466                                (pint)i,(pint)j,calc_score);
467                        else
468                                info("Sequences (%d:%d) Not Aligned",
469                                                (pint)i,(pint)j);
470                }
471        }
472
473        for (i=0;i<5;i++)
474           accum[i]=ckfree((void *)accum[i]);
475        accum=ckfree((void *)accum);
476
477        displ=ckfree((void *)displ);
478        slopes=ckfree((void *)slopes);
479        diag_index=ckfree((void *)diag_index);
480
481        zza=ckfree((void *)zza);
482        zzb=ckfree((void *)zzb);
483        zzc=ckfree((void *)zzc);
484        zzd=ckfree((void *)zzd);
485}
486
Note: See TracBrowser for help on using the repository browser.