Next version of JABA
[jabaws.git] / binaries / src / tcoffee / t_coffee_source / util_dp_ssec_pwaln.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <stdarg.h>
5 #include <string.h>
6
7 #include "io_lib_header.h"
8 #include "util_lib_header.h"
9 #include "define_header.h"
10 #include "dp_lib_header.h"
11
12 int ssec_pwaln_maln (Alignment *A, int *ns, int **ls, Constraint_list *CL)
13   {
14
15     static Dp_Model *M=NULL;
16     Dp_Result *R=NULL;
17     int a, ndiag;
18     int Sa,Sb,St, Da, Db, Dt, Ia, Ib, It;
19     int ala, alb, s,b;
20
21     a=0;    
22     Sa=a++;Da=a++;Ia=a++;
23     Sb=a++;Db=a++;Ib=a++;
24     St=a++;Dt=a++;It=a++;
25     
26     if ( strm (CL->matrices_list[0], "analyse"))
27       {
28       for ( a=0; a< CL->n_matrices; a++)
29         {
30
31           rescale_two_mat(CL->matrices_list[1],CL->matrices_list[2],1000, 100, AA_ALPHABET);    
32           exit (0);
33         }
34       }
35
36     
37
38     /*2 Prepare the Model*/
39     M=initialize_sseq_model(2,2,CL);    
40     ndiag=strlen (A->seq_al[0])+strlen (A->seq_al[1])-1;
41     M->diag=vcalloc (ndiag+1, sizeof (int));
42     M->diag[0]=ndiag-1;
43     for ( a=1; a<=M->diag[0]; a++)M->diag[a]=a;
44     
45     /*3 Prepare Sequence Presentation*/
46     R=make_fast_generic_dp_pair_wise(A, ns, ls, M);
47     
48   
49     ala=alb=0;
50
51     A=realloc_aln2(A,A->nseq, R->len+1);
52     for (b=1; b<R->len;b++)
53       {
54         if (R->traceback[b]==Sa ||  R->traceback[b]==Sb ||R->traceback[b]==St )
55           {
56             for (s=0; s<ns[0]; s++)
57               A->seq_al[ls[0][s]][b-1]=(CL->S)->seq[A->order[ls[0][s]][0]][ala];
58             ala++;
59             for (s=0; s<ns[1]; s++)
60               A->seq_al[ls[1][s]][b-1]=(CL->S)->seq[A->order[ls[1][s]][0]][alb];
61             alb++;
62           }
63         else if ( R->traceback[b]==Da ||  R->traceback[b]==Db ||R->traceback[b]==Dt )
64           {
65             for (s=0; s<ns[0]; s++)
66               A->seq_al[ls[0][s]][b-1]=(CL->S)->seq[A->order[ls[0][s]][0]][ala];
67             ala++;
68             for (s=0; s<ns[1]; s++)
69               A->seq_al[ls[1][s]][b-1]='-';        
70           }
71         else if ( R->traceback[b]==Ia ||  R->traceback[b]==Ib ||R->traceback[b]==It )
72           {
73             for (s=0; s<ns[0]; s++)
74               A->seq_al[ls[0][s]][b-1]='-';
75             
76             for (s=0; s<ns[1]; s++)
77               A->seq_al[ls[1][s]][b-1]=(CL->S)->seq[A->order[ls[1][s]][0]][alb];
78             alb++;
79           }
80       }
81     for (s=0; s<ns[0]; s++)
82       A->seq_al[ls[0][s]][b-1]='\0';
83     for (s=0; s<ns[1]; s++)
84       A->seq_al[ls[1][s]][b-1]='\0';
85     
86     A->len_aln=strlen (A->seq_al[ls[0][0]]);
87     R->Dp_model=M;
88     A->Dp_result=R;
89     return A->score;
90   }
91
92 Dp_Model * initialize_sseq_model(int left_tg_mode, int right_tg_mode, Constraint_list *CL)
93   {
94     
95     Dp_Model *M;
96     int a, b, c,d;
97     int Sa,Sb,St, Da, Db, Dt, Ia, Ib, It;
98     int tgop=CL->gep*3;
99     
100
101     
102     
103     M=vcalloc ( 1, sizeof (Dp_Model));
104     
105     M->nstate=9;
106     M->START=M->nstate++;
107     M->END  =M->nstate++;
108     
109     M->model_comments=declare_char (M->nstate+1, 100);
110     M->bounded_model=declare_int (M->nstate+1, M->nstate+1); 
111     M->model=declare_int (M->nstate+1, M->nstate+1); 
112     for ( a=0; a<=M->nstate; a++)
113       for ( b=0; b<= M->nstate; b++)
114         M->model[a][b]=UNDEFINED;
115     
116     
117     M->model_properties=declare_int ( M->nstate, 10); 
118     
119     a=0;     
120     M->TYPE=a++;M->LEN_I=a++; M->LEN_J=a++; M->DELTA_I=a++;M->DELTA_J=a++;M->EMISSION=a++;M->TERM_EMISSION=a++;M->START_EMISSION=a++;
121     M->CODING0=a++;M->DELETION=a++;
122     M->model_properties=declare_int ( M->nstate, 10); 
123
124     a=0;
125     M->EMISSION=a++;M->TERM_EMISSION=a++;M->START_EMISSION=a++;
126     M->model_emission_function=vcalloc(M->nstate, sizeof (int (**)(Alignment*, int **, int, int*, int, int **, int, int*, int, struct Constraint_list *)));
127     for ( a=0; a< M->nstate; a++)
128        M->model_emission_function[a]=vcalloc(3, sizeof (int (*)(Alignment*, int **, int, int*, int, int **, int, int*, int, struct Constraint_list *)));
129     
130
131         
132     a=0;
133     
134     Sa=a++;Da=a++;Ia=a++;
135     Sb=a++;Db=a++;Ib=a++;
136     St=a++;Dt=a++;It=a++;
137    
138
139     sprintf ( M->model_comments[M->START], "START");
140     sprintf ( M->model_comments[M->END], "END");
141               
142     /*ALPHA*/
143     /*Substitution in Alpha*/
144     if (CL->matrices_list[0][0])sprintf ( M->model_comments[Sa], "Substitution %s", CL->matrices_list[0]);
145     M->model_properties[Sa][M->TYPE]=Sa;
146     M->model_properties[Sa][M->LEN_I]=1;
147     M->model_properties[Sa][M->LEN_J]=1;
148     M->model_properties[Sa][M->DELTA_I]=-1;
149     M->model_properties[Sa][M->DELTA_J]= 0;     
150
151     M->model_emission_function[Sa][M->EMISSION]      =get_alpha_sub_cost;
152     M->model_emission_function[Sa][M->START_EMISSION]=get_ssec_no_cost;
153     M->model_emission_function[Sa][M->TERM_EMISSION] =get_ssec_no_cost;
154    
155     /*Deletions*/       
156     if (CL->matrices_list[0][0])sprintf ( M->model_comments[Da], "Deletion %s", CL->matrices_list[0]);
157     M->model_properties[Da][M->TYPE]=Da;
158     M->model_properties[Da][M->LEN_I]=1;
159     M->model_properties[Da][M->LEN_J]=0;
160     M->model_properties[Da][M->DELTA_I]=-1;
161     M->model_properties[Da][M->DELTA_J]=+1;
162
163     
164     M->model_emission_function[Da][M->EMISSION]      =get_alpha_gep_cost;
165     M->model_emission_function[Da][M->START_EMISSION]=get_alpha_start_gep_cost;
166     M->model_emission_function[Da][M->TERM_EMISSION] =get_alpha_term_gep_cost;
167
168         
169     /*Insertion*/
170     if (CL->matrices_list[0][0])sprintf ( M->model_comments[Ia], "Insertion %s", CL->matrices_list[0]);
171     M->model_properties[Ia][M->TYPE]=Ia;
172     M->model_properties[Ia][M->LEN_I]=0;
173     M->model_properties[Ia][M->LEN_J]=1;
174     M->model_properties[Ia][M->DELTA_I]=0;
175     M->model_properties[Ia][M->DELTA_J]=-1;
176     
177     M->model_emission_function[Ia][M->EMISSION]      =get_alpha_gep_cost;
178     M->model_emission_function[Ia][M->START_EMISSION]=get_alpha_start_gep_cost;
179     M->model_emission_function[Ia][M->TERM_EMISSION] =get_alpha_term_gep_cost;
180     
181 /*BETA*/
182     /*Substitution in Beta*/
183     if (CL->matrices_list[1][0])sprintf ( M->model_comments[Sb], "Substitution %s", CL->matrices_list[1]);
184     M->model_properties[Sb][M->TYPE]=Sb;
185     M->model_properties[Sb][M->LEN_I]=1;
186     M->model_properties[Sb][M->LEN_J]=1;
187     M->model_properties[Sb][M->DELTA_I]=-1;
188     M->model_properties[Sb][M->DELTA_J]= 0;     
189     
190     M->model_emission_function[Sb][M->EMISSION]      =get_beta_sub_cost;
191     M->model_emission_function[Sb][M->START_EMISSION]=get_ssec_no_cost;
192     M->model_emission_function[Sb][M->TERM_EMISSION] =get_ssec_no_cost;
193     
194    
195     /*Deletions*/       
196     if (CL->matrices_list[1][0])sprintf ( M->model_comments[Db], "Deletion %s", CL->matrices_list[1]);
197     M->model_properties[Db][M->TYPE]=Db;
198     M->model_properties[Db][M->LEN_I]=1;
199     M->model_properties[Db][M->LEN_J]=0;
200     M->model_properties[Db][M->DELTA_I]=-1;
201     M->model_properties[Db][M->DELTA_J]=+1;
202     
203     M->model_emission_function[Db][M->EMISSION]      =get_beta_gep_cost;
204     M->model_emission_function[Db][M->START_EMISSION]=get_beta_start_gep_cost;
205     M->model_emission_function[Db][M->TERM_EMISSION] =get_beta_term_gep_cost;
206     
207     
208     /*Insertion*/
209     
210     if (CL->matrices_list[1][0])sprintf ( M->model_comments[Ib], "Insertion %s", CL->matrices_list[1]);
211     M->model_properties[Ib][M->TYPE]=Ib;
212     M->model_properties[Ib][M->LEN_I]=0;
213     M->model_properties[Ib][M->LEN_J]=1;
214     M->model_properties[Ib][M->DELTA_I]=0;
215     M->model_properties[Ib][M->DELTA_J]=-1;
216
217     
218     
219     M->model_emission_function[Ib][M->EMISSION]      =get_beta_gep_cost;
220     M->model_emission_function[Ib][M->START_EMISSION]=get_beta_start_gep_cost;
221     M->model_emission_function[Ib][M->TERM_EMISSION] =get_beta_term_gep_cost;
222     
223  /*TURNS*/
224     /*Substitution in Turn*/
225     if (CL->matrices_list[2][0])sprintf ( M->model_comments[St], "Substitution %s", CL->matrices_list[2]);
226     M->model_properties[St][M->TYPE]=St;
227     M->model_properties[St][M->LEN_I]=1;
228     M->model_properties[St][M->LEN_J]=1;
229     M->model_properties[St][M->DELTA_I]=-1;
230     M->model_properties[St][M->DELTA_J]= 0;
231         
232     M->model_emission_function[St][M->EMISSION]      =get_turn_sub_cost;
233     M->model_emission_function[St][M->START_EMISSION]=get_ssec_no_cost;
234     M->model_emission_function[St][M->TERM_EMISSION] =get_ssec_no_cost;
235     
236    
237     /*Deletions*/       
238     if (CL->matrices_list[2][0])sprintf ( M->model_comments[Dt], "Deletion %s", CL->matrices_list[2]);
239     M->model_properties[Dt][M->TYPE]=Dt;
240     M->model_properties[Dt][M->LEN_I]=1;
241     M->model_properties[Dt][M->LEN_J]=0;
242     M->model_properties[Dt][M->DELTA_I]=-1;
243     M->model_properties[Dt][M->DELTA_J]=+1;
244     
245     M->model_emission_function[Dt][M->EMISSION]      =get_turn_gep_cost;
246     M->model_emission_function[Dt][M->START_EMISSION]=get_turn_start_gep_cost;
247     M->model_emission_function[Dt][M->TERM_EMISSION] =get_turn_term_gep_cost;
248     /*Insertion*/
249     if (CL->matrices_list[2][0])sprintf ( M->model_comments[It], "Insertion %s", CL->matrices_list[2]);
250     M->model_properties[It][M->TYPE]=It;
251     M->model_properties[It][M->LEN_I]=0;
252     M->model_properties[It][M->LEN_J]=1;
253     M->model_properties[It][M->DELTA_I]=0;
254     M->model_properties[It][M->DELTA_J]=-1;
255
256     M->model_emission_function[It][M->EMISSION]      =get_turn_gep_cost;
257     M->model_emission_function[It][M->START_EMISSION]=get_turn_start_gep_cost;
258     M->model_emission_function[It][M->TERM_EMISSION] =get_turn_term_gep_cost;
259
260
261 /*Transitions*/
262
263     M->model[M->START][Sa]=ALLOWED;
264     M->model[M->START][Sb]=ALLOWED;
265     M->model[M->START][St]=ALLOWED;   
266     M->model[M->START][Db]=M->model[M->START][Ib]=(CL->TG_MODE==0)?CL->gop*SCORE_K:0;
267     M->model[M->START][Da]=M->model[M->START][Ia]=(CL->TG_MODE==0)?CL->gop*SCORE_K:0;
268     M->model[M->START][Dt]=M->model[M->START][It]=(CL->TG_MODE==0)?CL->gop*SCORE_K:0;
269     
270     
271     M->model[Sa][M->END]=ALLOWED;
272     M->model[Sb][M->END]=ALLOWED;
273     M->model[St][M->END]=ALLOWED;
274     M->model[Ia][M->END]=M->model[Da][M->END]=(CL->TG_MODE==0)?0:CL->gop*SCORE_K*(-1);
275     M->model[Ib][M->END]=M->model[Db][M->END]=(CL->TG_MODE==0)?0:CL->gop*SCORE_K*(-1);
276     M->model[It][M->END]=M->model[Dt][M->END]=(CL->TG_MODE==0)?0:CL->gop*SCORE_K*(-1);
277     
278     for ( a=0; a< M->nstate; a++)M->model[a][a]=ALLOWED;
279     
280     M->model[Sa][Ia]=M->model[Sa][Da]=CL->gop*SCORE_K;
281     M->model[Sa][Ib]=M->model[Sa][Db]=CL->gop*SCORE_K+tgop*SCORE_K;
282     M->model[Sa][It]=M->model[Sa][Dt]=CL->gop*SCORE_K+tgop*SCORE_K;
283     M->model[Sa][Sb]=M->model[Sa][St]=tgop*SCORE_K;
284
285     M->model[Sb][Ib]=M->model[Sb][Db]=CL->gop*SCORE_K;
286     M->model[Sb][Ia]=M->model[Sb][Da]=CL->gop*SCORE_K+tgop*SCORE_K;
287     M->model[Sb][It]=M->model[Sb][Dt]=CL->gop*SCORE_K+tgop*SCORE_K;
288     M->model[Sb][Sa]=M->model[Sb][St]=tgop*SCORE_K;
289
290     M->model[St][It]=M->model[St][Dt]=CL->gop*SCORE_K;
291     M->model[St][Ia]=M->model[St][Da]=CL->gop*SCORE_K+tgop*SCORE_K;
292     M->model[St][Ib]=M->model[St][Db]=CL->gop*SCORE_K+tgop*SCORE_K;
293     M->model[St][Sa]=M->model[St][Sb]=tgop*SCORE_K;
294     
295     M->model[Ia][Sa]=M->model[Da][Sa]=ALLOWED;
296     M->model[Ia][Sb]=M->model[Da][Sb]=tgop*SCORE_K;
297     M->model[Ia][St]=M->model[Da][St]=tgop*SCORE_K;
298
299     M->model[Ib][Sa]=M->model[Db][Sa]=tgop*SCORE_K;
300     M->model[Ib][Sb]=M->model[Db][Sb]=ALLOWED;
301     M->model[Ib][St]=M->model[Db][St]=tgop*SCORE_K;
302
303     M->model[It][Sa]=M->model[Dt][Sa]=tgop*SCORE_K;
304     M->model[It][Sb]=M->model[Dt][Sb]=tgop*SCORE_K;
305     M->model[It][St]=M->model[Dt][St]=ALLOWED;
306     
307
308         
309     /*Prune the model*/
310
311     for (c=0,a=0, d=0; a< M->START; a++)
312       for ( b=0; b<M->START; b++, d++)
313         {
314           if (M->model[a][b]!=UNDEFINED)
315             {
316               M->bounded_model[b][1+M->bounded_model[b][0]++]=a;
317               c++;
318             }
319         }
320     M->CL=CL;
321    
322     return M;
323   }
324
325
326
327
328
329 int get_alpha_sub_cost (Alignment *A, int**pos1, int ns1, int*list1, int col1, int**pos2, int ns2, int*list2, int col2, Constraint_list *CL)
330 {
331   static int **mat;
332   int s1, r1, s2, r2;
333   float score;
334   
335
336   if (!mat && CL->matrices_list[0][0])mat=read_matrice (CL->matrices_list[0]);
337   else if ( !CL->matrices_list[0][0])return UNDEFINED;
338   
339
340   
341   
342   s1=A->order[list1[0]][0];
343   r1=pos1[list1[0]][col1];
344   s2=A->order[list2[0]][0];
345   r2=pos1[list2[0]][col2];
346   
347   if ( r1<0 || r2<0)return 0;
348
349   score=mat[(CL->S)->seq[s1][r1-1]-'A'][(CL->S)->seq[s2][r2-1]-'A']*SCORE_K;
350   return (int)score;
351   
352 }
353 int get_beta_sub_cost (Alignment *A, int**pos1, int ns1, int*list1, int col1, int**pos2, int ns2, int*list2, int col2, Constraint_list *CL)
354 {
355   static int **mat;
356   int s1, r1, s2, r2;
357   float score;
358   
359    if (!mat && CL->matrices_list[1][0])mat=read_matrice (CL->matrices_list[1]);
360    else if ( !CL->matrices_list[1][0])return UNDEFINED;
361  
362
363   
364   s1=A->order[list1[0]][0];
365   r1=pos1[list1[0]][col1];
366   s2=A->order[list2[0]][0];
367   r2=pos1[list2[0]][col2];
368   if ( r1<0 || r2<0)return 0;
369   
370   score=mat[(CL->S)->seq[s1][r1-1]-'A'][(CL->S)->seq[s2][r2-1]-'A']*SCORE_K;
371   return (int)score;
372   
373 }
374 int get_turn_sub_cost (Alignment *A, int**pos1, int ns1, int*list1, int col1, int**pos2, int ns2, int*list2, int col2, Constraint_list *CL)
375 {
376   static int **mat;
377   int s1, r1, s2, r2;
378   float score;
379
380   
381   
382   if (!mat && CL->matrices_list[2][0])mat=read_matrice (CL->matrices_list[2]);
383   else if ( !CL->matrices_list[2][0])return UNDEFINED;
384   
385   
386   s1=A->order[list1[0]][0];
387   r1=pos1[list1[0]][col1];
388   s2=A->order[list2[0]][0];
389   r2=pos1[list2[0]][col2];
390
391   
392   if ( r1<0 || r2<0)return 0;
393   score=mat[(CL->S)->seq[s1][r1-1]-'A'][(CL->S)->seq[s2][r2-1]-'A']*SCORE_K;
394   return (int)score;
395   
396 }
397
398 int get_turn_gep_cost (Alignment *A, int**pos1, int ns1, int*list1, int col1, int**pos2, int ns2, int*list2, int col2, Constraint_list *CL)
399 {
400   return (CL->gep) *SCORE_K;
401 }
402 int get_turn_start_gep_cost (Alignment *A, int**pos1, int ns1, int*list1, int col1, int**pos2, int ns2, int*list2, int col2, Constraint_list *CL)
403 {
404   return ((CL->TG_MODE)==2)?0:get_turn_gep_cost(A,pos1, ns1, list1, col1, pos2, ns2, list2, col2, CL);
405 }
406 int get_turn_term_gep_cost (Alignment *A, int**pos1, int ns1, int*list1, int col1, int**pos2, int ns2, int*list2, int col2, Constraint_list *CL)
407 {
408   return ((CL->TG_MODE)==2)?-get_turn_gep_cost(A,pos1, ns1, list1, col1, pos2, ns2, list2, col2, CL):0;
409 }
410
411
412 int get_alpha_gep_cost (Alignment *A, int**pos1, int ns1, int*list1, int col1, int**pos2, int ns2, int*list2, int col2, Constraint_list *CL)
413 {
414   return (CL->gep)*SCORE_K;
415 }
416 int get_alpha_start_gep_cost (Alignment *A, int**pos1, int ns1, int*list1, int col1, int**pos2, int ns2, int*list2, int col2, Constraint_list *CL)
417 {
418   return ((CL->TG_MODE)==2)?0:get_alpha_gep_cost(A,pos1, ns1, list1, col1, pos2, ns2, list2, col2, CL);
419 }
420 int get_alpha_term_gep_cost (Alignment *A, int**pos1, int ns1, int*list1, int col1, int**pos2, int ns2, int*list2, int col2, Constraint_list *CL)
421 {
422   return ((CL->TG_MODE)==2)?-get_alpha_gep_cost(A,pos1, ns1, list1, col1, pos2, ns2, list2, col2, CL):0;
423 }
424
425
426 int get_beta_gep_cost (Alignment *A, int**pos1, int ns1, int*list1, int col1, int**pos2, int ns2, int*list2, int col2, Constraint_list *CL)
427 {
428   return (CL->gep)*SCORE_K;
429 }
430 int get_beta_start_gep_cost (Alignment *A, int**pos1, int ns1, int*list1, int col1, int**pos2, int ns2, int*list2, int col2, Constraint_list *CL)
431 {
432   return ((CL->TG_MODE)==2)?0:get_beta_gep_cost(A,pos1, ns1, list1, col1, pos2, ns2, list2, col2, CL);
433 }
434 int get_beta_term_gep_cost (Alignment *A, int**pos1, int ns1, int*list1, int col1, int**pos2, int ns2, int*list2, int col2, Constraint_list *CL)
435 {
436   return ((CL->TG_MODE)==2)?-get_beta_gep_cost(A,pos1, ns1, list1, col1, pos2, ns2, list2, col2, CL):0;
437 }
438
439
440 int get_ssec_no_cost (Alignment *A, int**pos1, int ns1, int*list1, int col1, int**pos2, int ns2, int*list2, int col2, Constraint_list *CL)
441 {
442   return 0;
443 }
444
445
446
447
448 /*********************************COPYRIGHT NOTICE**********************************/
449 /*© Centro de Regulacio Genomica */
450 /*and */
451 /*Cedric Notredame */
452 /*Tue Oct 27 10:12:26 WEST 2009. */
453 /*All rights reserved.*/
454 /*This file is part of T-COFFEE.*/
455 /**/
456 /*    T-COFFEE is free software; you can redistribute it and/or modify*/
457 /*    it under the terms of the GNU General Public License as published by*/
458 /*    the Free Software Foundation; either version 2 of the License, or*/
459 /*    (at your option) any later version.*/
460 /**/
461 /*    T-COFFEE is distributed in the hope that it will be useful,*/
462 /*    but WITHOUT ANY WARRANTY; without even the implied warranty of*/
463 /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the*/
464 /*    GNU General Public License for more details.*/
465 /**/
466 /*    You should have received a copy of the GNU General Public License*/
467 /*    along with Foobar; if not, write to the Free Software*/
468 /*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA*/
469 /*...............................................                                                                                      |*/
470 /*  If you need some more information*/
471 /*  cedric.notredame@europe.com*/
472 /*...............................................                                                                                                                                     |*/
473 /**/
474 /**/
475 /*      */
476 /*********************************COPYRIGHT NOTICE**********************************/