7 #include "io_lib_header.h"
8 #include "util_lib_header.h"
9 #include "define_header.h"
10 #include "dp_lib_header.h"
12 int ssec_pwaln_maln (Alignment *A, int *ns, int **ls, Constraint_list *CL)
15 static Dp_Model *M=NULL;
18 int Sa,Sb,St, Da, Db, Dt, Ia, Ib, It;
26 if ( strm (CL->matrices_list[0], "analyse"))
28 for ( a=0; a< CL->n_matrices; a++)
31 rescale_two_mat(CL->matrices_list[1],CL->matrices_list[2],1000, 100, AA_ALPHABET);
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));
43 for ( a=1; a<=M->diag[0]; a++)M->diag[a]=a;
45 /*3 Prepare Sequence Presentation*/
46 R=make_fast_generic_dp_pair_wise(A, ns, ls, M);
51 A=realloc_aln2(A,A->nseq, R->len+1);
52 for (b=1; b<R->len;b++)
54 if (R->traceback[b]==Sa || R->traceback[b]==Sb ||R->traceback[b]==St )
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];
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];
63 else if ( R->traceback[b]==Da || R->traceback[b]==Db ||R->traceback[b]==Dt )
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];
68 for (s=0; s<ns[1]; s++)
69 A->seq_al[ls[1][s]][b-1]='-';
71 else if ( R->traceback[b]==Ia || R->traceback[b]==Ib ||R->traceback[b]==It )
73 for (s=0; s<ns[0]; s++)
74 A->seq_al[ls[0][s]][b-1]='-';
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];
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';
86 A->len_aln=strlen (A->seq_al[ls[0][0]]);
92 Dp_Model * initialize_sseq_model(int left_tg_mode, int right_tg_mode, Constraint_list *CL)
97 int Sa,Sb,St, Da, Db, Dt, Ia, Ib, It;
103 M=vcalloc ( 1, sizeof (Dp_Model));
106 M->START=M->nstate++;
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;
117 M->model_properties=declare_int ( M->nstate, 10);
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);
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 *)));
134 Sa=a++;Da=a++;Ia=a++;
135 Sb=a++;Db=a++;Ib=a++;
136 St=a++;Dt=a++;It=a++;
139 sprintf ( M->model_comments[M->START], "START");
140 sprintf ( M->model_comments[M->END], "END");
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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);
278 for ( a=0; a< M->nstate; a++)M->model[a][a]=ALLOWED;
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;
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;
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;
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;
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;
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;
311 for (c=0,a=0, d=0; a< M->START; a++)
312 for ( b=0; b<M->START; b++, d++)
314 if (M->model[a][b]!=UNDEFINED)
316 M->bounded_model[b][1+M->bounded_model[b][0]++]=a;
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)
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;
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];
347 if ( r1<0 || r2<0)return 0;
349 score=mat[(CL->S)->seq[s1][r1-1]-'A'][(CL->S)->seq[s2][r2-1]-'A']*SCORE_K;
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)
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;
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;
370 score=mat[(CL->S)->seq[s1][r1-1]-'A'][(CL->S)->seq[s2][r2-1]-'A']*SCORE_K;
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)
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;
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];
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;
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)
400 return (CL->gep) *SCORE_K;
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)
404 return ((CL->TG_MODE)==2)?0:get_turn_gep_cost(A,pos1, ns1, list1, col1, pos2, ns2, list2, col2, CL);
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)
408 return ((CL->TG_MODE)==2)?-get_turn_gep_cost(A,pos1, ns1, list1, col1, pos2, ns2, list2, col2, CL):0;
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)
414 return (CL->gep)*SCORE_K;
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)
418 return ((CL->TG_MODE)==2)?0:get_alpha_gep_cost(A,pos1, ns1, list1, col1, pos2, ns2, list2, col2, CL);
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)
422 return ((CL->TG_MODE)==2)?-get_alpha_gep_cost(A,pos1, ns1, list1, col1, pos2, ns2, list2, col2, CL):0;
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)
428 return (CL->gep)*SCORE_K;
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)
432 return ((CL->TG_MODE)==2)?0:get_beta_gep_cost(A,pos1, ns1, list1, col1, pos2, ns2, list2, col2, CL);
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)
436 return ((CL->TG_MODE)==2)?-get_beta_gep_cost(A,pos1, ns1, list1, col1, pos2, ns2, list2, col2, CL):0;
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)
448 /*********************************COPYRIGHT NOTICE**********************************/
449 /*© Centro de Regulacio Genomica */
451 /*Cedric Notredame */
452 /*Tue Oct 27 10:12:26 WEST 2009. */
453 /*All rights reserved.*/
454 /*This file is part of T-COFFEE.*/
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.*/
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.*/
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 /*............................................... |*/
476 /*********************************COPYRIGHT NOTICE**********************************/