Logo Search packages:      
Sourcecode: t-coffee version File versions  Download package

util_declare.c

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <limits.h>
#include <float.h>
#include <assert.h>
#include <math.h>
#include <stdarg.h>
#include <sys/times.h>
#include <signal.h>
#include <unistd.h>

#include "io_lib_header.h"
#include "util_lib_header.h"
#include "define_header.h"

/************************************************************************/
/*                                                                      */
/*            CONSTRAINT_LIST                                           */
/*                                                                      */
/*                                                                      */
/************************************************************************/ 
Constraint_list * declare_constraint_list ( Sequence *S, char *name, int **L, int ne,FILE *fp, int **M)
    {
    Constraint_list *CL;

    CL=vcalloc (1, sizeof ( Constraint_list));
  
    
    CL->S=S;
    CL->M=M;

    if ( name!=NULL)
      {
      sprintf ( CL->list_name, "%s", name);
      
      }
    CL->cpu=1;
    CL->fp=fp;
    CL->L=L;
    CL->ne=ne;
    CL->entry_len=LIST_N_FIELDS;
    CL->el_size=sizeof (CLIST_TYPE);
    CL->matrices_list=declare_char(20,20);
    
    CL->chunk=500;
    CL->weight_field=WE;
    if ( S)CL->seq_for_quadruplet=vcalloc ( S->nseq, sizeof (int));
    CL->Prot_Blast=vcalloc ( 1, sizeof ( Blast_param));
    CL->DNA_Blast=vcalloc ( 1, sizeof ( Blast_param));
    CL->Pdb_Blast=vcalloc ( 1, sizeof ( Blast_param));
    
    return CL;
    }
/*
Constraint_list *cache_dp_value4constraint_list ( char mode[],Constraint_list *CL)
    {
      static char dp_mode[100];
      static int gop;
      static int f_gop;
      static int f_gep;
      static int gep;
      static int TG_MODE;
      static int F_TG_MODE;
      static int maximise;
      static char matrix_for_aa_group[100];
      static int diagonal_threshold;
      static int ktup;
      static int fasta_step;
      static int use_fragments;
      static int extend_jit;
      static int (*get_dp_cost)(Alignment*, int **, int, int*, int, int **, int, int*, int, struct Constraint_list *);
        static int (*evaluate_residue_pair)(struct Constraint_list *, int, int, int, int);


      if ( strm (mode, "cache"))
         {
         sprintf (dp_mode, "%s", CL->dp_mode);
         gop=CL->gop;
         f_gop=CL->f_gop;
         gep=CL->gep;
         f_gep=CL->f_gep;
         TG_MODE=CL->TG_MODE;
         F_TG_MODE=CL->F_TG_MODE;
         maximise=CL->maximise;
         sprintf (matrix_for_aa_group, "%s", CL->matrix_for_aa_group);
         diagonal_threshold=CL->diagonal_threshold;
         ktup=CL->ktup;
         fasta_step=CL->fasta_step;
         use_fragments=CL->use_fragments;
         extend_jit=CL->extend_jit;
         get_dp_cost=CL->get_dp_cost;
         evaluate_residue_pair=CL->evaluate_residue_pair;
         return CL;
         }
      else if ( strm (mode, "restaure"))
         {
         sprintf ( CL->dp_mode, "%s",dp_mode);
         CL->gop=gop;
         CL->f_gop=f_gop;
         CL->gep=gep;
         CL->f_gep=f_gep;
         
         CL->TG_MODE=TG_MODE;
         CL->F_TG_MODE=F_TG_MODE;
         CL->maximise=maximise;
         sprintf ( CL->matrix_for_aa_group, "%s", matrix_for_aa_group);
         CL->diagonal_threshold=diagonal_threshold;
         CL->fasta_step=fasta_step;
         CL->ktup=ktup;
         CL->use_fragments=use_fragments;
         CL->extend_jit=extend_jit;
         CL->get_dp_cost=get_dp_cost;
         CL->evaluate_residue_pair=evaluate_residue_pair;
         return CL;
         
         }
      else
         {
             fprintf (stderr, "\nUNKNOWN MODE %s for cache_dp_value4comstraint_list[FATAL]", mode);
             crash ( "");
             return 0;
         }
    }
*/        
Constraint_list *free_constraint_list4lib_computation (Constraint_list *CL)
{
  
  free_int (CL->L, -1);
  free_int (CL->M, -1);
  
  vfree (CL);
  return CL;
}
Constraint_list *duplicate_constraint_list4lib_computation (Constraint_list *CL)
{
  Constraint_list *SCL;
  SCL=vcalloc (1, sizeof ( Constraint_list));
  SCL[0]=CL[0];
  SCL->L=NULL;
  SCL->max_L_len=0;
  SCL->M=NULL;
  SCL->ne=0;
  SCL->residue_indexed=0;
  SCL->residue_index=NULL;
  return SCL;
}
Constraint_list *duplicate_constraint_list_soft (Constraint_list *CL)
{
  /*Duplication that does not copy the long lists*/
  return copy_constraint_list (CL,SOFT_COPY);
}
Constraint_list *duplicate_constraint_list (Constraint_list *CL)
     {
     /*Duplicate everything in the constraint_list*/
     return copy_constraint_list (CL,HARD_COPY);
     }
Constraint_list *copy_constraint_list (Constraint_list *CL, int mode)
    {   
    Constraint_list *NCL;
    Sequence *S;
    int a, b;
    
   
    
    /*Sequences*/
    

      S=(mode==HARD_COPY)?duplicate_sequence (CL->S):CL->S;
 
      if (mode==HARD_COPY)
      NCL=declare_constraint_list (S, NULL, NULL,0, NULL, NULL);
      else
      {
        NCL=vcalloc ( 1, sizeof (Constraint_list));
        NCL[0]=CL[0];
      }
      
      NCL->S=S;
      NCL->copy_mode=mode;
      if (mode==SOFT_COPY)NCL->pCL=CL;
      

      NCL->STRUC_LIST=(mode==HARD_COPY)?duplicate_sequence (CL->STRUC_LIST):CL->STRUC_LIST;
      sprintf ( NCL->align_pdb_param_file, "%s", CL->align_pdb_param_file);
      sprintf ( NCL->align_pdb_hasch_mode, "%s", CL->align_pdb_hasch_mode);

      
      NCL->W=(mode==HARD_COPY)?duplicate_weights (CL->W):CL->W;
      NCL->similarity_matrix=(mode==HARD_COPY)?duplicate_int (CL->similarity_matrix, -1, -1):CL->similarity_matrix;
      NCL->score_similarity_matrix=(mode==HARD_COPY)?duplicate_int (CL->score_similarity_matrix, -1, -1):CL->score_similarity_matrix;
      NCL->distance_matrix=(mode==HARD_COPY)?duplicate_int (CL->distance_matrix, -1, -1):CL->distance_matrix;
      
      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];}
      else{NCL->translation=CL->translation;}
      
      NCL->out_aln_format=(mode==HARD_COPY)?duplicate_char (CL->out_aln_format, -1, -1):CL->out_aln_format;
      NCL->n_out_aln_format=CL->n_out_aln_format;
      
    /*Packing Sequence: To use with domain analysis*/
      NCL->packed_seq_lu=(mode==HARD_COPY)?duplicate_int (CL->packed_seq_lu, -1, -1):CL->packed_seq_lu;
    /*DATA*/
      if (CL->fp)(mode==HARD_COPY)?NCL->fp=vtmpfile():CL->fp;
      
      if ( mode==HARD_COPY)
      {
        for ( a=0; a< CL->ne; a++)
          for ( b=0; b< CL->entry_len; b++)     vwrite_clist(NCL, a, b, vread_clist(CL, a, b));
      }
      else NCL->L=CL->L;
      
    
     if ( mode==HARD_COPY)
       {
       NCL->M=copy_int ( CL->M,NCL->M,-1, -1);
       }
     else
       NCL->M=CL->M;
    

    /*List Information*/   
      NCL->ne=CL->ne;
      sprintf ( NCL->list_name, "%s", CL->list_name);
      NCL->entry_len=CL->entry_len;
      NCL->el_size=CL->el_size;

    /*Normalisation information*/
      NCL->normalise=CL->normalise;
      NCL->max_ext_value=CL->max_ext_value;
      NCL->max_value=CL->max_value;
    
    /*Pair wise alignment method*/
      NCL->pw_parameters_set=CL->pw_parameters_set;
      NCL->gop=CL->gop;
      NCL->f_gop=CL->f_gop;
      NCL->gep=CL->gep;
      NCL->f_gep=CL->f_gep;
      
      NCL->nomatch=CL->nomatch;
     
      NCL->TG_MODE=CL->TG_MODE;
      NCL->F_TG_MODE=CL->F_TG_MODE;
      
      sprintf ( NCL->dp_mode, "%s", CL->dp_mode);
      NCL->maximise=CL->maximise;
      sprintf ( NCL->matrix_for_aa_group, "%s", CL->matrix_for_aa_group);
      sprintf ( NCL->method_matrix, "%s", CL->method_matrix);
      
      NCL->diagonal_threshold=CL->diagonal_threshold;
      NCL->ktup=CL->ktup;
      
      NCL->use_fragments=CL->use_fragments;
      NCL->fasta_step=CL->fasta_step;
      NCL->lalign_n_top=CL->lalign_n_top;
      NCL->sw_min_dist=CL->sw_min_dist;
      NCL->matrices_list=(mode==HARD_COPY)?duplicate_char (CL->matrices_list, -1, -1):CL->matrices_list;
      NCL->n_matrices=CL->n_matrices;
      sprintf (NCL->tree_mode, "%s", CL->tree_mode);
      NCL->tree_aln=(mode==HARD_COPY)?copy_aln (CL->tree_aln, NULL):CL->tree_aln;
    /*Functions used for dynamic programming and Evaluation*/ 
      NCL->profile_mode=CL->profile_mode;
      sprintf ( NCL->profile_comparison, "%s",CL->profile_comparison);
      NCL->get_dp_cost=CL->get_dp_cost;
      NCL->evaluate_residue_pair=CL->evaluate_residue_pair;
      NCL->pair_wise=CL->pair_wise;

      NCL->weight_field=CL->weight_field;
      NCL->max_n_pair=CL->max_n_pair;
      
    /*threading parameters*/
      NCL->Prot_Blast=(mode==HARD_COPY)?duplicate_blast_param ( CL->Prot_Blast):CL->Prot_Blast;
      NCL->DNA_Blast =(mode==HARD_COPY)?duplicate_blast_param ( CL->DNA_Blast):CL->DNA_Blast;
      NCL->Pdb_Blast =(mode==HARD_COPY)?duplicate_blast_param ( CL->Pdb_Blast):CL->Pdb_Blast;
    /*Split parameters*/
      NCL->split=CL->split;
      NCL->split_nseq_thres= CL->split_nseq_thres;
      NCL->split_score_thres= CL->split_score_thres;
    /*Structural status*/
      NCL->check_pdb_status=CL->check_pdb_status;
    /*log*/
      sprintf ( NCL->method_log, "%s",CL->method_log);
      sprintf ( NCL->evaluate_mode, "%s",CL->evaluate_mode);
      
    /*Parameters for domain extraction*/
      NCL->moca=(mode==HARD_COPY)?duplicate_moca ( CL->moca):CL->moca;
      
      

    /*Functions for hiding forbiden pairs of residues*/
      /* Copy only for soft_copy*/
      if (mode==SOFT_COPY)
      {
        NCL->forbiden_pair_list=CL->forbiden_pair_list;
      }
   /*extention properties:*/
      NCL->nseq_for_quadruplet=CL->nseq_for_quadruplet;
      if (mode==HARD_COPY && CL->seq_for_quadruplet)
      {NCL->seq_for_quadruplet=vcalloc ( S->nseq, sizeof(int));
      for ( a=0; a< S->nseq; a++)
        NCL->seq_for_quadruplet[a]=CL->seq_for_quadruplet[a];
      }
      else if (mode==SOFT_COPY)
      {
        NCL->seq_for_quadruplet=CL->seq_for_quadruplet;
      }
      
   /*extention properties: Do only a soft copy*/
      /* Not To be copied yet */
      if ( mode==SOFT_COPY)
      {
        NCL->extend_jit=CL->extend_jit;
        NCL->extend_threshold=CL->extend_threshold;
        sprintf ( NCL->extend_clean_mode, "%s", CL->extend_clean_mode);
        sprintf ( NCL->extend_compact_mode, "%s", CL->extend_compact_mode);
      }
        
    /*Lookup table parameteres*/
      NCL->chunk= CL->chunk;
      /* Do NOT copy NCL->seq_indexed, NCL->start_index, NCL->max_L_len, NCL->chunk*/
      /*if ( mode==SOFT_COPY)
      {
        NCL->seq_indexed=CL->seq_indexed;
        NCL->start_index=CL->start_index;
        NCL->end_index=CL->start_index;
        NCL->max_L_len=CL->max_L_len;
        }*/
    /*PDB STRUCTURE ALIGNMENTS*/
      /* Do only a soft copy */
      if ( mode==SOFT_COPY)
      { 
        NCL->T=CL->T;
      }
    /*MISC*/  
       NCL->cpu=CL->cpu;
       NCL->local_stderr=CL->local_stderr;
       sprintf (NCL->multi_thread, "%s", CL->multi_thread);
    
    return NCL;
    }
Constraint_list *free_constraint_list_full (Constraint_list *CL)
{
  free_sequence (free_constraint_list (CL), -1);
  return NULL;
}
Sequence *free_constraint_list (Constraint_list *CL)
    {
    Sequence *S;
    int a, b;
    Constraint_list *pCL;


    /*Prepare the selective freeing of the CL data structure:
      If the CL has been obtain from copy, every pointer that is identical to the parent CL (CL->pCL)
      will not be saved.
    */
      
    if ( !CL)return NULL;
    else S=CL->S;
    
    if ( CL->copy_mode==SOFT_COPY && !CL->pCL){vfree(CL); return S;}
    else if ( CL->copy_mode==SOFT_COPY)
      {
      pCL=CL->pCL;
      CL->L=NULL;
      if ( CL->M                      ==pCL->M                       )CL->M=NULL;
      /*if (CL->start_index             ==pCL->start_index             )pCL->start_index=NULL;
      if (CL->residue_index           ==pCL->residue_index           )CL->residue_index=NULL;
      */
      
      if ( CL->fp                     ==pCL->fp                      )CL->fp=NULL;
      if ( CL->matrices_list          ==pCL->matrices_list           )CL->matrices_list=NULL;
      
      if ( CL->STRUC_LIST             ==pCL->STRUC_LIST              )CL->STRUC_LIST=NULL;
      if ( CL->W                      ==pCL->W                       )CL->W=NULL;
      if ( CL->similarity_matrix      ==pCL->similarity_matrix       )CL->similarity_matrix=NULL;
      if ( CL->score_similarity_matrix==pCL->score_similarity_matrix )CL->score_similarity_matrix=NULL;
      if ( CL->distance_matrix        ==pCL->distance_matrix         )CL->distance_matrix=NULL;
      if ( CL->translation            ==pCL->translation             )CL->translation=NULL;
      if ( CL->moca                   ==pCL->moca                    )CL->moca=NULL;
      if ( CL->Prot_Blast             ==pCL->Prot_Blast              )CL->Prot_Blast=NULL;
      if ( CL->DNA_Blast              ==pCL->DNA_Blast               )CL->DNA_Blast=NULL;
      if ( CL->Pdb_Blast              ==pCL->Pdb_Blast               )CL->Pdb_Blast=NULL;
      if ( CL->seq_for_quadruplet     ==pCL->seq_for_quadruplet       )CL->seq_for_quadruplet=NULL;
      
      }

    /*End of selective freeing of the CL data structure*/


    if ( CL->L)free_int (CL->L, -1);
    if ( CL->M)free_int (CL->M, -1);
    if ( CL->fp)vfclose (CL->fp);
    if ( CL->matrices_list)free_char(CL->matrices_list,-1);
    if ( CL->start_index)free_int ( CL->start_index,-1);
    if ( CL->end_index)free_int ( CL->end_index,-1);
    if ( CL->residue_index)
      {
      for ( a=0; a< (CL->S)->nseq; a++)
        {
          for ( b=0; b<=(CL->S)->len[a]; b++)
            vfree(CL->residue_index[a][b]);
          vfree (CL->residue_index[a]);
        }
      vfree(CL->residue_index);
      }
    
 
    if ( CL->STRUC_LIST)free_sequence ( CL->STRUC_LIST, (CL->STRUC_LIST)->nseq);
    if ( CL->W)free_weights (CL->W);
    if ( CL->similarity_matrix) free_int (CL->similarity_matrix, -1);
    if ( CL->score_similarity_matrix) free_int (CL->score_similarity_matrix, -1);
    if ( CL->distance_matrix) free_int (CL->distance_matrix, -1);
    
    if ( CL->translation)vfree(CL->translation);
    if ( CL->moca)free_moca (CL->moca);
    if ( CL->Prot_Blast)free_blast_param ( CL->Prot_Blast);
    if ( CL->DNA_Blast) free_blast_param ( CL->DNA_Blast);
    if ( CL->Pdb_Blast) free_blast_param ( CL->Pdb_Blast);
       
    if (CL->seq_for_quadruplet)vfree (CL->seq_for_quadruplet);
    
    vfree(CL);
    return S;
    }

/************************************************************************/
/*                                                                      */
/*            MOCA Functions                                            */
/*                                                                      */
/*                                                                      */
/************************************************************************/
Moca * duplicate_moca ( Moca *m)
      {
      Moca *nm;
      
      if ( m==NULL)return m;

      nm=vcalloc ( 1, sizeof (Moca));

      nm->moca_scale=m->moca_scale; 
      nm->evaluate_domain=m->evaluate_domain;
      nm->moca_threshold=m->moca_threshold;
      nm->cache_cl_with_domain=m->cache_cl_with_domain;
      if ( m->forbiden_residues)nm->forbiden_residues=copy_int  (m->forbiden_residues,nm->forbiden_residues,    -1, -1);
      nm->make_nol_aln=m->make_nol_aln;

      
      return nm;
      }
Moca * free_moca ( Moca *m)
      {
      if ( m->forbiden_residues)free_int ( m->forbiden_residues, -1);
      vfree ( m);
      return NULL;
      }

/************************************************************************/
/*                                                                      */
/*            Blast_param Functions                                            */
/*                                                                      */
/*                                                                      */
/************************************************************************/
Blast_param * duplicate_blast_param ( Blast_param*B)
{
  Blast_param *N;
  N=vcalloc (1, sizeof ( Blast_param));
  sprintf ( N->blast_server, "%s", B->blast_server);
  sprintf ( N->db, "%s", B->db);
  N->min_id=B->min_id;
  N->max_id=B->min_id;
  N->min_cov=B->min_cov;
  return N;
}
Blast_param * free_blast_param ( Blast_param*B)
{
  vfree (B);
  return NULL;
}

/************************************************************************/
/*                                                                      */
/*            PDB Functions                                             */
/*                                                                      */
/*                                                                      */
/************************************************************************/         
Structure* declare_structure ( int n, char **array)
    {
    Structure *S;
    int a;

    S=vcalloc (1, sizeof (Structure));
    S->n_fields=1;
    S->nseq=n;
    
    S->struc=vcalloc ( n, sizeof (int**));
    S->len=vcalloc ( n, sizeof (int));
    for ( a=0; a< n; a++)
        {
      S->len[a]=strlen(array[a]);
      S->struc[a]=declare_int ( strlen ( array[a])+2, 1);   
      }
    return S;
    }

Structure *extend_structure ( Structure *S)
    {
    int a, b;


    for ( a=0; a< S->nseq; a++)
        {
      for ( b=0; b< S->len[a]; b++)
            S->struc[a][b]=vrealloc ( S->struc[a][b],( S->n_fields+1)*sizeof (int));               
      }
    S->n_fields++;
    return S;
    }

Sequence * declare_sequence ( int min, int max, int nseq)
    {
    Sequence *LS;
    
    

    LS=vcalloc (1, sizeof ( Sequence));

    LS->seq_comment=declare_char ( nseq,STRING+1);
    LS->aln_comment=declare_char ( nseq,STRING+1);
    
    LS->file=declare_char( nseq,STRING+1);
    LS->seq=declare_char ( nseq, max+1);
    LS->name=declare_char( nseq,STRING+1);
    
    LS->len=vcalloc ( nseq, sizeof (int));
    LS->max_len=max;
    LS->min_len=min;
    LS->nseq=nseq;
    LS->max_nseq=nseq;
    LS->type=vcalloc(30, sizeof (char));
    LS->T=declare_arrayN(2, sizeof (Template), nseq, 1);
    return LS;
    }
Sequence * realloc_sequence   (Sequence *OUT, int new_nseq, int max_len)
    {
      int a;

      if ( new_nseq<OUT->max_nseq)return OUT;

      OUT->min_len =MIN(OUT->min_len,max_len);
      OUT->max_len =MAX(OUT->max_len,max_len);
      OUT->seq_comment =new_realloc_char ( OUT->seq_comment, new_nseq,STRING+1);
      OUT->aln_comment =new_realloc_char ( OUT->aln_comment, new_nseq,STRING+1);
      
      OUT->seq     =new_realloc_char ( OUT->seq,     new_nseq,OUT->max_len+1);
      OUT->name    =new_realloc_char ( OUT->name,    new_nseq,STRING+1);
      
      
      OUT->file    =new_realloc_char ( OUT->file,    new_nseq,STRING+1);
      OUT->len     =vrealloc     ( OUT->len,     (new_nseq+1)*sizeof (int));
      
      OUT->T=(Template**)realloc_arrayN (2, (void **)OUT->T,sizeof (Template), new_nseq, 1);
      
      OUT->max_nseq=new_nseq;
      return OUT;
    }
      
Sequence * duplicate_sequence (Sequence *S )
    {
    Sequence *LS;
    int a;
    

    if (S==NULL)return S;
    LS=declare_sequence (S->min_len, S->max_len, S->nseq);
        
    for ( a=0; a<S->nseq; a++)
        {
      sprintf ( LS->file[a], "%s", S->file[a]); 
      sprintf ( LS->seq_comment[a], "%s", S->seq_comment[a]);
      sprintf ( LS->aln_comment[a], "%s", S->aln_comment[a]);
      
      sprintf ( LS->seq[a], "%s", S->seq[a]);
      sprintf ( LS->name[a], "%s", S->name[a]);
      LS->len[a]=S->len[a];
      
      LS->T[a][0]=S->T[a][0];
      }

    LS->max_len=S->max_len;
    LS->min_len=S->min_len;
    LS->nseq=S->nseq;

    if ( S->W)LS->W=duplicate_weights (S->W);
    sprintf ( LS->type, "%s", S->type);
    sprintf ( LS->template_file, "%s", S->template_file);
    LS->max_nseq=S->nseq;
    return LS;
    }

void free_sequence ( Sequence *LS, int nseq)
      {
      int a;
      
      if ( !LS) return;


      free_char ( LS->file, -1);
      free_char ( LS->seq_comment, -1);
      free_char ( LS->aln_comment, -1);
      free_char ( LS->seq, -1);
      free_char ( LS->name,-1);
      
      free_arrayN((void*)LS->T, 2);
      vfree (LS->type);
      vfree (LS->len);
      free_weights (LS->W);
      vfree (LS);

      }
/************************************************************************/
/*                                                                      */
/*            Weights Functions                                         */
/*                                                                      */
/*                                                                      */
/************************************************************************/
Weights* declare_weights ( int nseq)
      {
      Weights *W;
      
      W=vcalloc ( 1, sizeof ( Weights));
      W->comments=vcalloc ( 1000, sizeof (char));
      W->nseq=nseq;
      W->mode=vcalloc (FILENAMELEN, sizeof (char));
      W->seq_name= declare_char ( W->nseq*2, 200);
      W->PW_SD=declare_float ( W->nseq, W->nseq);
      W->PW_ID=declare_float ( W->nseq, W->nseq);
      W->SEQ_W=vcalloc ( W->nseq, sizeof ( float));
      return W;
      }  
Weights* duplicate_weights (Weights *W)
     {
       Weights *NW;
       int a, b;

       NW=declare_weights (W->nseq);
       sprintf ( NW->comments, "%s", W->comments);
       sprintf ( NW->mode, "%s", W->mode);
       for (a=0; a< W->nseq; a++)
       {
         sprintf ( NW->seq_name[a], "%s", W->seq_name[a]);
         NW->SEQ_W[a]=W->SEQ_W[a];
         for(b=0; b< W->nseq; b++)
           {
                  NW->PW_SD[a][b]=W->PW_SD[a][b];
            NW->PW_ID[a][b]=W->PW_ID[a][b];
           }
       }
       return NW;
     }
Weights* free_weights ( Weights* W)
      {

      if ( !W)return NULL;
      
      vfree(W->comments);
      
      
      vfree(W->mode);
      
      free_char(W->seq_name, -1);   
      free_float(W->PW_SD,-1);      
      free_float(W->PW_ID, -1);     
      vfree(W->SEQ_W);
      vfree(W);
      return NULL;
      } 


Alignment* copy_aln ( Alignment *A, Alignment *B)
        {
        int a, b;
        
        /*    c[100]=10;*/
       

        
        if ( A==NULL){free_aln(B); return NULL;}          
        free_aln (B);
       
        
        if ( A->S && A->nseq>(A->S)->nseq)
          {
            B=declare_aln2(A->nseq, MAX((A->S)->max_len+1, A->len_aln+1));
            B->S=A->S;
          }
        else 
          B=declare_aln ((A->S));

        
          
/*SIZES*/    
          B->max_len=A->max_len;
          B->min_len=A->min_len;
          B->declared_len=A->declared_len;
          B->max_n_seq=A->max_n_seq;

          B->nseq=A->nseq;    
          B->len_aln=A->len_aln;
         
         
/*sequence Information*/      

          if ( (A->S)==NULL){vfree (B->len); B->len=vcalloc ( A->max_n_seq, sizeof (int));}
          ga_memcpy_int ( A->len, B->len, B->nseq);
          
          B->seq_comment=copy_char ( A->seq_comment,  B->seq_comment,  -1,-1);
          B->aln_comment=copy_char ( A->aln_comment,  B->aln_comment,  -1,-1);
          
          B->name=copy_char ( A->name,     B->name,     -1,-1);
          
          B->file=copy_char ( A->file,     B->file,     -1,-1);
          B->tree_order=copy_char ( A->tree_order,     B->tree_order,     -1,-1);
          B->expanded_order=A->expanded_order;
          free_char ( B->seq_al, -1);
          B->seq_al=declare_char(B->max_n_seq, B->declared_len);
          for ( a=0; a< A->max_n_seq; a++)
            {
            for ( b=0; b< A->declared_len; b++)
              B->seq_al[a][b]=A->seq_al[a][b];
            }
          
          
          
          B->order=copy_int  ( A->order,    B->order,    -1, -1);
          B->S=A->S;
          if (A->seq_cache)
              {
            B->seq_cache=copy_int  ( A->seq_cache,    B->seq_cache,-1,-1);
            }
          
          if (A->cdna_cache)
              {
            B->cdna_cache=copy_int  ( A->cdna_cache,    B->cdna_cache,-1,-1);
            }

          B->P=copy_profile (A->P);
          
          B->Dp_result=A->Dp_result;
          
/*Score*/

          if ( (A->S)==NULL){vfree (B->score_seq); B->score_seq=vcalloc ( A->max_n_seq, sizeof (int));}
          ga_memcpy_int(  A->score_seq,B->score_seq,B->nseq);
          

          B->score_aln=A->score_aln;
          B->score=A->score;
          B->cpu=A->cpu;
          B->finished=A->finished;

/*Output Options*/    
          B->output_res_num=A->output_res_num;
          B->residue_case=A->residue_case;
          
          B->CL=A->CL;
          B->random_tag=A->random_tag;
          
/*Make the function Recursive */
          if ( A->A)
            {
            B->A=copy_aln (A->A, NULL);
            }
          else B->A=NULL;

          return B;
      }

Alignment* shrink_aln ( Alignment *A, int nseq, int *list)
        {
      Alignment *B=NULL;
      int a,seq;
      
      B=copy_aln (A, B);
      for ( a=0; a< nseq; a++)
          {
          seq=list[a];
          sprintf ( A->seq_comment[a], "%s",B->seq_comment[seq]);
          sprintf ( A->aln_comment[a], "%s",B->aln_comment[seq]);
          
          sprintf ( A->seq_al [a], "%s",B->seq_al [seq]);
          A->order[a][0]=B->order[seq][0];
          A->order[a][1]=B->order[seq][1];
          A->score_seq[a]=B->score_seq[seq];
          A->len[a]=B->len[seq];
          }
      A->nseq=nseq;
      A->len_aln=strlen (A->seq_al[0]);
      free_aln (B);
      return A;
      }
Alignment* extract_sub_aln2 ( Alignment *B, int ns, char **ls)
        {
        int *list;
        Alignment *A;
        
        list=name_array2index_array(ls, ns, B->name, B->nseq);
        A=extract_sub_aln ( B,ns, list);
        vfree (list);
        return A;
      }
Alignment* extract_sub_aln ( Alignment *B, int nseq, int *list)
        {
      Alignment *A=NULL;
      int a,b,n,seq;
      
      A=declare_aln2(nseq, B->len_aln+1);
      for ( n=0,a=0; a< nseq; a++)
          {
          seq=list[a];
          if ( seq==-1)continue;
          else n++;
          sprintf ( A->seq_comment[a], "%s",B->seq_comment[seq]);
          sprintf ( A->aln_comment[a], "%s",B->aln_comment[seq]);
          sprintf ( A->name[a], "%s",B->name[seq]);
          
          
          for (b=0; b<=B->len_aln; b++)A->seq_al [a][b]=B->seq_al [seq][b];
          A->order[a][0]=B->order[seq][0];
          A->order[a][1]=B->order[seq][1];
          A->score_seq[a]=B->score_seq[seq];
          A->len[a]=B->len[seq];
          }
      A->nseq=n;
      A->len_aln=B->len_aln;
      return A;
      }
          
Alignment *declare_aln2 ( int nseq, int len)
        {
        Sequence *S;
        Alignment *A;
        
        S=vcalloc ( 1, sizeof ( Sequence));
        S->nseq=nseq;
        S->max_len=len;
        
        A=declare_aln (S);
        A->S=NULL;
        vfree(S);
        return A;
      }
  


Alignment *declare_aln ( Sequence *S){return declare_Alignment(S);}

Alignment *declare_Alignment ( Sequence *S)
      {
      Alignment *LA;
      int a;
      
      /*ordre:
        [x][0]= which is the xth seq of aln
        [x][1]= how many deleted residues before the first one
      */

      LA=vcalloc (1, sizeof ( Alignment));
      if ( S==NULL)
          {
          LA->declared_len=MAX_LEN_ALN;
          LA->max_n_seq=MAX_N_SEQ;
          }
      else
          {
          LA->declared_len=2*S->max_len+1;
          LA->max_n_seq=S->nseq+1;
          }
      LA->S=S;
      
      LA->seq_comment=declare_char_nomemset (LA->max_n_seq, STRING);
      LA->aln_comment=declare_char_nomemset (LA->max_n_seq, STRING);
      
      LA->seq_al=declare_char ( LA->max_n_seq,LA->declared_len );
      LA->name=declare_char_nomemset (LA->max_n_seq, STRING);
      
      
      LA->file=declare_char (LA->max_n_seq, STRING);
      LA->tree_order=declare_char (LA->max_n_seq, STRING);
      LA->order= declare_int (LA->max_n_seq , 2);
      LA->score_seq= vcalloc (LA->max_n_seq, sizeof (int));

      for ( a=0; a< LA->max_n_seq; a++)LA->order[a][0]=a;
      
      LA->len_aln=0;
      LA->score_aln=0;
      LA->len=vcalloc (LA->max_n_seq, sizeof (int));        
      
      if (S && S->name)for ( a=0; a<S->nseq; a++)
        {
          sprintf ( LA->name[a], "%s", S->name[a]);
          
        }
          
      return LA;
            
      }
Alignment * realloc_aln ( Alignment *A, int new_len){return realloc_alignment(A, new_len);}
Alignment * realloc_alignment ( Alignment *A, int new_len)
      {
      if (A==NULL)A=declare_Alignment (NULL);

      return realloc_alignment2( A, A->max_n_seq,new_len);

      }

Alignment * realloc_aln2 ( Alignment *A, int n_nseq, int n_len){return realloc_alignment2(A, n_nseq, n_len);}



Alignment * realloc_alignment2 ( Alignment *A, int n_nseq, int n_len)
      {
      int a;
      int len, nseq;
      int delta_len, delta_nseq;
      
        if ( A==NULL) A=declare_Alignment(NULL);
        
      n_len++;
      n_nseq++;

      len=A->declared_len;
      nseq=A->max_n_seq;
      
      n_len=MAX(len, n_len);
      n_nseq=MAX(nseq,n_nseq);
      delta_nseq=MAX(0,n_nseq-nseq);
      delta_len =MAX(0,n_len-len);

      if ( delta_nseq<=0 && delta_len<=0)return A;
      
      
      else
          {     
              A->len          =vrealloc( A->len      , sizeof (int)*n_nseq);
            for (a=nseq; a< n_nseq; a++)A->len[a]=0;
            A->declared_len =n_len;
            A->max_n_seq    =n_nseq;

            
            A->seq_comment=new_realloc_char ( A->seq_comment, n_nseq, -1);
            A->aln_comment=new_realloc_char ( A->aln_comment, n_nseq, -1);
            
            A->name   =new_realloc_char ( A->name, n_nseq, -1);
            
            
            A->file   =new_realloc_char ( A->file, n_nseq, -1);
            
            A->tree_order   =new_realloc_char ( A->tree_order, n_nseq, -1);
            A->seq_al =new_realloc_char ( A->seq_al, n_nseq, n_len);
            A->order  =new_realloc_int  ( A->order, n_nseq, -1);
            
            if ( A->seq_cache) A->seq_cache=new_realloc_int  ( A->seq_cache, n_nseq,n_len);
            if ( A->cdna_cache)A->cdna_cache=new_realloc_int  ( A->cdna_cache, n_nseq,n_len);
      

            A->score_seq    =vrealloc( A->score_seq, sizeof (int)*(n_nseq));
            for ( a=nseq; a< n_nseq; a++)A->score_seq[a]=0;
            
            
                     
          }   
      return A;
      }

Sequence* free_aln ( Alignment *LA){ return free_Alignment(LA);}
Sequence* free_Alignment ( Alignment *LA)
      {
        /* Does not free the A->S field (sequences of A)*/
      
        Sequence *S;
              
        if ( LA==NULL){return NULL;}
        
        S=LA->S;
        free_char ( LA->file, -1);
        free_char ( LA->seq_al, -1);
        free_int  ( LA->seq_cache, -1);
        free_int  ( LA->cdna_cache, -1);
        free_char ( LA->name,-1);
        
        free_char ( LA->tree_order,-1);   
        free_char ( LA->seq_comment, -1);
        free_char ( LA->aln_comment, -1);
        
        free_int  ( LA->order, -1);
        
        vfree ( LA->score_seq);
        vfree ( LA->len);
        free_profile (LA->P);
        if ( LA->A){free_Alignment (LA->A);LA->A=NULL;}
        

        vfree ( LA);        
        return S;
      }

Profile   *copy_profile   (Profile *P1)
{

  Profile *P;
  
  if ( !P1) return NULL;
  P=declare_profile ( P1->alphabet, P1->max_len);
  P->count=copy_int (P1->count, P->count, -1, -1);
  P->count2=copy_int (P1->count2, P->count2, -1, -1);
  P->count3=copy_int (P1->count3, P->count3, -1, -1);
  
  return P;
  
}
  
  
Profile   *declare_profile(char *alphabet, int len)
{
  Profile *P;
  P=vcalloc ( 1, sizeof ( Profile));
  P->alp_size=strlen(alphabet);
  P->max_len=len;
  P->alphabet=vcalloc ( strlen (alphabet)+2, sizeof (char));
  sprintf ( P->alphabet, "%s", alphabet);
  
  P->count=declare_int( P->alp_size+2, len);
  P->count2=declare_int(100, len);
  P->count3=declare_int(100, len);
  
  return P;
}
Profile * free_profile ( Profile *P)
{
  if (!P) return NULL;
  else
    {
      vfree (P->alphabet);
      free_int ( P->count, -1);
      free_int ( P->count2, -1);
      vfree (P);
    }
  return NULL;
}


/************************************************************************/
/*                                                                      */
/*             ALLOCATION                                               */
/*                                                                      */
/*                                                                      */
/************************************************************************/   


double alloc_mem;
double max_mem;
double tot_mem;
Memcontrol *memlast;

FILE* print_mem_usage (FILE *fp, char *comment)
{
  fprintf ( fp, "\n%s Memory Usage: Current= %.3f Mb, Max= %.3f Mb\n", comment,(float)((float)alloc_mem/(1024*1024)),(float)((float)tot_mem/(1024*1024)) );
  return fp;
}
void set_max_mem (int m)
{
  max_mem=m*1024*1024;
}

int verify_memory (int s)
{
  alloc_mem+=s;

  tot_mem=(alloc_mem>tot_mem)?alloc_mem:tot_mem;

  if (max_mem && alloc_mem>max_mem)
    {
      fprintf (stderr, "\n%s Requires Too Much Memory: %d Megabytes [FATAL:%s]\n", PROGRAM,(int)(alloc_mem/1024*1024),PROGRAM);
      fprintf (stderr, "Tip: Rerun your Job with a smaller dataset\n");
      
      exit (EXIT_FAILURE);
    }
  else
    return 1;
  return 0;
  
}

int my_assert ( void *p, int index)
{
  static int warning;

  if (!warning)
    {
      fprintf ( stderr, "\n****************************************************************\n");
      fprintf ( stderr, "\n          DEBUG MODE [Rebuild For Better Performances]          \n");
      fprintf ( stderr, "\n*****************************************************************\n");
      warning=1;
    }

  if ( !is_dynamic_memory(p)) return 1;
  else if ( read_array_size_new (p)<=index)
    {
      fprintf ( stderr, "\nFaulty Allocation: Size=%d Access=%d\n", read_array_size (p,0),index);
      return 0;
    }
  else
    {
      return 1;
    }
}
  
  

void * vmalloc ( size_t size)
      {
      void * x;
      Memcontrol *M;    

      verify_memory (size+2*sizeof (Memcontrol));
      
      if ( size==0)
          return NULL; /*crash ("\n0 bytes in vmalloc\n");*/
      else
          {
          x= malloc (size + 2*sizeof (Memcontrol));
          
          if ( x==NULL)
            {
            crash ( "\nFAILED TO ALLOCATE REQUIRED MEMORY (vmalloc)\n");
            return NULL;
            }
          else
            {
            M=x;
            M[0].size=size;
            M[0].size_element=0;
            sprintf ( M[0].check, "dy");
            M+=2;
            x=M;
            return x;
            }
          }
      }



void *vcalloc (size_t nobj, size_t size)
{
  return sub_vcalloc (nobj,size, MEMSET0);
}
void *vcalloc_nomemset ( size_t nobj, size_t size)
{
  return sub_vcalloc (nobj, size, NO_MEMSET0);
}
void *sub_vcalloc ( size_t nobj, size_t size, int MODE)
      {
      void *x;
      Memcontrol *M;
      static int set, unset, count;
      if ( nobj<=0 || size<=0)return NULL;/*crash ("\n0 bytes in vmalloc\n");*/
      else x=vmalloc (nobj*size);


      M=x;M-=2;M[0].size_element=size;M+=2;x=M;

      if ( x==NULL)
            {
            crash ( "\nFAILED TO ALLOCATE REQUIRED MEMORY (vcalloc)\n");
            return NULL;
            }
      else
        {
          if ( MODE==MEMSET0)
            {
            x=memset (x,0, nobj*size);
            }
          else 
            {
            if (nobj)x=memset (x, 0, size);
            }
          return x;
        }
      }

void *vrealloc ( void *p, size_t size)
      {
      void *x;
      Memcontrol *M;
      size_t i_size;
      int a;
      
      
      if ( p==NULL)
        {
          x=vmalloc (size);
          memset (x, 0, size);

          return x;
        }
      else
        {
          M=p;
          M-=2;
          i_size=M[0].size;
          p=M;
      
          
          if ( size<=0){return NULL;vfree (p);return NULL;}
          else
            {
            verify_memory (size - i_size);
            x=realloc ( p, size+2*sizeof(Memcontrol));
                        
            if ( x==NULL){crash ( "\nFAILED TO ALLOCATE REQUIRED MEMORY (realloc)\n");return NULL;}
            M=x;
            M[0].size=size;
            M+=2;
            x=M;
            for ( a=i_size; a< size; a++)((char*)x)[a]=0;
            return x;
            }
        }
      return NULL;
      }
void vfree ( void *p)
     {
       Memcontrol *M;
       size_t size;

       if ( !p)return;
       else
       {
         M=p;
         M-=2;
         size=M[0].size;
         
         p=M;
         free(p);
         
         verify_memory (-(size+2*sizeof(Memcontrol)));
       }
     }
void vfree_all (void *p)
{
  Memcontrol *n;
  while (memlast)
    {
      n=memlast->p;
      vfree (memlast+2);
      memlast=n;
    }
}
/*********************************************************************/
/*                                                                   */
/*                          SIZES                                    */
/*                                                                   */
/*                                                                   */
/*********************************************************************/
#define WRITE_SIZE(type,function)\
void function ( int x, type *array, int os)\
     {\
     fprintf(stderr, "\nwrite_size is a deprecated function [Warning:%s]\n", PROGRAM);return;\
     }
WRITE_SIZE(short,write_size_short)
WRITE_SIZE(char,write_size_char)
WRITE_SIZE(int,write_size_int)
WRITE_SIZE(float,write_size_float)
WRITE_SIZE(double,write_size_double)

#define READ_ARRAY_SIZE(type, function)\
int function (void *array, size_t size)\
    {\
      return read_array_size (array, size);\
    }
READ_ARRAY_SIZE(short,read_size_short)
READ_ARRAY_SIZE(char,read_size_char)
READ_ARRAY_SIZE(int,read_size_int)
READ_ARRAY_SIZE(float,read_size_float)
READ_ARRAY_SIZE(double,read_size_double)


int read_array_size_new (void *array)
{
  return read_array_size ( array, 0);
}
int read_array_size (void *array, size_t size)
    {
    Memcontrol *p;
    if (array==NULL)return 0;
    p=(Memcontrol *)array;
    p-=2;
    if ( p[0].size_element ==0 && size==0)
      {
      fprintf ( stderr, "\nERROR in read_array_size: trying to read the size of a malloced block");
      }
    else if ( size ==0) return (int)p[0].size/p[0].size_element;
    
    return (int)p[0].size/size;
    
    }      
int is_dynamic_memory ( void *array)
{
  Memcontrol *p;
  if (array==NULL)return 0;
  p=(Memcontrol *)array;
  p-=2;
  if ( strm (p[0].check, "dy"))return 1;
  return 0;
}

/************************************************************************/
/*                                                                      */
/*             DECLARE 2d ARRAYS                                        */
/*                                                                      */
/*                                                                      */
/************************************************************************/   

void * free_arrayN(void *p, int n)
{
  int a, s;
  void **i;

  
  if ( p==NULL) return NULL;
  else if ( n==1)vfree ((void *)p); 
  else
    {
      i=(void**)p;
      s=read_array_size ( (void *)p, sizeof ( void *));
      for ( a=0; a< s; a++)free_arrayN ((void *)i[a], n-1);
      vfree (p);
    }
  return NULL;
}
      
void * declare_arrayNnomemset (int ndim, size_t size, ...)
{
  va_list ap;
  int *array;
  void **p;
  int a;

  va_start (ap, size);
  
  array=vcalloc (ndim, sizeof (int));
  for ( a=0; a< ndim; a++)
    {
      array[a]=va_arg (ap,int);
      if ( array[a]<0){va_end(ap);return NULL;}
      
    }
  va_end (ap);

  if ( ndim==2)
    {
      int t;
      p=vcalloc_nomemset (array[0], sizeof ( void*));
      for (a=0; a< array[0]; a++)
      p[a]=vcalloc_nomemset (array[1], size);
    }
  else
    {
      p=declare_arrayN2nomemset (ndim, array, size);
    }
  vfree (array);
  return p;
}

void *declare_arrayN2nomemset ( int ndim, int *A, size_t size)
{
  int a;
  void **p;
    
  if ( ndim>1)
    {
      p=vcalloc_nomemset (A[0], sizeof (void*));
      for ( a=0; a<A[0]; a++)
      p[a]=declare_arrayN2(ndim-1, A+1, size);
    }
  else
    {
      p=vcalloc_nomemset (A[0], size);
    }
  return p;
}

void * declare_arrayN (int ndim, size_t size, ...)
{
  va_list ap;
  int *array;
  void **p;
  int a;


  va_start (ap, size);
  
  array=vcalloc (ndim, sizeof (int));
  for ( a=0; a< ndim; a++)
    {
      array[a]=va_arg (ap,int);
      if ( array[a]<0){va_end(ap);return NULL;}
      
    }
  va_end (ap);

  if ( ndim==2)
    {
      int t;
      p=vcalloc_nomemset (array[0], sizeof ( void*));
      for (a=0; a< array[0]; a++)
      p[a]=vcalloc (array[1], size);
    }
  else
    {
      p=declare_arrayN2 (ndim, array, size);
    }
  vfree (array);
  return p;
}

void *declare_arrayN2 ( int ndim, int *A, size_t size)
{
  int a;
  void **p;
    
  if ( ndim>1)
    {
      p=vcalloc_nomemset (A[0], sizeof (void*));
      for ( a=0; a<A[0]; a++)
      p[a]=declare_arrayN2(ndim-1, A+1, size);
    }
  else
    {
      p=vcalloc (A[0], size);
    }
  return p;
}

void **declare_array (int first, int second, size_t size)
{
  return (void **)declare_arrayN (2,size,first, second);
}
    

#define DECLARE_ARRAY(type,wf,rf,function)\
type**  function (int first, int second)\
  {\
    return (type **)declare_arrayN (2,sizeof(type), first, second);\
   }
DECLARE_ARRAY(short,write_size_short,read_size_short,declare_short)
DECLARE_ARRAY(char,write_size_char,read_size_char,declare_char)
DECLARE_ARRAY(int,write_size_int,read_size_int,declare_int)
DECLARE_ARRAY(float,write_size_float,read_size_float,declare_float)
DECLARE_ARRAY(double,write_size_double,read_size_double,declare_double)

void **declare_array_nomemset (int first, int second, size_t size)
{
  return (void **)declare_arrayNnomemset (2,size,first, second);
}
#define DECLARE_ARRAY_NMS(type,wf,rf,function)\
type**  function (int first, int second)\
  {\
    return (type **)declare_arrayNnomemset (2,sizeof(type), first, second);\
   }
DECLARE_ARRAY_NMS(short,write_size_short,read_size_short,declare_short_nomemset)
DECLARE_ARRAY_NMS(char,write_size_char,read_size_char,declare_char_nomemset)
DECLARE_ARRAY_NMS(int,write_size_int,read_size_int,declare_int_nomemset)
DECLARE_ARRAY_NMS(float,write_size_float,read_size_float,declare_float_nomemset)
DECLARE_ARRAY_NMS(double,write_size_double,read_size_double,declare_double_nomemset)


Alignment ** declare_aln_array ( int first)
    {
    Alignment ** array;
    int a;
    
    
    array=vcalloc (first, sizeof (Alignment*));
    for ( a=0; a< first; a++)
      array[a]=declare_Alignment (NULL);
    return array;
    }




/************************************************************************/
/*                                                                      */
/*             Realloc 2d ARRAYS                                        */
/*                                                                      */
/*                                                                      */
/************************************************************************/   
void ** realloc_arrayN(int ndim,void **main_array,size_t size, ...)
{
  va_list ap;
  int *array;
  void **p;
  int a;

  /*dim size==-1: keep current size*/
  /*Dim sizes are the absolute size (not the extension*/
  /*If array getting shorter, memory is Not Claimed back*/
  
  array=vcalloc (ndim, sizeof (int));
  va_start (ap, size);
  for ( a=0; a< ndim; a++)
    {
      array[a]=va_arg (ap,int);
      if ( array[a]<-1){va_end(ap);return NULL;}
    }
  va_end (ap);
  
  p=realloc_arrayN2 (ndim, main_array, array,size);
  vfree (array);
  return p;
}

void **realloc_arrayN2 ( int ndim, void ** p, int *A, size_t size)
{
  int a;
  int o, n;
  
  
  if (ndim==0)return NULL;

  if ( ndim>1)
    {
      o=read_array_size (p,sizeof (void*));
      if (A[0]>o)p=vrealloc (p, sizeof (void*)*A[0]);
      n=(A[0]==-1)?o:A[0];
      for ( a=0; a<n; a++)
      p[a]=realloc_arrayN2(ndim-1,p[a], A+1, size);
    }
  else
    {
      o=read_array_size (p, size);
      if (A[0]>o)p=vrealloc (p, size*A[0]);
    }
  return p;
}


void ** realloc_array (void **array,size_t size, int first, int second, int ext1, int ext2)
{
  int a;
  if ( array==NULL)return declare_array (((first==-1)?0:first)+ext1, ((second==-1)?0:second)+ext2, size);
  else if ( first==-1)
    {
      first=read_array_size (array, sizeof (void *));
    }
  for ( a=(first+ext1); a<first; a++)vfree (array[a]);
  
  
  array=vrealloc (array, sizeof (void*)*(ext1+first));
  if (second==-1)second=read_array_size(array[0], size);
  for (a=0; a< (first+ext1); a++)
    array[a]=vrealloc ( array[a], size*(second+ext2));
  return array;
}

  

#define REALLOC_ARRAY(type,wf,rf,function1,function2,function3)\
type ** function1 ( type **array, int first, int second, int ext1, int ext2)\
    {\
return (type **)realloc_array ((void **)array, sizeof (type),first, second, ext1, ext2);\
\
    }
REALLOC_ARRAY(short,write_size_short,read_size_short,realloc_short,declare_short,free_short)
REALLOC_ARRAY(char,write_size_char,read_size_char,realloc_char,declare_char,free_char)
REALLOC_ARRAY(int,write_size_int,read_size_int,realloc_int,declare_int,free_int)
REALLOC_ARRAY(float,write_size_float,read_size_float,realloc_float,declare_float,free_float)
REALLOC_ARRAY(double,write_size_double,read_size_double,realloc_double,declare_double,free_double)



#define NEW_REALLOC_ARRAY(type,wf,rf,function1,function2,function3)\
type ** function1 ( type **array, int ext1, int ext2)\
    {int a, b;\
     int first, l1;\
     int second, l2;\
     type **new_array;\
\
     first=rf(array,sizeof (type*));\
     second=rf(array[0],sizeof (type));\
     \
     if ( ext1==-1)ext1=first;\
     if ( ext2==-1)ext2=second;\
     l1=MIN(ext1, first);\
     l2=MIN(ext2, second);\
     new_array=declare_arrayN(2,sizeof(type),ext1, ext2);\
     for ( a=0; a<l1; a++)\
       for ( b=0; b<l2; b++)new_array[a][b]=array[a][b];\
     function3(array, -1);\
     return new_array;\
    }
NEW_REALLOC_ARRAY(short,write_size_short,read_size_short,new_realloc_short,declare_short,free_short)
NEW_REALLOC_ARRAY(char,write_size_char,read_size_char,new_realloc_char,declare_char,free_char)
NEW_REALLOC_ARRAY(int,write_size_int,read_size_int,new_realloc_int,declare_int,free_int)
NEW_REALLOC_ARRAY(float,write_size_float,read_size_float,new_realloc_float,declare_float,free_float)
NEW_REALLOC_ARRAY(double,write_size_double,read_size_double,new_realloc_double,declare_double,free_double)
Alignment ** realloc_aln_array ( Alignment **array, int ext1)
    {
    int a;
    int first;
    
    
    if ( array==NULL)
       {     
       array=declare_aln_array(ext1);     
       return array;
       }
     first=read_array_size ( array, sizeof ( Alignment *));
     if ( ext1>0)
      {
      array=vrealloc ( array, (sizeof (Alignment*))*(first+ext1));
        for ( a=first; a<first+ext1; a++)array[a]=declare_Alignment (NULL);
      }
    else if ( ext1==0);
    else if ( ext1<0)
         {
       for ( a=first-1; a>=(first+ext1);a--)free_Alignment (array[a]);
       array=vrealloc ( array, (sizeof (Alignment*))*(first+ext1));
       }
     return array;    
    }

/************************************************************************/
/*                                                                      */
/*            free 2d ARRAYS                                        */
/*                                                                      */
/*                                                                      */
/************************************************************************/   
#define FREE_ARRAY(type,wf,rf,function) \
type ** function (type **array, int first)\
    {\
      return free_arrayN((void*)array, 2);\
    }
FREE_ARRAY(short,write_size_short,read_size_short,free_short)
FREE_ARRAY(char,write_size_char,read_size_char,free_char)
FREE_ARRAY(int,write_size_int,read_size_int,free_int)
FREE_ARRAY(float,write_size_float,read_size_float,free_float)
FREE_ARRAY(double,write_size_double,read_size_double,free_double)



Alignment ** free_aln_array (Alignment **array)
   {
   int a;
   int len;


   if ( array==NULL)return NULL;
   len=read_array_size ( array, sizeof (Alignment *));
   for ( a=1; a< len; a++)free_Alignment(array[a]);
   vfree ( array);
   return  NULL;
   }

Fname *declare_fname ()
   {
   Fname *F;

   F=vcalloc ( 1, sizeof (Fname));
   F->name  =vcalloc ( FILENAMELEN, sizeof (char));
   F->path  =vcalloc ( FILENAMELEN, sizeof (char));
   F->suffix=vcalloc ( FILENAMELEN, sizeof (char));
   return F;
   }

Fname *free_fname ( Fname *F)
   {
   vfree (F->name);
   vfree (F->path);
   vfree (F->suffix);
   return NULL;
   }
/*********************************COPYRIGHT NOTICE**********************************/
/* Centre National de la Recherche Scientifique (CNRS) */
/*and */
/*Cedric Notredame */
/*Tue May 10 12:08:44     2005. */
/*All rights reserved.*/
/*This file is part of T-COFFEE.*/
/**/
/*    T-COFFEE is free software; you can redistribute it and/or modify*/
/*    it under the terms of the GNU General Public License as published by*/
/*    the Free Software Foundation; either version 2 of the License, or*/
/*    (at your option) any later version.*/
/**/
/*    T-COFFEE is distributed in the hope that it will be useful,*/
/*    but WITHOUT ANY WARRANTY; without even the implied warranty of*/
/*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the*/
/*    GNU General Public License for more details.*/
/**/
/*    You should have received a copy of the GNU General Public License*/
/*    along with Foobar; if not, write to the Free Software*/
/*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA*/
/*...............................................                                                                                      |*/
/*  If you need some more information*/
/*  cedric.notredame@europe.com*/
/*...............................................                                                                                                                                     |*/
/**/
/**/
/*    */
/*********************************COPYRIGHT NOTICE**********************************/

Generated by  Doxygen 1.6.0   Back to index