1 // DIR_4_TCOFFEE [def: ~/.t_coffee]: UNIQUE_DIR_4_TCOFFEE -> DIR_4_TCOFFEE ->HOME/.t_coffee
2 //TMP_4_TCOFFEE [def: ~/.t_coffee/tmp]:: UNIQUE_DIR_4_TCOFFEE -> TMP_4_TCOFFEE ->DIR_4_TCOFFEE/tmp
18 #include "io_lib_header.h"
19 #include "util_lib_header.h"
20 #include "define_header.h"
21 #include "perl_header_lib.h"
23 //defined this way because all compilers cannot pas ap
24 //safe printf: it declares buf to the proper size
25 #define cvsprintf(buf,string)\
31 va_start (ap,string); \
32 n=vsnprintf (buf2,1, string, ap)+3; \
34 va_start (ap, string); \
35 buf=vcalloc (n, sizeof (char)); \
36 vsnprintf (buf, n,string, ap); \
39 int my_vsscanf(char *buf, char *fmt, va_list parms);
40 static int get_vtmpnam2_root();
45 static int global_exit_signal;
46 static int no_error_report;
47 static int clean_exit_started;
48 static int debug_lock;
51 /*********************************************************************/
56 /*********************************************************************/
57 double dichotomy (double value, double target_value, double middle, double *bottom,double *top)
59 if ( value> target_value)top[0]=middle;
60 else if ( value<target_value)bottom[0]=middle;
61 return (top[0]-bottom[0])/2+bottom[0];
65 /*********************************************************************/
70 /*********************************************************************/
72 #define MAXSTACK (sizeof(size_t) * CHAR_BIT)
73 static void exchange(void *a, void *b, size_t size) {
85 for (i = sizeof(int); i <= size; i += sizeof(int)) {
94 for (i = i - sizeof(int) + 1; i <= size; i++) {
105 void qsort(void *base, size_t nmemb, size_t size,
106 int (*compar)(const void *, const void *)) {
107 void *lbStack[MAXSTACK], *ubStack[MAXSTACK];
111 /********************
113 ********************/
115 lbStack[0] = (char *)base;
116 ubStack[0] = (char *)base + (nmemb-1)*size;
117 for (sp = 0; sp >= 0; sp--) {
126 /* select pivot and exchange with 1st element */
127 offset = (ub - lb) >> 1;
128 P = lb + offset - offset % size;
129 exchange (lb, P, size);
131 /* partition into two segments */
135 while (i < j && compar(lb, i) > 0) i += size;
136 while (j >= i && compar(j, lb) > 0) j -= size;
138 exchange (i, j, size);
143 /* pivot belongs in A[j] */
144 exchange (lb, j, size);
147 /* keep processing smallest segment, and stack largest */
148 if (m - lb <= ub - m) {
150 lbStack[sp] = m + size;
157 ubStack[sp++] = m - size;
165 int pstrcmp(char *p1, char *p2);
169 /*********************************************************************/
174 /*********************************************************************/
175 FILE * hsort_file ( FILE *fp,int n,int len, size_t size,int first_comp_field, int n_comp_fields,int (*compare)(const void *, const void*,int, int, size_t),void * (*copy)(void *,void*, size_t))
177 unsigned long i, ir, j, l;
178 void *rra, *rrb, *ra_j, *ra_j_1;
186 rra =vcalloc ( len, size);
187 rrb =vcalloc ( len, size);
188 ra_j =vcalloc ( len, size);
189 ra_j_1=vcalloc ( len, size);
200 fseek( fp, start+(((l-1)*len)*size), SEEK_SET);
201 fread( rra, size, len,fp); /*rra=ra[--l]*/
205 fseek( fp, start+((ir-1)*len*size), SEEK_SET);
206 fread( rra, size, len,fp); /*rra=ra[ir]*/
208 fseek( fp, start, SEEK_SET);
209 fread( rrb, size, len,fp); /*rrb=ra[0]*/
211 fseek( fp, start+((ir-1)*len*size), SEEK_SET);
212 fwrite(rrb,size, len, fp); /*ra[ir]=rrb=ra[0]*/
216 fseek ( fp,start, SEEK_SET);
217 fwrite(rra,size, len, fp); /*ra[0]=rra*/
225 fseek ( fp, start+((j-1)*len*size), SEEK_SET);
226 fread (ra_j, size, len, fp);
230 fseek ( fp, start+(((j-1)+1)*len*size), SEEK_SET);
231 fread (ra_j_1, size, len, fp);
234 if ( j<ir && compare( ra_j, ra_j_1,first_comp_field,n_comp_fields, size )<0)
236 SWAPP(ra_j, ra_j_1, tp);
240 if (compare(rra, ra_j, first_comp_field,n_comp_fields, size)<0)
242 fseek ( fp, start+((i-1)*len*size), SEEK_SET);
243 fwrite(ra_j,size, len, fp);
251 fseek ( fp, start+((i-1)*len*size), SEEK_SET);
252 fwrite(rra,size, len, fp);
261 void ** hsort_array ( void **ra,int n,int len, size_t size,int first_comp_field, int n_comp_fields,int (*compare)(const void *, const void*,int, int, size_t),void * (*copy)(void *,void*, size_t))
263 unsigned long i, ir, j, l;
274 rra =vcalloc ( len, size);
285 copy ( rra, ra[--l],len);
289 copy ( rra, ra[ir],len);
290 copy ( ra[ir], ra[FE], len);
293 copy ( ra[FE],rra,len);
301 if ( j<ir && compare( ra[j], ra[j+1],first_comp_field,n_comp_fields, size )<0)j++;
302 if (compare(rra, ra[j], first_comp_field,n_comp_fields, size)<0)
303 {copy(ra[i], ra[j],len);i=j;j<<= 1;}
307 copy( ra[i], rra,len);
314 /*********************************************************************/
319 /*********************************************************************/
320 void * bsearch_file ( const void *key,int *p,int comp_first,int comp_len, FILE *fp,int len, int entry_len,size_t el_size, int (*compare)(const void *, const void*,int, int, size_t))
322 int upper, lower, c, i;
325 static long key2_size;
332 if ( key2==NULL){key2=vcalloc (entry_len, el_size);key2_size=entry_len* el_size;}
333 else if (key2_size< (entry_len* el_size)){vfree(key2);key2=vcalloc (entry_len, el_size);key2_size=entry_len* el_size;}
335 while ((lower-upper)>1)
337 i=(lower+upper) >> 1;
339 fseek ( fp,start+(i*el_size*entry_len), SEEK_SET);
340 fread ( key2, el_size, entry_len,fp);
341 c=compare(key2,key, comp_first, comp_len,el_size);
343 if ( c==0){p[0]=i;return key2;}
344 else if ( c< 0)upper=i;
345 else if ( c> 0)lower=i;
350 void * bsearch_array ( const void *key,int *p,int comp_first, int comp_len,void**list,int len, int entry_len,size_t el_size, int (*compare)(const void *, const void*,int, int, size_t))
352 int upper, lower, c, i;
357 while ((lower-upper)>1)
361 c=compare(key2,key, comp_first,comp_len,el_size);
363 if ( c==0){p[0]=i;return key2;}
364 else if ( c< 0)upper=i;
365 else if ( c> 0)lower=i;
370 /**********************************************************************/
372 /* HSORT/BSEARCH WRAPPERS */
375 /**********************************************************************/
376 void **search_in_list_file ( void *key, int *p,int comp_len,FILE *fp, int len, size_t size, int entry_len)
381 if ( l==NULL)l=vcalloc ( 1, sizeof (int*));
383 l[0]=bsearch_file (key,p,0,comp_len,fp,len,entry_len,size,hsort_cmp);
384 if (l[0]==NULL)return NULL;
387 void **search_in_list_array ( void *key,int *p, int comp_len,void **L, int len, size_t size, int entry_len)
391 if ( l==NULL)l=vcalloc ( 1, sizeof (int*));
393 l[0]=bsearch_array (key,p,0,comp_len,L,len,entry_len,size,hsort_cmp);
394 if (l[0]==NULL)return NULL;
397 void **hsort_list_array ( void **L, int len, size_t size, int entry_len, int first_comp_field, int n_comp_fields)
399 return hsort_array (L, len,entry_len, size,first_comp_field, n_comp_fields,hsort_cmp , hsort_cpy);
401 FILE *hsort_list_file ( FILE*fp , int len, size_t size, int entry_len, int first_comp_field, int n_comp_fields)
404 return hsort_file (fp, len,entry_len, size,first_comp_field, n_comp_fields,hsort_cmp , hsort_cpy);
407 int hsort_cmp ( const void *a, const void *b, int first, int clen, size_t size)
415 for ( p=first; p<clen+first; p++)
417 if ( ax[p]<bx[p])return -1;
418 else if ( ax[p]==bx[p]);
423 void *hsort_cpy(void*to, void *from, size_t size)
431 for (p=0; p<(int)size; p++)
440 void test_hsort_list_array()
446 array=declare_int(n, 3);
447 for ( a=0; a<n; a++)array[a][0]=a;
449 hsort_list_array( (void**)array,n, sizeof (int), 3, 0, 1);
450 for ( a=0; a<n; a++)fprintf ( stderr, "\n%d %d", array[a][0],a);
451 myexit(EXIT_FAILURE);
455 /*********************************************************************/
457 /* B_SEARCH_FILE FUNCTIONS */
460 /*********************************************************************/
462 /*********************************************************************/
464 /* SORT/COMPARE/SEARCH FUNCTIONS */
467 /*********************************************************************/
468 static int sort_field;
469 int **search_in_list_int ( int *key, int k_len, int **list, int ne)
473 l=bsearch (&key,list, ne, sizeof(int**),(int(*)(const void*,const void*))(cmp_list_int));
476 void sort_float ( float **V,int N_F, int F, int left, int right)
479 qsort ( V, right+1, sizeof(int**),(int(*)(const void*,const void*))(cmp_int));
481 int cmp_float ( const float **a, const float **b)
483 if ( a[0][sort_field]< b[0][sort_field])return-1;
484 else if ( a[0][sort_field]==b[0][sort_field])return 0;
488 void sort_int_1D ( int *L, int n)
493 array=declare_int ( n, 1);
494 for ( a=0; a< n; a++)
496 sort_int ( array, 1, 0, 0, n-1);
497 for ( a=0; a< n; a++)
499 free_int ( array, n);
502 char** sort_string_array (char **V, int n)
506 qsort ( V,n,sizeof (char*),(int(*)(const void*,const void*))(pstrcmp));
512 int pstrcmp(char *p1, char *p2)
514 return strcmp(*(char **)p1, *(char **)p2);
516 void sort_int ( int **V,int N_F, int F, int left, int right)
520 qsort ( V, (right-left)+1, sizeof(int**),(int(*)(const void*,const void*))(cmp_int));
522 void sort_list_int ( int **V,int N_F, int F, int left, int right)
526 qsort ( V, (right-left)+1, sizeof(int**),(int(*)(const void*,const void*))(cmp_list_int));
529 void sort_list_int2 ( int **V,int *list,int N_F, int left, int right)
531 // just like sort_int_list, but uses list to to order the comparison of the keys
535 qsort ( V, (right-left)+1, sizeof(int**),(int(*)(const void*,const void*))(cmp_list_int2));
537 int cmp_list_int2 (const int**a, const int**b)
544 while ((c=order[p])!=-1)
547 if ( a[0][c]>b[0][c])return 1;
548 else if ( a[0][c]<b[0][c])return -1;
553 void sort_int_inv ( int **V,int N_F, int F, int left, int right)
559 qsort ( V, (right-left)+1, sizeof(int**),(int(*)(const void*,const void*))(cmp_int));
561 list=declare_int ((right-left)+1, N_F);
562 for ( a=left; a< (right-left)+1; a++)
564 for ( b=0; b< N_F; b++)
566 list[a-left][b]=V[a][b];
569 for ( a=left; a< (right-left)+1; a++)
571 for ( b=0; b< N_F; b++)
572 V[a][b]=list[(right-left)-a][b];
577 void sort_list_int_inv ( int **V,int N_F, int F, int left, int right)
583 qsort ( V, (right-left)+1, sizeof(int**),(int(*)(const void*,const void*))(cmp_list_int));
585 list=declare_int ((right-left)+1, N_F);
586 for ( a=left; a< (right-left)+1; a++)
588 for ( b=0; b< N_F; b++)
590 list[a-left][b]=V[a][b];
593 for ( a=left; a< (right-left)+1; a++)
595 for ( b=0; b< N_F; b++)
596 V[a][b]=list[(right-left)-a][b];
603 int cmp_int ( const int**a, const int**b)
605 if ( a[0][sort_field]< b[0][sort_field])return-1;
606 else if ( a[0][sort_field]==b[0][sort_field])return 0;
609 int cmp_list_int (const int**a, const int**b)
615 for ( c=0; c<=sort_field; c++)
617 if ( a[0][c]==UNDEFINED|| b[0][c]==UNDEFINED)ret=0;
618 else if ( a[0][c]>b[0][c])return 1;
619 else if ( a[0][c]<b[0][c])return -1;
621 if (undef==sort_field)
623 if (a[0][0]==b[0][0])return 0;
629 int name_is_in_list ( char *name, char **name_list, int n_name, int len)
633 /*Note: RETURNS THE Offset of the LAST Occurence of name in name_list*/
635 if ( name_list==NULL || name ==NULL)return -1;
638 for ( a=0; a< n_name; a++)
640 if ( name_list[a]==NULL);
643 if (strncmp ( name, name_list[a], len)==0)pos=a;
645 else if ( strm ( name, name_list[a]))pos=a;
649 char * check_list_for_dup ( char **list, int ne)
653 for ( a=0; a< ne-1; a++)
654 for ( b=a+1; b< ne; b++)if (strm ( list[a], list[b]))return list[a];
657 FILE *get_number_list_in_file ( FILE *fp, int *list, int *n, int *max_len)
662 while ( isspace((c=fgetc (fp))));
666 while ( isspace((c=fgetc (fp))) && c!='\n');
671 if ( n[0]>=max_len[0])
672 list=vrealloc ( list, (n[0]+100)*sizeof (int));
673 max_len[0]=(n[0]+100);
675 fscanf ( fp, "%d",&list[n[0]++]);
680 /*********************************************************************/
685 /*********************************************************************/
686 int quantile (int argc, char *argv[])
691 char *** string_list;
692 char *name, s1[1000], s2[1000];
698 fprintf (stderr, "\nquantile <fname> <quant: 0.00-1.00> [<top | bottom>]");
699 fprintf (stderr, "\nSplits your data in two according to the quantile");
700 fprintf (stderr, "\nReturns the top quantile or the bottom quantile");
701 fprintf (stderr, "\nData must be in <fname> with two fields/line: Field1=index, Field2=value\n");
702 fprintf (stderr, "\n1 27\n2 46\n3 5\n...\n");
703 fprintf (stderr, "\nValue can either be integer or float");
706 myexit (EXIT_FAILURE);
709 if (strm (argv[1], "stdin"))
712 fp=vfopen (name, "w");
713 while ( (c=fgetc(stdin))!=EOF)
715 fprintf ( fp, "%c", c);
725 n=count_n_line_in_file (name);
726 list=declare_int (n, 2);
727 string_list=declare_arrayN(3,sizeof (char), n, 2, 10);
729 fp=vfopen (name, "r");
731 while ( (c=fgetc (fp))!=EOF)
734 fscanf ( fp, "%s %s\n", s1, s2);
735 list[n][0]=(int)(atof(s1)*1000);
736 list[n][1]=(int)(atof(s2)*1000);
739 sprintf (string_list[n][0],"%s",s1);
740 sprintf (string_list[n][1],"%s",s2);
745 sort_int_inv ( list,3, 1, 0, n-1);
746 t=quantile_rank ( list,1,n, atof (argv[2]));
747 if ( argc!=4 || (argc==4 && strm (argv[3], "bottom")))
750 fprintf ( stdout, "%s %s\n", string_list[list[a][2]][0], string_list[list[a][2]][1]);
755 fprintf ( stdout, "%s %s\n", string_list[list[a][2]][0], string_list[list[a][2]][1]);
758 fprintf (stderr, "\nQuantile %.2f T= %d Out of N= %d entries\n", atof (argv[2]), t, n),
764 int quantile_rank (int **list,int field, int n, float p)
770 else if ( p==0) nr=n;
774 double g, q, np, i_part;
775 l=vcalloc ( n, sizeof (int));
776 for (a=n-1, b=0; b<n; a--, b++)
779 np=(double)n*(double)p;
780 g=modf (np, &i_part);
784 q=(float)l[j]+g*((float)l[j+1]-(float)l[j]);
787 while (nr<n && list[nr][field]>=q)nr++;
792 /*********************************************************************/
797 /*********************************************************************/
798 short* ga_memcpy_short ( short *array1, short *array2, int n)
802 for ( a=0; a< n; a++)
807 int * ga_memcpy_int ( int *array1, int *array2, int n)
811 for ( a=0; a< n; a++)
817 float* ga_memcpy_float ( float *array1, float *array2, int n)
821 for ( a=0; a< n; a++)
826 double* ga_memcpy_double (double *array1, double*array2, int n)
830 for ( a=0; a< n; a++)
838 /*recycle: get the bottom pointer on the top of the heap*/
840 void ** recycle (void **A, int l, int cycle)
844 B=vcalloc (l, sizeof (void*));
846 for ( c=0; c< cycle; c++)
848 for ( a=1, b=0; a<l; a++, b++) B[b]=A[a];
850 for ( a=0; a<l; a++)A[a]=B[a];
856 /* Old READ/WRITE ARRAY SIZE*/
858 #define WRITE_SIZE(type,function)\
859 void function ( int x, type *array, int os)\
862 char buf[SIZE_OF_INT+1];\
863 array+=os*SIZE_OF_INT;\
864 for ( a=0;a<SIZE_OF_INT; a++)array[a]=0;\
865 sprintf ( buf, "%d", x);\
867 array+=SIZE_OF_INT-l;\
868 for (a=0; a<l; a++)array[a]=(type)buf[a];\
870 WRITE_SIZE(short,write_size_short)
871 WRITE_SIZE(char,write_size_char)
872 WRITE_SIZE(int,write_size_int)
873 WRITE_SIZE(float,write_size_float)
874 WRITE_SIZE(double,write_size_double)
876 #define READ_ARRAY_SIZE(type, function)\
877 int function (type *array, int os)\
880 char buf[SIZE_OF_INT+1];\
882 array+=os*SIZE_OF_INT;\
883 while ( a!=SIZE_OF_INT && array[a]==0)a++;\
884 while ( a!=SIZE_OF_INT)buf[b++]=(char)array[a++];\
888 READ_ARRAY_SIZE(short,read_size_short)
889 READ_ARRAY_SIZE(char,read_size_char)
890 READ_ARRAY_SIZE(int,read_size_int)
891 READ_ARRAY_SIZE(float,read_size_float)
892 READ_ARRAY_SIZE(double,read_size_double)
894 /*********************************************************************/
899 /*********************************************************************/
901 #define SET_NUMBERS(type,function)\
902 type * function(type *list,int n, ...)\
910 buf=vcalloc(n, sizeof(type));\
911 index=vcalloc(n, sizeof(int));\
913 for ( i=0; i< n; i++)\
915 buf[i] =va_arg(ap,type);\
916 index[i]=va_arg(ap, int);\
917 if (index[i]>max)max=index[i];\
920 if (list==NULL)list=vcalloc ( max+1, sizeof (type));\
921 for ( i=0; i<n; i++)list[index[i]]=buf[i];\
926 /*SET_NUMBERS(short ,set_short)*/
927 /*SET_NUMBERS(char ,set_char)*/
928 SET_NUMBERS(int ,set_int)
929 /*SET_NUMBERS(float ,set_float)*/
930 SET_NUMBERS(double,set_double)
932 short ** duplicate_short ( short **array , int len, int field)
934 return copy_short (array ,declare_short ( len, field), len, field);
936 int ** duplicate_int ( int **array , int len, int field)
938 return copy_int (array ,declare_int ( len, field), len, field);
940 char ** duplicate_char ( char **array , int len, int field)
942 return copy_char (array ,declare_char ( len, field), len, field);
944 char * duplicate_string ( char *string)
954 buf=vcalloc ( l+1, sizeof(char));
955 sprintf ( buf, "%s", string);
959 float ** duplicate_float ( float **array , int len, int field)
961 return copy_float (array ,declare_float ( len, field), len, field);
963 double ** duplicate_double ( double **array , int len, int field)
965 return copy_double (array ,declare_double ( len, field), len, field);
970 /*********************************************************************/
972 /* COPY OF 2D ARRAY */
975 /*********************************************************************/
976 short ** copy_short( short **array1, short **array2, int len, int number_field)
980 if ( len==-1)len=read_array_size (array1,sizeof (short*));
981 if ( number_field==-1)number_field=read_array_size (array1[0],sizeof (short));
982 if ( array2)free_short ( array2, -1);
983 array2=declare_short ( len, number_field);
985 for ( a=0; a< len; a++)
986 ga_memcpy_short( array1[a],array2[a],number_field);
990 char ** copy_char ( char **array1, char **array2, int len, int number_field)
994 if ( array1==NULL)return NULL;
995 if ( len==-1)len=read_size_char (array1,sizeof (char*));
996 if ( number_field==-1)
998 number_field=read_size_char (array1[0],sizeof(char));
999 for ( a=0; a< len; a++)
1000 number_field=MAX(number_field, strlen ( array1[a]))+1;
1003 if ( array2)free_char (array2, -1);
1004 array2=declare_char(len, number_field);
1006 for ( a=0; a< len; a++)
1007 sprintf ( array2[a], "%s", array1[a]);
1011 int ** copy_int ( int **array1, int **array2, int len, int number_field)
1015 if ( array1==NULL) return NULL;
1016 if ( len==-1)len=read_size_int (array1, sizeof (int*));
1017 if ( number_field==-1)number_field=read_size_int (array1[0],sizeof (int));
1021 if (array2)free_int (array2, -1);
1022 array2=declare_int ( len, number_field);
1024 for ( a=0; a< len; a++)
1025 ga_memcpy_int( array1[a],array2[a],number_field);
1030 float ** copy_float ( float **array1, float **array2, int len, int number_field)
1034 if ( array1==NULL) return NULL;
1035 if ( len==-1)len=read_size_float (array1,sizeof (float*));
1036 if ( number_field==-1)number_field=read_size_float (array1[0],sizeof (float));
1038 if ( array2)free_float (array2, -1);
1039 array2=declare_float ( len, number_field);
1041 for ( a=0; a< len; a++)
1042 ga_memcpy_float( array1[a],array2[a],number_field);
1045 double ** copy_double (double **array1, double **array2, int len, int number_field)
1049 if ( array1==NULL) return NULL;
1050 if ( len==-1)len=read_size_double (array1,sizeof (double*));
1051 if ( number_field==-1)number_field=read_size_double (array1[0], sizeof (double));
1052 if ( array2)free_double (array2, -1);
1053 array2=declare_double ( len, number_field);
1055 for ( a=0; a< len; a++)
1056 ga_memcpy_double( array1[a],array2[a],number_field);
1059 /*********************************************************************/
1064 /*********************************************************************/
1070 Alignment ** cat_aln_list ( Alignment **list_to_cat,int first, int end, Alignment **rec_list)
1075 if ( list_to_cat==NULL)return rec_list;
1078 rec_list_start=(rec_list[-1])->nseq;
1079 rec_list=realloc_aln_array ( rec_list, end-first);
1080 for ( a=first, b=rec_list_start; a<end; a++, b++)copy_aln (list_to_cat[a], rec_list[b]);
1081 free_aln_array ( list_to_cat);
1086 /*********************************************************************/
1088 /* NUMBER ARRAY ANALYSE */
1091 /*********************************************************************/
1093 int output_array_int (char* fname, int **array)
1099 fp=vfopen (fname, "w");
1100 len=read_array_size_new (array);
1101 fprintf (fp, "%d\n",len);
1102 for ( a=0; a<len; a++)
1104 nf=read_array_size_new(array[a]);
1105 fprintf (fp, "%d ",nf);
1106 for (b=0; b<nf; b++)fprintf (fp, "%5d ", array[a][b]);
1109 fprintf ( fp, "\n");
1111 return EXIT_SUCCESS;
1114 int **input_array_int (char *fname)
1120 fp=vfopen (fname, "r");
1121 fscanf (fp, "%d\n", &len);
1122 array=vcalloc (len, sizeof (int*));
1123 for (a=0; a<len; a++)
1125 fscanf(fp, "%d ", &len2);
1126 array[a]=vcalloc (len2, sizeof (int));
1127 for (b=0; b<len2; b++)
1128 fscanf(fp, "%d ", &array[a][b]);
1136 #define RETURN_MAX_COOR(type,wf,rf,function,comparison,undef)\
1137 type function ( type ** array, int len_array, int field, int *coor)\
1142 if (array==NULL || len_array==0)return 0;\
1145 len_array=rf(array,sizeof (type*));\
1146 max=array[0][field];\
1148 for ( a=1; a< len_array; a++)\
1149 if ( max==undef)max=array[a][field];\
1150 else if ( array[a][field]!=undef)\
1151 if (array[a][field] comparison max)\
1152 {max=array[a][field];\
1159 RETURN_MAX_COOR(short,write_size_short,read_size_short,return_max_coor_short,>, UNDEFINED_SHORT)
1160 RETURN_MAX_COOR(char,write_size_char,read_size_char,return_max_coor_char,>, UNDEFINED_CHAR)
1161 RETURN_MAX_COOR(int,write_size_int,read_size_int,return_max_coor_int,>, UNDEFINED_INT)
1162 RETURN_MAX_COOR(float,write_size_float,read_size_float,return_max_coor_float,>, UNDEFINED_FLOAT)
1163 RETURN_MAX_COOR(double,write_size_double,read_size_double,return_max_coor_double,>, UNDEFINED_DOUBLE)
1164 RETURN_MAX_COOR(short,write_size_short,read_size_short,return_min_coor_short,<, UNDEFINED_SHORT)
1165 RETURN_MAX_COOR(char,write_size_char,read_size_char,return_min_coor_char,<, UNDEFINED_CHAR)
1166 RETURN_MAX_COOR(int,write_size_int,read_size_int,return_min_coor_int,<, UNDEFINED_INT)
1167 RETURN_MAX_COOR(float,write_size_float,read_size_float,return_min_coor_float,<, UNDEFINED_FLOAT)
1168 RETURN_MAX_COOR(double,write_size_double,read_size_double,return_min_coor_double,<, UNDEFINED_DOUBLE)
1169 #define RETURN_MAX(type,wf,rf,function,comparison,undef)\
1170 type function ( type ** array, int len_array, int field)\
1175 if (array==NULL || len_array==0)return 0;\
1178 if (len_array==-1)len_array=rf(array,sizeof (type*));\
1179 max=array[0][field];\
1180 for ( a=1; a< len_array; a++)\
1181 if ( max==undef)max=array[a][field];\
1182 else if ( array[a][field]!=undef)max=( array[a][field] comparison max)?array[a][field]:max;\
1184 return (max==undef)?0:max;\
1187 RETURN_MAX(short,write_size_short,read_size_short,return_max_short,>,UNDEFINED_SHORT)
1188 RETURN_MAX(char,write_size_char,read_size_char,return_max_char,>,UNDEFINED_CHAR)
1189 RETURN_MAX(int,write_size_int,read_size_int,return_max_int,>,UNDEFINED_INT)
1190 RETURN_MAX(float,write_size_float,read_size_float,return_max_float,>,UNDEFINED_FLOAT)
1191 RETURN_MAX(double,write_size_double,read_size_double,return_max_double,>,UNDEFINED_DOUBLE)
1192 RETURN_MAX(short,write_size_short,read_size_short,return_min_short,<,UNDEFINED_SHORT)
1193 RETURN_MAX(char,write_size_char,read_size_char,return_min_char,<,UNDEFINED_CHAR)
1194 RETURN_MAX(int,write_size_int,read_size_int,return_min_int,<,UNDEFINED_INT)
1195 RETURN_MAX(float,write_size_float,read_size_float,return_min_float,<,UNDEFINED_FLOAT)
1196 RETURN_MAX(double,write_size_double,read_size_double,return_min_double,<,UNDEFINED_DOUBLE)
1200 #define RETURN_2DMAX(type,wf,rf,function,comparison,undef)\
1201 type function ( type ** array, int start, int len_array, int first_field, int number_field)\
1205 if (array==NULL || len_array==0 || first_field<0 || number_field==0)return 0;\
1207 {max=array[start][first_field];\
1208 for ( a=start; a< start+len_array; a++)\
1209 for (b=first_field; b< first_field+number_field; b++)\
1210 if (array[a][b]!=undef)max=( array[a][b] comparison max)?array[a][b]:max;\
1214 RETURN_2DMAX(short,write_size_short,read_size_short,return_2Dmax_short,>, UNDEFINED_SHORT)
1215 RETURN_2DMAX(char,write_size_char,read_size_char,return_2Dmax_char,>,UNDEFINED_CHAR)
1216 RETURN_2DMAX(int,write_size_int,read_size_int,return_2Dmax_int,>,UNDEFINED_INT)
1217 RETURN_2DMAX(float,write_size_float,read_size_float,return_2Dmax_float,>,UNDEFINED_FLOAT)
1218 RETURN_2DMAX(double,write_size_double,read_size_double,return_2Dmax_double,>,UNDEFINED_DOUBLE)
1219 RETURN_2DMAX(short,write_size_short,read_size_short,return_2Dmin_short,<,UNDEFINED_SHORT)
1220 RETURN_2DMAX(char,write_size_char,read_size_char,return_2Dmin_char,<,UNDEFINED_CHAR)
1221 RETURN_2DMAX(int,write_size_int,read_size_int,return_2Dmin_int,<,UNDEFINED_INT)
1222 RETURN_2DMAX(float,write_size_float,read_size_float,return_2Dmin_float,<,UNDEFINED_FLOAT)
1223 RETURN_2DMAX(double,write_size_double,read_size_double,return_2Dmin_double,<,UNDEFINED_DOUBLE)
1225 #define RETURN_2DMAX_COOR(type,wf,rf,function,compare,undef)\
1226 type function ( type **array, int start1 , int end1, int start2, int end2,int *i, int *j)\
1230 if ( start1==-1)start1=0;\
1231 if ( start2==-1)start2=0;\
1232 if ( end1==-1)end1=rf(array,sizeof (type*));\
1233 if ( end2==-1)end2=rf(array[0],sizeof (type));\
1234 if ( array==NULL || (end1-start1)==0 || (end1-start1)>rf ( array,sizeof (type*)) || (end2-start2)==0)\
1242 for ( a=start1; a<end1; a++)\
1243 for ( b=start2; b<end2; b++)\
1245 if ( max==undef && array[a][b]!=undef)max=array[a][b];\
1246 else if ( array[a][b]!=undef && (array[a][b] compare max))\
1255 RETURN_2DMAX_COOR(short,write_size_short,read_size_short,return_2Dmax_coor_short,>,UNDEFINED_SHORT)
1256 RETURN_2DMAX_COOR(char,write_size_char,read_size_char,return_2Dmax_coor_char,>,UNDEFINED_CHAR)
1257 RETURN_2DMAX_COOR(int,write_size_int,read_size_int,return_2Dmax_coor_int,>,UNDEFINED_INT)
1258 RETURN_2DMAX_COOR(float,write_size_float,read_size_float,return_2Dmax_coor_float,>,UNDEFINED_FLOAT)
1259 RETURN_2DMAX_COOR(double,write_size_double,read_size_double,return_2Dmax_coor_double,>,UNDEFINED_DOUBLE)
1260 RETURN_2DMAX_COOR(short,write_size_short,read_size_short,return_2Dmin_coor_short,<,UNDEFINED_SHORT)
1261 RETURN_2DMAX_COOR(char,write_size_char,read_size_char,return_2Dmin_coor_char,<,UNDEFINED_CHAR)
1262 RETURN_2DMAX_COOR(int,write_size_int,read_size_int,return_2Dmin_coor_int,<,UNDEFINED_INT)
1263 RETURN_2DMAX_COOR(float,write_size_float,read_size_float,return_2Dmin_coor_float,<,UNDEFINED_FLOAT)
1264 RETURN_2DMAX_COOR(double,write_size_double,read_size_double,return_2Dmin_coor_double,<,UNDEFINED_DOUBLE)
1266 #define RETURN_WMEAN(type,wf,rf,function,sum_function,undef)\
1267 double function ( type **array, int len, int wfield,int sfield)\
1271 if ( len==0 ||array==NULL || len>rf ( array,sizeof (type*)))return 0;\
1274 if ( len==-1)len=rf(array,sizeof (type*));\
1275 for ( b=0, c=0,a=0; a< len; a++)\
1277 if (array[a][sfield]!=undef && array[a][wfield]!=undef )\
1279 b+=array[a][sfield];\
1280 c+=array[a][wfield];\
1284 return (c==0)?0:(b/c);\
1286 RETURN_WMEAN(short,write_size_short,read_size_short,return_wmean_short, return_sum_short,UNDEFINED_SHORT)
1287 RETURN_WMEAN(char,write_size_char,read_size_char, return_wmean_char,return_sum_char,UNDEFINED_CHAR)
1288 RETURN_WMEAN(int,write_size_int,read_size_int,return_wmean_int,return_sum_int,UNDEFINED_INT)
1289 RETURN_WMEAN(float,write_size_float,read_size_float,return_wmean_float,return_sum_float,UNDEFINED_FLOAT)
1290 RETURN_WMEAN(double,write_size_double,read_size_double,return_wmean_double,return_sum_double,UNDEFINED_DOUBLE)
1293 #define RETURN_MEAN(type,wf,rf,function,sum_function,undef)\
1294 double function ( type **array, int len, int field)\
1298 if ( len==0 ||array==NULL || len>rf ( array,sizeof(type*)))return 0;\
1301 for ( b=0, c=0,a=0; a< len; a++)\
1303 if (array[a][field]!=undef)\
1305 b+=array[a][field];\
1310 return (c==0)?0:(b/c);\
1312 RETURN_MEAN(short,write_size_short,read_size_short,return_mean_short, return_sum_short,UNDEFINED_SHORT)
1313 RETURN_MEAN(char,write_size_char,read_size_char, return_mean_char,return_sum_char,UNDEFINED_CHAR)
1314 RETURN_MEAN(int,write_size_int,read_size_int,return_mean_int,return_sum_int,UNDEFINED_INT)
1315 RETURN_MEAN(float,write_size_float,read_size_float,return_mean_float,return_sum_float,UNDEFINED_FLOAT)
1316 RETURN_MEAN(double,write_size_double,read_size_double,return_mean_double,return_sum_double,UNDEFINED_DOUBLE)
1318 #define RETURN_SUM(type,wf,rf,function,undef)\
1319 type function(type **array, int len, int field)\
1323 if ( len==0 ||array==NULL)return 0;\
1326 if ( len==-1)len=rf ( array,sizeof (type*));\
1327 for ( a=0; a< len; a++)\
1328 if ( array[a][field]!=undef)b+=array[a][field];\
1332 RETURN_SUM(short,write_size_short,read_size_short, return_sum_short,UNDEFINED_SHORT)
1333 RETURN_SUM(char,write_size_char,read_size_char,return_sum_char,UNDEFINED_CHAR)
1334 RETURN_SUM(int,write_size_int,read_size_int,return_sum_int,UNDEFINED_INT)
1335 RETURN_SUM(float,write_size_float,read_size_float,return_sum_float,UNDEFINED_FLOAT)
1336 RETURN_SUM(double,write_size_double,read_size_double,return_sum_double,UNDEFINED_DOUBLE)
1338 #define RETURN_SD(type,wf,rf,function,undef)\
1339 type function ( type **array, int len, int field,type mean) \
1343 if ( len==0 ||array==NULL || len>rf ( array,sizeof(type*)))return 0;\
1346 for ( a=0; a< len; a++)\
1348 if ((array[a][field]!=undef) && (mean-array[a][field])!=0)\
1349 c+=((double)mean-array[a][field])*((double)mean-array[a][field]);\
1351 c=sqrt(c)/(double)len;\
1352 return (type)MAX(c,1);\
1355 RETURN_SD(short,write_size_short,read_size_short, return_sd_short,UNDEFINED_SHORT)
1356 RETURN_SD(char,write_size_char,read_size_char,return_sd_char,UNDEFINED_CHAR)
1357 RETURN_SD(int,write_size_int,read_size_int,return_sd_int,UNDEFINED_INT)
1358 RETURN_SD(float,write_size_float,read_size_float,return_sd_float,UNDEFINED_FLOAT)
1359 RETURN_SD(double,write_size_double,read_size_double,return_sd_double,UNDEFINED_DOUBLE)
1360 double return_z_score( double x,double sum, double sum2, double n)
1367 sd=(n==0)?0:sqrt(sum2*n -sum*sum)/n;
1368 avg=(n==0)?0:(sum/n);
1369 z=(sd==0)?0:(x-avg)/sd;
1373 double* return_r (double **list, int n)
1375 double Sy, Sx, Sxy, Sx2, Sy2,r_up, r_low, x, y;
1379 r=vcalloc ( 3, sizeof (double));
1380 Sy=Sx=Sxy=Sx2=Sy2=0;
1382 for ( a=0; a<n; a++)
1393 r_low=(n*Sx2-(Sx*Sx))*(n*Sy2-(Sy*Sy));
1395 r[0]=(r_low==0)?0:r_up/r_low;
1401 r_low=(Sx2-n*x*x)*(Sy2-n*y*y);
1402 r[1]=(r_low==0)?0:r_up/r_low;
1407 #define INVERT_LIST(type,wf,rf,function,swap_function)\
1408 type* function (type *list, int len)\
1411 for ( a=0, b=len-1; a<b; a++, b--)swap_function ( &list[a], &list[b], 1);\
1414 INVERT_LIST(short,write_size_short,read_size_short, invert_list_short,swap_short)
1415 INVERT_LIST(char,write_size_char,read_size_char,invert_list_char,swap_char)
1416 INVERT_LIST(int,write_size_int,read_size_int,invert_list_int,swap_int)
1417 INVERT_LIST(float,write_size_float,read_size_float,invert_list_float,swap_float)
1418 INVERT_LIST(double,write_size_double,read_size_double,invert_list_double,swap_double)
1420 #define SWAP_FUNCTION(type,wf,rf,function)\
1421 void function(type *a, type *b, int n)\
1431 SWAP_FUNCTION(short,write_size_short,read_size_short,swap_short)
1432 SWAP_FUNCTION(char,write_size_char,read_size_char,swap_char)
1433 SWAP_FUNCTION(int,write_size_int,read_size_int,swap_int)
1434 SWAP_FUNCTION(float,write_size_float,read_size_float,swap_float)
1435 SWAP_FUNCTION(double,write_size_double,read_size_double,swap_double)
1437 #define RETURN_MAX_HORIZ(type,wf,rf,function,comparison,undef)\
1438 type function (type ** array, int len_array, int field)\
1442 if ( len_array==0)return 0;\
1445 max=array[field][0];\
1446 for ( a=1; a< len_array; a++)\
1447 if ( array[field][a]!=undef) max=( array[field][a] comparison max)?array[field][a]:max;\
1451 RETURN_MAX_HORIZ(short,write_size_short,read_size_short,return_max_short_hor,>,UNDEFINED_SHORT)
1452 RETURN_MAX_HORIZ(char,write_size_char,read_size_char,return_max_char_hor,>,UNDEFINED_CHAR)
1453 RETURN_MAX_HORIZ(int,write_size_int,read_size_int,return_max_int_hor,>,UNDEFINED_INT)
1454 RETURN_MAX_HORIZ(float,write_size_float,read_size_float,return_max_float_hor,>,UNDEFINED_FLOAT)
1455 RETURN_MAX_HORIZ(double,write_size_double,read_size_double,return_max_double_hor,>,UNDEFINED_DOUBLE)
1457 RETURN_MAX_HORIZ(short,write_size_short,read_size_short,return_min_short_hor,<,UNDEFINED_SHORT)
1458 RETURN_MAX_HORIZ(char,write_size_char,read_size_char,return_min_char_hor,<,UNDEFINED_CHAR)
1459 RETURN_MAX_HORIZ(int,write_size_int,read_size_int,return_min_int_hor,<,UNDEFINED_INT)
1460 RETURN_MAX_HORIZ(float,write_size_float,read_size_float,return_min_float_hor,<,UNDEFINED_FLOAT)
1461 RETURN_MAX_HORIZ(double,write_size_double,read_size_double,return_min_double_hor,<,UNDEFINED_DOUBLE)
1465 #define BEST_OF_MANY(type,wf,rf,function,undef)\
1466 type function (int n, ...)\
1472 /*first Arg: number of values\
1473 2nd Arg: maximise(1)/minimise(0)\
1474 3rd Arg: *int contains the indice of the best value\
1475 ... Arg: n type values\
1478 maximise=va_arg (ap, int);\
1479 fop=va_arg (ap, int*);\
1480 best=va_arg (ap, type);\
1482 for ( a=1; a<n; a++)\
1484 v=va_arg (ap, type);\
1490 if ( best==undef || v==undef);\
1491 else if ( maximise==1 && v>best)\
1496 else if ( maximise==0 && v<best)\
1505 /*BEST_OF_MANY(short,write_size_short,read_size_short, best_short,UNDEFINED_SHORT)*/
1506 /*BEST_OF_MANY(char,write_size_char,read_size_char,best_char,UNDEFINED_CHAR)*/
1507 BEST_OF_MANY(int,write_size_int,read_size_int,best_int,UNDEFINED_INT)
1508 /*BEST_OF_MANY(float,write_size_float,read_size_float,best_float,UNDEFINED_FLOAT)*/
1509 BEST_OF_MANY(double,write_size_double,read_size_double,best_double,UNDEFINED_DOUBLE)
1510 #define IS_DEFINED(type,function,undef)\
1511 int function(int n, ...)\
1517 for ( i=0; i< n; i++)\
1519 if(va_arg(ap,type)==undef)\
1528 /*IS_DEFINED(short,is_defined_short,UNDEFINED_SHORT)*/
1529 /*IS_DEFINED(char,is_defined_char, UNDEFINED_CHAR)*/
1530 IS_DEFINED(int,is_defined_int, UNDEFINED_INT)
1531 /*IS_DEFINED(float,is_defined_float, UNDEFINED_FLOAT)*/
1532 IS_DEFINED(double,is_defined_double,UNDEFINED_DOUBLE)
1534 int return_maxlen ( char ** array, int number)
1538 for ( a=0; a< number; a++)
1539 max=( strlen ( array[a])>max)?strlen ( array[a]):max;
1545 int return_minlen ( char ** array, int number)
1550 min=strlen( array[0]);
1551 for ( a=1; a< number; a++)
1552 min=( strlen ( array[a])>min)?strlen ( array[a]):min;
1559 float return_mean_diff_float ( float **array, int len, int field,float mean)
1564 for ( a=0; a< len; a++)
1566 if ( (mean-array[a][field])!=0)
1567 b+=sqrt((double)((float) ( mean-array[a][field])*(float)(mean-array[a][field])));
1570 return ((float)b/(float)len);
1575 void inverse_int ( int**array, int len, int field, int max, int min)
1578 for ( a=0; a< len; a++)
1579 array[a][field]=max-array[a][field]+min;
1581 void inverse_float ( float**array, int len, int field, int max, int min)
1584 for ( a=0; a< len; a++)
1585 array[a][field]=max-array[a][field]+min;
1587 void inverse_2D_float ( float **array, int start, int len, int start_field, int number_field, float max,float min)
1590 for ( a=0; a< start+len; a++)
1591 for ( b=start_field; b< start_field+ number_field; b++)
1592 array[a][b]=max-array[a][b]+min;
1595 int max_int (int*i, ...)
1598 int index, best_value=0, value;
1600 // expects n values : n, &index, i1, v1, i2, v2...., -1
1602 while ((index=va_arg(ap,int))!=-1)
1604 value=va_arg(ap, int);
1605 if ( a==0 || value>best_value)
1616 /*********************************************************************/
1618 /* SHELL INTERFACES */
1621 /*********************************************************************/
1622 char* getenv4debug (const char * val)
1624 /*efficient mean of getting an environment variable: checks only if one DEBUG is on*/
1630 if (getenv ("DEBUG_BLAST"))check=1;
1631 else if ( getenv ("DEBUG_TREE_COMPARE"))check=1;
1632 else if ( getenv ("DEBUG_MALN"))check=1;
1633 else if ( getenv ("DEBUG_EXTRACT_FROM_PDB"))check=1;
1634 else if ( getenv ("DEBUG_LIBRARY"))check=1;
1635 else if ( getenv ("DEBUG_FUGUE"))check=1;
1637 else if ( getenv ("DEBUG_REFORMAT"))check=1;
1638 else if ( getenv ("DEBUG_RECONCILIATION"))check=1;
1639 else if ( getenv ("DEBUG_TMP_FILE"))check=1;
1640 else if ( getenv ("DEBUG_TREE"))check=1;
1642 else if ( getenv ("DEBUG_SEQ_REFORMAT") && strm (PROGRAM, "SEQ_REFORMAT"))check=2;
1643 else if ( getenv ("DEBUG_TCOFFEE") && strm (PROGRAM, "T-COFFEE"))check=2;
1647 if ( check>0 && strm ( val, "DEBUG_TMP_FILE"))
1654 return getenv (val);
1664 int atoigetenv (const char*var)
1668 else if (!(v=getenv(var)))return 0;
1669 else if ( is_number(v))return atoi(v);
1672 char* get_env_variable ( const char *var, int mode)
1674 /*mode 0: return NULL if variable not set*/
1675 /*mode 1: crash if variable not set*/
1678 if (mode==NO_REPORT)return NULL;
1679 else if ( mode ==IS_NOT_FATAL)
1681 myexit(fprintf_error ( stderr, "\nYou must set the variable %s [FATAL]\n", var));
1686 myexit(fprintf_error ( stderr, "\nYou must set the variable %s [FATAL]\n", var));
1687 myexit (EXIT_FAILURE);
1691 else return getenv (var);
1694 void get_pwd ( char *name)
1700 string=vtmpnam(NULL);
1701 printf_system_direct ("pwd > %s", string);
1703 fp=vfopen ( string, "r");
1704 fscanf ( fp, "%s",name);
1706 printf_system_direct ("rm %s", string);
1708 int pg_is_installed ( char *pg)
1716 fname= vtmpnam(NULL);
1718 printf_system_direct("which %s > %s", pg, fname);
1720 if ((fp=find_token_in_file ( fname, NULL, "Command"))){r=1;vfclose(fp);}
1728 /*********************************************************************/
1732 /*********************************************************************/
1733 char *num2plot (int value, int max, int line_len)
1738 static char *string;
1740 if ( string==NULL)string=vcalloc (1000, sizeof(char));
1742 if ( line_len==-1)len=30;
1745 value_len=((float)value/(float)max)*(float)len;
1747 sprintf ( string, "|");
1750 buf=generate_string(value_len, '*');
1751 sprintf ( string,"%s", buf);
1757 int perl_strstr ( char *string, char *pattern)
1765 if (!string) return 0;
1766 if (!pattern) return 0;
1770 string2=vcalloc ( strlen (string)+1, sizeof (char));
1771 sprintf ( string2,"%s", string);
1772 string2=substitute (string2, "(", " ");
1773 string2=substitute (string2, ")", " ");
1774 string2=substitute (string2, "'", " ");
1776 printf_system_direct("perl -e '$s=\"%s\";$x=($s=~/%s/);$x=($x==1)?1:0;print $x;'>%s", string2, pattern,tmp);
1778 if (check_file_exists(tmp))
1780 fp=vfopen (tmp, "r");
1781 fscanf (fp, "%d", &r);
1786 fprintf ( stderr, "COM: %s\n", string);
1793 void crash_if ( int val, char *s)
1795 if ( val==0)crash(s);
1797 void crash ( char *s)
1803 fprintf ( stderr, "%s",s);
1804 a=vcalloc ( 10, sizeof (int));
1809 static int *local_table;
1810 int ** make_recursive_combination_table ( int tot_n_param, int *n_param, int *nc, int**table, int field)
1814 /* makes a table of all possible combinations*/
1816 if ( tot_n_param==0)
1819 fprintf ( stderr, "\nNULL RETURNED");
1824 if ( local_table!=NULL)vfree (local_table);
1825 local_table=vcalloc ( tot_n_param, sizeof (int));
1827 for ( a=0; a< tot_n_param; a++)local_table[a]=-1;
1828 for ( a=0; a< tot_n_param; a++)nc[0]=nc[0]*n_param[a];
1831 table=declare_int ( nc[0],tot_n_param);
1835 for ( b=0; b<n_param[field]; b++)
1838 local_table[field]=b;
1839 if ( field<tot_n_param-1)
1841 table=make_recursive_combination_table ( tot_n_param, n_param, nc, table, field+1);
1845 for ( c=0; c< tot_n_param; c++)table[nc[0]][c]=local_table[c];
1852 /*********************************************************************/
1854 /* STRING PROCESSING */
1857 /*********************************************************************/
1858 char *strnrchr ( char *s,char x, int n)
1861 for (a=0; a< n; a++)if (s[a]=='x')return s+a;
1867 sprintf ( buf, "%d", n);
1868 return strlen (buf)+1;
1870 char * update_string (char string1[], char string2[])
1872 if ( string1==string2);
1873 else if ( string2==NULL)
1875 if ( string1==NULL)string1=vcalloc ( 1, sizeof(char));
1881 l1=read_array_size_new (string1);
1882 l2=strlen (string2)+1;
1884 string1=vrealloc (string1, (l2)*sizeof (char));
1885 sprintf ( string1, "%s", string2);
1890 char* strcatf (char *string1,char *string2, ...)
1896 buf=vcalloc ( read_array_size_new (string1)+1, sizeof (char));
1897 va_start (ap, string2);
1898 vsprintf (buf, string2, ap);
1900 string1=vcat (string1, buf);
1905 char *vcat ( char *st1, char *st2)
1910 if ( !st1 && !st2)return NULL;
1911 if ( st1) l+=strlen (st1);
1912 if ( st2) l+=strlen (st2);
1915 ns=vcalloc ( l, sizeof (char));
1916 sprintf ( ns, "%s%s", (st1)?st1:"", (st2)?st2:"");
1919 int print_param ( char *param, FILE *fp);
1920 int print_param ( char *param, FILE *fp)
1926 if (!p)p=vcalloc (1000, sizeof (char*));
1928 for ( a=0; a<np; a++)if (p[a]==param) return 0;
1932 fprintf ( fp, "\nPARAMETERS: %s\n", param);
1936 int strget_param ( char *string, char *token1, char *token2, char *format, ...)
1938 /*string: command line
1940 token2: default value (in a string)
1941 format: standard scanf format
1942 ... arguments for scanf
1949 if (!string) return 0;
1950 //print_param (string, stdout);
1951 //print_param (string, stderr);
1952 va_start (ap, format);
1953 buf=after_strstr ( string, token1);
1958 buf2=vcalloc (strlen (buf)+1, sizeof (char));
1959 sprintf ( buf2, "%s", buf);
1960 buf2=substitute (buf2, "__", " ");
1961 n=my_vsscanf (buf2, format, ap);
1964 else {n=my_vsscanf (token2, format, ap);}
1970 char* estrstr (char *string,char *token,...)
1975 if (!token) return NULL;
1976 if (!string) return NULL;
1978 cvsprintf (etoken,token);
1979 ret=strstr(string,etoken);
1984 char* festrstr (char *file,char *token,...)
1990 if (!token || !file || !file_exists(NULL,file))return NULL;
1991 if ((string=file2string(string)))
1993 cvsprintf (etoken,token);
1994 ret=strstr(string,etoken);
1997 if (ret)return token;
2003 int strscanf (char *string1,char *token, char *format,...)
2009 va_start (ap,format);
2010 buf=after_strstr ( string1, token);
2011 if ( buf){n=my_vsscanf (buf, format, ap);}
2018 int match_motif ( char *string, char **motif)//crude string matching, the motif and the string have the same length
2021 if (!motif) return 0;
2023 for ( a=0; a<l; a++)
2025 if ( motif[a][0]!='*' && !strchr (motif[a], string[a]))return 0;
2030 char *after_strstr ( char *string, char *token)
2033 if ( (p=vstrstr (string, token)))return p+strlen (token);
2036 char* lstrstr ( char *in, char *token)//return strstr if matches on the left
2039 if ((s=vstrstr(in, token))&&s==in)return s;
2044 char *vstrstr ( char *in, char *token)
2046 if (!in || !token) return NULL;
2047 else return strstr (in, token);
2049 char ** push_string (char *val, char **stack, int *nval, int position)
2055 if (!val || nval[0]<=position)return stack;
2057 new_stack=vcalloc ( nval[0], sizeof (char*));
2058 new_stack[position]=val;
2059 for (a=0; a< position; a++)new_stack[a]=stack[a];
2060 for (a=position+1; a<nval[0]; a++)new_stack[a]=stack[a-1];
2066 int vsrand (int val)
2068 static int initialized;
2070 if (initialized) return 0;
2071 else if ( getenv ("DEBUG_SRAND"))
2079 /*Make sure two processes in a row do not get the same time value*/
2080 srand ((val==0)?time(NULL):val);
2085 int *randomize_list (int *list, int len, int ncycle)
2090 if ( ncycle==0)ncycle=len;
2091 for ( a=0; a<ncycle; a++)
2101 /*Replace by a gap the parts of the two strings that do not OVERLAP*/
2102 /*returns the length of the overlap*/
2103 int vstrcmp (const char *s1, const char *s2)
2105 if ( !s1 && !s2)return 0;
2106 else if ( !s1 || !s2)return 1;
2107 else return strcmp ( s1, s2);
2109 int vstrncmp (const char *s1, const char *s2, int n)
2111 if ( !s1 && !s2)return 0;
2112 else if ( !s1 || !s2)return 1;
2113 else return strncmp ( s1, s2, n);
2115 FILE *print_array_char (FILE *out, char **array, int n, char *sep)
2118 if ( array==NULL || read_size_char (array,sizeof (char*))<n)
2120 myexit(fprintf_error ( stderr, "\nORB in print_array_char [FATAL]\n"));
2123 for ( a=0; a< n; a++)fprintf ( out, "%s%s", array[a],sep);
2126 char * path2filename ( char *array)
2131 name=vcalloc ( strlen (array)+2, sizeof (char));
2132 F=parse_fname (array);
2133 if ( F->suffix)sprintf ( name, "%s.%s", F->name, F->suffix);
2134 else sprintf (name, "%s", F->name);
2138 Fname* parse_fname ( char *array)
2145 F=declare_fname (sizeof (array));
2147 sprintf ( F->full, "%s", array);
2148 sprintf ( F->path, "%s", array);
2150 while (l!=-1 && (F->path)[l]!='/')(F->path)[l--]='\0';
2152 sprintf ( F->name, "%s", array+l+1);
2156 if((F->name)[l]=='.')
2159 sprintf ( F->suffix, "%s", F->name+l+1);
2167 char *filename2path (char *name)
2171 if (isdir (name))return name;
2174 nname=vcalloc (x+2, sizeof (char));
2175 sprintf ( nname, "%s", name);
2176 while ( x >=0 && nname[x]!='/')nname[x--]='\0';
2178 if ( !isdir (nname) || !nname[0]){vfree (nname); return NULL;}
2186 char *extract_suffixe ( char *array)
2192 new_string=vcalloc ( l+1, sizeof (char));
2193 sprintf (new_string, "%s",array);
2196 while (x!=new_string && x[0]!='.' && x[0]!='/' )x--;
2197 if ( x[0]=='.')x[0]='\0';
2198 else if (x[0]=='/')return x+1;
2200 while ( x!=new_string && x[0]!='/')x--;
2202 return (x[0]=='/')?x+1:x;
2204 void string_array_upper ( char **string, int n)
2207 for ( a=0; a< n; a++)upper_string (string[a]);
2209 void string_array_lower ( char **string, int n)
2212 for ( a=0; a< n; a++)lower_string (string[a]);
2215 char *upper_string ( char *string)
2219 len=strlen ( string);
2220 for ( a=0; a< len; a++)string[a]=toupper ( string[a]);
2223 char *lower_string ( char *string)
2227 len=strlen ( string);
2228 for ( a=0; a< len; a++)string[a]=tolower ( string[a]);
2231 void string_array_convert ( char **array, int n_strings, int ns, char **sl)
2235 for ( a=0; a< n_strings; a++)string_convert ( array[a], ns, sl);
2237 void string_convert( char *string, int ns, char **sl)
2241 for ( a=0; a< l; a++)
2242 string[a]=convert(string[a], ns, sl);
2244 int convert ( char c, int ns, char **sl)
2249 for ( a=0; a< ns; a++)
2251 if ((return_char=convert2 ( c, sl[a]))!=-1)
2258 int convert2 ( char c, char *list)
2266 return_char=(list[l1-1]=='#')?c:list[l1-1];
2268 for ( a=0; a< l1; a++)
2269 if (list[a]=='#')return return_char;
2270 else if ( list[a]==c)return return_char;
2274 char* substitute_old ( char *string_in, char *t, char *r)
2279 /*REplaces every occurence of token t with token r in string_in*/
2281 if ( string_in==NULL || t==NULL || r==NULL) return string_in;
2285 l=read_array_size_new ((void*)string_in)+1;
2287 string_out=vcalloc (l, sizeof (char));
2288 delta=strlen(r)-strlen (t);
2289 delta=(delta<0)?0:delta;
2291 while ( (p=strstr ( string_in, t))!=NULL)
2298 string_out=vrealloc(string_out, sizeof (char)*l);
2301 strcat ( string_out, string_in);
2302 strcat ( string_out, r);
2303 string_in=p+strlen (t);
2305 strcat ( string_out, string_in);
2306 if (l<strlen (string_out))
2308 heap_in=vrealloc (heap_in, sizeof(char)*(strlen (string_out)+1));
2310 sprintf ( heap_in, "%s", string_out);
2314 //Makes sure substitutions of the tild only occur on valid UNIX pathnames
2315 char* tild_substitute ( char *string_in, char *t, char *r)
2319 p=strstr ( string_in, "~");
2320 if ( p==NULL)return string_in;
2321 else if (p && p!=string_in && p[-1]!='/')return string_in;
2325 return substituteN (string_in, t, r,1);
2330 char* substitute_char_set (char *s, char *set, char r)
2334 if ( !s || !set || !r) return s;
2337 s=substitute_char (s, set[a], r);
2342 char* substitute_char ( char *string_in, char t, char r)
2345 while ( string_in && string_in[n]!='\0')
2347 if ( string_in[n] == t)string_in[n]=r;
2352 char* substitute ( char *string_in, char *t, char *r)
2354 /*REplaces every occurence of token t with token r in string_in*/
2355 return substituteN(string_in, t, r,0);
2357 char* substituteN ( char *string_in, char *t, char *r, int N)
2360 char *p, *heap_in, n=0;
2361 int delta, l, lsi,lso,lr,lt,nt;
2363 /*REplaces the first N Occurences*/
2364 if ( string_in==NULL || t==NULL || r==NULL) return string_in;
2368 l=read_array_size_new ((void*)string_in)+1;
2371 lsi=strlen (string_in);
2373 delta=((lr-lt)>0)?(lr-lt):0;
2375 while ( (p=strstr (string_in, t))!=NULL)
2383 string_out=vcalloc (lso+1, sizeof (char));
2385 while ((N==0 ||n<N) && (p=strstr ( string_in, t))!=NULL)
2388 strcat ( string_out, string_in);
2389 strcat ( string_out, r);
2393 strcat ( string_out, string_in);
2394 if (l<(lso+1)){HERE ("Realloc %s %s delta=%d %d %d", t, r,delta, lsi, lso);heap_in=vrealloc (heap_in, sizeof (char)*(lso +1));}
2395 sprintf ( heap_in, "%s", string_out);
2401 char **clean_string (int n, char **string)
2404 if ( !string) return string;
2405 for (a=0; a< n; a++)
2407 if (!string[a]) continue;
2408 l=strlen (string[a]);
2412 if (!isgraph(c) && c!='\n' && c!='\t' && c!=' ')string[a][b]=' ';
2421 int str_overlap ( char *string1, char *string2, char x)
2432 if ( strm ( string1, string2))return 0;
2435 l1=strlen ( string1);
2436 l2=strlen ( string2);
2437 array=declare_int ( strlen ( string1), strlen ( string2));
2438 for ( a=0; a< l1; a++)
2439 for ( b=0; b< l2; b++)
2441 if ( a==0 || b==0)array[a][b]=(string1[a]==string2[b])?1:0;
2443 if (string1[a]==string2[b])
2445 score=array[a][b]=array[a-1][b-1]+1;
2454 start1=(max1+1)-max_val;
2457 start2=(max2+1)-max_val;
2460 for ( a=0; a< l1; a++)if ( a<start1 || a> end1)string1[a]=x;
2461 for ( a=0; a< l2; a++)if ( a<start2 || a> end2)string2[a]=x;
2463 free_int ( array, l1);
2469 int get_string_line ( int start, int n_lines, char *in, char *out)
2477 while ( (c=in[start++])!='\n' && c!='\0')
2485 return (c=='\0')?-1:start;
2489 FILE * output_string_wrap ( int wrap,char *string, FILE *fp)
2496 for ( a=0, b=1; a< l; a++, b++)
2498 fprintf ( fp, "%c", string[a]);
2501 fprintf ( fp, "\n");
2508 char * extract_char ( char * array, int first, int len)
2513 len= ( len<0)?0:len;
2515 array2=vcalloc ((len+1), sizeof (char));
2517 for ( a=0; a<len; a++)
2518 array2[a]=array[first++];
2525 int check_cl4t_coffee (int argc, char **argv)
2528 if ( name_is_in_list ("-other_pg", argv, argc, 100)!=-1)return 1;
2529 else if (name_is_in_list ( "tcoffee_test_seq.pep", argv, argc, 100)!=-1)return 1;
2532 int a, inseq, result;
2534 char command[10000];
2537 for ( inseq=0,a=0; a<argc; a++)
2539 if ( inseq && argv[a][0]=='-')
2543 else if (strm ( argv[a], "-seq"))inseq=1;
2547 strcat ( command, " ");
2548 strcat ( command, argv[a]);
2551 name=vcalloc ( 100, sizeof (char));
2552 sprintf ( name, "TCIT_%d", (int)rand()%10000);
2553 strcat ( command, " -seq tcoffee_test_seq.pep -quiet -no_error_report");
2554 fp=vfopen ( name, "w");
2555 fprintf ( fp, ">A\nthecat\n>B\nthecat\n");
2557 result=safe_system (command);
2558 printf_system ( "rm %s.*", name);
2560 if (result) {myexit (EXIT_FAILURE);return 0;}
2565 char** merge_list ( char **argv, int *argc)
2570 char current [STRING];
2572 out=declare_char (argc[0], STRING);
2577 while (a< n_in && !is_parameter ( argv[a]))
2579 sprintf (out[argc[0]++], "%s", argv[a]);
2585 for ( a=0; a< n_in; a++)
2587 if ( is_parameter (argv[a]))
2589 sprintf ( out[argc[0]++], "%s", argv[a]);
2590 sprintf ( current, "%s", argv[a]);
2592 for ( b=0; b< n_in;)
2594 if ( is_parameter (argv[b]) && strm (current, argv[b]))
2598 while ( b<n_in && !is_parameter ( argv[b]) )
2602 sprintf ( out[argc[0]++], "%s", argv[b]);
2617 free_char (argv, -1);
2622 int * string2num_list_old ( char *string)
2624 /*Breaks down a list of numbers separated by any legal separator and put them in an array of the right size*/
2625 /*Returns list a list of integer*/
2626 /*Skips non numbers*/
2632 buf=vcalloc ( strlen (string)+1, sizeof (char));
2635 sprintf ( buf, "%s", string);
2636 s=strtok (buf, SEPARATORS);
2640 s=strtok (NULL, SEPARATORS);
2642 list=vcalloc (n+1, sizeof (int));
2645 sprintf ( buf, "%s", string);
2646 s=strtok (buf, SEPARATORS);
2649 if (is_number(s))list[n++]=atoi(s);
2650 s=strtok (NULL, SEPARATORS);
2659 int *name_array2index_array ( char **list1, int n1, char **list2, int n2)
2662 /*returns an indexed list of the position of list1 in list2*/
2663 list=vcalloc ( n1, sizeof (int));
2664 for ( a=0, max=0; a< n1; a++)max=MAX(max, strlen (list1[a]));
2665 for ( a=0 ; a< n2; a++)max=MAX(max, strlen (list2[a]));
2667 for ( a=0; a< n1; a++)
2669 list[a]=name_is_in_list (list1[a],list2,n2,max);
2674 int * string2num_list ( char *string)
2676 return string2num_list2(string, SEPARATORS);
2678 int * string2num_list2 ( char *string, char *separators)
2680 /*Breaks down a list of numbers separated by any legal separator and put them in an array of the right size*/
2681 /*Returns list a list of integer*/
2682 /*Skips non numbers*/
2688 nlist=string2list2 (string, separators);
2690 if (nlist==NULL) return NULL;
2693 list=vcalloc ( n, sizeof (int));
2695 for (m=1, a=1; a< n; a++)
2697 if (is_number(nlist[a]))list[m++]=atoi(nlist[a]);
2701 free_char (nlist, -1);
2702 if ( m==0){vfree(list); return NULL;}
2711 char * list2string ( char **list, int n)
2713 return list2string2 ( list, n, " ");
2715 char * list2string2 ( char **list,int n, char* sep)
2720 for ( l=0,a=0; a<n; a++)l+=strlen ( list[a])+1;
2721 string=vcalloc (l+1, sizeof (char));
2722 for ( a=0; a< n; a++)
2724 strcat ( string, list[a]);
2725 strcat ( string, sep);
2730 char ** string2list ( char *string)
2732 return string2list2(string, SEPARATORS);
2734 char ** string2list2 ( char *string, char *separators)
2736 /*Breaks down a list of words separated by any legal separator and put them in an array of the right size*/
2737 /*Returns list a list of char with the size written in list[0]*/
2744 if ( string==NULL)return NULL;
2745 buf=vcalloc ( strlen (string)+2, sizeof (char));
2749 sprintf ( buf, "%s", string);
2750 s=strtok (buf, separators);
2755 max_len=MAX(max_len,(strlen (s)));
2756 s=strtok (NULL, separators);
2760 if ( n==0){vfree(buf); return NULL;}
2762 list=declare_arrayN (2, sizeof (char), n+2, max_len+1);
2765 sprintf ( buf, "%s", string);
2766 s=strtok (buf, separators);
2769 sprintf (list[n++], "%s",s);
2770 s=strtok (NULL, separators);
2773 sprintf (list[0], "%d", n);
2780 char** break_list ( char **argv, int *argc, char *separators)
2789 /*Breaks down the argv command line in smaller units, breaking at every separator*/
2790 out=vcalloc (MAX_N_PARAM, sizeof (char*));
2794 if ( n_in>=MAX_N_PARAM)
2796 myexit(fprintf_error ( stderr, "\nERROR: too many parameters, recompile with MAX_N_PARAM set at a higher velue [FATAL:%s]\n", PROGRAM));\
2797 myexit (EXIT_FAILURE);
2800 for ( a=0; a< n_in; a++)
2805 if (cont)ar=get_list_of_tokens( argv[a], separators,&n_ar);
2806 else ar=get_list_of_tokens( argv[a],"",&n_ar);
2809 for ( b=0; b< n_ar; b++)
2811 out[argc[0]]=vcalloc( strlen (ar[b])+1, sizeof (char));
2812 sprintf (out[argc[0]++], "%s", ar[b]);
2816 if ( strstr (argv[a], "-other_pg"))cont=0;
2822 char *invert_string2 (char *string)
2828 buf=vcalloc ( l+1, sizeof (char));
2829 for ( a=l-1, b=0; a>=0; a--, b++)
2831 sprintf (string, "%s", buf);
2835 char *invert_string (char *string)
2837 return string2inverted_string(string);
2839 char* string2inverted_string(char *string)
2845 buf=vcalloc ( l+1, sizeof (char));
2846 for ( a=l-1, b=0; a>=0; a--, b++)
2851 char ** get_list_of_tokens ( char *in_string, char *separators, int *n_tokens)
2859 if ( in_string==NULL || strm(in_string, ""));
2860 else if ( in_string[0]=='[')
2862 list=declare_char (1, strlen ( in_string)+1);
2863 sprintf ( list[n_tokens[0]], "%s",in_string);
2868 list=declare_char (strlen ( in_string)+1, 1);
2869 string=vcalloc ( strlen(in_string)+1, sizeof (char));
2870 sprintf ( string, "%s", in_string);
2872 while ( (p=strtok ((p==NULL)?string:NULL, ((separators==NULL)?SEPARATORS:separators)))!=NULL)
2874 list[n_tokens[0]]=vrealloc ( list[n_tokens[0]], sizeof (char) *strlen (p)+1);
2875 sprintf ( list[n_tokens[0]], "%s", p);
2884 char **ungap_array ( char **array, int n)
2887 for ( a=0; a< n; a++)ungap(array[a]);
2891 void ungap ( char *seq)
2893 remove_charset ( seq, "ungap");
2895 int seq2len (char *seq, char *pset,char *nset)
2898 //count all the residues in pset and NOT in nset
2899 if ( !seq) return 0;
2902 //returns the len of the string
2903 for (a=0; a< l; a++)
2906 if ( pset && nset && strchr (pset, c) && !strchr (nset, c))t++;
2907 else if ( pset && strchr (pset, c))t++;
2908 else if ( nset && !strchr (nset, c))t++;
2912 int seq2res_len (char *seq)
2914 return seq2len (seq, NULL, GAP_LIST);
2916 char* remove_charset_from_file (char *fname, char *set)
2923 fp1=vfopen (fname, "r");
2924 fp2=vfopen (tmp=vtmpnam (NULL), "w");
2925 while ( (c=fgetc(fp1))!=EOF)
2927 if (!strchr ( set,c))fprintf ( fp2, "%c", c);
2934 void remove_charset ( char *seq, char *set)
2939 set2=vcalloc (256, sizeof (char));
2940 if ( strm (set, "!alnum"))
2942 for ( b=0,a=1;a< 256; a++)if ( !isalnum (a))set2[b++]=a;
2944 else if ( strm ( set, "ungap"))
2946 sprintf ( set2, "%s", GAP_LIST);
2950 sprintf ( set2, "%s", set);
2954 for (b=0, a=0; a<=l; a++)
2956 if ( strchr ( set2, seq[a]));
2957 else seq[b++]=seq[a];
2964 char **char_array2number ( char ** array, int n)
2967 for ( a=0; a< n; a++)array[a]=char2number(array[a]);
2970 char *char2number ( char * array)
2976 for ( a=0; a< l; a++)
2978 if ( isdigit(array[a]) && array[a]!=NO_COLOR_RESIDUE && array[a]!=NO_COLOR_GAP )array[a]-='0';
2979 else if ( array[a]<9);
2980 else if ( array[a]==NO_COLOR_RESIDUE || array[a]==NO_COLOR_GAP)array[a]=NO_COLOR_RESIDUE;
2986 /*turns a char into a pointer*/
2987 if ( p==NULL) return 0;
2988 else return atol(p);
2991 char *mark_internal_gaps(char *seq, char symbol)
2998 cache_seq=vcalloc ( l+1, sizeof (char));
2999 sprintf ( cache_seq, "%s", seq);
3001 for ( gap=0, in_seq=0,a=0; a< l; a++)
3004 if ( !gap && !in_seq)in_seq=1;
3005 if (gap && in_seq)seq[a]=symbol;
3008 for (gap=0, in_seq=0,a=l-1; a>=0; a--)
3011 if ( !gap && !in_seq)break;
3012 if (gap && !in_seq)seq[a]=cache_seq[a];
3018 void splice_out ( char *seq, char x)
3024 for (b=0, a=0; a<=l; a++)
3026 else seq[b++]=seq[a];
3029 char *splice_out_seg ( char *seq, int pos, int len)
3033 if (seq==NULL || pos<0) return seq;
3036 printf_exit ( EXIT_FAILURE, stderr, "Splice_out_seg out of bound: Length %d seg: [%d %d] [splice_out_seg::util.c][FATAL:%s]\n", l, pos, pos+len, PROGRAM);
3038 for (a=pos; a< l; a++)
3044 int isblanc ( char *buf)
3048 if ( buf==NULL)return 0;
3050 for ( a=0; a< l; a++)
3051 if (isalnum (buf[a]))return 0;
3057 int is_number ( char *num)
3063 if ( !strchr ("0123456789.-+", num[a]))return 0;
3067 int is_alnum_line ( char *buf)
3071 for ( a=0; a< l; a++)
3072 if (isalnum (buf[a]))return 1;
3075 int is_alpha_line ( char *buf)
3079 for ( a=0; a< l; a++)
3080 if (isalpha (buf[a]))return 1;
3083 int case_insensitive_strcmp ( char *string1, char *string2)
3088 if ( !string1 && !string2) return 1;
3089 else if ( !string1 && !string2) return 0;
3093 for ( a=0; a< l; a++)
3095 if (tolower(string1[a])!=tolower(string2[a]))return 0;
3100 int get_string_sim ( char *string1, char *string2, char *ignore)
3110 len1=strlen (string1);
3111 len2=strlen (string2);
3113 if ( len1!=len2)return 0;
3115 for ( a=0; a< len1; a++)
3119 if ( !is_in_set (r1, ignore) && !is_in_set (r2, ignore))
3132 return (int) (sim*100)/pos;
3135 int haslower (char *s)
3142 for (a=0; a<l; a++)if (islower(s[a]))return 1;
3146 int hasupper (char *s)
3153 for (a=0; a<l; a++)if (isupper(s[a]))return 1;
3160 return (is_in_set (x, AA_ALPHABET) && !is_in_set (x, GAP_LIST));
3162 int is_rna ( char x)
3164 return (is_in_set (x, RNAONLY_ALPHABET)&& !is_in_set (x, GAP_LIST));
3166 int is_dna ( char x)
3168 return (is_in_set (x, DNA_ALPHABET)&& !is_in_set (x, GAP_LIST));
3170 int is_gap ( char x)
3172 return ( is_in_set ( x, GAP_LIST));
3174 int is_gop ( int p, char *s)
3177 else if (p<=0)return 0;
3178 else if (s[p]!='-' && s[p+1]=='-')return 1;
3182 char* get_alphabet ( char *seq, char *alphabet)
3183 /*This function builds an alphabet from the string seq*/
3184 /*Alphabet does not need to be emppty. The total number of unique symbols is returned*/
3185 /*Gaps, as defined in the GAP_LIST are ignored*/
3190 if ( !alphabet) alphabet=vcalloc ( 200, sizeof (char));
3194 for ( a=0; a<len; a++)
3195 if ( !is_gap ( seq[a]) && !is_in_set ( seq[a], alphabet+1)){alphabet[(int)++alphabet[0]]=seq[a];}
3199 int array_is_in_set ( char *x, char *set )
3205 for ( a=0; a< len; a++)
3206 if ( !is_in_set ( x[a], set))return 0;
3211 int is_in_set ( char r, char *list)
3218 if ( strstr ( list,s)!=NULL)return 1;
3222 char * generate_void ( int x)
3224 return generate_string (x, ' ');
3227 char * generate_null ( int x)
3229 return generate_string ( x, '-');
3232 char * generate_string ( int x, char y)
3237 string = vcalloc ( x+1, sizeof ( char));
3239 for ( a=0; a< x; a++)
3245 char * translate_string (char *string, char *in, char*out)
3253 if ( l2!=strlen (out))
3255 myexit(fprintf_error ( stderr, "\nERROR: translate_string function: IN alphabet (%s) must have the same size as OUT alphabet (%s) [FATAL:%s]\n", in, out, PROGRAM));
3256 myexit (EXIT_FAILURE);
3259 for ( a=0; a< l1; a++)
3261 for ( b=0; b< l2; b++)
3263 if ( string[a]==in[b])
3274 int get_longest_string (char **array,int n, int *len, int *index)
3277 int max_len=0, max_index=0;
3279 if ( n==0 || array==NULL )return 0;
3280 if ( n==-1)n=read_size_char(array,sizeof (char*));
3283 if (read_size_char ( array,sizeof (char*))<n)
3285 myexit(fprintf_error ( stderr, "\nBAD REQUEST: Number of strings=%d, expected number=%d", read_size_char ( array,sizeof (char*)),n));
3286 crash ("[FATAL/util.c]");
3290 max_len=strlen(array[0]);
3292 for ( a=1; a< n; a++)
3294 l=strlen ( array[a]);
3301 if (index!=NULL)index[0]=max_index;
3302 if (len!=NULL)len[0]=max_len;
3308 int get_shortest_string (char **array,int n, int *len, int *index)
3313 if ( n==0|| array==NULL || read_size_char ( array,sizeof (char*))<n)return 0;
3316 min_len=strlen(array[0]);
3318 for ( a=1; a< n; a++)
3320 l=strlen ( array[a]);
3327 if (index!=NULL)index[0]=a;
3328 if (len!=NULL)len[0]=min_len;
3333 char **pad_string_array ( char **array, int n, int len, char pad)
3336 for ( a=0; a< n; a++)
3338 l= strlen ( array[a]);
3339 for (b=l; b<len; b++)array[a][b]=pad;
3344 char **right_pad_string_array ( char **array, int n, int len, char pad)
3346 return pad_string_array(array, n, len, pad);
3348 char **left_pad_string_array ( char **array, int n, int len, char pad)
3353 buf=vcalloc ( len+1, sizeof (char));
3354 for ( a=0; a< n; a++)
3356 l= strlen ( array[a]);
3357 for (b=0; b<(len-l); b++)buf[b]=pad;
3358 for (c=0,b=(len-l); b<=len; c++,b++)buf[b]=array[a][c];
3359 sprintf (array[a], "%s", buf);
3365 char * crop_string (char *string, int start, int end)
3368 /*Extract [start-end[*/
3370 if ( strlen (string)<end)
3372 myexit(fprintf_error ( stderr, "\nString=%s, start=%d end=%d", string, start, end));
3373 crash ( "Wrong End in crop String [FATAL]");
3377 buf=vcalloc (strlen (string)+1, sizeof (char));
3379 sprintf ( buf, "%s", string+start);
3380 sprintf ( string,"%s", buf);
3387 int get_distance2char ( char *x, char*list)
3392 if (x==NULL) return 0;
3393 while (!is_in_set (y[0], list) && y[0]!='\0')y++;
3396 /*********************************************************************/
3398 /* TIME FUNCTIONS */
3401 /*********************************************************************/
3405 static long milli_sec_conv=1000;
3408 FILE *print_program_information (FILE *fp, char *comment)
3410 fprintf ( fp, "# Results Produced with %s (%s)\n",PROGRAM, VERSION);
3411 fprintf ( fp, "# %s is available from %s\n",PROGRAM,URL);
3412 fprintf ( fp, "# Register on: https://groups.google.com/group/tcoffee/\n");
3413 if (comment)fprintf ( fp, "# %s\n", comment);
3416 FILE* print_cpu_usage (FILE *fp, char *comment)
3418 fprintf ( fp, "# %s CPU Usage: %d millisec\n", comment, get_time());
3421 void print_exit_success_message ()
3424 fprintf ( stderr, "\n# TERMINATION STATUS: SUCCESS [PROGRAM: %s pid %d ppid %d]\n#CL: %s\n", PROGRAM, getpid(), getppid(),in_cl);
3426 void print_exit_failure_message ()
3428 fprintf ( stderr, "\n# TERMINATION STATUS: FAILURE [PROGRAM: %s pid %d ppid %d\n#CL: %s\n", PROGRAM, getpid(), getppid(), in_cl);
3437 struct tms time_buf[1];
3438 long tms_stime, tms_utime;
3440 if ( child==1)return get_ctime();
3442 if ( ticks==0)ticks = sysconf(_SC_CLK_TCK);
3445 tms_stime=(long)time_buf->tms_stime*milli_sec_conv;
3446 tms_utime=(long)time_buf->tms_utime*milli_sec_conv;
3453 ref=(tms_stime+tms_utime);
3458 time=(tms_utime+tms_stime)-ref;
3459 return (int) ((time)/ticks);
3465 struct tms time_buf[1];
3466 long tms_cutime, tms_cstime;
3468 if ( ticks==0)ticks = sysconf(_SC_CLK_TCK);
3473 tms_cstime=(long)time_buf->tms_cstime*milli_sec_conv;
3474 tms_cutime=(long)time_buf->tms_cutime*milli_sec_conv;
3479 ref=tms_cstime+tms_cutime;
3484 time=(tms_cutime+tms_cstime)-ref;
3485 return (int)((time)/ticks);
3491 return (int)get_time();
3493 int increase_ref_time(int increase)
3495 if ( ref==0)get_time();
3497 ref-=(long)ticks*(long)increase;
3502 /*********************************************************************/
3507 /*********************************************************************/
3508 int evaluate_sys_call_io ( char *out_file, char *com, char *fonc)
3510 if ( file_exists (NULL,out_file))return 1;
3513 add_warning (stderr, "COMMAND FAILED: %s",com);
3517 void HERE (char *string, ...)
3521 va_start (ap, string);
3522 fprintf ( stderr, "HERE: ");
3523 vfprintf (stderr, string, ap);
3524 fprintf ( stderr, "\n");
3530 int fprintf_fork (FILE *fp, char *string, ...)
3533 static char *closeF;
3544 openF=vcalloc (100, sizeof (char));
3545 sprintf (openF, "cedric1");
3546 closeF=vcalloc (100, sizeof (char));
3547 sprintf (closeF, "cedric2");
3549 //openF =vtmpnam (NULL);
3550 //closeF=vtmpnam (NULL);
3551 vfclose(vfopen (openF,"w"));
3553 while ((rename (openF,closeF))==-1);
3555 cvsprintf (buf,string);
3556 fprintf ( fp, "%s", buf);
3558 rename (closeF, openF);
3562 int fprintf_fork2 (FILE *fp, char *string, ...)
3569 cvsprintf (buf, string);
3570 fprintf ( fp, "%s", buf);
3576 int printf_file (char *file,char *mode, char *string,...)
3581 if (!(fp=vfopen (file, mode)))return 0;
3582 va_start (ap, string);
3583 vfprintf (fp, string, ap);
3588 int printf_system_direct (char *string, ...)
3593 cvsprintf (buf, string);
3601 int printf_system (char *string, ...)
3606 cvsprintf (buf, string);
3612 int my_system_cl (int argc, char *argv[])
3617 for ( a=0, l=0; a< argc; a++)l+=(strlen(argv[a])+2);
3618 command=vcalloc (l+1, sizeof(char));
3619 for ( a=0; a< argc; a++)
3621 command=strcat (command, argv[a]);
3622 command=strcat (command, " ");
3624 a=my_system ( command);
3629 int my_system ( char *command0)
3631 static char ***unpacked_list;
3632 static int n_unpacked;
3636 unpacked_list=declare_arrayN(3, sizeof (char), 3, 200,300);
3639 if ( getenv ("DEBUG_PERL"))return safe_system (command0);
3649 command1=vcalloc ( 3*strlen (command0)+1, sizeof (char));
3650 command2=vcalloc ( 100000, sizeof (char));
3651 sprintf ( command1, "%s", command0);
3653 command1=substitute (command1, "|", " | ");
3654 command1=substitute (command1, ";", " ; ");
3656 list=string2list (command1);
3658 if ( !list) return EXIT_SUCCESS;
3661 //Identify T-Coffee self threads and install threads
3663 for ( a=1; a< atoi(list[0]); a++)
3667 if ( strstr ( list[a], "unpack_"))
3669 unpack_all_perl_script (list[a]+strlen ("unpack_"));
3670 myexit (EXIT_SUCCESS);
3672 else if ((c=name_is_in_list (list[a], unpacked_list[0], n_unpacked, 100))!=-1);
3676 n_unpacked=unpack_perl_script (list[a], unpacked_list, n_unpacked);c=n_unpacked-1;
3679 //if non unpacked script check pg is installed:
3681 if ( strm (unpacked_list[2][c], "shell"))
3683 check_program_is_installed (list[a], NULL, NULL, NULL, INSTALL_OR_DIE);
3685 strcat (command2, ((c!=-1)?unpacked_list[1][c]:list[a]));
3686 strcat (command2, " ");
3692 strcat (command2, list[a]);
3693 strcat (command2, " ");
3694 if ( strm (list[a], ",") ||strm (list[a], "|")) is_command=1;
3698 free_char (list,-1);
3700 command2=substitute ( command2, "//", "/");
3702 return_val=safe_system (command2);
3708 int has_warning_lock()
3710 if (lock(getpid(), LWARNING, LCHECK,NULL))return 1;
3713 int has_error_lock()
3715 if (lock(getpid(), LERROR, LCHECK,NULL))return 1;
3718 int is_shellpid(int pid)
3720 if ( lock(pid, LLOCK, LCHECK, NULL) && strstr (lock(pid,LLOCK, LREAD, NULL), "-SHELL-"))return 1;
3729 fprintf ( stderr,"\n\t------ check if %d isrootpid (util): %s->%d", getpid(),f=lock2name (getppid(),LLOCK), (lock(getppid(), LLOCK, LCHECK, NULL))?1:0);
3734 if(lock (getppid(), LLOCK, LCHECK, NULL)!=NULL)return 0;
3738 int shift_lock ( int from, int to, int from_type,int to_type, int action)
3740 //action: SET (concatenate) or RESET (replace parent with child content)
3742 if (!lock (from,from_type, LCHECK, NULL))return 0;
3743 e=lock (from,from_type, LREAD, NULL);
3744 lock (from,from_type, LRELEASE, NULL);
3746 if ( action==LSET || action==LRESET)lock (to, to_type,action, e);
3749 myexit(fprintf_error (stderr, "Unsupported type for shift_lock"));
3755 char*lock2name (int pid, int type)
3759 gethostname(host, 1023);
3761 fname=vcalloc (strlen(host)+strlen (get_lockdir_4_tcoffee())+1000, sizeof (char));
3762 if (type == LLOCK)sprintf (fname, "%s/.%d.%s.lock4tcoffee",get_lockdir_4_tcoffee(), pid,host);
3763 else if ( type == LERROR) sprintf (fname, "%s/.%d.%s.error4tcoffee", get_lockdir_4_tcoffee(),pid,host);
3764 else if ( type == LWARNING) sprintf (fname, "%s/.%d.%s.warning4tcoffee",get_lockdir_4_tcoffee(),pid,host);
3765 else myexit(fprintf_error ( stderr, "ERROR: Unknown type for lock"));
3769 int release_all_locks (int pid)
3771 lock (pid, LLOCK, LRELEASE, NULL);
3772 lock (pid, LERROR, LRELEASE, NULL);
3773 lock (pid, LWARNING, LRELEASE, NULL);
3776 char* lock(int pid,int type, int action,char *string, ...)
3783 fname=lock2name (pid, type);
3787 fprintf (stderr,"\n\t\t---loc4tc(util.h) %d =>%s [RD: %s]\n", action, fname, getcwd(NULL, 0));
3790 if (action == LREAD)
3792 r=file2string (fname);
3794 else if ( action == LCHECK)
3796 r=(file_exists (NULL,fname))?"x":NULL;
3798 else if (action== LRELEASE)
3803 printf_system_direct ("mv %s %s.released", fname, fname);
3805 else if (file_exists (NULL, fname))
3808 //safe_remove (fname);return NULL;
3812 else if ( clean_exit_started) return NULL; //NO MORE LOCK SETTING during EXIT Phase
3813 else if (action== LSET || action == LRESET)
3819 cvsprintf (value,string);
3823 value=vcalloc (2, sizeof(char));
3824 sprintf (value, " ");
3826 string2file (fname, (action==LSET)?"a":"w", value);
3831 else myexit(fprintf_error ( stderr, "ERROR: Unknown action for LOCK"));
3836 int check_process (const char *com,int pid,int r, int failure_handling)
3839 //If the child process has an error lock, copy that lock into the parent'lock
3840 //The error stack trace of the child gets passed to the parent
3841 if (debug_lock)fprintf (stderr, "\nEVAL_CALL ----- %d ->%s\n",pid, (r==EXIT_FAILURE)?"FAIL":"SUCCESS");
3843 if ( failure_handling == IGNORE_FAILURE) return r;
3844 if ( lock(pid, LWARNING, LCHECK, NULL))
3846 shift_lock (pid, getpid(), LWARNING, LWARNING,LSET);
3850 if ( lock(pid, LERROR, LCHECK, NULL))
3852 shift_lock (pid, getpid(), LERROR,LERROR, LSET);
3854 else if (r==EXIT_FAILURE)
3856 //Reconstruct missing errorlock
3857 lock (getpid(), LERROR,LSET,"%d -- ERROR: UNSPECIFIED UNSPECIFIED\n",pid);
3858 lock (getpid(), LERROR,LSET,"%d -- COM: %s\n",pid,com);
3859 lock (getpid(), LERROR, LSET,"%d -- STACK: %d -> %d\n",pid,getpid(), pid);
3861 //lock is now ready. Shall we use it?
3863 if (lock(getpid(), LERROR, LCHECK, NULL))
3865 if (failure_handling==RETURN_ON_FAILURE)
3867 shift_lock(getpid(),getpid(),LERROR, LWARNING,LSET);
3871 myexit (EXIT_FAILURE);
3878 int safe_system (const char * com_in)
3883 int failure_handling;
3889 if ( clean_exit_started) return system (com_in);
3890 if ( com)vfree (com);
3891 if ( strstr ( com_in, "SCRATCH_FILE"))
3893 com=vcalloc ( strlen ( com_in)+1, sizeof (char));
3894 sprintf ( com, "%s", com_in);
3895 while (strstr ( com, "SCRATCH_FILE"))
3899 com=vrealloc (com, (strlen (com)+strlen (t)+1)*sizeof (char));
3902 com=substitute (com,"SCRATCH_FILE", t);
3907 com=vcalloc (strlen (com_in)+1, sizeof (char));
3908 sprintf ( com, "%s", com_in);
3914 else if ( (p=strstr (com, "::IGNORE_FAILURE::")))
3917 failure_handling=IGNORE_FAILURE;
3919 else if ( (p=strstr (com, "::RETURN_ON_FAILURE::")))
3922 failure_handling=RETURN_ON_FAILURE;
3926 failure_handling=EXIT_ON_FAILURE;
3930 sprintf ( command, " -SHELL-%s (tc)", com_in);
3931 if ((pid = vvfork (command)) < 0)
3942 argv [2] =(char*) com;
3945 if ( debug_lock)fprintf (stderr,"\n--- safe_system (util.h): %s (%d)\n", com, getpid());
3946 execvp ("/bin/sh", argv);
3957 r=vwaitpid (pid, &status, 0);
3960 if (errno ==EINTR)r=EXIT_SUCCESS;
3961 else if (r==-1 || status != EXIT_SUCCESS)r=EXIT_FAILURE;
3962 else r=EXIT_SUCCESS;
3964 fprintf ( stderr, "\n--- safe system return (util.c): p:%d c:%d r:%d (wait for %d", getppid(), getpid(), r, pid);
3965 return check_process (com_in,pid,r, failure_handling);
3973 static int **pidtable;
3974 int assert_pid (pid_t p)
3976 if ( p>= MAX_N_PID || p<0)
3978 printf_exit (EXIT_FAILURE, stderr, "MAX_N_PID exceded -- Recompile changing the value of MAX_N_PID (current: %d Requested: %d)", MAX_N_PID, p);
3982 pid_t **declare_pidtable ()
3986 pidtable=vcalloc (MAX_N_PID, sizeof (pid_t*));
3987 for (a=0; a<MAX_N_PID; a++)
3989 pidtable[a]=vcalloc (2, sizeof (pid_t));
3993 pid_t set_pid (pid_t p)
3997 if (!pidtable)declare_pidtable();
3998 if ( p<=0) return (pid_t)0;
3999 pidtable[(int)p][0]=getpid();
4000 pidtable[(int)p][1]=1;
4002 pid_t vvfork (char *type)
4013 myexit(fprintf_error (stderr,"FORK_ERROR fork returned -1"));
4017 add_warning (stderr, "Could Not Fork %s [%d/%d tries]", PROGRAM, attempt, 1000);
4019 return vvfork(type);
4026 lock (getpid(), LLOCK, LSET, "%d\n",p);//Update the parent lock
4032 release_all_locks (getpid());
4033 lock (getpid(), LLOCK, LSET, "%d%s\n", getppid(), (type)?type:"");//Create lock for the fork
4034 if (debug_lock)fprintf ( stderr, "\nFORKED (util): p=%d child=%d\n", getppid(), getpid());
4040 int vwait_npid (int sub, int max, int min)
4063 pid_t vwaitpid (pid_t p, int *status, int options)
4067 p=waitpid (p, status, options);
4072 pidtable[(int)p][0]=pidtable[(int)p][1]=0;
4076 pid_t vwait (pid_t *p)
4082 if (atoigetenv("RETURN_ON_FAILURE"))handle_failure=RETURN_ON_FAILURE;
4083 else handle_failure=EXIT_ON_FAILURE;
4087 if (p2!=-1)rv=check_process("forked::T-Coffee", p2, rv,handle_failure);
4094 int get_child_list (int pid,int *clist);
4095 int kill_child_list (int *list);
4096 int kill_child_pid(int pid)
4102 list=vcalloc (MAX_N_PID, sizeof (int));
4104 while ((n=get_child_list (pid,list)))
4106 kill_child_list (list);
4109 for (a=0; a<MAX_N_PID; a++)
4111 if ( list [a] && a!=cpid)
4113 shift_lock (a,cpid, LERROR, LERROR, LSET);
4114 shift_lock (a,cpid, LWARNING, LWARNING, LSET);
4115 lock (a, LLOCK, LRELEASE, " ");
4116 lock (a, LERROR,LRELEASE, " ");
4117 lock (a, LWARNING,LRELEASE, " ");
4123 int kill_child_list (int *list)
4127 for (a=0; a<MAX_N_PID; a++)
4129 if (list[a]==1 && a!=cpid)
4137 int get_child_list (int pid,int *clist)
4148 add_information ( stderr, "Corrupted Lock System" );
4149 for (a=0; a<MAX_N_PID; a++)release_all_locks (a);
4154 lockf=lock2name (pid, LLOCK);
4155 if ( lockf && file_exists (NULL,lockf))
4157 list=file2list (lockf, "\n");
4160 while (list && list[a])
4162 n+=get_child_list (atoi(list[a++][1]), clist);
4164 free_arrayN ((void **)list, 3);
4168 if (!clist[pid]){clist[pid]=1; n++;}
4173 int kill_child_pid_pld()
4176 string2file (logfile, "a","\n----TC: %d kill ALL CHILDREN", getpid());
4178 // kill (0, SIGTERM); //send a sigterm to ALL children processes
4181 if ( !pidtable)return 0;
4187 for (a=0; a<MAX_N_PID; a++)
4189 if (pidtable[a][1] && pidtable[a][0]==cpid )
4191 if (debug_lock)fprintf ( stderr, "\n--- KILL %d from TC (%d)\n", a, getpid());
4193 pidtable[a][1]=pidtable[a][0]=0;
4194 kill((pid_t)a, SIGTERM);
4195 //lock (a, LLOCK, LRELEASE, "");
4196 shift_lock(a,getpid(),LERROR,LERROR, LSET);
4197 shift_lock(a,getpid(),LWARNING,LWARNING, LSET);
4209 void unpack_all_perl_script (char *script)
4215 while ( !strm(PerlScriptName[a], "EndList"))
4217 if (script==NULL || strm (script, "all") ||strm (script, "perl_script")|| strm (script,PerlScriptName[a]))
4219 fprintf ( stderr, "\nUnpack Script %s\n", PerlScriptName[a]);
4220 fp=vfopen ( PerlScriptName[a], "w");
4221 fprintf (fp, "%s\n%s\n", PERL_HEADER,PerlScriptFile[a]);
4223 sprintf ( command, "chmod u+x %s",PerlScriptName[a] );
4224 safe_system (command);
4228 myexit (EXIT_SUCCESS);
4234 else return atoi (c);
4237 int unpack_perl_script (char *name, char ***unpacked, int n)
4242 set_file2remove_extension(".pl", SET);
4245 if ( name==NULL) unpack_all_perl_script (NULL);
4246 while ( !strm(PerlScriptName[a], "EndList") && !strm ( name, PerlScriptName[a]))
4251 if ( strm(PerlScriptName[a], "EndList")|| (check_file_exists (name) && isexec(name) && satoi(getenv("RUN_LOCAL_SCRIPT"))) || (pg_is_installed (name) &&(satoi(getenv("RUN_LOCAL_SCRIPT"))) ))
4253 if ( check_file_exists (name) && ! strstr (name, "/") && isexec(name) && satoi(getenv ("RUN_LOCAL_SCRIPT")))
4255 add_warning ( stderr, "Local Copy of %s detected [Debug mode]. DELETE this file", name);
4256 sprintf ( unpacked[0][n], "%s", name);
4257 sprintf ( unpacked[1][n], "./%s", name);
4258 sprintf ( unpacked[2][n], "shell");
4260 else if ( pg_is_installed (name) &&satoi(getenv("RUN_LOCAL_SCRIPT")) )
4262 add_warning ( stderr, "Running external copy of %s [Debug mode].", name);
4263 sprintf ( unpacked[0][n], "%s", name);
4264 sprintf ( unpacked[1][n], "%s", name);
4265 sprintf ( unpacked[2][n], "local");
4267 else if ( strstr (name, "t_coffee"))
4269 /*Cygwin pb: t_coffee cannot call itself: making a local copy for recursion*/
4273 sprintf (buf, "cp `which %s` %s; chmod u+x %s",name, b2, b2);
4275 sprintf ( unpacked[0][n], "%s", name);
4276 sprintf ( unpacked[1][n], "%s", b2);
4277 sprintf ( unpacked[2][n], "shell");
4282 sprintf ( unpacked[0][n], "%s", name);
4283 sprintf ( unpacked[1][n], "%s", name);
4284 sprintf ( unpacked[2][n], "shell");
4290 sprintf ( unpacked[0][n], "%s", name);
4291 sprintf ( unpacked[1][n], "%s", vtmpnam(NULL));
4292 sprintf ( unpacked[2][n], "unpack");
4293 fp=vfopen (unpacked[1][n], "w");
4294 fprintf (fp, "%s\n%s\n", PERL_HEADER,PerlScriptFile[a]);
4296 printf_system_direct ("chmod u+x %s", unpacked[1][n]);
4300 set_file2remove_extension(".pl", UNSET);
4304 /*********************************************************************/
4309 /*********************************************************************/
4312 void set_command_line (char *cl)
4314 in_cl=vcalloc ( strlen (cl)+1, sizeof (char));
4315 sprintf (in_cl, "%s", cl);
4317 FILE * print_command_line (FILE *fp )
4319 fprintf ( fp, "# Command Line: %s [PROGRAM:%s]\n",in_cl, PROGRAM);
4322 int check_dir_getenv ( char *string);
4323 char *get_plugins_4_tcoffee_old ( char *mode)
4325 static char *plugins_4_tcoffee;
4327 if ( !plugins_4_tcoffee)plugins_4_tcoffee=vcalloc ( 1000, sizeof (char));
4329 if (mode)plugins_4_tcoffee[0]='\0';
4332 if ( plugins_4_tcoffee[0])return plugins_4_tcoffee;
4333 else if ( mode) sprintf (plugins_4_tcoffee, "%s", mode);
4334 else if ( check_dir_getenv ("PLUGINS_4_TCOFFEE"))
4336 sprintf (plugins_4_tcoffee, "%s", getenv ("PLUGINS_4_TCOFFEE"));
4340 sprintf (plugins_4_tcoffee, "%s/plugins/%s/", get_dir_4_tcoffee(), get_os());
4342 //if (!isdir (plugins_4_tcoffee)){printf_exit (EXIT_FAILURE, stderr, "ERROR: The declared plugin directory for T-Coffee is invalid: [%s] [FATAL:%s %s]", plugins_4_tcoffee, PROGRAM, VERSION); }
4343 return plugins_4_tcoffee;
4345 char *get_home_4_tcoffee ()
4347 static char *home_4_tcoffee;
4348 static char home[1000];
4351 if ( !home_4_tcoffee)
4352 home_4_tcoffee=vcalloc ( 1000, sizeof (char));
4354 if ( home_4_tcoffee[0])return home_4_tcoffee;
4355 else if ( check_dir_getenv ("HOME_4_TCOFFEE"))
4357 sprintf (home_4_tcoffee, "%s/", getenv ("HOME_4_TCOFFEE"));
4359 else if ( check_dir_getenv ("HOME"))
4361 sprintf (home_4_tcoffee, "%s/", getenv ("HOME"));
4362 sprintf (home, "%s/", home_4_tcoffee);
4364 else if ( check_dir_getenv ("TMP"))
4366 sprintf (home_4_tcoffee, "%s/", getenv ("TMP"));
4368 else if (check_dir_getenv ("TEMP"))
4370 sprintf (home_4_tcoffee, "%s/", getenv ("TEMP"));
4374 printf_exit (EXIT_FAILURE, stderr, "ERROR: Could not set a HOME directory.\nSet any of the following environement variables to some suitable location: HOME, HOME_4_TCOFFEE, TMP or TEMP [FATAL:%s]\n", PROGRAM);
4378 return home_4_tcoffee;
4380 char *get_dir_4_tcoffee()
4382 static char dir_4_tcoffee[1000];
4384 if (dir_4_tcoffee[0])return dir_4_tcoffee;
4387 if ( getenv ("UNIQUE_DIR_4_TCOFFEE"))sprintf (dir_4_tcoffee, "%s/", getenv("UNIQUE_DIR_4_TCOFFEE"));
4388 else if ( getenv ("DIR_4_TCOFFEE"))sprintf (dir_4_tcoffee, "%s/", getenv("DIR_4_TCOFFEE"));
4389 else sprintf ( dir_4_tcoffee, "%s/.t_coffee/",get_home_4_tcoffee());
4390 my_mkdir (dir_4_tcoffee);
4392 return dir_4_tcoffee;
4394 char *get_tmp_4_tcoffee ()
4396 static char tmp_4_tcoffee [1000];
4398 if ( tmp_4_tcoffee[0])return tmp_4_tcoffee;
4401 char *v=getenv("TMP_4_TCOFFEE");
4404 gethostname(host, 1023);
4405 if (getenv ("UNIQUE_DIR_4_TCOFFEE"))sprintf (tmp_4_tcoffee, "%s/", getenv("UNIQUE_DIR_4_TCOFFEE"));
4406 else if (v && strstr (v, "TMP"))sprintf (tmp_4_tcoffee, "%s/", getenv("TMP_4_TCOFFEE"));
4407 else if (v && strstr (v, "LOCAL"))sprintf (tmp_4_tcoffee, "%s/", getcwd(NULL,0));
4408 else if (v && strstr (v, "."))sprintf (tmp_4_tcoffee, "%s/", getcwd(NULL,0));
4409 else if (v)sprintf (tmp_4_tcoffee, "%s", v);
4410 else if (isdir("/var/tmp"))sprintf (tmp_4_tcoffee, "/var/tmp/");
4411 else sprintf (tmp_4_tcoffee, "%s/", getcwd(NULL,0));
4413 //now that rough location is decided, create the subdir structure
4416 sprintf (buf, "%s/t_coffee.tmp.%s/tmp.%d/", tmp_4_tcoffee, host,getpid());
4417 sprintf (tmp_4_tcoffee, "%s", buf);
4418 my_mkdir(tmp_4_tcoffee);
4419 my_rmdir(tmp_4_tcoffee);
4420 my_mkdir(tmp_4_tcoffee);
4424 return tmp_4_tcoffee;
4426 char *get_cache_4_tcoffee ()
4429 static char cache_4_tcoffee [1000];
4430 if ( cache_4_tcoffee[0])return cache_4_tcoffee;
4433 if (getenv ("UNIQUE_DIR_4_TCOFFEE"))sprintf (cache_4_tcoffee, "%s/", getenv("UNIQUE_DIR_4_TCOFFEE"));
4434 else if ( getenv ("CACHE_4_TCOFFEE"))sprintf (cache_4_tcoffee, "%s/", getenv("CACHE_4_TCOFFEE"));
4435 else sprintf ( cache_4_tcoffee, "%s/cache/", get_dir_4_tcoffee());
4437 my_mkdir(cache_4_tcoffee); /*Do not use mkdir: not yet initialized*/
4439 return cache_4_tcoffee;
4442 char *get_mcoffee_4_tcoffee ()
4444 static char mcoffee_4_tcoffee [1000];
4445 if ( mcoffee_4_tcoffee[0])return mcoffee_4_tcoffee;
4448 if (getenv ("UNIQUE_DIR_4_TCOFFEE"))sprintf (mcoffee_4_tcoffee, "%s/", getenv("UNIQUE_DIR_4_TCOFFEE"));
4449 else if ( getenv ("MCOFFEE_4_TCOFFEE"))sprintf (mcoffee_4_tcoffee, "%s/", getenv("MCOFFEE_4_TCOFFEE"));
4450 else sprintf ( mcoffee_4_tcoffee, "%s/mcoffee/", get_dir_4_tcoffee());
4451 my_mkdir (mcoffee_4_tcoffee);
4453 return mcoffee_4_tcoffee;
4455 char *get_methods_4_tcoffee ()
4457 static char methods_4_tcoffee [1000];
4458 if ( methods_4_tcoffee[0])return methods_4_tcoffee;
4461 if (getenv ("UNIQUE_DIR_4_TCOFFEE"))sprintf (methods_4_tcoffee, "%s/", getenv("UNIQUE_DIR_4_TCOFFEE"));
4462 else if ( getenv ("METHODS_4_TCOFFEE"))sprintf (methods_4_tcoffee, "%s/", getenv("METHODS_4_TCOFFEE"));
4463 else sprintf ( methods_4_tcoffee, "%s/methods/", get_dir_4_tcoffee());
4464 my_mkdir (methods_4_tcoffee);
4466 return methods_4_tcoffee;
4469 char *get_plugins_4_tcoffee ()
4471 static char plugins_4_tcoffee [1000];
4472 if ( plugins_4_tcoffee[0])return plugins_4_tcoffee;
4475 if (getenv ("UNIQUE_DIR_4_TCOFFEE"))sprintf (plugins_4_tcoffee, "%s/", getenv("UNIQUE_DIR_4_TCOFFEE"));
4476 else if (isdir4path (getenv("PLUGINS_4_TCOFFEE")))sprintf (plugins_4_tcoffee, "%s/", getenv("PLUGINS_4_TCOFFEE"));
4477 else sprintf (plugins_4_tcoffee, "%s/plugins/%s/", get_dir_4_tcoffee(), get_os());
4478 my_mkdir(plugins_4_tcoffee);
4480 return plugins_4_tcoffee;
4482 char *get_lockdir_4_tcoffee ()
4484 static char lockdir_4_tcoffee [1000];
4487 if ( lockdir_4_tcoffee[0])return lockdir_4_tcoffee;
4491 v=getenv ("LOCKDIR_4_TCOFFEE");
4492 if (getenv ("UNIQUE_DIR_4_TCOFFEE"))sprintf (lockdir_4_tcoffee, "%s/", get_tmp_4_tcoffee());
4493 else if (v)sprintf (lockdir_4_tcoffee, "%s/", v);
4494 else sprintf (lockdir_4_tcoffee, "%s/", get_tmp_4_tcoffee());
4498 return lockdir_4_tcoffee;
4507 else if ( get_int_variable ("n_core"))
4509 nproc=get_int_variable ("n_core");
4511 else if ( getenv ("NUMBER_OF_PROCESSORS_4_TCOFFEE"))
4513 nproc=atoi(getenv ("NUMBER_OF_PROCESSORS_4_TCOFFEE"));
4515 else if ( getenv ("NUMBER_OF_PROCESSORS"))
4517 nproc=atoi(getenv ("NUMBER_OF_PROCESSORS"));
4519 else if ( file_exists (NULL,"/proc/cpuinfo"))
4524 printf_system_direct ("grep \"^processor\" /proc/cpuinfo | wc -l>%s", tmp);
4525 sprintf ( string, "%s", file2string (tmp));
4527 nproc=atoi (string);
4541 static char os[100];
4544 if ( os[0])return os;
4550 printf_system_direct ("uname > %s", file);
4552 s=file2string (file);
4555 if (strstr (s, "cygwin"))sprintf ( os, "windows");
4556 else if ( strstr (s, "linux"))sprintf ( os, "linux");
4557 else if ( strstr (s, "osx"))sprintf ( os, "macosx");
4558 else if ( strstr (s, "darwin"))sprintf ( os, "macosx");
4559 else sprintf (os, "linux");
4570 int cputenv (char *string, ...)
4582 if (!string)return 0;
4584 cvsprintf (s, string);
4586 s2=vcalloc (strlen (s)+1, sizeof (char));
4587 sprintf ( s2, "%s", s);
4590 //vfree (s); //Potential leak
4595 int fcputenv (char *file, char *mode,char * string, ...)
4599 if (!string)return 0;
4601 if (!(fp=vfopen (file, mode)))return 0;
4603 va_start (ap, string);
4604 vfprintf (fp, string, ap);
4609 int isdir4path (char *p)
4612 if ( !p || access (p, F_OK)==-1 || access (p, W_OK)==-1 || access(p, R_OK)==-1 || access (p, X_OK)==-1)return 0;
4616 int check_dir_getenv ( char *string)
4619 return (isdir4path(p=getenv ( string)));
4626 int set_unique_dir_4_tcoffee (char *dir);
4627 int set_unique_dir_4_tcoffee (char *dir)
4629 static char **string;
4632 if ( !dir || !isdir(dir) || strm (dir, "no"))return 0;
4633 string=declare_char (10, 100);
4634 sprintf ( string[m++], "DIR_4_TCOFFEE=%s", dir);putenv (string[n++]);
4635 sprintf ( string[m++], "CACHE_4_TCOFFEE=%s", dir);putenv (string[n++]);
4636 sprintf ( string[m++], "TMP_4_TCOFFEE=%s", dir);putenv (string[n++]);
4637 sprintf ( string[m++], "PLUGINS_4_TCOFFEE=%s", dir);putenv (string[n++]);
4638 sprintf ( string[m++], "MCOFFEE_4_TCOFFEE=%s", dir);putenv (string[n++]);
4639 sprintf ( string[m++], "METHODS_4_TCOFFEE=%s", dir);putenv (string[n++]);
4648 void myexit (int signal)
4651 if (clean_exit_started==1)return; //protects processes while they are doing a clean exit
4652 global_exit_signal=signal;
4653 exit (global_exit_signal); //ONLY BARE EXIT!!!!!!!!!!!!!!
4657 static int n_warning;
4658 static char **warning_list;
4660 FILE *fatal_exit (FILE *fp,int exit_mode, char *string, ...)
4663 va_start (ap, string);
4664 vfprintf (fp, string, ap);
4669 static int warning_mode;
4670 int set_warning_mode ( int mode)
4676 int fprintf_error( FILE *fp, char *string, ...)
4680 cvsprintf (msg, string);
4681 msg=substitute ( msg, "\n", "");
4682 msg=substitute ( msg, "ERROR", " ");
4683 if (fp)fprintf ( fp, "\n--ERROR: %s\n", msg);
4684 if ( clean_exit_started) return EXIT_FAILURE;
4686 lock (getpid(), LERROR,LSET,"%d -- ERROR: %s\n",getpid(),msg);
4687 lock (getpid(), LERROR,LSET,"%d -- COM: %s\n",getpid(),in_cl);
4688 lock (getpid(), LERROR, LSET,"%d -- STACK: %d -> %d\n",getpid(), getppid(),getpid());
4691 return EXIT_FAILURE;
4693 void printf_exit (int exit_code, FILE *fp, char *string, ...)
4697 cvsprintf (msg, string);
4698 myexit(fprintf_error (fp,msg));
4703 FILE *add_warning (FILE *fp, char *string, ...)
4707 if ( warning_mode==NO || getenv("NO_WARNING_4_TCOFFEE"))return fp;
4711 cvsprintf (buf, string);
4712 if (fp)fprintf (fp, "\npid %d -- %s\n",getpid(), buf);
4713 if ( clean_exit_started)return fp;
4716 lock(getpid(),LWARNING, LSET, "%d -- WARNING: %s\n", getpid(),buf);
4721 FILE *add_information (FILE *fp, char *string, ...)
4725 if ( warning_mode==NO || getenv("NO_INFORMATION_4_TCOFFEE"))return fp;
4729 cvsprintf (buf, string);
4730 if (fp)fprintf (fp, "\npid %d -- %s\n",getpid(), buf);
4731 if ( clean_exit_started)return fp;
4734 lock(getpid(),LWARNING, LSET, "%d -- INFORMATION: %s\n", getpid(),buf);
4742 int count_n_res_in_array (char *array, int len)
4744 return count_n_symbol_in_array(array, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", len);
4746 int count_n_gap_in_array (char *array, int len)
4749 if ( len<=0 ||len>strlen(array) )l=strlen(array);
4752 return l- count_n_res_in_array (array,len);
4754 int count_n_symbol_in_array ( char *array, char *array_list, int len)
4759 if ( len<=0 ||len>strlen(array) )l=strlen(array);
4762 for ( a=0; a< l; a++)t+=is_in_set (array[a], array_list);
4766 char* count_strings_in_file ( char *in, char *out)
4770 char **list, **result;
4773 if (!out) out=vtmpnam (NULL);
4774 list=declare_char (count_n_line_in_file(in)+1, measure_longest_line_in_file (in)+1);
4777 fp=vfopen (in, "r");
4778 while ((c=fgetc (fp))!=EOF)
4781 fscanf (fp, "%s\n",list[n]);
4786 result=count_strings (list, n);
4790 fp=vfopen (out, "w");
4791 while (result[n])fprintf ( fp,"%s\n", result[n++]);
4794 free_char (list, -1);
4795 free_char (result, -1);
4800 int ** count_int_strings (int **array, int len, int s)
4805 sort_list_int (array,s, s,0, len-1);
4806 result=vcalloc (len, sizeof (int*));
4807 for (n=-1,a=0; a<len; a++)
4809 if (n==-1 || memcmp (array[a], result[n], sizeof (int)*s)!=0)
4812 result[n]=vcalloc (s+1, sizeof (int));
4813 memcpy (result[n],array[a], sizeof (int)*s);
4814 fprintf ( stdout, "*");
4818 sort_int (result, s+1, s, 0, n);
4819 result=vrealloc ( result, sizeof (int*)*n);
4823 char** count_strings ( char **array, int len)
4829 result=vcalloc (len+1, sizeof (char*));
4830 array=sort_string_array (array, len);
4832 for (c=0, a=0, n=0, cs=NULL; a< len; a++)
4834 if (cs == NULL || !strm (cs, array[a]))
4838 result[c]=vcalloc (strlen (cs)+20, sizeof (char));
4839 sprintf ( result[c], "%s %d", cs, n);
4849 result[c]=vcalloc (strlen (cs)+20, sizeof (char));
4850 sprintf ( result[c], "%s %d", cs, n);
4855 int get_first_non_white_char (char *name)
4857 if ( !name) return 0;
4858 else if ( !file_exists (NULL,name))return 0;
4864 fp=vfopen (name, "r");
4865 while (isspace ((c=fgetc (fp)))&& c!=EOF);
4870 int count_n_char_x_in_file(char *name, char x)
4876 fp=vfopen(name, "r");
4877 while ( (c=fgetc(fp))!=EOF)n+=(c==x);
4881 int count_n_char_in_file(char *name)
4887 fp=vfopen(name, "r");
4888 while ( (c=fgetc(fp))!=EOF){n++;}
4892 int count_n_line_in_file ( char *name )
4898 fp=vfopen(name, "r");
4899 while ( (c=fgetc(fp))!=EOF)n+=(c=='\n');
4903 int measure_longest_line_in_file ( char *name )
4911 fp=vfopen(name, "r");
4912 while ( (c=fgetc(fp))!=EOF)
4914 if ( c=='\n'){longest=MAX(longest, current+1);current=0;}
4917 longest=MAX(longest, current+1);
4928 string= vcalloc ( 500, sizeof ( char));
4931 while ( ( ch=getchar())!='\n' && a<500)
4935 if ( string[0]=='\0')
4948 int string_variable_isset (char *name)
4950 if (store_string_variable (name,NULL, ISSET)==NULL)return 0;
4953 char* set_string_variable (char *name, char * v)
4955 return store_string_variable (name, v, SET);
4957 char * get_string_variable (char *name)
4959 return store_string_variable (name,NULL, GET);
4961 char* unset_string_variable (char *name)
4963 return store_string_variable (name,0, UNSET);
4966 char* store_string_variable (char *name, char* v, int mode)
4968 static char **name_array, **val_array;
4977 if ( strm (name,name_array[a]))
4981 val_array[a]=vrealloc (val_array[a], strlen (v)+1);
4982 sprintf (val_array[a],"%s",v);
4984 else val_array[a]='\0';
4990 name_array=vcalloc (1, sizeof (char*));
4991 val_array=vcalloc (1, sizeof (char*));
4995 name_array=vrealloc (name_array, (n+1)*sizeof (char*));
4996 val_array=vrealloc (val_array, (n+1)*sizeof (char*));
4998 name_array[n]=vcalloc ( strlen (name)+1, sizeof (char));
4999 val_array[n]=vcalloc ( strlen (v)+1, sizeof (char));
5001 sprintf ( name_array[n], "%s", name);
5002 sprintf ( val_array[n], "%s", v);
5008 else if ( mode == ISSET)
5011 if ( strm (name_array[a], name))return (char *)1;
5013 else if ( mode == UNSET)
5016 if ( strm (name_array[a], name))
5018 name_array[a][0]='\0';
5019 val_array[a][0]='\0';
5022 add_warning (stdout, "Could not UNSET the value of %s. You must SET the value before it is used", name);
5028 if ( strm (name_array[a], name))
5029 return val_array[a];
5034 int int_variable_isset (char *name)
5036 return store_int_variable (name,0, ISSET);
5038 int set_int_variable (char *name, int v)
5040 return store_int_variable (name, v, SET);
5042 int get_int_variable (char *name)
5044 return store_int_variable (name, 0, GET);
5046 int unset_int_variable (char *name)
5048 return store_int_variable (name,0, UNSET);
5051 int store_int_variable (char *name, int v, int mode)
5053 static char **name_array;
5054 static int *val_array;
5062 if ( strm (name,name_array[a]))
5070 name_array=vcalloc (1, sizeof (char*));
5071 val_array=vcalloc (1, sizeof (int));
5075 name_array=vrealloc (name_array, (n+1)*sizeof (char*));
5076 val_array=vrealloc (val_array, (n+1)*sizeof (int));
5078 name_array[n]=vcalloc ( strlen (name)+1, sizeof (char));
5079 sprintf ( name_array[n], "%s", name);
5086 else if ( mode == ISSET)
5089 if ( strm (name_array[a], name))return 1;
5091 else if ( mode == UNSET)
5094 if ( strm (name_array[a], name))
5096 name_array[a][0]='\0';
5100 add_warning (stdout, "Could not UNSET the value of %s. You must SET the value before it is used", name);
5106 if ( strm (name_array[a], name))
5107 return val_array[a];
5114 char * get_proxy_from_env ()
5118 if ((proxy=get_string_variable ("cl_proxy"))){;}//Command line proxy always wins
5119 else if ((proxy=getenv ("http_proxy_4_TCOFFEE")));
5120 else if ((proxy=get_string_variable ("proxy")));//use default T-Coffee proxy
5121 else if ( getenv ("HTTP_proxy") && getenv ("http_proxy")){return getenv ("HTTP_proxy");}//get environement proxy
5122 else if ((proxy=getenv ("HTTP_proxy")));//id
5123 else if ((proxy=getenv ("http_proxy")));//id
5124 else if ((proxy=getenv ("HTTP_PROXY")));//id
5125 else if ((proxy=getenv ("ALL_proxy")));//id
5126 else if ((proxy=getenv ("all_proxy")));//id
5127 else if ((proxy=getenv ("ALL_PROXY")));//id
5129 if (proxy)set_proxy(proxy);
5131 {proxy=vcalloc (1, sizeof(char));}
5137 return get_proxy_from_env ();
5139 int set_proxy (char *proxy)
5142 if (!proxy) return 0;
5144 cputenv ("HTTP_proxy_4_TCOFFEE=%s", proxy);
5145 cputenv ("HTTP_proxy=%s", proxy);
5146 cputenv ("http_proxy=%s", proxy);
5147 cputenv ("HTTP_PROXY=%s", proxy);
5148 cputenv ("ALL_proxy=%s", proxy);
5149 cputenv ("ALL_PROXY=%s", proxy);
5150 cputenv ("all_proxy=%s", proxy);
5155 FILE* warning_msg(FILE*fp)
5159 msg=lock ( getpid(),LWARNING, LREAD,NULL);
5160 if (!msg) return fp;
5161 fprintf ( fp, "\n\n");
5162 fprintf ( fp, "*************************************************************************************************\n");
5163 fprintf ( fp, "* MESSAGES RECAPITULATION \n");
5164 fprintf ( fp, "%s",msg);
5165 fprintf ( fp, "*************************************************************************************************\n");
5168 FILE* stack_msg(FILE*fp)
5172 error=lock ( getpid(),LERROR, LREAD,NULL);
5173 if (!error)return fp;
5174 fprintf ( fp, "\n\n");
5175 fprintf ( fp, "*************************************************************************************************\n");
5176 fprintf ( fp, "* FULL TRACE BACK PID: %d \n", getpid());
5177 fprintf ( fp, "%s", error);
5178 fprintf ( fp, "*************************************************************************************************\n");
5181 FILE * install_msg(FILE *fp)
5183 fprintf ( fp, "\n\n");
5184 fprintf ( fp, "*************************************************************************************************\n");
5185 fprintf ( fp, "* CONFIGURATION: Missing Package \n");
5186 fprintf ( fp, "* \n");
5187 fprintf ( fp, "* \n");
5188 fprintf ( fp, "* It looks like a package is either not installed or not on your PATH\n (see trace)\n");
5189 fprintf ( fp, "* If it is on your path, declare its location:\n");
5190 fprintf ( fp, "* export PATH=<executable directory>:$PATH\n");
5191 fprintf ( fp, "* Make this permanent by adding this line to the file\n");
5192 fprintf ( fp, "* ~/.bashrc\n");
5193 fprintf ( fp, "* If this package is not installed but supported you can try to install it via t_coffee:\n");
5194 fprintf ( fp, "* t_coffee -other_pg install <pakage name>\n");
5195 fprintf ( fp, "* Otherwise you must install it yourself\n");
5196 fprintf ( fp, "*************************************************************************************************\n");
5200 FILE* proxy_msg(FILE*fp)
5202 fprintf ( fp, "\n\n");
5203 fprintf ( fp, "*************************************************************************************************\n");
5204 fprintf ( fp, "* CONFIGURATION: Faulty Network OR Missing Proxy \n");
5205 fprintf ( fp, "* \n");
5206 fprintf ( fp, "* \n");
5207 fprintf ( fp, "* It looks like you cannot access the network\n");
5208 fprintf ( fp, "* Check that your network is up and running...\n");
5209 fprintf ( fp, "* If you are behind a firewall, you must enter your proxy address to use webservices\n");
5210 fprintf ( fp, "* This address is usualy something like: http://some.place.here:8080\n");
5211 fprintf ( fp, "* Todo this via the command line:\n");
5212 fprintf ( fp, "* \n");
5213 fprintf ( fp, "* -proxy=<youremail> \n");
5214 fprintf ( fp, "* \n");
5215 fprintf ( fp, "*To make it permanent:\n");
5216 fprintf ( fp, "* export PROXY_4_TCOFFEE=<your email>\n");
5217 fprintf ( fp, "*Add this line to either:\n");
5218 fprintf ( fp, "* <yourhome>/.bashrc\n");
5219 fprintf ( fp, "* OR %s/.t_coffee_env\n", get_dir_4_tcoffee());
5220 fprintf ( fp, "*************************************************************************************************\n");
5224 FILE* email_msg(FILE*fp)
5226 fprintf ( fp, "\n\n");
5227 fprintf ( fp, "*************************************************************************************************\n");
5228 fprintf ( fp, "* CONFIGURATION: Missing Email \n");
5229 fprintf ( fp, "* \n");
5231 fprintf ( fp, "* This mode of T-Coffee uses the EBI BLAST webservices. The EBI requires a valid E-mail \n");
5232 fprintf ( fp, "* address for this service to be used (check: www.ebi.ac.uk/Tools/webservices/). \n");
5234 fprintf ( fp, "* \n");
5235 fprintf ( fp, "* To provide the email, add to the command line:*\n");
5236 fprintf ( fp, "* \n");
5237 fprintf ( fp, "* -email=<youremail> *\n");
5238 fprintf ( fp, "*To make it permanent:\n");
5239 fprintf ( fp, "* export EMAIL_4_TCOFFEE=<your email>\n");
5240 fprintf ( fp, "*Add this line to either:\n");
5241 fprintf ( fp, "* ~/.bashrc\n");
5242 fprintf ( fp, "* OR %s/.t_coffee_env\n", get_dir_4_tcoffee());
5243 fprintf ( fp, "*************************************************************************************************\n");
5248 void update_error_dir()
5257 void dump_tcoffee(char *target, char *nature)
5265 if ((fp=fopen (target, "w")))
5267 fprintf (fp, "<T-CoffeeApplicationDump>\n<nature>%s</nature>\n", nature);
5268 fprintf (fp, "<program>%s</program>\n", PROGRAM);
5269 fprintf (fp, "<version>%s</version>\n",VERSION);
5270 if ((f=strstr (in_cl, "-dump")))f[0]='\0';
5271 fprintf (fp, "<cl>%s</cl>\n",in_cl);
5272 fprintf (fp, "<stack>\n");
5274 fprintf (fp, "</stack>\n<warning>\n");
5276 fprintf (fp, "</warning>\n");
5279 list=string2list (in_cl);
5280 out_list=file2string (get_string_variable ("dump_output_file_list"));
5282 for (a=1; a<atoi (list[0]); a++)
5287 if (out_list && estrstr(out_list, "#%s#",s))continue;
5288 if (file_exists (NULL,s))
5291 fprintf (fp, "<file>\n<stream>input</stream>\n");
5292 fprintf (fp, "<name>%s</name>\n",s);
5293 fprintf (fp, "<content>\n");
5294 while ((c=fgetc(fp2))!=EOF)fprintf ( fp, "%c", c);
5296 fprintf (fp, "</content>\n</file>\n");
5300 if ((f=get_string_variable ("dump_output_file")))
5305 if ((fp2=fopen (f, "r"))!=NULL)
5307 while ((c=fgetc (fp2))!=EOF)fprintf (fp, "%c",c);
5313 fprintf (fp, "<environement>\n");
5315 printf_system_direct("printenv >> %s", target);
5316 fp=fopen (target, "a");
5317 fprintf (fp, "</environement>\n</T-CoffeeApplicationDump>");
5320 fprintf ( stderr, "\n#----- Dumped File: %s\n",target);
5322 else fprintf ( stderr, "\n#----- Could NOT Produce Dump File: %s -- Sorry \n", target);
5326 void dump_error_file()
5337 sprintf ( target, "%s",getenv("ERRORFILE_4_TCOFFEE"));
5338 dump_tcoffee (target, "error");
5341 if ((fp=fopen (target, "w")))
5344 fprintf ( fp, "\n######### RUN_REPORT START ######");
5345 fprintf ( fp, "\n######### PROGRAM_VERSION START ######");
5346 fprintf ( fp, "\n %s, %s", PROGRAM, VERSION);
5347 fprintf ( fp, "\n######### PROGRAM_VERSION END ######");
5349 fprintf ( fp, "\n######### COMMAND_LINE START ######");
5350 fprintf ( fp, "\n%s", in_cl);
5351 fprintf ( fp, "\n######### COMMAND_LINE END ######\n");
5352 fprintf ( fp, "\n######### MESSAGES START ######\n");
5357 fprintf ( fp, "\n######### MESSAGES END ######\n");
5358 fprintf ( fp, "\n######### FILES START ######\n");
5360 list=string2list (in_cl);
5361 for (a=1; a<atoi (list[0]); a++)
5366 if (file_exists (NULL,s))
5369 fprintf ( fp, "\n************* Start_Input_File: %s *************\n", s);
5370 while ((c=fgetc(fp2))!=EOF)fprintf ( fp, "%c", c);
5372 fprintf ( fp, "\n************* End_Input_File: %s *************", s);
5376 fprintf ( fp, "\n######### FILES END ######\n");
5377 fprintf ( fp, "\n######### ENVIRONEMENT ######\n");
5379 printf_system_direct("printenv >> %s", target);
5381 fprintf ( stderr, "\n#----- Dumped ErrorFile: %s\n",target);
5383 else fprintf ( stderr, "\n#----- Could NOT Dumpe ErrorFile: %s -- Sorry \n", target);
5389 FILE* error_msg(FILE*fp )
5391 if ( no_error_report)return fp;
5393 fprintf( fp,"\n\t******************************************************************");
5394 fprintf( fp, "\n\t* Abnormal Termination");
5395 fprintf( fp, "\n\t* Job NOT Completed:[%s, %s]",PROGRAM, VERSION);
5396 fprintf( fp, "\n\t* Please CHECK: ");
5397 fprintf( fp, "\n\t* \t-1 The format of your Input Files ");
5398 fprintf( fp, "\n\t* \t-2 The parameters ");
5399 fprintf( fp, "\n\t* \t-3 The use of special characters in sequence names:");
5400 fprintf( fp, "\n\t* \t\t (@, |, %%...)");
5402 fprintf( fp, "\n\t* \t-4 The Online Doc (%s) ", URL);
5403 fprintf( fp, "\n\t* \t-5 Send the file:");
5404 fprintf (fp, "\n\t*");
5405 fprintf (fp, "\n\t*\t %s ", getenv("ERRORFILE_4_TCOFFEE"));
5406 fprintf (fp, "\n\t* to:");
5407 fprintf( fp, "\n\t* \t\t%s",EMAIL);
5409 fprintf( fp, "\n\t* If you run T-Coffee over the WEB:");
5410 fprintf( fp, "\n\t* \tWindows Cut and Paste is sometimes erratic and");
5411 fprintf( fp, "\n\t* \tit can loose carriage returns. If you suspect this,");
5412 fprintf( fp, "\n\t* \ttry to cut and paste through an intermediate application");
5413 fprintf( fp, "\n\t* \t(word pad) and inspect the results\n\n");
5414 fprintf( fp, "\n\t* CONFIDENTIALITY:");
5415 fprintf( fp, "\n\t* \tThe File %s may contain your personnal DATA", getenv("ERRORFILE_4_TCOFFEE"));
5416 fprintf( fp, "\n\t* \tRemove ALL confidential DATA from this file BEFORE sending it");
5417 fprintf( fp, "\n\t******************************************************************\n");
5418 print_command_line(fp);
5423 char *get_email_from_env ()
5426 if ( (email=get_string_variable ("cl_email")));
5427 else if ( (email=get_string_variable ("email")));
5428 else if ( (email=getenv ("EMAIL_4_TCOFFEE")));
5429 else if ( (email=getenv ("EMAIL")));
5430 else email=vcalloc ( 1, sizeof (char));
5434 int set_email (char *email)
5436 if (!email) return 0;
5438 cputenv ("EMAIL_4_TCOFFEE=%s", email);
5439 cputenv ("EMAIL=%s",email);
5443 char *chomp (char *name)
5446 while ( name[a]!='\n' && name[a]!='\0')a++;
5450 static Tmpname *tmpname;
5451 static Tmpname *ntmpname;
5453 static int n_tmpname;
5454 static int file2remove_flag;
5456 char *set_file2remove_extension (char *extension, int mode)
5458 static char ext[100];
5459 if (mode==SET)sprintf (ext, "%s", extension);
5460 else if ( mode==UNSET) ext[0]='\0';
5461 else if ( mode==GET);
5464 int flag_file2remove_is_on ()
5466 return file2remove_flag;
5468 void set_file2remove_on()
5472 void set_file2remove_off()
5477 char *add2file2remove_list (char *name)
5481 if ( !tmpname || !name)ntmpname=tmpname=vcalloc ( 1, sizeof (Tmpname));
5482 else if (!ntmpname->name);
5483 else ntmpname=ntmpname->next=vcalloc ( 1, sizeof (Tmpname));
5485 if (!name) return NULL;
5487 ntmpname->name=vcalloc(strlen(name)+1, sizeof (char));
5489 sprintf (ntmpname->name, "%s", name);
5490 return ntmpname->name;
5492 //char *short_tmpnam_2(char *s);//used to generate very compact tmp names
5493 void initiate_vtmpnam (char *file)
5495 add2file2remove_list (NULL);
5498 char *vtmpnam ( char *s1)
5504 standard_initialisation(NULL, NULL);
5506 s=vcalloc ( VERY_LONG_STRING, sizeof (char));
5511 s2=add2file2remove_list (s);
5512 if (s!=s2)vfree (s);
5513 if (s1){sprintf (s1, "%s",s2);return s1;}
5519 int get_vtmpnam2_root()
5521 int MAX_TMPNAM_ROOT=10000;
5528 v=rand()%MAX_TMPNAM_ROOT;
5532 char *tmpnam_2 (char *s)
5536 char buf[VERY_LONG_STRING];
5537 static char root2[VERY_LONG_STRING];
5538 static char *tmpdir;
5539 static int name_size;
5543 char *vtmpnam_prefixe;
5545 name_size=MAX( 2*L_tmpnam, MAXNAMES*2)+1;
5546 root=get_vtmpnam2_root();
5547 sprintf ( root2, "%d%d_", root, (int)getpid());
5549 vtmpnam_prefixe=vcalloc (strlen (root2)+strlen (get_tmp_4_tcoffee())+2, sizeof (char));
5550 sprintf (vtmpnam_prefixe, "%s/%s", get_tmp_4_tcoffee(), root2);
5551 set_string_variable ("vtmpnam_prefixe1", vtmpnam_prefixe);
5552 set_string_variable ("vtmpnam_prefixe2", root2);
5553 vfree (vtmpnam_prefixe);
5557 tmpdir=get_tmp_4_tcoffee();
5559 sprintf (buf, "%s/%s%d_TCtmp%s",tmpdir,root2, file++,set_file2remove_extension (NULL, GET));
5560 if ( strlen(buf)>=name_size)s=vrealloc (s,(strlen(buf)+1)*sizeof (char));
5561 sprintf (s, "%s", buf);
5564 char *short_tmpnam_2(char *s)
5568 char buf[VERY_LONG_STRING];
5569 static char root2[VERY_LONG_STRING];
5571 static int name_size;
5575 char *vtmpnam_prefixe;
5577 name_size=MAX( 2*L_tmpnam, MAXNAMES*2)+1;
5578 root=get_vtmpnam2_root();
5579 sprintf ( root2, "%d%d", root,getpid());
5581 vtmpnam_prefixe=vcalloc (strlen (root2)+strlen (get_tmp_4_tcoffee())+2, sizeof (char));
5582 sprintf (vtmpnam_prefixe, "%s", root2);
5583 set_string_variable ("vtmpnam_prefixe1", vtmpnam_prefixe);
5584 set_string_variable ("vtmpnam_prefixe2", root2);
5585 vfree (vtmpnam_prefixe);
5587 if (!s) return NULL;
5589 sprintf (buf, "%s%d%s",root2, file++,set_file2remove_extension (NULL, GET));
5590 if ( strlen(buf)>=name_size)s=vrealloc (s,(strlen(buf)+1)*sizeof (char));
5591 sprintf (s, "%s", buf);
5595 char *vremove2 (char *s)
5597 char list_file[1000];
5602 //Remove filenames with a wildcard
5604 sprintf (list_file, "list_file_%d", (int)getpid());
5605 printf_system_direct("ls -1 %s>%s 2>/dev/null", s, list_file);
5606 list=file2list (list_file, " ");
5609 while (list && list[a])
5611 if ( file_exists (NULL,list[a][1]))
5613 vremove (list[a][1]);
5617 vremove (list_file);
5620 void safe_remove (char *s)//remove even if the file is partly unaccessible
5626 else if (!(fp=fopen (s, "w")))return;
5634 char *vremove (char *s)
5638 if ( s && strstr (s, "*"))return vremove2(s);
5639 else if ( !s || !file_exists(NULL,s) ) return NULL;
5640 else if ( isdir (s))
5652 int log_function ( char *fname)
5656 if ( file_exists (NULL,error_file))
5659 printf_system_direct ("cp %s %s", error_file, fname);
5661 fprintf( stderr,"\n\t******************************************************************");
5662 fprintf( stderr, "\n\t* Full Log of [%s, %s] in File [%s]",PROGRAM, VERSION, fname);
5663 fprintf( stderr, "\n\t******************************************************************\n");
5670 FILE *NFP;/*Null file pointer: should only be open once*/
5672 /*********************************************************************/
5674 /* CACHE_FUNCTION */
5677 /*********************************************************************/
5679 char * prepare_cache ( const char *mode)
5681 cache =vcalloc ( 10000, sizeof(char));
5683 if (strm (mode, "use"))
5685 sprintf (cache, "%s",get_cache_4_tcoffee());
5688 else if ( strm (mode, "ignore") || strm (mode, "no"))
5691 cache=vtmpnam(cache);
5692 strcat (cache, "/");
5693 printf_system_direct ("mkdir %s",cache);
5696 else if ( strm (mode, "update"))
5698 cache=vtmpnam(cache);
5699 strcat (cache, "/");
5700 printf_system_direct ("mkdir %s",cache);
5702 else if ( strm (mode, "local"))
5708 sprintf ( cache, "%s/",mode);
5715 char * get_cache_dir()
5717 if ( cache==NULL){cache=vcalloc (1, sizeof (char));cache[0]='\0';}
5721 void update_cache ()
5723 char old_cache[1000];
5725 sprintf ( old_cache, "%s", get_cache_dir());
5726 prepare_cache( "use");
5727 printf_system_direct ("mv %s* %s",old_cache, get_cache_dir());
5728 printf_system_direct ("rmdir %s",old_cache);
5732 if (getenv4debug ("DEBUG_TMP_FILE"))
5734 fprintf ( stderr, "\n[DEBUG_TMP_FILE:%s] TEMPORARY CACHE HAS NOT Been Removed:\n\t%s\n", PROGRAM,get_cache_dir());
5739 printf_system_direct ("rm -r %s",get_cache_dir());
5745 FILE *fopenN ( char *fname, char *mode, int max_n_tries, int delay);
5746 FILE * vfopen ( char *name_in, char *mode)
5750 int tolerate_mistake;
5756 static char *stdin_file;
5759 if ( !name_in)return NULL;
5760 if (!name){name=vcalloc (1000, sizeof (char));}
5761 if (!name2){name2=vcalloc (1000, sizeof (char));}
5763 sprintf ( name, "%s", name_in);
5764 tild_substitute (name, "~", get_home_4_tcoffee());
5766 get_new_name=tolerate_mistake=0;
5767 if ( mode[0]=='g'){get_new_name=1; mode++;}
5768 else if ( mode[0]=='t'){tolerate_mistake=1;mode++;}
5769 /*Use the cached version from CACHE_4_TCOFFEE*/
5770 else if ( mode[0]=='c'){cache_used=1;mode++;}
5772 if (name==NULL ||strm5 ( name, "no","NO","No","NULL","/dev/null") || strm2 (name, "no_file", "NO_FILE"))
5774 if ( NFP==NULL)NFP=fopen (NULL_DEVICE, mode);
5777 else if ( strm3 (name,"stderr","STDERR","Stderr"))return stderr;
5778 else if ( strm3 (name,"stdout","STDOUT","Stdout"))return stdout;
5779 else if ( strm3 ( name, "stdin","STDIN","Stdin"))
5783 stdin_file=vtmpnam (NULL);
5784 tmp_fp=vfopen ( stdin_file, "w");
5785 while ( (c=fgetc(stdin))!=EOF)fprintf (tmp_fp, "%c", c);
5788 return vfopen (stdin_file, "r");
5791 else if ( strm (name, "") && (strm (mode, "w") ||strm (mode, "a")) )return stdout;
5792 else if ( strm (name, "") && strm (mode, "r"))return stdin;
5793 else if ( (fp= fopen ( name, mode))==NULL)
5795 if ( strcmp (mode, "r")==0 && cache_used==0)
5797 sprintf ( name2, "%s%s",get_cache_dir(), name);
5798 return vfopen ( name2, "cr");
5800 else if ( strcmp (mode, "r")==0 && cache_used==1)
5802 fprintf (stderr, "\n--COULD NOT READ %s\n", name);
5803 if ( get_new_name){fprintf ( stderr, "\nNew name: ");return vfopen (input_name(), mode-1);}
5804 else if ( tolerate_mistake)return NULL;
5807 myexit(fprintf_error (stderr, "\nFORCED EXIT (NON INTERACTIVE MODE pid %d)\n", getpid()));
5810 else if ( strcmp (mode, "a")==0 && cache_used==0)
5812 sprintf ( name2, "%s%s",get_cache_dir(), name);
5813 return vfopen ( name, "ca");
5815 else if ( strcmp (mode, "a")==0 && cache_used==1)
5817 fprintf (stderr, "\nCOULD NOT Append anything to %s\n", name);
5818 if ( get_new_name){fprintf ( stderr, "\nNew name: ");return vfopen (input_name(), mode-1);}
5819 else if ( tolerate_mistake)return NULL;
5822 myexit(fprintf_error (stderr, "\nFORCED EXIT (NON INTERACTIVE MODE pid %d)\n", getpid()));
5825 else if ( strcmp (mode, "w")==0)
5827 fprintf (stderr, "\nCANNOT WRITE %s\n", name);
5828 if ( get_new_name==1){fprintf ( stderr, "\nNew name: ");return vfopen (input_name(), mode-1);}
5829 else if ( tolerate_mistake)return NULL;
5832 myexit(fprintf_error (stderr, "\nFORCED EXIT (NON INTERACTIVE MODE pid %d): %s %s\n", getpid(),(strcmp ( mode, "r")==0)?"READ":"WRITE", name));
5842 FILE *fopenN ( char *fname, char *mode, int max_n_tries, int delay)
5847 for (a=0; a< max_n_tries; a++)
5849 if ((fp=fopen (fname, mode))) return fp;
5851 HERE ("---- failed opening: %s", fname);
5856 FILE * vfclose ( FILE *fp)
5858 if ( fp==NFP)return NULL;
5859 if ( fp==stdout)return stdout;
5860 if ( fp==stderr)return stderr;
5861 if ( fp==stdin) return stdin;
5862 if ( fp==NULL)return NULL;
5868 int echo ( char *string, char *fname)
5873 prints the content of string into file fname
5876 string= string to print
5877 fname =name of the file to create
5882 fp=vfopen ( fname, "w");
5883 fprintf (fp, "%s", string);
5889 int file_cat ( char *from, char *to)
5892 //appends the content of file1 to file 2
5893 if (!(fp=vfopen (to, "a")))return 0;
5894 if (!display_file_content (fp, from)) return 0;
5899 FILE* display_file_content (FILE *output, char *name)
5903 if ( !name || !file_exists (NULL,name) || !(fp=vfopen (name, "r")))return NULL;
5904 while ( (c=fgetc(fp))!=EOF)fprintf (output,"%c", c);
5909 char ***file2list ( char *name, char *sep)
5911 /*Rturns an array where
5913 list[0][0]: number of words
5914 list[0][1]:first word;
5917 char **lines, ***list;
5920 lines=file2lines (name);
5921 if (!lines) return NULL;
5926 list=vcalloc ( n+1, sizeof (char**));
5927 for ( a=1; a<n; a++)
5930 list[a-1]=string2list2(lines[a], sep);
5934 free_arrayN((void**)lines, 2);
5937 char **file2lines (char *name)
5940 lines[1]->first_line
5946 string=file2string (name);
5947 if ( !string) return NULL;
5950 lines=string2list2(string, "\n");
5955 int string2file (char *file, char *mode, char *string,...)
5960 if (!file) return 0;
5961 else if ( !mode) return 0;
5962 else if ( !(fp=vfopen (file, mode)))return 0;
5963 va_start (ap, string);
5964 vfprintf (fp, string, ap);
5970 char *file2string (char *name)
5976 if (!name || !file_exists (NULL,name))return NULL;
5980 if ((fp=fopen (name, "r")))
5982 while ( (c=fgetc(fp))!=EOF)a++;
5987 string=vcalloc (a+1, sizeof (char));
5989 if ((fp=fopen (name, "r")))
5991 while ( (c=fgetc(fp))!=EOF)string[a++]=c;
6000 int get_cl_param (int argc, char **argv, FILE **fp,char *para_name, int *set_flag, char *type, int optional, int max_n_val,char *usage, ...)
6007 set_flag set to 1 if param set;
6008 para_type F, I, S, R_FN (read_file, name), W_FN (written file, name), R_FP (pointer)
6009 max_n_val maximum number of values;
6010 optional 1 for yes, 0 for no
6011 usage usage list with optional value;
6012 val pointer to the varaible holding the value(s)
6013 default1 default value (if value id not there)
6014 default2 default value if the flag is there but no value set ("")indicates an error
6015 range_left min value ( "any" for any);
6016 range_right max_value ( "any" for any);
6023 float *float_val=NULL;
6024 char **string_val=NULL;
6031 char *default_value1;
6032 char *default_value2;
6036 static char **parameter_list;
6037 static int number_of_parameters;
6039 char **para_name_list;
6053 /*CHECK THAT ALL THE PARAM IN ARG EXIST*/
6054 if ( para_name==NULL)
6056 for ( a=1; a< argc; a++)
6058 if ( is_parameter ( argv[a]))
6060 if (strstr (argv[a], "help"))myexit (EXIT_SUCCESS);
6061 else if ( name_is_in_list ( argv[a], parameter_list, number_of_parameters, STRING)==-1)
6063 myexit(fprintf_error ( stderr, "\n%s IS NOT A PARAMETER OF %s [FATAL/%s %s]\n",argv[a], argv[0], argv[0], VERSION));
6070 free_char (parameter_list,-1);
6074 if ( parameter_list==NULL)parameter_list=declare_char(MAX_N_PARAM,STRING);
6075 para_name_list=get_list_of_tokens(para_name,NULL, &n_para_name);
6076 for ( a=0; a< n_para_name; a++)
6078 sprintf ( parameter_list[number_of_parameters++],"%s", para_name_list[a]);
6080 free_char(para_name_list,-1);
6087 va_start (ap, usage);
6089 if (strm3 (type, "S","R_F","W_F"))
6090 string_val=va_arg(ap, char**);
6091 else if (strm2 (type, "D","FL"))
6092 int_val=va_arg(ap, int*);
6093 else if (strm (type, "F"))
6094 float_val=va_arg(ap, float*);
6096 myexit (EXIT_FAILURE);
6100 default_value1=va_arg(ap, char*);
6101 default_value2=va_arg(ap, char*);
6102 range_left =va_arg(ap, char*);
6103 range_right =va_arg(ap, char*);
6107 para_name_list=get_list_of_tokens(para_name, NULL, &n_para_name);
6108 for ( a=0; a<n_para_name; a++)
6110 if ( (pos=name_is_in_list(para_name_list[a], argv,argc, STRING))!=-1)break;
6112 free_char (para_name_list,-1);
6114 if ( (name_is_in_list("-help" , argv,argc ,STRING)!=-1) && (argc==2 || (name_is_in_list( para_name , argv,argc ,STRING)!=-1)))
6117 fprintf ( stderr, "PARAMETER : %s\n", para_name);
6118 fprintf ( stderr, "USAGE : %s\n", usage);
6119 fprintf ( stderr, "MAX_N_VALUES: %d\n", max_n_val);
6120 fprintf ( stderr, "DEFAULT : %s OR %s (when flag set)\n", default_value1, default_value2);
6121 fprintf ( stderr, "RANGE : [%s]...[%s]\n", range_left,(strm(range_right,"any"))?"":range_right);
6122 fprintf ( stderr, "TYPE : %s\n\n", type);
6125 else if ( name_is_in_list ("-help" , argv,argc ,STRING)!=-1)
6129 else if (para_name[0]!='-')
6131 myexit(fprintf_error ( stderr, "\nWRONG PARAMETER DEFINITION %s Must Start with a dash", para_name));
6136 if ( optional==OPTIONAL)
6139 para_val=get_list_of_tokens(default_value1, NULL, &n_para_val);
6141 for (n_para=0; n_para<n_para_val && !strm (default_value1, "NULL"); n_para++)
6143 if ( strm (para_val[n_para], ""))
6148 else if ( strm (type, "FL"))
6150 set_flag[0]=atoi(para_val[n_para]);
6153 else if (strm3 (type, "S", "R_F","W_F"))
6156 sprintf ( string_val[n_para], "%s",para_val[n_para]);
6158 else if ( strm (type, "D"))
6159 int_val[n_para]=atoi(para_val[n_para]);
6160 else if ( strm (type, "F"))
6161 float_val[n_para]=atof(para_val[n_para]);
6163 free_char (para_val, -1);
6165 if (n_para==0 && strm3(type, "S","W_F","R_F") && strm (default_value1, "NULL"))
6167 vfree (string_val[0]);
6171 else if (n_para==0 && strm (type, "D") && strm (default_value1, "NULL"))int_val[0]=0;
6172 else if (n_para==0 && strm (type, "F") && strm (default_value1, "NULL"))float_val[0]=0;
6177 myexit(fprintf_error ( stderr, "\nParameter %s is not optional",para_name));
6184 for (a=pos+1; a< argc; a++)
6186 if ( is_parameter(argv[a]))break;
6189 if ( n_para>=max_n_val)
6194 if ( !(strm ( argv[a], "NULL")))
6196 if ( strm3(type, "S", "R_F", "W_F"))
6198 sprintf ( string_val[n_para],"%s", argv[a]);
6200 else if (strm (type, "D"))
6202 int_val[n_para]=atoi(argv[a]);
6204 else if (strm ( type,"F"))
6206 float_val[n_para]=atof(argv[a]);
6213 if ( n_para==0 && !strm2(default_value2,"","NULL") && !strm(type, "FL"))
6215 para_val=get_list_of_tokens(default_value2, NULL, &n_para_val);
6216 for ( n_para=0; n_para<n_para_val; n_para++)
6218 if ( strm3(type, "S", "R_F", "W_F"))sprintf ( string_val[n_para],"%s", para_val[n_para]);
6219 else if (strm (type, "D"))int_val [n_para]=atoi(para_val[n_para]);
6220 else if (strm ( type,"F"))float_val[n_para]=atof(para_val[n_para]);
6222 free_char (para_val,-1);
6224 else if (n_para==0 && strm (type, "FL"));
6225 else if (n_para==0 && strm3(type, "S","W_F","R_F") && strm (default_value2, "NULL")){vfree (string_val[0]);string_val[0]=NULL;}
6226 else if (n_para==0 && strm (type, "D") && strm (default_value2, "NULL"))int_val[0]=0;
6227 else if (n_para==0 && strm (type, "F") && strm (default_value2, "NULL"))float_val[0]=0;
6228 else if (n_para==0 && strm (default_value2, ""))
6230 myexit(fprintf_error ( stderr, "\nParam %s needs a value [FATAL/%s]", para_name, argv[0]));
6236 /*Check That The Parameters are in the Good Range*/
6238 pv_l=get_list_of_tokens( range_left , NULL, &n_pv_l);
6239 pv_r=get_list_of_tokens( range_right, NULL, &n_pv_r);
6241 for ( a=0; a< n_para; a++)
6243 if ( strm (type, "R_F") && !check_file_exists(string_val[a]) && !check_file_exists(string_val[a]+1))
6245 myexit(fprintf_error ( stderr, "PARAM %s: File %s does not exist [FATAL/%s]\n",para_name,string_val[a], argv[0]));
6248 else if ( strm (pv_l[0], "any"));
6249 else if ( strm (type, "D"))
6253 min=(double)atoi(pv_l[0]);
6254 max=(double)atoi(pv_r[0]);
6255 if ( int_val[a]<min || int_val[a]>max)
6257 myexit(fprintf_error ( stderr, "\n%s out of range [%d %d] [FATAL/%s]\n", para_name, (int)min, (int)max,argv[0]));
6263 sprintf ( value, "%d", int_val[a]);
6264 if ( name_is_in_list(value, pv_l, n_pv_l, STRING)==-1)
6265 fprintf ( stderr, "\n%s out of range [%s: ", para_name, value);
6266 print_array_char (stderr, pv_l, n_pv_l, " ");
6267 fprintf ( stderr, "\n");
6268 myexit(EXIT_FAILURE);
6271 else if ( strm (type, "F"))
6275 min=(double)atof(range_left);
6276 max=(double)atof(range_right);
6277 if ( float_val[a]<min || float_val[a]>max)
6279 myexit(fprintf_error ( stderr, "\n%s out of range [%f %f] [FATAL/%s]\n", para_name, (float)min, (float)max,argv[0]));
6285 sprintf ( value, "%f", float_val[a]);
6286 if ( name_is_in_list(value, pv_l, n_pv_l, STRING)==-1)
6287 fprintf ( stderr, "\n%s out of range [%s: ", para_name, value);
6288 print_array_char (stderr, pv_l, n_pv_l, " ");
6289 fprintf ( stderr, "\n");
6298 fprintf (fp[0], "%-15s\t%s\t[%d] ", para_name, type, set_flag[0]);
6299 for (a=0; a<n_para; a++)
6301 if ( strm3 ( type, "S", "R_F", "W_F"))fprintf ( fp[0], "\t%s", string_val[a]);
6302 else if ( strm ( type, "D"))fprintf ( fp[0], "\t%d ", int_val[a]);
6303 else if ( strm ( type, "F"))fprintf ( fp[0], "\t%f ", float_val[a]);
6305 if ( strm (type, "FL"))fprintf ( fp[0], "\t%d", int_val[0]);
6306 fprintf ( fp[0], "\n");
6309 free_char ( pv_l, -1);
6310 free_char ( pv_r, -1);
6316 char ** get_parameter ( char *para_name, int *np, char *fname)
6320 para_name: the name of the parameter to look for
6321 fname: the name of the file containing the parameters
6325 char ** containing the np[0] values taken by para_name in fname.
6328 if fname=NULL, para_name is searched using the last value taken by fp.
6330 Note: by default, the function keeps a file handle open until the first unsuccessful call.
6335 char ** return_value;
6337 if ( strm (para_name, "CLOSE THE FILE"))
6343 if ( line==NULL)line=vcalloc ( VERY_LONG_STRING+1, sizeof (char));
6344 if ( fname!=NULL && fp!=NULL)vfclose (fp);
6348 if ((fp=find_token_in_file ( fname,(fname==NULL)?fp:NULL, para_name))==NULL)
6354 fgets ( line, VERY_LONG_STRING,fp);
6355 return_value=get_list_of_tokens ( line, NULL, np);
6356 return return_value;
6360 FILE * set_fp_id ( FILE *fp, char *id)
6362 /*Sets fp just after id, id needs to be at the begining of the line*/
6374 fscanf ( fp, "%s", string);
6376 if ( strcmp ( string, id)==0)
6378 else while ( c!='\n' && c!=EOF)
6389 FILE * set_fp_after_char ( FILE *fp, char x)
6391 /*sets fp just after the first occurence of x*/
6416 FILE * find_token_in_file_nlines ( char *fname, FILE * fp, char *token, int n_line)
6419 /*This function finds the string TOKEN (as a single word) in the n_line first lines of fname.
6420 It returns NULL if not found or the position of the fp
6423 char *tmp_name=NULL;
6428 if ( !fp && !file_exists(NULL,fname))return NULL;
6431 tmp_name=vtmpnam ( NULL);
6433 fp1=vfopen (fname, "r");
6434 fp2=vfopen (tmp_name, "w");
6436 for ( a=0; a< n_line && fgets(buffer, 10000, fp1)!=NULL; a++)
6438 fprintf ( fp2, "%s", buffer);
6443 return find_token_in_file ( tmp_name,fp,token);
6446 int token_is_in_file ( char *fname, char *token)
6448 /*TH:an argument against torture: innocents get tortured longer, likewise for token testing*/
6467 if ( !fname || !file_exists(NULL,fname))return 0;
6470 fp=vfopen (fname, "r");
6471 while ((b=vfgets (buf,fp))!=NULL)
6474 p=strstr (buf, token);
6476 else if ( begining==1 && p==buf){vfclose (fp); return 1;}
6477 else if ( begining==0 && p){vfclose (fp); return 1;}
6485 char *vfgets ( char *buf, FILE *fp)
6494 debug=(getenv ("DEBUG_TCOFFEE"))?1:-1;
6501 buf=vcalloc ( 1000, sizeof (char));
6506 buf_len=read_array_size_new (buf);
6511 if ( (c=fgetc (fp))==EOF)return NULL;
6512 else ungetc (c, fp);
6515 while ( (c=fgetc (fp))!='\n' && c!=EOF)
6518 {buf_len+=100;buf=vrealloc (buf, buf_len*sizeof (char));}
6521 /*Add the cariage return*/
6524 if (l>=buf_len){buf_len+=100,buf=vrealloc (buf, buf_len*sizeof (char));}
6527 /*add the terminator*/
6528 if (l>=buf_len){buf_len+=100,buf=vrealloc (buf, buf_len*sizeof (char));}
6531 if ( bufin!=buf && bufin!=NULL && debug==1)
6532 fprintf ( stderr, "\nPointer change in vfgets...");
6538 FILE * find_token_in_file ( char *fname, FILE * fp, char *token)
6546 /*Note: Token: any string
6547 If Token[0]=='\n' Then Token only from the beginning of the line
6550 if (!fp && !file_exists("CACHE",fname))return NULL;
6552 if ( token[0]=='\n'){token++;only_start=1;}
6555 token_len=strlen (token);
6563 if (name)vfree (name);
6564 name = vcalloc (((fname)?measure_longest_line_in_file (fname):10000)+1, sizeof (char));
6565 fp=vfopen ( fname, "r");
6568 while ( (fscanf ( fp, "%s", name))!=EOF)
6571 if ( name[0]=='*')while ( ((c=fgetc (fp))!='\n')&& (c!=EOF));
6572 else if (strncmp ( name, token,token_len)==0){return fp;}
6573 else if (only_start) while ( ((c=fgetc (fp))!='\n')&& (c!=EOF));
6579 int **get_file_block_pattern (char *fname, int *n_blocks, int max_n_line)
6593 lline=measure_longest_line_in_file (fname)+1;
6594 line=vcalloc ( sizeof (char),lline+1);
6596 fp=vfopen (fname, "r");
6597 max_block_size=block_size=0;
6601 while ((c=fgetc(fp))!=EOF && (n_line<max_n_line || !max_n_line))
6604 fgets ( line, lline,fp);
6607 if ( is_alnum_line (line) && !in_block){n_blocks[0]++;in_block=1;}
6608 if ( is_alnum_line (line))
6615 max_block_size=MAX( max_block_size, block_size);
6621 max_block_size=MAX( max_block_size, block_size);
6624 l=declare_int (n_blocks[0]+1,max_block_size+1);
6627 fp=vfopen (fname, "r");
6631 while ((c=fgetc(fp))!=EOF && (n_line<max_n_line || !(max_n_line)))
6634 fgets ( line, lline,fp);
6637 if ( is_alnum_line (line) && !in_block){n_blocks[0]++;in_block=1;}
6638 if ( is_alnum_line (line))
6640 l[n_blocks[0]][0]++;
6641 free_char (get_list_of_tokens (line, " \t\n*:,", &x), -1);
6643 if ( l[n_blocks[0]][0]> max_block_size)myexit(fprintf_error ( stderr, "\nERROR %d", l[n_blocks[0]][0]));
6645 l[n_blocks[0]] [l[n_blocks[0]][0]]=x;
6658 char * strip_file_from_comments (char *com, char *in_file)
6660 /*Removes in file in_file every portion of line to the right of one of the symbols included in com
6661 Writes the striped file into a vtmpnam file
6668 out_file=vtmpnam(NULL);
6671 fp1=vfopen (in_file , "r");
6672 fp2=vfopen (out_file, "w");
6673 while ( (c=fgetc(fp1))!=EOF)
6677 while ( (c=fgetc(fp1))!='\n' && c!=EOF);
6681 fprintf (fp2, "%c", c);
6682 while ( (c=fgetc(fp1))!='\n' && c!=EOF)fprintf (fp2, "%c", c);
6683 if ( c!=EOF)fprintf (fp2, "%c", c);
6691 FILE * skip_commentary_line_in_file ( char com, FILE *fp)
6695 if ( fp==NULL)return NULL;
6696 while ((c=fgetc(fp))==com)
6698 while ((c=fgetc(fp))!='\n' && c!=EOF);
6700 if ( c!=EOF && c!='\n')ungetc(c, fp);
6704 int check_for_update ( char *web_address)
6708 float new_version, old_version;
6711 check_internet_connection (IS_NOT_FATAL);
6714 sprintf ( command, "%s/%s.version",DISTRIBUTION_ADDRESS, PROGRAM);
6715 url2file ( command, file);
6717 fp=vfopen ( file, "r");
6718 fscanf ( fp, "Version_%f", &new_version);
6720 sscanf ( VERSION, "Version_%f", &old_version);
6722 if ( old_version<new_version)
6724 fprintf ( stdout, "\nUpdate Status: outdated");
6725 fprintf ( stdout, "\nYour version of %s is not up to date", PROGRAM);
6726 fprintf ( stdout, "\nDownload the latest version %.2f with the following command:", new_version);
6727 fprintf ( stdout, "\n\twget %s/%s_distribution_Version_%.2f.tar.gz\n", DISTRIBUTION_ADDRESS, PROGRAM, new_version);
6728 return EXIT_FAILURE;
6730 else if ( old_version> new_version)
6732 fprintf ( stdout, "\nUpdate Status: beta-release");
6733 fprintf ( stdout, "\nYour are using a beta-release of %s(%s)\n", PROGRAM, VERSION);
6737 fprintf (stdout, "\nUpdate Status: uptodate");
6738 fprintf (stdout, "\nProgram %s(%s) is up to date\n", PROGRAM, VERSION);
6740 return EXIT_SUCCESS;
6747 int check_environement_variable_is_set ( char *variable, char *description, int fatal)
6749 if ( getenv (variable)==NULL)
6751 myexit(fprintf_error ( stderr, "\nERROR: You must set %s\n%s %s", variable, description, description));
6752 if ( fatal==IS_FATAL)
6754 myexit(fprintf_error ( stderr, "\n[%s:FATAL]\n", PROGRAM));
6758 add_warning ( stderr, "\n[%s:WARNING]\n", PROGRAM);
6763 int url2file (char *address, char *out)
6766 if (check_program_is_installed ("wget",NULL, NULL,WGET_ADDRESS, IS_NOT_FATAL))return printf_system( "wget %s -O%s >/dev/null 2>/dev/null", address, out);
6767 else if (check_program_is_installed ("curl",NULL, NULL,CURL_ADDRESS, IS_NOT_FATAL))return printf_system("curl %s -o%s >/dev/null 2>/dev/null", address, out);
6770 printf_exit (EXIT_FAILURE, stderr, "ERROR: Impossible to fectch external file: Neither wget nor curl is installed on your system [FATAL:%s]\n", PROGRAM);
6771 return EXIT_FAILURE;
6775 int wget (char *address, char *out)
6777 return printf_system ( "curl %s -O%s >/dev/null 2>/dev/null", address, out);
6780 int curl (char *address, char *out)
6782 return printf_system ( "curl %s -o%s >/dev/null 2>/dev/null", address, out);
6786 int simple_check_internet_connection (char *ref_site)
6791 test=vtmpnam (NULL);
6792 if (url2file((ref_site)?ref_site:TEST_WWWSITE_4_TCOFFEE,test)!=EXIT_SUCCESS)internet=0;
6793 else if ((n=count_n_char_in_file(test))<10)internet=0;
6798 int check_internet_connection (int mode)
6801 internet=simple_check_internet_connection (NULL);
6802 if (internet)return 1;
6803 else if ( mode==IS_NOT_FATAL)return internet;
6804 else proxy_msg(stderr);
6805 myexit (EXIT_FAILURE);
6807 char *pg2path (char *pg)
6814 if ( !pg) return NULL;
6817 printf_system_direct("which %s>%s 2>/dev/null", pg, tmp);
6818 path=file2string (tmp);
6820 if (!file_exists (NULL,path) && !strstr (pg, ".exe"))
6823 sprintf ( pg2, "%s.exe", pg);
6831 int check_program_is_installed ( char *program_name, char *path_variable, char *path_variable_name, char *where2getit, int fatal)
6835 int install_4_tcoffee=0;
6837 if (atoigetenv("INSTALL_4_TCOFFEE"))install_4_tcoffee=1;
6839 if ( strm (where2getit, "built_in"))return 1;
6841 if (path)vfree (path);
6843 if ( check_file_exists (path_variable))
6850 path=pg2path (program_name);
6851 if (path && path[0])return 1;
6854 int install=EXIT_FAILURE;
6856 if ((fatal==INSTALL || fatal==INSTALL_OR_DIE) && install_4_tcoffee)
6858 HERE ("************** %s is missing from your system. T-Coffee will make an attempt to install it.\n", program_name);
6859 install=printf_system ("install.pl %s -plugins=%s -clean", program_name, get_plugins_4_tcoffee());
6861 if ( install==EXIT_SUCCESS)return 1;
6862 else if ( fatal==INSTALL)return 0;
6863 else if ( fatal==NO_REPORT)return 0;
6865 if (fatal==IS_FATAL || fatal==INSTALL_OR_DIE)check_configuration4program();
6867 fprintf ( stderr, "\n#*****************************************************************");
6868 if (fatal) fprintf_error ( stderr, "\n#ERROR [FATAL:%s]", PROGRAM);
6869 else fprintf ( stderr, "\n#WARNING [%s]", PROGRAM);
6870 fprintf ( stderr, "\n# The Program %s Needed by %s Could not be found", program_name, PROGRAM);
6871 fprintf ( stderr, "\n# If %s is installed on your system:", program_name);
6872 fprintf ( stderr, "\n#\t -Make sure %s is in your $path:",program_name);
6874 fprintf ( stderr, "\n# If %s is NOT installed obtain a copy from:", program_name);
6875 fprintf ( stderr, "\n#\t%s\n#\n#",where2getit);
6876 fprintf ( stderr, "\n# and install it manualy");
6877 fprintf ( stderr, "\n******************************************************************\n");
6880 if ( fatal==IS_FATAL || fatal==INSTALL_OR_DIE) myexit (EXIT_FAILURE);
6884 FILE * display_output_filename ( FILE *io, char *type, char *format, char *name, int check_output)
6890 if ( strm ( name, "stdout") || strm (name, "stderr"))return io;
6892 if ( check_output==STORE)
6895 if ( buf==NULL)buf=vcalloc ( 1000, sizeof (char**));
6897 for (a=0; a<nbuf; a++)
6898 if ( strm (name, buf[a][2]))return io;
6900 buf[nbuf]=declare_char (3, 1000);
6901 sprintf ( buf[nbuf][0], "%s", type);
6902 sprintf ( buf[nbuf][1], "%s", format);
6903 sprintf ( buf[nbuf][2], "%s", name);
6907 else if ( check_output==FLUSH)
6911 for ( a=0; a< nbuf; a++)
6913 io=display_output_filename ( io, buf[a][0], buf[a][1], buf[a][2], CHECK);
6915 free_char (buf[a], -1);
6919 else if ( check_output==CHECK)
6921 if (check_file_exists(name)==NULL)
6923 if ( !strm (name, "no") && !strm (name, "NO") && !strm (name, "STDOUT") && !strm(name, "stdout"))
6924 add_warning( io, "\n\t#### File Type= %-15s Format= %-15s Name= %s | NOT PRODUCED [WARNING:%s:%s]\n",type, format, name, PROGRAM, VERSION );
6927 fprintf ( io, "\n\t#### File Type= %-15s Format= %-15s Name= %s",type, format, name );
6928 io=display_output_filename ( io,type,format,name, DUMP);
6930 else if (check_output==DUMP)
6935 if ((f=get_string_variable ("dump_output_file")))
6940 if ((list=get_string_variable ("dump_output_file_list")))
6942 fp=fopen (list, "a");
6943 fprintf (fp, "##%s##",name);
6947 if ((out=fopen (f, "a"))!=NULL)
6949 fprintf (out, "<file>\n");
6950 fprintf (out,"<stream>output</stream>\n");
6951 fprintf (out,"<type>%s</type>\n", type);
6952 fprintf (out, "<format>%s</format>\n", format);
6953 fprintf (out, "<name>%s</name>\n", name);
6954 fprintf (out, "<content>\n");
6955 if ((fp=fopen (name, "r"))!=NULL)
6957 while ((c=fgetc (fp))!=EOF){fprintf(out, "%c",c);}
6960 fprintf (out,"</content>\n</file>\n");
6969 FILE * display_input_filename ( FILE *io, char *type, char *format, char *name, int check_output)
6971 if ( check_output==CHECK && check_file_exists(name)==NULL)
6973 fprintf ( io, "\n\tIIII INPUT File Type= %10s Format= %10s Name= %s | NOT PRODUCED [WARNING:%s:%s]\n",type, format, name, PROGRAM, VERSION );
6976 fprintf ( io, "\n\t#### File Type= %10s Format= %10s Name= %s",type, format, name );
6980 int file_is_empty (char *fname)
6983 if (!fname) return 1;
6986 if (s.st_size)return 0;
6989 int file_exists (char *path, char *fname)
6994 if (!fname)return 0;
6995 else if (path && strm (path, "CACHE"))
6997 if (file_exists (NULL, fname))return 1;
6998 else return file_exists (get_cache_dir(), fname);
7000 else if (path) sprintf ( file, "%s/%s", path, fname);
7001 else if (!path)sprintf (file, "%s", fname);
7003 if (stat(file,& s)!=-1)
7004 return S_ISREG(s.st_mode);
7008 int isdir (char *file)
7011 if (stat (file,&s)!=-1)
7012 return S_ISDIR(s.st_mode);
7015 int rrmdir (char *s)
7017 if (isdir(s))return printf_system_direct ("rm -r %s", s);
7018 return EXIT_FAILURE;
7021 int isexec (char *file)
7026 state=ls_l(NULL,file);
7028 if (state[0]==0) return 0;
7030 if ( state[0]=='d') return 0;
7031 if ( state[3]=='x') return 1;
7032 if ( state[6]=='x') return 1;
7033 if ( state[9]=='x') return 1;
7037 char *ls_l ( char *path,char *file)
7044 tmpfile=vtmpnam (NULL);
7048 state=vcalloc (100, sizeof (char));
7050 for (a=0;a<100; a++)state[a]=0;
7051 if (!file || !file_exists (path, file))return state;
7052 printf_system_direct ("ls -l %s%s%s >%s 2>/dev/null",(path!=NULL)?path:"", (path!=NULL)?"/":"",file, tmpfile);
7054 fp=vfopen (tmpfile, "r");
7055 if (!fscanf ( fp, "%s", state))
7057 vfclose(fp); return 0;
7063 int my_rmdir ( char *dir_in)
7070 dir=vcalloc ( strlen (dir_in)+strlen (get_home_4_tcoffee())+100, sizeof (char));
7071 sprintf ( dir, "%s", dir_in);
7072 tild_substitute ( dir, "~",get_home_4_tcoffee());
7074 if (access(dir, F_OK)==-1);
7077 if ( strstr (dir, "coffee"))printf_system ( "rm -rf %s", dir);
7078 else myexit(fprintf_error ( stderr, "\nERROR: directory %s does not contain 'coffee' [FATAL:%s]", dir, PROGRAM)); }
7082 int my_mkdir ( char *dir_in)
7090 dir=vcalloc ( strlen (dir_in)+strlen (get_home_4_tcoffee())+100, sizeof (char));
7091 sprintf ( dir, "%s", dir_in);
7092 tild_substitute ( dir, "~",get_home_4_tcoffee());
7098 while (dir[a]!='\0')
7101 if ( dir[a]==dir_sep || dir[a+1]=='\0')
7106 if (access(dir, F_OK)==-1)
7110 printf_system_direct("mkdir %s", dir);
7111 if ( access (dir, F_OK)==-1)
7113 myexit(fprintf_error ( stderr, "\nERROR: Could Not Create Directory %s [FATAL:%s]", dir, PROGRAM)); }
7124 int filename_is_special (char *fname)
7126 if ( strm5 (fname, "default", "stdin", "stdout","stderr", "/dev/null"))return 1;
7127 if ( strm3 (fname, "STDIN", "STDOUT", "STDERR"))return 1;
7131 char* check_file_exists ( char *fname_in)
7134 static char *fname1;
7135 static char *fname2;
7138 if (!fname_in)return NULL;
7139 if (!fname_in[0])return NULL;
7140 if (fname_in[0]=='-')return NULL;
7142 if (!fname1){fname1=vcalloc (1000, sizeof (char));}
7143 if (!fname2){fname2=vcalloc (1000, sizeof (char));}
7145 sprintf ( fname1, "%s", fname_in);tild_substitute (fname1, "~", get_home_4_tcoffee());
7146 sprintf ( fname2, "%s%s", get_cache_dir(),fname1);
7148 if ( filename_is_special (fname1))return fname1;
7149 if ( strm5 (fname1, "no", "NO", "No", "NO_FILE","no_file"))return NULL/*fname1*/;
7150 if (!file_exists( NULL,fname1))
7152 if (!file_exists (NULL,fname2))return NULL;
7160 void create_file ( char *name)
7164 fp=fopen (name, "w");
7167 void delete_file ( char *fname)
7172 fp=fopen ( fname, "w");
7176 printf_system_direct ("rm %s", fname);
7179 int util_rename ( char *from, char *to)
7186 if ( !check_file_exists (from))return 0;
7187 else if ( check_file_exists (to) && !vremove (to) && !rename ( from, to)==0 );
7191 fp_from=vfopen ( from, "r");
7192 fp_to=vfopen ( to, "w");
7194 while ( (c=fgetc (fp_from))!=EOF)fprintf ( fp_to, "%c", c);
7206 int util_copy ( char *from, char *to)
7213 if (!check_file_exists (from))return 0;
7217 fp_from=vfopen ( from, "r");
7218 fp_to=vfopen ( to, "w");
7220 while ( (c=fgetc (fp_from))!=EOF)fprintf ( fp_to, "%c", c);
7228 FILE * output_completion4halfmat ( FILE *fp,int n, int tot, int n_reports, char *s)
7231 int max, left, achieved;
7238 max=((tot*tot)-tot)/2;
7239 left=((tot-n)*(tot-n)-(tot-n))/2;
7250 return output_completion (fp,achieved, max, n_reports, s);
7254 FILE * output_completion ( FILE *fp,int n, int tot, int n_reports, char *string)
7259 static int ref_time;
7266 ref_time=get_time()/1000;
7271 if ( !ref_val && !flag)
7273 fprintf (fp, "\n\t\t[%s][TOT=%5d][%3d %%][ELAPSED TIME: %4d sec.]",(string)?string:"",tot,(tot==1)?100:0, elapsed);
7276 else if ( n==tot)fprintf (fp, "\r\t\t[%s][TOT=%5d][%3d %%][ELAPSED TIME: %4d sec.]",(string)?string:"", tot,100, elapsed);
7277 else if ( ((n*100)/tot)>ref_val)
7279 ref_val=((n*100)/tot);
7280 t=(ref_val==0)?0:elapsed/ref_val;
7283 fprintf (fp, "\r\t\t[%s][TOT=%5d][%3d %%][ELAPSED TIME: %4d sec.]", (string)?string:"",tot,ref_val, elapsed);
7288 void * null_function (int a,...)
7290 myexit(fprintf_error ( stderr, "\n[ERROR] Attempt to use the Null Function [FATAL:%s]", PROGRAM));
7295 int btoi ( int nc,...)
7300 for ( a=0, b=0; a< nc; a++)
7302 b+=pow(2,a)*va_arg (ap,int);
7308 /*********************************************************************/
7310 /* Geometric FUNCTIONS */
7313 /*********************************************************************/
7315 float get_geometric_distance ( float ** matrix, int ncoor, int d1, int d2, char *mode)
7321 if ( strm (mode, "euclidian"))
7323 for ( a=0; a< ncoor; a++)
7325 d=(matrix[d1][a]-matrix[d2][a]);
7328 return (float)sqrt((double)t);
7335 /*********************************************************************/
7337 /* MATHEMATICAL FUNCTIONS */
7340 /*********************************************************************/
7341 static double EXP_UNDERFLOW_THRESHOLD = -4.60f;
7342 static double LOG_UNDERFLOW_THRESHOLD = 7.50f;
7343 static double LOG_ZERO = -FLT_MAX;
7344 static double LOG_ONE = 0.0f;
7345 double log_addN (int N, double*L)
7351 if ( N==1)return L[0];
7354 for ( a=1; a<N; a++)
7356 v=log_add2(v, L[a]);
7360 double log_add6 (double x, double y, double z, double w, double v, double e)
7362 return log_add2(log_add3(x, y, z),log_add3(z,w,e));
7364 double log_add5 (double x, double y, double z, double w, double v)
7366 return log_add3(log_add2(x, y),log_add2(z,w),v );
7368 double log_add4 (double x, double y, double z, double w)
7370 return log_add2(log_add2(x, y),log_add2(z,w));
7372 double log_add3 (double x, double y, double z)
7374 return log_add2(log_add2(x, y),z);
7376 double log_add2 ( double x, double y)
7379 x = (x == LOG_ZERO || ((y - x) >= LOG_UNDERFLOW_THRESHOLD)) ? y : log (exp (x-y) + 1) + x;
7381 x = (y == LOG_ZERO || ((x - y) >= LOG_UNDERFLOW_THRESHOLD)) ? x : log (exp (x-y) + 1) + y;
7388 float M_chooses_Nlog ( int m, int N)
7390 /*Choose M elemets in N*/
7392 if ( m==N) return 0;
7395 myexit(fprintf_error ( stderr, "\nERROR: M chosses N out of bounds ( M>N) [FATAL:%s]", PROGRAM));
7396 myexit (EXIT_FAILURE);
7400 z1=factorial_log (m+1, N);
7401 z2=factorial_log (1, N-m);
7409 float factorial_log ( int start, int end)
7411 if ( end==0)return 0;
7412 else if ( end==start) return (float)my_int_log((double)start);
7413 else if ( start>end)
7415 fprintf_error ( stderr, "\nERROR: factorial log out of bounds (%d %d) [FATAL:%s]",start, end, PROGRAM);
7416 myexit (EXIT_FAILURE);
7422 for ( x=0,a=start; a<=end; a++)
7424 x+=(float)my_int_log(a);
7431 float my_int_log(int a)
7434 if ( a>=100000)return log(a);
7438 if (!lu) lu=vcalloc ( 100000, sizeof (float));
7439 if ( !lu[a]){lu[a]=log(a);}
7445 double factorial (int start, int end);
7446 double M_chooses_N ( int m, int N)
7448 /*Choose M elemets in N*/
7449 if ( m==N) return 1;
7452 fprintf_error ( stderr, "\nERROR: M chosses N out of bounds ( M>N) [FATAL:%s]", PROGRAM);
7453 myexit (EXIT_FAILURE);
7457 return factorial (m+1, N)/factorial (1, N-m);
7461 fprintf_error ( stderr, "\nERROR: M chosses N out of bounds ( N>50). Use log space [FATAL:%s]", PROGRAM);
7462 myexit (EXIT_FAILURE);
7466 double factorial (int start, int end)
7469 if ( start>end || start<0 || end<0)
7471 fprintf_error ( stderr, "\nERROR: Negative Factorial [FATAL:%s]", PROGRAM);
7472 myexit ( EXIT_FAILURE);
7474 else if (end==0) return 1;
7475 else if (end==start) return end;
7479 if ( !lu)lu=declare_double (100, 100);
7481 if ( lu[start][end])return lu[start][end];
7485 lu[start][end]=(double)start;
7486 for ( a=start+1; a<=end; a++)
7488 lu[start][end]*=(double)a;
7490 return lu[start][end];
7495 /*********************************************************************/
7497 /* Fast Log Additions (adapted from Probcons)*/
7500 /*********************************************************************/
7501 double EXP (double x){
7507 return (((0.03254409303190190000*x + 0.16280432765779600000)*x + 0.49929760485974900000)*x + 0.99995149601363700000)*x + 0.99999925508501600000;
7510 return (((0.01973899026052090000*x + 0.13822379685007000000)*x + 0.48056651562365000000)*x + 0.99326940370383500000)*x + 0.99906756856399500000;
7511 return (((0.00940528203591384000*x + 0.09414963667859410000)*x + 0.40825793595877300000)*x + 0.93933625499130400000)*x + 0.98369508190545300000;
7515 return (((0.00217245711583303000*x + 0.03484829428350620000)*x + 0.22118199801337800000)*x + 0.67049462206469500000)*x + 0.83556950223398500000;
7516 return (((0.00012398771025456900*x + 0.00349155785951272000)*x + 0.03727721426017900000)*x + 0.17974997741536900000)*x + 0.33249299994217400000;
7519 return (((0.00000051741713416603*x + 0.00002721456879608080)*x + 0.00053418601865636800)*x + 0.00464101989351936000)*x + 0.01507447981459420000;
7523 float LOOKUP (float x){
7525 if (x <= 1.00f) return ((-0.009350833524763f * x + 0.130659527668286f) * x + 0.498799810682272f) * x + 0.693203116424741f;
7526 if (x <= 2.50f) return ((-0.014532321752540f * x + 0.139942324101744f) * x + 0.495635523139337f) * x + 0.692140569840976f;
7527 if (x <= 4.50f) return ((-0.004605031767994f * x + 0.063427417320019f) * x + 0.695956496475118f) * x + 0.514272634594009f;
7529 return ((-0.000458661602210f * x + 0.009695946122598f) * x + 0.930734667215156f) * x + 0.168037164329057f;
7531 void LOG_PLUS_EQUALS (float *x, float y){
7534 x[0] = (x[0] == LOG_ZERO || y - x[0] >= LOG_UNDERFLOW_THRESHOLD) ? y : LOOKUP(y-x[0]) + x[0];
7536 x[0] = (y == LOG_ZERO || x[0] - y >= LOG_UNDERFLOW_THRESHOLD) ? x[0] : LOOKUP(x[0]-y) + y;
7539 float LOG_ADD (float x, float y){
7540 if (x < y) return (x == LOG_ZERO || y - x >= LOG_UNDERFLOW_THRESHOLD) ? y : LOOKUP((y-x)) + x;
7541 return (y == LOG_ZERO || x - y >= LOG_UNDERFLOW_THRESHOLD) ? x : LOOKUP((x-y)) + y;
7544 float LOG_ADD3 (float x1, float x2, float x3){
7545 return LOG_ADD (x1, LOG_ADD (x2, x3));
7547 float LOG_ADD4 (float x1, float x2, float x3, float x4){
7548 return LOG_ADD (x1, LOG_ADD (x2, LOG_ADD (x3, x4)));
7550 float LOG_ADD5 (float x1, float x2, float x3, float x4, float x5){
7551 return LOG_ADD (x1, LOG_ADD (x2, LOG_ADD (x3, LOG_ADD (x4, x5))));
7553 float LOG_ADD6 (float x1, float x2, float x3, float x4, float x5, float x6){
7554 return LOG_ADD (x1, LOG_ADD (x2, LOG_ADD (x3, LOG_ADD (x4, LOG_ADD (x5, x6)))));
7556 float LOG_ADD7 (float x1, float x2, float x3, float x4, float x5, float x6, float x7){
7557 return LOG_ADD (x1, LOG_ADD (x2, LOG_ADD (x3, LOG_ADD (x4, LOG_ADD (x5, LOG_ADD (x6, x7))))));
7562 #define SHORT_SIZE 1
7565 char *strscn(char *s, char *pattern);
7566 long unsigned strtou(char *s, int base, char **scan_end);
7567 long int strtoi(char *s, int base, char **scan_end);
7568 int my_isnumber(char c, int base);
7569 int tonumber(char c);
7571 int my_vsscanf(char *buf, char *fmt, va_list parms)
7573 int scanned = 0, size = 0, suppress = 0;
7574 int w = 0, flag = 0, l = 0;
7579 long unsigned n2, *n2l, parsing = 0;
7581 short unsigned *n2s;
7586 if (*fmt != '%' && !parsing) {
7587 /* No token detected */
7590 /* We need to make a conversion */
7613 w = strtou(fmt, 10, &base);
7614 /* We use SPACE_PAD to parse %10s
7615 * commands where the number is the
7616 * maximum number of char to store!
7624 c_ptr = va_arg(parms, char *);
7630 c_ptr = va_arg(parms, char *);
7631 while (*buf != 0 && isspace(*buf))
7634 while (*buf != 0 && !isspace(*buf)) {
7635 if (!(flag & SPACE_PAD))
7649 buf = strscn(buf, "1234567890-+");
7650 n1 = strtoi(buf, 10, &base);
7655 n1b = va_arg(parms, int *);
7666 *n1s = (short) (n1);
7674 buf = strscn(buf, "1234567890");
7675 n2 = strtou(buf, 10, &base);
7682 *n2b = (unsigned) n2;
7690 n2s = va_arg(parms, short unsigned
7692 *n2s = (short) (n2);
7700 buf = strscn(buf, "1234567890xabcdefABCDEF");
7701 n2 = strtou(buf, 16, &base);
7708 *n2b = (unsigned) n2;
7716 n2s = va_arg(parms, short unsigned
7718 *n2s = (short) (n2);
7728 buf = strscn(buf, "1234567890.e+-");
7729 n3 = strtod(buf, &base);
7734 n3l = va_arg(parms, double *);
7738 n3l = va_arg(parms, double *);
7742 n3s = va_arg(parms, float *);
7743 *n3s = (float) (n3);
7769 char *strscn(char *s, char *pattern)
7774 while (*scan != 0) {
7785 long unsigned strtou(char *s, int base, char **scan_end)
7787 int value, overflow = 0;
7788 long unsigned result = 0, oldresult;
7789 /* Skip trailing zeros */
7792 if (*s == 'x' && base == 16) {
7797 /* Convert number */
7798 while (my_isnumber(*s, base)) {
7799 value = tonumber(*s++);
7800 if (value > base || value < 0)
7805 /* Detect overflow */
7806 if (oldresult > result)
7815 long int strtoi(char *s, int base, char **scan_end)
7817 int sign, value, overflow = 0;
7818 long int result = 0, oldresult;
7823 } else if (*s == '+') {
7828 /* Skip trailing zeros */
7831 /* Convert number */
7832 while (my_isnumber(*s, base)) {
7833 value = tonumber(*s++);
7834 if (value > base || value < 0)
7839 /* Detect overflow */
7840 if (oldresult > result)
7851 int my_isnumber(char c, int base)
7853 static char *digits = "0123456789ABCDEF";
7854 if ((c >= '0' && c <= digits[base - 1]))
7860 int tonumber(char c)
7862 if (c >= '0' && c <= '9')
7864 else if (c >= 'A' && c <= 'F')
7865 return (c - 'A' + 10);
7866 else if (c >= 'a' && c <= 'f')
7867 return (c - 'a' + 10);
7872 ///////////////////////////////////////////////////////////////////////////////////////////
7874 ////////////////////////////////////////////////////////////////////////////////////////////
7875 unsigned long hash_file(char* file) //returns the hash value for key
7877 // Calculate a hash value by the division method:
7878 // Transform key into a natural number k = sum ( key[i]*128^(L-i) ) and calculate i= k % num_slots.
7879 // Since calculating k would lead to an overflow, i is calculated iteratively
7880 // and at each iteration the part divisible by num_slots is subtracted, i.e. (% num_slots is taken).
7882 unsigned long i=0; // Start of iteration: k is zero
7883 unsigned long num_slots=999999999;
7890 if (file==NULL || !check_file_exists (file) ) {printf("Warning from util.c:hasch_file: No File [FATAL:%s]\n", PROGRAM); myexit (EXIT_FAILURE);}
7892 fp=vfopen (file, "r");
7893 while ( (c=fgetc (fp))!=EOF)
7895 i = ((i<<7) + c) % num_slots;
7901 int ** r_generate_array_int_list ( int len, int min, int max,int step, int **array, int f, int *n,FILE *fp, int *c_array);
7902 int **generate_array_int_list (int len, int min, int max, int step, int *n, char *file)
7904 int **array, *c_array;
7910 fp=vfopen (file, "w");
7916 for (s=1, a=0; a<len; a++)s*=((max-min)+1)/step;
7917 array=declare_int (s, len+1);
7919 c_array=vcalloc (len, sizeof (int));
7920 array=r_generate_array_int_list ( len, max, min, step, array, 0, n,fp, c_array);
7922 if ( fp) vfclose (fp);
7925 int ** r_generate_array_int_list ( int len, int min, int max,int step, int **array, int f, int *n,FILE *fp, int *c_array)
7933 for (a=0; a<len; a++)
7935 array[n[0]][a]=c_array[a];
7943 for (a=0; a<len; a++)fprintf ( fp, "%3d ",c_array[a]);
7951 for (a=max; a<=min;a+=step)
7954 r_generate_array_int_list (len, min, max, step, array, f+1, n,fp, c_array);
7960 char *** r_generate_array_string_list ( int len, char ***alp,int *alp_size, char ***array, int f, int *n,FILE *fp, char **c_array, int mode, int pstart);
7961 char ***generate_array_string_list (int len, char ***alp, int *alp_size, int *n, char *file, int mode)
7963 char ***array, **c_array;
7970 fp=vfopen (file, "w");
7977 for (s=1, a=0; a<len; a++)
7981 array=declare_arrayN (3,sizeof (char), s, len,0);
7983 c_array=declare_char (len,0);
7984 array=r_generate_array_string_list ( len, alp, alp_size, array, 0, n,fp, c_array, mode, -1);
7986 if ( fp) vfclose (fp);
7989 char *** r_generate_array_string_list ( int len, char ***alp, int *alp_size, char ***array, int f, int *n,FILE *fp, char **c_array, int mode, int pstart)
7998 for (a=0; a<len; a++)
8000 array[n[0]][a]=c_array[a];
8008 for (a=0; a<len; a++)fprintf ( fp, "%s ",c_array[a]);
8020 else if ( mode==NO_OVERLAP)
8025 for (a=start; a<alp_size[f]; a++)
8027 c_array[f]=alp[f][a];
8028 r_generate_array_string_list (len,alp, alp_size, array, f+1, n,fp, c_array, mode, a);
8033 float *display_accuracy (float *count, FILE *fp)
8036 r=counts2accuracy (count);
8037 fprintf (fp, "Sp: %.3f Sn: %.3f Sen2: %.3f AC: %.3f\n",r[0], r[1], r[2], r[3]);
8041 float *counts2accuracy (float *count)
8048 float TP, TN, FP, FN;
8050 result=vcalloc (4, sizeof (float));
8057 result [0]=((TN+FP)==0)?-1:TN/(TN+FP); //Sp
8058 result [1]=((TP+FN)==0)?-1:TP/(TP+FN); //Sn
8059 result [2]=((TP+FP)==0)?-1:TP/(TP+FP); //Sen2
8060 result [3]=(((TP+FN)==0) || ((TP+FP)==0) || ((TN+FP)==0) || ((TN+FN)==0))?-1:0.5*((TP/(TP+FN)) + (TP/(TP+FP)) + (TN/(TN+FP)) + (TN/(TN+FN))) - 1 ;//AC
8065 float rates2sensitivity (int tp, int tn, int fp, int fn, float *sp, float *sn, float *sen2, float *b)
8069 sp=vcalloc (1, sizeof (float));
8070 sn=vcalloc (1, sizeof (float));
8071 sen2=vcalloc (1, sizeof (float));
8072 b=vcalloc (1, sizeof (float));
8074 sn[0] =((tp+fn)==0)?1:(float)tp/(float)(tp+fn);
8075 sen2[0]=((tp+fp)==0)?1:(float)tp/(float)(tp+fp);
8076 sp[0] =((tn+fp)==0)?1:(float)tn/(float)(tn+fp);
8077 b[0]=MIN((MIN((sp[0]),(sn[0]))),(sen2[0]));
8080 float profile2sensitivity (char *pred, char *ref, float *sp, float *sn, float *sen2, float *b)
8082 int tp=0, tn=0, fp=0, fn=0;
8089 if (pred[a]=='I' && ref[a]=='I')tp++;
8090 if (pred[a]=='O' && ref[a]=='I')fn++;
8091 if (pred[a]=='I' && ref[a]=='O')fp++;
8092 if (pred[a]=='O' && ref[a]=='O')tn++;
8094 return rates2sensitivity (tp, tn, fp, fn, sp, sn, sen2, b);
8097 float profile2evalue (char *pred, char *ref)
8109 if (pred[a]=='I')P++;
8110 if (ref[a]=='I') E++;
8112 if (pred[a]=='I' && ref[a]=='I')II++;
8117 p1= M_chooses_Nlog (P,l) + M_chooses_Nlog (II, P) + M_chooses_Nlog (E-II, l-P);
8118 p2=(M_chooses_Nlog (P,l)+M_chooses_Nlog (E,l));
8130 // NEW Intitialization
8132 int string_putenv (char *p);
8135 char ** standard_initialisation (char **in_argv, int *in_argc)
8150 debug_lock=atoigetenv("DEBUG_LOCK");
8154 if (!in_argv){done=0;return NULL;}
8155 else if ( done){return in_argv;}
8162 global_exit_signal=EXIT_SUCCESS;
8163 atexit (clean_exit);
8167 signal (SIGTERM,signal_exit);
8168 signal (SIGINT, signal_exit);
8169 signal (SIGKILL, signal_exit);
8171 signal (SIGABRT ,error_exit);
8172 signal (SIGFPE ,error_exit);
8173 signal (SIGILL ,error_exit);
8174 signal (SIGSEGV ,error_exit);
8176 program_name=vcalloc ( strlen (in_argv[0])+strlen (PROGRAM)+1, sizeof (char));
8179 sprintf ( program_name, "%s", in_argv[0]);
8180 out_argv=break_list ( in_argv, in_argc, "=;, \n");
8181 s=list2string2 (out_argv, in_argc[0], " ");
8182 set_command_line (s);
8184 else sprintf ( program_name, "%s",PROGRAM);
8186 if ( name_is_in_list ( "-no_error_report", out_argv, in_argc[0], 100)!=-1)no_error_report=1;
8195 if (!getenv ("UPDATED_ENV_4_TCOFFEE"))
8197 //1-set the environment variables;
8198 file_putenv ("/usr/local/t_coffee/.t_coffee_env");//make sure child processes do not update env
8199 sprintf (buf, "%s/.t_coffee/.t_coffee_env", getenv ("HOME"));
8201 file_putenv ("./.t_coffee_env");
8202 if (getenv ("ENV_4_TCOFFEE"))file_putenv (getenv ("ENV_4_TCOFFEE"));
8205 string_putenv (s); //let Command line update go through
8208 cputenv ("HOME_4_TCOFFEE=%s",get_home_4_tcoffee());
8209 cputenv ("DIR_4_TCOFFEE=%s",get_dir_4_tcoffee());
8210 cputenv ("TMP_4_TCOFFEE=%s",get_tmp_4_tcoffee());
8211 cputenv ("CACHE_4_TCOFFEE=%s",get_cache_4_tcoffee());
8212 cputenv ("MCOFFEE_4_TCOFFEE=%s",get_mcoffee_4_tcoffee());
8213 cputenv ("METHODS_4_TCOFFEE=%s",get_methods_4_tcoffee());
8214 cputenv ("PLUGINS_4_TCOFFEE=%s",get_plugins_4_tcoffee());
8215 cputenv ("LOCKDIR_4_TCOFFEE=%s",get_lockdir_4_tcoffee());
8216 cputenv ("ERRORFILE_4_TCOFFEE=t_coffee.ErrorReport");
8222 string_putenv (s); //let Command line update go through //Twice in case an executable dir not created
8224 if (!getenv ("ENV_4_TCOFFEE"))cputenv ("ENV_4_TCOFFEE=%s/.t_coffee_env", get_dir_4_tcoffee());
8227 if (!getenv ("UPDATED_ENV_4_TCOFFEE"))
8229 cputenv4path ("/usr/local/t_coffee/plugins");
8230 sprintf (buf, "%s/.t_coffee/plugins", getenv ("HOME"));
8232 cputenv4path ("./.plugins");
8233 if ( getenv ("PLUGINS_4_TCOFFEE"))cputenv4path (getenv ("PLUGINS_4_TCOFFEE"));
8235 cputenv ("UPDATED_ENV_4_TCOFFEE=1");
8237 if ( debug_lock){fprintf ( stderr, "\n*************** LOCKDIR: %s *************\n", get_lockdir_4_tcoffee());}
8239 lock(getpid(),LLOCK, LRESET, "%d\n",getppid());//set the main lock
8240 if (is_shellpid(getppid()))lock(getppid(),LLOCK, LSET, "%d\n",getpid());//update parent lock when parent is shell
8243 //set special Variables
8244 if (getenv ("MAFFT_BINARIES") || isdir4path ("/usr/local/lib/mafft"));
8245 else cputenv ( "MAFFT_BINARIES=%s",get_plugins_4_tcoffee());
8248 set_proxy(get_proxy_from_env());
8249 set_email(get_email_from_env ());
8252 for (a=1, stdi=0; a<in_argc[0]; a++)
8254 if ( (strm ( out_argv[a], "stdin") || strm (out_argv[a], "STDIN")) && stdi==0)
8261 file=vtmpnam (NULL);
8262 vfree (out_argv[a]);
8263 out_argv[a]=vcalloc ( strlen (file)+1, sizeof (char));
8264 sprintf (out_argv[a], "%s", file);
8265 fp_stdi=vfopen ("stdin", "r");
8266 fp=vfopen (file, "w");
8267 while ( (c=fgetc (fp_stdi))!=EOF)fprintf (fp, "%c", c);
8274 printf_exit ( EXIT_FAILURE,stderr, "ERROR: Attempt to get more than one file via pipe [FATAL:%s]\n", PROGRAM);
8284 if (is_rootpid())fprintf ( stderr, "****** Forced interuption of main parent process %d\n", getpid());
8286 global_exit_signal=EXIT_SUCCESS;
8287 myexit (EXIT_SUCCESS);
8291 lock (getpid(), LERROR, LSET, "%d -- ERROR: COREDUMP: %s %s\n",getpid(), PROGRAM, VERSION, getpid());
8292 global_exit_signal=EXIT_FAILURE;
8293 myexit (EXIT_FAILURE);
8303 clean_exit_started=1;//prevent new locks
8306 //1-start killing all the children
8309 //3-update error lock if needed
8310 if (has_error_lock())//Update error lock
8312 lock (getpid(), LERROR, LSET, "%d -- STACK: %d -> %d -- %s %s\n", getpid(), getppid(), getpid(), PROGRAM, VERSION);
8313 lock (getpid(), LERROR, LSET, "%d -- COM: %s\n",getpid(),in_cl );
8322 kill_child_pid(getpid());
8323 if (has_error_lock())
8327 warning_msg (stderr);
8328 e=lock (getpid(), LERROR, LREAD, NULL);
8332 //explicit the most common error messages
8333 if ( strstr (e, "EMAIL"))email_msg (stderr);
8334 if ( strstr (e, "INTERNET"))proxy_msg (stderr);
8335 if ( strstr (e, "PG")) install_msg(stderr);
8336 if ( strstr (e, "COREDUMP"))
8341 print_exit_failure_message ();
8344 else if ( has_warning_lock())
8346 warning_msg (stderr);
8349 print_exit_success_message();
8351 if ( (f=get_string_variable ("dump")))
8353 dump_tcoffee (f, "standard dump");
8354 //unset_string_variable ("dump");
8355 //unset_string_variable ("dump_output_file");
8356 //display_output_filename (stdout, "DUMP", "DUMP_4_TCOFFEE",f, CHECK);
8360 lock (getpid(), LLOCK, LRELEASE, "");
8361 lock (getpid(), LWARNING, LRELEASE, "");
8362 lock (getpid(), LERROR, LRELEASE, "");
8368 add_method_output2method_log (NULL, NULL, NULL, NULL, decode_name (NULL, CODELIST));
8370 //Remove all temporary files
8371 debug=(atoigetenv ("DEBUG_TMP_FILE"));
8380 if (isdir(start->name))rrmdir (start->name);
8384 vremove (start->name);
8385 if (start->name)sprintf (test, "%s.dnd", start->name);vremove (test);
8386 if (start->name)sprintf (test, "%s.html",start->name);vremove (test);
8391 if (isdir(start->name))
8392 {fprintf ( stderr, "DEBUG_TMP_FILE SET : Dir %s not removed (%d)\n", start->name, getpid());}
8394 {fprintf ( stderr, "DEBUG_TMP_FILE SET : File %s not removed (%d)\n", start->name, getpid());}
8398 //vfree(b->name);vfree(b);
8400 if (!debug && is_rootpid())my_rmdir (get_tmp_4_tcoffee());
8404 //lock (getpid(), LLOCK, LRELEASE,NULL); Now keep the lock unless it is a parent process
8406 //UNIQUE TERMINATION FOR EVERYBODY!!!!!!
8411 int cputenv4path (char *p)
8414 else if (isdir4path (p))
8416 cputenv ("PATH=%s:%s", p, getenv("PATH"));
8426 int string_putenv ( char *s)
8428 //extract from command line all the occurences -setenv val1 val2 and sets environement
8436 v1=vcalloc ( strlen (s)+1, sizeof (char));
8437 v2=vcalloc ( strlen (s)+1, sizeof (char));
8441 while ( (p=strstr (p, "-setenv")))
8443 if (sscanf (p, "-setenv %s %s", v1,v2)==2)
8446 if (strm (v1, "PATH"))cputenv4path (v2);
8447 else cputenv ( "%s=%s", v1, v2);
8449 p+=strlen ("-setenv");
8453 if ( (p=strstr (p, "-plugins")))
8455 sscanf (p, "-plugins %s", v1);
8456 cputenv ("PROXY_4_TCOFFEE=%s",v1);
8461 if ( (p=strstr (p, "-email")))
8463 sscanf (p, "-email %s", v1);
8464 cputenv ("EMAIL_4_TCOFFEE=%s", v1);
8467 if ( (p=strstr (p, "-proxy")))
8469 sscanf (p, "-proxy %s", v1);
8470 cputenv ("PROXY_4_TCOFFEE=%s", v1);
8475 vfree (v1); vfree (v2);
8479 char* file_putenv (char *file)
8481 //puts in environement all the variables conatinned in file
8482 //format VAR=value on each line
8488 if (!file || !file_exists(NULL,file)) return NULL;
8490 list=file2list (file, "\n=");
8491 fprintf ( stderr, "Import Environement Variables from %s\n", file);
8495 if ( list[n][1][0]!='#')
8497 if ( strm (list[n][1], "PATH"))
8500 cputenv ( "PATH=%s:%s",list[n][2], getenv ("PATH"));
8501 fprintf ( stderr, "\tPATH=%s:$PATH", list[n][2]);
8506 cputenv("%s=%s", list[n][1],list[n][2]);
8507 fprintf ( stderr, "\t%s=%s", list[n][1],list[n][2]);
8512 free_arrayN ((void ***)list, 3);
8516 char * bachup_env (char *mode,char *f)
8522 file=vtmpnam (NULL);
8523 buf=vcalloc ( 10000, sizeof (char));
8527 if (strm (mode, "DUMP"))
8529 printf_system_direct ("/usr/bin/env > %s", f);
8530 return EXIT_SUCCESS;
8532 else if ( strm (mode,"RESTAURE") && file_exists (NULL,f))
8535 return EXIT_SUCCESS;
8541 /******************************COPYRIGHT NOTICE*******************************/
8542 /*© Centro de Regulacio Genomica */
8544 /*Cedric Notredame */
8545 /*Fri Feb 18 08:27:45 CET 2011 - Revision 596. */
8546 /*All rights reserved.*/
8547 /*This file is part of T-COFFEE.*/
8549 /* T-COFFEE is free software; you can redistribute it and/or modify*/
8550 /* it under the terms of the GNU General Public License as published by*/
8551 /* the Free Software Foundation; either version 2 of the License, or*/
8552 /* (at your option) any later version.*/
8554 /* T-COFFEE is distributed in the hope that it will be useful,*/
8555 /* but WITHOUT ANY WARRANTY; without even the implied warranty of*/
8556 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the*/
8557 /* GNU General Public License for more details.*/
8559 /* You should have received a copy of the GNU General Public License*/
8560 /* along with Foobar; if not, write to the Free Software*/
8561 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/
8562 /*............................................... |*/
8563 /* If you need some more information*/
8564 /* cedric.notredame@europe.com*/
8565 /*............................................... |*/
8569 /******************************COPYRIGHT NOTICE*******************************/