7 #include "io_lib_header.h"
8 #include "util_lib_header.h"
9 #include "define_header.h"
10 #include "dp_lib_header.h"
11 int check_link (CL_node ***G, int S1, int r1, int s2, int r2);
12 void light_nodes (CL_node *A, int va, CL_node*B, int vb, CL_node*C,int vc, char *s);
13 void print_graph (CL_node *G, Sequence *S);
20 Alignment * add_constraint2aln ( Alignment *A, int s1, int r1, int s2, int r2)
22 /*Note sCL_node ***G;1 and r1 must be numbered from 0 to n-1*/
27 G=add_constraint2graph_aln (G,s1,r1, s2, r2);
28 A=graph2aln (A,G[0][0],aln2seq(A));
29 vfree_graph (G[0][0]);
32 Alignment * graph_aln (Alignment *A, Constraint_list *iCL, Sequence *S)
36 CLIST_TYPE *entry=NULL;
39 HERE ("Function graph_aln is deprecated");
44 void print_graph (CL_node *G, Sequence *S)
49 A=seq2aln (Seq, A, 1);
50 A=graph2aln (A,G, Seq);
54 Alignment* graph2aln (Alignment *A, CL_node *G, Sequence *S)
65 while (Gi){Gi=Gi->r;l++;}
67 while (Gi){Gi=Gi->c;s++;}
69 A=realloc_alignment (A, l+1);
80 if ( G->res==-1)A->seq_al[s][l]='-';
81 else if ( G->res==-2)A->seq_al[s][l]='*';
82 else if ( G->res==-3)A->seq_al[s][l]='#';
83 else if (G->res>=0)A->seq_al[s][l]=S->seq[G->seq][G->res];
96 A->len_aln=strlen (A->seq_al[0]);
102 CL_node ***aln2graph (Alignment *A)
105 static CL_node ***galn;
106 CL_node *N, *iN, *pN;
111 galn=calloc ( A->nseq, sizeof (CL_node**));
112 for ( a=0; a< A->nseq; a++)
114 galn[a]=calloc (A->len_aln, sizeof (CL_node*));
118 N=declare_cl_nodes(-1, a);
121 for ( a=0; a<A->nseq; a++)
124 for (res=A->order[a][1], b=0; b<A->len_aln; b++)
128 N->r=declare_cl_nodes(-1, a);
137 N->seq=A->order[a][0];
138 N->res=(is_gap(A->seq_al[a][b]))?-1:res++;
140 if (N->res!=-1)galn[a][res-1]=N;
149 if ( a<A->nseq-1)iN->c=declare_cl_nodes(-1, a);
156 CL_node ***add_constraint2graph_aln (CL_node ***G, int s1, int r1, int s2, int r2)
166 d=get_node_distance (S,E);
167 if (d<0){B=S;S=E;E=B;}
171 insert_gap_columns (E,d);
172 shift_segment( S,d+1,d);
179 CL_node *shift_segment ( CL_node *S, int segL, int shiftL)
185 if ( !shiftL)return S;
187 /*find segment coordinates*/
188 for (E=S, a=1; a< segL; a++)E=E->r;
192 G=swap_gap_in_graph (S, E);
193 for (a=1; a< shiftL; a++)swap_gap_in_graph (S, E);
195 while (G!=E)G=remove_graph_gap_column (G);
196 remove_graph_gap_column (E);
201 int is_graph_gap_column(CL_node *S)
207 if (S->res>=0)return 0;
212 CL_node * remove_graph_gap_column (CL_node *S)
214 CL_node *R,*L, *P, *RV;
223 if ( !is_graph_gap_column (S))return RV;
244 CL_node * swap_gap_in_graph ( CL_node*S, CL_node *E)
246 /*Moves gap AFTER End to BEFORE Start
249 straightens the links in between
251 CL_node *G, *N, *iE, *iS, *SP, *SC, *SL;
254 /*Preserve the E/S values*/
259 /*prepare the parent/child links first*/
270 if (N->p)(S->p)->c=S;
273 if (N->c)(S->c)->p=S;
283 if ( G->res>=0)fprintf ( stderr, "\nERROR: NOT a GAP");
286 if (E->r)(E->r)->l=E;
307 CL_node * declare_cl_nodes ( int len, int seq)
316 IN=calloc ( 1, sizeof (CL_node));
326 N=calloc (len, sizeof (CL_node*));
329 if ( len==0)return NULL;
331 for (a=0; a<len; a++)N[a]=calloc ( 1, sizeof (CL_node));
332 for (a=0; a<len; a++)
336 if (a!=0)(N[a])->l=N[a-1];
337 if (a!=len-1)(N[a])->r=N[a+1];
346 CL_node *insert_gap_columns (CL_node *S, int d)
348 CL_node *Gs,*Ge, *pGs, *Gi, *Si;
355 while (S->p!=NULL)S=S->p;
359 Gs=declare_cl_nodes(d, S->seq);
363 if (Ge->r)(Ge->r)->l=Ge;
389 int get_node_distance ( CL_node *S, CL_node *E)
395 /*project the two points onto one sequence*/
396 if (S->seq>E->seq){B=S;S=E;E=B;swap*=-1;}
397 while (S->seq!=E->seq)S=S->c;
399 /*Walk from E to S */
401 while ( iS->res<0 && iS->r!=NULL){iS=iS->r;}
402 if (iS->res<0 || iS->res>E->res){B=S; S=E; E=B;swap*=-1;}
420 int check_graph ( CL_node *S, char *string)
426 if ( S==NULL)S=Start;
427 fprintf ( stderr, "\n\tGRAPH Check %s #%d\n",string, ++n);
428 while ( S->p!=NULL)S=S->p;
429 while ( S->l!=NULL)S=S->l;
436 if (iS->l && (iS->l)->seq!=iS->seq){fprintf ( stderr, "\n\t\tSEq pb");myexit(EXIT_FAILURE);}
437 if (iS->free==1){fprintf ( stderr, "\n\t\tFree Node read");myexit(EXIT_FAILURE);}
440 if (lr!=-1 && iS->res-lr!=1){fprintf ( stderr, "\n\t\tERROR: lost residues");myexit (EXIT_FAILURE);}
443 if ( iS->r && (iS->r)->l!=iS){fprintf ( stderr, "\n\t\tERROR: left != right: [%d %d][%d %d]", iS->seq, iS->res, (iS->l)->seq, (iS->r)->res);myexit (EXIT_FAILURE);}
444 if ( iS->p && (iS->p)->c!=iS){fprintf ( stderr, "\n\t\tERROR: parent != child: [%d %d][%d %d]", iS->seq, iS->res, (iS->p)->seq, (iS->p)->res);myexit (EXIT_FAILURE);}
445 if ( iS->c && (iS->c)->p!=iS){fprintf ( stderr, "\n\t\tERROR: parent != child: [%d %d][%d %d]", iS->seq, iS->res, (iS->c)->seq, (iS->c)->res);myexit (EXIT_FAILURE);}
453 CL_node * vfree_graph ( CL_node *S)
457 while ( S->p!=NULL)S=S->p;
458 while ( S->l!=NULL)S=S->l;
467 if (S)vfree_cl_node (S->l);
474 CL_node *vfree_cl_node ( CL_node *N)
476 if ( N->free==1)crash("freeing free block");
483 void light_nodes (CL_node *A, int va, CL_node*B, int vb, CL_node*C,int vc, char *string )
485 int ta=0, tb=0, tc=0;
487 fprintf ( stderr, "\nCycle %d\n LIGHT NODE: %s", cycle,string);
488 if ( A){ta=A->res; A->res=va;fprintf ( stderr, "\nA: seq %d res %d", A->seq, A->res);}
489 if ( B){tb=B->res; B->res=vb;fprintf ( stderr, "\nB: seq %d res %d", B->seq, B->res);}
490 if ( C){tc=C->res; C->res=vc;fprintf ( stderr, "\nC: seq %d res %d", C->seq, C->res);}
496 int check_link (CL_node ***G, int s1, int r1, int s2, int r2)
511 /******************************COPYRIGHT NOTICE*******************************/
512 /*© Centro de Regulacio Genomica */
514 /*Cedric Notredame */
515 /*Fri Feb 18 08:27:45 CET 2011 - Revision 596. */
516 /*All rights reserved.*/
517 /*This file is part of T-COFFEE.*/
519 /* T-COFFEE is free software; you can redistribute it and/or modify*/
520 /* it under the terms of the GNU General Public License as published by*/
521 /* the Free Software Foundation; either version 2 of the License, or*/
522 /* (at your option) any later version.*/
524 /* T-COFFEE is distributed in the hope that it will be useful,*/
525 /* but WITHOUT ANY WARRANTY; without even the implied warranty of*/
526 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the*/
527 /* GNU General Public License for more details.*/
529 /* You should have received a copy of the GNU General Public License*/
530 /* along with Foobar; if not, write to the Free Software*/
531 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/
532 /*............................................... |*/
533 /* If you need some more information*/
534 /* cedric.notredame@europe.com*/
535 /*............................................... |*/
539 /******************************COPYRIGHT NOTICE*******************************/