12 #include "io_lib_header.h"
13 #include "util_lib_header.h"
14 #include "define_header.h"
15 #include "dp_lib_header.h"
19 //Free static allocated memory
20 free_proba_pair_wise();
23 /************************************************************************/
28 /************************************************************************/
29 Constraint_list * declare_constraint_list_simple ( Sequence *S)
31 return declare_constraint_list (S, NULL, NULL, 0, NULL, NULL);
34 Constraint_list * declare_constraint_list ( Sequence *S, char *name, int *L, int ne,FILE *fp, int **M)
38 CL=vcalloc (1, sizeof ( Constraint_list));
46 sprintf ( CL->list_name, "%s", name);
53 CL->entry_len=LIST_N_FIELDS;
54 CL->el_size=sizeof (CLIST_TYPE);
55 CL->matrices_list=declare_char(20,20);
59 if ( S)CL->seq_for_quadruplet=vcalloc ( S->nseq, sizeof (int));
60 CL->Prot_Blast=vcalloc ( 1, sizeof ( Blast_param));
61 CL->DNA_Blast=vcalloc ( 1, sizeof ( Blast_param));
62 CL->Pdb_Blast=vcalloc ( 1, sizeof ( Blast_param));
63 CL->TC=vcalloc (1, sizeof (TC_param));
68 Constraint_list *free_constraint_list4lib_computation (Constraint_list *CL)
78 Constraint_list *duplicate_constraint_list4lib_computation (Constraint_list *CL)
81 SCL=vcalloc (1, sizeof ( Constraint_list));
84 SCL->RunName=CL->RunName;
89 SCL->residue_indexed=0;
90 SCL->residue_index=NULL;
94 Constraint_list *duplicate_constraint_list_soft (Constraint_list *CL)
96 /*Duplication that does not copy the long lists*/
97 return copy_constraint_list (CL,SOFT_COPY);
99 Constraint_list *duplicate_constraint_list (Constraint_list *CL)
101 /*Duplicate everything in the constraint_list*/
102 return copy_constraint_list (CL,HARD_COPY);
104 Constraint_list *copy_constraint_list (Constraint_list *CL, int mode)
106 Constraint_list *NCL;
115 S=(mode==HARD_COPY)?duplicate_sequence (CL->S):CL->S;
118 NCL=declare_constraint_list (S, NULL, NULL,0, NULL, NULL);
121 NCL=vcalloc ( 1, sizeof (Constraint_list));
127 if (mode==SOFT_COPY)NCL->pCL=CL;
129 NCL->STRUC_LIST=(mode==HARD_COPY)?duplicate_sequence (CL->STRUC_LIST):CL->STRUC_LIST;
130 sprintf ( NCL->align_pdb_param_file, "%s", CL->align_pdb_param_file);
131 sprintf ( NCL->align_pdb_hasch_mode, "%s", CL->align_pdb_hasch_mode);
134 NCL->W=(mode==HARD_COPY)?duplicate_weights (CL->W):CL->W;
135 NCL->DM=(mode==HARD_COPY)?duplicate_distance_matrix (CL->DM):CL->DM;
136 NCL->ktupDM=(mode==HARD_COPY)?duplicate_distance_matrix (CL->ktupDM):CL->ktupDM;
137 NCL->RunName=CL->RunName;
139 if ( mode==HARD_COPY && CL->translation){NCL->translation=vcalloc ((CL->S)->nseq, sizeof (int)); for ( a=0; a< (CL->S)->nseq; a++)NCL->translation[a]=CL->translation[a];}
140 else{NCL->translation=CL->translation;}
142 NCL->out_aln_format=(mode==HARD_COPY)?duplicate_char (CL->out_aln_format, -1, -1):CL->out_aln_format;
143 NCL->n_out_aln_format=CL->n_out_aln_format;
145 /*Packing Sequence: To use with domain analysis*/
146 NCL->packed_seq_lu=(mode==HARD_COPY)?duplicate_int (CL->packed_seq_lu, -1, -1):CL->packed_seq_lu;
148 if (CL->fp)(mode==HARD_COPY)?NCL->fp=vtmpfile():CL->fp;
150 if ( mode==HARD_COPY)
152 for ( a=0; a< CL->ne; a++)
153 for ( b=0; b< CL->entry_len; b++) vwrite_clist(NCL, a, b, vread_clist(CL, a, b));
158 if ( mode==HARD_COPY)
160 NCL->M=copy_int ( CL->M,NCL->M,-1, -1);
168 sprintf ( NCL->list_name, "%s", CL->list_name);
169 NCL->entry_len=CL->entry_len;
170 NCL->el_size=CL->el_size;
172 /*Normalisation information*/
173 NCL->filter_lib=CL->filter_lib;
174 NCL->normalise=CL->normalise;
175 NCL->overweight=CL->overweight;
176 NCL->max_ext_value=CL->max_ext_value;
177 NCL->max_value=CL->max_value;
179 /*Pair wise alignment method*/
180 NCL->pw_parameters_set=CL->pw_parameters_set;
182 NCL->f_gop=CL->f_gop;
184 NCL->f_gep=CL->f_gep;
186 NCL->nomatch=CL->nomatch;
188 NCL->TG_MODE=CL->TG_MODE;
189 NCL->F_TG_MODE=CL->F_TG_MODE;
191 sprintf ( NCL->dp_mode, "%s", CL->dp_mode);
192 NCL->maximise=CL->maximise;
193 sprintf ( NCL->matrix_for_aa_group, "%s", CL->matrix_for_aa_group);
194 sprintf ( NCL->method_matrix, "%s", CL->method_matrix);
196 NCL->diagonal_threshold=CL->diagonal_threshold;
199 NCL->use_fragments=CL->use_fragments;
200 NCL->fasta_step=CL->fasta_step;
201 NCL->lalign_n_top=CL->lalign_n_top;
202 NCL->sw_min_dist=CL->sw_min_dist;
203 NCL->matrices_list=(mode==HARD_COPY)?duplicate_char (CL->matrices_list, -1, -1):CL->matrices_list;
204 NCL->n_matrices=CL->n_matrices;
206 sprintf (NCL->distance_matrix_mode, "%s", CL->distance_matrix_mode);
207 sprintf (NCL->distance_matrix_sim_mode, "%s", CL->distance_matrix_sim_mode);
209 sprintf (NCL->tree_mode, "%s", CL->tree_mode);
210 NCL->tree_aln=(mode==HARD_COPY)?copy_aln (CL->tree_aln, NULL):CL->tree_aln;
211 /*Functions used for dynamic programming and Evaluation*/
212 NCL->no_overaln=CL->no_overaln;
213 NCL->profile_mode=CL->profile_mode;
214 sprintf ( NCL->profile_comparison, "%s",CL->profile_comparison);
215 NCL->get_dp_cost=CL->get_dp_cost;
216 NCL->evaluate_residue_pair=CL->evaluate_residue_pair;
217 NCL->pair_wise=CL->pair_wise;
219 NCL->weight_field=CL->weight_field;
220 NCL->max_n_pair=CL->max_n_pair;
222 /*threading parameters*/
223 NCL->Prot_Blast=(mode==HARD_COPY)?duplicate_blast_param ( CL->Prot_Blast):CL->Prot_Blast;
224 NCL->DNA_Blast =(mode==HARD_COPY)?duplicate_blast_param ( CL->DNA_Blast):CL->DNA_Blast;
225 NCL->Pdb_Blast =(mode==HARD_COPY)?duplicate_blast_param ( CL->Pdb_Blast):CL->Pdb_Blast;
226 NCL->TC =(mode==HARD_COPY)?duplicate_TC_param ( CL->TC):CL->TC;
229 NCL->split=CL->split;
230 NCL->split_nseq_thres= CL->split_nseq_thres;
231 NCL->split_score_thres= CL->split_score_thres;
232 /*Structural status*/
233 NCL->check_pdb_status=CL->check_pdb_status;
235 sprintf ( NCL->method_log, "%s",CL->method_log);
236 sprintf ( NCL->evaluate_mode, "%s",CL->evaluate_mode);
238 /*Parameters for domain extraction*/
239 NCL->moca=(mode==HARD_COPY)?duplicate_moca ( CL->moca):CL->moca;
243 /*Functions for hiding forbiden pairs of residues*/
244 /* Copy only for soft_copy*/
247 NCL->forbiden_pair_list=CL->forbiden_pair_list;
249 /*extention properties:*/
250 NCL->nseq_for_quadruplet=CL->nseq_for_quadruplet;
251 if (mode==HARD_COPY && CL->seq_for_quadruplet)
252 {NCL->seq_for_quadruplet=vcalloc ( S->nseq, sizeof(int));
253 for ( a=0; a< S->nseq; a++)
254 NCL->seq_for_quadruplet[a]=CL->seq_for_quadruplet[a];
256 else if (mode==SOFT_COPY)
258 NCL->seq_for_quadruplet=CL->seq_for_quadruplet;
261 /*extention properties: Do only a soft copy*/
262 /* Not To be copied yet */
263 if ( mode==SOFT_COPY)
265 NCL->extend_jit=CL->extend_jit;
266 NCL->extend_threshold=CL->extend_threshold;
267 sprintf ( NCL->extend_clean_mode, "%s", CL->extend_clean_mode);
268 sprintf ( NCL->extend_compact_mode, "%s", CL->extend_compact_mode);
271 /*Lookup table parameteres*/
272 NCL->chunk= CL->chunk;
273 /* Do NOT copy NCL->seq_indexed, NCL->start_index, NCL->max_L_len, NCL->chunk*/
275 if ( mode==SOFT_COPY)
277 NCL->seq_indexed=CL->seq_indexed;
278 NCL->start_index=CL->start_index;
279 NCL->end_index=CL->start_index;
280 NCL->max_L_len=CL->max_L_len;
283 /*PDB STRUCTURE ALIGNMENTS*/
284 /* Do only a soft copy */
285 if ( mode==SOFT_COPY)
291 NCL->local_stderr=CL->local_stderr;
292 sprintf (NCL->multi_thread, "%s", CL->multi_thread);
296 Constraint_list *free_constraint_list_full (Constraint_list *CL)
298 free_sequence (free_constraint_list (CL), -1);
301 Sequence *free_constraint_list (Constraint_list *CL)
305 Constraint_list *pCL;
308 /*Prepare the selective freeing of the CL data structure:
309 If the CL has been obtained from copy, every pointer that is identical to the parent CL (CL->pCL)
314 if ( !CL)return NULL;
317 if ( CL->copy_mode==SOFT_COPY && !CL->pCL)
318 {vfree(CL); return S;}
319 else if ( CL->copy_mode==SOFT_COPY)
325 if ( CL->M ==pCL->M )CL->M=NULL;
327 if (CL->start_index ==pCL->start_index )CL->start_index=NULL;
328 if (CL->end_index ==pCL->end_index )CL->end_index=NULL;
329 if (CL->residue_index ==pCL->residue_index )CL->residue_index=NULL;
331 if ( CL->fp ==pCL->fp )CL->fp=NULL;
332 if ( CL->matrices_list ==pCL->matrices_list )CL->matrices_list=NULL;
335 if ( CL->STRUC_LIST ==pCL->STRUC_LIST )CL->STRUC_LIST=NULL;
336 if ( CL->W ==pCL->W )CL->W=NULL;
337 if ( CL->DM ==pCL->DM )CL->DM=NULL;
338 if ( CL->ktupDM ==pCL->ktupDM )CL->ktupDM=NULL;
341 if ( CL->translation ==pCL->translation )CL->translation=NULL;
342 if ( CL->moca ==pCL->moca )CL->moca=NULL;
343 if ( CL->Prot_Blast ==pCL->Prot_Blast )CL->Prot_Blast=NULL;
344 if ( CL->DNA_Blast ==pCL->DNA_Blast )CL->DNA_Blast=NULL;
345 if ( CL->Pdb_Blast ==pCL->Pdb_Blast )CL->Pdb_Blast=NULL;
346 if ( CL->seq_for_quadruplet ==pCL->seq_for_quadruplet )CL->seq_for_quadruplet=NULL;
347 if ( CL->TC ==pCL->TC )CL->TC=NULL;
352 /*End of selective freeing of the CL data structure*/
356 if ( CL->L)vfree (CL->L);
357 if ( CL->M)free_int (CL->M, -1);
358 if ( CL->fp)vfclose (CL->fp);
359 if ( CL->matrices_list)free_char(CL->matrices_list,-1);
362 if ( CL->start_index)free_int ( CL->start_index,-1);
363 if ( CL->end_index)free_int ( CL->end_index,-1);
365 if ( CL->residue_index)
367 for ( a=0; a< (CL->S)->nseq; a++)
369 for ( b=0; b<=(CL->S)->len[a]; b++)
370 vfree(CL->residue_index[a][b]);
371 vfree (CL->residue_index[a]);
373 vfree(CL->residue_index);
377 if ( CL->STRUC_LIST)free_sequence ( CL->STRUC_LIST, (CL->STRUC_LIST)->nseq);
378 if ( CL->W)free_weights (CL->W);
380 CL->DM=free_distance_matrix (CL->DM);
381 CL->ktupDM=free_distance_matrix (CL->ktupDM);
383 if ( CL->translation)vfree(CL->translation);
384 if ( CL->moca)free_moca (CL->moca);
385 if ( CL->Prot_Blast)free_blast_param ( CL->Prot_Blast);
386 if ( CL->DNA_Blast) free_blast_param ( CL->DNA_Blast);
387 if ( CL->Pdb_Blast) free_blast_param ( CL->Pdb_Blast);
388 if ( CL->TC) free_TC_param ( CL->TC);
390 if (CL->seq_for_quadruplet)vfree (CL->seq_for_quadruplet);
396 Distance_matrix * free_distance_matrix ( Distance_matrix *DM)
399 free_int ( DM->similarity_matrix,-1);
400 free_int ( DM->distance_matrix,-1);
401 free_int ( DM->score_similarity_matrix,-1);
405 Distance_matrix * duplicate_distance_matrix ( Distance_matrix *DMin)
408 if (!DMin) return NULL;
410 DM=vcalloc ( 1, sizeof (Distance_matrix));
411 DM->similarity_matrix=duplicate_int ( DMin->similarity_matrix, -1, -1);
412 DM->distance_matrix=duplicate_int ( DMin->distance_matrix, -1, -1);
413 DM->score_similarity_matrix=duplicate_int ( DMin->score_similarity_matrix, -1, -1);
417 /************************************************************************/
422 /************************************************************************/
423 Moca * duplicate_moca ( Moca *m)
427 if ( m==NULL)return m;
429 nm=vcalloc ( 1, sizeof (Moca));
431 nm->moca_scale=m->moca_scale;
432 nm->evaluate_domain=m->evaluate_domain;
433 nm->moca_threshold=m->moca_threshold;
434 nm->cache_cl_with_domain=m->cache_cl_with_domain;
435 if ( m->forbiden_residues)nm->forbiden_residues=copy_int (m->forbiden_residues,nm->forbiden_residues, -1, -1);
436 nm->make_nol_aln=m->make_nol_aln;
441 Moca * free_moca ( Moca *m)
443 if ( m->forbiden_residues)free_int ( m->forbiden_residues, -1);
447 /************************************************************************/
449 /* TC_param Functions */
452 /************************************************************************/
453 TC_param * duplicate_TC_param ( TC_param*B)
456 N=vcalloc (1, sizeof ( TC_param));
457 memcpy(B, N, sizeof(TC_param));
460 TC_param * free_TC_param ( TC_param*B)
465 /************************************************************************/
467 /* Blast_param Functions */
470 /************************************************************************/
471 Blast_param * duplicate_blast_param ( Blast_param*B)
474 N=vcalloc (1, sizeof ( Blast_param));
475 sprintf ( N->blast_server, "%s", B->blast_server);
476 sprintf ( N->db, "%s", B->db);
479 N->min_cov=B->min_cov;
482 Blast_param * free_blast_param ( Blast_param*B)
488 /************************************************************************/
493 /************************************************************************/
494 Structure* declare_structure ( int n, char **array)
499 S=vcalloc (1, sizeof (Structure));
503 S->struc=vcalloc ( n, sizeof (int**));
504 S->len=vcalloc ( n, sizeof (int));
505 for ( a=0; a< n; a++)
507 S->len[a]=strlen(array[a]);
508 S->struc[a]=declare_int ( strlen ( array[a])+2, 1);
513 Structure *extend_structure ( Structure *S)
518 for ( a=0; a< S->nseq; a++)
520 for ( b=0; b< S->len[a]; b++)
521 S->struc[a][b]=vrealloc ( S->struc[a][b],( S->n_fields+1)*sizeof (int));
527 Sequence * declare_sequence ( int min, int max, int nseq)
533 LS=vcalloc (1, sizeof ( Sequence));
535 LS->seq_comment=declare_char ( nseq,COMMENT_SIZE);
536 LS->aln_comment=declare_char ( nseq,COMMENT_SIZE);
538 LS->file=declare_char( nseq,STRING+1);
539 LS->seq=declare_char ( nseq, max+1);
540 LS->name=declare_char( nseq,MAXNAMES+1);
542 LS->len=vcalloc ( nseq, sizeof (int));
547 LS->type=vcalloc(30, sizeof (char));
548 LS->T=declare_arrayN(2, sizeof (Template), nseq, 1);
549 LS->dc=declare_int (nseq, 2);
552 Sequence * realloc_sequence (Sequence *OUT, int new_nseq, int max_len)
556 if ( new_nseq<OUT->max_nseq)return OUT;
558 OUT->min_len =MIN(OUT->min_len,max_len);
559 OUT->max_len =MAX(OUT->max_len,max_len);
560 OUT->seq_comment =new_realloc_char ( OUT->seq_comment, new_nseq,COMMENT_SIZE);
561 OUT->aln_comment =new_realloc_char ( OUT->aln_comment, new_nseq,COMMENT_SIZE);
563 OUT->seq =new_realloc_char ( OUT->seq, new_nseq,OUT->max_len+1);
564 OUT->name =new_realloc_char ( OUT->name, new_nseq,MAXNAMES+1);
567 OUT->file =new_realloc_char ( OUT->file, new_nseq,STRING+1);
568 OUT->len =vrealloc ( OUT->len, (new_nseq+1)*sizeof (int));
570 OUT->T=(Template**)realloc_arrayN (2, (void **)OUT->T,sizeof (Template), new_nseq, 1);
571 OUT->dc=(int **)realloc_arrayN (2, (void **)OUT->dc,sizeof (int), new_nseq, 2);
572 OUT->max_nseq=new_nseq;
576 Sequence * duplicate_sequence (Sequence *S )
582 if (S==NULL)return S;
583 LS=declare_sequence (S->min_len, S->max_len, S->nseq);
584 for (b=0, a=0; a<S->nseq; a++)
586 if (S->seq && S->seq[a])
589 sprintf ( LS->file[b], "%s", S->file[a]);
590 if ( S->seq_comment && S->seq_comment[a])sprintf ( LS->seq_comment[b], "%s", S->seq_comment[a]);
591 if ( S->aln_comment && S->aln_comment[a])sprintf ( LS->aln_comment[b], "%s", S->aln_comment[a]);
592 if ( S->seq && S->seq[a])sprintf ( LS->seq[b], "%s", S->seq[a]);
593 if ( S->name&& S->name[a])sprintf ( LS->name[b], "%s", S->name[a]);
594 LS->dc[b][0]=S->dc[a][0];
595 LS->dc[b][1]=S->dc[a][1];
596 LS->len[b]=S->len[a];
597 LS->T[b][0]=S->T[a][0];
603 LS->max_len=S->max_len;
604 LS->min_len=S->min_len;
607 if ( S->W)LS->W=duplicate_weights (S->W);
608 sprintf ( LS->type, "%s", S->type);
609 sprintf ( LS->template_file, "%s", S->template_file);
610 LS->max_nseq=S->nseq;
615 void free_sequence ( Sequence *LS, int nseq)
622 free_char ( LS->file, -1);
623 free_char ( LS->seq_comment, -1);
624 free_char ( LS->aln_comment, -1);
625 free_char ( LS->seq, -1);
626 free_char ( LS->name,-1);
627 free_int (LS->dc, -1);
628 free_arrayN((void*)LS->T, 2);
631 free_weights (LS->W);
635 /************************************************************************/
637 /* Weights Functions */
640 /************************************************************************/
641 Weights* declare_weights ( int nseq)
645 W=vcalloc ( 1, sizeof ( Weights));
646 W->comments=vcalloc ( 1000, sizeof (char));
648 W->mode=vcalloc (FILENAMELEN, sizeof (char));
649 W->seq_name= declare_char ( W->nseq*2, 200);
650 W->PW_SD=declare_float ( W->nseq, W->nseq);
651 W->PW_ID=declare_float ( W->nseq, W->nseq);
652 W->SEQ_W=vcalloc ( W->nseq, sizeof ( float));
655 Weights* duplicate_weights (Weights *W)
660 NW=declare_weights (W->nseq);
661 sprintf ( NW->comments, "%s", W->comments);
662 sprintf ( NW->mode, "%s", W->mode);
663 for (a=0, c=0; a< W->nseq; a++)
667 sprintf ( NW->seq_name[c], "%s", W->seq_name[a]);
668 NW->SEQ_W[c]=W->SEQ_W[a];
669 for(b=0; b< W->nseq; b++)
671 NW->PW_SD[c][b]=W->PW_SD[a][b];
672 NW->PW_ID[c][b]=W->PW_ID[a][b];
679 Weights* free_weights ( Weights* W)
689 free_char(W->seq_name, -1);
690 free_float(W->PW_SD,-1);
691 free_float(W->PW_ID, -1);
698 Alignment* copy_aln ( Alignment *A, Alignment *B)
706 if ( A==NULL){free_aln(B); return NULL;}
710 B=realloc_alignment2 (B, A->nseq, A->len_aln);
711 else if ( A->S && A->nseq>(A->S)->nseq)
713 B=declare_aln2(A->nseq, MAX((A->S)->max_len+1, A->len_aln+1));
717 B=declare_aln ((A->S));
722 B->max_len=A->max_len;
723 B->min_len=A->min_len;
724 B->declared_len=A->declared_len;
725 B->max_n_seq=A->max_n_seq;
728 B->len_aln=A->len_aln;
731 /*sequence Information*/
732 if ( A->generic_comment)
734 vfree(B->generic_comment);
735 B->generic_comment=vcalloc (strlen(A->generic_comment)+1, sizeof (char));
736 sprintf ( B->generic_comment, "%s", A->generic_comment);
738 if ( (A->S)==NULL){vfree (B->len); B->len=vcalloc ( A->max_n_seq, sizeof (int));}
739 ga_memcpy_int ( A->len, B->len, B->nseq);
741 B->seq_comment=copy_char ( A->seq_comment, B->seq_comment, -1,-1);
742 B->aln_comment=copy_char ( A->aln_comment, B->aln_comment, -1,-1);
744 B->name=copy_char ( A->name, B->name, -1,-1);
746 B->file=copy_char ( A->file, B->file, -1,-1);
747 B->tree_order=copy_char ( A->tree_order, B->tree_order, -1,-1);
748 B->expanded_order=A->expanded_order;
749 free_char ( B->seq_al, -1);
750 B->seq_al=declare_char(B->max_n_seq, B->declared_len);
751 for ( a=0; a< A->max_n_seq; a++)
753 for ( b=0; b< A->declared_len; b++)
754 B->seq_al[a][b]=A->seq_al[a][b];
759 B->order=copy_int ( A->order, B->order, -1, -1);
763 B->seq_cache=copy_int ( A->seq_cache, B->seq_cache,-1,-1);
768 B->cdna_cache=copy_int ( A->cdna_cache, B->cdna_cache,-1,-1);
771 B->P=copy_profile (A->P);
773 B->Dp_result=A->Dp_result;
777 if ( (A->S)==NULL){vfree (B->score_seq); B->score_seq=vcalloc ( A->max_n_seq, sizeof (int));}
778 ga_memcpy_int( A->score_seq,B->score_seq,B->nseq);
781 B->score_aln=A->score_aln;
784 B->finished=A->finished;
787 B->output_res_num=A->output_res_num;
788 B->residue_case=A->residue_case;
792 B->random_tag=A->random_tag;
794 /*Make the function Recursive */
797 B->A=copy_aln (A->A, NULL);
804 Alignment* shrink_aln ( Alignment *A, int nseq, int *list)
810 for ( a=0; a< nseq; a++)
813 sprintf ( A->seq_comment[a], "%s",B->seq_comment[seq]);
814 sprintf ( A->aln_comment[a], "%s",B->aln_comment[seq]);
816 sprintf ( A->seq_al [a], "%s",B->seq_al [seq]);
817 A->order[a][0]=B->order[seq][0];
818 A->order[a][1]=B->order[seq][1];
819 A->order[a][2]=B->order[seq][2];
820 A->order[a][3]=B->order[seq][3];
821 A->order[a][4]=B->order[seq][4];
823 A->score_seq[a]=B->score_seq[seq];
824 A->len[a]=B->len[seq];
827 A->len_aln=strlen (A->seq_al[0]);
831 Alignment* extract_sub_aln2 ( Alignment *B, int ns, char **ls)
836 list=name_array2index_array(ls, ns, B->name, B->nseq);
837 A=extract_sub_aln ( B,ns, list);
841 Alignment* extract_sub_aln ( Alignment *B, int nseq, int *list)
846 A=declare_aln2(nseq, B->len_aln+1);
847 for ( n=0,a=0; a< nseq; a++)
850 if ( seq==-1)continue;
852 sprintf ( A->seq_comment[a], "%s",B->seq_comment[seq]);
853 sprintf ( A->aln_comment[a], "%s",B->aln_comment[seq]);
854 sprintf ( A->name[a], "%s",B->name[seq]);
857 for (b=0; b<=B->len_aln; b++)A->seq_al [a][b]=B->seq_al [seq][b];
858 A->order[a][0]=B->order[seq][0];
859 A->order[a][1]=B->order[seq][1];
860 A->order[a][2]=B->order[seq][2];
861 A->order[a][3]=B->order[seq][3];
862 A->order[a][4]=B->order[seq][4];
864 A->score_seq[a]=B->score_seq[seq];
865 A->len[a]=B->len[seq];
868 A->len_aln=B->len_aln;
872 Alignment *declare_aln2 ( int nseq, int len)
877 S=vcalloc ( 1, sizeof ( Sequence));
889 Alignment *declare_aln ( Sequence *S){return declare_Alignment(S);}
891 Alignment *declare_Alignment ( Sequence *S)
897 [x][0]= which is the xth seq of aln
898 [x][1]= how many deleted residues before the first one
902 LA=vcalloc (1, sizeof ( Alignment));
903 aln_stack (LA, DECLARE_ALN);
906 LA->declared_len=MAX_LEN_ALN;
907 LA->max_n_seq=MAX_N_SEQ;
911 LA->declared_len=2*S->max_len+1;
912 LA->max_n_seq=S->nseq+1;
917 LA->seq_comment=declare_char (LA->max_n_seq, COMMENT_SIZE);
918 LA->aln_comment=declare_char (LA->max_n_seq, COMMENT_SIZE);
921 LA->seq_al=declare_char ( LA->max_n_seq,LA->declared_len );
922 LA->name=declare_char (LA->max_n_seq, MAXNAMES+1);
925 LA->file=declare_char (LA->max_n_seq, STRING);
926 LA->tree_order=declare_char (LA->max_n_seq, STRING);
927 LA->order= declare_int (LA->max_n_seq , 5);
928 //order[a][0]: sequence index in S
929 //order[a][1]: offset of the sequence
930 //order[a][2]: used by sw_gotoh_pair_wise
931 //order[a][3]: used by sw_gotoh_pair_wise
932 //order[a][4]: weight, -1
933 LA->score_seq= vcalloc (LA->max_n_seq, sizeof (int));
935 for ( a=0; a< LA->max_n_seq; a++)LA->order[a][0]=a;
939 LA->len=vcalloc (LA->max_n_seq, sizeof (int));
941 if (S && S->name)for ( a=0; a<S->nseq; a++)
943 sprintf ( LA->name[a], "%s", S->name[a]);
950 Alignment * realloc_aln ( Alignment *A, int new_len){return realloc_alignment(A, new_len);}
951 Alignment * realloc_alignment ( Alignment *A, int new_len)
953 if (A==NULL)A=declare_Alignment (NULL);
955 return realloc_alignment2( A, A->max_n_seq,new_len);
958 Alignment * realloc_aln2 ( Alignment *A, int n_nseq, int n_len){return realloc_alignment2(A, n_nseq, n_len);}
962 Alignment * realloc_alignment2 ( Alignment *A, int n_nseq, int n_len)
966 int delta_len, delta_nseq;
968 if ( A==NULL) A=declare_Alignment(NULL);
976 n_len=MAX(len, n_len);
977 n_nseq=MAX(nseq,n_nseq);
978 delta_nseq=MAX(0,n_nseq-nseq);
979 delta_len =MAX(0,n_len-len);
981 if ( delta_nseq<=0 && delta_len<=0)return A;
986 A->len =vrealloc( A->len , sizeof (int)*n_nseq);
987 for (a=nseq; a< n_nseq; a++)A->len[a]=0;
989 A->declared_len =n_len;
990 A->max_n_seq =n_nseq;
993 A->seq_comment=new_realloc_char ( A->seq_comment, n_nseq, -1);
994 A->aln_comment=new_realloc_char ( A->aln_comment, n_nseq, -1);
996 A->name =new_realloc_char ( A->name, n_nseq, -1);
999 A->file =new_realloc_char ( A->file, n_nseq, -1);
1001 A->tree_order =new_realloc_char ( A->tree_order, n_nseq, -1);
1002 A->seq_al =new_realloc_char ( A->seq_al, n_nseq, n_len);
1003 A->order =new_realloc_int ( A->order, n_nseq, -1);
1005 if ( A->seq_cache) A->seq_cache=new_realloc_int ( A->seq_cache, n_nseq,n_len);
1006 if ( A->cdna_cache)A->cdna_cache=new_realloc_int ( A->cdna_cache, n_nseq,n_len);
1009 A->score_seq =vrealloc( A->score_seq, sizeof (int)*(n_nseq));
1010 for ( a=nseq; a< n_nseq; a++)A->score_seq[a]=0;
1019 long aln_stack (Alignment *A, int mode)
1023 static int max_size;
1026 if (A==NULL) return 0;
1027 else if ( mode==DECLARE_ALN)
1029 if ( size==max_size)
1032 list=vrealloc (list, max_size*sizeof (long));
1034 list[size++]=(long)A;
1037 else if (mode==FREE_ALN)
1040 for (a=0; a<size; a++)
1042 if (list[a]==(long)A)
1044 for (b=a+1; b<size;b++)
1055 else if ( mode==EXTRACT_ALN)
1057 return list[size--];
1061 printf_exit (EXIT_FAILURE, stderr, "ERROR: Unknown mode for aln_stack");
1066 Sequence* free_aln ( Alignment *LA){ return free_Alignment(LA);}
1067 Sequence* free_Alignment ( Alignment *LA)
1069 /* Does not free the A->S field (sequences of A)*/
1072 //aln_stack checks the alignment has not already been freed
1073 if ( LA==NULL || !aln_stack(LA,FREE_ALN)){return NULL;}
1076 free_char ( LA->file, -1);
1077 free_char ( LA->seq_al, -1);
1078 free_int ( LA->seq_cache, -1);
1079 free_int ( LA->cdna_cache, -1);
1080 free_char ( LA->name,-1);
1082 free_char ( LA->tree_order,-1);
1083 vfree ( LA->generic_comment);
1084 free_char ( LA->seq_comment, -1);
1085 free_char ( LA->aln_comment, -1);
1087 free_int ( LA->order, -1);
1089 vfree ( LA->score_seq);
1092 free_profile (LA->P);
1093 if ( LA->A){free_Alignment (LA->A);LA->A=NULL;}
1100 Alignment * update_aln_random_tag ( Alignment *A)
1105 A->random_tag=++tag;
1109 Profile *copy_profile (Profile *P1)
1114 if ( !P1) return NULL;
1115 P=declare_profile ( P1->alphabet, P1->max_len);
1116 P->count=copy_int (P1->count, P->count, -1, -1);
1117 P->count2=copy_int (P1->count2, P->count2, -1, -1);
1118 P->count3=copy_int (P1->count3, P->count3, -1, -1);
1125 Profile *declare_profile(char *alphabet, int len)
1128 P=vcalloc ( 1, sizeof ( Profile));
1129 P->alp_size=strlen(alphabet);
1131 P->alphabet=vcalloc ( strlen (alphabet)+2, sizeof (char));
1132 sprintf ( P->alphabet, "%s", alphabet);
1134 P->count=declare_int( P->alp_size+2, len);
1135 P->count2=declare_int(100, len);
1136 P->count3=declare_int(100, len);
1140 Profile * free_profile ( Profile *P)
1142 if (!P) return NULL;
1145 vfree (P->alphabet);
1146 free_int ( P->count, -1);
1147 free_int ( P->count2, -1);
1154 /************************************************************************/
1159 /************************************************************************/
1165 Memcontrol *memlast;
1167 FILE* print_mem_usage (FILE *fp, char *comment)
1169 fprintf ( fp, "# %s Memory Usage: Current= %.3f Mb, Max= %.3f Mb\n", comment,(float)((float)alloc_mem/(1024*1024)),(float)((float)tot_mem/(1024*1024)) );
1172 void set_max_mem (int m)
1174 max_mem=m*1024*1024;
1177 int verify_memory (int s)
1181 tot_mem=(alloc_mem>tot_mem)?alloc_mem:tot_mem;
1183 if (max_mem && alloc_mem>max_mem)
1185 fprintf (stderr, "\n%s Requires Too Much Memory: %d Megabytes [FATAL:%s]\n", PROGRAM,(int)(alloc_mem/1024*1024),PROGRAM);
1186 fprintf (stderr, "Tip: Rerun your Job with a smaller dataset\n");
1188 myexit (EXIT_FAILURE);
1196 int my_assert ( void *p, int index)
1202 fprintf ( stderr, "\n****************************************************************\n");
1203 fprintf ( stderr, "\n DEBUG MODE [Rebuild For Better Performances] \n");
1204 fprintf ( stderr, "\n*****************************************************************\n");
1208 if ( !is_dynamic_memory(p)) return 1;
1209 else if ( read_array_size_new (p)<=index)
1211 fprintf ( stderr, "\nFaulty Allocation: Size=%d Access=%d\n", read_array_size (p,0),index);
1222 void * vmalloc ( size_t size)
1227 verify_memory (size+2*sizeof (Memcontrol));
1230 return NULL; /*crash ("\n0 bytes in vmalloc\n");*/
1233 x= malloc (size + 2*sizeof (Memcontrol));
1237 printf_exit (EXIT_FAILURE,stderr, "\nFAILED TO ALLOCATE REQUIRED MEMORY (vmalloc)\n");
1244 M[0].size_element=0;
1245 sprintf ( M[0].check, "dy");
1255 void *vcalloc (size_t nobj, size_t size)
1257 return sub_vcalloc (nobj,size, MEMSET0);
1259 void *vcalloc_nomemset ( size_t nobj, size_t size)
1261 return sub_vcalloc (nobj, size, NO_MEMSET0);
1263 void *sub_vcalloc ( size_t nobj, size_t size, int MODE)
1268 if ( nobj<=0 || size<=0)return NULL;/*crash ("\n0 bytes in vmalloc\n");*/
1269 else x=vmalloc (nobj*size);
1272 M=x;M-=2;M[0].size_element=size;M+=2;x=M;
1276 crash ( "\nFAILED TO ALLOCATE REQUIRED MEMORY (vcalloc)\n");
1283 x=memset (x,0, nobj*size);
1287 if (nobj)x=memset (x, 0, size);
1293 void *vrealloc ( void *p, size_t size)
1304 memset (x, 0, size);
1316 if ( size<=0){return NULL;vfree (p);return NULL;}
1319 verify_memory (size - i_size);
1320 x=realloc ( p, size+2*sizeof(Memcontrol));
1322 if ( x==NULL){crash ( "\nFAILED TO ALLOCATE REQUIRED MEMORY (realloc)\n");return NULL;}
1327 for ( a=i_size; a< size; a++)((char*)x)[a]=0;
1333 void vfree ( void *p)
1348 verify_memory (-(size+2*sizeof(Memcontrol)));
1351 void vfree_all (void *p)
1361 /*********************************************************************/
1366 /*********************************************************************/
1367 #define WRITE_SIZE(type,function)\
1368 void function ( int x, type *array, int os)\
1370 fprintf(stderr, "\nwrite_size is a deprecated function [Warning:%s]\n", PROGRAM);return;\
1372 WRITE_SIZE(short,write_size_short)
1373 WRITE_SIZE(char,write_size_char)
1374 WRITE_SIZE(int,write_size_int)
1375 WRITE_SIZE(float,write_size_float)
1376 WRITE_SIZE(double,write_size_double)
1378 #define READ_ARRAY_SIZE(type, function)\
1379 int function (void *array, size_t size)\
1381 return read_array_size (array, size);\
1383 READ_ARRAY_SIZE(short,read_size_short)
1384 READ_ARRAY_SIZE(char,read_size_char)
1385 READ_ARRAY_SIZE(int,read_size_int)
1386 READ_ARRAY_SIZE(float,read_size_float)
1387 READ_ARRAY_SIZE(double,read_size_double)
1390 int read_array_size_new (void *array)
1392 return read_array_size ( array, 0);
1394 int read_array_size (void *array, size_t size)
1397 if (array==NULL)return 0;
1398 p=(Memcontrol *)array;
1400 if ( p[0].size_element ==0 && size==0)
1402 fprintf ( stderr, "\nERROR in read_array_size: trying to read the size of a malloced block");
1404 else if ( size ==0) return (int)p[0].size/p[0].size_element;
1406 return (int)p[0].size/size;
1409 int is_dynamic_memory ( void *array)
1412 if (array==NULL)return 0;
1413 p=(Memcontrol *)array;
1415 if ( strm (p[0].check, "dy"))return 1;
1419 /************************************************************************/
1421 /* DECLARE 2d ARRAYS */
1424 /************************************************************************/
1426 void * free_arrayN(void *p, int n)
1432 if ( p==NULL) return NULL;
1433 else if ( n==1)vfree ((void *)p);
1437 s=read_array_size ( (void *)p, sizeof ( void *));
1438 for ( a=0; a< s; a++)free_arrayN ((void *)i[a], n-1);
1444 void * declare_arrayNnomemset (int ndim, size_t size, ...)
1451 va_start (ap, size);
1453 array=vcalloc (ndim, sizeof (int));
1454 for ( a=0; a< ndim; a++)
1456 array[a]=va_arg (ap,int);
1457 if ( array[a]<0){va_end(ap);return NULL;}
1465 p=vcalloc_nomemset (array[0], sizeof ( void*));
1466 for (a=0; a< array[0]; a++)
1468 p[a]=vcalloc_nomemset (array[1], size);
1473 p=declare_arrayN2nomemset (ndim, array, size);
1479 void *declare_arrayN2nomemset ( int ndim, int *A, size_t size)
1486 p=vcalloc_nomemset (A[0], sizeof (void*));
1487 for ( a=0; a<A[0]; a++)
1488 p[a]=declare_arrayN2(ndim-1, A+1, size);
1492 p=vcalloc_nomemset (A[0], size);
1497 void * declare_arrayN (int ndim, size_t size, ...)
1505 va_start (ap, size);
1507 array=vcalloc (ndim, sizeof (int));
1508 for ( a=0; a< ndim; a++)
1510 array[a]=va_arg (ap,int);
1511 if ( array[a]<0){va_end(ap);return NULL;}
1519 p=vcalloc_nomemset (array[0], sizeof ( void*));
1520 for (a=0; a< array[0]; a++)
1521 p[a]=vcalloc (array[1], size);
1525 p=declare_arrayN2 (ndim, array, size);
1531 void *declare_arrayN2 ( int ndim, int *A, size_t size)
1538 p=vcalloc_nomemset (A[0], sizeof (void*));
1539 for ( a=0; a<A[0]; a++)
1540 p[a]=declare_arrayN2(ndim-1, A+1, size);
1544 p=vcalloc (A[0], size);
1549 void **declare_array (int first, int second, size_t size)
1551 return (void **)declare_arrayN (2,size,first, second);
1555 #define DECLARE_ARRAY(type,wf,rf,function)\
1556 type** function (int first, int second)\
1558 return (type **)declare_arrayN (2,sizeof(type), first, second);\
1560 DECLARE_ARRAY(short,write_size_short,read_size_short,declare_short)
1561 DECLARE_ARRAY(char,write_size_char,read_size_char,declare_char)
1562 DECLARE_ARRAY(int,write_size_int,read_size_int,declare_int)
1563 DECLARE_ARRAY(float,write_size_float,read_size_float,declare_float)
1564 DECLARE_ARRAY(double,write_size_double,read_size_double,declare_double)
1566 void **declare_array_nomemset (int first, int second, size_t size)
1568 return (void **)declare_arrayNnomemset (2,size,first, second);
1570 #define DECLARE_ARRAY_NMS(type,wf,rf,function)\
1571 type** function (int first, int second)\
1573 return (type **)declare_arrayNnomemset (2,sizeof(type), first, second);\
1575 DECLARE_ARRAY_NMS(short,write_size_short,read_size_short,declare_short_nomemset)
1576 DECLARE_ARRAY_NMS(char,write_size_char,read_size_char,declare_char_nomemset)
1577 DECLARE_ARRAY_NMS(int,write_size_int,read_size_int,declare_int_nomemset)
1578 DECLARE_ARRAY_NMS(float,write_size_float,read_size_float,declare_float_nomemset)
1579 DECLARE_ARRAY_NMS(double,write_size_double,read_size_double,declare_double_nomemset)
1582 Alignment ** declare_aln_array ( int first)
1588 array=vcalloc (first, sizeof (Alignment*));
1589 for ( a=0; a< first; a++)
1590 array[a]=declare_Alignment (NULL);
1597 /************************************************************************/
1599 /* Realloc 2d ARRAYS */
1602 /************************************************************************/
1603 void ** realloc_arrayN(int ndim,void **main_array,size_t size, ...)
1610 /*dim size==-1: keep current size*/
1611 /*Dim sizes are the absolute size (not the extension*/
1612 /*If array getting shorter, memory is Not Claimed back*/
1614 array=vcalloc (ndim, sizeof (int));
1615 va_start (ap, size);
1616 for ( a=0; a< ndim; a++)
1618 array[a]=va_arg (ap,int);
1619 if ( array[a]<-1){va_end(ap);return NULL;}
1623 p=realloc_arrayN2 (ndim, main_array, array,size);
1628 void **realloc_arrayN2 ( int ndim, void ** p, int *A, size_t size)
1634 if (ndim==0)return NULL;
1638 o=read_array_size (p,sizeof (void*));
1639 if (A[0]>o)p=vrealloc (p, sizeof (void*)*A[0]);
1640 n=(A[0]==-1)?o:A[0];
1641 for ( a=0; a<n; a++)
1642 p[a]=realloc_arrayN2(ndim-1,p[a], A+1, size);
1646 o=read_array_size (p, size);
1647 if (A[0]>o)p=vrealloc (p, size*A[0]);
1654 void ** realloc_array (void **array,size_t size, int first, int second, int ext1, int ext2)
1658 if ( array==NULL)return declare_array (((first==-1)?0:first)+ext1, ((second==-1)?0:second)+ext2, size);
1659 else if ( first==-1)
1661 first=read_array_size (array, sizeof (void *));
1663 if (second==-1)second=read_array_size(array[0], size);
1668 for ( a=d1; a<first; a++)vfree (array[a]);
1669 array=vrealloc (array, sizeof (void*)*d1);
1673 for (a=0; a<d1 && a<first; a++)
1674 array[a]=vrealloc ( array[a], size*d2);
1676 for ( a=first; a< d1; a++)
1677 array[a]=vrealloc ( array[a], size*d2);
1685 #define REALLOC_ARRAY(type,wf,rf,function1,function2,function3)\
1686 type ** function1 ( type **array, int first, int second, int ext1, int ext2)\
1688 return (type **)realloc_array ((void **)array, sizeof (type),first, second, ext1, ext2);\
1691 REALLOC_ARRAY(short,write_size_short,read_size_short,realloc_short,declare_short,free_short)
1692 REALLOC_ARRAY(char,write_size_char,read_size_char,realloc_char,declare_char,free_char)
1693 REALLOC_ARRAY(int,write_size_int,read_size_int,realloc_int,declare_int,free_int)
1694 REALLOC_ARRAY(float,write_size_float,read_size_float,realloc_float,declare_float,free_float)
1695 REALLOC_ARRAY(double,write_size_double,read_size_double,realloc_double,declare_double,free_double)
1699 #define NEW_REALLOC_ARRAY(type,wf,rf,function1,function2,function3)\
1700 type ** function1 ( type **array, int ext1, int ext2)\
1706 first=rf(array,sizeof (type*));\
1707 second=rf(array[0],sizeof (type));\
1709 if ( ext1==-1)ext1=first;\
1710 if ( ext2==-1)ext2=second;\
1711 l1=MIN(ext1, first);\
1712 l2=MIN(ext2, second);\
1713 new_array=declare_arrayN(2,sizeof(type),ext1, ext2);\
1714 for ( a=0; a<l1; a++)\
1715 for ( b=0; b<l2; b++)new_array[a][b]=array[a][b];\
1716 function3(array, -1);\
1720 NEW_REALLOC_ARRAY(short,write_size_short,read_size_short,new_realloc_short,declare_short,free_short)
1721 NEW_REALLOC_ARRAY(char,write_size_char,read_size_char,new_realloc_char,declare_char,free_char)
1722 NEW_REALLOC_ARRAY(int,write_size_int,read_size_int,new_realloc_int,declare_int,free_int)
1723 NEW_REALLOC_ARRAY(float,write_size_float,read_size_float,new_realloc_float,declare_float,free_float)
1724 NEW_REALLOC_ARRAY(double,write_size_double,read_size_double,new_realloc_double,declare_double,free_double)
1725 Alignment ** realloc_aln_array ( Alignment **array, int ext1)
1733 array=declare_aln_array(ext1);
1736 first=read_array_size ( array, sizeof ( Alignment *));
1739 array=vrealloc ( array, (sizeof (Alignment*))*(first+ext1));
1740 for ( a=first; a<first+ext1; a++)array[a]=declare_Alignment (NULL);
1745 for ( a=first-1; a>=(first+ext1);a--)free_Alignment (array[a]);
1746 array=vrealloc ( array, (sizeof (Alignment*))*(first+ext1));
1751 /************************************************************************/
1753 /* free 2d ARRAYS */
1756 /************************************************************************/
1757 #define FREE_ARRAY(type,wf,rf,function) \
1758 type ** function (type **array, int first)\
1760 return free_arrayN((void*)array, 2);\
1762 FREE_ARRAY(short,write_size_short,read_size_short,free_short)
1763 FREE_ARRAY(char,write_size_char,read_size_char,free_char)
1764 FREE_ARRAY(int,write_size_int,read_size_int,free_int)
1765 FREE_ARRAY(float,write_size_float,read_size_float,free_float)
1766 FREE_ARRAY(double,write_size_double,read_size_double,free_double)
1770 Alignment ** free_aln_array (Alignment **array)
1776 if ( array==NULL)return NULL;
1777 len=read_array_size ( array, sizeof (Alignment *));
1778 for ( a=1; a< len; a++)free_Alignment(array[a]);
1783 Fname *declare_fname (int size)
1787 size+=strlen (get_home_4_tcoffee())+FILENAMELEN+1;
1789 F=vcalloc ( 1, sizeof (Fname));
1790 F->name =vcalloc ( size, sizeof (char));
1791 F->path =vcalloc ( size, sizeof (char));
1792 F->suffix=vcalloc ( size, sizeof (char));
1793 F->full=vcalloc ( size, sizeof (char));
1797 Fname *free_fname ( Fname *F)
1804 /*********************************COPYRIGHT NOTICE**********************************/
1805 /*© Centro de Regulacio Genomica */
1807 /*Cedric Notredame */
1808 /*Tue Oct 27 10:12:26 WEST 2009. */
1809 /*All rights reserved.*/
1810 /*This file is part of T-COFFEE.*/
1812 /* T-COFFEE is free software; you can redistribute it and/or modify*/
1813 /* it under the terms of the GNU General Public License as published by*/
1814 /* the Free Software Foundation; either version 2 of the License, or*/
1815 /* (at your option) any later version.*/
1817 /* T-COFFEE is distributed in the hope that it will be useful,*/
1818 /* but WITHOUT ANY WARRANTY; without even the implied warranty of*/
1819 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the*/
1820 /* GNU General Public License for more details.*/
1822 /* You should have received a copy of the GNU General Public License*/
1823 /* along with Foobar; if not, write to the Free Software*/
1824 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/
1825 /*............................................... |*/
1826 /* If you need some more information*/
1827 /* cedric.notredame@europe.com*/
1828 /*............................................... |*/
1832 /*********************************COPYRIGHT NOTICE**********************************/