959a287e2768f6bdcc2e8823c1119a0fe76e73a7
[jabaws.git] / binaries / src / tcoffee / t_coffee_source / reformat.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <stdarg.h>
5 #include <string.h>
6 #include <ctype.h>
7 #include <time.h>
8 #include "io_lib_header.h"
9 #include "util_lib_header.h"
10 #include "dp_lib_header.h" 
11 #include "define_header.h"
12 #include "dev1_lib_header.h"  //JM_STRAT
13
14 #define ACTION(x) ((n_actions>=(x+1))?action_list[x]:NULL)
15 #define ACTION2(x,y) ((n_actions>=(x+1))?action_list[x]:y)
16 #define ATOI_ACTION(x) ((ACTION(x)!=NULL)?(atoi(ACTION(x))):0)
17
18 /**************************************************************************************************/
19 /*****************************    SEQ_REFORMAT     ******************************************/
20 /**************************************************************************************************/
21 int output_transitions(char *outfile, Alignment *A);
22 static int output_age_matrix ( char *outfile, int val);
23 int SeqGCGCheckSum(char *seq, int len);
24 static Sequence *seq2year ( Sequence *S, int modulo);
25 static Sequence* output_n_pavie_age_channel (Sequence *S, char *name, int n);
26 static Sequence* output_pavie_age_channel (Sequence *S, char *name, int modulo);
27
28 static int output_seq2struc(char *outfile, Alignment *A);
29 void output_conservation_statistics ( char *file, Alignment *A);
30 /**************************************************************************************************/
31 /*****************************    SEQ_REFORMAT     ******************************************/
32 /**************************************************************************************************/
33 int seq_reformat ( int argc, char **in_argv) 
34         {
35         
36         Sequence_data_struc *D1=NULL;
37         Sequence_data_struc *D2=NULL;
38         Sequence_data_struc *D_ST=NULL;
39         Action_data_struc  *RAD;
40         
41         
42         
43         int a, b;
44         
45         char *in_format;
46         char *in2_format;
47         char *out_format;
48         char *in_file;
49         char *in2_file;
50         char *out_file;
51         char *out2_file;
52         char *struc_in_format;
53         char *struc_out_format;
54         char *struc_in_file;
55         char *struc_out_file;
56         char**action_list;
57         char **action;
58         char *rename_file;
59         char *cache;
60         char ***rename_list=NULL;
61         int code=CODE;
62         char **argv;
63         
64         int n_actions=0;
65         int print_format=0;
66         /*INITIALIZATIONS*/
67         
68         RAD=vcalloc ( 1, sizeof ( Action_data_struc));
69         RAD->keep_case=1;
70         declare_name (cache);sprintf ( cache, "use");
71         declare_name(in_file);
72         declare_name(in2_file);
73         declare_name(out_file);
74         declare_name(out2_file);
75         declare_name(struc_in_format);
76         declare_name(struc_out_format);
77         declare_name(RAD->coor_file);
78         
79         declare_name(struc_in_file);
80         declare_name(struc_out_file);
81         declare_name(in_format);
82         declare_name(in2_format);
83         declare_name(out_format);
84         declare_name(rename_file);
85         
86         
87         argv=break_list ( in_argv, &argc, "=;, \n");
88                 
89         action_list=declare_char ( 100, 100);
90
91 /*END INITIALIZATION*/
92
93         addrandinit ( (unsigned long) 500);
94         
95         if ( argc==1 || strm6 ( argv[1], "h", "-h", "help", "-help", "-man", "?"))
96                 {
97                 
98                   fprintf ( stdout, "\n%s (%s,%s,%s [%s])\n",PROGRAM, VERSION,AUTHOR, DATE, URL);
99                 fprintf ( stdout, "\n***********     MINIMUM SYNTAX        *****************");
100                 fprintf ( stdout, "\nseq_reformat -in <in_file> -output <out_format>");
101                 fprintf ( stdout, "\nSome File formats are automatically recognised");
102                 fprintf ( stdout, "\nSee Format section");
103                 fprintf ( stdout, "\n");
104                 fprintf ( stdout, "\n***********        MAIN FLAGS              ******************");
105                 fprintf ( stdout, "\n-in     name........Name of the file read");
106                 
107
108                 fprintf ( stdout, "\n-input  format......Name of the format read, see Input Format Section");
109                 fprintf ( stdout, "\n...................Automatic detection, except for seqs of numbers");
110                 fprintf ( stdout, "\n...................-input number_aln | number_fasta");
111                 fprintf ( stdout, "\n-in2    fname......Second alignment");
112                 fprintf ( stdout, "\n-input2 format.....See -input");
113                 fprintf ( stdout, "\n-exon_boundaries   obj file");
114                 fprintf ( stdout, "\n-out    fname......Output file (defualt is STDOUT");
115                 fprintf ( stdout, "\n-output format.....Output Format, default is fasta_aln");
116                 fprintf ( stdout, "\n-struc_in   name...File containing a coded aln");
117                 fprintf ( stdout, "\n-struc_in_f format.See -input and output format section");
118                 fprintf ( stdout, "\n-struc_out  fname..Name of the output structure");
119                 fprintf ( stdout, "\n-struc_out_f symbol");
120                 fprintf ( stdout, "\n-keep_case=on|off..keep case, On by default");
121                 fprintf ( stdout, "\n-action +ac1 +ac2..See the action section");
122                 fprintf ( stdout, "\n-rename <file>.....Rename the sequences following <file> indications");
123                 fprintf ( stdout, "\n...................File Format: One couple <name1><space><name2>/line");
124                 fprintf ( stdout, "\n...................Rename order <name1> into <name2>");
125                 fprintf ( stdout, "\n...................code file: -output code_name");
126                 fprintf ( stdout, "\n-code   <file>     Rename file <name1> to <name2>");
127                 fprintf ( stdout, "\n-decode <file>     Rename file <name2> to <name1>");
128                 fprintf ( stdout, "\n-no_warning........Suppresses all warnings");
129                 fprintf ( stdout, "\n-cache.............use,ignore,update,local, DirectoryName");
130                 
131                 
132                 fprintf ( stdout, "\n");
133
134                 fprintf ( stdout, "\n***********     REFORMAT ACTIONS               *****************");
135                 fprintf ( stdout, "\n     +Xaction.............Specifies which file undergoes the action");
136                 fprintf ( stdout, "\n     +Xaction.............X=1: -in");
137                 fprintf ( stdout, "\n     +Xaction.............X=2: -in2");
138                 fprintf ( stdout, "\n     +Xaction.............X=3: -struc_in");
139                 fprintf ( stdout, "\n     +name2unique_name....replace duplicated name with name_#");
140                 fprintf ( stdout, "\n     +swap_header........,swapp comments: replace comments/name in 1 by in 2");
141                 fprintf ( stdout, "\n     +swap_lib_header.F...Replace the sequences in the tc_lib (-in) with those in F");
142                 fprintf ( stdout, "\n     .....................F is a legal FASTA file");
143                 
144                 
145                 fprintf ( stdout, "\n     +translate[0-2]......Translate on Frame 0, 1, 2 ");
146                 fprintf ( stdout, "\n     +translate[3]........longuest ORF on direct strand");
147                 fprintf ( stdout, "\n     +translate[4]........longuest ORF on direct+complementary strand");
148                 
149                 
150                 fprintf ( stdout, "\n     +add_scale..<offset>.addscale below aln"); 
151                 
152                 fprintf ( stdout, "\n     +rm_gap n ...........Removes col with n%% gap [n=100]");
153                 fprintf ( stdout, "\n     +rmgap_col SEQ1:SEQ2.Removes column with a gap in SEQ [#] ");
154         
155                 fprintf ( stdout, "\n     +backtranslate.......Random Backtranslation");
156                 fprintf ( stdout, "\n     +complement..........Produces the reverse complement");
157                 
158                 fprintf ( stdout, "\n     +reorder.............Reorders sequences of <in> according to <in2>");
159                 fprintf ( stdout, "\n     .........random......Random_order");
160                 fprintf ( stdout, "\n     .........tree........Tree Order (in2)");
161                 fprintf ( stdout, "\n     +reorder_columns.....Reorders sequences of <in> according to <in2>");
162                 fprintf ( stdout, "\n     .........random......Random_order");
163                 fprintf ( stdout, "\n     .........tree..mode..Tree Order (comuted with mode: sarmat, idmat, blosum62mt...");
164                 fprintf ( stdout, "\n     +aln2random_aln SCR..Randomize the aln, S: swap sequences names");
165                 fprintf ( stdout, "\n     .....................Swap residues within colums");
166                 fprintf ( stdout, "\n     .....................Swap residues across the aln");
167                 fprintf ( stdout, "\n     +aln2sample......N......");
168                 fprintf ( stdout, "\n     +aln2bootstrap...N......");
169                 
170
171                 fprintf ( stdout, "\n     +chain...............Identifies all the intermediate sequences from <-in>");
172                 fprintf ( stdout, "\n     .....................needed to join every sequence pair in <-in2>");
173         
174                 fprintf ( stdout, "\n     +aln2cons  mat_name..Ouputs a consensus sequence");
175                 fprintf ( stdout, "\n     .....................The consensus is determined using mat");
176                 fprintf ( stdout, "\n     .....................By Default, mat=blosum62mt, name=Cons");
177                 fprintf ( stdout, "\n     +aln2resindex........Prints the sequence index of each residue in -in for each -in2 sequence");
178                 fprintf ( stdout, "\n     +collapse_aln <new name> <seq1> <seq2...> | file name");
179                 fprintf ( stdout, "\n     .....................Replaces a group of sequences with its consensus");
180                 fprintf ( stdout, "\n     .....................The replacement sequence is named <new_seq>");
181                 fprintf ( stdout, "\n     .....................List of sequences can be provided via a file");          
182                 fprintf ( stdout, "\n     .....................File:>new_name seq1 seq2 seq3....");
183                 fprintf ( stdout, "\n     +original_seqnos.....Keep original seqnos [SWITCH]");
184                 fprintf ( stdout, "\n     +seqnos..............Print Seqnos [SWITCH]");
185                 fprintf ( stdout, "\n     +code_dna_aln........Undocumented")  ;
186                 fprintf ( stdout, "\n     +grep..[NAME|SEQ|COMMENT]..[KEEP|REMOVE]..[string]......");
187                 fprintf ( stdout, "\n     .....................Keeps or Removes Sequences matching string");
188                 fprintf ( stdout, "\n     +extract_block <seq> <start> <end> | <seq> <pos> |<filename>");
189                 fprintf ( stdout, "\n     .....................Extract column pos OR [start to end[");
190                 fprintf ( stdout, "\n     .....................<filename> Format");
191                 fprintf ( stdout, "\n     .......................seq start end | seq pos");
192                 fprintf ( stdout, "\n     .......................# for comments");
193                 fprintf ( stdout, "\n     .......................! seq offset_value (0 by default)");
194                 fprintf ( stdout, "\n     .....................Can extract as many positions as needed");
195                 fprintf ( stdout, "\n     .....................seq=cons: measure positions on the full aln");
196                 fprintf ( stdout, "\n     +cat_aln.............Concatenates the alignments input via -in and -in2");
197                 fprintf ( stdout, "\n     +cat_aln.............-if no -in2, -in is expected to be a list of alignments to concatenate");
198                 
199                 fprintf ( stdout, "\n     +msalist2cat_pwaln.min..max");
200                 fprintf ( stdout, "\n     .....................extract all pw projections and conctaenates those\n");
201                 fprintf ( stdout, "\n     .....................where id>=min and id<=max\n");
202                 fprintf ( stdout, "\n     .....................min and max can be omitted (min=0, max=100)\n");
203                 
204                 fprintf ( stdout, "\n     +seq2blast <matrix>..gather all possible homologues from NR (EBI BLAST)");
205                 fprintf ( stdout, "\n     +seq2msa <matrix>....makes a standard progressive alignment using matrix");
206                 fprintf ( stdout, "\n     +realign_block <c1> <c2> <pg>");
207                 fprintf ( stdout, "\n     .....................Realign column c1 to c2 (non inc.) with pg)");
208                 fprintf ( stdout, "\n     .....................pg reads fasta and outputs fasta");
209                 fprintf ( stdout, "\n     .....................pg -infile=<infile> -outfile=<outfile>");
210                 fprintf ( stdout, "\n     +extract_seq seq_name (start end seq_name start end...) | filename");
211                 fprintf ( stdout, "\n     .....................seq_name='*': every seq");
212                 fprintf ( stdout, "\n     .....................start='*'   : real start");
213                 fprintf ( stdout, "\n     .....................end='*'     : real end");
214                 fprintf ( stdout, "\n     .....................filename: fasta format");
215                 fprintf ( stdout, "\n     +extract_seq_list name1 name2");
216                 fprintf ( stdout, "\n     .....................Extracts entire sequences");
217                 fprintf ( stdout, "\n     +remove_seq sn1 sn2..Removes sequences sn1, sn2...");
218                 fprintf ( stdout, "\n     +remove_seq empty....Removes empty sequences (gap only)");
219                 fprintf ( stdout, "\n     +remove_seq unique...Remove all multiple occurences except the first");
220                 fprintf ( stdout, "\n     +thread_profile_on_msa <file>");
221                 fprintf ( stdout, "\n     .....................Threads a list of profiles on corresponding seq");
222                 fprintf ( stdout, "\n     .....................File: >seqname _R_ <msa file> [nlines]");
223                 
224                 fprintf ( stdout, "\n     +thread_dna_on_prot_aln");
225                 fprintf ( stdout, "\n     .....................-in DNA.seq and -in2 AA.aln"); 
226                 fprintf ( stdout, "\n     +thread_struc_on_aln");
227                 fprintf ( stdout, "\n     .....................-in structure and -in2 aln"); 
228                 fprintf ( stdout, "\n     +use_cons............Use the consensus for n[SWITCH]");
229                 fprintf ( stdout, "\n     +upper.n|[n1-n2].....n omitted sets everything to upper case");
230                 fprintf ( stdout, "\n     .....................To use n: provide a number_aln via:");
231                 fprintf ( stdout, "\n     .....................-struc_in <number_file> -struc_in_f number_aln");
232                 fprintf ( stdout, "\n     .....................if use_cons is set n, is read on the cons");
233                 fprintf ( stdout, "\n     .....................n: will upper every residue with a value of n in struc_in");
234                 fprintf ( stdout, "\n     .....................[n1-n2]: upper residues between n1 and n2");
235                 fprintf ( stdout, "\n     +lower  n|[n1-n2]....See +upper");
236                 fprintf ( stdout, "\n     +switchcase  n|[n1-n2]See +upper");
237                 fprintf ( stdout, "\n     +color_residue <seq> <pos> <color> | file");
238                 fprintf ( stdout, "\n     .....................File: seq_name pos color");
239                 fprintf ( stdout, "\n     .....................color: 0-9");
240                 fprintf ( stdout, "\n     +edit_residue <seq> <pos> <edit> | file");
241                 fprintf ( stdout, "\n     .....................File: seq_name pos color");
242                 fprintf ( stdout, "\n     .....................edit: upper|lower|symbol");        
243                 
244                 
245                 
246                 fprintf ( stdout, "\n     +keep   n|[n1-n2]....Only keep residues that have a score between n1 and n2");
247                 
248                 fprintf ( stdout, "\n     +invert..............Inverts the sequences: CAT => TAC");
249                 fprintf ( stdout, "\n     +rotate name         Rotate an MSA, names each sequence name_col#");
250                 fprintf ( stdout, "\n     +convert n|[n1-n2] s1 s2 ....");
251                 fprintf ( stdout, "\n     +merge_annotation.... ");
252                 
253                 fprintf ( stdout, "\n     .....................Converts residues with your alignment");
254                 fprintf ( stdout, "\n     .....................similar to upper");
255                 fprintf ( stdout, "\n     .....................s1: ABCDe turns every ABCD into e");
256                 fprintf ( stdout, "\n     .....................s1: #e turns any residue into e");
257                 fprintf ( stdout, "\n     aln2short_aln L C S..Turns sequences into shorter sequences");
258                 fprintf ( stdout, "\n     .....................L: list of residues to keep");
259                 fprintf ( stdout, "\n     .....................S: Size of Streches replaced by symbol C");
260
261                                 
262                 fprintf ( stdout, "\n     +random n l..........Generates N random sequences of len l");
263                 fprintf ( stdout, "\n     .....................You must provide a file with -in");
264                 fprintf ( stdout, "\n     +count n|[n1-n2] s1 s2....");
265                 fprintf ( stdout, "\n     .....................Counts residues with your alignment");
266                 fprintf ( stdout, "\n     .....................similar to convert");
267                 fprintf ( stdout, "\n     +print_format........prints the format name");
268                 fprintf ( stdout, "\n     +keep_name...........Keep the original sequence name on extraction");
269                 
270                 fprintf ( stdout, "\n     +remove_aa pos Ml Ncycle Random_len");
271                 fprintf ( stdout, "\n     .....................Randomly modifies an alignment");
272                 fprintf ( stdout, "\n     .....................pos=0: chosen randomly");
273                 fprintf ( stdout, "\n     .....................MaxLen of the deletions, Ncycle: number of cycles");
274                 fprintf ( stdout, "\n     .....................Random_len: 0 sets the len to maxlen, 1 to a random value");
275                 fprintf ( stdout, "\n     +remove_nuc.x........Remove Position 1, 2 or 3 of every codon"); 
276                 fprintf ( stdout, "\n     +evaluate matrix..gop..gep");
277                 fprintf ( stdout, "\n     .....................Make a similarity evaluation with matrix");
278                 fprintf ( stdout, "\n     .....................use -output=score_ascii, or score_html.");
279                 fprintf ( stdout, "\n     .....................You can filter on the values");
280                 fprintf ( stdout, "\n     +evaluate matrix..gop..gep");
281                 fprintf ( stdout, "\n     .....................Make an SP evaluation with matrix");
282                 fprintf ( stdout, "\n     .....................Uses Natural Gap penalties");
283                 fprintf ( stdout, "\n     .....................gop and gep must be negative");
284                 fprintf ( stdout, "\n     .....................use -output=color_ascii, color_html to get a color display");
285
286                 fprintf ( stdout, "\n.....+evaluate_lat........Make a lateral evaluation with matrix");
287                 fprintf ( stdout, "\n     +msa_weight proc.....Computes weights using the procedure");
288                 fprintf ( stdout, "\nRNA analysis Post Processing___________________________________________________");
289                 fprintf ( stdout, "\n     +aln2alifold.........Turns the MSA into a consensus structure");
290                 fprintf ( stdout, "\n     +add_alifold.........adds an alifold consensus structure");
291
292                 fprintf ( stdout, "\n     +alifold2analyze.mode..mode=stat_cache_list_aln_color_html_ps_usegap");
293                 fprintf ( stdout, "\n     .......................stat: compile Number of compensated mutations");
294                 fprintf ( stdout, "\n     .......................cache: ascii-code compensated mutations on aln");
295                 fprintf ( stdout, "\n     .......................html: color-code compensated mutations on aln");
296                 fprintf ( stdout, "\n     .......................aln: mark compensated mutations on stockholm aln");
297                 fprintf ( stdout, "\n     .......................usegap: do not ignore positions with gaps");
298                 
299                 fprintf ( stdout, "\n     +cmp_RNAfold.........compares the sec struc of in1 and in2 (computes them with alifold if missing)");
300                                 
301                 fprintf ( stdout, "\nMSA Post Processing___________________________________________________");
302                         fprintf ( stdout, "\n     +force_aln filename|seq1 res1 seq2 res2");
303                 fprintf ( stdout, "\n     .....................Forces residue 1 of seq1 to be aligned with res2 of seq 2");
304                 fprintf ( stdout, "\n     .....................In a file, there must be one pair of interaction/line");
305                 fprintf ( stdout, "\n     +sim_filter[_aln_Ix_iy_Cz_cw <seq>");
306                 fprintf ( stdout, "\n     ....................._<unaln or aln>, aln is assumed");
307                 fprintf ( stdout, "\n     ....................._I max identity to seq");
308                 fprintf ( stdout, "\n     ....................._i min identity to seq");
309                 fprintf ( stdout, "\n     ....................._C max cov on seq");
310                 fprintf ( stdout, "\n     ....................._c min cov on seq");
311                 fprintf ( stdout, "\n     +trim[_aln_%%%%50_n111_N50_T_Fn_fS_pS_max_sim] [string2]");
312                 fprintf ( stdout, "\n     ....................._<seq or aln>, aln is assumed");
313                 fprintf ( stdout, "\n     ....................._%%%%<max/min_percent_similarity>");
314                 fprintf ( stdout, "\n     ....................._max Or _min <keep sequences for which sim is the max or the min [Def: _max>");
315                 fprintf ( stdout, "\n     ....................._cov Or _sim Filter according to the coverage [Def: _sim]");
316                 fprintf ( stdout, "\n     ....................._n<max_number_of_sequence>       ");
317                 fprintf ( stdout, "\n     ....................._N<percent_of_sequences_to_keep>");
318                 fprintf ( stdout, "\n     ....................._T Reorder the sequences according to a tree BEFORE triming");
319                 fprintf ( stdout, "\n     ....................._Fn Keep only sequences that have AT LEAST ONE residue aligned");
320                 fprintf ( stdout, "\n     ......................in the n first and n last columns. ");
321                 fprintf ( stdout, "\n     ....................._O<min sim> Remove outlayers that have less than min average sim with other sequences"); 
322                 
323                 fprintf ( stdout, "\n     .....................Keeping Sequences: Sequences provided via -in2 will be kept");
324                 
325                 fprintf ( stdout, "\n     .....................Keeping Sequences: Sequences whose name contains <string> in field fS will be kept");
326                 fprintf ( stdout, "\n     ....................._f<NAME|SEQ|COMMENT> designates a field"); 
327                 fprintf ( stdout, "\n     .....................<string> is a Perl regular expression");
328                 fprintf ( stdout, "\n     +aln2unalign Mode Penalty Threshold");
329                 fprintf ( stdout, "\n     .....................Identifies all the streches less conserved than than the average");
330                 fprintf ( stdout, "\n     .....................Mode: lower|number|unalign Act on all the resiues withs score<Thres");
331                 fprintf ( stdout, "\n     .....................Penalty: FSA penalty align2unalign, Def=90");
332                 fprintf ( stdout, "\n     .....................Threshold: Fraction of unaligned residues(0-9) Def=2");
333                 
334                 fprintf ( stdout, "\n     +clean_cdna..........Undocumented"); 
335                 fprintf ( stdout, "\n     +clean_maln..........Undocumented"); 
336                 fprintf ( stdout, "\nTree Analysis___________________________________________________");
337                 
338         
339                 fprintf ( stdout, "\n     +tree_prune..........Prune the tree -in using the sequences provided via -in2");
340                 fprintf ( stdout, "\n     +tree_cmp............Compares the tree -in and the tree -in2");
341                 fprintf ( stdout, "\n     .....................-in and -in2 can contain different taxons");
342                 fprintf ( stdout, "\n     +tree_scan.P1..P2.....scans alignment <-in> with tree <-in2>)");
343                 fprintf ( stdout, "\n     ......................+tree_scan help to get P1 information");
344                 fprintf ( stdout, "\n     ......................+aln2tree help to get P2 information");
345                 
346                 fprintf ( stdout, "\n     .....................-in and -in2 can contain different taxons");
347                 fprintf ( stdout, "\n     +treelist2groups.N....count all topologies within a list of trees");
348                 fprintf ( stdout, "\n     .....................-in is in fasta format with each name being a newick file");
349                 fprintf ( stdout, "\n     .....................-in2 can be a list of sequences used to trim the trees");
350                 fprintf ( stdout, "\n     ......................N can be used to unresolve the trees with Depth N");
351                 fprintf ( stdout, "\n     +treelist2lti.N.C.....Reports the average stability of each sequence neighborhood");
352                 fprintf ( stdout, "\n     ......................Species can be selected via -in2 [Fasta file with Taxon names]");
353                 fprintf ( stdout, "\n     ......................OR the sequences observed in C% of the files are kept [Def: C=100]");
354                 
355                 
356                 fprintf ( stdout, "\n     +treelist2seq.C.......Reports the species observed in C% of the trees");
357                 fprintf ( stdout, "\n     +treelist2splits......List and counts all the splits in a list of trees");
358                 fprintf ( stdout, "\n     ......................splits can be restricted to a list of sequences provided via -in2");
359                 fprintf ( stdout, "\n     +treelist2dmat.......outputs a distance matrix for a list of trees");
360
361                 fprintf ( stdout, "\n     +tree_compute n s....Computes a tree using the MSA provided with -in");
362                 fprintf ( stdout, "\n     ....................n:0-9, controls the way the MSA is filtered");
363                 fprintf ( stdout, "\n     ....................s:pam250mt|blosum62mt|categories|enthropy");
364                 fprintf ( stdout, "\n     ....................s:controls the column evaluation in MSA");
365                 fprintf ( stdout, "\n     +change_distances.f.f:float, sets all the distances to f in the tree");
366                 fprintf ( stdout, "\n     +change_bootstrap n..:n=0 removes all the bootstrap values");
367                 fprintf ( stdout, "\n     .....................:n!=0 adds a the value n to every node");
368                 fprintf ( stdout, "\n     +tree2dpatree........Replaces tree distances with the minimum %%ID in");
369                 fprintf ( stdout, "\n     .....................the depending subgroup. The ID is measured on an");
370                 fprintf ( stdout, "\n     .....................-in=TREE -in2=ALN");
371                 fprintf ( stdout, "\n     +unroot..............Removes the root in the input tree");
372                 fprintf ( stdout, "\n     +tree2group.N.I.P....Reports all the tree subgroup with at most Nseq");
373                 fprintf ( stdout, "\n     .....................and at min I%% identity. Output format can be read by");
374                 fprintf ( stdout, "\n     .....................collapse_tree. New groups are named P_1, P_2...");               
375                 fprintf ( stdout, "\n     +collapse_tree.F.....Collapses trees. F is either a file or a list");
376                 fprintf ( stdout, "\n     .....................<new name> <seq1> <seq2>...");
377                 fprintf ( stdout, "\n     +aln2tree............Computes a tree");
378                 fprintf ( stdout, "\n     ..ktupN|aln|sarmat   ktupN: match size N to estimate distances");
379                 fprintf ( stdout, "\n     .....................aln: Measures distances on aln");
380                 fprintf ( stdout, "\n     .....................sarmat: expects in to be a SAR matrix of O and I");
381                 fprintf ( stdout, "\n     ..nj | cw............Runs Neighbor Joining OR Cw to compute Tree");
382                 fprintf ( stdout, "\n     ..dpa................Turns the tree into a daptree (+tree2dpatree)");
383                 fprintf ( stdout, "\n     +node_sort..<name>...Sort leafs of tree n1, by node distance");
384                           
385                 
386                 fprintf ( stdout, "\nMatrix Analysis___________________________________________________");
387                 fprintf ( stdout, "\n     +aln2mat_diaa........computes a dinucleotide matrix on a list of aln");
388                 fprintf ( stdout, "\n     +aln2mat.............computes a log odd matrix");
389                 
390                 fprintf ( stdout, "\n     +seq2lat_mat.........computes a transition matrix on seq provided via -in");
391                 
392                 fprintf ( stdout, "\nStructure Analysis___________________________________________________");
393                 fprintf ( stdout, "\n     +struc2contacts.A.B D.Displays in capitals all the residues of A");
394                 fprintf ( stdout, "\n     ......................Less than D Angs from a residue of B");
395                 fprintf ( stdout, "\n     ......................A and B are pdb file, D is a distance in Angs");
396                 fprintf ( stdout, "\n     +seq2contacts.A.D.....Identifies all the residues in contact with ligands");
397                 fprintf ( stdout, "\n     ......................Ligands are in the FASTA header of struc in");
398                 fprintf ( stdout, "\n     ......................>Name _S_ [Target Struc] [Ligand1] [Chain] ...");
399                 fprintf ( stdout, "\n     ......................Output: number_fasta: 0=no contact, 1=ligand 1...");
400                 fprintf ( stdout, "\n     ......................9: residues in contact with more than 1 ligand");
401                 fprintf ( stdout, "\n     ......................Use -output=color_html/ascii to display result");
402                 fprintf ( stdout, "\n     +struc2nb...D.........Display a list of all the residues D appart");
403                 fprintf ( stdout, "\n     +rm_template...V......Removes _[S|G|R]_[template] to sequence names");
404                 fprintf ( stdout, "\n     ......................V: omitted | sequences <=> Output sequences");
405                 fprintf ( stdout, "\n     ......................V: template <=> Output templates");
406                         
407                 fprintf ( stdout, "\n     +add_template.F.......Add _[S|G|R]_[template] to sequence names");
408                 fprintf ( stdout, "\n     ......................F can either be a fasta file or an executable");
409                 fprintf ( stdout, "\n     ......................F: File: >name _S_ template");
410                 fprintf ( stdout, "\n     ......................F: executable: pg -infile=<seq> -outfile=<tagged>");
411                 fprintf ( stdout, "\nMatrix Comparison___________________________________________________");
412                 fprintf ( stdout, "\n    +mat2cmp...............Returns the correlation coefficient between two matrices");
413                 fprintf ( stdout, "\n    .......................-in mat1 -input matrix, -in2 mat2 -input2 matrix");
414                 fprintf ( stdout, "\n***********  INPUT FORMATS: Alignments *****************");
415                 fprintf ( stdout, "\n     AUTOMATIC RECOGNITION");
416                 fprintf ( stdout, "\n     perl_xxx:............. runs xxx onto the input file");
417                 fprintf ( stdout, "\n     xxxx <file> > outfile..xxx reads any formats, outputs fasta");
418                 fprintf ( stdout, "\n     amps_aln       saga_aln      ");
419                 fprintf ( stdout, "\n     clustal_aln    fasta_aln     msf_aln  ");
420                 fprintf ( stdout, "\n     dali_aln       gotoh_aln     pima_aln");
421                 fprintf ( stdout, "\n     dialign_aln    matrix        conc_aln");    
422                 fprintf ( stdout, "\n     NON AUTOMATIC RECOGNITION (use the -input file to specify the format");
423                 fprintf ( stdout, "\n     number_aln     newick_tree");
424                 fprintf ( stdout, "\n");
425                 fprintf ( stdout, "\n***********  INPUT FORMATS: Sequences *****************");
426                 fprintf ( stdout, "\n     fasta_seq      dali_seq       pir_seq");
427                 fprintf ( stdout, "\n     barton_list_tc amps_sd_scores EST_fasta");
428                 fprintf ( stdout, "\n     gor_seq        gor_struc      number_fasta[*]");
429                 fprintf ( stdout, "\n     swissprot      tc_lib         pdb_struc");
430                 fprintf ( stdout, "\n");
431                 fprintf ( stdout, "\n***********  INPUT FORMATS: Structures   *****************");
432                 fprintf ( stdout, "\n    rna_number");
433                 fprintf ( stdout, "\n    alifold");
434                 fprintf ( stdout, "\n***********  OUTPUT FORMATS: Alignments ******************");
435                 fprintf ( stdout, "\n     compressed_aln saga_aln        clustal_aln");
436                 fprintf ( stdout, "\n     phylip_aln     msf_aln         fasta_aln ");
437                 fprintf ( stdout, "\n     pir_aln        ");
438                 fprintf ( stdout, "\n     color_html,color_ps......colored using the struc_in file  ");
439                 fprintf ( stdout, "\n     color_protogene..........colors codons");
440                 fprintf ( stdout, "\n     color_exoset.............mixes conservation (gray) and introns (RGB)");
441                 fprintf ( stdout, "\n     color_pdf      pw_lib_saga_aln tdna_aln");
442                 fprintf ( stdout, "\n     thread_dna_on_prot_aln");
443                 fprintf ( stdout, "\n");
444                 fprintf ( stdout, "\n*********** OUTPUT FORMATS: sequence  ******************");
445                 fprintf ( stdout, "\n     fasta_seq      fasta_seq1     gotoh_seq");
446                 fprintf ( stdout, "\n     gor_seq        cache_id");
447                 fprintf ( stdout, "\n");
448                 fprintf ( stdout, "\n*********** OUTPUT FORMATS: weights ******************");
449                 fprintf ( stdout, "\n     constraints    saga_pw_sd_weights  nseq\n");
450                 fprintf ( stdout, "\n");
451                 fprintf ( stdout, "\n*********** OUTPUT Formats: special  ****************");
452                 fprintf ( stdout, "\n     len             name               statistics<_hnrglNL>");
453                 fprintf ( stdout, "\n      sim............outputs a similarity matrix based on an id comparison of -in");
454                 fprintf ( stdout, "\n      sim_sarmat.....in is sar matrix");
455                 fprintf ( stdout, "\n      sim_idscore....makes dp alignment of the sequences using Blosum62mt");
456                 fprintf ( stdout, "\n      sim_idscoreDNA.makes dp alignment of the sequences using idmat");
457                 fprintf ( stdout, "\n      sim............if -in2 is set: in1 vs in2, idscore");
458                 
459                 fprintf ( stdout, "\n     code_name......Outputs a compact list of names for code/decode");
460
461                 
462                 
463                 fprintf ( stdout, "\n");
464         
465                 
466                 fprintf ( stdout, "\n");
467                 return EXIT_SUCCESS;
468                 }
469         
470         argv=standard_initialisation (argv, &argc);
471         
472                 
473         for ( a=1; a< argc; a++)
474                 {
475                   if (a==1 && argv[1][0]!='-')
476                     {
477                       sprintf( in_file, "%s", argv[a]);
478                     }
479                   else if ( strcmp ( argv[a], "-in_f")==0 ||strm(argv[a],"-input") )
480                         {
481                         if ( strcmp ( argv[a], "-in_f")==0) fprintf ( stdout,"\nWARNING: %s deprecated, use -input instead", argv[a]);
482                 
483                         sprintf ( in_format, "%s", argv[a+1]);
484                         a++;
485                         }
486                 
487                 else if ( strcmp ( argv[a], "-cache")==0 )
488                         {
489                         sprintf (cache, "%s", argv[a+1]);
490                 
491                         a++;
492                         }
493                   
494                 
495                 else if ( strcmp ( argv[a], "-exon_boundaries")==0 )
496                         {
497                 
498                           set_string_variable ("exon_boundaries", argv[a+1]);
499                           a++;
500                         }
501                 else if ( strcmp ( argv[a], "-overaln_threshold")==0 )
502                         {
503                 
504                           set_int_variable ("overaln_threshold", atoi(argv[a+1]));
505                           a++;
506                         }
507                 else if ( strcmp ( argv[a], "-overaln_target")==0 )
508                         {
509                 
510                           set_int_variable ("overaln_target", atoi(argv[a+1]));
511                           a++;
512                         }
513                 else if ( strcmp ( argv[a], "-overaln_P1")==0 )
514                         {
515                           
516                           set_int_variable ("overaln_P1", atoi(argv[a+1]));
517                           a++;
518                         }
519                 else if ( strcmp ( argv[a], "-overaln_P2")==0 )
520                         {
521                 
522                           set_int_variable ("overaln_P2", atoi(argv[a+1]));
523                           a++;
524                         }
525                 else if ( strcmp ( argv[a], "-overaln_P3")==0 )
526                         {
527                 
528                           set_int_variable ("overaln_P3", atoi(argv[a+1]));
529                           a++;
530                         }
531                 else if ( strcmp ( argv[a], "-overaln_P4")==0 )
532                         {
533                 
534                           set_int_variable ("overaln_P4", atoi(argv[a+1]));
535                           a++;
536                         }
537                   
538                 else if ( strcmp ( argv[a], "-in2_f")==0||strm(argv[a],"-input2") )
539                         {
540                           if ( strcmp ( argv[a], "-in_f")==0) fprintf ( stdout,"\nWARNING: %s deprecated, use -input2 instead", argv[a]);
541                 
542                         sprintf ( in2_format, "%s", argv[a+1]);
543                         a++;
544                         }
545                 else if ( strcmp ( argv[a], "-seqnos")==0)
546                         {
547                         sprintf (action_list[n_actions++], "seqnos");
548                         }
549                 
550                 else if ( strcmp( argv[a], "-action")==0)
551                         {
552                         while ((a+1)<argc && argv[a+1][0]!='-')
553                           {
554                             sprintf (action_list[n_actions++], "%s", argv[a+1]);
555                             a++;
556                           }
557                         }
558                 else if ( strcmp ( argv[a], "-keep_case")==0)
559                         {
560                           if(!NEXT_ARG_IS_FLAG)RAD->keep_case=1;
561                           else RAD->keep_case=(strm3(argv[a], "on","ON","On"))?1:0;
562                           
563                         }
564                 
565                 else if ( strcmp ( argv[a], "-conv")==0)
566                         {
567                         if ( strncmp ( argv[a+1],"set",3)==0)RAD->symbol_list=make_symbols (argv[++a],&(RAD->n_symbol));
568                         else
569                             {
570                             RAD->symbol_list=declare_char (STRING, STRING);
571                             while(!NEXT_ARG_IS_FLAG)
572                                   { 
573                                   sprintf ( RAD->symbol_list[RAD->n_symbol], "%s", argv[++a]);
574                                   RAD->n_symbol++; 
575                                   }
576                             }
577                         }
578                 else if ( strcmp ( argv[a], "-struc_in_f")==0 ||strcmp ( argv[a], "-input3")==0 )
579                         {
580                         sprintf ( struc_in_format, "%s", argv[a+1]);
581                         a++;
582                         }
583                 else if ( strcmp ( argv[a], "-out_f")==0 ||strm(argv[a],"-output") )
584                         {
585                         if ( strcmp ( argv[a], "-out_f")==0) fprintf (stdout, "\nWARNING: %s deprecated, use -output instead", argv[a]);
586                         sprintf ( out_format, "%s", argv[a+1]);
587                         a++;
588                         }
589                 else if ( strm ( argv[a], "-struc_out_f") || strm ( argv[a], "-output_struc") )
590                         {
591                         sprintf ( struc_out_format, "%s", argv[a+1]);
592                         a++;
593                         }
594                 else if ( strcmp (argv[a],"-in")==0)
595                         {
596                         sprintf( in_file, "%s", argv[a+1]);
597                         a++;
598                         }
599                 else if ( strcmp (argv[a],"-rename")==0)
600                         {
601                         sprintf( rename_file, "%s", argv[a+1]);
602                         a++;
603                         }
604                 else if ( strcmp (argv[a],"-code")==0)
605                         {
606                         code=CODE;
607                         sprintf( rename_file, "%s", argv[a+1]);
608                         a++;
609                         }
610                 else if ( strcmp (argv[a],"-decode")==0)
611                         {
612                           code=DECODE;
613                           sprintf( rename_file, "%s", argv[a+1]);
614                           a++;
615                         }
616                 else if ( strcmp (argv[a],"-in2")==0)
617                         {
618                         sprintf( in2_file, "%s", argv[a+1]);
619                         a++;
620                         }
621                 else if ( strcmp (argv[a],"-coor")==0)
622                         {                       
623                         sprintf( RAD->coor_file, "%s", argv[a+1]);
624                         a++;
625                         }
626                 else if (strcmp (argv[a],"-out")==0) 
627                         {
628                         sprintf (out_file, "%s", argv[a+1]);
629                         a++;
630                         }
631                 else if (strcmp (argv[a],"-out2")==0) 
632                         {
633                         sprintf (out2_file, "%s", argv[a+1]);
634                         a++;
635                         }
636                 else if ( strcmp (argv[a],"-struc_in")==0 || strcmp (argv[a],"-in3")==0 )
637                         {
638                         sprintf( struc_in_file, "%s", argv[a+1]);
639                         a++;
640                         }
641                 else if (strcmp (argv[a],"-struc_out")==0) 
642                         {
643                         sprintf (struc_out_file, "%s", argv[a+1]);
644                         a++;
645                         }
646                 else if ( strcmp ( argv[a], "-rm_gap")==0)
647                         {
648                         RAD->rm_gap=1;
649                         }
650                 else if ( strcmp ( argv[a], "-print_format")==0)
651                         {
652                         print_format=1;
653                         }
654                 else if ( strcmp ( argv[a], "-no_warning")==0)
655                         {
656                         set_warning_mode (NO);
657                         }
658                 
659                 else    
660                         {
661                         fprintf ( stdout, "\nUNKNOWN OPTION: %s", argv[a]);
662                         myexit(EXIT_FAILURE);
663                         }
664                 }
665 /****************************************************************/
666 /*                                                              */
667 /*                          Data Preparation                    */
668 /*                                                              */
669 /*                                                              */
670 /****************************************************************/
671         
672         prepare_cache (cache);    
673 /****************************************************************/
674 /*                                                              */
675 /*                          INPUT SEQ/ALN                       */
676 /*                                                              */
677 /*                                                              */
678 /****************************************************************/
679         
680
681         if ( strm (out_format, "hasch"))
682           {
683             fprintf ( stdout, "%d\n", (int)hash_file(in_file));
684             return EXIT_SUCCESS;
685           }
686
687         if ( rename_file[0])
688           {
689             rename_list=read_rename_file ( rename_file,code);
690           }
691
692
693         if ((D1=read_data_structure (in_format, in_file,RAD))!=NULL)
694           {
695             in_format=(in_format && in_format[0])?in_format:identify_seq_format(in_file);
696             
697             if (print_format)fprintf ( stdout, "\nFILE:%s FORMAT:%s\n", in_file, in_format);
698           }
699         else if ( in_file[0])
700                 {
701                   fprintf ( stdout, "\nFORMAT of file %s Not Supported[FATAL:%s]\n", in_file, PROGRAM);
702                 myexit(EXIT_FAILURE);
703                 }
704         
705         if ((D2=read_data_structure (in2_format, in2_file,RAD))!=NULL){if (print_format)fprintf ( stderr, "\nFILE:%s FORMAT:%s\n", in2_file, (in2_format&&in2_format[0])?in2_format:identify_seq_format(in2_file));}
706
707         else if (!D2 && in2_file[0])
708                 {
709                   fprintf ( stderr, "\nFORMAT of file %s Not Supported [FATAL:%s]\n", in2_file, PROGRAM);
710                 myexit(EXIT_FAILURE);
711                 }
712         
713 /*STRUCTURE INPUT*/     
714         
715         
716         if ((D_ST=read_data_structure (struc_in_format, struc_in_file,RAD)))
717             {
718
719               if ( D_ST->CL)
720                 {
721                   Constraint_list *CL;
722                   int *entry;
723
724                   CL=D_ST->CL;
725                   
726                   entry=vcalloc ( LIST_N_FIELDS, sizeof (int));
727                   
728                   for (a=0; a<CL->ne; a++)
729                     {
730                       entry=extract_entry (entry, a, CL);
731                       if ( D_ST->S)(D_ST->S)->seq[entry[SEQ1]][entry[R1]-1]=entry[WE];
732                     }
733                   thread_seq_struc2aln (D_ST->A, D_ST->S);
734                 }
735               else if ( name_is_in_list ("cons", ((D_ST)->A)->name, ((D_ST)->A)->nseq, 100));
736               else
737                 {
738                   D_ST->A=copy_aln ( D1->A, D_ST->A);
739                   
740                   thread_seq_struc2aln (D_ST->A, D_ST->S);
741                 }
742             }
743         else if ((strcmp (struc_in_format, "rna_number")==0) && in_file[0])
744                 {
745                 D_ST->RNA_ST=read_rna_struc_number((D1->A),struc_in_file);
746                 }
747         else if ( struc_in_format[0] && struc_in_file[0])
748                 {
749                     
750                 fprintf ( stderr, "\nSTRUC %s UNKNOWN[FATAL]", struc_in_format);
751                 myexit(EXIT_FAILURE);
752                 }
753         else
754           {
755             D_ST=vcalloc ( 1, sizeof (Sequence_data_struc));
756           }
757
758         action=declare_char(100, 100);
759         for ( a=0; a< n_actions;)
760           {
761            if (action_list[a][0]!='+')
762               {
763                 fprintf ( stderr, "\nWARNING: Action %s Unknown. Actions start with a +", action_list[a]);
764                 myexit (EXIT_FAILURE);
765               }
766            else
767              {
768              b=0;
769              sprintf ( action[b++], "%s", action_list[a++]+1);
770              while ( a<n_actions && action_list[a][0]!='+')sprintf ( action[b++], "%s", action_list[a++]);
771              modify_data( D1, D2, D_ST, action,b, RAD);
772              }
773           }
774
775         if (rename_list)
776           {
777             if (D1)D1->A= rename_seq_in_aln(D1->A, rename_list);
778             if (D2)D2->A=rename_seq_in_aln (D2->A, rename_list);
779             if (D_ST)D_ST->A=rename_seq_in_aln (D_ST->A,rename_list);
780             
781             if (D1)D1->T  =rename_seq_in_tree (D1->T, rename_list);
782             if (D2)D2->T  =rename_seq_in_tree (D2->T, rename_list);
783             if (D_ST)D_ST->T=rename_seq_in_tree (D_ST->T,rename_list);
784           }
785
786
787         if ( !out_format[0] && ! struc_out_format[0])sprintf ( out_format, "%s", (in_format && in_format[0])?in_format:"fasta_aln");
788         main_output  ( D1, D2, D_ST, out_format, out_file);
789         main_output  ( D1, D2, D_ST, struc_out_format, struc_out_file);
790         return EXIT_SUCCESS;
791         }
792         
793
794
795
796 /**************************************************************************************************/
797 /*****************************    FORMAT GUESSING     ******************************************/
798 /**************************************************************************************************/
799 Sequence_data_struc *read_data_structure ( char *in_format, char *in_file,      Action_data_struc  *RAD) 
800          
801         {
802         Sequence_data_struc *D;
803         char **seq_name=NULL, **sequences=NULL;
804         int nseq=0, a;
805
806         
807         D=vcalloc ( 1, sizeof (Sequence_data_struc));
808         
809
810         if (!in_file[0])return NULL; 
811         if (!in_format[0])
812           {
813             in_format=identify_seq_format(in_file);
814           }
815         if (!in_format[0])return NULL;
816         
817
818
819         D->A=declare_Alignment(NULL);                 
820         if ( RAD->keep_case)(D->A)->residue_case=KEEP_CASE;
821         
822         D->rm_gap=RAD->rm_gap;
823         sprintf ( D->format, "%s", in_format);
824         sprintf ( D->file, "%s", in_file);
825         
826
827        
828
829         if ( strm2(in_format,"saga_aln","clustal_aln"))
830                 {
831                 read_aln (in_file, D->A);
832                 D->S=aln2seq(D->A);
833                 
834                 }
835                 
836         else if ( strm (in_format, "treefile_list"))
837           {
838             int z;
839             D->S=get_tree_file_list(in_file);
840             D->A=seq2aln(D->S, D->A,NO_PAD);
841           }
842         else if ( strm (in_format, "file_list") || strm (in_format, "list"))
843           {
844             D->S=get_file_list(in_file);
845             D->A=seq2aln(D->S, D->A,KEEP_GAP);
846           }
847         else if ( strm (in_format, "fasta_tree"))
848           {
849             int z;
850             D->S=get_fasta_tree (in_file, NULL);
851             D->A=seq2aln(D->S, D->A,NO_PAD);
852            
853           }
854         else if ( strm (in_format, "tree_list") || strm (in_format, "treelist"))
855           {
856             char **line;
857             FILE *seq,*dnd;
858             int n=0;
859             char *seq_file;
860             FILE *fp;
861             Sequence *T;
862             seq_file=vtmpnam(NULL);
863             seq=vfopen (seq_file, "w");
864             line=file2lines (in_file);
865             fp=vfopen (seq_file, "w");
866             for ( n=1; n<atoi(line[0]); n++)
867               {
868                 fprintf ( fp, ">Tree_%d\n%s\n", n,line[n]);
869               }
870             vfclose (fp);
871             
872             free_char (line, -1);
873             return read_data_structure ( "fasta_tree",seq_file,RAD); 
874           }
875         
876         else if (strm (in_format, "matrix"))
877           {
878             D->M=read_matrice (in_file);
879           }
880         else if (strm4 (in_format, "newick_tree", "newick", "nh", "new_hampshire"))
881           {
882             D->T=main_read_tree (in_file);
883             D->S=tree2seq(D->T, NULL);
884             D->A=seq2aln (D->S,D->A, 0); 
885           }
886         else if (strm (in_format, "blast_aln"))
887                 {
888                   if (read_blast_aln (in_file, D->A))
889                   {
890                     D->S=aln2seq(D->A);
891                   }
892                 else
893                   {
894                     return NULL;
895                   }
896                 }
897         else if ( strm( in_format,"number_aln"))
898                 {               
899                 read_number_aln (in_file, D->A);
900                 D->S=aln2seq(D->A);
901                 }
902         else if ( strm( in_format,"stockholm_aln"))
903                 {               
904                 read_stockholm_aln (in_file, D->A);
905                 D->S=aln2seq(D->A);
906                 }
907         else if ( strm( in_format,"gotoh_aln"))
908                 {               
909                 read_gotoh_aln (in_file, D->A);
910                 D->S=aln2seq(D->A);
911                 }
912         
913         else if ( strm ( in_format, "msf_aln"))
914                 {
915                 read_msf_aln (in_file, D->A);
916                 D->S=aln2seq(D->A);
917                 }
918         else if ( strm ( in_format, "amps_aln"))
919                 {
920                 read_amps_aln (in_file, D->A);
921                 D->S=aln2seq(D->A);
922                 }
923         else if ( strm (in_format, "excel_seq"))
924                 {               
925                   D->S=perl_reformat2fasta ("excel2fasta.pl",in_file);
926                   (D->S)->contains_gap=0;
927                   D->A=seq2aln(D->S, D->A,RAD->rm_gap);
928                 }
929         else if ( strm (in_format, "pavie_seq"))
930                 {               
931                   D->S=perl_reformat2fasta ("pavie2fasta.pl",in_file);
932                   (D->S)->contains_gap=0;
933                   D->A=seq2aln(D->S, D->A,RAD->rm_gap);
934                 }
935         else if ( strncmp (in_format, "perl_",5 )==0)
936                 {               
937                   D->S=perl_reformat2fasta (in_format+5,in_file);
938                   (D->S)->contains_gap=0;
939                   D->A=seq2aln(D->S, D->A,RAD->rm_gap);
940                 }
941         else if ( strm (in_format, "number_fasta"))
942                 {               
943                 D->S=get_fasta_sequence_num (in_file, NULL);
944                 (D->S)->contains_gap=0;
945                 D->A=seq2aln(D->S, D->A,RAD->rm_gap);
946                 }       
947         else if ( strm (in_format, "raw_fasta"))
948                 {               
949                 D->S=get_fasta_sequence_raw (in_file, NULL);
950                 (D->S)->contains_gap=0;
951                 D->A=seq2aln(D->S, D->A,RAD->rm_gap);
952                 }       
953
954         else if ( strm2 (in_format, "fasta_aln", "fasta_seq"))
955                 {
956                 
957                 D->S=get_fasta_sequence (in_file, NULL);
958                 if ( strcmp (in_format, "fasta_aln")==0)(D->S)->contains_gap=0;
959                 D->A=seq2aln(D->S, D->A,RAD->rm_gap);
960                 }       
961         else if ( strm (in_format, "fasta_tree"))
962                 {
963                 
964                 D->S=get_fasta_tree (in_file, NULL);
965                 D->A=seq2aln(D->S, D->A, NO_PAD);
966                 }       
967         
968         else if ( strm (in_format, "pdb") || strm (in_format, "pdb_struc"))
969                 {
970                     D->S=get_pdb_sequence (in_file);
971                     if ( D->S==NULL)
972                       {
973                         add_warning (stderr, "FAILED TO find PDB File %s", in_file);
974                         myexit (EXIT_FAILURE);
975                       }
976                     D->A=seq2aln(D->S, D->A,RAD->rm_gap);
977                 }
978         else if ( strm2(in_format, "pir_seq", "pir_aln"))
979                 {
980                 D->S=get_pir_sequence ( in_file,NULL );
981                 seq2aln(D->S, D->A, RAD->rm_gap);
982                 }
983         else if ( strm(in_format, "gor_seq") )
984                 {
985                 D->S=get_gor_sequence ( in_file,NULL );
986                 seq2aln(D->S, D->A, RAD->rm_gap);
987                 }
988         else if ( strm2 ( in_format, "dali_aln", "dali_seq"))
989                 {
990                 D->S=get_sequence_dali ( in_file);
991                 seq2aln(D->S, D->A, RAD->rm_gap);
992                 }
993         else if ( strm (in_format, "barton_list_tc"))
994                 {
995                 get_barton_list_tc_seq ( in_file);
996                 }
997         else if ( strm (in_format, "amps_sd_scores"))
998                 {
999                 D->W=get_amps_sd_scores ( in_file);
1000                 }
1001         
1002         else if ( strm ( in_format, "pima_aln"))
1003                 {
1004                 D->S=get_pima_sequence ( in_file);
1005                 seq2aln (D->S, D->A, RAD->rm_gap);
1006                 }
1007         else if ( strm( in_format, "gor_struc"))
1008                 {
1009                 D->S=get_struc_gor ( in_file);
1010                 seq2aln(D->S, D->A, RAD->rm_gap);
1011                 }
1012         else if ( strm( in_format, "dialign_aln"))
1013                 {
1014                 D->S=get_dialign_sequence ( in_file);
1015                 seq2aln (D->S, D->A, RAD->rm_gap);
1016                 }
1017         else if ( strm( in_format, "tc_lib") ||  strm( in_format, "mocca_lib") ||  strm( in_format, "lib"))
1018                 {
1019                   read_seq_in_list (in_file,&nseq,&sequences,&seq_name); 
1020                   D->S=fill_sequence_struc ( nseq, sequences, seq_name);
1021                   D->CL=declare_constraint_list ( D->S,NULL, NULL, 0,NULL, NULL); 
1022                   D->CL=read_constraint_list_file(D->CL,in_file);
1023                   seq2aln (D->S, D->A, RAD->rm_gap);
1024                   free_char (sequences,-1);
1025                   free_char (seq_name, -1);
1026                 }
1027         else if ( strm( in_format,"swissprot_seq"))
1028                 {
1029                   D->S=get_swissprot_sequence ( in_file,NULL);
1030                   seq2aln (D->S, D->A, RAD->rm_gap);
1031                 }
1032         else if  (strm (in_format, "alifold"))
1033           {
1034             D->S=read_alifold ( in_file);
1035             seq2aln (D->S, D->A,0);
1036           }
1037         else
1038                 {
1039                 return NULL; 
1040                 }
1041
1042         if ( D->A)
1043           {
1044             for ( a=0; a<(D->A)->nseq; a++)sprintf ( (D->A)->file[a], "%s", in_file);
1045           }
1046         if ( D->S)
1047           {
1048             for ( a=0; a<(D->A)->nseq; a++)sprintf ( (D->S)->file[a], "%s", in_file);
1049           }
1050         
1051         return D;
1052         }
1053 Sequence *read_sequences (char *name)
1054 {
1055   return main_read_seq (name);
1056 }
1057 Alignment * alifold2aln  (char *file)
1058 {
1059   Sequence *S;
1060   S=read_alifold(file);
1061   sprintf ( S->seq[0],"%s", S->seq[1]);
1062   return seq2aln (S, NULL, 0);
1063 }
1064 Sequence  * read_alifold (char *file)
1065 {
1066   Sequence *S;
1067   char **list;
1068   int l;
1069   S=declare_sequence (1,count_n_char_in_file (file),2);
1070   list=file2lines (file);
1071   
1072   S->seq[0]=list[1];
1073   S->seq[1]=list[2];
1074   substitute (S->seq[0], "\n", "\0");
1075   substitute (S->seq[0], " ", "\0");
1076   substitute (S->seq[0], "_", STOCKHOLM_STRING);
1077   l=strlen (S->seq[0]);
1078   substitute (S->seq[1], "\n", "\0");
1079   substitute (S->seq[1], " ", "\0");
1080   substitute (S->seq[1], ".", STOCKHOLM_STRING);
1081   S->seq[1][l]='\0';
1082   sprintf (S->name[0], "cons", file);
1083   sprintf (S->name[1], "#=GC SS_cons", file);
1084   return S;
1085 }
1086
1087   
1088   
1089
1090   
1091   
1092 Sequence  * main_read_seq ( char *name)
1093        {
1094        char *format=NULL;
1095        Sequence *S=NULL;
1096        Alignment *A=NULL;
1097        int a;
1098
1099
1100        format=identify_seq_format (name);
1101        
1102
1103        if ( getenv4debug ("DEBUG_REFORMAT"))fprintf ( stderr, "\n\nFormat %s\n", format); 
1104
1105        
1106        if (format &&strm(format, "fasta_seq"))        
1107          {
1108            S= get_fasta_sequence ( name, NULL);
1109          }
1110        else if (format &&strm(format, "pir_seq"))     S= get_pir_sequence ( name, NULL);
1111        else if (format &&strm(format,"swissprot_seq"))S= get_swissprot_sequence (name, NULL); 
1112        else if (format && strstr (format, "aln")) 
1113          {
1114            A=main_read_aln ( name, NULL);
1115            S=aln2seq(A);
1116            ungap_seq(S);
1117            free_aln(A);
1118          }
1119        else if ( format && strstr (format, "tc_lib"))
1120          {
1121            int nseq,b;
1122            char **sequences=NULL, **seq_name=NULL;
1123            
1124            read_seq_in_list (name,&nseq,&sequences,&seq_name);             
1125            S=fill_sequence_struc ( nseq, sequences, seq_name);
1126            for ( b=0; b< S->nseq; b++)sprintf ( S->file[b], "%s",name);
1127            free_char (seq_name, -1);free_char (sequences, -1);
1128          }
1129        else
1130           {
1131           /*Use The ClustalW routine*/
1132             S=cw_read_sequences (name);
1133           }
1134        
1135        for ( a=0; a<S->nseq; a++)sprintf ( S->file[a], "%s", name);
1136        vfree(format);
1137        ungap_seq(S);
1138        S=clean_sequence ( S);
1139        return S;
1140        }
1141
1142 Alignment * main_read_aln ( char *name, Alignment *A)
1143        {
1144        int a;
1145
1146        static char *format;
1147        Sequence *S=NULL;
1148        Sequence *IN_SEQ;
1149  
1150        
1151        if ( !name)return NULL;
1152        else if (!check_file_exists(name))
1153          {
1154            if ( !check_file_exists (name+1))return NULL;
1155            else if ( name[0]=='A') name++;
1156            else if ( name[0]=='S') name++;/*Line Added for the -convert flag of T-Coffee*/
1157          }
1158        
1159
1160        if (!A)A=declare_aln(NULL);
1161        format=identify_seq_format (name);
1162        
1163        IN_SEQ=A->S;
1164        
1165        if      ((format && strm(format, "saga_aln" )) ||strm(format, "clustal_aln")||strm(format, "t_coffee_aln" ) )
1166          {
1167
1168            read_aln ( name, A);
1169
1170          }
1171        else if (format && strm (format, "conc_aln"))A=input_conc_aln (name,NULL);
1172        else if (format &&strm(format, "msf_aln"  ))read_msf_aln ( name, A);
1173        else if (format &&strm(format, "blast_aln"))read_blast_aln (name, A);
1174        else if (format &&(strm(format, "fasta_aln")))
1175                 {
1176                   
1177                   
1178                 S=get_fasta_sequence ( name, NULL);
1179                 
1180                 S->contains_gap=0;
1181                 seq2aln (S, A, 0);              
1182                 }
1183        else if (format &&strm(format, "pir_aln"))
1184                 {
1185                 S=get_pir_sequence ( name, NULL);
1186                 S->contains_gap=0;
1187                 seq2aln (S, A, 0);
1188                 } 
1189        else if (format && strm(format, "fasta_seq") && A)
1190            {
1191            S=get_fasta_sequence ( name, NULL);
1192            
1193            for ( a=1; a<S->nseq; a++)if ( strlen (S->seq[a-1])!=strlen (S->seq[a])){free_sequence (S, S->nseq); free_aln (A); return NULL;}
1194            S->contains_gap=0;
1195            seq2aln (S, A, 0);
1196            }
1197
1198        else if (format && strm(format, "pir_seq") && A)
1199            {
1200            S=get_pir_sequence ( name, NULL);
1201           
1202            for ( a=1; a<S->nseq; a++)if ( strlen (S->seq[a-1])!=strlen (S->seq[a])){free_sequence (S, S->nseq); free_aln (A); return NULL;}
1203            S->contains_gap=0;
1204            seq2aln (S, A, 0);
1205            }
1206        else
1207           {
1208               free_aln(A);
1209               return NULL;        
1210           }
1211
1212      
1213          if ( check_list_for_dup( A->name, A->nseq))
1214           {
1215               fprintf ( stderr, "\nWARNING (main_read_aln): %s is duplicated in File %s ", check_list_for_dup( A->name, A->nseq), A->file[0]);
1216               A=aln2unique_name_aln(A);
1217           }
1218      
1219        if (IN_SEQ)A->S=IN_SEQ;
1220        else if (!A->S){A->S=aln2seq(A);}
1221        
1222        A->S=ungap_seq(A->S);
1223        A=fix_aln_seq(A, A->S);     
1224        compress_aln (A);
1225        for ( a=0; a< A->nseq; a++) sprintf ( A->file[a], "%s", name);
1226        
1227        A=clean_aln (A);
1228        return A;
1229        }
1230
1231
1232 char * identify_aln_format ( char *file)
1233        {
1234         /*This function identify known sequence and alignmnent formats*/
1235          return identify_seq_format (file);
1236        }
1237 char * identify_seq_format ( char *file)
1238        {
1239        char *format=NULL;
1240        /*This function identify known sequence and alignmnent formats*/
1241     
1242        if ( format==NULL)format=vcalloc ( 100, sizeof (char));
1243        else format[0]='\0';
1244        
1245
1246        
1247        if ( !check_file_exists(file))
1248          {
1249            fprintf (stderr, "ERROR: %s Does Not Exist [FATAL:%s]\n",file, PROGRAM);
1250            myexit (EXIT_FAILURE);
1251          }
1252        else if ( is_stockholm_aln (file))sprintf (format, "stockholm_aln");
1253        else if ( is_blast_file (file))sprintf ( format, "blast_aln");      
1254        else if ( is_pdb_file(file))sprintf ( format, "pdb_struc");  
1255        else if ( format_is_msf      (file))sprintf ( format, "msf_aln");
1256        else if ( format_is_fasta_seq(file))sprintf ( format, "fasta_seq");
1257        else if ( format_is_fasta_aln(file))sprintf ( format, "fasta_aln");       
1258        else if ( format_is_pir_aln  (file))sprintf ( format, "pir_aln");
1259        else if ( format_is_pir_seq  (file))sprintf ( format, "pir_seq");
1260        else if ( format_is_oligo    (file))sprintf ( format, "oligo_aln");
1261        else if ( format_is_swissprot     (file))sprintf ( format, "swissprot_seq");
1262        else if ( format_is_saga     (file))sprintf ( format, "clustal_aln");
1263        else if ( format_is_conc_aln (file))sprintf ( format, "conc_aln");
1264        else if ( is_lib (file))sprintf ( format, "tc_lib");
1265        else if ( is_lib_02 (file))sprintf ( format, "tc_lib_02");
1266        else if ( is_newick(file))sprintf ( format, "newick_tree");
1267  
1268        else 
1269          {
1270            add_warning ( stderr, "\nThe Format of File: %s was not recognized [SERIOUS:%s]",file, PROGRAM);
1271          }
1272        return format;
1273        }
1274 char **identify_list_format ( char **list, int n)
1275        {
1276            int a;
1277            char *name;
1278            char *string;
1279            char mode;
1280         
1281
1282            
1283            declare_name (name);
1284            for ( a=0; a< n; a++)
1285                {
1286                  
1287                  sprintf (name, "%s", list[a]);
1288                  string=list[a];
1289                  if ((mode=identify_format ( &string))!='?')
1290                    {
1291                        sprintf ( name, "%s", string);
1292                        sprintf ( list[a], "%c%s", mode,name);
1293                    }
1294                else
1295                    {
1296                        fprintf ( stderr, "\nERROR: %s not recognised [FATAL:%s]", name, PROGRAM);
1297                    }
1298              
1299                }
1300            
1301            vfree(name);
1302            return list;
1303        }
1304                
1305 char * name2type_name ( char *name)
1306 {
1307   /*turns <file> into <Sfile>, <Afile>...*/
1308   char *new_name;
1309   char mode;
1310   
1311   new_name=vcalloc ( strlen (name)+2, sizeof (char));
1312   sprintf ( new_name, "%s", name);
1313   if (is_in_set (name[0], "ALSMXPRW") && !check_file_exists(name))
1314     {
1315       sprintf ( new_name, "%s", name);
1316     }
1317   else
1318     {
1319       mode=identify_format (&new_name);
1320       sprintf ( new_name, "%c%s", mode,name);
1321     }
1322   return new_name;
1323 }          
1324
1325 char identify_format (char **fname)
1326        {
1327            char mode='?';
1328            mode=fname[0][0];
1329            
1330            if ((is_in_set (mode, "ALMSPR") && check_file_exists(fname[0]+1)) ||(mode=='X' && is_matrix ( fname[0]+1)) ||(mode=='M' && is_method(fname[0]+1)) )
1331              {
1332                
1333                fname[0]++;
1334              }
1335            else if (mode=='W' && !check_file_exists(fname[0])){fname[0]++;}
1336            else
1337                {
1338                    
1339                  /*WARNING: Order matters => internal methods can be confused with files, must be checked last*/
1340                       if (is_lib(fname[0]))mode='L';
1341                       else if (is_pdb_file(fname[0]))mode='P';
1342                       else if (is_seq(fname[0]))mode='S';
1343                       else if (is_aln(fname[0]))mode='A';
1344                       else if (is_matrix(fname[0]))mode='X';
1345                       else if (is_method(fname[0]))mode='M';                  
1346                       else mode='?';
1347                   }
1348            return mode;
1349        }
1350
1351
1352
1353 int is_pdb_name ( char *name)
1354     {
1355       char command[1000];
1356       int result;
1357       char *result_file;
1358       static char **buf_names;
1359       static int   *buf_result;
1360       static int   nbuf;
1361       FILE *fp;
1362
1363       
1364       /*Use the look up*/
1365       if ( !buf_names)
1366         {
1367           buf_names=declare_char (1000, 100);
1368           buf_result=vcalloc (1000, sizeof (int));
1369         }
1370       if ( (result=name_is_in_list ( name, buf_names,nbuf,100))!=-1)return buf_result[result];
1371       
1372       
1373
1374       result_file=vtmpnam (NULL);
1375    
1376       sprintf ( command, "extract_from_pdb -is_pdb_name \'%s\' > %s", name, result_file);
1377       if ( getenv4debug ("DEBUG_EXTRACT_FROM_PDB"))fprintf ( stderr, "\n[DEBUG_EXTRACT_FROM_PDB:is_pdb_name] %s\n", command);
1378       my_system ( command);
1379       
1380       fp=vfopen ( result_file, "r");
1381       fscanf ( fp, "%d", &result);
1382       vfclose (fp);
1383       vremove ( result_file);
1384       
1385       sprintf ( buf_names[nbuf], "%s", name);
1386       result=buf_result[nbuf++]=(result==1)?1:0;
1387
1388       return result;
1389       
1390     }
1391
1392 char*  get_pdb_id ( char *file)
1393 {
1394   /*receives the name of a pdb file*/
1395   /*reads the structure id in the header*/
1396   /*returns the pdb_id*/
1397   char *tmp_name;
1398   char command[10000];
1399   char cached [1000];
1400   char fname[1000];
1401   FILE *fp;
1402   char *id;
1403   char buf[1000];
1404
1405
1406   tmp_name=vtmpnam(NULL);
1407   
1408   sprintf ( cached, "%s/%s", get_cache_dir(),file);
1409   if ( check_file_exists(cached))sprintf ( fname, "%s", cached);
1410   else sprintf ( fname, "%s", file);
1411
1412   sprintf ( command, "extract_from_pdb -get_pdb_id %s > %s",fname, tmp_name);
1413   
1414   if ( getenv4debug ("DEBUG_EXTRACT_FROM_PDB"))fprintf ( stderr, "\n[DEBUG_EXTRACT_FROM_PDB:get_pdb_id] %s\n", command);
1415   my_system ( command);
1416   
1417   buf[0]='\0';
1418   fp=vfopen (tmp_name, "r");
1419   fscanf ( fp, "\n%s\n", buf);  
1420   vfclose (fp);
1421
1422   if ( getenv4debug ("DEBUG_EXTRACT_FROM_PDB"))fprintf ( stderr, "\n[DEBUG_EXTRACT_FROM_PDB:get_pdb_id]DONE\n");
1423   
1424   id=vcalloc ( strlen (buf)+1, sizeof (char));
1425   sprintf ( id, "%s", buf);
1426   
1427   
1428
1429   return id;  
1430 }
1431     
1432   
1433 char*  get_pdb_struc(char *in_name, int start, int end)
1434     {
1435       char *name1,*name2;
1436       char command[LONG_STRING];
1437       char *name;
1438
1439       
1440       
1441
1442       name=vcalloc ( STRING, sizeof (char));
1443       sprintf ( name, "%s", in_name);
1444      
1445       if ( (name1=is_pdb_struc(name))==NULL && (name[0]=='P' && ((name1=is_pdb_struc (name+1))==NULL)))
1446         {
1447           fprintf ( stderr, "\nERROR Could not download structure %s [FATAL:%s]\n", name, PROGRAM);crash("");
1448         }
1449       else if ( (start==0) && (end==0))return name1;
1450       else
1451         {
1452           declare_name(name2);
1453           sprintf ( name2, "%s_%d_%d.pdb", name, start, end);
1454           sprintf ( command, "extract_from_pdb -infile \'%s\' -chain FIRST -coor %d %d > %s%s",check_file_exists(name1),start, end, get_cache_dir(),name2);
1455           if ( getenv4debug ("DEBUG_EXTRACT_FROM_PDB"))fprintf ( stderr, "\n[DEBUG_EXTRACT_FROM_PDB:get_pdb_struc] %s\n", command);
1456           my_system (command);
1457
1458           if ( is_pdb_file(name2))return name2;
1459           else
1460             {
1461               fprintf ( stderr, "\nERROR Could not extract segment [%d %d] from structure %s [FATAL:%s]\n",start, end, name, PROGRAM);crash("");
1462             }
1463           exit (EXIT_FAILURE);
1464         }
1465
1466       return NULL;
1467     }
1468
1469 char*  seq_is_pdb_struc ( Sequence *S, int i)
1470 {
1471   
1472   if (!S){return NULL;}
1473   else if ( !S->T[i]){return NULL;}
1474   else if ( !((S->T[i])->P)){return NULL;}
1475   else return ((S->T[i])->P)->template_file;
1476 }
1477 char*  is_pdb_struc ( char *name)
1478    {
1479      /*Receives a name
1480        checks if this is the name of a local file that contains PDB data
1481        checks if this is the name of a file from a local db
1482                                             put the file in the cache
1483        checks if this is a file from a remote db (extract_from_pdb
1484        return NULL if everything fails
1485      */
1486
1487      static char *file_name1;
1488      static char *file_name2;
1489      static char **buf_names;
1490      static char **buf_result;
1491      static int   nbuf, s;
1492     
1493      
1494      char *r=NULL;
1495      char command[1000];
1496      
1497
1498      if ( !name || name[0]=='\0')return NULL;
1499        
1500
1501      /*Use the look up*/
1502      if ( !buf_names)
1503         {
1504
1505           buf_names=vcalloc ( 1000, sizeof (char*));
1506           buf_result=vcalloc ( 1000, sizeof (char*));
1507           file_name1=vcalloc ( 1000, sizeof (char));
1508           file_name2=vcalloc ( 1000, sizeof (char));
1509         }
1510      if ( (s=name_is_in_list ( name, buf_names,nbuf,-1))!=-1)return buf_result[s];
1511       
1512
1513      r=NULL;
1514      sprintf ( file_name1, "%s", name);
1515      sprintf ( file_name2, "%s.pdb", name);
1516
1517
1518      if (is_pdb_file(file_name1)){r=file_name1;}
1519      else if (is_pdb_file(file_name2)){r=file_name2;}
1520      else if (is_pdb_name (name))
1521        {
1522          char *tmpname;
1523          tmpname=vtmpnam (NULL);
1524
1525
1526          sprintf ( file_name2, "%s.pdb", name);
1527          /*sprintf ( command, "extract_from_pdb -netfile \'%s\' > %s%s 2>/dev/null",name, get_cache_dir(), file_name2);*/
1528          sprintf ( command, "extract_from_pdb -netfile \'%s\' > %s 2>/dev/null",name,tmpname);
1529          if ( getenv4debug ("DEBUG_EXTRACT_FROM_PDB"))fprintf ( stderr, "\n[DEBUG_EXTRACT_FROM_PDB:is_pdb_struc] %s\n", command);
1530          my_system (command);
1531          
1532          sprintf ( command, "cp %s %s%s", tmpname, get_cache_dir(), file_name2);
1533          my_system (command);
1534          
1535          if ( is_pdb_file(file_name2))r=file_name2; 
1536          else r=NULL;    
1537
1538        }
1539
1540       /*Fill the buffer*/
1541      buf_names[nbuf]=vcalloc ( strlen (name)+1, sizeof (char)); 
1542      sprintf ( buf_names[nbuf], "%s", name);
1543      if ( r)
1544        {
1545          buf_result[nbuf]=vcalloc ( strlen (r)+1, sizeof (char));
1546          sprintf (buf_result[nbuf], "%s", r);
1547        }
1548      else buf_result[nbuf]=NULL;      
1549      nbuf++;
1550
1551      return r;
1552    }
1553
1554 char *fix_pdb_file ( char *in)
1555 {
1556   char *empty;
1557   
1558   empty=vcalloc(1, sizeof(char));
1559
1560   if ( !in || !check_file_exists (in))return empty;
1561   else if ( is_pdb_file(in))return in;
1562   else
1563     {
1564       char command[10000];
1565       char *tmp;
1566       char *tmp2;
1567       tmp=vtmpnam (NULL);
1568       tmp2=vcalloc (strlen (tmp)+1, sizeof (char));
1569       sprintf (tmp2, "%s", tmp);
1570       sprintf ( command, "extract_from_pdb %s > %s", check_file_exists(in), tmp2);
1571       my_system (command);
1572       if ( is_pdb_file (tmp))return tmp2;
1573       else return empty;
1574
1575     }
1576 }
1577
1578 int is_sap_file ( char *name)
1579         {
1580         FILE *fp;
1581         if (!name);
1582         if (!check_file_exists(name))return 0;
1583         
1584         if ((fp=find_token_in_file (name, NULL, "Percent"))!=NULL)
1585           {
1586             if ((fp=find_token_in_file (name,fp, "Percent"))!=NULL)
1587               {
1588                 vfclose (fp);
1589                 return 1;
1590               }
1591             else
1592               { 
1593                 return 0;
1594               }
1595           }
1596         else
1597           {
1598             return 0;
1599           }
1600         }
1601
1602
1603 int is_blast_file ( char *name)
1604        {
1605          if ( !check_file_exists(name) ) return 0;
1606          else if (token_is_in_file (name, "<SequenceSimilaritySearchResult>"))
1607            {
1608              return BLAST_XML;
1609            }
1610          else
1611            {
1612              if (token_is_in_file (name, "Lambda"))
1613                {
1614                  return BLAST_TXT;
1615                }
1616              else
1617                {
1618                  return 0;
1619                }
1620            }
1621          return 0;
1622        }
1623 int is_simple_pdb_file ( char *name)
1624 {
1625   FILE *fp;
1626   if ((fp=find_token_in_file (name, NULL, "SIMPLE_PDB_FORMAT"))!=NULL){vfclose (fp);return 1;}
1627   return 0;
1628 }
1629
1630
1631 int is_pdb_file ( char *name)
1632        {
1633          FILE *fp;
1634          int ispdb=0;
1635
1636          if ( name==NULL) return 0;
1637          if (!check_file_exists (name))return 0;
1638        
1639               
1640         
1641        if ((fp=find_token_in_file (name, NULL, "\nHEADER"))!=NULL)
1642            {vfclose (fp);
1643              ispdb++; 
1644            }
1645        if ((fp=find_token_in_file (name, NULL, "\nSEQRES"))!=NULL)
1646            {vfclose (fp);
1647           
1648              ispdb++;
1649            }
1650        
1651        if ((fp=find_token_in_file (name, NULL, "\nATOM"))!=NULL)
1652          {
1653            vfclose (fp);
1654            ispdb++;
1655           
1656          }
1657        else
1658          {
1659            ispdb=0;
1660          }
1661
1662          
1663
1664        if ( ispdb>=2)return 1;
1665        else return 0;
1666        }
1667 int is_seq ( char *name)
1668        {
1669          char *format;
1670
1671          if ( !check_file_exists(name))return 0;
1672          
1673          format= identify_seq_format(name);
1674          if(!format || format[0]=='\0'){vfree (format);return 0;}
1675          else if (strstr(format, "seq")){vfree (format);return 1;}
1676          else return 0;
1677        }
1678 int is_aln ( char *name)
1679        {
1680        char *format;     
1681        if ( !check_file_exists       (name))return 0;   
1682         
1683        format= identify_seq_format(name);
1684        if ( !format || format[0]=='\0'){vfree (format);return 0;}
1685        else if (strstr(format, "aln")){vfree (format); return 1;}
1686        else return 0;
1687        }   
1688
1689 int is_matrix (char *name)
1690        {
1691        int **m;
1692        
1693        if ((m=read_matrice (name))!=NULL){free_int (m, -1); return 1;}
1694        return 0;              
1695        }
1696 int is_newick (char *name)
1697    {
1698      int c;
1699      FILE *fp;
1700      
1701
1702      fp=vfopen (name, "r");
1703      if ( (c=fgetc(fp))!='('){vfclose (fp); return 0;}
1704
1705
1706      while ( (c=fgetc(fp))!=EOF)
1707        {
1708          if ( c==';'){vfclose (fp); return 1;}
1709        }
1710      vfclose (fp);
1711      return 0;
1712    }
1713
1714 int is_clustalw_matrix ( char *name)
1715 {
1716        
1717   FILE *fp;
1718
1719
1720        if ( (fp=find_token_in_file (name, NULL, "CLUSTALW_MATRIX"))!=NULL){vfclose(fp);return 1;}
1721        else return 0;
1722 }
1723 int is_pavie_matrix ( char *name)
1724 {
1725        
1726   FILE *fp;
1727
1728
1729        if ( (fp=find_token_in_file (name, NULL, "PAVIE_MATRIX"))!=NULL){vfclose(fp);return 1;}
1730        else return 0;
1731 }
1732 int is_distance_matrix_file (char *name)
1733 {
1734   FILE *fp;
1735   if ( (fp=find_token_in_file (name, NULL, "TC_DISTANCE_MATRIX_FORMAT_01"))!=NULL){vfclose(fp);return 1;}
1736   else return 0;
1737 }  
1738 int is_similarity_matrix_file (char *name)
1739 {
1740   FILE *fp;
1741   if ( (fp=find_token_in_file (name, NULL, "TC_SIMILARITY_MATRIX_FORMAT_01"))!=NULL){vfclose(fp);return 1;}
1742   else return 0;
1743 }  
1744 int is_blast_matrix ( char *name)
1745 {
1746        
1747   FILE *fp;
1748
1749
1750   if ( (fp=find_token_in_file (name, NULL, "BLAST_MATRIX"))!=NULL){vfclose(fp);return 1;}
1751        else return 0;
1752 }
1753
1754 int is_single_seq_weight_file ( char *name)
1755 {
1756  
1757   
1758   return token_is_in_file ( name, "SINGLE_SEQ_WEIGHT_FORMAT_01");
1759   
1760 }
1761 int is_stockholm_aln (char *file)
1762 {
1763   FILE *fp;
1764   
1765   if ((fp=find_token_in_file_nlines (file, NULL, "STOCKHOLM",2)))
1766     {
1767       vfclose (fp);
1768       return 1;
1769     }
1770   return 0;
1771 }
1772
1773 int is_lib ( char *name)
1774 {
1775   return is_lib_01(name);
1776 }
1777
1778 int is_lib_02 ( char *name)
1779 {
1780   
1781   return token_is_in_file ( name, "TC_LIB_FORMAT_02");
1782   
1783 }
1784
1785 int is_lib_01 (char *name)
1786        {
1787
1788          
1789          if ( token_is_in_file ( name, "TC_LIB_FORMAT_01")) return 1;
1790          else if (token_is_in_file ( name, "T-COFFEE_LIB_FORMAT_01"))return 1;
1791          else if (token_is_in_file (name, "SEQ_1_TO_N"))return 1;
1792          else return 0;
1793        }
1794 int is_lib_list ( char *name)
1795 {
1796   if ( !check_file_exists (name))return 0;
1797   if ( token_is_in_file ( name, "TC_LIB_LIST_FORMAT_01")) return 1;
1798   return 0;
1799 }
1800 int is_method ( char *file)
1801     {
1802         char new_file[200];
1803
1804         
1805         sprintf ( new_file, "%s", file);
1806         if ( (token_is_in_file(new_file, "TC_METHOD_FORMAT_01"))){return 1;}
1807         if ( is_in_pre_set_method_list(new_file)) 
1808             {
1809         
1810                 vremove ( new_file);
1811                 return 1;
1812             }
1813         else
1814           {
1815         
1816             return 0;
1817           }
1818     }
1819
1820 /*******************************************************************************************/
1821 /*                                                                                         */
1822 /*                                                                                         */
1823 /*                              SEQUENCE FORMAT IDENTIFIERS                                */
1824 /*                                                                                         */
1825 /***************************************************************************************** */
1826 int type_is_exon_boundaries(char **seq, int n)
1827 {
1828   int a, l, b;
1829   for (a=0; a<n; a++)
1830     {
1831       l=strlen (seq[a]);
1832       for (b=0; b<l; b++)
1833         if ( strchr ("bojBOJ", seq[a][b]))return 1;
1834     }
1835   return 0;
1836 }
1837
1838 int format_is_oligo(char *file)
1839     { 
1840     char *buf=NULL;
1841     FILE *fp;
1842     int r=0;
1843     
1844     fp=vfopen ( file, "r");
1845     buf=vfgets(buf,fp);
1846     vfclose ( fp);
1847     
1848
1849     if ( strm (buf, "ALPHABET"))r=1;
1850     
1851     vfree (buf);
1852     
1853     return r;
1854     }
1855 int format_is_msf ( char *file)
1856     {
1857     char buf[1000];
1858     FILE *fp;
1859
1860    
1861     
1862     if ( (fp=find_token_in_file_nlines (file,NULL,"MSF:", 30))!=NULL){vfclose (fp);return 1;}
1863     else
1864       {
1865        return 0;
1866       }
1867     
1868     fp=vfopen ( file, "r");
1869     fscanf (fp , "%s", buf);
1870     vfclose ( fp);
1871
1872     if ( strm (buf, "MSF:"))return 1;
1873     return 0;
1874     }
1875
1876 //Fasta and PIR
1877 int format_is_fasta_aln ( char *file)
1878
1879     {
1880       if ( format_is_fasta(file) && !format_is_fasta_seq(file))return 1;
1881       else return 0;
1882     }
1883
1884
1885 int format_is_fasta_seq  ( char *file)
1886     {
1887       int a, l1, l2,l;
1888       Sequence *S;
1889
1890       if ( format_is_fasta (file))
1891         {       
1892         S=get_fasta_sequence (file, NULL);
1893         if (!S) return 0;
1894         else if ( !S->seq[0]){free_sequence (S, S->nseq); return 1;}
1895         l=strlen ( S->seq[0]);
1896         for ( a=0; a< S->nseq; a++)if(strlen(S->seq[a])!=l){free_sequence (S, S->nseq);return 1;}
1897         for ( a=0; a< S->nseq; a++)
1898           {
1899             l1=strlen ( S->seq[a]);
1900             ungap (S->seq[a]);
1901             l2=strlen ( S->seq[a]);
1902             if ( l1!=l2)
1903               {
1904                 free_sequence (S, S->nseq);
1905                 return 0;
1906               }
1907           }
1908         free_sequence (S, S->nseq);
1909         return 1;
1910       }
1911     else
1912       {
1913         return 0;
1914       }
1915     }
1916
1917 int format_is_fasta ( char *file)
1918     {
1919       Sequence *S;
1920       
1921       if ( !check_file_exists(file))return 0;
1922
1923       if ( get_first_non_white_char (file)!='>')return 0;
1924       if ( !(S=get_fasta_sequence (file, NULL)))return 0;
1925       free_sequence (S, -1);
1926       if ( format_is_pir(file)) return 0;
1927       return 1;
1928     }
1929
1930 int format_is_pir_aln ( char *file)
1931
1932     {
1933       if ( format_is_pir(file) && !format_is_pir_seq(file))return 1;
1934       else return 0;
1935     }
1936
1937 int format_is_pir_seq ( char *file)
1938     {
1939       int a, l1, l2;
1940       Sequence *S;
1941
1942       
1943     if ( format_is_pir (file))
1944       {
1945         S=get_pir_sequence (file, NULL);
1946         for ( a=0; a< S->nseq; a++)
1947           {
1948             l1=strlen ( S->seq[a]);
1949             ungap (S->seq[a]);
1950             l2=strlen ( S->seq[a]);
1951             if ( l1!=l2)
1952               {
1953                 free_sequence (S, S->nseq);
1954                 return 0;
1955               }
1956           }
1957         return 1;
1958       }
1959     else
1960       {
1961         return 0;
1962       }
1963     }
1964     
1965
1966 int format_is_pir ( char *file)
1967     {
1968       Sequence *S;
1969       int pir_name=1, star_end=1, a;
1970       
1971       S=get_fasta_sequence (file, NULL);
1972       if (!S)return 0;
1973       else if (!S->seq[0])return 0;
1974       
1975       pir_name=1; star_end=1;
1976       for (a=0; a< S->nseq; a++)
1977         {
1978           int l;
1979           if (!is_pir_name(S->name[a]))pir_name=0;
1980           l=strlen (S->seq[a]);
1981           if (!l || (l && S->seq[a][l-1]!='*'))
1982             star_end=0;
1983         }
1984       free_sequence(S,-1);
1985       if ( pir_name && star_end) return 1;
1986       else return 0;
1987     }
1988 int is_pir_name (char *name)
1989 {
1990   if ( strstr (name, "P1;"))return 1;
1991   if ( strstr (name, "F1;"))return 1;
1992   if ( strstr (name, "DL;"))return 1;
1993   if ( strstr (name, "DC;"))return 1;
1994   if ( strstr (name, "RL;"))return 1;
1995   if ( strstr (name, "RC;"))return 1;
1996   if ( strstr (name, "XX;"))return 1;
1997   return 0;
1998 }
1999   
2000   
2001 int format_is_conc_aln (char *file)
2002 {
2003   FILE *fp;
2004   if ( (fp=find_token_in_file (file, NULL, "CONC_MSF_FORMAT_01"))){vfclose (fp); return 1;}
2005   return 0;
2006 }
2007 int format_is_saga ( char *file) 
2008     {
2009     FILE *fp;
2010     int **list;
2011     int n_blocks;
2012     int n_seq;
2013     int a, b;
2014     
2015     if ( (fp=find_token_in_file (file, NULL, "SAGA"))){vfclose (fp); return 1;}
2016     else if  ((fp=find_token_in_file (file, NULL, "CLUSTAL"))){vfclose (fp); return 1;}
2017     else if  ((fp=find_token_in_file (file, NULL, "ClustalW"))){vfclose (fp); return 1;}
2018     else if  ((fp=find_token_in_file (file, NULL, "clustalw"))){vfclose (fp); return 1;}
2019     else if  ((fp=find_token_in_file (file, NULL, "clustal"))){vfclose (fp); return 1;}
2020     else if  ((fp=find_token_in_file (file, NULL, "T-COFFEE_MSA"))){vfclose (fp); return 1;}
2021     else if  ((fp=find_token_in_file (file, NULL, "INTERLEAVED_MSA"))){vfclose (fp); return 1;}
2022     
2023     else return 0;
2024     
2025     if (1==1);
2026     else if  ((fp=find_token_in_file (file, NULL, "T-COFFEE"))){vfclose (fp); return 1;}
2027     else if  ((fp=find_token_in_file (file, NULL, "SAGA_FORMAT"))){vfclose (fp); return 1;}
2028     else if  ((fp=find_token_in_file (file, NULL, "GARP"))){vfclose (fp); return 1;}
2029     else if  ((fp=find_token_in_file (file, NULL, "INTERLEAVED"))){vfclose (fp); return 1;}
2030     
2031     else 
2032        {
2033            list=get_file_block_pattern (file,&n_blocks,100); 
2034            if (n_blocks<=2){free_int (list, -1);return 0;}
2035            else 
2036                {                  
2037                n_seq=list[1][0];
2038                for ( a=1; a< n_blocks-1; a++)
2039                    {
2040                        if ( list[a][0]!=n_seq){free_int (list, -1);return 0;}
2041                        else
2042                        {
2043                            for ( b=1; b<=list[a][0]; b++)
2044                                if ( list[a][b]!=2){free_int (list, -1);return 0;}
2045                        }
2046                    }
2047                }
2048            return 1;
2049        }
2050     
2051     return 0;
2052     }
2053
2054
2055 int format_is_swissprot (char *name)
2056     {
2057       FILE *fp;
2058       
2059       if ( !check_file_exists(name))return 0;
2060          
2061          
2062    
2063     
2064       if (   (fp=find_token_in_file_nlines (name,NULL,"\nID ",10))!=NULL\
2065            &&(fp=find_token_in_file (name,NULL,"\nSQ "))!=NULL  )
2066         {
2067           
2068           vfclose (fp);return 1;
2069         }
2070       else
2071         {
2072           return 0;
2073         }
2074     } 
2075
2076 /*******************************************************************************************/
2077 /*                                                                                         */
2078 /*                                                                                         */
2079 /*                               OUTPUT STUFF                                              */
2080 /*                                                                                         */
2081 /***************************************************************************************** */               
2082 int output_format_aln ( char *format, Alignment *inA, Alignment *inEA,char *name)
2083         {
2084         Sequence_data_struc *D1=NULL;
2085         Sequence_data_struc *D2=NULL;
2086         Alignment *A=NULL;
2087         Alignment *EA=NULL;
2088         
2089         
2090         A =copy_aln (inA, NULL);
2091         A->CL=inA->CL;
2092         EA=copy_aln (inEA,NULL);
2093         A =expand_aln(A);
2094         EA=expand_number_aln(inA,EA);   
2095         
2096         
2097         if (A && A->expanded_order  )A=reorder_aln ( A, A->expanded_order,A->nseq);  
2098         if (EA && EA->expanded_order)EA=reorder_aln ( EA, EA->expanded_order,EA->nseq);  
2099
2100           
2101         D1=vcalloc ( 1, sizeof (Sequence_data_struc));
2102         D1->A=A;
2103         if (EA)
2104            {
2105            D2=vcalloc ( 1, sizeof (Sequence_data_struc));
2106            D2->A=EA;
2107            }
2108         
2109         main_output ( D1, NULL,D2, format, name);
2110         
2111         vfree(D1);
2112         vfree(D2);
2113         free_aln (A);
2114         free_aln (EA);
2115         return 1;
2116         }
2117 int main_output  (Sequence_data_struc *D1, Sequence_data_struc *D2, Sequence_data_struc *DST, char *out_format, char *out_file)
2118
2119         {  
2120         FILE *fp;
2121         int value;
2122         Alignment *BUF_A;
2123         int expanded=0;
2124         
2125         if ( !out_format[0])return 0;
2126         if ( D1 && D1->rm_gap)ungap_aln ((D1->A));
2127         
2128         if ( (strstr (out_format, "expanded_")))
2129           {
2130             if (!D1) return 1;
2131             out_format+=strlen ("expanded_");
2132             BUF_A=copy_aln (D1->A, NULL);
2133             (D1->A)=thread_profile_files2aln ((D1->A), NULL, NULL);
2134             expanded=1;
2135           }
2136         
2137         if ( strm (out_format, ""))return 0;
2138         else if (    ( strm (out_format, "aln2lib")))
2139           {
2140             int a, b, c;
2141             int r1,r2,s1, s2,s;
2142             Constraint_list *CL;
2143             FILE *fp;
2144             Alignment *IN;
2145             int **pos;
2146             
2147             if (!D1)return 1;
2148             IN=D1->A;
2149             CL=(D1->A)->CL;
2150             pos=aln2pos_simple(IN, IN->nseq);
2151             fp=vfopen (out_file, "w");
2152             fp=save_list_header (fp,CL);
2153             
2154           
2155             for ( b=0; b< IN->nseq-1; b++)
2156               {
2157                 for ( c=b+1; c< IN->nseq; c++)
2158                   {
2159                     s1=IN->order[b][0];
2160                     s2=IN->order[c][0];
2161                     fprintf ( fp, "#%d %d\n", s1+1, s2+1);
2162                     for ( a=0; a< IN->len_aln; a++)
2163                       {
2164                         r1=pos[b][a];
2165                         r2=pos[c][a];
2166
2167                         if ( s1==s2 && !CL->do_self)continue;
2168                         
2169                         if ( s1< s2)s=(CL->evaluate_residue_pair)( CL, s1, r1, s2, r2);
2170                         else        s=(CL->evaluate_residue_pair)( CL, s2, r2, s1, r1);
2171                         
2172                         s=(s!=UNDEFINED)?s:0;
2173                         if ( r1>0 && r2>0)
2174                           {
2175                             fprintf (fp, "\t%5d %5d %5d \n", r1, r2, s);
2176                           }
2177                       }
2178                   }
2179               }
2180              vfclose (save_list_footer (fp, CL));
2181           }
2182         else if      ( strncmp (out_format, "score",5)==0 || strm (out_format, "html"))
2183                 {
2184                   Alignment *BUF;
2185
2186                   if (!D1)return 1;
2187                   if ( !DST) 
2188                     {
2189                       fprintf ( stderr,"\n[You Need an evaluation File: Change the output format or use +evaluate][FATAL:%s]\n", PROGRAM);      
2190                       myexit(EXIT_FAILURE);
2191                     }
2192                   if ( !strm ("html", out_format))while ( out_format[0]!='_' && out_format[0]!='\0' )out_format++;
2193                   
2194                   D1->S=aln2seq(D1->A);
2195                   BUF=copy_aln (DST->A, NULL);
2196                   DST->A=aln2number (DST->A);
2197                   
2198                   if     ( strstr ( out_format, "html"  ))output_reliability_html  ( D1->A,  DST->A, out_file);
2199                   else if( strm ( out_format, "_ps"    ))output_reliability_ps    ( D1->A,  DST->A, out_file);
2200                   else if( strm ( out_format, "_pdf"   ))output_reliability_pdf   ( D1->A,  DST->A, out_file);  
2201                   else if( strm ( out_format, "_ascii" ))output_reliability_ascii ( D1->A,  DST->A, out_file);  
2202                   else if( strm ( out_format, "_seq"   ))output_seq_reliability_ascii ( D1->A,  DST->A, out_file);
2203                   else
2204                     {
2205                       DST->A=BUF;
2206                       main_output (DST, NULL, NULL, out_format+1, out_file);
2207                     }
2208                 }
2209         else if (strm (out_format, "sec_html") || strm (out_format, "_E_html"))
2210           {
2211             Alignment *ST, *A;
2212             Sequence *S;
2213             
2214             int a, b,c,i, ns=0;
2215             char *buf;
2216             if (!D1)return 1;
2217             A=D1->A;
2218             
2219             
2220             S=A->S;
2221             ST=copy_aln (A, NULL);
2222             for (a=0; a<ST->nseq; a++)
2223               {
2224                 i=name_is_in_list (ST->name[a],S->name, S->nseq, 100);
2225                 if ( i!=-1)
2226                   {
2227                     buf=seq2E_template_string(S, i);
2228                     if ( buf==NULL)continue;
2229                     else ns++;
2230                     for (c=0,b=0; b<ST->len_aln; b++)
2231                       {
2232                         int r1, s;
2233                         r1=ST->seq_al[a][b];
2234                         if ( r1!='-')
2235                           {
2236                             s=tolower (buf[c]);
2237                             if (s=='e')r1='0';
2238                             else if (s=='h')r1='9';
2239                             else if (s=='c')r1='5';
2240                             c++;
2241                           }
2242                         ST->seq_al[a][b]=r1;
2243                       }
2244                   }
2245               }
2246             
2247             if (!ns)
2248               {
2249                 printf_exit ( EXIT_FAILURE, stderr, "\nYou must provide a TM template file [FATAL:%s]", PROGRAM);
2250               }
2251             output_color_html  ( A, ST, out_file);
2252           }
2253         else if (strm (out_format, "tm_html") || strm (out_format, "_T_html"))
2254           {
2255             Alignment *ST, *A;
2256             Sequence *S;
2257             
2258             int a, b,c,i, ns=0;
2259             char *buf;
2260             if (!D1)return 1;
2261             A=D1->A;
2262             
2263             
2264             S=A->S;
2265             ST=copy_aln (A, NULL);
2266             for (a=0; a<ST->nseq; a++)
2267               {
2268                 i=name_is_in_list (ST->name[a],S->name, S->nseq, 100);
2269                 if ( i!=-1)
2270                   {
2271                     buf=seq2T_template_string(S, i);
2272                     if ( buf==NULL)continue;
2273                     else ns++;
2274                     for (c=0,b=0; b<ST->len_aln; b++)
2275                       {
2276                         int r1, s;
2277                         r1=ST->seq_al[a][b];
2278                         if ( r1!='-')
2279                           {
2280                             s=tolower (buf[c]);
2281                             if (s=='o')r1='0';
2282                             else if (s=='h')r1='9';
2283                             else if (s=='i')r1='5';
2284                             c++;
2285                           }
2286                         ST->seq_al[a][b]=r1;
2287                       }
2288                   }
2289               }
2290             
2291             if (!ns)
2292               {
2293                 printf_exit ( EXIT_FAILURE, stderr, "\nYou must provide a TM template file [FATAL:%s]", PROGRAM);
2294               }
2295             output_color_html  ( A, ST, out_file);
2296           }
2297         
2298         else if (strm (out_format, "color_exoset"))
2299           {
2300             Alignment *ST, *EX, *A;
2301             Constraint_list *CL;
2302             int a, b, n;
2303             char *buf;
2304             
2305             if ( !DST->A)
2306               {
2307                 printf_exit ( EXIT_FAILURE, stderr, "\nYou must provide an obj file via the -struc_in flag [FATAL:%s]", PROGRAM);
2308               }
2309             EX=DST->A;
2310             A=D1->A;
2311             
2312             CL=declare_constraint_list ( DST->S,NULL, NULL, 0,NULL, read_matrice("pam250mt"));
2313             
2314             ST=copy_aln (A, NULL);
2315             buf=vcalloc ( EX->len_aln+1, sizeof (int));
2316             
2317             for ( a=0; a< A->nseq; a++)
2318               {
2319                 int i;
2320                                 
2321                 i=name_is_in_list (A->name[a],EX->name, EX->nseq, -1);
2322                 if ( i==-1)continue;
2323                                 
2324                 sprintf ( buf, "%s", EX->seq_al[i]);
2325                 ungap (buf);
2326                 
2327                 for (n=0,b=0; b<A->len_aln; b++)
2328                   {
2329                     if (!is_gap(A->seq_al[a][b]))
2330                       {
2331                         if ( buf[n]=='o')
2332                           ST->seq_al[a][b]='0';
2333                         else if ( buf[n]=='j')
2334                           ST->seq_al[a][b]='1';
2335                         else if ( buf[n]=='b')
2336                           ST->seq_al[a][b]='2';
2337                         n++;
2338                       }
2339                   }
2340               }
2341             vfree (buf);
2342         
2343             output_color_html  ( A, ST, out_file);
2344             return EXIT_SUCCESS;
2345           }
2346         
2347         else if (strm (out_format, "color_protogene"))
2348           {
2349             int n, a, b;
2350             DST->A=copy_aln (D1->A, NULL);
2351             for (n=1,a=0; a< (D1->A)->len_aln; a++, n++)
2352               {
2353                 for ( b=0; b<(D1->A)->nseq; b++)
2354                   {
2355                     if (is_gap((D1->A)->seq_al[b][a]));
2356                     else if ( n<=3)(DST->A)->seq_al[b][a]=2;
2357                     else if ( n>3)(DST->A)->seq_al[b][a]=9;
2358                   }
2359
2360                 if ( n==6)n=0;
2361               }
2362             output_color_html  ( D1->A,  DST->A, out_file);
2363             return EXIT_SUCCESS;
2364             
2365           }
2366         else if      ( strncmp (out_format, "color",5)==0)
2367          {
2368            Alignment *BUF;
2369           
2370            if (!D1)return 1;
2371            
2372            if ( !DST) 
2373              {
2374                fprintf ( stderr,"\n[You Need an evaluation File: Change the output format or use +evaluate][FATAL:%s]\n", PROGRAM);     
2375                myexit(EXIT_FAILURE);
2376              }
2377            while ( out_format[0]!='_' && out_format[0]!='\0' )out_format++;
2378            
2379            BUF=copy_aln (DST->A, NULL);
2380
2381         
2382         
2383
2384            if     ( strm ( out_format, "_html"  ))output_color_html  ( D1->A,  DST->A, out_file);
2385            else if( strm ( out_format, "_ps"    ))output_color_ps    ( D1->A,  DST->A, out_file);
2386            else if( strm ( out_format, "_pdf"   ))output_color_pdf   ( D1->A,  DST->A, out_file);       
2387            else if( strm ( out_format, "_ascii"   ))output_color_ascii   ( D1->A,  DST->A, out_file);   
2388            else
2389              {
2390                DST->A=BUF;
2391                return main_output (DST, NULL, NULL, out_format+1, out_file);
2392              }
2393            return EXIT_SUCCESS;
2394          }
2395         else if ( strm4  ( out_format, "tc_aln","t_coffee_aln", "t_coffee", "tcoffee"))
2396           {
2397             if (!D1)return 1;
2398             vfclose (output_aln ( D1->A, vfopen (out_file, "w")));
2399           }
2400         else if ( strm  ( out_format, "analyse_pdb"))
2401           {
2402             if (!D1)return 1;
2403             if ( !DST) 
2404               {
2405                 fprintf ( stderr,"\n[You Need an evaluation File: Change the output format][FATAL:%s]\n", PROGRAM);     
2406                 myexit(EXIT_FAILURE);
2407               }
2408             analyse_pdb ( D1->A,DST->A, "stdout");
2409             (DST->A)=aln2number (DST->A);
2410             output_reliability_ps    ( D1->A,  DST->A, out_file);
2411           }
2412         else if ( strm4 ( out_format, "lower0", "lower1", "lower2", "lower3") || strm4(out_format, "lower4", "lower5", "lower6", "lower7") || strm4 (out_format,"lower8", "lower9", "align_pdb", "malign_pdb") )
2413           {
2414             if (!D1)return 1;
2415             if ( !DST) 
2416               {
2417                 fprintf ( stderr,"\n[You Need an evaluation File: Change the output format][FATAL:%s]\n", PROGRAM);     
2418                 myexit(EXIT_FAILURE);
2419               }
2420             
2421             
2422             
2423             (DST->A)=aln2number (DST->A);
2424             if ( strm (out_format, "align_pdb"))value=0;
2425             else if (  strm (out_format, "malign_pdb"))value=5;
2426             else value=atoi(out_format+5);
2427             
2428             D1->A=filter_aln_upper_lower (D1->A, DST->A,0, value);
2429             output_clustal_aln ( out_file, D1->A);
2430           }
2431         else if ( strnm (out_format, "repeat", 6))
2432           {
2433             int size;
2434             int a, b, c;
2435             Alignment *CONC;
2436             
2437             if ( !D1)return 1;
2438             size=atoi (out_format+6);
2439             print_aln (D1->A);
2440             CONC=declare_aln2 ( (D1->A)->nseq, ((D1->A)->len_aln+1)*size+1);
2441
2442             for ( a=0; a< (D1->A)->nseq; a++)(D1->A)->seq_al[a][(D1->A)->len_aln]='\0';
2443             for ( c=0,a=0; a< (D1->A)->nseq;c++)
2444               {
2445                 
2446                 sprintf ( CONC->name[c], "%s", (D1->A)->name[a]);
2447                 for ( b=0; b<size; b++, a++)
2448                   {
2449                     strcat (CONC->seq_al[c], (D1->A)->seq_al[a]);
2450                     strcat (CONC->seq_al[c], "O");
2451                   }
2452               }
2453             CONC->nseq=c;CONC->len_aln=strlen (CONC->seq_al[0]);
2454             output_clustal_aln ( out_file, CONC);
2455             free_aln (CONC);
2456           }
2457         
2458         else if ( strnm (out_format, "upper", 5))
2459               {
2460                 
2461                 if (!D1)return 1;
2462                 if ( !DST) 
2463                    {
2464                    fprintf ( stderr,"\n[You Need an evaluation File: Change the output format][FATAL:%s]\n", PROGRAM);  
2465                    myexit(EXIT_FAILURE);
2466                    }
2467                 
2468                 
2469                 (DST->A)=aln2number (DST->A);
2470
2471                 value=atoi(out_format+5);
2472
2473                 D1->A=filter_aln_lower_upper (D1->A, DST->A,0, value);
2474                 output_clustal_aln ( out_file, D1->A);
2475               }
2476         
2477         else if ( strm4 ( out_format, "filter0", "filter1", "filter2", "filter3"))
2478                {
2479                if (!D1)return 1;
2480                if ( !DST) 
2481                    {
2482                    fprintf ( stderr,"\n[You Need an evaluation File: Change the output format][FATAL:%s]\n", PROGRAM);  
2483                    myexit(EXIT_FAILURE);
2484                    }
2485                 
2486                (DST->A)=aln2number (DST->A);
2487                
2488                D1->A=filter_aln (D1->A, DST->A, atoi(out_format+6));
2489                output_clustal_aln ( out_file, D1->A);
2490                }
2491         
2492         else if ( strm3 ( out_format, "phylip_aln", "phylip", "phy"))
2493                 {
2494                 if (!D1)return 1;
2495                 output_phylip_aln ( out_file, D1->A);
2496                 }
2497         else if ( strm ( out_format, "mocca_aln"))
2498                 {
2499                 if (!D1)return 1;
2500                 output_mocca_aln ( out_file, D1->A, DST->A);
2501                 }       
2502         else if ( strm ( out_format, "saga_pw_sd_weights") )
2503                 {
2504                 if (!D1)return 1;
2505                 output_pw_weights4saga ((D1->W),(D1->W)->PW_SD, out_file);
2506                 }
2507         else if ( strm ( out_format, "saga_aln"))
2508                 {
2509                 if (!D1)return 1;
2510                 output_saga_aln (out_file, D1->A);
2511                 }
2512         else if (strm2 ( out_format, "aln","clustal_tc")|| strm (out_format, "msa"))
2513           {              
2514            
2515             if (!D1)return 1;
2516             output_clustal_aln (out_file, D1->A);
2517           }
2518         else if (strm5 ( out_format, "strict_clustal","clustal_aln", "clustalw","clustal", "clustalw_aln") || strm (out_format,"number_aln"))
2519           {              
2520             if (!D1)return 1;
2521             output_strict_clustal_aln (out_file, D1->A);
2522           }
2523         else if ( strm ( out_format, "conc_aln"))
2524               {
2525                 if (!D1)return 1;
2526                 output_conc_aln (out_file, D1->A);
2527                 }
2528         else if ( strm2 ( out_format, "lalign_aln","lalign"))
2529                 {
2530                 if (!D1)return 1;
2531                 output_lalign (out_file, D1->A);
2532                 }
2533         else if ( strm2 ( out_format, "glalign_aln","glalign"))
2534                 {
2535                 if (!D1)return 1;
2536                 output_glalign (out_file, D1->A, DST->A);
2537                 }
2538         
2539         else if ( strm2 ( out_format, "fasta_aln","fasta" ) || strm (out_format, "blast_aln"))
2540                 {
2541                 if (!D1)return 1;
2542                 output_fasta_aln( out_file, D1->A);
2543                 }
2544         else if ( strm (out_format, "overaln"))
2545                 {
2546                   Alignment *EB=NULL;
2547                   char *s, mode[100];
2548                   OveralnP *F;
2549                   int eb=0;
2550                   if (!D1) return 1;
2551                   F=vcalloc (1, sizeof (OveralnP));
2552                   
2553                   string_array_upper ((D1->A)->seq_al, (D1->A)->nseq);
2554                   if ( D2 && D2->A)
2555                     {
2556                       D1->A=mark_exon_boundaries (D1->A, D2->A);
2557                       eb=1;
2558                     }
2559                   else if ( (s=get_string_variable ("exon_boundaries")))
2560                     {
2561                       Sequence *S;
2562                       Alignment *EB;
2563                       EB=seq2aln(S=main_read_seq(s),NULL, 0);
2564                       D1->A=mark_exon_boundaries (D1->A, EB);
2565                       free_sequence (S, S->nseq); free_aln (EB);
2566                       eb=1;
2567                     }
2568                   
2569                   sprintf (F->mode, "%s", ((s=get_string_variable ("overaln_mode")))?s:"lower");
2570                   if (!strm (F->mode, "lower") && !strm (F->mode, "unalign"))printf_exit (EXIT_FAILURE,stderr,"\nERROR: unknown overal_mode in overal output [%s] [FATAL:%s]", mode, PROGRAM);
2571                   
2572                   if (int_variable_isset ("overaln_threshold"))F->t=get_int_variable ("overaln_threshold");
2573                   if (int_variable_isset ("overaln_target"))F->f=get_int_variable ("overaln_target");
2574                   if (int_variable_isset ("overaln_P1"))F->p1=get_int_variable ("overaln_P1");
2575                   if (int_variable_isset ("overaln_P2"))F->p2=get_int_variable ("overaln_P2");
2576                   if (int_variable_isset ("overaln_P3"))F->p3=get_int_variable ("overaln_P3");
2577                   if (int_variable_isset ("overaln_P4"))F->p4=get_int_variable ("overaln_P4");
2578                   
2579                   if (eb)sprintf (F->model, "fsa2");
2580                   else   sprintf (F->model, "fsa1");
2581                   D1->A=aln2clean_pw_aln (D1->A, F);
2582                   
2583                   //if (eb)D1->A=aln2clean_pw_aln (D1->A, mode,t, f,p1,p2,p3, "fsa2");
2584                   //else   D1->A=aln2clean_pw_aln (D1->A, mode,t, f,p1,p2,p3, "fsa1");
2585         
2586                   D1->S=aln2seq(D1->A);
2587                   output_clustal_aln (out_file, D1->A);
2588                 } 
2589         else if ( strm ( out_format, "est_prf" ))
2590                 {
2591                 if (!D1)return 1;
2592                 output_est_prf( out_file, D1->A);
2593                 }
2594         else if ( strm ( out_format, "clean_est_fasta_seq" ))
2595                 {
2596                 if (!D1)return 1;
2597                 D1->A=clean_est(D1->A);
2598                 output_fasta_seq(out_file, D1->A);
2599                 
2600                 }
2601         
2602         else if ( strm3 ( out_format, "msf_aln", "gcg", "msf"))
2603                 {
2604                 if (!D1)return 1;
2605                 output_msf_aln( out_file, D1->A);
2606                 }
2607         else if ( strm ( out_format, "rnalign"))
2608                 {
2609                 if (!D1)return 1;
2610                 output_rnalign (out_file, D1->A, DST->S);
2611                 }
2612         else if ( strm ( out_format, "fasta_seq") ||strm ( out_format, "list")||strm ( out_format, "file_list"))
2613                 {
2614                   int z;
2615                 if (!D1)return 1;
2616                 output_fasta_seq (out_file,D1->A);
2617                 }
2618         else if (strm (out_format, "fasta_tree") )
2619                 {
2620                 if (!D1)return 1;
2621                 output_fasta_tree (out_file,D1->A);
2622                 }
2623         
2624         else if ( strm ( out_format, "gotoh_seq"))
2625                 {
2626                 if (!D1)return 1;
2627                 output_gotoh_seq (out_file,D1->A);
2628                 }
2629         else if ( strm (out_format, "fasta_seq1"))
2630                 {
2631                 if (!D1)return 1;
2632                 output_fasta_seq1 (out_file, D1->A);
2633                 }
2634         else if ( strm2 (out_format, "pir_aln", "pir"))
2635                 {
2636                 if (!D1)return 1;
2637                 output_pir_aln (out_file, D1->A);
2638                 }
2639         else if ( strm (out_format, "pir_seq"))
2640                 {
2641                 if (!D1)return 1;
2642                 output_pir_seq (out_file, D1->A);
2643                 }
2644         else if ( strm (out_format, "gor_seq"))
2645                 {
2646                 if (!D1)return 1;
2647                 output_gor_seq (out_file, D1->A);
2648                 }
2649         else if ( strm (out_format, "pir_seq1"))
2650                 {
2651                   if (!D1)return 1;
2652                 output_pir_seq1 (out_file, D1->A);
2653                 }
2654         else if ( strm (out_format, "pw_lib_saga_aln"))
2655                 {
2656                   if (!D1)return 1;
2657                 output_pw_lib_saga_aln (out_file, D1->A);
2658                 }
2659         else if ( strm (out_format, "lib"))
2660                 {
2661                   if (!D1)return 1;
2662                 output_lib (out_file, D1->A);
2663                 }
2664         else if ( strm (out_format, "pdb_constraint_list"))
2665                 {
2666                   if (!D1)return 1;
2667                 output_constraints (out_file, "pdb",D1->A);
2668                 }
2669         else if ( strm2 (out_format, "constraint_list","tc_lib"))
2670                 {
2671                   
2672                   if (!D1)return 1;
2673                   else if (!D1->CL)output_constraints (out_file,"sim", D1->A);
2674                   else if (D1->CL) vfclose ( save_constraint_list ( D1->CL, 0, (D1->CL)->ne, out_file, NULL, "ascii",(D1->CL)->S)); 
2675                 }
2676         else if (  strm2 (out_format, "extended_lib","extended_cosmetic"))
2677                 {
2678                   if (!D1)return 1;
2679                   output_constraints (out_file,out_format, D1->A);
2680                 }
2681         else if ( strncmp (out_format, "extended_pair", 13)==0)
2682                 {
2683                   if (!D1)return 1;
2684                   output_constraints (out_file,out_format, D1->A);
2685                 }
2686         else if ( strm (out_format, "cache_id"))
2687                 {
2688                   if (!D1)return 1;
2689                   cache_id (D1->A);
2690                 output_saga_aln (out_file, D1->A);
2691                 }
2692         else if ( strm (out_format, "compress_aln"))
2693                 {
2694                   if (!D1)return 1;
2695                 compress_aln (D1->A);
2696                 output_saga_aln (out_file, D1->A);
2697                 } 
2698         else if (strm (out_format, "n_seq") ||strm (out_format, "nseq") )
2699                 {
2700                   if (!D1)return 1;
2701                 fp=vfopen ( out_file, "w");
2702                 fprintf ( fp, "%d\n", (D1->A)->nseq);
2703                 vfclose (fp);
2704                 }
2705         
2706         else if ( strm ( out_format, "thread_dna_on_prot_aln"))
2707                 {
2708                   if (!D1)return 1;
2709                 D1->A=thread_dnaseq_on_prot_aln (D1->S, D2->A);
2710                 output_saga_aln ( out_file, D1->A);
2711                 }
2712         else if ( strm ( out_format, "tdna_fasta_seq1"))
2713                 {if (!D1)return 1;
2714                 D1->A=translate_dna_aln (D1->A,0);
2715                 output_fasta_seq1 (out_file, D1->A);
2716                 }
2717         else if ( strm ( out_format, "tdna_aln"))
2718                 {if (!D1)return 1;              
2719                 D1->A=translate_dna_aln (D1->A,0);
2720                 output_saga_aln ( out_file, D1->A);
2721                 }
2722         else if ( strm ( out_format, "cdna_fasta_seq1"))
2723                 {if (!D1)return 1;              
2724                 D1->A= gene2prot(D1->A);
2725                 output_fasta_seq1 ( out_file, D1->A);
2726                 }
2727         else if ( strm ( out_format, "mutate_cdna_aln"))
2728                 {if (!D1)return 1;
2729                     D1->A= mutate_cdna_aln ( D1->A);
2730                     output_clustal_aln ( out_file, D1->A);
2731                 }
2732         else if ( strm ( out_format, "tdna_sp_aln"))
2733                 { if (!D1)return 1;
2734                 if ( !DST) 
2735                    {
2736                    fprintf ( stderr,"\n[You Need an evaluation File: Change the output format][FATAL:%s]\n", PROGRAM);  
2737                    myexit(EXIT_FAILURE);
2738                    }            
2739                (DST->A)=aln2number (DST->A);
2740                 D1->A=translate_splice_dna_aln (D1->A, DST->A);
2741                 output_saga_aln ( out_file, D1->A);
2742                 }
2743         else if (out_format && out_format[0] && (strcmp ( out_format,"rna_graph_fasta")==0))
2744                 {
2745                   if (!D1)return 1;
2746                 sprintf ( (D1->A)->seq_al[0], "%s",(DST->S)->seq[0]);
2747                 (D1->A)->nseq=0;
2748                 output_fasta_seq (out_file, DST->A);
2749                 }
2750         else if (strm ( out_format, "freq_mat"))
2751                 {
2752                   if (!D1)return 1;
2753                 output_freq_mat (out_file, D1->A);  
2754                 }
2755         else if (strm ( out_format, "maln_pval"))
2756                 {if (!D1)return 1;
2757                 output_maln_pval ( out_file, D1->A);
2758                 }
2759         else if ( strm ( out_format, "model_aln"))
2760                 {
2761                   if (!D1)return 1;
2762                 output_model_aln ( out_file, D1->A);
2763                 }
2764         else if (strncmp (out_format, "mult",4)==0)
2765                 {
2766                   if (!D1)return 1;
2767                 output_mult_fasta_seq ( out_file, D1->A, atoi(out_format+4));
2768                 }
2769         else if (strm (out_format, "conservation"))
2770           {
2771             output_conservation_statistics (out_file, D1->A);
2772           }
2773         else if (strm (out_format, "len"))
2774                 {
2775                   if (!D1)return 1;
2776                   output_statistics (out_file, D1->A, "nrl");
2777                 }
2778         else if ( strm (out_format, "name"))
2779                 {
2780                   if (!D1)return 1;
2781                   if ( D1->A)output_statistics (out_file, D1->A, "n");
2782                   if ( D1->T)
2783                     {
2784                       Sequence *TS;
2785                       TS=tree2seq(D1->T, NULL);print_array_char (vfopen(out_file, "w"), TS->name, TS->nseq, "\n");
2786                     }
2787                 }
2788         else if ( strm (out_format, "code_name"))
2789                 {
2790                   char **nl=NULL;
2791                   int num, n=0;
2792                   Sequence *TS;
2793                   FILE *lfp;
2794                   if ( D1->A){n=(D1->A)->nseq, nl=(D1->A)->name;}
2795                   if ( D1->T){TS=tree2seq(D1->T, NULL);nl=TS->name;n=TS->nseq;}
2796                   
2797                   lfp=vfopen (out_file, "w");
2798                   for ( num=0; num<n; num++)
2799                     fprintf (lfp, "\n%s C%d", nl[num], num+1);
2800                   fprintf (lfp, "\n");
2801                   vfclose (lfp);
2802                 }
2803         else if ( strm ( out_format, "seq2struc"))
2804                   {
2805                     output_seq2struc (out_file, D1->A);
2806                   }
2807         else if ( strstr  ( out_format, "pavie_age_channel"))
2808           {
2809             output_n_pavie_age_channel ( D1->S,out_file, atoi((out_format+strlen ("pavie_age_channel"))));
2810             return EXIT_SUCCESS;
2811           }
2812         else if ( strstr ( out_format, "age_matrix"))
2813                   {
2814                     output_age_matrix (out_file, atoi((out_format+10)));
2815                   }
2816         else if ( strm ( out_format, "transitions"))
2817                   {
2818                     output_transitions (out_file, D1->A);
2819                   }
2820         
2821         else if ( strncmp (out_format, "statistics",10)==0)
2822                 {
2823                   if (!D1)return 1;
2824                   
2825                   output_statistics (out_file, D1->A,out_format+10);
2826                 }
2827
2828
2829
2830
2831         else if ( strm4 (out_format, "newick_tree","newick","binary","nh"))
2832                 {
2833                   if (!D1)return 1;
2834                   
2835                   /*D1->T=unroot_tree(D1->T);*/
2836                   vfclose (print_tree ((D1->T), out_format, vfopen ( out_file, "w")));
2837                 }
2838         else if ( strncmp (out_format, "sarsim", 6)==0)
2839                 {
2840                   if (!D1)return 1;
2841                   compare_sar_sequence (D1->S, (D2 &&D2->S)?D2->S:D1->S, atoi(out_format+6));
2842                   return EXIT_SUCCESS;
2843                 }
2844         else if ( strncmp (out_format, "sim",3)==0)
2845                 {
2846                   if (!D1)return 1;
2847                   output_similarities (out_file, D1->A,out_format);
2848                 }
2849
2850         else if ( strncmp (out_format, "cov",3)==0)
2851                 {
2852                   if (!D1)return 1;
2853                   output_similarities (out_file, D1->A,out_format);
2854                 }
2855         else if ( strm (out_format, "stockholm_aln"))
2856           {
2857             output_stockholm_aln (out_file,D1->A, (D2)?D2->A:NULL);
2858           }
2859         else if ( strm (out_format, "pair_sim"))
2860           {
2861             if ( !D2)
2862               {
2863                 fprintf ( stderr, "\n-output=pair_sim: provide aln1 via -in and aln2 via -in2 [FATAL:%s]\n", PROGRAM);
2864                 myexit (EXIT_FAILURE);
2865               }
2866             output_similarities_pw (out_file, D1->A,D2->A,out_format);
2867           }
2868         else if ( strm (out_format, "matrix") || strm (out_format, "blast_matrix"))
2869           {
2870             output_blast_mat (D1->M, out_file);
2871           }
2872         else 
2873                 {
2874
2875                     fprintf ( stderr, "\n%s is an UNKNOWN OUTPUT FORMAT [FATAL:%s]\n",out_format, PROGRAM); 
2876                     myexit (EXIT_FAILURE);
2877                     
2878                 }
2879         
2880         //Remove the expansion
2881         if ( expanded)
2882           {
2883             free_aln (D1->A);
2884             D1->A=BUF_A;
2885           }
2886         return 0;
2887         }
2888 int is_in_format_list ( char *name)
2889         {
2890         if ( strcmp ( name, "saga_aln")==0)return 1;
2891         if ( strcmp ( name, "number_aln")==0)return 1;
2892         if ( strcmp ( name, "clustal_aln")==0)return 1; 
2893         if ( strcmp ( name, "fasta_aln")==0)return 1;
2894         if ( strcmp ( name, "number_fasta")==0)return 1;
2895         if ( strcmp ( name, "fasta_seq")==0)return 1;
2896         if ( strcmp ( name, "pdb")==0)return 1;
2897         if ( strcmp ( name, "msf_aln")==0)return 1;
2898         if ( strcmp ( name, "dali_aln")==0)return 1;
2899         if ( strcmp ( name, "dali_seq")==0)return 1;
2900         if ( strcmp ( name, "barton_list_tc")==0)return 1;
2901         if ( strcmp ( name, "est_prf")==0)return 1;
2902         
2903         if ( strcmp ( name, "gotoh_aln")==0)return 1;
2904         if ( strcmp ( name, "amps_aln")==0)return 1;
2905         if ( strcmp ( name, "pir_aln")==0)return 1;
2906         if ( strcmp ( name, "pir_seq")==0)return 1;
2907         if ( strcmp ( name, "est_fasta")==0)return 1;
2908         if ( strcmp ( name, "amps_sd_scores")==0)return 1;
2909         if ( strcmp ( name, "pima_aln")==0)return 1;
2910         if ( strcmp ( name, "dialign_aln")==0)return 1;
2911         if ( strcmp ( name, "gor_seq")==0)return 1;
2912         if ( strcmp ( name, "gor_struc")==0)return 1;
2913         if ( strcmp ( name, "stockholm_aln")==0)return 1;
2914         
2915         return 0;
2916         }
2917 int is_struc_in_format_list ( char *name)
2918         {
2919         if ( strcmp ( name, "rna_number")==0)return 1;
2920         if ( strcmp ( name, "fasta_seq")==0)return 1;
2921         return 0;
2922         }
2923 char *format_name2aln_format_name (char *name)
2924         {
2925           if ( strm (name, "gcg"))sprintf (name, "msf");
2926           else if ( strm (name, "fasta"))sprintf (name, "fasta_aln");
2927           return name;
2928         }
2929 int is_out_format_list ( char *name)
2930         {
2931           return main_output (NULL, NULL, NULL, name, NULL);    
2932         }
2933         
2934 int is_struc_out_format_list ( char *name)
2935         {
2936           return main_output (NULL, NULL, NULL, name, NULL);    
2937         }       
2938
2939 /**************************************************************************************************/
2940 /*************************************REFORMAT UTIL*************************************************/
2941 /**************************************************************************************************/
2942
2943 /*************************************REFORMAT IN**************************************************/
2944 /**************************************************************************************************/
2945 /*******************************************************************************************/
2946 /*                                                                                         */
2947 /*                                                                                         */
2948 /*                               READ COG FILE                                             */
2949 /*                                                                                         */
2950 /***************************************************************************************** */
2951          
2952 /*******************************************************************************************/
2953 /*                                                                                         */
2954 /*                                                                                         */
2955 /*                               INPUT WEIGHTS                                            */
2956 /*                                                                                         */
2957 /***************************************************************************************** */
2958         
2959 Weights* get_amps_sd_scores ( char *fname)
2960         {
2961         FILE *fp;
2962         char *buf;
2963         char *buf2;
2964         int nseq;
2965         Weights *W;
2966         int a, b,e;
2967         int c;
2968         float array[20];
2969         
2970         buf=vcalloc ( 1001, sizeof (char));
2971         buf2=vcalloc ( 1001, sizeof (char));
2972         
2973         fp=vfopen ( fname, "r");
2974         set_fp_id ( fp, "Index");
2975         buf=fgets ( buf, 1000, fp);
2976         fscanf ( fp, "%s", buf2);       
2977         
2978         nseq=0;
2979         while ( isalnum(buf2[0]) && !isalpha(buf2[0]))
2980                 {
2981                 nseq++;
2982                 buf=fgets ( buf, 1000, fp);
2983                 fscanf ( fp, "%s", buf2);
2984                 }
2985         vfclose ( fp);
2986         
2987         W=declare_weights (nseq);
2988         
2989         fp=vfopen ( fname, "r");
2990         set_fp_id ( fp, "Index");
2991         buf=fgets ( buf, 1000, fp);
2992         fscanf ( fp, "%s", buf2);       
2993         
2994         a=0;
2995         while ( isalnum(buf2[0]) && !isalpha(buf2[0]))
2996                 {
2997                 fp=set_fp_after_char (fp, '>');
2998                 fscanf ( fp, "%s",W->seq_name[a]);
2999                 buf=fgets ( buf, 1000, fp);
3000                 fscanf ( fp, "%s", buf2);
3001                 a++;
3002                 }
3003         buf=fgets ( buf, 1000, fp);
3004         c=1;
3005         while ( c!=0)
3006                 {
3007                 for ( e=0; e< 16; e++)
3008                         {
3009                         c=fscanf ( fp, "%f", &array[e]);
3010                         }
3011                 fscanf ( fp, "\n");
3012                 if ( c!=0)
3013                         {
3014                         
3015                         a=(int)array[0]-1;
3016                         b=(int)array[1]-1;
3017                         W->PW_ID[b][a]=W->PW_ID[a][b]=array[9];
3018                         W->PW_SD[b][a]=W->PW_SD[a][b]=array[14];
3019                         }
3020                 
3021                 }
3022         vfclose ( fp);
3023         sprintf ( W->comments, "SD WEIGHTS GENERATED WITH THE PROGRAM AMPS IN PAIRWISE MODE");
3024         vfree ( buf);
3025         return W;
3026         }
3027
3028 Weights *read_seq_weight (char **name, int nseq, char* seq_weight)
3029        {
3030        int a, p;
3031        Weights *W;
3032        float w;
3033        
3034        FILE *fp;
3035        char line[LONG_STRING];
3036        char sname[MAXNAMES];
3037        
3038        
3039        /*Read sequence weights:
3040         * comment
3041         name1 weight1
3042         .....
3043
3044
3045         NOTE:
3046         weights must be between 0 and 1;
3047         
3048         sequences not in S do not get any weight
3049         sequences in S but not in file get a weight of 1
3050        */
3051        if ( !is_single_seq_weight_file (seq_weight))
3052          {
3053            fprintf ( stderr, "\nERROR: File %s is not in Format SINGLE_SEQ_WEIGHT_FORMAT_01 [FATA:%s]", seq_weight,PROGRAM);
3054            myexit (EXIT_FAILURE);
3055            return NULL;
3056          }
3057        else
3058          {
3059            W=declare_weights(nseq);
3060            for ( a=0; a< nseq; a++)
3061              {
3062                sprintf ( W->seq_name[a], "%s", name[a]);
3063                W->SEQ_W[a]=1;
3064              }
3065            sprintf ( W->mode, "%s", seq_weight);
3066            fp=vfopen (seq_weight, "r");
3067            
3068            
3069            while ( fgets( line,LONG_STRING-1, fp))
3070              {
3071                if ( line[0]=='*' ||line[0]=='#' || isblanc(line));
3072                else
3073                  {
3074                    if (sscanf(line, "%s %f", sname, &w)!=2)continue;
3075                    if ( (p=name_is_in_list ( sname, W->seq_name, nseq, MAXNAMES-1))!=-1)
3076                      {
3077                        W->SEQ_W[p]=w;
3078                      }
3079                  }
3080              }
3081            vfclose (fp);
3082            return W;
3083          }
3084        }
3085        
3086   
3087 /*******************************************************************************************/
3088 /*                                                                                         */
3089 /*                                                                                         */
3090 /*                               INPUT MISC                                               */
3091 /*                                                                                         */
3092 /***************************************************************************************** */
3093
3094 char *** read_rename_file ( char *fname, int code)
3095 {
3096   int n;
3097   FILE *fp;
3098   char ***convert=NULL;
3099   
3100   convert=declare_arrayN(3, sizeof (char),count_n_line_in_file(fname) +1,2,MAXNAMES+1);
3101   fp=vfopen (fname, "r");
3102   n=0;
3103   if ( code==CODE)      while ( fscanf ( fp, "%s %s\n", convert[n][0], convert[n][1])==2)n++;
3104   else if (code==DECODE)while ( fscanf ( fp, "%s %s\n", convert[n][1], convert[n][0])==2)n++;
3105   vfclose (fp);
3106   return convert;
3107 }
3108
3109 void get_barton_list_tc_seq ( char *in_file)
3110         {
3111         FILE *fp, *fp_make, *fp_length, *fp_long;
3112         FILE *fp_small[9];
3113         
3114         static char *buf;
3115         int len_buf=10000;
3116         char name[100];
3117         
3118         char pwd[100];
3119         int a,c,nseq;
3120         int k=0;
3121         int *length;
3122         int longest=0;
3123         
3124         c=0;
3125         length=vcalloc ( 1000, sizeof(int));
3126         if ( buf==NULL)buf=vcalloc ( len_buf, sizeof (char));
3127         fp=vfopen (in_file, "r");
3128         fp_long=vfopen ( "barton_seq_list_large", "w");
3129         fp_make=vfopen ( "make_dir", "w");
3130         fp_length=vfopen ( "barton_length", "w");
3131         for ( a=0; a< 9; a++)
3132                 {
3133                 sprintf ( name, "barton_nseq%d",a);
3134                 fp_small[a]=vfopen ( name, "w");
3135                 }
3136         get_pwd (pwd);
3137         
3138         
3139         while ( c!=EOF)
3140                 {a=0;
3141                 while ( (c=fgetc(fp))!='#');
3142                 while ( (c=fgetc(fp))=='#');
3143                 ungetc ( c, fp);
3144                 while ( (c=fgetc(fp))!='#')buf[a++]=c;
3145                 buf[a]='\0';
3146                 
3147                 sprintf ( name, "%s", buf);
3148         
3149                 while ( (c=fgetc(fp))=='#');
3150                 
3151                 if ( c!=EOF)
3152                         {
3153                         a=0;
3154                         while ( (c=fgetc(fp))!='#' && c!=EOF)
3155                                 {
3156                                 buf[a++]=c;
3157                                 if (a==len_buf)
3158                                         {
3159                                         len_buf+=10000;
3160                                         buf=vrealloc ( buf, len_buf*sizeof (char));
3161                                         }
3162                                 } 
3163                         buf[a]='\0';
3164                         if (c!=EOF)
3165                                 {
3166                                 
3167                                 nseq=process_barton_entry ( buf,name);
3168                                 length[nseq]++;
3169                                 longest=(longest<nseq)?nseq:longest;
3170                                 
3171                                 if ( nseq<=8) fprintf ( fp_small[nseq], "%s.pep\n", name);
3172                                 else fprintf ( fp_long, "%s.pep\n",name);
3173                                 fprintf ( fp_make, "mkdir %s\nmv %s.pep %s\nmv %s.check %s\n", name, name, name, name, name);
3174                                 k++;
3175                                 }
3176                         
3177                                 
3178                         }
3179                 }
3180         
3181         vfclose (fp);
3182         vfclose (fp_long);
3183         for ( a=0; a< 9; a++)vfclose (fp_small[a]);
3184         vfclose (fp_make);
3185         for ( a=0; a<= longest; a++)fprintf ( fp_length, "%d: %d\n", a, length[a]);
3186         vfclose ( fp_length);
3187         
3188         }
3189         
3190 int process_barton_entry (char *buf, char *name)                        
3191     {       
3192     Alignment *LA;
3193     Sequence *LS;
3194     int a,c;
3195     static char *buf2;
3196     int clen=0;
3197     int current=0;
3198     int p=0;
3199     int max_len_seq=0;
3200     int min_len_seq=999999;
3201     int nseq=0;
3202     int l;
3203     char fname[100];
3204     char com_name[100];
3205     int rm_gap=1;
3206
3207     sprintf ( fname, "%s.pep", name);
3208     sprintf ( com_name, "%s.check",name);
3209     
3210     if ( buf2==NULL)buf2=vcalloc ( 10000, sizeof (char));
3211     a=0;                
3212     while (buf[a]!='\0')
3213                 {
3214                  if (buf[a]=='>')
3215                         {
3216                         a=get_string_line (a,2, buf, buf2); 
3217                         while ((c=buf[a++])!='*')
3218                                 if (isalnum (c)|| c=='.' || c=='-')
3219                                         clen++;
3220                          max_len_seq=(clen> max_len_seq)?clen: max_len_seq;
3221                          min_len_seq=(clen< min_len_seq)?clen: min_len_seq;
3222                          nseq++;
3223                          clen=0;
3224                         }
3225                 if ( buf[a]!='\0')a++;
3226                 }
3227     
3228     
3229     LS=declare_sequence (  min_len_seq,  max_len_seq,  nseq); 
3230     LS->nseq=nseq;
3231     
3232     
3233     for (a=0, current=0; current< nseq; current++) 
3234         {
3235         a=get_string_line ( a, 1, buf, buf2);
3236         sscanf ( buf2, ">P1;%s", LS->name[current]);
3237         a=get_string_line ( a, 1, buf, buf2);
3238         l=strlen ( buf2);
3239         buf2[l-1]='\0';
3240         sprintf ( LS->seq_comment[current], buf2);
3241         
3242         p=0;
3243         while ( (c=buf[a++])!='*')
3244                 {
3245                 if (isalpha (c))
3246                         LS->seq[current][p++]=tolower (c);
3247                 else if ( isgraph(c))
3248                         LS->seq[current][p++]=(c);
3249                 }       
3250         a++;
3251         }
3252     
3253     LA=declare_Alignment(LS);
3254     seq2aln ( LS, LA,rm_gap);
3255     output_fasta_seq (fname,LA);
3256     output_pir_check (com_name,LA->nseq, LA->seq_comment);
3257     free_Alignment ( LA);
3258     free_sequence ( LS, nseq);   
3259     
3260     return nseq;
3261     }
3262
3263         
3264         
3265
3266 Structure *read_rna_struc_number (Alignment *A,char *fname)
3267         {
3268         FILE *fp;
3269         int a;
3270         char x,y;
3271         float f;
3272         Sequence *SA;
3273         Structure *ST;
3274         int first, last;
3275         
3276         SA=declare_sequence ( A->len_aln, A->len_aln, 1);
3277         SA->len[0]=A->len[0];
3278         for ( a=0; a< SA->len[0]; a++)
3279                 SA->seq[0][a]='.';
3280         ST=declare_rna_structure_num (SA);
3281         ST->S=SA;
3282         
3283         fp=vfopen ( fname, "r");
3284         fscanf ( fp, "%c\n%d\n",&x, &(ST)->tot_list);
3285         for ( a=0; a<(ST)->tot_list; a++)
3286                 {
3287                 fscanf ( fp, "%d %d %d %c %c %f\n", &(ST)->list[a][0],&(ST)->list[a][1],&(ST)->list[a][2], &x, &y, &f);
3288                 (ST)->list[a][0]--;
3289                 (ST)->list[a][1]--;
3290                 (ST)->list[a][2]--;
3291                 if ( a==0)
3292                         {
3293                         (ST)->stem[0][0]=(ST)->list[a][0];
3294                         (ST)->stem[0][1]=a;
3295                         }
3296                 else if ( (ST)->stem[(ST)->tot_stem][0]==(ST)->list[a][0]);
3297                 else if ( (ST)->stem[(ST)->tot_stem][0]!=(ST)->list[a][0])
3298                         {
3299                         (ST)->stem[(ST)->tot_stem][2]=a-1;
3300                         (ST)->tot_stem++;
3301                         (ST)->stem[(ST)->tot_stem][0]=(ST)->list[a][0];
3302                         (ST)->stem[(ST)->tot_stem][1]=a;
3303                         }
3304                         
3305                 SA->seq[0][(ST)->list[a][1]]='-';
3306                 SA->seq[0][(ST)->list[a][2]]='-';
3307                 }
3308         (ST)->stem[(ST)->tot_stem][2]=a-1;      
3309         (ST)->tot_stem++;
3310         for ( a=0; a< (ST)->tot_stem; a++)
3311                 {
3312         
3313                 first=(ST)->stem[a][1];
3314                 last=(ST)->stem[a][2];
3315                 SA->seq[0][(ST)->list[first][1]]='>';
3316                 SA->seq[0][(ST)->list[first][2]]='<';
3317                 SA->seq[0][(ST)->list[last][1]]='>';
3318                 SA->seq[0][(ST)->list[last][2]]='<';    
3319                 }
3320         
3321         return ST;      
3322         }
3323                   
3324 Structure * declare_rna_structure_num (Sequence *SA)
3325         {
3326         Structure *ST;
3327         ST=vcalloc ( 1, sizeof ( Structure));
3328         ST->list=declare_int ( SA->len[0], 3);
3329         ST->stem=declare_int ( SA->len[0], 3);
3330         return ST;
3331         }
3332 char ** read_lib_list (char *name, int *n)
3333 {
3334
3335   char **lines;
3336   char **list;
3337   int a, b, l;
3338   
3339   lines=file2lines (name);
3340   l=atoi (lines[0]);
3341   
3342   list=vcalloc (l, sizeof (char*));
3343   for ( n[0]=0,a=1; a<l; a++,b++)
3344     if ( !strstr (lines[a], "TC_LIB_LIST_FORMAT_01"))list[n[0]++]=lines[a];
3345   vfree (lines);
3346   return list;
3347 }
3348   
3349 /*******************************************************************************************/
3350 /*                                                                                         */
3351 /*                                                                                         */
3352 /*                               INPUT SEQ                                                */
3353 /*                                                                                         */
3354 /***************************************************************************************** */
3355 char ***read_group ( char *file)
3356 {
3357   /*Format: Fasta like, the name fo the group followed with the name of the sequences
3358     ><Group name> <First Seq> <second seq> ....
3359     Groups must NOT be overlaping
3360     list[group_index][0]="number of sequences"
3361     list[group_index][1]="group name"
3362     list[group_index][2...N]="sequence"
3363   */ 
3364
3365   FILE *fp;
3366   char *buf;
3367   char ***list;
3368   int a, c, l;
3369
3370
3371   
3372   l=measure_longest_line_in_file (file)+1;
3373   buf=vcalloc (l, sizeof (char)); 
3374   list=vcalloc ( count_n_line_in_file (file )+1, sizeof (char**));
3375   
3376   fp=vfopen (file, "r");
3377   
3378   a=0;
3379   while ((c=fgetc(fp))!=EOF)
3380     {
3381       buf=fgets (buf,l-1, fp);
3382       if ( c=='>')list[a++]=string2list (buf);
3383     }
3384   vfclose (fp);
3385   vfree (buf);
3386   return list;
3387 }
3388 static Sequence* get_pdb_sequence_from_field   (char *fname, char *field);
3389 Sequence* get_pdb_sequence   (char *fname)
3390 {
3391   Sequence *S;
3392
3393   
3394   if ( (S=get_pdb_sequence_from_field(fname, "SEQRES"))!=NULL);
3395   else if ( (S=get_pdb_sequence_from_field(fname, "ATOM"))!=NULL)
3396     {
3397       add_warning (stderr,"Warning: Read Sequence from ATOM field in %s [%s:WARNING]", fname, PROGRAM);
3398     }
3399   else
3400     {
3401       add_warning ( stderr, "\nWARNING: failed to extract sequence from %s [%s:WARNING]\n", fname, PROGRAM);
3402       S=NULL;
3403     }
3404   return S;
3405 }
3406 static Sequence* get_pdb_sequence_from_field   (char *fname, char *field)
3407      {
3408          char *tp_name;
3409          char *command;
3410          char *pdbid;
3411          Sequence *S;
3412
3413
3414          command=vcalloc ( LONG_STRING, sizeof (char));
3415          tp_name=vtmpnam (NULL);
3416          
3417          sprintf ( command, "extract_from_pdb -seq_field %s -chain FIRST -infile \'%s\' -mode fasta > %s", field, check_file_exists(fname), tp_name);    
3418          if ( getenv4debug ("DEBUG_EXTRACT_FROM_PDB"))fprintf ( stderr, "\n[DEBUG_EXTRACT_FROM_PDB:get_pdb_seq] %s\n", command);
3419          my_system ( command);
3420          
3421
3422          S=get_fasta_sequence ( tp_name, NULL);
3423          if (S==NULL)return NULL;
3424          
3425          if ( (pdbid=get_pdb_id (fname))){sprintf ( S->name[0], "%s",pdbid);vfree (pdbid);} 
3426          S->nseq=1;
3427
3428          sprintf ( S->file[0], "%s", fname);
3429          S->max_len=S->min_len=S->len[0];
3430          if ( S->len[0]==0)
3431            {
3432              free_sequence (S, -1);
3433              S=NULL;
3434            }
3435
3436          vremove ( tp_name);
3437          vfree ( command);
3438          
3439          return S;
3440      }
3441
3442 char * get_pdb_file   ( char *fname)
3443      {
3444          char *file;
3445          int a, c;
3446          FILE *fp;
3447          
3448
3449          a=0;
3450          file=vcalloc ( sizeof (char),count_n_char_in_file ( fname)+1);
3451          fp=vfopen ( fname, "r");
3452          while ( (c=fgetc(fp))!=EOF)file[a++]=c;
3453          file[a]='\0'; 
3454          return file;
3455      }
3456          
3457 Sequence* get_struc_gor ( char *fname)
3458     {
3459     int nseq, min_len, max_len;
3460     int a, c;
3461     int len;
3462     char name[STRING];
3463     
3464
3465     FILE *fp;
3466     Sequence *S;
3467
3468     min_len=max_len=-1;
3469     fp=vfopen ( fname, "r");
3470     nseq=0;
3471     while ( (c=fgetc(fp))!=EOF)
3472             {
3473             if ( c!='!');
3474             else
3475                 {
3476                 nseq++;
3477                 fscanf ( fp, "%s %d", name, &len);
3478                 if (min_len==-1)min_len=max_len=len;
3479                 else
3480                     {
3481                     min_len=(len>min_len)?min_len:len;
3482                     max_len=(len>max_len)?len:max_len;
3483                     }
3484                 }
3485             
3486             }
3487     vfclose (fp);
3488    
3489     S=declare_sequence (  min_len,  max_len+1,nseq); 
3490     S->nseq=0;
3491     
3492     fp=vfopen (fname,"r");      
3493      while ( (c=fgetc(fp))!=EOF)
3494              {
3495              if ( c!='!');
3496              else
3497                 {
3498                 fscanf ( fp, "%s %d\n",S->name[S->nseq], &(S->len[S->nseq]));
3499                 
3500                 while ( (c=fgetc(fp))!='\n');
3501         
3502                 for ( a=0; a<S->len[S->nseq]; a++)
3503                     fscanf ( fp, " %*c %c %*f %*f %*f\n",&(S->seq[S->nseq][a]));
3504                 
3505                 S->seq[S->nseq][a]='\0';
3506                 while ( (c=fgetc(fp))!='!' && c!=EOF);
3507                 ungetc (c, fp);
3508                 S->nseq++;
3509                 }
3510              
3511              }
3512     vfclose (fp);
3513     return S;           
3514     }
3515                 
3516 Sequence* get_sequence_dali (char *fname)
3517     {
3518     Sequence *LS;
3519     FILE *fp;
3520     int c;
3521
3522     char name[100];
3523     int clen=0;
3524     int current=0;
3525     int p=0;
3526     int max_len_seq=0;
3527     int min_len_seq=999999;
3528     int nseq=0;
3529     
3530     if ((fp=vfopen (fname,"r"))==NULL)
3531          {printf ( "\nCOULDN'T OPEN %s",fname);
3532           myexit(EXIT_FAILURE);
3533          }  
3534     c=fgetc(fp);
3535     while (c!=EOF)
3536                 {
3537                  if (isdigit(c))
3538                         {
3539                         ungetc(c, fp);
3540                         fscanf (fp, "%s",name);
3541                         while (!isdigit(c=fgetc(fp)) && c!=EOF)
3542                                 if (isalnum (c) || c=='.' || c=='-')
3543                                         clen++;
3544                          max_len_seq=(clen> max_len_seq)?clen: max_len_seq;
3545                          min_len_seq=(clen< min_len_seq)?clen: min_len_seq;
3546                          nseq++;
3547                         clen=0;
3548                         }
3549                 else
3550                     c=fgetc (fp);
3551                 }
3552     vfclose (fp);
3553                 
3554     LS=declare_sequence (  min_len_seq,  max_len_seq+1,nseq); 
3555     LS->nseq=nseq;
3556     
3557     fp=vfopen (fname,"r");
3558     
3559     current=0;
3560     c=fgetc(fp);
3561         while (c!=EOF)
3562                 {
3563                 if (isdigit(c))
3564                         {
3565                         ungetc(c, fp);
3566                         fscanf_seq_name (fp, LS->name[current]);
3567                         p=0;
3568                         while (!isdigit(c=fgetc(fp)) && c!=EOF)
3569                                 {
3570                                 if (isalpha (c))
3571                                     LS->seq[current][p++]=tolower (c);
3572                                 else if ( c=='.')
3573                                     LS->seq[current][p++]='-';
3574                                 else if ( c=='-')
3575                                     LS->seq[current][p++]='-';
3576                                 }           
3577                         LS->seq[current][p]='\0';
3578                         LS->len[current]=strlen ( LS->seq[current]);
3579                         current++;
3580                         }
3581                 else
3582                     c=fgetc ( fp);
3583                 }
3584
3585     vfclose (fp);
3586     
3587     
3588     return LS;
3589     }   
3590
3591 Sequence* get_dialign_sequence (char *fname)
3592     {
3593     Sequence *LS;
3594     FILE *fp;
3595     int c;
3596
3597     char name[10000];
3598     int clen=0;
3599     int current=0;
3600     int p=0;
3601     int max_len_seq=0;
3602     int min_len_seq=999999;
3603     int nseq=0, l=0;
3604     char *buf;
3605     
3606     buf=vcalloc ( 1000, sizeof (char));
3607     if ((fp=vfopen (fname,"r"))==NULL)
3608          {printf ( "\nCOULDN'T OPEN %s",fname);
3609           myexit(EXIT_FAILURE);
3610          }  
3611     c=fgetc(fp);
3612     while (c!=EOF)
3613                 {
3614                  if (c=='>')
3615                         {fscanf (fp, "%s",name);
3616                         
3617                         buf=fgets ( buf, 1000, fp);
3618                         while ((c=fgetc(fp))!='>' && c!=EOF && c!=' ' && c!='\t')
3619                                 if (isalnum (c)|| is_gap(c))
3620                                         clen++;
3621                          max_len_seq=(clen> max_len_seq)?clen: max_len_seq;
3622                          min_len_seq=(clen< min_len_seq)?clen: min_len_seq;
3623                          nseq++;
3624                         clen=0;
3625                         }
3626                 else
3627                     c=fgetc (fp);
3628                 }
3629     vfclose (fp);
3630                 
3631     LS=declare_sequence (  min_len_seq,  max_len_seq, nseq); 
3632     LS->nseq=nseq;
3633     
3634     fp=vfopen (fname,"r");
3635     
3636     current=0;
3637     c=fgetc(fp);
3638         while (c!=EOF)
3639                 {
3640                 if (c=='>')
3641                         {
3642                           
3643                         fscanf_seq_name (fp, LS->name[current]);
3644                         l=strlen ( LS->name[current]);
3645                         if ( LS->name[current][l-1]==','||LS->name[current][l-1]==',')LS->name[current][l-1]='\0';
3646                         buf=fgets ( buf, 1000, fp);
3647                         p=0;
3648                         while ((c=fgetc(fp))!='>' && c!=EOF && c!=EOF && c!=' ' && c!='\t')
3649                                 if (isalpha (c))
3650                                     LS->seq[current][p++]=tolower (c);
3651                                 else if ( isgraph(c))
3652                                     LS->seq[current][p++]=(c);
3653                         LS->seq[current][p]='\0';
3654                         LS->len[current]=strlen ( LS->seq[current]);
3655                         current++;
3656                         }
3657                 else
3658                     c=fgetc ( fp);
3659                 }
3660
3661     vfclose (fp);
3662     return LS;
3663     }
3664
3665 Sequence* get_pima_sequence (char *fname)
3666     {
3667     Sequence *LS;
3668
3669     FILE *fp;
3670     int c;
3671
3672     char name[10000];
3673     int clen=0;
3674     int current=0;
3675     int p=0;
3676     int max_len_seq=0;
3677     int min_len_seq=999999;
3678     int nseq=0, l=0, len=0;
3679     char *buf, *buf2;
3680     char prefix[1000];
3681     
3682     sprintf (  prefix, "%s",fname);
3683     
3684     buf=strstr(prefix, "-");
3685     buf[0]='\0';
3686     len=strlen (prefix);
3687         
3688    
3689     
3690     buf=vcalloc ( 1000, sizeof (char));
3691     if ((fp=vfopen (fname,"r"))==NULL)
3692          {printf ( "\nCOULDN'T OPEN %s",fname);
3693           myexit(EXIT_FAILURE);
3694          }  
3695     c=fgetc(fp);
3696     while (c!=EOF)
3697                 {
3698                  if (c=='>')
3699                         {
3700                           fscanf_seq_name (fp,name);
3701                           if ( strlen(name)>=len && strncmp ( name, prefix, len)==0)
3702                                 {
3703                                   c=fgetc(fp);
3704                                 }
3705                           else
3706                                 {
3707                                 
3708                                 buf=fgets ( buf, 1000, fp);
3709                                 while ((c=fgetc(fp))!='>' && c!=EOF)
3710                                         if (isalnum (c)|| is_gap(c))
3711                                                 clen++;
3712                                  max_len_seq=(clen> max_len_seq)?clen: max_len_seq;
3713                                  min_len_seq=(clen< min_len_seq)?clen: min_len_seq;
3714                                 nseq++;
3715                                 clen=0;
3716                                 }
3717                         }
3718                 else
3719                         c=fgetc (fp);
3720                 }
3721     vfclose (fp);
3722                 
3723     LS=declare_sequence (  min_len_seq,  max_len_seq, nseq); 
3724     LS->nseq=nseq;
3725     
3726     fp=vfopen (fname,"r");
3727     
3728     current=0;
3729     c=fgetc(fp);
3730         while (c!=EOF)
3731                 {
3732                 if (c=='>')
3733                         {
3734                         fscanf_seq_name (fp,LS->name[current]);
3735                         if ( strlen(LS->name[current])>=len && strncmp ( LS->name[current], prefix, len)==0)
3736                                 c=fgetc (fp);
3737                         else
3738                                 {
3739                                 buf2=strstr (LS->name[current], ".");
3740                                 if ( buf2!=NULL) buf2[0]='\0';
3741                                  
3742                                 l=strlen ( LS->name[current]);
3743                                 if ( LS->name[current][l-1]==','||LS->name[current][l-1]==',')LS->name[current][l-1]='\0';
3744                                 buf=fgets ( buf, 1000, fp);
3745                                 p=0;
3746                                 while ((c=fgetc(fp))!='>' && c!=EOF)
3747                                         if (isalpha (c))
3748                                             LS->seq[current][p++]=tolower (c);
3749                                         else if ( isgraph(c))
3750                                             LS->seq[current][p++]=(c);
3751                                 LS->seq[current][p]='\0';
3752                                 LS->len[current]=strlen ( LS->seq[current]);
3753                                 current++;
3754                                 }
3755                         }
3756                 else
3757                     c=fgetc ( fp);
3758                 }
3759
3760     vfclose (fp);
3761     return LS;
3762     }
3763
3764 Sequence* perl_reformat2fasta (char *perl_command, char *fname)
3765     {
3766       char command[1000];
3767       char *file;
3768
3769       file=vtmpnam (NULL);
3770       
3771       check_program_is_installed ( perl_command,"", perl_command,EMAIL,IS_FATAL);
3772       sprintf ( command, "%s %s > %s", perl_command, fname, file);
3773       my_system ( command);
3774       return get_fasta_sequence (file, NULL);
3775     }
3776 Sequence* get_fasta_sequence_num (char *fname, char *comment_out)
3777     {
3778     Sequence *LS;
3779     char *buffer;
3780     FILE *fp;
3781     int a;
3782
3783     int   c;
3784     char *name;
3785     int clen=0;
3786     int current=0;
3787     int p=0;
3788     int max;
3789     int max_len_seq=0;
3790     int min_len_seq=0;
3791     int nseq=0, l=0;
3792  
3793     
3794     
3795     
3796     int *sub;
3797     
3798     buffer=vcalloc (1000, sizeof (char)); 
3799     name=vcalloc ( 100, sizeof (char));
3800
3801     nseq=count_n_char_x_in_file(fname, '>');
3802     min_len_seq=max=count_n_char_in_file(fname);
3803     sub=vcalloc (max+1, sizeof (int));
3804
3805     fp=vfopen (fname,"r");
3806
3807     
3808     c=fgetc(fp);
3809     while (c!=EOF)
3810                 {
3811                  if (c=='>')
3812                         {
3813                         fscanf_seq_name (fp,name);
3814                         while ((c=fgetc(fp))!='\n' && c!=EOF);
3815                         while ((c=fgetc(fp))!='>' && c!=EOF)
3816                                 if (isalnum (c)|| is_gap(c))
3817                                         clen++;
3818                          max_len_seq=(clen> max_len_seq)?clen: max_len_seq;
3819                          min_len_seq=(clen< min_len_seq)?clen: min_len_seq;
3820                          clen=0;
3821                         }
3822                 else
3823                     c=fgetc (fp);
3824                  
3825                 }  
3826
3827     vfclose (fp);               
3828     LS=declare_sequence (  min_len_seq,  max_len_seq,nseq); 
3829     
3830     LS->nseq=nseq;
3831     
3832     fp=vfopen (fname,"r");
3833     current=0;
3834     c=fgetc(fp);
3835     while (c!=EOF)
3836                 {
3837                 if (c=='>')
3838                         {
3839                         
3840                         fscanf_seq_name (fp,LS->name[current]);
3841                         l=strlen ( LS->name[current]);
3842                         if ( LS->name[current][l-1]==','||LS->name[current][l-1]==';')LS->name[current][l-1]='\0';
3843                         LS->name[current]=translate_name ( LS->name[current]);
3844                         a=0;
3845                         while ((c=fgetc(fp))!='\n' && c!=EOF && a<(COMMENT_SIZE-1))LS->seq_comment[current][a++]=c;
3846                         LS->seq_comment[current][a]='\0';
3847
3848                         
3849                         p=0;
3850                         while ((c=fgetc(fp))!='>' && c!=EOF)
3851                                 {
3852                                 if (isalnum (c))
3853                                     LS->seq[current][p++]=c;
3854                                 else if (is_gap(c))
3855                                     LS->seq[current][p++]=c;                            
3856                                 }
3857
3858                         LS->seq[current][p]='\0';
3859                         LS->len[current]=strlen ( LS->seq[current]);
3860
3861                         current++;
3862                 
3863                         }
3864                 else
3865                     c=fgetc ( fp);
3866                 }
3867                         
3868     
3869     vfclose (fp);
3870     
3871
3872     vfree (sub);
3873     vfree (name);
3874     vfree (buffer);
3875     return LS;
3876     }
3877
3878 Sequence *get_tree_file_list ( char *fname)
3879 {
3880
3881   char ***list;
3882   char *tmp;
3883   int a;
3884   FILE *fp;
3885   
3886   tmp=vtmpnam (NULL);
3887   list=file2list (fname, "\n");
3888   fp=vfopen (tmp, "w");
3889   a=0;
3890   while (list[a] && !isspace(list[a][1][0]))
3891     {
3892       char *s;
3893       s=file2string (list[a][1]);
3894       fprintf ( fp, ">%s\n%s\n", list[a][1], (s)?s:"");
3895       a++;
3896     }
3897   vfclose (fp);
3898   free_arrayN((void ***)list, 3);
3899   return get_fasta_tree (tmp, NULL);
3900 }
3901 Sequence *get_file_list ( char *fname)
3902 {
3903
3904   char ***list;
3905   char *tmp;
3906   int a;
3907   FILE *fp;
3908   
3909   tmp=vtmpnam (NULL);
3910   list=file2list (fname, "\n");
3911   fp=vfopen (tmp, "w");
3912   a=0;
3913   while (list[a] && !isspace(list[a][1][0]))
3914     {
3915
3916       fprintf ( fp, ">%s\n", list[a][1]);
3917       a++;
3918     }
3919   vfclose (fp);
3920   free_arrayN((void ***)list, 3);
3921   return get_fasta_sequence (tmp, NULL);
3922 }
3923 Sequence*get_fasta_tree (char *fname, char *comment_out)
3924 {
3925   Sequence *LS;
3926     char *buffer;
3927     FILE *fp;
3928     int a;
3929
3930     int   c;
3931     char *name;
3932     int clen=0;
3933     int current=0;
3934     int p=0;
3935     int max;
3936     int max_len_seq=0;
3937     int min_len_seq=0;
3938     int nseq=0, l=0;
3939  
3940     
3941     
3942     
3943     int *sub;
3944     
3945     buffer=vcalloc (1000, sizeof (char)); 
3946     name=vcalloc ( 100, sizeof (char));
3947
3948     nseq=count_n_char_x_in_file(fname, '>');
3949     min_len_seq=max=count_n_char_in_file(fname);
3950     sub=vcalloc (max+1, sizeof (int));
3951
3952     fp=vfopen (fname,"r");
3953
3954     
3955     c=fgetc(fp);
3956     while (c!=EOF)
3957                 {
3958                  if (c=='>')
3959                         {
3960                         fscanf_seq_name (fp,name);
3961                         while ((c=fgetc(fp))!='\n' && c!=EOF);
3962                         while ((c=fgetc(fp))!='>' && c!=EOF)
3963                           if (isgraph(c))
3964                             clen++;
3965                          max_len_seq=(clen> max_len_seq)?clen: max_len_seq;
3966                          min_len_seq=(clen< min_len_seq)?clen: min_len_seq;
3967                          clen=0;
3968                         }
3969                 else
3970                     c=fgetc (fp);
3971                  
3972                 }  
3973
3974     vfclose (fp);               
3975     LS=declare_sequence (  min_len_seq,  max_len_seq,nseq); 
3976     
3977     LS->nseq=nseq;
3978     
3979     fp=vfopen (fname,"r");
3980     current=0;
3981     c=fgetc(fp);
3982     while (c!=EOF)
3983                 {
3984                 if (c=='>')
3985                         {
3986                         
3987                         fscanf_seq_name (fp,LS->name[current]);
3988                         l=strlen ( LS->name[current]);
3989                         if ( LS->name[current][l-1]==','||LS->name[current][l-1]==';')LS->name[current][l-1]='\0';
3990                         LS->name[current]=translate_name ( LS->name[current]);
3991                         a=0;
3992                         while ((c=fgetc(fp))!='\n' && c!=EOF && a<(COMMENT_SIZE-1))LS->seq_comment[current][a++]=c;
3993                         LS->seq_comment[current][a]='\0';
3994
3995                         
3996                         p=0;
3997                         while ((c=fgetc(fp))!='>' && c!=EOF)
3998                                 {
3999                                   LS->seq[current][p++]=c;
4000                                 }
4001
4002                         LS->seq[current][p]='\0';
4003                         LS->len[current]=strlen ( LS->seq[current]);
4004
4005                         current++;
4006                 
4007                         }
4008                         
4009                 else
4010                     c=fgetc ( fp);
4011                 }
4012                         
4013     
4014     vfclose (fp);
4015     
4016
4017     vfree (sub);
4018     vfree (name);
4019     vfree (buffer);
4020     
4021     return LS;
4022 }
4023 Sequence* get_fasta_sequence_raw (char *fname, char *comment_out)
4024     {
4025     Sequence *LS;
4026     char *buffer;
4027     FILE *fp;
4028     int a;
4029
4030     int   c;
4031     char *name;
4032     int clen=0;
4033     int current=0;
4034     int p=0;
4035     int max;
4036     int max_len_seq=0;
4037     int min_len_seq=0;
4038     int nseq=0, l=0;
4039  
4040     
4041     
4042     
4043     int *sub;
4044     
4045     buffer=vcalloc (1000, sizeof (char)); 
4046     name=vcalloc ( 100, sizeof (char));
4047
4048     nseq=count_n_char_x_in_file(fname, '>');
4049     min_len_seq=max=count_n_char_in_file(fname);
4050     sub=vcalloc (max+1, sizeof (int));
4051
4052     fp=vfopen (fname,"r");
4053
4054     
4055     c=fgetc(fp);
4056     while (c!=EOF)
4057                 {
4058                  if (c=='>')
4059                         {
4060                         fscanf_seq_name (fp,name);
4061                         while ((c=fgetc(fp))!='\n' && c!=EOF);
4062                         while ((c=fgetc(fp))!='>' && c!=EOF)
4063                           if (isgraph(c))
4064                             clen++;
4065                          max_len_seq=(clen> max_len_seq)?clen: max_len_seq;
4066                          min_len_seq=(clen< min_len_seq)?clen: min_len_seq;
4067                          clen=0;
4068                         }
4069                 else
4070                     c=fgetc (fp);
4071                  
4072                 }  
4073
4074     vfclose (fp);               
4075     LS=declare_sequence (  min_len_seq,  max_len_seq,nseq); 
4076     
4077     LS->nseq=nseq;
4078     
4079     fp=vfopen (fname,"r");
4080     current=0;
4081     c=fgetc(fp);
4082     while (c!=EOF)
4083                 {
4084                 if (c=='>')
4085                         {
4086                         
4087                         fscanf_seq_name (fp,LS->name[current]);
4088                         l=strlen ( LS->name[current]);
4089                         if ( LS->name[current][l-1]==','||LS->name[current][l-1]==';')LS->name[current][l-1]='\0';
4090                         LS->name[current]=translate_name ( LS->name[current]);
4091                         a=0;
4092                         while ((c=fgetc(fp))!='\n' && c!=EOF && a<(COMMENT_SIZE-1))LS->seq_comment[current][a++]=c;
4093                         LS->seq_comment[current][a]='\0';
4094
4095                         
4096                         p=0;
4097                         while ((c=fgetc(fp))!='>' && c!=EOF)
4098                                 {
4099                                   //if (c<'A')c+='z';
4100                                   if (c!='\n')LS->seq[current][p++]=c;
4101                                 }
4102
4103                         LS->seq[current][p]='\0';
4104                         LS->len[current]=strlen ( LS->seq[current]);
4105
4106                         current++;
4107                 
4108                         }
4109                         
4110                 else
4111                     c=fgetc ( fp);
4112                 }
4113                         
4114     
4115     vfclose (fp);
4116     
4117
4118     vfree (sub);
4119     vfree (name);
4120     vfree (buffer);
4121     return LS;
4122     }
4123 Sequence* get_fasta_sequence (char *fname, char *comment_out)
4124     {
4125     Sequence *LS;
4126     Sequence *pdb_S;
4127     int a;
4128
4129     char *pdb_name;
4130     
4131     char *buffer;
4132     FILE *fp;
4133
4134     int   c;
4135     char *name;
4136     int clen=0;
4137     int current=0;
4138     int p=0;
4139     int max;
4140     int max_len_seq=0;
4141     int min_len_seq=0;
4142     int nseq=0, l=0;
4143     char *sub;
4144     int disk=0;
4145     int coor=0;
4146     char *test;
4147     
4148        
4149     buffer=vcalloc (1000, sizeof (char)); 
4150     name=vcalloc ( 10000, sizeof (char));
4151
4152     nseq=count_n_char_x_in_file(fname, '>');
4153     if (disk==1 || get_int_variable ("use_disk") || getenv ("SEQ_ON_DISK_4_TCOFFEE")){disk=1;}
4154     if ( nseq==0)
4155       {
4156         vfree (buffer); vfree (name);
4157         return NULL;
4158       }
4159     
4160     min_len_seq=max=count_n_char_in_file(fname);
4161     sub=vcalloc (max+1, sizeof (char));
4162
4163     fp=vfopen (fname,"r");
4164
4165     nseq=0;
4166     c=fgetc(fp);
4167     while (c!=EOF)
4168                 {
4169                  if (c=='>')
4170                         {
4171                           nseq++;
4172                           fscanf_seq_name (fp,name);
4173                           while ((c=fgetc(fp))!='\n' && c!=EOF);
4174                           while ((c=fgetc(fp))!='>' && c!=EOF)
4175                             {
4176                               if (isalnum (c)|| is_gap(c))
4177                                 sub[clen++]=c;
4178                             }
4179                           
4180                           if (strm (sub, "PDB"))
4181                             {
4182                               pdb_name=get_pdb_struc(name,0, 0);
4183                               pdb_S=get_pdb_sequence (pdb_name);
4184                               if (pdb_S)
4185                                 {
4186                                   clen=strlen( pdb_S->seq[0]);
4187                                   free_sequence ( pdb_S,1);
4188                                 }
4189                               else
4190                                 clen=0;
4191                               
4192                             }
4193                                                   
4194                           max_len_seq=(clen> max_len_seq)?clen: max_len_seq;
4195                           min_len_seq=(clen< min_len_seq)?clen: min_len_seq;
4196                           clen=0;
4197                         }
4198                  else
4199                    c=fgetc (fp);
4200                  
4201                 }  
4202     
4203     vfclose (fp);       
4204     
4205     
4206     if ( disk==0)
4207       LS=declare_sequence (  min_len_seq,  max_len_seq,nseq); 
4208     else
4209       {
4210         LS=declare_sequence (0,0,nseq);
4211         for (a=0; a<nseq; a++)LS->seq[a]=NULL;
4212       }
4213     LS->nseq=nseq;
4214     
4215     fp=vfopen (fname,"r");
4216     current=0;
4217     c=fgetc(fp);coor++;
4218     
4219     while (c!=EOF)
4220                 {
4221                 if (c=='>')
4222                         {
4223                           coor+=fscanf_seq_name (fp, LS->name[current]);
4224
4225                           
4226                           l=strlen ( LS->name[current]);
4227                           if ( LS->name[current][l-1]==','||LS->name[current][l-1]==';')LS->name[current][l-1]='\0';
4228                           LS->name[current]=translate_name ( LS->name[current]);                        
4229                           a=0;
4230                           while ((c=fgetc(fp))!='\n' && c!=EOF && a<(COMMENT_SIZE-1)){LS->seq_comment[current][a++]=c;coor++;}
4231                           coor++;
4232                           
4233                           LS->seq_comment[current][a]='\0';
4234                           
4235                           p=0;
4236                           while ((c=fgetc(fp))!='>' && c!=EOF)
4237                             {
4238                               coor++;
4239                               
4240                               if (!isspace(c))
4241                                 {
4242                                   if (p==0)LS->dc[current][0]=coor;
4243                                   
4244                                   if (disk==0)LS->seq[current][p++]=c;
4245                                   else p++;
4246                                 }
4247                               
4248                               LS->dc[current][1]=coor;
4249                             }
4250                           coor++;
4251                           
4252                           if ( disk==0)LS->seq[current][p]='\0';
4253                           
4254                           if (LS->seq[current] && strm (LS->seq[current], "PDB"))
4255                             {
4256                               
4257                               pdb_name=get_pdb_struc(LS->name[current],0, 0);
4258                               pdb_S=get_pdb_sequence (pdb_name);
4259                               if (pdb_S)
4260                                 {
4261                                   sprintf ( LS->seq[current], "%s", pdb_S->seq[0]);
4262                                   clen=strlen( pdb_S->seq[0]);
4263                                   free_sequence ( pdb_S, 1);
4264                                 }
4265                               else
4266                                 {
4267                                   add_warning (stderr, "WARNING: Could not fetch PDB file: %s", pdb_name);
4268                                 }
4269                             }
4270                         
4271                         
4272                           LS->len[current]=p;
4273                           current++;            
4274                         }
4275                         
4276                 else
4277                   {
4278                     c=fgetc ( fp);
4279                     coor++;
4280                   }
4281                 }
4282      
4283     vfclose (fp);
4284     vfree (sub);
4285     vfree (name);
4286     vfree (buffer);
4287     //LS=clean_sequence (LS);
4288     
4289     return LS;
4290     }
4291
4292 Sequence* get_sub_fasta_sequence (char *fname, char *comment_out)
4293     {
4294     Sequence *LS;
4295     
4296     FILE *fp;
4297
4298     int c;
4299     char name[100];
4300     int clen=0;
4301     int current=0;
4302     int p=0;
4303     int max;
4304     int max_len_seq=0;
4305     int min_len_seq=0;
4306     int nseq=0, l=0;
4307     char *buf;
4308     
4309     
4310     
4311     int *sub;
4312
4313     nseq=count_n_char_x_in_file(fname, '>');
4314     min_len_seq=max=count_n_char_in_file(fname);
4315     sub=vcalloc (max+1, sizeof (int));
4316     buf=vcalloc ( max+1, sizeof (char));
4317     fp=vfopen (fname,"r");
4318
4319     
4320     c=fgetc(fp);
4321     while (c!=EOF)
4322                 {
4323                  if (c=='>')
4324                         {
4325                         fscanf_seq_name (fp,name);
4326                         while ((c=fgetc(fp))!='\n' && c!=EOF);
4327                         buf=fgets ( buf,max, fp);
4328                         while ((c=fgetc(fp))!='>' && c!=EOF)
4329                                 if (isalnum (c)|| is_gap(c))
4330                                         clen++;
4331                          max_len_seq=(clen> max_len_seq)?clen: max_len_seq;
4332                          min_len_seq=(clen< min_len_seq)?clen: min_len_seq;
4333                          clen=0;
4334                         }
4335                 else
4336                     c=fgetc (fp);
4337                  
4338                 }  
4339
4340     vfclose (fp);               
4341     LS=declare_sequence (  min_len_seq,  max_len_seq,nseq); 
4342     LS->nseq=nseq;
4343     
4344     fp=vfopen (fname,"r");
4345     current=0;
4346     c=fgetc(fp);
4347     while (c!=EOF)
4348                 {
4349                 if (c=='>')
4350                         {
4351                         
4352                         fscanf_seq_name (fp,LS->name[current]);
4353                         l=strlen ( LS->name[current]);
4354                         if ( LS->name[current][l-1]==','||LS->name[current][l-1]==';')LS->name[current][l-1]='\0';
4355                         LS->name[current]=translate_name ( LS->name[current]);
4356                         while ((c=fgetc(fp))!='\n' && c!=EOF);
4357                 
4358                         p=0;
4359                         while ((c=fgetc(fp))!='>' && c!=EOF)
4360                                 {
4361                                 if (isalpha (c))
4362                                     LS->seq[current][p++]=tolower (c);
4363                                 else if (is_gap(c))
4364                                     LS->seq[current][p++]=(c);                          
4365                                 }
4366
4367                         LS->seq[current][p]='\0';
4368                         LS->len[current]=strlen ( LS->seq[current]);
4369
4370                         current++;
4371                 
4372                         }
4373                         
4374                 else
4375                     c=fgetc ( fp);
4376                 }
4377                         
4378     
4379     vfclose (fp);
4380     
4381
4382     vfree (sub);
4383     return LS;
4384     }
4385 Sequence* get_pir_sequence (char *fname, char *comment_out)
4386     {
4387     Sequence *LS;
4388
4389     FILE *fp;
4390     int c;
4391
4392     char name[100];
4393     int clen=0;
4394     int current=0;
4395     int p=0;
4396     int max_len_seq=0;
4397     int min_len_seq=999999;
4398     int nseq=0, l=0;
4399     char *buf;
4400     
4401     buf=vcalloc ( 1000, sizeof (char));
4402     if ((fp=vfopen (fname,"r"))==NULL)
4403          {printf ( "\nCOULDN'T OPEN %s",fname);
4404           myexit(EXIT_FAILURE);
4405          }  
4406     c=fgetc(fp);
4407     while (c!=EOF)
4408                 {
4409                  if (c=='>')
4410                         {
4411                         if ( (c=fgetc(fp))=='P')while ( (c=fgetc(fp))!=';');
4412                         else ungetc ( c, fp);
4413                         fscanf_seq_name (fp,name);
4414                         
4415                         buf=fgets ( buf, 1000, fp);
4416                         while ((c=fgetc(fp))!='>' && c!=EOF)
4417                                 if (isalnum (c)|| is_gap(c))
4418                                         clen++;
4419                          max_len_seq=(clen> max_len_seq)?clen: max_len_seq;
4420                          min_len_seq=(clen< min_len_seq)?clen: min_len_seq;
4421                          nseq++;
4422                         clen=0;
4423                         }
4424                 else
4425                     c=fgetc (fp);
4426                 }
4427     vfclose (fp);
4428
4429
4430    
4431     LS=declare_sequence (  min_len_seq,  max_len_seq,nseq); 
4432     LS->nseq=nseq;
4433     
4434     fp=vfopen (fname,"r");
4435     
4436     current=0;
4437     c=fgetc(fp);
4438         while (c!=EOF)
4439                 {
4440                 if (c=='>')
4441                         {
4442                         if ( (c=fgetc(fp))=='P')while ( (c=fgetc(fp))!=';');
4443                         else ungetc ( c, fp);
4444
4445                         fscanf_seq_name (fp,LS->name[current]);
4446                 
4447                         l=strlen ( LS->name[current]);
4448                         if ( LS->name[current][l-1]==','||LS->name[current][l-1]==',')LS->name[current][l-1]='\0';
4449                         LS->name[current]=translate_name ( LS->name[current]);
4450                         buf=fgets ( buf, 1000, fp);
4451                         
4452                         LS->seq_comment[current]=fgets ( LS->seq_comment[current],COMMENT_SIZE-1, fp);
4453                         LS->seq_comment[current][strlen(LS->seq_comment[current])-1]='\0';
4454                         p=0;
4455                         while ((c=fgetc(fp))!='>' && c!=EOF)
4456                                 if (isalpha (c))
4457                                     LS->seq[current][p++]=tolower (c);
4458                                 else if ( !isspace(c) && c!='*')
4459                                     LS->seq[current][p++]=(c);
4460                         LS->seq[current][p]='\0';
4461                         LS->len[current]=strlen ( LS->seq[current]);
4462                         current++;
4463                         }
4464                 else
4465                     c=fgetc ( fp);
4466                 }
4467
4468     vfclose (fp);
4469     if (comment_out!=NULL) output_pir_check ( comment_out,LS->nseq, LS->seq_comment);
4470     return LS;
4471     }
4472
4473 Sequence* get_gor_sequence (char *fname, char *comment_out)
4474     {
4475     Sequence *LS;
4476
4477     FILE *fp;
4478     int c;
4479
4480     char name[100];
4481     int clen=0;
4482     int current=0;
4483     int p=0;
4484     int max_len_seq=0;
4485     int min_len_seq=99999;
4486     int nseq=0;
4487     char *buf;
4488     
4489     buf=vcalloc ( 1000, sizeof (char));
4490     if ((fp=vfopen (fname,"r"))==NULL)
4491          {printf ( "\nCOULDN'T OPEN %s",fname);
4492           myexit(EXIT_FAILURE);
4493          }  
4494     c=fgetc(fp);
4495     while (c!=EOF)
4496                 {
4497                  if (c=='!')
4498                         {
4499                         fscanf_seq_name (fp,name);
4500                         
4501                         buf=fgets ( buf, 1000, fp);
4502                         while ((c=fgetc(fp))!='!' && c!=EOF)
4503                                 if (isalnum (c)|| is_gap(c))
4504                                         clen++;
4505                          max_len_seq=(clen> max_len_seq)?clen: max_len_seq;
4506                          min_len_seq=(clen< min_len_seq)?clen: min_len_seq;
4507                          nseq++;
4508                         clen=0;
4509                         }
4510                 else
4511                     c=fgetc (fp);
4512                 }
4513     vfclose (fp);
4514                 
4515     LS=declare_sequence (  min_len_seq,  max_len_seq,nseq); 
4516     LS->nseq=nseq;
4517     
4518     fp=vfopen (fname,"r");
4519     
4520     current=0;
4521     c=fgetc(fp);
4522         while (c!=EOF)
4523                 {
4524                 if (c=='!')
4525                         {
4526                         
4527                        
4528                         fscanf_seq_name (fp,LS->name[current]);
4529                         LS->name[current]=translate_name ( LS->name[current]);
4530                         buf=fgets ( buf, 1000, fp);
4531                         
4532                         p=0;
4533                         while ((c=fgetc(fp))!='!' && c!=EOF)
4534                                 if (isalnum (c)|| is_gap(c))
4535                                     LS->seq[current][p++]=tolower (c);
4536                                 
4537                         LS->seq[current][p]='\0';
4538                         LS->len[current]=strlen ( LS->seq[current]);
4539                         current++;
4540                         }
4541                 else
4542                     c=fgetc ( fp);
4543                 }
4544
4545     vfclose (fp);
4546
4547     return LS;
4548     }
4549 Sequence* get_swissprot_sequence (char *fname, char *comment_out)
4550     {
4551     Sequence *LS;
4552     FILE *fp;
4553     int c;
4554     char *buf;    
4555     int nseq=0;
4556     int len, max_len_seq=0, min_len_seq=0;
4557     
4558     if ( !check_file_exists(fname))
4559       {printf ( "\nCOULDN'T OPEN %s",fname);
4560           myexit(EXIT_FAILURE);
4561       }  
4562
4563     buf=vcalloc (LONG_STRING+1, sizeof (char));
4564     fp=NULL;   
4565     while ( (fp=find_token_in_file(fname,fp,"\nSQ")))
4566       {
4567         nseq++;
4568         fgets (buf, LONG_STRING, fp);
4569         len=0;
4570         while ((c=fgetc(fp))!='/')if(isalpha(c))len++;
4571         if ( max_len_seq==0)max_len_seq=min_len_seq=len;
4572         else
4573           {
4574             max_len_seq=MAX(len, max_len_seq);
4575             min_len_seq=MIN(len, min_len_seq);
4576           }
4577       }
4578
4579     LS=declare_sequence (  min_len_seq,  max_len_seq,nseq);     
4580     LS->nseq=0;
4581     
4582     fp=NULL;
4583     while ( (fp=find_token_in_file(fname,fp,"\nID")))
4584       {
4585         fscanf_seq_name (fp, LS->name[LS->nseq]);
4586         fp=find_token_in_file(fname,fp,"\nSQ");
4587         fgets (buf, LONG_STRING, fp);
4588         while ((c=fgetc(fp))!='/')if (isalpha(c))LS->seq[LS->nseq][LS->len[LS->nseq]++]=c;
4589         LS->seq[LS->nseq][LS->len[LS->nseq]]='\0';
4590         LS->nseq++;
4591       }
4592
4593    
4594     return LS;
4595     }
4596 int fscanf_seq_name ( FILE *fp, char *sname)
4597 {
4598   static char *name;
4599   int r;
4600   if ( !sname) return 0;
4601   
4602   if ( !name)name=vcalloc ( 10000, sizeof (char));
4603   fscanf (fp, "%s", name);
4604   r=strlen (name);
4605   if ( strlen (name)>MAXNAMES)
4606     add_warning (stderr, "\nWARNING: Seq Name Too long: [%s]. Truncated to %d", name, MAXNAMES);
4607   name[MAXNAMES]='\0';
4608   sprintf ( sname, "%s", name);
4609   return r;
4610 }
4611
4612 /*******************************************************************************************/
4613 /*                                                                                         */
4614 /*                                                                                         */
4615 /*                               INPUT ALN                                                 */
4616 /*                                                                                         */
4617 /***************************************************************************************** */
4618 void undump_msa ( Alignment *A, char *tmp)
4619 {
4620   FILE *fp;
4621   int m;
4622   char *buf;
4623   int index;
4624   
4625   if ( !A || !tmp || !check_file_exists (tmp))return;
4626   m=measure_longest_line_in_file (tmp );
4627   A=realloc_aln2 ( A,A->max_n_seq,m+1);
4628
4629   buf=vcalloc (m+1, sizeof (char));
4630   fp=vfopen (tmp, "r");
4631   while (fscanf (fp, "%d %s\n", &index, buf)==2)
4632     {
4633       sprintf ( A->seq_al[index], "%s", buf);
4634     }
4635   vfclose (fp);
4636   vfree (buf);
4637 }
4638 void dump_msa ( char *file,Alignment *A, int nseq, int *lseq)
4639 {
4640   FILE *fp;
4641   int a;
4642   fp=vfopen (file, "w");
4643   for (a=0; a<nseq; a++)
4644     fprintf ( fp, "%d %s\n", lseq[a], A->seq_al[lseq[a]]);
4645   vfclose (fp);
4646 }
4647
4648 void read_aln (char *file_name, Alignment *A)
4649 {
4650   char *tmp_name;
4651   Sequence *S;
4652   
4653
4654   tmp_name=vtmpnam (NULL);
4655   if (printf_system ( "clustalw_aln2fasta_aln.pl %s > %s",file_name, tmp_name)!=EXIT_SUCCESS)
4656     {
4657       printf_exit ( EXIT_FAILURE, stderr, "Could Not Read File %s [FATAL:%s]\n", file_name, PROGRAM);
4658     }
4659   else
4660     {
4661       S=get_fasta_sequence ( tmp_name,NULL);
4662       A=seq2aln (S, A, 0);  
4663     }
4664   return;
4665 }
4666 void read_stockholm_aln (char *file_name, Alignment *A)
4667 {
4668   char *tmp_name;
4669   Sequence *S;
4670   
4671
4672   tmp_name=vtmpnam (NULL);
4673   if (printf_system ( "clustalw_aln2fasta_aln.pl %s > %s",file_name, tmp_name)!=EXIT_SUCCESS)
4674     {
4675       printf_exit ( EXIT_FAILURE, stderr, "Could Not Read File %s [FATAL:%s]\n", file_name, PROGRAM);
4676     }
4677   else
4678     {
4679       int a;
4680       S=get_fasta_sequence ( tmp_name,NULL);
4681       for (a=0; a<S->nseq; a++)
4682         {
4683           if (strstr (S->name[a], "_stockholm"))
4684             {
4685               substitute ( S->name[a], "_stockholmspace_", " ");
4686               substitute ( S->name[a], "_stockholmhasch_", "#");
4687             }
4688         }
4689       A=seq2aln (S, A, 0);  
4690     }
4691   return;
4692 }
4693 Alignment* read_blast_aln ( char *file_name, Alignment *A)
4694 {
4695   char *tmp_name;
4696   Sequence *S;
4697   int type;
4698   int a;
4699   
4700   if ( !(type=is_blast_file (file_name)))
4701     {
4702       myexit (EXIT_FAILURE);
4703     }
4704   tmp_name=vtmpnam ( NULL);
4705   if (type==BLAST_TXT)
4706     {
4707       printf_system("cat %s | blast_aln2fasta_aln.pl | fasta_aln2fasta_aln_unique_name.pl >%s", file_name, tmp_name);
4708     }
4709   else if (type==BLAST_XML)
4710     {
4711       
4712       printf_system("blast_xml2fasta_aln.pl %s >%s", file_name, tmp_name);
4713     }
4714
4715   main_read_aln (tmp_name, A);
4716   return A;
4717 }
4718
4719
4720 void read_number_aln ( char *file_name, Alignment *A)
4721    {
4722     FILE *fp, *fp2;
4723     int * ptr_aln;
4724     int a,b,d;
4725     int c;
4726     char *buf=NULL;
4727
4728     int tot=0;
4729     int flag=0;
4730     char *fname;   
4731     int n_comment=0;
4732
4733     int nseq=0;
4734     int max_len=0;
4735
4736     
4737     fp=vfopen ( file_name, "r");
4738     
4739     fname=vtmpnam(NULL);
4740     fp2=vfopen ( fname, "w");
4741     while ( (c=fgetc(fp))!=EOF)
4742         {
4743             fprintf ( fp2, "%c", c);
4744         }
4745     vfclose (fp);
4746     vfclose (fp2);
4747
4748   
4749     /*1 Count The number of sequences*/ 
4750     fp=vfopen ( fname, "r");
4751     buf=vfgets ( buf,fp);
4752     if ( !isblanc (buf));
4753     while ( isblanc (buf))
4754         {
4755           buf=vfgets ( buf, fp);
4756         }
4757     while (!isblanc (buf))
4758         {
4759         buf=vfgets ( buf,fp);
4760         }
4761     while ( !isalnum ((c=fgetc(fp))))
4762         {       
4763         ungetc(c,fp);
4764         buf=vfgets ( buf,fp);           
4765         }
4766     
4767     if ( c!='\n')ungetc(c,fp);
4768     
4769     while ( isalnum ((c=fgetc(fp))))
4770         {
4771         ungetc(c,fp);           
4772         a=0;
4773         while ( isgraph ((c=fgetc(fp))));                       
4774         nseq++;
4775         buf=vfgets ( buf, fp);
4776         }    
4777     vfclose (fp);
4778
4779     /*DONE*/
4780     /*2 get_max_len*/
4781     max_len=count_n_char_in_file(fname)/nseq;
4782     A=realloc_alignment2( A, nseq+1, max_len+1);
4783
4784     /*DONE*/
4785         
4786    
4787     fp=vfopen ( fname, "r");
4788     buf=vfgets ( buf, fp);
4789     if ( !isblanc (buf))sprintf (A->aln_comment[n_comment++], "%s", buf);
4790     while ( isblanc (buf))
4791         {
4792         buf=vfgets ( buf,fp);           
4793         }
4794     while (!isblanc (buf))
4795         {
4796         buf=vfgets ( buf, fp);
4797         sprintf ( A->aln_comment[n_comment++], "%s", buf);
4798         
4799         }
4800     while ( !isalnum ((c=fgetc(fp))))
4801         {       
4802         ungetc(c,fp);
4803         buf=vfgets ( buf, fp);
4804         
4805         }
4806     
4807     if ( c!='\n')ungetc(c,fp);
4808     
4809     while ( isalnum ((c=fgetc(fp))))
4810         {
4811         ungetc(c,fp);
4812         
4813         fscanf_seq_name (fp, A->name[A->nseq]); 
4814
4815         if ( name_is_in_list (A->name[A->nseq], A->name, A->nseq, 100)!=-1)
4816           {
4817             fprintf ( stderr, "\nWARNING (read_number_aln): Sequence %s Duplicated in File %s ", A->name[A->nseq], A->file[A->nseq]);
4818             if (!getenv("ALLOW_DUPLICATE"))
4819               {
4820                 fprintf ( stderr, " [FATAL:%s]\n", PROGRAM);
4821                 myexit (EXIT_FAILURE);
4822               }
4823           }      
4824         A->nseq++;
4825         buf=vfgets ( buf,fp);
4826         }
4827     
4828     vfclose (fp);
4829   
4830     
4831      
4832     if ((fp=vfopen ( fname, "r"))==NULL)
4833         printf ( "\nCOULDN'T READ %s", fname);
4834    
4835     ptr_aln=vcalloc ( A->nseq, sizeof(int));
4836     while ( flag==0)
4837         {
4838         while (  (c=fgetc(fp))!='\n');
4839         if ( (c=fgetc(fp))=='\n')
4840             flag=1;
4841         }
4842     while ( !isalnum(c=fgetc(fp)));
4843     ungetc ( c, fp);
4844     while ( c!=EOF)
4845         {
4846         tot=0;
4847         while(tot< A->nseq && c!=EOF)
4848             {
4849              b=0;
4850              while ( !isgraph (c=fgetc(fp)) && c!=EOF);
4851              if ( c!=EOF)ungetc(c, fp);
4852              while ( isgraph((buf[b++]=fgetc(fp))));
4853              buf[b-1]='\0';
4854              for ( a=-1,d=0; d< A->nseq; d++)
4855                 if ( strcmp (A->name[d], buf)==0)
4856                     {a=d;
4857                      tot++;
4858                     }
4859
4860              if ( a==-1) while ( (c=fgetc(fp))!='\n' && c!=EOF);
4861              else
4862                {
4863                  while ( (c=fgetc(fp))!='\n')
4864                    {
4865                      if ( isgraph(c) || is_gap(c))
4866                        {if ( isalpha(c))
4867                          c=(A->residue_case==2)?c:tolower(c);
4868                        
4869                        if (!isspace(c))A->seq_al[a][ptr_aln[a]++]=c;
4870                        }
4871                    }
4872                }
4873              }
4874          while ( !isalnum(c=getc(fp)) && c!=EOF);
4875          if ( c!=EOF)
4876             ungetc (c, fp);
4877          }
4878          
4879     vfclose (fp);
4880     
4881    
4882     for ( a=0; a< A->nseq; a++)
4883         {A->seq_al[a][ptr_aln[a]]='\0';
4884          A->order[a][0]=a;
4885          A->order[a][1]=0;
4886         }
4887     
4888     A->len_aln= strlen(A->seq_al[0]);  
4889     
4890     vfree (buf);
4891     vfree(ptr_aln);
4892     vremove (fname);
4893     
4894     }           
4895 void read_amps_aln ( char *in_file, Alignment *A)
4896         {
4897         FILE *fp;
4898         int a, b, c, cont=1;
4899         A->nseq=get_amps_seq_name ( A->name, in_file);
4900         
4901         fp=vfopen ( in_file, "r");
4902         fp=set_fp_id(fp, "1*");
4903         while ( (c=fgetc(fp))!='\n');
4904         b=0;
4905         while ( cont==1)
4906                 {
4907                 c=fgetc ( fp);
4908                 c=fgetc(fp);
4909                 if ( c=='*')
4910                         {
4911                         cont=0;
4912                         for ( a=0; a<A->nseq; a++)
4913                                 A->seq_al[a][b]='\0';
4914                         A->len_aln=b;
4915                         }
4916                          
4917                 else
4918                         {
4919                         ungetc (c, fp);
4920                         for ( a=0; a< A->nseq; a++)
4921                                 {
4922                                 c=fgetc(fp);
4923                                 if ( c==' ')A->seq_al[a][b]='-';
4924                                 else
4925                                         {
4926                                         A->seq_al[a][b]=c;
4927                                         A->len[a]++;
4928                                         }
4929                                 }
4930                         while ((c=fgetc(fp))!='\n');
4931                         b++;
4932                         }
4933                 }
4934         }
4935
4936
4937
4938
4939
4940
4941 int get_amps_seq_name ( char **name, char* fname)
4942         {
4943         FILE *fp;
4944         int nseq=0;
4945         
4946         fp=vfopen ( fname, "r");
4947         fp=set_fp_id ( fp, "Index");
4948         while ( (fgetc(fp))!='\n');
4949         while ( isspace(fgetc(fp)))
4950                 {fscanf (fp, "%*d >%s", name[nseq++]);
4951                  while ( (fgetc(fp))!='\n');
4952                 }
4953         vfclose ( fp);
4954         return nseq;
4955         }
4956 Alignment * read_gotoh_aln ( char *fname, Alignment *A)
4957    {
4958     FILE *fp;
4959     int * ptr_aln;
4960     int a,b,d,e;
4961
4962
4963     char *buf;
4964     char buf2[VERY_LONG_STRING+1];
4965     char buf3[VERY_LONG_STRING+1];
4966     char buf4[VERY_LONG_STRING+1];
4967
4968     int tot=0;
4969
4970     int l;
4971     int nseq, max_len;
4972     
4973    
4974     if ( !check_file_exists (fname))return NULL;
4975     fp=vfopen ( fname, "r");
4976
4977 /*1 GET THE NUMBER OF SEQUENCES*/
4978     nseq=0;
4979     buf=vcalloc ( VERY_LONG_STRING+1, sizeof (char));    
4980     while ( isblanc (buf=fgets ( buf, VERY_LONG_STRING, fp)));
4981     while (!isblanc (buf=fgets ( buf, VERY_LONG_STRING, fp)));
4982     while ( isblanc (buf=fgets ( buf, VERY_LONG_STRING, fp)));
4983     while ( !isblanc ( buf) && buf!=NULL)
4984         {
4985         a=-1;
4986         d=sscanf ( buf, "%d %s %s %s", &a, buf2, A->name[A->nseq],buf3);
4987         if ( a!=-1)
4988                 {
4989                 if ( name_is_in_list (A->name[A->nseq], A->name, A->nseq, 100)!=-1)
4990                   {
4991                     fprintf ( stderr, "\nWARNING (get_amps_seq_name): Sequence %s Duplicated in File %s ", A->name[A->nseq], A->file[A->nseq]);
4992                     if (!getenv("ALLOW_DUPLICATE"))
4993                       {
4994                         fprintf ( stderr, " [FATAL:%s]\n", PROGRAM);
4995                         myexit (EXIT_FAILURE);
4996                       }
4997                   }                 
4998                 nseq++;
4999                 fgets(buf, VERY_LONG_STRING, fp);
5000                 }
5001         else ( buf=NULL);
5002         }
5003     vfclose (fp);
5004 /*2 Get the MAX Len and Reallocate*/
5005     max_len=count_n_char_in_file(fname)/nseq;
5006     A=realloc_aln2( A, nseq+1, max_len+1);
5007 /*3 Get The Sequences Names*/
5008     A->nseq=0;
5009     fp=vfopen ( fname, "r");
5010     while ( isblanc (buf=fgets ( buf, VERY_LONG_STRING, fp)));
5011     while (!isblanc (buf=fgets ( buf, VERY_LONG_STRING, fp)));
5012     while ( isblanc (buf=fgets ( buf, VERY_LONG_STRING, fp)));
5013     while ( !isblanc ( buf) && buf!=NULL)
5014         {
5015         a=-1;
5016         d=sscanf ( buf, "%d %s %s %s", &a, buf2, A->name[A->nseq],buf3);
5017         if ( a!=-1)
5018                 {
5019                 if ( d==4)sprintf (A->name[A->nseq],"%s", buf3);        
5020                 A->nseq++;
5021                 fgets(buf, VERY_LONG_STRING, fp);
5022                 }
5023         else ( buf=NULL);
5024         }
5025     vfclose (fp);   
5026
5027 /*READ THE ALN*/     
5028     fp=vfopen ( fname, "r");
5029
5030     buf=vcalloc ( VERY_LONG_STRING+1, sizeof (char));;  
5031     ptr_aln=vcalloc ( A->nseq, sizeof(int));
5032     
5033     while ( isblanc (buf=fgets ( buf, VERY_LONG_STRING, fp)));
5034     while (!isblanc (buf=fgets ( buf, VERY_LONG_STRING, fp)));
5035     
5036     
5037     while ( isblanc (buf=fgets ( buf, VERY_LONG_STRING, fp)));
5038     
5039     while (buf!=NULL)
5040         {
5041         tot=0;
5042         while(tot< A->nseq)
5043             {
5044             
5045             e=sscanf (buf, "%d %s %s %s", &e, buf2, buf3, buf4);
5046             if ( e==4)sprintf( buf3, "%s", buf4);
5047             
5048             
5049             for ( d=0; d< A->nseq; d++)
5050                 {
5051                 
5052                 if ( strcmp (A->name[d], buf3)==0)
5053                     {a=d;
5054                      tot++;
5055                     }
5056                 }
5057              l=strlen (buf2);
5058              if ( buf2[l-1]=='|')l--;
5059              buf2[l]='\0';
5060             
5061              for (b=0; b<l; b++)
5062                 {
5063                 if ( isgraph (buf2[b]))
5064                         A->seq_al[a][ptr_aln[a]++]=(A->residue_case==2)?buf2[b]:tolower (buf2[b]);
5065                  }
5066              buf=fgets(buf, VERY_LONG_STRING, fp);      
5067              }
5068          if ( buf!=NULL)
5069                 {
5070                 buf=fgets(buf, VERY_LONG_STRING, fp);
5071                 while ( isblanc (buf) && buf!=NULL)
5072                         {
5073                         buf=fgets ( buf, VERY_LONG_STRING, fp);
5074                         }
5075                 }
5076          
5077          }
5078          
5079     vfclose (fp);
5080     
5081    
5082     for ( a=0; a< A->nseq; a++)
5083         {A->seq_al[a][ptr_aln[a]]='\0';
5084         }
5085     
5086     A->len_aln= strlen(A->seq_al[0]);  
5087     
5088    
5089     
5090     for ( a=0; a< A->nseq; a++)
5091         {
5092         for ( b=0; b< A->len_aln; b++)
5093                 A->len[a]+=1-is_gap(A->seq_al[a][b]);
5094         }
5095     for ( a=0, b=0; a< A->len_aln; a++)
5096         {
5097         if ( !is_gap(A->seq_al[0][a]) &&!is_gap(A->seq_al[1][a]))b++;
5098         }
5099     return A;
5100     }
5101     
5102
5103
5104
5105
5106 void read_msf_aln ( char *fname, Alignment *A)
5107    {
5108     char command[1000];
5109     char *tmp_name;
5110     Sequence *S;
5111     
5112     tmp_name=vtmpnam(NULL);
5113     sprintf ( command, "msf_aln2fasta_aln.pl %s > %s", fname, tmp_name);
5114
5115     if ( my_system (command)!=EXIT_SUCCESS)
5116       {
5117         fprintf ( stderr, "\nERROR: file %s does not have a legal msf format [FATAL:%s]", fname,PROGRAM);
5118         myexit (EXIT_FAILURE);
5119       }
5120
5121     S=get_fasta_sequence ( tmp_name,NULL);
5122     A=seq2aln (S, A, 0);  
5123     vremove (tmp_name);
5124     return;
5125     }           
5126
5127 /**************************************************************************************************/
5128 /*************************************REFORMAT OUT*************************************************/
5129 /**************************************************************************************************/
5130 /*******************************************************************************************/
5131 /*                                                                                         */
5132 /*                                                                                         */
5133 /*                               OUTPUT MATRICES                                           */
5134 /*                                                                                         */
5135 /***************************************************************************************** */
5136
5137
5138
5139 int output_freq_mat ( char *outfile, Alignment *A)
5140     { /*
5141         function documentation: start
5142         
5143         int output_freq_mat ( char *outfile, Aligmnent *A)
5144
5145         This function counts the number of residues in each column of an alignment (Prot)
5146         It outputs these values in the following format
5147
5148         A | 0 0 0 1 0
5149         B | 1 0 0 0 1
5150         - | 0 1 1 0 0
5151
5152         This format can be piped into:
5153         The routine used for computing the p-value  gmat-inf-gc-v2c
5154         
5155         function documentation: end
5156       */
5157       
5158     int a, b;
5159     int **freq_mat;
5160     FILE *fp;
5161     
5162     
5163     freq_mat=aln2count_mat (A);
5164             
5165     fp=vfopen ( outfile, "w");
5166     for ( b=0; b< 26; b++)
5167       {
5168         fprintf (fp, "%c |", 'A'+b);
5169         for ( a=0; a< A->len_aln; a++)fprintf (fp,"%d ", freq_mat[b][a]);
5170         fprintf (fp, "\n");
5171       }
5172     fprintf (fp, "- |");
5173     for ( a=0; a< A->len_aln; a++)fprintf (fp,"%d ", freq_mat[26][a]);
5174     
5175     free_int (freq_mat, -1);
5176     vfclose ( fp);
5177     return 1;
5178     }
5179 /*******************************************************************************************/
5180 /*                                                                                         */
5181 /*                                                                                         */
5182 /*                               OUTPUT P-Values                                           */
5183 /*                                                                                         */
5184 /***************************************************************************************** */   
5185 float output_maln_pval ( char *outfile, Alignment *A)
5186     {
5187       /*
5188         function documentation: start
5189         float output_maln_pval ( char *outfile, Aligmnent *A)
5190
5191         This function outputs the p-value of a multiple alignmnet as described 
5192         in Hertz, Stormo, Bioinformatics, 15-7/8, 563/577
5193             ftp beagle.colorado.edu /pub/cosensus
5194         Locally
5195             packages/consensus/gmat-inf-gc-v2c
5196         
5197         
5198         The routine used for computing the p-value is the program gmat-inf-gc-v2c
5199         function documentation: end
5200       */
5201
5202   
5203     char *mat;
5204     char *result;
5205     FILE *fp;
5206     float value;
5207     char command[LONG_STRING];
5208     char string[STRING];
5209     mat=vtmpnam (NULL);
5210     result=vtmpnam (NULL);
5211     
5212     output_freq_mat (mat,A);
5213     sprintf ( command, "more %s | gmat-inf-gc-v2c -A abcdefghijklmnopqrstuvwxyz> %s",mat, result);
5214     my_system ( command);
5215     
5216     if ( !check_file_exists(result))return 0;
5217     fp=find_token_in_file ( result, NULL, "ln(p-value):");
5218     
5219     fscanf ( fp, "%s",string);
5220     value=atof ( string);
5221     vfclose ( fp);
5222     
5223     vremove ( mat);
5224     vremove ( result);
5225     
5226     fp=vfopen ( outfile, "w");
5227     fprintf ( fp, "%.6f\n", value);
5228     vfclose ( fp);
5229     
5230     return value;
5231     }
5232               
5233     
5234 /*******************************************************************************************/
5235 /*                                                                                         */
5236 /*                                                                                         */
5237 /*                               OUTPUT WEIGHTS                                            */
5238 /*                                                                                         */
5239 /***************************************************************************************** */
5240 int output_seq_weights ( Weights *W, char *wfile)
5241         {
5242         FILE*fp;
5243         int a;
5244         
5245         if ( W==NULL)return 0;
5246         
5247         fp=vfopen (wfile, "w");
5248         if ( fp==NULL)return 0;
5249         
5250         
5251         for ( a=0; a< W->nseq; a++)
5252                 {
5253                 
5254                   fprintf ( fp, "%s %.2f\n", W->seq_name[a],W->SEQ_W[a]);
5255                 }
5256         vfclose ( fp);
5257         return 1;
5258         }  
5259 void output_pw_weights4saga ( Weights *W, float **w_list, char *wfile)
5260         {
5261         FILE*fp;
5262         int a, b;
5263         fp=vfopen (wfile, "w");
5264         
5265         fprintf ( fp, "%s\n$\n", W->comments); 
5266         for ( a=0; a< W->nseq-1; a++)
5267                 {
5268                 for (b=a+1; b< W->nseq; b++)
5269                         {
5270                         fprintf ( fp, "%s %s %f\n", W->seq_name[a], W->seq_name[b],w_list[a][b]);
5271                         }
5272                 }
5273         fprintf ( fp, "$\n");
5274         vfclose ( fp);
5275         }
5276
5277 FILE * display_weights (Weights *W, FILE *fp)
5278 {
5279   int a;
5280   int max_len;
5281   
5282   if ( W==NULL)
5283     {
5284       fprintf ( fp, "\n\nUN-WEIGHTED MODE: EVERY SEQUENCE WEIGHTS 1\n");
5285       return fp;
5286     }
5287   fprintf ( fp, "\n\nWEIGHTED MODE:%s\n\n", (W)->mode);
5288   for ( a=0, max_len=0; a< W->nseq; a++)max_len=MAX(max_len, strlen (W->seq_name[a]));
5289   for ( a=0; a< (W->nseq); a++)
5290     {
5291       fprintf ( fp, "\t%*s %.2f\n", max_len,(W)->seq_name[a],W->SEQ_W[a]);
5292     }
5293   fprintf ( fp, "\n");
5294   return fp;
5295 }
5296
5297 /*******************************************************************************************/
5298 /*                                                                                         */
5299 /*                                                                                         */
5300 /*                               OUTPUT SEQ                                                */
5301 /*                                                                                         */
5302 /***************************************************************************************** */
5303 int ** input_similarities (char *file, Alignment *A, char *mode)
5304 {
5305   int a, b, i, n;
5306   int **sim;
5307   float score;
5308   char name[1000];
5309   FILE *fp=NULL;
5310   char *buf1=NULL, *buf2=NULL;
5311   int new_aln=0;
5312   
5313
5314   
5315   if ( !check_file_exists (file) || !is_distance_matrix_file (file) ||!is_similarity_matrix_file (file) )
5316     {
5317       return NULL;
5318     }
5319   
5320   if ( A)
5321     {
5322       fp=vfopen (file, "r");
5323       while ((buf2=vfgets (buf1,fp))!=NULL )
5324         {
5325           if (strstr (buf2, "SEQ_INDEX"))
5326             {
5327               buf1=buf2;
5328               sscanf (buf1, "# SEQ_INDEX %s %d",name, &i);
5329               if ( !strm (A->name[i], name))
5330                 {
5331                   return NULL;
5332                 }
5333             }
5334         }
5335       vfclose (fp);
5336     }
5337   else
5338     {
5339   
5340       A=similarities_file2aln(file);
5341       new_aln=1;
5342     }
5343   
5344   sim=declare_int ( A->nseq, A->nseq);
5345   for ( a=0; a<A->nseq; a++)sim[a][a]=100;
5346   
5347   
5348   fp=find_token_in_file (file, NULL, "PW_SEQ_DISTANCES");
5349   fp=find_token_in_file (file, fp, "BOT");
5350   while ((buf2=vfgets (buf1,fp))!=NULL )
5351     {
5352       if ( !(strstr (buf2, "BOT\t") || strstr (buf2, "TOP\t")))continue;
5353       buf1=buf2;
5354       n=sscanf (buf1, "%*s %d %d %f", &a, &b, &score);
5355       if ( n!=3)
5356         {
5357           free_int (sim, -1);
5358           return NULL;
5359         }
5360       else sim[a][b]=sim[b][a]=(int)score;
5361     }
5362   vfclose (fp);
5363   vfree (buf1);
5364   if (new_aln)free_aln(A);
5365   return sim;
5366 }
5367
5368 Alignment * similarities_file2aln ( char *file)
5369 {
5370   int nseq=0, i;
5371   FILE *fp;
5372   char name[1000];
5373   Alignment *A;
5374   
5375
5376   fp=vfopen (file, "r");
5377   while ((fp=find_token_in_file (file,fp, "SEQ_INDEX")))nseq++;
5378   A=declare_aln2 (nseq+1, 10);
5379   
5380   while ((fp=find_token_in_file (file,fp, "SEQ_INDEX")))
5381     {
5382       fscanf (fp, "%s %d", name,&i);
5383       sprintf ( A->name[i], "%s", name);
5384     }
5385   A->nseq=nseq;
5386
5387   return A;
5388 }
5389   
5390 void output_similarities (char *file, Alignment *A, char *mode)
5391 {
5392   float s;
5393   float *tot;
5394   float bigtot=0;
5395   int n, max;
5396   FILE *fp;
5397   int a, b;
5398   char *p;
5399   int **M=NULL;
5400   for (max=0, a=0; a< A->nseq; a++)max=MAX(max,(strlen (A->name[a])));
5401   
5402
5403   tot=vcalloc ( A->nseq, sizeof (float));
5404   fp=vfopen (file, "w");
5405   fprintf (fp, "# TC_SIMILARITY_MATRIX_FORMAT_01\n");
5406   for ( a=0; a<A->nseq; a++)
5407     fprintf ( fp, "# SEQ_INDEX %s %d\n",A->name[a],a);
5408   fprintf ( fp, "# PW_SEQ_DISTANCES \n");
5409   for (n=0,a=0;a< A->nseq-1; a++)
5410     {
5411       for ( b=a+1; b<A->nseq; b++, n++)
5412         {
5413            if (strstr (mode, "_sarmat2"))
5414             {
5415               s=get_sar_sim (A->seq_al[a], A->seq_al[b]);
5416             }
5417           else if (strstr (mode, "_sar"))
5418             {
5419               s=get_sar_sim (A->seq_al[a], A->seq_al[b]);
5420             }
5421           else if ( (p=strstr (mode, "_memory_")))
5422             {
5423               int **sim;
5424               sscanf ( p, "_memory_%ld", (long int*)&sim);
5425               s=sim[a][b];
5426             }
5427           else if ( strstr (mode, "_idscore") || strstr ( mode, "_covscore"))
5428             {
5429               static Sequence *S;
5430               if (a==0 && b==1)
5431                 {
5432                   free_sequence (S, -1);
5433                   if ( strstr (mode, "idscoreDNA"))
5434                     M=read_matrice ("idmat");
5435                   else
5436                     M=read_matrice("blosum62mt");
5437               
5438                   S=aln2seq(A);
5439                 }
5440               if ( strstr (mode, "_idscore"))s=idscore_pairseq(S->seq[a], S->seq[b], -10,-1, M, "sim");
5441               else            s=idscore_pairseq(S->seq[a], S->seq[b], -10,-1, M, "cov");
5442             }
5443           else if ( strstr (mode, "cov"))
5444             {
5445               s=get_seq_sim ( A->seq_al[a], A->seq_al[b],GAP_LIST, "cov");
5446             }
5447           else
5448             {
5449               s=get_seq_fsim2 (A->seq_al[a], A->seq_al[b],GAP_LIST, mode);
5450             }
5451           fprintf (fp, "BOT\t %4d %4d\t %5.2f %*s\t %*s\t %5.2f\n", a,b,s,max,A->name[a], max, A->name[b], s);
5452           fprintf (fp, "TOP\t %4d %4d\t %5.2f %*s\t %*s\t %5.2f\n", b,a,s,max,A->name[b], max, A->name[a], s);
5453           tot[a]+=s;
5454           tot[b]+=s;
5455           bigtot+=s;
5456         }
5457     }
5458   for ( a=0; a< A->nseq; a++)
5459     {
5460       fprintf (fp, "AVG\t %d\t %*s\t %*s\t %5.2f\n", a,max,A->name[a], max, "*", tot[a]/(A->nseq-1));
5461       
5462     }
5463   vfree (tot);free_int (M, -1);
5464   fprintf (fp, "TOT\t %*s\t %*s\t %5.2f\n", max,"TOT", max, "*", bigtot/n);
5465   vfclose (fp);
5466 }
5467
5468 void output_similarities_pw (char *file, Alignment *A, Alignment *B,char *mode)
5469 {
5470   float s;
5471   float *tot;
5472   float bigtot=0;
5473   int n, max;
5474   FILE *fp;
5475   int a, b;
5476
5477   int **M=NULL;
5478   Sequence *SA, *SB;
5479   
5480   if ( strstr (mode, "idscoreDNA"))
5481     M=read_matrice ("idmat");
5482   else
5483     M=read_matrice("blosum62mt");
5484   
5485   SA=aln2seq(A);
5486   SB=aln2seq(B);
5487    
5488   for (max=0, a=0; a< A->nseq; a++)max=MAX(max,(strlen (A->name[a])));
5489   for (a=0; a< B->nseq; a++)max=MAX(max,(strlen (B->name[a])));
5490   
5491
5492   tot=vcalloc ( A->nseq, sizeof (float));
5493   fp=vfopen (file, "w");
5494   fprintf (fp, "# TC_SIMILARITY_MATRIX_FORMAT_01\n");
5495   for ( a=0; a<A->nseq; a++)
5496     fprintf ( fp, "# SEQ_INDEX %s %d\n",A->name[a],a);
5497   fprintf ( fp, "# PW_SEQ_DISTANCES \n");
5498   for (n=0,a=0;a< A->nseq; a++)
5499     {
5500       for ( b=0; b<B->nseq; b++, n++)
5501         {
5502           s=idscore_pairseq(SA->seq[a], SB->seq[b], -10,-1, M, "sim");
5503           fprintf (fp, "BOT\t %4d %4d\t %5.2f %*s\t %*s\t %5.2f\n", a,b,s,max,A->name[a], max, B->name[b], s);
5504           fprintf (fp, "TOP\t %4d %4d\t %5.2f %*s\t %*s\t %5.2f\n", b,a,s,max,B->name[b], max, A->name[a], s);
5505           tot[a]+=s;
5506           tot[b]+=s;
5507           bigtot+=s;
5508         }
5509     }
5510   
5511   for ( a=0; a< A->nseq; a++)
5512     {
5513       fprintf (fp, "AVG\t %d\t %*s\t %*s\t %5.2f\n", a,max,A->name[a], max, "*", tot[a]/(A->nseq-1));
5514     }
5515   vfree (tot);free_int (M, -1);
5516   fprintf (fp, "TOT\t %*s\t %*s\t %5.2f\n", max,"TOT", max, "*", bigtot/n);
5517   vfclose (fp);
5518 }
5519 void output_conservation_statistics ( char *file, Alignment *A)
5520 {
5521   int a, b, c,c1, c2;
5522   double **tot;
5523   char aa[1000];
5524   int naa;
5525   
5526   sprintf (aa, "%s", BLAST_AA_ALPHABET);
5527   naa=strlen (aa);
5528   
5529   tot=declare_double (256, 256);
5530   
5531   
5532   for ( a=0; a<A->nseq; a+=2)
5533     {
5534       b=a+1;
5535       for ( c=0; c<A->len_aln; c++)
5536         {
5537           c1=tolower (A->seq_al[a][c]);
5538           c2=tolower (A->seq_al[b][c]);
5539           if ( !is_gap(c1) && !is_gap(c2))
5540             {
5541               tot[c1][c2]++;
5542               tot[c2][c1]++;
5543               tot[c1][0]++;
5544               tot[c2][0]++;
5545               tot[0][0]++;
5546             }
5547         }
5548     }
5549   
5550   fprintf ( stdout, "# BLAST_MATRIX FORMAT\n#ALPHABET=%s\n",aa);
5551   for (a=0; a<naa; a++)fprintf ( stdout, "%3c ", toupper(aa[a]));
5552   fprintf ( stdout, "\n");
5553   for (a=0; a< naa; a++)
5554     {
5555       fprintf (stdout, "%c", toupper(aa[a]));
5556       for ( b=0; b< naa; b++)
5557         {
5558           float f1, f2, f3, r, v;
5559           c1=tolower(aa[a]);c2=tolower(aa[b]);
5560           f1=(float)((tot[c1][c2]*2)/tot[0][0]);
5561           f2=(float)((tot[c1][0])/tot[0][0]);
5562           f3=(float)((tot[c2][0])/tot[0][0]);
5563           r=(float)(f2==0 || f3==0)?0:(f1/(f2*f3));
5564           v=(r==0)?0:((float)10*log((double)r));
5565           fprintf (stdout, " %5d",(int)v);
5566         }
5567       fprintf ( stdout, "\n");
5568     }
5569 }
5570 void output_statistics (char *file, Alignment *A, char *mode)
5571     {
5572       FILE *fp;
5573       int a, b, c, d=0, n;
5574       int maxname=0;
5575
5576       
5577       if (!mode || !mode[0])
5578         mode="hnrglNL";
5579       else if ( mode[0]=='_')
5580         mode++;
5581       for ( a=0; a<A->nseq; a++)maxname=MAX(strlen(A->name[a]), maxname);
5582       maxname++;
5583       
5584       
5585       fp=vfopen (file, "w");
5586       
5587       if (mode[0]=='h')
5588         {
5589           b=0;
5590           while ((c=mode[b++])!='\0')
5591             {
5592               if ( c=='n') fprintf (fp, "%-*s ",maxname,"name");
5593               if ( c=='l') fprintf (fp, "%-*s ",5,"nres");
5594               if ( c=='g') fprintf (fp, "%-*s ",5,"ngap");
5595               if ( c=='t') fprintf (fp, "%-*s ",5,"len");
5596             }
5597           if (is_in_set ( c, "nlgt"))     fprintf (fp, "\n");
5598           mode++;
5599         }
5600       b=0;
5601       while ((c=mode[b++])!='\0')
5602         {
5603           if ( c=='n')break;
5604           if ( c=='N'){d=1;fprintf (fp, "NSEQ %d ", A->nseq);}
5605           if ( c=='L'){d=1;fprintf (fp, "LEN  %d ", A->len_aln);}         
5606         }
5607       if ( d) fprintf (fp, "\n");
5608       
5609       for (a=0; a<A->nseq; a++)
5610         {
5611           b=0;
5612           d=0;
5613           while ((c=mode[b++])!='\0')
5614             {
5615               if (is_in_set ( c, "nlgt"))d=1;
5616
5617               if (c=='n'){d=1;fprintf ( fp, "%-*s ", maxname,A->name[a]);}
5618               if (c=='l')
5619                 {
5620                   for (n=0,d=0; d<A->len_aln; d++)n+=!is_gap(A->seq_al[a][d]);
5621                   fprintf ( fp, "%-5d ",n);
5622                 }
5623               if (c=='g')
5624                 {
5625                   for (n=0,d=0; d<A->len_aln; d++)n+=((is_gap(A->seq_al[a][d]) && !is_gap(A->seq_al[a][d+1]))||(is_gap(A->seq_al[a][d])&& A->seq_al[a][d+1]=='\0')) ;
5626                   fprintf ( fp, "%-5d ",n); 
5627                 }
5628               if (c=='t')
5629                 {
5630                  fprintf ( fp, "%-5d ",strlen (A->seq_al[a]));           
5631                 }
5632                if (c=='N' && d)
5633                 {
5634                  fprintf ( fp, "%-5d ",A->nseq);                 
5635                 }
5636               if (c=='L'&& d)
5637                 {
5638                  fprintf ( fp, "%-5d ",A->len_aln);              
5639                 }
5640             }
5641           if (d)fprintf ( fp, "\n"); 
5642         }
5643       vfclose (fp);
5644     }
5645
5646 int output_age_matrix ( char *outfile, int val)
5647 {
5648   int **mat;
5649   int a, b;
5650   char alp[]="abcdefghij-";
5651   int naa;
5652   
5653   mat=declare_int ( 256, 256);
5654   naa=strlen (alp);
5655   for ( a=0; a<naa; a++)
5656     for ( b=0; b<naa; b++)
5657       {
5658         if (is_gap(alp[a]) ||is_gap(alp[b] ))mat[(int)alp[a]][(int)alp[b]]=((val==0)?1:val)*-1;
5659         else mat[(int)alp[a]][(int)alp[b]]=(FABS((a-b))*-1)*((val==0)?1:val);
5660         
5661       }
5662   output_mat ( mat,outfile, alp, 0);
5663   free_arrayN((void**)mat, 2);
5664   return 1;
5665 }
5666      
5667
5668
5669
5670 int output_transitions(char *outfile, Alignment *A)
5671 {
5672   double table[256][256];
5673   double symbols[256];
5674   double tot, l, freq, expected, log_odd;
5675   int a, b;
5676   char *s;
5677   char *alp;
5678   int naa=0;
5679   int **mat;
5680   float **fmat;
5681   
5682   FILE *fp;
5683   
5684   for ( a=0; a< 256; a++)
5685     for (b=0; b<256; b++) 
5686       {
5687         symbols[b]=0;
5688         table[a][b]=0;
5689       }
5690   alp=vcalloc ( 256, sizeof (char));
5691   mat=declare_int ( 256,256);
5692   fmat=declare_float ( 256,256);
5693   
5694   for (tot=0,a=0; a< A->nseq; a++)
5695     {
5696       ungap (A->seq_al[a]);
5697       lower_string (A->seq_al[a]);
5698       s=A->seq_al[a];
5699       l=strlen (s);
5700       if ( s[0]=='\0') continue;
5701       symbols[(int)s[0]]++;
5702       for ( b=1; b< l; b++)
5703         {
5704           symbols[(int)s[b]]++;
5705           table[(int)s[b-1]][(int)s[b]]++;
5706           tot++;
5707         }
5708     }
5709   for (naa=0, a=0; a< 256; a++)
5710     {
5711       if (symbols[a])alp[naa++]=a;
5712     }
5713   
5714       
5715   for ( a=0; a< 256; a++)
5716     for (b=0; b<256; b++) 
5717       {
5718         if (symbols[a]&& symbols[b] && table[a][b] && tot>0)
5719           {
5720             freq=(table[a][b])/tot;
5721             expected=(symbols[a]*symbols[b])/(tot*tot);
5722             log_odd=log (freq/expected);
5723             mat[a-'A'][b-'A']=log_odd*10;
5724             fmat[a-'A'][b-'A']=log_odd;
5725           }
5726         else if ( symbols[a]&& symbols[b])
5727           {
5728             mat[a-'A'][b-'A']=-999;
5729             fmat[a-'A'][b-'A']=-999;
5730           }
5731       }
5732   output_mat ( mat,outfile, alp, 'A');
5733   
5734   fp=vfopen (outfile, "a");
5735   for ( a=0; a<256; a++)
5736     if ( symbols[a])
5737       {
5738         fprintf (fp, "# %c tot: %6d freq: %7.5f\n", a, (int)symbols[a],(float)symbols[a]/tot);
5739       }
5740   
5741   for ( a=0; a< 256; a++)
5742     for (b=0; b<256; b++) 
5743       {
5744         if (symbols[a]&& symbols[b])
5745           {
5746             freq=(table[a][b])/tot;
5747             fprintf (fp, "# %c%c tot: %6d freq: %7.5f log_odd: %9.3f\n", a, b, (int)table[a][b],(float)freq,fmat[a-'A'][b-'A']);                    
5748           }
5749       }
5750   vfclose (fp);
5751   vfree(alp);
5752   free_arrayN ((void **)mat, 2);
5753   free_arrayN ((void **)fmat, 2);
5754   
5755   return 1;
5756 }
5757
5758
5759
5760 void output_est_prf   (char *fname, Alignment *A)
5761         {
5762         int a;
5763         FILE *fp;
5764
5765         if ( !A->P)
5766           {
5767             fprintf ( stderr, "\nFormat output_est_prf Impossible: No profile\n");
5768             myexit(EXIT_FAILURE);
5769           }
5770         
5771
5772         fp=vfopen ( fname, "w");
5773         fprintf ( fp, "Consensus Sequence\nReconstructed with %s (%s,%s)\n",PROGRAM,AUTHOR,DATE);
5774         fprintf ( fp, "%4c %4c %4c %4c %15s    Consensus\n",  'A','G','C','T', "Internal Gaps");
5775
5776         for ( a=0; a< A->len_aln; a++)
5777           {
5778             fprintf (fp, "%4d %4d %4d %4d %15d %c\n", (A->P)->count[0][a],(A->P)->count[1][a],(A->P)->count[2][a], (A->P)->count[3][a], (A->P)->count[4][a],A->seq_al[0][a]);
5779           }
5780         return;
5781         }
5782
5783           
5784 void output_gotoh_seq (char *fname, Alignment*A )
5785         {
5786         int a;
5787         FILE *fp;
5788                 
5789         fp=vfopen ( fname, "w");
5790         fprintf ( fp, "%d %d\n",A->nseq, A->max_len);
5791         for ( a=0; a< A->nseq; a++)
5792                 {
5793                 ungap ( A->seq_al[a]);
5794                 fprintf ( fp, ">%s\n", A->name[a]);
5795                 fp=output_string_wrap ( 50,A->seq_al[a] , fp);
5796                 fprintf ( fp, "//\n");
5797                 }
5798                 
5799         vfclose (fp);
5800         }           
5801
5802 void output_mult_fasta_seq (char *fname, Alignment*A, int n )
5803         {
5804         int a;
5805         FILE *fp;
5806         
5807         fp=vfopen (fname, "w");
5808         ungap(A->seq_al[0]);
5809         for (a=0; a<n; a++)
5810           {
5811             fprintf (fp, ">%s_%d\n%s\n", A->name[0],a+1, A->seq_al[0]);
5812           }
5813         vfclose (fp);
5814         }
5815
5816 char * output_fasta_seqX (char *name, char *mode, Sequence *S, Alignment *A, int i)
5817 {
5818   FILE *fp;
5819   
5820   if (!name)name=vtmpnam (NULL);
5821   fp=vfopen (name, mode);
5822   if ( (S && S->nseq<=i) || (A && S->nseq<=i) || (!A && !S))
5823     {
5824       fprintf ( stderr, "\nERROR in function reformat:output_fasta_seqX[FATAL:%s]", PROGRAM);
5825       myexit (EXIT_FAILURE);
5826     }
5827
5828   else if ( S) 
5829     fprintf ( fp, ">%s %s\n%s\n", S->name[i], S->seq_comment[i], S->seq[i]);
5830   else if ( A)
5831     {
5832       ungap (A->seq_al[i]);
5833       fprintf ( fp, ">%s %s\n%s\n", A->name[i], A->seq_comment[i], A->seq_al[i]);
5834     }
5835   vfclose (fp);
5836   return name;
5837 }
5838
5839 void output_fasta_seq1 (char *fname, Alignment*A )
5840         {
5841         char seq_name[VERY_LONG_STRING];
5842         int a;
5843         FILE *fp;
5844         char *extension;
5845         
5846         for ( a=0; a< A->nseq; a++)
5847                 {
5848                 if ( strncmp( fname, "name",4)==0)
5849                   {
5850                     if ( (fname+4)[0]!='\0')extension=fname+5;
5851                     else
5852                       extension=NULL;
5853                     
5854                      sprintf ( seq_name,"%s.%s", A->name[a],(extension==NULL)?"seq":extension);
5855                   }
5856                 else
5857                    sprintf ( seq_name,"%s.seq",A->name[a]);
5858                 
5859                 ungap ( A->seq_al[a]);
5860                 fp=vfopen (seq_name, "w");
5861                 fprintf (fp, ">%s %s\n", A->name[a], A->seq_comment[a]);
5862                 fp=output_string_wrap ( 50, A->seq_al[a],fp);
5863                 fprintf ( fp, "\n");
5864                 vfclose (fp);
5865                 }
5866         }
5867 void output_pir_check (char *fname,int nseq, char **comment )
5868         {
5869         int a;
5870         FILE *fp;
5871         
5872         if ( fname==NULL)return;
5873         fp=vfopen ( fname, "w");
5874         
5875         for ( a=0; a< nseq; a++)fprintf (fp, "%s\n", comment[a]);
5876         vfclose (fp);
5877         }
5878 void output_fasta_seq (char *fname, Alignment*A)
5879 {
5880   main_output_fasta_seq (fname, A, HEADER);
5881 }
5882 void output_fasta_tree (char *fname, Alignment*A)
5883         {
5884         int a;
5885         FILE *fp;
5886         if ( !A || !A->nseq) return;
5887         
5888         fp=vfopen ( fname, "w");
5889         
5890         for ( a=0; a< A->nseq; a++)
5891                 {
5892                   fprintf ( fp, ">%s %s\n%s\n", A->name[a], A->seq_comment[a], A->seq_al[a]);
5893                 }
5894         vfclose (fp);
5895         }
5896 void main_output_fasta_seq (char *fname, Alignment*A,int header )
5897         {
5898         int a;
5899         FILE *fp;
5900         
5901         fp=vfopen ( fname, "w");
5902
5903         for ( a=0; a< A->nseq; a++)
5904                 {
5905                 ungap(A->seq_al[a]);
5906                 fprintf ( fp, ">%s", A->name[a]);
5907                 if (header==HEADER && A->seq_comment[a][0] && !isblanc(A->seq_comment[a]))fprintf (fp," %s\n",A->seq_comment[a]);
5908                 else fprintf ( fp, "\n");
5909                 fp=output_string_wrap ( 50, A->seq_al[a],fp);
5910                 fprintf ( fp, "\n");
5911                 }
5912         vfclose (fp);
5913         }    
5914 void output_gor_seq (char *fname, Alignment*A )
5915         {
5916         int a;
5917         FILE *fp;
5918         
5919         fp=vfopen ( fname, "w");
5920         
5921         for ( a=0; a< A->nseq; a++)
5922                 {
5923                 ungap(A->seq_al[a]);
5924                 fprintf ( fp, "!%s %d \n", A->name[a], (int)strlen(A->seq_al[a]));
5925                 upper_string ( A->seq_al[a]);
5926                 fp=output_string_wrap ( 50, A->seq_al[a],fp);
5927                 fprintf ( fp, "@\n");
5928                 }
5929         vfclose (fp);
5930         }    
5931 void output_pir_seq (char *fname, Alignment*A )
5932         {
5933         int a;
5934         for ( a=0; a< A->nseq; a++)ungap(A->seq_al[a]);
5935         output_pir_aln (fname, A);
5936         } 
5937 void output_pir_seq1 (char *fname, Alignment*A )
5938         {
5939         char seq_name[VERY_LONG_STRING];
5940         int a;
5941         FILE *fp;
5942         char type[20];
5943
5944         
5945         for ( a=0; a< A->nseq; a++)
5946                 {
5947                 if      ( strm ( get_string_type (A->seq_al[a]),"DNA") || strm ( get_string_type (A->seq_al[a]),"RNA"))sprintf(type, "DL");
5948                 else if ( strm ( get_string_type (A->seq_al[a]),"PROTEIN"))sprintf(type, "P1"); 
5949                 sprintf ( seq_name,"%s;%s_%s.seq",type, fname,A->name[a]);
5950                 ungap ( A->seq_al[a]);
5951                 fp=vfopen (seq_name, "w");
5952                 fprintf (fp, ">%s\n\n", A->name[a]);
5953                 fp=output_string_wrap ( 50, A->seq_al[a],fp);
5954                 fprintf ( fp, "\n*\n");
5955                 vfclose (fp);
5956                 }
5957         } 
5958 /*******************************************************************************************/
5959 /*                                                                                         */
5960 /*                                                                                         */
5961 /*                               OUTPUT ALN                                                */
5962 /*                                                                                         */
5963 /***************************************************************************************** */
5964 void output_mocca_aln (char *outfile, Alignment *A, Alignment *S)
5965     {
5966     FILE *fp;
5967     int **score;
5968     char **new_name_order;
5969     int a, maxl;
5970
5971     score=declare_int (S->nseq, 2);
5972     new_name_order=declare_char ( S->nseq,MAXNAMES+1); 
5973     for ( a=0; a<A->nseq; a++)
5974       {
5975         score[a][0]=a;
5976         score[a][1]=S->score_seq[a];
5977       }
5978     sort_int_inv (score+1,2,1,0,S->nseq-2);
5979     for ( a=0; a<A->nseq; a++)
5980       {
5981         sprintf ( new_name_order[a], "%s", A->name[score[a][0]]);
5982       }
5983     A=reorder_aln (A, new_name_order, A->nseq);
5984
5985     fp=vfopen (outfile, "w");
5986     fprintf ( fp, "MOCCA,(%s,%s, C. Notredame)\nSCORE %d\nNSEQ  %d\nLEN   %d\n",VERSION,DATE, A->score_aln, A->nseq, A->len_aln);     
5987     
5988     maxl=return_maxlen ( new_name_order, A->nseq); 
5989     
5990    
5991     for (a=0; a< A->nseq; a++)
5992       {
5993         fprintf (fp, "%-*s: %3d\n", maxl, A->name[a], score[a][1]);
5994       }
5995     
5996     fprintf ( fp, "\n");
5997     
5998     fp=output_Alignment_without_header ( A, fp);
5999     vfclose (fp);
6000     free_int  (score, -1);
6001     free_char (new_name_order, -1);
6002     return ;
6003     }
6004   
6005 void print_sub_aln ( Alignment *B, int *ns, int **ls)
6006 {
6007   Alignment *X;
6008   int a, b;
6009  
6010
6011   X=copy_aln (B, NULL);
6012   X->nseq=0;
6013   X->len_aln=strlen ( B->seq_al[ls[0][0]]);
6014
6015
6016   for (a=0; a< 2; a++)
6017     for ( b=0; b<ns[a]; b++, X->nseq++)
6018       {
6019         sprintf ( X->seq_al[X->nseq], "%s", B->seq_al[ls[a][b]]);
6020         sprintf ( X->name[X->nseq], "%s", B->name[ls[a][b]]);
6021       }
6022   X->name[X->nseq][0]='\0';
6023   
6024   print_aln (X);
6025   free_aln (X);
6026 }
6027 void print_aln ( Alignment *B)
6028     {
6029       
6030     while(B)
6031       {
6032         output_Alignment_without_header ( B, stderr);
6033         B=B->A;
6034       }
6035     }
6036
6037
6038 FILE * output_aln ( Alignment *B, FILE *fp){return output_Alignment(B, fp);}
6039 FILE * output_Alignment ( Alignment *B, FILE *fp)
6040     {
6041       fprintf ( fp, "%s, %s (%s) [%s] [MODE: %s]\n%s\nCPU   %d sec\nSCORE %d\nNSEQ  %d\nLEN   %d\n",PROGRAM,VERSION,DATE,retrieve_mode(),URL,AUTHOR,  (B->cpu+get_time())/1000, B->score_aln, B->nseq, B->len_aln);     
6042       return output_Alignment_without_header ( B, fp);
6043     }
6044   
6045 FILE * output_Alignment_without_header ( Alignment *B, FILE *fp)
6046     {
6047     int a,b, c;
6048     int max_len=0;
6049     int line;       
6050     int *n_residues;
6051     char s;
6052
6053     
6054     if (fp==NULL)return fp;
6055     for ( a=0; a< B->nseq; a++)
6056             {if ( strlen (B->name[a])>max_len)
6057                 max_len= strlen ( (B->name[a]));
6058             }
6059     max_len=MAX(max_len+2, 16);
6060     line=get_msa_line_length (0, 0);
6061     n_residues=vcalloc ( B->nseq+1, sizeof (int));
6062     for ( a=0; a<B->nseq; a++)n_residues[a]=(B->output_res_num==2)?B->order[a][1]:0;
6063     
6064     
6065     
6066     
6067   fprintf ( fp, "\n"); 
6068     for (a=0; a<B->len_aln; a+=line)
6069            {for (b=0; b<=B->nseq; b++)
6070              {
6071                fprintf (fp,"%-*s",max_len,B->name[b]);
6072                if (B->output_res_num)fprintf (fp, " %4d ", n_residues[b]+1);
6073                for (c=a;c<a+line && c<B->len_aln;c++)
6074                  {
6075                    if (b==B->nseq){n_residues[b]++;s=analyse_aln_column ( B, c);}
6076                    else 
6077                      {n_residues[b]+=!is_gap(B->seq_al[b][c]);
6078                        s=GET_CASE(B->residue_case, B->seq_al[b][c]);
6079                      }
6080                    
6081                    fprintf (fp,"%c",s );
6082                                 }
6083                if (B->output_res_num)fprintf (fp, " %4d", n_residues[b]);
6084                fprintf (fp,"\n");
6085              }
6086              
6087              fprintf (fp,"\n");
6088            }
6089     
6090      fprintf (fp,"\n\n");
6091      vfree (n_residues);
6092      
6093      return fp;
6094     }
6095 FILE * output_aln_score ( Alignment *B, FILE *fp){return output_Alignment_score(B, fp);}
6096 FILE * output_Alignment_score ( Alignment *B, FILE *fp)
6097     {
6098     int a, b, c;
6099     static int max_len=0;
6100     static int line;        
6101     int ch;
6102     
6103     if (fp==NULL)return fp;
6104     if ( max_len==0)
6105         {
6106         for ( a=0; a< B->nseq; a++)
6107             {if ( strlen (B->name[a])>max_len)
6108                 max_len= strlen ( (B->name[a]));
6109             }
6110         max_len+=4;
6111
6112         }       
6113    line=get_msa_line_length(0, 0);
6114    sprintf (B->name[B->nseq], "CONS"); 
6115    fprintf ( fp, "T_COFFEE ALIGNMENT\nCPU TIME:%d sec.\n", (B->cpu+get_time())/1000);  
6116    fprintf ( fp, "SCORE=%d\n", B->score_aln);
6117    for ( a=0;a<B->nseq; a++)fprintf ( fp, "%s: %d\n", B->name[a], B->score_seq[a]);
6118    fprintf ( fp, "\n"); 
6119     for (a=0; a<B->len_aln; a+=line)
6120            {for (b=0; b<B->nseq; b++)
6121               {
6122               fprintf (fp,"%-*s",max_len,B->name[b]);
6123               for (c=a;c<a+line && c<B->len_aln;c++)
6124                 {
6125                 ch=B->seq_al[b][c];
6126                 if (ch==NO_COLOR_RESIDUE)fprintf (fp,"-");
6127                 else if ( ch==NO_COLOR_GAP)fprintf (fp,"*");
6128                 else if ( ch<10 && ch>=0)fprintf (fp,"%d",ch);
6129                 else if ( ch>10)fprintf (fp,"#");
6130                 else if ( ch<0)fprintf  (fp,".");
6131                 else fprintf (fp,"9");          
6132                 }             
6133               fprintf (fp,"\n");              
6134               }
6135             fprintf (fp,"\n");
6136             fprintf (fp,"%-*s",max_len,B->name[b]);
6137             for (c=a;c<a+line && c<B->len_aln;c++)
6138               {
6139               ch=B->seq_al[b][c];
6140               if (ch==NO_COLOR_RESIDUE)fprintf (fp,"-");
6141               else if ( ch==NO_COLOR_GAP)fprintf ( fp, "*");
6142               else if ( ch<10 && ch>=0)fprintf (fp,"%d",ch);
6143               else if ( ch>10)fprintf (fp,"#");
6144               else if ( ch<0)fprintf (fp,".");
6145               else fprintf (fp,"9");            
6146               }       
6147             fprintf (fp,"\n\n\n");
6148            }
6149     fprintf (fp,"\n\n");
6150     return fp;
6151     }
6152 FILE * output_aln_with_res_number ( Alignment *B, FILE *fp){return  output_Alignment_with_res_number(B, fp);}
6153 FILE * output_Alignment_with_res_number ( Alignment *B, FILE *fp)
6154     {
6155     int a, b, c;
6156     static int max_len=0;
6157     static int line;        
6158     int**order;
6159
6160     if (fp==NULL)return fp;
6161     if ( max_len==0)
6162         {
6163         for ( a=0; a< B->nseq; a++)
6164             {if ( strlen (B->name[a])>max_len)
6165                 max_len= strlen ( (B->name[a]));
6166             }
6167         max_len+=4;
6168         line=60;
6169         }       
6170    order=copy_int ( B->order,declare_int ( B->nseq, 2), B->nseq, 2);
6171     
6172    fprintf ( fp, "T_COFFEE ALIGNMENT\nCPU TIME:%d sec.\n", (B->cpu+get_time())/1000);     
6173    fprintf ( fp, "\n"); 
6174     for (a=0; a<B->len_aln; a+=line)
6175            {for (b=0; b<B->nseq; b++)
6176              {
6177               fprintf (fp,"%-*s %3d %4d ",max_len,B->name[b], order[b][0], order[b][1] );
6178               for (c=a;c<a+line && c<B->len_aln;c++)
6179                 {
6180                 order[b][1]+=1-is_gap(B->seq_al[b][c]);
6181                 fprintf (fp,"%c",toupper(B->seq_al[b][c]) );
6182                 }
6183               fprintf (fp," %4d\n", order[b][1] );
6184               }
6185             fprintf (fp,"\n");
6186             }
6187     fprintf (fp,"\n\n");
6188
6189     free_int (order, -1);
6190     return fp;
6191     }
6192
6193 void output_constraints ( char *fname, char *mode,Alignment *A)
6194         {
6195         FILE *fp;
6196         Constraint_list *CL;
6197         char *buf;
6198         char **name_list;
6199         
6200         if ( !A->CL || strm ( mode, "pdb"))
6201            {
6202                if (!A->S)
6203                   {
6204                       A->S=aln2seq(A);
6205                   }
6206                
6207                CL=declare_constraint_list ( A->S, NULL, NULL, 0, NULL, NULL);
6208                CL=aln2constraint_list (A,CL, mode);
6209                compact_list (CL, 0, CL->ne, "default");
6210                fp=save_constraint_list ( CL, 0, CL->ne,fname, NULL, "lib",A->S);
6211                vfclose (fp);
6212                free_constraint_list (CL);
6213                return;
6214            }
6215         else if ( strncmp ( mode, "extended_pair", 13)==0)
6216           {
6217             buf=duplicate_string (mode+14);
6218             
6219             name_list=vcalloc(2, sizeof(char*));
6220             name_list[0]=strtok (buf,"_");
6221             name_list[1]=strtok (NULL,"_");
6222             mode[13]='\0';
6223             
6224         
6225             CL=A->CL;
6226             compact_list (CL, 0, CL->ne, "default");
6227             fp=save_sub_list_header (vfopen(fname, "w"),2, name_list,CL);
6228             fp=save_extended_constraint_list_pair (CL, "pair",name_list[0],name_list[1],fp);
6229             fp=save_list_footer (fp, CL);
6230             vfree (buf);
6231           }
6232         else if ( strm2 (mode, "extended_lib","extended_cosmetic"))
6233           {
6234             CL=A->CL;
6235             compact_list (CL, 0, CL->ne, "default");
6236             fp=save_extended_constraint_list ( CL,mode+9, vfopen(fname, "w"));
6237           }
6238         else 
6239            {
6240                CL=(Constraint_list *)A->CL;
6241                compact_list (CL, 0, CL->ne, "default");
6242                fp=save_constraint_list ( CL, 0, CL->ne,fname, NULL, "lib",A->S);
6243            }
6244         vfclose ( fp);
6245         
6246         if ( (Constraint_list *)A->CL !=CL)free_constraint_list (CL);
6247
6248         return;
6249
6250         }
6251 void output_model_aln (char *fname, Alignment*A )
6252         {
6253           FILE *fp;
6254           int a;
6255           Dp_Model *M;
6256           Dp_Result *R;
6257           char *string;
6258           
6259           if ( A->Dp_result==NULL)
6260             {
6261               fprintf ( stderr, "\nWARNING Could Not Output Model %s [%s]", fname, PROGRAM);
6262             }
6263           R=A->Dp_result;
6264           M=R->Dp_model;
6265
6266           fp=vfopen ( fname, "w");
6267           for (a=0; a<M->nstate; a++)
6268             {
6269               if (M->model_comments[a][0])fprintf ( fp, "#STATE %c: %s\n", 'a'+a, M->model_comments[a]);
6270             }
6271           string=vcalloc ( R->len+1, sizeof (char));
6272           for (a=0; a<R->len; a++)string[a]=R->traceback[a]+'a';
6273           fprintf ( fp, ">%s\n",fname);
6274           fp=output_string_wrap ( 50,string, fp);
6275           vfree(string);
6276           fprintf ( fp, "\n");
6277         
6278           vfclose (fp);
6279           return;
6280         }
6281 char * output_fasta_sub_aln (char *fname, Alignment*A, int ns, int *ls  )
6282 {
6283   int a,s;
6284   FILE *fp;
6285   if (fname==NULL)fname=vtmpnam (NULL);
6286   fp=vfopen (fname, "w");
6287   for (a=0; a<ns; a++)
6288     {
6289       s=ls[a];
6290       fprintf (fp, ">%s %s\n%s\n", A->name[s],A->seq_comment[s],A->seq_al[s]);
6291     }
6292   vfclose (fp);
6293   return fname;
6294 }
6295 char * output_fasta_sub_aln2 (char *fname, Alignment*A, int *ns, int **ls  )
6296 {
6297   int a,g,s;
6298   FILE *fp;
6299   if (fname==NULL)fname=vtmpnam (NULL);
6300   fp=vfopen (fname, "w");
6301   for ( g=0; g<2; g++)
6302     for (a=0; a<ns[g]; a++)
6303       {
6304         s=ls[g][a];
6305         fprintf (fp, ">%s %s\n%s\n", A->name[s],A->seq_comment[s],A->seq_al[s]);
6306       }
6307   vfclose (fp);
6308   return fname;
6309 }
6310
6311 int output_suchard_aln (char *out_file, Alignment *A)
6312 {
6313   int a, b, c, d;
6314   FILE *fp;
6315   
6316   A=back_translate_dna_aln (A);
6317   
6318   for ( c=0,a=0; a<A->len_aln; a++, c++)
6319              {
6320                if (c==3)c=0;
6321                for (b=0; b<A->nseq; b++)
6322                  {
6323                  if (c==2)
6324                    {
6325                      A->seq_al[b][a]='-';
6326                    }
6327                  }
6328              }
6329   A=ungap_aln_n (A, 1);    
6330   fp=vfopen (out_file, "w");
6331   for ( a=0; a< A->nseq; a++)
6332     {
6333       for (b=0; b< A->len_aln; b++)
6334         {
6335           c=tolower(A->seq_al[a][b]);
6336           if ( c=='a')d=1;
6337           else if ( c=='g')d=2;
6338           else if ( c=='c')d=3;
6339           else if ( c=='t')d=4;
6340           else if ( c=='u')d=5;
6341           else d=6;
6342
6343           fprintf ( fp, "%d", d);
6344         }
6345       fprintf ( fp, "\n");
6346     }
6347   vfclose (fp);
6348   exit (EXIT_SUCCESS);
6349 }
6350   
6351 void output_fasta_aln (char *fname, Alignment*A )
6352         {
6353         FILE *fp;
6354         int a;
6355         int line=0;
6356
6357         line=get_msa_line_length (line, A->len_aln+1);
6358         fp=vfopen ( fname, "w");
6359
6360         for ( a=0; a< A->nseq; a++)
6361                 {
6362                   fprintf ( fp, ">%s", A->name[a]);
6363                   
6364                   if ( A->seq_comment[a][0] && !isblanc (A->seq_comment[a]))fprintf ( fp, " %s", A->seq_comment[a]);
6365                   fprintf ( fp, "\n");
6366                   fp=output_string_wrap ( line,A->seq_al[a] , fp);
6367                   fprintf ( fp, "\n");
6368                 }
6369         vfclose (fp);
6370         }
6371         
6372 void output_pir_aln (char *fname, Alignment*A )
6373         {
6374         int a;
6375         FILE *fp;
6376         char type[20];
6377         
6378         
6379
6380         
6381         
6382         fp=vfopen ( fname, "w");
6383         for ( a=0; a< A->nseq; a++)
6384                 {
6385                 if      ( strm ( get_string_type (A->seq_al[a]),"DNA") || strm ( get_string_type (A->seq_al[a]),"RNA"))sprintf(type, "DL");
6386                 else if ( strm ( get_string_type (A->seq_al[a]),"PROTEIN"))sprintf(type, "P1");
6387                 fprintf ( fp, ">%s;%s\n%s\n",type, A->name[a], A->seq_comment[a]);
6388                 fp=output_string_wrap ( 50,A->seq_al[a] , fp);
6389                 fprintf ( fp, "\n*\n");
6390                 }
6391                 
6392         vfclose (fp);
6393         }           
6394
6395 int landscape_msa;
6396 int  set_landscape_msa (int len)
6397 {
6398   if ( len==0)landscape_msa=-1;
6399   else
6400     {
6401       landscape_msa=len;
6402     }
6403   return landscape_msa;
6404 }
6405 int get_msa_line_length (int line, int aln_len)
6406 {
6407   if (landscape_msa==-1) return aln_len;
6408   else if ( landscape_msa)return landscape_msa;
6409   else if (line) return line;
6410   else 
6411     {
6412       return (getenv ("ALN_LINE_LENGTH"))?atoi(getenv("ALN_LINE_LENGTH")):ALN_LINE_LENGTH;
6413     }
6414 }
6415
6416 void output_msf_aln (char *fname,Alignment *B)
6417         {
6418         int a, b, c;    
6419         char *seq;
6420         int *all_checks;
6421         int i,j;
6422         long grand_checksum;
6423         FILE *fp;
6424         int max_len;
6425         int line=0;
6426         int block=10;
6427         int c_block;
6428         char aa;
6429         
6430
6431         line=get_msa_line_length (line, B->len_aln+1);
6432         
6433
6434         for ( max_len=0,a=0; a< B->nseq; a++)max_len= MAX(strlen ( B->name[a]),max_len);
6435
6436
6437         max_len+=5;
6438         
6439         fp=vfopen (fname, "w");
6440         
6441         seq =vcalloc(B->len_aln,  sizeof(char));
6442         all_checks =vcalloc(B->nseq, sizeof(int));
6443         for ( i=0; i< B->nseq; i++)
6444           {
6445             for ( j=0; j<B->len_aln; j++)
6446               {
6447                 if ( is_gap(B->seq_al[i][j]))seq[j]='.';
6448                 else seq[j]=B->seq_al[i][j]=toupper(B->seq_al[i][j]);
6449                 
6450               }
6451             all_checks[i] = SeqGCGCheckSum(seq, (int)B->len_aln);
6452           }
6453         grand_checksum = 0;
6454         for(i=0; i<B->nseq; i++) grand_checksum += all_checks[i];
6455         grand_checksum = grand_checksum % 10000;
6456         fprintf(fp,"PileUp\n\n");
6457         B=get_aln_type(B);
6458         fprintf(fp,"\n\n   MSF:%5d  Type: ",B->len_aln);
6459         if(strm ( (B->S)->type, "DNA") || strm ( (B->S)->type, "RNA"))
6460                 fprintf(fp,"N");
6461         else
6462                 fprintf(fp,"P");
6463         fprintf(fp,"    Check:%6ld   .. \n\n", (long)grand_checksum);
6464         for (i=0; i< B->nseq; i++)
6465           {
6466             fprintf ( fp, " Name: %s oo  Len:%5d  Check:%6ld  Weight:  %.3f\n", B->name[i], B->len_aln,(long)all_checks[i],(B->S)->W?((B->S)->W)->SEQ_W[i]:1.00);
6467           }
6468         fprintf(fp,"\n//\n\n");
6469         
6470         for (a=0; a<B->len_aln; a+=line)
6471            {
6472              fprintf ( fp,"\n\n"); 
6473              for (b=0; b<B->nseq; b++)
6474                {
6475                  fprintf (fp,"%-*s ",max_len,B->name[b]);
6476                  for (c_block=0,c=a;c<a+line && c<B->len_aln;c++)
6477                    {
6478                      if ( c_block==block)
6479                             {
6480                               fprintf (fp, " ");
6481                               c_block=0;
6482                             }
6483                         c_block++;
6484                      aa=(is_gap(B->seq_al[b][c]))?'.': toupper(B->seq_al[b][c]);
6485                      fprintf (fp,"%c",aa );
6486                    }
6487                  if ( c_block==block)
6488                             {
6489                               fprintf (fp, " ");
6490                               c_block=0;
6491                             }
6492                  fprintf (fp,"\n");
6493                  
6494                }
6495            }
6496         fprintf ( fp,"\n");              
6497         vfclose ( fp);
6498         
6499         
6500         vfree(seq);
6501         vfree(all_checks);
6502         
6503
6504         return;
6505
6506 int SeqGCGCheckSum(char *seq, int len)
6507 {
6508         int  i;
6509         long check;
6510         
6511         for( i=0, check=0; i< len; i++,seq++)
6512                 check += ((i % 57)+1) * toupper(*seq);
6513
6514         return(check % 10000);
6515 }  
6516 void old_output_msf_aln (char *fname,Alignment *B)
6517         {
6518         FILE *fp;
6519         static int *put_seq;
6520         int a, b, c;
6521         int line=0;
6522         char aa;
6523         char *buf;
6524         int max_len;
6525         int seq_max_len;
6526         
6527         line=get_msa_line_length (line, B->len_aln+1);
6528         
6529         
6530         for ( max_len=0,a=0; a< B->nseq; a++)max_len= MAX(strlen ( B->name[a]),max_len);
6531         for ( seq_max_len=0,a=0; a< B->nseq; a++)seq_max_len= MAX(strlen ( B->seq_al[a]),max_len);
6532         
6533
6534         buf=vcalloc(seq_max_len+1, sizeof (int)); 
6535         
6536         if ( put_seq==NULL)
6537                 put_seq= vcalloc ( B->nseq, sizeof (int));
6538         put_seq[0]=1;
6539         
6540         
6541         for ( b=1; b< B->nseq; b++)
6542                 {
6543                 sprintf ( buf, "%s", B->seq_al[b]);
6544                 ungap(buf);
6545                 put_seq[b]=( strlen (buf)>0)?1:0;
6546                 }       
6547         
6548         fp=vfopen ( fname, "w");
6549         fprintf ( fp, "MSF: %d Type P Check: 5083 ..\n", B->len_aln);
6550         for ( a=0; a< B->nseq; a++)
6551                 {
6552                 if ( put_seq[a]==1)
6553                         fprintf ( fp,"Name: %s\n",B->name[a]);
6554                 }
6555          fprintf ( fp, "//\n");
6556          for (a=0; a<B->len_aln; a+=line)
6557            {for (b=0; b<B->nseq; b++)
6558              {
6559              if ( put_seq[b]==1)
6560                 {
6561                 fprintf (fp,"%-*s ",max_len,B->name[b]);
6562                 for (c=a;c<a+line && c<B->len_aln;c++)
6563                         {
6564                         
6565                           
6566                             
6567                         aa=(B->seq_al[b][c]=='-')?'.': toupper(B->seq_al[b][c]);
6568                         fprintf (fp,"%c",aa );
6569                         }
6570                 fprintf (fp,"\n");
6571                 }
6572               }
6573             fprintf (fp,"\n");
6574             }
6575         fprintf ( fp,"\n\n");            
6576         vfclose ( fp);
6577
6578         vfree (buf);
6579         vfree(put_seq);
6580         }
6581         
6582 void output_saga_aln ( char *name, Alignment *B)
6583     {
6584     int a, b, c;
6585     FILE *fp;
6586
6587
6588
6589     int max_len;
6590     int line=0;
6591     
6592     line=get_msa_line_length (line, B->len_aln+1);
6593     
6594     
6595     
6596     for ( max_len=0,a=0; a< B->nseq; a++)max_len= (strlen ( B->name[a])>max_len)?(strlen ( B->name[a])):max_len;
6597             
6598         
6599
6600
6601     fp= vfopen ( name, "w");
6602     
6603     fprintf (fp, "\nSAGA FORMAT\nalignement  %s nseq=%d len=%d\n", name, B->nseq, B->len_aln);
6604        
6605     fprintf (fp, "\n\n");
6606     for (a=0; a<B->len_aln; a+=line)
6607            {for (b=0; b<B->nseq; b++)
6608              {fprintf (fp,"%-*s ",max_len,B->name[b]);
6609               for (c=a;c<a+line && c<B->len_aln;c++)
6610                 {
6611                   fprintf (fp,"%c",(B->seq_al[b][c]) );
6612                 }
6613               fprintf (fp,"\n");
6614               }
6615             fprintf (fp,"\n");
6616             }
6617     fprintf (fp,"\n\n");
6618     vfclose ( fp);
6619     }
6620 void output_compact_aln ( char *name, Alignment *B)
6621     {
6622     int a, b, c;
6623     FILE *fp;
6624     int do_print=0;
6625
6626
6627     int max_len;
6628     int line=0;
6629
6630     line=get_msa_line_length (line, B->len_aln+1);
6631     
6632     
6633     for ( max_len=0,a=0; a< B->nseq; a++)max_len= (strlen ( B->name[a])>max_len)?(strlen ( B->name[a])):max_len;
6634             
6635         
6636
6637
6638     fp= vfopen ( name, "w");
6639     
6640     fprintf (fp, "\nSAGA FORMAT\nalignement  %s nseq=%d len=%d", name, B->nseq, B->len_aln);
6641     fprintf (fp, "\n\n");
6642     for (a=0; a<B->len_aln; a+=line)
6643            {for (b=0; b<B->nseq; b++)
6644              {
6645              
6646              for ( do_print=0, c=a;c<a+line && c<B->len_aln;c++)
6647                do_print+=1-is_gap(B->seq_al[b][c]);
6648              if ( do_print>0)
6649                    {
6650                      fprintf (fp,"%-*s ",max_len,B->name[b]);
6651              
6652              
6653              
6654                      for (c=a;c<a+line && c<B->len_aln;c++)
6655                        {
6656                          if ( is_gap(B->seq_al[b][c])&& B->seq_al[b][c]!='-' )fprintf (fp,"%c", '-');
6657                          else fprintf (fp,"%c",(B->seq_al[b][c]) );
6658                        }
6659                      fprintf (fp,"\n");
6660                    }
6661               }
6662             fprintf (fp,"\n");
6663             }
6664     fprintf (fp,"\n\n");
6665     vfclose ( fp);
6666     }
6667
6668 void output_clustal_aln ( char *name, Alignment *B)
6669 {
6670   return output_generic_clustal_aln (name, B, "tc_clustal");
6671 }
6672 void output_strict_clustal_aln ( char *name, Alignment *B)
6673 {
6674   return output_generic_clustal_aln (name, B, "strict_clustal");
6675 }
6676
6677 void output_generic_clustal_aln ( char *name, Alignment *B, char *mode)
6678     {
6679     int a, b, c;
6680     FILE *fp;
6681     int max_len=0;
6682     int line=0; 
6683     int *n_residues;
6684     
6685     if ( getenv ("SEP_4_TCOFFEE"))
6686       {
6687         while ( line<B->len_aln && B->seq_al[0][line]!='o' && B->seq_al[0][line]!='O')line++;
6688         if ( B->seq_al[0][line]=='O' || B->seq_al[0][line]=='o')line++;
6689       }
6690     else
6691       {
6692         while ( line<B->len_aln)line++;
6693       }
6694     
6695     if ( line==B->len_aln)line=get_msa_line_length (0, B->len_aln+1);
6696     
6697     n_residues=vcalloc ( B->nseq+1, sizeof (int));
6698     for ( a=0; a< B->nseq; a++)
6699             {if ( strlen (B->name[a])>max_len)
6700                 max_len= strlen ( (B->name[a]));
6701             n_residues[a]=B->order[a][1];
6702             }
6703     max_len=MAX(max_len+2, 16);
6704         
6705
6706     fp= vfopen ( name, "w");
6707
6708     if ( strm (mode, "strict_clustal"))
6709       fprintf ( fp, "CLUSTAL W (1.83) multiple sequence alignment");
6710     else
6711       fprintf (fp, "CLUSTAL FORMAT for %s %s [%s] [MODE: %s ], CPU=%.2f sec, SCORE=%d, Nseq=%d, Len=%d ", PROGRAM, VERSION,URL, retrieve_mode (),(float)(B->cpu+get_time())/1000, B->score_aln, B->nseq, B->len_aln);
6712     fprintf (fp, "\n\n");
6713
6714
6715     if ( B->len_aln==0)
6716       {
6717         for (b=0; b<=B->nseq; b++)
6718           fprintf (fp,"%-*s -\n",max_len, B->name[b]);
6719       }
6720     
6721     else
6722       {
6723         for (a=0; a<B->len_aln; a+=line)
6724           {for (b=0; b<=B->nseq; b++)
6725             {
6726               if (b!=B->nseq)
6727                 {
6728                   fprintf (fp,"%-*s",max_len, B->name[b]);
6729                   for (c=a;c<a+line && c<B->len_aln;c++)
6730                     {
6731                       if ( is_gap(B->seq_al[b][c]))fprintf (fp,"%c", '-');
6732                       else 
6733                         {
6734                           n_residues[b]++;
6735                           fprintf (fp, "%c", GET_CASE(B->residue_case, B->seq_al[b][c]));
6736                           
6737                         }
6738                       
6739                     }
6740                   if (B->output_res_num)fprintf (fp, " %d", n_residues[b]);
6741                   fprintf (fp,"\n");
6742                 }
6743               else if ( b==B->nseq)
6744                 {
6745                   fprintf (fp,"%-*s",max_len," ");                  
6746                   for (c=a;c<a+line && c<B->len_aln;c++)
6747                     {
6748                       fprintf ( fp, "%c", analyse_aln_column (B, c));
6749                     }
6750                   fprintf (fp,"\n");
6751                 }
6752             }
6753           fprintf (fp,"\n"); 
6754           }
6755       }
6756     fprintf (fp,"\n\n");
6757     vfree (n_residues);
6758     vfclose ( fp);
6759     }    
6760 FILE * output_generic_interleaved_aln (FILE *fp, Alignment *B, int line, char gap, char *mode)
6761     {
6762     int a, b, c;
6763     int max_len=0;
6764     int *n_residues;
6765     
6766     
6767     n_residues=vcalloc ( B->nseq+1, sizeof (int));
6768     for ( a=0; a< B->nseq; a++)
6769             {if ( strlen (B->name[a])>max_len)
6770                 max_len= strlen ( (B->name[a]));
6771             n_residues[a]=B->order[a][1];
6772             }
6773     max_len=MAX(max_len+2, 16);
6774         
6775
6776     
6777     
6778     if ( B->len_aln==0)
6779       {
6780         for (b=0; b<=B->nseq; b++)
6781           fprintf (fp,"%-*s -\n",max_len, B->name[b]);
6782       }
6783     
6784     else
6785       {
6786         for (a=0; a<B->len_aln; a+=line)
6787           {for (b=0; b<=B->nseq; b++)
6788             {
6789               if (b!=B->nseq)
6790                 {
6791                   fprintf (fp,"%-*s",max_len, B->name[b]);
6792                   for (c=a;c<a+line && c<B->len_aln;c++)
6793                     {
6794                       if ( is_gap(B->seq_al[b][c]))fprintf (fp,"%c", gap);
6795                       else 
6796                         {
6797                           n_residues[b]++;
6798                           fprintf (fp, "%c", GET_CASE(B->residue_case, B->seq_al[b][c]));
6799                           
6800                         }
6801                       
6802                     }
6803                   if (B->output_res_num)fprintf (fp, " %d", n_residues[b]);
6804                   fprintf (fp,"\n");
6805                 }
6806             }
6807           fprintf (fp,"\n"); 
6808           }
6809       }
6810     vfree (n_residues);
6811     return fp;
6812     }    
6813 void output_phylip_aln ( char *name, Alignment *B)
6814     {
6815       int a, b, c, d;
6816     FILE *fp;
6817
6818     int *print_name;
6819     static int line=0;      
6820     line=get_msa_line_length(0, 0);
6821     
6822     print_name=vcalloc ( B->nseq, sizeof (int));
6823     fp= vfopen ( name, "w");
6824     
6825     fprintf (fp, "%3d %d\n", B->nseq, B->len_aln);
6826     for (a=0; a<B->len_aln; a+=line)
6827            {for (b=0; b<B->nseq; b++)
6828              {if ( print_name[b]==0)
6829                 {
6830                   
6831                   fprintf (fp,"%-10.10s  ",B->name[b]);
6832                   print_name[b]=1;
6833                 }
6834                else
6835                  {
6836                    fprintf (fp, "%10.10s ", " ");
6837                  }
6838
6839
6840                for (d=0,c=a;c<a+line && c<B->len_aln;c++, d++)
6841                  {
6842                    if ( d==10)
6843                      {
6844                        fprintf ( fp, " ");
6845                        d=0;
6846                      }
6847                    if ( is_gap(B->seq_al[b][c])&& B->seq_al[b][c]!='-' )fprintf (fp,"%c", '-');
6848                    else fprintf (fp,"%c",(B->seq_al[b][c]) );
6849                  }
6850               fprintf (fp,"\n");
6851               }
6852             fprintf (fp,"\n");
6853             }
6854     fprintf (fp,"\n\n");
6855     vfclose ( fp);
6856     }
6857
6858 void output_rnalign (char *out_file, Alignment *A, Sequence *STRUC)
6859     {
6860     int a, b;
6861     FILE *fp;
6862     char bank_file[100];
6863     char pep_file[100];
6864     char *buf;
6865     
6866     sprintf ( bank_file, "%s.mss", out_file);
6867     sprintf ( pep_file, "%s.one_rna", out_file);
6868     
6869    
6870     buf=vcalloc ( strlen ( A->seq_al[0]+1), sizeof (char));
6871     
6872     for ( b=0,a=0; a< strlen(A->seq_al[0]); a++) 
6873         {
6874         if ( is_gap(A->seq_al[0][a]))
6875                 buf[a]='.';
6876         else
6877                 buf[a]=STRUC->seq[0][b++];
6878         }
6879     buf[a]='\0';
6880     
6881     fp=vfopen ( bank_file, "w");
6882     
6883     fprintf ( fp, "ST\n");
6884     fp=output_string_wrap ( 50, buf, fp);
6885     fprintf ( fp, "\n\n");
6886     
6887     for ( a=0; a<A->nseq-1; a++)
6888         {
6889         fprintf ( fp, "AS %s\n ", A->name[a]);
6890         fp=output_string_wrap ( 50, A->seq_al[a], fp); 
6891         fprintf ( fp, "\n\n");
6892         }
6893     vfclose ( fp);
6894     fp=vfopen ( pep_file, "w");
6895     fprintf ( fp, ">%s\n", A->name[A->nseq-1]); 
6896     fp=output_string_wrap ( 50, A->seq_al[A->nseq-1], fp);
6897     fprintf ( fp, "\n");
6898     vfclose (fp);
6899     }
6900
6901 void output_lib (char *pw_lib_saga_aln_name, Alignment *A )
6902     {
6903     Alignment *B;
6904     char fname[VERY_LONG_STRING];
6905     int a,b;
6906     
6907     B=declare_Alignment (NULL);
6908     
6909     B->nseq=2;
6910     
6911     for ( a=0; a< A->nseq-1; a++)
6912         {
6913         for ( b=a+1; b<A->nseq; b++)
6914                 {
6915                 sprintf ( B->seq_al[0], "%s", A->seq_al[a]);
6916                 sprintf ( B->name[0], "%s", A->name[a]);
6917                 sprintf(B->name[1], "%s", A->name[b]);
6918                 sprintf ( B->seq_al[1], "%s",A->seq_al[b]);
6919                 B->nseq=2;
6920                 sprintf ( fname, "%s_%s_%s.lib",pw_lib_saga_aln_name, A->name[a], A->name[b]);
6921         
6922                 B->len_aln=strlen ( B->seq_al[0]);
6923                 ungap_aln (B);
6924                 output_clustal_aln (fname,B);  
6925                 }
6926         }
6927     } 
6928 void output_pw_lib_saga_aln (char *pw_lib_saga_aln_name, Alignment *A )
6929     {
6930     Alignment *B;
6931     char fname[VERY_LONG_STRING];
6932     int a,b;
6933     
6934     B=declare_Alignment (NULL);
6935     
6936     B->nseq=2;
6937     
6938     for ( a=0; a< A->nseq-1; a++)
6939         {
6940         for ( b=a+1; b<A->nseq; b++)
6941                 {
6942                 sprintf ( B->seq_al[0], "%s", A->seq_al[a]);
6943                 sprintf ( B->name[0], "%s", A->name[a]);
6944                 sprintf(B->name[1], "%s", A->name[b]);
6945                 sprintf ( B->seq_al[1], "%s",A->seq_al[b]);
6946                 B->nseq=2;
6947                 sprintf ( fname, "%s_%s_%s.pw_lib_saga_aln",pw_lib_saga_aln_name, A->name[a], A->name[b]);
6948         
6949                 B->len_aln=strlen ( B->seq_al[0]);
6950                 ungap_aln (B);
6951                 output_clustal_aln (fname,B);  
6952                 }
6953         }
6954     }   
6955 void output_lalign_header( char *name, Alignment *A)
6956     {
6957     FILE *fp;
6958     
6959     fp=vfopen ( name, "w");
6960     fprintf ( fp, " Lalign mode: best local alignments between two sequences\n");
6961     fprintf ( fp, " %s(%s) [%s]\n\n", VERSION, DATE, URL);
6962     fprintf ( fp, " Comparison of:\n(A) %s\t%s\t-%d aa\n", (A->S)->file[A->order[0][0]],(A->S)->name[A->order[0][0]], (A->S)->len[A->order[0][0]]);
6963     fprintf ( fp, "(B) %s\t%s\t-%d aa\n", (A->S)->file[A->order[1][0]],(A->S)->name[A->order[1][0]], (A->S)->len[A->order[1][0]]);
6964     
6965     
6966     vfclose ( fp);
6967     return;
6968     }
6969 void output_stockholm_aln (char *file, Alignment *A, Alignment *ST)
6970 {
6971   FILE *fp;
6972   int a,b,l;
6973     
6974   for (a=0; a<A->nseq; a++)
6975     for (b=0; b<A->len_aln; b++)
6976       if (A->seq_al[a][b]==STOCKHOLM_CHAR)A->seq_al[a][b]='.';
6977   
6978   fp=vfopen (file, "w");
6979   fprintf ( fp, "# STOCKHOLM 1.0\n\n");
6980   output_generic_interleaved_aln (fp,A, 50, '.', NULL);
6981   fprintf ( fp, "//\n");
6982   vfclose (fp);
6983 }
6984                            
6985 void output_glalign ( char *name, Alignment *B, Alignment *S)
6986 {
6987   int a, b, g, s;
6988   int naln=0;
6989   FILE *fp;
6990   int **nr;
6991   B=B->A;
6992   if ( B==NULL){return;}
6993   
6994   fp=vfopen (name, "w");
6995   fprintf (fp, "Format: GLALIGN_01 [Generated with %s ]\n", PROGRAM);
6996   fprintf (fp, "#Each Line corresponds to a column\n");
6997   fprintf (fp, "#First column coresponds to first genome\n");
6998   fprintf (fp, "#Last Column gives the column reliability on a 0-9 scale\n");
6999   fprintf (fp, "#[-1] Indicates that the reliability was not evaluated\n");
7000   
7001   fprintf (fp, "Genome List\n");
7002   for ( a=0; a< B->nseq; a++)
7003     fprintf (fp, "\tGenome %s\n", B->name[a]);
7004   fprintf (fp, "Alignment List\n");
7005   while (B)
7006     {
7007       fprintf (fp, "Alignment %d Len %d Score %d\n", ++naln, B->len_aln, S->score_aln);
7008       nr=duplicate_int (B->order, -1, -1);
7009       for ( a=0; a< B->len_aln; a++)
7010         {
7011           fprintf ( fp, "\t");
7012           for ( b=0; b< B->nseq; b++)
7013             {
7014               g=is_gap (B->seq_al[b][a]);
7015               nr[b][1]+=1-g;
7016
7017               if (g)fprintf (fp, "---- ");
7018               else fprintf ( fp, "%4d ",nr[b][1]);
7019             }
7020           s=((S)?S->seq_al[S->nseq][a]:-1);
7021           if (s==NO_COLOR_RESIDUE)s=-1;
7022           fprintf ( fp,"[ %d ]",s);
7023           fprintf ( fp, "\n");  
7024           
7025         } 
7026       free_int (nr, -1);
7027       B=B->A;
7028       S=S->A;
7029     }
7030   vfclose ( fp);
7031 }
7032 Alignment *input_conc_aln ( char *name, Alignment *IN)
7033 {
7034   FILE *fp;
7035   char *string, *p, *file;
7036   Alignment *F=NULL,*A=NULL, *B=NULL;
7037   
7038   file=vtmpnam (NULL);
7039  
7040   string=file2string(name);
7041   string=substitute ( string, "@", "!Protected!");
7042   string=substitute ( string, TC_REC_SEPARATOR, "@");
7043   strtok (string,"@");
7044   
7045
7046   while ( (p=strtok (NULL,"@"))!=NULL)
7047     {
7048       char *buf;
7049       buf=vcalloc ( strlen (p)+1, sizeof (char));
7050       sprintf (buf,"%s", p);
7051       buf=substitute (buf,"!protected!", "@");
7052       
7053       fp=vfopen (file, "w");
7054       fprintf ( fp, "%s",buf);
7055       vfclose (fp);
7056       vfree (buf);
7057             
7058       if ( is_aln (file))
7059         {
7060           B=main_read_aln (file,NULL);
7061           
7062           if ( !A)
7063             {
7064               if (IN){copy_aln (B, IN);F=A=IN;}
7065               else F=A=B;
7066             }
7067           else
7068             {
7069               A->A=B;
7070               A=A->A;
7071             }
7072         }
7073     }
7074   
7075   vfree (string);
7076   return F;
7077 }
7078
7079 void output_conc_aln ( char *name, Alignment *B)
7080 {
7081   FILE *fp;
7082   int a;
7083   
7084   fp=vfopen (name, "w");
7085   fprintf (fp, "# CONC_MSF_FORMAT_01\n");
7086   while (B)
7087     {
7088       fprintf (fp, "%s\n", TC_REC_SEPARATOR);
7089       for ( a=0; a< B->nseq; a++)
7090         {
7091           fprintf ( fp, ">%s\n%s\n", B->name[a], B->seq_al[a]);
7092         }
7093       B=B->A;
7094       
7095     }
7096   vfclose (fp);
7097 }
7098
7099 void output_lalign ( char *name, Alignment *B)
7100 {
7101   static int output_header;
7102  
7103   B=B->A;
7104   if ( B==NULL){output_header=0;return;}
7105     else if ( output_header==0)
7106       {
7107         output_lalign_header(name, B);
7108         output_header=1;
7109       }
7110   while (B)
7111     {
7112       output_lalign_aln   ( name, B);
7113       B=B->A;
7114     }
7115 }
7116 void output_lalign_aln   ( char *name, Alignment *B)
7117     {
7118     int a, b, c,d=0, s=0;
7119     char col;
7120
7121     float tot=0;
7122     float id=0;
7123
7124     FILE *fp;
7125     int max_len=0;
7126     int line;   
7127     int *n_residues;
7128     int res;
7129
7130         
7131     n_residues=vcalloc ( B->nseq+1, sizeof (int));
7132     for ( a=0; a< B->nseq; a++)
7133             {if ( strlen (B->name[a])>max_len)
7134                 max_len= strlen ( (B->name[a]));
7135             n_residues[a]=B->order[a][1];
7136             }
7137     max_len=MAX(max_len+2, 16);
7138     line=60;
7139     
7140     
7141
7142     fp= vfopen ( name, "a");
7143     
7144     for (a=0; a< B->len_aln; a++)
7145       {
7146         if ( !is_gap(B->seq_al[0][a]) && !is_gap(B->seq_al[1][a]))
7147              {
7148                tot++;
7149                id+=(B->seq_al[0][a]==B->seq_al[1][a]);
7150              }
7151       }
7152     
7153     id=(id*100)/tot;
7154     fprintf (fp, " %.1f%% identity in %d aa overlap; score: %d\n\n", id,(int)tot, B->score_aln);
7155     
7156     
7157     for (a=0; a<B->len_aln; a+=line)
7158            {for (b=0; b<5; b++)
7159              {
7160                  if ( b==0 || b==4)
7161                    {
7162                      if ( b==0)s=0;
7163                      if ( b==4)s=1;
7164                      fprintf (fp,"%-*s",max_len," ");
7165                      for (d=0,c=a;c<a+line && c<B->len_aln;c++)
7166                        {
7167                          res=!is_gap ( B->seq_al[s][c]);
7168                          n_residues[s]+=res;
7169                          if ( (n_residues[s]%10)==0 && res && (c-a+4)<line){fprintf (fp, "%-4d", n_residues[s]);d=-3;}  
7170                          else
7171                            {
7172                              if ( d==0)fprintf (fp, " ");
7173                              else d++;
7174                            }
7175                        }
7176                      fprintf (fp,"\n");
7177                    }
7178                  else if (b==1 || b==3)
7179                     {
7180                       if ( b==1)s=0;
7181                       if ( b==3)s=1;
7182                         fprintf (fp,"%-*s",max_len, B->name[s]);
7183                         for (c=a;c<a+line && c<B->len_aln;c++)
7184                             {
7185                                 if ( is_gap(B->seq_al[s][c]))fprintf (fp,"%c", '-');
7186                                 else 
7187                                     {
7188                                         fprintf (fp, "%c", GET_CASE(B->residue_case, B->seq_al[s][c]));
7189                                     }
7190                             }
7191                         fprintf (fp,"\n");
7192                     }
7193                  else if ( b==2)
7194                     {
7195                     fprintf (fp,"%-*s",max_len," ");                
7196                     for (c=a;c<a+line && c<B->len_aln;c++)
7197                             {
7198                             col=analyse_aln_column (B, c);
7199                             if ( col=='*')col=':';
7200                             else if ( col==':')col='.';
7201                             else if ( col=='.')col=' ';
7202                             fprintf ( fp, "%c", col);
7203                             }
7204                     fprintf (fp,"\n");
7205                     }
7206              }
7207            fprintf (fp,"\n"); 
7208            }
7209     
7210     fprintf (fp,"\n\n----------\n\n");
7211     vfree (n_residues);
7212     vfclose ( fp);
7213     }   
7214
7215
7216 /****************************************************************************************************/
7217 /*************************************UTIL *********************************************************/
7218 /**************************************************************************************************/
7219
7220
7221 /****************************************************************************************************/
7222 /***************************                                    *************************************/
7223 /***************************             PROCESSING             *************************************/
7224 /***************************                                    *************************************/
7225 /*******************************************************************************************/
7226 /*                                                                                         */
7227 /*                                                                                         */
7228 /*                              THREADING                                                  */
7229 /***************************************************************************************** */
7230
7231 char *thread_aa_seq_on_dna_seq( char *s)
7232      {
7233          int l, b, c;
7234          char *array;
7235          
7236
7237          l=strlen ( s);
7238          array=vcalloc ( l*3 +1, sizeof (char));
7239          for ( b=0, c=0; b< l; b++, c+=3)
7240              {
7241                  array[c]=s[b];
7242                  array[c+1]='o';
7243                  array[c+2]='o';
7244              }
7245          array[c]='\0';
7246          return array;
7247      }
7248
7249 Alignment *thread_dnaseq_on_prot_aln (Sequence *S, Alignment *A)
7250         {
7251             Alignment *B=NULL;
7252             int a, b, c, n, la, ls, ln, m;
7253
7254             B=copy_aln ( A, B);
7255             B=realloc_aln2 ( B, B->nseq, B->len_aln*3 +1);
7256
7257             for ( n=0,a=0; a< A->nseq; a++)
7258                 {
7259                 for ( m=0,b=0; b< S->nseq; b++)
7260                     {
7261                     if (strm (A->name[a], S->name[b]) )
7262                        {
7263                            m=1;
7264                            n++;
7265                            ungap ( S->seq[b]);
7266                            B->seq_al[a][0]='\0';
7267                            for (la=0, ls=0, ln=0; la< A->len_aln; la++)
7268                                {
7269                                    for (c=0; c< 3; c++)
7270                                        B->seq_al[a][ls++]=(is_gap(A->seq_al[a][la]))?'-':S->seq[b][ln++];
7271                                }
7272                        B->seq_al[a][ls]='\0';
7273                        }
7274                     }
7275                 if ( m==0)
7276                        {
7277                        for (la=0, ls=0, ln=0; la< A->len_aln; la++)
7278                                {
7279                                 
7280                                    B->seq_al[a][ls++]=A->seq_al[a][la];
7281                                    B->seq_al[a][ls++]='-';
7282                                    B->seq_al[a][ls++]='-';
7283                                }
7284                        }
7285                 }
7286             
7287             B->len_aln=strlen ( B->seq_al[0]);
7288             return B;
7289         }
7290 void thread_seq_struc2aln ( Alignment *A, Sequence *ST)
7291         {
7292         int a, b, c,d;
7293         int len, cons;
7294         
7295         for ( a=0; a< A->nseq; a++)
7296                 for ( b=0; b< ST->nseq; b++)
7297                         {
7298                         if ( strcmp ( A->name[a], ST->name[b])==0)
7299                                 {
7300                                 ungap (ST->seq[b]);
7301                                 len=strlen(A->seq_al[a]);
7302                                 for ( c=0, d=0; c<len; c++)
7303                                         {
7304                                         if ( !is_gap(A->seq_al[a][c]))A->seq_al[a][c]=ST->seq[b][d++];
7305                                         }
7306                                 }
7307                         }
7308         cons=name_is_in_list ("Cons", ST->name, ST->nseq, 100);
7309         if ( cons!=-1 && A->len_aln==strlen ( ST->seq[cons]))
7310           {
7311             sprintf (A->name[A->nseq], "Cons");
7312             sprintf (A->seq_al[A->nseq],"%s", ST->seq[cons]);
7313             A->nseq++;
7314           }
7315         }
7316 void cache_id ( Alignment *A)
7317         {
7318         int a, b,n;
7319         char r1, r2, r3;
7320         
7321         for ( a=0; a< A->len_aln; a++)
7322                 {
7323                 for ( b=0, n=0; b< A->nseq; b++)if ( !is_gap(A->seq_al[b][a]))n++;
7324                 for ( b=0; b< A->nseq; b++)
7325                         if ( !is_gap(A->seq_al[b][a]) && n==A->nseq)A->seq_al[b][a]='h';
7326                         else if( !is_gap(A->seq_al[b][a]))A->seq_al[b][a]='x';
7327                 }
7328         for ( a=0; a< A->nseq; a++)
7329                 {
7330                 for ( b=1; b< A->len_aln-1; b++)
7331                         {
7332                         r1=A->seq_al[a][b-1];
7333                         r2=A->seq_al[a][b];
7334                         r3=A->seq_al[a][b+1];
7335                         if (r2=='h')
7336                                 {
7337                                 if ( (r1=='h' || r1=='b') && (r3=='h' || r3=='b'))A->seq_al[a][b]='h';
7338                                 else A->seq_al[a][b]='b';
7339                                 }
7340                         }
7341                 for ( b=1; b< A->len_aln-1; b++)if ( A->seq_al[a][b]=='b')A->seq_al[a][b]='x';
7342                 }
7343                         
7344         }
7345                         
7346                                                                          
7347 /*******************************************************************************************/
7348 /*                                                                                         */
7349 /*                                                                                         */
7350 /*                               PROCESING OF EST                                          */
7351 /*                                                                                         */
7352 /***************************************************************************************** */
7353 int process_est_sequence ( Sequence *S, int *cluster_list)
7354         {
7355         char **inverted_seq;
7356         int T=20;
7357         int a, b;
7358         int V1, V2;
7359         int **sens;
7360         int **a_sens;
7361         int **best;
7362         int *solution;
7363         char buf [VERY_LONG_STRING];
7364         int n_clusters=0;
7365         int n;
7366         
7367         sens=declare_int ( S->nseq,S->nseq);
7368         a_sens=declare_int ( S->nseq,S->nseq);
7369         best=declare_int ( S->nseq,S->nseq);
7370         
7371         
7372         inverted_seq=vcalloc ( S->nseq, sizeof (char*));
7373         for ( a=0; a<S->nseq; a++)
7374                 inverted_seq[a]=invert_seq ( S->seq[a]);
7375                 
7376         for ( a=0; a< S->nseq-1; a++)
7377                 {
7378                 
7379                 for ( b=a+1; b<S->nseq; b++)
7380                                      {
7381                                      
7382                                      V1=sens[a][b]=sens[b][a]=get_best_match ( S->seq[a], S->seq[b]);
7383                                      V2=a_sens[a][b]=a_sens[b][a]=get_best_match ( S->seq[a],inverted_seq[b]);
7384                                      best[a][b]=best[b][a]=(V1>V2)?V1:V2;
7385                                      }
7386                 }
7387         solution=SHC ( S->nseq, a_sens, sens);
7388         
7389         
7390         for ( a=0; a<S->nseq; a++)cluster_list[a]=-1;
7391         for ( a=0; a<S->nseq; a++)
7392                 {
7393                 n=search_for_cluster (a, n_clusters, cluster_list, T, S->nseq, best);
7394                 if ( n>0)n_clusters++;
7395                 }
7396         fprintf ( stderr, "\nTHERE %s %d Independant Cluster(s) in your sequences",(n_clusters>1)?"are":"is",(n_clusters));
7397         for (a=0; a<n_clusters; a++)
7398                 {
7399                 fprintf (stderr, "\n");
7400                 for ( b=0; b<S->nseq; b++)
7401                         {
7402                         if ( cluster_list[b]==a)fprintf ( stderr, "%s ", S->name[b]);
7403                         }
7404                 }
7405                 
7406         for ( a=0; a<S->nseq; a++)
7407                 {
7408                 if ( solution[a]==-1)
7409                         {
7410                         S->seq[a]=inverted_seq[a];
7411                         sprintf ( buf, "i_%s", S->name[a]);
7412                         sprintf ( S->name[a], "%s", buf);
7413                         }
7414                 }
7415         return n_clusters;
7416         }
7417
7418 int search_for_cluster ( int seq, int cluster_number, int *cluster_list, int T, int nseq, int **S)      
7419         {
7420         int n=0,a;
7421         
7422         if (cluster_list[seq]==-1)
7423                 {
7424                 cluster_list[seq]=cluster_number;
7425                 n++;
7426                 }
7427         for ( a=0; a<nseq; a++)
7428                 if ( cluster_list[a]==-1)
7429                         {
7430                         
7431                         if (S[seq][a]>T)
7432                                 {
7433                                 n++;
7434                                 cluster_list[a]=cluster_number;
7435                                 n+=search_for_cluster ( a, cluster_number, cluster_list, T, nseq, S);
7436                                 }
7437                         }
7438         return n;
7439         }       
7440                 
7441 int * SHC ( int nseq, int **NST, int **ST)
7442         {
7443         int a;
7444         int mut;
7445         int score, new_score;
7446         int N_IT=VERY_LONG_STRING;
7447         int *sol;
7448         int count;
7449         
7450         sol=vcalloc ( nseq, sizeof (int));
7451         for ( a=0; a<nseq; a++)
7452                 sol[a]=(addrand ((unsigned long)100)>49)?1:-1;
7453                 
7454         score=evaluate_sol (sol, nseq, ST, NST);
7455         fprintf ( stderr, "\nI_Score=%d\n", score);
7456         N_IT=N_IT*nseq;
7457         
7458         for ( count=0,a=0; a< N_IT && score<VERY_LONG_STRING; a++, count++)
7459                 {
7460                 mut=mutate_sol ( sol,nseq);
7461                 new_score=evaluate_sol (sol, nseq, ST, NST);
7462                 if ( new_score>score)
7463                         {
7464                         score=new_score;
7465                         }
7466                 else if ( (addrand ((unsigned long)VERY_LONG_STRING))>score)
7467                         {
7468                         score=new_score;
7469                         }
7470                 else
7471                         sol[mut]=sol[mut]*-1;
7472                 if ( count==VERY_LONG_STRING)
7473                         {
7474                         count=0;
7475                         fprintf ( stderr, "\nScore=%d", score);
7476                         }       
7477                 }
7478         fprintf ( stderr, "\nScore=%d\n", score);
7479         return sol;
7480         }
7481
7482 int mutate_sol (int *sol, int nseq)
7483         {
7484         int n;
7485         n=addrand ((unsigned long)nseq);
7486         sol[n]=sol[n]*-1;
7487         return n;
7488         }
7489 int evaluate_sol ( int *sol, int nseq, int **ST, int **NST)
7490         {
7491         static int max_score;
7492         int a, b, score=0;
7493         
7494         if ( max_score==0)
7495                 {
7496                 for ( a=0; a<nseq-1; a++)
7497                         for ( b=a+1; b<nseq; b++)
7498                                 {
7499                                 max_score+=(ST[a][b]>NST[a][b])?ST[a][b]:NST[a][b];
7500                                 }
7501                 }
7502         
7503         for ( a=0; a<nseq-1; a++)
7504                 for (b=a+1; b<nseq; b++)
7505                         if ( (sol[a]*sol[b])<0)score+=NST[a][b];
7506                         else score+=ST[a][b];
7507         return (score*VERY_LONG_STRING)/max_score;
7508         }               
7509                 
7510      
7511 char * invert_seq ( char *seq)
7512         {
7513         int a, b;
7514         
7515         char *nseq;
7516         int l;
7517         
7518         
7519         l=strlen ( seq);
7520         for ( a=0; a<l; a++)
7521                 seq[a]=tolower ( seq[a]);
7522         nseq=vcalloc ( l+1, sizeof (char));
7523         
7524         for ( a=0, b=l-1; a<l; a++, b--)
7525                 {
7526                 if (seq[b]=='n')nseq[a]='n';
7527                 else if (seq[b]=='g')nseq[a]='c';
7528                 else if (seq[b]=='c')nseq[a]='g';
7529                 else if (seq[b]=='a')nseq[a]='t';
7530                 else if (seq[b]=='t')nseq[a]='a';
7531                 }
7532                 
7533         nseq[l]='\0';
7534         return nseq;
7535         }
7536                 
7537                 
7538 int get_best_match ( char *seq1, char *seq2)
7539         {
7540         static int **m;
7541         static int ml;
7542         int a, b;
7543         int **mdiag;
7544         int n_mdiag=0;
7545         int best;
7546         int l1, l2;
7547         
7548         
7549         l1=strlen ( seq1);
7550         l2=strlen (seq2);
7551         if ( m==NULL)
7552                 {
7553                 ml=(l1>l2)?l1:l2;
7554                 m=declare_int (ml, ml);
7555                 }
7556         else if ( (ml<l1) || (ml<l2))
7557                 {
7558                 free_int (m, ml);
7559                 ml=(l1>l2)?l1:l2;
7560                 m=declare_int (ml, ml);
7561                 }
7562                 
7563         for ( a=0; a<l1; a++)
7564                 {
7565                 for ( b=0; b<l2; b++)
7566                         m[a][b]=((seq1[a]==seq2[b])|| seq1[a]=='n' ||seq2[b]=='n')?1:0;
7567                 }
7568         mdiag= extract_m_diag_streches ( m, l1, l2,seq1, seq2, &n_mdiag); 
7569         
7570         for ( best=0,a=0; a<n_mdiag; a++)
7571                 best=(mdiag[a][0]>best)?mdiag[a][0]:best;
7572         
7573         return best;
7574         }
7575
7576 int** extract_m_diag_streches ( int ** m, int l1, int l2,char *seq1, char *seq2, int *n_mdiag)
7577         {
7578         
7579         int b, x, y, s1, s2;
7580         static int **mdiag;
7581         int in;
7582         static int max_diag=VERY_LONG_STRING;
7583          
7584          /*
7585          diag[0]=len;
7586          diag[1]=x_start;
7587          diag[2]=y_start;
7588          diag[3]=x_end;
7589          diag[4]=y_end;
7590          */
7591          
7592         if ( mdiag==NULL)
7593                 mdiag=declare_int ( max_diag, 5);
7594         
7595         for ( s1=l1-1, s2=0;s2<l2;)
7596                 {
7597                 for ( in=0,x=s1, y=s2; x<l1 && y<l2; x++, y++)
7598                         { 
7599                         if (m[x][y]>0)
7600                                 {
7601                                 if (in==1)
7602                                         mdiag[n_mdiag[0]][0]++;
7603                                 else 
7604                                         {
7605                                         mdiag[n_mdiag[0]][0]=1;
7606                                         mdiag[n_mdiag[0]][1]=x;
7607                                         mdiag[n_mdiag[0]][2]=y;
7608                                         in=1;
7609                                         }
7610                                 }
7611                         else
7612                                 if (in==1)
7613                                         {
7614                                         in=0;
7615                                         mdiag[n_mdiag[0]][3]=x-1;
7616                                         mdiag[n_mdiag[0]][4]=y-1;
7617                                         if ( !is_strech ( "ta", seq1, seq2,mdiag[n_mdiag[0]][0], mdiag[n_mdiag[0]][1],mdiag[n_mdiag[0]][2]))n_mdiag[0]++;
7618                                         }
7619                         if (n_mdiag[0]==(max_diag-1))
7620                                 {mdiag=vrealloc (mdiag, (max_diag+VERY_LONG_STRING)*sizeof (int*));
7621                                 for ( b=max_diag; b<max_diag+VERY_LONG_STRING; b++)mdiag[b]=vcalloc ( 5, sizeof (int));
7622                                 max_diag+=VERY_LONG_STRING;
7623                                 }
7624                         }
7625                 s2+= (s1==0)?1:0;       
7626                 s1-= (s1==0)?0:1;
7627                 if (in==1)
7628                         {
7629                         in=0;
7630                         mdiag[n_mdiag[0]][3]=x-1;
7631                         mdiag[n_mdiag[0]][4]=y-1;
7632                         if ( !is_strech ( "ta", seq1, seq2,mdiag[n_mdiag[0]][0], mdiag[n_mdiag[0]][1],mdiag[n_mdiag[0]][2]))n_mdiag[0]++;
7633                         }
7634                 }
7635         
7636         return mdiag;
7637         }                               
7638 int is_strech ( char *AA, char *seq1, char *seq2, int len, int x, int y)
7639         {
7640         int n, i, j, c,a,nr;
7641         int T=70;
7642         
7643         n=strlen ( AA);
7644         for ( a=0; a<n; a++)
7645                 {
7646                 for (nr=0, i=x, j=y, c=0; c<len; c++, i++, j++)
7647                         if ((seq1[i]==AA[a]) && (seq2[j]==AA[a]))nr++;
7648                 if ( ((nr*100)/len)>T)return 1;
7649                 }
7650         return 0;
7651         }
7652         
7653         
7654 /************************************************************************************/
7655 /*                                                                                  */
7656 /*                                      STRUC                                       */
7657 /*                                                                                  */
7658 /*                                                                                  */
7659 /************************************************************************************/
7660
7661 char * oneletaa2threeletaa(char aa);
7662 float aa2property   (char aa, char *mode);
7663
7664 int output_seq2struc(char *outfile, Alignment *A)
7665 {
7666   FILE *fp1, *fp2;
7667   int a,c, l;
7668   float v, h, x, y, z, dx, dy, dz;
7669   char *s;
7670   char *tmpfile1, *tmpfile2;
7671   char command[1000];
7672
7673   tmpfile1=vtmpnam(NULL);
7674   tmpfile2=vtmpnam(NULL);
7675   
7676   ungap (A->seq_al[0]);
7677   s=A->seq_al[0];l=strlen (s);
7678   fp1=vfopen (tmpfile1, "w");
7679
7680   x=y=z=0;
7681   for ( a=0; a< l; a++)
7682     {
7683       h=aa2property ( s[a], "doolittle"   );
7684       v=aa2property (s[a], "volume");
7685       /*14.398907: peptide bond length*/
7686       dx=(float)sqrt ((double)(14.398907/(((h*h)/(v*v))+1)));
7687       dy=dx*(h/v);
7688       dz=0;
7689
7690         
7691       x+=dx;
7692       y+=dy;
7693       z+=dz;
7694       fprintf (fp1, "ATOM%7d   CA %s A%4d%12.3f%8.3f%8.3f  1.00   5.30\n",a+1, oneletaa2threeletaa(s[a]),a+1, x, y, z);
7695     }   
7696   vfclose (fp1);
7697   sprintf ( command, "extract_from_pdb -infile %s -force > %s", tmpfile1,  tmpfile2);
7698   my_system  (command);
7699   fp1=vfopen (tmpfile2, "r");
7700   fp2=vfopen (outfile, "w");
7701   
7702   while ( (c=fgetc(fp1))!=EOF)fprintf (fp2, "%c", c);
7703   vfclose (fp1);
7704   vfclose (fp2);
7705   
7706   return 0;
7707 }
7708
7709 char * oneletaa2threeletaa(char aa)
7710   {
7711     aa=tolower (aa);
7712     if ( aa=='a')return "ALA";
7713     else if ( aa=='r') return "ARG";
7714     else if ( aa=='n') return "ASN";
7715     else if ( aa=='d') return "ASP";
7716     else if ( aa=='c') return "CYS";
7717     else if ( aa=='q') return "GLN";
7718     else if ( aa=='e') return "GLU";
7719     else if ( aa=='g') return "GLY";
7720     else if ( aa=='h') return "HIS";
7721     else if ( aa=='i') return "ILE";
7722     else if ( aa=='l') return "LEU";
7723     else if ( aa=='k') return "LYS";
7724     else if ( aa=='m') return "MET";
7725     else if ( aa=='f') return "PHE";
7726     else if ( aa=='p') return "PRO";
7727     else if ( aa=='s') return "SER";
7728     else if ( aa=='t') return "THR";
7729     else if ( aa=='w') return "TRP";
7730     else if ( aa=='y') return "TYR";
7731     else if ( aa=='v') return "VAL";
7732     else 
7733       {
7734         fprintf ( stderr, "\nERROR: %c is not an amino acid [FATAL::aa2hydropathy::%s]", aa, PROGRAM);
7735         myexit (EXIT_FAILURE);
7736         return NULL;
7737       }
7738     return NULL;
7739   } 
7740   
7741 float aa2property   (char aa, char *mode)
7742   {
7743     if ( mode==NULL || strm (mode, "doolittle"))
7744          {
7745            aa=tolower (aa);
7746            if ( aa=='i')return 4.5;
7747            else if ( aa=='v') return 4.2;
7748            else if ( aa=='l') return 3.8;
7749            else if ( aa=='f') return 2.8;
7750            else if ( aa=='c') return 2.5;
7751            else if ( aa=='m') return 1.9;
7752            else if ( aa=='a') return 1.8;
7753            else if ( aa=='g') return -0.4;
7754            else if ( aa=='t') return -0.7;
7755            else if ( aa=='w') return -0.9;
7756            else if ( aa=='s') return -0.8;
7757            else if ( aa=='y') return -1.3;
7758            else if ( aa=='p') return -1.6;
7759            else if ( aa=='h') return -3.2;
7760            else if ( aa=='e') return -3.5;
7761            else if ( aa=='q') return -3.5;
7762            else if ( aa=='d') return -3.5;
7763            else if ( aa=='n') return -3.5;
7764            else if ( aa=='k') return -3.9;
7765            else if ( aa=='r') return -4.5;
7766            else 
7767              {
7768                fprintf ( stderr, "\nERROR: %c is not an amino acid [FATAL::aa2hydropathy::%s]", aa, PROGRAM);
7769                myexit (EXIT_FAILURE);
7770              }
7771          }
7772     else if (strm (mode, "volume"))
7773          {
7774            aa=tolower (aa);
7775            if ( aa=='a')return 0.915;
7776            else if ( aa=='r') return 2.02;
7777            else if ( aa=='n') return 1.35;
7778            else if ( aa=='d') return 1.24;
7779            else if ( aa=='c') return 1.18;
7780            else if ( aa=='q') return 1.61;
7781            else if ( aa=='e') return 1.55;
7782            else if ( aa=='g') return 0.66;
7783            else if ( aa=='h') return 1.67;
7784            else if ( aa=='i') return 1.69;
7785            else if ( aa=='l') return 1.68;
7786            else if ( aa=='k') return 1.71;
7787            else if ( aa=='m') return 1.70;
7788            else if ( aa=='f') return 2.03;
7789            else if ( aa=='p') return 1.29;
7790            else if ( aa=='s') return 0.99;
7791            else if ( aa=='t') return 1.22;
7792            else if ( aa=='w') return 2.37;
7793            else if ( aa=='y') return 2.03;
7794            else if ( aa=='v') return 1.41;
7795            else 
7796              {
7797                fprintf ( stderr, "\nERROR: %c is not an amino acid [FATAL::aa2hydropathy::%s]", aa, PROGRAM);
7798                myexit (EXIT_FAILURE);
7799              }
7800          }
7801     
7802     else
7803       {
7804         fprintf ( stderr, "\nERROR: %s is an unknown mode [FATAL::aa2hydropathy::%s]", mode  , PROGRAM);
7805         myexit (EXIT_FAILURE);
7806       }
7807   return 0;
7808   } 
7809            
7810
7811           
7812            
7813         
7814 /************************************************************************************/
7815 /*                                                                                  */
7816 /*                                      DNA                                         */
7817 /*                                                                                  */
7818 /*                                                                                  */
7819 /************************************************************************************/
7820
7821 Alignment *code_dna_aln (Alignment *A)
7822        {
7823          int a, b,l,r;
7824
7825          for ( a=0; a< A->nseq; a++)
7826            {
7827              for (l=0, b=0; b< A->len_aln; b++)
7828                {
7829                  r=A->seq_al[a][b];
7830                  if ( r=='-')l++;
7831                  else if ( r=='~')continue;
7832                  else if ( r=='.')l++;
7833                  else if ( !islower(r))A->seq_al[a][b]='4';
7834                  else
7835                    {
7836                      A->seq_al[a][b]=(l+3)%3+'0';
7837                      l++;
7838                    }
7839                }
7840            }
7841          return A;
7842        }
7843
7844   
7845 Alignment *back_translate_dna_aln (Alignment *A)
7846        {
7847          /*Given a set of aligned sequences
7848            starts from left to right
7849            1 aa->3 nuc
7850            ambiguities are randomly resolved.
7851            returns the corresponding amino acid alignment
7852          */      
7853           int a;
7854           char *seq    ;
7855          
7856          ungap_aln(A);
7857          A=realloc_aln (A, 10000);
7858          seq=vcalloc ( 10000, sizeof (char));
7859
7860          
7861          for ( a=0; a< A->nseq; a++)
7862            {
7863            seq=back_translate_dna_seq (A->seq_al[a], seq, RANDOM);
7864            sprintf ( A->seq_al[a], "%s", seq);
7865            }
7866          A->len_aln=A->len_aln*3;
7867          compress_aln (A);
7868          vfree (seq);
7869          return A;
7870        }
7871 char * back_translate_dna_seq ( char *in_seq,char *out_seq, int mode)     
7872        {
7873          int a,len;
7874
7875          len=strlen(in_seq);
7876          
7877          if (out_seq==NULL)out_seq=vcalloc ( len*3+1, sizeof (char));
7878          
7879          out_seq[0]='\0';
7880          for (a=0; a<len; a++)
7881            {
7882            strcat (out_seq,  back_translate_dna_codon (in_seq[a],mode));
7883            }
7884          
7885          return out_seq;
7886        }
7887
7888 static Sequence *rna_seq2dna_seq (Sequence *S);
7889 static Sequence *dna_seq2rna_seq (Sequence *S);
7890 Sequence * transform_sequence ( Sequence *S, char *mode)
7891 {
7892   if ( strm (mode, "rna2dna"))
7893     return rna_seq2dna_seq (S);
7894   else if ( strm (mode, "dna2rna"))
7895     return dna_seq2rna_seq (S);
7896   else 
7897     printf_exit (EXIT_FAILURE, stderr, "Unknown -transform mode: %s [FATAL:%s]\n", mode,PROGRAM);
7898   return NULL;}
7899
7900 Sequence *rna_seq2dna_seq (Sequence *S)
7901 {
7902   int a, b;
7903   
7904   if ( !strm(S->type, "DNA") && !strm (S->type, "RNA")) printf_exit (EXIT_FAILURE, stderr, "Sequences should be *RNA* type [FATAL:%s]\n", PROGRAM);
7905   for ( a=0; a<S->nseq; a++)
7906     {
7907       for (b=0; b<strlen (S->seq[a]); b++)
7908         {
7909           if ( S->seq[a][b]=='u') S->seq[a][b]='t';
7910           if ( S->seq[a][b]=='U') S->seq[a][b]='T';
7911         }
7912       HERE ("%s", S->seq[a]);
7913     }
7914   return S;
7915 }
7916 Sequence *dna_seq2rna_seq (Sequence *S)
7917 {
7918   int a, b;
7919   
7920   if ( !strm(S->type, "DNA") && !strm (S->type, "RNA")) printf_exit (EXIT_FAILURE, stderr, "Sequences should be *DNA* type (type=%s) [FATAL:%s]\n", PROGRAM, S->type);
7921   for ( a=0; a<S->nseq; a++)
7922     for (b=0; b<S->len[a]; b++)
7923       {
7924         if ( S->seq[a][b]=='t') S->seq[a][b]='u';
7925         if ( S->seq[a][b]=='T') S->seq[a][b]='U';
7926       }
7927   return S;
7928 }
7929
7930   
7931
7932 int get_longest_frame (char *seq, int mode);
7933 Alignment *translate_dna_aln (Alignment *A, int frame)
7934        {
7935          /*Given a set of aligned sequences
7936            starts from left to right
7937            3 nuc->1 aa
7938            2nuc+1gap, 1nuc+2gap->3 gaps
7939            1 stop-> 3gaps
7940            returns the corresponding amino acid alignment
7941          */
7942
7943
7944          int a, b,r;
7945
7946
7947          if (frame==3 || frame ==4)
7948            {
7949              
7950              for (a=0; a< A->nseq; a++)
7951                {
7952                  char *d, *buf, f;
7953                  d=A->seq_al[a];
7954                  f=get_longest_frame (d,frame);
7955                  buf=vcalloc ( strlen (d)+1, sizeof (char));
7956                  if ( f<3)
7957                    {
7958                      sprintf (buf, "%s", d+f);
7959                      sprintf (d, "%s", buf);
7960                      sprintf (A->seq_comment[a], " frame: %d", f);
7961                    }
7962                  else if ( f>=3)
7963                    {
7964                      f-=3;
7965                      sprintf ( buf, "%s", d);
7966                      buf=complement_string (buf);
7967                      sprintf (d, "%s",buf+f);
7968                      sprintf (A->seq_comment[a], " frame: %d Reverse Complement", f);
7969                    }
7970                  vfree (buf);
7971                }
7972            }
7973          else
7974            {
7975              
7976              for ( a=0; a< A->nseq; a++)
7977                for (b=0; b< frame; b++)
7978                  A->seq_al[a][b]='-';
7979              ungap_aln(A);
7980            }
7981
7982          for ( b=0; b< A->nseq; b++)
7983            for ( a=0; a< A->len_aln;)
7984              {
7985                
7986                r=translate_dna_codon (A->seq_al[b]+a, 'z');
7987                if (is_gap(r))
7988                  {
7989                    A->seq_al[b][a++]='-';
7990                    A->seq_al[b][a++]='-';
7991                    A->seq_al[b][a++]='-';
7992                  }
7993                else if ( r=='x')
7994                  {
7995                    A->seq_al[b][a++]='o';
7996                    A->seq_al[b][a++]='-';
7997                    A->seq_al[b][a++]='-';
7998                  }
7999                else if ( r=='z')
8000                  {
8001                    A->seq_al[b][a++]='x';
8002                    A->seq_al[b][a++]='-';
8003                    A->seq_al[b][a++]='-';
8004                  }
8005                else
8006                  {
8007                    A->seq_al[b][a++]=r;
8008                    A->seq_al[b][a++]='-';
8009                    A->seq_al[b][a++]='-';
8010                  }
8011              }   
8012          compress_aln (A);
8013          
8014          return A;
8015        }
8016
8017 int get_longest_frame (char *in_seq, int mode)
8018 {
8019   char *prot, *seq;
8020   int a;
8021   int max_l=0, l;
8022   int best_frame=0;
8023   int nf;
8024   
8025   seq=vcalloc (strlen (in_seq)+1, sizeof (char));
8026   prot=vcalloc (strlen (in_seq)+1, sizeof (char));
8027   sprintf ( seq, "%s", in_seq);
8028   
8029   if ( mode == 3)nf=3;
8030   else if ( mode == 4) nf=6;
8031   
8032   for (a=0; a<nf; a++)
8033     {
8034       int f;
8035       if (a==3)seq=complement_string (seq);
8036       f=(a>=3)?a-3:a;
8037       prot=translate_dna_seq ( seq,f,'\0', prot);
8038       l=strlen (prot);
8039       if (l>=max_l){max_l=l;best_frame=a;}
8040     }
8041   vfree (seq);
8042   vfree (prot);
8043   return best_frame;
8044 }
8045
8046 Alignment *clean_gdna_aln (Alignment *A)
8047        {
8048            int a, b, c, r1, r2,s, p, n, tn;
8049            int *col;
8050            static int **mat;
8051            Alignment *T=NULL;
8052            int **score;
8053            char *buffer;
8054            
8055
8056            /*Viterbi Parameters*/
8057            int AL=0;        /*Allowed Transition*/
8058            int F=-1000000; /*Forbiden Transition*/
8059            int SPLICE_PENALTY=100;
8060            int ORF1=0, ORF2=1, ORF3=2, NC=3;
8061            
8062            int state, pstate, best_e, best_pstate_p,best_state_p, best_pstate_v, best_state_v, v;
8063            int nstate=4;
8064            int **transitions;
8065            int e;
8066            int **v_tab_p;
8067            int **v_tab;
8068            int * is_dna;
8069            
8070            best_state_p=best_state_v=best_pstate_p=best_pstate_v=best_e=0;
8071            buffer=vcalloc ( 100000, sizeof (char));
8072            is_dna=vcalloc ( A->nseq, sizeof (int));
8073            score=declare_int ( A->nseq+1, A->len_aln);
8074
8075
8076            if ( !mat)mat=read_matrice("pam250mt");
8077            T=copy_aln (A, T);
8078            col=vcalloc ( A->nseq, sizeof (int));
8079            
8080            for (a=0; a<= A->len_aln; a++)
8081                for ( b=0; b< A->nseq; b++){A->seq_al[b][a]=tolower(A->seq_al[b][a]); A->seq_al[b][a]=(A->seq_al[b][a]=='t')?'u':A->seq_al[b][a];}
8082
8083            for ( a=0; a< A->nseq; a++)
8084                {
8085                    sprintf ( buffer, "%s", A->seq_al[a]);
8086                    ungap (buffer);
8087                    is_dna[a]=strm ( get_string_type (buffer), "DNA");
8088                }
8089            
8090
8091            for (a=0; a< A->len_aln-2; a++)
8092                {
8093                for (b=0; b< A->nseq; b++)
8094                        {
8095                        if (is_dna[b])col[b]=translate_dna_codon (A->seq_al[b]+a, 'x');
8096                        else col[b]=tolower ( A->seq_al[b][a]);   
8097                        }
8098
8099                for (n=0,tn=0,b=0; b< A->nseq; b++)
8100                    for ( c=b; c< A->nseq; c++   )
8101                        {
8102                            r1=col[b];
8103                            r2=col[c];
8104                            
8105                            if (r1=='x' || r2=='x'){score[A->nseq][a]=F;break;}
8106                            else if (r1=='-' && r2=='-');
8107                            else if (r1=='-' || r2=='-');
8108                            else 
8109                                {
8110                                
8111                                    if ( is_dna[b] && is_dna[c])score[A->nseq][a]+= mat[r1-'A'][r2-'A'];
8112                                    else score[A->nseq][a]+=mat[r1-'A'][r2-'A']* (A->nseq*A->nseq);
8113                                }
8114                            n+=( !is_gap(r1) && !is_gap(r2));
8115                            score[A->nseq][a]=(((tn!=0)?score[A->nseq][a]/tn:0));
8116                        }
8117                
8118                }
8119
8120            /*initialisation*/
8121
8122            transitions=declare_int ( nstate, nstate);
8123            v_tab=declare_int ( A->len_aln+2, nstate       );
8124            v_tab_p=declare_int ( A->len_aln+2, nstate       );
8125
8126            for (a=0; a<nstate;a++)
8127                for (b=0; b<nstate;b++)
8128                      {transitions[a][b]=F;}
8129
8130            transitions[ORF1][ORF2]=AL;
8131            transitions[ORF2][ORF3]=AL;
8132            transitions[ORF3][ORF1]=AL;     
8133            
8134            transitions[ORF3][NC]  =AL-SPLICE_PENALTY;
8135            transitions[NC][ORF1]  =AL-SPLICE_PENALTY;
8136
8137
8138            for ( s=0; s<A->nseq; s++)
8139                {
8140                for ( p=0; p<=A->len_aln; p++){for (state=0; state< nstate; state++)v_tab_p[p][state]=-1; }
8141                for (p=1+2; p<= A->len_aln; p++)
8142                    {
8143
8144                    for (state=0; state< nstate; state++)
8145                        {
8146                            
8147                            if ( state==NC){e=-best_e;}
8148                            else
8149                               {
8150                                   e=score[A->nseq][(p-1)-state];
8151                                   if ( state==0)best_e=e;
8152                                   else best_e=MAX(e, best_e);
8153                               }
8154
8155                            for ( pstate=0; pstate<nstate; pstate++)
8156                                {
8157                                    v=e+transitions[pstate][state]+v_tab[p-1][pstate];
8158                                    if (pstate==0 ||(v>best_pstate_v) )
8159                                       {
8160                                        best_pstate_v=v;
8161                                        best_pstate_p=pstate;
8162                                       }
8163                                }
8164                         
8165                            v_tab[p][state]=best_pstate_v;
8166                            v_tab_p[p][state]=best_pstate_p;
8167                            if (state==0 ||best_pstate_v>best_state_v )
8168                               {
8169                                best_state_p=state; 
8170                                best_state_v=best_pstate_v;
8171                               }
8172                        }
8173
8174                    }
8175
8176                
8177        
8178                for (p=0; p< A->len_aln; p++)T->seq_al[s][p]='.';
8179                for (p=A->len_aln; p>0; p--)
8180                    {
8181                        
8182                        if ( best_state_p==0)T->seq_al[s][p-1]=translate_dna_codon (A->seq_al[s]+(p-1), 'x');
8183                        else if ( best_state_p==1 || best_state_p==2)T->seq_al[s][p-1]='-';
8184                       
8185                       
8186                        
8187                        best_state_p=v_tab_p[p][best_state_p];
8188                        
8189                    }
8190                }
8191            
8192            
8193
8194            vfree (col);
8195            return T;
8196        }
8197
8198 Alignment *clean_cdna_aln (Alignment *A)
8199        {
8200          /*Given an alignmnet of nucleotides
8201            Returns the same alignmnent whith non coding nucleotides replaced with dots
8202            
8203            at each position, the emission probability is the sum of pair of the substitution of amino-acids
8204          */
8205          
8206            int a, b, c,s, p;
8207            static int **mat;
8208            int   *emission;
8209            float em1, em2;
8210            char *buffer;
8211            Alignment *B=NULL;
8212
8213
8214            
8215
8216            /*Viterbi Parameters*/
8217            int AL=0;        /*Allowed Transition*/
8218            int F=-1000000; /*Forbiden Transition*/
8219            int PENALTY=30;
8220            int NC, C1,C2, C3, START, END;
8221            int nstate=0;
8222            int state=0,best_state=0, score=0, best_score=0;
8223            int p_state;
8224            int e=0;
8225            int **score_tab;
8226            int **state_tab;
8227          
8228            int **transitions;
8229            int n;
8230            int r1, r2, r3;
8231
8232            NC=nstate++;
8233            C1=nstate++;
8234            C2=nstate++;
8235            C3=nstate++;
8236            START=nstate++;
8237            END=nstate++;
8238
8239            
8240            B=copy_aln (A, B);
8241            buffer=vcalloc ( 100000, sizeof (char));
8242            emission=vcalloc (A->len_aln, sizeof (int));
8243
8244            if ( !mat)
8245              {
8246                mat=read_matrice("pam250mt");
8247              }
8248
8249            /*Computation of the emission proba for the coding state*/
8250
8251
8252            for (a=0; a< A->len_aln; a++)
8253              {
8254
8255                /*First component: % occupancy of the column*/
8256                em1=0;
8257                for ( b=0; b< A->nseq; b++) em1+=!is_gap(translate_dna_codon (A->seq_al[b]+a, '-'));
8258                em1=em1/(float)A->nseq;
8259                
8260                /*Second Component: % similarity within column*/
8261                em2=0;
8262                for (n=0,b=0; b< A->nseq-1; b++)
8263                  {
8264                    r1=translate_dna_codon (A->seq_al[b]+a, '-');
8265                    
8266                    for (c=b+1; c<A->nseq; c++)
8267                      {
8268                        r2=translate_dna_codon (A->seq_al[c]+a, '-');
8269                        if (is_gap(r2) || is_gap(r1));
8270                        else
8271                          {
8272                            n++;
8273                            em2+=((mat[r1-'A'][r2-'A'])>1)?1:0;
8274                          }
8275                      }
8276                  }
8277                em2=em2/(float)((n==0)?1:n);
8278                
8279                
8280                emission[a]=(em1*100);
8281
8282              }
8283            
8284          
8285
8286            /*initialisation*/
8287
8288            transitions=declare_int ( nstate, nstate);
8289            score_tab=declare_int ( A->len_aln+2, nstate       );
8290            state_tab=declare_int ( A->len_aln+2, nstate       );
8291
8292            for (a=0; a<nstate;a++)
8293                for (b=0; b<nstate;b++)
8294                      {transitions[a][b]=F;}
8295
8296            
8297            transitions[START][C1]=AL;
8298            transitions[START][NC]=AL;
8299            transitions[C3][END]=AL;
8300            transitions[NC][END]=AL;
8301            transitions[C1 ][C2 ]=AL;
8302            transitions[C2 ][C3 ]=AL;
8303            transitions[C3 ][C1 ]=AL;
8304            transitions[C3 ][NC ]=AL-PENALTY;
8305            transitions[NC ][C1 ]=AL-PENALTY;
8306            transitions[NC][NC]=AL-PENALTY;
8307            
8308            
8309                    
8310            for ( s=0; s< A->nseq; s++)
8311              {
8312              for ( p=0; p<=A->len_aln; p++){for (state=0; state< nstate; state++){score_tab[p][state]=F;state_tab[p][state]=-1;} }
8313              score_tab[0][START]=0;
8314              
8315              for (p=1; p<= A->len_aln; p++)
8316                {
8317                  for (state=0; state< nstate; state++)
8318                    {
8319                      if ( state==START || state==END)continue;
8320                      else if      ( state==NC)  e=-10;
8321                      else if ( state==C1)
8322                        {
8323                          e=emission[p-1];
8324                        }
8325                      else if ( state ==C2)
8326                        {
8327                          if ( p-2<0)e=F;
8328                          else e=emission[p-2];
8329                        }
8330                      else if ( state==C3)
8331                        {
8332                          if ( p-3<0)e=F;
8333                          else e=emission[p-3];
8334                        }
8335                      
8336                      for (p_state=0; p_state<nstate; p_state++)
8337                        {
8338                          
8339                          if (e==F)score=F;
8340                          else 
8341                            {
8342                              score=(score_tab[p-1][p_state]==F)?F:(e+transitions[p_state][state]+score_tab[p-1][p_state]);
8343                            }
8344                          
8345                          if(p_state==0 || score>best_score){ best_score=score;best_state=p_state;}
8346                          
8347                        }
8348                      
8349                      score_tab[p][state]=best_score;
8350                      state_tab[p][state]=best_state;
8351                      
8352                    }
8353                }
8354              
8355              best_score=best_state=UNDEFINED;
8356              for (state=0; state<nstate; state++)
8357                {
8358                  if (state==START || state==END)continue;
8359                  e=transitions[state][END];
8360                  if (e==F || score_tab[p-1][state]==F)continue;
8361                  
8362                  if (best_score==UNDEFINED || score_tab[p-1][state]>best_score)
8363                    {
8364                      best_score=score_tab[p-1][state]+e; 
8365                      best_state=state;
8366                    }
8367                  
8368                }
8369              
8370              for (p=A->len_aln; p>0;)
8371                {
8372                  B->seq_al[s][p-1]=best_state+'0';
8373                  best_state=state_tab[p][best_state];
8374                  p--;
8375                }
8376              }
8377
8378            for ( a=0; a< A->nseq; a++)
8379              for ( b=0; b< A->len_aln;)
8380                {
8381                  s=B->seq_al[a][b];
8382                  if ( s==C1+'0')
8383                    {
8384                      r1=A->seq_al[a][b];
8385                      r2=A->seq_al[a][b+1];
8386                      r3=A->seq_al[a][b+2];
8387                      
8388
8389                      if ( is_gap(r1) ||is_gap(r2) ||  is_gap(r3))
8390                        {
8391                          A->seq_al[a][b]=(is_gap(r1))?'~':'.';
8392                          A->seq_al[a][b+1]=(is_gap(r2))?'~':'.';
8393                          A->seq_al[a][b+2]=(is_gap(r3))?'~':'.';
8394                        }
8395                      b+=3;
8396                    }
8397                  else if ( s==NC+'0')
8398                    {
8399                      A->seq_al[a][b]=(is_gap(A->seq_al[a][b]))?'~':'.';
8400                      b++;
8401                    }
8402                  else 
8403                    {
8404                      fprintf (stderr, "\nPROBLEM: [%d %d]->%d", a, b, s-'0');
8405                    }
8406                }
8407                  
8408
8409            free_aln (B);
8410            free_int (transitions, -1);
8411            free_int (score_tab, -1);
8412            free_int (state_tab, -1);
8413            vfree (emission);
8414            vfree (buffer);
8415            
8416            return A;
8417        }
8418
8419
8420
8421
8422 Alignment *translate_splice_dna_aln (Alignment *A, Alignment *ST)
8423        {
8424            int a, b, c, r1, r2,s, p, n, tn;
8425            int *col;
8426            static int **mat;
8427            Alignment *T=NULL;
8428            int **score;
8429            
8430            /*Viterbi Parameters*/
8431            int AL=0;        /*Allowed Transition*/
8432            int F=-1000000; /*Forbiden Transition*/
8433            int ORF1=0, ORF2=1, ORF3=2,SPL1=3, SPL2=4, SPL3=5, SPL4=6, NC=7;
8434            int SPLICE_PENALTY;
8435            int frame1, frame2, frame3, best_frame;
8436            int nstate=8;
8437            char r;
8438            
8439
8440
8441            int state=0, pstate=0, best_pstate_p=0,best_state_p=0, best_pstate_v=0, best_state_v=0, v=0;
8442            
8443            int **transitions;
8444            int e=0;
8445            int **v_tab_p;
8446            int **v_tab;
8447
8448            score=declare_int ( A->nseq+1, A->len_aln);
8449
8450
8451            if ( !mat)mat=read_matrice("pam250mt");
8452            T=copy_aln (A, T);
8453            col=vcalloc ( A->nseq, sizeof (int));
8454            
8455            for (a=0; a<= A->len_aln; a++)
8456                for ( b=0; b< A->nseq; b++){A->seq_al[b][a]=tolower(A->seq_al[b][a]); A->seq_al[b][a]=(A->seq_al[b][a]=='t')?'u':A->seq_al[b][a];}
8457
8458            
8459            
8460
8461            for (a=0; a< A->len_aln-2; a++)
8462                {
8463                for (b=0; b< A->nseq; b++)
8464                        {
8465                        col[b]=translate_dna_codon (A->seq_al[b]+a, 'x');
8466                        }
8467                
8468                for (n=0,tn=0,b=0; b< A->nseq-1; b++)
8469                    for ( c=b+1; c< A->nseq; c++, tn++   )
8470                        {
8471                            r1=col[b];
8472                            r2=col[c];
8473                            
8474                            if (r1=='x' || r2=='x')score[A->nseq][a]=F;
8475                            else if (r1=='-' && r2=='-');
8476                            else if (r1=='-' || r2=='-');
8477                            else 
8478                                {
8479                                    score[A->nseq][a]+= mat[r1-'A'][r2-'A'];
8480                                    
8481                                }
8482                            n+=( !is_gap(r1) && !is_gap(r2));
8483                        }   
8484                score[A->nseq][a]=(((tn!=0)?score[A->nseq][a]/tn:0));
8485                  
8486                }
8487                
8488            /*initialisation*/
8489
8490            transitions=declare_int ( nstate, nstate);
8491            v_tab=declare_int ( A->len_aln+2, nstate*nstate);
8492            v_tab_p=declare_int ( A->len_aln+2, nstate*nstate);
8493
8494            for (a=0; a<nstate;a++)
8495              for (b=0; b<nstate;b++)
8496                {transitions[a][b]=F;}
8497
8498            SPLICE_PENALTY=-1000;
8499
8500            transitions[ORF1][ORF2]    =AL;
8501            transitions[ORF1][SPL1]    =AL-SPLICE_PENALTY;
8502            
8503            transitions[ORF2][ORF3]    =AL;
8504            transitions[ORF2][SPL1]    =AL-SPLICE_PENALTY;
8505            
8506            transitions[ORF3][ORF1]    =AL;
8507            transitions[ORF3][SPL1]    =AL-SPLICE_PENALTY;
8508            
8509            transitions[ORF3][ORF1]    =AL;
8510            transitions[ORF3][SPL1]    =AL-SPLICE_PENALTY;
8511            
8512            transitions[ORF3][NC]=AL-100;
8513            transitions[NC][ORF1]=AL-100;
8514
8515
8516            transitions[SPL1][SPL2]=AL;
8517            transitions[SPL2][NC  ]=AL-SPLICE_PENALTY;
8518            transitions[NC  ][NC  ]=AL;
8519            transitions[NC  ][SPL3]=AL-SPLICE_PENALTY;
8520            transitions[SPL3][SPL4]=AL;
8521            transitions[SPL4][ORF1]=AL;
8522            transitions[SPL4][ORF2]=AL;
8523            transitions[SPL4][ORF3]=AL;
8524            
8525
8526            for ( s=0; s<A->nseq; s++)
8527                {
8528                for ( p=0; p<=A->len_aln; p++){for (state=0; state< nstate; state++)v_tab_p[p][state]=-1; }
8529                for (p=1+2; p<= A->len_aln; p++)
8530                    {
8531                     frame1=score[A->nseq][(p-1)];
8532                     frame2=score[A->nseq][(p-1)-1];
8533                     frame3=score[A->nseq][(p-1)-2];  
8534                     best_frame=best_int (3, 1, &a, frame1, frame2, frame3);
8535                     for (state=0; state< nstate; state++)
8536                        {
8537                          r=tolower (A->seq_al[s][p-1]);
8538                          r=(r=='u')?'t':r;
8539                          
8540                          if      (state==ORF1)e=frame1;
8541                          else if (state==ORF2)e=frame2;
8542                          else if (state==ORF3)e=frame3;
8543                          else if (state==SPL1)e=(r=='g')?best_frame:F;
8544                          else if (state==SPL2)e=(r=='t')?best_frame:F;
8545                          else if (state==SPL3)e=(r=='a')?best_frame:F;
8546                          else if (state==SPL4)e=(r=='g')?best_frame:F;
8547                          else if (state==NC)e=-best_frame;
8548                          for ( pstate=0; pstate<nstate; pstate++)
8549                                {
8550                                    v=e+transitions[pstate][state]+v_tab[p-1][pstate];
8551                                    if (pstate==0 ||(v>best_pstate_v) ){best_pstate_v=v;best_pstate_p=pstate;}
8552                                }
8553                         
8554                            v_tab[p][state]=best_pstate_v;
8555                            v_tab_p[p][state]=best_pstate_p;
8556                            if (state==0 ||best_pstate_v>best_state_v ){best_state_p=state; best_state_v=best_pstate_v;}
8557                        }
8558                    }
8559
8560                
8561        
8562                for (p=0; p< A->len_aln; p++)T->seq_al[s][p]='.';
8563                for (p=A->len_aln; p>0; p--)
8564                    {
8565                        if ( best_state_p==0)T->seq_al[s][p-1]=toupper(translate_dna_codon (A->seq_al[s]+(p-1), 'x'));
8566                        else if ( best_state_p>=SPL1  && best_state_p<=SPL4)T->seq_al[s][p-1]='-';
8567                        best_state_p=v_tab_p[p][best_state_p];
8568                    }
8569                }
8570            
8571            
8572
8573            vfree (col);
8574            return T;
8575        }
8576
8577 Alignment * mutate_cdna_aln ( Alignment *A)
8578 {
8579     int a, b, c, n;
8580     int n1, n2, r1, r2;
8581     int **pos, ps;
8582     int neutral_substitution=50;
8583     int random_substitution=0;
8584     int random_deletion=0;
8585     int amino_acid_deletion=0;
8586     int amino_acid_substitution=0;
8587     char nuc_list[]="agct";
8588     char *new_codon;
8589
8590     neutral_substitution=atoi(get_env_variable ("NEUTRAL_SUBSTITUTION",IS_FATAL));
8591     random_substitution =atoi(get_env_variable ("RANDOM_SUBSTITUTION", IS_FATAL));
8592     random_deletion     =atoi(get_env_variable ("RANDOM_DELETION", IS_FATAL));
8593     amino_acid_deletion =atoi(get_env_variable ("AMINO_ACID_DELETION", IS_FATAL));
8594     amino_acid_substitution =atoi(get_env_variable ("AMINO_ACID_SUBSTITUTION", IS_FATAL));
8595     
8596     
8597     if (A->S)free_sequence ( A->S, (A->S)->nseq);
8598     A->S=aln2seq(A);
8599
8600     addrandinit(time (NULL));
8601
8602     
8603     pos=aln2pos_simple ( A, A->nseq);
8604     
8605     /* 1 Apply neutral substitutions    */
8606     
8607     if ( neutral_substitution)
8608         {
8609         for (  c=0; c< neutral_substitution; c++)
8610             {
8611             for (  a=0; a< A->nseq; a++)
8612                 {
8613                     
8614                     for ( b=0; b< A->len_aln; b++)
8615                         {
8616                         
8617                         if (pos[a][b]<=0)continue; 
8618                         ps=MAX(0,pos[a][b]-(pos[a][b]-1)%3-1);
8619
8620
8621                         n1=(A->S)->seq[a][pos[a][b]-1];
8622                         r1=translate_dna_codon ( (A->S)->seq[a]+ps, 'o');
8623                         
8624                         n2=nuc_list[(int)addrand((unsigned long) 4)];
8625                         (A->S)->seq[a][pos[a][b]-1]=n2;
8626                         r2=translate_dna_codon ( (A->S)->seq[a]+ps, 'o');
8627                         
8628                         
8629                         if ( r1==r2 && r1!='o')A->seq_al[a][b]=n2;
8630                         
8631                         else (A->S)->seq[a][pos[a][b]-1]=n1;
8632                         }
8633                 }
8634             }
8635         }
8636
8637     /* 2 Apply         substitutions    */
8638      if ( random_substitution)
8639         {
8640         for (  a=0; a< A->nseq; a++)
8641             {
8642                 for ( b=0; b< A->len_aln; b++)
8643                     {
8644                     if (pos[a][b]<=0)continue; 
8645                     if (addrand ((unsigned long) 100)>random_substitution)continue; 
8646                     
8647                     n1=nuc_list[(int)addrand((unsigned long)4)];
8648                     (A->S)->seq[a][pos[a][b]-1]=n1;
8649                     A->seq_al[a][b]=n1;
8650                     }
8651             }
8652         }
8653     
8654     /* 3 Apply amino acid substitutions */
8655       if ( amino_acid_substitution)
8656         {
8657         for (  a=0; a< A->nseq; a++)
8658             {
8659                 for ( b=0; b< A->len_aln; b+=3)
8660                     {
8661                     if (pos[a][b]<=0)continue; 
8662                     if (addrand ((unsigned long) 100)>amino_acid_substitution)continue; 
8663                     ps=MAX(0,pos[a][b]-(pos[a][b]-1)%3-1);
8664                     
8665                     r1=translate_dna_codon ( (A->S)->seq[a]+ps, 'o');
8666                     new_codon=mutate_amino_acid(r1, "clustalw_col");
8667                     
8668                     for ( c=ps; c<ps+3; c++)(A->S)->seq[a][c]=new_codon[c-ps];
8669                     }
8670                 for ( b=0; b< A->len_aln; b++)
8671                     {
8672                     if (pos[a][b]<=0)continue; 
8673                     else A->seq_al[a][b]=(A->S)->seq[a][pos[a][b]-1];
8674                     }
8675             }
8676         }  
8677     /* 3 Apply amino acid deletions     */
8678      if ( amino_acid_deletion)
8679         {
8680         for (  a=0; a< A->nseq; a++)
8681             {
8682                 for ( b=0; b< A->len_aln; b+=3)
8683                     {
8684                     if (pos[a][b]<=0)continue; 
8685                     if (addrand ((unsigned long) 1000)>amino_acid_deletion)continue; 
8686                     ps=MAX(0,pos[a][b]-(pos[a][b]-1)%3-1);
8687                     n=addrand ((unsigned long) 4)+1;
8688                     
8689                     for ( c=ps; c<ps+(3*n) && c<A->len_aln; c++)(A->S)->seq[a][c]='-';
8690                     }
8691                 for ( b=0; b< A->len_aln; b++)
8692                     {
8693                     if (pos[a][b]<=0)continue; 
8694                     else A->seq_al[a][b]=(A->S)->seq[a][pos[a][b]-1];
8695                     }
8696             }
8697         }
8698     /* 4 Apply amino acid insertions    */
8699
8700 /*FRAMESHIFT MUTATIONS*/
8701     /* 5 Apply nucleotide deletions*/
8702      if ( random_deletion)
8703         {
8704         for (  a=0; a< A->nseq; a++)
8705             {
8706                 for ( b=0; b< A->len_aln; b++)
8707                     {
8708                     if (pos[a][b]<=0)continue; 
8709                     if (addrand ((unsigned long) 1000)>random_deletion)continue; 
8710                     
8711                     n1='-';
8712                     (A->S)->seq[a][pos[a][b]-1]=n1;
8713                     A->seq_al[a][b]=n1;
8714                     }
8715             }
8716         }
8717     /* 6 Apply nucleotide deletions*/
8718      free_int (pos, -1);
8719    return A;
8720
8721 }    
8722     
8723 Alignment* clean_est  ( Alignment *A)
8724         {
8725           /*Rules are as follow:
8726             Internal Gap > 30% Requences ----> -
8727             Best Residue < 50% Residues  ----> 'N'
8728           */
8729           int a, b,c;
8730           int best;
8731           int tot;
8732
8733           for ( a=0; a< A->len_aln; a++)
8734             {
8735               
8736               for (tot=0, b=0; b<4; b++)tot+=(A->P)->count[b][a];
8737               best=best_int (5,1, &c, (A->P)->count[0][a],(A->P)->count[1][a],(A->P)->count[2][a],(A->P)->count[3][a],(A->P)->count[4][a]);
8738               
8739               if ( tot==0)
8740                 {
8741                   fprintf ( stderr, "\nWARNING: POSITION WITH NO INFORMATION [clean_est:%s]", PROGRAM);
8742                   A->seq_al[0][a]='-';
8743                 }
8744               else if (((A->P)->count[4][a]*100)/tot >30)A->seq_al[0][a]='-';
8745               else if ( (best*100)/tot<50)A->seq_al[0][a]='n';
8746               
8747             }
8748         return A;
8749         }
8750            
8751     
8752
8753 char **make_symbols ( char *name, int *n)
8754     {
8755     char **symbol;
8756
8757     symbol=declare_char ( STRING, STRING);
8758     
8759     if ( strcmp (name, "3d_ali")==0)
8760         {
8761         sprintf ( symbol[0], "gih");
8762         sprintf ( symbol[1], "eb");
8763         sprintf ( symbol[2], "x");
8764         sprintf ( symbol[3], "#l");
8765         n[0]=4;
8766         }
8767     
8768     else if ( strcmp (name, "all")==0)
8769         {
8770           int a, i;
8771           for ( i=0,a=0; a<26; a++)
8772             {
8773               sprintf ( symbol[i++], "%c%c", 'a'+a, 'a'+a);
8774               sprintf ( symbol[i++], "%c%c", 'A'+a, 'A'+a);
8775             }
8776           sprintf ( symbol[i++], "--");
8777           n[0]=i;
8778         }
8779     
8780     else if ( strcmp (name, "set1")==0)
8781         {
8782         sprintf ( symbol[0], "ilvmfywhktcagH");
8783         sprintf ( symbol[1], "reqdnsP");
8784         sprintf ( symbol[2], "--");
8785         sprintf ( symbol[3], "#l");
8786         n[0]=4;
8787         }
8788     else if ( strcmp (name, "set2")==0)
8789         {
8790         n[0]=0;
8791         sprintf ( symbol[n[0]++], "gsacT");
8792         sprintf ( symbol[n[0]++], "ndtvpS");
8793         sprintf ( symbol[n[0]++], "ilkreqL");
8794         sprintf ( symbol[n[0]++], "--");
8795         sprintf ( symbol[n[0]++],"#l"); 
8796         }
8797     else if ( strcmp ( name, "any")==0)
8798         {
8799         sprintf ( symbol[0], "*x");
8800         n[0]=1;
8801         }
8802
8803
8804
8805
8806     return symbol;
8807     }
8808
8809 char * translate_dna_seq_on3frame (  char *dna_seq, char stop, char *prot)
8810        {
8811           int a, l;
8812           char *buf;
8813
8814           l=strlen (dna_seq);
8815           if ( prot==NULL)prot=vcalloc ( l+2, sizeof (char));
8816            
8817            buf=vcalloc (l+4, sizeof (char));
8818            sprintf (buf, "%s", dna_seq);
8819            lower_string ( buf);
8820            for ( a=0; a< l; a++)buf[a]=(buf[a]=='t')?'u':buf[a];
8821            
8822            for (a=0; a< l; a++)
8823                prot[a]=translate_dna_codon (buf+a, stop);
8824            vfree (buf);
8825            prot[a]='\0';
8826
8827            return prot;
8828        }
8829 char * translate_dna_seq ( char *dna_seq, int frame, char stop, char *prot)
8830        {
8831            int a, b, l;
8832            char *buf;
8833
8834            l=strlen (dna_seq);
8835            if ( prot==NULL)prot=vcalloc ( l/3 +2, sizeof (char));
8836            
8837            buf=vcalloc (l+4, sizeof (char));
8838            sprintf (buf, "%s", dna_seq);
8839            lower_string ( buf);
8840            for ( a=0; a< l; a++)buf[a]=(buf[a]=='t')?'u':buf[a];
8841            
8842            for ( b=0,a=0+frame; a< l; a+=3,b++)
8843                prot[b]=translate_dna_codon (buf+a, stop);
8844            vfree (buf);
8845            prot[b]='\0';
8846
8847            return prot;
8848        }
8849 char * back_translate_dna_codon ( char aa, int deterministic)
8850         {
8851         static char *r;
8852         int choice;
8853         
8854         vsrand(0);
8855         if ( r==NULL)r=vcalloc (4, sizeof (char));
8856         
8857         if (!is_gap(aa))aa=tolower(aa);
8858         
8859         if (is_gap(aa))sprintf (r, "---");
8860         else if ( aa>=0 && aa<=9)
8861           {
8862             sprintf (r, "%d%d%d", aa, aa,aa);
8863           }
8864         else if ( aa>='0' && aa<='9')
8865           {
8866              sprintf (r, "%c%c%c", aa, aa,aa);
8867           }
8868         else if ( aa=='a')
8869           {
8870             choice=(deterministic)?0:rand()%4;
8871             if      ( choice==0)sprintf (r, "gca");
8872             else if ( choice==1)sprintf (r, "gcg");
8873             else if ( choice==2)sprintf (r, "gcc");
8874             else if ( choice==3)sprintf (r, "gct");
8875           }
8876         else if ( aa=='c')
8877           {
8878            choice=(deterministic)?0:rand()%2;
8879             if      ( choice==0)sprintf (r, "tgc");
8880             else if ( choice==1)sprintf (r, "tgt");
8881           } 
8882         else if ( aa=='d')
8883           {
8884           choice=(deterministic)?0:rand()%2;
8885           if ( choice==0)sprintf (r, "gac");
8886           else if ( choice==1)sprintf (r, "gat");
8887           }
8888         
8889         else if ( aa=='e')
8890           {
8891             choice=(deterministic)?0:rand()%2;
8892             if ( choice==0)sprintf (r, "gaa");
8893             else sprintf (r, "gag");
8894           }
8895         else if ( aa=='f')
8896           {
8897             choice=(deterministic)?0:rand()%2;
8898             if ( choice==0)sprintf (r, "ttc");
8899             else sprintf (r, "ttt");
8900           }
8901         else if ( aa=='g')
8902           {
8903             choice=(deterministic)?0:rand()%4;
8904             if  ( choice==0)     sprintf (r, "gga");
8905             else if ( choice==1) sprintf (r, "ggg");
8906             else if ( choice==2) sprintf (r, "ggc");
8907             else if ( choice==3) sprintf (r, "ggt");
8908           }     
8909         else if ( aa=='h')
8910           {
8911             choice =rand()%2;
8912             if ( choice==0)sprintf (r, "cac");
8913             else sprintf (r, "cat");
8914           }
8915         else if ( aa=='i')
8916           {
8917             choice=(deterministic)?0:rand()%3;
8918             if  ( choice==0)     sprintf (r, "ata");
8919             else if ( choice==1) sprintf (r, "atc");
8920             else if ( choice==2) sprintf (r, "att");
8921           }     
8922         else if ( aa=='k')
8923           {
8924             choice=(deterministic)?0:rand()%2;
8925             if  ( choice==0)     sprintf (r, "aaa");
8926             else if ( choice==1) sprintf (r, "aag");
8927             
8928           }
8929         else if ( aa=='l')
8930           {
8931             choice=(deterministic)?0:rand()%6;
8932             if  ( choice==0)     sprintf (r, "cta");
8933             else if ( choice==1) sprintf (r, "ctg");
8934             else if ( choice==2) sprintf (r, "ctc");
8935             else if ( choice==3) sprintf (r, "ctt");
8936             else if ( choice==4) sprintf (r, "tta");
8937             else if ( choice==5) sprintf (r, "ttg");        
8938           }     
8939         else if ( aa=='m')sprintf ( r, "atg");
8940         else if ( aa=='n')
8941           {
8942             choice=(deterministic)?0:rand()%2;
8943             if  ( choice==0)     sprintf (r, "aac");
8944             else if ( choice==1) sprintf (r, "aat");
8945           }     
8946         else if ( aa=='p')
8947           {
8948             choice=(deterministic)?0:rand()%4;
8949             if  ( choice==0)     sprintf (r, "cca");
8950             else if ( choice==1) sprintf (r, "ccg");
8951             else if ( choice==2) sprintf (r, "ccc");
8952             else if ( choice==3) sprintf (r, "cct");
8953           }     
8954         else if ( aa=='q')
8955           {
8956             choice=(deterministic)?0:rand()%2;
8957             if  ( choice==0)     sprintf (r, "caa");
8958             else if ( choice==1) sprintf (r, "cag");
8959           }
8960         else if ( aa=='r')
8961           {
8962             choice=(deterministic)?0:rand()%6;
8963             if  ( choice==0)     sprintf (r, "cga");
8964             else if ( choice==1) sprintf (r, "cgg");
8965             else if ( choice==2) sprintf (r, "cgc");
8966             else if ( choice==3) sprintf (r, "cgt");
8967             else if ( choice==4) sprintf (r, "aga");
8968             else if ( choice==5) sprintf (r, "agg");
8969             
8970           }
8971         else if ( aa=='s')
8972           {
8973             choice=(deterministic)?0:rand()%6;
8974             if  ( choice==0)     sprintf (r, "tca");
8975             else if ( choice==1) sprintf (r, "tcg");
8976             else if ( choice==2) sprintf (r, "tcc");
8977             else if ( choice==3) sprintf (r, "tct");
8978             else if ( choice==4) sprintf (r, "agt");
8979             else if ( choice==5) sprintf (r, "agc");
8980             
8981           }
8982         else if ( aa=='t')
8983           {
8984             choice=(deterministic)?0:rand()%4;
8985             if  ( choice==0)     sprintf (r, "aca");
8986             else if ( choice==1) sprintf (r, "acg");
8987             else if ( choice==2) sprintf (r, "acc");
8988             else if ( choice==3) sprintf (r, "act");
8989           }
8990         else if ( aa=='v')
8991           {
8992             choice=(deterministic)?0:rand()%4;
8993             if  ( choice==0)     sprintf (r, "gta");
8994             else if ( choice==1) sprintf (r, "gtg");
8995             else if ( choice==2) sprintf (r, "gtc");
8996             else if ( choice==3) sprintf (r, "gtt");
8997           }
8998         else if ( aa=='w')
8999           {
9000             sprintf (r, "tgg");
9001           }
9002         else if ( aa=='y')
9003           {
9004              choice=(deterministic)?0:rand()%2;
9005             if  ( choice==0)     sprintf (r, "tac");
9006             else if ( choice==1) sprintf (r, "tat");
9007           }
9008         else
9009           {
9010             sprintf (r, "nnn");
9011           }
9012         return r;
9013                 
9014         }
9015 int translate_dna_codon ( char *sequence, char stop)
9016         {
9017         char seq[4];
9018         int a,b;
9019
9020
9021         if ( (b=strlen (sequence))<3)
9022           {
9023             for ( a=0; a<b; a++)
9024               if ( !is_gap(sequence[a]))return 'x';
9025           return '-';
9026           }
9027         else 
9028           {
9029             seq[0]=tolower(sequence[0]);
9030             seq[1]=tolower(sequence[1]); 
9031             seq[2]=tolower(sequence[2]);
9032             seq[3]='\0';
9033             
9034             seq[0]=(seq[0]=='u')?'t':seq[0];
9035             seq[1]=(seq[1]=='u')?'t':seq[1];
9036             seq[2]=(seq[2]=='u')?'t':seq[2];
9037            
9038         }
9039         
9040
9041         
9042         if ( is_gap(seq[0])||is_gap(seq[1]) || is_gap(seq[2]))return '-';
9043         else if ( strm5(seq, "gca", "gcg", "gcc", "gct","gcn"))return 'a';
9044         else if ( strm2(seq, "tgc","tgt"))return 'c';
9045         else if ( strm2(seq, "gac","gat"))return 'd';
9046         else if ( strm2(seq, "gaa","gag"))return 'e';
9047         else if ( strm2(seq, "ttc","ttt"))return 'f';
9048         else if ( strm5(seq, "gga","ggg","ggc", "ggt", "ggn"))return 'g';
9049         else if ( strm2(seq, "cac","cat"))return 'h';
9050         else if ( strm3(seq, "ata","atc","att"))return 'i';
9051         else if ( strm2(seq, "aaa","aag"))return 'k';
9052         else if ( strm6(seq, "cta","ctg","ctc", "ctt", "tta", "ttg"))return 'l';
9053         else if ( strm (seq, "ctn"))return 'l';
9054         else if ( strm (seq, "atg"))return 'm';
9055         else if ( strm2(seq, "aac","aat"))return 'n';
9056         else if ( strm5(seq, "cca","ccg","ccc", "cct","ccn"))return 'p';
9057         else if ( strm2(seq, "cag","caa"))return 'q';
9058         else if ( strm6(seq, "cga","cgg","cgc", "cgt","aga","agg"))return 'r';
9059         else if ( strm (seq, "cgn"))return 'r';
9060         else if ( strm6(seq, "tca","tcg","tcc", "tct","agc","agt"))return 's';
9061         else if ( strm (seq, "ccn"))return 's';
9062         else if ( strm5(seq, "aca","acg","acc", "act", "acn"))return 't';
9063         else if ( strm5(seq, "gta","gtg","gtc", "gtt", "gtn"))return 'v';
9064         else if ( strm (seq, "tgg"))return 'w';
9065         else if ( strm2(seq, "tac","tat"))return 'y';
9066         else if ( strm3(seq, "tag","taa","tga"))return stop;
9067         else if ( seq[0]=='n' || seq[1]=='n' || seq[2]=='n') return stop;
9068         else
9069           {
9070             fprintf ( stderr, "\n%s is an unknown codon [FATAL:%s]",seq, PROGRAM);
9071             myexit (EXIT_FAILURE);
9072             return 1;
9073           }
9074         }
9075            
9076 Alignment * mutate_aln ( Alignment *A, char *r)
9077 {
9078   int a, b, c, mut,type, ratio;
9079   char alp[30];
9080   int alp_size;
9081   Sequence *S;
9082   Alignment*B;
9083   int n_mut, tot;
9084
9085   vsrand(0);
9086   if ( r[0]=='\0')ratio=0.01*RAND_MAX;
9087   else ratio=atof(r)*RAND_MAX;
9088
9089   S=aln2seq(A);
9090   S=get_sequence_type(S);
9091   
9092
9093
9094   if ( strm(S->type, "DNA") ||  strm(S->type, "RNA"))sprintf (alp, "AGCT");
9095   else if (  strm(S->type, "PROTEIN"))sprintf (alp, "ACDEFGHIKLMNPQRSTVWY");
9096
9097   alp_size=strlen(alp);
9098
9099   B=copy_aln (A,NULL);
9100   B=realloc_aln(B, B->len_aln*2+1);
9101
9102   for ( a=0, b=0; a< A->len_aln; a++, b+=2)
9103     {
9104       for ( c=0; c< A->nseq; c++)
9105         {
9106           B->seq_al[c][b]=tolower(A->seq_al[c][a]);
9107           B->seq_al[c][b+1]='~';
9108         }      
9109     }
9110
9111   for ( c=0; c< A->nseq; c++)B->seq_al[c][b]='\0';
9112   B->len_aln=A->len_aln*2;
9113   
9114
9115  
9116   tot=n_mut=0;
9117   for (a=0; a< B->len_aln; a+=2)
9118     for ( b=0; b<B->nseq; b++)
9119       {
9120         if ( is_gap(B->seq_al[b][a]))continue;
9121         mut=((rand()%RAND_MAX)>ratio)?0:1;
9122         tot++;
9123         n_mut+=mut;
9124
9125         if (mut)
9126           {
9127             type=rand()%2;
9128             if (type==0)/*deletion*/
9129               {
9130                 B->seq_al[b][a]='.';
9131               }
9132             else if ( type==1)
9133               {
9134                 B->seq_al[b][a+1]=alp[rand()%alp_size];
9135               }
9136             else if (type==2)
9137               {
9138                 B->seq_al[b][a]=alp[rand()%alp_size];
9139               }
9140             
9141           }
9142       }
9143   ungap_aln (B);
9144   
9145   
9146   free_sequence (S, S->nseq);
9147   free_aln (A);
9148   return B;
9149   
9150 }
9151
9152 char* mutate_amino_acid ( char aa, char *mode)
9153
9154      {
9155          int a, b, c, d;
9156          char nucleotide[]="agct";
9157          char amino_acid[]="acdefghiklmnpqrstvwy";
9158          static char **triplet;
9159          static char **cw_col;
9160          int ng_cw_col;
9161          static int **amino_acid_list;
9162          static int *lu;
9163          char a1, a2;
9164          char *mat;
9165          
9166          aa=tolower(aa);
9167          declare_name(mat);
9168          if ( !mode)sprintf (mat, "clustalw_col");
9169          else sprintf (mat, "%s", mode);
9170          if (!triplet)
9171             {
9172                 triplet=declare_char ( 64, 4);
9173                 for (d=0, a=0; a< 4;a++)
9174                     for ( b=0; b< 4; b++)
9175                         for ( c=0; c< 4; c++, d++)
9176                             {
9177                                 triplet[d][0]=nucleotide[a];
9178                                 triplet[d][1]=nucleotide[b];
9179                                 triplet[d][2]=nucleotide[c];
9180                             }
9181             }
9182          if ( !cw_col)cw_col=make_group_aa ( &ng_cw_col,mat);
9183          if ( !amino_acid_list)
9184             {
9185                 amino_acid_list=declare_int ( 20, 65);
9186                 for ( a=0; a< 20; a++)
9187                     for ( b=0; b< 64; b++)
9188                         {
9189                             a1=translate_dna_codon ( triplet[b], 'x');
9190                             a2=amino_acid[a];
9191                             for ( d=0; d< ng_cw_col; d++)
9192                                 if ( is_in_set ( a1, cw_col[d]) && is_in_set ( a2, cw_col[d]))
9193                                    {
9194                                        amino_acid_list[a][++amino_acid_list[a][0]]=b;
9195                                    }
9196                         }
9197                 lu=vcalloc ( 26, sizeof (int));
9198                 for ( a=0; a<20; a++)
9199                     {
9200                         lu[amino_acid[a]-'a']=a;
9201                     }
9202                 /*
9203                 for ( a=0; a< 20; a++)
9204                     {
9205                         fprintf ( stderr, "\n%c", amino_acid[a]);
9206                         for ( b=1; b<=amino_acid_list[a][0]; b++)
9207                             fprintf ( stderr, "\n\t%s %c", triplet[amino_acid_list[a][b]], translate_dna_codon (triplet[amino_acid_list[a][b]], 'x'));
9208                     }
9209                 */                  
9210             }
9211         
9212          return triplet [addrand((unsigned long)amino_acid_list[lu[aa-'a']][0])+1];
9213      }                  
9214                                  
9215 /**************************************************************************************************/
9216 /********************************                      ********************************************/
9217 /********************************    PROCESSING        ********************************************/
9218 /********************************                      ********************************************/
9219
9220
9221          
9222 void modify_data  (Sequence_data_struc *D1in, Sequence_data_struc *D2in, Sequence_data_struc *DSTin, char **action_list,int n_actions, Action_data_struc *RAD)
9223      {
9224        Sequence  *COOR=NULL, *NS=NULL,*BUFS=NULL, *OUT_S=NULL;
9225        Constraint_list *CL;
9226        char *s;
9227        int value,upper_value, lower_value, start, end, a, b,c;
9228        int *count_table=NULL;
9229        char *action;
9230        Sequence_data_struc *D1; 
9231        Sequence_data_struc *D2;
9232        Sequence_data_struc *DST;
9233        int s1, s2, r1, r2;
9234        static int clean_flag;
9235        Alignment *BUF;
9236        
9237        /*Switches*/
9238
9239        action=action_list[0];
9240
9241        if (action[0]=='2')
9242          {
9243            
9244            D1=D2in;
9245            D2=D1in;
9246            DST=DSTin;
9247            action++;
9248          }
9249        else if ( action[0]=='1')
9250          {
9251            D1=D1in;
9252            D2=D2in;
9253            DST=DSTin;
9254            action++;
9255          }
9256        else if ( action[0]=='3')
9257          {
9258            D1=DSTin;
9259            D2=D1in;
9260            DST=DSTin;
9261            action++;
9262          }
9263        else
9264          {
9265            D1=D1in;
9266            D2=D2in;
9267            DST=DSTin;
9268          }
9269        if (!D1->A)D1->A=copy_aln (D1in->A, NULL); 
9270          
9271        if (  strm(action, "seqnos"))
9272          {
9273           (D1->A)->output_res_num=1;
9274          } 
9275        else if ( strm (action,"aln2bootstrap"))
9276          {
9277            (D1->A)=aln2bootstrap (D1->A, ATOI_ACTION (1));
9278            D1->S=aln2seq (D1->A);
9279          }
9280        else if ( strm (action,"aln2sample"))
9281          {
9282            (D1->A)=aln2sample (D1->A, ATOI_ACTION (1));
9283            D1->S=aln2seq (D1->A);
9284          }
9285        else if ( strm (action,"aln2random_aln"))
9286          {
9287            (D1->A)=aln2random_aln (D1->A, ACTION (1));
9288            D1->S=aln2seq (D1->A);
9289          }
9290        else if ( strm (action, "or_scan"))
9291          {
9292            HERE ("OR SCAN");
9293            D1->A=or_scan(D1->A, D2->A, ACTION(1));
9294            D1->S=aln2seq (D1->A);
9295          }
9296        else if ( strm (action, "or_sar"))
9297          {
9298            D1->A=or_sar(D1->A, D2->A, ACTION(1), PRINT);
9299            D1->S=aln2seq (D1->A);
9300          }
9301        else if ( strm ( action, "sar2subsar"))
9302          {
9303            /*in->sequences
9304              in2->sar data
9305            */
9306            Alignment *subA, *subS;
9307            
9308            if ( n_actions==1)
9309              {
9310                fprintf ( stderr, "\nin=aln, in2=sar sar2subsar [filter value compound1 compound2...] | [jack1] | [file]\n");
9311                myexit (EXIT_FAILURE);
9312              }
9313            
9314            sarset2subsarset ( D1->A, D2->A, &subA, &subS, main_read_aln (action_list[2], NULL));
9315            D1->A=subA;D2->A=subS;
9316          }
9317        else if ( strm (action, "display_sar"))
9318          {
9319            D1->A=display_sar (D1->A, D2->A, action_list[1]);
9320          }
9321        else if ( strm ( action, "sar2simpred"))
9322          {
9323            /*in->sequences
9324              in2->sar data
9325            */
9326            sar2simpred ( D1->A, D2->A, action_list[1], action_list[2], atoi(action_list[3]), atoi (action_list[4]));
9327          }
9328        else if ( strm ( action, "sar2simpred2"))
9329          {
9330            /*in->sequences
9331              in2->sar data
9332            */
9333            if ( n_actions!=5)
9334              {
9335                fprintf ( stderr, "\nERROR: +sar2simpred2 seqnamesfile posfile compound limit");
9336                myexit (EXIT_FAILURE);
9337              }
9338            sar2simpred2 ( D1->A, D2->A, action_list[1], action_list[2], action_list[3], atoi (action_list[4]));
9339          }
9340         else if ( strm ( action, "sar_analyze"))
9341          {
9342            /*in->sequences
9343              in2->sar data
9344            */
9345            sar_analyze ( D1->A, D2->A,action_list[1]);
9346          }
9347         else if ( strm ( action, "simple_sar_predict"))
9348           {
9349             //displays each column with ist score;
9350             simple_sar_predict (D1->A, D2->A,ACTION(1));
9351             exit (EXIT_SUCCESS);
9352           }
9353         else if ( strm ( action, "display_sar_analyze"))
9354           {
9355             //displays each column with ist score;
9356             display_simple_sar_analyze_col (D1->A, D2->A,ACTION(1));
9357             exit (EXIT_SUCCESS);
9358           }
9359        else if ( strm ( action, "display_sar_analyze_pc"))
9360           {
9361             //displays each column with ist score;
9362             display_simple_sar_analyze_pair_col (D1->A, D2->A,ACTION(1));
9363             exit (EXIT_SUCCESS);
9364           }
9365        else if ( strm ( action, "weight2sar"))
9366          {
9367            /*in->sequences
9368              in2->sar data
9369            */
9370            if ( n_actions!=3)
9371              {
9372                fprintf ( stderr, "\nERROR: +weight2sar <weight_file> <limit>");
9373                myexit (EXIT_FAILURE);
9374              }
9375            D1->A=weight2sar ( D1->A,D2->A, action_list[1], atoi(action_list[2]));
9376            
9377          }
9378         else if ( strm ( action, "sar_weight"))
9379          {
9380            /*in->sequences
9381              in2->sar data
9382            */
9383            if ( n_actions!=3)
9384              {
9385                fprintf ( stderr, "\nERROR: +sar_weight <sar_analyze> <compound>");
9386                myexit (EXIT_FAILURE);
9387              }
9388            D1->A=aln2weighted_sar_score ( D1->A,D2->A, action_list[1], action_list[2]);
9389            D1->S=aln2seq ( D1->A);
9390          }
9391        
9392        else if ( strm (action, "name2unique_name"))
9393          {
9394            char *tmp1, *tmp2;
9395            char command[1000];
9396            tmp1=vtmpnam (NULL); tmp2=vtmpnam (NULL);
9397            
9398            output_fasta_aln (tmp1,D1->A);
9399            free_aln (D1->A);free_sequence (D1->S, -1);
9400            sprintf ( command, "fasta_aln2fasta_aln_unique_name.pl %s >%s", tmp1, tmp2);
9401            my_system ( command);
9402            D1->S=get_fasta_sequence ( tmp2, NULL);
9403            D1->A=seq2aln (D1->S,NULL, 1);
9404          }
9405        else if ( strm (action, "rm_tag") || strm (action, "rm_template"))
9406          {
9407
9408            char **temp_name=NULL,**temp_list=NULL, temp_nseq=0;
9409            int z;
9410                         
9411            if ( D1 && D1->A){temp_name=(D1->A)->name;temp_nseq=(D1->A)->nseq;}
9412            else if ( D1 && D1->S){temp_name=(D1->S)->name;temp_nseq=(D1->S)->nseq;}
9413            temp_list=rm_name_tag (temp_name,temp_nseq, NULL);
9414            if ( n_actions>1 && strm (action_list[1], "template"))
9415               {
9416               
9417                for ( z=0; z<temp_nseq; z++)
9418                 {
9419                 if (temp_list[z][0])
9420                         {fprintf (stdout, "%s\n", temp_list[z]);}
9421                 }
9422                 myexit (EXIT_SUCCESS);
9423               } 
9424          }
9425        else if (strm (action, "add_template") || strm (action, "swap_header"))
9426          {
9427            D1->S=seq2template_seq (D1->S, action_list[1], NULL);
9428             D1->A=seq2aln(D1->S, NULL, 1);
9429          }
9430        else if ( strm ( action, "seq2year"))
9431          {
9432            D1->S=seq2year (D1->S, (n_actions>1)?atoi(action_list[1]):1);
9433            D1->A=seq2aln(D1->S, NULL, 1);
9434          }
9435        else if ( strm (action, "swap_lib_header"))
9436          {
9437            Sequence *S;
9438            S=main_read_seq (action_list[1]);
9439            (D1->CL)->S=S;
9440            
9441          }
9442        else if ( strm (action, "weight_lib"))
9443          {
9444            int l;
9445            int w;
9446            w=atoi (action_list[1]);
9447            if ( D1->CL)
9448              {
9449              for (l=0; l<(D1->CL)->ne; l++)
9450                (D1->CL)->L[l*CL->entry_len+WE]=w;
9451              }
9452          }
9453        else if ( strm (action, "struc2nb"))
9454          {
9455            int c;
9456            for ( c=0; c< (D1->S)->nseq; c++)
9457              {
9458                struclist2nb ((D1->S)->name[c],(D1->S)->seq[c], (D1->S)->seq_comment[c], atof(action_list[1]),ACTION(2),ACTION(3) );
9459              }
9460            myexit (EXIT_SUCCESS);
9461          }
9462       
9463      
9464       
9465        else if ( strm(action, "seq2contacts"))
9466          {
9467            int z;
9468            D1->S=swap_header (D1->S, D2->S); 
9469            for ( z=0; z< (D1->S)->nseq; z++)sprintf ( (D1->A)->name[z], "%s", (D1->S)->name[z]);
9470            DST->S=seq2contacts (D1->S, atof (action_list[1]));
9471            DST->A=copy_aln (D1->A, NULL);
9472            thread_seq_struc2aln ( DST->A,DST->S);
9473            for (z=0; z< (D1->S)->nseq; z++)
9474            (DST->A)->S=D1->S;
9475            
9476          }
9477        else if ( strm(action, "struc2contacts"))
9478          {
9479            char *seq;
9480            if ( atof (action_list[3])>0)
9481              {
9482                seq=map_contacts  (action_list[1], action_list[2], atof (action_list[3]));
9483                fprintf ( stderr, "\n>%s %s\n%s",action_list[1], action_list[2],seq);
9484              }
9485            else 
9486              print_contacts  (action_list[1], action_list[2], atof (action_list[3]));
9487            
9488            myexit (EXIT_SUCCESS);
9489          }
9490        else if ( strm(action, "treelist_prune")|| strm(action, "prune_treelist"))
9491          {
9492            Sequence *TS;
9493            if (D2 && D2->S)TS=D2->S;
9494            else TS=treelist2sub_seq((D1->S),ATOI_ACTION(1));
9495            treelist2prune_treelist ( D1->S,TS, NULL);
9496            D1->A=seq2aln (D1->S, NULL, NO_PAD);
9497          }
9498        else if ( strm (action, "tree2unresolved_nodes"))
9499          {
9500            int ns;
9501            int *l;
9502            ns=tree2nseq (D1->T);
9503            l=vcalloc (ns, sizeof (int));
9504            tree2nnode_unresolved (D1->T, l);
9505            for ( a=0; a<ns; a++)if (l[a])fprintf ( stdout, "SIZE: %d COUNT: %d\n", a, l[a]); 
9506            vfree (l);
9507            exit (EXIT_SUCCESS);
9508          }
9509        else if ( strm(action, "tree_prune") || strm(action, "prune_tree"))
9510          {
9511            D1->T=main_prune_tree ( D1->T, D2->S);
9512          }
9513        else if ( strm ( action, "tree2seq"))
9514          {
9515            D1->S=tree2seq(D1->T, NULL);
9516            D1->A=seq2aln (D1->S, D1->A, 1);
9517            (D1->A)->len_aln=1;
9518            for ( a=0; a< (D1->A)->nseq; a++)sprintf ( (D1->A)->seq_al[a], "sequence");
9519          }
9520        else if ( strm (action, "seq2dpatree"))
9521          {
9522            D1->T= seq2dpa_tree(D1->S,"ktup");
9523          }
9524        else if ( strm (action, "tree2dpatree"))
9525          {
9526            D1->T= tree2dpa_tree(D1->T,(D2 && D2->A)?D2->A:D1->A, (n_actions==1)?"idmat":action_list[1]);
9527          }
9528        else if ( strm (action, "tree2group"))
9529          {
9530            vfclose (tree2group (D1->T, (tree2seq(D1->T,NULL)), atoi(action_list[1]), atoi(action_list[2]),(n_actions==4)?action_list[3]:NULL, stdout));    
9531            myexit (EXIT_SUCCESS);
9532          }
9533        else if ( strm(action, "unroot"))
9534          {
9535            D1->T=unroot_tree(D1->T);
9536          }
9537      
9538        
9539        else if ( strm(action, "treelist2group")|| strm(action, "treelist2groups") )
9540          {
9541            Sequence *TS;
9542            
9543            if (D2 && D2->S)TS=D2->S;
9544            else TS=treelist2seq((D1->S));
9545            treelist2groups (D1->S, TS, ACTION(1), stdout);
9546            myexit (EXIT_SUCCESS);
9547            
9548            //      treelist2groups (D1->S,(D2)?D2->S:NULL, ACTION(1), stdout );
9549            //exit (EXIT_SUCCESS);
9550          }
9551        else if ( strm(action, "splits2tree"))
9552           {
9553           
9554            D1->T=split2tree ((D2)?D2->T:NULL,D1->S, ACTION(1));
9555            
9556          }
9557        else if ( strm(action, "count_splits"))
9558          {
9559           
9560            count_splits ((D2)?D2->T:NULL,D1->S, ACTION(1));
9561            exit (EXIT_SUCCESS);
9562          }
9563         else if ( strm(action, "count_groups"))
9564          {
9565            count_tree_groups (D1->S, ACTION(1));
9566          }
9567        else if ( strm (action, "tree2dist"))
9568          {
9569            int ta, tb, ***td;
9570            Sequence *TS;
9571            
9572            TS=(D2)?D2->S:NULL;
9573            td=tree2dist (D1->T,TS, NULL);
9574            if (!TS)TS=tree2seq(D1->T, NULL);
9575            for (ta=0; ta<TS->nseq; ta++)
9576              {
9577                fprintf ( stdout, "%-15s ",TS->name[ta]);
9578                for ( tb=0; tb<TS->nseq; tb++)
9579                  {
9580                    int n=0;
9581                    if ( ACTION(1) && strm (ACTION(1), "length"))n=1;
9582                    
9583                    fprintf (stdout, " %4d", td [n][ta][tb]);
9584                  }
9585                fprintf ( stdout, "\n");
9586              }
9587            exit (EXIT_SUCCESS);
9588          }
9589        else if ( strm (action, "treelist2lti"))
9590          {
9591            Sequence *TS;
9592            if (D2 && D2->S)TS=D2->S;
9593            else TS=treelist2sub_seq((D1->S),ATOI_ACTION(2));
9594            treelist2lti (D1->S,TS, (int)ATOI_ACTION(1), stdout );
9595            exit (0);
9596          }       
9597        else if ( strm (action,"treelist2frame"))
9598          {
9599            Sequence *TS;
9600            if (D2 && D2->S)TS=D2->S;
9601            else TS=treelist2sub_seq((D1->S),ATOI_ACTION(1));
9602            treelist2frame (D1->S, TS);
9603            myexit (EXIT_SUCCESS);
9604          }
9605        
9606        else if ( strm (action, "treelist2seq"))
9607          {
9608            D1->S=treelist2sub_seq (D1->S,ATOI_ACTION(1));
9609            D1->A=seq2aln(D1->S, NULL, 1);
9610          }       
9611        else if ( strm (action, "treelist2leafgroup"))
9612          {
9613            treelist2leafgroup (D1->S, (D2)?D2->S:NULL, ACTION(1));
9614            exit (0);
9615          }       
9616        else if ( strm(action, "treelist2splits"))
9617          {
9618            if (D1->T)D1->S=add_file2file_list ((D1->T)->file, NULL);
9619            treelist2splits (D1->S, (D2)?D2->S:NULL);
9620          }
9621       
9622        else if ( strm(action, "treelist2dmat"))
9623          {
9624            treelist2dmat (D1->S);
9625          }
9626        else if ( strm(action, "tree_cmp") || strm (action, "tree_compare"))
9627          {
9628            D1->T=main_compare_trees ( D1->T, D2->T, stdout);       
9629          }
9630        else if ( strm (action, "tree_scan"))
9631          {
9632            D1->T=tree_scan (D1->A, D2->T, ACTION(1), ACTION(2));
9633          }
9634        else if ( strm (action, "split_cmp"))
9635          {
9636            main_compare_splits (D1->T, D2->T, ACTION(1), stdout);
9637          }
9638        
9639        else if ( strm(action, "node_sort"))
9640          {
9641            node_sort ( action_list[1], D1->T);
9642            exit (EXIT_SUCCESS);
9643          }
9644        
9645        else if ( strm ( action, "avg_bootstrap"))
9646          {
9647            display_avg_bootstrap (D1->T);
9648            myexit (EXIT_SUCCESS);
9649          }
9650            
9651        else if ( strm (action, "tree_cog_cmp"))
9652          {
9653            main_compare_cog_tree (D1->T,action_list[1]);
9654            exit (EXIT_SUCCESS);
9655          }
9656        else if ( strm (action, "tree_aln_cmp"))
9657          {
9658            main_compare_aln_tree (D1->T, D2->A, stdout);
9659            exit (EXIT_SUCCESS);
9660          }
9661        else if ( strm(action, "change_bootstrap"))
9662          {
9663            D1->T=reset_boot_tree ( D1->T, (n_actions>=2)?atoi(action_list[1]):0);
9664          }
9665        else if ( strm(action, "change_distances"))
9666          {
9667            D1->T=reset_dist_tree ( D1->T, (n_actions>=2)?atof(action_list[1]):0.00);
9668          }
9669        
9670        else if ( strm(action, "aln2tree"))
9671          {
9672            D1->T=tree_compute (D1->A, n_actions-1, action_list+1);
9673          }
9674        else if ( strm(action, "similarities2tree"))
9675          {
9676            D1->T=similarities_file2tree (ACTION(1));
9677          }
9678        
9679        else if (  strm(action, "original_seqnos"))
9680          {
9681           (D1->A)->output_res_num=2;
9682          }
9683        
9684        else if ( strm (action, "aln2pred"))
9685          {
9686            aln2pred (D1->A, D2->A, ACTION (1));
9687            exit (EXIT_SUCCESS);
9688          }
9689        else if ( strm(action, "evaluate"))
9690          {
9691            Alignment *A;
9692
9693
9694            DST->A=copy_aln (D1->A, NULL);
9695            DST->S=aln2seq(DST->A);
9696            if (n_actions>1 && strm (  action_list[1], "categories"))
9697              {
9698                CL=declare_constraint_list ( DST->S,NULL, NULL, 0,NULL, read_matrice("pam250mt"));
9699                DST->A=  main_coffee_evaluate_output(DST->A, CL, "categories");
9700              }
9701            else if (n_actions>1 && strm (  action_list[1], "sar"))
9702              {
9703                CL=declare_constraint_list ( DST->S,NULL, NULL, 0,NULL, read_matrice("pam250mt"));
9704                DST->A=  main_coffee_evaluate_output(DST->A, CL, "sar");
9705              }
9706            else if (n_actions>1 && strstr (  action_list[1], "boxshade"))
9707              {
9708                char color_mode[1000];
9709                sprintf (color_mode,"boxshade_%d", atoi(ACTION2(2,"30")));
9710                CL=declare_constraint_list ( DST->S,NULL, NULL, 0,NULL, read_matrice("pam250mt"));
9711                DST->A=  main_coffee_evaluate_output(DST->A, CL, color_mode);
9712              }
9713            
9714            else
9715              {
9716                CL=declare_constraint_list ( DST->S,NULL, NULL, 0,NULL, read_matrice((n_actions==1)?"pam250mt":action_list[1]));
9717                DST->A=  main_coffee_evaluate_output(DST->A, CL, "matrix");
9718              }
9719            
9720            DST->S=aln2seq ( DST->A);
9721            
9722            A=D1->A;
9723            
9724            sprintf ( A->name[A->nseq], "cons");
9725            sprintf ( A->seq_al[A->nseq], "%s", aln2cons_seq_mat (A, "idmat"));
9726           
9727          }
9728        else if ( strm (action, "sp_evaluate"))
9729          {
9730            fprintf ( stdout, "SP Score: %.2f", sum_pair ((DST && DST->A)?DST->A:D1->A,ACTION(1),atoi(ACTION2(2,"0")),atoi(ACTION2(3,"0"))));
9731            exit (EXIT_SUCCESS);
9732          }
9733        else if ( strm (action, "lat_evaluate"))
9734          {
9735            float score;
9736            score=lat_sum_pair ( D1->A, action_list[1]);
9737            fprintf ( stdout, "\nLAT_SCORE: %.2f", score);
9738            exit (EXIT_SUCCESS);
9739            
9740          }
9741        else if ( strm (action, "add_scale"))
9742          {
9743            D1->A=aln2scale (D1->A, ACTION(1));
9744          }
9745        else if ( strm (action, "RNAfold_cmp"))
9746          {
9747            D1->A=compare_RNA_fold (D1->A, D2->A);
9748          }
9749        else if ( strm (action, "aln2alifold"))
9750          {
9751            D1->A=aln2alifold (D1->A);
9752            D1->S=aln2seq ( D1->A);
9753          }
9754                  
9755
9756        else if ( strm (action, "add_alifold"))
9757          {
9758            D1->A=add_alifold2aln (D1->A, (D2)?D2->A:NULL);
9759          
9760          }
9761        else if ( strm (action, "alifold2analyze"))
9762          {
9763            D1->A=alifold2analyze (D1->A, (D2)?D2->A:NULL, ACTION(1));
9764            D1->S=aln2seq(D1->A);
9765          }
9766        else if ( strm (action, "aln2conservation"))
9767          {
9768            D1->A=aln2conservation ( D1->A, ATOI_ACTION (1), ACTION (2));
9769            exit (EXIT_FAILURE);
9770          }
9771        else if ( strm (action, "aln2cons"))
9772          {
9773            char *cons_seq;
9774            char *cons_name;
9775            cons_name=vcalloc (100, sizeof (char));
9776            sprintf(cons_name, "%s", (n_actions<=2)?"Cons":action_list[2]);
9777            cons_seq=aln2cons_seq_mat (D1->A, (n_actions==1)?"blosum62mt":action_list[1]);
9778            free_aln (D1->A);free_sequence(D1->S, -1);
9779            D1->S=fill_sequence_struc (1, &cons_seq, &cons_name);
9780            /*keep the gaps*/
9781            (D1->S)->len[0]=strlen (cons_seq); sprintf ( (D1->S)->seq[0], "%s", cons_seq);
9782            D1->A=seq2aln (D1->S, NULL, KEEP_GAP);
9783            vfree (cons_name);vfree (cons_seq);
9784          }
9785        else if ( strm (action, "seq2filter"))
9786          {
9787            D1->S=seq2filter ( D1->S, atoi(action_list[1]), atoi(action_list[2]));
9788                               
9789          }
9790        else if ( strm (action, "aln2resindex"))
9791          {
9792            //-in: aln, file: ref_seq ref_res target_seq
9793            //-in2 target sequences
9794            aln2resindex (D1->A, (D2)?D2->A:NULL, stdout);
9795            exit (EXIT_SUCCESS);
9796          }
9797        else if (strm(action, "keep_name"))
9798          {
9799            RAD->keep_name=1-RAD->keep_name;
9800          }
9801         else if (strm(action, "use_consensus") ||strm(action, "use_cons") )
9802          {
9803            RAD->use_consensus=1-RAD->use_consensus;
9804          }
9805        else if ( strm(action, "ungap"))
9806          {
9807            seq2aln (D1->S, D1->A, 1);
9808          }
9809        else if ( strm2(action, "rmgap", "rm_gap"))
9810          {
9811
9812            ungap_aln_n (D1->A, (n_actions==1)?100:atoi(action_list[1]));
9813            free_sequence ( D1->S, (D1->S)->nseq);
9814            D1->S=aln2seq ( D1->A);
9815            (D1->A)->S=D1->S;
9816          }
9817        else if ( strm(action, "rmgap_col"))
9818          {
9819            D1->A=remove_gap_column ( D1->A,action_list[1]);
9820          }
9821        else if ( strm(action,"random"))
9822          {
9823            
9824            D1->A= make_random_aln(NULL,(n_actions==1)?1:atoi(action_list[1]),(n_actions==2)?100:atoi(action_list[2]),"acdefghiklmnpqrstvwy");
9825            
9826            D1->S=aln2seq ( D1->A);
9827          }
9828             
9829        else if ( strm(action, "landscape"))
9830           {
9831             
9832             set_landscape_msa ((n_actions==1)?0:atoi(action_list[1]));
9833           }
9834        else if ( strm(action, "clean_maln"))
9835           {
9836             if ( !DST) 
9837                    {
9838                    fprintf ( stderr,"\n[You Need an evaluation File: Change the output format][FATAL:%s]\n", PROGRAM);  
9839                    myexit(EXIT_FAILURE);
9840                    }
9841             (DST->A)=aln2number (DST->A);
9842             D1->A=clean_maln(D1->A, DST->A,(n_actions==1)?1:atoi(action_list[1]),(n_actions==1)?1:atoi(action_list[2]));   
9843           }
9844        else if ( strm (action, "extract"))
9845          {
9846            
9847            COOR=get_pir_sequence  (RAD->coor_file, NULL);
9848            D1->S=extract_sub_seq ( COOR, D1->S);
9849            free_aln (D1->A);
9850            D1->A=declare_Alignment(D1->S);
9851            seq2aln (D1->S, D1->A, RAD->rm_gap);
9852            free_sequence (COOR, COOR->nseq);
9853          }
9854        else if ( strm (action, "reorder_column"))
9855          {
9856
9857
9858           
9859                Alignment *RO1, *RO2;
9860                Sequence *OUT_S;
9861                int s;
9862                
9863                RO1=rotate_aln (D1->A,NULL);
9864                if (ACTION(1) && strm (ACTION(1), "tree"))
9865                  {
9866                    D1->T=tree_compute (RO1,n_actions-2, action_list+2);
9867                     OUT_S=tree2seq(D1->T, NULL);
9868                     RO1=reorder_aln(RO1, OUT_S->name, OUT_S->nseq);
9869                   }
9870                else if ( ACTION(1) && strm (ACTION(1), "random"))
9871                  {
9872                    RO1=reorder_aln ( RO1, NULL, RO1->nseq);
9873                  }
9874                
9875                RO2=rotate_aln (RO1, NULL);
9876                for (s=0; s< RO2->nseq; s++)
9877                  sprintf ( RO2->name[s], "%s", (D1->A)->name[s]);
9878                free_aln (RO1);
9879                free_aln (D1->A);
9880                D1->A=RO2;
9881                D1->S=aln2seq(D1->A);
9882          }
9883                                
9884        else if ( strm (action, "reorder"))
9885          {
9886            
9887            if ( n_actions==2 && strm (action_list[1], "random"))
9888              {
9889                D1->A=reorder_aln ( D1->A, NULL, (D1->A)->nseq);
9890              }
9891            else if (n_actions==2 && strm (action_list[1], "scramble"))
9892              {
9893                D1->A=aln2scramble_seq(D1->A);
9894              }
9895
9896            else if ( n_actions==2 && strm (action_list[1], "tree"))
9897              {
9898
9899                OUT_S=tree2seq (D2->T, NULL);
9900                D1->A=reorder_aln(D1->A, OUT_S->name, OUT_S->nseq);
9901                free_sequence (D1->S,(D1->S)->nseq);
9902                D1->S=aln2seq (D1->A);
9903              }
9904            else
9905              {
9906                (D2->A)->S=aln2seq (D2->A);
9907                (D1->A)->S=aln2seq (D1->A);
9908                OUT_S=trim_aln_seq_name(D2->A, D1->A);
9909                D1->A=reorder_aln(D1->A, OUT_S->name, OUT_S->nseq);
9910                free_sequence (D1->S,(D1->S)->nseq);
9911                D1->S=aln2seq (D1->A);
9912              }
9913          }
9914        else if ( strm (action, "cat_aln"))
9915          {
9916            /*D1->A=aln_cat ( D1->A, D2 ->A);*/
9917
9918            if (D2 && D2->A && !ACTION(1))
9919              D1->A=concatenate_aln (D1->A, D2->A, ACTION(1));
9920            else if (ACTION(1) && is_aln(ACTION(1)))
9921              {
9922                  Alignment *B;
9923                  int n=1;
9924
9925                  while (ACTION(n))
9926                    {
9927
9928                      B=main_read_aln (ACTION(n), NULL);
9929                      D1->A=concatenate_aln (D1->A, B, NULL);
9930                      n++;
9931                    }
9932                  D1->S=aln2seq(D1->A);
9933              }
9934                      
9935            else
9936              {
9937                Alignment *A, *B;
9938                
9939                A=main_read_aln ((D1->A)->name[0], NULL);
9940                
9941                for ( a=1; a<(D1->A)->nseq; a++)
9942                  {
9943                    B=main_read_aln ((D1->A)->name[a], NULL);
9944                    A=concatenate_aln (A, B, ACTION(1));
9945                    
9946                  }
9947                D1->A=A;
9948                D1->S=aln2seq(D1->A);
9949              }
9950          }
9951            
9952        else if ( strm ( action, "msalist2cat_pwaln"))
9953          {
9954            int a, b, c;
9955            int sim, min, max;
9956            
9957            if (n_actions!=3)
9958              {
9959                min=0;
9960                max=100;
9961              }
9962            else
9963              {
9964                min=atoi(action_list[1]);
9965                max=atoi(action_list[2]);
9966              }
9967            
9968            fprintf ( stdout, ">A\n");
9969            for (a=0;a<(D1->S)->nseq; a++)
9970              {
9971                Alignment *A;
9972                HERE ("process %s",  (D1->S)->name[a]);
9973                A=main_read_aln((D1->S)->name[a],NULL);
9974                for (b=0; b<A->nseq-1; b++)
9975                  {
9976                    for ( c=b+1; c<A->nseq; c++)
9977                      {
9978                        sim=get_seq_sim (A->seq_al[b], A->seq_al[c], "-", "");
9979                        if (sim>=min && sim<=max)fprintf (stdout, "xxx%s", A->seq_al[b]);
9980                      }
9981                  }
9982                free_aln (A);
9983              }
9984            fprintf ( stdout, "\n>B\n");
9985            for (a=0;a<(D1->S)->nseq; a++)
9986              {
9987                Alignment *A;
9988                HERE ("process %s",  (D1->S)->name[a]);
9989                A=main_read_aln((D1->S)->name[a],NULL);
9990                for (b=0; b<A->nseq-1; b++)
9991                  {
9992                    for ( c=b+1; c<A->nseq; c++)
9993                      {
9994                        sim=get_seq_sim (A->seq_al[b], A->seq_al[c], "-", "");
9995                        if (sim>=min && sim<=max)fprintf (stdout, "xxx%s", A->seq_al[c]);
9996                      }
9997                  }
9998                free_aln (A);
9999              }
10000            
10001            fprintf ( stdout, "\n");
10002            exit (EXIT_SUCCESS);
10003          }
10004        
10005        else if ( strm (action, "collapse_tree"))
10006          {
10007            D1->T=tree2collapsed_tree (D1->T, n_actions-1, action_list+1);
10008          }
10009        else if ( strm (action, "collapse_aln"))
10010          {
10011            D1->A=aln2collapsed_aln (D1->A, n_actions-1, action_list+1);
10012          }
10013        else if ( strm (action, "extract_aln"))
10014          {
10015            D1->A=aln2sub_aln_file (D1->A, n_actions-1, action_list+1);
10016            myexit (EXIT_SUCCESS);
10017          }
10018       
10019        
10020       
10021        else if ( strm (action, "remove_aa"))
10022          {
10023            int pos,len, n;
10024            pos=atoi(action_list[1]);
10025            len=atoi(action_list[2]);
10026            n=atoi (action_list[3]);
10027            if ( atoi (action_list[4])==1)len=-len; 
10028            if (pos && n>1) 
10029              {
10030                fprintf ( stderr, "\nWARNING: rm_aa, position (pos) and iteration number (n) simulatneously defined. Iteration number reset to 1 [%s]\n", PROGRAM);
10031                n=1;
10032              }
10033            for ( a=0; a< n; a++)
10034              D1->A=probabilistic_rm_aa (D1->A, pos, len);
10035          }
10036        else if ( strm (action, "remove_nuc"))
10037          {
10038            int pos;
10039            pos=atoi(action_list[1]);
10040            
10041            if ( pos>3 || pos<1)
10042              printf_exit (EXIT_FAILURE, stderr, "Remove_nuc: indicate a number between 1 and 3\n");
10043            
10044            pos--;
10045            for ( c=0,a=0; a<(D1->A)->len_aln; a++, c++)
10046              {
10047                if (c==3)c=0;
10048                for (b=0; b<(D1->A)->nseq; b++)
10049                  {
10050                  if (c==pos)
10051                    {
10052                      (D1->A)->seq_al[b][a]='-';
10053                    }
10054                  }
10055              }
10056            
10057            D1->S=aln2seq (D1->A);
10058          }
10059        
10060        else if (strm ( action, "conserved_positions"))
10061          {
10062            Alignment *A;
10063            int  a, b, c;
10064            int *cache=NULL;
10065            
10066
10067            A=D1->A;
10068            for ( a=0; a< A->nseq && !cache; a++)
10069              {
10070                if ( strm (action_list[1], A->name[a]))
10071                  {
10072                    cache=vcalloc ( A->len_aln+1, sizeof (int));
10073                    for ( c=0,b=0; b<A->len_aln; b++)
10074                      {
10075                        if ( is_gap (A->seq_al[a][b]))cache[b]=-1;
10076                        else cache[b]=++c;
10077                      }
10078                  }
10079              }
10080
10081            for ( a=0; a< A->len_aln; a++)
10082              {
10083                r1=A->seq_al[0][a];
10084                if ( is_gap(r1))continue;
10085                for ( c=0,b=0; b<A->nseq; b++)
10086                  {
10087                    r2=A->seq_al[b][a];
10088                    c+=(r1==r2)?1:0;
10089                  }
10090                if ( (c*100)/A->nseq>=atoi(action_list[2]))
10091                  fprintf ( stdout, "COL: %d Res: %c %s %d\n", a+1, r1, action_list[1], cache[a]+atoi(action_list[3]));
10092              }
10093            exit (EXIT_FAILURE);
10094          }
10095        else if (strm ( action, "extract_block") )
10096          {
10097            
10098            BUF=copy_aln (D1->A, NULL);
10099            if ( check_file_exists(action_list[1]))
10100              BUF=extract_aln3(BUF,action_list[1]);
10101            else
10102              BUF=extract_aln2(BUF,atoi(action_list[2]),atoi(action_list[3]),action_list[1]);
10103            D1->A=copy_aln (BUF,D1->A);
10104           
10105          }
10106        else if ( strm ( action, "extract_pos_list"))
10107          {
10108            D1->A=alnpos_list2block (D1->A, n_actions-1, action_list+1);
10109          }
10110        else if ( strm ( action, "seq2msa"))
10111          {
10112            D1->A=simple_progressive_aln ( D1->S, NULL, NULL, action_list[1]);
10113          }
10114        else if ( strm ( action, "realign_block") )
10115          {
10116            D1->A=realign_block ( D1->A, atoi (action_list[1]), atoi (action_list[2]), (n_actions==4)?action_list[3]:NULL);
10117          }
10118        else if ( strm (action, "extract_seq"))   
10119          {
10120            int is_file;
10121            if ( check_file_exists (action_list[1]))
10122              {
10123                is_file=1;
10124                BUFS=main_read_seq (action_list[1]);
10125                action_list=BUFS->name;
10126                n_actions=BUFS->nseq;           
10127              }
10128            else
10129              {
10130                is_file=0;
10131                action_list++;
10132                n_actions--;
10133              }
10134            
10135            for ( a=0; a< n_actions;)
10136              {
10137                s=action_list[a];
10138                
10139                if ( n_actions==1 || is_file==1)
10140                  {
10141                    start=1;
10142                    end=0;
10143                    a+=1;
10144                  }
10145                else
10146                  {
10147
10148                    start=(strm2 (s,"#","*"))?1:(atoi(action_list[a+1]));
10149                    end=  (strm2 (action_list[a+2],"#","*"))?0:(atoi(action_list[a+2]));
10150                    a+=3;
10151                  }
10152                
10153                if ( strm2 (s, "#", "*"))
10154                  {
10155                    OUT_S=extract_one_seq((D1->A)->name[0],start, end, D1->A, RAD->keep_name);
10156                    for (b=1; b< (D1->A)->nseq; b++)
10157                      {
10158                       NS=extract_one_seq((D1->A)->name[b],start, end, D1->A, RAD->keep_name);
10159                       if (count_n_res_in_array(NS->seq[0], -1))
10160                         OUT_S=add_sequence ( NS,OUT_S, 0);
10161                      }
10162                  }
10163                else
10164                  {
10165                   if ( a==1)OUT_S=extract_one_seq(s,start, end, D1->A, RAD->keep_name);
10166                   else
10167                     {
10168                       NS=extract_one_seq(s,start, end, D1->A, RAD->keep_name); 
10169                       OUT_S=add_sequence ( NS,OUT_S, 0);
10170                     }
10171                  }
10172              }
10173            D1->S=OUT_S;
10174            free_aln (D1->A);
10175            D1->A=declare_Alignment(D1->S);
10176            seq2aln (D1->S, D1->A, RAD->rm_gap);
10177          }
10178        
10179        else if ( strm (action, "extract_seq_list"))      
10180          {
10181            if ( check_file_exists (action_list[1]))
10182              {
10183               
10184                BUFS=main_read_seq (action_list[1]);
10185                action_list=BUFS->name;
10186                n_actions=BUFS->nseq;           
10187              }
10188            else
10189              {
10190                action_list++;
10191                n_actions--;
10192              }
10193            
10194            for ( a=0; a< n_actions;a++)
10195              {
10196                NS=extract_one_seq(action_list[a],1,0, D1->A, KEEP_NAME); 
10197                OUT_S=add_sequence ( NS,OUT_S, 0);
10198              }
10199            
10200            D1->S=OUT_S;
10201            free_aln (D1->A);
10202            D1->A=declare_Alignment(D1->S);
10203            seq2aln (D1->S, D1->A, RAD->rm_gap);
10204          }
10205        else if ( strm (action, "remove_seq") || strm (action, "rm_seq"))         
10206          {
10207            char *buf;
10208            char **list;
10209            int n;
10210            int l;
10211            
10212            list=declare_char ((D1->S)->nseq, 200);
10213            
10214            buf=vcalloc ((D1->S)->max_len+1, sizeof (char));
10215            for ( n=0,a=0; a< (D1->A)->nseq; a++)
10216              {
10217                
10218                sprintf (buf, "%s", (D1->S)->seq[a]);
10219                ungap (buf);
10220                l=strlen(buf);
10221                
10222                for (c=1, b=1; b< n_actions; b++)
10223                  {
10224                    if ( strm (action_list[b], (D1->S)->name[a])){(D1->S)->seq[a]=NULL;break;}
10225                    else if ( strm (action_list[b], "empty") && l==0)
10226                      {
10227                        fprintf ( stderr, "WARNING: Sequence %s does not contain any residue: automatically removed from the set [WARNING:%s]\n",(D1->S)->name[a], PROGRAM);
10228                        (D1->S)->seq[a]=NULL;break;
10229                      }
10230                    else if ( strm (action_list[b], "unique"))
10231                      {
10232                        if ( name_is_in_list ((D1->S)->name[a], list,n, 100)!=-1)
10233                          {
10234                            (D1->S)->seq[a]=NULL;break;
10235                          }
10236                        else
10237                          {
10238                            sprintf ( list[n++], "%s", (D1->S)->name[a]);
10239                          }
10240                      }
10241                  }
10242              }
10243            D1->S=duplicate_sequence (D1->S);
10244            free_aln (D1->A);
10245            free_char ( list, -1);
10246            D1->A=declare_Alignment(D1->S);
10247            seq2aln (D1->S, D1->A, RAD->rm_gap);
10248          }
10249        
10250        else if (  strm (action, "aln2overaln")|| strm (action,"overaln_param"))
10251          {
10252            //mode (lower|number|uanlign) Penalty (0-100) Thresold (0-9)
10253            int  p1,p2,p3,f, t;
10254            char *s;
10255            int eb=0;
10256            char clean_mode[100];
10257            OveralnP *F;
10258
10259            F=vcalloc (1, sizeof (OveralnP));
10260            if ( D2 && D2->A)
10261              {
10262                D1->A=mark_exon_boundaries (D1->A, D2->A);
10263                eb=1;
10264              }
10265            else if ( get_string_variable ("exon_boundaries"))
10266              {
10267               Sequence *S;
10268               Alignment *EB;
10269               EB=seq2aln(S=main_read_seq(s),NULL, 0);
10270               D1->A=mark_exon_boundaries (D1->A, EB);
10271               free_sequence (S, S->nseq); free_aln (EB);
10272               eb=1; 
10273              }
10274            
10275
10276            if (ACTION(1)==NULL)sprintf (F->mode, "lower");
10277            else if (strstr (ACTION(1), "h"))
10278              {
10279                fprintf ( stdout, "aln2unalign lower|number|unalign F P1 P2 P3 T\n");
10280                exit (EXIT_SUCCESS);
10281              }
10282            else sprintf (F->mode, "%s", ACTION(1));
10283            
10284            F->t=ATOI_ACTION(2);
10285            F->f=ATOI_ACTION(3);
10286            F->p1=ATOI_ACTION(4); 
10287            F->p2=ATOI_ACTION(5);
10288            F->p3=ATOI_ACTION(6);
10289            F->p3=ATOI_ACTION(7);
10290            
10291            if (int_variable_isset ("overaln_target"))f=get_int_variable ("overaln_target");
10292            if (int_variable_isset ("overaln_threshold"))t=get_int_variable ("overaln_threshold");
10293            if (eb)sprintf (F->model, "fsa2");
10294            else   sprintf (F->model, "fsa1");
10295            
10296            D1->A=aln2clean_pw_aln (D1->A, F);
10297            
10298          }
10299        else if (  strm (action,"aln2unalign"))
10300          {
10301            Alignment *SA;
10302            Sequence *SS;
10303            
10304            SS=aln2seq(SA);
10305            SA=copy_aln (D1->A, NULL);
10306            thread_seq_struc2aln (SA, SS);
10307            D1->A=unalign_aln (D1->A,SA, ATOI_ACTION(1));
10308            D1->S=aln2seq ( D1->A);
10309          }
10310        else if (  strm (action, "clean_cdna"))
10311          { 
10312            Alignment *A;
10313            A=D1->A;
10314            for (a=0; a< A->nseq; a++)
10315              {
10316                char *d, *buf, f;
10317                
10318                d=A->seq_al[a];
10319                f=get_longest_frame (d, 3);
10320                buf=vcalloc ( strlen (d)+1, sizeof (char));
10321                sprintf (buf, "%s", d+f);
10322                sprintf (d, "%s", buf);
10323                vfree (buf);
10324              }
10325          }
10326        else if ( strm (action, "clean_cdna2"))
10327          {
10328            D1->A=clean_cdna_aln ( D1->A);
10329            free_sequence ( D1->S, (D1->S)->nseq);
10330            D1->S=aln2seq ( D1->A);
10331          }
10332        else if ( strm  (action, "aln2short_aln"))
10333            {
10334              D1->A=aln2short_aln (D1->A, action_list[1], action_list[2], atoi(action_list[3]));
10335              free_sequence ( D1->S, (D1->S)->nseq);
10336              D1->S=aln2seq ( D1->A);
10337            }
10338        else if ( strm ( action, "complement"))
10339          {
10340            D1->A=complement_aln (D1->A);
10341            free_sequence ( D1->S, (D1->S)->nseq);
10342            D1->S=aln2seq ( D1->A);
10343          }
10344        else if ( strm ( action, "translate"))
10345          {
10346            D1->A=translate_dna_aln( D1->A,(n_actions==1)?0:atoi(action_list[1]));
10347            free_sequence ( D1->S, (D1->S)->nseq);
10348            D1->S=aln2seq ( D1->A);
10349          }
10350        else if (strm2 ( action, "back_translate","backtranslate"))
10351          {
10352           D1->A=back_translate_dna_aln( D1->A);
10353           free_sequence ( D1->S, (D1->S)->nseq);
10354           D1->S=aln2seq ( D1->A);
10355          }
10356        else if (strm ( action, "rotate"))
10357          {
10358            D1->A=rotate_aln( D1->A, action_list[1]);
10359            free_sequence ( D1->S, (D1->S)->nseq);
10360            D1->S=aln2seq ( D1->A);
10361          }
10362        else if (strm ( action, "invert"))
10363          {
10364           D1->A=invert_aln( D1->A);
10365           free_sequence ( D1->S, (D1->S)->nseq);
10366           D1->S=aln2seq ( D1->A);
10367          }
10368        else if (strm ( action, "code_dna_aln"))
10369          {
10370           D1->A=code_dna_aln( D1->A);
10371           free_sequence ( D1->S, (D1->S)->nseq);
10372           D1->S=aln2seq ( D1->A);
10373          }
10374        
10375        else if ( strm ( action, "mutate"))
10376          {
10377            D1->A=mutate_aln( D1->A,(n_actions==1)?"0":action_list[1]);
10378            free_sequence ( D1->S, (D1->S)->nseq);
10379            D1->S=aln2seq (D1->A);
10380          }
10381        else if ( strm ( action, "thread_profile_on_msa"))
10382          {
10383            (D1->A)->S=NULL;
10384            D1->A=thread_profile_files2aln (D1->A, action_list[1], NULL);
10385            D1->S=aln2seq(D1->A);
10386          }
10387        else if ( strm ( action, "thread_dna_on_prot_aln"))
10388           {
10389             D1->A=thread_dnaseq_on_prot_aln (D1->S, D2->A);
10390             free_sequence (D1->S,(D1->S)->nseq);
10391             D1->S=aln2seq (D1->A); 
10392           }
10393        else if ( strm ( action, "thread_struc_on_aln"))
10394          {
10395            thread_seq_struc2aln ( D2->A, D1->S);
10396            D1->A=copy_aln(D2->A, D1->A);
10397            free_sequence ( D1->S, (D1->S)->nseq);
10398            D1->S=aln2seq (D1->A);
10399          }
10400        else if ( strm (action, "sim_filter"))
10401          {
10402            D1->A=sim_filter (D1->A, action_list[1], ACTION (2));
10403            free_sequence (D1->S,(D1->S)->nseq);
10404            D1->S=aln2seq (D1->A); 
10405          }
10406        else if ( strm (action, "seq2blast"))
10407          {
10408            D1->A=seq2blast (D1->S);
10409            free_sequence (D1->S,(D1->S)->nseq);
10410            D1->S=aln2seq (D1->A); 
10411          }
10412            
10413        else if ( strm (action, "trim"))
10414          {
10415            D1->A=simple_trimseq (D1->A,(D2)?D2->A:NULL, action_list[1], ACTION (2));
10416            
10417            free_sequence (D1->S,(D1->S)->nseq);
10418            D1->S=aln2seq (D1->A); 
10419          }
10420        
10421        else if (strm ( action, "trimTC"))
10422          {
10423            value=(n_actions==1)?10:atoi(action_list[1]);
10424         
10425            D1->A=tc_trimseq(D1->A,D1->S,action_list[1]);
10426            free_sequence (D1->S,(D1->S)->nseq);
10427            D1->S=aln2seq (D1->A); 
10428          }
10429        else if (strm ( action, "trimTC2"))
10430          {
10431            char *group_file;
10432            Alignment *B=NULL;
10433            char trim_mode[100];
10434            if ( n_actions==1 || !(strm (action_list[1], "NSEQ") ||strm (action_list[1], "MINID")) )
10435              {
10436                fprintf ( stderr, "\nTrimTC2 <NSEQ | MINID>  <number sequences| minimum identity> (<matrix>)\n");
10437                myexit (EXIT_FAILURE);
10438              }
10439            sprintf (trim_mode, "%s", action_list[1]);action_list+=2; n_actions-=2;
10440            
10441            if ( strm ( trim_mode, "NSEQ"))
10442              {
10443                group_file=tree2Ngroup( (D1)?D1->A:NULL, (D2)?D2->T:NULL, atoi (action_list[0]), vtmpnam(NULL), (n_actions==1)?"idmat":action_list[1]);
10444              }
10445            else
10446              {
10447                group_file=tree2Ngroup( (D1)?D1->A:NULL, (D2)?D2->T:NULL, -1*atoi (action_list[0]), vtmpnam(NULL), (n_actions==1)?"idmat":action_list[1]);
10448              }
10449            
10450            B=copy_aln (D1->A, B);
10451            B=aln2sub_aln_file (B,1,&group_file);
10452            B=aln2sub_seq (B, 1, &group_file);
10453            D1->A=extract_sub_aln2 (D1->A, B->nseq, B->name);
10454          }
10455        else if ( strm (action, "chain"))
10456          {
10457            D1->A=seq2seq_chain (D1->A,D2->A, ACTION(2));
10458          }
10459        
10460        
10461        else if (strm ( action, "master_trim"))
10462          {
10463            value=(n_actions==1)?10:atoi(action_list[1]);
10464         
10465            D1->A=master_trimseq(D1->A,D1->S,action_list[1]);
10466            free_sequence (D1->S,(D1->S)->nseq);
10467            D1->S=aln2seq (D1->A); 
10468          }
10469         else if ( strm (action, "force_aln"))    
10470          {
10471            char ***rlist=NULL;
10472            int count=0;
10473
10474            if ( n_actions==2)
10475              {
10476                if (!is_lib_02(action_list[1]))
10477                  {
10478                    fprintf ( stderr, "\nERROR: force_aln requires files in TC_LIB_FORMAT_02 [FATAL:%s]", PROGRAM);
10479                    myexit (EXIT_FAILURE);
10480                  }
10481                else
10482                    rlist=file2list (action_list[1], " ");
10483              }
10484            else
10485              {
10486                rlist=declare_arrayN(3, sizeof (char),3,7, 10);
10487                
10488                strcat (rlist[1][1],action_list[1]);strcat (rlist[1][3],action_list[2]);
10489                strcat (rlist[1][4],action_list[3]);strcat (rlist[1][6],action_list[4]);
10490                sprintf ( rlist[2][0], "-1");
10491              }
10492            count=1;
10493            while (rlist[count] && atoi(rlist[count][0])!=-1)
10494              {
10495                char st1[100], st2[100], st3[100], st4[100];
10496                
10497                sprintf ( st1, "%s", rlist[count][1]);sprintf ( st2, "%s", rlist[count][3]);
10498                sprintf ( st3, "%s", rlist[count][4]);sprintf ( st4, "%s", rlist[count][6]);
10499                fprintf ( stderr, "\nFORCE: %s %s %s %s", st1, st2, st3, st4);
10500                
10501                if (is_number (st1))s1=atoi (st1)-1;
10502                else s1=name_is_in_list (st1,(D1->A)->name, (D1->A)->nseq, 100);
10503                if ( s1<0 || s1>= (D1->A)->nseq)crash ("wrong sequence index");
10504                r1=atoi (st2)-1;
10505                
10506                if (is_number (st3))s2=atoi (st3)-1;
10507                else s2=name_is_in_list (st3,(D1->A)->name, (D1->A)->nseq, 100);
10508                if ( s2<0 || s2>= (D1->A)->nseq)crash ("wrong sequence index");
10509                r2=atoi (st4)-1;
10510                
10511                (D1->A)=add_constraint2aln ((D1->A), s1, r1, s2, r2);
10512                count++;
10513              }
10514            fprintf ( stderr, "\n");
10515            free_arrayN((void*)rlist,3);
10516          }
10517       
10518         else if (strm ( action, "grep"))
10519           {
10520             D1->A=grep_seq (D1->A, ACTION(1),ACTION(2), ACTION(3));
10521             if (D1->A==NULL) myexit (EXIT_SUCCESS);
10522             else D1->S=aln2seq (D1->A); 
10523           }
10524                       
10525         else if (strm (action, "find"))
10526           {
10527             int r, l;
10528             char *search_string;
10529
10530             search_string=vcalloc ( 30, sizeof (char));
10531             if ( strm (action_list[1], "lower"))sprintf ( search_string, "abcdefghijklmnopqrstuvwxyz");
10532             else if ( strm ( action_list[1], "upper"))sprintf ( search_string, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
10533             else 
10534               {
10535                 vfree (search_string);search_string=vcalloc ( strlen (action_list[1])+1, sizeof (char));
10536                 sprintf (search_string, "%s", action_list[1]);
10537               }
10538             
10539             for (a=0; a<(D1->A)->nseq; a++)
10540               for ( l=0,b=0; b< (D1->A)->len_aln; b++)
10541                 {
10542                   r=(D1->A)->seq_al[a][b];
10543                   l+=!is_gap(r);
10544                   if ( r!='\0' && strrchr (search_string, r))
10545                     {
10546                       /*fprintf ( stdout, "%-15s res %c alnpos %4d seqpos %4d\n", (D1->A)->name[a], r, b+1, l);*/
10547                       fprintf ( stdout, "%s %d %d\n", (D1->A)->name[a], l, l+1);
10548                     }
10549                 }
10550             myexit (EXIT_SUCCESS);
10551           }
10552         else if ( strm (action, "merge_annotation"))
10553           {
10554             D1->A=merge_annotation (D1->A, DST?DST->A:NULL, ACTION(1));
10555             D1->S=aln2seq (D1->A); 
10556           }
10557         else if ( strm  (action, "color_residue"))
10558           {
10559             int i;
10560             Alignment *A;
10561             A=D1->A;
10562             
10563             DST->A=copy_aln (D1->A, NULL);
10564             DST->S=aln2seq (DST->A);
10565             for (a=0; a< (DST->S)->nseq; a++)ungap ((DST->S)->seq[a]);
10566             
10567             if (n_actions>2)
10568               {
10569                 for (a=1; a<n_actions; a+=3)
10570                   {
10571                     i=name_is_in_list(action_list[a], (D1->A)->name, (D1->A)->nseq, 100);
10572                     if (i!=-1)
10573                       {
10574                         (DST->S)->seq[i][atoi(action_list[a+1])-1]='0'+atoi(action_list[a+2])-1;
10575                       }
10576                     else fprintf (stderr, "\nWARNING: Could not find Sequence %s", action_list[a]);
10577                   }
10578               }
10579             else
10580               {
10581                 char name[1000];
10582                 int pos, val;
10583                 FILE *fp;
10584                 
10585                 fp=vfopen (action_list[1], "r");
10586                 while (fscanf (fp, "%s %d %d\n", name, &pos, &val)==3)
10587                   {
10588                 
10589                      i=name_is_in_list(name, (D1->A)->name, (D1->A)->nseq, 100);
10590                      if (i!=-1)(DST->S)->seq[i][pos-1]='0'+val; 
10591                      else fprintf (stderr, "\nWARNING: Could not find Sequence %s", action_list[a]);
10592                   }
10593                 vfclose (fp);
10594               }
10595             DST->A=seq2aln (DST->S, NULL, 1);
10596           }
10597        else if ( strm  (action, "edit_residue"))
10598           {
10599             FILE *fp;
10600             int i, pos;
10601             int **p;
10602             char mod[100], name[100];
10603             Alignment *A;           
10604             
10605             A=D1->A;
10606             
10607             p=aln2inv_pos (A);
10608             if (n_actions>2)
10609               {
10610                 for (a=1; a<n_actions; a+=3)
10611                   {
10612                     
10613                     i=name_is_in_list(action_list[a], (D1->A)->name, (D1->A)->nseq, 100);
10614                     if (i!=-1)
10615                       {
10616                         pos=atoi(action_list[a+1]);
10617                         
10618                         pos=p[i][pos]-1;
10619                         sprintf (mod, "%s", action_list[a+2]);
10620                         if ( strm (mod, "upper"))(D1->A)->seq_al[i][pos]=toupper((D1->A)->seq_al[i][pos]);
10621                         else if ( strm (mod, "lower"))(D1->A)->seq_al[i][pos]=tolower((D1->A)->seq_al[i][pos]);
10622                         else (D1->A)->seq_al[i][pos]=mod[0];
10623                       }
10624                     else fprintf (stderr, "\nWARNING: Could not find Sequence %s", action_list[a]);
10625                     
10626                   }
10627               }
10628             else
10629               {
10630                 fp=vfopen (action_list[1], "r");
10631                 while (fscanf (fp, "%s %d %s\n", name, &pos, mod)==3)
10632                   {
10633                 
10634                      i=name_is_in_list(name, (D1->A)->name, (D1->A)->nseq, 100);
10635                      if (i!=-1)
10636                        {
10637                          pos=p[i][pos]-1;
10638                          if ( strm (mod, "upper"))(D1->A)->seq_al[i][pos]=toupper(A->seq_al[i][pos]);
10639                          else if ( strm (mod, "lower"))A->seq_al[i][pos]=tolower(A->seq_al[i][pos]);
10640                          else A->seq_al[i][pos]=mod[0];
10641                        }
10642                       else fprintf(stderr, "\nWARNING: Could not find Sequence %s", action_list[a]);
10643                   }
10644                 vfclose (fp);
10645               }
10646             D1->S=aln2seq (D1->A);
10647           }
10648        else if ( strm (action, "clean_flag"))
10649          {
10650            clean_flag=1-clean_flag;
10651          }
10652        else if ( strm  (action, "aln2case"))
10653          {
10654            D1->A=aln2case_aln (D1->A, ACTION(1), ACTION(2));
10655            D1->S=aln2seq(D1->A);
10656          }
10657        
10658        else if ( strm5 (action, "convert","upper","lower", "keep", "switchcase"))
10659          {
10660            b=1;
10661
10662            if ( n_actions>1 && is_number (action_list[b]))
10663              {
10664                lower_value=upper_value=atoi(action_list[b++]);
10665              }
10666            else if ( n_actions>1 && strm (action_list[b], "gap"))
10667              {
10668                DST=vcalloc (1,sizeof(Sequence_data_struc));
10669                DST->A=aln2gap_cache (D1->A,0);
10670                lower_value=0;
10671                upper_value=0;
10672                b++;
10673              }
10674            else if (n_actions>1 && action_list[b] && action_list[b][0]=='[')
10675              
10676              {
10677                lower_value=atoi(strtok (action_list[b]+1, "-[]"));
10678                upper_value=atoi(strtok (NULL, "-[]"));
10679              
10680                b++;
10681              }
10682            else
10683              {
10684                lower_value=upper_value=-1;
10685              }
10686
10687            if ( n_actions >b ||strm (action, "keep") )
10688              {
10689                if ( !RAD->symbol_list)RAD->symbol_list=declare_char (STRING, STRING);
10690                RAD->n_symbol=0;
10691                if ( strm (action, "keep") )sprintf ( RAD->symbol_list[RAD->n_symbol++], "#-");
10692                else
10693                  {
10694                    for (a=b; a< n_actions; a++)
10695                      {
10696                        sprintf ( RAD->symbol_list[RAD->n_symbol], "%s", action_list[a]);
10697                        RAD->n_symbol++;
10698                      }
10699                  }
10700              }
10701            
10702            for ( value=0; value<=9; value++)
10703              {
10704                if ( lower_value==-1)value=-1;
10705                
10706                if ( (value>=lower_value && value<=upper_value)|| value==-1)
10707                  {
10708                    if (strm(action,"convert")) D1->A=filter_aln_convert (D1->A, DST?DST->A:NULL,RAD->use_consensus,value,RAD->n_symbol, RAD->symbol_list);
10709                    else if (strm(action,"upper"))D1->A=filter_aln_lower_upper (D1->A, DST?DST->A:NULL,RAD->use_consensus,value);
10710                    else if (strm(action,"lower"))D1->A=filter_aln_upper_lower (D1->A, DST?DST->A:NULL,RAD->use_consensus,value);
10711                    else if (strm(action,"switchcase"))D1->A=filter_aln_switchcase (D1->A, DST?DST->A:NULL,RAD->use_consensus,value);
10712                  }
10713                else
10714                  {
10715                    if (strm(action,"keep")) D1->A=filter_aln_convert (D1->A, DST?DST->A:NULL,RAD->use_consensus,value,RAD->n_symbol, RAD->symbol_list);
10716                  }
10717                if (value==-1)break;
10718
10719              }
10720
10721            /*free_sequence (D1->S,(D1->S)->nseq);*/
10722            if (!D1->S)D1->S=aln2seq (D1->A); 
10723          } 
10724         else if ( strm ( action, "count_pairs"))
10725           {
10726             int a, b,c,v, **matrix;
10727             Alignment *A;
10728             matrix=declare_int (300,300);
10729             A=D1->A;
10730             for ( a=0; a< A->nseq-1; a++)
10731               for (b=0; b< A->nseq; b++)
10732                 for (c=0; c<A->len_aln; c++)
10733                   matrix[(int)A->seq_al[a][c]][(int)A->seq_al[b][c]]++;
10734             for ( a=0; a<255; a++)
10735               for ( b=a; b<256; b++)
10736                 {
10737                   v=matrix[a][b]+matrix[b][a];
10738                   if (v)fprintf ( stdout, "\n%c %c %d", a, b, v);
10739                 }
10740             exit (EXIT_SUCCESS);
10741           }
10742         else if ( strm (action, "count_misc"))
10743           {
10744             count_misc (D1->A, (!D2)?NULL:D2->A);
10745           }
10746        else if ( strm (action, "count"))
10747          {
10748            b=1;
10749            if ( n_actions>1 && is_number (action_list[b]))
10750              {
10751                lower_value=upper_value=atoi(action_list[b++]);
10752              }
10753            else if (n_actions>1 && action_list[b] && action_list[b] && action_list[b][0]=='[')
10754              
10755              {
10756                lower_value=atoi(strtok (action_list[b]+1, "-[]"));
10757                upper_value=atoi(strtok (NULL, "-[]"));
10758              
10759                b++;
10760              }
10761            else
10762              {
10763                lower_value=upper_value=-1;
10764              }
10765            if ( n_actions >b)
10766              {
10767                if ( !RAD->symbol_list)RAD->symbol_list=declare_char (STRING, STRING);
10768                RAD->n_symbol=0;
10769                for (a=b; a< n_actions; a++)
10770                  {
10771                    sprintf ( RAD->symbol_list[RAD->n_symbol], "%s", action_list[a]);
10772                    RAD->n_symbol++;
10773                  }
10774              }
10775            for ( value=lower_value; value<=upper_value; value++)
10776              {
10777                count_table=count_in_aln (D1->A, DST?DST->A:NULL,value,RAD->n_symbol, RAD->symbol_list, count_table);           
10778              }
10779            for ( a=0; a<RAD->n_symbol; a++)
10780              {
10781                fprintf ( stdout, "%s %d\n", RAD->symbol_list[a], count_table[a]);
10782              }
10783            free_sequence (D1->S,(D1->S)->nseq);
10784            D1->S=aln2seq (D1->A); 
10785            vfree(count_table);
10786            exit(EXIT_SUCCESS);
10787          }
10788        else if ( strm (action, "msa_weight"))
10789          {
10790            int random_value;
10791            char command [LONG_STRING];
10792            char aln_name[FILENAMELEN];
10793            char tree_name[FILENAMELEN];
10794            char dist_matrix_name[FILENAMELEN];
10795            char weight_name[FILENAMELEN]; 
10796            char method_4_msa_weights[1000];
10797            
10798            if ( n_actions==1)
10799              {
10800                fprintf ( stderr, "\nError: msa_weight requires a weight_method");
10801              }
10802            
10803            sprintf ( method_4_msa_weights, "%s", (get_env_variable ("METHOD_4_MSA_WEIGHTS",NO_REPORT))?get_env_variable ("METHOD_4_MSA_WEIGHTS",NO_REPORT):METHOD_4_MSA_WEIGHTS);
10804            
10805            /*1 Computation of the tree and the distance matrix*/
10806            random_value=addrand ((unsigned long) 100000)+1;
10807            sprintf (aln_name, "%d.aln", random_value);
10808            sprintf (tree_name, "%d.ph", random_value);
10809            sprintf (dist_matrix_name, "%d.dst", random_value);
10810            sprintf (weight_name, "%d.weight", random_value);
10811            output_fasta_aln (aln_name, D1->A);
10812           
10813            sprintf ( command, "clustalw -infile=%s -tree -outputtree=dist %s", aln_name, TO_NULL_DEVICE);
10814            my_system ( command);
10815            sprintf ( command, "%s -method %s -aln %s -tree %s -dmatrix %s -weightfile %s %s",method_4_msa_weights, action_list[1],aln_name, tree_name, dist_matrix_name,weight_name, TO_NULL_DEVICE); 
10816            my_system ( command);
10817            
10818            (D1->A)->S=aln2seq (D1->A);
10819            ((D1->A)->S)->W=read_seq_weight ( (D1->A)->name, (D1->A)->nseq,weight_name);    
10820            vremove (weight_name);
10821            vremove (aln_name);
10822            vremove (tree_name);
10823            vremove (dist_matrix_name);     
10824          }
10825        else if ( strm (action, "pavie_seq2random_seq"))
10826          {
10827            D1->S=pavie_seq2random_seq (D1->S, action_list[1]);
10828            D1->A=seq2aln (D1->S,NULL,1);
10829          }
10830        else if ( strm ( action, "pavie_seq2noisy_seq"))
10831          {
10832            /*<amount of noise: 0-100> (<alp>)*/
10833            
10834            D1->S=pavie_seq2noisy_seq (D1->S, atoi(action_list[1]),ACTION(2));
10835            D1->A=seq2aln (D1->S,NULL,1);
10836          }
10837        else if ( strm (action, "pavie_seq2pavie_mat"))
10838          {
10839
10840            pavie_seq2trained_pavie_mat ( D1->S, (n_actions==2)?action_list[1]:NULL);
10841            myexit (EXIT_SUCCESS);
10842          }
10843        else if ( strm (action, "pavie_seq2pavie_aln"))
10844          {
10845
10846            pavie_seq2pavie_aln ( D1->S, action_list[1], ACTION(2));
10847            myexit (EXIT_SUCCESS);
10848          }
10849        else if ( strm (action, "pavie_seq2pavie_dm"))
10850          {
10851             if (strstr (ACTION2(2,""), "_MSA_"))
10852               D1->S=aln2seq_main(D1->A, KEEP_GAP);
10853            
10854                    
10855            pavie_seq2pavie_aln ( D1->S, action_list[1],(n_actions==3)?action_list[2]:"_MATDIST_");
10856            myexit (EXIT_SUCCESS);
10857          }
10858        else if ( strm (action, "pavie_seq2pavie_msa"))
10859          {
10860            D1->A=pavie_seq2pavie_msa ( D1->S, action_list[1], (n_actions==3)?action_list[2]:NULL);
10861          }
10862        else if ( strm (action, "pavie_seq2pavie_tree"))
10863          {
10864            D1->T=pavie_seq2pavie_tree ( D1->S, action_list[1], (n_actions==3)?action_list[2]:NULL);
10865          }
10866        else if ( strm (action, "pavie_seq2pavie_sort"))
10867          {
10868            D1->A=pavie_seq2pavie_sort ( D1->S, action_list[1], (n_actions==3)?action_list[2]:NULL);
10869          }
10870       
10871        else if ( strm (action, "aln2mat_diaa"))
10872          {
10873            aln2mat_diaa (D1->S);
10874          }
10875        else if ( strm (action, "aln2mat"))
10876          {
10877            aln2mat(D1->S);
10878          }
10879        
10880        else if ( strm (action, "seq2latmat"))
10881          {
10882            seq2latmat ( D1->S, "stdout");
10883            myexit (EXIT_SUCCESS);
10884          }
10885        else if ( strm (action , "rm_target_pdb"))
10886          {
10887            int i, j;
10888            char *buf;
10889            
10890            for (i=0; i< (D1->A)->nseq; i++)
10891              {
10892                j=1;buf=(D1->A)->name[i];
10893                while (buf[j]!='_' && buf[j-1]!='_' && buf[j]!='\0')j++;
10894                buf[j]='\0';
10895              }
10896          }
10897        else if ( strm ( action, "mat2cmp"))
10898          {
10899            double *r;
10900            r=mat2cmp (D1->M, D2->M);
10901            fprintf ( stdout, "\nMATRIX COMPARISON: R=%.3f R2=%.3f On %d pairs of values\n", (float)r[0], (float)r[1], (int)r[2]);
10902            myexit (EXIT_SUCCESS);
10903          }
10904 //Special modes
10905        else if ( strm ( action, "overaln_list"))
10906          {
10907            float *re, tre=0,sn, tsn=0, sp, tsp=0;
10908            int p1,p2,p3, t, f;
10909            FILE *fp;
10910            char fname [100];
10911            Alignment **LA;
10912            Alignment **LB;
10913            
10914            HERE ("F P1 P2 P3 T");
10915
10916            t=ATOI_ACTION(1);
10917            f=ATOI_ACTION(2);
10918            p1=ATOI_ACTION(3);
10919            p2=ATOI_ACTION(4);
10920            p3=ATOI_ACTION(5);
10921            
10922            
10923            
10924            LA=vcalloc ((D1->A)->nseq, sizeof (Alignment*));
10925            LB=vcalloc ((D2->A)->nseq, sizeof (Alignment*));
10926            for (a=0; a<(D1->A)->nseq; a++)
10927              {
10928                 LA[a]=main_read_aln ((D1->A)->name[a], NULL);
10929                 LB[a]=main_read_aln ((D2->A)->name[a], NULL);
10930              }
10931         
10932            for ( a=0; a<(D1->A)->nseq; a++)
10933              {
10934                Alignment *A, *B;
10935                A=LA[a];
10936                B=LB[a];
10937                re=analyze_overaln (A, B, "_case_l_",t,f,p1,p2,p3);
10938                fprintf (stdout, "\n%d: sn: %.2f sp: %.2f re: %.2f F: %d P: %d P2: %d T: %d",a, re[0],re[1],re[2],f, p1,p2,t);
10939                tsn+=re[0];
10940                tsp+=re[1];
10941                tre+=re[2];
10942                vfree(re);
10943              }
10944            fprintf (stdout, "\nTOT: sn: %.2f sp: %.2f re: %.2f F: %d P: %d P2: %d T: %d", tsn/(D1->A)->nseq,tsp/(D1->A)->nseq, tre/(D1->A)->nseq,f,p1,p2,t);
10945            
10946            exit (0);
10947          }
10948        else if ( strm ( action, "overaln_list_scan"))
10949          {
10950            float *re, tre=0, tsn=0, tsp;
10951            int p1,p2, p3, t, f;
10952            FILE *fp;
10953            char fname [100];
10954            Alignment **LA;
10955            Alignment **LB;
10956            
10957            if ( ACTION(1))sprintf ( fname, "%s", ACTION(1));
10958            else sprintf ( fname, "scan_results.txt");
10959            
10960            fprintf ( stdout, "SCAN Results will be ouput in %s\n", fname);
10961            
10962            
10963            LA=vcalloc ((D1->A)->nseq, sizeof (Alignment*));
10964            LB=vcalloc ((D2->A)->nseq, sizeof (Alignment*));
10965            for (a=0; a<(D1->A)->nseq; a++)
10966              {
10967                 LA[a]=main_read_aln ((D1->A)->name[a], NULL);
10968                 LB[a]=main_read_aln ((D2->A)->name[a], NULL);
10969              }
10970            for (f=32; f<=40; f++)
10971              {
10972                for (p1=90; p1<=100; p1+=5)
10973                  {
10974                    for ( t=1; t<=3; t++)
10975                      {
10976                        for (p2=0; p2<=40; p2+=5)
10977                          {
10978                            for (p3=0;p3<=0;p3+=5)
10979                              {
10980                                tre=tsn=tsp=0;
10981                                for ( a=0; a<(D1->A)->nseq; a++)
10982                                  {
10983                                    Alignment *A, *B;
10984                                    A=LA[a];
10985                                    B=LB[a];
10986                                    re=analyze_overaln (A, B, "_case_l_",t,f,p1,p2,p3);
10987                                    
10988                                    tsn+=re[0];
10989                                    tsp+=re[1];
10990                                    tre+=re[2];
10991                                    vfree (re);
10992                                  }
10993                                fp=vfopen (fname, "a");
10994                                fprintf (fp, "\nTOT: sn: %.2f sp: %.2f re: %.2f P: %d P2: %d P3: %d T: %d F: %d", tsn/(D1->A)->nseq,tsp/(D1->A)->nseq, tre/(D1->A)->nseq, p1,p2, p3,t,f);
10995                                fprintf (stderr, "\nTOT: sn: %.2f sp: %.2f re: %.2f P: %d P2: %d P3: %d T: %d F: %d", tsn/(D1->A)->nseq,tsp/(D1->A)->nseq, tre/(D1->A)->nseq, p1,p2, p3,t,f);
10996                                vfclose (fp);
10997                              }
10998                          }
10999                      }
11000                  }
11001              }
11002            exit (0);
11003          }
11004        else if ( strm ( action, "overaln"))//Evaluate the capacity to predict over-aligned regions
11005          {
11006            OveralnP *F;
11007            F=vcalloc (1, sizeof (OveralnP));
11008            //al1: ref
11009            //al2: alignment
11010            //ATOI(1): P (0-100)
11011            //ATOI(2): T (0-9)
11012            
11013            float *r;
11014            DST=vcalloc (1,sizeof(Sequence_data_struc));
11015            DST->A=aln2gap_cache (D1->A,0);
11016            lower_value=0;
11017            upper_value=0;
11018            D1->A=filter_aln_upper_lower (D1->A, DST->A, 0, 0);
11019            
11020            sprintf (F->mode, "%s", ((s=get_string_variable ("overaln_mode")))?s:"lower");
11021            if (!strm (F->mode, "lower") && !strm (F->mode, "unalign"))printf_exit (EXIT_FAILURE,stderr,"\nERROR: unknown overal_mode in overal output [%s] [FATAL:%s]", F->mode, PROGRAM);
11022            
11023            if (int_variable_isset ("overaln_threshold"))F->t=get_int_variable ("overaln_threshold");
11024            if (int_variable_isset ("overaln_target"))F->f=get_int_variable ("overaln_target");
11025            if (int_variable_isset ("overaln_P1"))F->f=get_int_variable ("overaln_P1");
11026            if (int_variable_isset ("overaln_P1"))F->f=get_int_variable ("overaln_P2");
11027            if (int_variable_isset ("overaln_P1"))F->f=get_int_variable ("overaln_P3");
11028            if (int_variable_isset ("overaln_P1"))F->f=get_int_variable ("overaln_P4");//F P1 P2 P3 T;
11029            
11030            D2->A=aln2clean_pw_aln (D2->A, F);
11031            r=aln2pred (D1->A, D2->A,"case_l_");
11032            fprintf ( stdout, "sn %.2f sp %.2f re %.2f\n", r[0], r[1], r[2]);
11033            exit (0);
11034          }
11035        
11036            
11037 //JM_START
11038        else if ( strm ( action, "aln2hitMat"))
11039          {
11040                 aln2hitMat(D1->A, ACTION(1));
11041                 myexit (EXIT_SUCCESS);
11042          }
11043 //JM_END
11044        
11045        else
11046          {
11047            fprintf ( stderr, "\nWARNING: ACTION %s UNKNOWN and IGNORED\n", action);
11048          }
11049      }
11050
11051
11052 void aln2mat_diaa (Sequence *S)
11053 {
11054   int a, aa1, aa2, aa3, aa4;
11055   int s1, s2, p;
11056   Alignment *A;
11057   
11058   int ****m;
11059   int **c;
11060   int naa=0;
11061   int count=0;
11062   double Delta=0.00001;
11063   int *alp;
11064   int tot,u;
11065   double observed, expected, f_diaa1, f_diaa2, v;
11066   
11067
11068   alp=vcalloc (256, sizeof (int));
11069   for (a=0; a<26; a++)alp[a+'a']=1;
11070   alp['b']=0;
11071   alp['j']=0;
11072   alp['o']=0;
11073   alp['u']=0;
11074   alp['x']=0;
11075   alp['z']=0;
11076
11077   m=declare_arrayN (4,sizeof (int),26,26,26,26);
11078   c=declare_arrayN  (2,sizeof (int),26,26);
11079   
11080   for ( a=0; a< S->nseq; a++)
11081     {
11082       fprintf ( stderr, "%s\n", S->name[a]);
11083       A=main_read_aln (S->name[a],NULL);
11084       for (s1=0; s1<A->nseq; s1++)lower_string (A->seq_al[s1]);
11085       
11086       for ( s1=0; s1<A->nseq-1; s1++)
11087         for (s2=s1+1; s2<A->nseq; s2++)
11088           {
11089             for (p=0; p<A->len_aln-1; p++)
11090               {
11091                 
11092                 u =alp[aa1=A->seq_al[s1][p]];
11093                 u+=alp[aa2=A->seq_al[s1][p+1]];
11094                 u+=alp[aa3=A->seq_al[s2][p]];
11095                 u+=alp[aa4=A->seq_al[s2][p+1]];
11096         
11097                 if ( u==4)
11098                   {
11099                     aa1-='a';aa2-='a';aa3-='a'; aa4-='a';
11100                     
11101                     c[aa1][aa2]++;
11102                     c[aa3][aa4]++;
11103                     m[aa1][aa2][aa3][aa4]++;
11104                     count+=2;
11105                   }
11106               }
11107           }
11108       free_aln (A);
11109     }
11110   fprintf ( stdout, "# DIAA_MATRIX_FORMAT_01\n");
11111   naa=26;
11112   for (aa1=0; aa1<naa; aa1++)
11113     for (aa2=0; aa2<naa; aa2++)
11114       for (aa3=0; aa3<naa; aa3++)
11115         for (aa4=0; aa4<naa;aa4++) 
11116           {
11117             u =alp[aa1+'a'];
11118             u+=alp[aa2+'a'];
11119             u+=alp[aa3+'a'];
11120             u+=alp[aa4+'a'];
11121             
11122             if ( u==4)
11123               {
11124                 tot=m[aa1][aa2][aa3][aa4]+m[aa3][aa4][aa1][aa2];
11125                 observed=((double)tot)/(double)((double)count/(double)2);
11126                 f_diaa1=(double)c[aa1][aa2]/(double)count;
11127                 f_diaa2=(double)c[aa3][aa4]/(double)count;
11128                 
11129                 expected=f_diaa1*f_diaa2;
11130                 if (expected<Delta)v=0;
11131                 else if (observed<Delta)v=-100;
11132                 else 
11133                   {
11134                     v=log(observed/expected)*10;
11135                   }
11136             // if (tot>0)fprintf ( stdout, "TEST C=%d expected=%.4f observed=%.4f v=%.4f [%d %d %d][%d] tot=%d\n", count, (float)expected, (float)observed, (float) v, c[aa1][aa2], c[aa3][aa4], count, m[aa1][aa2][aa3][aa4], tot);
11137                 fprintf ( stdout, "%c%c %c%c %d %d\n", aa1+'a', aa2+'a', aa3+'a', aa4+'a', (int)v, m[aa1][aa2][aa3][aa4]+ m[aa3][aa4][aa1][aa2]);
11138               }
11139           }
11140   exit (EXIT_SUCCESS);
11141 }
11142 void aln2mat (Sequence *S)
11143 {
11144   int a, aa1, aa3;
11145   int s1, s2, p;
11146   Alignment *A;
11147   
11148   int **m;
11149   int *c;
11150   int naa=0;
11151   int count=0;
11152   double Delta=0.00001;
11153   int *alp;
11154   int tot,u;
11155   double observed, expected, f_diaa1, f_diaa2, v;
11156   
11157
11158   alp=vcalloc (256, sizeof (int));
11159   for (a=0; a<26; a++)alp[a+'a']=1;
11160   alp['b']=0;
11161   alp['j']=0;
11162   alp['o']=0;
11163   alp['u']=0;
11164   alp['x']=0;
11165   alp['z']=0;
11166
11167   m=declare_arrayN (2,sizeof (int),26,26);
11168   c=declare_arrayN  (1,sizeof (int),26);
11169   
11170   for ( a=0; a< S->nseq; a++)
11171     {
11172       fprintf ( stderr, "%s\n", S->name[a]);
11173       A=main_read_aln (S->name[a],NULL);
11174       for (s1=0; s1<A->nseq; s1++)lower_string (A->seq_al[s1]);
11175       
11176       for ( s1=0; s1<A->nseq-1; s1++)
11177         for (s2=s1+1; s2<A->nseq; s2++)
11178           {
11179             for (p=0; p<A->len_aln-1; p++)
11180               {
11181                 
11182                 u =alp[aa1=A->seq_al[s1][p]];
11183                 u+=alp[aa3=A->seq_al[s2][p]];
11184                 
11185                 if ( u==2)
11186                   {
11187                     aa1-='a';aa3-='a';
11188                     
11189                     c[aa1]++;
11190                     c[aa3]++;
11191                     m[aa1][aa3]++;
11192                     count+=2;
11193                   }
11194               }
11195           }
11196       free_aln (A);
11197     }
11198   fprintf ( stdout, "# MONOAA_MATRIX_FORMAT_01\n");
11199   naa=26;
11200   for (aa1=0; aa1<naa; aa1++)
11201       for (aa3=0; aa3<naa; aa3++)
11202           {
11203             u =alp[aa1+'a'];
11204             u+=alp[aa3+'a'];
11205             
11206             if ( u==2)
11207               {
11208                 tot=m[aa1][aa3]+m[aa3][aa1];
11209                 observed=((double)tot)/(double)((double)count/(double)2);
11210                 f_diaa1=(double)c[aa1]/(double)count;
11211                 f_diaa2=(double)c[aa3]/(double)count;
11212                 
11213                 expected=f_diaa1*f_diaa2;
11214                 if (expected<Delta)v=0;
11215                 else if (observed<Delta)v=-100;
11216                 else 
11217                   {
11218                     v=log(observed/expected)*10;
11219                   }
11220             // if (tot>0)fprintf ( stdout, "TEST C=%d expected=%.4f observed=%.4f v=%.4f [%d %d %d][%d] tot=%d\n", count, (float)expected, (float)observed, (float) v, c[aa1][aa2], c[aa3][aa4], count, m[aa1][aa2][aa3][aa4], tot);
11221                 fprintf ( stdout, "%c %c %d %d\n", aa1+'a', aa3+'a', (int)v, m[aa1][aa3]+ m[aa3][aa1]);
11222               }
11223           }
11224   exit (EXIT_SUCCESS);
11225 }           
11226         
11227
11228 int **seq2latmat ( Sequence *S, char *fname)
11229 {
11230   int a, b, r0, r1;
11231   char *aa;
11232   int naa;
11233   int *count, tot;
11234   int **mat;
11235   double observed, expected;
11236   FILE *fp;
11237
11238   fp=vfopen (fname, "w");
11239   
11240   count=vcalloc ( 256, sizeof (int));
11241   mat=declare_int (256, 256);
11242   
11243   naa=strlen ( BLAST_AA_ALPHABET);
11244   aa=vcalloc ( naa+2, sizeof (char));
11245   sprintf ( aa, "%s", BLAST_AA_ALPHABET);
11246   lower_string (aa);
11247   
11248   for ( tot=0,a=0; a< S->nseq; a++)
11249     {
11250       ungap (S->seq[a]);
11251       for ( b=1; b<S->len[a]; b++)
11252         {
11253           r0=tolower(S->seq[a][b-1]);
11254           r1=tolower(S->seq[a][b]);
11255           
11256           mat[r0][r1]++;
11257           //count[r0]++;
11258           count[r1]++;
11259           tot++;
11260         }
11261     }
11262   for ( a=0; a< naa; a++)
11263     for (b=0; b< naa; b++)
11264       {
11265         if ( aa[a]=='*' || aa[b]=='*');
11266         else
11267           {
11268             expected=((double)count[(int)aa[a]]/(double)tot)* ((double)count[(int)aa[b]]/(double)tot)*(double)tot;
11269             observed=((double)mat[(int)aa[a]][(int)aa[b]]);
11270         
11271             /*
11272               fprintf ( stderr, "\n%c=%d %c=%d Tot=%d Obs=%d Exp=%d\n", aa[a],count[aa[a]], aa[b],count[aa[b]],tot, mat[aa[a]][aa[b]],(int)expected);  
11273               fprintf ( stderr, "\n%d", mat[aa[a]][aa[b]]);
11274               fprintf ( stderr, "\n%d", mat[aa[a]][aa[b]]);
11275             */
11276             mat[(int)aa[a]][(int)aa[b]]=(expected==0 || observed==0)?0:((int)10*log((observed/expected)));
11277           }
11278       }
11279  
11280   fprintf (fp,"# BLAST_MATRIX FORMAT\n#ALPHABET=%s\n#TRANSITION MATRIX TRAINED ON %d Sequence\n#", BLAST_AA_ALPHABET, S->nseq);
11281   for (a=0; a< naa; a++)fprintf ( fp, "%3c ", toupper(aa[a]));
11282   fprintf (fp,"\n");
11283   for (a=0; a< naa; a++)
11284     {
11285       
11286       fprintf (fp, "%c", toupper(aa[a]));
11287       for ( b=0; b< naa; b++)
11288         {
11289           fprintf (fp, "%3d ", mat[(int)aa[a]][(int)aa[b]]);
11290         }
11291       fprintf ( fp, "\n");
11292     }
11293   vfclose (fp);
11294   vfree (count);
11295   vfree (aa);
11296   
11297   return mat;
11298 }
11299  
11300 double* mat2cmp ( int **mat1, int **mat2)
11301 {
11302   int a, b, n, x, y;
11303   double **list, *r;
11304   if ( !mat1 || !mat2)
11305     {
11306       fprintf ( stderr, "\nERROR: mat2cmp needs two matrices [FATAL:%s]", PROGRAM);
11307       myexit (EXIT_FAILURE);
11308     }
11309   
11310   for (n=0, a=0; a< 256; a++)
11311     for ( b=0; b<256; b++)
11312       {
11313         x=mat1[a][b];
11314         y=mat2[a][b];
11315         if ( x|| y)n++;
11316       }
11317   if ( n==0) return 0;
11318   list=declare_double (n, 2);
11319   
11320   for (n=0, a=0; a<256; a++)
11321     for ( b=0; b<256; b++)
11322       {
11323         x=mat1[a][b];
11324         y=mat2[a][b];
11325         if ( x || y)
11326           {
11327             list[n][0]=x;
11328             list[n][1]=y;
11329             n++;
11330           }
11331       }
11332   r=return_r (list, n);
11333   free_double(list, -1);
11334   return r;
11335 }
11336    
11337 int ** read_blast_matrix ( char *mat_name)
11338         {
11339         FILE *fp;
11340         int n_aa,aa1, aa2;
11341         int a, b, c;
11342         int **matrix;
11343         int value;
11344         char sbuf[VERY_LONG_STRING];
11345         char buf[2];
11346         char alp[257];
11347
11348         matrix=declare_int (256,256);
11349         vfree ( matrix[30]);
11350         matrix[30]=vcalloc(10000, sizeof (int));
11351         fp=vfopen ( mat_name, "r");
11352         while ( (c=fgetc(fp))=='#' || isspace(c) )
11353           {
11354             char *p;
11355             fgets ( sbuf, VERY_LONG_STRING, fp);
11356             if ( (p=strstr (sbuf, "ALPHABET")))
11357               sscanf (p, "ALPHABET=%s", alp);
11358         }
11359         ungetc(c, fp);
11360         lower_string (alp);
11361         n_aa=strlen (alp);
11362
11363         for ( a=0; a< n_aa; a++)
11364             {
11365             fscanf ( fp, "%s ", buf);
11366
11367             aa1=tolower(buf[0]);
11368             
11369             if ( aa1!=alp[a])
11370                 {
11371                 fprintf ( stderr, "\nParsing_error when reading blast_matrix %s:\n%c %c",mat_name, aa1,alp[a]);
11372                 fprintf ( stderr, "\n%c ", fgetc(fp));
11373                 myexit (EXIT_FAILURE);
11374                 }
11375             for ( b=0; b<n_aa; b++)
11376                 {
11377                 aa2=tolower ((char) alp[b]);
11378                 fscanf ( fp, "%d ", &value);
11379                 if (is_gap(aa1) || is_gap(aa2))
11380                   {
11381                     int c1, c2;
11382                     c1=(is_gap(aa1))?GAP_CODE:aa1;
11383                     c2=(is_gap(aa2))?GAP_CODE:aa2;
11384                     if ( c1==GAP_CODE && c2==GAP_CODE)
11385                       matrix[c1][c2]=value;
11386                     else if ( c1==GAP_CODE)
11387                       {
11388                         matrix[c1][tolower(c2)]=value;
11389                         matrix[c1][toupper(c2)]=value;
11390                       }
11391                     else
11392                       {
11393                         matrix[tolower(c1)][c2]=value;
11394                         matrix[toupper(c1)][c2]=value;
11395                       }
11396                   }
11397                 else if ( aa1!='*' && aa2!='*')
11398                   {
11399                     matrix[tolower(aa1)-'A'][tolower(aa2)-'A']=value;
11400                     matrix[toupper(aa1)-'A'][toupper(aa2)-'A']=value;
11401                     matrix[tolower(aa1)-'A'][toupper(aa2)-'A']=value;
11402                     matrix[toupper(aa1)-'A'][tolower(aa2)-'A']=value;
11403                   }
11404                 }
11405             fscanf(fp, "\n");
11406             }
11407         fclose (fp);
11408
11409         return matrix;
11410         }
11411
11412 int output_blast_mat (int **mat, char *fname)
11413 {
11414   return output_mat(mat, fname, BLAST_AA_ALPHABET, 'A');
11415   
11416 }
11417                       
11418 int output_mat (int **mat, char *fname, char *alp, int offset)
11419 {
11420   char *aa;
11421   int a,b, naa;
11422   FILE *fp;
11423
11424
11425   
11426   naa=strlen (alp);
11427   aa=vcalloc ( naa+2, sizeof (char));
11428   sprintf ( aa, "%s",alp);
11429   lower_string (aa);
11430   if (!(fp=vfopen (fname, "w")))return 0;
11431   fprintf (fp,"# BLAST_MATRIX FORMAT\n#ALPHABET=%s\n",alp);
11432   for (a=0; a< naa; a++)fprintf ( fp, "%3c ", toupper(aa[a]));
11433   fprintf (fp,"\n");
11434   for (a=0; a< naa; a++)
11435     {
11436       
11437       fprintf (fp, "%c", toupper(aa[a]));
11438       for ( b=0; b< naa; b++)
11439         {
11440           fprintf (fp, " %5d", mat[aa[a]-offset][aa[b]-offset]);
11441         }
11442       fprintf ( fp, "\n");
11443     }
11444   vfree (aa);
11445   vfclose (fp);
11446   return 1;
11447 }               
11448
11449 void output_pavie_mat (int **mat, char *fname, double gep, char *alp)
11450 {
11451   int n, a, b;
11452   FILE *fp;
11453   
11454   n=strlen (alp);
11455   fp=vfopen (fname, "w");
11456   fprintf (fp,"# PAVIE_MATRIX FORMAT\n#ALPHABET=%s\n",alp);
11457   
11458   for(a=0; a< n; a++)
11459      {
11460        for ( b=a; b<n; b++)
11461         {         
11462           fprintf (fp, "%c %c %.3f\n", toupper(alp[a]), toupper(alp[b]), (float)mat[alp[a]-'A'][alp[b]-'A']/PAVIE_MAT_FACTOR);
11463         }
11464      }
11465    if ( gep!=UNDEFINED)fprintf ( fp, "- - %.3f\n", gep/PAVIE_MAT_FACTOR);
11466    vfclose(fp);
11467  }
11468
11469 int ** read_pavie_matrix ( char *mat_name)
11470         {
11471         FILE *fp;
11472         int c, n_aa;
11473         char aa1, aa2;
11474         float v;
11475         int **matrix;
11476         char sbuf[VERY_LONG_STRING];
11477         char alp[257];
11478         int gep=UNDEFINED;
11479         
11480         matrix=declare_int (256,256);
11481         
11482         
11483         fp=vfopen ( mat_name, "r");
11484         while ( (c=fgetc(fp))=='#' || isspace(c) )
11485           {
11486             fgets ( sbuf, VERY_LONG_STRING, fp);
11487             if ( sscanf (sbuf, "ALPHABET=%s", alp)==1);
11488           }
11489         ungetc(c, fp);
11490         
11491         n_aa=strlen (alp);
11492         while ( fgets ( sbuf, VERY_LONG_STRING, fp)!=NULL)
11493             {
11494               aa1=aa2='Z';
11495               if (sscanf (sbuf, "%c %c %f",&aa1, &aa2, &v)==3)
11496                 {
11497                   v*=PAVIE_MAT_FACTOR;
11498                   if (aa1=='-' && aa2=='-')gep=v;
11499                   else
11500                     {
11501                       matrix[tolower(aa1)-'A'][tolower(aa2)-'A']=v;
11502                       matrix[toupper(aa1)-'A'][toupper(aa2)-'A']=v;
11503                       matrix[tolower(aa1)-'A'][toupper(aa2)-'A']=v;
11504                       matrix[toupper(aa1)-'A'][tolower(aa2)-'A']=v;
11505                       
11506                       matrix[tolower(aa2)-'A'][tolower(aa1)-'A']=v;
11507                       matrix[toupper(aa2)-'A'][toupper(aa1)-'A']=v;
11508                       matrix[tolower(aa2)-'A'][toupper(aa1)-'A']=v;
11509                       matrix[toupper(aa2)-'A'][tolower(aa1)-'A']=v;
11510                     }             
11511               }
11512             }
11513         if ( gep!=UNDEFINED)
11514           {
11515             int a;
11516             for (a=0; a< n_aa; a++)
11517               {
11518                 if (!matrix[tolower(alp[a])-'A'][GAP_CODE])
11519                   {
11520                     matrix[tolower(alp[a])-'A'][GAP_CODE]=gep;
11521                     matrix[toupper(alp[a])-'A'][GAP_CODE]=gep;
11522                   }
11523               }
11524           }
11525         vfclose (fp);
11526         return matrix;
11527         }
11528
11529 Sequence *seq2year ( Sequence *S, int modulo)
11530 {
11531   int a, b, y;
11532   int first;
11533   char *s;
11534   char new_channel[100];
11535   
11536   sprintf( new_channel, "_agechannel%d",modulo);
11537   
11538   for ( a=0; a<S->nseq; a++)
11539     {
11540       if (S->seq_comment[a] && (s=strstr(S->seq_comment[a], "_FIRSTYEAR")))
11541         {
11542           sscanf (s, "_FIRSTYEAR%d_", &first);
11543         }      
11544       else first=1;
11545      
11546       for ( y=first,b=0; b<S->len[a]; b++)
11547         {
11548           if ( !is_gap(S->seq[a][b]))
11549             {
11550               S->seq[a][b]='a'+((y/modulo))%10;
11551               y++;
11552             }
11553         }
11554       if ( (s=strstr ( S->name[a], "_agechannel")))
11555            {
11556              sprintf ( s, "%s", new_channel);
11557            }
11558       else strcat (S->name[a], new_channel);
11559     }
11560   return S;
11561 }
11562
11563 Sequence* output_n_pavie_age_channel (Sequence *S, char *name, int n)
11564 {
11565   int x, a;
11566   if (!n)n=2;
11567   
11568
11569   for ( x=1,a=0; a< n; a++, x*=10)
11570     {
11571       S=output_pavie_age_channel(S, name,x);
11572     }
11573 return S;
11574 }
11575   
11576
11577
11578
11579 Sequence* output_pavie_age_channel (Sequence *S, char *name, int modulo)
11580   {
11581     Alignment *A;
11582     FILE *fp;
11583     static int display;
11584     char mat_list_name[100];
11585     char seq_list[1000];
11586     char mat_name[1000];
11587     char *tmp;
11588     
11589     sprintf ( mat_list_name, "%s_pavie_age_matrix.mat_list", name);
11590     sprintf (seq_list, "%s_age_channel.fasta",name);
11591     
11592     if ( display==0 )
11593       {
11594         if (check_file_exists(seq_list))vremove (seq_list);
11595         if (check_file_exists(mat_list_name))vremove (mat_list_name);
11596       }
11597     sprintf (mat_name, "%s_age_mat_mod%d.mat",name, modulo);
11598     output_age_matrix ( mat_name, modulo);
11599       
11600     fp=vfopen  ( mat_list_name,"a"); 
11601     fprintf ( fp, "%s\n", mat_name);
11602     vfclose ( fp);
11603     
11604     S=seq2year (S,modulo);
11605     A=seq2aln (S, NULL, KEEP_GAP);
11606     output_fasta_seq (tmp=vtmpnam (NULL),A);
11607     file_cat ( tmp, seq_list);
11608         
11609     if ( display==0)
11610       {
11611         display_output_filename ( stdout, "AGE_MAT_LIST", "MAT_LIST", mat_list_name, CHECK);
11612         display_output_filename ( stdout, "AGE_SEQ", "FASTA", seq_list, CHECK);
11613         display=1;
11614       }
11615     fprintf ( stderr, "\nModulo:%d years", modulo); 
11616     fprintf ( stderr, "\n");
11617     free_aln (A);
11618     return S;
11619   }
11620 //
11621 // Name MAnipulation
11622 //
11623
11624 Alignment *clean_aln (Alignment *A)
11625 {
11626   if ( A)
11627     {
11628       A->seq_comment=clean_string (A->nseq, A->seq_comment);
11629       A->aln_comment=clean_string (A->nseq, A->aln_comment);
11630       A->name=translate_names(A->nseq, A->name);
11631       (A->S)=clean_sequence ((A->S));
11632     }
11633   return A;
11634 }
11635 Sequence *clean_sequence ( Sequence *S)
11636 {
11637   if ( !S) return S;
11638   
11639   S->seq_comment=clean_string (S->nseq, S->seq_comment);
11640   S->name=translate_names(S->nseq, S->name);
11641   return S;
11642 }
11643 char ** translate_names (int n, char **name)
11644 {
11645   int a;
11646   for ( a=0; a<n; a++)
11647     name[a]=translate_name(name[a]);
11648   return name;
11649 }
11650 char * translate_name ( char *name)
11651         {
11652
11653         int len;
11654         int a;
11655         char buf[1000];
11656         
11657         len=strlen (name);
11658                 
11659         if ( name[0]=='\'')return name;
11660         
11661         for ( a=0; a<len; a++)
11662                 {
11663                 if ( isspace(name[a]))name[a]='\0';
11664                 else if (strchr (";(),:#><", name[a]))name[a]='_';
11665                 
11666                 }
11667         sprintf (buf,"%s",decode_name (name, DECODE));
11668         if ( strlen (buf)>read_array_size_new ((char *)name))
11669           {
11670             name=vrealloc (name, sizeof (char)*(strlen (buf)+1));
11671           }
11672         sprintf (name, "%s", buf);
11673         
11674         return name;
11675         }
11676 char *decode_name (char *name, int mode)
11677 {
11678   static char ***name_list;
11679   static int n;
11680   static char tag[100];
11681   int a;
11682   
11683   if (mode==CLEAN)
11684     {
11685       for (a=0; a<n; a++)
11686         {
11687           vfree (name_list[a][0]);
11688           vfree (name_list[a][1]);
11689           vfree (name_list[a]);
11690         }
11691       vfree (name_list);
11692       tag[0]='\0';
11693     }
11694   
11695   //spacial modes
11696   if ( mode == CODELIST)
11697     {
11698       char *file;
11699       file=vtmpnam (NULL);
11700       for (a=0; a< n; a++)
11701         printf_file(file, "a", "#CODE: %s <=> %s\n", name_list[a][0], name_list[a][1]);
11702       return file;
11703     }
11704   if (mode ==DECODE && name_list==NULL)return name;
11705   if ( name==NULL) return name;
11706   
11707         
11708   
11709   if (!tag[0])
11710     {
11711       vsrand (0);
11712       sprintf ( tag, "TCTAG_%d",rand ()%100000);
11713     }
11714   
11715   if ( mode==CODE)
11716     {
11717       for (a=0; a< n; a++)
11718         if ( strm (name, name_list[a][0]))return name_list[a][1];
11719      
11720
11721       name_list=realloc (name_list, sizeof (char**)*(n+1));
11722       name_list[n]=vcalloc (2, sizeof (char*));
11723       name_list[n][0]=vcalloc (strlen (name)+1, sizeof (char));
11724       name_list[n][1]=vcalloc (100, sizeof (char));
11725       sprintf ( name_list[n][0], "%s", name);
11726       sprintf ( name_list[n][1], "%s_%d", tag,n+1);
11727       return name_list[n++][1];
11728     }
11729   else if ( mode ==DECODE)
11730     {
11731       char *p;
11732       int i;
11733       if ( !(p=after_strstr (name, tag)))return name;
11734       else 
11735         {
11736           sscanf (p, "_%d", &i);
11737           return name_list[i-1][0];
11738         }
11739     }
11740   else 
11741     {
11742       printf_exit (EXIT_FAILURE, stderr,"Unknown Mode for Decode_name [FATAL:%s]", PROGRAM);
11743     }
11744   return NULL;
11745 }
11746       
11747
11748 FILE * display_sequences_names (Sequence *S, FILE *fp, int check_pdb_status, int print_templates)
11749         {
11750             int a;
11751             int max_len;
11752             char *r;
11753             
11754             if ( !S)
11755                {
11756                    fprintf (fp,"\nERROR: NO SEQUENCE READ [FATAL:%s]\n", PROGRAM); myexit (EXIT_FAILURE);
11757                }
11758             for ( a=0, max_len=0; a< S->nseq; a++)max_len=MAX(max_len, strlen (S->name[a]));
11759             fprintf ( fp, "\nINPUT SEQUENCES: %d SEQUENCES  [%s]", S->nseq,(S->type)?S->type:"Unknown type");
11760             for ( a=0; a< S->nseq; a++)
11761                 {
11762                   fprintf (fp, "\n  Input File %-*s Seq %-*s Length %4d type %s",max_len,S->file[a], max_len,S->name[a],(int)strlen ( S->seq[a]), S->type);
11763                     if (check_pdb_status)
11764                       {
11765                         if ((r=seq_is_pdb_struc (S, a)))fprintf (fp, " Struct Yes PDBID %s", get_pdb_id(r));
11766                         else fprintf (fp, " Struct No");
11767                         /*
11768                         if (is_pdb_struc (S->name[a])||is_pdb_struc (S->file[a]) )fprintf (fp, " Struct Yes");
11769                         else fprintf (fp, " Struct No");
11770                         */
11771                       }
11772                     else fprintf (fp, " Struct Unchecked");
11773                     if ( print_templates)fp=display_sequence_templates (S, a, fp);
11774                     
11775
11776                 }
11777             fprintf ( fp, "\n");
11778             return fp;
11779             
11780         }
11781 Sequence *add_file2file_list (char *name, Sequence *S)
11782 {
11783   
11784   if (!S) S=declare_sequence (1,1,10);
11785   else S=realloc_sequence   (S,S->nseq+1,0);S->nseq=0;
11786  
11787   sprintf ( S->name[S->nseq++], "%s", name);
11788   return S;
11789
11790 }
11791 /*********************************COPYRIGHT NOTICE**********************************/
11792 /*© Centro de Regulacio Genomica */
11793 /*and */
11794 /*Cedric Notredame */
11795 /*Tue Oct 27 10:12:26 WEST 2009. */
11796 /*All rights reserved.*/
11797 /*This file is part of T-COFFEE.*/
11798 /**/
11799 /*    T-COFFEE is free software; you can redistribute it and/or modify*/
11800 /*    it under the terms of the GNU General Public License as published by*/
11801 /*    the Free Software Foundation; either version 2 of the License, or*/
11802 /*    (at your option) any later version.*/
11803 /**/
11804 /*    T-COFFEE is distributed in the hope that it will be useful,*/
11805 /*    but WITHOUT ANY WARRANTY; without even the implied warranty of*/
11806 /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the*/
11807 /*    GNU General Public License for more details.*/
11808 /**/
11809 /*    You should have received a copy of the GNU General Public License*/
11810 /*    along with Foobar; if not, write to the Free Software*/
11811 /*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA*/
11812 /*...............................................                                                                                      |*/
11813 /*  If you need some more information*/
11814 /*  cedric.notredame@europe.com*/
11815 /*...............................................                                                                                                                                     |*/
11816 /**/
11817 /**/
11818 /*      */
11819 /*********************************COPYRIGHT NOTICE**********************************/