Revert multithreading support for mafft as it does not seem to work
[jabaws.git] / binaries / src / tcoffee / t_coffee_source / util_declare.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <strings.h>
4 #include <string.h>
5 #include <limits.h>
6 #include <float.h>
7 #include <assert.h>
8 #include <math.h>
9 #include <stdarg.h>
10
11
12 #include "io_lib_header.h"
13 #include "util_lib_header.h"
14 #include "define_header.h"
15 #include "dp_lib_header.h"
16
17 void free_pair_wise()
18 {
19   //Free static allocated memory
20   free_proba_pair_wise();
21 }
22
23 /************************************************************************/
24 /*                                                                      */
25 /*            CONSTRAINT_LIST                                           */
26 /*                                                                      */
27 /*                                                                      */
28 /************************************************************************/ 
29 Constraint_list * declare_constraint_list_simple ( Sequence *S)
30 {
31   return declare_constraint_list (S, NULL, NULL, 0, NULL, NULL);
32 }
33
34 Constraint_list * declare_constraint_list ( Sequence *S, char *name, int *L, int ne,FILE *fp, int **M)
35     {
36     Constraint_list *CL;
37
38     CL=vcalloc (1, sizeof ( Constraint_list));
39   
40     
41     CL->S=S;
42     CL->M=M;
43
44     if ( name!=NULL)
45         {
46         sprintf ( CL->list_name, "%s", name);
47         
48         }
49     CL->cpu=1;
50     CL->fp=fp;
51     CL->L=L;
52     CL->ne=ne;
53     CL->entry_len=LIST_N_FIELDS;
54     CL->el_size=sizeof (CLIST_TYPE);
55     CL->matrices_list=declare_char(20,20);
56     
57     CL->chunk=500;
58     CL->weight_field=WE;
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));
64   
65     return CL;
66     }
67
68 Constraint_list *free_constraint_list4lib_computation (Constraint_list *CL)
69 {
70   if (!CL)return NULL;
71   
72   vfree (CL->L);
73   free_int (CL->M, -1);
74   
75   vfree (CL);
76   return CL;
77 }
78 Constraint_list *duplicate_constraint_list4lib_computation (Constraint_list *CL)
79 {
80   Constraint_list *SCL;
81   SCL=vcalloc (1, sizeof ( Constraint_list));
82   SCL[0]=CL[0];
83   SCL->S=CL->S;
84   SCL->RunName=CL->RunName;
85   SCL->L=NULL;
86   SCL->max_L_len=0;
87   SCL->M=NULL;
88   SCL->ne=0;
89   SCL->residue_indexed=0;
90   SCL->residue_index=NULL;
91   
92   return SCL;
93 }
94 Constraint_list *duplicate_constraint_list_soft (Constraint_list *CL)
95 {
96   /*Duplication that does not copy the long lists*/
97   return copy_constraint_list (CL,SOFT_COPY);
98 }
99 Constraint_list *duplicate_constraint_list (Constraint_list *CL)
100      {
101      /*Duplicate everything in the constraint_list*/
102      return copy_constraint_list (CL,HARD_COPY);
103      }
104 Constraint_list *copy_constraint_list (Constraint_list *CL, int mode)
105     {   
106     Constraint_list *NCL;
107     Sequence *S;
108     int a, b;
109     
110    
111     
112     /*Sequences*/
113     
114
115       S=(mode==HARD_COPY)?duplicate_sequence (CL->S):CL->S;
116  
117       if (mode==HARD_COPY)
118         NCL=declare_constraint_list (S, NULL, NULL,0, NULL, NULL);
119       else
120         {
121           NCL=vcalloc ( 1, sizeof (Constraint_list));
122           NCL[0]=CL[0];
123         }
124       
125       NCL->S=S;
126       NCL->copy_mode=mode;
127       if (mode==SOFT_COPY)NCL->pCL=CL;
128       
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);
132
133       
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;
138       
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;}
141       
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;
144       
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;
147     /*DATA*/
148       if (CL->fp)(mode==HARD_COPY)?NCL->fp=vtmpfile():CL->fp;
149       
150       if ( mode==HARD_COPY)
151         {
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));
154         }
155       else NCL->L=CL->L;
156       
157     
158      if ( mode==HARD_COPY)
159        {
160          NCL->M=copy_int ( CL->M,NCL->M,-1, -1);
161        }
162      else
163        NCL->M=CL->M;
164     
165
166     /*List Information*/   
167       NCL->ne=CL->ne;
168       sprintf ( NCL->list_name, "%s", CL->list_name);
169       NCL->entry_len=CL->entry_len;
170       NCL->el_size=CL->el_size;
171
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;
178     
179     /*Pair wise alignment method*/
180       NCL->pw_parameters_set=CL->pw_parameters_set;
181       NCL->gop=CL->gop;
182       NCL->f_gop=CL->f_gop;
183       NCL->gep=CL->gep;
184       NCL->f_gep=CL->f_gep;
185       
186       NCL->nomatch=CL->nomatch;
187      
188       NCL->TG_MODE=CL->TG_MODE;
189       NCL->F_TG_MODE=CL->F_TG_MODE;
190       
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);
195       
196       NCL->diagonal_threshold=CL->diagonal_threshold;
197       NCL->ktup=CL->ktup;
198       
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;
205       
206       sprintf (NCL->distance_matrix_mode, "%s", CL->distance_matrix_mode);
207       sprintf (NCL->distance_matrix_sim_mode, "%s", CL->distance_matrix_sim_mode);
208
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;
218
219       NCL->weight_field=CL->weight_field;
220       NCL->max_n_pair=CL->max_n_pair;
221       
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;
227       
228     /*Split parameters*/
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;
234     /*log*/
235       sprintf ( NCL->method_log, "%s",CL->method_log);
236       sprintf ( NCL->evaluate_mode, "%s",CL->evaluate_mode);
237       
238     /*Parameters for domain extraction*/
239       NCL->moca=(mode==HARD_COPY)?duplicate_moca ( CL->moca):CL->moca;
240       
241       
242
243     /*Functions for hiding forbiden pairs of residues*/
244       /* Copy only for soft_copy*/
245       if (mode==SOFT_COPY)
246         {
247           NCL->forbiden_pair_list=CL->forbiden_pair_list;
248         }
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];
255         }
256       else if (mode==SOFT_COPY)
257         {
258           NCL->seq_for_quadruplet=CL->seq_for_quadruplet;
259         }
260       
261    /*extention properties: Do only a soft copy*/
262       /* Not To be copied yet */
263       if ( mode==SOFT_COPY)
264         {
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);
269         }
270           
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*/
274       /*
275         if ( mode==SOFT_COPY)
276         {
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;
281           }
282       */
283     /*PDB STRUCTURE ALIGNMENTS*/
284       /* Do only a soft copy */
285       if ( mode==SOFT_COPY)
286         { 
287           NCL->T=CL->T;
288         }
289     /*MISC*/  
290        NCL->cpu=CL->cpu;
291        NCL->local_stderr=CL->local_stderr;
292        sprintf (NCL->multi_thread, "%s", CL->multi_thread);
293     
294     return NCL;
295     }
296 Constraint_list *free_constraint_list_full (Constraint_list *CL)
297 {
298   free_sequence (free_constraint_list (CL), -1);
299   return NULL;
300 }
301 Sequence *free_constraint_list (Constraint_list *CL)
302     {
303     Sequence *S;
304     int a, b;
305     Constraint_list *pCL;
306
307     
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)
310       will not be saved.
311     */
312    
313     
314     if ( !CL)return NULL;
315     else S=CL->S;
316     
317     if ( CL->copy_mode==SOFT_COPY && !CL->pCL)
318       {vfree(CL); return S;}
319     else if ( CL->copy_mode==SOFT_COPY)
320       {
321
322         pCL=CL->pCL;
323         CL->L=NULL;
324
325         if ( CL->M                      ==pCL->M                       )CL->M=NULL;
326         
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;
330
331         if ( CL->fp                     ==pCL->fp                      )CL->fp=NULL;
332         if ( CL->matrices_list          ==pCL->matrices_list           )CL->matrices_list=NULL;
333
334         
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;
339
340
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;
348         
349       }
350
351     
352     /*End of selective freeing of the CL data structure*/
353
354
355     
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);
360    
361
362     if ( CL->start_index)free_int ( CL->start_index,-1);
363     if ( CL->end_index)free_int ( CL->end_index,-1);
364     
365     if ( CL->residue_index)
366       {
367         for ( a=0; a< (CL->S)->nseq; a++)
368           {
369             for ( b=0; b<=(CL->S)->len[a]; b++)
370               vfree(CL->residue_index[a][b]);
371             vfree (CL->residue_index[a]);
372           }
373         vfree(CL->residue_index);
374       }
375        
376     
377     if ( CL->STRUC_LIST)free_sequence ( CL->STRUC_LIST, (CL->STRUC_LIST)->nseq);
378     if ( CL->W)free_weights (CL->W);
379     
380     CL->DM=free_distance_matrix (CL->DM);
381     CL->ktupDM=free_distance_matrix (CL->ktupDM);
382     
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);
389     
390     if (CL->seq_for_quadruplet)vfree (CL->seq_for_quadruplet);
391
392     vfree(CL);
393     return S;
394     }
395
396 Distance_matrix * free_distance_matrix ( Distance_matrix *DM)
397 {
398   if (!DM)return NULL;
399   free_int ( DM->similarity_matrix,-1);
400   free_int ( DM->distance_matrix,-1);
401   free_int ( DM->score_similarity_matrix,-1);
402   vfree (DM);
403   return NULL;
404 }
405 Distance_matrix * duplicate_distance_matrix ( Distance_matrix *DMin)
406 {
407   Distance_matrix *DM;
408   if (!DMin) return NULL;
409   
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);
414   return DM;
415 }
416   
417 /************************************************************************/
418 /*                                                                      */
419 /*            MOCA Functions                                            */
420 /*                                                                      */
421 /*                                                                      */
422 /************************************************************************/
423 Moca * duplicate_moca ( Moca *m)
424       {
425         Moca *nm;
426         
427         if ( m==NULL)return m;
428
429         nm=vcalloc ( 1, sizeof (Moca));
430
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;
437
438         
439         return nm;
440       }
441 Moca * free_moca ( Moca *m)
442       {
443         if ( m->forbiden_residues)free_int ( m->forbiden_residues, -1);
444         vfree ( m);
445         return NULL;
446       }
447 /************************************************************************/
448 /*                                                                      */
449 /*            TC_param Functions                                            */
450 /*                                                                      */
451 /*                                                                      */
452 /************************************************************************/
453 TC_param * duplicate_TC_param ( TC_param*B)
454 {
455   TC_param *N;
456   N=vcalloc (1, sizeof ( TC_param));
457   memcpy(B, N, sizeof(TC_param));
458   return N;
459   }
460 TC_param * free_TC_param ( TC_param*B)
461 {
462   vfree (B);
463   return NULL;
464 }
465 /************************************************************************/
466 /*                                                                      */
467 /*            Blast_param Functions                                            */
468 /*                                                                      */
469 /*                                                                      */
470 /************************************************************************/
471 Blast_param * duplicate_blast_param ( Blast_param*B)
472 {
473   Blast_param *N;
474   N=vcalloc (1, sizeof ( Blast_param));
475   sprintf ( N->blast_server, "%s", B->blast_server);
476   sprintf ( N->db, "%s", B->db);
477   N->min_id=B->min_id;
478   N->max_id=B->min_id;
479   N->min_cov=B->min_cov;
480   return N;
481 }
482 Blast_param * free_blast_param ( Blast_param*B)
483 {
484   vfree (B);
485   return NULL;
486 }
487
488 /************************************************************************/
489 /*                                                                      */
490 /*            PDB Functions                                             */
491 /*                                                                      */
492 /*                                                                      */
493 /************************************************************************/           
494 Structure* declare_structure ( int n, char **array)
495     {
496     Structure *S;
497     int a;
498
499     S=vcalloc (1, sizeof (Structure));
500     S->n_fields=1;
501     S->nseq=n;
502     
503     S->struc=vcalloc ( n, sizeof (int**));
504     S->len=vcalloc ( n, sizeof (int));
505     for ( a=0; a< n; a++)
506         {
507         S->len[a]=strlen(array[a]);
508         S->struc[a]=declare_int ( strlen ( array[a])+2, 1);     
509         }
510     return S;
511     }
512
513 Structure *extend_structure ( Structure *S)
514     {
515     int a, b;
516
517
518     for ( a=0; a< S->nseq; a++)
519         {
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));                   
522         }
523     S->n_fields++;
524     return S;
525     }
526
527 Sequence * declare_sequence ( int min, int max, int nseq)
528     {
529     Sequence *LS;
530     
531     
532
533     LS=vcalloc (1, sizeof ( Sequence));
534
535     LS->seq_comment=declare_char ( nseq,COMMENT_SIZE);
536     LS->aln_comment=declare_char ( nseq,COMMENT_SIZE);
537     
538     LS->file=declare_char( nseq,STRING+1);
539     LS->seq=declare_char ( nseq, max+1);
540     LS->name=declare_char( nseq,MAXNAMES+1);
541     
542     LS->len=vcalloc ( nseq, sizeof (int));
543     LS->max_len=max;
544     LS->min_len=min;
545     LS->nseq=nseq;
546     LS->max_nseq=nseq;
547     LS->type=vcalloc(30, sizeof (char));
548     LS->T=declare_arrayN(2, sizeof (Template), nseq, 1);
549     LS->dc=declare_int (nseq, 2);
550     return LS;
551     }
552 Sequence * realloc_sequence   (Sequence *OUT, int new_nseq, int max_len)
553     {
554   
555
556       if ( new_nseq<OUT->max_nseq)return OUT;
557
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);
562       
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);
565       
566       
567       OUT->file    =new_realloc_char ( OUT->file,    new_nseq,STRING+1);
568       OUT->len     =vrealloc     ( OUT->len,     (new_nseq+1)*sizeof (int));
569       
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;
573       return OUT;
574     }
575         
576 Sequence * duplicate_sequence (Sequence *S )
577     {
578     Sequence *LS;
579     int a, b;
580     
581
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++)
585         {
586           if (S->seq && S->seq[a])
587             {
588               
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];
598               b++;
599               
600             }
601         }
602  
603     LS->max_len=S->max_len;
604     LS->min_len=S->min_len;
605     LS->nseq=b;
606
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;
611     
612     return LS;
613     }
614
615 void free_sequence ( Sequence *LS, int nseq)
616         {
617
618         
619         if ( !LS) return;
620
621
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);
629         vfree (LS->type);
630         vfree (LS->len);
631         free_weights (LS->W);
632         vfree (LS);
633
634         }
635 /************************************************************************/
636 /*                                                                      */
637 /*            Weights Functions                                         */
638 /*                                                                      */
639 /*                                                                      */
640 /************************************************************************/
641 Weights* declare_weights ( int nseq)
642         {
643         Weights *W;
644         
645         W=vcalloc ( 1, sizeof ( Weights));
646         W->comments=vcalloc ( 1000, sizeof (char));
647         W->nseq=nseq;
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));
653         return W;
654         }  
655 Weights* duplicate_weights (Weights *W)
656      {
657        Weights *NW;
658        int a, b, c;
659
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++)
664          {
665            if ( W->seq_name[a])
666              {
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++)
670                  {
671                    NW->PW_SD[c][b]=W->PW_SD[a][b];
672                    NW->PW_ID[c][b]=W->PW_ID[a][b];
673                  }
674                c++;
675              }
676          }
677        return NW;
678      }
679 Weights* free_weights ( Weights* W)
680         {
681
682         if ( !W)return NULL;
683         
684         vfree(W->comments);
685         
686         
687         vfree(W->mode);
688         
689         free_char(W->seq_name, -1);     
690         free_float(W->PW_SD,-1);        
691         free_float(W->PW_ID, -1);       
692         vfree(W->SEQ_W);
693         vfree(W);
694         return NULL;
695         } 
696
697
698 Alignment* copy_aln ( Alignment *A, Alignment *B)
699         {
700           int a, b;
701           
702           /*      c[100]=10;*/
703          
704
705           
706           if ( A==NULL){free_aln(B); return NULL;}          
707           
708           
709           if (B)
710             B=realloc_alignment2 (B, A->nseq, A->len_aln);
711           else if ( A->S && A->nseq>(A->S)->nseq)
712             {
713               B=declare_aln2(A->nseq, MAX((A->S)->max_len+1, A->len_aln+1));
714               B->S=A->S;
715             }
716           else 
717             B=declare_aln ((A->S));
718
719           
720             
721 /*SIZES*/    
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;
726
727             B->nseq=A->nseq;    
728             B->len_aln=A->len_aln;
729            
730            
731 /*sequence Information*/        
732             if ( A->generic_comment)
733               {
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);
737               }
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);
740             
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);
743             
744             B->name=copy_char ( A->name,     B->name,     -1,-1);
745             
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++)
752               {
753                 for ( b=0; b< A->declared_len; b++)
754                   B->seq_al[a][b]=A->seq_al[a][b];
755               }
756             
757             
758             
759             B->order=copy_int  ( A->order,    B->order,    -1, -1);
760             B->S=A->S;
761             if (A->seq_cache)
762                 {
763                 B->seq_cache=copy_int  ( A->seq_cache,    B->seq_cache,-1,-1);
764                 }
765             
766             if (A->cdna_cache)
767                 {
768                 B->cdna_cache=copy_int  ( A->cdna_cache,    B->cdna_cache,-1,-1);
769                 }
770
771             B->P=copy_profile (A->P);
772             
773             B->Dp_result=A->Dp_result;
774             
775 /*Score*/
776
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);
779             
780
781             B->score_aln=A->score_aln;
782             B->score=A->score;
783             B->cpu=A->cpu;
784             B->finished=A->finished;
785
786 /*Output Options*/    
787             B->output_res_num=A->output_res_num;
788             B->residue_case=A->residue_case;
789             B->expand=A->expand;
790             
791             B->CL=A->CL;
792             B->random_tag=A->random_tag;
793             
794 /*Make the function Recursive */
795             if ( A->A)
796               {
797                 B->A=copy_aln (A->A, NULL);
798               }
799             else B->A=NULL;
800
801             return B;
802         }
803
804 Alignment* shrink_aln ( Alignment *A, int nseq, int *list)
805         {
806         Alignment *B=NULL;
807         int a,seq;
808         
809         B=copy_aln (A, B);
810         for ( a=0; a< nseq; a++)
811             {
812             seq=list[a];
813             sprintf ( A->seq_comment[a], "%s",B->seq_comment[seq]);
814             sprintf ( A->aln_comment[a], "%s",B->aln_comment[seq]);
815             
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];
822             
823             A->score_seq[a]=B->score_seq[seq];
824             A->len[a]=B->len[seq];
825             }
826         A->nseq=nseq;
827         A->len_aln=strlen (A->seq_al[0]);
828         free_aln (B);
829         return A;
830         }
831 Alignment* extract_sub_aln2 ( Alignment *B, int ns, char **ls)
832         {
833           int *list;
834           Alignment *A;
835           
836           list=name_array2index_array(ls, ns, B->name, B->nseq);
837           A=extract_sub_aln ( B,ns, list);
838           vfree (list);
839           return A;
840         }
841 Alignment* extract_sub_aln ( Alignment *B, int nseq, int *list)
842         {
843         Alignment *A=NULL;
844         int a,b,n,seq;
845         
846         A=declare_aln2(nseq, B->len_aln+1);
847         for ( n=0,a=0; a< nseq; a++)
848             {
849             seq=list[a];
850             if ( seq==-1)continue;
851             else n++;
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]);
855             
856             
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];
863             
864             A->score_seq[a]=B->score_seq[seq];
865             A->len[a]=B->len[seq];
866             }
867         A->nseq=n;
868         A->len_aln=B->len_aln;
869         return A;
870         }
871             
872 Alignment *declare_aln2 ( int nseq, int len)
873         {
874           Sequence *S;
875           Alignment *A;
876           
877           S=vcalloc ( 1, sizeof ( Sequence));
878           S->nseq=nseq;
879           S->max_len=len;
880           
881           A=declare_aln (S);
882           A->S=NULL;
883           vfree(S);
884           return A;
885         }
886   
887
888
889 Alignment *declare_aln ( Sequence *S){return declare_Alignment(S);}
890
891 Alignment *declare_Alignment ( Sequence *S)
892         {
893         Alignment *LA;
894         int a;
895         
896         /*ordre:
897           [x][0]= which is the xth seq of aln
898           [x][1]= how many deleted residues before the first one
899         */
900         
901         
902         LA=vcalloc (1, sizeof ( Alignment));
903         aln_stack (LA, DECLARE_ALN);
904         if ( S==NULL)
905             {
906             LA->declared_len=MAX_LEN_ALN;
907             LA->max_n_seq=MAX_N_SEQ;
908             }
909         else
910             {
911             LA->declared_len=2*S->max_len+1;
912             LA->max_n_seq=S->nseq+1;
913             }
914         LA->S=S;
915         
916         
917         LA->seq_comment=declare_char (LA->max_n_seq, COMMENT_SIZE);
918         LA->aln_comment=declare_char (LA->max_n_seq, COMMENT_SIZE);
919         
920
921         LA->seq_al=declare_char ( LA->max_n_seq,LA->declared_len );
922         LA->name=declare_char (LA->max_n_seq, MAXNAMES+1);
923         
924         
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));
934
935         for ( a=0; a< LA->max_n_seq; a++)LA->order[a][0]=a;
936         
937         LA->len_aln=0;
938         LA->score_aln=0;
939         LA->len=vcalloc (LA->max_n_seq, sizeof (int));          
940                 
941         if (S && S->name)for ( a=0; a<S->nseq; a++)
942           {
943             sprintf ( LA->name[a], "%s", S->name[a]);
944             
945           }
946             
947         return LA;
948                 
949         }
950 Alignment * realloc_aln ( Alignment *A, int new_len){return realloc_alignment(A, new_len);}
951 Alignment * realloc_alignment ( Alignment *A, int new_len)
952         {
953         if (A==NULL)A=declare_Alignment (NULL);
954
955         return realloc_alignment2( A, A->max_n_seq,new_len);
956         }
957
958 Alignment * realloc_aln2 ( Alignment *A, int n_nseq, int n_len){return realloc_alignment2(A, n_nseq, n_len);}
959
960
961
962 Alignment * realloc_alignment2 ( Alignment *A, int n_nseq, int n_len)
963         {
964         int a;
965         int len, nseq;
966         int delta_len, delta_nseq;
967         
968         if ( A==NULL) A=declare_Alignment(NULL);
969           
970         n_len++;
971         n_nseq++;
972
973         len=A->declared_len;
974         nseq=A->max_n_seq;
975         
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);
980
981         if ( delta_nseq<=0 && delta_len<=0)return A;
982         
983         
984         else
985             {       
986                 A->len          =vrealloc( A->len      , sizeof (int)*n_nseq);
987                 for (a=nseq; a< n_nseq; a++)A->len[a]=0;
988                                 
989                 A->declared_len =n_len;
990                 A->max_n_seq    =n_nseq;
991
992                 
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);
995                 
996                 A->name   =new_realloc_char ( A->name, n_nseq, -1);
997                 
998                 
999                 A->file   =new_realloc_char ( A->file, n_nseq, -1);
1000                 
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);
1004                 
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);
1007         
1008
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;
1011                 
1012                 
1013                            
1014             }     
1015         return A;
1016         }
1017
1018
1019 long aln_stack (Alignment *A, int mode)
1020 {
1021   static long *list;
1022   static int size;
1023   static int max_size;
1024   
1025   
1026   if (A==NULL) return 0;
1027   else if ( mode==DECLARE_ALN)
1028     {
1029       if ( size==max_size)
1030         {
1031           max_size+=1000;
1032           list=vrealloc (list, max_size*sizeof (long));
1033         }
1034       list[size++]=(long)A;
1035       return 0;
1036     }
1037   else if (mode==FREE_ALN)
1038     {
1039       int a, b;
1040       for (a=0; a<size; a++)
1041         {
1042           if (list[a]==(long)A)
1043             {
1044               for (b=a+1; b<size;b++)
1045                 {
1046                   list[b-1]=list[b];
1047                 }
1048               list[b-1]=0;
1049               size--;
1050               return 1;
1051             }
1052         }
1053       return 0;
1054     }
1055   else if ( mode==EXTRACT_ALN)
1056     {
1057       return list[size--];
1058     }
1059   else
1060     {
1061       printf_exit (EXIT_FAILURE, stderr, "ERROR: Unknown mode for aln_stack");
1062       return 0;
1063     }
1064 }
1065
1066 Sequence* free_aln ( Alignment *LA){ return free_Alignment(LA);}
1067 Sequence* free_Alignment ( Alignment *LA)
1068         {
1069           /* Does not free the A->S field (sequences of A)*/
1070         
1071           Sequence *S;
1072           //aln_stack checks the alignment has not already been freed
1073           if ( LA==NULL || !aln_stack(LA,FREE_ALN)){return NULL;}
1074           
1075           S=LA->S;
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);
1081           
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);
1086           
1087           free_int  ( LA->order, -1);
1088           
1089           vfree ( LA->score_seq);
1090           vfree ( LA->len);
1091           
1092           free_profile (LA->P);
1093           if ( LA->A){free_Alignment (LA->A);LA->A=NULL;}
1094           
1095
1096           vfree ( LA);      
1097           return S;
1098         }
1099
1100 Alignment * update_aln_random_tag ( Alignment *A)
1101 {
1102   static int tag;
1103   if ( !A) return A;
1104   
1105   A->random_tag=++tag;
1106   return A;
1107 }
1108   
1109 Profile   *copy_profile   (Profile *P1)
1110 {
1111
1112   Profile *P;
1113   
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);
1119   
1120   return P;
1121   
1122 }
1123   
1124   
1125 Profile   *declare_profile(char *alphabet, int len)
1126 {
1127   Profile *P;
1128   P=vcalloc ( 1, sizeof ( Profile));
1129   P->alp_size=strlen(alphabet);
1130   P->max_len=len;
1131   P->alphabet=vcalloc ( strlen (alphabet)+2, sizeof (char));
1132   sprintf ( P->alphabet, "%s", alphabet);
1133   
1134   P->count=declare_int( P->alp_size+2, len);
1135   P->count2=declare_int(100, len);
1136   P->count3=declare_int(100, len);
1137   
1138   return P;
1139 }
1140 Profile * free_profile ( Profile *P)
1141 {
1142   if (!P) return NULL;
1143   else
1144     {
1145       vfree (P->alphabet);
1146       free_int ( P->count, -1);
1147       free_int ( P->count2, -1);
1148       vfree (P);
1149     }
1150   return NULL;
1151 }
1152
1153
1154 /************************************************************************/
1155 /*                                                                      */
1156 /*             ALLOCATION                                               */
1157 /*                                                                      */
1158 /*                                                                      */
1159 /************************************************************************/   
1160
1161
1162 double alloc_mem;
1163 double max_mem;
1164 double tot_mem;
1165 Memcontrol *memlast;
1166
1167 FILE* print_mem_usage (FILE *fp, char *comment)
1168 {
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)) );
1170   return fp;
1171 }
1172 void set_max_mem (int m)
1173 {
1174   max_mem=m*1024*1024;
1175 }
1176
1177 int verify_memory (int s)
1178 {
1179   alloc_mem+=s;
1180
1181   tot_mem=(alloc_mem>tot_mem)?alloc_mem:tot_mem;
1182
1183   if (max_mem && alloc_mem>max_mem)
1184     {
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");
1187       
1188       myexit (EXIT_FAILURE);
1189     }
1190   else
1191     return 1;
1192   return 0;
1193   
1194 }
1195
1196 int my_assert ( void *p, int index)
1197 {
1198   static int warning;
1199
1200   if (!warning)
1201     {
1202       fprintf ( stderr, "\n****************************************************************\n");
1203       fprintf ( stderr, "\n          DEBUG MODE [Rebuild For Better Performances]          \n");
1204       fprintf ( stderr, "\n*****************************************************************\n");
1205       warning=1;
1206     }
1207
1208   if ( !is_dynamic_memory(p)) return 1;
1209   else if ( read_array_size_new (p)<=index)
1210     {
1211       fprintf ( stderr, "\nFaulty Allocation: Size=%d Access=%d\n", read_array_size (p,0),index);
1212       return 0;
1213     }
1214   else
1215     {
1216       return 1;
1217     }
1218 }
1219   
1220   
1221
1222 void * vmalloc ( size_t size)
1223         {
1224         void * x;
1225         Memcontrol *M;  
1226
1227         verify_memory (size+2*sizeof (Memcontrol));
1228         
1229         if ( size==0)
1230             return NULL; /*crash ("\n0 bytes in vmalloc\n");*/
1231         else
1232             {
1233             x= malloc (size + 2*sizeof (Memcontrol));
1234             
1235             if ( x==NULL)
1236                 {
1237                   printf_exit (EXIT_FAILURE,stderr, "\nFAILED TO ALLOCATE REQUIRED MEMORY (vmalloc)\n");
1238                   
1239                 }
1240             else
1241               {
1242                 M=x;
1243                 M[0].size=size;
1244                 M[0].size_element=0;
1245                 sprintf ( M[0].check, "dy");
1246                 M+=2;
1247                 x=M;
1248                 return x;
1249               }
1250             }
1251         return NULL;}
1252
1253
1254
1255 void *vcalloc (size_t nobj, size_t size)
1256 {
1257   return sub_vcalloc (nobj,size, MEMSET0);
1258 }
1259 void *vcalloc_nomemset ( size_t nobj, size_t size)
1260 {
1261   return sub_vcalloc (nobj, size, NO_MEMSET0);
1262 }
1263 void *sub_vcalloc ( size_t nobj, size_t size, int MODE)
1264         {
1265         void *x;
1266         Memcontrol *M;
1267         
1268         if ( nobj<=0 || size<=0)return NULL;/*crash ("\n0 bytes in vmalloc\n");*/
1269         else x=vmalloc (nobj*size);
1270
1271
1272         M=x;M-=2;M[0].size_element=size;M+=2;x=M;
1273
1274         if ( x==NULL)
1275                 {
1276                 crash ( "\nFAILED TO ALLOCATE REQUIRED MEMORY (vcalloc)\n");
1277                 return NULL;
1278                 }
1279         else
1280           {
1281             if ( MODE==MEMSET0)
1282               {
1283                 x=memset (x,0, nobj*size);
1284               }
1285             else 
1286               {
1287                 if (nobj)x=memset (x, 0, size);
1288               }
1289             return x;
1290           }
1291         }
1292
1293 void *vrealloc ( void *p, size_t size)
1294         {
1295         void *x;
1296         Memcontrol *M;
1297         size_t i_size;
1298         int a;
1299         
1300         
1301         if ( p==NULL)
1302           {
1303             x=vmalloc (size);
1304             memset (x, 0, size);
1305
1306             return x;
1307           }
1308         else
1309           {
1310             M=p;
1311             M-=2;
1312             i_size=M[0].size;
1313             p=M;
1314         
1315             
1316             if ( size<=0){return NULL;vfree (p);return NULL;}
1317             else
1318               {
1319                 verify_memory (size - i_size);
1320                 x=realloc ( p, size+2*sizeof(Memcontrol));
1321                                 
1322                 if ( x==NULL){crash ( "\nFAILED TO ALLOCATE REQUIRED MEMORY (realloc)\n");return NULL;}
1323                 M=x;
1324                 M[0].size=size;
1325                 M+=2;
1326                 x=M;
1327                 for ( a=i_size; a< size; a++)((char*)x)[a]=0;
1328                 return x;
1329               }
1330           }
1331         return NULL;
1332         }
1333 void vfree ( void *p)
1334      {
1335        Memcontrol *M;
1336        size_t size;
1337
1338        if ( !p)return;
1339        else
1340          {
1341            M=p;
1342            M-=2;
1343            size=M[0].size;
1344            
1345            p=M;
1346            free(p);
1347            
1348            verify_memory (-(size+2*sizeof(Memcontrol)));
1349          }
1350      }
1351 void vfree_all (void *p)
1352 {
1353   Memcontrol *n;
1354   while (memlast)
1355     {
1356       n=memlast->p;
1357       vfree (memlast+2);
1358       memlast=n;
1359     }
1360 }
1361 /*********************************************************************/
1362 /*                                                                   */
1363 /*                          SIZES                                    */
1364 /*                                                                   */
1365 /*                                                                   */
1366 /*********************************************************************/
1367 #define WRITE_SIZE(type,function)\
1368 void function ( int x, type *array, int os)\
1369      {\
1370      fprintf(stderr, "\nwrite_size is a deprecated function [Warning:%s]\n", PROGRAM);return;\
1371      }
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)
1377
1378 #define READ_ARRAY_SIZE(type, function)\
1379 int function (void *array, size_t size)\
1380     {\
1381       return read_array_size (array, size);\
1382     }
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)
1388
1389
1390 int read_array_size_new (void *array)
1391 {
1392   return read_array_size ( array, 0);
1393 }
1394 int read_array_size (void *array, size_t size)
1395     {
1396     Memcontrol *p;
1397     if (array==NULL)return 0;
1398     p=(Memcontrol *)array;
1399     p-=2;
1400     if ( p[0].size_element ==0 && size==0)
1401       {
1402         fprintf ( stderr, "\nERROR in read_array_size: trying to read the size of a malloced block");
1403       }
1404     else if ( size ==0) return (int)p[0].size/p[0].size_element;
1405     
1406     return (int)p[0].size/size;
1407     
1408     }      
1409 int is_dynamic_memory ( void *array)
1410 {
1411   Memcontrol *p;
1412   if (array==NULL)return 0;
1413   p=(Memcontrol *)array;
1414   p-=2;
1415   if ( strm (p[0].check, "dy"))return 1;
1416   return 0;
1417 }
1418
1419 /************************************************************************/
1420 /*                                                                      */
1421 /*             DECLARE 2d ARRAYS                                        */
1422 /*                                                                      */
1423 /*                                                                      */
1424 /************************************************************************/   
1425
1426 void * free_arrayN(void *p, int n)
1427 {
1428   int a, s;
1429   void **i;
1430
1431   
1432   if ( p==NULL) return NULL;
1433   else if ( n==1)vfree ((void *)p); 
1434   else
1435     {
1436       i=(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);
1439       vfree (p);
1440     }
1441   return NULL;
1442 }
1443       
1444 void * declare_arrayNnomemset (int ndim, size_t size, ...)
1445 {
1446   va_list ap;
1447   int *array;
1448   void **p;
1449   int a;
1450   
1451   va_start (ap, size);
1452   
1453   array=vcalloc (ndim, sizeof (int));
1454   for ( a=0; a< ndim; a++)
1455     {
1456       array[a]=va_arg (ap,int);
1457       if ( array[a]<0){va_end(ap);return NULL;}
1458       
1459     }
1460   va_end (ap);
1461
1462   if ( ndim==2)
1463     {
1464       
1465       p=vcalloc_nomemset (array[0], sizeof ( void*));
1466       for (a=0; a< array[0]; a++)
1467         {
1468         p[a]=vcalloc_nomemset (array[1], size);
1469         }
1470     }
1471   else
1472     {
1473       p=declare_arrayN2nomemset (ndim, array, size);
1474     }
1475   vfree (array);
1476   return p;
1477 }
1478
1479 void *declare_arrayN2nomemset ( int ndim, int *A, size_t size)
1480 {
1481   int a;
1482   void **p;
1483     
1484   if ( ndim>1)
1485     {
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);
1489     }
1490   else
1491     {
1492       p=vcalloc_nomemset (A[0], size);
1493     }
1494   return p;
1495 }
1496
1497 void * declare_arrayN (int ndim, size_t size, ...)
1498 {
1499   va_list ap;
1500   int *array;
1501   void **p;
1502   int a;
1503
1504
1505   va_start (ap, size);
1506   
1507   array=vcalloc (ndim, sizeof (int));
1508   for ( a=0; a< ndim; a++)
1509     {
1510       array[a]=va_arg (ap,int);
1511       if ( array[a]<0){va_end(ap);return NULL;}
1512       
1513     }
1514   va_end (ap);
1515
1516   if ( ndim==2)
1517     {
1518
1519       p=vcalloc_nomemset (array[0], sizeof ( void*));
1520       for (a=0; a< array[0]; a++)
1521         p[a]=vcalloc (array[1], size);
1522     }
1523   else
1524     {
1525       p=declare_arrayN2 (ndim, array, size);
1526     }
1527   vfree (array);
1528   return p;
1529 }
1530
1531 void *declare_arrayN2 ( int ndim, int *A, size_t size)
1532 {
1533   int a;
1534   void **p;
1535     
1536   if ( ndim>1)
1537     {
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);
1541     }
1542   else
1543     {
1544       p=vcalloc (A[0], size);
1545     }
1546   return p;
1547 }
1548
1549 void **declare_array (int first, int second, size_t size)
1550 {
1551   return (void **)declare_arrayN (2,size,first, second);
1552 }
1553     
1554
1555 #define DECLARE_ARRAY(type,wf,rf,function)\
1556 type**  function (int first, int second)\
1557   {\
1558     return (type **)declare_arrayN (2,sizeof(type), first, second);\
1559    }
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)
1565
1566 void **declare_array_nomemset (int first, int second, size_t size)
1567 {
1568   return (void **)declare_arrayNnomemset (2,size,first, second);
1569 }
1570 #define DECLARE_ARRAY_NMS(type,wf,rf,function)\
1571 type**  function (int first, int second)\
1572   {\
1573     return (type **)declare_arrayNnomemset (2,sizeof(type), first, second);\
1574    }
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)
1580
1581
1582 Alignment ** declare_aln_array ( int first)
1583     {
1584     Alignment ** array;
1585     int a;
1586     
1587     
1588     array=vcalloc (first, sizeof (Alignment*));
1589     for ( a=0; a< first; a++)
1590       array[a]=declare_Alignment (NULL);
1591     return array;
1592     }
1593
1594
1595
1596
1597 /************************************************************************/
1598 /*                                                                      */
1599 /*             Realloc 2d ARRAYS                                        */
1600 /*                                                                      */
1601 /*                                                                      */
1602 /************************************************************************/   
1603 void ** realloc_arrayN(int ndim,void **main_array,size_t size, ...)
1604 {
1605   va_list ap;
1606   int *array;
1607   void **p;
1608   int a;
1609
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*/
1613   
1614   array=vcalloc (ndim, sizeof (int));
1615   va_start (ap, size);
1616   for ( a=0; a< ndim; a++)
1617     {
1618       array[a]=va_arg (ap,int);
1619       if ( array[a]<-1){va_end(ap);return NULL;}
1620     }
1621   va_end (ap);
1622   
1623   p=realloc_arrayN2 (ndim, main_array, array,size);
1624   vfree (array);
1625   return p;
1626 }
1627
1628 void **realloc_arrayN2 ( int ndim, void ** p, int *A, size_t size)
1629 {
1630   int a;
1631   int o, n;
1632   
1633   
1634   if (ndim==0)return NULL;
1635
1636   if ( ndim>1)
1637     {
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);
1643     }
1644   else
1645     {
1646       o=read_array_size (p, size);
1647       if (A[0]>o)p=vrealloc (p, size*A[0]);
1648     }
1649   return p;
1650 }
1651
1652
1653
1654 void ** realloc_array (void **array,size_t size, int first, int second, int ext1, int ext2)
1655 {
1656   int a;
1657   int d1, d2;
1658   if ( array==NULL)return declare_array (((first==-1)?0:first)+ext1, ((second==-1)?0:second)+ext2, size);
1659   else if ( first==-1)
1660     {
1661       first=read_array_size (array, sizeof (void *));
1662     }
1663   if (second==-1)second=read_array_size(array[0], size);
1664   
1665   d1=first+ext1;
1666   d2=second+ext2;
1667
1668   for ( a=d1; a<first; a++)vfree (array[a]);
1669   array=vrealloc (array, sizeof (void*)*d1);
1670   
1671   if ( d2!=second)
1672     {
1673       for (a=0; a<d1 && a<first; a++)
1674         array[a]=vrealloc ( array[a], size*d2);
1675     }
1676   for ( a=first; a< d1; a++)
1677     array[a]=vrealloc ( array[a], size*d2);
1678   return array;
1679 }
1680
1681
1682
1683   
1684
1685 #define REALLOC_ARRAY(type,wf,rf,function1,function2,function3)\
1686 type ** function1 ( type **array, int first, int second, int ext1, int ext2)\
1687     {\
1688 return (type **)realloc_array ((void **)array, sizeof (type),first, second, ext1, ext2);\
1689 \
1690     }
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)
1696
1697
1698
1699 #define NEW_REALLOC_ARRAY(type,wf,rf,function1,function2,function3)\
1700 type ** function1 ( type **array, int ext1, int ext2)\
1701     {int a, b;\
1702      int first, l1;\
1703      int second, l2;\
1704      type **new_array;\
1705 \
1706      first=rf(array,sizeof (type*));\
1707      second=rf(array[0],sizeof (type));\
1708      \
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);\
1717      return new_array;\
1718     }
1719
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)
1726     {
1727     int a;
1728     int first;
1729     
1730     
1731     if ( array==NULL)
1732          {       
1733          array=declare_aln_array(ext1); 
1734          return array;
1735          }
1736      first=read_array_size ( array, sizeof ( Alignment *));
1737      if ( ext1>0)
1738         {
1739         array=vrealloc ( array, (sizeof (Alignment*))*(first+ext1));
1740         for ( a=first; a<first+ext1; a++)array[a]=declare_Alignment (NULL);
1741         }
1742     else if ( ext1==0);
1743     else if ( ext1<0)
1744          {
1745          for ( a=first-1; a>=(first+ext1);a--)free_Alignment (array[a]);
1746          array=vrealloc ( array, (sizeof (Alignment*))*(first+ext1));
1747          }
1748      return array;    
1749     }
1750
1751 /************************************************************************/
1752 /*                                                                      */
1753 /*            free 2d ARRAYS                                        */
1754 /*                                                                      */
1755 /*                                                                      */
1756 /************************************************************************/   
1757 #define FREE_ARRAY(type,wf,rf,function) \
1758 type ** function (type **array, int first)\
1759     {\
1760       return free_arrayN((void*)array, 2);\
1761     }
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)
1767
1768
1769
1770 Alignment ** free_aln_array (Alignment **array)
1771    {
1772    int a;
1773    int len;
1774
1775
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]);
1779    vfree ( array);
1780    return  NULL;
1781    }
1782
1783 Fname *declare_fname (int size)
1784    {
1785    Fname *F;
1786
1787    size+=strlen (get_home_4_tcoffee())+FILENAMELEN+1;
1788   
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));
1794    return F;
1795    }
1796
1797 Fname *free_fname ( Fname *F)
1798    {
1799    vfree (F->name);
1800    vfree (F->path);
1801    vfree (F->suffix);
1802    return NULL;
1803    }
1804 /*********************************COPYRIGHT NOTICE**********************************/
1805 /*© Centro de Regulacio Genomica */
1806 /*and */
1807 /*Cedric Notredame */
1808 /*Tue Oct 27 10:12:26 WEST 2009. */
1809 /*All rights reserved.*/
1810 /*This file is part of T-COFFEE.*/
1811 /**/
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.*/
1816 /**/
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.*/
1821 /**/
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 /*...............................................                                                                                                                                     |*/
1829 /**/
1830 /**/
1831 /*      */
1832 /*********************************COPYRIGHT NOTICE**********************************/