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