15 #include "io_lib_header.h"
16 #include "util_lib_header.h"
17 #include "define_header.h"
18 #include "perl_header_lib.h"
20 int my_vsscanf(char *buf, char *fmt, va_list parms);
21 static int get_vtmpnam2_root();
23 static char CACHE_4_TCOFFEE[1000];
24 static char TMP_4_TCOFFEE[1000];
25 static char DIR_4_TCOFFEE[1000];
26 static int global_exit_signal;
27 static int no_error_report;
28 /*********************************************************************/
33 /*********************************************************************/
34 double dichotomy (double value, double target_value, double middle, double *bottom,double *top)
36 if ( value> target_value)top[0]=middle;
37 else if ( value<target_value)bottom[0]=middle;
38 return (top[0]-bottom[0])/2+bottom[0];
42 /*********************************************************************/
47 /*********************************************************************/
49 #define MAXSTACK (sizeof(size_t) * CHAR_BIT)
50 static void exchange(void *a, void *b, size_t size) {
62 for (i = sizeof(int); i <= size; i += sizeof(int)) {
71 for (i = i - sizeof(int) + 1; i <= size; i++) {
81 void qsort(void *base, size_t nmemb, size_t size,
82 int (*compar)(const void *, const void *)) {
83 void *lbStack[MAXSTACK], *ubStack[MAXSTACK];
91 lbStack[0] = (char *)base;
92 ubStack[0] = (char *)base + (nmemb-1)*size;
93 for (sp = 0; sp >= 0; sp--) {
102 /* select pivot and exchange with 1st element */
103 offset = (ub - lb) >> 1;
104 P = lb + offset - offset % size;
105 exchange (lb, P, size);
107 /* partition into two segments */
111 while (i < j && compar(lb, i) > 0) i += size;
112 while (j >= i && compar(j, lb) > 0) j -= size;
114 exchange (i, j, size);
119 /* pivot belongs in A[j] */
120 exchange (lb, j, size);
123 /* keep processing smallest segment, and stack largest */
124 if (m - lb <= ub - m) {
126 lbStack[sp] = m + size;
133 ubStack[sp++] = m - size;
141 int pstrcmp(char *p1, char *p2);
145 /*********************************************************************/
150 /*********************************************************************/
151 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))
153 unsigned long i, ir, j, l;
154 void *rra, *rrb, *ra_j, *ra_j_1;
162 rra =vcalloc ( len, size);
163 rrb =vcalloc ( len, size);
164 ra_j =vcalloc ( len, size);
165 ra_j_1=vcalloc ( len, size);
176 fseek( fp, start+(((l-1)*len)*size), SEEK_SET);
177 fread( rra, size, len,fp); /*rra=ra[--l]*/
181 fseek( fp, start+((ir-1)*len*size), SEEK_SET);
182 fread( rra, size, len,fp); /*rra=ra[ir]*/
184 fseek( fp, start, SEEK_SET);
185 fread( rrb, size, len,fp); /*rrb=ra[0]*/
187 fseek( fp, start+((ir-1)*len*size), SEEK_SET);
188 fwrite(rrb,size, len, fp); /*ra[ir]=rrb=ra[0]*/
192 fseek ( fp,start, SEEK_SET);
193 fwrite(rra,size, len, fp); /*ra[0]=rra*/
201 fseek ( fp, start+((j-1)*len*size), SEEK_SET);
202 fread (ra_j, size, len, fp);
206 fseek ( fp, start+(((j-1)+1)*len*size), SEEK_SET);
207 fread (ra_j_1, size, len, fp);
210 if ( j<ir && compare( ra_j, ra_j_1,first_comp_field,n_comp_fields, size )<0)
212 SWAPP(ra_j, ra_j_1, tp);
216 if (compare(rra, ra_j, first_comp_field,n_comp_fields, size)<0)
218 fseek ( fp, start+((i-1)*len*size), SEEK_SET);
219 fwrite(ra_j,size, len, fp);
227 fseek ( fp, start+((i-1)*len*size), SEEK_SET);
228 fwrite(rra,size, len, fp);
237 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))
239 unsigned long i, ir, j, l;
250 rra =vcalloc ( len, size);
261 copy ( rra, ra[--l],len);
265 copy ( rra, ra[ir],len);
266 copy ( ra[ir], ra[FE], len);
269 copy ( ra[FE],rra,len);
277 if ( j<ir && compare( ra[j], ra[j+1],first_comp_field,n_comp_fields, size )<0)j++;
278 if (compare(rra, ra[j], first_comp_field,n_comp_fields, size)<0)
279 {copy(ra[i], ra[j],len);i=j;j<<= 1;}
283 copy( ra[i], rra,len);
290 /*********************************************************************/
295 /*********************************************************************/
296 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))
298 int upper, lower, c, i;
301 static long key2_size;
308 if ( key2==NULL){key2=vcalloc (entry_len, el_size);key2_size=entry_len* el_size;}
309 else if (key2_size< (entry_len* el_size)){vfree(key2);key2=vcalloc (entry_len, el_size);key2_size=entry_len* el_size;}
311 while ((lower-upper)>1)
313 i=(lower+upper) >> 1;
315 fseek ( fp,start+(i*el_size*entry_len), SEEK_SET);
316 fread ( key2, el_size, entry_len,fp);
317 c=compare(key2,key, comp_first, comp_len,el_size);
319 if ( c==0){p[0]=i;return key2;}
320 else if ( c< 0)upper=i;
321 else if ( c> 0)lower=i;
326 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))
328 int upper, lower, c, i;
333 while ((lower-upper)>1)
337 c=compare(key2,key, comp_first,comp_len,el_size);
339 if ( c==0){p[0]=i;return key2;}
340 else if ( c< 0)upper=i;
341 else if ( c> 0)lower=i;
346 /**********************************************************************/
348 /* HSORT/BSEARCH WRAPPERS */
351 /**********************************************************************/
352 void **search_in_list_file ( void *key, int *p,int comp_len,FILE *fp, int len, size_t size, int entry_len)
357 if ( l==NULL)l=vcalloc ( 1, sizeof (int*));
359 l[0]=bsearch_file (key,p,0,comp_len,fp,len,entry_len,size,hsort_cmp);
360 if (l[0]==NULL)return NULL;
363 void **search_in_list_array ( void *key,int *p, int comp_len,void **L, int len, size_t size, int entry_len)
367 if ( l==NULL)l=vcalloc ( 1, sizeof (int*));
369 l[0]=bsearch_array (key,p,0,comp_len,L,len,entry_len,size,hsort_cmp);
370 if (l[0]==NULL)return NULL;
373 void **hsort_list_array ( void **L, int len, size_t size, int entry_len, int first_comp_field, int n_comp_fields)
375 return hsort_array (L, len,entry_len, size,first_comp_field, n_comp_fields,hsort_cmp , hsort_cpy);
377 FILE *hsort_list_file ( FILE*fp , int len, size_t size, int entry_len, int first_comp_field, int n_comp_fields)
380 return hsort_file (fp, len,entry_len, size,first_comp_field, n_comp_fields,hsort_cmp , hsort_cpy);
383 int hsort_cmp ( const void *a, const void *b, int first, int clen, size_t size)
391 for ( p=first; p<clen+first; p++)
393 if ( ax[p]<bx[p])return -1;
394 else if ( ax[p]==bx[p]);
399 void *hsort_cpy(void*to, void *from, size_t size)
407 for (p=0; p<(int)size; p++)
416 void test_hsort_list_array()
422 array=declare_int(n, 3);
423 for ( a=0; a<n; a++)array[a][0]=a;
425 hsort_list_array( (void**)array,n, sizeof (int), 3, 0, 1);
426 for ( a=0; a<n; a++)fprintf ( stderr, "\n%d %d", array[a][0],a);
427 myexit(EXIT_FAILURE);
431 /*********************************************************************/
433 /* B_SEARCH_FILE FUNCTIONS */
436 /*********************************************************************/
438 /*********************************************************************/
440 /* SORT/COMPARE/SEARCH FUNCTIONS */
443 /*********************************************************************/
444 static int sort_field;
445 int **search_in_list_int ( int *key, int k_len, int **list, int ne)
449 l=bsearch (&key,list, ne, sizeof(int**),(int(*)(const void*,const void*))(cmp_list_int));
452 void sort_float ( float **V,int N_F, int F, int left, int right)
455 qsort ( V, right+1, sizeof(int**),(int(*)(const void*,const void*))(cmp_int));
457 int cmp_float ( const float **a, const float **b)
459 if ( a[0][sort_field]< b[0][sort_field])return-1;
460 else if ( a[0][sort_field]==b[0][sort_field])return 0;
464 void sort_int_1D ( int *L, int n)
469 array=declare_int ( n, 1);
470 for ( a=0; a< n; a++)
472 sort_int ( array, 1, 0, 0, n-1);
473 for ( a=0; a< n; a++)
475 free_int ( array, n);
478 char** sort_string_array (char **V, int n)
482 qsort ( V,n,sizeof (char*),(int(*)(const void*,const void*))(pstrcmp));
488 int pstrcmp(char *p1, char *p2)
490 return strcmp(*(char **)p1, *(char **)p2);
492 void sort_int ( int **V,int N_F, int F, int left, int right)
496 qsort ( V, (right-left)+1, sizeof(int**),(int(*)(const void*,const void*))(cmp_int));
498 void sort_list_int ( int **V,int N_F, int F, int left, int right)
502 qsort ( V, (right-left)+1, sizeof(int**),(int(*)(const void*,const void*))(cmp_list_int));
505 void sort_list_int2 ( int **V,int *list,int N_F, int left, int right)
507 // just like sort_int_list, but uses list to to order the comparison of the keys
511 qsort ( V, (right-left)+1, sizeof(int**),(int(*)(const void*,const void*))(cmp_list_int2));
513 int cmp_list_int2 (const int**a, const int**b)
520 while ((c=order[p])!=-1)
522 if ( a[0][c]>b[0][c])return 1;
523 else if ( a[0][c]<b[0][c])return -1;
528 void sort_int_inv ( int **V,int N_F, int F, int left, int right)
534 qsort ( V, (right-left)+1, sizeof(int**),(int(*)(const void*,const void*))(cmp_int));
536 list=declare_int ((right-left)+1, N_F);
537 for ( a=left; a< (right-left)+1; a++)
539 for ( b=0; b< N_F; b++)
541 list[a-left][b]=V[a][b];
544 for ( a=left; a< (right-left)+1; a++)
546 for ( b=0; b< N_F; b++)
547 V[a][b]=list[(right-left)-a][b];
552 void sort_list_int_inv ( int **V,int N_F, int F, int left, int right)
558 qsort ( V, (right-left)+1, sizeof(int**),(int(*)(const void*,const void*))(cmp_list_int));
560 list=declare_int ((right-left)+1, N_F);
561 for ( a=left; a< (right-left)+1; a++)
563 for ( b=0; b< N_F; b++)
565 list[a-left][b]=V[a][b];
568 for ( a=left; a< (right-left)+1; a++)
570 for ( b=0; b< N_F; b++)
571 V[a][b]=list[(right-left)-a][b];
578 int cmp_int ( const int**a, const int**b)
580 if ( a[0][sort_field]< b[0][sort_field])return-1;
581 else if ( a[0][sort_field]==b[0][sort_field])return 0;
584 int cmp_list_int (const int**a, const int**b)
590 for ( c=0; c<=sort_field; c++)
592 if ( a[0][c]==UNDEFINED|| b[0][c]==UNDEFINED)ret=0;
593 else if ( a[0][c]>b[0][c])return 1;
594 else if ( a[0][c]<b[0][c])return -1;
596 if (undef==sort_field)
598 if (a[0][0]==b[0][0])return 0;
604 int name_is_in_list ( char *name, char **name_list, int n_name, int len)
608 /*Note: RETURNS THE Offset of the LAST Occurence of name in name_list*/
610 if ( name_list==NULL || name ==NULL)return -1;
613 for ( a=0; a< n_name; a++)
615 if ( name_list[a]==NULL);
618 if (strncmp ( name, name_list[a], len)==0)pos=a;
620 else if ( strm ( name, name_list[a]))pos=a;
624 char * check_list_for_dup ( char **list, int ne)
628 for ( a=0; a< ne-1; a++)
629 for ( b=a+1; b< ne; b++)if (strm ( list[a], list[b]))return list[a];
632 FILE *get_number_list_in_file ( FILE *fp, int *list, int *n, int *max_len)
637 while ( isspace((c=fgetc (fp))));
641 while ( isspace((c=fgetc (fp))) && c!='\n');
646 if ( n[0]>=max_len[0])
647 list=vrealloc ( list, (n[0]+100)*sizeof (int));
648 max_len[0]=(n[0]+100);
650 fscanf ( fp, "%d",&list[n[0]++]);
655 /*********************************************************************/
660 /*********************************************************************/
661 int quantile (int argc, char *argv[])
666 char *** string_list;
667 char *name, s1[1000], s2[1000];
673 fprintf (stderr, "\nquantile <fname> <quant: 0.00-1.00> [<top | bottom>]");
674 fprintf (stderr, "\nSplits your data in two according to the quantile");
675 fprintf (stderr, "\nReturns the top quantile or the bottom quantile");
676 fprintf (stderr, "\nData must be in <fname> with two fields/line: Field1=index, Field2=value\n");
677 fprintf (stderr, "\n1 27\n2 46\n3 5\n...\n");
678 fprintf (stderr, "\nValue can either be integer or float");
681 myexit (EXIT_FAILURE);
684 if (strm (argv[1], "stdin"))
687 fp=vfopen (name, "w");
688 while ( (c=fgetc(stdin))!=EOF)
690 fprintf ( fp, "%c", c);
700 n=count_n_line_in_file (name);
701 list=declare_int (n, 2);
702 string_list=declare_arrayN(3,sizeof (char), n, 2, 10);
704 fp=vfopen (name, "r");
706 while ( (c=fgetc (fp))!=EOF)
709 fscanf ( fp, "%s %s\n", s1, s2);
710 list[n][0]=(int)(atof(s1)*1000);
711 list[n][1]=(int)(atof(s2)*1000);
714 sprintf (string_list[n][0],"%s",s1);
715 sprintf (string_list[n][1],"%s",s2);
720 sort_int_inv ( list,3, 1, 0, n-1);
721 t=quantile_rank ( list,1,n, atof (argv[2]));
722 if ( argc!=4 || (argc==4 && strm (argv[3], "bottom")))
725 fprintf ( stdout, "%s %s\n", string_list[list[a][2]][0], string_list[list[a][2]][1]);
730 fprintf ( stdout, "%s %s\n", string_list[list[a][2]][0], string_list[list[a][2]][1]);
733 fprintf (stderr, "\nQuantile %.2f T= %d Out of N= %d entries\n", atof (argv[2]), t, n),
739 int quantile_rank (int **list,int field, int n, float p)
745 else if ( p==0) nr=n;
749 double g, q, np, i_part;
750 l=vcalloc ( n, sizeof (int));
751 for (a=n-1, b=0; b<n; a--, b++)
754 np=(double)n*(double)p;
755 g=modf (np, &i_part);
759 q=(float)l[j]+g*((float)l[j+1]-(float)l[j]);
762 while (nr<n && list[nr][field]>=q)nr++;
767 /*********************************************************************/
772 /*********************************************************************/
773 short* ga_memcpy_short ( short *array1, short *array2, int n)
777 for ( a=0; a< n; a++)
782 int * ga_memcpy_int ( int *array1, int *array2, int n)
786 for ( a=0; a< n; a++)
792 float* ga_memcpy_float ( float *array1, float *array2, int n)
796 for ( a=0; a< n; a++)
801 double* ga_memcpy_double (double *array1, double*array2, int n)
805 for ( a=0; a< n; a++)
813 /*recycle: get the bottom pointer on the top of the heap*/
815 void ** recycle (void **A, int l, int cycle)
819 B=vcalloc (l, sizeof (void*));
821 for ( c=0; c< cycle; c++)
823 for ( a=1, b=0; a<l; a++, b++) B[b]=A[a];
825 for ( a=0; a<l; a++)A[a]=B[a];
831 /* Old READ/WRITE ARRAY SIZE*/
833 #define WRITE_SIZE(type,function)\
834 void function ( int x, type *array, int os)\
837 char buf[SIZE_OF_INT+1];\
838 array+=os*SIZE_OF_INT;\
839 for ( a=0;a<SIZE_OF_INT; a++)array[a]=0;\
840 sprintf ( buf, "%d", x);\
842 array+=SIZE_OF_INT-l;\
843 for (a=0; a<l; a++)array[a]=(type)buf[a];\
845 WRITE_SIZE(short,write_size_short)
846 WRITE_SIZE(char,write_size_char)
847 WRITE_SIZE(int,write_size_int)
848 WRITE_SIZE(float,write_size_float)
849 WRITE_SIZE(double,write_size_double)
851 #define READ_ARRAY_SIZE(type, function)\
852 int function (type *array, int os)\
855 char buf[SIZE_OF_INT+1];\
857 array+=os*SIZE_OF_INT;\
858 while ( a!=SIZE_OF_INT && array[a]==0)a++;\
859 while ( a!=SIZE_OF_INT)buf[b++]=(char)array[a++];\
863 READ_ARRAY_SIZE(short,read_size_short)
864 READ_ARRAY_SIZE(char,read_size_char)
865 READ_ARRAY_SIZE(int,read_size_int)
866 READ_ARRAY_SIZE(float,read_size_float)
867 READ_ARRAY_SIZE(double,read_size_double)
869 /*********************************************************************/
874 /*********************************************************************/
876 #define SET_NUMBERS(type,function)\
877 type * function(type *list,int n, ...)\
885 buf=vcalloc(n, sizeof(type));\
886 index=vcalloc(n, sizeof(int));\
888 for ( i=0; i< n; i++)\
890 buf[i] =va_arg(ap,type);\
891 index[i]=va_arg(ap, int);\
892 if (index[i]>max)max=index[i];\
895 if (list==NULL)list=vcalloc ( max+1, sizeof (type));\
896 for ( i=0; i<n; i++)list[index[i]]=buf[i];\
901 /*SET_NUMBERS(short ,set_short)*/
902 /*SET_NUMBERS(char ,set_char)*/
903 SET_NUMBERS(int ,set_int)
904 /*SET_NUMBERS(float ,set_float)*/
905 SET_NUMBERS(double,set_double)
907 short ** duplicate_short ( short **array , int len, int field)
909 return copy_short (array ,declare_short ( len, field), len, field);
911 int ** duplicate_int ( int **array , int len, int field)
913 return copy_int (array ,declare_int ( len, field), len, field);
915 char ** duplicate_char ( char **array , int len, int field)
917 return copy_char (array ,declare_char ( len, field), len, field);
919 char * duplicate_string ( char *string)
929 buf=vcalloc ( l+1, sizeof(char));
930 sprintf ( buf, "%s", string);
934 float ** duplicate_float ( float **array , int len, int field)
936 return copy_float (array ,declare_float ( len, field), len, field);
938 double ** duplicate_double ( double **array , int len, int field)
940 return copy_double (array ,declare_double ( len, field), len, field);
945 /*********************************************************************/
947 /* COPY OF 2D ARRAY */
950 /*********************************************************************/
951 short ** copy_short( short **array1, short **array2, int len, int number_field)
955 if ( len==-1)len=read_array_size (array1,sizeof (short*));
956 if ( number_field==-1)number_field=read_array_size (array1[0],sizeof (short));
957 if ( array2)free_short ( array2, -1);
958 array2=declare_short ( len, number_field);
960 for ( a=0; a< len; a++)
961 ga_memcpy_short( array1[a],array2[a],number_field);
965 char ** copy_char ( char **array1, char **array2, int len, int number_field)
969 if ( array1==NULL)return NULL;
970 if ( len==-1)len=read_size_char (array1,sizeof (char*));
971 if ( number_field==-1)
973 number_field=read_size_char (array1[0],sizeof(char));
974 for ( a=0; a< len; a++)
975 number_field=MAX(number_field, strlen ( array1[a]))+1;
978 if ( array2)free_char (array2, -1);
979 array2=declare_char(len, number_field);
981 for ( a=0; a< len; a++)
982 sprintf ( array2[a], "%s", array1[a]);
986 int ** copy_int ( int **array1, int **array2, int len, int number_field)
990 if ( array1==NULL) return NULL;
991 if ( len==-1)len=read_size_int (array1, sizeof (int*));
992 if ( number_field==-1)number_field=read_size_int (array1[0],sizeof (int));
996 if (array2)free_int (array2, -1);
997 array2=declare_int ( len, number_field);
999 for ( a=0; a< len; a++)
1000 ga_memcpy_int( array1[a],array2[a],number_field);
1005 float ** copy_float ( float **array1, float **array2, int len, int number_field)
1009 if ( array1==NULL) return NULL;
1010 if ( len==-1)len=read_size_float (array1,sizeof (float*));
1011 if ( number_field==-1)number_field=read_size_float (array1[0],sizeof (float));
1013 if ( array2)free_float (array2, -1);
1014 array2=declare_float ( len, number_field);
1016 for ( a=0; a< len; a++)
1017 ga_memcpy_float( array1[a],array2[a],number_field);
1020 double ** copy_double (double **array1, double **array2, int len, int number_field)
1024 if ( array1==NULL) return NULL;
1025 if ( len==-1)len=read_size_double (array1,sizeof (double*));
1026 if ( number_field==-1)number_field=read_size_double (array1[0], sizeof (double));
1027 if ( array2)free_double (array2, -1);
1028 array2=declare_double ( len, number_field);
1030 for ( a=0; a< len; a++)
1031 ga_memcpy_double( array1[a],array2[a],number_field);
1034 /*********************************************************************/
1039 /*********************************************************************/
1045 Alignment ** cat_aln_list ( Alignment **list_to_cat,int first, int end, Alignment **rec_list)
1050 if ( list_to_cat==NULL)return rec_list;
1053 rec_list_start=(rec_list[-1])->nseq;
1054 rec_list=realloc_aln_array ( rec_list, end-first);
1055 for ( a=first, b=rec_list_start; a<end; a++, b++)copy_aln (list_to_cat[a], rec_list[b]);
1056 free_aln_array ( list_to_cat);
1061 /*********************************************************************/
1063 /* NUMBER ARRAY ANALYSE */
1066 /*********************************************************************/
1068 FILE * output_array_int (int **array, int len, int nf ,FILE *fp)
1071 for ( a=0; a<len; a++)
1072 {fprintf ( fp, "\n");
1073 for (b=0; b<nf; b++)fprintf (fp, "%5d ", array[a][b]);
1076 fprintf ( fp, "\n");
1079 #define RETURN_MAX_COOR(type,wf,rf,function,comparison,undef)\
1080 type function ( type ** array, int len_array, int field, int *coor)\
1085 if (array==NULL || len_array==0)return 0;\
1088 len_array=rf(array,sizeof (type*));\
1089 max=array[0][field];\
1091 for ( a=1; a< len_array; a++)\
1092 if ( max==undef)max=array[a][field];\
1093 else if ( array[a][field]!=undef)\
1094 if (array[a][field] comparison max)\
1095 {max=array[a][field];\
1102 RETURN_MAX_COOR(short,write_size_short,read_size_short,return_max_coor_short,>, UNDEFINED_SHORT)
1103 RETURN_MAX_COOR(char,write_size_char,read_size_char,return_max_coor_char,>, UNDEFINED_CHAR)
1104 RETURN_MAX_COOR(int,write_size_int,read_size_int,return_max_coor_int,>, UNDEFINED_INT)
1105 RETURN_MAX_COOR(float,write_size_float,read_size_float,return_max_coor_float,>, UNDEFINED_FLOAT)
1106 RETURN_MAX_COOR(double,write_size_double,read_size_double,return_max_coor_double,>, UNDEFINED_DOUBLE)
1107 RETURN_MAX_COOR(short,write_size_short,read_size_short,return_min_coor_short,<, UNDEFINED_SHORT)
1108 RETURN_MAX_COOR(char,write_size_char,read_size_char,return_min_coor_char,<, UNDEFINED_CHAR)
1109 RETURN_MAX_COOR(int,write_size_int,read_size_int,return_min_coor_int,<, UNDEFINED_INT)
1110 RETURN_MAX_COOR(float,write_size_float,read_size_float,return_min_coor_float,<, UNDEFINED_FLOAT)
1111 RETURN_MAX_COOR(double,write_size_double,read_size_double,return_min_coor_double,<, UNDEFINED_DOUBLE)
1112 #define RETURN_MAX(type,wf,rf,function,comparison,undef)\
1113 type function ( type ** array, int len_array, int field)\
1118 if (array==NULL || len_array==0)return 0;\
1121 if (len_array==-1)len_array=rf(array,sizeof (type*));\
1122 max=array[0][field];\
1123 for ( a=1; a< len_array; a++)\
1124 if ( max==undef)max=array[a][field];\
1125 else if ( array[a][field]!=undef)max=( array[a][field] comparison max)?array[a][field]:max;\
1127 return (max==undef)?0:max;\
1130 RETURN_MAX(short,write_size_short,read_size_short,return_max_short,>,UNDEFINED_SHORT)
1131 RETURN_MAX(char,write_size_char,read_size_char,return_max_char,>,UNDEFINED_CHAR)
1132 RETURN_MAX(int,write_size_int,read_size_int,return_max_int,>,UNDEFINED_INT)
1133 RETURN_MAX(float,write_size_float,read_size_float,return_max_float,>,UNDEFINED_FLOAT)
1134 RETURN_MAX(double,write_size_double,read_size_double,return_max_double,>,UNDEFINED_DOUBLE)
1135 RETURN_MAX(short,write_size_short,read_size_short,return_min_short,<,UNDEFINED_SHORT)
1136 RETURN_MAX(char,write_size_char,read_size_char,return_min_char,<,UNDEFINED_CHAR)
1137 RETURN_MAX(int,write_size_int,read_size_int,return_min_int,<,UNDEFINED_INT)
1138 RETURN_MAX(float,write_size_float,read_size_float,return_min_float,<,UNDEFINED_FLOAT)
1139 RETURN_MAX(double,write_size_double,read_size_double,return_min_double,<,UNDEFINED_DOUBLE)
1143 #define RETURN_2DMAX(type,wf,rf,function,comparison,undef)\
1144 type function ( type ** array, int start, int len_array, int first_field, int number_field)\
1148 if (array==NULL || len_array==0 || first_field<0 || number_field==0)return 0;\
1150 {max=array[start][first_field];\
1151 for ( a=start; a< start+len_array; a++)\
1152 for (b=first_field; b< first_field+number_field; b++)\
1153 if (array[a][b]!=undef)max=( array[a][b] comparison max)?array[a][b]:max;\
1157 RETURN_2DMAX(short,write_size_short,read_size_short,return_2Dmax_short,>, UNDEFINED_SHORT)
1158 RETURN_2DMAX(char,write_size_char,read_size_char,return_2Dmax_char,>,UNDEFINED_CHAR)
1159 RETURN_2DMAX(int,write_size_int,read_size_int,return_2Dmax_int,>,UNDEFINED_INT)
1160 RETURN_2DMAX(float,write_size_float,read_size_float,return_2Dmax_float,>,UNDEFINED_FLOAT)
1161 RETURN_2DMAX(double,write_size_double,read_size_double,return_2Dmax_double,>,UNDEFINED_DOUBLE)
1162 RETURN_2DMAX(short,write_size_short,read_size_short,return_2Dmin_short,<,UNDEFINED_SHORT)
1163 RETURN_2DMAX(char,write_size_char,read_size_char,return_2Dmin_char,<,UNDEFINED_CHAR)
1164 RETURN_2DMAX(int,write_size_int,read_size_int,return_2Dmin_int,<,UNDEFINED_INT)
1165 RETURN_2DMAX(float,write_size_float,read_size_float,return_2Dmin_float,<,UNDEFINED_FLOAT)
1166 RETURN_2DMAX(double,write_size_double,read_size_double,return_2Dmin_double,<,UNDEFINED_DOUBLE)
1168 #define RETURN_2DMAX_COOR(type,wf,rf,function,compare,undef)\
1169 type function ( type **array, int start1 , int end1, int start2, int end2,int *i, int *j)\
1173 if ( start1==-1)start1=0;\
1174 if ( start2==-1)start2=0;\
1175 if ( end1==-1)end1=rf(array,sizeof (type*));\
1176 if ( end2==-1)end2=rf(array[0],sizeof (type));\
1177 if ( array==NULL || (end1-start1)==0 || (end1-start1)>rf ( array,sizeof (type*)) || (end2-start2)==0)\
1185 for ( a=start1; a<end1; a++)\
1186 for ( b=start2; b<end2; b++)\
1188 if ( max==undef && array[a][b]!=undef)max=array[a][b];\
1189 else if ( array[a][b]!=undef && (array[a][b] compare max))\
1198 RETURN_2DMAX_COOR(short,write_size_short,read_size_short,return_2Dmax_coor_short,>,UNDEFINED_SHORT)
1199 RETURN_2DMAX_COOR(char,write_size_char,read_size_char,return_2Dmax_coor_char,>,UNDEFINED_CHAR)
1200 RETURN_2DMAX_COOR(int,write_size_int,read_size_int,return_2Dmax_coor_int,>,UNDEFINED_INT)
1201 RETURN_2DMAX_COOR(float,write_size_float,read_size_float,return_2Dmax_coor_float,>,UNDEFINED_FLOAT)
1202 RETURN_2DMAX_COOR(double,write_size_double,read_size_double,return_2Dmax_coor_double,>,UNDEFINED_DOUBLE)
1203 RETURN_2DMAX_COOR(short,write_size_short,read_size_short,return_2Dmin_coor_short,<,UNDEFINED_SHORT)
1204 RETURN_2DMAX_COOR(char,write_size_char,read_size_char,return_2Dmin_coor_char,<,UNDEFINED_CHAR)
1205 RETURN_2DMAX_COOR(int,write_size_int,read_size_int,return_2Dmin_coor_int,<,UNDEFINED_INT)
1206 RETURN_2DMAX_COOR(float,write_size_float,read_size_float,return_2Dmin_coor_float,<,UNDEFINED_FLOAT)
1207 RETURN_2DMAX_COOR(double,write_size_double,read_size_double,return_2Dmin_coor_double,<,UNDEFINED_DOUBLE)
1209 #define RETURN_WMEAN(type,wf,rf,function,sum_function,undef)\
1210 double function ( type **array, int len, int wfield,int sfield)\
1214 if ( len==0 ||array==NULL || len>rf ( array,sizeof (type*)))return 0;\
1217 if ( len==-1)len=rf(array,sizeof (type*));\
1218 for ( b=0, c=0,a=0; a< len; a++)\
1220 if (array[a][sfield]!=undef && array[a][wfield]!=undef )\
1222 b+=array[a][sfield];\
1223 c+=array[a][wfield];\
1227 return (c==0)?0:(b/c);\
1229 RETURN_WMEAN(short,write_size_short,read_size_short,return_wmean_short, return_sum_short,UNDEFINED_SHORT)
1230 RETURN_WMEAN(char,write_size_char,read_size_char, return_wmean_char,return_sum_char,UNDEFINED_CHAR)
1231 RETURN_WMEAN(int,write_size_int,read_size_int,return_wmean_int,return_sum_int,UNDEFINED_INT)
1232 RETURN_WMEAN(float,write_size_float,read_size_float,return_wmean_float,return_sum_float,UNDEFINED_FLOAT)
1233 RETURN_WMEAN(double,write_size_double,read_size_double,return_wmean_double,return_sum_double,UNDEFINED_DOUBLE)
1236 #define RETURN_MEAN(type,wf,rf,function,sum_function,undef)\
1237 double function ( type **array, int len, int field)\
1241 if ( len==0 ||array==NULL || len>rf ( array,sizeof(type*)))return 0;\
1244 for ( b=0, c=0,a=0; a< len; a++)\
1246 if (array[a][field]!=undef)\
1248 b+=array[a][field];\
1253 return (c==0)?0:(b/c);\
1255 RETURN_MEAN(short,write_size_short,read_size_short,return_mean_short, return_sum_short,UNDEFINED_SHORT)
1256 RETURN_MEAN(char,write_size_char,read_size_char, return_mean_char,return_sum_char,UNDEFINED_CHAR)
1257 RETURN_MEAN(int,write_size_int,read_size_int,return_mean_int,return_sum_int,UNDEFINED_INT)
1258 RETURN_MEAN(float,write_size_float,read_size_float,return_mean_float,return_sum_float,UNDEFINED_FLOAT)
1259 RETURN_MEAN(double,write_size_double,read_size_double,return_mean_double,return_sum_double,UNDEFINED_DOUBLE)
1261 #define RETURN_SUM(type,wf,rf,function,undef)\
1262 type function(type **array, int len, int field)\
1266 if ( len==0 ||array==NULL)return 0;\
1269 if ( len==-1)len=rf ( array,sizeof (type*));\
1270 for ( a=0; a< len; a++)\
1271 if ( array[a][field]!=undef)b+=array[a][field];\
1275 RETURN_SUM(short,write_size_short,read_size_short, return_sum_short,UNDEFINED_SHORT)
1276 RETURN_SUM(char,write_size_char,read_size_char,return_sum_char,UNDEFINED_CHAR)
1277 RETURN_SUM(int,write_size_int,read_size_int,return_sum_int,UNDEFINED_INT)
1278 RETURN_SUM(float,write_size_float,read_size_float,return_sum_float,UNDEFINED_FLOAT)
1279 RETURN_SUM(double,write_size_double,read_size_double,return_sum_double,UNDEFINED_DOUBLE)
1281 #define RETURN_SD(type,wf,rf,function,undef)\
1282 type function ( type **array, int len, int field,type mean) \
1286 if ( len==0 ||array==NULL || len>rf ( array,sizeof(type*)))return 0;\
1289 for ( a=0; a< len; a++)\
1291 if ((array[a][field]!=undef) && (mean-array[a][field])!=0)\
1292 c+=((double)mean-array[a][field])*((double)mean-array[a][field]);\
1294 c=sqrt(c)/(double)len;\
1295 return (type)MAX(c,1);\
1298 RETURN_SD(short,write_size_short,read_size_short, return_sd_short,UNDEFINED_SHORT)
1299 RETURN_SD(char,write_size_char,read_size_char,return_sd_char,UNDEFINED_CHAR)
1300 RETURN_SD(int,write_size_int,read_size_int,return_sd_int,UNDEFINED_INT)
1301 RETURN_SD(float,write_size_float,read_size_float,return_sd_float,UNDEFINED_FLOAT)
1302 RETURN_SD(double,write_size_double,read_size_double,return_sd_double,UNDEFINED_DOUBLE)
1303 double return_z_score( double x,double sum, double sum2, double n)
1310 sd=(n==0)?0:sqrt(sum2*n -sum*sum)/n;
1311 avg=(n==0)?0:(sum/n);
1312 z=(sd==0)?0:(x-avg)/sd;
1316 double* return_r (double **list, int n)
1318 double Sy, Sx, Sxy, Sx2, Sy2,r_up, r_low, x, y;
1322 r=vcalloc ( 3, sizeof (double));
1323 Sy=Sx=Sxy=Sx2=Sy2=0;
1325 for ( a=0; a<n; a++)
1336 r_low=(n*Sx2-(Sx*Sx))*(n*Sy2-(Sy*Sy));
1338 r[0]=(r_low==0)?0:r_up/r_low;
1344 r_low=(Sx2-n*x*x)*(Sy2-n*y*y);
1345 r[1]=(r_low==0)?0:r_up/r_low;
1350 #define INVERT_LIST(type,wf,rf,function,swap_function)\
1351 type* function (type *list, int len)\
1354 for ( a=0, b=len-1; a<b; a++, b--)swap_function ( &list[a], &list[b], 1);\
1357 INVERT_LIST(short,write_size_short,read_size_short, invert_list_short,swap_short)
1358 INVERT_LIST(char,write_size_char,read_size_char,invert_list_char,swap_char)
1359 INVERT_LIST(int,write_size_int,read_size_int,invert_list_int,swap_int)
1360 INVERT_LIST(float,write_size_float,read_size_float,invert_list_float,swap_float)
1361 INVERT_LIST(double,write_size_double,read_size_double,invert_list_double,swap_double)
1363 #define SWAP_FUNCTION(type,wf,rf,function)\
1364 void function(type *a, type *b, int n)\
1374 SWAP_FUNCTION(short,write_size_short,read_size_short,swap_short)
1375 SWAP_FUNCTION(char,write_size_char,read_size_char,swap_char)
1376 SWAP_FUNCTION(int,write_size_int,read_size_int,swap_int)
1377 SWAP_FUNCTION(float,write_size_float,read_size_float,swap_float)
1378 SWAP_FUNCTION(double,write_size_double,read_size_double,swap_double)
1380 #define RETURN_MAX_HORIZ(type,wf,rf,function,comparison,undef)\
1381 type function (type ** array, int len_array, int field)\
1385 if ( len_array==0)return 0;\
1388 max=array[field][0];\
1389 for ( a=1; a< len_array; a++)\
1390 if ( array[field][a]!=undef) max=( array[field][a] comparison max)?array[field][a]:max;\
1394 RETURN_MAX_HORIZ(short,write_size_short,read_size_short,return_max_short_hor,>,UNDEFINED_SHORT)
1395 RETURN_MAX_HORIZ(char,write_size_char,read_size_char,return_max_char_hor,>,UNDEFINED_CHAR)
1396 RETURN_MAX_HORIZ(int,write_size_int,read_size_int,return_max_int_hor,>,UNDEFINED_INT)
1397 RETURN_MAX_HORIZ(float,write_size_float,read_size_float,return_max_float_hor,>,UNDEFINED_FLOAT)
1398 RETURN_MAX_HORIZ(double,write_size_double,read_size_double,return_max_double_hor,>,UNDEFINED_DOUBLE)
1400 RETURN_MAX_HORIZ(short,write_size_short,read_size_short,return_min_short_hor,<,UNDEFINED_SHORT)
1401 RETURN_MAX_HORIZ(char,write_size_char,read_size_char,return_min_char_hor,<,UNDEFINED_CHAR)
1402 RETURN_MAX_HORIZ(int,write_size_int,read_size_int,return_min_int_hor,<,UNDEFINED_INT)
1403 RETURN_MAX_HORIZ(float,write_size_float,read_size_float,return_min_float_hor,<,UNDEFINED_FLOAT)
1404 RETURN_MAX_HORIZ(double,write_size_double,read_size_double,return_min_double_hor,<,UNDEFINED_DOUBLE)
1408 #define BEST_OF_MANY(type,wf,rf,function,undef)\
1409 type function (int n, ...)\
1415 /*first Arg: number of values\
1416 2nd Arg: maximise(1)/minimise(0)\
1417 3rd Arg: *int contains the indice of the best value\
1418 ... Arg: n type values\
1421 maximise=va_arg (ap, int);\
1422 fop=va_arg (ap, int*);\
1423 best=va_arg (ap, type);\
1425 for ( a=1; a<n; a++)\
1427 v=va_arg (ap, type);\
1433 if ( best==undef || v==undef);\
1434 else if ( maximise==1 && v>best)\
1439 else if ( maximise==0 && v<best)\
1448 /*BEST_OF_MANY(short,write_size_short,read_size_short, best_short,UNDEFINED_SHORT)*/
1449 /*BEST_OF_MANY(char,write_size_char,read_size_char,best_char,UNDEFINED_CHAR)*/
1450 BEST_OF_MANY(int,write_size_int,read_size_int,best_int,UNDEFINED_INT)
1451 /*BEST_OF_MANY(float,write_size_float,read_size_float,best_float,UNDEFINED_FLOAT)*/
1452 BEST_OF_MANY(double,write_size_double,read_size_double,best_double,UNDEFINED_DOUBLE)
1453 #define IS_DEFINED(type,function,undef)\
1454 int function(int n, ...)\
1460 for ( i=0; i< n; i++)\
1462 if(va_arg(ap,type)==undef)\
1471 /*IS_DEFINED(short,is_defined_short,UNDEFINED_SHORT)*/
1472 /*IS_DEFINED(char,is_defined_char, UNDEFINED_CHAR)*/
1473 IS_DEFINED(int,is_defined_int, UNDEFINED_INT)
1474 /*IS_DEFINED(float,is_defined_float, UNDEFINED_FLOAT)*/
1475 IS_DEFINED(double,is_defined_double,UNDEFINED_DOUBLE)
1477 int return_maxlen ( char ** array, int number)
1481 for ( a=0; a< number; a++)
1482 max=( strlen ( array[a])>max)?strlen ( array[a]):max;
1488 int return_minlen ( char ** array, int number)
1493 min=strlen( array[0]);
1494 for ( a=1; a< number; a++)
1495 min=( strlen ( array[a])>min)?strlen ( array[a]):min;
1502 float return_mean_diff_float ( float **array, int len, int field,float mean)
1507 for ( a=0; a< len; a++)
1509 if ( (mean-array[a][field])!=0)
1510 b+=sqrt((double)((float) ( mean-array[a][field])*(float)(mean-array[a][field])));
1513 return ((float)b/(float)len);
1518 void inverse_int ( int**array, int len, int field, int max, int min)
1521 for ( a=0; a< len; a++)
1522 array[a][field]=max-array[a][field]+min;
1524 void inverse_float ( float**array, int len, int field, int max, int min)
1527 for ( a=0; a< len; a++)
1528 array[a][field]=max-array[a][field]+min;
1530 void inverse_2D_float ( float **array, int start, int len, int start_field, int number_field, float max,float min)
1533 for ( a=0; a< start+len; a++)
1534 for ( b=start_field; b< start_field+ number_field; b++)
1535 array[a][b]=max-array[a][b]+min;
1538 int max_int (int*i, ...)
1541 int index, best_value=0, value;
1543 // expects n values : n, &index, i1, v1, i2, v2...., -1
1545 while ((index=va_arg(ap,int))!=-1)
1547 value=va_arg(ap, int);
1548 if ( a==0 || value>best_value)
1559 /*********************************************************************/
1561 /* SHELL INTERFACES */
1564 /*********************************************************************/
1565 char* getenv4debug (const char * val)
1567 /*efficient mean of getting an environment variable: checks only if one DEBUG is on*/
1573 if (getenv ("DEBUG_BLAST"))check=1;
1574 else if ( getenv ("DEBUG_TREE_COMPARE"))check=1;
1575 else if ( getenv ("DEBUG_MALN"))check=1;
1576 else if ( getenv ("DEBUG_EXTRACT_FROM_PDB"))check=1;
1577 else if ( getenv ("DEBUG_LIBRARY"))check=1;
1578 else if ( getenv ("DEBUG_FUGUE"))check=1;
1580 else if ( getenv ("DEBUG_REFORMAT"))check=1;
1581 else if ( getenv ("DEBUG_RECONCILIATION"))check=1;
1582 else if ( getenv ("DEBUG_TMP_FILE"))check=1;
1583 else if ( getenv ("DEBUG_TREE"))check=1;
1585 else if ( getenv ("DEBUG_SEQ_REFORMAT") && strm (PROGRAM, "SEQ_REFORMAT"))check=2;
1586 else if ( getenv ("DEBUG_TCOFFEE") && strm (PROGRAM, "T-COFFEE"))check=2;
1590 if ( check>0 && strm ( val, "DEBUG_TMP_FILE"))
1597 return getenv (val);
1607 char* get_env_variable ( const char *var, int mode)
1609 /*mode 0: return NULL if variable not set*/
1610 /*mode 1: crash if variable not set*/
1613 if (mode==NO_REPORT)return NULL;
1614 else if ( mode ==IS_NOT_FATAL)
1616 fprintf ( stderr, "\nYou must set the variable %s [FATAL]\n", var);
1621 fprintf ( stderr, "\nYou must set the variable %s [FATAL]\n", var);
1622 myexit (EXIT_FAILURE);
1626 else return getenv (var);
1629 void get_pwd ( char *name)
1636 string=vtmpnam(NULL);
1637 sprintf ( command, "pwd > %s", string);
1638 my_system (command);
1639 fp=vfopen ( string, "r");
1640 fscanf ( fp, "%s",name);
1642 sprintf ( command, "rm %s", string);
1643 my_system ( command);
1645 int pg_is_installed ( char *pg)
1654 fname= vtmpnam(NULL);
1656 sprintf ( command, "which %s > %s", pg, fname);
1657 my_system ( command);
1660 if ((fp=find_token_in_file ( fname, NULL, "Command"))){r=1;vfclose(fp);}
1668 /*********************************************************************/
1672 /*********************************************************************/
1673 char *num2plot (int value, int max, int line_len)
1678 static char *string;
1680 if ( string==NULL)string=vcalloc (1000, sizeof(char));
1682 if ( line_len==-1)len=30;
1685 value_len=((float)value/(float)max)*(float)len;
1687 sprintf ( string, "|");
1690 buf=generate_string(value_len, '*');
1691 sprintf ( string,"%s", buf);
1697 int perl_strstr ( char *string, char *pattern)
1702 char command[10000];
1705 if (!string) return 0;
1706 if (!pattern) return 0;
1710 string2=vcalloc ( strlen (string)+1, sizeof (char));
1711 sprintf ( string2,"%s", string);
1712 string2=substitute (string2, "(", " ");
1713 string2=substitute (string2, ")", " ");
1714 string2=substitute (string2, "'", " ");
1716 sprintf (command, "perl -e '$s=\"%s\";$x=($s=~/%s/);$x=($x==1)?1:0;print $x;'>%s", string2, pattern,tmp);
1718 if (check_file_exists(tmp))
1720 fp=vfopen (tmp, "r");
1721 fscanf (fp, "%d", &r);
1726 fprintf ( stderr, "COM: %s\n");
1732 float grep_function ( char *pattern, char *file)
1734 char command [1000];
1746 sprintf ( command, "grep %s %s > %s",pattern,file, s);
1747 my_system ( command);
1748 if ((fp=vfopen (s, "r"))==NULL )return 0;
1751 fgets ( command, 900, fp);
1752 l=strlen ( command);
1753 while ( !isdigit (command[l]))l--;
1755 while ( isdigit (command[l]) || command[l]=='.')
1757 buf1[a++]=command[l];
1762 for ( a=0, b=l-1;a< l; a++, b--)
1766 sscanf ( buf2, "%f", &f);
1767 sprintf ( command,"rm %s", s);
1768 my_system ( command);
1774 void crash_if ( int val, char *s)
1776 if ( val==0)crash(s);
1778 void crash ( char *s)
1784 fprintf ( stderr, "%s",s);
1785 a=vcalloc ( 10, sizeof (int));
1790 static int *local_table;
1791 int ** make_recursive_combination_table ( int tot_n_param, int *n_param, int *nc, int**table, int field)
1795 /* makes a table of all possible combinations*/
1797 if ( tot_n_param==0)
1800 fprintf ( stderr, "\nNULL RETURNED");
1805 if ( local_table!=NULL)vfree (local_table);
1806 local_table=vcalloc ( tot_n_param, sizeof (int));
1808 for ( a=0; a< tot_n_param; a++)local_table[a]=-1;
1809 for ( a=0; a< tot_n_param; a++)nc[0]=nc[0]*n_param[a];
1812 table=declare_int ( nc[0],tot_n_param);
1816 for ( b=0; b<n_param[field]; b++)
1819 local_table[field]=b;
1820 if ( field<tot_n_param-1)
1822 table=make_recursive_combination_table ( tot_n_param, n_param, nc, table, field+1);
1826 for ( c=0; c< tot_n_param; c++)table[nc[0]][c]=local_table[c];
1833 /*********************************************************************/
1835 /* STRING PROCESSING */
1838 /*********************************************************************/
1839 char *strnrchr ( char *s,char x, int n)
1842 for (a=0; a< n; a++)if (s[a]=='x')return s+a;
1848 sprintf ( buf, "%d", n);
1849 return strlen (buf)+1;
1851 char * update_string (char string1[], char string2[])
1853 if ( string1==string2);
1854 else if ( string2==NULL)
1856 if ( string1==NULL)string1=vcalloc ( 1, sizeof(char));
1862 l1=read_array_size_new (string1);
1863 l2=strlen (string2)+1;
1865 string1=vrealloc (string1, (l2)*sizeof (char));
1866 sprintf ( string1, "%s", string2);
1871 char* strcatf (char *string1,char *string2, ...)
1877 buf=vcalloc ( read_array_size_new (string1)+1, sizeof (char));
1878 va_start (ap, string2);
1879 vsprintf (buf, string2, ap);
1881 string1=vcat (string1, buf);
1886 char *vcat ( char *st1, char *st2)
1891 if ( !st1 && !st2)return NULL;
1892 if ( st1) l+=strlen (st1);
1893 if ( st2) l+=strlen (st2);
1896 ns=vcalloc ( l, sizeof (char));
1897 sprintf ( ns, "%s%s", (st1)?st1:"", (st2)?st2:"");
1900 int print_param ( char *param, FILE *fp);
1901 int print_param ( char *param, FILE *fp)
1907 if (!p)p=vcalloc (1000, sizeof (char*));
1909 for ( a=0; a<np; a++)if (p[a]==param) return 0;
1913 fprintf ( fp, "\nPARAMETERS: %s\n", param);
1917 int strget_param ( char *string, char *token1, char *token2, char *format, ...)
1919 /*string: command line
1921 token2: default value (in a string)
1922 format: standard scanf format
1923 ... arguments for scanf
1930 if (!string) return 0;
1931 //print_param (string, stdout);
1932 //print_param (string, stderr);
1933 va_start (ap, format);
1934 buf=after_strstr ( string, token1);
1939 buf2=vcalloc (strlen (buf)+1, sizeof (char));
1940 sprintf ( buf2, "%s", buf);
1941 buf2=substitute (buf2, "__", " ");
1942 n=my_vsscanf (buf2, format, ap);
1945 else {n=my_vsscanf (token2, format, ap);}
1951 int strscanf (char *string1,char *token, char *format,...)
1957 va_start (ap,format);
1958 buf=after_strstr ( string1, token);
1959 if ( buf){n=my_vsscanf (buf, format, ap);}
1966 int match_motif ( char *string, char **motif)//crude string matching, the motif and the string have the same length
1969 if (!motif) return 0;
1971 for ( a=0; a<l; a++)
1973 if ( motif[a][0]!='*' && !strchr (motif[a], string[a]))return 0;
1978 char *after_strstr ( char *string, char *token)
1981 if ( (p=vstrstr (string, token)))return p+strlen (token);
1984 char* lstrstr ( char *in, char *token)//return strstr if matches on the left
1987 if ((s=vstrstr(in, token))&&s==in)return s;
1992 char *vstrstr ( char *in, char *token)
1994 if (!in || !token) return NULL;
1995 else return strstr (in, token);
1997 char ** push_string (char *val, char **stack, int *nval, int position)
2003 if (!val || nval[0]<=position)return stack;
2005 new_stack=vcalloc ( nval[0], sizeof (char*));
2006 new_stack[position]=val;
2007 for (a=0; a< position; a++)new_stack[a]=stack[a];
2008 for (a=position+1; a<nval[0]; a++)new_stack[a]=stack[a-1];
2014 int vsrand (int val)
2016 static int initialized;
2018 if (initialized) return 0;
2019 else if ( getenv ("DEBUG_SRAND"))
2027 /*Make sure two processes in a row do not get the same time value*/
2028 srand ((val==0)?time(NULL):val);
2033 int *randomize_list (int *list, int len, int ncycle)
2038 if ( ncycle==0)ncycle=len;
2039 for ( a=0; a<ncycle; a++)
2049 /*Replace by a gap the parts of the two strings that do not OVERLAP*/
2050 /*returns the length of the overlap*/
2051 int vstrcmp (const char *s1, const char *s2)
2053 if ( !s1 && !s2)return 0;
2054 else if ( !s1 || !s2)return 1;
2055 else return strcmp ( s1, s2);
2057 int vstrncmp (const char *s1, const char *s2, int n)
2059 if ( !s1 && !s2)return 0;
2060 else if ( !s1 || !s2)return 1;
2061 else return strncmp ( s1, s2, n);
2063 FILE *print_array_char (FILE *out, char **array, int n, char *sep)
2066 if ( array==NULL || read_size_char (array,sizeof (char*))<n)
2068 fprintf ( stderr, "\nORB in print_array_char [FATAL]\n");
2071 for ( a=0; a< n; a++)fprintf ( out, "%s%s", array[a],sep);
2074 char * path2filename ( char *array)
2079 name=vcalloc ( strlen (array)+2, sizeof (char));
2080 F=parse_fname (array);
2081 if ( F->suffix)sprintf ( name, "%s.%s", F->name, F->suffix);
2082 else sprintf (name, "%s", F->name);
2086 Fname* parse_fname ( char *array)
2093 F=declare_fname (sizeof (array));
2095 sprintf ( F->full, "%s", array);
2096 sprintf ( F->path, "%s", array);
2098 while (l!=-1 && (F->path)[l]!='/')(F->path)[l--]='\0';
2100 sprintf ( F->name, "%s", array+l+1);
2104 if((F->name)[l]=='.')
2107 sprintf ( F->suffix, "%s", F->name+l+1);
2115 char *filename2path (char *name)
2119 if (isdir (name))return name;
2122 nname=vcalloc (x+2, sizeof (char));
2123 sprintf ( nname, "%s", name);
2124 while ( x >=0 && nname[x]!='/')nname[x--]='\0';
2126 if ( !isdir (nname) || !nname[0]){vfree (nname); return NULL;}
2134 char *extract_suffixe ( char *array)
2140 new_string=vcalloc ( l+1, sizeof (char));
2141 sprintf (new_string, "%s",array);
2144 while (x!=new_string && x[0]!='.' && x[0]!='/' )x--;
2145 if ( x[0]=='.')x[0]='\0';
2146 else if (x[0]=='/')return x+1;
2148 while ( x!=new_string && x[0]!='/')x--;
2150 return (x[0]=='/')?x+1:x;
2152 void string_array_upper ( char **string, int n)
2155 for ( a=0; a< n; a++)upper_string (string[a]);
2157 void string_array_lower ( char **string, int n)
2160 for ( a=0; a< n; a++)lower_string (string[a]);
2163 char *upper_string ( char *string)
2167 len=strlen ( string);
2168 for ( a=0; a< len; a++)string[a]=toupper ( string[a]);
2171 char *lower_string ( char *string)
2175 len=strlen ( string);
2176 for ( a=0; a< len; a++)string[a]=tolower ( string[a]);
2179 void string_array_convert ( char **array, int n_strings, int ns, char **sl)
2183 for ( a=0; a< n_strings; a++)string_convert ( array[a], ns, sl);
2185 void string_convert( char *string, int ns, char **sl)
2189 for ( a=0; a< l; a++)
2190 string[a]=convert(string[a], ns, sl);
2192 int convert ( char c, int ns, char **sl)
2197 for ( a=0; a< ns; a++)
2199 if ((return_char=convert2 ( c, sl[a]))!=-1)
2206 int convert2 ( char c, char *list)
2214 return_char=(list[l1-1]=='#')?c:list[l1-1];
2216 for ( a=0; a< l1; a++)
2217 if (list[a]=='#')return return_char;
2218 else if ( list[a]==c)return return_char;
2222 char* substitute_old ( char *string_in, char *t, char *r)
2227 /*REplaces every occurence of token t with token r in string_in*/
2229 if ( string_in==NULL || t==NULL || r==NULL) return string_in;
2233 l=read_array_size_new ((void*)string_in)+1;
2235 string_out=vcalloc (l, sizeof (char));
2236 delta=strlen(r)-strlen (t);
2237 delta=(delta<0)?0:delta;
2239 while ( (p=strstr ( string_in, t))!=NULL)
2246 string_out=vrealloc(string_out, sizeof (char)*l);
2249 strcat ( string_out, string_in);
2250 strcat ( string_out, r);
2251 string_in=p+strlen (t);
2253 strcat ( string_out, string_in);
2254 if (l<strlen (string_out))
2256 heap_in=vrealloc (heap_in, sizeof(char)*(strlen (string_out)+1));
2258 sprintf ( heap_in, "%s", string_out);
2262 //Makes sure substitutions of the tild only occur on valid UNIX pathnames
2263 char* tild_substitute ( char *string_in, char *t, char *r)
2267 p=strstr ( string_in, "~");
2268 if ( p==NULL)return string_in;
2269 else if (p && p!=string_in && p[-1]!='/')return string_in;
2273 return substituteN (string_in, t, r,1);
2277 char* substitute_char ( char *string_in, char t, char r)
2280 while ( string_in && string_in[n]!='\0')
2282 if ( string_in[n] == t)string_in[n]=r;
2287 char* substitute ( char *string_in, char *t, char *r)
2289 /*REplaces every occurence of token t with token r in string_in*/
2290 return substituteN(string_in, t, r,0);
2292 char* substituteN ( char *string_in, char *t, char *r, int N)
2295 char *p, *heap_in, n=0;
2296 int delta, l, lsi,lso,lr,lt,nt;
2298 /*REplaces the first N Occurences*/
2299 if ( string_in==NULL || t==NULL || r==NULL) return string_in;
2303 l=read_array_size_new ((void*)string_in)+1;
2306 lsi=strlen (string_in);
2308 delta=((lr-lt)>0)?(lr-lt):0;
2310 while ( (p=strstr (string_in, t))!=NULL)
2318 string_out=vcalloc (lso+1, sizeof (char));
2320 while ((N==0 ||n<N) && (p=strstr ( string_in, t))!=NULL)
2323 strcat ( string_out, string_in);
2324 strcat ( string_out, r);
2328 strcat ( string_out, string_in);
2329 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));}
2330 sprintf ( heap_in, "%s", string_out);
2336 char **clean_string (int n, char **string)
2339 if ( !string) return string;
2340 for (a=0; a< n; a++)
2342 if (!string[a]) continue;
2343 l=strlen (string[a]);
2347 if (!isgraph(c) && c!='\n' && c!='\t' && c!=' ')string[a][b]=' ';
2356 int str_overlap ( char *string1, char *string2, char x)
2367 if ( strm ( string1, string2))return 0;
2370 l1=strlen ( string1);
2371 l2=strlen ( string2);
2372 array=declare_int ( strlen ( string1), strlen ( string2));
2373 for ( a=0; a< l1; a++)
2374 for ( b=0; b< l2; b++)
2376 if ( a==0 || b==0)array[a][b]=(string1[a]==string2[b])?1:0;
2378 if (string1[a]==string2[b])
2380 score=array[a][b]=array[a-1][b-1]+1;
2389 start1=(max1+1)-max_val;
2392 start2=(max2+1)-max_val;
2395 for ( a=0; a< l1; a++)if ( a<start1 || a> end1)string1[a]=x;
2396 for ( a=0; a< l2; a++)if ( a<start2 || a> end2)string2[a]=x;
2398 free_int ( array, l1);
2404 int get_string_line ( int start, int n_lines, char *in, char *out)
2412 while ( (c=in[start++])!='\n' && c!='\0')
2420 return (c=='\0')?-1:start;
2424 FILE * output_string_wrap ( int wrap,char *string, FILE *fp)
2431 for ( a=0, b=1; a< l; a++, b++)
2433 fprintf ( fp, "%c", string[a]);
2436 fprintf ( fp, "\n");
2443 char * extract_char ( char * array, int first, int len)
2448 len= ( len<0)?0:len;
2450 array2=vcalloc ((len+1), sizeof (char));
2452 for ( a=0; a<len; a++)
2453 array2[a]=array[first++];
2460 int check_cl4t_coffee (int argc, char **argv)
2463 if ( name_is_in_list ("-other_pg", argv, argc, 100)!=-1)return 1;
2464 else if (name_is_in_list ( "tcoffee_test_seq.pep", argv, argc, 100)!=-1)return 1;
2467 int a, inseq, result;
2469 char command[10000];
2472 for ( inseq=0,a=0; a<argc; a++)
2474 if ( inseq && argv[a][0]=='-')
2478 else if (strm ( argv[a], "-seq"))inseq=1;
2482 strcat ( command, " ");
2483 strcat ( command, argv[a]);
2486 name=vcalloc ( 100, sizeof (char));
2487 sprintf ( name, "TCIT_%d", (int)rand()%10000);
2488 strcat ( command, " -seq tcoffee_test_seq.pep -quiet -no_error_report");
2489 fp=vfopen ( name, "w");
2490 fprintf ( fp, ">A\nthecat\n>B\nthecat\n");
2492 result=safe_system (command);
2493 printf_system ( "rm %s.*", name);
2495 if (result) {myexit (EXIT_FAILURE);return 0;}
2500 char** merge_list ( char **argv, int *argc)
2505 char current [STRING];
2507 out=declare_char (argc[0], STRING);
2512 while (a< n_in && !is_parameter ( argv[a]))
2514 sprintf (out[argc[0]++], "%s", argv[a]);
2520 for ( a=0; a< n_in; a++)
2522 if ( is_parameter (argv[a]))
2524 sprintf ( out[argc[0]++], "%s", argv[a]);
2525 sprintf ( current, "%s", argv[a]);
2527 for ( b=0; b< n_in;)
2529 if ( is_parameter (argv[b]) && strm (current, argv[b]))
2533 while ( b<n_in && !is_parameter ( argv[b]) )
2537 sprintf ( out[argc[0]++], "%s", argv[b]);
2552 free_char (argv, -1);
2557 int * string2num_list_old ( char *string)
2559 /*Breaks down a list of numbers separated by any legal separator and put them in an array of the right size*/
2560 /*Returns list a list of integer*/
2561 /*Skips non numbers*/
2567 buf=vcalloc ( strlen (string)+1, sizeof (char));
2570 sprintf ( buf, "%s", string);
2571 s=strtok (buf, SEPARATORS);
2575 s=strtok (NULL, SEPARATORS);
2577 list=vcalloc (n+1, sizeof (int));
2580 sprintf ( buf, "%s", string);
2581 s=strtok (buf, SEPARATORS);
2584 if (is_number(s))list[n++]=atoi(s);
2585 s=strtok (NULL, SEPARATORS);
2594 int *name_array2index_array ( char **list1, int n1, char **list2, int n2)
2597 /*returns an indexed list of the position of list1 in list2*/
2598 list=vcalloc ( n1, sizeof (int));
2599 for ( a=0, max=0; a< n1; a++)max=MAX(max, strlen (list1[a]));
2600 for ( a=0 ; a< n2; a++)max=MAX(max, strlen (list2[a]));
2602 for ( a=0; a< n1; a++)
2604 list[a]=name_is_in_list (list1[a],list2,n2,max);
2609 int * string2num_list ( char *string)
2611 return string2num_list2(string, SEPARATORS);
2613 int * string2num_list2 ( char *string, char *separators)
2615 /*Breaks down a list of numbers separated by any legal separator and put them in an array of the right size*/
2616 /*Returns list a list of integer*/
2617 /*Skips non numbers*/
2623 nlist=string2list2 (string, separators);
2625 if (nlist==NULL) return NULL;
2628 list=vcalloc ( n, sizeof (int));
2630 for (m=1, a=1; a< n; a++)
2632 if (is_number(nlist[a]))list[m++]=atoi(nlist[a]);
2636 free_char (nlist, -1);
2637 if ( m==0){vfree(list); return NULL;}
2646 char * list2string ( char **list, int n)
2648 return list2string2 ( list, n, " ");
2650 char * list2string2 ( char **list,int n, char* sep)
2655 for ( l=0,a=0; a<n; a++)l+=strlen ( list[a])+1;
2656 string=vcalloc (l+1, sizeof (char));
2657 for ( a=0; a< n; a++)
2659 strcat ( string, list[a]);
2660 strcat ( string, sep);
2665 char ** string2list ( char *string)
2667 return string2list2(string, SEPARATORS);
2669 char ** string2list2 ( char *string, char *separators)
2671 /*Breaks down a list of words separated by any legal separator and put them in an array of the right size*/
2672 /*Returns list a list of char with the size written in list[0]*/
2679 if ( string==NULL)return NULL;
2680 buf=vcalloc ( strlen (string)+1, sizeof (char));
2684 sprintf ( buf, "%s", string);
2685 s=strtok (buf, separators);
2690 max_len=MAX(max_len,(strlen (s)));
2691 s=strtok (NULL, separators);
2695 if ( n==0){vfree(buf); return NULL;}
2697 list=declare_arrayN (2, sizeof (char), n+1, max_len+1);
2700 sprintf ( buf, "%s", string);
2701 s=strtok (buf, separators);
2704 sprintf (list[n++], "%s",s);
2705 s=strtok (NULL, separators);
2708 sprintf (list[0], "%d", n);
2715 char** break_list ( char **argv, int *argc, char *separators)
2724 /*Breaks down the argv command line in smaller units, breaking at every separator*/
2725 out=vcalloc (MAX_N_PARAM, sizeof (char*));
2729 if ( n_in>=MAX_N_PARAM)
2731 fprintf ( stderr, "\nERROR: too many parameters, recompile with MAX_N_PARAM set at a higher velue [FATAL:%s]\n", PROGRAM);\
2732 myexit (EXIT_FAILURE);
2735 for ( a=0; a< n_in; a++)
2740 if (cont)ar=get_list_of_tokens( argv[a], separators,&n_ar);
2741 else ar=get_list_of_tokens( argv[a],"",&n_ar);
2744 for ( b=0; b< n_ar; b++)
2746 out[argc[0]]=vcalloc( strlen (ar[b])+1, sizeof (char));
2747 sprintf (out[argc[0]++], "%s", ar[b]);
2751 if ( strstr (argv[a], "-other_pg"))cont=0;
2757 char *invert_string2 (char *string)
2763 buf=vcalloc ( l+1, sizeof (char));
2764 for ( a=l-1, b=0; a>=0; a--, b++)
2766 sprintf (string, "%s", buf);
2770 char *invert_string (char *string)
2772 return string2inverted_string(string);
2774 char* string2inverted_string(char *string)
2780 buf=vcalloc ( l+1, sizeof (char));
2781 for ( a=l-1, b=0; a>=0; a--, b++)
2786 char ** get_list_of_tokens ( char *in_string, char *separators, int *n_tokens)
2794 if ( in_string==NULL || strm(in_string, ""));
2795 else if ( in_string[0]=='[')
2797 list=declare_char (1, strlen ( in_string)+1);
2798 sprintf ( list[n_tokens[0]], "%s",in_string);
2803 list=declare_char (strlen ( in_string)+1, 1);
2804 string=vcalloc ( strlen(in_string)+1, sizeof (char));
2805 sprintf ( string, "%s", in_string);
2807 while ( (p=strtok ((p==NULL)?string:NULL, ((separators==NULL)?SEPARATORS:separators)))!=NULL)
2809 list[n_tokens[0]]=vrealloc ( list[n_tokens[0]], sizeof (char) *strlen (p)+1);
2810 sprintf ( list[n_tokens[0]], "%s", p);
2819 char **ungap_array ( char **array, int n)
2822 for ( a=0; a< n; a++)ungap(array[a]);
2826 void ungap ( char *seq)
2828 remove_charset ( seq, "ungap");
2830 int seq2len (char *seq, char *pset,char *nset)
2833 //count all the residues in pset and NOT in nset
2834 if ( !seq) return 0;
2837 //returns the len of the string
2838 for (a=0; a< l; a++)
2841 if ( pset && nset && strchr (pset, c) && !strchr (nset, c))t++;
2842 else if ( pset && strchr (pset, c))t++;
2843 else if ( nset && !strchr (nset, c))t++;
2847 int seq2res_len (char *seq)
2849 return seq2len (seq, NULL, GAP_LIST);
2851 char* remove_charset_from_file (char *fname, char *set)
2858 fp1=vfopen (fname, "r");
2859 fp2=vfopen (tmp=vtmpnam (NULL), "w");
2860 while ( (c=fgetc(fp1))!=EOF)
2862 if (!strchr ( set,c))fprintf ( fp2, "%c", c);
2869 void remove_charset ( char *seq, char *set)
2874 set2=vcalloc (256, sizeof (char));
2875 if ( strm (set, "!alnum"))
2877 for ( b=0,a=1;a< 256; a++)if ( !isalnum (a))set2[b++]=a;
2879 else if ( strm ( set, "ungap"))
2881 sprintf ( set2, "%s", GAP_LIST);
2885 sprintf ( set2, "%s", set);
2889 for (b=0, a=0; a<=l; a++)
2891 if ( strchr ( set2, seq[a]));
2892 else seq[b++]=seq[a];
2899 char **char_array2number ( char ** array, int n)
2902 for ( a=0; a< n; a++)array[a]=char2number(array[a]);
2905 char *char2number ( char * array)
2911 for ( a=0; a< l; a++)
2913 if ( isdigit(array[a]) && array[a]!=NO_COLOR_RESIDUE && array[a]!=NO_COLOR_GAP )array[a]-='0';
2914 else if ( array[a]<9);
2915 else if ( array[a]==NO_COLOR_RESIDUE || array[a]==NO_COLOR_GAP)array[a]=NO_COLOR_RESIDUE;
2921 /*turns a char into a pointer*/
2922 if ( p==NULL) return 0;
2923 else return atol(p);
2926 char *mark_internal_gaps(char *seq, char symbol)
2933 cache_seq=vcalloc ( l+1, sizeof (char));
2934 sprintf ( cache_seq, "%s", seq);
2936 for ( gap=0, in_seq=0,a=0; a< l; a++)
2939 if ( !gap && !in_seq)in_seq=1;
2940 if (gap && in_seq)seq[a]=symbol;
2943 for (gap=0, in_seq=0,a=l-1; a>=0; a--)
2946 if ( !gap && !in_seq)break;
2947 if (gap && !in_seq)seq[a]=cache_seq[a];
2953 void splice_out ( char *seq, char x)
2959 for (b=0, a=0; a<=l; a++)
2961 else seq[b++]=seq[a];
2964 char *splice_out_seg ( char *seq, int pos, int len)
2968 if (seq==NULL || pos<0) return seq;
2971 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);
2973 for (a=pos; a< l; a++)
2979 int isblanc ( char *buf)
2983 if ( buf==NULL)return 0;
2985 for ( a=0; a< l; a++)
2986 if (isalnum (buf[a]))return 0;
2992 int is_number ( char *num)
2998 if ( !strchr ("0123456789.-+", num[a]))return 0;
3002 int is_alnum_line ( char *buf)
3006 for ( a=0; a< l; a++)
3007 if (isalnum (buf[a]))return 1;
3010 int is_alpha_line ( char *buf)
3014 for ( a=0; a< l; a++)
3015 if (isalpha (buf[a]))return 1;
3018 int case_insensitive_strcmp ( char *string1, char *string2)
3023 if ( !string1 && !string2) return 1;
3024 else if ( !string1 && !string2) return 0;
3028 for ( a=0; a< l; a++)
3030 if (tolower(string1[a])!=tolower(string2[a]))return 0;
3035 int get_string_sim ( char *string1, char *string2, char *ignore)
3045 len1=strlen (string1);
3046 len2=strlen (string2);
3048 if ( len1!=len2)return 0;
3050 for ( a=0; a< len1; a++)
3054 if ( !is_in_set (r1, ignore) && !is_in_set (r2, ignore))
3067 return (int) (sim*100)/pos;
3072 return (is_in_set (x, AA_ALPHABET) && !is_in_set (x, GAP_LIST));
3074 int is_rna ( char x)
3076 return (is_in_set (x, RNAONLY_ALPHABET)&& !is_in_set (x, GAP_LIST));
3078 int is_dna ( char x)
3080 return (is_in_set (x, DNA_ALPHABET)&& !is_in_set (x, GAP_LIST));
3082 int is_gap ( char x)
3084 return ( is_in_set ( x, GAP_LIST));
3086 int is_gop ( int p, char *s)
3089 else if (p<=0)return 0;
3090 else if (s[p]!='-' && s[p+1]=='-')return 1;
3094 char* get_alphabet ( char *seq, char *alphabet)
3095 /*This function builds an alphabet from the string seq*/
3096 /*Alphabet does not need to be emppty. The total number of unique symbols is returned*/
3097 /*Gaps, as defined in the GAP_LIST are ignored*/
3102 if ( !alphabet) alphabet=vcalloc ( 200, sizeof (char));
3106 for ( a=0; a<len; a++)
3107 if ( !is_gap ( seq[a]) && !is_in_set ( seq[a], alphabet+1)){alphabet[(int)++alphabet[0]]=seq[a];}
3111 int array_is_in_set ( char *x, char *set )
3117 for ( a=0; a< len; a++)
3118 if ( !is_in_set ( x[a], set))return 0;
3123 int is_in_set ( char r, char *list)
3130 if ( strstr ( list,s)!=NULL)return 1;
3134 char * generate_void ( int x)
3136 return generate_string (x, ' ');
3139 char * generate_null ( int x)
3141 return generate_string ( x, '-');
3144 char * generate_string ( int x, char y)
3149 string = vcalloc ( x+1, sizeof ( char));
3151 for ( a=0; a< x; a++)
3157 char * translate_string (char *string, char *in, char*out)
3165 if ( l2!=strlen (out))
3167 fprintf ( stderr, "\nERROR: translate_string function: IN alphabet (%s) must have the same size as OUT alphabet (%s) [FATAL:%s]\n", in, out, PROGRAM);
3168 myexit (EXIT_FAILURE);
3171 for ( a=0; a< l1; a++)
3173 for ( b=0; b< l2; b++)
3175 if ( string[a]==in[b])
3186 int get_longest_string (char **array,int n, int *len, int *index)
3189 int max_len=0, max_index=0;
3191 if ( n==0 || array==NULL )return 0;
3192 if ( n==-1)n=read_size_char(array,sizeof (char*));
3195 if (read_size_char ( array,sizeof (char*))<n)
3197 fprintf ( stderr, "\nBAD REQUEST: Number of strings=%d, expected number=%d", read_size_char ( array,sizeof (char*)),n);
3198 crash ("[FATAL/util.c]");
3202 max_len=strlen(array[0]);
3204 for ( a=1; a< n; a++)
3206 l=strlen ( array[a]);
3213 if (index!=NULL)index[0]=max_index;
3214 if (len!=NULL)len[0]=max_len;
3220 int get_shortest_string (char **array,int n, int *len, int *index)
3225 if ( n==0|| array==NULL || read_size_char ( array,sizeof (char*))<n)return 0;
3228 min_len=strlen(array[0]);
3230 for ( a=1; a< n; a++)
3232 l=strlen ( array[a]);
3239 if (index!=NULL)index[0]=a;
3240 if (len!=NULL)len[0]=min_len;
3245 char **pad_string_array ( char **array, int n, int len, char pad)
3248 for ( a=0; a< n; a++)
3250 l= strlen ( array[a]);
3251 for (b=l; b<len; b++)array[a][b]=pad;
3256 char **right_pad_string_array ( char **array, int n, int len, char pad)
3258 return pad_string_array(array, n, len, pad);
3260 char **left_pad_string_array ( char **array, int n, int len, char pad)
3265 buf=vcalloc ( len+1, sizeof (char));
3266 for ( a=0; a< n; a++)
3268 l= strlen ( array[a]);
3269 for (b=0; b<(len-l); b++)buf[b]=pad;
3270 for (c=0,b=(len-l); b<=len; c++,b++)buf[b]=array[a][c];
3271 sprintf (array[a], "%s", buf);
3277 char * crop_string (char *string, int start, int end)
3280 /*Extract [start-end[*/
3282 if ( strlen (string)<end)
3284 fprintf ( stderr, "\nString=%s, start=%d end=%d", string, start, end);
3285 crash ( "Wrong End in crop String [FATAL]");
3289 buf=vcalloc (strlen (string)+1, sizeof (char));
3291 sprintf ( buf, "%s", string+start);
3292 sprintf ( string,"%s", buf);
3299 int get_distance2char ( char *x, char*list)
3304 if (x==NULL) return 0;
3305 while (!is_in_set (y[0], list) && y[0]!='\0')y++;
3308 /*********************************************************************/
3310 /* TIME FUNCTIONS */
3313 /*********************************************************************/
3317 static long milli_sec_conv=1000;
3320 FILE *print_program_information (FILE *fp, char *comment)
3322 fprintf ( fp, "# Results Produced with %s (%s)\n",PROGRAM, VERSION);
3323 fprintf ( fp, "# %s is available from %s\n",PROGRAM,URL);
3324 if (comment)fprintf ( fp, "# %s\n", comment);
3327 FILE* print_cpu_usage (FILE *fp, char *comment)
3329 fprintf ( fp, "# %s CPU Usage: %d millisec\n", comment, get_time());
3332 void print_exit_success_message ()
3335 fprintf ( stderr, "\n# TERMINATION STATUS: SUCCESS [PROGRAM: %s]\n", PROGRAM);
3337 void print_exit_failure_message ()
3339 fprintf ( stderr, "\n# TERMINATION STATUS: FAILURE [PROGRAM: %s]\n", PROGRAM);
3348 struct tms time_buf[1];
3349 long tms_stime, tms_utime;
3351 if ( child==1)return get_ctime();
3353 if ( ticks==0)ticks = sysconf(_SC_CLK_TCK);
3356 tms_stime=(long)time_buf->tms_stime*milli_sec_conv;
3357 tms_utime=(long)time_buf->tms_utime*milli_sec_conv;
3364 ref=(tms_stime+tms_utime);
3369 time=(tms_utime+tms_stime)-ref;
3370 return (int) ((time)/ticks);
3376 struct tms time_buf[1];
3377 long tms_cutime, tms_cstime;
3379 if ( ticks==0)ticks = sysconf(_SC_CLK_TCK);
3384 tms_cstime=(long)time_buf->tms_cstime*milli_sec_conv;
3385 tms_cutime=(long)time_buf->tms_cutime*milli_sec_conv;
3390 ref=tms_cstime+tms_cutime;
3395 time=(tms_cutime+tms_cstime)-ref;
3396 return (int)((time)/ticks);
3402 return (int)get_time();
3404 int increase_ref_time(int increase)
3406 if ( ref==0)get_time();
3408 ref-=(long)ticks*(long)increase;
3413 /*********************************************************************/
3418 /*********************************************************************/
3419 int evaluate_sys_call_io ( char *out_file, char *com, char *fonc)
3421 if ( check_file_exists (out_file))return 1;
3424 fprintf ( stderr, "\nCommand\n%s\nFailed to produce File\n%s\n", com, out_file);
3428 void HERE (char *string, ...)
3432 va_start (ap, string);
3433 fprintf ( stderr, "HERE: ");
3434 vfprintf (stderr, string, ap);
3435 fprintf ( stderr, "\n");
3439 void printf_exit (int exit_code, FILE *fp, char *string, ...)
3445 va_start (ap, string);
3446 vfprintf (fp, string, ap);
3452 int fprintf_fork (FILE *fp, char *string, ...)
3456 static char *closeF;
3467 openF=vcalloc (100, sizeof (char));
3468 sprintf (openF, "cedric1");
3469 closeF=vcalloc (100, sizeof (char));
3470 sprintf (closeF, "cedric2");
3472 //openF =vtmpnam (NULL);
3473 //closeF=vtmpnam (NULL);
3474 vfclose(vfopen (openF,"w"));
3476 while ((rename (openF,closeF))==-1);
3478 va_start (ap, string);
3479 vsprintf (buf, string, ap);
3481 fprintf ( fp, "%s", buf);
3483 rename (closeF, openF);
3487 int fprintf_fork2 (FILE *fp, char *string, ...)
3491 static struct flock fl;
3494 va_start (ap, string);
3495 vsprintf (buf, string, ap);
3498 fprintf ( fp, "%s", buf);
3503 int printf_file (char *file,char *mode, char *string,...)
3508 if (!(fp=vfopen (file, mode)))return 0;
3509 va_start (ap, string);
3510 vfprintf (fp, string, ap);
3515 int printf_system_direct (char *string, ...)
3521 va_start (ap, string);
3522 vsprintf (buf, string, ap);
3524 return safe_system (buf);
3527 int printf_system (char *string, ...)
3533 va_start (ap, string);
3534 vsprintf (buf, string, ap);
3536 return my_system (buf);
3539 int my_system_cl (int argc, char *argv[])
3544 for ( a=0, l=0; a< argc; a++)l+=(strlen(argv[a])+2);
3545 command=vcalloc (l+1, sizeof(char));
3546 for ( a=0; a< argc; a++)
3548 command=strcat (command, argv[a]);
3549 command=strcat (command, " ");
3551 a=my_system ( command);
3556 int my_system ( char *command0)
3558 static char ***unpacked_list;
3559 static int n_unpacked;
3560 static int proxy_set;
3561 static int email_set;
3562 int email=0, proxy=0, update_env=0;
3564 //Set the net and E-mail status
3565 //if ( strstr (command0, "wget"))proxy=1;
3566 //if ( strstr (command0, "curl"))proxy=1;
3570 if ( strstr (command0, "extract_from_pdb"))proxy=1;
3571 if ( strstr (command0, "tc_generic_method"))proxy=1;
3572 if ( strstr (command0, "install"))proxy=1;
3573 if ( strstr (command0, "tc_generic_method"))email=1;
3579 unpacked_list=declare_arrayN(3, sizeof (char), 3, 200,300);
3582 if ( getenv ("DEBUG_PERL"))return safe_system (command0);
3592 command1=vcalloc ( 3*strlen (command0)+1, sizeof (char));
3593 command2=vcalloc ( 100000, sizeof (char));
3594 sprintf ( command1, "%s", command0);
3596 command1=substitute (command1, "|", " | ");
3597 command1=substitute (command1, ";", " ; ");
3599 list=string2list (command1);
3600 if ( !list) return EXIT_SUCCESS;
3603 //Identify T-Coffee self threads and install threads
3604 if ( strstr (list[1], "t_coffee"))update_env=1;
3605 else if ( strstr (list[1], "install.pl"))check_internet_connection (IS_FATAL);
3608 for ( a=1; a< atoi(list[0]); a++)
3612 if ( strstr ( list[a], "unpack_"))
3614 unpack_all_perl_script (list[a]+strlen ("unpack_"));
3615 myexit (EXIT_SUCCESS);
3617 else if ((c=name_is_in_list (list[a], unpacked_list[0], n_unpacked, 100))!=-1);
3620 n_unpacked=unpack_perl_script (list[a], unpacked_list, n_unpacked);c=n_unpacked-1;
3622 //if non unpacked script check pg is installed:
3624 if ( strm (unpacked_list[2][c], "shell"))
3626 check_program_is_installed (list[a], NULL, NULL, NULL, INSTALL_OR_DIE);
3628 strcat (command2, ((c!=-1)?unpacked_list[1][c]:list[a]));
3629 strcat (command2, " ");
3635 strcat (command2, list[a]);
3636 strcat (command2, " ");
3637 if ( strm (list[a], ",") ||strm (list[a], "|")) is_command=1;
3641 free_char (list,-1);
3643 command2=substitute ( command2, "//", "/");
3645 return_val=safe_system (command2);
3647 //INTERCEPT POTENTIAL NETWORK CALLS
3649 if (return_val!=EXIT_SUCCESS && proxy==1 && proxy_set==0)
3651 if (simple_check_internet_connection (NULL))proxy_set=1;
3654 check_internet_connection (IS_NOT_FATAL);proxy_set=1;
3655 return_val=safe_system (command2);
3658 //Intercept potential Missing E-mails
3659 if (return_val!=EXIT_SUCCESS && email==1 && email_set==0)
3661 Email(INPUT, RESET);email_set=1;
3662 return_val=safe_system (command2);
3665 //update the environement that may have been modified by a thread
3667 if (update_env)get_t_coffee_environement (NULL);
3673 int safe_system (const char * com)
3680 if ((pid = fork ()) < 0)
3689 argv [2] =(char*) com;
3691 execvp ("/bin/sh", argv);
3701 if (vwaitpid (pid, &status, 0) == -1)
3704 return (EXIT_FAILURE);
3712 static int **pidtable;
3713 static int pidtable_s;
3714 pid_t **declare_pidtable ()
3717 pidtable_s=MAX_N_PID;
3719 pidtable=vcalloc (pidtable_s, sizeof (pid_t*));
3720 for (a=0; a< pidtable_s; a++)
3722 pidtable[a]=vcalloc (2, sizeof (pid_t));
3726 pid_t set_pid (pid_t p)
3730 if (!pidtable)declare_pidtable();
3732 else if ( p>=MAX_N_PID)printf_exit (EXIT_FAILURE,stderr,"ERROR PID=%d superior to MAX_N_PID=%d [FATAL]",p, MAX_N_PID);
3733 pidtable[(int)p][0]=getpid();
3734 pidtable[(int)p][1]=1;
3741 if ( attempt==1000) printf_exit (EXIT_FAILURE, stderr,"\nERROR: Could not fork processes. Run again with -multi_core=no\n");
3757 int vwait_npid (int sub, int max, int min)
3779 pid_t vwaitpid (pid_t p, int *status, int options)
3783 p=waitpid (p, status, options);
3785 if (pidtable)pidtable[(int)p][0]=pidtable[(int)p][1]=0;
3788 pid_t vwait (pid_t *p)
3794 if (pidtable)pidtable[(int)p2][0]=pidtable[(int)p2][1]=0;
3797 int kill_child_pid()
3801 if ( !pidtable)return 0;
3807 for (a=0; a<pidtable_s; a++)
3809 if (pidtable[a][1] && pidtable[a][0]==cpid )
3811 pidtable[a][1]=pidtable[a][0]=0;
3812 kill((pid_t)a, SIGTERM);n++;
3822 void unpack_all_perl_script (char *script)
3828 while ( !strm(PerlScriptName[a], "EndList"))
3830 if (script==NULL || strm (script, "all") ||strm (script, "perl_script")|| strm (script,PerlScriptName[a]))
3832 fprintf ( stderr, "\nUnpack Script %s\n", PerlScriptName[a]);
3833 fp=vfopen ( PerlScriptName[a], "w");
3834 fprintf (fp, "%s\n%s\n", PERL_HEADER,PerlScriptFile[a]);
3836 sprintf ( command, "chmod u+x %s",PerlScriptName[a] );
3837 safe_system (command);
3841 myexit (EXIT_SUCCESS);
3847 else return atoi (c);
3850 int unpack_perl_script (char *name, char ***unpacked, int n)
3854 set_file2remove_extension(".pl", SET);
3856 if ( name==NULL) unpack_all_perl_script (NULL);
3857 while ( !strm(PerlScriptName[a], "EndList") && !strm ( name, PerlScriptName[a]))
3862 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"))) ))
3864 if ( check_file_exists (name) && ! strstr (name, "/") && isexec(name) && satoi(getenv ("RUN_LOCAL_SCRIPT")))
3866 add_warning ( stderr, "\nWARNING: Local Copy of %s detected [Debug mode]. DELETE this file\n", name);
3867 sprintf ( unpacked[0][n], "%s", name);
3868 sprintf ( unpacked[1][n], "./%s", name);
3869 sprintf ( unpacked[2][n], "shell");
3871 else if ( pg_is_installed (name) &&satoi(getenv("RUN_LOCAL_SCRIPT")) )
3873 add_warning ( stderr, "\nWARNING: Running external copy of %s [Debug mode].\n", name);
3874 sprintf ( unpacked[0][n], "%s", name);
3875 sprintf ( unpacked[1][n], "%s", name);
3876 sprintf ( unpacked[2][n], "local");
3878 else if ( strstr (name, "t_coffee"))
3880 /*Cygwin pb: t_coffee cannot call itself: making a local copy for recursion*/
3884 sprintf (buf, "cp `which %s` %s; chmod u+x %s",name, b2, b2);
3886 sprintf ( unpacked[0][n], "%s", name);
3887 sprintf ( unpacked[1][n], "%s", b2);
3888 sprintf ( unpacked[2][n], "shell");
3893 sprintf ( unpacked[0][n], "%s", name);
3894 sprintf ( unpacked[1][n], "%s", name);
3895 sprintf ( unpacked[2][n], "shell");
3902 sprintf ( unpacked[0][n], "%s", name);
3903 sprintf ( unpacked[1][n], "%s", vtmpnam(NULL));
3904 sprintf ( unpacked[2][n], "unpack");
3905 fp=vfopen (unpacked[1][n], "w");
3906 fprintf (fp, "%s\n%s\n", PERL_HEADER,PerlScriptFile[a]);
3908 sprintf ( command, "chmod u+x %s", unpacked[1][n]);
3909 safe_system (command);
3911 set_file2remove_extension(".pl", UNSET);
3915 /*********************************************************************/
3920 /*********************************************************************/
3923 static int add_file2error_report ( char *type, char *format, char *name);
3926 FILE * print_command_line (FILE *fp )
3928 fprintf ( fp, "# Command Line: %s [PROGRAM:%s]\n",in_cl, PROGRAM);
3931 int check_dir_getenv ( char *string);
3932 char *get_plugins_4_tcoffee ( char *mode)
3934 static char *plugins_4_tcoffee;
3936 if ( !plugins_4_tcoffee)plugins_4_tcoffee=vcalloc ( 1000, sizeof (char));
3938 if (mode)plugins_4_tcoffee[0]='\0';
3941 if ( plugins_4_tcoffee[0])return plugins_4_tcoffee;
3942 else if ( mode) sprintf (plugins_4_tcoffee, "%s", mode);
3943 else if ( check_dir_getenv ("PLUGINS_4_TCOFFEE"))
3945 sprintf (plugins_4_tcoffee, "%s", getenv ("PLUGINS_4_TCOFFEE"));
3949 sprintf (plugins_4_tcoffee, "%s/plugins/%s/", get_dir_4_tcoffee(), get_os());
3951 //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); }
3952 return plugins_4_tcoffee;
3954 char *get_home_4_tcoffee ()
3956 static char *home_4_tcoffee;
3957 static char home[1000];
3960 if ( !home_4_tcoffee)
3961 home_4_tcoffee=vcalloc ( 1000, sizeof (char));
3963 if ( home_4_tcoffee[0])return home_4_tcoffee;
3964 else if ( check_dir_getenv ("HOME_4_TCOFFEE"))
3966 sprintf (home_4_tcoffee, "%s", getenv ("HOME_4_TCOFFEE"));
3968 else if ( check_dir_getenv ("HOME"))
3970 sprintf (home_4_tcoffee, "%s", getenv ("HOME"));
3971 sprintf (home, "%s", home_4_tcoffee);
3973 else if ( check_dir_getenv ("TMP"))
3975 sprintf (home_4_tcoffee, "%s", getenv ("TMP"));
3977 else if (check_dir_getenv ("TEMP"))
3979 sprintf (home_4_tcoffee, "%s", getenv ("TEMP"));
3983 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);
3988 if ( !strm( home, home_4_tcoffee))
3989 add_warning (stderr, "WARNING: The Home directory was set to %s", home_4_tcoffee);
3991 return home_4_tcoffee;
3998 else if ( get_int_variable ("n_core"))
4000 nproc=get_int_variable ("n_core");
4002 else if ( getenv ("NUMBER_OF_PROCESSORS_4_TCOFFEE"))
4004 nproc=atoi(getenv ("NUMBER_OF_PROCESSORS_4_TCOFFEE"));
4006 else if ( getenv ("NUMBER_OF_PROCESSORS"))
4008 nproc=atoi(getenv ("NUMBER_OF_PROCESSORS"));
4010 else if ( check_file_exists ("/proc/cpuinfo"))
4015 printf_system ("grep \"^processor\" /proc/cpuinfo | wc -l>%s", tmp);
4016 sprintf ( string, "%s", file2string (tmp));
4018 nproc=atoi (string);
4027 static char os[100];
4030 if ( os[0])return os;
4036 command=vcalloc (100, sizeof (char));
4038 sprintf ( command, "uname > %s", file);
4039 safe_system (command);
4040 s=file2string (file);
4043 if (strstr (s, "cygwin"))sprintf ( os, "windows");
4044 else if ( strstr (s, "linux"))sprintf ( os, "linux");
4045 else if ( strstr (s, "osx"))sprintf ( os, "macosx");
4046 else if ( strstr (s, "darwin"))sprintf ( os, "macosx");
4047 else sprintf (os, "%s", s);
4055 char *file_putenv (char *file)
4057 if (!file) return NULL;
4058 else if ( !check_file_exists (file))return NULL;
4063 list=file2list (file, "\n");
4066 if ( list[n][1][0]!='#')cputenv ("%s",list[n][1]);
4069 free_arrayN ((void ***)list, 3);
4074 int cputenv (char * string, ...)
4080 if (!string)return 0;
4081 file=vtmpnam (NULL);
4082 va_start (ap, string);
4083 fp=vfopen (file, "w");
4084 vfprintf (fp, string, ap);
4087 s=file2string (file);
4093 int check_dir_getenv ( char *string)
4101 if ( !p || access (p, F_OK)==-1 || access (p, W_OK)==-1 || access(p, R_OK)==-1 || access (p, X_OK)==-1)return 0;
4106 char *get_dir_4_tcoffee()
4108 static char dir_4_tcoffee[1000];
4109 if (dir_4_tcoffee[0])return dir_4_tcoffee;
4112 if ( getenv ("DIR_4_TCOFFEE"))sprintf (dir_4_tcoffee, "%s", getenv("DIR_4_TCOFFEE"));
4113 else sprintf ( dir_4_tcoffee, "%s/.t_coffee",get_home_4_tcoffee());
4114 sprintf ( DIR_4_TCOFFEE, "%s", dir_4_tcoffee);
4115 my_mkdir (dir_4_tcoffee);
4117 return dir_4_tcoffee;
4119 char *get_tmp_4_tcoffee ()
4121 static char tmp_4_tcoffee [1000];
4123 if ( tmp_4_tcoffee[0])return tmp_4_tcoffee;
4127 if ( getenv ("TMP_4_TCOFFEE"))sprintf (tmp_4_tcoffee, "%s", getenv("TMP_4_TCOFFEE"));
4130 char command [1000];
4132 if ( strm (get_os(), "windows"))
4134 sprintf ( tmp_4_tcoffee, ".TCtmp");
4138 sprintf ( tmp_4_tcoffee, "%s/tmp", get_dir_4_tcoffee());
4142 sprintf ( TMP_4_TCOFFEE, "%s", tmp_4_tcoffee);
4143 my_mkdir (tmp_4_tcoffee);
4146 return tmp_4_tcoffee;
4148 char *get_cache_4_tcoffee ()
4151 static char cache_4_tcoffee [1000];
4152 if ( cache_4_tcoffee[0])return cache_4_tcoffee;
4155 if ( getenv ("CACHE_4_TCOFFEE"))sprintf (cache_4_tcoffee, "%s", getenv("CACHE_4_TCOFFEE"));
4156 else sprintf ( cache_4_tcoffee, "%s/cache/", get_dir_4_tcoffee());
4157 sprintf ( CACHE_4_TCOFFEE, "%s", cache_4_tcoffee);
4158 my_mkdir(cache_4_tcoffee); /*Do not use mkdir: not yet initialized*/
4160 return cache_4_tcoffee;
4162 char *get_mcoffee_4_tcoffee ()
4164 static char mcoffee_4_tcoffee [1000];
4165 if ( mcoffee_4_tcoffee[0])return mcoffee_4_tcoffee;
4168 if ( getenv ("MCOFFEE_4_TCOFFEE"))sprintf (mcoffee_4_tcoffee, "%s", getenv("MCOFFEE_4_TCOFFEE"));
4169 else sprintf ( mcoffee_4_tcoffee, "%s/mcoffee/", get_dir_4_tcoffee());
4170 my_mkdir (mcoffee_4_tcoffee);
4172 return mcoffee_4_tcoffee;
4174 char *get_methods_4_tcoffee ()
4176 static char methods_4_tcoffee [1000];
4177 if ( methods_4_tcoffee[0])return methods_4_tcoffee;
4180 if ( getenv ("METHODS_4_TCOFFEE"))sprintf (methods_4_tcoffee, "%s", getenv("METHODS_4_TCOFFEE"));
4181 else sprintf ( methods_4_tcoffee, "%s/methods/", get_dir_4_tcoffee());
4182 my_mkdir(methods_4_tcoffee);
4184 return methods_4_tcoffee;
4187 char ** standard_initialisation ( char **in_argv, int *in_argc)
4189 return standard_initialisation_end (standard_initialisation_start (in_argv, in_argc), in_argc);
4194 if (!pid)pid=getpid();
4198 char ** standard_initialisation_start ( char **in_argv, int *in_argc)
4200 static int standard_initialisation_done;
4204 //Break the command line, intercept the pipe, prepare the exit
4209 standard_initialisation_done=0;
4213 else if ( standard_initialisation_done==1)
4217 else standard_initialisation_done=1;
4218 /*1 Check for the cache and tmp directories*/
4220 get_dir_4_tcoffee();
4221 get_tmp_4_tcoffee();
4223 for (c=0,a=0; a<in_argc[0]; a++)c+=strlen (in_argv[a])+3;
4224 in_cl=vcalloc ( c, sizeof (char));
4225 for (a=0; a<in_argc[0]; a++)
4227 strcat ( in_cl, in_argv[a]);
4228 strcat (in_cl, " ");
4232 global_exit_signal=EXIT_SUCCESS;
4234 atexit (output_warning_list);
4235 signal (SIGTERM, main_exit);
4236 signal (SIGINT, main_exit);
4238 signal (SIGABRT ,error_exit);
4239 signal (SIGFPE ,error_exit);
4240 signal (SIGILL ,error_exit);
4241 signal (SIGSEGV ,error_exit);
4244 /*Process the parameters*/
4246 program_name=vcalloc ( strlen (in_argv[0])+strlen (PROGRAM)+1, sizeof (char));
4249 sprintf ( program_name, "%s", in_argv[0]);
4250 out_argv=break_list ( in_argv, in_argc, "=;, \n");
4254 sprintf ( program_name, "%s",PROGRAM);
4257 if ( name_is_in_list ( "-no_error_report", out_argv, in_argc[0], 100)!=-1)
4260 /*Error Log File: contains every input file*/
4261 error_file=vtmpnam(NULL);
4262 fp=vfopen (error_file, "w");
4263 fprintf ( fp, "\n######### RUN_REPORT START ######");
4264 fprintf ( fp, "\n######### PROGRAM_VERSION START ######");
4265 fprintf ( fp, "\n %s, %s", PROGRAM, VERSION);
4266 fprintf ( fp, "\n######### PROGRAM_VERSION END ######");
4268 fprintf ( fp, "\n######### COMMAND_LINE START ######");
4269 fprintf ( fp, "\n");
4270 for (a=0; a<in_argc[0]; a++)fprintf (fp, "%s ", out_argv[a]);
4271 fprintf ( fp, "\n######### COMMAND_LINE END ######\n");
4273 for ( a=1; a<in_argc[0]; a++)
4275 if ( check_file_exists ( out_argv[a]) && !filename_is_special (out_argv[a]))
4278 fprintf ( fp, "\n######### FILE: %s START ######\n", out_argv[a]);
4279 fprintf ( fp, "\n");
4280 fp2=vfopen ( out_argv[a], "r");
4281 while ((c=fgetc(fp2))!=EOF)fprintf (fp, "%c", c);
4283 fprintf ( fp, "\n######### FILE: %s END ######\n", out_argv[1]);
4286 fprintf ( fp, "\n######### RUN_REPORT END ######\n");
4289 /*Initialise the time*/
4293 for (a=1, stdi=0; a<in_argc[0]; a++)
4295 if ( (strm ( out_argv[a], "stdin") || strm (out_argv[a], "STDIN")) && stdi==0)
4302 file=vtmpnam (NULL);
4303 vfree (out_argv[a]);
4304 out_argv[a]=vcalloc ( strlen (file)+1, sizeof (char));
4305 sprintf (out_argv[a], "%s", file);
4306 fp_stdi=vfopen ("stdin", "r");
4307 fp=vfopen (file, "w");
4308 while ( (c=fgetc (fp_stdi))!=EOF)fprintf (fp, "%c", c);
4315 printf_exit ( EXIT_FAILURE,stderr, "ERROR: Attempt to get more than one file via pipe [FATAL:%s]\n", PROGRAM);
4322 char ** standard_initialisation_end ( char **in_argv, int *in_argc)
4324 //All the initialization that
4325 static int standard_initialisation_end_done;
4326 //Break the command line, intercept the pipe, prepare the exit
4329 standard_initialisation_end_done=0;
4332 else if ( standard_initialisation_end_done==1)
4336 else standard_initialisation_end_done=1;
4338 //set the environement variables (proxy and E-mail if possible)
4339 get_t_coffee_environement (NULL);
4342 //clean the cache if needed
4343 //printf_system ("clean_cache.pl -dir=%s/cache -size=%d",get_dir_4_tcoffee (), CACHE_MAX_SIZE);
4348 int add_file2error_report ( char *type, char *format, char *name)
4355 fp1=vfopen ( error_file, "a");
4356 fp2=vfopen (name, "r");
4357 fprintf ( fp1, "######### OUTFILE: %s START ######\n", name);
4358 while ( (c=fgetc(fp2))!=EOF)fprintf (fp1, "%c", c);
4359 c=ungetc(c,fp2);c=fgetc(fp2); if ( c!='\n')fprintf (fp1, "\n");
4360 fprintf ( fp1, "######### OUTFILE: %s END ######\n", name);
4361 vfclose ( fp1);vfclose(fp2);
4367 void myexit (int signal)
4369 global_exit_signal=signal;
4370 if ( signal == EXIT_FAILURE)
4372 print_exit_failure_message();
4374 else if ( signal==EXIT_SUCCESS)
4376 /*print_exit_success_message();*/;
4378 exit (global_exit_signal);
4382 static int n_warning;
4383 static char **warning_list;
4385 FILE *fatal_exit (FILE *fp,int exit_mode, char *string, ...)
4388 va_start (ap, string);
4389 vfprintf (fp, string, ap);
4394 static int warning_mode;
4395 int set_warning_mode ( int mode)
4400 FILE *add_warning (FILE *fp, char *string, ...)
4403 static int max_n_warning=100;
4407 if ( warning_mode==NO || getenv("NO_WARNING_4_TCOFFEE"))return fp;
4410 if ( !warning_list)warning_list=vcalloc (max_n_warning, sizeof (char*));
4411 else if ( n_warning>=max_n_warning)
4414 warning_list=vrealloc ( warning_list,sizeof (char*)*max_n_warning);
4417 va_start (ap, string);
4420 fprintf ( fp, "\n");
4421 vfprintf (fp, string, ap);
4425 va_start (ap, string);
4426 vsprintf (buf, string, ap);
4429 warning_list[n_warning]=vcalloc (strlen (buf)+1, sizeof (char));
4430 sprintf ( warning_list[n_warning], "%s", buf);
4435 void output_warning_list()
4438 if ( n_warning==0){;}
4441 fprintf ( stderr, "\nWARNING RECAPITULATION: %d Warning%c [PROGRAM: %s]\n", n_warning, (n_warning>1)?'s':' ', PROGRAM);
4442 for (a=0; a< n_warning; a++)
4444 fprintf (stderr, "**WARNING: %3d** %s\n",a+1, warning_list[a]);
4450 int count_n_res_in_array (char *array, int len)
4452 return count_n_symbol_in_array(array, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", len);
4454 int count_n_gap_in_array (char *array, int len)
4457 if ( len<=0 ||len>strlen(array) )l=strlen(array);
4460 return l- count_n_res_in_array (array,len);
4462 int count_n_symbol_in_array ( char *array, char *array_list, int len)
4467 if ( len<=0 ||len>strlen(array) )l=strlen(array);
4470 for ( a=0; a< l; a++)t+=is_in_set (array[a], array_list);
4474 char* count_strings_in_file ( char *in, char *out)
4478 char **list, **result;
4481 if (!out) out=vtmpnam (NULL);
4482 list=declare_char (count_n_line_in_file(in)+1, measure_longest_line_in_file (in)+1);
4485 fp=vfopen (in, "r");
4486 while ((c=fgetc (fp))!=EOF)
4489 fscanf (fp, "%s\n",list[n]);
4494 result=count_strings (list, n);
4498 fp=vfopen (out, "w");
4499 while (result[n])fprintf ( fp,"%s\n", result[n++]);
4502 free_char (list, -1);
4503 free_char (result, -1);
4508 int ** count_int_strings (int **array, int len, int s)
4513 sort_list_int (array,s, s,0, len-1);
4514 result=vcalloc (len, sizeof (int*));
4515 for (n=-1,a=0; a<len; a++)
4517 if (n==-1 || memcmp (array[a], result[n], sizeof (int)*s)!=0)
4520 result[n]=vcalloc (s+1, sizeof (int));
4521 memcpy (result[n],array[a], sizeof (int)*s);
4522 fprintf ( stdout, "*");
4526 sort_int (result, s+1, s, 0, n);
4527 result=vrealloc ( result, sizeof (int*)*n);
4531 char** count_strings ( char **array, int len)
4537 result=vcalloc (len+1, sizeof (char*));
4538 array=sort_string_array (array, len);
4540 for (c=0, a=0, n=0, cs=NULL; a< len; a++)
4542 if (cs == NULL || !strm (cs, array[a]))
4546 result[c]=vcalloc (strlen (cs)+20, sizeof (char));
4547 sprintf ( result[c], "%s %d", cs, n);
4557 result[c]=vcalloc (strlen (cs)+20, sizeof (char));
4558 sprintf ( result[c], "%s %d", cs, n);
4563 int get_first_non_white_char (char *name)
4565 if ( !name) return 0;
4566 else if ( !check_file_exists (name))return 0;
4572 fp=vfopen (name, "r");
4573 while (isspace ((c=fgetc (fp)))&& c!=EOF);
4578 int count_n_char_x_in_file(char *name, char x)
4584 fp=vfopen(name, "r");
4585 while ( (c=fgetc(fp))!=EOF)n+=(c==x);
4589 int count_n_char_in_file(char *name)
4595 fp=vfopen(name, "r");
4596 while ( (c=fgetc(fp))!=EOF){n++;}
4600 int count_n_line_in_file ( char *name )
4606 fp=vfopen(name, "r");
4607 while ( (c=fgetc(fp))!=EOF)n+=(c=='\n');
4611 int measure_longest_line_in_file ( char *name )
4619 fp=vfopen(name, "r");
4620 while ( (c=fgetc(fp))!=EOF)
4622 if ( c=='\n'){longest=MAX(longest, current+1);current=0;}
4625 longest=MAX(longest, current+1);
4636 string= vcalloc ( 500, sizeof ( char));
4639 while ( ( ch=getchar())!='\n' && a<500)
4643 if ( string[0]=='\0')
4656 int string_variable_isset (char *name)
4658 if (store_string_variable (name,NULL, ISSET)==NULL)return 0;
4661 char* set_string_variable (char *name, char * v)
4663 return store_string_variable (name, v, SET);
4665 char * get_string_variable (char *name)
4667 return store_string_variable (name,NULL, GET);
4669 char* unset_string_variable (char *name)
4671 return store_string_variable (name,0, UNSET);
4674 char* store_string_variable (char *name, char* v, int mode)
4676 static char **name_array, **val_array;
4685 if ( strm (name,name_array[a]))
4689 val_array[a]=vrealloc (val_array[a], strlen (v)+1);
4690 sprintf (val_array[a],"%s",v);
4692 else val_array[a]='\0';
4698 name_array=vcalloc (1, sizeof (char*));
4699 val_array=vcalloc (1, sizeof (char*));
4703 name_array=vrealloc (name_array, (n+1)*sizeof (char*));
4704 val_array=vrealloc (val_array, (n+1)*sizeof (char*));
4706 name_array[n]=vcalloc ( strlen (name)+1, sizeof (char));
4707 val_array[n]=vcalloc ( strlen (v)+1, sizeof (char));
4709 sprintf ( name_array[n], "%s", name);
4710 sprintf ( val_array[n], "%s", v);
4716 else if ( mode == ISSET)
4719 if ( strm (name_array[a], name))return (char *)1;
4721 else if ( mode == UNSET)
4724 if ( strm (name_array[a], name))
4726 name_array[a][0]='\0';
4727 val_array[a][0]='\0';
4730 add_warning (stdout, "WARNING: Could not UNSET the value of %s. You must SET the value before it is used", name);
4736 if ( strm (name_array[a], name))
4737 return val_array[a];
4742 int int_variable_isset (char *name)
4744 return store_int_variable (name,0, ISSET);
4746 int set_int_variable (char *name, int v)
4748 return store_int_variable (name, v, SET);
4750 int get_int_variable (char *name)
4752 return store_int_variable (name, 0, GET);
4754 int unset_int_variable (char *name)
4756 return store_int_variable (name,0, UNSET);
4759 int store_int_variable (char *name, int v, int mode)
4761 static char **name_array;
4762 static int *val_array;
4770 if ( strm (name,name_array[a]))
4778 name_array=vcalloc (1, sizeof (char*));
4779 val_array=vcalloc (1, sizeof (int));
4783 name_array=vrealloc (name_array, (n+1)*sizeof (char*));
4784 val_array=vrealloc (val_array, (n+1)*sizeof (int));
4786 name_array[n]=vcalloc ( strlen (name)+1, sizeof (char));
4787 sprintf ( name_array[n], "%s", name);
4794 else if ( mode == ISSET)
4797 if ( strm (name_array[a], name))return 1;
4799 else if ( mode == UNSET)
4802 if ( strm (name_array[a], name))
4804 name_array[a][0]='\0';
4808 add_warning (stdout, "WARNING: Could not UNSET the value of %s. You must SET the value before it is used", name);
4814 if ( strm (name_array[a], name))
4815 return val_array[a];
4821 char *add2t_coffee_environement (char *env, ...)
4828 FILE *in, *out, *fp;
4829 static char *buf,*value1, *value2;
4833 buf=vcalloc (LONG_STRING, sizeof (char));
4834 value1=vcalloc (LONG_STRING, sizeof (char));
4835 value2=vcalloc (LONG_STRING, sizeof (char));
4837 file=vtmpnam (NULL);
4838 tmp_env_file=vtmpnam (NULL);
4841 fp=vfopen (file, "w");
4842 vfprintf (fp,env, ap);
4845 s=file2string (file);
4846 sprintf (value1, "%s", s);
4847 substitute_char (value1, '=', '\0');
4849 //add the new environement variable to the env file
4850 if (!get_string_variable ("t_coffee_env"))get_t_coffee_environement (NULL);
4851 env_file=get_string_variable ("t_coffee_env");
4853 out=vfopen(tmp_env_file, "w");
4854 if ( check_file_exists (env_file))
4856 in=vfopen (env_file, "r");
4858 while ( (fgets (buf,LONG_STRING, in))!=NULL)
4860 sprintf (value2, "%s", buf);
4861 substitute_char (value2, '=', '\0');
4862 if ( !strm (value1, value2))fprintf ( out, "%s",buf);
4867 fprintf ( out, "%s\n", s);
4870 if ( check_file_exists (tmp_env_file))printf_system ("cp %s %s", tmp_env_file, env_file);
4871 // fp=vfopen (get_string_variable ("t_coffee_env"), "a");
4872 //fprintf ( fp, "%s\n",s);
4875 //add the variable to T-Coffee
4879 char *get_t_coffee_environement (char *file)
4885 else if ((file=getenv ("ENV_4_TCOFFEE")));
4886 else if (get_dir_4_tcoffee())
4888 file=vcalloc ( 100, sizeof (char));
4889 sprintf (file, "%s/t_coffee_env", get_dir_4_tcoffee());
4893 file=vcalloc ( 100, sizeof (char));
4894 sprintf (file, "%s/t_coffee_env", getenv ("HOME"));
4896 set_string_variable ("t_coffee_env", file);
4898 def_env=vtmpnam (NULL);
4899 printf_system ("env > %s", def_env);
4900 file_putenv (def_env);
4902 if (!getenv ("DIR_4_TCOFFEE")) cputenv("DIR_4_TCOFFEE=%s", get_dir_4_tcoffee());
4903 if (!getenv ("TMP_4_TCOFFEE")) cputenv("TMP_4_TCOFFEE=%s", get_tmp_4_tcoffee());
4904 if (!getenv ("CACHE_4_TCOFFEE")) cputenv("CACHE_4_TCOFFEE=%s", get_cache_4_tcoffee());
4906 set_path_4_plugins (NULL);
4907 get_email_from_env();
4908 get_proxy_from_env();
4910 char * set_path_4_plugins (char *plugins)
4912 if (plugins)get_plugins_4_tcoffee (plugins);
4914 //set various important variables
4915 if (!getenv ("MAFFT_BINARIES"))cputenv ("MAFFT_BINARIES=%s", get_plugins_4_tcoffee(NULL));
4916 if (!getenv ("PLUGINS_4_TCOFFEE")) cputenv("PLUGINS_4_TCOFFEE=%s", get_plugins_4_tcoffee(NULL));
4918 if (!getenv ("MCOFFEE_4_TCOFFEE")) cputenv("MCOFFEE_4_TCOFFEE=%s", get_mcoffee_4_tcoffee());
4919 if (!getenv ("METHODS_4_TCOFFEE")) cputenv("METHODS_4_TCOFFEE=%s", get_methods_4_tcoffee());
4920 cputenv ("PATH=%s:%s", get_plugins_4_tcoffee(NULL), getenv ("PATH"));
4921 //set various packages
4922 add_package2_tcoffee_env ("CLUSTALW_4_TCOFFEE");
4923 add_package2_tcoffee_env ("CLUSTALW2_4_TCOFFEE");
4924 add_package2_tcoffee_env ("FUGUE_4_TCOFFEE");
4925 add_package2_tcoffee_env ("CLUSTALW2_4_TCOFFEE");
4926 add_package2_tcoffee_env ("TMALIGN_4_TCOFFEE");
4927 add_package2_tcoffee_env ("SAP_4_TCOFFEE");
4928 add_package2_tcoffee_env ("DALILITEc_4_TCOFFEE");
4929 add_package2_tcoffee_env ("MUSTANG_4_TCOFFEE");
4930 add_package2_tcoffee_env ("NCBIBLAST_4_TCOFFEE");
4931 add_package2_tcoffee_env ("MAFFT_4_TCOFFEE");
4932 add_package2_tcoffee_env ("DIALIGNTX_4_TCOFFEE");
4933 add_package2_tcoffee_env ("POA_4_TCOFFEE");
4934 add_package2_tcoffee_env ("PROBCONS_4_TCOFFEE");
4935 add_package2_tcoffee_env ("PROBCONSRNA_4_TCOFFEE");
4936 add_package2_tcoffee_env ("TCOFFEE_4_TCOFFEE");
4937 add_package2_tcoffee_env ("PCMA_4_TCOFFEE");
4938 add_package2_tcoffee_env ("KALIGN_4_TCOFFEE");
4939 add_package2_tcoffee_env ("AMAP_4_TCOFFEE");
4940 add_package2_tcoffee_env ("PRODA_4_TCOFFEE");
4941 add_package2_tcoffee_env ("PRANK_4_TCOFFEE");
4942 add_package2_tcoffee_env ("CONSAN_4_TCOFFEE");
4943 add_package2_tcoffee_env ("RNAPlfold_4_TCOFFEE");
4944 add_package2_tcoffee_env ("HMMtop_4_TCOFFEE");
4945 add_package2_tcoffee_env ("GOR4_4_TCOFFEE");
4949 int add_package2_tcoffee_env (char *package)
4953 if ((v1=getenv (package))==NULL)return 0;
4955 v2=filename2path (v1);
4956 if (!v2){vfree (v1);return 0;}
4957 cputenv ("PATH=%s:%s",v2, getenv ("PATH"));
4958 vfree (v1); vfree (v2);
4963 char * Proxy( int input_mode, int write_mode)
4968 if ( write_mode==RESET){set=0;vfree (proxy); proxy=NULL;return Proxy(input_mode, SET);}
4971 else if ( input_mode ==INPUT){proxy=get_proxy();}
4972 else if ( input_mode ==ENV ){proxy=get_proxy_from_env (); }
4973 else printf_exit (EXIT_FAILURE,stderr, "Unknown mode for Proxy [FATAL:%s(%s)]", PROGRAM, VERSION);
4976 if (proxy)set_proxy (proxy);
4977 else proxy=vcalloc (1, sizeof (char));
4981 char * get_proxy_from_env ()
4985 if ((proxy=get_string_variable ("cl_proxy"))){;}//Command line proxy always wins
4986 else if ((proxy=getenv ("http_proxy_4_TCOFFEE")));
4987 else if ((proxy=get_string_variable ("proxy")));//use default T-Coffee proxy
4988 else if ( getenv ("HTTP_proxy") && getenv ("http_proxy")){return getenv ("HTTP_proxy");}//get environement proxy
4989 else if ((proxy=getenv ("HTTP_proxy")));//id
4990 else if ((proxy=getenv ("http_proxy")));//id
4991 else if ((proxy=getenv ("HTTP_PROXY")));//id
4992 else if ((proxy=getenv ("ALL_proxy")));//id
4993 else if ((proxy=getenv ("all_proxy")));//id
4994 else if ((proxy=getenv ("ALL_PROXY")));//id
4996 if (proxy)set_proxy(proxy);
5004 if ( (proxy=get_proxy_from_env()) && simple_check_internet_connection(NULL));
5005 else //read in the proxy
5013 int set_proxy (char *proxy)
5016 if (!proxy) return 0;
5018 cputenv ("HTTP_proxy_4_TCOFFEE=%s", proxy);
5019 cputenv ("HTTP_proxy=%s", proxy);
5020 cputenv ("http_proxy=%s", proxy);
5021 cputenv ("HTTP_PROXY=%s", proxy);
5022 cputenv ("ALL_proxy=%s", proxy);
5023 cputenv ("ALL_PROXY=%s", proxy);
5024 cputenv ("all_proxy=%s", proxy);
5028 char *proxy_message ()
5030 fprintf ( stderr, "\n\n");
5031 fprintf ( stderr, "*************************************************************************************************\n");
5032 fprintf ( stderr, "* IMPORTANT: Please Read Carefuly *\n");
5033 fprintf ( stderr, "* *\n");
5034 fprintf ( stderr, "* ------------------------------FIRST RUN CONFIGURATION---------------------------------------- *\n");
5035 fprintf ( stderr, "* *\n");
5036 fprintf ( stderr, "* *\n");
5037 fprintf ( stderr, "* If you are behind a firewall, you must enter your proxy address to use webservices *\n");
5038 fprintf ( stderr, "* This address is usualy something like: http://some.place.here:8080 *\n");
5039 fprintf ( stderr, "* *\n");
5040 fprintf ( stderr, "* The proxy you will provide can be changed anytime by editing the file: *\n");
5041 fprintf ( stderr, "* %s/t_coffee_env \n",get_dir_4_tcoffee());
5042 fprintf ( stderr, "*************************************************************************************************\n");
5045 char *input_proxy ()
5052 if ( ntries==MAX_N_TRIES)
5053 printf_exit (EXIT_FAILURE,stderr, "\nERROR: Could not use provided proxy. Provide proxy via the -email flag [FATAL:%s]\n", PROGRAM);
5055 fprintf ( stderr, "\n ### Tip: If you work from home (ADSL) you probably do no need a proxy. ");
5056 fprintf ( stderr, "\n ### Tip: The proxy is the address you may have had to enter in your navigator. ");
5058 fprintf ( stderr, "\n ### Proxy Input, Enter your proxy and type Return (Type Return if you do not need a proxy): ");
5063 proxy=vcalloc ( 100, sizeof (char));
5064 sprintf ( proxy, "");
5066 else if (!strstr (proxy, "."))return input_proxy();
5071 while ((c=proxy[a])!='\0')
5073 if (!isalnum(c) && c!='.' && c!='_' )return input_proxy();
5077 fprintf ( stderr, "\n\tYou have entered:[%s]%s \nIs this correct ([Y] or N)?: ",proxy, (strm (proxy, "")?"(No Proxy Needed)":""));
5078 answer=input_name();
5080 if ( !answer || answer[0]=='y' ||answer[0]=='Y')
5087 return input_proxy ();
5089 add2t_coffee_environement ("http_proxy_4_TCOFFEE=%s", proxy);
5090 set_string_variable ("proxy", proxy);
5096 char *input_email ()
5104 if ( ntries==MAX_N_TRIES)
5105 printf_exit (EXIT_FAILURE, stderr,"\nERROR: Could not use provided Email. Provide Email via the -email flag [FATAL:%s]\n", PROGRAM);
5109 fprintf ( stderr, "\n ### EMAIL Input, Enter your email and type Return: ");
5111 fprintf ( stderr, "\n\tYou have entered:%s \nIs this correct ([Y] or N)?: ", email);
5112 answer=input_name();
5114 if ( !answer || answer[0]=='y' ||answer[0]=='Y')
5121 return input_email ();
5123 add2t_coffee_environement("EMAIL_4_TCOFFEE=%s", email);
5124 set_string_variable ("email", email);
5127 static char *ebi_email_message ();
5128 char *ebi_email_message ()
5130 fprintf ( stderr, "\n\n");
5131 fprintf ( stderr, "*************************************************************************************************\n");
5132 fprintf ( stderr, "* IMPORTANT: Please Read Carefuly *\n");
5133 fprintf ( stderr, "* *\n");
5134 fprintf ( stderr, "* ------------------------------FIRST RUN CONFIGURATION---------------------------------------- *\n");
5135 fprintf ( stderr, "* *\n");
5136 fprintf ( stderr, "* Some commands of T-Coffee use the EBI BLAST webservices. The EBI requires a valid E-mail *\n");
5137 fprintf ( stderr, "* address for this service to be used (check: www.ebi.ac.uk/Tools/webservices/). *\n");
5138 fprintf ( stderr, "* T-Coffee will keep it for further run but only to be used with the EBI webservices. *\n");
5139 fprintf ( stderr, "* *\n");
5140 fprintf ( stderr, "* !!!!!!!!!!!!!!!!!! Your Email will not be sent to us, ONLY to the EBI !!!!!!!!!!!!!!!!!!!!!!!!*\n");
5141 fprintf ( stderr, "* *\n");
5142 fprintf ( stderr, "* -blast_server=EBI is the default mode of T-Coffee. If you do NOT want to provide your E-mail *\n");
5143 fprintf ( stderr, "* you can use: *\n");
5144 fprintf ( stderr, "* -blast_server=NCBI (NCBI netblast) *\n");
5145 fprintf ( stderr, "* -blast_server=LOCAL Local NCBI BLAST *\n");
5146 fprintf ( stderr, "* *\n");
5147 fprintf ( stderr, "*The address you provide can be changed anytime by editing the file: *\n");
5148 fprintf ( stderr, "* %s/t_coffee_env \n",get_dir_4_tcoffee());
5149 fprintf ( stderr, "*************************************************************************************************\n");
5152 char *Email( int input_mode, int write_mode)
5157 if ( write_mode==RESET){set=0;vfree (email); email=NULL;return Email(input_mode, SET);}
5160 else if (input_mode ==INPUT){email=get_email();}
5161 else if (input_mode ==ENV ){email=get_email_from_env (); }
5162 else printf_exit (EXIT_FAILURE, stderr,"Unknown mode for Email [FATAL:%s]",PROGRAM);
5166 if ( email)set_email (email);
5167 else email=vcalloc (1, sizeof (char));
5170 char *get_email_from_env ()
5174 if ( (email=get_string_variable ("cl_email")));
5175 else if ( (email=get_string_variable ("email")));
5176 else if ( (email=getenv ("EMAIL_4_TCOFFEE")));
5177 else if ( (email=getenv ("EMAIL")));
5185 if ( (email=get_email_from_env ()));
5188 ebi_email_message();
5189 email=input_email();
5192 if (!strstr (email, "@"))
5194 ebi_email_message();
5195 email=input_email();
5201 int set_email (char *email)
5203 if (!email) return 0;
5205 cputenv ("EMAIL_4_TCOFFEE=%s", email);
5206 cputenv ("EMAIL=%s",email);
5209 char *chomp (char *name)
5212 while ( name[a]!='\n' && name[a]!='\0')a++;
5216 static Tmpname *tmpname;
5217 static Tmpname *ntmpname;
5219 static int n_tmpname;
5220 static int file2remove_flag;
5222 char *set_file2remove_extension (char *extension, int mode)
5224 static char ext[100];
5225 if (mode==SET)sprintf (ext, "%s", extension);
5226 else if ( mode==UNSET) ext[0]='\0';
5227 else if ( mode==GET);
5230 int flag_file2remove_is_on ()
5232 return file2remove_flag;
5234 void set_file2remove_on()
5238 void set_file2remove_off()
5243 char *add2file2remove_list (char *name)
5247 if ( !tmpname || !name)ntmpname=tmpname=vcalloc ( 1, sizeof (Tmpname));
5248 else if (!ntmpname->name);
5249 else ntmpname=ntmpname->next=vcalloc ( 1, sizeof (Tmpname));
5251 if (!name) return NULL;
5253 ntmpname->name=vcalloc(strlen(name)+1, sizeof (char));
5255 sprintf (ntmpname->name, "%s", name);
5256 return ntmpname->name;
5258 //char *short_tmpnam_2(char *s);//used to generate very compact tmp names
5259 void initiate_vtmpnam (char *file)
5261 add2file2remove_list (NULL);
5264 char *vtmpnam ( char *s1)
5270 standard_initialisation(NULL, NULL);
5272 s=vcalloc ( VERY_LONG_STRING, sizeof (char));
5277 s2=add2file2remove_list (s);
5278 if (s!=s2)vfree (s);
5279 if (s1){sprintf (s1, "%s",s2);return s1;}
5285 int get_vtmpnam2_root()
5287 int MAX_TMPNAM_ROOT=10000;
5294 v=rand()%MAX_TMPNAM_ROOT;
5298 char *tmpnam_2 (char *s)
5302 char buf[VERY_LONG_STRING];
5303 static char root2[VERY_LONG_STRING];
5304 static char *tmpdir;
5305 static int name_size;
5309 char *vtmpnam_prefixe;
5311 name_size=MAX( 2*L_tmpnam, MAXNAMES*2)+1;
5312 root=get_vtmpnam2_root();
5313 sprintf ( root2, "%d%d_", root, (int)getpid());
5315 vtmpnam_prefixe=vcalloc (strlen (root2)+strlen (get_tmp_4_tcoffee())+2, sizeof (char));
5316 sprintf (vtmpnam_prefixe, "%s/%s", get_tmp_4_tcoffee(), root2);
5317 set_string_variable ("vtmpnam_prefixe1", vtmpnam_prefixe);
5318 set_string_variable ("vtmpnam_prefixe2", root2);
5319 vfree (vtmpnam_prefixe);
5323 tmpdir=get_tmp_4_tcoffee();
5325 sprintf (buf, "%s/%s%d_TCtmp%s",tmpdir,root2, file++,set_file2remove_extension (NULL, GET));
5326 if ( strlen(buf)>=name_size)s=vrealloc (s,(strlen(buf)+1)*sizeof (char));
5327 sprintf (s, "%s", buf);
5330 char *short_tmpnam_2(char *s)
5334 char buf[VERY_LONG_STRING];
5335 static char root2[VERY_LONG_STRING];
5336 static char *tmpdir;
5337 static int name_size;
5341 char *vtmpnam_prefixe;
5343 name_size=MAX( 2*L_tmpnam, MAXNAMES*2)+1;
5344 root=get_vtmpnam2_root();
5345 sprintf ( root2, "%d%d", root,getpid());
5347 vtmpnam_prefixe=vcalloc (strlen (root2)+strlen (get_tmp_4_tcoffee())+2, sizeof (char));
5348 sprintf (vtmpnam_prefixe, "%s", root2);
5349 set_string_variable ("vtmpnam_prefixe1", vtmpnam_prefixe);
5350 set_string_variable ("vtmpnam_prefixe2", root2);
5351 vfree (vtmpnam_prefixe);
5353 if (!s) return NULL;
5355 sprintf (buf, "%s%d%s",root2, file++,set_file2remove_extension (NULL, GET));
5356 if ( strlen(buf)>=name_size)s=vrealloc (s,(strlen(buf)+1)*sizeof (char));
5357 sprintf (s, "%s", buf);
5361 char *vremove2 (char *s)
5364 char list_file[1000];
5369 //Remove filenames with a wildcard
5371 sprintf (list_file, "list_file_%d", (int)getpid());
5372 sprintf (command, "ls -1 %s>%s 2>/dev/null", s, list_file);
5373 safe_system (command);
5375 list=file2list (list_file, " ");
5378 while (list && list[a])
5380 if ( check_file_exists (list[a][1]))
5382 vremove (list[a][1]);
5386 vremove (list_file);
5389 char *vremove (char *s)
5393 if ( s && strstr (s, "*"))return vremove2(s);
5394 else if ( !s || !check_file_exists(s) ) return NULL;
5395 else if ( isdir (s))
5407 int log_function ( char *fname)
5411 if ( check_file_exists (error_file))
5414 sprintf ( command, "cp %s %s", error_file, fname);
5415 my_system ( command);
5416 fprintf( stderr,"\n\t******************************************************************");
5417 fprintf( stderr, "\n\t* Full Log of [%s, %s] in File [%s]",PROGRAM, VERSION, fname);
5418 fprintf( stderr, "\n\t******************************************************************\n");
5426 myexit (global_exit_signal);
5431 char final_report[1000];
5433 if ( no_error_report)return;
5434 if ( check_file_exists (error_file))
5436 sprintf ( final_report, "error_report.%s",PROGRAM);
5437 if ( !getenv ("NO_ERROR_REPORT_4_TCOFFEE"))sprintf ( command, "cp %s %s", error_file, final_report);
5438 my_system ( command);
5439 fprintf( stderr,"\n\t******************************************************************");
5440 fprintf( stderr, "\n\t* Job NOT Completed:[%s, %s]",PROGRAM, VERSION);
5441 fprintf( stderr, "\n\t* Please CHECK: ");
5442 fprintf( stderr, "\n\t* \t-1 The format of your Input Files ");
5443 fprintf( stderr, "\n\t* \t-2 The parameters ");
5444 fprintf( stderr, "\n\t* \t-3 The use of special characters in sequence names:");
5445 fprintf( stderr, "\n\t* \t\t (@, |, %%...)");
5447 fprintf( stderr, "\n\t* \t-4 The Online Doc (%s) ", URL);
5448 fprintf( stderr, "\n\t* \t-5 Send the file %s to:", final_report);
5449 fprintf( stderr, "\n\t* \t\t%s",EMAIL);
5451 fprintf( stderr, "\n\t* If you run T-Coffee over the WEB:");
5452 fprintf( stderr, "\n\t* \tWindows Cut and Paste is sometimes erratic and");
5453 fprintf( stderr, "\n\t* \tit can loose carriage returns. If you suspect this,");
5454 fprintf( stderr, "\n\t* \ttry to cut and paste through an intermediate application");
5455 fprintf( stderr, "\n\t* \t(word pad) and inspect the results\n\n");
5456 fprintf( stderr, "\n\t* CONFIDENTIALITY:");
5457 fprintf( stderr, "\n\t* \tThe File %s may contain your personnal DATA", final_report);
5458 fprintf( stderr, "\n\t* \tRemove ALL confidential DATA from this file BEFORE sending it");
5459 fprintf( stderr, "\n\t******************************************************************\n");
5460 print_command_line(stderr);
5462 print_exit_failure_message ();
5463 myexit (global_exit_signal=EXIT_FAILURE);
5469 exit (global_exit_signal);
5473 void clean_function ()
5482 if ( done==1) return;
5486 add_method_output2method_log (NULL, NULL, NULL, NULL, decode_name (NULL, CODELIST));
5488 //Kill all child processes (if any)
5491 if (getenv ("DEBUG_TMP_FILE") && atoi (getenv("DEBUG_TMP_FILE"))==1)
5493 fprintf ( stderr, "\n[DEBUG_TMP_FILE:%s] TEMPORARY FILES HAVE NOT Been Removed:", PROGRAM);
5496 if ( getenv("PRINT_TMPFILE_NAME") && atoi(getenv("PRINT_TMPFILE_NAME"))==1)
5498 fprintf ( stderr, "\n\t%s [EXISTS:%s]", tmpname->name, (check_file_exists(tmpname->name))?"YES":"NO");
5506 fprintf ( stderr, "\n");
5514 //printf_system ("clean_cache.pl -dir=%s -size=0 -age=10 -force",get_tmp_4_tcoffee ());
5515 //Remove all the temporary files generated during THIS run
5519 remove (start->name);
5520 if (isdir(start->name))rrmdir (start->name);
5524 vfree(b->name);vfree(b);
5527 //Remove potential log
5528 //remove ( TO_NULL_DEVICE);
5529 //Remove all the tmp/tmp* files generated by this process [potentialy generated by secondary processes]
5530 //if ((x=get_string_variable("vtmpnam_prefixe1"))){ sprintf ( name, "%s*", x); vremove(name);}
5531 //Remove all the tmp* file that may have sneaked in (from clustalw for instance) and could be in the current dir
5532 //if ((x=get_string_variable("vtmpnam_prefixe2"))){ sprintf ( name, "%s*", x); vremove(name);}
5533 //remove (get_tmp_4_tcoffee ());//removes tmpdir if empty
5537 FILE *NFP;/*Null file pointer: should only be open once*/
5539 /*********************************************************************/
5541 /* CACHE_FUNCTION */
5544 /*********************************************************************/
5546 char * prepare_cache ( const char *mode)
5550 cache =vcalloc ( 10000, sizeof(char));
5552 if (strm (mode, "use"))
5554 sprintf (cache, "%s",get_cache_4_tcoffee());
5557 else if ( strm (mode, "ignore") || strm (mode, "no"))
5560 cache=vtmpnam(cache);
5561 strcat (cache, "/");
5562 sprintf ( command, "mkdir %s",cache);
5563 my_system ( command);
5565 else if ( strm (mode, "update"))
5567 cache=vtmpnam(cache);
5568 strcat (cache, "/");
5569 sprintf ( command, "mkdir %s",cache);
5570 my_system ( command);
5572 else if ( strm (mode, "local"))
5578 sprintf ( cache, "%s/",mode);
5585 char * get_cache_dir()
5587 if ( cache==NULL){cache=vcalloc (1, sizeof (char));cache[0]='\0';}
5591 void update_cache ()
5594 char old_cache[1000];
5596 sprintf ( old_cache, "%s", get_cache_dir());
5597 prepare_cache( "use");
5598 sprintf ( command, "mv %s* %s",old_cache, get_cache_dir());
5599 my_system (command);
5600 sprintf ( command, "rmdir %s",old_cache);
5601 my_system (command);
5607 if (getenv4debug ("DEBUG_TMP_FILE"))
5609 fprintf ( stderr, "\n[DEBUG_TMP_FILE:%s] TEMPORARY CACHE HAS NOT Been Removed:\n\t%s\n", PROGRAM,get_cache_dir());
5614 sprintf ( command, "rm -r %s",get_cache_dir());
5615 my_system (command);
5622 FILE * vfopen ( char *name_in, char *mode)
5626 int tolerate_mistake;
5632 static char *stdin_file;
5635 if ( !name_in)return NULL;
5636 if (!name){name=vcalloc (1000, sizeof (char));}
5637 if (!name2){name2=vcalloc (1000, sizeof (char));}
5639 sprintf ( name, "%s", name_in);
5640 tild_substitute (name, "~", get_home_4_tcoffee());
5642 get_new_name=tolerate_mistake=0;
5643 if ( mode[0]=='g'){get_new_name=1; mode++;}
5644 else if ( mode[0]=='t'){tolerate_mistake=1;mode++;}
5645 /*Use the cached version from CACHE_4_TCOFFEE*/
5646 else if ( mode[0]=='c'){cache_used=1;mode++;}
5648 if (name==NULL ||strm5 ( name, "no","NO","No","NULL","/dev/null") || strm2 (name, "no_file", "NO_FILE"))
5650 if ( NFP==NULL)NFP=fopen (NULL_DEVICE, mode);
5653 else if ( strm3 (name,"stderr","STDERR","Stderr"))return stderr;
5654 else if ( strm3 (name,"stdout","STDOUT","Stdout"))return stdout;
5655 else if ( strm3 ( name, "stdin","STDIN","Stdin"))
5659 stdin_file=vtmpnam (NULL);
5660 tmp_fp=vfopen ( stdin_file, "w");
5661 while ( (c=fgetc(stdin))!=EOF)fprintf (tmp_fp, "%c", c);
5664 return vfopen (stdin_file, "r");
5667 else if ( strm (name, "") && (strm (mode, "w") ||strm (mode, "a")) )return stdout;
5668 else if ( strm (name, "") && strm (mode, "r"))return stdin;
5669 else if ( (fp= fopen ( name, mode))==NULL)
5671 if ( strcmp (mode, "r")==0 && cache_used==0)
5673 sprintf ( name2, "%s%s",get_cache_dir(), name);
5674 return vfopen ( name2, "cr");
5676 else if ( strcmp (mode, "r")==0 && cache_used==1)
5678 fprintf (stderr, "\nCOULD NOT READ %s\n", name);
5679 if ( get_new_name){fprintf ( stderr, "\nNew name: ");return vfopen (input_name(), mode-1);}
5680 else if ( tolerate_mistake)return NULL;
5683 fprintf (stderr, "\nFORCED EXIT (NON INTERACTIVE MODE)\n");
5684 if ( getenv ( "DEBUG_TCOFFEE"))crash ("DEBUG");
5685 else myexit (EXIT_FAILURE);
5688 else if ( strcmp (mode, "a")==0 && cache_used==0)
5690 sprintf ( name2, "%s%s",get_cache_dir(), name);
5691 return vfopen ( name, "ca");
5693 else if ( strcmp (mode, "a")==0 && cache_used==1)
5695 fprintf (stderr, "\nCOULD NOT Append anything to %s\n", name);
5696 if ( get_new_name){fprintf ( stderr, "\nNew name: ");return vfopen (input_name(), mode-1);}
5697 else if ( tolerate_mistake)return NULL;
5700 fprintf (stderr, "\nFORCED EXIT (NON INTERACTIVE MODE)\n");
5701 myexit (EXIT_FAILURE);
5704 else if ( strcmp (mode, "w")==0)
5706 fprintf (stderr, "\nCANNOT WRITE %s\n", name);
5707 if ( get_new_name==1){fprintf ( stderr, "\nNew name: ");return vfopen (input_name(), mode-1);}
5708 else if ( tolerate_mistake)return NULL;
5711 fprintf (stderr, "\nFORCED EXIT (NON INTERACTIVE MODE): %s %s\n", (strcmp ( mode, "r")==0)?"READ":"WRITE", name);
5712 myexit(EXIT_FAILURE);
5722 FILE * vfclose ( FILE *fp)
5724 if ( fp==NFP)return NULL;
5725 if ( fp==stdout)return stdout;
5726 if ( fp==stderr)return stderr;
5727 if ( fp==stdin) return stdin;
5728 if ( fp==NULL)return NULL;
5734 int echo ( char *string, char *fname)
5739 prints the content of string into file fname
5742 string= string to print
5743 fname =name of the file to create
5748 fp=vfopen ( fname, "w");
5749 fprintf (fp, "%s", string);
5755 int file_cat ( char *from, char *to)
5758 //appends the content of file1 to file 2
5759 if (!(fp=vfopen (to, "a")))return 0;
5760 if (!display_file_content (fp, from)) return 0;
5765 FILE* display_file_content (FILE *output, char *name)
5769 if ( !name || !check_file_exists (name) || !(fp=vfopen (name, "r")))return NULL;
5770 while ( (c=fgetc(fp))!=EOF)fprintf (output,"%c", c);
5775 char ***file2list ( char *name, char *sep)
5777 /*Rturns an array where
5779 list[0][0]: number of words
5780 list[0][1]:first word;
5783 char **lines, ***list;
5786 lines=file2lines (name);
5787 if (!lines) return NULL;
5792 list=vcalloc ( n+1, sizeof (char**));
5793 for ( a=1; a<n; a++)
5796 list[a-1]=string2list2(lines[a], sep);
5800 free_arrayN((void**)lines, 2);
5803 char **file2lines (char *name)
5806 lines[1]->first_line
5812 string=file2string (name);
5813 if ( !string) return NULL;
5816 lines=string2list2(string, "\n");
5822 char *string2file ( char *string, char *file, char *mode)
5826 file=vtmpnam (NULL);
5827 fp=vfopen (file, mode);
5828 fprintf (fp, "%s", string);
5832 char *file2string (char *name)
5838 if (!name || !check_file_exists (name))return NULL;
5841 string=vcalloc ( count_n_char_in_file(name)+1, sizeof (char));
5842 fp=vfopen (name, "r");a=0;
5843 while ( (c=fgetc(fp))!=EOF)
5853 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, ...)
5860 set_flag set to 1 if param set;
5861 para_type F, I, S, R_FN (read_file, name), W_FN (written file, name), R_FP (pointer)
5862 max_n_val maximum number of values;
5863 optional 1 for yes, 0 for no
5864 usage usage list with optional value;
5865 val pointer to the varaible holding the value(s)
5866 default1 default value (if value id not there)
5867 default2 default value if the flag is there but no value set ("")indicates an error
5868 range_left min value ( "any" for any);
5869 range_right max_value ( "any" for any);
5876 float *float_val=NULL;
5877 char **string_val=NULL;
5884 char *default_value1;
5885 char *default_value2;
5889 static char **parameter_list;
5890 static int number_of_parameters;
5892 char **para_name_list;
5906 /*CHECK THAT ALL THE PARAM IN ARG EXIST*/
5907 if ( para_name==NULL)
5909 for ( a=1; a< argc; a++)
5911 if ( is_parameter ( argv[a]))
5912 if ( name_is_in_list ( argv[a], parameter_list, number_of_parameters, STRING)==-1)
5914 fprintf ( stderr, "\n%s IS NOT A PARAMETER OF %s [FATAL/%s %s]\n",argv[a], argv[0], argv[0], VERSION);
5915 myexit(EXIT_FAILURE);
5921 free_char (parameter_list,-1);
5925 if ( parameter_list==NULL)parameter_list=declare_char(MAX_N_PARAM,STRING);
5926 para_name_list=get_list_of_tokens(para_name,NULL, &n_para_name);
5927 for ( a=0; a< n_para_name; a++)
5929 sprintf ( parameter_list[number_of_parameters++],"%s", para_name_list[a]);
5931 free_char(para_name_list,-1);
5938 va_start (ap, usage);
5940 if (strm3 (type, "S","R_F","W_F"))
5941 string_val=va_arg(ap, char**);
5942 else if (strm2 (type, "D","FL"))
5943 int_val=va_arg(ap, int*);
5944 else if (strm (type, "F"))
5945 float_val=va_arg(ap, float*);
5947 myexit (EXIT_FAILURE);
5951 default_value1=va_arg(ap, char*);
5952 default_value2=va_arg(ap, char*);
5953 range_left =va_arg(ap, char*);
5954 range_right =va_arg(ap, char*);
5958 para_name_list=get_list_of_tokens(para_name, NULL, &n_para_name);
5959 for ( a=0; a<n_para_name; a++)
5961 if ( (pos=name_is_in_list(para_name_list[a], argv,argc, STRING))!=-1)break;
5963 free_char (para_name_list,-1);
5965 if ( (name_is_in_list("-help" , argv,argc ,STRING)!=-1) && (argc==2 || (name_is_in_list( para_name , argv,argc ,STRING)!=-1)))
5968 fprintf ( stderr, "PARAMETER : %s\n", para_name);
5969 fprintf ( stderr, "USAGE : %s\n", usage);
5970 fprintf ( stderr, "MAX_N_VALUES: %d\n", max_n_val);
5971 fprintf ( stderr, "DEFAULT : %s OR %s (when flag set)\n", default_value1, default_value2);
5972 fprintf ( stderr, "RANGE : [%s]...[%s]\n", range_left,(strm(range_right,"any"))?"":range_right);
5973 fprintf ( stderr, "TYPE : %s\n\n", type);
5976 else if ( name_is_in_list ("-help" , argv,argc ,STRING)!=-1)
5980 else if (para_name[0]!='-')
5982 fprintf ( stderr, "\nWRONG PARAMETER DEFINITION %s Must Start with a dash", para_name);
5983 myexit (EXIT_FAILURE);
5987 if ( optional==OPTIONAL)
5990 para_val=get_list_of_tokens(default_value1, NULL, &n_para_val);
5992 for (n_para=0; n_para<n_para_val && !strm (default_value1, "NULL"); n_para++)
5994 if ( strm (para_val[n_para], ""))
5999 else if ( strm (type, "FL"))
6001 set_flag[0]=atoi(para_val[n_para]);
6004 else if (strm3 (type, "S", "R_F","W_F"))
6007 sprintf ( string_val[n_para], "%s",para_val[n_para]);
6009 else if ( strm (type, "D"))
6010 int_val[n_para]=atoi(para_val[n_para]);
6011 else if ( strm (type, "F"))
6012 float_val[n_para]=atof(para_val[n_para]);
6014 free_char (para_val, -1);
6016 if (n_para==0 && strm3(type, "S","W_F","R_F") && strm (default_value1, "NULL"))
6018 vfree (string_val[0]);
6022 else if (n_para==0 && strm (type, "D") && strm (default_value1, "NULL"))int_val[0]=0;
6023 else if (n_para==0 && strm (type, "F") && strm (default_value1, "NULL"))float_val[0]=0;
6028 fprintf ( stderr, "\nParameter %s is not optional",para_name);
6029 myexit (EXIT_FAILURE);
6035 for (a=pos+1; a< argc; a++)
6037 if ( is_parameter(argv[a]))break;
6040 if ( n_para>=max_n_val)
6045 if ( !(strm ( argv[a], "NULL")))
6047 if ( strm3(type, "S", "R_F", "W_F"))
6049 sprintf ( string_val[n_para],"%s", argv[a]);
6051 else if (strm (type, "D"))
6053 int_val[n_para]=atoi(argv[a]);
6055 else if (strm ( type,"F"))
6057 float_val[n_para]=atof(argv[a]);
6064 if ( n_para==0 && !strm2(default_value2,"","NULL") && !strm(type, "FL"))
6066 para_val=get_list_of_tokens(default_value2, NULL, &n_para_val);
6067 for ( n_para=0; n_para<n_para_val; n_para++)
6069 if ( strm3(type, "S", "R_F", "W_F"))sprintf ( string_val[n_para],"%s", para_val[n_para]);
6070 else if (strm (type, "D"))int_val [n_para]=atoi(para_val[n_para]);
6071 else if (strm ( type,"F"))float_val[n_para]=atof(para_val[n_para]);
6073 free_char (para_val,-1);
6075 else if (n_para==0 && strm (type, "FL"));
6076 else if (n_para==0 && strm3(type, "S","W_F","R_F") && strm (default_value2, "NULL")){vfree (string_val[0]);string_val[0]=NULL;}
6077 else if (n_para==0 && strm (type, "D") && strm (default_value2, "NULL"))int_val[0]=0;
6078 else if (n_para==0 && strm (type, "F") && strm (default_value2, "NULL"))float_val[0]=0;
6079 else if (n_para==0 && strm (default_value2, ""))
6081 fprintf ( stderr, "\nParam %s needs a value [FATAL/%s]", para_name, argv[0]);
6082 myexit(EXIT_FAILURE);
6087 /*Check That The Parameters are in the Good Range*/
6089 pv_l=get_list_of_tokens( range_left , NULL, &n_pv_l);
6090 pv_r=get_list_of_tokens( range_right, NULL, &n_pv_r);
6092 for ( a=0; a< n_para; a++)
6094 if ( strm (type, "R_F") && !check_file_exists(string_val[a]) && !check_file_exists(string_val[a]+1))
6096 fprintf ( stderr, "PARAM %s: File %s does not exist [FATAL/%s]\n",para_name,string_val[a], argv[0]);
6097 myexit(EXIT_FAILURE);
6099 else if ( strm (pv_l[0], "any"));
6100 else if ( strm (type, "D"))
6104 min=(double)atoi(pv_l[0]);
6105 max=(double)atoi(pv_r[0]);
6106 if ( int_val[a]<min || int_val[a]>max)
6108 fprintf ( stderr, "\n%s out of range [%d %d] [FATAL/%s]\n", para_name, (int)min, (int)max,argv[0]);
6109 myexit (EXIT_FAILURE);
6114 sprintf ( value, "%d", int_val[a]);
6115 if ( name_is_in_list(value, pv_l, n_pv_l, STRING)==-1)
6116 fprintf ( stderr, "\n%s out of range [%s: ", para_name, value);
6117 print_array_char (stderr, pv_l, n_pv_l, " ");
6118 fprintf ( stderr, "\n");
6119 myexit(EXIT_FAILURE);
6122 else if ( strm (type, "F"))
6126 min=(double)atof(range_left);
6127 max=(double)atof(range_right);
6128 if ( float_val[a]<min || float_val[a]>max)
6130 fprintf ( stderr, "\n%s out of range [%f %f] [FATAL/%s]\n", para_name, (float)min, (float)max,argv[0]);
6131 myexit (EXIT_FAILURE);
6136 sprintf ( value, "%f", float_val[a]);
6137 if ( name_is_in_list(value, pv_l, n_pv_l, STRING)==-1)
6138 fprintf ( stderr, "\n%s out of range [%s: ", para_name, value);
6139 print_array_char (stderr, pv_l, n_pv_l, " ");
6140 fprintf ( stderr, "\n");
6141 myexit(EXIT_FAILURE);
6149 fprintf (fp[0], "%-15s\t%s\t[%d] ", para_name, type, set_flag[0]);
6150 for (a=0; a<n_para; a++)
6152 if ( strm3 ( type, "S", "R_F", "W_F"))fprintf ( fp[0], "\t%s", string_val[a]);
6153 else if ( strm ( type, "D"))fprintf ( fp[0], "\t%d ", int_val[a]);
6154 else if ( strm ( type, "F"))fprintf ( fp[0], "\t%f ", float_val[a]);
6156 if ( strm (type, "FL"))fprintf ( fp[0], "\t%d", int_val[0]);
6157 fprintf ( fp[0], "\n");
6160 free_char ( pv_l, -1);
6161 free_char ( pv_r, -1);
6167 char ** get_parameter ( char *para_name, int *np, char *fname)
6171 para_name: the name of the parameter to look for
6172 fname: the name of the file containing the parameters
6176 char ** containing the np[0] values taken by para_name in fname.
6179 if fname=NULL, para_name is searched using the last value taken by fp.
6181 Note: by default, the function keeps a file handle open until the first unsuccessful call.
6186 char ** return_value;
6188 if ( strm (para_name, "CLOSE THE FILE"))
6194 if ( line==NULL)line=vcalloc ( VERY_LONG_STRING+1, sizeof (char));
6195 if ( fname!=NULL && fp!=NULL)vfclose (fp);
6199 if ((fp=find_token_in_file ( fname,(fname==NULL)?fp:NULL, para_name))==NULL)
6205 fgets ( line, VERY_LONG_STRING,fp);
6206 return_value=get_list_of_tokens ( line, NULL, np);
6207 return return_value;
6211 FILE * set_fp_id ( FILE *fp, char *id)
6213 /*Sets fp just after id, id needs to be at the begining of the line*/
6225 fscanf ( fp, "%s", string);
6227 if ( strcmp ( string, id)==0)
6229 else while ( c!='\n' && c!=EOF)
6240 FILE * set_fp_after_char ( FILE *fp, char x)
6242 /*sets fp just after the first occurence of x*/
6267 FILE * find_token_in_file_nlines ( char *fname, FILE * fp, char *token, int n_line)
6270 /*This function finds the string TOKEN (as a single word) in the n_line first lines of fname.
6271 It returns NULL if not found or the position of the fp
6274 char *tmp_name=NULL;
6279 if ( !fp && !check_file_exists(fname))return NULL;
6282 tmp_name=vtmpnam ( NULL);
6284 fp1=vfopen (fname, "r");
6285 fp2=vfopen (tmp_name, "w");
6287 for ( a=0; a< n_line && fgets(buffer, 10000, fp1)!=NULL; a++)
6289 fprintf ( fp2, "%s", buffer);
6294 return find_token_in_file ( tmp_name,fp,token);
6297 int token_is_in_file ( char *fname, char *token)
6299 /*TH:an argument against torture: innocents get tortured longer, likewise for token testing*/
6318 if ( !fname || !check_file_exists(fname))return 0;
6321 fp=vfopen (fname, "r");
6322 while ((b=vfgets (buf,fp))!=NULL)
6325 p=strstr (buf, token);
6327 else if ( begining==1 && p==buf){vfclose (fp); return 1;}
6328 else if ( begining==0 && p){vfclose (fp); return 1;}
6336 char *vfgets ( char *buf, FILE *fp)
6345 debug=(getenv ("DEBUG_TCOFFEE"))?1:-1;
6352 buf=vcalloc ( 1000, sizeof (char));
6357 buf_len=read_array_size_new (buf);
6362 if ( (c=fgetc (fp))==EOF)return NULL;
6363 else ungetc (c, fp);
6366 while ( (c=fgetc (fp))!='\n' && c!=EOF)
6369 {buf_len+=100;buf=vrealloc (buf, buf_len*sizeof (char));}
6372 /*Add the cariage return*/
6375 if (l>=buf_len){buf_len+=100,buf=vrealloc (buf, buf_len*sizeof (char));}
6378 /*add the terminator*/
6379 if (l>=buf_len){buf_len+=100,buf=vrealloc (buf, buf_len*sizeof (char));}
6382 if ( bufin!=buf && bufin!=NULL && debug==1)
6383 fprintf ( stderr, "\nPointer change in vfgets...");
6389 FILE * find_token_in_file ( char *fname, FILE * fp, char *token)
6397 /*Note: Token: any string
6398 If Token[0]=='\n' Then Token only from the beginning of the line
6401 if (!fp && !check_file_exists(fname))return NULL;
6403 if ( token[0]=='\n'){token++;only_start=1;}
6406 token_len=strlen (token);
6414 if (name)vfree (name);
6415 name = vcalloc (((fname)?measure_longest_line_in_file (fname):10000)+1, sizeof (char));
6416 fp=vfopen ( fname, "r");
6419 while ( (fscanf ( fp, "%s", name))!=EOF)
6422 if ( name[0]=='*')while ( ((c=fgetc (fp))!='\n')&& (c!=EOF));
6423 else if (strncmp ( name, token,token_len)==0){return fp;}
6424 else if (only_start) while ( ((c=fgetc (fp))!='\n')&& (c!=EOF));
6430 int **get_file_block_pattern (char *fname, int *n_blocks, int max_n_line)
6444 lline=measure_longest_line_in_file (fname)+1;
6445 line=vcalloc ( sizeof (char),lline+1);
6447 fp=vfopen (fname, "r");
6448 max_block_size=block_size=0;
6452 while ((c=fgetc(fp))!=EOF && (n_line<max_n_line || !max_n_line))
6455 fgets ( line, lline,fp);
6458 if ( is_alnum_line (line) && !in_block){n_blocks[0]++;in_block=1;}
6459 if ( is_alnum_line (line))
6466 max_block_size=MAX( max_block_size, block_size);
6472 max_block_size=MAX( max_block_size, block_size);
6475 l=declare_int (n_blocks[0]+1,max_block_size+1);
6478 fp=vfopen (fname, "r");
6482 while ((c=fgetc(fp))!=EOF && (n_line<max_n_line || !(max_n_line)))
6485 fgets ( line, lline,fp);
6488 if ( is_alnum_line (line) && !in_block){n_blocks[0]++;in_block=1;}
6489 if ( is_alnum_line (line))
6491 l[n_blocks[0]][0]++;
6492 free_char (get_list_of_tokens (line, " \t\n*:,", &x), -1);
6494 if ( l[n_blocks[0]][0]> max_block_size)fprintf ( stderr, "\nERROR %d", l[n_blocks[0]][0]);
6496 l[n_blocks[0]] [l[n_blocks[0]][0]]=x;
6509 char * strip_file_from_comments (char *com, char *in_file)
6511 /*Removes in file in_file every portion of line to the right of one of the symbols included in com
6512 Writes the striped file into a vtmpnam file
6519 out_file=vtmpnam(NULL);
6522 fp1=vfopen (in_file , "r");
6523 fp2=vfopen (out_file, "w");
6524 while ( (c=fgetc(fp1))!=EOF)
6528 while ( (c=fgetc(fp1))!='\n' && c!=EOF);
6532 fprintf (fp2, "%c", c);
6533 while ( (c=fgetc(fp1))!='\n' && c!=EOF)fprintf (fp2, "%c", c);
6534 if ( c!=EOF)fprintf (fp2, "%c", c);
6542 FILE * skip_commentary_line_in_file ( char com, FILE *fp)
6546 if ( fp==NULL)return NULL;
6547 while ((c=fgetc(fp))==com)
6549 while ((c=fgetc(fp))!='\n' && c!=EOF);
6551 if ( c!=EOF && c!='\n')ungetc(c, fp);
6555 int check_for_update ( char *web_address)
6559 float new_version, old_version;
6562 check_internet_connection (IS_NOT_FATAL);
6565 sprintf ( command, "%s/%s.version",DISTRIBUTION_ADDRESS, PROGRAM);
6566 url2file ( command, file);
6568 fp=vfopen ( file, "r");
6569 fscanf ( fp, "Version_%f", &new_version);
6571 sscanf ( VERSION, "Version_%f", &old_version);
6573 if ( old_version<new_version)
6575 fprintf ( stdout, "\nUpdate Status: outdated");
6576 fprintf ( stdout, "\nYour version of %s is not up to date", PROGRAM);
6577 fprintf ( stdout, "\nDownload the latest version %.2f with the following command:", new_version);
6578 fprintf ( stdout, "\n\twget %s/%s_distribution_Version_%.2f.tar.gz\n", DISTRIBUTION_ADDRESS, PROGRAM, new_version);
6579 return EXIT_FAILURE;
6581 else if ( old_version> new_version)
6583 fprintf ( stdout, "\nUpdate Status: beta-release");
6584 fprintf ( stdout, "\nYour are using a beta-release of %s(%s)\n", PROGRAM, VERSION);
6588 fprintf (stdout, "\nUpdate Status: uptodate");
6589 fprintf (stdout, "\nProgram %s(%s) is up to date\n", PROGRAM, VERSION);
6591 return EXIT_SUCCESS;
6598 int check_environement_variable_is_set ( char *variable, char *description, int fatal)
6600 if ( getenv (variable)==NULL)
6602 fprintf ( stderr, "\nERROR: You must set %s\n%s %s", variable, description, description);
6603 if ( fatal==IS_FATAL)
6605 fprintf ( stderr, "\n[%s:FATAL]\n", PROGRAM);
6606 exit (EXIT_FAILURE);
6609 fprintf ( stderr, "\n[%s:WARNING]\n", PROGRAM);
6614 int url2file (char *address, char *out)
6619 if (check_program_is_installed ("wget",NULL, NULL,WGET_ADDRESS, IS_NOT_FATAL))sprintf (command, "wget %s -O%s >/dev/null 2>/dev/null", address, out);
6620 else if (check_program_is_installed ("curl",NULL, NULL,CURL_ADDRESS, IS_NOT_FATAL))sprintf (command, "curl %s -o%s >/dev/null 2>/dev/null", address, out);
6623 printf_exit (EXIT_FAILURE, stderr, "ERROR: Impossible to fectch external file: Neither wget nor curl is installed on your system [FATAL:%s]\n", PROGRAM);
6624 return EXIT_FAILURE;
6626 return safe_system (command);
6629 int wget (char *address, char *out)
6631 return printf_system ( "curl %s -O%s >/dev/null 2>/dev/null", address, out);
6634 int curl (char *address, char *out)
6636 return printf_system ( "curl %s -o%s >/dev/null 2>/dev/null", address, out);
6640 int simple_check_internet_connection (char *ref_site)
6642 char *test,command[1000];
6645 test=vtmpnam (NULL);
6646 if (url2file((ref_site)?ref_site:TEST_WWWSITE_4_TCOFFEE,test)!=EXIT_SUCCESS)internet=0;
6647 else if ((n=count_n_char_in_file(test))<10)internet=0;
6652 int check_internet_connection (int mode)
6655 internet=simple_check_internet_connection (NULL);
6656 if (internet)return 1;
6657 else if ( mode == NON_INTERACTIVE)return internet;
6658 else if ( mode == IS_FATAL)
6660 add_warning ( stderr,"\nERROR: You do not seem to have an active Internet Connection. Check your Proxy Setting [proxy:%s][%s:SERIOUS]\n", getenv ("http_proxy"), PROGRAM);
6661 return EXIT_FAILURE;
6663 else if ( mode==IS_NOT_FATAL)
6666 fprintf ( stderr, "\n\n\n");
6667 fprintf ( stderr, "*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
6668 fprintf ( stderr, "*\n");
6669 fprintf ( stderr, "* Your Internet Connection Does not seem to be available. You may need to reconfigure your Proxy\n");
6670 fprintf ( stderr, "* ------- Current Proxy Value: [%s]\n*\n", (getenv ("http_proxy")?getenv ("http_proxy"):"NO Proxy Set"));
6671 Proxy(INPUT, RESET);
6673 return check_internet_connection (IS_FATAL);
6680 char *pg2path (char *pg)
6687 if ( !pg) return NULL;
6690 printf_system_direct("which %s>%s 2>/dev/null", pg, tmp);
6691 path=file2string (tmp);
6693 if (!check_file_exists (path) && !strstr (pg, ".exe"))
6696 sprintf ( pg2, "%s.exe", pg);
6704 int check_program_is_installed ( char *program_name, char *path_variable, char *path_variable_name, char *where2getit, int fatal)
6706 char command[LONG_STRING];
6709 if ( strm (where2getit, "built_in"))return 1;
6711 if (path)vfree (path);
6713 if ( check_file_exists (path_variable))
6720 path=pg2path (program_name);
6721 if (path && path[0])return 1;
6724 int install=EXIT_FAILURE;
6726 if (fatal==INSTALL || fatal==INSTALL_OR_DIE)
6728 HERE ("************** %s is missing from your system. T-Coffee will make an attempt to install it.\n", program_name);
6729 install=printf_system ("install.pl %s -plugins=%s -clean", program_name, get_plugins_4_tcoffee(NULL));
6731 if ( install==EXIT_SUCCESS)return 1;
6732 else if ( fatal==INSTALL)return 0;
6733 else if ( fatal==NO_REPORT)return 0;
6735 if (fatal==IS_FATAL || fatal==INSTALL_OR_DIE)check_configuration4program();
6737 fprintf ( stderr, "\n#*****************************************************************");
6738 if (fatal) fprintf ( stderr, "\n#ERROR [FATAL:%s]", PROGRAM);
6739 else fprintf ( stderr, "\n#WARNING [%s]", PROGRAM);
6740 fprintf ( stderr, "\n# The Program %s Needed by %s Could not be found", program_name, PROGRAM);
6741 fprintf ( stderr, "\n# If %s is installed on your system:", program_name);
6742 fprintf ( stderr, "\n#\t -Make sure %s is in your $path:",program_name);
6744 fprintf ( stderr, "\n# If %s is NOT installed obtain a copy from:", program_name);
6745 fprintf ( stderr, "\n#\t%s\n#\n#",where2getit);
6746 fprintf ( stderr, "\n# and install it manualy");
6747 fprintf ( stderr, "\n******************************************************************\n");
6750 if ( fatal==IS_FATAL || fatal==INSTALL_OR_DIE) myexit (EXIT_FAILURE);
6754 FILE * display_output_filename ( FILE *io, char *type, char *format, char *name, int check_output)
6759 if ( strm ( name, "stdout") || strm (name, "stderr"))return io;
6761 if ( check_output==STORE)
6764 if ( buf==NULL)buf=vcalloc ( 1000, sizeof (char**));
6766 for (a=0; a<nbuf; a++)
6767 if ( strm (name, buf[a][2]))return io;
6769 buf[nbuf]=declare_char (3, 1000);
6770 sprintf ( buf[nbuf][0], "%s", type);
6771 sprintf ( buf[nbuf][1], "%s", format);
6772 sprintf ( buf[nbuf][2], "%s", name);
6776 else if ( check_output==FLUSH)
6779 for ( a=0; a< nbuf; a++)
6781 io=display_output_filename ( io, buf[a][0], buf[a][1], buf[a][2], CHECK);
6782 free_char (buf[a], -1);
6786 else if ( check_output==CHECK)
6788 if (check_file_exists(name)==NULL)
6790 fprintf ( io, "\n\t#### File Type= %10s Format= %10s Name= %s | NOT PRODUCED [WARNING:%s:%s]\n",type, format, name, PROGRAM, VERSION );
6793 add_file2error_report (type, format, name);
6794 fprintf ( io, "\n\t#### File Type= %10s Format= %10s Name= %s",type, format, name );
6799 FILE * display_input_filename ( FILE *io, char *type, char *format, char *name, int check_output)
6801 if ( check_output==CHECK && check_file_exists(name)==NULL)
6803 fprintf ( io, "\n\tIIII INPUT File Type= %10s Format= %10s Name= %s | NOT PRODUCED [WARNING:%s:%s]\n",type, format, name, PROGRAM, VERSION );
6806 add_file2error_report (type, format, name);
6808 fprintf ( io, "\n\t#### File Type= %10s Format= %10s Name= %s",type, format, name );
6812 int file_is_empty (char *fname)
6815 if (!fname) return 1;
6818 if (s.st_size)return 0;
6821 int file_exists (char *path, char *fname)
6826 if (!fname)return 0;
6827 else if (!path)sprintf (file, "%s", fname);
6828 else sprintf ( file, "%s/%s", path, fname);
6832 if (stat(file,& s)!=-1)
6833 return S_ISREG(s.st_mode);
6837 int isdir (char *file)
6840 if (stat (file,&s)!=-1)
6841 return S_ISDIR(s.st_mode);
6844 int rrmdir (char *s)
6846 if (isdir(s))return printf_system_direct ("rm -r %s", s);
6847 return EXIT_FAILURE;
6850 int isexec (char *file)
6854 state=ls_l(NULL,file);
6855 if (state[0]==0) return 0;
6857 if ( state[0]=='d') return 0;
6858 if ( state[3]=='x') return 1;
6859 if ( state[6]=='x') return 1;
6860 if ( state[9]=='x') return 1;
6864 char *ls_l ( char *path,char *file)
6868 char command[10000];
6872 tmpfile=vtmpnam (NULL);
6876 state=vcalloc (100, sizeof (char));
6878 for (a=0;a<100; a++)state[a]=0;
6879 if (!file)return state;
6882 sprintf ( command, "ls -l %s%s%s >%s 2>/dev/null", (path!=NULL)?path:"", (path!=NULL)?"/":"",file, tmpfile);
6883 safe_system (command);
6884 fp=vfopen (tmpfile, "r");
6885 if (!fscanf ( fp, "%s", state))
6887 vfclose(fp); return 0;
6893 int my_mkdir ( char *dir_in)
6901 dir=vcalloc ( strlen (dir_in)+strlen (get_home_4_tcoffee())+100, sizeof (char));
6902 sprintf ( dir, "%s", dir_in);
6903 tild_substitute ( dir, "~",get_home_4_tcoffee());
6909 while (dir[a]!='\0')
6912 if ( dir[a]==dir_sep || dir[a+1]=='\0')
6917 if (access(dir, F_OK)==-1)
6920 printf_system_direct("mkdir %s", dir);
6922 if ( access (dir, F_OK)==-1)
6924 fprintf ( stderr, "\nERROR: Could Not Create Directory %s [FATAL:%s]", dir, PROGRAM);
6925 exit (EXIT_FAILURE);
6937 int filename_is_special (char *fname)
6939 if ( strm5 (fname, "default", "stdin", "stdout","stderr", "/dev/null"))return 1;
6940 if ( strm3 (fname, "STDIN", "STDOUT", "STDERR"))return 1;
6944 char* check_file_exists ( char *fname_in)
6947 static char *fname1;
6948 static char *fname2;
6951 if (!fname_in)return NULL;
6952 if (!fname_in[0])return NULL;
6953 if (fname_in[0]=='-')return NULL;
6955 if (!fname1){fname1=vcalloc (1000, sizeof (char));}
6956 if (!fname2){fname2=vcalloc (1000, sizeof (char));}
6958 sprintf ( fname1, "%s", fname_in);tild_substitute (fname1, "~", get_home_4_tcoffee());
6959 sprintf ( fname2, "%s%s", get_cache_dir(),fname1);
6961 if ( filename_is_special (fname1))return fname1;
6962 if ( strm5 (fname1, "no", "NO", "No", "NO_FILE","no_file"))return NULL/*fname1*/;
6963 if (!file_exists( NULL,fname1))
6965 if (!file_exists (NULL,fname2))return NULL;
6973 void create_file ( char *name)
6977 fp=fopen (name, "w");
6980 void delete_file ( char *fname)
6985 fp=fopen ( fname, "w");
6989 sprintf ( command, "rm %s", fname);
6990 my_system ( command);
6994 int util_rename ( char *from, char *to)
7001 if ( !check_file_exists (from))return 0;
7002 else if ( check_file_exists (to) && !vremove (to) && !rename ( from, to)==0 );
7006 fp_from=vfopen ( from, "r");
7007 fp_to=vfopen ( to, "w");
7009 while ( (c=fgetc (fp_from))!=EOF)fprintf ( fp_to, "%c", c);
7021 int util_copy ( char *from, char *to)
7028 if (!check_file_exists (from))return 0;
7032 fp_from=vfopen ( from, "r");
7033 fp_to=vfopen ( to, "w");
7035 while ( (c=fgetc (fp_from))!=EOF)fprintf ( fp_to, "%c", c);
7043 FILE * output_completion4halfmat ( FILE *fp,int n, int tot, int n_reports, char *s)
7046 int max, left, achieved;
7053 max=((tot*tot)-tot)/2;
7054 left=((tot-n)*(tot-n)-(tot-n))/2;
7065 return output_completion (fp,achieved, max, n_reports, s);
7069 FILE * output_completion ( FILE *fp,int n, int tot, int n_reports, char *string)
7074 static int ref_time;
7081 ref_time=get_time()/1000;
7086 if ( !ref_val && !flag)
7088 fprintf (fp, "\n\t\t[%s][TOT=%5d][%3d %%][ELAPSED TIME: %4d sec.]",(string)?string:"",tot,(tot==1)?100:0, elapsed);
7091 else if ( n==tot)fprintf (fp, "\r\t\t[%s][TOT=%5d][%3d %%][ELAPSED TIME: %4d sec.]",(string)?string:"", tot,100, elapsed);
7092 else if ( ((n*100)/tot)>ref_val)
7094 ref_val=((n*100)/tot);
7095 t=(ref_val==0)?0:elapsed/ref_val;
7098 fprintf (fp, "\r\t\t[%s][TOT=%5d][%3d %%][ELAPSED TIME: %4d sec.]", (string)?string:"",tot,ref_val, elapsed);
7103 void * null_function (int a,...)
7105 fprintf ( stderr, "\n[ERROR] Attempt to use the Null Function [FATAL:%s]", PROGRAM);
7110 int btoi ( int nc,...)
7115 for ( a=0, b=0; a< nc; a++)
7117 b+=pow(2,a)*va_arg (ap,int);
7123 /*********************************************************************/
7125 /* Geometric FUNCTIONS */
7128 /*********************************************************************/
7130 float get_geometric_distance ( float ** matrix, int ncoor, int d1, int d2, char *mode)
7136 if ( strm (mode, "euclidian"))
7138 for ( a=0; a< ncoor; a++)
7140 d=(matrix[d1][a]-matrix[d2][a]);
7143 return (float)sqrt((double)t);
7150 /*********************************************************************/
7152 /* MATHEMATICAL FUNCTIONS */
7155 /*********************************************************************/
7156 static double EXP_UNDERFLOW_THRESHOLD = -4.60f;
7157 static double LOG_UNDERFLOW_THRESHOLD = 7.50f;
7158 static double LOG_ZERO = -FLT_MAX;
7159 static double LOG_ONE = 0.0f;
7160 double log_addN (int N, double*L)
7166 if ( N==1)return L[0];
7169 for ( a=1; a<N; a++)
7171 v=log_add2(v, L[a]);
7175 double log_add6 (double x, double y, double z, double w, double v, double e)
7177 return log_add2(log_add3(x, y, z),log_add3(z,w,e));
7179 double log_add5 (double x, double y, double z, double w, double v)
7181 return log_add3(log_add2(x, y),log_add2(z,w),v );
7183 double log_add4 (double x, double y, double z, double w)
7185 return log_add2(log_add2(x, y),log_add2(z,w));
7187 double log_add3 (double x, double y, double z)
7189 return log_add2(log_add2(x, y),z);
7191 double log_add2 ( double x, double y)
7194 x = (x == LOG_ZERO || ((y - x) >= LOG_UNDERFLOW_THRESHOLD)) ? y : log (exp (x-y) + 1) + x;
7196 x = (y == LOG_ZERO || ((x - y) >= LOG_UNDERFLOW_THRESHOLD)) ? x : log (exp (x-y) + 1) + y;
7203 float M_chooses_Nlog ( int m, int N)
7205 /*Choose M elemets in N*/
7207 if ( m==N) return 0;
7210 fprintf ( stderr, "\nERROR: M chosses N out of bounds ( M>N) [FATAL:%s]", PROGRAM);
7211 myexit (EXIT_FAILURE);
7215 z1=factorial_log (m+1, N);
7216 z2=factorial_log (1, N-m);
7224 float factorial_log ( int start, int end)
7226 if ( end==0)return 0;
7227 else if ( end==start) return (float)my_int_log((double)start);
7228 else if ( start>end)
7230 fprintf ( stderr, "\nERROR: factorial log out of bounds (%d %d) [FATAL:%s]",start, end, PROGRAM);
7231 myexit (EXIT_FAILURE);
7237 for ( x=0,a=start; a<=end; a++)
7239 x+=(float)my_int_log(a);
7246 float my_int_log(int a)
7249 if ( a>=100000)return log(a);
7253 if (!lu) lu=vcalloc ( 100000, sizeof (float));
7254 if ( !lu[a]){lu[a]=log(a);}
7260 double factorial (int start, int end);
7261 double M_chooses_N ( int m, int N)
7263 /*Choose M elemets in N*/
7264 if ( m==N) return 1;
7267 fprintf ( stderr, "\nERROR: M chosses N out of bounds ( M>N) [FATAL:%s]", PROGRAM);
7268 myexit (EXIT_FAILURE);
7272 return factorial (m+1, N)/factorial (1, N-m);
7276 fprintf ( stderr, "\nERROR: M chosses N out of bounds ( N>50). Use log space [FATAL:%s]", PROGRAM);
7277 myexit (EXIT_FAILURE);
7281 double factorial (int start, int end)
7284 if ( start>end || start<0 || end<0)
7286 fprintf ( stderr, "\nERROR: Negative Factorial [FATAL:%s]", PROGRAM);
7287 myexit ( EXIT_FAILURE);
7289 else if (end==0) return 1;
7290 else if (end==start) return end;
7294 if ( !lu)lu=declare_double (100, 100);
7296 if ( lu[start][end])return lu[start][end];
7300 lu[start][end]=(double)start;
7301 for ( a=start+1; a<=end; a++)
7303 lu[start][end]*=(double)a;
7305 return lu[start][end];
7310 /*********************************************************************/
7312 /* Fast Log Additions (adapted from Probcons)*/
7315 /*********************************************************************/
7316 double EXP (double x){
7322 return (((0.03254409303190190000*x + 0.16280432765779600000)*x + 0.49929760485974900000)*x + 0.99995149601363700000)*x + 0.99999925508501600000;
7325 return (((0.01973899026052090000*x + 0.13822379685007000000)*x + 0.48056651562365000000)*x + 0.99326940370383500000)*x + 0.99906756856399500000;
7326 return (((0.00940528203591384000*x + 0.09414963667859410000)*x + 0.40825793595877300000)*x + 0.93933625499130400000)*x + 0.98369508190545300000;
7330 return (((0.00217245711583303000*x + 0.03484829428350620000)*x + 0.22118199801337800000)*x + 0.67049462206469500000)*x + 0.83556950223398500000;
7331 return (((0.00012398771025456900*x + 0.00349155785951272000)*x + 0.03727721426017900000)*x + 0.17974997741536900000)*x + 0.33249299994217400000;
7334 return (((0.00000051741713416603*x + 0.00002721456879608080)*x + 0.00053418601865636800)*x + 0.00464101989351936000)*x + 0.01507447981459420000;
7338 float LOOKUP (float x){
7340 if (x <= 1.00f) return ((-0.009350833524763f * x + 0.130659527668286f) * x + 0.498799810682272f) * x + 0.693203116424741f;
7341 if (x <= 2.50f) return ((-0.014532321752540f * x + 0.139942324101744f) * x + 0.495635523139337f) * x + 0.692140569840976f;
7342 if (x <= 4.50f) return ((-0.004605031767994f * x + 0.063427417320019f) * x + 0.695956496475118f) * x + 0.514272634594009f;
7344 return ((-0.000458661602210f * x + 0.009695946122598f) * x + 0.930734667215156f) * x + 0.168037164329057f;
7346 void LOG_PLUS_EQUALS (float *x, float y){
7349 x[0] = (x[0] == LOG_ZERO || y - x[0] >= LOG_UNDERFLOW_THRESHOLD) ? y : LOOKUP(y-x[0]) + x[0];
7351 x[0] = (y == LOG_ZERO || x[0] - y >= LOG_UNDERFLOW_THRESHOLD) ? x[0] : LOOKUP(x[0]-y) + y;
7354 float LOG_ADD (float x, float y){
7355 if (x < y) return (x == LOG_ZERO || y - x >= LOG_UNDERFLOW_THRESHOLD) ? y : LOOKUP(y-x) + x;
7356 return (y == LOG_ZERO || x - y >= LOG_UNDERFLOW_THRESHOLD) ? x : LOOKUP(x-y) + y;
7359 float LOG_ADD3 (float x1, float x2, float x3){
7360 return LOG_ADD (x1, LOG_ADD (x2, x3));
7362 float LOG_ADD4 (float x1, float x2, float x3, float x4){
7363 return LOG_ADD (x1, LOG_ADD (x2, LOG_ADD (x3, x4)));
7365 float LOG_ADD5 (float x1, float x2, float x3, float x4, float x5){
7366 return LOG_ADD (x1, LOG_ADD (x2, LOG_ADD (x3, LOG_ADD (x4, x5))));
7368 float LOG_ADD6 (float x1, float x2, float x3, float x4, float x5, float x6){
7369 return LOG_ADD (x1, LOG_ADD (x2, LOG_ADD (x3, LOG_ADD (x4, LOG_ADD (x5, x6)))));
7371 float LOG_ADD7 (float x1, float x2, float x3, float x4, float x5, float x6, float x7){
7372 return LOG_ADD (x1, LOG_ADD (x2, LOG_ADD (x3, LOG_ADD (x4, LOG_ADD (x5, LOG_ADD (x6, x7))))));
7377 #define SHORT_SIZE 1
7380 char *strscn(char *s, char *pattern);
7381 long unsigned strtou(char *s, int base, char **scan_end);
7382 long int strtoi(char *s, int base, char **scan_end);
7383 int my_isnumber(char c, int base);
7384 int tonumber(char c);
7386 int my_vsscanf(char *buf, char *fmt, va_list parms)
7388 int scanned = 0, size = 0, suppress = 0;
7389 int w = 0, flag = 0, l = 0;
7394 long unsigned n2, *n2l, parsing = 0;
7396 short unsigned *n2s;
7401 if (*fmt != '%' && !parsing) {
7402 /* No token detected */
7405 /* We need to make a conversion */
7428 w = strtou(fmt, 10, &base);
7429 /* We use SPACE_PAD to parse %10s
7430 * commands where the number is the
7431 * maximum number of char to store!
7439 c_ptr = va_arg(parms, char *);
7445 c_ptr = va_arg(parms, char *);
7446 while (*buf != 0 && isspace(*buf))
7449 while (*buf != 0 && !isspace(*buf)) {
7450 if (!(flag & SPACE_PAD))
7464 buf = strscn(buf, "1234567890-+");
7465 n1 = strtoi(buf, 10, &base);
7470 n1b = va_arg(parms, int *);
7481 *n1s = (short) (n1);
7489 buf = strscn(buf, "1234567890");
7490 n2 = strtou(buf, 10, &base);
7497 *n2b = (unsigned) n2;
7505 n2s = va_arg(parms, short unsigned
7507 *n2s = (short) (n2);
7515 buf = strscn(buf, "1234567890xabcdefABCDEF");
7516 n2 = strtou(buf, 16, &base);
7523 *n2b = (unsigned) n2;
7531 n2s = va_arg(parms, short unsigned
7533 *n2s = (short) (n2);
7543 buf = strscn(buf, "1234567890.e+-");
7544 n3 = strtod(buf, &base);
7549 n3l = va_arg(parms, double *);
7553 n3l = va_arg(parms, double *);
7557 n3s = va_arg(parms, float *);
7558 *n3s = (float) (n3);
7584 char *strscn(char *s, char *pattern)
7589 while (*scan != 0) {
7600 long unsigned strtou(char *s, int base, char **scan_end)
7602 int value, overflow = 0;
7603 long unsigned result = 0, oldresult;
7604 /* Skip trailing zeros */
7607 if (*s == 'x' && base == 16) {
7612 /* Convert number */
7613 while (my_isnumber(*s, base)) {
7614 value = tonumber(*s++);
7615 if (value > base || value < 0)
7620 /* Detect overflow */
7621 if (oldresult > result)
7630 long int strtoi(char *s, int base, char **scan_end)
7632 int sign, value, overflow = 0;
7633 long int result = 0, oldresult;
7638 } else if (*s == '+') {
7643 /* Skip trailing zeros */
7646 /* Convert number */
7647 while (my_isnumber(*s, base)) {
7648 value = tonumber(*s++);
7649 if (value > base || value < 0)
7654 /* Detect overflow */
7655 if (oldresult > result)
7666 int my_isnumber(char c, int base)
7668 static char *digits = "0123456789ABCDEF";
7669 if ((c >= '0' && c <= digits[base - 1]))
7675 int tonumber(char c)
7677 if (c >= '0' && c <= '9')
7679 else if (c >= 'A' && c <= 'F')
7680 return (c - 'A' + 10);
7681 else if (c >= 'a' && c <= 'f')
7682 return (c - 'a' + 10);
7687 ///////////////////////////////////////////////////////////////////////////////////////////
7689 ////////////////////////////////////////////////////////////////////////////////////////////
7690 unsigned long hash_file(char* file) //returns the hash value for key
7692 // Calculate a hash value by the division method:
7693 // Transform key into a natural number k = sum ( key[i]*128^(L-i) ) and calculate i= k % num_slots.
7694 // Since calculating k would lead to an overflow, i is calculated iteratively
7695 // and at each iteration the part divisible by num_slots is subtracted, i.e. (% num_slots is taken).
7697 unsigned long i=0; // Start of iteration: k is zero
7698 unsigned long num_slots=999999999;
7705 if (file==NULL || !check_file_exists (file) ) {printf("Warning from util.c:hasch_file: No File [FATAL:%s]\n", PROGRAM); myexit (EXIT_FAILURE);}
7707 fp=vfopen (file, "r");
7708 while ( (c=fgetc (fp))!=EOF)
7710 i = ((i<<7) + c) % num_slots;
7716 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);
7717 int **generate_array_int_list (int len, int min, int max, int step, int *n, char *file)
7719 int **array, *c_array;
7725 fp=vfopen (file, "w");
7731 for (s=1, a=0; a<len; a++)s*=((max-min)+1)/step;
7732 array=declare_int (s, len+1);
7734 c_array=vcalloc (len, sizeof (int));
7735 array=r_generate_array_int_list ( len, max, min, step, array, 0, n,fp, c_array);
7737 if ( fp) vfclose (fp);
7740 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)
7748 for (a=0; a<len; a++)
7750 array[n[0]][a]=c_array[a];
7758 for (a=0; a<len; a++)fprintf ( fp, "%3d ",c_array[a]);
7766 for (a=max; a<=min;a+=step)
7769 r_generate_array_int_list (len, min, max, step, array, f+1, n,fp, c_array);
7775 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);
7776 char ***generate_array_string_list (int len, char ***alp, int *alp_size, int *n, char *file, int mode)
7778 char ***array, **c_array;
7785 fp=vfopen (file, "w");
7792 for (s=1, a=0; a<len; a++)
7796 array=declare_arrayN (3,sizeof (char), s, len,0);
7798 c_array=declare_char (len,0);
7799 array=r_generate_array_string_list ( len, alp, alp_size, array, 0, n,fp, c_array, mode, -1);
7801 if ( fp) vfclose (fp);
7804 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)
7813 for (a=0; a<len; a++)
7815 array[n[0]][a]=c_array[a];
7823 for (a=0; a<len; a++)fprintf ( fp, "%s ",c_array[a]);
7835 else if ( mode==NO_OVERLAP)
7840 for (a=start; a<alp_size[f]; a++)
7842 c_array[f]=alp[f][a];
7843 r_generate_array_string_list (len,alp, alp_size, array, f+1, n,fp, c_array, mode, a);
7849 float rates2sensitivity (int tp, int tn, int fp, int fn, float *sp, float *sn, float *sen2, float *b)
7853 sp=vcalloc (1, sizeof (float));
7854 sn=vcalloc (1, sizeof (float));
7855 sen2=vcalloc (1, sizeof (float));
7856 b=vcalloc (1, sizeof (float));
7858 sn[0] =((tp+fn)==0)?1:(float)tp/(float)(tp+fn);
7859 sen2[0]=((tp+fp)==0)?1:(float)tp/(float)(tp+fp);
7860 sp[0] =((tn+fp)==0)?1:(float)tn/(float)(tn+fp);
7861 b[0]=MIN((MIN((sp[0]),(sn[0]))),(sen2[0]));
7864 float profile2sensitivity (char *pred, char *ref, float *sp, float *sn, float *sen2, float *b)
7866 int tp=0, tn=0, fp=0, fn=0;
7873 if (pred[a]=='I' && ref[a]=='I')tp++;
7874 if (pred[a]=='O' && ref[a]=='I')fn++;
7875 if (pred[a]=='I' && ref[a]=='O')fp++;
7876 if (pred[a]=='O' && ref[a]=='O')tn++;
7878 return rates2sensitivity (tp, tn, fp, fn, sp, sn, sen2, b);
7880 float profile2evalue (char *pred, char *ref)
7892 if (pred[a]=='I')P++;
7893 if (ref[a]=='I') E++;
7895 if (pred[a]=='I' && ref[a]=='I')II++;
7900 p1= M_chooses_Nlog (P,l) + M_chooses_Nlog (II, P) + M_chooses_Nlog (E-II, l-P);
7901 p2=(M_chooses_Nlog (P,l)+M_chooses_Nlog (E,l));
7911 /*********************************COPYRIGHT NOTICE**********************************/
7912 /*� Centro de Regulacio Genomica */
7914 /*Cedric Notredame */
7915 /*Sat Aug 16 20:30:21 WEST 2008. */
7916 /*All rights reserved.*/
7917 /*This file is part of T-COFFEE.*/
7919 /* T-COFFEE is free software; you can redistribute it and/or modify*/
7920 /* it under the terms of the GNU General Public License as published by*/
7921 /* the Free Software Foundation; either version 2 of the License, or*/
7922 /* (at your option) any later version.*/
7924 /* T-COFFEE is distributed in the hope that it will be useful,*/
7925 /* but WITHOUT ANY WARRANTY; without even the implied warranty of*/
7926 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the*/
7927 /* GNU General Public License for more details.*/
7929 /* You should have received a copy of the GNU General Public License*/
7930 /* along with Foobar; if not, write to the Free Software*/
7931 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/
7932 /*............................................... |*/
7933 /* If you need some more information*/
7934 /* cedric.notredame@europe.com*/
7935 /*............................................... |*/
7939 /*********************************COPYRIGHT NOTICE**********************************/
7945 float fset (FILE *fp, int i, float v)
7947 fseek (fp, i*sizeof (float), SEEK_SET);
7948 fwrite(&v, sizeof (float), fp);
7951 float fread (FILE *fp, int i, float v)
7953 fseek (fp, i*sizeof (float), SEEK_SET);
7954 fread (&v, sizeof (float), fp);
7958 /*********************************COPYRIGHT NOTICE**********************************/
7959 /*© Centro de Regulacio Genomica */
7961 /*Cedric Notredame */
7962 /*Tue Oct 27 10:12:26 WEST 2009. */
7963 /*All rights reserved.*/
7964 /*This file is part of T-COFFEE.*/
7966 /* T-COFFEE is free software; you can redistribute it and/or modify*/
7967 /* it under the terms of the GNU General Public License as published by*/
7968 /* the Free Software Foundation; either version 2 of the License, or*/
7969 /* (at your option) any later version.*/
7971 /* T-COFFEE is distributed in the hope that it will be useful,*/
7972 /* but WITHOUT ANY WARRANTY; without even the implied warranty of*/
7973 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the*/
7974 /* GNU General Public License for more details.*/
7976 /* You should have received a copy of the GNU General Public License*/
7977 /* along with Foobar; if not, write to the Free Software*/
7978 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/
7979 /*............................................... |*/
7980 /* If you need some more information*/
7981 /* cedric.notredame@europe.com*/
7982 /*............................................... |*/
7986 /*********************************COPYRIGHT NOTICE**********************************/