Next version of JABA
[jabaws.git] / binaries / src / tcoffee / t_coffee_source / io_func.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <ctype.h>
4 #include <string.h>
5 #include <ctype.h>
6 #include <stdlib.h>
7 #include "io_lib_header.h"
8 #include "util_lib_header.h"
9 #include "define_header.h"
10 #include "matrices.h"
11
12 #define DEFAULT_COLOR -1
13 #define GAP_COLOR     -2
14 #define INK_COLOR     -3
15         
16 Sequence * cw_read_sequences ( char *seq_name)
17         {
18         Sequence *S;
19
20
21         char **seq=NULL;
22         char **name=NULL;
23         int *len=NULL;
24         int nseq=0;
25         int min_len=0;
26         int max_len=0;
27         int a;
28
29         get_sequence ( seq_name, &nseq, &seq, &name, &len, &min_len, &max_len); 
30         
31         S=declare_sequence ( min_len, max_len, nseq);
32         for ( a=0; a< nseq; a++)sprintf ( S->file[a], "%s", seq_name);
33         
34         for ( a=0; a<S->nseq; a++)
35             {
36             S->len[a]=len[a];
37             sprintf ( S->name[a],"%s", name[a]);
38             vfree ( name[a]);
39             sprintf ( S->seq[a], "%s", seq[a]);
40             vfree ( seq[a]);
41             }
42         vfree (seq);
43         vfree (name);
44         vfree (len);
45         S=get_sequence_type ( S);
46         return S;
47         }
48 char *    get_string_type   (char *S)
49         {
50         int a, l;
51         int protein=0,  dna=0,rna=0, tot=0;
52         char *type;
53         static char *ltype;
54         static int warning;
55         
56         if ( !ltype)
57           declare_name(ltype);
58         
59         declare_name(type);
60         l=(int)strlen (S);
61         
62         if (l==0)
63           {
64             sprintf ( type, "UNKNOWN");
65             return type;
66           }
67         
68         for ( a=0; a<l; a++)     
69                 {
70                     if ( !is_gap(S[a]))
71                         {
72                         protein+=( is_aa (S[a]) && !is_dna(S[a]));
73                         dna+=    ( is_dna(S[a]));
74                         rna+=    ( is_rna(S[a]));
75                         tot++;
76                         }
77                 }
78
79         protein=(protein*100)/tot;
80         dna=(dna*100)/tot;
81         rna=(rna*100)/tot;
82         
83         if ( l<20 && warning==0)
84           {
85             /*add_warning ( stderr, "WARNING: short sequences, use -type=DNA or PROTEIN");*/
86             warning=1;
87           }
88         if ( l<20 && ltype && ltype[0])
89           {
90             if (dna==100)
91               {
92                 sprintf ( type, "%s", ltype);
93                 ltype[0]='\0';
94               }
95             else
96               sprintf ( type, "PROTEIN");
97             
98           }
99         else if ( dna>98 && rna>10)sprintf ( type, "RNA");
100         else if ( dna>98)sprintf ( type, "DNA");
101         else sprintf ( type, "PROTEIN");
102         
103         sprintf ( ltype, "%s", type);
104         return type;
105         }
106
107 Alignment* get_aln_type (Alignment *A)
108         {
109           if ( !A) return A;
110           
111           if ( A->S && !(A->S)->type)(A->S)->type=vcalloc (30, sizeof (char));
112           
113           if ( A->S && (A->S)->type[0]!='\0')
114             {
115             ;
116             }
117           else if (A->S!=NULL && (A->S)->type[0]=='\0') 
118             {
119               A->S=get_sequence_type (A->S);
120             }
121           else if (A->S==NULL) 
122               {
123                 A->S=aln2seq (A);
124                 A->S=get_sequence_type(A->S);
125               }
126           return A;
127         }
128
129
130
131 char *unset_mode ()
132 {
133   return set_mode (UNSET, NULL);
134 }
135 char *store_mode (char *val)
136 {
137   return set_mode (SET, val);
138 }
139 char *retrieve_mode ()
140 {
141   return set_mode (GET,NULL);
142 }
143 char *set_mode (int mode, char *val)
144 {
145   static char type[100];
146   if (mode==SET)
147     {
148       if (!val)printf_exit (EXIT_FAILURE, stderr, "Error:  programme mode unset in io_func.c:set_seq_type");
149       sprintf ( type,"%s", val);
150     }
151   else if ( mode==GET)
152     {
153       ;
154     }
155   else if ( mode==UNSET)
156     {
157       type[0]='\0';
158     }
159   else
160     {
161       printf_exit (EXIT_FAILURE, stderr, "Error: unknown mode in function io_func.c:set_seq_type, use SET, GET or UNSET");
162     }
163   return type;
164 }
165 /************************************************************/
166
167   
168         
169 char *unset_seq_type ()
170 {
171   return set_seq_type (UNSET, NULL);
172 }
173 char *store_seq_type (char *val)
174 {
175   return set_seq_type (SET, val);
176 }
177 char *retrieve_seq_type ()
178 {
179   return set_seq_type (GET,NULL);
180 }
181 char *set_seq_type (int mode, char *val)
182 {
183   static char type[100];
184   if (mode==SET)
185     {
186       if (!val)printf_exit (EXIT_FAILURE, stderr, "Error: sequence type unset in io_func.c:set_seq_type");
187       sprintf ( type,"%s", val);
188     }
189   else if ( mode==GET)
190     {
191       ;
192     }
193   else if ( mode==UNSET)
194     {
195       type[0]='\0';
196     }
197   else
198     {
199       printf_exit (EXIT_FAILURE, stderr, "Error: unknown mode in function io_func.c:set_seq_type, use SET, GET or UNSET");
200     }
201   return type;
202 }
203 char * get_array_type (int n, char **seq)
204 {
205   char *buf, *buf2;
206   int a, tot=0;
207   buf2=vcalloc ( 100, sizeof (char));
208                  
209   
210   for ( tot=0,a=0; a<n; a++)tot+=strlen (seq[a]);
211   buf=vcalloc (tot+1, sizeof (char));
212   for ( a=0; a<n; a++)strcat (buf, seq[a]);
213   sprintf ( buf2, "%s", get_string_type(buf));
214   vfree (buf);
215   return buf2;
216 }
217 Sequence *get_sequence_type (Sequence *S)
218 {
219   if ( !S) return NULL;
220   else sprintf ( S->type, "%s", get_array_type (S->nseq, S->seq));
221   return S;
222 }
223
224
225 void get_sequence (char *seq_file,int *NSEQ, char ***SEQ, char ***SN, int **sl, int *min, int *max)
226         {
227         int a,b;
228         int min_len;
229         int max_len;
230         int nseq;
231
232         int **SL;
233                 
234         nseq=NSEQ[0]= readseqs ( seq_file,  SEQ, SN, &SL);
235         sl[0]=vcalloc ( nseq, sizeof (int));
236         
237          
238         min_len= max_len= (SL)[0][0];
239         for ( a=0; a<NSEQ[0]; a++)
240                 {
241                 sl[0][a]=SL[a][0];
242                 for ( b=0; b<(SL)[a][0]; b++) 
243                         (SEQ[0])[a][b]=tolower ((SEQ[0])[a][b]);
244                  } 
245         for ( a=1; a<NSEQ[0]; a++)
246                 {
247                 min_len= ( min_len > (SL)[a][0])?(SL)[a][0]:min_len;
248                 max_len= ( max_len < (SL)[a][0])?(SL)[a][0]:max_len;
249                 }
250         min[0]=min_len;
251         max[0]=max_len;
252         }
253
254 int ** get_matrix   ( char *name, char *format)
255        {
256
257        if ( strm ( "blast", format))return read_blast_matrix ( name);
258        else if ( strm ( "clustalw", format))return read_matrice(name);
259        else
260            {
261            fprintf ( stderr, "\nError:\nUnknowm Format %s for Matrix %s[FATAL]", format, name);
262            myexit (EXIT_FAILURE);
263            }
264        return NULL;
265        }
266 void display_matrix (int **mat);
267 int ** read_matrice (char *mat_name_in)
268         {
269         int a,b,c, l;
270
271         char AA[]="abcdefghiklmnpqrstvwxyz";
272         FILE *fp;
273         int **matrice;
274         int **matrix2;
275         char mat_name[200];
276         int *vector=NULL;
277         
278         l=strlen(AA);
279
280         
281
282
283         if ( strm2 (mat_name_in, "pam", "PAM"))sprintf ( mat_name, "pam250mt");
284         else if (strm2 (mat_name_in, "blosum", "BLOSUM"))sprintf ( mat_name, "blosum62mt");
285         else if (strm3 (mat_name_in, "id", "ID", "idmat"))sprintf ( mat_name, "idmat");
286         else sprintf ( mat_name, "%s", mat_name_in);
287         
288         /*Read Header Matrices*/
289         if (strm(mat_name, "pam250mt"))vector=pam250mt;
290         else if (strm(mat_name, "idmat"))vector=idmat;
291         else if (strm(mat_name, "dna_idmat"))vector=idmat;
292         else if (strm(mat_name, "est_idmat"))vector=est_idmat;
293         else if (strm(mat_name, "md_350mt"))vector=md_350mt;
294         else if (strm(mat_name, "md_250mt"))vector=md_250mt;
295         else if (strm(mat_name, "md_120mt"))vector=md_120mt;
296         else if (strm(mat_name, "md_40mt" ))vector= md_40mt;
297         else if (strm(mat_name, "pam350mt" ))vector=pam350mt;
298         else if (strm(mat_name, "pam160mt" ))vector=pam160mt;
299         else if (strm(mat_name, "pam120mt" ))vector=pam120mt;
300         
301         else if (strm(mat_name, "blosum80mt" ))vector=blosum80mt;
302         else if (strm(mat_name, "blosum62mt" ))vector=blosum62mt;
303         else if (strm(mat_name, "exon2mt" ))vector=blosum62mt;
304         else if (strm(mat_name, "blosum62mt3" ))vector=blosum62mt3;
305         
306         else if (strm(mat_name, "blosum62mt2" ))vector=blosum62mt2;
307         else if (strm(mat_name, "blosum55mt" ))vector=blosum55mt;
308         else if (strm(mat_name, "blosum50mt" ))vector=blosum50mt;
309         else if (strm(mat_name, "blosum45mt" ))vector=blosum45mt;
310         
311         else if (strm(mat_name, "blosum40mt" ))vector=blosum40mt;
312         else if (strm(mat_name, "blosum30mt" ))vector=blosum30mt;
313         else if (strm(mat_name, "beta_mat" ))vector=beta_mat;
314         else if (strm(mat_name, "alpha_mat" ))vector=alpha_mat;
315         else if (strm(mat_name, "coil_mat" ))vector=coil_mat;
316         
317         else if (strm(mat_name, "rblosum80mt" ))vector=rblosum80mt;
318         else if (strm(mat_name, "rblosum62mt" ))vector=rblosum62mt;
319         else if (strm(mat_name, "rblosum30mt" ))vector=rblosum30mt;
320         
321         else if (strm(mat_name, "rpam250mt" ))vector=rpam250mt;
322         else if (strm(mat_name, "rpam350mt" ))vector=rpam350mt;
323         else if (strm(mat_name, "rpam160mt" ))vector=rpam160mt;
324         else if (strm(mat_name, "rpam120mt" ))vector=rpam120mt;
325
326         else if (strm(mat_name, "tmpam250mt" ))vector=tmpam250mt;
327         else if (strm(mat_name, "rtmpam250mt" ))vector=rtmpam250mt;
328
329         else if (strm(mat_name, "rbeta_mat" ))vector=rbeta_mat;
330         else if (strm(mat_name, "ralpha_mat" ))vector=ralpha_mat;
331         else if (strm(mat_name, "rcoil_mat" ))vector=rcoil_mat;
332         else if (strm (mat_name, "jtttm250mt"))vector=jtttm250mt;
333
334         /*Header Matrices*/
335         if(vector)
336           {
337             matrice=declare_int ( 256, 256);
338             for (a=0; a<l; ++a)
339                 {
340                  for (b=0;b<=a;++b)
341                    {
342                      matrice[a][b]=matrice[b][a]=(vector[(a*a+a)/2+b]);
343                    }
344                 }
345           }
346
347           /*Hard coded Matrices*/
348         
349         else if ( strm (mat_name, "exon_mt"))
350           {
351             matrice=declare_int ( 256, 256);
352             sprintf (AA, "bojx");
353             l=4;
354             for ( a=0; a<l-1; a++)
355               {
356                 matrice[a][a]=10;
357                 for ( b=a+1; b< l-2; b++)
358                   {
359                     matrice[a][b]=matrice[b][a]=5;
360                   }
361               }
362             
363           }
364         else if ( strm ( mat_name, "tdamat"))
365           {
366             int x, y;
367             matrice=declare_int ( 256, 256);
368             sprintf ( AA, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
369             l=strlen (AA);
370             for ( x=0; x<l; x++)
371               for ( y=0; y<l; y++)matrice[x][y]=-10;
372             for ( x=0; x<l; x++) {matrice[x][x]=0; matrice[x][GAP_CODE]=-3;}
373             return matrice;
374           }
375         /*Blast Matrices*/
376         else if ( check_file_exists(mat_name) && is_blast_matrix (mat_name))
377           {
378             
379             matrice=read_blast_matrix ( mat_name);
380             return matrice;
381           }
382         else if ( check_file_exists(mat_name) && is_pavie_matrix (mat_name))
383           {
384             
385             matrice=read_pavie_matrix ( mat_name);
386             return matrice;
387           }
388         
389         else if ( check_file_exists(mat_name) && is_clustalw_matrix (mat_name))
390                 {
391                   
392                   fp=vfopen ( mat_name, "r");
393                   while ( (c=fgetc (fp))!='$' && c!=EOF);
394                   if ( c==EOF){vfclose (fp);return NULL;};
395         
396                   matrice=declare_int ( 256, 256);
397                   fgetc(fp);
398                   for ( a=0; a<l; a++)
399                     {
400                       for ( b=0; b<=a; b++)
401                         if (fscanf ( fp, "%d,", &matrice[a][b])==0){vfclose (fp);free_int (matrice, -1);return NULL;};
402                       fscanf ( fp, "\n");
403                     }           
404                   fclose ( fp);
405                 }
406         else {return NULL;}
407         
408
409         matrix2=declare_int ( 256, 256);
410         for ( b=0; b<l; b++)
411                 for ( c=0; c<l; c++)
412                         {
413                         
414                         matrix2[toupper(AA[b])-'A'][toupper(AA[c])-'A']=matrice[b][c];
415                         matrix2[toupper(AA[b])-'A'][tolower(AA[c])-'A']=matrice[b][c];
416                         matrix2[tolower(AA[b])-'A'][toupper(AA[c])-'A']=matrice[b][c];
417                         matrix2[tolower(AA[b])-'A'][tolower(AA[c])-'A']=matrice[b][c];
418                         }
419         if ( strm ( "exon2mt", mat_name))
420           {
421             char EE[4];
422             int v=(getenv ("EXONVALUE_4_TCOFFEE"))?atoi("EXONVALUE_4_TCOFFEE"):100;
423             sprintf (EE, "obj");
424             for (a=0; a<strlen (EE); a++)
425               {
426                 matrix2[toupper(EE[a])-'A'][toupper(EE[a])-'A']=v;
427                 matrix2[tolower(EE[a])-'A'][toupper(EE[a])-'A']=v;
428                 matrix2[toupper(EE[a])-'A'][tolower(EE[a])-'A']=v;
429                 matrix2[tolower(EE[a])-'A'][tolower(EE[a])-'A']=v;
430               }
431           }
432         
433         /*Correct for RNA: Add the U cost*/
434         for (b=0; b<l; b++)
435           {
436             
437             matrix2['U'-'A'][toupper(AA[b])-'A']=matrix2['T'-'A'][toupper(AA[b])-'A'];
438             matrix2[toupper(AA[b])-'A']['U'-'A']=matrix2[toupper(AA[b])-'A']['T'-'A'];
439             matrix2['U'-'A'][toupper(AA[b])-'A']=matrix2['t'-'A'][tolower(AA[b])-'A'];
440             matrix2[tolower(AA[b])-'A']['u'-'A']=matrix2[tolower(AA[b])-'A']['t'-'A'];
441                                         
442           }
443         matrix2['U'-'A']['U'-'A']=matrix2['T'-'A']['T'-'A'];
444         matrix2['u'-'A']['U'-'A']=matrix2['t'-'A']['T'-'A'];
445         matrix2['U'-'A']['u'-'A']=matrix2['T'-'A']['t'-'A'];
446         matrix2['u'-'A']['u'-'A']=matrix2['t'-'A']['t'-'A'];
447         
448         free_int (matrice, -1);
449         return (matrix2);
450
451         }
452
453 void display_matrix (int **mat)
454 {
455   int a, b;
456   
457   for ( a=0; a< 26; a++)
458     {
459       fprintf ( stderr, "\n%c ", a+'a');
460       for ( b=0; b< 26; b++)
461         fprintf ( stderr, " %2d", mat[a][b]);
462     }
463 }
464
465 int **neg_matrix2pos_matrix ( int **matrix)
466     {
467       int b,c,l, min, max;
468       char AA[]="abcdefghiklmnpqrstvwxyzABCDEFGHIKLMNPQRSTVWXYZ";
469       l=strlen(AA);
470       min=max=matrix[AA[0]-'A'][AA[0]-'A'];
471       for ( b=0; b<l; b++)
472         for ( c=0; c<l; c++)
473           {
474             min=(matrix[AA[b]-'A'][AA[c]-'A']<min)?matrix[AA[b]-'A'][AA[c]-'A']:min;
475             max=(matrix[AA[b]-'A'][AA[c]-'A']<max)?matrix[AA[b]-'A'][AA[c]-'A']:max;               
476           }
477       if (min>0)return matrix;
478       else
479          {
480            for ( b=0; b<l; b++)
481              for ( c=0; c<l; c++)
482                {
483                  matrix[b][c]=matrix[b][c]-min;
484                }
485          }
486       return matrix;
487     }
488
489       
490
491 /*****************************************************************/
492 void get_rgb_values ( int val, Color *C)
493      {
494
495        /*Colors can be modified, see the definition of 
496          COLOR_FILE in prgogrammes_define.h
497        */
498        
499      static char **html_code;
500      static float **ps_code;
501      int class, n, c;
502      float *r, *g, *b;
503      FILE *fp;
504
505
506
507      if ( !html_code)
508        {
509          html_code=declare_char(10, 10);
510          ps_code=declare_float  (10, 3);
511          
512          n=0;
513          /*0*/
514          sprintf (html_code[n], "#6666FF");
515          ps_code[n][0]=0.4;
516          ps_code[n][1]=0.4;
517          ps_code[n][2]=1;
518          n++;
519          /*1*/
520          sprintf (html_code[n], "#00FF00");
521          ps_code[n][0]=0.6;
522          ps_code[n][1]=1;
523          ps_code[n][2]=0;
524          n++;
525
526          /*2*/
527          sprintf (html_code[n], "#66FF00");
528          ps_code[n][0]=0.8;
529          ps_code[n][1]=1;
530          ps_code[n][2]=0;
531          n++;
532            
533          /*3*/
534          sprintf (html_code[n], "#CCFF00");
535          ps_code[n][0]=1.0;
536          ps_code[n][1]=1.0;
537          ps_code[n][2]=0;
538          n++;
539            
540          /*4*/
541          sprintf (html_code[n], "#FFFF00");
542          ps_code[n][0]=1;
543          ps_code[n][1]=0.85;
544          ps_code[n][2]=0;
545          n++;
546            
547          /*5*/
548          sprintf (html_code[n], "#FFCC00");
549          ps_code[n][0]=1;
550          ps_code[n][1]=0.7;
551          ps_code[n][2]=0;
552          n++;
553            
554          /*6*/
555          sprintf (html_code[n], "#FF9900");
556          ps_code[n][0]=1;
557          ps_code[n][1]=0.6;
558          ps_code[n][2]=0;
559          n++;
560            
561          /*7*/
562          sprintf (html_code[n], "#FF6600");
563          ps_code[n][0]=1;
564          ps_code[n][1]=0.4;
565          ps_code[n][2]=0;
566          n++;
567          
568          /*8*/
569          sprintf (html_code[n], "#FF3300");
570          ps_code[n][0]=1;
571          ps_code[n][1]=0.2;
572          ps_code[n][2]=0;
573          n++;
574          
575            
576          /*9*/
577          sprintf (html_code[n], "#FF2000");
578          ps_code[n][0]=1;
579          ps_code[n][1]=0;
580          ps_code[n][2]=0;
581          n++;
582          
583          
584          
585          
586
587          if ( check_file_exists(COLOR_FILE))
588            {
589              fp=vfopen( COLOR_FILE, "r");
590              
591              while ((c=fgetc(fp))!='*');
592              while ((c=fgetc(fp))!='\n');
593              
594              c=0;
595              while ((c=fgetc(fp))!=EOF)
596                {
597                  ungetc(c, fp);
598                  if ( fscanf (fp, "%d", &class)==0)break;
599                  fscanf (fp, "%s %f %f %f", html_code[class], &ps_code[class][0], &ps_code[class][1],&ps_code[class][2]);
600                  while ((c=fgetc(fp))!='\n' && c!=EOF);
601                  if ( c==EOF)ungetc(c, fp);
602                }
603              vfclose(fp);
604            }
605        }
606      
607
608
609      /*Conversions*/
610      if ( val==10)val--;
611        
612      r=&C->r;
613      g=&C->g;
614      b=&C->b;
615
616      if ( val==10)val--;
617      sprintf ( C->html_color_class, "value%d",val); 
618      
619      
620      if (val<=9 && val>=0)
621        {
622
623          sprintf ( C->html_color, "%s", html_code[val]);
624          r[0]=ps_code[val][0];
625          g[0]=ps_code[val][1];
626          b[0]=ps_code[val][2];
627        }
628
629      else if (val==DEFAULT_COLOR || val==NO_COLOR_RESIDUE || val==NO_COLOR_GAP || (val>'A' && val<'z'))
630        {
631         C->html_color[0]='\0';
632         sprintf ( C->html_color_class, "valuedefault");
633         r[0]=1.;
634         g[0]=1;
635         b[0]=1;
636         
637         } 
638      else if (val==GAP_COLOR)
639        {
640         C->html_color[0]='\0';
641         sprintf ( C->html_color_class, "valuegap");
642         r[0]=1.;
643         g[0]=1;
644         b[0]=1; 
645         } 
646      else if (val==INK_COLOR )
647         {
648         sprintf ( C->html_color, "000000");
649         sprintf ( C->html_color_class, "valueink");
650         r[0]=0.;
651         g[0]=0;
652         b[0]=0;
653         }
654      return;
655
656     
657      }
658 int output_color_format ( Alignment *B,Alignment *Sin,char *name, \
659 FILE_format *(*vfopen_format)          ( char *),\
660 FILE_format *(*print_format_string)    ( char * ,Color *, Color *, FILE_format*),\
661 FILE_format *(*print_format_char)      ( int    ,Color *, Color *, FILE_format*),\
662 void         (*get_rgb_values_format)  ( int    ,Color *),\
663 FILE_format* (*vfclose_format)         ( FILE_format *))
664     {
665     int a, b, c;
666     int max_name_len=15;
667     int max_len=0;
668     char *buf2, *buf3;
669     
670     static char *buf;
671     int s;
672     int *n_residues;
673     static FILE_format *fps;
674     Color *ink;
675     Color *box_c;
676     Color *white;
677     Alignment *S;
678
679     
680     S=copy_aln (B, NULL);
681     
682     buf2=vcalloc (Sin->len_aln+1, sizeof (char));
683     buf3=vcalloc (  B->len_aln+1, sizeof (char));
684     for ( a=0; a<B->nseq; a++)
685       {
686         int i,n, r;
687         
688         i=name_is_in_list ( B->name[a], Sin->name, Sin->nseq, -1);
689         if (i==-1)continue;
690         sprintf (buf2, "%s", Sin->seq_al[i]);ungap(buf2);
691         sprintf (buf3, "%s", S->seq_al[a]);ungap(buf3);
692         
693         if ( strlen (buf2) !=strlen(buf3))
694           {
695             
696             fprintf ( stderr, "\nERROR: Incompatible cache ON sEQ: %s\n", S->name[a]);
697             fprintf ( stderr, "\n%s\n%s", buf2, buf3); 
698             fprintf ( stderr, "\n\n%s\n%s", Sin->seq_al[i],S->seq_al[a]); exit (EXIT_FAILURE);
699           }
700         
701         for (n=0,b=0;b<B->len_aln; b++)
702           {
703             r=S->seq_al[a][b];
704             if (!is_gap(r))
705               {
706                 S->seq_al[a][b]=buf2[n++];
707               }
708           }
709       }
710
711     S=aln2number(S);
712     vfree (buf2);
713     
714     box_c=vcalloc ( 1, sizeof (Color));    
715     get_rgb_values_format (DEFAULT_COLOR, (white=vcalloc ( 1, sizeof (Color))));        
716     get_rgb_values_format (INK_COLOR,     (ink  =vcalloc ( 1, sizeof (Color))));
717
718     n_residues=vcalloc ( B->nseq+1, sizeof (int));
719     for ( a=0; a<B->nseq; a++)n_residues[a]=B->order[a][1];
720
721     fps=vfopen_format( name);      
722     if ( buf==NULL)
723         {
724         buf=vcalloc (10000, sizeof (int));
725         }
726
727     if ( max_len==0)
728         {
729         for ( a=0; a< B->nseq; a++)
730             {if ( strlen (B->name[a])>max_len)
731                 max_len= strlen ( (B->name[a]));
732             }
733         }       
734     if ( max_len>max_name_len)max_len=max_name_len;
735     
736    sprintf (buf, "\n%s, %s(%s)\n%s\n",PROGRAM,VERSION,DATE, AUTHOR);     
737    fps=print_format_string ( buf,white, ink, fps);
738
739    fps=print_format_string ( "\n\n",white,ink, fps);
740    
741    fps->line-=max_len;
742    fps->line=fps->line-fps->line%3;
743    
744   
745    
746    
747    for (a=0; a<B->len_aln; a+=fps->line)
748            {       
749            
750            if ( (fps->n_line+(B->nseq+4))>fps->max_line_ppage && !((B->nseq+4)>fps->max_line_ppage))
751                  {
752                  fps=print_format_char ( fps->eop,white, ink, fps);             
753                  }
754           
755            for (b=0; b<=B->nseq; b++)
756              {
757                sprintf (buf,"%-*.*s ",max_len+2, max_len,(b==B->nseq)?"":S->name[b]);
758                fps=print_format_string ( buf,white, ink, fps);
759                if(B->output_res_num)
760                  {
761                    sprintf (buf, " %4d ", n_residues[b]+1);
762                    fps=print_format_string ( buf,white, ink, fps);
763                  }
764                
765                for (fps->in_seq=1,c=a;c<a+fps->line && c<B->len_aln;c++)
766                  {
767                    if (b==B->nseq)
768                      {
769                        n_residues[b]++;
770                        get_rgb_values_format (DEFAULT_COLOR,box_c);
771                        s=analyse_aln_column ( B, c);
772                      }
773                    else
774                      {
775                        n_residues[b]+=!is_gap(B->seq_al[b][c]);
776                        s=B->seq_al[b][c]; 
777                        if (!is_gap(s) && S->seq_al[b][c]!=NO_COLOR_RESIDUE )
778                          {
779                            get_rgb_values_format ( S->seq_al[b][c], box_c);                                             
780                          }
781                        else
782                          {
783                            get_rgb_values_format (GAP_COLOR, box_c);                    
784                          }
785                      }
786                 fps=print_format_char ( s,box_c, ink,fps);
787                 }
788               fps->in_seq=0;
789
790                if(B->output_res_num)
791                  {
792                  sprintf (buf, " %4d ", n_residues[b]);
793                  fps=print_format_string ( buf,white, ink, fps);
794                  }
795
796               fps=print_format_char ( '\n', white, ink, fps);         
797              
798              }
799             fps=print_format_string ( "\n\n",white, ink, fps);
800             }
801     fps=print_format_string ( "\n\n\n",white, ink,fps);
802     
803     
804     vfclose_format( fps);
805     free_aln (S);
806     vfree (n_residues);
807     return 1;
808
809     }
810
811 int output_reliability_format ( Alignment *B,Alignment *S,char *name, \
812 FILE_format *(*vfopen_format)          ( char *),\
813 FILE_format *(*print_format_string)    ( char * ,Color *, Color *, FILE_format*),\
814 FILE_format *(*print_format_char)      ( int    ,Color *, Color *, FILE_format*),\
815 void         (*get_rgb_values_format)  ( int    ,Color *),\
816 FILE_format* (*vfclose_format)         ( FILE_format *))
817     {
818     int a, b, c,l;
819     int max_name_len=15;
820     int max_len=0;
821     static char *buf,*buf2;
822     int s;
823     static FILE_format *fps;
824     Color *ink;
825     Color *box_c;
826     Color *white;
827     int *n_residues;
828    
829
830     box_c=vcalloc ( 1, sizeof (Color));    
831     get_rgb_values_format (DEFAULT_COLOR, (white=vcalloc ( 1, sizeof (Color))));        
832     get_rgb_values_format (INK_COLOR,     (ink  =vcalloc ( 1, sizeof (Color))));
833     
834     n_residues=vcalloc ( B->nseq+1, sizeof (int));
835     for ( a=0; a<B->nseq; a++)n_residues[a]=B->order[a][1];
836
837
838     fps=vfopen_format( name);      
839     if ( buf==NULL)
840         {
841         buf=vcalloc (10000, sizeof (int));
842         buf2=vcalloc (10000, sizeof (int));
843         }
844
845     if ( max_len==0)
846         {
847         for ( a=0; a< B->nseq; a++)
848             {if ( strlen (B->name[a])>max_len)
849                 max_len= strlen ( (B->name[a]));
850             }
851         }
852
853     if ( vfopen_format==vfopen_ascii)
854       {
855         fps->line+= max_len;
856       }
857     else if ( max_len>max_name_len)max_len=max_name_len;
858     
859     
860     
861     sprintf (buf, "%s, %s(%s)\n%s\nCPU TIME:%d sec.\n%s",PROGRAM,VERSION,DATE, AUTHOR,  (B->cpu+get_time())/1000, (S->generic_comment)?S->generic_comment:"");     
862     fps=print_format_string ( buf,white, ink, fps);
863     sprintf (buf, "SCORE=%d\n*\n", S->score_aln);
864     fps=print_format_string ( buf,white, ink, fps);
865     
866    sprintf ( buf2, " BAD AVG GOOD");
867    l=strlen(buf2);
868    get_rgb_values_format ( DEFAULT_COLOR, box_c);
869    fps=print_format_char ( buf2[0],box_c, ink, fps);
870    for ( a=1; a<l-1; a++)
871         {
872         get_rgb_values_format ( MIN(9,a-1), box_c);
873         fps=print_format_char ( buf2[a],box_c,ink,fps);
874         }
875    fps=print_format_char ( buf2[a], box_c, ink, fps);
876    
877    
878    fps=print_format_string ( "\n*\n",white,ink, fps);
879    
880    for ( a=0;S->score_seq && a< B->nseq; a++)
881        {
882        get_rgb_values_format (S->score_seq[a]/10, box_c);
883        sprintf ( buf, "%-*.*s ", max_len+2,max_len, S->name[a]);
884        fps=print_format_string ( buf,box_c, ink,fps);
885        sprintf ( buf, ": %3d\n", S->score_seq[a]);
886        fps=print_format_string ( buf,white, ink,fps);
887        }
888    //Print the Consensus score
889    get_rgb_values_format (S->score_aln/10, box_c);
890    sprintf ( buf, "%-*.*s ", max_len+2,max_len, S->name[S->nseq]);
891    fps=print_format_string ( buf,box_c, ink,fps);
892    sprintf ( buf, ": %3d\n", S->score_aln);
893    fps=print_format_string ( buf,white, ink,fps);
894    
895    fps=print_format_string ( "\n",white, ink,fps);
896
897
898    
899    fps->line-=max_len;
900    fps->line=fps->line-(fps->line%3);
901    
902    for (a=0; a<B->len_aln; a+=fps->line)
903            {       
904            
905            if ( (fps->n_line+(B->nseq+4))>fps->max_line_ppage && !((B->nseq+4)>fps->max_line_ppage))
906                  {
907                  fps=print_format_char ( fps->eop,white, ink, fps);             
908                  }
909           
910            for (b=0; b<=S->nseq; b++)
911              {
912              if ( b==S->nseq && print_format_string !=print_ascii_string) fps=print_format_string ( "\n",white, ink, fps);
913              sprintf (buf,"%-*.*s ",max_len+2,max_len,S->name[b]);
914              fps=print_format_string ( buf,white, ink, fps);
915              if(B->output_res_num)
916                  {
917                  sprintf (buf, " %4d ", n_residues[b]+1);
918                  fps=print_format_string ( buf,white, ink, fps);
919                  }
920              
921              for (fps->in_seq=1,c=a;c<a+fps->line && c<B->len_aln;c++)
922                 {
923                 if (b==S->nseq)
924                    {
925                      
926                    if (S->score_seq)
927                      {
928                        int s;
929                        s=S->seq_al[b][c];
930                        if ( s>='0' && s<='9')s-='0';
931                        get_rgb_values_format (s,box_c);
932                      }
933                    else get_rgb_values_format (DEFAULT_COLOR,box_c);
934                    n_residues[b]++;
935                    s=analyse_aln_column ( B, c);
936                    }
937                 else
938                    {
939                    n_residues[b]+=!is_gap(B->seq_al[b][c]);
940                    //s=toupper(B->seq_al[b][c]);
941                    s=B->seq_al[b][c];
942                    if (!is_gap(s) && S->seq_al[b][c]!=NO_COLOR_RESIDUE )
943                         {
944                         get_rgb_values_format ( S->seq_al[b][c], box_c);                                                
945                         
946                         }
947                    else
948                         {
949                         get_rgb_values_format (GAP_COLOR, box_c);                       
950                         
951                         }
952                    
953                    }
954                 fps=print_format_char ( s,box_c, ink,fps);
955                 }
956               fps->in_seq=0;
957
958               if(B->output_res_num)
959                  {
960                  sprintf (buf, " %4d ",n_residues[b]);
961                  fps=print_format_string ( buf,white, ink, fps);
962                  }
963
964               fps=print_format_char ( '\n', white, ink, fps);         
965              
966              }
967             fps=print_format_string ( "\n\n",white, ink, fps);
968             }
969     fps=print_format_string ( "\n\n\n",white, ink,fps);
970     vfclose_format( fps);
971     return 1;
972
973     }
974
975
976 /*****************************************************************************/
977 /*                       PDF         FUNCTIONS                               */
978 /*                                                                           */
979 /*****************************************************************************/
980 int       output_color_pdf    ( Alignment *B,Alignment *S, char *name)
981       {
982       char *tmp_name;
983       char command[LONG_STRING];
984
985       
986 #ifndef PS2PDF 
987       fprintf (stderr, "\nPDF FORMAT IS NOT SUPPORTED: INSTALL THE PROGRAM PS2PDF\n");
988       myexit (EXIT_FAILURE);
989 #else
990       tmp_name=vtmpnam(NULL);
991       
992       output_color_ps (B, S, tmp_name);
993       sprintf ( command, "%s %s %s", PS2PDF, tmp_name, name);
994       my_system  ( command); 
995       vremove  ( tmp_name); 
996 #endif      
997       
998       
999       return 1;
1000       }
1001 int       output_reliability_pdf    ( Alignment *B,Alignment *S, char *name)
1002       {
1003       char *tmp_name;
1004       char command[LONG_STRING];
1005
1006       
1007
1008 #ifndef PS2PDF 
1009       fprintf (stderr, "\nPDF FORMAT IS NOT SUPPORTED: INSTALL THE PROGRAM PS2PDF\n");
1010       myexit (EXIT_FAILURE);
1011 #else
1012       tmp_name=vtmpnam(NULL);
1013       
1014       output_reliability_ps (B, S, tmp_name);
1015       sprintf ( command, "%s %s %s", PS2PDF, tmp_name, name);
1016       my_system  ( command); 
1017       vremove  ( tmp_name); 
1018 #endif      
1019       
1020       
1021       return 1;
1022       }
1023 /*****************************************************************************/
1024 /*                       POST SCRIPT FUNCTIONS                               */
1025 /*                                                                           */
1026 /*****************************************************************************/
1027 int       output_color_ps     ( Alignment *B,Alignment *S, char *name)
1028       {
1029       output_color_format (B, S, name, vfopen_ps,print_ps_string,print_ps_char,get_rgb_values_ps, vfclose_ps);
1030       return 1;
1031       }
1032 int       output_reliability_ps     ( Alignment *B,Alignment *S, char *name)
1033       {
1034       output_reliability_format (B, S, name, vfopen_ps,print_ps_string,print_ps_char,get_rgb_values_ps, vfclose_ps);
1035       return 1;
1036       }
1037 FILE_format *print_ps_string( char *s, Color *box, Color *ink, FILE_format *fps)
1038       {
1039       int l;
1040       int a;
1041       
1042       l=strlen (s);
1043       
1044       for ( a=0; a< l; a++)
1045           {
1046           fps=print_ps_char (s[a], box, ink, fps);
1047           }
1048       return fps;
1049       }
1050       
1051
1052 FILE_format * print_ps_char ( int c, Color *box, Color *ink, FILE_format *f)
1053        {
1054        
1055        int ch;
1056        int cw;
1057
1058        ch=f->font+3;
1059        cw=f->font-2;
1060         
1061        if ( c=='(' || c==')')return f;
1062        else if (c!='\n' && c!=f->eop)
1063           {
1064           fprintf(f->fp,"%d %d moveto\n", f->x,f->y);
1065           fprintf(f->fp,"0 %d rlineto\n%d 0 rlineto\n0 -%d rlineto\nclosepath\n",ch,cw,ch   );
1066           fprintf(f->fp,"%3.1f %3.1f %3.1f setrgbcolor\nfill\n%3.1f %3.1f %3.1f setrgbcolor\n", box->r,box->g,box->b, ink->r, ink->g, ink->b);
1067           fprintf(f->fp,"%d %d moveto\n(%c) show\n", f->x+1,f->y+3, c);
1068           
1069           f->x+=cw;
1070           }
1071        else 
1072           {
1073           f->n_line++;
1074           if ( f->n_line==f->max_line_ppage || c==f->eop)
1075              {
1076              
1077              f->n_line=0;
1078              f->x=f->x0;
1079              f->y=f->y0;
1080              fprintf(f->fp,"showpage\n");
1081              f->n_pages++;
1082              fprintf ( f->fp, "%c%cPage:  %d %d\n",'%', '%', f->n_pages, f->n_pages);       
1083              }
1084           else
1085              {
1086              f->x=f->x0;
1087              f->y-=ch;    
1088              }
1089           }
1090        return f;
1091        }
1092 void get_rgb_values_ps ( int val, Color *C)
1093      {
1094      get_rgb_values ( val, C);
1095      }
1096
1097
1098
1099 FILE_format* vfopen_ps ( char *name)
1100       {
1101       FILE_format*fps;
1102
1103       fps=vcalloc ( 1, sizeof ( FILE_format));
1104       fps->font=9;
1105       fps->max_line_ppage=60;
1106       fps->line=get_msa_line_length (0, 0);/*N char per line*/
1107       fps->x0=15;
1108       fps->y0=750;
1109       fps->eop='^';
1110       
1111       fps->fp=vfopen ( name, "w");
1112       fprintf(fps->fp,"%%!PS-Adobe-2.0\n/Courier findfont\n%d scalefont\nsetfont\n",fps->font);
1113       fprintf(fps->fp, "%%%%Pages: (atend)\n");
1114       fprintf(fps->fp,"newpath\n"); 
1115       ++(fps->n_pages);
1116       fprintf (fps->fp, "%%%%Page:  %d %d\n", fps->n_pages, fps->n_pages);
1117       fprintf (fps->fp,"%d %d translate\n",fps->x0, fps->y0);
1118       return fps;
1119       }
1120
1121 FILE_format* vfclose_ps ( FILE_format *fps)
1122       {
1123       
1124       fprintf(fps->fp,"showpage\n");      
1125       fprintf ( fps->fp, "%%%%Pages:  %d\n", fps->n_pages);
1126       fprintf(fps->fp,"%%%%EOF"); 
1127       fprintf(fps->fp,"%%%%\n"); 
1128       vfclose ( fps->fp);
1129       vfree (fps);
1130       return NULL;
1131       }
1132 /*****************************************************************************/
1133 /*                       HTML FUNCTIONS                               */
1134 /*                                                                           */
1135 /*****************************************************************************/
1136
1137 /*****************************************************************************/
1138 //JM_ADD
1139 /*****************************************************************************/
1140 void output_hit_matrix(char *fileName, float **ffpHitScoreMatrix, int nl)
1141 {
1142         int i, j;
1143         FILE *fp;
1144
1145         fp=vfopen(fileName, "w");
1146         for(i = 0; i < nl; i++)
1147         {
1148                 for(j = 0; j < i; j++)
1149                         fprintf(fp, "%6.2f ", ffpHitScoreMatrix[j][i-j]);
1150                 for(j = i; j < nl; j++)
1151                         fprintf(fp, "%6.2f ", ffpHitScoreMatrix[i][j-i]);
1152                 fprintf(fp, "\n");
1153         }
1154         vfclose(fp);
1155 }
1156
1157 int output_hit_color_html (Alignment *B, float **ffPScoreTable, int nl, char *name)
1158 {
1159    output_hit_color_format (B, ffPScoreTable, nl, name, vfopen_html,print_html_string,print_html_char,get_rgb_values_html, vfclose_html);
1160    return 1;
1161 }
1162
1163 int output_hit_color_format (Alignment *B, float **ffPScoreTable, int nl, char *name, \
1164 FILE_format *(*vfopen_format)          ( char *),\
1165 FILE_format *(*print_format_string)    ( char * ,Color *, Color *, FILE_format*),\
1166 FILE_format *(*print_format_char)      ( int    ,Color *, Color *, FILE_format*),\
1167 void         (*get_rgb_values_format)  ( int    ,Color *),\
1168 FILE_format* (*vfclose_format)         ( FILE_format *))
1169 {
1170         int a, b;
1171         int max_name_len=15;
1172         int max_len=0;
1173     
1174         static char *buf;
1175         static FILE_format *fps;
1176         Color *ink;
1177         Color *box_c;
1178         Color *white;
1179
1180         box_c=vcalloc ( 1, sizeof (Color));    
1181         get_rgb_values_format (DEFAULT_COLOR, (white=vcalloc ( 1, sizeof (Color))));    
1182         get_rgb_values_format (INK_COLOR,     (ink  =vcalloc ( 1, sizeof (Color))));
1183
1184         if ( max_len==0)
1185         {
1186                 for ( a=0; a< B->nseq; a++)
1187                 {
1188                         if ( strlen (B->name[a])>max_len)
1189                                 max_len= strlen ( (B->name[a]));
1190                 }
1191         }
1192         if ( max_len>max_name_len)max_len=max_name_len;
1193
1194         if ( buf==NULL)
1195                 buf=vcalloc (10000, sizeof (int));
1196         int iEmptyChr = 32; //SPACE ASCIICODE
1197         int iColorValue;
1198         fps=vfopen_format(name);
1199         for (a=0; a < nl; a++)
1200         {          
1201                 sprintf (buf,"%*d ", max_len+2, a);
1202                 fps=print_format_string ( buf,white, ink, fps);
1203                 for(b = 0; b < a; b++)
1204                 {
1205                       iColorValue = (int)((ffPScoreTable[b][a-b]*9)/100);
1206                       get_rgb_values_format (iColorValue, box_c);
1207                       fps=print_format_char (iEmptyChr,box_c, ink,fps);
1208                 }
1209                 for(b = a; b < nl; b++)
1210                 {
1211                       iColorValue = (int)((ffPScoreTable[a][b-a]*9)/100);
1212                       get_rgb_values_format (iColorValue, box_c);
1213                       fps=print_format_char (iEmptyChr,box_c, ink,fps);
1214                 }
1215               fps=print_format_char ('\n', white, ink, fps);
1216         }
1217         vfclose_format(fps);
1218         vfree(buf);
1219         vfree(box_c);
1220         return 1;
1221 }
1222
1223 /*****************************************************************************/
1224
1225 int       output_color_html     ( Alignment *B,Alignment *S, char *name)
1226       {
1227       output_color_format (B, S, name, vfopen_html,print_html_string,print_html_char,get_rgb_values_html, vfclose_html);
1228       return 1;
1229       }
1230 int       output_reliability_html     ( Alignment *B,Alignment *S, char *name)
1231       {
1232       output_reliability_format (B, S, name, vfopen_html,print_html_string,print_html_char,get_rgb_values_html, vfclose_html);
1233       return 1;
1234       }
1235 FILE_format *print_html_string( char *s, Color *box, Color *ink, FILE_format *fhtml)
1236       {
1237       int l;
1238       int a;
1239       
1240       l=strlen (s);
1241       
1242       for ( a=0; a< l; a++)
1243           {
1244           fhtml=print_html_char (s[a], box, ink, fhtml);
1245           }
1246       fhtml=print_html_char (CLOSE_HTML_SPAN,NULL,NULL,fhtml);
1247       return fhtml;
1248       }
1249       
1250
1251 FILE_format * print_html_char ( int c, Color *box, Color *ink, FILE_format *f)
1252        {
1253        char html_color[100];
1254        int in_span, new_color;
1255        char string[1000];
1256
1257
1258         if (c==CLOSE_HTML_SPAN)
1259          {
1260            if (f->in_html_span)fprintf ( f->fp, "</span>");
1261            f->in_html_span=0;
1262            return f;
1263          }
1264         
1265         
1266        in_span=f->in_html_span;
1267        new_color=1-(strm (box->html_color_class, f->previous_html_color));
1268      
1269        
1270  
1271        sprintf (f->previous_html_color, "%s", box->html_color_class);
1272        sprintf ( html_color, "class=%s", box->html_color_class);
1273
1274        
1275        if ( c!=' ')sprintf ( string, "%c", c);
1276        else sprintf ( string, "&nbsp;");
1277        
1278        if ( !in_span &&                  c!='\n' && c!=f->eop)
1279           {
1280           fprintf ( f->fp, "<span %s>%s",html_color,string );
1281           f->in_html_span=1;
1282           }
1283        else if (in_span && !new_color && c!='\n' && c!=f->eop)
1284           {
1285          
1286           fprintf ( f->fp, "%s",string);
1287           }
1288        else if (in_span &&  new_color && c!='\n' && c!=f->eop)
1289           {
1290           fprintf ( f->fp, "</span><span %s>%s",html_color,string);
1291           }        
1292        else if ( c=='\n')
1293           {
1294           if ( f->in_html_span)fprintf ( f->fp, "</span>");
1295           fprintf ( f->fp, "<br>");
1296           sprintf ( f->previous_html_color, "no_color_set");
1297           f->in_html_span=0;
1298           f->n_line++;
1299           }
1300        
1301        
1302        
1303        
1304      
1305        return f;
1306        }
1307       
1308 void get_rgb_values_html ( int val, Color *C)
1309      {
1310      get_rgb_values ( val, C);
1311      }
1312
1313 FILE_format* vfopen_html ( char *name)
1314       {
1315       FILE_format*fhtml;
1316       Color *color;
1317       int a;
1318
1319       color=vcalloc ( 1, sizeof (Color));
1320
1321       fhtml=vcalloc ( 1, sizeof ( FILE_format));
1322       fhtml->font=11;
1323       fhtml->max_line_ppage=100000;
1324       fhtml->line=get_msa_line_length (0, 0);/*N char per line*/
1325       fhtml->x0=15;
1326       fhtml->y0=800;
1327       fhtml->eop='^';
1328       sprintf ( fhtml->previous_html_color, "no_value_set");
1329       fhtml->fp=vfopen ( name, "w");
1330
1331       fprintf(fhtml->fp,"<html>\n<style>\n");
1332      
1333       fprintf(fhtml->fp,"SPAN { font-family: courier new, courier-new, courier; font-weight: bold; font-size: %dpt;}\n", fhtml->font);
1334       fprintf(fhtml->fp,"SPAN { line-height:100%%}\n");
1335       fprintf(fhtml->fp,"SPAN { white-space: pre}\n");
1336       
1337       for ( a=0; a< 10; a++)
1338           {
1339           get_rgb_values_html ( a, color);    
1340           if ( !strm (color->html_color, ""))fprintf (fhtml->fp, "SPAN.%s {background: %s}\n", color->html_color_class, color->html_color );
1341           else fprintf (fhtml->fp, "SPAN.%s {}\n", color->html_color_class );
1342           }
1343       get_rgb_values_html (DEFAULT_COLOR, color);
1344       if ( !strm (color->html_color, ""))fprintf (fhtml->fp, "SPAN.%s {background: %s}\n", color->html_color_class, color->html_color );
1345       else fprintf (fhtml->fp, "SPAN.%s {}\n", color->html_color_class );
1346       
1347       get_rgb_values_html (GAP_COLOR, color);
1348       if ( !strm (color->html_color, ""))fprintf (fhtml->fp, "SPAN.%s {background: %s}\n", color->html_color_class, color->html_color );
1349       else fprintf (fhtml->fp, "SPAN.%s {}\n", color->html_color_class );
1350        
1351       get_rgb_values_html (INK_COLOR, color);
1352       if ( !strm (color->html_color, ""))fprintf (fhtml->fp, "SPAN.%s {background: %s}\n", color->html_color_class, color->html_color );
1353       else fprintf (fhtml->fp, "SPAN.%s {}\n", color->html_color_class );
1354       
1355       
1356       
1357       fprintf(fhtml->fp,"</style>");
1358       fprintf(fhtml->fp,"<body>");
1359       
1360       return fhtml;
1361       }
1362 FILE_format* vfclose_html ( FILE_format *fhtml)
1363       {
1364       if ( fhtml->in_html_span)fprintf(fhtml->fp,"</span>");
1365       fprintf(fhtml->fp,"</body></html>\n");      
1366       vfclose ( fhtml->fp);
1367       vfree (fhtml);
1368       return NULL;
1369       }
1370 /*****************************************************************************/
1371 /*                       ascii FUNCTIONS                               */
1372 /*                                                                           */
1373 /*****************************************************************************/
1374 int       output_color_ascii     ( Alignment *B,Alignment *S, char *name)
1375       {
1376         output_color_format (B, S, name, vfopen_ascii,print_ascii_string,print_ascii_char,get_rgb_values_ascii, vfclose_ascii);
1377         return 1;
1378       }
1379 int       output_reliability_ascii     ( Alignment *B,Alignment *S, char *name)
1380       {
1381       output_reliability_format (B, S, name, vfopen_ascii,print_ascii_string,print_ascii_char,get_rgb_values_ascii, vfclose_ascii);
1382       return 1;
1383       }
1384
1385 FILE_format *print_ascii_string( char *s, Color *box, Color *ink, FILE_format *fascii)
1386       {
1387       int l;
1388       int a;
1389       
1390       l=strlen (s);
1391       
1392       for ( a=0; a< l; a++)
1393           {
1394           fascii=print_ascii_char (s[a], box, ink, fascii);
1395           }
1396       return fascii;
1397       }
1398       
1399
1400 FILE_format * print_ascii_char ( int c, Color *box, Color *ink, FILE_format *f)
1401        {             
1402        if (box->ascii_value>=0 && f->in_seq)fprintf ( f->fp, "%c", box->ascii_value);
1403        else fprintf ( f->fp, "%c",c);
1404        return f;
1405        }
1406
1407       
1408 void get_rgb_values_ascii ( int val, Color *C)
1409      {
1410      
1411      if ( val==NO_COLOR_RESIDUE)C->ascii_value='-';
1412      else if ( val==NO_COLOR_GAP)C->ascii_value='*';
1413      else if ( val>9)C->ascii_value='#';     
1414      else if ( val>=0 && val<=9) C->ascii_value=val+'0';
1415      else   C->ascii_value=val;
1416      }
1417
1418 FILE_format* vfopen_ascii ( char *name)
1419       {
1420       FILE_format*fascii;
1421       fascii=vcalloc ( 1, sizeof ( FILE_format));
1422       fascii->font=11;
1423       fascii->max_line_ppage=100000;
1424       fascii->line=get_msa_line_length (0,0);/*N char per line*/
1425       fascii->x0=15;
1426       fascii->y0=800;
1427       fascii->eop='^';
1428       fascii->fp=vfopen ( name, "w");
1429      
1430       
1431       return fascii;
1432       }
1433 FILE_format* vfclose_ascii ( FILE_format *fascii)
1434       {
1435        vfclose ( fascii->fp);
1436       vfree (fascii);
1437       return NULL;
1438       }
1439
1440
1441 /*****************************************************************************/
1442 /*                       seq_score output                                    */
1443 /*                                                                           */
1444 /*****************************************************************************/
1445
1446 int       output_seq_reliability_ascii     ( Alignment *B,Alignment *S, char *name)
1447 {
1448   FILE *fp;
1449   int a;
1450   int max_len=0;
1451   for ( a=0; a< B->nseq; a++)
1452     {if ( strlen (B->name[a])>max_len)
1453       max_len= strlen ( (B->name[a]));
1454     }
1455
1456   fp=vfopen ( name, "w");
1457   fprintf ( fp, "ALN_SCORE %d\n", S->score_aln);
1458   for ( a=0; a< S->nseq; a++)fprintf (fp, "SEQ_SCORE %*.*s %3d\n",  max_len+2,max_len,S->name[a],S->score_seq[a]);
1459   vfclose (fp);
1460   
1461   return 1;
1462 }
1463   
1464 /*********************************COPYRIGHT NOTICE**********************************/
1465 /*© Centro de Regulacio Genomica */
1466 /*and */
1467 /*Cedric Notredame */
1468 /*Tue Oct 27 10:12:26 WEST 2009. */
1469 /*All rights reserved.*/
1470 /*This file is part of T-COFFEE.*/
1471 /**/
1472 /*    T-COFFEE is free software; you can redistribute it and/or modify*/
1473 /*    it under the terms of the GNU General Public License as published by*/
1474 /*    the Free Software Foundation; either version 2 of the License, or*/
1475 /*    (at your option) any later version.*/
1476 /**/
1477 /*    T-COFFEE is distributed in the hope that it will be useful,*/
1478 /*    but WITHOUT ANY WARRANTY; without even the implied warranty of*/
1479 /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the*/
1480 /*    GNU General Public License for more details.*/
1481 /**/
1482 /*    You should have received a copy of the GNU General Public License*/
1483 /*    along with Foobar; if not, write to the Free Software*/
1484 /*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA*/
1485 /*...............................................                                                                                      |*/
1486 /*  If you need some more information*/
1487 /*  cedric.notredame@europe.com*/
1488 /*...............................................                                                                                                                                     |*/
1489 /**/
1490 /**/
1491 /*      */
1492 /*********************************COPYRIGHT NOTICE**********************************/