Mac binaries
[jabaws.git] / website / archive / binaries / mac / 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_column.....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                 fprintf ( stdout, "\n     +orthologous_cat..<mode>: mode=voronoi or nothing");
199                 fprintf ( stdout, "\n     ......................-in: sequences from different species");
200                 fprintf ( stdout, "\n     ..................... -in2: list of species in fasta");
201                 fprintf ( stdout, "\n     ..................... sequence must be named: <species>_<genename>");
202                 fprintf ( stdout, "\n     ..................... all paralogues will be concatenated");
203                 
204                 fprintf ( stdout, "\n     +aln2replicate N name");
205                 fprintf ( stdout, "\n     ..................... Generates N replicates in Fasta");
206                 fprintf ( stdout, "\n     ..................... Voronoi weights can be used");
207                                 
208                 fprintf ( stdout, "\n     +msalist2cat_pwaln.min..max");
209                 fprintf ( stdout, "\n     .....................extract all pw projections and conctaenates those\n");
210                 fprintf ( stdout, "\n     .....................where id>=min and id<=max\n");
211                 fprintf ( stdout, "\n     .....................min and max can be omitted (min=0, max=100)\n");
212                 
213                 fprintf ( stdout, "\n     +seq2blast <matrix>..gather all possible homologues from NR (EBI BLAST)");
214                 fprintf ( stdout, "\n     +seq2msa <matrix>....makes a standard progressive alignment using matrix");
215                 fprintf ( stdout, "\n     +realign_block <c1> <c2> <pg>");
216                 fprintf ( stdout, "\n     .....................Realign column c1 to c2 (non inc.) with pg)");
217                 fprintf ( stdout, "\n     .....................pg reads fasta and outputs fasta");
218                 fprintf ( stdout, "\n     .....................pg -infile=<infile> -outfile=<outfile>");
219                 fprintf ( stdout, "\n     +extract_seq seq_name (start end seq_name start end...) | filename");
220                 fprintf ( stdout, "\n     .....................seq_name='*': every seq");
221                 fprintf ( stdout, "\n     .....................start='*'   : real start");
222                 fprintf ( stdout, "\n     .....................end='*'     : real end");
223                 fprintf ( stdout, "\n     .....................filename: fasta format");
224                 fprintf ( stdout, "\n     +extract_seq_list name1 name2");
225                 fprintf ( stdout, "\n     .....................Extracts entire sequences");
226                 fprintf ( stdout, "\n     +remove_seq sn1 sn2..Removes sequences sn1, sn2...");
227                 fprintf ( stdout, "\n     +remove_seq empty....Removes empty sequences (gap only)");
228                 fprintf ( stdout, "\n     +remove_seq unique...Remove all multiple occurences except the first");
229                 fprintf ( stdout, "\n     +thread_profile_on_msa <file>");
230                 fprintf ( stdout, "\n     .....................Threads a list of profiles on corresponding seq");
231                 fprintf ( stdout, "\n     .....................File: >seqname _R_ <msa file> [nlines]");
232                 
233                 fprintf ( stdout, "\n     +thread_dna_on_prot_aln");
234                 fprintf ( stdout, "\n     .....................-in DNA.seq and -in2 AA.aln"); 
235                 fprintf ( stdout, "\n     +thread_struc_on_aln");
236                 fprintf ( stdout, "\n     .....................-in structure and -in2 aln"); 
237                 fprintf ( stdout, "\n     +use_cons............Use the consensus for n[SWITCH]");
238                 fprintf ( stdout, "\n     +upper.n|[n1-n2].....n omitted sets everything to upper case");
239                 fprintf ( stdout, "\n     .....................To use n: provide a number_aln via:");
240                 fprintf ( stdout, "\n     .....................-struc_in <number_file> -struc_in_f number_aln");
241                 fprintf ( stdout, "\n     .....................if use_cons is set n, is read on the cons");
242                 fprintf ( stdout, "\n     .....................n: will upper every residue with a value of n in struc_in");
243                 fprintf ( stdout, "\n     .....................[n1-n2]: upper residues between n1 and n2");
244                 fprintf ( stdout, "\n     +lower  n|[n1-n2]....See +upper");
245                 fprintf ( stdout, "\n     +switchcase  n|[n1-n2]See +upper");
246                 fprintf ( stdout, "\n     +color_residue <seq> <pos> <color> | file");
247                 fprintf ( stdout, "\n     .....................File: seq_name pos color");
248                 fprintf ( stdout, "\n     .....................color: 0-9");
249                 fprintf ( stdout, "\n     +edit_residue <seq> <pos> <edit> | file");
250                 fprintf ( stdout, "\n     .....................File: seq_name pos color");
251                 fprintf ( stdout, "\n     .....................edit: upper|lower|symbol");        
252                 
253                 
254                 
255                 fprintf ( stdout, "\n     +keep   n|[n1-n2]....Only keep residues that have a score between n1 and n2");
256                 
257                 fprintf ( stdout, "\n     +invert..............Inverts the sequences: CAT => TAC");
258                 fprintf ( stdout, "\n     +rotate name         Rotate an MSA, names each sequence name_col#");
259                 fprintf ( stdout, "\n     +convert n|[n1-n2] s1 s2 ....");
260                 fprintf ( stdout, "\n     +merge_annotation.... ");
261                 
262                 fprintf ( stdout, "\n     .....................Converts residues with your alignment");
263                 fprintf ( stdout, "\n     .....................similar to upper");
264                 fprintf ( stdout, "\n     .....................s1: ABCDe turns every ABCD into e");
265                 fprintf ( stdout, "\n     .....................s1: #e turns any residue into e");
266                 fprintf ( stdout, "\n     aln2short_aln L C S..Turns sequences into shorter sequences");
267                 fprintf ( stdout, "\n     .....................L: list of residues to keep");
268                 fprintf ( stdout, "\n     .....................S: Size of Streches replaced by symbol C");
269
270                                 
271                 fprintf ( stdout, "\n     +random n l..........Generates N random sequences of len l");
272                 fprintf ( stdout, "\n     .....................You must provide a file with -in");
273                 fprintf ( stdout, "\n     +count n|[n1-n2] s1 s2....");
274                 fprintf ( stdout, "\n     .....................Counts residues with your alignment");
275                 fprintf ( stdout, "\n     .....................similar to convert");
276                 fprintf ( stdout, "\n     +print_format........prints the format name");
277                 fprintf ( stdout, "\n     +keep_name...........Keep the original sequence name on extraction");
278                 
279                 fprintf ( stdout, "\n     +remove_aa pos Ml Ncycle Random_len");
280                 fprintf ( stdout, "\n     .....................Randomly modifies an alignment");
281                 fprintf ( stdout, "\n     .....................pos=0: chosen randomly");
282                 fprintf ( stdout, "\n     .....................MaxLen of the deletions, Ncycle: number of cycles");
283                 fprintf ( stdout, "\n     .....................Random_len: 0 sets the len to maxlen, 1 to a random value");
284                 fprintf ( stdout, "\n     +remove_nuc.x........Remove Position 1, 2 or 3 of every codon"); 
285                 fprintf ( stdout, "\n     +evaluate matrix..gop..gep");
286                 fprintf ( stdout, "\n     .....................Make a similarity evaluation with matrix");
287                 fprintf ( stdout, "\n     .....................use -output=score_ascii, or score_html.");
288                 fprintf ( stdout, "\n     .....................You can filter on the values");
289                 fprintf ( stdout, "\n     +evaluate matrix..gop..gep");
290                 fprintf ( stdout, "\n     .....................Make an SP evaluation with matrix");
291                 fprintf ( stdout, "\n     .....................Uses Natural Gap penalties");
292                 fprintf ( stdout, "\n     .....................gop and gep must be negative");
293                 fprintf ( stdout, "\n     .....................use -output=color_ascii, color_html to get a color display");
294
295                 fprintf ( stdout, "\n.....+evaluate_lat........Make a lateral evaluation with matrix");
296                 fprintf ( stdout, "\n     +msa_weight proc.....Computes weights using the procedure");
297                 fprintf ( stdout, "\nRNA analysis Post Processing___________________________________________________");
298                 fprintf ( stdout, "\n     +aln2alifold.........Turns the MSA into a consensus structure");
299                 fprintf ( stdout, "\n     +add_alifold.........adds an alifold consensus structure");
300
301                 fprintf ( stdout, "\n     +alifold2analyze.mode..mode=stat_cache_list_aln_color_html_ps_usegap");
302                 fprintf ( stdout, "\n     .......................stat: compile Number of compensated mutations");
303                 fprintf ( stdout, "\n     .......................cache: ascii-code compensated mutations on aln");
304                 fprintf ( stdout, "\n     .......................html: color-code compensated mutations on aln");
305                 fprintf ( stdout, "\n     .......................aln: mark compensated mutations on stockholm aln");
306                 fprintf ( stdout, "\n     .......................usegap: do not ignore positions with gaps");
307                 
308                 fprintf ( stdout, "\n     +RNAfold_cmp.........compares the sec struc of in1 and in2 (computes them with alifold if missing)");
309                                 
310                 fprintf ( stdout, "\nMSA Post Processing___________________________________________________");
311                         fprintf ( stdout, "\n     +force_aln filename|seq1 res1 seq2 res2");
312                 fprintf ( stdout, "\n     .....................Forces residue 1 of seq1 to be aligned with res2 of seq 2");
313                 fprintf ( stdout, "\n     .....................In a file, there must be one pair of interaction/line");
314                 fprintf ( stdout, "\n     +sim_filter[_aln_Ix_iy_Cz_cw <seq>");
315                 fprintf ( stdout, "\n     ....................._<unaln or aln>, aln is assumed");
316                 fprintf ( stdout, "\n     ....................._I max identity to seq");
317                 fprintf ( stdout, "\n     ....................._i min identity to seq");
318                 fprintf ( stdout, "\n     ....................._C max cov on seq");
319                 fprintf ( stdout, "\n     ....................._c min cov on seq");
320                 fprintf ( stdout, "\n     +trim[_aln_%%%%50_n111_N50_T_Fn_fS_pS_max_sim_P0_K0] [string2]");
321                 fprintf ( stdout, "\n     ....................._<seq or aln>, aln is assumed");
322                 fprintf ( stdout, "\n     ....................._%%%%<max/min_percent_similarity>");
323                 fprintf ( stdout, "\n     ....................._max Or _min <keep sequences for which sim is the max or the min [Def: _max>");
324                 fprintf ( stdout, "\n     ....................._cov Or _sim Filter according to the coverage [Def: _sim]");
325                 fprintf ( stdout, "\n     ....................._n<max_number_of_sequence>       ");
326                 fprintf ( stdout, "\n     ....................._N<percent_of_sequences_to_keep>");
327                 fprintf ( stdout, "\n     ....................._T Reorder the sequences according to a tree BEFORE triming");
328                 fprintf ( stdout, "\n     ....................._Fn Keep only sequences that have AT LEAST ONE residue aligned");
329                 fprintf ( stdout, "\n     ......................in the n first and n last columns. ");
330                 fprintf ( stdout, "\n     ....................._O<min sim> Remove outlayers that have less than min average sim with other sequences"); 
331                 fprintf ( stdout, "\n     ....................._Kn Forces the n top sequences to be kept");
332                 fprintf ( stdout, "\n     ....................._P_ Print a summary in stderr");
333                 
334                 
335                 fprintf ( stdout, "\n     .....................Keeping Sequences: Sequences provided via -in2 will be kept");
336                 
337                 fprintf ( stdout, "\n     .....................Keeping Sequences: Sequences whose name contains <string> in field fS will be kept");
338                 fprintf ( stdout, "\n     ....................._f<NAME|SEQ|COMMENT> designates a field"); 
339                 fprintf ( stdout, "\n     .....................<string> is a Perl regular expression");
340                 fprintf ( stdout, "\n     +aln2unalign Mode Penalty Threshold");
341                 fprintf ( stdout, "\n     .....................Identifies all the streches less conserved than than the average");
342                 fprintf ( stdout, "\n     .....................Mode: lower|number|unalign Act on all the resiues withs score<Thres");
343                 fprintf ( stdout, "\n     .....................Penalty: FSA penalty align2unalign, Def=90");
344                 fprintf ( stdout, "\n     .....................Threshold: Fraction of unaligned residues(0-9) Def=2");
345                 
346                 fprintf ( stdout, "\n     +clean_cdna..........Undocumented"); 
347                 fprintf ( stdout, "\n     +clean_maln..........Undocumented"); 
348                 fprintf ( stdout, "\nTree Analysis___________________________________________________");
349                 
350         
351                 fprintf ( stdout, "\n     +tree_prune..........Prune the tree -in using the sequences provided via -in2");
352                 fprintf ( stdout, "\n     +tree_cmp............Compares the tree -in and the tree -in2");
353                 fprintf ( stdout, "\n     +tree_cmp_list......Compares the tree -in and the tree_list -in2");
354                 fprintf ( stdout, "\n     .....................Sets the support as boostrap value in the -in tree");
355                 
356                 fprintf ( stdout, "\n     .....................-in and -in2 can contain different taxons");
357                 fprintf ( stdout, "\n     +tree_scan.P1..P2.....scans alignment <-in> with tree <-in2>)");
358                 fprintf ( stdout, "\n     ......................+tree_scan help to get P1 information");
359                 fprintf ( stdout, "\n     ......................+aln2tree help to get P2 information");
360                 
361                 fprintf ( stdout, "\n     .....................-in and -in2 can contain different taxons");
362                 fprintf ( stdout, "\n     +tree2node.......... Reports the node list along with the split");
363                 fprintf ( stdout, "\n     ..................... splits can be described with the seq order ");
364                 fprintf ( stdout, "\n     ..................... provided via -in3=<sequence> ");
365                 
366                 fprintf ( stdout, "\n     +treelist2groups.N....count all topologies within a list of trees");
367                 fprintf ( stdout, "\n     .....................-in is in fasta format with each name being a newick file");
368                 fprintf ( stdout, "\n     .....................-in2 can be a list of sequences used to trim the trees");
369                 fprintf ( stdout, "\n     ......................N can be used to unresolve the trees with Depth N");
370                 fprintf ( stdout, "\n     +treelist2lti.N.C.....Reports the average stability of each sequence neighborhood");
371                 fprintf ( stdout, "\n     ......................Species can be selected via -in2 [Fasta file with Taxon names]");
372                 fprintf ( stdout, "\n     ......................OR the sequences observed in C%% of the files are kept [Def: C=100]");
373                 
374                 
375                 fprintf ( stdout, "\n     +treelist2seq.C.......Reports the species observed in C%% of the trees");
376                 fprintf ( stdout, "\n     +treelist2splits......List and counts all the splits in a list of trees");
377                 fprintf ( stdout, "\n     ......................splits can be restricted to a list of sequences provided via -in2");
378                 fprintf ( stdout, "\n     +treelist2dmat.......outputs a distance matrix for a list of trees");
379
380                 fprintf ( stdout, "\n     +tree_compute n s....Computes a tree using the MSA provided with -in");
381                 fprintf ( stdout, "\n     ....................n:0-9, controls the way the MSA is filtered");
382                 fprintf ( stdout, "\n     ....................s:pam250mt|blosum62mt|categories|enthropy");
383                 fprintf ( stdout, "\n     ....................s:controls the column evaluation in MSA");
384                 fprintf ( stdout, "\n     +change_distances.f.f:float, sets all the distances to f in the tree");
385                 fprintf ( stdout, "\n     +change_bootstrap n..:n=0 removes all the bootstrap values");
386                 fprintf ( stdout, "\n     .....................:n!=0 adds a the value n to every node");
387                 fprintf ( stdout, "\n     +tree2dpatree........Replaces tree distances with the minimum %%ID in");
388                 fprintf ( stdout, "\n     .....................the depending subgroup. The ID is measured on an");
389                 fprintf ( stdout, "\n     .....................-in=TREE -in2=ALN");
390                 fprintf ( stdout, "\n     +unroot..............Removes the root in the input tree");
391                 fprintf ( stdout, "\n     +tree2group.N.I.P....Reports all the tree subgroup with at most Nseq");
392                 fprintf ( stdout, "\n     .....................and at min I%% identity. Output format can be read by");
393                 fprintf ( stdout, "\n     .....................collapse_tree. New groups are named P_1, P_2...");               
394                 fprintf ( stdout, "\n     +collapse_tree.F.....Collapses trees. F is either a file or a list");
395                 fprintf ( stdout, "\n     .....................<new name> <seq1> <seq2>...");
396                 fprintf ( stdout, "\n     +aln2tree............Computes a tree");
397                 fprintf ( stdout, "\n     ..ktupN|aln|sarmat   ktupN: match size N to estimate distances");
398                 fprintf ( stdout, "\n     .....................aln: Measures distances on aln");
399                 fprintf ( stdout, "\n     .....................sarmat: expects in to be a SAR matrix of O and I");
400                 fprintf ( stdout, "\n     ..nj | cw............Runs Neighbor Joining OR Cw to compute Tree");
401                 fprintf ( stdout, "\n     ..dpa................Turns the tree into a daptree (+tree2dpatree)");
402                 fprintf ( stdout, "\n     +node_sort..<name>...Sort leafs of tree n1, by node distance");
403                           
404                 
405                 fprintf ( stdout, "\nMatrix Analysis___________________________________________________");
406                 fprintf ( stdout, "\n     +aln2mat_diaa........computes a dinucleotide matrix on a list of aln");
407                 fprintf ( stdout, "\n     +aln2mat.............computes a log odd matrix");
408                 
409                 fprintf ( stdout, "\n     +seq2lat_mat.........computes a transition matrix on seq provided via -in");
410                 
411                 fprintf ( stdout, "\nStructure Analysis___________________________________________________");
412                 fprintf ( stdout, "\n     +struc2contacts.A.B D.Displays in capitals all the residues of A");
413                 fprintf ( stdout, "\n     ......................Less than D Angs from a residue of B");
414                 fprintf ( stdout, "\n     ......................A and B are pdb file, D is a distance in Angs");
415                 fprintf ( stdout, "\n     +seq2contacts.A.D.....Identifies all the residues in contact with ligands");
416                 fprintf ( stdout, "\n     ......................Ligands are in the FASTA header of struc in");
417                 fprintf ( stdout, "\n     ......................>Name _S_ [Target Struc] [Ligand1] [Chain] ...");
418                 fprintf ( stdout, "\n     ......................Output: number_fasta: 0=no contact, 1=ligand 1...");
419                 fprintf ( stdout, "\n     ......................9: residues in contact with more than 1 ligand");
420                 fprintf ( stdout, "\n     ......................Use -output=color_html/ascii to display result");
421                 fprintf ( stdout, "\n     +struc2nb...D.........Display a list of all the residues D appart");
422                 fprintf ( stdout, "\n     +rm_template...V......Removes _[S|G|R]_[template] to sequence names");
423                 fprintf ( stdout, "\n     ......................V: omitted | sequences <=> Output sequences");
424                 fprintf ( stdout, "\n     ......................V: template <=> Output templates");
425                         
426                 fprintf ( stdout, "\n     +add_template.F.......Add _[S|G|R]_[template] to sequence names");
427                 fprintf ( stdout, "\n     ......................F can either be a fasta file or an executable");
428                 fprintf ( stdout, "\n     ......................F: File: >name _S_ template");
429                 fprintf ( stdout, "\n     ......................F: executable: pg -infile=<seq> -outfile=<tagged>");
430                 fprintf ( stdout, "\nMatrix Comparison___________________________________________________");
431                 fprintf ( stdout, "\n    +mat2cmp...............Returns the correlation coefficient between two matrices");
432                 fprintf ( stdout, "\n    .......................-in mat1 -input matrix, -in2 mat2 -input2 matrix");
433                 fprintf ( stdout, "\n***********  INPUT FORMATS: Alignments *****************");
434                 fprintf ( stdout, "\n     AUTOMATIC RECOGNITION");
435                 fprintf ( stdout, "\n     perl_xxx:............. runs xxx onto the input file");
436                 fprintf ( stdout, "\n     xxxx <file> > outfile..xxx reads any formats, outputs fasta");
437                 fprintf ( stdout, "\n     amps_aln       saga_aln      ");
438                 fprintf ( stdout, "\n     clustal_aln    fasta_aln     msf_aln  ");
439                 fprintf ( stdout, "\n     dali_aln       gotoh_aln     pima_aln");
440                 fprintf ( stdout, "\n     dialign_aln    matrix        conc_aln");    
441                 fprintf ( stdout, "\n     NON AUTOMATIC RECOGNITION (use the -input file to specify the format");
442                 fprintf ( stdout, "\n     number_aln     newick_tree");
443                 fprintf ( stdout, "\n");
444                 fprintf ( stdout, "\n***********  INPUT FORMATS: Sequences *****************");
445                 fprintf ( stdout, "\n     fasta_seq      dali_seq       pir_seq");
446                 fprintf ( stdout, "\n     barton_list_tc amps_sd_scores EST_fasta");
447                 fprintf ( stdout, "\n     gor_seq        gor_struc      number_fasta[*]");
448                 fprintf ( stdout, "\n     swissprot      tc_lib         pdb_struc");
449                 fprintf ( stdout, "\n");
450                 fprintf ( stdout, "\n***********  INPUT FORMATS: Structures   *****************");
451                 fprintf ( stdout, "\n    rna_number");
452                 fprintf ( stdout, "\n    alifold");
453                 fprintf ( stdout, "\n***********  OUTPUT FORMATS: Alignments ******************");
454                 fprintf ( stdout, "\n     compressed_aln saga_aln        clustal_aln");
455                 fprintf ( stdout, "\n     phylip_aln     msf_aln         fasta_aln ");
456                 fprintf ( stdout, "\n     pir_aln        ");
457                 fprintf ( stdout, "\n     color_html,color_ps......colored using the struc_in file  ");
458                 fprintf ( stdout, "\n     color_protogene..........colors codons");
459                 fprintf ( stdout, "\n     color_exoset.............mixes conservation (gray) and introns (RGB)");
460                 fprintf ( stdout, "\n     color_pdf      pw_lib_saga_aln tdna_aln");
461                 fprintf ( stdout, "\n     thread_dna_on_prot_aln");
462                 fprintf ( stdout, "\n");
463                 fprintf ( stdout, "\n*********** OUTPUT FORMATS: sequence  ******************");
464                 fprintf ( stdout, "\n     fasta_seq      fasta_seq1     gotoh_seq");
465                 fprintf ( stdout, "\n     gor_seq        cache_id");
466                 fprintf ( stdout, "\n     tblastx_db1     tblastx_db2    tblastx_db3");
467                 fprintf ( stdout, "\n*********** OUTPUT FORMATS: weights ******************");
468                 fprintf ( stdout, "\n     constraints    saga_pw_sd_weights  nseq\n");
469                 fprintf ( stdout, "\n");
470                 fprintf ( stdout, "\n*********** OUTPUT Formats: special  ****************");
471                 fprintf ( stdout, "\n     len             name               statistics<_hnrglNL>");
472                 fprintf ( stdout, "\n      sim............outputs a similarity matrix based on an id comparison of -in");
473                 fprintf ( stdout, "\n      sim_sarmat.....in is sar matrix");
474                 fprintf ( stdout, "\n      sim_idscore....makes dp alignment of the sequences using Blosum62mt");
475                 fprintf ( stdout, "\n      sim_idscoreDNA.makes dp alignment of the sequences using idmat");
476                 fprintf ( stdout, "\n      sim............if -in2 is set: in1 vs in2, idscore");
477                 
478                 fprintf ( stdout, "\n     code_name......Outputs a compact list of names for code/decode");
479
480                 
481                 
482                 fprintf ( stdout, "\n");
483         
484                 
485                 fprintf ( stdout, "\n");
486                 return EXIT_SUCCESS;
487                 }
488         
489         argv=standard_initialisation (argv, &argc);
490         
491                 
492         for ( a=1; a< argc; a++)
493                 {
494                   if (a==1 && argv[1][0]!='-')
495                     {
496                       sprintf( in_file, "%s", argv[a]);
497                     }
498                   else if ( strcmp ( argv[a], "-in_f")==0 ||strm(argv[a],"-input") )
499                         {
500                         if ( strcmp ( argv[a], "-in_f")==0) fprintf ( stdout,"\nWARNING: %s deprecated, use -input instead", argv[a]);
501                 
502                         sprintf ( in_format, "%s", argv[a+1]);
503                         a++;
504                         }
505                 
506                 else if ( strcmp ( argv[a], "-cache")==0 )
507                         {
508                         sprintf (cache, "%s", argv[a+1]);
509                 
510                         a++;
511                         }
512                   
513                 
514                 else if ( strcmp ( argv[a], "-exon_boundaries")==0 )
515                         {
516                 
517                           set_string_variable ("exon_boundaries", argv[a+1]);
518                           a++;
519                         }
520                 else if ( strcmp ( argv[a], "-overaln_threshold")==0 )
521                         {
522                 
523                           set_int_variable ("overaln_threshold", atoi(argv[a+1]));
524                           a++;
525                         }
526                 else if ( strcmp ( argv[a], "-overaln_target")==0 )
527                         {
528                 
529                           set_int_variable ("overaln_target", atoi(argv[a+1]));
530                           a++;
531                         }
532                 else if ( strcmp ( argv[a], "-overaln_P1")==0 )
533                         {
534                           
535                           set_int_variable ("overaln_P1", atoi(argv[a+1]));
536                           a++;
537                         }
538                 else if ( strcmp ( argv[a], "-overaln_P2")==0 )
539                         {
540                 
541                           set_int_variable ("overaln_P2", atoi(argv[a+1]));
542                           a++;
543                         }
544                 else if ( strcmp ( argv[a], "-overaln_P3")==0 )
545                         {
546                 
547                           set_int_variable ("overaln_P3", atoi(argv[a+1]));
548                           a++;
549                         }
550                 else if ( strcmp ( argv[a], "-overaln_P4")==0 )
551                         {
552                 
553                           set_int_variable ("overaln_P4", atoi(argv[a+1]));
554                           a++;
555                         }
556                   
557                 else if ( strcmp ( argv[a], "-in2_f")==0||strm(argv[a],"-input2") )
558                         {
559                           if ( strcmp ( argv[a], "-in_f")==0) fprintf ( stdout,"\nWARNING: %s deprecated, use -input2 instead", argv[a]);
560                 
561                         sprintf ( in2_format, "%s", argv[a+1]);
562                         a++;
563                         }
564                 else if ( strcmp ( argv[a], "-seqnos")==0)
565                         {
566                         sprintf (action_list[n_actions++], "seqnos");
567                         }
568                 
569                 else if ( strcmp( argv[a], "-action")==0)
570                         {
571                         while ((a+1)<argc && argv[a+1][0]!='-')
572                           {
573                             sprintf (action_list[n_actions++], "%s", argv[a+1]);
574                             a++;
575                           }
576                         }
577                 else if ( strcmp ( argv[a], "-keep_case")==0)
578                         {
579                           if(!NEXT_ARG_IS_FLAG)RAD->keep_case=1;
580                           else RAD->keep_case=(strm3(argv[a], "on","ON","On"))?1:0;
581                           
582                         }
583                 
584                 else if ( strcmp ( argv[a], "-conv")==0)
585                         {
586                         if ( strncmp ( argv[a+1],"set",3)==0)RAD->symbol_list=make_symbols (argv[++a],&(RAD->n_symbol));
587                         else
588                             {
589                             RAD->symbol_list=declare_char (STRING, STRING);
590                             while(!NEXT_ARG_IS_FLAG)
591                                   { 
592                                   sprintf ( RAD->symbol_list[RAD->n_symbol], "%s", argv[++a]);
593                                   RAD->n_symbol++; 
594                                   }
595                             }
596                         }
597                 else if ( strcmp ( argv[a], "-struc_in_f")==0 ||strcmp ( argv[a], "-input3")==0 )
598                         {
599                         sprintf ( struc_in_format, "%s", argv[a+1]);
600                         a++;
601                         }
602                 else if ( strcmp ( argv[a], "-out_f")==0 ||strm(argv[a],"-output") )
603                         {
604                         if ( strcmp ( argv[a], "-out_f")==0) fprintf (stdout, "\nWARNING: %s deprecated, use -output instead", argv[a]);
605                         sprintf ( out_format, "%s", argv[a+1]);
606                         a++;
607                         }
608                 else if ( strm ( argv[a], "-struc_out_f") || strm ( argv[a], "-output_struc") )
609                         {
610                         sprintf ( struc_out_format, "%s", argv[a+1]);
611                         a++;
612                         }
613                 else if ( strcmp (argv[a],"-in")==0)
614                         {
615                         sprintf( in_file, "%s", argv[a+1]);
616                         a++;
617                         }
618                 else if ( strcmp (argv[a],"-rename")==0)
619                         {
620                         sprintf( rename_file, "%s", argv[a+1]);
621                         a++;
622                         }
623                 else if ( strcmp (argv[a],"-code")==0)
624                         {
625                         code=CODE;
626                         sprintf( rename_file, "%s", argv[a+1]);
627                         a++;
628                         }
629                 else if ( strcmp (argv[a],"-decode")==0)
630                         {
631                           code=DECODE;
632                           sprintf( rename_file, "%s", argv[a+1]);
633                           a++;
634                         }
635                 else if ( strcmp (argv[a],"-in2")==0)
636                         {
637                         sprintf( in2_file, "%s", argv[a+1]);
638                         a++;
639                         }
640                 else if ( strcmp (argv[a],"-coor")==0)
641                         {                       
642                         sprintf( RAD->coor_file, "%s", argv[a+1]);
643                         a++;
644                         }
645                 else if (strcmp (argv[a],"-out")==0) 
646                         {
647                         sprintf (out_file, "%s", argv[a+1]);
648                         a++;
649                         }
650                 else if (strcmp (argv[a],"-out2")==0) 
651                         {
652                         sprintf (out2_file, "%s", argv[a+1]);
653                         a++;
654                         }
655                 else if ( strcmp (argv[a],"-struc_in")==0 || strcmp (argv[a],"-in3")==0 )
656                         {
657                         sprintf( struc_in_file, "%s", argv[a+1]);
658                         a++;
659                         }
660                 else if (strcmp (argv[a],"-struc_out")==0) 
661                         {
662                         sprintf (struc_out_file, "%s", argv[a+1]);
663                         a++;
664                         }
665                 else if ( strcmp ( argv[a], "-rm_gap")==0)
666                         {
667                         RAD->rm_gap=1;
668                         }
669                 else if ( strcmp ( argv[a], "-print_format")==0)
670                         {
671                         print_format=1;
672                         }
673                 else if ( strcmp ( argv[a], "-no_warning")==0)
674                         {
675                         set_warning_mode (NO);
676                         }
677                 
678                 else    
679                         {
680                         fprintf ( stdout, "\nUNKNOWN OPTION: %s", argv[a]);
681                         myexit(EXIT_FAILURE);
682                         }
683                 }
684 /****************************************************************/
685 /*                                                              */
686 /*                          Data Preparation                    */
687 /*                                                              */
688 /*                                                              */
689 /****************************************************************/
690         
691         prepare_cache (cache);    
692 /****************************************************************/
693 /*                                                              */
694 /*                          INPUT SEQ/ALN                       */
695 /*                                                              */
696 /*                                                              */
697 /****************************************************************/
698         
699
700         if ( strm (out_format, "hasch"))
701           {
702             fprintf ( stdout, "%d\n", (int)hash_file(in_file));
703             return EXIT_SUCCESS;
704           }
705
706         if ( rename_file[0])
707           {
708             rename_list=read_rename_file ( rename_file,code);
709           }
710
711
712         if ((D1=read_data_structure (in_format, in_file,RAD))!=NULL)
713           {
714             in_format=(in_format && in_format[0])?in_format:identify_seq_format(in_file);
715             
716             if (print_format)fprintf ( stdout, "\nFILE:%s FORMAT:%s\n", in_file, in_format);
717           }
718         else if ( in_file[0])
719                 {
720                   fprintf ( stdout, "\nFORMAT of file %s Not Supported[FATAL:%s]\n", in_file, PROGRAM);
721                 myexit(EXIT_FAILURE);
722                 }
723         
724         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));}
725
726         else if (!D2 && in2_file[0])
727                 {
728                   fprintf ( stderr, "\nFORMAT of file %s Not Supported [FATAL:%s]\n", in2_file, PROGRAM);
729                 myexit(EXIT_FAILURE);
730                 }
731         
732 /*STRUCTURE INPUT*/     
733         
734         
735         if ((D_ST=read_data_structure (struc_in_format, struc_in_file,RAD)))
736             {
737
738               if ( D_ST->CL)
739                 {
740                   Constraint_list *CL;
741                   int *entry;
742
743                   CL=D_ST->CL;
744                   
745                   while ((entry=extract_entry (CL)))
746                     {
747                       if ( D_ST->S)(D_ST->S)->seq[entry[SEQ1]][entry[R1]-1]=entry[WE];
748                     }
749                   thread_seq_struc2aln (D_ST->A, D_ST->S);
750                 }
751               else if ( name_is_in_list ("cons", ((D_ST)->A)->name, ((D_ST)->A)->nseq, 100));
752               else
753                 {
754                   D_ST->A=copy_aln ( D1->A, D_ST->A);
755                   
756                   thread_seq_struc2aln (D_ST->A, D_ST->S);
757                 }
758             }
759         else if ((strcmp (struc_in_format, "rna_number")==0) && in_file[0])
760                 {
761                 D_ST->RNA_ST=read_rna_struc_number((D1->A),struc_in_file);
762                 }
763         else if ( struc_in_format[0] && struc_in_file[0])
764                 {
765                     
766                 fprintf ( stderr, "\nSTRUC %s UNKNOWN[FATAL]", struc_in_format);
767                 myexit(EXIT_FAILURE);
768                 }
769         else
770           {
771             D_ST=vcalloc ( 1, sizeof (Sequence_data_struc));
772           }
773
774         action=declare_char(100, 100);
775         for ( a=0; a< n_actions;)
776           {
777            if (action_list[a][0]!='+')
778               {
779                 fprintf ( stderr, "\nWARNING: Action %s Unknown. Actions start with a +", action_list[a]);
780                 myexit (EXIT_FAILURE);
781               }
782            else
783              {
784              b=0;
785              sprintf ( action[b++], "%s", action_list[a++]+1);
786              while ( a<n_actions && action_list[a][0]!='+')sprintf ( action[b++], "%s", action_list[a++]);
787              modify_data( D1, D2, D_ST, action,b, RAD);
788              }
789           }
790
791         if (rename_list)
792           {
793             if (D1)D1->A= rename_seq_in_aln(D1->A, rename_list);
794             if (D2)D2->A=rename_seq_in_aln (D2->A, rename_list);
795             if (D_ST)D_ST->A=rename_seq_in_aln (D_ST->A,rename_list);
796             
797             if (D1)D1->T  =rename_seq_in_tree (D1->T, rename_list);
798             if (D2)D2->T  =rename_seq_in_tree (D2->T, rename_list);
799             if (D_ST)D_ST->T=rename_seq_in_tree (D_ST->T,rename_list);
800           }
801
802
803         if ( !out_format[0] && ! struc_out_format[0])sprintf ( out_format, "%s", (in_format && in_format[0])?in_format:"fasta_aln");
804         main_output  ( D1, D2, D_ST, out_format, out_file);
805         main_output  ( D1, D2, D_ST, struc_out_format, struc_out_file);
806         return EXIT_SUCCESS;
807         }
808         
809
810
811
812 /**************************************************************************************************/
813 /*****************************    FORMAT GUESSING     ******************************************/
814 /**************************************************************************************************/
815 Sequence_data_struc *read_data_structure ( char *in_format, char *in_file,      Action_data_struc  *RAD) 
816          
817         {
818         Sequence_data_struc *D;
819         char **seq_name=NULL, **sequences=NULL;
820         int nseq=0, a;
821
822         
823         D=vcalloc ( 1, sizeof (Sequence_data_struc));
824         
825
826         if (!in_file[0])return NULL; 
827         if (!in_format[0])
828           {
829             in_format=identify_seq_format(in_file);
830           }
831         if (!in_format[0])return NULL;
832         
833
834
835         D->A=declare_Alignment(NULL);                 
836         if ( RAD->keep_case)(D->A)->residue_case=KEEP_CASE;
837         
838         D->rm_gap=RAD->rm_gap;
839         sprintf ( D->format, "%s", in_format);
840         sprintf ( D->file, "%s", in_file);
841         
842
843        
844
845         if ( strm2(in_format,"saga_aln","clustal_aln"))
846                 {
847                 read_aln (in_file, D->A);
848                 D->S=aln2seq(D->A);
849                 
850                 }
851                 
852         else if ( strm (in_format, "treefile_list"))
853           {
854
855             D->S=get_tree_file_list(in_file);
856             D->A=seq2aln(D->S, D->A,NO_PAD);
857           }
858         else if ( strm (in_format, "file_list") || strm (in_format, "list"))
859           {
860             D->S=get_file_list(in_file);
861             D->A=seq2aln(D->S, D->A,KEEP_GAP);
862           }
863         else if ( strm (in_format, "fasta_tree"))
864           {
865
866             D->S=get_fasta_tree (in_file, NULL);
867             D->A=seq2aln(D->S, D->A,NO_PAD);
868            
869           }
870         else if ( strm (in_format, "tree_list") || strm (in_format, "treelist"))
871           {
872             char **line;
873             FILE *seq;
874             int n=0;
875             char *seq_file;
876             FILE *fp;
877
878             seq_file=vtmpnam(NULL);
879             seq=vfopen (seq_file, "w");
880             line=file2lines (in_file);
881             fp=vfopen (seq_file, "w");
882             for ( n=1; n<atoi(line[0]); n++)
883               {
884                 fprintf ( fp, ">Tree_%d\n%s\n", n,line[n]);
885               }
886             vfclose (fp);
887             
888             free_char (line, -1);
889             return read_data_structure ( "fasta_tree",seq_file,RAD); 
890           }
891         
892         else if (strm (in_format, "matrix"))
893           {
894             D->M=read_matrice (in_file);
895           }
896         else if (strm4 (in_format, "newick_tree", "newick", "nh", "new_hampshire"))
897           {
898             D->T=main_read_tree (in_file);
899             D->S=tree2seq(D->T, NULL);
900             D->A=seq2aln (D->S,D->A, 0); 
901           }
902         else if (strm (in_format, "blast_aln"))
903                 {
904                   if (read_blast_aln (in_file, D->A))
905                   {
906                     D->S=aln2seq(D->A);
907                   }
908                 else
909                   {
910                     return NULL;
911                   }
912                 }
913         else if ( strm( in_format,"number_aln"))
914                 {               
915                 read_number_aln (in_file, D->A);
916                 D->S=aln2seq(D->A);
917                 }
918         else if ( strm( in_format,"stockholm_aln"))
919                 {               
920                 read_stockholm_aln (in_file, D->A);
921                 D->S=aln2seq(D->A);
922                 }
923         else if ( strm( in_format,"gotoh_aln"))
924                 {               
925                 read_gotoh_aln (in_file, D->A);
926                 D->S=aln2seq(D->A);
927                 }
928         
929         else if ( strm ( in_format, "msf_aln"))
930                 {
931                 read_msf_aln (in_file, D->A);
932                 D->S=aln2seq(D->A);
933                 }
934         else if ( strm ( in_format, "amps_aln"))
935                 {
936                 read_amps_aln (in_file, D->A);
937                 D->S=aln2seq(D->A);
938                 }
939         else if ( strm (in_format, "excel_seq"))
940                 {               
941                   D->S=perl_reformat2fasta ("excel2fasta.pl",in_file);
942                   (D->S)->contains_gap=0;
943                   D->A=seq2aln(D->S, D->A,RAD->rm_gap);
944                 }
945         else if ( strm (in_format, "pavie_seq"))
946                 {               
947                   D->S=perl_reformat2fasta ("pavie2fasta.pl",in_file);
948                   (D->S)->contains_gap=0;
949                   D->A=seq2aln(D->S, D->A,RAD->rm_gap);
950                 }
951         else if ( strncmp (in_format, "perl_",5 )==0)
952                 {               
953                   D->S=perl_reformat2fasta (in_format+5,in_file);
954                   (D->S)->contains_gap=0;
955                   D->A=seq2aln(D->S, D->A,RAD->rm_gap);
956                 }
957         else if ( strm (in_format, "number_fasta"))
958                 {               
959                 D->S=get_fasta_sequence_num (in_file, NULL);
960                 (D->S)->contains_gap=0;
961                 D->A=seq2aln(D->S, D->A,RAD->rm_gap);
962                 }       
963         else if ( strm (in_format, "raw_fasta"))
964                 {               
965                 D->S=get_fasta_sequence_raw (in_file, NULL);
966                 (D->S)->contains_gap=0;
967                 D->A=seq2aln(D->S, D->A,RAD->rm_gap);
968                 }       
969
970         else if ( strm2 (in_format, "fasta_aln", "fasta_seq"))
971                 {
972                 
973                 D->S=get_fasta_sequence (in_file, NULL);
974                 if ( strcmp (in_format, "fasta_aln")==0)(D->S)->contains_gap=0;
975                 D->A=seq2aln(D->S, D->A,RAD->rm_gap);
976                 }       
977         else if ( strm (in_format, "fasta_tree"))
978                 {
979                 
980                 D->S=get_fasta_tree (in_file, NULL);
981                 D->A=seq2aln(D->S, D->A, NO_PAD);
982                 }       
983         
984         else if ( strm (in_format, "pdb") || strm (in_format, "pdb_struc"))
985                 {
986                     D->S=get_pdb_sequence (in_file);
987                     if ( D->S==NULL)
988                       {
989                         add_warning (stderr, "FAILED TO find PDB File %s", in_file);
990                         myexit (EXIT_FAILURE);
991                       }
992                     D->A=seq2aln(D->S, D->A,RAD->rm_gap);
993                 }
994         else if ( strm2(in_format, "pir_seq", "pir_aln"))
995                 {
996                 D->S=get_pir_sequence ( in_file,NULL );
997                 seq2aln(D->S, D->A, RAD->rm_gap);
998                 }
999         else if ( strm(in_format, "gor_seq") )
1000                 {
1001                 D->S=get_gor_sequence ( in_file,NULL );
1002                 seq2aln(D->S, D->A, RAD->rm_gap);
1003                 }
1004         else if ( strm2 ( in_format, "dali_aln", "dali_seq"))
1005                 {
1006                 D->S=get_sequence_dali ( in_file);
1007                 seq2aln(D->S, D->A, RAD->rm_gap);
1008                 }
1009         else if ( strm (in_format, "barton_list_tc"))
1010                 {
1011                 get_barton_list_tc_seq ( in_file);
1012                 }
1013         else if ( strm (in_format, "amps_sd_scores"))
1014                 {
1015                 D->W=get_amps_sd_scores ( in_file);
1016                 }
1017         
1018         else if ( strm ( in_format, "pima_aln"))
1019                 {
1020                 D->S=get_pima_sequence ( in_file);
1021                 seq2aln (D->S, D->A, RAD->rm_gap);
1022                 }
1023         else if ( strm( in_format, "gor_struc"))
1024                 {
1025                 D->S=get_struc_gor ( in_file);
1026                 seq2aln(D->S, D->A, RAD->rm_gap);
1027                 }
1028         else if ( strm( in_format, "dialign_aln"))
1029                 {
1030                 D->S=get_dialign_sequence ( in_file);
1031                 seq2aln (D->S, D->A, RAD->rm_gap);
1032                 }
1033         else if ( strm( in_format, "tc_lib") ||  strm( in_format, "mocca_lib") ||  strm( in_format, "lib"))
1034                 {
1035                   read_seq_in_list (in_file,&nseq,&sequences,&seq_name); 
1036                   D->S=fill_sequence_struc ( nseq, sequences, seq_name);
1037                   D->CL=declare_constraint_list ( D->S,NULL, NULL, 0,NULL, NULL); 
1038                   D->CL=read_constraint_list_file(D->CL,in_file);
1039                   seq2aln (D->S, D->A, RAD->rm_gap);
1040                   free_char (sequences,-1);
1041                   free_char (seq_name, -1);
1042                 }
1043         else if ( strm( in_format,"swissprot_seq"))
1044                 {
1045                   D->S=get_swissprot_sequence ( in_file,NULL);
1046                   seq2aln (D->S, D->A, RAD->rm_gap);
1047                 }
1048         else if  (strm (in_format, "alifold"))
1049           {
1050             D->S=read_alifold ( in_file);
1051             seq2aln (D->S, D->A,0);
1052           }
1053         else
1054                 {
1055                 return NULL; 
1056                 }
1057
1058         if ( D->A)
1059           {
1060             for ( a=0; a<(D->A)->nseq; a++)sprintf ( (D->A)->file[a], "%s", in_file);
1061           }
1062         if ( D->S)
1063           {
1064             for ( a=0; a<(D->A)->nseq; a++)sprintf ( (D->S)->file[a], "%s", in_file);
1065           }
1066         
1067         return D;
1068         }
1069 Sequence *read_sequences (char *name)
1070 {
1071   return main_read_seq (name);
1072 }
1073 Alignment * alifold2aln  (char *file)
1074 {
1075   Sequence *S;
1076   S=read_alifold(file);
1077   sprintf ( S->seq[0],"%s", S->seq[1]);
1078   return seq2aln (S, NULL, 0);
1079 }
1080 Sequence  * read_alifold (char *file)
1081 {
1082   Sequence *S;
1083   char **list;
1084   int l;
1085   S=declare_sequence (1,count_n_char_in_file (file),2);
1086   list=file2lines (file);
1087   
1088   S->seq[0]=list[1];
1089   S->seq[1]=list[2];
1090   substitute (S->seq[0], "\n", "\0");
1091   substitute (S->seq[0], " ", "\0");
1092   substitute (S->seq[0], "_", STOCKHOLM_STRING);
1093   l=strlen (S->seq[0]);
1094   substitute (S->seq[1], "\n", "\0");
1095   substitute (S->seq[1], " ", "\0");
1096   substitute (S->seq[1], ".", STOCKHOLM_STRING);
1097   S->seq[1][l]='\0';
1098   sprintf (S->name[0], "cons");
1099   sprintf (S->name[1], "#=GC SS_cons");
1100   return S;
1101 }
1102
1103   
1104   
1105
1106   
1107   
1108 Sequence  * main_read_seq ( char *name)
1109        {
1110        char *format=NULL;
1111        Sequence *S=NULL;
1112        Alignment *A=NULL;
1113        int a;
1114
1115
1116        format=identify_seq_format (name);
1117        
1118
1119        if ( getenv4debug ("DEBUG_REFORMAT"))fprintf ( stderr, "\n\nFormat %s\n", format); 
1120
1121        
1122        if (format &&strm(format, "fasta_seq"))        
1123          {
1124            S= get_fasta_sequence ( name, NULL);
1125          }
1126        else if (format &&strm(format, "pir_seq"))     S= get_pir_sequence ( name, NULL);
1127        else if (format &&strm(format,"swissprot_seq"))S= get_swissprot_sequence (name, NULL); 
1128        else if (format && strstr (format, "aln")) 
1129          {
1130            A=main_read_aln ( name, NULL);
1131            S=aln2seq(A);
1132            ungap_seq(S);
1133            free_aln(A);
1134          }
1135        else if ( format && strstr (format, "tc_lib"))
1136          {
1137            int nseq,b;
1138            char **sequences=NULL, **seq_name=NULL;
1139            
1140            read_seq_in_list (name,&nseq,&sequences,&seq_name);             
1141            S=fill_sequence_struc ( nseq, sequences, seq_name);
1142            for ( b=0; b< S->nseq; b++)sprintf ( S->file[b], "%s",name);
1143            free_char (seq_name, -1);free_char (sequences, -1);
1144          }
1145        else
1146           {
1147           /*Use The ClustalW routine*/
1148             S=cw_read_sequences (name);
1149           }
1150        
1151        for ( a=0; a<S->nseq; a++)sprintf ( S->file[a], "%s", name);
1152        vfree(format);
1153        ungap_seq(S);
1154        S=clean_sequence ( S);
1155        return S;
1156        }
1157
1158 Alignment * main_read_aln ( char *name, Alignment *A)
1159        {
1160        int a;
1161
1162        static char *format;
1163        Sequence *S=NULL;
1164        Sequence *IN_SEQ;
1165  
1166       
1167        if ( !name)return NULL;
1168        else if (!check_file_exists(name))
1169          {
1170            if ( !check_file_exists (name+1))return NULL;
1171            else if ( name[0]=='A') name++;
1172            else if ( name[0]=='S') name++;/*Line Added for the -convert flag of T-Coffee*/
1173          }
1174        
1175
1176        if (!A)A=declare_aln(NULL);
1177        format=identify_seq_format (name);
1178        
1179        IN_SEQ=A->S;
1180        
1181        if      ((format && strm(format, "saga_aln" )) ||strm(format, "clustal_aln")||strm(format, "t_coffee_aln" ) )
1182          {
1183         
1184            read_aln ( name, A);
1185         
1186          }
1187        else if (format && strm (format, "conc_aln"))A=input_conc_aln (name,NULL);
1188        else if (format &&strm(format, "msf_aln"  ))read_msf_aln ( name, A);
1189        else if (format &&strm(format, "blast_aln"))read_blast_aln (name, A);
1190        else if (format &&(strm(format, "fasta_aln")))
1191                 {
1192                   
1193                   
1194                 S=get_fasta_sequence ( name, NULL);
1195                 
1196                 S->contains_gap=0;
1197                 seq2aln (S, A, 0);              
1198                 }
1199        else if (format &&strm(format, "pir_aln"))
1200                 {
1201                 S=get_pir_sequence ( name, NULL);
1202                 S->contains_gap=0;
1203                 seq2aln (S, A, 0);
1204                 } 
1205        else if (format && strm(format, "fasta_seq") && A)
1206            {
1207            S=get_fasta_sequence ( name, NULL);
1208            
1209            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;}
1210            S->contains_gap=0;
1211            seq2aln (S, A, 0);
1212            }
1213
1214        else if (format && strm(format, "pir_seq") && A)
1215            {
1216            S=get_pir_sequence ( name, NULL);
1217           
1218            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;}
1219            S->contains_gap=0;
1220            seq2aln (S, A, 0);
1221            }
1222        else
1223           {
1224               free_aln(A);
1225               return NULL;        
1226           }
1227
1228      
1229          if ( check_list_for_dup( A->name, A->nseq))
1230           {
1231               fprintf ( stderr, "\nWARNING (main_read_aln): %s is duplicated in File %s ", check_list_for_dup( A->name, A->nseq), A->file[0]);
1232               A=aln2unique_name_aln(A);
1233           }
1234      
1235        if (IN_SEQ)A->S=IN_SEQ;
1236        else if (!A->S){A->S=aln2seq(A);}
1237        
1238        A->S=ungap_seq(A->S);
1239        A=fix_aln_seq(A, A->S);     
1240        compress_aln (A);
1241        for ( a=0; a< A->nseq; a++) sprintf ( A->file[a], "%s", name);
1242        
1243        A=clean_aln (A);
1244        return A;
1245        }
1246
1247
1248 char * identify_aln_format ( char *file)
1249        {
1250         /*This function identify known sequence and alignmnent formats*/
1251          return identify_seq_format (file);
1252        }
1253 char * identify_seq_format ( char *file)
1254        {
1255        char *format=NULL;
1256        /*This function identify known sequence and alignmnent formats*/
1257     
1258        if ( format==NULL)format=vcalloc ( 100, sizeof (char));
1259        else format[0]='\0';
1260        
1261
1262        
1263        if ( !check_file_exists(file))
1264          {
1265            fprintf (stderr, "ERROR: %s Does Not Exist [FATAL:%s]\n",file, PROGRAM);
1266            myexit (EXIT_FAILURE);
1267          }
1268        else if ( is_stockholm_aln (file))sprintf (format, "stockholm_aln");
1269        else if ( is_blast_file (file))sprintf ( format, "blast_aln");      
1270        else if ( is_pdb_file(file))sprintf ( format, "pdb_struc");  
1271        else if ( format_is_msf      (file))sprintf ( format, "msf_aln");
1272        else if ( format_is_fasta_seq(file))sprintf ( format, "fasta_seq");
1273        else if ( format_is_fasta_aln(file))sprintf ( format, "fasta_aln");       
1274        else if ( format_is_pir_aln  (file))sprintf ( format, "pir_aln");
1275        else if ( format_is_pir_seq  (file))sprintf ( format, "pir_seq");
1276        else if ( format_is_oligo    (file))sprintf ( format, "oligo_aln");
1277        else if ( format_is_swissprot     (file))sprintf ( format, "swissprot_seq");
1278        else if ( format_is_saga     (file))sprintf ( format, "clustal_aln");
1279        else if ( format_is_conc_aln (file))sprintf ( format, "conc_aln");
1280        else if ( is_lib (file))sprintf ( format, "tc_lib");
1281        else if ( is_lib_02 (file))sprintf ( format, "tc_lib_02");
1282        else if ( is_newick(file))sprintf ( format, "newick_tree");
1283  
1284        else 
1285          {
1286            //add_warning ( stderr, "\nThe Format of File: %s was not recognized [SERIOUS:%s]",file, PROGRAM);
1287            ;
1288          }
1289        return format;
1290        }
1291 char **identify_list_format ( char **list, int n)
1292        {
1293            int a;
1294            char *name;
1295            char *string;
1296            char mode;
1297         
1298
1299            
1300            declare_name (name);
1301            for ( a=0; a< n; a++)
1302                {
1303                  
1304                  sprintf (name, "%s", list[a]);
1305                  string=list[a];
1306                  if ((mode=identify_format ( &string))!='?')
1307                    {
1308                        sprintf ( name, "%s", string);
1309                        sprintf ( list[a], "%c%s", mode,name);
1310                    }
1311                else
1312                    {
1313                        fprintf ( stderr, "\nERROR: %s not recognised [FATAL:%s]", name, PROGRAM);
1314                    }
1315              
1316                }
1317            
1318            vfree(name);
1319            return list;
1320        }
1321                
1322 char * name2type_name ( char *name)
1323 {
1324   /*turns <file> into <Sfile>, <Afile>...*/
1325   char *new_name;
1326   char mode;
1327   
1328   new_name=vcalloc ( strlen (name)+2, sizeof (char));
1329   sprintf ( new_name, "%s", name);
1330   if (is_in_set (name[0], "ALSMXPRW") && !check_file_exists(name))
1331     {
1332       sprintf ( new_name, "%s", name);
1333     }
1334   else
1335     {
1336       mode=identify_format (&new_name);
1337       sprintf ( new_name, "%c%s", mode,name);
1338     }
1339   return new_name;
1340 }          
1341
1342 char identify_format (char **fname)
1343        {
1344            char mode='?';
1345            mode=fname[0][0];
1346            
1347            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)) )
1348              {
1349                
1350                fname[0]++;
1351              }
1352            else if (mode=='W' && !check_file_exists(fname[0])){fname[0]++;}
1353            else
1354                {
1355                    
1356                  /*WARNING: Order matters => internal methods can be confused with files, must be checked last*/
1357                       if (is_lib(fname[0]))mode='L';
1358                       else if (is_pdb_file(fname[0]))mode='P';
1359                       else if (is_seq(fname[0]))mode='S';
1360                       else if (is_aln(fname[0]))mode='A';
1361                       else if (is_matrix(fname[0]))mode='X';
1362                       else if (is_method(fname[0]))mode='M';                  
1363                       else mode='?';
1364                   }
1365            return mode;
1366        }
1367
1368
1369
1370 int is_pdb_name ( char *name)
1371     {
1372       char command[1000];
1373       int result;
1374       char *result_file;
1375       static char **buf_names;
1376       static int   *buf_result;
1377       static int   nbuf;
1378       FILE *fp;
1379
1380       
1381       /*Use the look up*/
1382       if ( !buf_names)
1383         {
1384           buf_names=declare_char (1000, 100);
1385           buf_result=vcalloc (1000, sizeof (int));
1386         }
1387       if ( (result=name_is_in_list ( name, buf_names,nbuf,100))!=-1)return buf_result[result];
1388       
1389       
1390
1391       result_file=vtmpnam (NULL);
1392    
1393       sprintf ( command, "extract_from_pdb -is_pdb_name \'%s\' > %s", name, result_file);
1394       if ( getenv4debug ("DEBUG_EXTRACT_FROM_PDB"))fprintf ( stderr, "\n[DEBUG_EXTRACT_FROM_PDB:is_pdb_name] %s\n", command);
1395       my_system ( command);
1396       
1397       fp=vfopen ( result_file, "r");
1398       fscanf ( fp, "%d", &result);
1399       vfclose (fp);
1400       vremove ( result_file);
1401       
1402       sprintf ( buf_names[nbuf], "%s", name);
1403       result=buf_result[nbuf++]=(result==1)?1:0;
1404
1405       return result;
1406       
1407     }
1408
1409 char*  get_pdb_id ( char *file)
1410 {
1411   /*receives the name of a pdb file*/
1412   /*reads the structure id in the header*/
1413   /*returns the pdb_id*/
1414   char *tmp_name;
1415   char command[10000];
1416   char cached [1000];
1417   char fname[1000];
1418   FILE *fp;
1419   char *id;
1420   char buf[1000];
1421
1422
1423   tmp_name=vtmpnam(NULL);
1424   
1425   sprintf ( cached, "%s/%s", get_cache_dir(),file);
1426   if ( check_file_exists(cached))sprintf ( fname, "%s", cached);
1427   else sprintf ( fname, "%s", file);
1428
1429   sprintf ( command, "extract_from_pdb -get_pdb_id %s > %s",fname, tmp_name);
1430   
1431   if ( getenv4debug ("DEBUG_EXTRACT_FROM_PDB"))fprintf ( stderr, "\n[DEBUG_EXTRACT_FROM_PDB:get_pdb_id] %s\n", command);
1432   my_system ( command);
1433   
1434   buf[0]='\0';
1435   fp=vfopen (tmp_name, "r");
1436   fscanf ( fp, "\n%s\n", buf);  
1437   vfclose (fp);
1438
1439   if ( getenv4debug ("DEBUG_EXTRACT_FROM_PDB"))fprintf ( stderr, "\n[DEBUG_EXTRACT_FROM_PDB:get_pdb_id]DONE\n");
1440   
1441   id=vcalloc ( strlen (buf)+1, sizeof (char));
1442   sprintf ( id, "%s", buf);
1443   
1444   
1445
1446   return id;  
1447 }
1448     
1449   
1450 char*  get_pdb_struc(char *in_name, int start, int end)
1451     {
1452       char *name1,*name2;
1453       char command[LONG_STRING];
1454       char *name;
1455
1456       
1457       
1458
1459       name=vcalloc ( STRING, sizeof (char));
1460       sprintf ( name, "%s", in_name);
1461      
1462       if ( (name1=is_pdb_struc(name))==NULL && (name[0]=='P' && ((name1=is_pdb_struc (name+1))==NULL)))
1463         {
1464           fprintf ( stderr, "\nERROR Could not download structure %s [FATAL:%s]\n", name, PROGRAM);crash("");
1465         }
1466       else if ( (start==0) && (end==0))return name1;
1467       else
1468         {
1469           declare_name(name2);
1470           sprintf ( name2, "%s_%d_%d.pdb", name, start, end);
1471           sprintf ( command, "extract_from_pdb -infile \'%s\' -chain FIRST -coor %d %d > %s%s",check_file_exists(name1),start, end, get_cache_dir(),name2);
1472           if ( getenv4debug ("DEBUG_EXTRACT_FROM_PDB"))fprintf ( stderr, "\n[DEBUG_EXTRACT_FROM_PDB:get_pdb_struc] %s\n", command);
1473           my_system (command);
1474
1475           if ( is_pdb_file(name2))return name2;
1476           else
1477             {
1478               fprintf ( stderr, "\nERROR Could not extract segment [%d %d] from structure %s [FATAL:%s]\n",start, end, name, PROGRAM);crash("");
1479             }
1480           myexit (EXIT_FAILURE);
1481         }
1482
1483       return NULL;
1484     }
1485
1486 char*  seq_is_pdb_struc ( Sequence *S, int i)
1487 {
1488   
1489   if (!S){return NULL;}
1490   else if ( !S->T[i]){return NULL;}
1491   else if ( !((S->T[i])->P)){return NULL;}
1492   else return ((S->T[i])->P)->template_file;
1493 }
1494 char*  is_pdb_struc ( char *name)
1495    {
1496      /*Receives a name
1497        checks if this is the name of a local file that contains PDB data
1498        checks if this is the name of a file from a local db
1499                                             put the file in the cache
1500        checks if this is a file from a remote db (extract_from_pdb
1501        return NULL if everything fails
1502      */
1503
1504      static char *file_name1;
1505      static char *file_name2;
1506           
1507      static char **buf_names;
1508      static char **buf_result;
1509      static int   nbuf, s;
1510     
1511      
1512      char *r=NULL;
1513      char command[1000];
1514      
1515
1516      if ( !name || name[0]=='\0')return NULL;
1517        
1518
1519      /*Use the look up*/
1520      if ( !buf_names)
1521         {
1522
1523           buf_names=vcalloc ( 1000, sizeof (char*));
1524           buf_result=vcalloc ( 1000, sizeof (char*));
1525           file_name1=vcalloc ( 1000, sizeof (char));
1526           file_name2=vcalloc ( 1000, sizeof (char));
1527         }
1528      if ( (s=name_is_in_list ( name, buf_names,nbuf,-1))!=-1)return buf_result[s];
1529       
1530
1531      r=NULL;
1532      sprintf ( file_name1, "%s", name);
1533      sprintf ( file_name2, "%s.pdb", name);
1534      
1535      
1536      if (is_pdb_file(file_name1)){r=file_name1;}
1537      else if (is_pdb_file(file_name2)){r=file_name2;}
1538      else if (is_pdb_name (name))
1539        {
1540          printf_system ("extract_from_pdb -netfile \'%s\' > %s/%s 2>/dev/null",name, get_cache_dir(), file_name2);
1541          if ( is_pdb_file(file_name2))r=file_name2; 
1542          else r=NULL;    
1543          
1544        }
1545      
1546
1547       /*Fill the buffer*/
1548      buf_names[nbuf]=vcalloc ( strlen (name)+1, sizeof (char)); 
1549      sprintf ( buf_names[nbuf], "%s", name);
1550      if ( r)
1551        {
1552          buf_result[nbuf]=vcalloc ( strlen (r)+1, sizeof (char));
1553          sprintf (buf_result[nbuf], "%s", r);
1554        }
1555      else buf_result[nbuf]=NULL;      
1556      nbuf++;
1557
1558      return r;
1559    }
1560
1561 char *fix_pdb_file ( char *in)
1562 {
1563   char *empty;
1564   
1565   empty=vcalloc(1, sizeof(char));
1566
1567   if ( !in || !check_file_exists (in))return empty;
1568   else if ( is_pdb_file(in))return in;
1569   else
1570     {
1571       char command[10000];
1572       char *tmp;
1573       char *tmp2;
1574       tmp=vtmpnam (NULL);
1575       tmp2=vcalloc (strlen (tmp)+1, sizeof (char));
1576       sprintf (tmp2, "%s", tmp);
1577       sprintf ( command, "extract_from_pdb %s > %s", check_file_exists(in), tmp2);
1578       my_system (command);
1579       if ( is_pdb_file (tmp))return tmp2;
1580       else return empty;
1581
1582     }
1583 }
1584
1585 int is_sap_file ( char *name)
1586         {
1587         FILE *fp;
1588         if (!name);
1589         if (!check_file_exists(name))return 0;
1590         
1591         if ((fp=find_token_in_file (name, NULL, "Percent"))!=NULL)
1592           {
1593             if ((fp=find_token_in_file (name,fp, "Percent"))!=NULL)
1594               {
1595                 vfclose (fp);
1596                 return 1;
1597               }
1598             else
1599               { 
1600                 return 0;
1601               }
1602           }
1603         else
1604           {
1605             return 0;
1606           }
1607         }
1608
1609
1610 int is_blast_file ( char *name)
1611        {
1612          if ( !check_file_exists(name) ) return 0;
1613          else if (token_is_in_file (name, "<SequenceSimilaritySearchResult>"))
1614            {
1615              return BLAST_XML;
1616            }
1617          else
1618            {
1619              if (token_is_in_file (name, "Lambda") && token_is_in_file (name, "Altschul,"))
1620                {
1621                  return BLAST_TXT;
1622                }
1623              else
1624                {
1625                  return 0;
1626                }
1627            }
1628          return 0;
1629        }
1630 int is_simple_pdb_file ( char *name)
1631 {
1632   FILE *fp;
1633   if ((fp=find_token_in_file (name, NULL, "SIMPLE_PDB_FORMAT"))!=NULL){vfclose (fp);return 1;}
1634   return 0;
1635 }
1636
1637
1638 int is_pdb_file ( char *name)
1639        {
1640          FILE *fp;
1641          int ispdb=0;
1642          
1643          if ( name==NULL) return 0;
1644          if (!check_file_exists (name))return 0;
1645          
1646          if ((fp=find_token_in_file (name, NULL, "\nHEADER"))!=NULL)
1647            {vfclose (fp);
1648              ispdb++; 
1649            }
1650          if ((fp=find_token_in_file (name, NULL, "\nSEQRES"))!=NULL)
1651            {
1652              vfclose (fp);
1653              ispdb++;
1654            }
1655          
1656          if ((fp=find_token_in_file (name, NULL, "\nATOM"))!=NULL)
1657            {
1658              vfclose (fp);
1659              ispdb++;
1660            }
1661          else
1662            {
1663              ispdb=0;
1664            }
1665          
1666          
1667          if ( ispdb>=2)return 1;
1668          else return 0;
1669        }
1670 int is_seq ( char *name)
1671        {
1672          char *format;
1673
1674          if ( !check_file_exists(name))return 0;
1675          
1676          format= identify_seq_format(name);
1677          if(!format || format[0]=='\0'){vfree (format);return 0;}
1678          else if (strstr(format, "seq")){vfree (format);return 1;}
1679          else return 0;
1680        }
1681 int is_aln ( char *name)
1682        {
1683        char *format;     
1684        if ( !check_file_exists       (name))return 0;   
1685         
1686        format= identify_seq_format(name);
1687        if ( !format || format[0]=='\0'){vfree (format);return 0;}
1688        else if (strstr(format, "aln")){vfree (format); return 1;}
1689        else return 0;
1690        }   
1691
1692 int is_matrix (char *name)
1693        {
1694        int **m;
1695        
1696        if ((m=read_matrice (name))!=NULL){free_int (m, -1); return 1;}
1697        return 0;              
1698        }
1699 int is_newick (char *name)
1700    {
1701      int c;
1702      FILE *fp;
1703      
1704
1705      fp=vfopen (name, "r");
1706      if ( (c=fgetc(fp))!='('){vfclose (fp); return 0;}
1707
1708
1709      while ( (c=fgetc(fp))!=EOF)
1710        {
1711          if ( c==';'){vfclose (fp); return 1;}
1712        }
1713      vfclose (fp);
1714      return 0;
1715    }
1716
1717 int is_clustalw_matrix ( char *name)
1718 {
1719        
1720   FILE *fp;
1721
1722
1723        if ( (fp=find_token_in_file (name, NULL, "CLUSTALW_MATRIX"))!=NULL){vfclose(fp);return 1;}
1724        else return 0;
1725 }
1726 int is_pavie_matrix ( char *name)
1727 {
1728        
1729   FILE *fp;
1730
1731
1732        if ( (fp=find_token_in_file (name, NULL, "PAVIE_MATRIX"))!=NULL){vfclose(fp);return 1;}
1733        else return 0;
1734 }
1735 int is_distance_matrix_file (char *name)
1736 {
1737   FILE *fp;
1738   if ( (fp=find_token_in_file (name, NULL, "TC_DISTANCE_MATRIX_FORMAT_01"))!=NULL){vfclose(fp);return 1;}
1739   else return 0;
1740 }  
1741 int is_similarity_matrix_file (char *name)
1742 {
1743   FILE *fp;
1744   if ( (fp=find_token_in_file (name, NULL, "TC_SIMILARITY_MATRIX_FORMAT_01"))!=NULL){vfclose(fp);return 1;}
1745   else return 0;
1746 }  
1747 int is_blast_matrix ( char *name)
1748 {
1749        
1750   FILE *fp;
1751
1752
1753   if ( (fp=find_token_in_file (name, NULL, "BLAST_MATRIX"))!=NULL){vfclose(fp);return 1;}
1754        else return 0;
1755 }
1756
1757 int is_single_seq_weight_file ( char *name)
1758 {
1759  
1760   
1761   return token_is_in_file ( name, "SINGLE_SEQ_WEIGHT_FORMAT_01");
1762   
1763 }
1764 int is_stockholm_aln (char *file)
1765 {
1766   FILE *fp;
1767   
1768   if ((fp=find_token_in_file_nlines (file, NULL, "STOCKHOLM",2)))
1769     {
1770       vfclose (fp);
1771       return 1;
1772     }
1773   return 0;
1774 }
1775
1776 int is_lib ( char *name)
1777 {
1778   return is_lib_01(name);
1779 }
1780
1781 int is_lib_02 ( char *name)
1782 {
1783   
1784   return token_is_in_file ( name, "TC_LIB_FORMAT_02");
1785   
1786 }
1787
1788 int is_lib_01 (char *name)
1789        {
1790
1791          
1792          if ( token_is_in_file ( name, "TC_LIB_FORMAT_01")) return 1;
1793          else if (token_is_in_file ( name, "T-COFFEE_LIB_FORMAT_01"))return 1;
1794          else if (token_is_in_file (name, "SEQ_1_TO_N"))return 1;
1795          else return 0;
1796        }
1797 int is_lib_list ( char *name)
1798 {
1799   if ( !check_file_exists (name))return 0;
1800   if ( token_is_in_file ( name, "TC_LIB_LIST_FORMAT_01")) return 1;
1801   return 0;
1802 }
1803 int is_method ( char *file)
1804     {
1805         char new_file[200];
1806
1807         
1808         sprintf ( new_file, "%s", file);
1809         if ( (token_is_in_file(new_file, "TC_METHOD_FORMAT_01"))){return 1;}
1810         if ( is_in_pre_set_method_list(new_file)) 
1811             {
1812         
1813                 vremove ( new_file);
1814                 return 1;
1815             }
1816         else
1817           {
1818         
1819             return 0;
1820           }
1821     }
1822
1823 /*******************************************************************************************/
1824 /*                                                                                         */
1825 /*                                                                                         */
1826 /*                              SEQUENCE FORMAT IDENTIFIERS                                */
1827 /*                                                                                         */
1828 /***************************************************************************************** */
1829 int type_is_exon_boundaries(char **seq, int n)
1830 {
1831   int a, l, b;
1832   for (a=0; a<n; a++)
1833     {
1834       l=strlen (seq[a]);
1835       for (b=0; b<l; b++)
1836         if ( strchr ("bojBOJ", seq[a][b]))return 1;
1837     }
1838   return 0;
1839 }
1840
1841 int format_is_oligo(char *file)
1842     { 
1843     char *buf=NULL;
1844     FILE *fp;
1845     int r=0;
1846     
1847     fp=vfopen ( file, "r");
1848     buf=vfgets(buf,fp);
1849     vfclose ( fp);
1850     
1851
1852     if ( strm (buf, "ALPHABET"))r=1;
1853     
1854     vfree (buf);
1855     
1856     return r;
1857     }
1858 int format_is_msf ( char *file)
1859     {
1860     char buf[1000];
1861     FILE *fp;
1862
1863    
1864     
1865     if ( (fp=find_token_in_file_nlines (file,NULL,"MSF:", 30))!=NULL){vfclose (fp);return 1;}
1866     else
1867       {
1868        return 0;
1869       }
1870     
1871     fp=vfopen ( file, "r");
1872     fscanf (fp , "%s", buf);
1873     vfclose ( fp);
1874
1875     if ( strm (buf, "MSF:"))return 1;
1876     return 0;
1877     }
1878
1879 //Fasta and PIR
1880 int format_is_fasta_aln ( char *file)
1881
1882     {
1883       if ( format_is_fasta(file) && !format_is_fasta_seq(file))return 1;
1884       else return 0;
1885     }
1886
1887
1888 int format_is_fasta_seq  ( char *file)
1889     {
1890       int a, l1, l2,l;
1891       Sequence *S;
1892
1893       if ( format_is_fasta (file))
1894         {       
1895         S=get_fasta_sequence (file, NULL);
1896         if (!S) return 0;
1897         else if ( !S->seq[0]){free_sequence (S, S->nseq); return 1;}
1898         l=strlen ( S->seq[0]);
1899         for ( a=0; a< S->nseq; a++)if(strlen(S->seq[a])!=l){free_sequence (S, S->nseq);return 1;}
1900         for ( a=0; a< S->nseq; a++)
1901           {
1902             l1=strlen ( S->seq[a]);
1903             ungap (S->seq[a]);
1904             l2=strlen ( S->seq[a]);
1905             if ( l1!=l2)
1906               {
1907                 free_sequence (S, S->nseq);
1908                 return 0;
1909               }
1910           }
1911         free_sequence (S, S->nseq);
1912         return 1;
1913       }
1914     else
1915       {
1916         return 0;
1917       }
1918     }
1919
1920 int format_is_fasta ( char *file)
1921     {
1922       Sequence *S;
1923       
1924       if ( !check_file_exists(file))return 0;
1925
1926       if ( get_first_non_white_char (file)!='>')return 0;
1927       if ( !(S=get_fasta_sequence (file, NULL)))return 0;
1928       free_sequence (S, -1);
1929       if ( format_is_pir(file)) return 0;
1930       return 1;
1931     }
1932
1933 int format_is_pir_aln ( char *file)
1934
1935     {
1936       if ( format_is_pir(file) && !format_is_pir_seq(file))return 1;
1937       else return 0;
1938     }
1939
1940 int format_is_pir_seq ( char *file)
1941     {
1942       int a, l1, l2;
1943       Sequence *S;
1944
1945       
1946     if ( format_is_pir (file))
1947       {
1948         S=get_pir_sequence (file, NULL);
1949         for ( a=0; a< S->nseq; a++)
1950           {
1951             l1=strlen ( S->seq[a]);
1952             ungap (S->seq[a]);
1953             l2=strlen ( S->seq[a]);
1954             if ( l1!=l2)
1955               {
1956                 free_sequence (S, S->nseq);
1957                 return 0;
1958               }
1959           }
1960         return 1;
1961       }
1962     else
1963       {
1964         return 0;
1965       }
1966     }
1967     
1968
1969 int format_is_pir ( char *file)
1970     {
1971       Sequence *S;
1972       int pir_name=1, star_end=1, a;
1973       
1974       S=get_fasta_sequence (file, NULL);
1975       if (!S)return 0;
1976       else if (!S->seq[0])return 0;
1977       
1978       pir_name=1; star_end=1;
1979       for (a=0; a< S->nseq; a++)
1980         {
1981           int l;
1982           if (!is_pir_name(S->name[a]))pir_name=0;
1983           l=strlen (S->seq[a]);
1984           if (!l || (l && S->seq[a][l-1]!='*'))
1985             star_end=0;
1986         }
1987       free_sequence(S,-1);
1988       if ( pir_name && star_end) return 1;
1989       else return 0;
1990     }
1991 int is_pir_name (char *name)
1992 {
1993   if ( strstr (name, "P1;"))return 1;
1994   if ( strstr (name, "F1;"))return 1;
1995   if ( strstr (name, "DL;"))return 1;
1996   if ( strstr (name, "DC;"))return 1;
1997   if ( strstr (name, "RL;"))return 1;
1998   if ( strstr (name, "RC;"))return 1;
1999   if ( strstr (name, "XX;"))return 1;
2000   return 0;
2001 }
2002   
2003   
2004 int format_is_conc_aln (char *file)
2005 {
2006   FILE *fp;
2007   if ( (fp=find_token_in_file (file, NULL, "CONC_MSF_FORMAT_01"))){vfclose (fp); return 1;}
2008   return 0;
2009 }
2010 int format_is_saga ( char *file) 
2011     {
2012     FILE *fp;
2013     int **list;
2014     int n_blocks;
2015     int n_seq;
2016     int a, b;
2017     
2018     if ( (fp=find_token_in_file (file, NULL, "SAGA"))){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, "ClustalW"))){vfclose (fp); return 1;}
2021     else if  ((fp=find_token_in_file (file, NULL, "clustalw"))){vfclose (fp); return 1;}
2022     else if  ((fp=find_token_in_file (file, NULL, "clustal"))){vfclose (fp); return 1;}
2023     else if  ((fp=find_token_in_file (file, NULL, "T-COFFEE_MSA"))){vfclose (fp); return 1;}
2024     else if  ((fp=find_token_in_file (file, NULL, "INTERLEAVED_MSA"))){vfclose (fp); return 1;}
2025     
2026     else return 0;
2027     
2028     if (1==1);
2029     else if  ((fp=find_token_in_file (file, NULL, "T-COFFEE"))){vfclose (fp); return 1;}
2030     else if  ((fp=find_token_in_file (file, NULL, "SAGA_FORMAT"))){vfclose (fp); return 1;}
2031     else if  ((fp=find_token_in_file (file, NULL, "GARP"))){vfclose (fp); return 1;}
2032     else if  ((fp=find_token_in_file (file, NULL, "INTERLEAVED"))){vfclose (fp); return 1;}
2033     
2034     else 
2035        {
2036            list=get_file_block_pattern (file,&n_blocks,100); 
2037            if (n_blocks<=2){free_int (list, -1);return 0;}
2038            else 
2039                {                  
2040                n_seq=list[1][0];
2041                for ( a=1; a< n_blocks-1; a++)
2042                    {
2043                        if ( list[a][0]!=n_seq){free_int (list, -1);return 0;}
2044                        else
2045                        {
2046                            for ( b=1; b<=list[a][0]; b++)
2047                                if ( list[a][b]!=2){free_int (list, -1);return 0;}
2048                        }
2049                    }
2050                }
2051            return 1;
2052        }
2053     
2054     return 0;
2055     }
2056
2057
2058 int format_is_swissprot (char *name)
2059     {
2060       FILE *fp;
2061       
2062       if ( !check_file_exists(name))return 0;
2063          
2064          
2065    
2066     
2067       if (   (fp=find_token_in_file_nlines (name,NULL,"\nID ",10))!=NULL\
2068            &&(fp=find_token_in_file (name,NULL,"\nSQ "))!=NULL  )
2069         {
2070           
2071           vfclose (fp);return 1;
2072         }
2073       else
2074         {
2075           return 0;
2076         }
2077     } 
2078
2079 /*******************************************************************************************/
2080 /*                                                                                         */
2081 /*                                                                                         */
2082 /*                               OUTPUT STUFF                                              */
2083 /*                                                                                         */
2084 /***************************************************************************************** */               
2085 int output_format_aln ( char *format, Alignment *inA, Alignment *inEA,char *name)
2086         {
2087         Sequence_data_struc *D1=NULL;
2088         Sequence_data_struc *D2=NULL;
2089         Alignment *A=NULL;
2090         Alignment *EA=NULL;
2091         
2092         
2093         A =copy_aln (inA, NULL);
2094         A->CL=inA->CL;
2095         EA=copy_aln (inEA,NULL);
2096         A =expand_aln(A);
2097         EA=expand_number_aln(inA,EA);   
2098         
2099         
2100         if (A && A->expanded_order  )A=reorder_aln ( A, A->expanded_order,A->nseq);  
2101         if (EA && EA->expanded_order)EA=reorder_aln ( EA, EA->expanded_order,EA->nseq);  
2102
2103           
2104         D1=vcalloc ( 1, sizeof (Sequence_data_struc));
2105         D1->A=A;
2106         if (EA)
2107            {
2108            D2=vcalloc ( 1, sizeof (Sequence_data_struc));
2109            D2->A=EA;
2110            }
2111         
2112         main_output ( D1, NULL,D2, format, name);
2113         
2114         vfree(D1);
2115         vfree(D2);
2116         free_aln (A);
2117         free_aln (EA);
2118         return 1;
2119         }
2120 int main_output  (Sequence_data_struc *D1, Sequence_data_struc *D2, Sequence_data_struc *DST, char *out_format, char *out_file)
2121
2122         {  
2123         FILE *fp;
2124         int value;
2125         Alignment *BUF_A;
2126         int expanded=0;
2127         
2128         if ( !out_format[0])return 0;
2129         if ( D1 && D1->rm_gap)ungap_aln ((D1->A));
2130         
2131         if ( (strstr (out_format, "expanded_")))
2132           {
2133             if (!D1) return 1;
2134             out_format+=strlen ("expanded_");
2135             BUF_A=copy_aln (D1->A, NULL);
2136             (D1->A)=thread_profile_files2aln ((D1->A), NULL, NULL);
2137             expanded=1;
2138           }
2139         
2140         if ( strm (out_format, ""))return 0;
2141         else if (    ( strm (out_format, "aln2lib")))
2142           {
2143             int a, b, c;
2144             int r1,r2,s1, s2,s;
2145             Constraint_list *CL;
2146             FILE *fp;
2147             Alignment *IN;
2148             int **pos;
2149             
2150             if (!D1)return 1;
2151             IN=D1->A;
2152             CL=(D1->A)->CL;
2153             pos=aln2pos_simple(IN, IN->nseq);
2154             fp=vfopen (out_file, "w");
2155             fp=save_list_header (fp,CL);
2156             
2157           
2158             for ( b=0; b< IN->nseq-1; b++)
2159               {
2160                 for ( c=b+1; c< IN->nseq; c++)
2161                   {
2162                     s1=IN->order[b][0];
2163                     s2=IN->order[c][0];
2164                     fprintf ( fp, "#%d %d\n", s1+1, s2+1);
2165                     for ( a=0; a< IN->len_aln; a++)
2166                       {
2167                         r1=pos[b][a];
2168                         r2=pos[c][a];
2169
2170                         if ( s1==s2 && !CL->do_self)continue;
2171                         
2172                         if ( s1< s2)s=(CL->evaluate_residue_pair)( CL, s1, r1, s2, r2);
2173                         else        s=(CL->evaluate_residue_pair)( CL, s2, r2, s1, r1);
2174                         
2175                         s=(s!=UNDEFINED)?s:0;
2176                         if ( r1>0 && r2>0)
2177                           {
2178                             fprintf (fp, "\t%5d %5d %5d \n", r1, r2, s);
2179                           }
2180                       }
2181                   }
2182               }
2183              vfclose (save_list_footer (fp, CL));
2184           }
2185         else if      ( strncmp (out_format, "score",5)==0 || strm (out_format, "html"))
2186                 {
2187                   Alignment *BUF;
2188
2189                   if (!D1)return 1;
2190                   if ( !DST) 
2191                     {
2192                       fprintf ( stderr,"\n[You Need an evaluation File: Change the output format or use +evaluate][FATAL:%s]\n", PROGRAM);      
2193                       myexit(EXIT_FAILURE);
2194                     }
2195                   if ( !strm ("html", out_format))while ( out_format[0]!='_' && out_format[0]!='\0' )out_format++;
2196                   
2197                   D1->S=aln2seq(D1->A);
2198                   BUF=copy_aln (DST->A, NULL);
2199                   DST->A=aln2number (DST->A);
2200                   
2201                   if     ( strstr ( out_format, "html"  ))output_reliability_html  ( D1->A,  DST->A, out_file);
2202                   else if( strm ( out_format, "_ps"    ))output_reliability_ps    ( D1->A,  DST->A, out_file);
2203                   else if( strm ( out_format, "_pdf"   ))output_reliability_pdf   ( D1->A,  DST->A, out_file);  
2204                   else if( strm ( out_format, "_ascii" ))output_reliability_ascii ( D1->A,  DST->A, out_file);  
2205                   else if( strm ( out_format, "_seq"   ))output_seq_reliability_ascii ( D1->A,  DST->A, out_file);
2206                   else
2207                     {
2208                       DST->A=BUF;
2209                       main_output (DST, NULL, NULL, out_format+1, out_file);
2210                     }
2211                 }
2212         else if (strm (out_format, "sec_html") || strm (out_format, "_E_html"))
2213           {
2214             Alignment *ST, *A;
2215             Sequence *S;
2216             
2217             int a, b,c,i, ns=0;
2218             char *buf;
2219             if (!D1)return 1;
2220             A=D1->A;
2221             
2222             
2223             S=A->S;
2224             ST=copy_aln (A, NULL);
2225             for (a=0; a<ST->nseq; a++)
2226               {
2227                 i=name_is_in_list (ST->name[a],S->name, S->nseq, 100);
2228                 if ( i!=-1)
2229                   {
2230                     buf=seq2E_template_string(S, i);
2231                     if ( buf==NULL)continue;
2232                     else ns++;
2233                     for (c=0,b=0; b<ST->len_aln; b++)
2234                       {
2235                         int r1, s;
2236                         r1=ST->seq_al[a][b];
2237                         if ( r1!='-')
2238                           {
2239                             s=tolower (buf[c]);
2240                             if (s=='e')r1='0';
2241                             else if (s=='h')r1='9';
2242                             else if (s=='c')r1='5';
2243                             c++;
2244                           }
2245                         ST->seq_al[a][b]=r1;
2246                       }
2247                   }
2248               }
2249             
2250             if (!ns)
2251               {
2252                 add_warning ( stderr, "Cannot output tm_html:_E_ template file (sec. struc.) is required for this output ", PROGRAM);
2253               }
2254             output_color_html  ( A, ST, out_file);
2255           }
2256         else if (strm (out_format, "tm_html") || strm (out_format, "_T_html"))
2257           {
2258             Alignment *ST, *A;
2259             Sequence *S;
2260             
2261             int a, b,c,i, ns=0;
2262             char *buf;
2263             if (!D1)return 1;
2264             A=D1->A;
2265             A->output_tm = 1;
2266             
2267             S=A->S;
2268             ST=copy_aln (A, NULL);
2269             for (a=0; a<ST->nseq; a++)
2270               {
2271                 i=name_is_in_list (ST->name[a],S->name, S->nseq, 100);
2272                 if ( i!=-1)
2273                   {
2274                     buf=seq2T_template_string(S, i);
2275                     if ( buf==NULL)continue;
2276                     else ns++;
2277                     for (c=0,b=0; b<ST->len_aln; b++)
2278                       {
2279                         int r1, s;
2280                         r1=ST->seq_al[a][b];
2281                         if ( r1!='-')
2282                           {
2283                             s=tolower (buf[c]);
2284                             if (s=='o')r1='0';
2285                             else if (s=='h')r1='9';
2286                             else if (s=='i')r1='5';
2287                             c++;
2288                           }
2289                         ST->seq_al[a][b]=r1;
2290                       }
2291                   }
2292               }
2293             
2294             if (!ns)
2295               {
2296                 add_warning ( stderr, "Cannot output tm_html:_T_ template file (trans. Memb. ) is required for this output ", PROGRAM);
2297               }
2298             output_color_html  ( A, ST, out_file);
2299           }
2300         
2301         else if (strm (out_format, "color_exoset"))
2302           {
2303             Alignment *ST, *EX, *A;
2304             Constraint_list *CL;
2305             int a, b, n;
2306             char *buf;
2307             
2308             if ( !DST->A)
2309               {
2310                 printf_exit ( EXIT_FAILURE, stderr, "\nYou must provide an obj file via the -struc_in flag [FATAL:%s]", PROGRAM);
2311               }
2312             EX=DST->A;
2313             A=D1->A;
2314             
2315             CL=declare_constraint_list ( DST->S,NULL, NULL, 0,NULL, read_matrice("pam250mt"));
2316             
2317             ST=copy_aln (A, NULL);
2318             buf=vcalloc ( EX->len_aln+1, sizeof (int));
2319             
2320             for ( a=0; a< A->nseq; a++)
2321               {
2322                 int i;
2323                                 
2324                 i=name_is_in_list (A->name[a],EX->name, EX->nseq, -1);
2325                 if ( i==-1)continue;
2326                                 
2327                 sprintf ( buf, "%s", EX->seq_al[i]);
2328                 ungap (buf);
2329                 
2330                 for (n=0,b=0; b<A->len_aln; b++)
2331                   {
2332                     if (!is_gap(A->seq_al[a][b]))
2333                       {
2334                         if ( buf[n]=='o')
2335                           ST->seq_al[a][b]='0';
2336                         else if ( buf[n]=='j')
2337                           ST->seq_al[a][b]='1';
2338                         else if ( buf[n]=='b')
2339                           ST->seq_al[a][b]='2';
2340                         n++;
2341                       }
2342                   }
2343               }
2344             vfree (buf);
2345         
2346             output_color_html  ( A, ST, out_file);
2347             return EXIT_SUCCESS;
2348           }
2349         
2350         else if (strm (out_format, "color_protogene"))
2351           {
2352             int n, a, b;
2353             DST->A=copy_aln (D1->A, NULL);
2354             for (n=1,a=0; a< (D1->A)->len_aln; a++, n++)
2355               {
2356                 for ( b=0; b<(D1->A)->nseq; b++)
2357                   {
2358                     if (is_gap((D1->A)->seq_al[b][a]));
2359                     else if ( n<=3)(DST->A)->seq_al[b][a]=2;
2360                     else if ( n>3)(DST->A)->seq_al[b][a]=9;
2361                   }
2362
2363                 if ( n==6)n=0;
2364               }
2365             output_color_html  ( D1->A,  DST->A, out_file);
2366             return EXIT_SUCCESS;
2367             
2368           }
2369         else if      ( strncmp (out_format, "color",5)==0)
2370          {
2371            Alignment *BUF;
2372           
2373            if (!D1)return 1;
2374            
2375            if ( !DST) 
2376              {
2377                fprintf ( stderr,"\n[You Need an evaluation File: Change the output format or use +evaluate][FATAL:%s]\n", PROGRAM);     
2378                myexit(EXIT_FAILURE);
2379              }
2380            while ( out_format[0]!='_' && out_format[0]!='\0' )out_format++;
2381            
2382            BUF=copy_aln (DST->A, NULL);
2383
2384         
2385         
2386
2387            if     ( strm ( out_format, "_html"  ))output_color_html  ( D1->A,  DST->A, out_file);
2388            else if( strm ( out_format, "_ps"    ))output_color_ps    ( D1->A,  DST->A, out_file);
2389            else if( strm ( out_format, "_pdf"   ))output_color_pdf   ( D1->A,  DST->A, out_file);       
2390            else if( strm ( out_format, "_ascii"   ))output_color_ascii   ( D1->A,  DST->A, out_file);   
2391            else
2392              {
2393                DST->A=BUF;
2394                return main_output (DST, NULL, NULL, out_format+1, out_file);
2395              }
2396            return EXIT_SUCCESS;
2397          }
2398         else if ( strm4  ( out_format, "tc_aln","t_coffee_aln", "t_coffee", "tcoffee"))
2399           {
2400             if (!D1)return 1;
2401             vfclose (output_aln ( D1->A, vfopen (out_file, "w")));
2402           }
2403         else if ( strm  ( out_format, "analyse_pdb"))
2404           {
2405             if (!D1)return 1;
2406             if ( !DST) 
2407               {
2408                 fprintf ( stderr,"\n[You Need an evaluation File: Change the output format][FATAL:%s]\n", PROGRAM);     
2409                 myexit(EXIT_FAILURE);
2410               }
2411             analyse_pdb ( D1->A,DST->A, "stdout");
2412             (DST->A)=aln2number (DST->A);
2413             output_reliability_ps    ( D1->A,  DST->A, out_file);
2414           }
2415         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") )
2416           {
2417             if (!D1)return 1;
2418             if ( !DST) 
2419               {
2420                 fprintf ( stderr,"\n[You Need an evaluation File: Change the output format][FATAL:%s]\n", PROGRAM);     
2421                 myexit(EXIT_FAILURE);
2422               }
2423             
2424             
2425             
2426             (DST->A)=aln2number (DST->A);
2427             if ( strm (out_format, "align_pdb"))value=0;
2428             else if (  strm (out_format, "malign_pdb"))value=5;
2429             else value=atoi(out_format+5);
2430             
2431             D1->A=filter_aln_upper_lower (D1->A, DST->A,0, value);
2432             output_clustal_aln ( out_file, D1->A);
2433           }
2434         else if ( strnm (out_format, "repeat", 6))
2435           {
2436             int size;
2437             int a, b, c;
2438             Alignment *CONC;
2439             
2440             if ( !D1)return 1;
2441             size=atoi (out_format+6);
2442             print_aln (D1->A);
2443             CONC=declare_aln2 ( (D1->A)->nseq, ((D1->A)->len_aln+1)*size+1);
2444
2445             for ( a=0; a< (D1->A)->nseq; a++)(D1->A)->seq_al[a][(D1->A)->len_aln]='\0';
2446             for ( c=0,a=0; a< (D1->A)->nseq;c++)
2447               {
2448                 
2449                 sprintf ( CONC->name[c], "%s", (D1->A)->name[a]);
2450                 for ( b=0; b<size; b++, a++)
2451                   {
2452                     strcat (CONC->seq_al[c], (D1->A)->seq_al[a]);
2453                     strcat (CONC->seq_al[c], "O");
2454                   }
2455               }
2456             CONC->nseq=c;CONC->len_aln=strlen (CONC->seq_al[0]);
2457             output_clustal_aln ( out_file, CONC);
2458             free_aln (CONC);
2459           }
2460         
2461         else if ( strnm (out_format, "upper", 5))
2462               {
2463                 
2464                 if (!D1)return 1;
2465                 if ( !DST) 
2466                    {
2467                    fprintf ( stderr,"\n[You Need an evaluation File: Change the output format][FATAL:%s]\n", PROGRAM);  
2468                    myexit(EXIT_FAILURE);
2469                    }
2470                 
2471                 
2472                 (DST->A)=aln2number (DST->A);
2473
2474                 value=atoi(out_format+5);
2475
2476                 D1->A=filter_aln_lower_upper (D1->A, DST->A,0, value);
2477                 output_clustal_aln ( out_file, D1->A);
2478               }
2479         
2480         else if ( strm4 ( out_format, "filter0", "filter1", "filter2", "filter3"))
2481                {
2482                if (!D1)return 1;
2483                if ( !DST) 
2484                    {
2485                    fprintf ( stderr,"\n[You Need an evaluation File: Change the output format][FATAL:%s]\n", PROGRAM);  
2486                    myexit(EXIT_FAILURE);
2487                    }
2488                 
2489                (DST->A)=aln2number (DST->A);
2490                
2491                D1->A=filter_aln (D1->A, DST->A, atoi(out_format+6));
2492                output_clustal_aln ( out_file, D1->A);
2493                }
2494         
2495         else if ( strm3 ( out_format, "phylip_aln", "phylip", "phy"))
2496                 {
2497                 if (!D1)return 1;
2498                 output_phylip_aln ( out_file, D1->A);
2499                 }
2500         else if ( strm ( out_format, "mocca_aln"))
2501                 {
2502                 if (!D1)return 1;
2503                 output_mocca_aln ( out_file, D1->A, DST->A);
2504                 }       
2505         else if ( strm ( out_format, "saga_pw_sd_weights") )
2506                 {
2507                 if (!D1)return 1;
2508                 output_pw_weights4saga ((D1->W),(D1->W)->PW_SD, out_file);
2509                 }
2510         else if ( strm ( out_format, "saga_aln"))
2511                 {
2512                 if (!D1)return 1;
2513                 output_saga_aln (out_file, D1->A);
2514                 }
2515         else if (strm2 ( out_format, "aln","clustal_tc")|| strm (out_format, "msa"))
2516           {              
2517            
2518             if (!D1)return 1;
2519             output_clustal_aln (out_file, D1->A);
2520           }
2521         else if (strm5 ( out_format, "strict_clustal","clustal_aln", "clustalw","clustal", "clustalw_aln") || strm (out_format,"number_aln"))
2522           {              
2523             if (!D1)return 1;
2524             output_strict_clustal_aln (out_file, D1->A);
2525           }
2526         else if ( strm ( out_format, "conc_aln"))
2527               {
2528                 if (!D1)return 1;
2529                 output_conc_aln (out_file, D1->A);
2530                 }
2531         else if ( strm2 ( out_format, "lalign_aln","lalign"))
2532                 {
2533                 if (!D1)return 1;
2534                 output_lalign (out_file, D1->A);
2535                 }
2536         else if ( strm2 ( out_format, "glalign_aln","glalign"))
2537                 {
2538                 if (!D1)return 1;
2539                 output_glalign (out_file, D1->A, DST->A);
2540                 }
2541         
2542         else if ( strm2 ( out_format, "fasta_aln","fasta" ) || strm (out_format, "blast_aln"))
2543                 {
2544                 if (!D1)return 1;
2545                 output_fasta_aln( out_file, D1->A);
2546                 }
2547         else if ( strstr (out_format, "overaln"))
2548                 {
2549                 
2550                   char *s, mode[100];
2551                   OveralnP *F;
2552                   int eb=0;
2553                   if (!D1) return 1;
2554                   F=vcalloc (1, sizeof (OveralnP));
2555                   ungap_aln (D1->A);
2556                   string_array_upper ((D1->A)->seq_al, (D1->A)->nseq);
2557                   if ( D2 && D2->A)
2558                     {
2559                       D1->A=mark_exon_boundaries (D1->A, D2->A);
2560                       eb=1;
2561                     }
2562                   else if ( (s=get_string_variable ("exon_boundaries")))
2563                     {
2564                       Sequence *S;
2565                       Alignment *EB;
2566                       EB=seq2aln(S=main_read_seq(s),NULL, 0);
2567                       D1->A=mark_exon_boundaries (D1->A, EB);
2568                       free_sequence (S, S->nseq); free_aln (EB);
2569                       eb=1;
2570                     }
2571                   if ( strstr (out_format, "lower")) sprintf (F->mode,"lower");
2572                   else if (strstr (out_format, "unalign2"))sprintf (F->mode, "unalign2");
2573                   else if (strstr (out_format, "unalign"))sprintf (F->mode, "unalign");
2574                   else sprintf (F->mode, "%s", ((s=get_string_variable ("overaln_mode")))?s:"lower");
2575                   if (!strm (F->mode, "lower") && !strm (F->mode, "unalign") && !strm (F->mode, "unalign2"))printf_exit (EXIT_FAILURE,stderr,"\nERROR: unknown overaln_mode in overaln output [%s] [FATAL:%s]", mode, PROGRAM);
2576                   
2577                   if (int_variable_isset ("overaln_threshold"))F->t=get_int_variable ("overaln_threshold");
2578                   if (int_variable_isset ("overaln_target"))F->f=get_int_variable ("overaln_target");
2579                   if (int_variable_isset ("overaln_P1"))F->p1=get_int_variable ("overaln_P1");
2580                   if (int_variable_isset ("overaln_P2"))F->p2=get_int_variable ("overaln_P2");
2581                   if (int_variable_isset ("overaln_P3"))F->p3=get_int_variable ("overaln_P3");
2582                   if (int_variable_isset ("overaln_P4"))F->p4=get_int_variable ("overaln_P4");
2583                   
2584                   if (eb)sprintf (F->model, "fsa2");
2585                   else   sprintf (F->model, "fsa1");
2586                   D1->A=aln2clean_pw_aln (D1->A, F);
2587                   
2588                   //if (eb)D1->A=aln2clean_pw_aln (D1->A, mode,t, f,p1,p2,p3, "fsa2");
2589                   //else   D1->A=aln2clean_pw_aln (D1->A, mode,t, f,p1,p2,p3, "fsa1");
2590         
2591                   D1->S=aln2seq(D1->A);
2592                   output_clustal_aln (out_file, D1->A);
2593                 } 
2594         else if ( strm ( out_format, "est_prf" ))
2595                 {
2596                 if (!D1)return 1;
2597                 output_est_prf( out_file, D1->A);
2598                 }
2599         else if ( strm ( out_format, "clean_est_fasta_seq" ))
2600                 {
2601                 if (!D1)return 1;
2602                 D1->A=clean_est(D1->A);
2603                 output_fasta_seq(out_file, D1->A);
2604                 
2605                 }
2606         
2607         else if ( strm3 ( out_format, "msf_aln", "gcg", "msf"))
2608                 {
2609                 if (!D1)return 1;
2610                 output_msf_aln( out_file, D1->A);
2611                 }
2612         else if ( strm ( out_format, "rnalign"))
2613                 {
2614                 if (!D1)return 1;
2615                 output_rnalign (out_file, D1->A, DST->S);
2616                 }
2617         else if ( strm ( out_format, "tblastx_db1"))
2618           {
2619             seq2tblastx_db (out_file,D1->S,1);
2620           }
2621         else if ( strm ( out_format, "tblastx_db") || strm (out_format, "tblastx_db3"))
2622           {
2623             seq2tblastx_db (out_file,D1->S,3);
2624           }
2625         else if ( strm ( out_format, "tblastx_db2"))
2626           {
2627             seq2tblastx_db (out_file,D1->S,2);
2628           }
2629         else if ( strm ( out_format, "fasta_seq") ||strm ( out_format, "list")||strm ( out_format, "file_list"))
2630           {
2631
2632                 if (!D1)return 1;
2633                 output_fasta_seq (out_file,D1->A);
2634                 }
2635         else if (strm (out_format, "fasta_tree") )
2636                 {
2637                 if (!D1)return 1;
2638                 output_fasta_tree (out_file,D1->A);
2639                 }
2640         
2641         else if ( strm ( out_format, "gotoh_seq"))
2642                 {
2643                 if (!D1)return 1;
2644                 output_gotoh_seq (out_file,D1->A);
2645                 }
2646         else if ( strm (out_format, "fasta_seq1"))
2647                 {
2648                 if (!D1)return 1;
2649                 output_fasta_seq1 (out_file, D1->A);
2650                 }
2651         else if ( strm2 (out_format, "pir_aln", "pir"))
2652                 {
2653                 if (!D1)return 1;
2654                 output_pir_aln (out_file, D1->A);
2655                 }
2656         else if ( strm (out_format, "pir_seq"))
2657                 {
2658                 if (!D1)return 1;
2659                 output_pir_seq (out_file, D1->A);
2660                 }
2661         else if ( strm (out_format, "gor_seq"))
2662                 {
2663                 if (!D1)return 1;
2664                 output_gor_seq (out_file, D1->A);
2665                 }
2666         else if ( strm (out_format, "pir_seq1"))
2667                 {
2668                   if (!D1)return 1;
2669                 output_pir_seq1 (out_file, D1->A);
2670                 }
2671         else if ( strm (out_format, "pw_lib_saga_aln"))
2672                 {
2673                   if (!D1)return 1;
2674                 output_pw_lib_saga_aln (out_file, D1->A);
2675                 }
2676         else if ( strm (out_format, "lib"))
2677                 {
2678                   if (!D1)return 1;
2679                 output_lib (out_file, D1->A);
2680                 }
2681         else if ( strm (out_format, "pdb_constraint_list"))
2682                 {
2683                   if (!D1)return 1;
2684                 output_constraints (out_file, "pdb",D1->A);
2685                 }
2686         else if ( strm2 (out_format, "constraint_list","tc_lib"))
2687                 {
2688                   
2689                   if (!D1)return 1;
2690                   else if (!D1->CL)output_constraints (out_file,"sim", D1->A);
2691                   else if (D1->CL) vfclose ( save_constraint_list ( D1->CL, 0, (D1->CL)->ne, out_file, NULL, "ascii",(D1->CL)->S)); 
2692                 }
2693         else if (  strm2 (out_format, "extended_lib","extended_cosmetic"))
2694                 {
2695                   if (!D1)return 1;
2696                   output_constraints (out_file,out_format, D1->A);
2697                 }
2698         else if ( strncmp (out_format, "extended_pair", 13)==0)
2699                 {
2700                   if (!D1)return 1;
2701                   output_constraints (out_file,out_format, D1->A);
2702                 }
2703         else if ( strm (out_format, "cache_id"))
2704                 {
2705                   if (!D1)return 1;
2706                   cache_id (D1->A);
2707                 output_saga_aln (out_file, D1->A);
2708                 }
2709         else if ( strm (out_format, "compress_aln"))
2710                 {
2711                   if (!D1)return 1;
2712                 compress_aln (D1->A);
2713                 output_saga_aln (out_file, D1->A);
2714                 } 
2715         else if (strm (out_format, "n_seq") ||strm (out_format, "nseq") )
2716                 {
2717                   if (!D1)return 1;
2718                 fp=vfopen ( out_file, "w");
2719                 fprintf ( fp, "%d\n", (D1->A)->nseq);
2720                 vfclose (fp);
2721                 }
2722         
2723         else if ( strm ( out_format, "thread_dna_on_prot_aln"))
2724                 {
2725                   if (!D1)return 1;
2726                 D1->A=thread_dnaseq_on_prot_aln (D1->S, D2->A);
2727                 output_saga_aln ( out_file, D1->A);
2728                 }
2729         else if ( strm ( out_format, "tdna_fasta_seq1"))
2730                 {if (!D1)return 1;
2731                 D1->A=translate_dna_aln (D1->A,0);
2732                 output_fasta_seq1 (out_file, D1->A);
2733                 }
2734         else if (strm (out_format, "exons"))
2735           {
2736             Alignment *A;
2737             //exons come in upper case
2738             //output alternates them upper/lower
2739             if (!D1)return 1;
2740             A=copy_aln (D1->A, NULL);
2741             A->seq_al=gene2exons(A->seq_al,A->nseq);
2742             output_fasta_seq (out_file,A);
2743             free_aln (A);
2744           }
2745         else if ( strm (out_format, "wexons"))
2746           {
2747             if (!D1)return 1;
2748             output_wexons (out_file,D1->A);
2749         
2750           }
2751         else if ( strm (out_format, "texons"))
2752           {
2753             Alignment *A;
2754             Sequence *S;
2755             //exons come in upper case
2756             //output alternate amino acids in upper/lower case
2757             //amino acid has the case of its first nucleotide
2758             if (!D1)return 1;
2759             A=copy_aln (D1->A, NULL);
2760             A->seq_al=gene2exons(A->seq_al,A->nseq);
2761             S=aln2seq(A);
2762             output_fasta_seqS (out_file,S=translate_dna_seqS(S,1,'X'));
2763           }
2764         else if ( strm (out_format, "sexons"))
2765           {
2766             Alignment *A;
2767
2768             //exons come in upper case
2769             //output alternate amino acids in upper/lower case
2770             //amino acid has the case of its first nucleotide
2771             if (!D1)return 1;
2772             A=copy_aln (D1->A, NULL);
2773             output_fasta_seq ( out_file, D1->A);
2774           }
2775         
2776         else if ( strm ( out_format, "tdna_aln"))
2777                 {if (!D1)return 1;              
2778                 D1->A=translate_dna_aln (D1->A,0);
2779                 output_saga_aln ( out_file, D1->A);
2780                 }
2781         else if ( strm ( out_format, "cdna_fasta_seq1"))
2782                 {if (!D1)return 1;              
2783                 D1->A= gene2prot(D1->A);
2784                 output_fasta_seq1 ( out_file, D1->A);
2785                 }
2786         else if ( strm ( out_format, "mutate_cdna_aln"))
2787                 {if (!D1)return 1;
2788                     D1->A= mutate_cdna_aln ( D1->A);
2789                     output_clustal_aln ( out_file, D1->A);
2790                 }
2791         else if ( strm ( out_format, "tdna_sp_aln"))
2792                 { if (!D1)return 1;
2793                 if ( !DST) 
2794                    {
2795                    fprintf ( stderr,"\n[You Need an evaluation File: Change the output format][FATAL:%s]\n", PROGRAM);  
2796                    myexit(EXIT_FAILURE);
2797                    }            
2798                (DST->A)=aln2number (DST->A);
2799                 D1->A=translate_splice_dna_aln (D1->A, DST->A);
2800                 output_saga_aln ( out_file, D1->A);
2801                 }
2802         else if (out_format && out_format[0] && (strcmp ( out_format,"rna_graph_fasta")==0))
2803                 {
2804                   if (!D1)return 1;
2805                 sprintf ( (D1->A)->seq_al[0], "%s",(DST->S)->seq[0]);
2806                 (D1->A)->nseq=0;
2807                 output_fasta_seq (out_file, DST->A);
2808                 }
2809         else if (strm ( out_format, "freq_mat"))
2810                 {
2811                   if (!D1)return 1;
2812                 output_freq_mat (out_file, D1->A);  
2813                 }
2814         else if (strm ( out_format, "maln_pval"))
2815                 {if (!D1)return 1;
2816                 output_maln_pval ( out_file, D1->A);
2817                 }
2818         else if ( strm ( out_format, "model_aln"))
2819                 {
2820                   if (!D1)return 1;
2821                 output_model_aln ( out_file, D1->A);
2822                 }
2823         else if (strncmp (out_format, "mult",4)==0)
2824                 {
2825                   if (!D1)return 1;
2826                 output_mult_fasta_seq ( out_file, D1->A, atoi(out_format+4));
2827                 }
2828         else if (strm (out_format, "conservation"))
2829           {
2830             output_conservation_statistics (out_file, D1->A);
2831           }
2832         else if (strm (out_format, "len"))
2833                 {
2834                   if (!D1)return 1;
2835                   output_statistics (out_file, D1->A, "nrl");
2836                 }
2837         else if ( strm (out_format, "name"))
2838                 {
2839                   if (!D1)return 1;
2840                   if ( D1->A)output_statistics (out_file, D1->A, "n");
2841                   if ( D1->T)
2842                     {
2843                       Sequence *TS;
2844                       TS=tree2seq(D1->T, NULL);print_array_char (vfopen(out_file, "w"), TS->name, TS->nseq, "\n");
2845                     }
2846                 }
2847         else if ( strm (out_format, "code_name"))
2848                 {
2849                   char **nl=NULL;
2850                   int num, n=0;
2851                   Sequence *TS;
2852                   FILE *lfp;
2853                   if ( D1->A){n=(D1->A)->nseq, nl=(D1->A)->name;}
2854                   if ( D1->T){TS=tree2seq(D1->T, NULL);nl=TS->name;n=TS->nseq;}
2855                   
2856                   lfp=vfopen (out_file, "w");
2857                   for ( num=0; num<n; num++)
2858                     fprintf (lfp, "\n%s C%d", nl[num], num+1);
2859                   fprintf (lfp, "\n");
2860                   vfclose (lfp);
2861                 }
2862         else if ( strm ( out_format, "seq2struc"))
2863                   {
2864                     output_seq2struc (out_file, D1->A);
2865                   }
2866         else if ( strstr  ( out_format, "pavie_age_channel"))
2867           {
2868             output_n_pavie_age_channel ( D1->S,out_file, atoi((out_format+strlen ("pavie_age_channel"))));
2869             return EXIT_SUCCESS;
2870           }
2871         else if ( strstr ( out_format, "age_matrix"))
2872                   {
2873                     output_age_matrix (out_file, atoi((out_format+10)));
2874                   }
2875         else if ( strm ( out_format, "transitions"))
2876                   {
2877                     output_transitions (out_file, D1->A);
2878                   }
2879         
2880         else if ( strncmp (out_format, "statistics",10)==0)
2881                 {
2882                   if (!D1)return 1;
2883                   
2884                   output_statistics (out_file, D1->A,out_format+10);
2885                 }
2886
2887
2888
2889
2890         else if ( strm4 (out_format, "newick_tree","newick","binary","nh"))
2891                 {
2892                   if (!D1)return 1;
2893                   
2894                   /*D1->T=unroot_tree(D1->T);*/
2895                   vfclose (print_tree ((D1->T), out_format, vfopen ( out_file, "w")));
2896                 }
2897         else if ( strncmp (out_format, "sarsim", 6)==0)
2898                 {
2899                   if (!D1)return 1;
2900                   compare_sar_sequence (D1->S, (D2 &&D2->S)?D2->S:D1->S, atoi(out_format+6));
2901                   return EXIT_SUCCESS;
2902                 }
2903         else if ( strncmp (out_format, "sim",3)==0)
2904                 {
2905                   if (!D1)return 1;
2906                   output_similarities (out_file, D1->A,out_format);
2907                 }
2908
2909         else if ( strncmp (out_format, "cov",3)==0)
2910                 {
2911                   if (!D1)return 1;
2912                   output_similarities (out_file, D1->A,out_format);
2913                 }
2914         else if ( strm (out_format, "stockholm_aln"))
2915           {
2916             output_stockholm_aln (out_file,D1->A, (D2)?D2->A:NULL);
2917           }
2918         else if ( strm (out_format, "pair_sim"))
2919           {
2920             if ( !D2)
2921               {
2922                 fprintf ( stderr, "\n-output=pair_sim: provide aln1 via -in and aln2 via -in2 [FATAL:%s]\n", PROGRAM);
2923                 myexit (EXIT_FAILURE);
2924               }
2925             output_similarities_pw (out_file, D1->A,D2->A,out_format);
2926           }
2927         else if ( strm (out_format, "matrix") || strm (out_format, "blast_matrix"))
2928           {
2929             output_blast_mat (D1->M, out_file);
2930           }
2931         else if ( strm (out_format, "header_matrix"))
2932           {
2933             output_header_mat(D1->M, out_file);
2934           }
2935                   
2936         else 
2937                 {
2938
2939                     fprintf ( stderr, "\n%s is an UNKNOWN OUTPUT FORMAT [FATAL:%s]\n",out_format, PROGRAM); 
2940                     myexit (EXIT_FAILURE);
2941                     
2942                 }
2943         
2944         //Remove the expansion
2945         if ( expanded)
2946           {
2947             free_aln (D1->A);
2948             D1->A=BUF_A;
2949           }
2950         return 0;
2951         }
2952 int is_in_format_list ( char *name)
2953         {
2954         if ( strcmp ( name, "saga_aln")==0)return 1;
2955         if ( strcmp ( name, "number_aln")==0)return 1;
2956         if ( strcmp ( name, "clustal_aln")==0)return 1; 
2957         if ( strcmp ( name, "fasta_aln")==0)return 1;
2958         if ( strcmp ( name, "number_fasta")==0)return 1;
2959         if ( strcmp ( name, "fasta_seq")==0)return 1;
2960         if ( strcmp ( name, "pdb")==0)return 1;
2961         if ( strcmp ( name, "msf_aln")==0)return 1;
2962         if ( strcmp ( name, "dali_aln")==0)return 1;
2963         if ( strcmp ( name, "dali_seq")==0)return 1;
2964         if ( strcmp ( name, "barton_list_tc")==0)return 1;
2965         if ( strcmp ( name, "est_prf")==0)return 1;
2966         
2967         if ( strcmp ( name, "gotoh_aln")==0)return 1;
2968         if ( strcmp ( name, "amps_aln")==0)return 1;
2969         if ( strcmp ( name, "pir_aln")==0)return 1;
2970         if ( strcmp ( name, "pir_seq")==0)return 1;
2971         if ( strcmp ( name, "est_fasta")==0)return 1;
2972         if ( strcmp ( name, "amps_sd_scores")==0)return 1;
2973         if ( strcmp ( name, "pima_aln")==0)return 1;
2974         if ( strcmp ( name, "dialign_aln")==0)return 1;
2975         if ( strcmp ( name, "gor_seq")==0)return 1;
2976         if ( strcmp ( name, "gor_struc")==0)return 1;
2977         if ( strcmp ( name, "stockholm_aln")==0)return 1;
2978         
2979         return 0;
2980         }
2981 int is_struc_in_format_list ( char *name)
2982         {
2983         if ( strcmp ( name, "rna_number")==0)return 1;
2984         if ( strcmp ( name, "fasta_seq")==0)return 1;
2985         return 0;
2986         }
2987 char *format_name2aln_format_name (char *name)
2988         {
2989           if ( strm (name, "gcg"))sprintf (name, "msf");
2990           else if ( strm (name, "fasta"))sprintf (name, "fasta_aln");
2991           return name;
2992         }
2993 int is_out_format_list ( char *name)
2994         {
2995           return main_output (NULL, NULL, NULL, name, NULL);    
2996         }
2997         
2998 int is_struc_out_format_list ( char *name)
2999         {
3000           return main_output (NULL, NULL, NULL, name, NULL);    
3001         }       
3002
3003 /**************************************************************************************************/
3004 /*************************************REFORMAT UTIL*************************************************/
3005 /**************************************************************************************************/
3006
3007 /*************************************REFORMAT IN**************************************************/
3008 /**************************************************************************************************/
3009 /*******************************************************************************************/
3010 /*                                                                                         */
3011 /*                                                                                         */
3012 /*                               READ COG FILE                                             */
3013 /*                                                                                         */
3014 /***************************************************************************************** */
3015          
3016 /*******************************************************************************************/
3017 /*                                                                                         */
3018 /*                                                                                         */
3019 /*                               INPUT WEIGHTS                                            */
3020 /*                                                                                         */
3021 /***************************************************************************************** */
3022         
3023 Weights* get_amps_sd_scores ( char *fname)
3024         {
3025         FILE *fp;
3026         char *buf;
3027         char *buf2;
3028         int nseq;
3029         Weights *W;
3030         int a, b,e;
3031         int c;
3032         float array[20];
3033         
3034         buf=vcalloc ( 1001, sizeof (char));
3035         buf2=vcalloc ( 1001, sizeof (char));
3036         
3037         fp=vfopen ( fname, "r");
3038         set_fp_id ( fp, "Index");
3039         buf=fgets ( buf, 1000, fp);
3040         fscanf ( fp, "%s", buf2);       
3041         
3042         nseq=0;
3043         while ( isalnum(buf2[0]) && !isalpha(buf2[0]))
3044                 {
3045                 nseq++;
3046                 buf=fgets ( buf, 1000, fp);
3047                 fscanf ( fp, "%s", buf2);
3048                 }
3049         vfclose ( fp);
3050         
3051         W=declare_weights (nseq);
3052         
3053         fp=vfopen ( fname, "r");
3054         set_fp_id ( fp, "Index");
3055         buf=fgets ( buf, 1000, fp);
3056         fscanf ( fp, "%s", buf2);       
3057         
3058         a=0;
3059         while ( isalnum(buf2[0]) && !isalpha(buf2[0]))
3060                 {
3061                 fp=set_fp_after_char (fp, '>');
3062                 fscanf ( fp, "%s",W->seq_name[a]);
3063                 buf=fgets ( buf, 1000, fp);
3064                 fscanf ( fp, "%s", buf2);
3065                 a++;
3066                 }
3067         buf=fgets ( buf, 1000, fp);
3068         c=1;
3069         while ( c!=0)
3070                 {
3071                 for ( e=0; e< 16; e++)
3072                         {
3073                         c=fscanf ( fp, "%f", &array[e]);
3074                         }
3075                 fscanf ( fp, "\n");
3076                 if ( c!=0)
3077                         {
3078                         
3079                         a=(int)array[0]-1;
3080                         b=(int)array[1]-1;
3081                         W->PW_ID[b][a]=W->PW_ID[a][b]=array[9];
3082                         W->PW_SD[b][a]=W->PW_SD[a][b]=array[14];
3083                         }
3084                 
3085                 }
3086         vfclose ( fp);
3087         sprintf ( W->comments, "SD WEIGHTS GENERATED WITH THE PROGRAM AMPS IN PAIRWISE MODE");
3088         vfree ( buf);
3089         return W;
3090         }
3091
3092 Weights *read_seq_weight (char **name, int nseq, char* seq_weight)
3093        {
3094        int a, p;
3095        Weights *W;
3096        float w;
3097        
3098        FILE *fp;
3099        char line[LONG_STRING];
3100        char sname[MAXNAMES];
3101        
3102        
3103        /*Read sequence weights:
3104         * comment
3105         name1 weight1
3106         .....
3107
3108
3109         NOTE:
3110         weights must be between 0 and 1;
3111         
3112         sequences not in S do not get any weight
3113         sequences in S but not in file get a weight of 1
3114        */
3115        if ( !is_single_seq_weight_file (seq_weight))
3116          {
3117            fprintf ( stderr, "\nERROR: File %s is not in Format SINGLE_SEQ_WEIGHT_FORMAT_01 [FATA:%s]", seq_weight,PROGRAM);
3118            myexit (EXIT_FAILURE);
3119            return NULL;
3120          }
3121        else
3122          {
3123            W=declare_weights(nseq);
3124            for ( a=0; a< nseq; a++)
3125              {
3126                sprintf ( W->seq_name[a], "%s", name[a]);
3127                W->SEQ_W[a]=1;
3128              }
3129            sprintf ( W->mode, "%s", seq_weight);
3130            fp=vfopen (seq_weight, "r");
3131            
3132            
3133            while ( fgets( line,LONG_STRING-1, fp))
3134              {
3135                if ( line[0]=='*' ||line[0]=='#' || isblanc(line));
3136                else
3137                  {
3138                    if (sscanf(line, "%s %f", sname, &w)!=2)continue;
3139                    if ( (p=name_is_in_list ( sname, W->seq_name, nseq, MAXNAMES-1))!=-1)
3140                      {
3141                        W->SEQ_W[p]=w;
3142                      }
3143                  }
3144              }
3145            vfclose (fp);
3146            return W;
3147          }
3148        }
3149        
3150   
3151 /*******************************************************************************************/
3152 /*                                                                                         */
3153 /*                                                                                         */
3154 /*                               INPUT MISC                                               */
3155 /*                                                                                         */
3156 /***************************************************************************************** */
3157
3158 char *** read_rename_file ( char *fname, int code)
3159 {
3160   int n;
3161   FILE *fp;
3162   char ***convert=NULL;
3163   
3164   convert=declare_arrayN(3, sizeof (char),count_n_line_in_file(fname) +1,2,MAXNAMES+1);
3165   fp=vfopen (fname, "r");
3166   n=0;
3167   if ( code==CODE)      while ( fscanf ( fp, "%s %s\n", convert[n][0], convert[n][1])==2)n++;
3168   else if (code==DECODE)while ( fscanf ( fp, "%s %s\n", convert[n][1], convert[n][0])==2)n++;
3169   vfclose (fp);
3170   return convert;
3171 }
3172
3173 void get_barton_list_tc_seq ( char *in_file)
3174         {
3175         FILE *fp, *fp_make, *fp_length, *fp_long;
3176         FILE *fp_small[9];
3177         
3178         static char *buf;
3179         int len_buf=10000;
3180         char name[100];
3181         
3182         char pwd[100];
3183         int a,c,nseq;
3184         int k=0;
3185         int *length;
3186         int longest=0;
3187         
3188         c=0;
3189         length=vcalloc ( 1000, sizeof(int));
3190         if ( buf==NULL)buf=vcalloc ( len_buf, sizeof (char));
3191         fp=vfopen (in_file, "r");
3192         fp_long=vfopen ( "barton_seq_list_large", "w");
3193         fp_make=vfopen ( "make_dir", "w");
3194         fp_length=vfopen ( "barton_length", "w");
3195         for ( a=0; a< 9; a++)
3196                 {
3197                 sprintf ( name, "barton_nseq%d",a);
3198                 fp_small[a]=vfopen ( name, "w");
3199                 }
3200         get_pwd (pwd);
3201         
3202         
3203         while ( c!=EOF)
3204                 {a=0;
3205                 while ( (c=fgetc(fp))!='#');
3206                 while ( (c=fgetc(fp))=='#');
3207                 ungetc ( c, fp);
3208                 while ( (c=fgetc(fp))!='#')buf[a++]=c;
3209                 buf[a]='\0';
3210                 
3211                 sprintf ( name, "%s", buf);
3212         
3213                 while ( (c=fgetc(fp))=='#');
3214                 
3215                 if ( c!=EOF)
3216                         {
3217                         a=0;
3218                         while ( (c=fgetc(fp))!='#' && c!=EOF)
3219                                 {
3220                                 buf[a++]=c;
3221                                 if (a==len_buf)
3222                                         {
3223                                         len_buf+=10000;
3224                                         buf=vrealloc ( buf, len_buf*sizeof (char));
3225                                         }
3226                                 } 
3227                         buf[a]='\0';
3228                         if (c!=EOF)
3229                                 {
3230                                 
3231                                 nseq=process_barton_entry ( buf,name);
3232                                 length[nseq]++;
3233                                 longest=(longest<nseq)?nseq:longest;
3234                                 
3235                                 if ( nseq<=8) fprintf ( fp_small[nseq], "%s.pep\n", name);
3236                                 else fprintf ( fp_long, "%s.pep\n",name);
3237                                 fprintf ( fp_make, "mkdir %s\nmv %s.pep %s\nmv %s.check %s\n", name, name, name, name, name);
3238                                 k++;
3239                                 }
3240                         
3241                                 
3242                         }
3243                 }
3244         
3245         vfclose (fp);
3246         vfclose (fp_long);
3247         for ( a=0; a< 9; a++)vfclose (fp_small[a]);
3248         vfclose (fp_make);
3249         for ( a=0; a<= longest; a++)fprintf ( fp_length, "%d: %d\n", a, length[a]);
3250         vfclose ( fp_length);
3251         
3252         }
3253         
3254 int process_barton_entry (char *buf, char *name)                        
3255     {       
3256     Alignment *LA;
3257     Sequence *LS;
3258     int a,c;
3259     static char *buf2;
3260     int clen=0;
3261     int current=0;
3262     int p=0;
3263     int max_len_seq=0;
3264     int min_len_seq=999999;
3265     int nseq=0;
3266     int l;
3267     char fname[100];
3268     char com_name[100];
3269     int rm_gap=1;
3270
3271     sprintf ( fname, "%s.pep", name);
3272     sprintf ( com_name, "%s.check",name);
3273     
3274     if ( buf2==NULL)buf2=vcalloc ( 10000, sizeof (char));
3275     a=0;                
3276     while (buf[a]!='\0')
3277                 {
3278                  if (buf[a]=='>')
3279                         {
3280                         a=get_string_line (a,2, buf, buf2); 
3281                         while ((c=buf[a++])!='*')
3282                                 if (isalnum (c)|| c=='.' || c=='-')
3283                                         clen++;
3284                          max_len_seq=(clen> max_len_seq)?clen: max_len_seq;
3285                          min_len_seq=(clen< min_len_seq)?clen: min_len_seq;
3286                          nseq++;
3287                          clen=0;
3288                         }
3289                 if ( buf[a]!='\0')a++;
3290                 }
3291     
3292     
3293     LS=declare_sequence (  min_len_seq,  max_len_seq,  nseq); 
3294     LS->nseq=nseq;
3295     
3296     
3297     for (a=0, current=0; current< nseq; current++) 
3298         {
3299         a=get_string_line ( a, 1, buf, buf2);
3300         sscanf ( buf2, ">P1;%s", LS->name[current]);
3301         a=get_string_line ( a, 1, buf, buf2);
3302         l=strlen ( buf2);
3303         buf2[l-1]='\0';
3304         sprintf ( LS->seq_comment[current],"%s", buf2);
3305         
3306         p=0;
3307         while ( (c=buf[a++])!='*')
3308                 {
3309                 if (isalpha (c))
3310                         LS->seq[current][p++]=tolower (c);
3311                 else if ( isgraph(c))
3312                         LS->seq[current][p++]=(c);
3313                 }       
3314         a++;
3315         }
3316     
3317     LA=declare_Alignment(LS);
3318     seq2aln ( LS, LA,rm_gap);
3319     output_fasta_seq (fname,LA);
3320     output_pir_check (com_name,LA->nseq, LA->seq_comment);
3321     free_Alignment ( LA);
3322     free_sequence ( LS, nseq);   
3323     
3324     return nseq;
3325     }
3326
3327         
3328         
3329
3330 Structure *read_rna_struc_number (Alignment *A,char *fname)
3331         {
3332         FILE *fp;
3333         int a;
3334         char x,y;
3335         float f;
3336         Sequence *SA;
3337         Structure *ST;
3338         int first, last;
3339         
3340         SA=declare_sequence ( A->len_aln, A->len_aln, 1);
3341         SA->len[0]=A->len[0];
3342         for ( a=0; a< SA->len[0]; a++)
3343                 SA->seq[0][a]='.';
3344         ST=declare_rna_structure_num (SA);
3345         ST->S=SA;
3346         
3347         fp=vfopen ( fname, "r");
3348         fscanf ( fp, "%c\n%d\n",&x, &(ST)->tot_list);
3349         for ( a=0; a<(ST)->tot_list; a++)
3350                 {
3351                 fscanf ( fp, "%d %d %d %c %c %f\n", &(ST)->list[a][0],&(ST)->list[a][1],&(ST)->list[a][2], &x, &y, &f);
3352                 (ST)->list[a][0]--;
3353                 (ST)->list[a][1]--;
3354                 (ST)->list[a][2]--;
3355                 if ( a==0)
3356                         {
3357                         (ST)->stem[0][0]=(ST)->list[a][0];
3358                         (ST)->stem[0][1]=a;
3359                         }
3360                 else if ( (ST)->stem[(ST)->tot_stem][0]==(ST)->list[a][0]);
3361                 else if ( (ST)->stem[(ST)->tot_stem][0]!=(ST)->list[a][0])
3362                         {
3363                         (ST)->stem[(ST)->tot_stem][2]=a-1;
3364                         (ST)->tot_stem++;
3365                         (ST)->stem[(ST)->tot_stem][0]=(ST)->list[a][0];
3366                         (ST)->stem[(ST)->tot_stem][1]=a;
3367                         }
3368                         
3369                 SA->seq[0][(ST)->list[a][1]]='-';
3370                 SA->seq[0][(ST)->list[a][2]]='-';
3371                 }
3372         (ST)->stem[(ST)->tot_stem][2]=a-1;      
3373         (ST)->tot_stem++;
3374         for ( a=0; a< (ST)->tot_stem; a++)
3375                 {
3376         
3377                 first=(ST)->stem[a][1];
3378                 last=(ST)->stem[a][2];
3379                 SA->seq[0][(ST)->list[first][1]]='>';
3380                 SA->seq[0][(ST)->list[first][2]]='<';
3381                 SA->seq[0][(ST)->list[last][1]]='>';
3382                 SA->seq[0][(ST)->list[last][2]]='<';    
3383                 }
3384         
3385         return ST;      
3386         }
3387                   
3388 Structure * declare_rna_structure_num (Sequence *SA)
3389         {
3390         Structure *ST;
3391         ST=vcalloc ( 1, sizeof ( Structure));
3392         ST->list=declare_int ( SA->len[0], 3);
3393         ST->stem=declare_int ( SA->len[0], 3);
3394         return ST;
3395         }
3396 char ** read_lib_list (char *name, int *n)
3397 {
3398
3399   char **lines;
3400   char **list;
3401   int a, b, l;
3402   
3403   lines=file2lines (name);
3404   l=atoi (lines[0]);
3405   
3406   list=vcalloc (l, sizeof (char*));
3407   for ( n[0]=0,a=1; a<l; a++,b++)
3408     if ( !strstr (lines[a], "TC_LIB_LIST_FORMAT_01"))list[n[0]++]=lines[a];
3409   vfree (lines);
3410   return list;
3411 }
3412   
3413 /*******************************************************************************************/
3414 /*                                                                                         */
3415 /*                                                                                         */
3416 /*                               INPUT SEQ                                                */
3417 /*                                                                                         */
3418 /***************************************************************************************** */
3419 char ***read_group ( char *file)
3420 {
3421   /*Format: Fasta like, the name fo the group followed with the name of the sequences
3422     ><Group name> <First Seq> <second seq> ....
3423     Groups must NOT be overlaping
3424     list[group_index][0]="number of sequences"
3425     list[group_index][1]="group name"
3426     list[group_index][2...N]="sequence"
3427   */ 
3428
3429   FILE *fp;
3430   char *buf;
3431   char ***list;
3432   int a, c, l;
3433
3434
3435   
3436   l=measure_longest_line_in_file (file)+1;
3437   buf=vcalloc (l, sizeof (char)); 
3438   list=vcalloc ( count_n_line_in_file (file )+1, sizeof (char**));
3439   
3440   fp=vfopen (file, "r");
3441   
3442   a=0;
3443   while ((c=fgetc(fp))!=EOF)
3444     {
3445       buf=fgets (buf,l-1, fp);
3446       if ( c=='>')list[a++]=string2list (buf);
3447     }
3448   vfclose (fp);
3449   vfree (buf);
3450   return list;
3451 }
3452
3453
3454 static Sequence* get_pdb_sequence_from_field   (char *fname, char *field);
3455
3456 Sequence* get_pdb_sequence   (char *fname)
3457 {
3458   Sequence *S;
3459
3460   if ( (S=get_pdb_sequence_from_field(fname, "SEQRES"))!=NULL);
3461   else if ( (S=get_pdb_sequence_from_field(fname, "ATOM"))!=NULL)
3462     {
3463       add_warning (stderr,"Warning: Read Sequence from ATOM field in %s [%s:WARNING]", fname, PROGRAM);
3464     }
3465   else
3466     {
3467       add_warning ( stderr, "\nWARNING: failed to extract sequence from %s [%s:WARNING]\n", fname, PROGRAM);
3468       S=NULL;
3469     }
3470   return S;
3471 }
3472
3473
3474 static Sequence* get_pdb_sequence_from_field   (char *fname, char *field)
3475 {
3476         char *tp_name;
3477         char *command;
3478         char *pdbid;
3479         Sequence *S;
3480
3481
3482         command=vcalloc ( LONG_STRING, sizeof (char));
3483         tp_name=vtmpnam (NULL);
3484         sprintf ( command, "extract_from_pdb -seq_field %s -chain FIRST -infile \'%s\' -mode fasta > %s", field, check_file_exists(fname), tp_name);
3485 //      printf("CO: %s\n", command);
3486 //      char *x = vcalloc ( LONG_STRING, sizeof (char));
3487 //      sprintf(x, "cp %s ~/Desktop/erg.txt", tp_name);
3488 //      my_system(x);
3489         if ( getenv4debug ("DEBUG_EXTRACT_FROM_PDB"))fprintf ( stderr, "\n[DEBUG_EXTRACT_FROM_PDB:get_pdb_seq] %s\n", command);
3490         my_system ( command);
3491
3492
3493         S=get_fasta_sequence ( tp_name, NULL);
3494         if (S==NULL)return NULL;
3495         
3496         if ( (pdbid=get_pdb_id (fname))){sprintf ( S->name[0], "%s",pdbid);vfree (pdbid);} 
3497         S->nseq=1;
3498
3499         sprintf ( S->file[0], "%s", fname);
3500         S->max_len=S->min_len=S->len[0];
3501         if ( S->len[0]==0)
3502         {
3503                 free_sequence (S, -1);
3504                 S=NULL;
3505         }
3506
3507         vremove ( tp_name);
3508         vfree ( command);
3509         
3510         return S;
3511 }
3512
3513 char * get_pdb_file   ( char *fname)
3514      {
3515          char *file;
3516          int a, c;
3517          FILE *fp;
3518          
3519
3520          a=0;
3521          file=vcalloc ( sizeof (char),count_n_char_in_file ( fname)+1);
3522          fp=vfopen ( fname, "r");
3523          while ( (c=fgetc(fp))!=EOF)file[a++]=c;
3524          file[a]='\0'; 
3525          return file;
3526      }
3527          
3528 Sequence* get_struc_gor ( char *fname)
3529     {
3530     int nseq, min_len, max_len;
3531     int a, c;
3532     int len;
3533     char name[STRING];
3534     
3535
3536     FILE *fp;
3537     Sequence *S;
3538
3539     min_len=max_len=-1;
3540     fp=vfopen ( fname, "r");
3541     nseq=0;
3542     while ( (c=fgetc(fp))!=EOF)
3543             {
3544             if ( c!='!');
3545             else
3546                 {
3547                 nseq++;
3548                 fscanf ( fp, "%s %d", name, &len);
3549                 if (min_len==-1)min_len=max_len=len;
3550                 else
3551                     {
3552                     min_len=(len>min_len)?min_len:len;
3553                     max_len=(len>max_len)?len:max_len;
3554                     }
3555                 }
3556             
3557             }
3558     vfclose (fp);
3559    
3560     S=declare_sequence (  min_len,  max_len+1,nseq); 
3561     S->nseq=0;
3562     
3563     fp=vfopen (fname,"r");      
3564      while ( (c=fgetc(fp))!=EOF)
3565              {
3566              if ( c!='!');
3567              else
3568                 {
3569                 fscanf ( fp, "%s %d\n",S->name[S->nseq], &(S->len[S->nseq]));
3570                 
3571                 while ( (c=fgetc(fp))!='\n');
3572         
3573                 for ( a=0; a<S->len[S->nseq]; a++)
3574                     fscanf ( fp, " %*c %c %*f %*f %*f\n",&(S->seq[S->nseq][a]));
3575                 
3576                 S->seq[S->nseq][a]='\0';
3577                 while ( (c=fgetc(fp))!='!' && c!=EOF);
3578                 ungetc (c, fp);
3579                 S->nseq++;
3580                 }
3581              
3582              }
3583     vfclose (fp);
3584     return S;           
3585     }
3586                 
3587 Sequence* get_sequence_dali (char *fname)
3588     {
3589     Sequence *LS;
3590     FILE *fp;
3591     int c;
3592
3593     char name[100];
3594     int clen=0;
3595     int current=0;
3596     int p=0;
3597     int max_len_seq=0;
3598     int min_len_seq=999999;
3599     int nseq=0;
3600     
3601     if ((fp=vfopen (fname,"r"))==NULL)
3602          {printf ( "\nCOULDN'T OPEN %s",fname);
3603           myexit(EXIT_FAILURE);
3604          }  
3605     c=fgetc(fp);
3606     while (c!=EOF)
3607                 {
3608                  if (isdigit(c))
3609                         {
3610                         ungetc(c, fp);
3611                         fscanf (fp, "%s",name);
3612                         while (!isdigit(c=fgetc(fp)) && c!=EOF)
3613                                 if (isalnum (c) || c=='.' || c=='-')
3614                                         clen++;
3615                          max_len_seq=(clen> max_len_seq)?clen: max_len_seq;
3616                          min_len_seq=(clen< min_len_seq)?clen: min_len_seq;
3617                          nseq++;
3618                         clen=0;
3619                         }
3620                 else
3621                     c=fgetc (fp);
3622                 }
3623     vfclose (fp);
3624                 
3625     LS=declare_sequence (  min_len_seq,  max_len_seq+1,nseq); 
3626     LS->nseq=nseq;
3627     
3628     fp=vfopen (fname,"r");
3629     
3630     current=0;
3631     c=fgetc(fp);
3632         while (c!=EOF)
3633                 {
3634                 if (isdigit(c))
3635                         {
3636                         ungetc(c, fp);
3637                         fscanf_seq_name (fp, LS->name[current]);
3638                         p=0;
3639                         while (!isdigit(c=fgetc(fp)) && c!=EOF)
3640                                 {
3641                                 if (isalpha (c))
3642                                     LS->seq[current][p++]=tolower (c);
3643                                 else if ( c=='.')
3644                                     LS->seq[current][p++]='-';
3645                                 else if ( c=='-')
3646                                     LS->seq[current][p++]='-';
3647                                 }           
3648                         LS->seq[current][p]='\0';
3649                         LS->len[current]=strlen ( LS->seq[current]);
3650                         current++;
3651                         }
3652                 else
3653                     c=fgetc ( fp);
3654                 }
3655
3656     vfclose (fp);
3657     
3658     
3659     return LS;
3660     }   
3661
3662 Sequence* get_dialign_sequence (char *fname)
3663     {
3664     Sequence *LS;
3665     FILE *fp;
3666     int c;
3667
3668     char name[10000];
3669     int clen=0;
3670     int current=0;
3671     int p=0;
3672     int max_len_seq=0;
3673     int min_len_seq=999999;
3674     int nseq=0, l=0;
3675     char *buf;
3676     
3677     buf=vcalloc ( 1000, sizeof (char));
3678     if ((fp=vfopen (fname,"r"))==NULL)
3679          {printf ( "\nCOULDN'T OPEN %s",fname);
3680           myexit(EXIT_FAILURE);
3681          }  
3682     c=fgetc(fp);
3683     while (c!=EOF)
3684                 {
3685                  if (c=='>')
3686                         {fscanf (fp, "%s",name);
3687                         
3688                         buf=fgets ( buf, 1000, fp);
3689                         while ((c=fgetc(fp))!='>' && c!=EOF && c!=' ' && c!='\t')
3690                                 if (isalnum (c)|| is_gap(c))
3691                                         clen++;
3692                          max_len_seq=(clen> max_len_seq)?clen: max_len_seq;
3693                          min_len_seq=(clen< min_len_seq)?clen: min_len_seq;
3694                          nseq++;
3695                         clen=0;
3696                         }
3697                 else
3698                     c=fgetc (fp);
3699                 }
3700     vfclose (fp);
3701                 
3702     LS=declare_sequence (  min_len_seq,  max_len_seq, nseq); 
3703     LS->nseq=nseq;
3704     
3705     fp=vfopen (fname,"r");
3706     
3707     current=0;
3708     c=fgetc(fp);
3709         while (c!=EOF)
3710                 {
3711                 if (c=='>')
3712                         {
3713                           
3714                         fscanf_seq_name (fp, LS->name[current]);
3715                         l=strlen ( LS->name[current]);
3716                         if ( LS->name[current][l-1]==','||LS->name[current][l-1]==',')LS->name[current][l-1]='\0';
3717                         buf=fgets ( buf, 1000, fp);
3718                         p=0;
3719                         while ((c=fgetc(fp))!='>' && c!=EOF && c!=EOF && c!=' ' && c!='\t')
3720                                 if (isalpha (c))
3721                                     LS->seq[current][p++]=tolower (c);
3722                                 else if ( isgraph(c))
3723                                     LS->seq[current][p++]=(c);
3724                         LS->seq[current][p]='\0';
3725                         LS->len[current]=strlen ( LS->seq[current]);
3726                         current++;
3727                         }
3728                 else
3729                     c=fgetc ( fp);
3730                 }
3731
3732     vfclose (fp);
3733     return LS;
3734     }
3735
3736 Sequence* get_pima_sequence (char *fname)
3737     {
3738     Sequence *LS;
3739
3740     FILE *fp;
3741     int c;
3742
3743     char name[10000];
3744     int clen=0;
3745     int current=0;
3746     int p=0;
3747     int max_len_seq=0;
3748     int min_len_seq=999999;
3749     int nseq=0, l=0, len=0;
3750     char *buf, *buf2;
3751     char prefix[1000];
3752     
3753     sprintf (  prefix, "%s",fname);
3754     
3755     buf=strstr(prefix, "-");
3756     buf[0]='\0';
3757     len=strlen (prefix);
3758         
3759    
3760     
3761     buf=vcalloc ( 1000, sizeof (char));
3762     if ((fp=vfopen (fname,"r"))==NULL)
3763          {printf ( "\nCOULDN'T OPEN %s",fname);
3764           myexit(EXIT_FAILURE);
3765          }  
3766     c=fgetc(fp);
3767     while (c!=EOF)
3768                 {
3769                  if (c=='>')
3770                         {
3771                           fscanf_seq_name (fp,name);
3772                           if ( strlen(name)>=len && strncmp ( name, prefix, len)==0)
3773                                 {
3774                                   c=fgetc(fp);
3775                                 }
3776                           else
3777                                 {
3778                                 
3779                                 buf=fgets ( buf, 1000, fp);
3780                                 while ((c=fgetc(fp))!='>' && c!=EOF)
3781                                         if (isalnum (c)|| is_gap(c))
3782                                                 clen++;
3783                                  max_len_seq=(clen> max_len_seq)?clen: max_len_seq;
3784                                  min_len_seq=(clen< min_len_seq)?clen: min_len_seq;
3785                                 nseq++;
3786                                 clen=0;
3787                                 }
3788                         }
3789                 else
3790                         c=fgetc (fp);
3791                 }
3792     vfclose (fp);
3793                 
3794     LS=declare_sequence (  min_len_seq,  max_len_seq, nseq); 
3795     LS->nseq=nseq;
3796     
3797     fp=vfopen (fname,"r");
3798     
3799     current=0;
3800     c=fgetc(fp);
3801         while (c!=EOF)
3802                 {
3803                 if (c=='>')
3804                         {
3805                         fscanf_seq_name (fp,LS->name[current]);
3806                         if ( strlen(LS->name[current])>=len && strncmp ( LS->name[current], prefix, len)==0)
3807                                 c=fgetc (fp);
3808                         else
3809                                 {
3810                                 buf2=strstr (LS->name[current], ".");
3811                                 if ( buf2!=NULL) buf2[0]='\0';
3812                                  
3813                                 l=strlen ( LS->name[current]);
3814                                 if ( LS->name[current][l-1]==','||LS->name[current][l-1]==',')LS->name[current][l-1]='\0';
3815                                 buf=fgets ( buf, 1000, fp);
3816                                 p=0;
3817                                 while ((c=fgetc(fp))!='>' && c!=EOF)
3818                                         if (isalpha (c))
3819                                             LS->seq[current][p++]=tolower (c);
3820                                         else if ( isgraph(c))
3821                                             LS->seq[current][p++]=(c);
3822                                 LS->seq[current][p]='\0';
3823                                 LS->len[current]=strlen ( LS->seq[current]);
3824                                 current++;
3825                                 }
3826                         }
3827                 else
3828                     c=fgetc ( fp);
3829                 }
3830
3831     vfclose (fp);
3832     return LS;
3833     }
3834
3835 Sequence* perl_reformat2fasta (char *perl_command, char *fname)
3836     {
3837       char command[1000];
3838       char *file;
3839
3840       file=vtmpnam (NULL);
3841       
3842       check_program_is_installed ( perl_command,"", perl_command,EMAIL,IS_FATAL);
3843       sprintf ( command, "%s %s > %s", perl_command, fname, file);
3844       my_system ( command);
3845       return get_fasta_sequence (file, NULL);
3846     }
3847 Sequence* get_fasta_sequence_num (char *fname, char *comment_out)
3848     {
3849     Sequence *LS;
3850     char *buffer;
3851     FILE *fp;
3852     int a;
3853
3854     int   c;
3855     char *name;
3856     int clen=0;
3857     int current=0;
3858     int p=0;
3859     int max;
3860     int max_len_seq=0;
3861     int min_len_seq=0;
3862     int nseq=0, l=0;
3863  
3864     
3865     
3866     
3867     int *sub;
3868     
3869     buffer=vcalloc (1000, sizeof (char)); 
3870     name=vcalloc ( 100, sizeof (char));
3871
3872     nseq=count_n_char_x_in_file(fname, '>');
3873     min_len_seq=max=count_n_char_in_file(fname);
3874     sub=vcalloc (max+1, sizeof (int));
3875
3876     fp=vfopen (fname,"r");
3877
3878     
3879     c=fgetc(fp);
3880     while (c!=EOF)
3881                 {
3882                  if (c=='>')
3883                         {
3884                         fscanf_seq_name (fp,name);
3885                         while ((c=fgetc(fp))!='\n' && c!=EOF);
3886                         while ((c=fgetc(fp))!='>' && c!=EOF)
3887                                 if (isalnum (c)|| is_gap(c))
3888                                         clen++;
3889                          max_len_seq=(clen> max_len_seq)?clen: max_len_seq;
3890                          min_len_seq=(clen< min_len_seq)?clen: min_len_seq;
3891                          clen=0;
3892                         }
3893                 else
3894                     c=fgetc (fp);
3895                  
3896                 }  
3897
3898     vfclose (fp);               
3899     LS=declare_sequence (  min_len_seq,  max_len_seq,nseq); 
3900     
3901     LS->nseq=nseq;
3902     
3903     fp=vfopen (fname,"r");
3904     current=0;
3905     c=fgetc(fp);
3906     while (c!=EOF)
3907                 {
3908                 if (c=='>')
3909                         {
3910                         
3911                         fscanf_seq_name (fp,LS->name[current]);
3912                         l=strlen ( LS->name[current]);
3913                         if ( LS->name[current][l-1]==','||LS->name[current][l-1]==';')LS->name[current][l-1]='\0';
3914                         LS->name[current]=translate_name ( LS->name[current]);
3915                         a=0;
3916                         while ((c=fgetc(fp))!='\n' && c!=EOF && a<(COMMENT_SIZE-1))LS->seq_comment[current][a++]=c;
3917                         LS->seq_comment[current][a]='\0';
3918
3919
3920                         p=0;
3921                         while ((c=fgetc(fp))!='>' && c!=EOF)
3922                                 {
3923                                 if (isalnum (c))
3924                                     LS->seq[current][p++]=c;
3925                                 else if (is_gap(c))
3926                                     LS->seq[current][p++]=c;
3927                                 }
3928                         LS->seq[current][p]='\0';
3929                         LS->len[current]=strlen ( LS->seq[current]);
3930
3931                         current++;
3932                         }
3933                 else
3934                     c=fgetc ( fp);
3935                 }
3936                         
3937     
3938     vfclose (fp);
3939     
3940
3941     vfree (sub);
3942     vfree (name);
3943     vfree (buffer);
3944     return LS;
3945     }
3946
3947 Sequence *get_tree_file_list ( char *fname)
3948 {
3949
3950   char ***list;
3951   char *tmp;
3952   int a;
3953   FILE *fp;
3954   
3955   tmp=vtmpnam (NULL);
3956   list=file2list (fname, "\n");
3957   fp=vfopen (tmp, "w");
3958   a=0;
3959   while (list[a] && !isspace(list[a][1][0]))
3960     {
3961       char *s;
3962       s=file2string (list[a][1]);
3963       fprintf ( fp, ">%s\n%s\n", list[a][1], (s)?s:"");
3964       a++;
3965     }
3966   vfclose (fp);
3967   free_arrayN((void ***)list, 3);
3968   return get_fasta_tree (tmp, NULL);
3969 }
3970 Sequence *get_file_list ( char *fname)
3971 {
3972
3973   char ***list;
3974   char *tmp;
3975   int a;
3976   FILE *fp;
3977   
3978   tmp=vtmpnam (NULL);
3979   list=file2list (fname, "\n");
3980   fp=vfopen (tmp, "w");
3981   a=0;
3982   while (list[a] && !isspace(list[a][1][0]))
3983     {
3984
3985       fprintf ( fp, ">%s\n", list[a][1]);
3986       a++;
3987     }
3988   vfclose (fp);
3989   free_arrayN((void ***)list, 3);
3990   return get_fasta_sequence (tmp, NULL);
3991 }
3992 Sequence*get_fasta_tree (char *fname, char *comment_out)
3993 {
3994   Sequence *LS;
3995     char *buffer;
3996     FILE *fp;
3997     int a;
3998
3999     int   c;
4000     char *name;
4001     int clen=0;
4002     int current=0;
4003     int p=0;
4004     int max;
4005     int max_len_seq=0;
4006     int min_len_seq=0;
4007     int nseq=0, l=0;
4008  
4009     
4010     
4011     
4012     int *sub;
4013     
4014     buffer=vcalloc (1000, sizeof (char)); 
4015     name=vcalloc ( 100, sizeof (char));
4016
4017     nseq=count_n_char_x_in_file(fname, '>');
4018     min_len_seq=max=count_n_char_in_file(fname);
4019     sub=vcalloc (max+1, sizeof (int));
4020
4021     fp=vfopen (fname,"r");
4022
4023     
4024     c=fgetc(fp);
4025     while (c!=EOF)
4026                 {
4027                  if (c=='>')
4028                         {
4029                         fscanf_seq_name (fp,name);
4030                         while ((c=fgetc(fp))!='\n' && c!=EOF);
4031                         while ((c=fgetc(fp))!='>' && c!=EOF)
4032                           if (isgraph(c))
4033                             clen++;
4034                          max_len_seq=(clen> max_len_seq)?clen: max_len_seq;
4035                          min_len_seq=(clen< min_len_seq)?clen: min_len_seq;
4036                          clen=0;
4037                         }
4038                 else
4039                     c=fgetc (fp);
4040                  
4041                 }  
4042
4043     vfclose (fp);               
4044     LS=declare_sequence (  min_len_seq,  max_len_seq,nseq); 
4045     
4046     LS->nseq=nseq;
4047     
4048     fp=vfopen (fname,"r");
4049     current=0;
4050     c=fgetc(fp);
4051     while (c!=EOF)
4052                 {
4053                 if (c=='>')
4054                         {
4055                         
4056                         fscanf_seq_name (fp,LS->name[current]);
4057                         l=strlen ( LS->name[current]);
4058                         if ( LS->name[current][l-1]==','||LS->name[current][l-1]==';')LS->name[current][l-1]='\0';
4059                         LS->name[current]=translate_name ( LS->name[current]);
4060                         a=0;
4061                         while ((c=fgetc(fp))!='\n' && c!=EOF && a<(COMMENT_SIZE-1))LS->seq_comment[current][a++]=c;
4062                         LS->seq_comment[current][a]='\0';
4063
4064                         
4065                         p=0;
4066                         while ((c=fgetc(fp))!='>' && c!=EOF)
4067                                 {
4068                                   LS->seq[current][p++]=c;
4069                                 }
4070
4071                         LS->seq[current][p]='\0';
4072                         LS->len[current]=strlen ( LS->seq[current]);
4073
4074                         current++;
4075                 
4076                         }
4077                         
4078                 else
4079                     c=fgetc ( fp);
4080                 }
4081                         
4082     
4083     vfclose (fp);
4084     
4085
4086     vfree (sub);
4087     vfree (name);
4088     vfree (buffer);
4089     
4090     return LS;
4091 }
4092 Sequence* get_fasta_sequence_raw (char *fname, char *comment_out)
4093     {
4094     Sequence *LS;
4095     char *buffer;
4096     FILE *fp;
4097     int a;
4098
4099     int   c;
4100     char *name;
4101     int clen=0;
4102     int current=0;
4103     int p=0;
4104     int max;
4105     int max_len_seq=0;
4106     int min_len_seq=0;
4107     int nseq=0, l=0;
4108  
4109     
4110     
4111     
4112     int *sub;
4113     
4114     buffer=vcalloc (1000, sizeof (char)); 
4115     name=vcalloc ( 100, sizeof (char));
4116
4117     nseq=count_n_char_x_in_file(fname, '>');
4118     min_len_seq=max=count_n_char_in_file(fname);
4119     sub=vcalloc (max+1, sizeof (int));
4120
4121     fp=vfopen (fname,"r");
4122
4123     
4124     c=fgetc(fp);
4125     while (c!=EOF)
4126                 {
4127                  if (c=='>')
4128                         {
4129                         fscanf_seq_name (fp,name);
4130                         while ((c=fgetc(fp))!='\n' && c!=EOF);
4131                         while ((c=fgetc(fp))!='>' && c!=EOF)
4132                           if (isgraph(c))
4133                             clen++;
4134                          max_len_seq=(clen> max_len_seq)?clen: max_len_seq;
4135                          min_len_seq=(clen< min_len_seq)?clen: min_len_seq;
4136                          clen=0;
4137                         }
4138                 else
4139                     c=fgetc (fp);
4140                  
4141                 }  
4142
4143     vfclose (fp);               
4144     LS=declare_sequence (  min_len_seq,  max_len_seq,nseq); 
4145     
4146     LS->nseq=nseq;
4147    
4148     fp=vfopen (fname,"r");
4149     current=0;
4150     c=fgetc(fp);
4151     while (c!=EOF)
4152                 {
4153                 if (c=='>')
4154                         {
4155                         
4156                         fscanf_seq_name (fp,LS->name[current]);
4157                         l=strlen ( LS->name[current]);
4158                         if ( LS->name[current][l-1]==','||LS->name[current][l-1]==';')LS->name[current][l-1]='\0';
4159                         LS->name[current]=translate_name ( LS->name[current]);
4160                         a=0;
4161                         while ((c=fgetc(fp))!='\n' && c!=EOF && a<(COMMENT_SIZE-1))LS->seq_comment[current][a++]=c;
4162                         LS->seq_comment[current][a]='\0';
4163
4164                         
4165                         p=0;
4166                         while ((c=fgetc(fp))!='>' && c!=EOF)
4167                                 {
4168                                   //if (c<'A')c+='z';
4169                                   if (c!='\n')LS->seq[current][p++]=c;
4170                                 }
4171
4172                         LS->seq[current][p]='\0';
4173                         LS->len[current]=strlen ( LS->seq[current]);
4174
4175                         current++;
4176                 
4177                         }
4178                         
4179                 else
4180                     c=fgetc ( fp);
4181                 }
4182                         
4183     
4184     vfclose (fp);
4185     
4186
4187     vfree (sub);
4188     vfree (name);
4189     vfree (buffer);
4190     return LS;
4191     }
4192 Sequence* get_fasta_sequence (char *fname, char *comment_out)
4193     {
4194     Sequence *LS;
4195     Sequence *pdb_S;
4196     int a;
4197
4198     char *pdb_name;
4199     
4200     char *buffer;
4201     FILE *fp;
4202
4203     int   c;
4204     char *name;
4205     int clen=0;
4206     int current=0;
4207     int p=0;
4208     int max;
4209     int max_len_seq=0;
4210     int min_len_seq=0;
4211     int nseq=0, l=0;
4212     char *sub;
4213     int disk=0;
4214     int coor=0;
4215
4216     
4217        
4218     buffer=vcalloc (1000, sizeof (char)); 
4219     name=vcalloc ( 10000, sizeof (char));
4220
4221     nseq=count_n_char_x_in_file(fname, '>');
4222     if (disk==1 || get_int_variable ("use_disk") || getenv ("SEQ_ON_DISK_4_TCOFFEE")){disk=1;}
4223     if ( nseq==0)
4224       {
4225         vfree (buffer); vfree (name);
4226         return NULL;
4227       }
4228     
4229     min_len_seq=max=count_n_char_in_file(fname);
4230     sub=vcalloc (max+1, sizeof (char));
4231
4232     fp=vfopen (fname,"r");
4233
4234     nseq=0;
4235     c=fgetc(fp);
4236     while (c!=EOF)
4237                 {
4238                  if (c=='>')
4239                         {
4240                           nseq++;
4241                           fscanf_seq_name (fp,name);
4242                           while ((c=fgetc(fp))!='\n' && c!=EOF);
4243                           while ((c=fgetc(fp))!='>' && c!=EOF)
4244                             {
4245                               if (isalnum (c)|| is_gap(c))
4246                                 sub[clen++]=c;
4247                             }
4248                           
4249                           if (strm (sub, "PDB"))
4250                             {
4251                               pdb_name=get_pdb_struc(name,0, 0);
4252                               pdb_S=get_pdb_sequence (pdb_name);
4253                               if (pdb_S)
4254                                 {
4255                                   clen=strlen( pdb_S->seq[0]);
4256                                   free_sequence ( pdb_S,1);
4257                                 }
4258                               else
4259                                 clen=0;
4260                               
4261                             }
4262                                                   
4263                           max_len_seq=(clen> max_len_seq)?clen: max_len_seq;
4264                           min_len_seq=(clen< min_len_seq)?clen: min_len_seq;
4265                           clen=0;
4266                         }
4267                  else
4268                    c=fgetc (fp);
4269                  
4270                 }  
4271     
4272     vfclose (fp);       
4273     
4274     
4275     if ( disk==0)
4276       LS=declare_sequence (  min_len_seq,  max_len_seq,nseq); 
4277     else
4278       {
4279         LS=declare_sequence (0,0,nseq);
4280         for (a=0; a<nseq; a++)LS->seq[a]=NULL;
4281       }
4282     LS->nseq=nseq;
4283     
4284     fp=vfopen (fname,"r");
4285     current=0;
4286     c=fgetc(fp);coor++;
4287     
4288     while (c!=EOF)
4289                 {
4290                 if (c=='>')
4291                         {
4292                           coor+=fscanf_seq_name (fp, LS->name[current]);
4293
4294                           
4295                           l=strlen ( LS->name[current]);
4296                           if ( LS->name[current][l-1]==','||LS->name[current][l-1]==';')LS->name[current][l-1]='\0';
4297                           LS->name[current]=translate_name ( LS->name[current]);                        
4298                           a=0;
4299                           while ((c=fgetc(fp))!='\n' && c!=EOF && a<(COMMENT_SIZE-1)){LS->seq_comment[current][a++]=c;coor++;}
4300                           coor++;
4301                           
4302                           LS->seq_comment[current][a]='\0';
4303                           
4304                           p=0;
4305                           while ((c=fgetc(fp))!='>' && c!=EOF)
4306                             {
4307                               coor++;
4308                               
4309                               if (!isspace(c))
4310                                 {
4311                                   if (p==0)LS->dc[current][0]=coor;
4312                                   
4313                                   if (disk==0)LS->seq[current][p++]=c;
4314                                   else p++;
4315                                 }
4316                               
4317                               LS->dc[current][1]=coor;
4318                             }
4319                           coor++;
4320                           
4321                           if ( disk==0)LS->seq[current][p]='\0';
4322                           
4323                           if (LS->seq[current] && strm (LS->seq[current], "PDB"))
4324                             {
4325                               
4326                               pdb_name=get_pdb_struc(LS->name[current],0, 0);
4327                               pdb_S=get_pdb_sequence (pdb_name);
4328                               if (pdb_S)
4329                                 {
4330                                   sprintf ( LS->seq[current], "%s", pdb_S->seq[0]);
4331                                   clen=strlen( pdb_S->seq[0]);
4332                                   free_sequence ( pdb_S, 1);
4333                                 }
4334                               else
4335                                 {
4336                                   add_warning (stderr, "WARNING: Could not fetch PDB file: %s", pdb_name);
4337                                 }
4338                             }
4339                         
4340                         
4341                           LS->len[current]=p;
4342                           current++;            
4343                         }
4344                         
4345                 else
4346                   {
4347                     c=fgetc ( fp);
4348                     coor++;
4349                   }
4350                 }
4351      
4352     vfclose (fp);
4353     vfree (sub);
4354     vfree (name);
4355     vfree (buffer);
4356     //LS=clean_sequence (LS);
4357    
4358     return LS;
4359     }
4360
4361 Sequence* get_sub_fasta_sequence (char *fname, char *comment_out)
4362     {
4363     Sequence *LS;
4364     
4365     FILE *fp;
4366
4367     int c;
4368     char name[100];
4369     int clen=0;
4370     int current=0;
4371     int p=0;
4372     int max;
4373     int max_len_seq=0;
4374     int min_len_seq=0;
4375     int nseq=0, l=0;
4376     char *buf;
4377     
4378     
4379     
4380     int *sub;
4381
4382     nseq=count_n_char_x_in_file(fname, '>');
4383     min_len_seq=max=count_n_char_in_file(fname);
4384     sub=vcalloc (max+1, sizeof (int));
4385     buf=vcalloc ( max+1, sizeof (char));
4386     fp=vfopen (fname,"r");
4387
4388     
4389     c=fgetc(fp);
4390     while (c!=EOF)
4391                 {
4392                  if (c=='>')
4393                         {
4394                         fscanf_seq_name (fp,name);
4395                         while ((c=fgetc(fp))!='\n' && c!=EOF);
4396                         buf=fgets ( buf,max, fp);
4397                         while ((c=fgetc(fp))!='>' && c!=EOF)
4398                                 if (isalnum (c)|| is_gap(c))
4399                                         clen++;
4400                          max_len_seq=(clen> max_len_seq)?clen: max_len_seq;
4401                          min_len_seq=(clen< min_len_seq)?clen: min_len_seq;
4402                          clen=0;
4403                         }
4404                 else
4405                     c=fgetc (fp);
4406                  
4407                 }  
4408
4409     vfclose (fp);               
4410     LS=declare_sequence (  min_len_seq,  max_len_seq,nseq); 
4411     LS->nseq=nseq;
4412     
4413     fp=vfopen (fname,"r");
4414     current=0;
4415     c=fgetc(fp);
4416     while (c!=EOF)
4417                 {
4418                 if (c=='>')
4419                         {
4420                         
4421                         fscanf_seq_name (fp,LS->name[current]);
4422                         l=strlen ( LS->name[current]);
4423                         if ( LS->name[current][l-1]==','||LS->name[current][l-1]==';')LS->name[current][l-1]='\0';
4424                         LS->name[current]=translate_name ( LS->name[current]);
4425                         while ((c=fgetc(fp))!='\n' && c!=EOF);
4426                 
4427                         p=0;
4428                         while ((c=fgetc(fp))!='>' && c!=EOF)
4429                                 {
4430                                 if (isalpha (c))
4431                                     LS->seq[current][p++]=tolower (c);
4432                                 else if (is_gap(c))
4433                                     LS->seq[current][p++]=(c);                          
4434                                 }
4435
4436                         LS->seq[current][p]='\0';
4437                         LS->len[current]=strlen ( LS->seq[current]);
4438
4439                         current++;
4440                 
4441                         }
4442                         
4443                 else
4444                     c=fgetc ( fp);
4445                 }
4446                         
4447     
4448     vfclose (fp);
4449     
4450
4451     vfree (sub);
4452     return LS;
4453     }
4454 Sequence* get_pir_sequence (char *fname, char *comment_out)
4455     {
4456     Sequence *LS;
4457
4458     FILE *fp;
4459     int c;
4460
4461     char name[100];
4462     int clen=0;
4463     int current=0;
4464     int p=0;
4465     int max_len_seq=0;
4466     int min_len_seq=999999;
4467     int nseq=0, l=0;
4468     char *buf;
4469     
4470     buf=vcalloc ( 1000, sizeof (char));
4471     if ((fp=vfopen (fname,"r"))==NULL)
4472          {printf ( "\nCOULDN'T OPEN %s",fname);
4473           myexit(EXIT_FAILURE);
4474          }  
4475     c=fgetc(fp);
4476     while (c!=EOF)
4477                 {
4478                  if (c=='>')
4479                         {
4480                         if ( (c=fgetc(fp))=='P')while ( (c=fgetc(fp))!=';');
4481                         else ungetc ( c, fp);
4482                         fscanf_seq_name (fp,name);
4483                         
4484                         buf=fgets ( buf, 1000, fp);
4485                         while ((c=fgetc(fp))!='>' && c!=EOF)
4486                                 if (isalnum (c)|| is_gap(c))
4487                                         clen++;
4488                          max_len_seq=(clen> max_len_seq)?clen: max_len_seq;
4489                          min_len_seq=(clen< min_len_seq)?clen: min_len_seq;
4490                          nseq++;
4491                         clen=0;
4492                         }
4493                 else
4494                     c=fgetc (fp);
4495                 }
4496     vfclose (fp);
4497
4498
4499    
4500     LS=declare_sequence (  min_len_seq,  max_len_seq,nseq); 
4501     LS->nseq=nseq;
4502     
4503     fp=vfopen (fname,"r");
4504     
4505     current=0;
4506     c=fgetc(fp);
4507         while (c!=EOF)
4508                 {
4509                 if (c=='>')
4510                         {
4511                         if ( (c=fgetc(fp))=='P')while ( (c=fgetc(fp))!=';');
4512                         else ungetc ( c, fp);
4513
4514                         fscanf_seq_name (fp,LS->name[current]);
4515                 
4516                         l=strlen ( LS->name[current]);
4517                         if ( LS->name[current][l-1]==','||LS->name[current][l-1]==',')LS->name[current][l-1]='\0';
4518                         LS->name[current]=translate_name ( LS->name[current]);
4519                         buf=fgets ( buf, 1000, fp);
4520                         
4521                         LS->seq_comment[current]=fgets ( LS->seq_comment[current],COMMENT_SIZE-1, fp);
4522                         LS->seq_comment[current][strlen(LS->seq_comment[current])-1]='\0';
4523                         p=0;
4524                         while ((c=fgetc(fp))!='>' && c!=EOF)
4525                                 if (isalpha (c))
4526                                     LS->seq[current][p++]=tolower (c);
4527                                 else if ( !isspace(c) && c!='*')
4528                                     LS->seq[current][p++]=(c);
4529                         LS->seq[current][p]='\0';
4530                         LS->len[current]=strlen ( LS->seq[current]);
4531                         current++;
4532                         }
4533                 else
4534                     c=fgetc ( fp);
4535                 }
4536
4537     vfclose (fp);
4538     if (comment_out!=NULL) output_pir_check ( comment_out,LS->nseq, LS->seq_comment);
4539     return LS;
4540     }
4541
4542 Sequence* get_gor_sequence (char *fname, char *comment_out)
4543     {
4544     Sequence *LS;
4545
4546     FILE *fp;
4547     int c;
4548
4549     char name[100];
4550     int clen=0;
4551     int current=0;
4552     int p=0;
4553     int max_len_seq=0;
4554     int min_len_seq=99999;
4555     int nseq=0;
4556     char *buf;
4557     
4558     buf=vcalloc ( 1000, sizeof (char));
4559     if ((fp=vfopen (fname,"r"))==NULL)
4560          {printf ( "\nCOULDN'T OPEN %s",fname);
4561           myexit(EXIT_FAILURE);
4562          }  
4563     c=fgetc(fp);
4564     while (c!=EOF)
4565                 {
4566                  if (c=='!')
4567                         {
4568                         fscanf_seq_name (fp,name);
4569                         
4570                         buf=fgets ( buf, 1000, fp);
4571                         while ((c=fgetc(fp))!='!' && c!=EOF)
4572                                 if (isalnum (c)|| is_gap(c))
4573                                         clen++;
4574                          max_len_seq=(clen> max_len_seq)?clen: max_len_seq;
4575                          min_len_seq=(clen< min_len_seq)?clen: min_len_seq;
4576                          nseq++;
4577                         clen=0;
4578                         }
4579                 else
4580                     c=fgetc (fp);
4581                 }
4582     vfclose (fp);
4583                 
4584     LS=declare_sequence (  min_len_seq,  max_len_seq,nseq); 
4585     LS->nseq=nseq;
4586     
4587     fp=vfopen (fname,"r");
4588     
4589     current=0;
4590     c=fgetc(fp);
4591         while (c!=EOF)
4592                 {
4593                 if (c=='!')
4594                         {
4595                         
4596                        
4597                         fscanf_seq_name (fp,LS->name[current]);
4598                         LS->name[current]=translate_name ( LS->name[current]);
4599                         buf=fgets ( buf, 1000, fp);
4600                         
4601                         p=0;
4602                         while ((c=fgetc(fp))!='!' && c!=EOF)
4603                                 if (isalnum (c)|| is_gap(c))
4604                                     LS->seq[current][p++]=tolower (c);
4605                                 
4606                         LS->seq[current][p]='\0';
4607                         LS->len[current]=strlen ( LS->seq[current]);
4608                         current++;
4609                         }
4610                 else
4611                     c=fgetc ( fp);
4612                 }
4613
4614     vfclose (fp);
4615
4616     return LS;
4617     }
4618 Sequence* get_swissprot_sequence (char *fname, char *comment_out)
4619     {
4620     Sequence *LS;
4621     FILE *fp;
4622     int c;
4623     char *buf;    
4624     int nseq=0;
4625     int len, max_len_seq=0, min_len_seq=0;
4626     
4627     if ( !check_file_exists(fname))
4628       {printf ( "\nCOULDN'T OPEN %s",fname);
4629           myexit(EXIT_FAILURE);
4630       }  
4631
4632     buf=vcalloc (LONG_STRING+1, sizeof (char));
4633     fp=NULL;   
4634     while ( (fp=find_token_in_file(fname,fp,"\nSQ")))
4635       {
4636         nseq++;
4637         fgets (buf, LONG_STRING, fp);
4638         len=0;
4639         while ((c=fgetc(fp))!='/')if(isalpha(c))len++;
4640         if ( max_len_seq==0)max_len_seq=min_len_seq=len;
4641         else
4642           {
4643             max_len_seq=MAX(len, max_len_seq);
4644             min_len_seq=MIN(len, min_len_seq);
4645           }
4646       }
4647
4648     LS=declare_sequence (  min_len_seq,  max_len_seq,nseq);     
4649     LS->nseq=0;
4650     
4651     fp=NULL;
4652     while ( (fp=find_token_in_file(fname,fp,"\nID")))
4653       {
4654         fscanf_seq_name (fp, LS->name[LS->nseq]);
4655         fp=find_token_in_file(fname,fp,"\nSQ");
4656         fgets (buf, LONG_STRING, fp);
4657         while ((c=fgetc(fp))!='/')if (isalpha(c))LS->seq[LS->nseq][LS->len[LS->nseq]++]=c;
4658         LS->seq[LS->nseq][LS->len[LS->nseq]]='\0';
4659         LS->nseq++;
4660       }
4661
4662    
4663     return LS;
4664     }
4665 int fscanf_seq_name ( FILE *fp, char *sname)
4666 {
4667   static char *name;
4668   int r;
4669   if ( !sname) return 0;
4670   
4671   if ( !name)name=vcalloc ( 10000, sizeof (char));
4672   fscanf (fp, "%s", name);
4673   r=strlen (name);
4674   if ( strlen (name)>MAXNAMES)
4675     add_warning (stderr, "\nWARNING: Seq Name Too long: [%s]. Truncated to %d", name, MAXNAMES);
4676   name[MAXNAMES]='\0';
4677   sprintf ( sname, "%s", name);
4678   return r;
4679 }
4680
4681 /*******************************************************************************************/
4682 /*                                                                                         */
4683 /*                                                                                         */
4684 /*                               INPUT ALN                                                 */
4685 /*                                                                                         */
4686 /***************************************************************************************** */
4687 void undump_msa ( Alignment *A, char *tmp)
4688 {
4689   FILE *fp;
4690   int m;
4691   char *buf;
4692   int index;
4693   
4694   if ( !A || !tmp || !check_file_exists (tmp))return;
4695   m=measure_longest_line_in_file (tmp );
4696   A=realloc_aln2 ( A,A->max_n_seq,m+1);
4697
4698   buf=vcalloc (m+1, sizeof (char));
4699   fp=vfopen (tmp, "r");
4700   while (fscanf (fp, "%d %s\n", &index, buf)==2)
4701     {
4702       sprintf ( A->seq_al[index], "%s", buf);
4703     }
4704   vfclose (fp);
4705   vfree (buf);
4706 }
4707 void dump_msa ( char *file,Alignment *A, int nseq, int *lseq)
4708 {
4709   FILE *fp;
4710   int a;
4711   fp=vfopen (file, "w");
4712   for (a=0; a<nseq; a++)
4713     fprintf ( fp, "%d %s\n", lseq[a], A->seq_al[lseq[a]]);
4714   vfclose (fp);
4715 }
4716
4717 void read_aln (char *file_name, Alignment *A)
4718 {
4719   char *tmp_name;
4720   Sequence *S;
4721   
4722
4723   tmp_name=vtmpnam (NULL);
4724   
4725   if (printf_system ( "clustalw_aln2fasta_aln.pl %s > %s",file_name, tmp_name)!=EXIT_SUCCESS)
4726     {
4727       printf_exit ( EXIT_FAILURE, stderr, "Could Not Read File %s [FATAL:%s]\n", file_name, PROGRAM);
4728     }
4729   else
4730     {
4731       S=get_fasta_sequence ( tmp_name,NULL);
4732       A=seq2aln (S, A, 0);  
4733     }
4734   return;
4735 }
4736 void read_stockholm_aln (char *file_name, Alignment *A)
4737 {
4738   char *tmp_name;
4739   Sequence *S;
4740   
4741
4742   tmp_name=vtmpnam (NULL);
4743   if (printf_system ( "clustalw_aln2fasta_aln.pl %s > %s",file_name, tmp_name)!=EXIT_SUCCESS)
4744     {
4745       printf_exit ( EXIT_FAILURE, stderr, "Could Not Read File %s [FATAL:%s]\n", file_name, PROGRAM);
4746     }
4747   else
4748     {
4749       int a;
4750       S=get_fasta_sequence ( tmp_name,NULL);
4751       for (a=0; a<S->nseq; a++)
4752         {
4753           if (strstr (S->name[a], "_stockholm"))
4754             {
4755               substitute ( S->name[a], "_stockholmspace_", " ");
4756               substitute ( S->name[a], "_stockholmhasch_", "#");
4757             }
4758         }
4759       A=seq2aln (S, A, 0);  
4760     }
4761   return;
4762 }
4763 Alignment* read_blast_aln ( char *file_name, Alignment *A)
4764 {
4765   char *tmp_name;
4766
4767   int type;
4768
4769   
4770   if ( !(type=is_blast_file (file_name)))
4771     {
4772       myexit (EXIT_FAILURE);
4773     }
4774   tmp_name=vtmpnam ( NULL);
4775   if (type==BLAST_TXT)
4776     {
4777       printf_system("cat %s | blast_aln2fasta_aln.pl | fasta_aln2fasta_aln_unique_name.pl >%s", file_name, tmp_name);
4778     }
4779   else if (type==BLAST_XML)
4780     {
4781       
4782       printf_system("blast_xml2fasta_aln.pl %s >%s", file_name, tmp_name);
4783     }
4784
4785   main_read_aln (tmp_name, A);
4786   return A;
4787 }
4788
4789
4790 void read_number_aln ( char *file_name, Alignment *A)
4791    {
4792     FILE *fp, *fp2;
4793     int * ptr_aln;
4794     int a,b,d;
4795     int c;
4796     char *buf=NULL;
4797
4798     int tot=0;
4799     int flag=0;
4800     char *fname;   
4801     int n_comment=0;
4802
4803     int nseq=0;
4804     int max_len=0;
4805
4806     
4807     fp=vfopen ( file_name, "r");
4808     
4809     fname=vtmpnam(NULL);
4810     fp2=vfopen ( fname, "w");
4811     while ( (c=fgetc(fp))!=EOF)
4812         {
4813             fprintf ( fp2, "%c", c);
4814         }
4815     vfclose (fp);
4816     vfclose (fp2);
4817
4818   
4819     /*1 Count The number of sequences*/ 
4820     fp=vfopen ( fname, "r");
4821     buf=vfgets ( buf,fp);
4822     if ( !isblanc (buf));
4823     while ( isblanc (buf))
4824         {
4825           buf=vfgets ( buf, fp);
4826         }
4827     while (!isblanc (buf))
4828         {
4829         buf=vfgets ( buf,fp);
4830         }
4831     while ( !isalnum ((c=fgetc(fp))))
4832         {       
4833         ungetc(c,fp);
4834         buf=vfgets ( buf,fp);           
4835         }
4836     
4837     if ( c!='\n')ungetc(c,fp);
4838     
4839     while ( isalnum ((c=fgetc(fp))))
4840         {
4841         ungetc(c,fp);           
4842         a=0;
4843         while ( isgraph ((c=fgetc(fp))));                       
4844         nseq++;
4845         buf=vfgets ( buf, fp);
4846         }    
4847     vfclose (fp);
4848
4849     /*DONE*/
4850     /*2 get_max_len*/
4851     max_len=count_n_char_in_file(fname)/nseq;
4852     A=realloc_alignment2( A, nseq+1, max_len+1);
4853
4854     /*DONE*/
4855         
4856    
4857     fp=vfopen ( fname, "r");
4858     buf=vfgets ( buf, fp);
4859     if ( !isblanc (buf))sprintf (A->aln_comment[n_comment++], "%s", buf);
4860     while ( isblanc (buf))
4861         {
4862         buf=vfgets ( buf,fp);           
4863         }
4864     while (!isblanc (buf))
4865         {
4866         buf=vfgets ( buf, fp);
4867         sprintf ( A->aln_comment[n_comment++], "%s", buf);
4868         
4869         }
4870     while ( !isalnum ((c=fgetc(fp))))
4871         {       
4872         ungetc(c,fp);
4873         buf=vfgets ( buf, fp);
4874         
4875         }
4876     
4877     if ( c!='\n')ungetc(c,fp);
4878     
4879     while ( isalnum ((c=fgetc(fp))))
4880         {
4881         ungetc(c,fp);
4882         
4883         fscanf_seq_name (fp, A->name[A->nseq]); 
4884
4885         if ( name_is_in_list (A->name[A->nseq], A->name, A->nseq, 100)!=-1)
4886           {
4887             fprintf ( stderr, "\nWARNING (read_number_aln): Sequence %s Duplicated in File %s ", A->name[A->nseq], A->file[A->nseq]);
4888             if (!getenv("ALLOW_DUPLICATE"))
4889               {
4890                 fprintf ( stderr, " [FATAL:%s]\n", PROGRAM);
4891                 myexit (EXIT_FAILURE);
4892               }
4893           }      
4894         A->nseq++;
4895         buf=vfgets ( buf,fp);
4896         }
4897     
4898     vfclose (fp);
4899   
4900     
4901      
4902     if ((fp=vfopen ( fname, "r"))==NULL)
4903         printf ( "\nCOULDN'T READ %s", fname);
4904    
4905     ptr_aln=vcalloc ( A->nseq, sizeof(int));
4906     while ( flag==0)
4907         {
4908         while (  (c=fgetc(fp))!='\n');
4909         if ( (c=fgetc(fp))=='\n')
4910             flag=1;
4911         }
4912     while ( !isalnum(c=fgetc(fp)));
4913     ungetc ( c, fp);
4914     while ( c!=EOF)
4915         {
4916         tot=0;
4917         while(tot< A->nseq && c!=EOF)
4918             {
4919              b=0;
4920              while ( !isgraph (c=fgetc(fp)) && c!=EOF);
4921              if ( c!=EOF)ungetc(c, fp);
4922              while ( isgraph((buf[b++]=fgetc(fp))));
4923              buf[b-1]='\0';
4924              for ( a=-1,d=0; d< A->nseq; d++)
4925                 if ( strcmp (A->name[d], buf)==0)
4926                     {a=d;
4927                      tot++;
4928                     }
4929
4930              if ( a==-1) while ( (c=fgetc(fp))!='\n' && c!=EOF);
4931              else
4932                {
4933                  while ( (c=fgetc(fp))!='\n')
4934                    {
4935                      if ( isgraph(c) || is_gap(c))
4936                        {if ( isalpha(c))
4937                          c=(A->residue_case==2)?c:tolower(c);
4938                        
4939                        if (!isspace(c))A->seq_al[a][ptr_aln[a]++]=c;
4940                        }
4941                    }
4942                }
4943              }
4944          while ( !isalnum(c=getc(fp)) && c!=EOF);
4945          if ( c!=EOF)
4946             ungetc (c, fp);
4947          }
4948          
4949     vfclose (fp);
4950     
4951    
4952     for ( a=0; a< A->nseq; a++)
4953         {A->seq_al[a][ptr_aln[a]]='\0';
4954          A->order[a][0]=a;
4955          A->order[a][1]=0;
4956         }
4957     
4958     A->len_aln= strlen(A->seq_al[0]);  
4959     
4960     vfree (buf);
4961     vfree(ptr_aln);
4962     vremove (fname);
4963     
4964     }           
4965 void read_amps_aln ( char *in_file, Alignment *A)
4966         {
4967         FILE *fp;
4968         int a, b, c, cont=1;
4969         A->nseq=get_amps_seq_name ( A->name, in_file);
4970         
4971         fp=vfopen ( in_file, "r");
4972         fp=set_fp_id(fp, "1*");
4973         while ( (c=fgetc(fp))!='\n');
4974         b=0;
4975         while ( cont==1)
4976                 {
4977                 c=fgetc ( fp);
4978                 c=fgetc(fp);
4979                 if ( c=='*')
4980                         {
4981                         cont=0;
4982                         for ( a=0; a<A->nseq; a++)
4983                                 A->seq_al[a][b]='\0';
4984                         A->len_aln=b;
4985                         }
4986                          
4987                 else
4988                         {
4989                         ungetc (c, fp);
4990                         for ( a=0; a< A->nseq; a++)
4991                                 {
4992                                 c=fgetc(fp);
4993                                 if ( c==' ')A->seq_al[a][b]='-';
4994                                 else
4995                                         {
4996                                         A->seq_al[a][b]=c;
4997                                         A->len[a]++;
4998                                         }
4999                                 }
5000                         while ((c=fgetc(fp))!='\n');
5001                         b++;
5002                         }
5003                 }
5004         }
5005
5006
5007
5008
5009
5010
5011 int get_amps_seq_name ( char **name, char* fname)
5012         {
5013         FILE *fp;
5014         int nseq=0;
5015         
5016         fp=vfopen ( fname, "r");
5017         fp=set_fp_id ( fp, "Index");
5018         while ( (fgetc(fp))!='\n');
5019         while ( isspace(fgetc(fp)))
5020                 {fscanf (fp, "%*d >%s", name[nseq++]);
5021                  while ( (fgetc(fp))!='\n');
5022                 }
5023         vfclose ( fp);
5024         return nseq;
5025         }
5026 Alignment * read_gotoh_aln ( char *fname, Alignment *A)
5027    {
5028     FILE *fp;
5029     int * ptr_aln;
5030     int a,b,d,e;
5031
5032
5033     char *buf;
5034     char buf2[VERY_LONG_STRING+1];
5035     char buf3[VERY_LONG_STRING+1];
5036     char buf4[VERY_LONG_STRING+1];
5037
5038     int tot=0;
5039
5040     int l;
5041     int nseq, max_len;
5042     
5043    
5044     if ( !check_file_exists (fname))return NULL;
5045     fp=vfopen ( fname, "r");
5046
5047 /*1 GET THE NUMBER OF SEQUENCES*/
5048     nseq=0;
5049     buf=vcalloc ( VERY_LONG_STRING+1, sizeof (char));    
5050     while ( isblanc (buf=fgets ( buf, VERY_LONG_STRING, fp)));
5051     while (!isblanc (buf=fgets ( buf, VERY_LONG_STRING, fp)));
5052     while ( isblanc (buf=fgets ( buf, VERY_LONG_STRING, fp)));
5053     while ( !isblanc ( buf) && buf!=NULL)
5054         {
5055         a=-1;
5056         d=sscanf ( buf, "%d %s %s %s", &a, buf2, A->name[A->nseq],buf3);
5057         if ( a!=-1)
5058                 {
5059                 if ( name_is_in_list (A->name[A->nseq], A->name, A->nseq, 100)!=-1)
5060                   {
5061                     fprintf ( stderr, "\nWARNING (get_amps_seq_name): Sequence %s Duplicated in File %s ", A->name[A->nseq], A->file[A->nseq]);
5062                     if (!getenv("ALLOW_DUPLICATE"))
5063                       {
5064                         fprintf ( stderr, " [FATAL:%s]\n", PROGRAM);
5065                         myexit (EXIT_FAILURE);
5066                       }
5067                   }                 
5068                 nseq++;
5069                 fgets(buf, VERY_LONG_STRING, fp);
5070                 }
5071         else ( buf=NULL);
5072         }
5073     vfclose (fp);
5074 /*2 Get the MAX Len and Reallocate*/
5075     max_len=count_n_char_in_file(fname)/nseq;
5076     A=realloc_aln2( A, nseq+1, max_len+1);
5077 /*3 Get The Sequences Names*/
5078     A->nseq=0;
5079     fp=vfopen ( fname, "r");
5080     while ( isblanc (buf=fgets ( buf, VERY_LONG_STRING, fp)));
5081     while (!isblanc (buf=fgets ( buf, VERY_LONG_STRING, fp)));
5082     while ( isblanc (buf=fgets ( buf, VERY_LONG_STRING, fp)));
5083     while ( !isblanc ( buf) && buf!=NULL)
5084         {
5085         a=-1;
5086         d=sscanf ( buf, "%d %s %s %s", &a, buf2, A->name[A->nseq],buf3);
5087         if ( a!=-1)
5088                 {
5089                 if ( d==4)sprintf (A->name[A->nseq],"%s", buf3);        
5090                 A->nseq++;
5091                 fgets(buf, VERY_LONG_STRING, fp);
5092                 }
5093         else ( buf=NULL);
5094         }
5095     vfclose (fp);   
5096
5097 /*READ THE ALN*/     
5098     fp=vfopen ( fname, "r");
5099
5100     buf=vcalloc ( VERY_LONG_STRING+1, sizeof (char));;  
5101     ptr_aln=vcalloc ( A->nseq, sizeof(int));
5102     
5103     while ( isblanc (buf=fgets ( buf, VERY_LONG_STRING, fp)));
5104     while (!isblanc (buf=fgets ( buf, VERY_LONG_STRING, fp)));
5105     
5106     
5107     while ( isblanc (buf=fgets ( buf, VERY_LONG_STRING, fp)));
5108     
5109     while (buf!=NULL)
5110         {
5111         tot=0;
5112         while(tot< A->nseq)
5113             {
5114             
5115             e=sscanf (buf, "%d %s %s %s", &e, buf2, buf3, buf4);
5116             if ( e==4)sprintf( buf3, "%s", buf4);
5117             
5118             
5119             for ( d=0; d< A->nseq; d++)
5120                 {
5121                 
5122                 if ( strcmp (A->name[d], buf3)==0)
5123                     {a=d;
5124                      tot++;
5125                     }
5126                 }
5127              l=strlen (buf2);
5128              if ( buf2[l-1]=='|')l--;
5129              buf2[l]='\0';
5130             
5131              for (b=0; b<l; b++)
5132                 {
5133                 if ( isgraph (buf2[b]))
5134                         A->seq_al[a][ptr_aln[a]++]=(A->residue_case==2)?buf2[b]:tolower (buf2[b]);
5135                  }
5136              buf=fgets(buf, VERY_LONG_STRING, fp);      
5137              }
5138          if ( buf!=NULL)
5139                 {
5140                 buf=fgets(buf, VERY_LONG_STRING, fp);
5141                 while ( isblanc (buf) && buf!=NULL)
5142                         {
5143                         buf=fgets ( buf, VERY_LONG_STRING, fp);
5144                         }
5145                 }
5146          
5147          }
5148          
5149     vfclose (fp);
5150     
5151    
5152     for ( a=0; a< A->nseq; a++)
5153         {A->seq_al[a][ptr_aln[a]]='\0';
5154         }
5155     
5156     A->len_aln= strlen(A->seq_al[0]);  
5157     
5158    
5159     
5160     for ( a=0; a< A->nseq; a++)
5161         {
5162         for ( b=0; b< A->len_aln; b++)
5163                 A->len[a]+=1-is_gap(A->seq_al[a][b]);
5164         }
5165     for ( a=0, b=0; a< A->len_aln; a++)
5166         {
5167         if ( !is_gap(A->seq_al[0][a]) &&!is_gap(A->seq_al[1][a]))b++;
5168         }
5169     return A;
5170     }
5171     
5172
5173
5174
5175
5176 void read_msf_aln ( char *fname, Alignment *A)
5177    {
5178     char command[1000];
5179     char *tmp_name;
5180     Sequence *S;
5181     
5182     tmp_name=vtmpnam(NULL);
5183     sprintf ( command, "msf_aln2fasta_aln.pl %s > %s", fname, tmp_name);
5184
5185     if ( my_system (command)!=EXIT_SUCCESS)
5186       {
5187         fprintf ( stderr, "\nERROR: file %s does not have a legal msf format [FATAL:%s]", fname,PROGRAM);
5188         myexit (EXIT_FAILURE);
5189       }
5190
5191     S=get_fasta_sequence ( tmp_name,NULL);
5192     A=seq2aln (S, A, 0);  
5193     vremove (tmp_name);
5194     return;
5195     }           
5196
5197 /**************************************************************************************************/
5198 /*************************************REFORMAT OUT*************************************************/
5199 /**************************************************************************************************/
5200 /*******************************************************************************************/
5201 /*                                                                                         */
5202 /*                                                                                         */
5203 /*                               OUTPUT MATRICES                                           */
5204 /*                                                                                         */
5205 /***************************************************************************************** */
5206
5207
5208
5209 int output_freq_mat ( char *outfile, Alignment *A)
5210     { /*
5211         function documentation: start
5212         
5213         int output_freq_mat ( char *outfile, Aligmnent *A)
5214
5215         This function counts the number of residues in each column of an alignment (Prot)
5216         It outputs these values in the following format
5217
5218         A | 0 0 0 1 0
5219         B | 1 0 0 0 1
5220         - | 0 1 1 0 0
5221
5222         This format can be piped into:
5223         The routine used for computing the p-value  gmat-inf-gc-v2c
5224         
5225         function documentation: end
5226       */
5227       
5228     int a, b;
5229     int **freq_mat;
5230     FILE *fp;
5231     
5232     
5233     freq_mat=aln2count_mat (A);
5234             
5235     fp=vfopen ( outfile, "w");
5236     for ( b=0; b< 26; b++)
5237       {
5238         fprintf (fp, "%c |", 'A'+b);
5239         for ( a=0; a< A->len_aln; a++)fprintf (fp,"%d ", freq_mat[b][a]);
5240         fprintf (fp, "\n");
5241       }
5242     fprintf (fp, "- |");
5243     for ( a=0; a< A->len_aln; a++)fprintf (fp,"%d ", freq_mat[26][a]);
5244     
5245     free_int (freq_mat, -1);
5246     vfclose ( fp);
5247     return 1;
5248     }
5249 /*******************************************************************************************/
5250 /*                                                                                         */
5251 /*                                                                                         */
5252 /*                               OUTPUT P-Values                                           */
5253 /*                                                                                         */
5254 /***************************************************************************************** */   
5255 float output_maln_pval ( char *outfile, Alignment *A)
5256     {
5257       /*
5258         function documentation: start
5259         float output_maln_pval ( char *outfile, Aligmnent *A)
5260
5261         This function outputs the p-value of a multiple alignmnet as described 
5262         in Hertz, Stormo, Bioinformatics, 15-7/8, 563/577
5263             ftp beagle.colorado.edu /pub/cosensus
5264         Locally
5265             packages/consensus/gmat-inf-gc-v2c
5266         
5267         
5268         The routine used for computing the p-value is the program gmat-inf-gc-v2c
5269         function documentation: end
5270       */
5271
5272   
5273     char *mat;
5274     char *result;
5275     FILE *fp;
5276     float value;
5277     char command[LONG_STRING];
5278     char string[STRING];
5279     mat=vtmpnam (NULL);
5280     result=vtmpnam (NULL);
5281     
5282     output_freq_mat (mat,A);
5283     sprintf ( command, "more %s | gmat-inf-gc-v2c -A abcdefghijklmnopqrstuvwxyz> %s",mat, result);
5284     my_system ( command);
5285     
5286     if ( !check_file_exists(result))return 0;
5287     fp=find_token_in_file ( result, NULL, "ln(p-value):");
5288     
5289     fscanf ( fp, "%s",string);
5290     value=atof ( string);
5291     vfclose ( fp);
5292     
5293     vremove ( mat);
5294     vremove ( result);
5295     
5296     fp=vfopen ( outfile, "w");
5297     fprintf ( fp, "%.6f\n", value);
5298     vfclose ( fp);
5299     
5300     return value;
5301     }
5302               
5303     
5304 /*******************************************************************************************/
5305 /*                                                                                         */
5306 /*                                                                                         */
5307 /*                               OUTPUT WEIGHTS                                            */
5308 /*                                                                                         */
5309 /***************************************************************************************** */
5310 int output_seq_weights ( Weights *W, char *wfile)
5311         {
5312         FILE*fp;
5313         int a;
5314         
5315         if ( W==NULL)return 0;
5316         
5317         fp=vfopen (wfile, "w");
5318         if ( fp==NULL)return 0;
5319         
5320         
5321         for ( a=0; a< W->nseq; a++)
5322                 {
5323                 
5324                   fprintf ( fp, "%s %.2f\n", W->seq_name[a],W->SEQ_W[a]);
5325                 }
5326         vfclose ( fp);
5327         return 1;
5328         }  
5329 void output_pw_weights4saga ( Weights *W, float **w_list, char *wfile)
5330         {
5331         FILE*fp;
5332         int a, b;
5333         fp=vfopen (wfile, "w");
5334         
5335         fprintf ( fp, "%s\n$\n", W->comments); 
5336         for ( a=0; a< W->nseq-1; a++)
5337                 {
5338                 for (b=a+1; b< W->nseq; b++)
5339                         {
5340                         fprintf ( fp, "%s %s %f\n", W->seq_name[a], W->seq_name[b],w_list[a][b]);
5341                         }
5342                 }
5343         fprintf ( fp, "$\n");
5344         vfclose ( fp);
5345         }
5346
5347 FILE * display_weights (Weights *W, FILE *fp)
5348 {
5349   int a;
5350   int max_len;
5351   
5352   if ( W==NULL)
5353     {
5354       fprintf ( fp, "\n\nUN-WEIGHTED MODE: EVERY SEQUENCE WEIGHTS 1\n");
5355       return fp;
5356     }
5357   fprintf ( fp, "\n\nWEIGHTED MODE:%s\n\n", (W)->mode);
5358   for ( a=0, max_len=0; a< W->nseq; a++)max_len=MAX(max_len, strlen (W->seq_name[a]));
5359   for ( a=0; a< (W->nseq); a++)
5360     {
5361       fprintf ( fp, "\t%*s %.2f\n", max_len,(W)->seq_name[a],W->SEQ_W[a]);
5362     }
5363   fprintf ( fp, "\n");
5364   return fp;
5365 }
5366
5367 /*******************************************************************************************/
5368 /*                                                                                         */
5369 /*                                                                                         */
5370 /*                               OUTPUT SEQ                                                */
5371 /*                                                                                         */
5372 /***************************************************************************************** */
5373 int ** input_similarities (char *file, Alignment *A, char *mode)
5374 {
5375   int a, b, i, n;
5376   int **sim;
5377   float score;
5378   char name[1000];
5379   FILE *fp=NULL;
5380   char *buf1=NULL, *buf2=NULL;
5381   int new_aln=0;
5382   
5383
5384   
5385   if ( !check_file_exists (file) || !is_distance_matrix_file (file) ||!is_similarity_matrix_file (file) )
5386     {
5387       return NULL;
5388     }
5389   
5390   if ( A)
5391     {
5392       fp=vfopen (file, "r");
5393       while ((buf2=vfgets (buf1,fp))!=NULL )
5394         {
5395           if (strstr (buf2, "SEQ_INDEX"))
5396             {
5397               buf1=buf2;
5398               sscanf (buf1, "# SEQ_INDEX %s %d",name, &i);
5399               if ( !strm (A->name[i], name))
5400                 {
5401                   return NULL;
5402                 }
5403             }
5404         }
5405       vfclose (fp);
5406     }
5407   else
5408     {
5409   
5410       A=similarities_file2aln(file);
5411       new_aln=1;
5412     }
5413   
5414   sim=declare_int ( A->nseq, A->nseq);
5415   for ( a=0; a<A->nseq; a++)sim[a][a]=100;
5416   
5417   
5418   fp=find_token_in_file (file, NULL, "PW_SEQ_DISTANCES");
5419   fp=find_token_in_file (file, fp, "BOT");
5420   while ((buf2=vfgets (buf1,fp))!=NULL )
5421     {
5422       if ( !(strstr (buf2, "BOT\t") || strstr (buf2, "TOP\t")))continue;
5423       buf1=buf2;
5424       n=sscanf (buf1, "%*s %d %d %f", &a, &b, &score);
5425       if ( n!=3)
5426         {
5427           free_int (sim, -1);
5428           return NULL;
5429         }
5430       else sim[a][b]=sim[b][a]=(int)score;
5431     }
5432   vfclose (fp);
5433   vfree (buf1);
5434   if (new_aln)free_aln(A);
5435   return sim;
5436 }
5437
5438 Alignment * similarities_file2aln ( char *file)
5439 {
5440   int nseq=0, i;
5441   FILE *fp;
5442   char name[1000];
5443   Alignment *A;
5444   
5445
5446   fp=vfopen (file, "r");
5447   while ((fp=find_token_in_file (file,fp, "SEQ_INDEX")))nseq++;
5448   A=declare_aln2 (nseq+1, 10);
5449   
5450   while ((fp=find_token_in_file (file,fp, "SEQ_INDEX")))
5451     {
5452       fscanf (fp, "%s %d", name,&i);
5453       sprintf ( A->name[i], "%s", name);
5454     }
5455   A->nseq=nseq;
5456
5457   return A;
5458 }
5459   
5460 void output_similarities (char *file, Alignment *A, char *mode)
5461 {
5462   float s;
5463   float *tot;
5464   float bigtot=0;
5465   int n, max;
5466   FILE *fp;
5467   int a, b;
5468   char *p;
5469   int **M=NULL;
5470   for (max=0, a=0; a< A->nseq; a++)max=MAX(max,(strlen (A->name[a])));
5471   
5472
5473   tot=vcalloc ( A->nseq, sizeof (float));
5474   fp=vfopen (file, "w");
5475   fprintf (fp, "# TC_SIMILARITY_MATRIX_FORMAT_01\n");
5476   for ( a=0; a<A->nseq; a++)
5477     fprintf ( fp, "# SEQ_INDEX %s %d\n",A->name[a],a);
5478   fprintf ( fp, "# PW_SEQ_DISTANCES \n");
5479   for (n=0,a=0;a< A->nseq-1; a++)
5480     {
5481       for ( b=a+1; b<A->nseq; b++, n++)
5482         {
5483            if (strstr (mode, "_sarmat2"))
5484             {
5485               s=get_sar_sim (A->seq_al[a], A->seq_al[b]);
5486             }
5487           else if (strstr (mode, "_sar"))
5488             {
5489               s=get_sar_sim (A->seq_al[a], A->seq_al[b]);
5490             }
5491           else if ( (p=strstr (mode, "_memory_")))
5492             {
5493               int **sim;
5494               sscanf ( p, "_memory_%ld", (long int*)&sim);
5495               s=sim[a][b];
5496             }
5497           else if ( strstr (mode, "_idscore") || strstr ( mode, "_covscore"))
5498             {
5499               static Sequence *S;
5500               if (a==0 && b==1)
5501                 {
5502                   free_sequence (S, -1);
5503                   if ( strstr (mode, "idscoreDNA"))
5504                     M=read_matrice ("idmat");
5505                   else
5506                     M=read_matrice("blosum62mt");
5507               
5508                   S=aln2seq(A);
5509                 }
5510               if ( strstr (mode, "_idscore"))s=idscore_pairseq(S->seq[a], S->seq[b], -10,-1, M, "sim");
5511               else            s=idscore_pairseq(S->seq[a], S->seq[b], -10,-1, M, "cov");
5512             }
5513           else if ( strstr (mode, "cov"))
5514             {
5515               s=get_seq_sim ( A->seq_al[a], A->seq_al[b],GAP_LIST, "cov");
5516             }
5517           else
5518             {
5519               s=get_seq_fsim2 (A->seq_al[a], A->seq_al[b],GAP_LIST, mode);
5520             }
5521           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);
5522           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);
5523           tot[a]+=s;
5524           tot[b]+=s;
5525           bigtot+=s;
5526         }
5527     }
5528   for ( a=0; a< A->nseq; a++)
5529     {
5530       fprintf (fp, "AVG\t %d\t %*s\t %*s\t %5.2f\n", a,max,A->name[a], max, "*", tot[a]/(A->nseq-1));
5531       
5532     }
5533   vfree (tot);free_int (M, -1);
5534   fprintf (fp, "TOT\t %*s\t %*s\t %5.2f\n", max,"TOT", max, "*", bigtot/n);
5535   vfclose (fp);
5536 }
5537
5538 void output_similarities_pw (char *file, Alignment *A, Alignment *B,char *mode)
5539 {
5540   float s;
5541   float *tot;
5542   float bigtot=0;
5543   int n, max;
5544   FILE *fp;
5545   int a, b;
5546
5547   int **M=NULL;
5548   Sequence *SA, *SB;
5549   
5550   if ( strstr (mode, "idscoreDNA"))
5551     M=read_matrice ("idmat");
5552   else
5553     M=read_matrice("blosum62mt");
5554   
5555   SA=aln2seq(A);
5556   SB=aln2seq(B);
5557    
5558   for (max=0, a=0; a< A->nseq; a++)max=MAX(max,(strlen (A->name[a])));
5559   for (a=0; a< B->nseq; a++)max=MAX(max,(strlen (B->name[a])));
5560   
5561
5562   tot=vcalloc ( A->nseq, sizeof (float));
5563   fp=vfopen (file, "w");
5564   fprintf (fp, "# TC_SIMILARITY_MATRIX_FORMAT_01\n");
5565   for ( a=0; a<A->nseq; a++)
5566     fprintf ( fp, "# SEQ_INDEX %s %d\n",A->name[a],a);
5567   fprintf ( fp, "# PW_SEQ_DISTANCES \n");
5568   for (n=0,a=0;a< A->nseq; a++)
5569     {
5570       for ( b=0; b<B->nseq; b++, n++)
5571         {
5572           s=idscore_pairseq(SA->seq[a], SB->seq[b], -10,-1, M, "sim");
5573           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);
5574           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);
5575           tot[a]+=s;
5576           tot[b]+=s;
5577           bigtot+=s;
5578         }
5579     }
5580   
5581   for ( a=0; a< A->nseq; a++)
5582     {
5583       fprintf (fp, "AVG\t %d\t %*s\t %*s\t %5.2f\n", a,max,A->name[a], max, "*", tot[a]/(A->nseq-1));
5584     }
5585   vfree (tot);free_int (M, -1);
5586   fprintf (fp, "TOT\t %*s\t %*s\t %5.2f\n", max,"TOT", max, "*", bigtot/n);
5587   vfclose (fp);
5588 }
5589 void output_conservation_statistics ( char *file, Alignment *A)
5590 {
5591   int a, b, c,c1, c2;
5592   double **tot;
5593   char aa[1000];
5594   int naa;
5595   
5596   sprintf (aa, "%s", BLAST_AA_ALPHABET);
5597   naa=strlen (aa);
5598   
5599   tot=declare_double (256, 256);
5600   
5601   
5602   for ( a=0; a<A->nseq; a+=2)
5603     {
5604       b=a+1;
5605       for ( c=0; c<A->len_aln; c++)
5606         {
5607           c1=tolower (A->seq_al[a][c]);
5608           c2=tolower (A->seq_al[b][c]);
5609           if ( !is_gap(c1) && !is_gap(c2))
5610             {
5611               tot[c1][c2]++;
5612               tot[c2][c1]++;
5613               tot[c1][0]++;
5614               tot[c2][0]++;
5615               tot[0][0]++;
5616             }
5617         }
5618     }
5619   
5620   fprintf ( stdout, "# BLAST_MATRIX FORMAT\n#ALPHABET=%s\n",aa);
5621   for (a=0; a<naa; a++)fprintf ( stdout, "%3c ", toupper(aa[a]));
5622   fprintf ( stdout, "\n");
5623   for (a=0; a< naa; a++)
5624     {
5625       fprintf (stdout, "%c", toupper(aa[a]));
5626       for ( b=0; b< naa; b++)
5627         {
5628           float f1, f2, f3, r, v;
5629           c1=tolower(aa[a]);c2=tolower(aa[b]);
5630           f1=(float)((tot[c1][c2]*2)/tot[0][0]);
5631           f2=(float)((tot[c1][0])/tot[0][0]);
5632           f3=(float)((tot[c2][0])/tot[0][0]);
5633           r=(float)(f2==0 || f3==0)?0:(f1/(f2*f3));
5634           v=(r==0)?0:((float)10*log((double)r));
5635           fprintf (stdout, " %5d",(int)v);
5636         }
5637       fprintf ( stdout, "\n");
5638     }
5639 }
5640 void output_statistics (char *file, Alignment *A, char *mode)
5641     {
5642       FILE *fp;
5643       int a, b, c, d=0, n;
5644       int maxname=0;
5645
5646       
5647       if (!mode || !mode[0])
5648         mode="hnrglNL";
5649       else if ( mode[0]=='_')
5650         mode++;
5651       for ( a=0; a<A->nseq; a++)maxname=MAX(strlen(A->name[a]), maxname);
5652       maxname++;
5653       
5654       
5655       fp=vfopen (file, "w");
5656       
5657       if (mode[0]=='h')
5658         {
5659           b=0;
5660           while ((c=mode[b++])!='\0')
5661             {
5662               if ( c=='n') fprintf (fp, "%-*s ",maxname,"name");
5663               if ( c=='l') fprintf (fp, "%-*s ",5,"nres");
5664               if ( c=='g') fprintf (fp, "%-*s ",5,"ngap");
5665               if ( c=='t') fprintf (fp, "%-*s ",5,"len");
5666             }
5667           if (is_in_set ( c, "nlgt"))     fprintf (fp, "\n");
5668           mode++;
5669         }
5670       b=0;
5671       while ((c=mode[b++])!='\0')
5672         {
5673           if ( c=='n')break;
5674           if ( c=='N'){d=1;fprintf (fp, "NSEQ %d ", A->nseq);}
5675           if ( c=='L'){d=1;fprintf (fp, "LEN  %d ", A->len_aln);}         
5676         }
5677       if ( d) fprintf (fp, "\n");
5678       
5679       for (a=0; a<A->nseq; a++)
5680         {
5681           b=0;
5682           d=0;
5683           while ((c=mode[b++])!='\0')
5684             {
5685               if (is_in_set ( c, "nlgt"))d=1;
5686
5687               if (c=='n'){d=1;fprintf ( fp, "%-*s ", maxname,A->name[a]);}
5688               if (c=='l')
5689                 {
5690                   for (n=0,d=0; d<A->len_aln; d++)n+=!is_gap(A->seq_al[a][d]);
5691                   fprintf ( fp, "%-5d ",n);
5692                 }
5693               if (c=='g')
5694                 {
5695                   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')) ;
5696                   fprintf ( fp, "%-5d ",n); 
5697                 }
5698               if (c=='t')
5699                 {
5700                   fprintf ( fp, "%-5d ",(int)strlen (A->seq_al[a]));             
5701                 }
5702                if (c=='N' && d)
5703                 {
5704                  fprintf ( fp, "%-5d ",A->nseq);                 
5705                 }
5706               if (c=='L'&& d)
5707                 {
5708                  fprintf ( fp, "%-5d ",A->len_aln);              
5709                 }
5710             }
5711           if (d)fprintf ( fp, "\n"); 
5712         }
5713       vfclose (fp);
5714     }
5715
5716 int output_age_matrix ( char *outfile, int val)
5717 {
5718   int **mat;
5719   int a, b;
5720   char alp[]="abcdefghij-";
5721   int naa;
5722   
5723   mat=declare_int ( 256, 256);
5724   naa=strlen (alp);
5725   for ( a=0; a<naa; a++)
5726     for ( b=0; b<naa; b++)
5727       {
5728         if (is_gap(alp[a]) ||is_gap(alp[b] ))mat[(int)alp[a]][(int)alp[b]]=((val==0)?1:val)*-1;
5729         else mat[(int)alp[a]][(int)alp[b]]=(FABS((a-b))*-1)*((val==0)?1:val);
5730         
5731       }
5732   output_mat ( mat,outfile, alp, 0);
5733   free_arrayN((void**)mat, 2);
5734   return 1;
5735 }
5736      
5737
5738
5739
5740 int output_transitions(char *outfile, Alignment *A)
5741 {
5742   double table[256][256];
5743   double symbols[256];
5744   double tot, l, freq, expected, log_odd;
5745   int a, b;
5746   char *s;
5747   char *alp;
5748   int naa=0;
5749   int **mat;
5750   float **fmat;
5751   
5752   FILE *fp;
5753   
5754   for ( a=0; a< 256; a++)
5755     for (b=0; b<256; b++) 
5756       {
5757         symbols[b]=0;
5758         table[a][b]=0;
5759       }
5760   alp=vcalloc ( 256, sizeof (char));
5761   mat=declare_int ( 256,256);
5762   fmat=declare_float ( 256,256);
5763   
5764   for (tot=0,a=0; a< A->nseq; a++)
5765     {
5766       ungap (A->seq_al[a]);
5767       lower_string (A->seq_al[a]);
5768       s=A->seq_al[a];
5769       l=strlen (s);
5770       if ( s[0]=='\0') continue;
5771       symbols[(int)s[0]]++;
5772       for ( b=1; b< l; b++)
5773         {
5774           symbols[(int)s[b]]++;
5775           table[(int)s[b-1]][(int)s[b]]++;
5776           tot++;
5777         }
5778     }
5779   for (naa=0, a=0; a< 256; a++)
5780     {
5781       if (symbols[a])alp[naa++]=a;
5782     }
5783   
5784       
5785   for ( a=0; a< 256; a++)
5786     for (b=0; b<256; b++) 
5787       {
5788         if (symbols[a]&& symbols[b] && table[a][b] && tot>0)
5789           {
5790             freq=(table[a][b])/tot;
5791             expected=(symbols[a]*symbols[b])/(tot*tot);
5792             log_odd=log (freq/expected);
5793             mat[a-'A'][b-'A']=log_odd*10;
5794             fmat[a-'A'][b-'A']=log_odd;
5795           }
5796         else if ( symbols[a]&& symbols[b])
5797           {
5798             mat[a-'A'][b-'A']=-999;
5799             fmat[a-'A'][b-'A']=-999;
5800           }
5801       }
5802   output_mat ( mat,outfile, alp, 'A');
5803   
5804   fp=vfopen (outfile, "a");
5805   for ( a=0; a<256; a++)
5806     if ( symbols[a])
5807       {
5808         fprintf (fp, "# %c tot: %6d freq: %7.5f\n", a, (int)symbols[a],(float)symbols[a]/tot);
5809       }
5810   
5811   for ( a=0; a< 256; a++)
5812     for (b=0; b<256; b++) 
5813       {
5814         if (symbols[a]&& symbols[b])
5815           {
5816             freq=(table[a][b])/tot;
5817             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']);                    
5818           }
5819       }
5820   vfclose (fp);
5821   vfree(alp);
5822   free_arrayN ((void **)mat, 2);
5823   free_arrayN ((void **)fmat, 2);
5824   
5825   return 1;
5826 }
5827
5828
5829
5830 void output_est_prf   (char *fname, Alignment *A)
5831         {
5832         int a;
5833         FILE *fp;
5834
5835         if ( !A->P)
5836           {
5837             fprintf ( stderr, "\nFormat output_est_prf Impossible: No profile\n");
5838             myexit(EXIT_FAILURE);
5839           }
5840         
5841
5842         fp=vfopen ( fname, "w");
5843         fprintf ( fp, "Consensus Sequence\nReconstructed with %s (%s,%s)\n",PROGRAM,AUTHOR,DATE);
5844         fprintf ( fp, "%4c %4c %4c %4c %15s    Consensus\n",  'A','G','C','T', "Internal Gaps");
5845
5846         for ( a=0; a< A->len_aln; a++)
5847           {
5848             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]);
5849           }
5850         return;
5851         }
5852
5853           
5854 void output_gotoh_seq (char *fname, Alignment*A )
5855         {
5856         int a;
5857         FILE *fp;
5858                 
5859         fp=vfopen ( fname, "w");
5860         fprintf ( fp, "%d %d\n",A->nseq, A->max_len);
5861         for ( a=0; a< A->nseq; a++)
5862                 {
5863                 ungap ( A->seq_al[a]);
5864                 fprintf ( fp, ">%s\n", A->name[a]);
5865                 fp=output_string_wrap ( 50,A->seq_al[a] , fp);
5866                 fprintf ( fp, "//\n");
5867                 }
5868                 
5869         vfclose (fp);
5870         }           
5871
5872 void output_mult_fasta_seq (char *fname, Alignment*A, int n )
5873         {
5874         int a;
5875         FILE *fp;
5876         
5877         fp=vfopen (fname, "w");
5878         ungap(A->seq_al[0]);
5879         for (a=0; a<n; a++)
5880           {
5881             fprintf (fp, ">%s_%d\n%s\n", A->name[0],a+1, A->seq_al[0]);
5882           }
5883         vfclose (fp);
5884         }
5885 int output_wexons (char *name, Alignment *A)
5886 {
5887   int a,b,c;
5888   FILE *fp;
5889   int **w;
5890   fp=vfopen (name, "w");
5891   if (!fp) return 0;
5892   if (!A) {vfclose(fp);return 0;}
5893   w=A->score_res;
5894   if (!w) {vfclose (fp);return 0; }
5895   
5896   for (a=0; a<A->nseq; a++)
5897     {
5898       fprintf (fp, ">%s\n", A->name[a]);
5899       for (c=0,b=0; b<A->len_aln; b++)
5900         {
5901           int r;
5902           r=A->seq_al[a][b];
5903           if (!is_gap(r) && r!='f' && r!='F')
5904             {
5905               fprintf (fp, " %c %d ", r,w[a][c++]);
5906             }
5907           else if (!is_gap(r))fprintf (fp,"%c -1 ",r);
5908         }
5909       fprintf (fp, "\n");
5910     }
5911   return 1;
5912 }
5913 char * output_fasta_seqX (char *name, char *mode, Sequence *S, Alignment *A, int i)
5914 {
5915   FILE *fp;
5916   
5917   if (!name)name=vtmpnam (NULL);
5918   fp=vfopen (name, mode);
5919   if ( (S && S->nseq<=i) || (A && S->nseq<=i) || (!A && !S))
5920     {
5921       fprintf ( stderr, "\nERROR in function reformat:output_fasta_seqX[FATAL:%s]", PROGRAM);
5922       myexit (EXIT_FAILURE);
5923     }
5924
5925   else if ( S) 
5926     fprintf ( fp, ">%s %s\n%s\n", S->name[i], S->seq_comment[i], S->seq[i]);
5927   else if ( A)
5928     {
5929       ungap (A->seq_al[i]);
5930       fprintf ( fp, ">%s %s\n%s\n", A->name[i], A->seq_comment[i], A->seq_al[i]);
5931     }
5932   vfclose (fp);
5933   return name;
5934 }
5935
5936 void output_fasta_seq1 (char *fname, Alignment*A )
5937         {
5938         char seq_name[VERY_LONG_STRING];
5939         int a;
5940         FILE *fp;
5941         char *extension;
5942         
5943         for ( a=0; a< A->nseq; a++)
5944                 {
5945                 if ( strncmp( fname, "name",4)==0)
5946                   {
5947                     if ( (fname+4)[0]!='\0')extension=fname+5;
5948                     else
5949                       extension=NULL;
5950                     
5951                      sprintf ( seq_name,"%s.%s", A->name[a],(extension==NULL)?"seq":extension);
5952                   }
5953                 else
5954                    sprintf ( seq_name,"%s.seq",A->name[a]);
5955                 
5956                 ungap ( A->seq_al[a]);
5957                 fp=vfopen (seq_name, "w");
5958                 fprintf (fp, ">%s %s\n", A->name[a], A->seq_comment[a]);
5959                 fp=output_string_wrap ( 50, A->seq_al[a],fp);
5960                 fprintf ( fp, "\n");
5961                 vfclose (fp);
5962                 }
5963         }
5964 void output_pir_check (char *fname,int nseq, char **comment )
5965         {
5966         int a;
5967         FILE *fp;
5968         
5969         if ( fname==NULL)return;
5970         fp=vfopen ( fname, "w");
5971         
5972         for ( a=0; a< nseq; a++)fprintf (fp, "%s\n", comment[a]);
5973         vfclose (fp);
5974         }
5975 void output_fasta_seqS (char *fname, Sequence *S)
5976 {
5977   Alignment *A;
5978   A=seq2aln (S,NULL,RM_GAP);
5979   output_fasta_seq (fname, A);
5980   free_aln (A);
5981 }
5982
5983 void output_fasta_seq (char *fname, Alignment*A)
5984 {
5985   main_output_fasta_seq (fname, A, HEADER);
5986 }
5987 void output_fasta_tree (char *fname, Alignment*A)
5988         {
5989         int a;
5990         FILE *fp;
5991         if ( !A || !A->nseq) return;
5992         
5993         fp=vfopen ( fname, "w");
5994         
5995         for ( a=0; a< A->nseq; a++)
5996                 {
5997                   fprintf ( fp, ">%s %s\n%s\n", A->name[a], A->seq_comment[a], A->seq_al[a]);
5998                 }
5999         vfclose (fp);
6000         }
6001 void main_output_fasta_seq (char *fname, Alignment*A,int header )
6002         {
6003         int a;
6004         FILE *fp;
6005         
6006         fp=vfopen ( fname, "w");
6007
6008         for ( a=0; a< A->nseq; a++)
6009                 {
6010                 ungap(A->seq_al[a]);
6011                 fprintf ( fp, ">%s", A->name[a]);
6012                 if (header==HEADER && A->seq_comment[a][0] && !isblanc(A->seq_comment[a]))fprintf (fp," %s\n",A->seq_comment[a]);
6013                 else fprintf ( fp, "\n");
6014                 fp=output_string_wrap ( 50, A->seq_al[a],fp);
6015                 fprintf ( fp, "\n");
6016                 }
6017         vfclose (fp);
6018         }    
6019 void output_gor_seq (char *fname, Alignment*A )
6020         {
6021         int a;
6022         FILE *fp;
6023         
6024         fp=vfopen ( fname, "w");
6025         
6026         for ( a=0; a< A->nseq; a++)
6027                 {
6028                 ungap(A->seq_al[a]);
6029                 fprintf ( fp, "!%s %d \n", A->name[a], (int)strlen(A->seq_al[a]));
6030                 upper_string ( A->seq_al[a]);
6031                 fp=output_string_wrap ( 50, A->seq_al[a],fp);
6032                 fprintf ( fp, "@\n");
6033                 }
6034         vfclose (fp);
6035         }    
6036 void output_pir_seq (char *fname, Alignment*A )
6037         {
6038         int a;
6039         for ( a=0; a< A->nseq; a++)ungap(A->seq_al[a]);
6040         output_pir_aln (fname, A);
6041         } 
6042 void output_pir_seq1 (char *fname, Alignment*A )
6043         {
6044         char seq_name[VERY_LONG_STRING];
6045         int a;
6046         FILE *fp;
6047         char type[20];
6048
6049         
6050         for ( a=0; a< A->nseq; a++)
6051                 {
6052                 if      ( strm ( get_string_type (A->seq_al[a]),"DNA") || strm ( get_string_type (A->seq_al[a]),"RNA"))sprintf(type, "DL");
6053                 else if ( strm ( get_string_type (A->seq_al[a]),"PROTEIN"))sprintf(type, "P1"); 
6054                 sprintf ( seq_name,"%s;%s_%s.seq",type, fname,A->name[a]);
6055                 ungap ( A->seq_al[a]);
6056                 fp=vfopen (seq_name, "w");
6057                 fprintf (fp, ">%s\n\n", A->name[a]);
6058                 fp=output_string_wrap ( 50, A->seq_al[a],fp);
6059                 fprintf ( fp, "\n*\n");
6060                 vfclose (fp);
6061                 }
6062         } 
6063 /*******************************************************************************************/
6064 /*                                                                                         */
6065 /*                                                                                         */
6066 /*                               OUTPUT ALN                                                */
6067 /*                                                                                         */
6068 /***************************************************************************************** */
6069 void output_mocca_aln (char *outfile, Alignment *A, Alignment *S)
6070     {
6071     FILE *fp;
6072     int **score;
6073     char **new_name_order;
6074     int a, maxl;
6075
6076     score=declare_int (S->nseq, 2);
6077     new_name_order=declare_char ( S->nseq,MAXNAMES+1); 
6078     for ( a=0; a<A->nseq; a++)
6079       {
6080         score[a][0]=a;
6081         score[a][1]=S->score_seq[a];
6082       }
6083     sort_int_inv (score+1,2,1,0,S->nseq-2);
6084     for ( a=0; a<A->nseq; a++)
6085       {
6086         sprintf ( new_name_order[a], "%s", A->name[score[a][0]]);
6087       }
6088     A=reorder_aln (A, new_name_order, A->nseq);
6089
6090     fp=vfopen (outfile, "w");
6091     fprintf ( fp, "MOCCA,(%s,%s, C. Notredame)\nSCORE %d\nNSEQ  %d\nLEN   %d\n",VERSION,DATE, A->score_aln, A->nseq, A->len_aln);     
6092     
6093     maxl=return_maxlen ( new_name_order, A->nseq); 
6094     
6095    
6096     for (a=0; a< A->nseq; a++)
6097       {
6098         fprintf (fp, "%-*s: %3d\n", maxl, A->name[a], score[a][1]);
6099       }
6100     
6101     fprintf ( fp, "\n");
6102     
6103     fp=output_Alignment_without_header ( A, fp);
6104     vfclose (fp);
6105     free_int  (score, -1);
6106     free_char (new_name_order, -1);
6107     return ;
6108     }
6109   
6110 void print_sub_aln ( Alignment *B, int *ns, int **ls)
6111 {
6112   Alignment *X;
6113   int a, b;
6114  
6115
6116   X=copy_aln (B, NULL);
6117   X->nseq=0;
6118   X->len_aln=strlen ( B->seq_al[ls[0][0]]);
6119
6120
6121   for (a=0; a< 2; a++)
6122     for ( b=0; b<ns[a]; b++, X->nseq++)
6123       {
6124         sprintf ( X->seq_al[X->nseq], "%s", B->seq_al[ls[a][b]]);
6125         sprintf ( X->name[X->nseq], "%s", B->name[ls[a][b]]);
6126       }
6127   X->name[X->nseq][0]='\0';
6128   
6129   print_aln (X);
6130   free_aln (X);
6131 }
6132 void print_aln ( Alignment *B)
6133     {
6134       
6135     while(B)
6136       {
6137         output_Alignment_without_header ( B, stderr);
6138         B=B->A;
6139       }
6140     }
6141
6142
6143 FILE * output_aln ( Alignment *B, FILE *fp){return output_Alignment(B, fp);}
6144 FILE * output_Alignment ( Alignment *B, FILE *fp)
6145     {
6146       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);     
6147      
6148       return output_Alignment_without_header ( B, fp);
6149     }
6150   
6151 FILE * output_Alignment_without_header ( Alignment *B, FILE *fp)
6152     {
6153     int a,b, c;
6154     int max_len=0;
6155     int line;       
6156     int *n_residues;
6157     char s;
6158
6159     
6160     if (fp==NULL)return fp;
6161     for ( a=0; a< B->nseq; a++)
6162             {if ( strlen (B->name[a])>max_len)
6163                 max_len= strlen ( (B->name[a]));
6164             }
6165     max_len=MAX(max_len+2, 16);
6166     line=get_msa_line_length (0, 0);
6167     n_residues=vcalloc ( B->nseq+1, sizeof (int));
6168     for ( a=0; a<B->nseq; a++)n_residues[a]=(B->output_res_num==2)?B->order[a][1]:0;
6169     
6170     
6171     
6172     
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",max_len,B->name[b]);
6178                if (B->output_res_num)fprintf (fp, " %4d ", n_residues[b]+1);
6179                for (c=a;c<a+line && c<B->len_aln;c++)
6180                  {
6181                    if (b==B->nseq){n_residues[b]++;s=analyse_aln_column ( B, c);}
6182                    else 
6183                      {n_residues[b]+=!is_gap(B->seq_al[b][c]);
6184                        s=GET_CASE(B->residue_case, B->seq_al[b][c]);
6185                      }
6186                    
6187                    fprintf (fp,"%c",s );
6188                                 }
6189                if (B->output_res_num)fprintf (fp, " %4d", n_residues[b]);
6190                fprintf (fp,"\n");
6191              }
6192              
6193              fprintf (fp,"\n");
6194            }
6195     
6196      fprintf (fp,"\n\n");
6197      vfree (n_residues);
6198      
6199      return fp;
6200     }
6201 FILE * output_aln_score ( Alignment *B, FILE *fp){return output_Alignment_score(B, fp);}
6202 FILE * output_Alignment_score ( Alignment *B, FILE *fp)
6203     {
6204     int a, b, c;
6205     static int max_len=0;
6206     static int line;        
6207     int ch;
6208     
6209     if (fp==NULL)return fp;
6210     if ( max_len==0)
6211         {
6212         for ( a=0; a< B->nseq; a++)
6213             {if ( strlen (B->name[a])>max_len)
6214                 max_len= strlen ( (B->name[a]));
6215             }
6216         max_len+=4;
6217
6218         }       
6219    line=get_msa_line_length(0, 0);
6220    sprintf (B->name[B->nseq], "CONS"); 
6221    fprintf ( fp, "T_COFFEE ALIGNMENT\nCPU TIME:%d sec.\n", (B->cpu+get_time())/1000);  
6222    fprintf ( fp, "SCORE=%d\n", B->score_aln);
6223    for ( a=0;a<B->nseq; a++)fprintf ( fp, "%s: %d\n", B->name[a], B->score_seq[a]);
6224    fprintf ( fp, "\n"); 
6225     for (a=0; a<B->len_aln; a+=line)
6226            {for (b=0; b<B->nseq; b++)
6227               {
6228               fprintf (fp,"%-*s",max_len,B->name[b]);
6229               for (c=a;c<a+line && c<B->len_aln;c++)
6230                 {
6231                 ch=B->seq_al[b][c];
6232                 if (ch==NO_COLOR_RESIDUE)fprintf (fp,"-");
6233                 else if ( ch==NO_COLOR_GAP)fprintf (fp,"*");
6234                 else if ( ch<10 && ch>=0)fprintf (fp,"%d",ch);
6235                 else if ( ch>10)fprintf (fp,"#");
6236                 else if ( ch<0)fprintf  (fp,".");
6237                 else fprintf (fp,"9");          
6238                 }             
6239               fprintf (fp,"\n");              
6240               }
6241             fprintf (fp,"\n");
6242             fprintf (fp,"%-*s",max_len,B->name[b]);
6243             for (c=a;c<a+line && c<B->len_aln;c++)
6244               {
6245               ch=B->seq_al[b][c];
6246               if (ch==NO_COLOR_RESIDUE)fprintf (fp,"-");
6247               else if ( ch==NO_COLOR_GAP)fprintf ( fp, "*");
6248               else if ( ch<10 && ch>=0)fprintf (fp,"%d",ch);
6249               else if ( ch>10)fprintf (fp,"#");
6250               else if ( ch<0)fprintf (fp,".");
6251               else fprintf (fp,"9");            
6252               }       
6253             fprintf (fp,"\n\n\n");
6254            }
6255     fprintf (fp,"\n\n");
6256     return fp;
6257     }
6258 FILE * output_aln_with_res_number ( Alignment *B, FILE *fp){return  output_Alignment_with_res_number(B, fp);}
6259 FILE * output_Alignment_with_res_number ( Alignment *B, FILE *fp)
6260     {
6261     int a, b, c;
6262     static int max_len=0;
6263     static int line;        
6264     int**order;
6265
6266     if (fp==NULL)return fp;
6267     if ( max_len==0)
6268         {
6269         for ( a=0; a< B->nseq; a++)
6270             {if ( strlen (B->name[a])>max_len)
6271                 max_len= strlen ( (B->name[a]));
6272             }
6273         max_len+=4;
6274         line=60;
6275         }       
6276    order=copy_int ( B->order,declare_int ( B->nseq, 2), B->nseq, 2);
6277     
6278    fprintf ( fp, "T_COFFEE ALIGNMENT\nCPU TIME:%d sec.\n", (B->cpu+get_time())/1000);     
6279    fprintf ( fp, "\n"); 
6280     for (a=0; a<B->len_aln; a+=line)
6281            {for (b=0; b<B->nseq; b++)
6282              {
6283               fprintf (fp,"%-*s %3d %4d ",max_len,B->name[b], order[b][0], order[b][1] );
6284               for (c=a;c<a+line && c<B->len_aln;c++)
6285                 {
6286                 order[b][1]+=1-is_gap(B->seq_al[b][c]);
6287                 fprintf (fp,"%c",toupper(B->seq_al[b][c]) );
6288                 }
6289               fprintf (fp," %4d\n", order[b][1] );
6290               }
6291             fprintf (fp,"\n");
6292             }
6293     fprintf (fp,"\n\n");
6294
6295     free_int (order, -1);
6296     return fp;
6297     }
6298
6299 void output_constraints ( char *fname, char *mode,Alignment *A)
6300         {
6301         FILE *fp;
6302         Constraint_list *CL;
6303         char *buf;
6304         char **name_list;
6305         
6306         if ( !A->CL || strm ( mode, "pdb"))
6307            {
6308                if (!A->S)
6309                   {
6310                       A->S=aln2seq(A);
6311                   }
6312                
6313                CL=declare_constraint_list ( A->S, NULL, NULL, 0, NULL, NULL);
6314                CL=aln2constraint_list (A,CL, mode);
6315                fp=save_constraint_list ( CL, 0, CL->ne,fname, NULL, "lib",A->S);
6316                vfclose (fp);
6317                free_constraint_list (CL);
6318                return;
6319            }
6320         else if ( strncmp ( mode, "extended_pair", 13)==0)
6321           {
6322             buf=duplicate_string (mode+14);
6323             
6324             name_list=vcalloc(2, sizeof(char*));
6325             name_list[0]=strtok (buf,"_");
6326             name_list[1]=strtok (NULL,"_");
6327             mode[13]='\0';
6328             
6329         
6330             CL=A->CL;
6331             fp=save_sub_list_header (vfopen(fname, "w"),2, name_list,CL);
6332             fp=save_extended_constraint_list_pair (CL, "pair",name_list[0],name_list[1],fp);
6333             fp=save_list_footer (fp, CL);
6334             vfree (buf);
6335           }
6336         else if ( strm2 (mode, "extended_lib","extended_cosmetic"))
6337           {
6338             CL=A->CL;
6339             fp=save_extended_constraint_list ( CL,mode+9, vfopen(fname, "w"));
6340           }
6341         else 
6342            {
6343                CL=(Constraint_list *)A->CL;
6344                fp=save_constraint_list ( CL, 0, CL->ne,fname, NULL, "lib",A->S);
6345            }
6346         vfclose ( fp);
6347         
6348         if ( (Constraint_list *)A->CL !=CL)free_constraint_list (CL);
6349
6350         return;
6351
6352         }
6353 void output_model_aln (char *fname, Alignment*A )
6354         {
6355           FILE *fp;
6356           int a;
6357           Dp_Model *M;
6358           Dp_Result *R;
6359           char *string;
6360           
6361           if ( A->Dp_result==NULL)
6362             {
6363               fprintf ( stderr, "\nWARNING Could Not Output Model %s [%s]", fname, PROGRAM);
6364             }
6365           R=A->Dp_result;
6366           M=R->Dp_model;
6367
6368           fp=vfopen ( fname, "w");
6369           for (a=0; a<M->nstate; a++)
6370             {
6371               if (M->model_comments[a][0])fprintf ( fp, "#STATE %c: %s\n", 'a'+a, M->model_comments[a]);
6372             }
6373           string=vcalloc ( R->len+1, sizeof (char));
6374           for (a=0; a<R->len; a++)string[a]=R->traceback[a]+'a';
6375           fprintf ( fp, ">%s\n",fname);
6376           fp=output_string_wrap ( 50,string, fp);
6377           vfree(string);
6378           fprintf ( fp, "\n");
6379         
6380           vfclose (fp);
6381           return;
6382         }
6383 char * output_fasta_sub_aln (char *fname, Alignment*A, int ns, int *ls  )
6384 {
6385   int a,s;
6386   FILE *fp;
6387   if (fname==NULL)fname=vtmpnam (NULL);
6388   fp=vfopen (fname, "w");
6389   for (a=0; a<ns; a++)
6390     {
6391       s=ls[a];
6392       fprintf (fp, ">%s %s\n%s\n", A->name[s],A->seq_comment[s],A->seq_al[s]);
6393     }
6394   vfclose (fp);
6395   return fname;
6396 }
6397 char * output_fasta_sub_aln2 (char *fname, Alignment*A, int *ns, int **ls  )
6398 {
6399   int a,g,s;
6400   FILE *fp;
6401   if (fname==NULL)fname=vtmpnam (NULL);
6402   fp=vfopen (fname, "w");
6403   for ( g=0; g<2; g++)
6404     for (a=0; a<ns[g]; a++)
6405       {
6406         s=ls[g][a];
6407         fprintf (fp, ">%s %s\n%s\n", A->name[s],A->seq_comment[s],A->seq_al[s]);
6408       }
6409   vfclose (fp);
6410   return fname;
6411 }
6412
6413 int output_suchard_aln (char *out_file, Alignment *A)
6414 {
6415   int a, b, c, d;
6416   FILE *fp;
6417   
6418   A=back_translate_dna_aln (A);
6419   
6420   for ( c=0,a=0; a<A->len_aln; a++, c++)
6421              {
6422                if (c==3)c=0;
6423                for (b=0; b<A->nseq; b++)
6424                  {
6425                  if (c==2)
6426                    {
6427                      A->seq_al[b][a]='-';
6428                    }
6429                  }
6430              }
6431   A=ungap_aln_n (A, 1);    
6432   fp=vfopen (out_file, "w");
6433   for ( a=0; a< A->nseq; a++)
6434     {
6435       for (b=0; b< A->len_aln; b++)
6436         {
6437           c=tolower(A->seq_al[a][b]);
6438           if ( c=='a')d=1;
6439           else if ( c=='g')d=2;
6440           else if ( c=='c')d=3;
6441           else if ( c=='t')d=4;
6442           else if ( c=='u')d=5;
6443           else d=6;
6444
6445           fprintf ( fp, "%d", d);
6446         }
6447       fprintf ( fp, "\n");
6448     }
6449   vfclose (fp);
6450   myexit (EXIT_SUCCESS);
6451 }
6452   
6453 void output_fasta_aln (char *fname, Alignment*A )
6454         {
6455         FILE *fp;
6456         int a;
6457         int line=0;
6458
6459         line=get_msa_line_length (line, A->len_aln+1);
6460         fp=vfopen ( fname, "w");
6461
6462         for ( a=0; a< A->nseq; a++)
6463                 {
6464                   fprintf ( fp, ">%s", A->name[a]);
6465                   
6466                   if ( A->seq_comment[a][0] && !isblanc (A->seq_comment[a]))fprintf ( fp, " %s", A->seq_comment[a]);
6467                   fprintf ( fp, "\n");
6468                   fp=output_string_wrap ( line,A->seq_al[a] , fp);
6469                   fprintf ( fp, "\n");
6470                 }
6471         vfclose (fp);
6472         }
6473         
6474 void output_pir_aln (char *fname, Alignment*A )
6475         {
6476         int a;
6477         FILE *fp;
6478         char type[20];
6479         
6480         
6481
6482         
6483         
6484         fp=vfopen ( fname, "w");
6485         for ( a=0; a< A->nseq; a++)
6486                 {
6487                 if      ( strm ( get_string_type (A->seq_al[a]),"DNA") || strm ( get_string_type (A->seq_al[a]),"RNA"))sprintf(type, "DL");
6488                 else if ( strm ( get_string_type (A->seq_al[a]),"PROTEIN"))sprintf(type, "P1");
6489                 fprintf ( fp, ">%s;%s\n%s\n",type, A->name[a], A->seq_comment[a]);
6490                 fp=output_string_wrap ( 50,A->seq_al[a] , fp);
6491                 fprintf ( fp, "\n*\n");
6492                 }
6493                 
6494         vfclose (fp);
6495         }           
6496
6497 int landscape_msa;
6498 int  set_landscape_msa (int len)
6499 {
6500   if ( len==0)landscape_msa=-1;
6501   else
6502     {
6503       landscape_msa=len;
6504     }
6505   return landscape_msa;
6506 }
6507 int get_msa_line_length (int line, int aln_len)
6508 {
6509   if (landscape_msa==-1) return aln_len;
6510   else if ( landscape_msa)return landscape_msa;
6511   else if (line) return line;
6512   else 
6513     {
6514       return (getenv ("ALN_LINE_LENGTH"))?atoi(getenv("ALN_LINE_LENGTH")):ALN_LINE_LENGTH;
6515     }
6516 }
6517
6518 void output_msf_aln (char *fname,Alignment *B)
6519         {
6520         int a, b, c;    
6521         char *seq;
6522         int *all_checks;
6523         int i,j;
6524         long grand_checksum;
6525         FILE *fp;
6526         int max_len;
6527         int line=0;
6528         int block=10;
6529         int c_block;
6530         char aa;
6531         
6532
6533         line=get_msa_line_length (line, B->len_aln+1);
6534         
6535
6536         for ( max_len=0,a=0; a< B->nseq; a++)max_len= MAX(strlen ( B->name[a]),max_len);
6537
6538
6539         max_len+=5;
6540         
6541         fp=vfopen (fname, "w");
6542         
6543         seq =vcalloc(B->len_aln,  sizeof(char));
6544         all_checks =vcalloc(B->nseq, sizeof(int));
6545         for ( i=0; i< B->nseq; i++)
6546           {
6547             for ( j=0; j<B->len_aln; j++)
6548               {
6549                 if ( is_gap(B->seq_al[i][j]))seq[j]='.';
6550                 else seq[j]=B->seq_al[i][j]=toupper(B->seq_al[i][j]);
6551                 
6552               }
6553             all_checks[i] = SeqGCGCheckSum(seq, (int)B->len_aln);
6554           }
6555         grand_checksum = 0;
6556         for(i=0; i<B->nseq; i++) grand_checksum += all_checks[i];
6557         grand_checksum = grand_checksum % 10000;
6558         fprintf(fp,"PileUp\n\n");
6559         B=get_aln_type(B);
6560         fprintf(fp,"\n\n   MSF:%5d  Type: ",B->len_aln);
6561         if(strm ( (B->S)->type, "DNA") || strm ( (B->S)->type, "RNA"))
6562                 fprintf(fp,"N");
6563         else
6564                 fprintf(fp,"P");
6565         fprintf(fp,"    Check:%6ld   .. \n\n", (long)grand_checksum);
6566         for (i=0; i< B->nseq; i++)
6567           {
6568             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);
6569           }
6570         fprintf(fp,"\n//\n\n");
6571         
6572         for (a=0; a<B->len_aln; a+=line)
6573            {
6574              fprintf ( fp,"\n\n"); 
6575              for (b=0; b<B->nseq; b++)
6576                {
6577                  fprintf (fp,"%-*s ",max_len,B->name[b]);
6578                  for (c_block=0,c=a;c<a+line && c<B->len_aln;c++)
6579                    {
6580                      if ( c_block==block)
6581                             {
6582                               fprintf (fp, " ");
6583                               c_block=0;
6584                             }
6585                         c_block++;
6586                      aa=(is_gap(B->seq_al[b][c]))?'.': toupper(B->seq_al[b][c]);
6587                      fprintf (fp,"%c",aa );
6588                    }
6589                  if ( c_block==block)
6590                             {
6591                               fprintf (fp, " ");
6592                               c_block=0;
6593                             }
6594                  fprintf (fp,"\n");
6595                  
6596                }
6597            }
6598         fprintf ( fp,"\n");              
6599         vfclose ( fp);
6600         
6601         
6602         vfree(seq);
6603         vfree(all_checks);
6604         
6605
6606         return;
6607
6608 int SeqGCGCheckSum(char *seq, int len)
6609 {
6610         int  i;
6611         long check;
6612         
6613         for( i=0, check=0; i< len; i++,seq++)
6614                 check += ((i % 57)+1) * toupper(*seq);
6615
6616         return(check % 10000);
6617 }  
6618 void old_output_msf_aln (char *fname,Alignment *B)
6619         {
6620         FILE *fp;
6621         static int *put_seq;
6622         int a, b, c;
6623         int line=0;
6624         char aa;
6625         char *buf;
6626         int max_len;
6627         int seq_max_len;
6628         
6629         line=get_msa_line_length (line, B->len_aln+1);
6630         
6631         
6632         for ( max_len=0,a=0; a< B->nseq; a++)max_len= MAX(strlen ( B->name[a]),max_len);
6633         for ( seq_max_len=0,a=0; a< B->nseq; a++)seq_max_len= MAX(strlen ( B->seq_al[a]),max_len);
6634         
6635
6636         buf=vcalloc(seq_max_len+1, sizeof (int)); 
6637         
6638         if ( put_seq==NULL)
6639                 put_seq= vcalloc ( B->nseq, sizeof (int));
6640         put_seq[0]=1;
6641         
6642         
6643         for ( b=1; b< B->nseq; b++)
6644                 {
6645                 sprintf ( buf, "%s", B->seq_al[b]);
6646                 ungap(buf);
6647                 put_seq[b]=( strlen (buf)>0)?1:0;
6648                 }       
6649         
6650         fp=vfopen ( fname, "w");
6651         fprintf ( fp, "MSF: %d Type P Check: 5083 ..\n", B->len_aln);
6652         for ( a=0; a< B->nseq; a++)
6653                 {
6654                 if ( put_seq[a]==1)
6655                         fprintf ( fp,"Name: %s\n",B->name[a]);
6656                 }
6657          fprintf ( fp, "//\n");
6658          for (a=0; a<B->len_aln; a+=line)
6659            {for (b=0; b<B->nseq; b++)
6660              {
6661              if ( put_seq[b]==1)
6662                 {
6663                 fprintf (fp,"%-*s ",max_len,B->name[b]);
6664                 for (c=a;c<a+line && c<B->len_aln;c++)
6665                         {
6666                         
6667                           
6668                             
6669                         aa=(B->seq_al[b][c]=='-')?'.': toupper(B->seq_al[b][c]);
6670                         fprintf (fp,"%c",aa );
6671                         }
6672                 fprintf (fp,"\n");
6673                 }
6674               }
6675             fprintf (fp,"\n");
6676             }
6677         fprintf ( fp,"\n\n");            
6678         vfclose ( fp);
6679
6680         vfree (buf);
6681         vfree(put_seq);
6682         }
6683         
6684 void output_saga_aln ( char *name, Alignment *B)
6685     {
6686     int a, b, c;
6687     FILE *fp;
6688
6689
6690
6691     int max_len;
6692     int line=0;
6693     
6694     line=get_msa_line_length (line, B->len_aln+1);
6695     
6696     
6697     
6698     for ( max_len=0,a=0; a< B->nseq; a++)max_len= (strlen ( B->name[a])>max_len)?(strlen ( B->name[a])):max_len;
6699             
6700         
6701
6702
6703     fp= vfopen ( name, "w");
6704     
6705     fprintf (fp, "\nSAGA FORMAT\nalignement  %s nseq=%d len=%d\n", name, B->nseq, B->len_aln);
6706        
6707     fprintf (fp, "\n\n");
6708     for (a=0; a<B->len_aln; a+=line)
6709            {for (b=0; b<B->nseq; b++)
6710              {fprintf (fp,"%-*s ",max_len,B->name[b]);
6711               for (c=a;c<a+line && c<B->len_aln;c++)
6712                 {
6713                   fprintf (fp,"%c",(B->seq_al[b][c]) );
6714                 }
6715               fprintf (fp,"\n");
6716               }
6717             fprintf (fp,"\n");
6718             }
6719     fprintf (fp,"\n\n");
6720     vfclose ( fp);
6721     }
6722 void output_compact_aln ( char *name, Alignment *B)
6723     {
6724     int a, b, c;
6725     FILE *fp;
6726     int do_print=0;
6727
6728
6729     int max_len;
6730     int line=0;
6731
6732     line=get_msa_line_length (line, B->len_aln+1);
6733     
6734     
6735     for ( max_len=0,a=0; a< B->nseq; a++)max_len= (strlen ( B->name[a])>max_len)?(strlen ( B->name[a])):max_len;
6736             
6737         
6738
6739
6740     fp= vfopen ( name, "w");
6741     
6742     fprintf (fp, "\nSAGA FORMAT\nalignement  %s nseq=%d len=%d", name, B->nseq, B->len_aln);
6743     fprintf (fp, "\n\n");
6744     for (a=0; a<B->len_aln; a+=line)
6745            {for (b=0; b<B->nseq; b++)
6746              {
6747              
6748              for ( do_print=0, c=a;c<a+line && c<B->len_aln;c++)
6749                do_print+=1-is_gap(B->seq_al[b][c]);
6750              if ( do_print>0)
6751                    {
6752                      fprintf (fp,"%-*s ",max_len,B->name[b]);
6753              
6754              
6755              
6756                      for (c=a;c<a+line && c<B->len_aln;c++)
6757                        {
6758                          if ( is_gap(B->seq_al[b][c])&& B->seq_al[b][c]!='-' )fprintf (fp,"%c", '-');
6759                          else fprintf (fp,"%c",(B->seq_al[b][c]) );
6760                        }
6761                      fprintf (fp,"\n");
6762                    }
6763               }
6764             fprintf (fp,"\n");
6765             }
6766     fprintf (fp,"\n\n");
6767     vfclose ( fp);
6768     }
6769
6770 void output_clustal_aln ( char *name, Alignment *B)
6771 {
6772   return output_generic_clustal_aln (name, B, "tc_clustal");
6773 }
6774 void output_strict_clustal_aln ( char *name, Alignment *B)
6775 {
6776   return output_generic_clustal_aln (name, B, "strict_clustal");
6777 }
6778
6779 void output_generic_clustal_aln ( char *name, Alignment *B, char *mode)
6780     {
6781     int a, b, c;
6782     FILE *fp;
6783     int max_len=0;
6784     int line=0; 
6785     int *n_residues;
6786     
6787     if ( getenv ("SEP_4_TCOFFEE"))
6788       {
6789         while ( line<B->len_aln && B->seq_al[0][line]!='o' && B->seq_al[0][line]!='O')line++;
6790         if ( B->seq_al[0][line]=='O' || B->seq_al[0][line]=='o')line++;
6791       }
6792     else
6793       {
6794         while ( line<B->len_aln)line++;
6795       }
6796     
6797     if ( line==B->len_aln)line=get_msa_line_length (0, B->len_aln+1);
6798     
6799     n_residues=vcalloc ( B->nseq+1, sizeof (int));
6800     for ( a=0; a< B->nseq; a++)
6801             {if ( strlen (B->name[a])>max_len)
6802                 max_len= strlen ( (B->name[a]));
6803             n_residues[a]=B->order[a][1];
6804             }
6805     max_len=MAX(max_len+2, 16);
6806         
6807
6808     fp= vfopen ( name, "w");
6809
6810     if ( strm (mode, "strict_clustal"))
6811       fprintf ( fp, "CLUSTAL W (1.83) multiple sequence alignment");
6812     else
6813       {
6814         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);
6815         if (B->ibit>0)
6816           {
6817             float ibit=(float)log ((double)B->ibit)/log ((double)2);
6818             float nibit=(float)log(ibit/(B->len_aln*B->nseq));
6819             fprintf ( fp, "\nTies: %.1f bits (%d alternative)\n",ibit, B->ibit-1);
6820           
6821           }
6822       }
6823     fprintf (fp, "\n\n");
6824
6825
6826     if ( B->len_aln==0)
6827       {
6828         for (b=0; b<=B->nseq; b++)
6829           fprintf (fp,"%-*s -\n",max_len, B->name[b]);
6830       }
6831     
6832     else
6833       {
6834         for (a=0; a<B->len_aln; a+=line)
6835           {for (b=0; b<=B->nseq; b++)
6836             {
6837               if (b!=B->nseq)
6838                 {
6839                   fprintf (fp,"%-*s",max_len, B->name[b]);
6840                   for (c=a;c<a+line && c<B->len_aln;c++)
6841                     {
6842                       if ( is_gap(B->seq_al[b][c]))fprintf (fp,"%c", '-');
6843                       else 
6844                         {
6845                           n_residues[b]++;
6846                           fprintf (fp, "%c", GET_CASE(B->residue_case, B->seq_al[b][c]));
6847                           
6848                         }
6849                       
6850                     }
6851                   if (B->output_res_num)fprintf (fp, " %d", n_residues[b]);
6852                   fprintf (fp,"\n");
6853                 }
6854               else if ( b==B->nseq)
6855                 {
6856                   fprintf (fp,"%-*s",max_len," ");                  
6857                   for (c=a;c<a+line && c<B->len_aln;c++)
6858                     {
6859                       fprintf ( fp, "%c", analyse_aln_column (B, c));
6860                     }
6861                   fprintf (fp,"\n");
6862                 }
6863             }
6864           fprintf (fp,"\n"); 
6865           }
6866       }
6867     fprintf (fp,"\n\n");
6868     vfree (n_residues);
6869     vfclose ( fp);
6870     }    
6871 FILE * output_generic_interleaved_aln (FILE *fp, Alignment *B, int line, char gap, char *mode)
6872     {
6873     int a, b, c;
6874     int max_len=0;
6875     int *n_residues;
6876     
6877     
6878     n_residues=vcalloc ( B->nseq+1, sizeof (int));
6879     for ( a=0; a< B->nseq; a++)
6880             {if ( strlen (B->name[a])>max_len)
6881                 max_len= strlen ( (B->name[a]));
6882             n_residues[a]=B->order[a][1];
6883             }
6884     max_len=MAX(max_len+2, 16);
6885         
6886
6887     
6888     
6889     if ( B->len_aln==0)
6890       {
6891         for (b=0; b<=B->nseq; b++)
6892           fprintf (fp,"%-*s -\n",max_len, B->name[b]);
6893       }
6894     
6895     else
6896       {
6897         for (a=0; a<B->len_aln; a+=line)
6898           {for (b=0; b<=B->nseq; b++)
6899             {
6900               if (b!=B->nseq)
6901                 {
6902                   fprintf (fp,"%-*s",max_len, B->name[b]);
6903                   for (c=a;c<a+line && c<B->len_aln;c++)
6904                     {
6905                       if ( is_gap(B->seq_al[b][c]))fprintf (fp,"%c", gap);
6906                       else 
6907                         {
6908                           n_residues[b]++;
6909                           fprintf (fp, "%c", GET_CASE(B->residue_case, B->seq_al[b][c]));
6910                           
6911                         }
6912                       
6913                     }
6914                   if (B->output_res_num)fprintf (fp, " %d", n_residues[b]);
6915                   fprintf (fp,"\n");
6916                 }
6917             }
6918           fprintf (fp,"\n"); 
6919           }
6920       }
6921     vfree (n_residues);
6922     return fp;
6923     }    
6924 void output_phylip_aln ( char *name, Alignment *B)
6925     {
6926       int a, b, c, d;
6927     FILE *fp;
6928
6929     int *print_name;
6930     static int line=0;      
6931     line=get_msa_line_length(0, 0);
6932     
6933     print_name=vcalloc ( B->nseq, sizeof (int));
6934     fp= vfopen ( name, "w");
6935     
6936     fprintf (fp, "%3d %d\n", B->nseq, B->len_aln);
6937     for (a=0; a<B->len_aln; a+=line)
6938            {for (b=0; b<B->nseq; b++)
6939              {if ( print_name[b]==0)
6940                 {
6941                   
6942                   fprintf (fp,"%-10.10s  ",B->name[b]);
6943                   print_name[b]=1;
6944                 }
6945                else
6946                  {
6947                    fprintf (fp, "%10.10s ", " ");
6948                  }
6949
6950
6951                for (d=0,c=a;c<a+line && c<B->len_aln;c++, d++)
6952                  {
6953                    if ( d==10)
6954                      {
6955                        fprintf ( fp, " ");
6956                        d=0;
6957                      }
6958                    if ( is_gap(B->seq_al[b][c])&& B->seq_al[b][c]!='-' )fprintf (fp,"%c", '-');
6959                    else fprintf (fp,"%c",(B->seq_al[b][c]) );
6960                  }
6961               fprintf (fp,"\n");
6962               }
6963             fprintf (fp,"\n");
6964             }
6965     fprintf (fp,"\n\n");
6966     vfclose ( fp);
6967     }
6968
6969 void output_rnalign (char *out_file, Alignment *A, Sequence *STRUC)
6970     {
6971     int a, b;
6972     FILE *fp;
6973     char bank_file[100];
6974     char pep_file[100];
6975     char *buf;
6976     
6977     sprintf ( bank_file, "%s.mss", out_file);
6978     sprintf ( pep_file, "%s.one_rna", out_file);
6979     
6980    
6981     buf=vcalloc ( strlen ( A->seq_al[0]+1), sizeof (char));
6982     
6983     for ( b=0,a=0; a< strlen(A->seq_al[0]); a++) 
6984         {
6985         if ( is_gap(A->seq_al[0][a]))
6986                 buf[a]='.';
6987         else
6988                 buf[a]=STRUC->seq[0][b++];
6989         }
6990     buf[a]='\0';
6991     
6992     fp=vfopen ( bank_file, "w");
6993     
6994     fprintf ( fp, "ST\n");
6995     fp=output_string_wrap ( 50, buf, fp);
6996     fprintf ( fp, "\n\n");
6997     
6998     for ( a=0; a<A->nseq-1; a++)
6999         {
7000         fprintf ( fp, "AS %s\n ", A->name[a]);
7001         fp=output_string_wrap ( 50, A->seq_al[a], fp); 
7002         fprintf ( fp, "\n\n");
7003         }
7004     vfclose ( fp);
7005     fp=vfopen ( pep_file, "w");
7006     fprintf ( fp, ">%s\n", A->name[A->nseq-1]); 
7007     fp=output_string_wrap ( 50, A->seq_al[A->nseq-1], fp);
7008     fprintf ( fp, "\n");
7009     vfclose (fp);
7010     }
7011
7012 void output_lib (char *pw_lib_saga_aln_name, Alignment *A )
7013     {
7014     Alignment *B;
7015     char fname[VERY_LONG_STRING];
7016     int a,b;
7017     
7018     B=declare_Alignment (NULL);
7019     
7020     B->nseq=2;
7021     
7022     for ( a=0; a< A->nseq-1; a++)
7023         {
7024         for ( b=a+1; b<A->nseq; b++)
7025                 {
7026                 sprintf ( B->seq_al[0], "%s", A->seq_al[a]);
7027                 sprintf ( B->name[0], "%s", A->name[a]);
7028                 sprintf(B->name[1], "%s", A->name[b]);
7029                 sprintf ( B->seq_al[1], "%s",A->seq_al[b]);
7030                 B->nseq=2;
7031                 sprintf ( fname, "%s_%s_%s.lib",pw_lib_saga_aln_name, A->name[a], A->name[b]);
7032         
7033                 B->len_aln=strlen ( B->seq_al[0]);
7034                 ungap_aln (B);
7035                 output_clustal_aln (fname,B);  
7036                 }
7037         }
7038     } 
7039 void output_pw_lib_saga_aln (char *pw_lib_saga_aln_name, Alignment *A )
7040     {
7041     Alignment *B;
7042     char fname[VERY_LONG_STRING];
7043     int a,b;
7044     
7045     B=declare_Alignment (NULL);
7046     
7047     B->nseq=2;
7048     
7049     for ( a=0; a< A->nseq-1; a++)
7050         {
7051         for ( b=a+1; b<A->nseq; b++)
7052                 {
7053                 sprintf ( B->seq_al[0], "%s", A->seq_al[a]);
7054                 sprintf ( B->name[0], "%s", A->name[a]);
7055                 sprintf(B->name[1], "%s", A->name[b]);
7056                 sprintf ( B->seq_al[1], "%s",A->seq_al[b]);
7057                 B->nseq=2;
7058                 sprintf ( fname, "%s_%s_%s.pw_lib_saga_aln",pw_lib_saga_aln_name, A->name[a], A->name[b]);
7059         
7060                 B->len_aln=strlen ( B->seq_al[0]);
7061                 ungap_aln (B);
7062                 output_clustal_aln (fname,B);  
7063                 }
7064         }
7065     }   
7066 void output_lalign_header( char *name, Alignment *A)
7067     {
7068     FILE *fp;
7069     
7070     fp=vfopen ( name, "w");
7071     fprintf ( fp, " Lalign mode: best local alignments between two sequences\n");
7072     fprintf ( fp, " %s(%s) [%s]\n\n", VERSION, DATE, URL);
7073     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]]);
7074     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]]);
7075     
7076     
7077     vfclose ( fp);
7078     return;
7079     }
7080 void output_stockholm_aln (char *file, Alignment *A, Alignment *ST)
7081 {
7082   FILE *fp;
7083   int a,b;
7084     
7085   for (a=0; a<A->nseq; a++)
7086     for (b=0; b<A->len_aln; b++)
7087       if (A->seq_al[a][b]==STOCKHOLM_CHAR)A->seq_al[a][b]='.';
7088   
7089   fp=vfopen (file, "w");
7090   fprintf ( fp, "# STOCKHOLM 1.0\n\n");
7091   output_generic_interleaved_aln (fp,A, 50, '.', NULL);
7092   fprintf ( fp, "//\n");
7093   vfclose (fp);
7094 }
7095                            
7096 void output_glalign ( char *name, Alignment *B, Alignment *S)
7097 {
7098   int a, b, g, s;
7099   int naln=0;
7100   FILE *fp;
7101   int **nr;
7102   B=B->A;
7103   if ( B==NULL){return;}
7104   
7105   fp=vfopen (name, "w");
7106   fprintf (fp, "Format: GLALIGN_01 [Generated with %s ]\n", PROGRAM);
7107   fprintf (fp, "#Each Line corresponds to a column\n");
7108   fprintf (fp, "#First column coresponds to first genome\n");
7109   fprintf (fp, "#Last Column gives the column reliability on a 0-9 scale\n");
7110   fprintf (fp, "#[-1] Indicates that the reliability was not evaluated\n");
7111   
7112   fprintf (fp, "Genome List\n");
7113   for ( a=0; a< B->nseq; a++)
7114     fprintf (fp, "\tGenome %s\n", B->name[a]);
7115   fprintf (fp, "Alignment List\n");
7116   while (B)
7117     {
7118       fprintf (fp, "Alignment %d Len %d Score %d\n", ++naln, B->len_aln, S->score_aln);
7119       nr=duplicate_int (B->order, -1, -1);
7120       for ( a=0; a< B->len_aln; a++)
7121         {
7122           fprintf ( fp, "\t");
7123           for ( b=0; b< B->nseq; b++)
7124             {
7125               g=is_gap (B->seq_al[b][a]);
7126               nr[b][1]+=1-g;
7127
7128               if (g)fprintf (fp, "---- ");
7129               else fprintf ( fp, "%4d ",nr[b][1]);
7130             }
7131           s=((S)?S->seq_al[S->nseq][a]:-1);
7132           if (s==NO_COLOR_RESIDUE)s=-1;
7133           fprintf ( fp,"[ %d ]",s);
7134           fprintf ( fp, "\n");  
7135           
7136         } 
7137       free_int (nr, -1);
7138       B=B->A;
7139       S=S->A;
7140     }
7141   vfclose ( fp);
7142 }
7143 Alignment *input_conc_aln ( char *name, Alignment *IN)
7144 {
7145   FILE *fp;
7146   char *string, *p, *file;
7147   Alignment *F=NULL,*A=NULL, *B=NULL;
7148   
7149   file=vtmpnam (NULL);
7150  
7151   string=file2string(name);
7152   string=substitute ( string, "@", "!Protected!");
7153   string=substitute ( string, TC_REC_SEPARATOR, "@");
7154   strtok (string,"@");
7155   
7156
7157   while ( (p=strtok (NULL,"@"))!=NULL)
7158     {
7159       char *buf;
7160       buf=vcalloc ( strlen (p)+1, sizeof (char));
7161       sprintf (buf,"%s", p);
7162       buf=substitute (buf,"!protected!", "@");
7163       
7164       fp=vfopen (file, "w");
7165       fprintf ( fp, "%s",buf);
7166       vfclose (fp);
7167       vfree (buf);
7168             
7169       if ( is_aln (file))
7170         {
7171           B=main_read_aln (file,NULL);
7172           
7173           if ( !A)
7174             {
7175               if (IN){copy_aln (B, IN);F=A=IN;}
7176               else F=A=B;
7177             }
7178           else
7179             {
7180               A->A=B;
7181               A=A->A;
7182             }
7183         }
7184     }
7185   
7186   vfree (string);
7187   return F;
7188 }
7189
7190 void output_conc_aln ( char *name, Alignment *B)
7191 {
7192   FILE *fp;
7193   int a;
7194   
7195   fp=vfopen (name, "w");
7196   fprintf (fp, "# CONC_MSF_FORMAT_01\n");
7197   while (B)
7198     {
7199       fprintf (fp, "%s\n", TC_REC_SEPARATOR);
7200       for ( a=0; a< B->nseq; a++)
7201         {
7202           fprintf ( fp, ">%s\n%s\n", B->name[a], B->seq_al[a]);
7203         }
7204       B=B->A;
7205       
7206     }
7207   vfclose (fp);
7208 }
7209
7210 void output_lalign ( char *name, Alignment *B)
7211 {
7212   static int output_header;
7213  
7214   B=B->A;
7215   if ( B==NULL){output_header=0;return;}
7216     else if ( output_header==0)
7217       {
7218         output_lalign_header(name, B);
7219         output_header=1;
7220       }
7221   while (B)
7222     {
7223       output_lalign_aln   ( name, B);
7224       B=B->A;
7225     }
7226 }
7227 void output_lalign_aln   ( char *name, Alignment *B)
7228     {
7229     int a, b, c,d=0, s=0;
7230     char col;
7231
7232     float tot=0;
7233     float id=0;
7234
7235     FILE *fp;
7236     int max_len=0;
7237     int line;   
7238     int *n_residues;
7239     int res;
7240
7241         
7242     n_residues=vcalloc ( B->nseq+1, sizeof (int));
7243     for ( a=0; a< B->nseq; a++)
7244             {if ( strlen (B->name[a])>max_len)
7245                 max_len= strlen ( (B->name[a]));
7246             n_residues[a]=B->order[a][1];
7247             }
7248     max_len=MAX(max_len+2, 16);
7249     line=60;
7250     
7251     
7252
7253     fp= vfopen ( name, "a");
7254     
7255     for (a=0; a< B->len_aln; a++)
7256       {
7257         if ( !is_gap(B->seq_al[0][a]) && !is_gap(B->seq_al[1][a]))
7258              {
7259                tot++;
7260                id+=(B->seq_al[0][a]==B->seq_al[1][a]);
7261              }
7262       }
7263     
7264     id=(id*100)/tot;
7265     fprintf (fp, " %.1f%% identity in %d aa overlap; score: %d\n\n", id,(int)tot, B->score_aln);
7266     
7267     
7268     for (a=0; a<B->len_aln; a+=line)
7269            {for (b=0; b<5; b++)
7270              {
7271                  if ( b==0 || b==4)
7272                    {
7273                      if ( b==0)s=0;
7274                      if ( b==4)s=1;
7275                      fprintf (fp,"%-*s",max_len," ");
7276                      for (d=0,c=a;c<a+line && c<B->len_aln;c++)
7277                        {
7278                          res=!is_gap ( B->seq_al[s][c]);
7279                          n_residues[s]+=res;
7280                          if ( (n_residues[s]%10)==0 && res && (c-a+4)<line){fprintf (fp, "%-4d", n_residues[s]);d=-3;}  
7281                          else
7282                            {
7283                              if ( d==0)fprintf (fp, " ");
7284                              else d++;
7285                            }
7286                        }
7287                      fprintf (fp,"\n");
7288                    }
7289                  else if (b==1 || b==3)
7290                     {
7291                       if ( b==1)s=0;
7292                       if ( b==3)s=1;
7293                         fprintf (fp,"%-*s",max_len, B->name[s]);
7294                         for (c=a;c<a+line && c<B->len_aln;c++)
7295                             {
7296                                 if ( is_gap(B->seq_al[s][c]))fprintf (fp,"%c", '-');
7297                                 else 
7298                                     {
7299                                         fprintf (fp, "%c", GET_CASE(B->residue_case, B->seq_al[s][c]));
7300                                     }
7301                             }
7302                         fprintf (fp,"\n");
7303                     }
7304                  else if ( b==2)
7305                     {
7306                     fprintf (fp,"%-*s",max_len," ");                
7307                     for (c=a;c<a+line && c<B->len_aln;c++)
7308                             {
7309                             col=analyse_aln_column (B, c);
7310                             if ( col=='*')col=':';
7311                             else if ( col==':')col='.';
7312                             else if ( col=='.')col=' ';
7313                             fprintf ( fp, "%c", col);
7314                             }
7315                     fprintf (fp,"\n");
7316                     }
7317              }
7318            fprintf (fp,"\n"); 
7319            }
7320     
7321     fprintf (fp,"\n\n----------\n\n");
7322     vfree (n_residues);
7323     vfclose ( fp);
7324     }   
7325
7326
7327 /****************************************************************************************************/
7328 /*************************************UTIL *********************************************************/
7329 /**************************************************************************************************/
7330
7331
7332 /****************************************************************************************************/
7333 /***************************                                    *************************************/
7334 /***************************             PROCESSING             *************************************/
7335 /***************************                                    *************************************/
7336 /*******************************************************************************************/
7337 /*                                                                                         */
7338 /*                                                                                         */
7339 /*                              THREADING                                                  */
7340 /***************************************************************************************** */
7341
7342 char *thread_aa_seq_on_dna_seq( char *s)
7343      {
7344          int l, b, c;
7345          char *array;
7346          
7347
7348          l=strlen ( s);
7349          array=vcalloc ( l*3 +1, sizeof (char));
7350          for ( b=0, c=0; b< l; b++, c+=3)
7351              {
7352                  array[c]=s[b];
7353                  array[c+1]='o';
7354                  array[c+2]='o';
7355              }
7356          array[c]='\0';
7357          return array;
7358      }
7359
7360 Alignment *thread_dnaseq_on_prot_aln (Sequence *S, Alignment *A)
7361         {
7362             Alignment *B=NULL;
7363             int a, b, c, n, la, ls, ln, m;
7364
7365             B=copy_aln ( A, B);
7366             B=realloc_aln2 ( B, B->nseq, B->len_aln*3 +1);
7367
7368             for ( n=0,a=0; a< A->nseq; a++)
7369                 {
7370                 for ( m=0,b=0; b< S->nseq; b++)
7371                     {
7372                     if (strm (A->name[a], S->name[b]) )
7373                        {
7374                            m=1;
7375                            n++;
7376                            ungap ( S->seq[b]);
7377                            B->seq_al[a][0]='\0';
7378                            for (la=0, ls=0, ln=0; la< A->len_aln; la++)
7379                                {
7380                                    for (c=0; c< 3; c++)
7381                                        B->seq_al[a][ls++]=(is_gap(A->seq_al[a][la]))?'-':S->seq[b][ln++];
7382                                }
7383                        B->seq_al[a][ls]='\0';
7384                        }
7385                     }
7386                 if ( m==0)
7387                        {
7388                        for (la=0, ls=0, ln=0; la< A->len_aln; la++)
7389                                {
7390                                 
7391                                    B->seq_al[a][ls++]=A->seq_al[a][la];
7392                                    B->seq_al[a][ls++]='-';
7393                                    B->seq_al[a][ls++]='-';
7394                                }
7395                        }
7396                 }
7397             
7398             B->len_aln=strlen ( B->seq_al[0]);
7399             return B;
7400         }
7401 void thread_seq_struc2aln ( Alignment *A, Sequence *ST)
7402         {
7403         int a, b, c,d;
7404         int len, cons;
7405         
7406         for ( a=0; a< A->nseq; a++)
7407                 for ( b=0; b< ST->nseq; b++)
7408                         {
7409                         if ( strcmp ( A->name[a], ST->name[b])==0)
7410                                 {
7411                                 ungap (ST->seq[b]);
7412                                 len=strlen(A->seq_al[a]);
7413                                 for ( c=0, d=0; c<len; c++)
7414                                         {
7415                                         if ( !is_gap(A->seq_al[a][c]))A->seq_al[a][c]=ST->seq[b][d++];
7416                                         }
7417                                 }
7418                         }
7419         cons=name_is_in_list ("Cons", ST->name, ST->nseq, 100);
7420         if ( cons!=-1 && A->len_aln==strlen ( ST->seq[cons]))
7421           {
7422             sprintf (A->name[A->nseq], "Cons");
7423             sprintf (A->seq_al[A->nseq],"%s", ST->seq[cons]);
7424             A->nseq++;
7425           }
7426         }
7427 void cache_id ( Alignment *A)
7428         {
7429         int a, b,n;
7430         char r1, r2, r3;
7431         
7432         for ( a=0; a< A->len_aln; a++)
7433                 {
7434                 for ( b=0, n=0; b< A->nseq; b++)if ( !is_gap(A->seq_al[b][a]))n++;
7435                 for ( b=0; b< A->nseq; b++)
7436                         if ( !is_gap(A->seq_al[b][a]) && n==A->nseq)A->seq_al[b][a]='h';
7437                         else if( !is_gap(A->seq_al[b][a]))A->seq_al[b][a]='x';
7438                 }
7439         for ( a=0; a< A->nseq; a++)
7440                 {
7441                 for ( b=1; b< A->len_aln-1; b++)
7442                         {
7443                         r1=A->seq_al[a][b-1];
7444                         r2=A->seq_al[a][b];
7445                         r3=A->seq_al[a][b+1];
7446                         if (r2=='h')
7447                                 {
7448                                 if ( (r1=='h' || r1=='b') && (r3=='h' || r3=='b'))A->seq_al[a][b]='h';
7449                                 else A->seq_al[a][b]='b';
7450                                 }
7451                         }
7452                 for ( b=1; b< A->len_aln-1; b++)if ( A->seq_al[a][b]=='b')A->seq_al[a][b]='x';
7453                 }
7454                         
7455         }
7456                         
7457                                                                          
7458 /*******************************************************************************************/
7459 /*                                                                                         */
7460 /*                                                                                         */
7461 /*                               PROCESING OF EST                                          */
7462 /*                                                                                         */
7463 /***************************************************************************************** */
7464 int process_est_sequence ( Sequence *S, int *cluster_list)
7465         {
7466         char **inverted_seq;
7467         int T=20;
7468         int a, b;
7469         int V1, V2;
7470         int **sens;
7471         int **a_sens;
7472         int **best;
7473         int *solution;
7474         char buf [VERY_LONG_STRING];
7475         int n_clusters=0;
7476         int n;
7477         
7478         sens=declare_int ( S->nseq,S->nseq);
7479         a_sens=declare_int ( S->nseq,S->nseq);
7480         best=declare_int ( S->nseq,S->nseq);
7481         
7482         
7483         inverted_seq=vcalloc ( S->nseq, sizeof (char*));
7484         for ( a=0; a<S->nseq; a++)
7485                 inverted_seq[a]=invert_seq ( S->seq[a]);
7486                 
7487         for ( a=0; a< S->nseq-1; a++)
7488                 {
7489                 
7490                 for ( b=a+1; b<S->nseq; b++)
7491                                      {
7492                                      
7493                                      V1=sens[a][b]=sens[b][a]=get_best_match ( S->seq[a], S->seq[b]);
7494                                      V2=a_sens[a][b]=a_sens[b][a]=get_best_match ( S->seq[a],inverted_seq[b]);
7495                                      best[a][b]=best[b][a]=(V1>V2)?V1:V2;
7496                                      }
7497                 }
7498         solution=SHC ( S->nseq, a_sens, sens);
7499         
7500         
7501         for ( a=0; a<S->nseq; a++)cluster_list[a]=-1;
7502         for ( a=0; a<S->nseq; a++)
7503                 {
7504                 n=search_for_cluster (a, n_clusters, cluster_list, T, S->nseq, best);
7505                 if ( n>0)n_clusters++;
7506                 }
7507         fprintf ( stderr, "\nTHERE %s %d Independant Cluster(s) in your sequences",(n_clusters>1)?"are":"is",(n_clusters));
7508         for (a=0; a<n_clusters; a++)
7509                 {
7510                 fprintf (stderr, "\n");
7511                 for ( b=0; b<S->nseq; b++)
7512                         {
7513                         if ( cluster_list[b]==a)fprintf ( stderr, "%s ", S->name[b]);
7514                         }
7515                 }
7516                 
7517         for ( a=0; a<S->nseq; a++)
7518                 {
7519                 if ( solution[a]==-1)
7520                         {
7521                         S->seq[a]=inverted_seq[a];
7522                         sprintf ( buf, "i_%s", S->name[a]);
7523                         sprintf ( S->name[a], "%s", buf);
7524                         }
7525                 }
7526         return n_clusters;
7527         }
7528
7529 int search_for_cluster ( int seq, int cluster_number, int *cluster_list, int T, int nseq, int **S)      
7530         {
7531         int n=0,a;
7532         
7533         if (cluster_list[seq]==-1)
7534                 {
7535                 cluster_list[seq]=cluster_number;
7536                 n++;
7537                 }
7538         for ( a=0; a<nseq; a++)
7539                 if ( cluster_list[a]==-1)
7540                         {
7541                         
7542                         if (S[seq][a]>T)
7543                                 {
7544                                 n++;
7545                                 cluster_list[a]=cluster_number;
7546                                 n+=search_for_cluster ( a, cluster_number, cluster_list, T, nseq, S);
7547                                 }
7548                         }
7549         return n;
7550         }       
7551                 
7552 int * SHC ( int nseq, int **NST, int **ST)
7553         {
7554         int a;
7555         int mut;
7556         int score, new_score;
7557         int N_IT=VERY_LONG_STRING;
7558         int *sol;
7559         int count;
7560         
7561         sol=vcalloc ( nseq, sizeof (int));
7562         for ( a=0; a<nseq; a++)
7563                 sol[a]=(addrand ((unsigned long)100)>49)?1:-1;
7564                 
7565         score=evaluate_sol (sol, nseq, ST, NST);
7566         fprintf ( stderr, "\nI_Score=%d\n", score);
7567         N_IT=N_IT*nseq;
7568         
7569         for ( count=0,a=0; a< N_IT && score<VERY_LONG_STRING; a++, count++)
7570                 {
7571                 mut=mutate_sol ( sol,nseq);
7572                 new_score=evaluate_sol (sol, nseq, ST, NST);
7573                 if ( new_score>score)
7574                         {
7575                         score=new_score;
7576                         }
7577                 else if ( (addrand ((unsigned long)VERY_LONG_STRING))>score)
7578                         {
7579                         score=new_score;
7580                         }
7581                 else
7582                         sol[mut]=sol[mut]*-1;
7583                 if ( count==VERY_LONG_STRING)
7584                         {
7585                         count=0;
7586                         fprintf ( stderr, "\nScore=%d", score);
7587                         }       
7588                 }
7589         fprintf ( stderr, "\nScore=%d\n", score);
7590         return sol;
7591         }
7592
7593 int mutate_sol (int *sol, int nseq)
7594         {
7595         int n;
7596         n=addrand ((unsigned long)nseq);
7597         sol[n]=sol[n]*-1;
7598         return n;
7599         }
7600 int evaluate_sol ( int *sol, int nseq, int **ST, int **NST)
7601         {
7602         static int max_score;
7603         int a, b, score=0;
7604         
7605         if ( max_score==0)
7606                 {
7607                 for ( a=0; a<nseq-1; a++)
7608                         for ( b=a+1; b<nseq; b++)
7609                                 {
7610                                 max_score+=(ST[a][b]>NST[a][b])?ST[a][b]:NST[a][b];
7611                                 }
7612                 }
7613         
7614         for ( a=0; a<nseq-1; a++)
7615                 for (b=a+1; b<nseq; b++)
7616                         if ( (sol[a]*sol[b])<0)score+=NST[a][b];
7617                         else score+=ST[a][b];
7618         return (score*VERY_LONG_STRING)/max_score;
7619         }               
7620                 
7621      
7622 char * invert_seq ( char *seq)
7623         {
7624         int a, b;
7625         
7626         char *nseq;
7627         int l;
7628         
7629         
7630         l=strlen ( seq);
7631         for ( a=0; a<l; a++)
7632                 seq[a]=tolower ( seq[a]);
7633         nseq=vcalloc ( l+1, sizeof (char));
7634         
7635         for ( a=0, b=l-1; a<l; a++, b--)
7636                 {
7637                 if (seq[b]=='n')nseq[a]='n';
7638                 else if (seq[b]=='g')nseq[a]='c';
7639                 else if (seq[b]=='c')nseq[a]='g';
7640                 else if (seq[b]=='a')nseq[a]='t';
7641                 else if (seq[b]=='t')nseq[a]='a';
7642                 }
7643                 
7644         nseq[l]='\0';
7645         return nseq;
7646         }
7647                 
7648                 
7649 int get_best_match ( char *seq1, char *seq2)
7650         {
7651         static int **m;
7652         static int ml;
7653         int a, b;
7654         int **mdiag;
7655         int n_mdiag=0;
7656         int best;
7657         int l1, l2;
7658         
7659         
7660         l1=strlen ( seq1);
7661         l2=strlen (seq2);
7662         if ( m==NULL)
7663                 {
7664                 ml=(l1>l2)?l1:l2;
7665                 m=declare_int (ml, ml);
7666                 }
7667         else if ( (ml<l1) || (ml<l2))
7668                 {
7669                 free_int (m, ml);
7670                 ml=(l1>l2)?l1:l2;
7671                 m=declare_int (ml, ml);
7672                 }
7673                 
7674         for ( a=0; a<l1; a++)
7675                 {
7676                 for ( b=0; b<l2; b++)
7677                         m[a][b]=((seq1[a]==seq2[b])|| seq1[a]=='n' ||seq2[b]=='n')?1:0;
7678                 }
7679         mdiag= extract_m_diag_streches ( m, l1, l2,seq1, seq2, &n_mdiag); 
7680         
7681         for ( best=0,a=0; a<n_mdiag; a++)
7682                 best=(mdiag[a][0]>best)?mdiag[a][0]:best;
7683         
7684         return best;
7685         }
7686
7687 int** extract_m_diag_streches ( int ** m, int l1, int l2,char *seq1, char *seq2, int *n_mdiag)
7688         {
7689         
7690         int b, x, y, s1, s2;
7691         static int **mdiag;
7692         int in;
7693         static int max_diag=VERY_LONG_STRING;
7694          
7695          /*
7696          diag[0]=len;
7697          diag[1]=x_start;
7698          diag[2]=y_start;
7699          diag[3]=x_end;
7700          diag[4]=y_end;
7701          */
7702          
7703         if ( mdiag==NULL)
7704                 mdiag=declare_int ( max_diag, 5);
7705         
7706         for ( s1=l1-1, s2=0;s2<l2;)
7707                 {
7708                 for ( in=0,x=s1, y=s2; x<l1 && y<l2; x++, y++)
7709                         { 
7710                         if (m[x][y]>0)
7711                                 {
7712                                 if (in==1)
7713                                         mdiag[n_mdiag[0]][0]++;
7714                                 else 
7715                                         {
7716                                         mdiag[n_mdiag[0]][0]=1;
7717                                         mdiag[n_mdiag[0]][1]=x;
7718                                         mdiag[n_mdiag[0]][2]=y;
7719                                         in=1;
7720                                         }
7721                                 }
7722                         else
7723                                 if (in==1)
7724                                         {
7725                                         in=0;
7726                                         mdiag[n_mdiag[0]][3]=x-1;
7727                                         mdiag[n_mdiag[0]][4]=y-1;
7728                                         if ( !is_strech ( "ta", seq1, seq2,mdiag[n_mdiag[0]][0], mdiag[n_mdiag[0]][1],mdiag[n_mdiag[0]][2]))n_mdiag[0]++;
7729                                         }
7730                         if (n_mdiag[0]==(max_diag-1))
7731                                 {mdiag=vrealloc (mdiag, (max_diag+VERY_LONG_STRING)*sizeof (int*));
7732                                 for ( b=max_diag; b<max_diag+VERY_LONG_STRING; b++)mdiag[b]=vcalloc ( 5, sizeof (int));
7733                                 max_diag+=VERY_LONG_STRING;
7734                                 }
7735                         }
7736                 s2+= (s1==0)?1:0;       
7737                 s1-= (s1==0)?0:1;
7738                 if (in==1)
7739                         {
7740                         in=0;
7741                         mdiag[n_mdiag[0]][3]=x-1;
7742                         mdiag[n_mdiag[0]][4]=y-1;
7743                         if ( !is_strech ( "ta", seq1, seq2,mdiag[n_mdiag[0]][0], mdiag[n_mdiag[0]][1],mdiag[n_mdiag[0]][2]))n_mdiag[0]++;
7744                         }
7745                 }
7746         
7747         return mdiag;
7748         }                               
7749 int is_strech ( char *AA, char *seq1, char *seq2, int len, int x, int y)
7750         {
7751         int n, i, j, c,a,nr;
7752         int T=70;
7753         
7754         n=strlen ( AA);
7755         for ( a=0; a<n; a++)
7756                 {
7757                 for (nr=0, i=x, j=y, c=0; c<len; c++, i++, j++)
7758                         if ((seq1[i]==AA[a]) && (seq2[j]==AA[a]))nr++;
7759                 if ( ((nr*100)/len)>T)return 1;
7760                 }
7761         return 0;
7762         }
7763         
7764         
7765 /************************************************************************************/
7766 /*                                                                                  */
7767 /*                                      STRUC                                       */
7768 /*                                                                                  */
7769 /*                                                                                  */
7770 /************************************************************************************/
7771
7772 char * oneletaa2threeletaa(char aa);
7773 float aa2property   (char aa, char *mode);
7774
7775 int output_seq2struc(char *outfile, Alignment *A)
7776 {
7777   FILE *fp1, *fp2;
7778   int a,c, l;
7779   float v, h, x, y, z, dx, dy, dz;
7780   char *s;
7781   char *tmpfile1, *tmpfile2;
7782   char command[1000];
7783
7784   tmpfile1=vtmpnam(NULL);
7785   tmpfile2=vtmpnam(NULL);
7786   
7787   ungap (A->seq_al[0]);
7788   s=A->seq_al[0];l=strlen (s);
7789   fp1=vfopen (tmpfile1, "w");
7790
7791   x=y=z=0;
7792   for ( a=0; a< l; a++)
7793     {
7794       h=aa2property ( s[a], "doolittle"   );
7795       v=aa2property (s[a], "volume");
7796       /*14.398907: peptide bond length*/
7797       dx=(float)sqrt ((double)(14.398907/(((h*h)/(v*v))+1)));
7798       dy=dx*(h/v);
7799       dz=0;
7800
7801         
7802       x+=dx;
7803       y+=dy;
7804       z+=dz;
7805       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);
7806     }   
7807   vfclose (fp1);
7808   sprintf ( command, "extract_from_pdb -infile %s -force > %s", tmpfile1,  tmpfile2);
7809   my_system  (command);
7810   fp1=vfopen (tmpfile2, "r");
7811   fp2=vfopen (outfile, "w");
7812   
7813   while ( (c=fgetc(fp1))!=EOF)fprintf (fp2, "%c", c);
7814   vfclose (fp1);
7815   vfclose (fp2);
7816   
7817   return 0;
7818 }
7819
7820 char * oneletaa2threeletaa(char aa)
7821   {
7822     aa=tolower (aa);
7823     if ( aa=='a')return "ALA";
7824     else if ( aa=='r') return "ARG";
7825     else if ( aa=='n') return "ASN";
7826     else if ( aa=='d') return "ASP";
7827     else if ( aa=='c') return "CYS";
7828     else if ( aa=='q') return "GLN";
7829     else if ( aa=='e') return "GLU";
7830     else if ( aa=='g') return "GLY";
7831     else if ( aa=='h') return "HIS";
7832     else if ( aa=='i') return "ILE";
7833     else if ( aa=='l') return "LEU";
7834     else if ( aa=='k') return "LYS";
7835     else if ( aa=='m') return "MET";
7836     else if ( aa=='f') return "PHE";
7837     else if ( aa=='p') return "PRO";
7838     else if ( aa=='s') return "SER";
7839     else if ( aa=='t') return "THR";
7840     else if ( aa=='w') return "TRP";
7841     else if ( aa=='y') return "TYR";
7842     else if ( aa=='v') return "VAL";
7843     else 
7844       {
7845         fprintf ( stderr, "\nERROR: %c is not an amino acid [FATAL::aa2hydropathy::%s]", aa, PROGRAM);
7846         myexit (EXIT_FAILURE);
7847         return NULL;
7848       }
7849     return NULL;
7850   } 
7851   
7852 float aa2property   (char aa, char *mode)
7853   {
7854     if ( mode==NULL || strm (mode, "doolittle"))
7855          {
7856            aa=tolower (aa);
7857            if ( aa=='i')return 4.5;
7858            else if ( aa=='v') return 4.2;
7859            else if ( aa=='l') return 3.8;
7860            else if ( aa=='f') return 2.8;
7861            else if ( aa=='c') return 2.5;
7862            else if ( aa=='m') return 1.9;
7863            else if ( aa=='a') return 1.8;
7864            else if ( aa=='g') return -0.4;
7865            else if ( aa=='t') return -0.7;
7866            else if ( aa=='w') return -0.9;
7867            else if ( aa=='s') return -0.8;
7868            else if ( aa=='y') return -1.3;
7869            else if ( aa=='p') return -1.6;
7870            else if ( aa=='h') return -3.2;
7871            else if ( aa=='e') return -3.5;
7872            else if ( aa=='q') return -3.5;
7873            else if ( aa=='d') return -3.5;
7874            else if ( aa=='n') return -3.5;
7875            else if ( aa=='k') return -3.9;
7876            else if ( aa=='r') return -4.5;
7877            else 
7878              {
7879                fprintf ( stderr, "\nERROR: %c is not an amino acid [FATAL::aa2hydropathy::%s]", aa, PROGRAM);
7880                myexit (EXIT_FAILURE);
7881              }
7882          }
7883     else if (strm (mode, "volume"))
7884          {
7885            aa=tolower (aa);
7886            if ( aa=='a')return 0.915;
7887            else if ( aa=='r') return 2.02;
7888            else if ( aa=='n') return 1.35;
7889            else if ( aa=='d') return 1.24;
7890            else if ( aa=='c') return 1.18;
7891            else if ( aa=='q') return 1.61;
7892            else if ( aa=='e') return 1.55;
7893            else if ( aa=='g') return 0.66;
7894            else if ( aa=='h') return 1.67;
7895            else if ( aa=='i') return 1.69;
7896            else if ( aa=='l') return 1.68;
7897            else if ( aa=='k') return 1.71;
7898            else if ( aa=='m') return 1.70;
7899            else if ( aa=='f') return 2.03;
7900            else if ( aa=='p') return 1.29;
7901            else if ( aa=='s') return 0.99;
7902            else if ( aa=='t') return 1.22;
7903            else if ( aa=='w') return 2.37;
7904            else if ( aa=='y') return 2.03;
7905            else if ( aa=='v') return 1.41;
7906            else 
7907              {
7908                fprintf ( stderr, "\nERROR: %c is not an amino acid [FATAL::aa2hydropathy::%s]", aa, PROGRAM);
7909                myexit (EXIT_FAILURE);
7910              }
7911          }
7912     
7913     else
7914       {
7915         fprintf ( stderr, "\nERROR: %s is an unknown mode [FATAL::aa2hydropathy::%s]", mode  , PROGRAM);
7916         myexit (EXIT_FAILURE);
7917       }
7918   return 0;
7919   } 
7920            
7921
7922           
7923            
7924         
7925 /************************************************************************************/
7926 /*                                                                                  */
7927 /*                                      DNA                                         */
7928 /*                                                                                  */
7929 /*                                                                                  */
7930 /************************************************************************************/
7931
7932 Alignment *code_dna_aln (Alignment *A)
7933        {
7934          int a, b,l,r;
7935
7936          for ( a=0; a< A->nseq; a++)
7937            {
7938              for (l=0, b=0; b< A->len_aln; b++)
7939                {
7940                  r=A->seq_al[a][b];
7941                  if ( r=='-')l++;
7942                  else if ( r=='~')continue;
7943                  else if ( r=='.')l++;
7944                  else if ( !islower(r))A->seq_al[a][b]='4';
7945                  else
7946                    {
7947                      A->seq_al[a][b]=(l+3)%3+'0';
7948                      l++;
7949                    }
7950                }
7951            }
7952          return A;
7953        }
7954
7955   
7956 Alignment *back_translate_dna_aln (Alignment *A)
7957        {
7958          /*Given a set of aligned sequences
7959            starts from left to right
7960            1 aa->3 nuc
7961            ambiguities are randomly resolved.
7962            returns the corresponding amino acid alignment
7963          */      
7964           int a;
7965           char *seq    ;
7966          
7967          ungap_aln(A);
7968          A=realloc_aln (A, 10000);
7969          seq=vcalloc ( 10000, sizeof (char));
7970
7971          
7972          for ( a=0; a< A->nseq; a++)
7973            {
7974            seq=back_translate_dna_seq (A->seq_al[a], seq, RANDOM);
7975            sprintf ( A->seq_al[a], "%s", seq);
7976            }
7977          A->len_aln=A->len_aln*3;
7978          compress_aln (A);
7979          vfree (seq);
7980          return A;
7981        }
7982 char * back_translate_dna_seq ( char *in_seq,char *out_seq, int mode)     
7983        {
7984          int a,len;
7985
7986          len=strlen(in_seq);
7987          
7988          if (out_seq==NULL)out_seq=vcalloc ( len*3+1, sizeof (char));
7989          
7990          out_seq[0]='\0';
7991          for (a=0; a<len; a++)
7992            {
7993            strcat (out_seq,  back_translate_dna_codon (in_seq[a],mode));
7994            }
7995          
7996          return out_seq;
7997        }
7998
7999 static Sequence *rna_seq2dna_seq (Sequence *S);
8000 static Sequence *dna_seq2rna_seq (Sequence *S);
8001 Sequence * transform_sequence ( Sequence *S, char *mode)
8002 {
8003   if ( strm (mode, "rna2dna"))
8004     return rna_seq2dna_seq (S);
8005   else if ( strm (mode, "dna2rna"))
8006     return dna_seq2rna_seq (S);
8007   else 
8008     printf_exit (EXIT_FAILURE, stderr, "Unknown -transform mode: %s [FATAL:%s]\n", mode,PROGRAM);
8009   return NULL;}
8010
8011 Sequence *rna_seq2dna_seq (Sequence *S)
8012 {
8013   int a, b;
8014   
8015   if ( !strm(S->type, "DNA") && !strm (S->type, "RNA")) printf_exit (EXIT_FAILURE, stderr, "Sequences should be *RNA* type [FATAL:%s]\n", PROGRAM);
8016   for ( a=0; a<S->nseq; a++)
8017     {
8018       for (b=0; b<strlen (S->seq[a]); b++)
8019         {
8020           if ( S->seq[a][b]=='u') S->seq[a][b]='t';
8021           if ( S->seq[a][b]=='U') S->seq[a][b]='T';
8022         }
8023       HERE ("%s", S->seq[a]);
8024     }
8025   return S;
8026 }
8027 Sequence *dna_seq2rna_seq (Sequence *S)
8028 {
8029   int a, b;
8030   
8031   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);
8032   for ( a=0; a<S->nseq; a++)
8033     for (b=0; b<S->len[a]; b++)
8034       {
8035         if ( S->seq[a][b]=='t') S->seq[a][b]='u';
8036         if ( S->seq[a][b]=='T') S->seq[a][b]='U';
8037       }
8038   return S;
8039 }
8040
8041   
8042
8043 int get_longest_frame (char *seq, int mode);
8044 Sequence  *translate_dna_seqS     (Sequence *S, int frame, int stop)
8045 {
8046   //frame: 1->3
8047   char *s;
8048   int a, b,c,l;
8049
8050   for (a=0; a<S->nseq; a++)
8051     {
8052       s=S->seq[a];
8053       ungap(s);
8054       l=strlen (s);
8055       for (b=(frame-1); b<l; b+=3)
8056         {
8057           s[b]=translate_dna_codon (s+b,stop);
8058           for (c=b+1; c<b+3 && c<l; c++)s[c]='-';
8059         }
8060     }
8061   return S;
8062 }
8063 Alignment *translate_dna_aln (Alignment *A, int frame)
8064        {
8065          /*Given a set of aligned sequences
8066            starts from left to right
8067            3 nuc->1 aa
8068            2nuc+1gap, 1nuc+2gap->3 gaps
8069            1 stop-> 3gaps
8070            returns the corresponding amino acid alignment
8071          */
8072
8073
8074          int a, b,r;
8075
8076
8077          if (frame==3 || frame ==4)
8078            {
8079              
8080              for (a=0; a< A->nseq; a++)
8081                {
8082                  char *d, *buf, f;
8083                  d=A->seq_al[a];
8084                  f=get_longest_frame (d,frame);
8085                  buf=vcalloc ( strlen (d)+1, sizeof (char));
8086                  if ( f<3)
8087                    {
8088                      sprintf (buf, "%s", d+f);
8089                      sprintf (d, "%s", buf);
8090                      sprintf (A->seq_comment[a], " frame: %d", f);
8091                    }
8092                  else if ( f>=3)
8093                    {
8094                      f-=3;
8095                      sprintf ( buf, "%s", d);
8096                      buf=complement_string (buf);
8097                      sprintf (d, "%s",buf+f);
8098                      sprintf (A->seq_comment[a], " frame: %d Reverse Complement", f);
8099                    }
8100                  vfree (buf);
8101                }
8102            }
8103          else
8104            {
8105              
8106              for ( a=0; a< A->nseq; a++)
8107                for (b=0; b< frame; b++)
8108                  A->seq_al[a][b]='-';
8109              ungap_aln(A);
8110            }
8111
8112          for ( b=0; b< A->nseq; b++)
8113            for ( a=0; a< A->len_aln;)
8114              {
8115                
8116                r=translate_dna_codon (A->seq_al[b]+a, 'z');
8117                if (is_gap(r))
8118                  {
8119                    A->seq_al[b][a++]='-';
8120                    A->seq_al[b][a++]='-';
8121                    A->seq_al[b][a++]='-';
8122                  }
8123                else if ( r=='x')
8124                  {
8125                    A->seq_al[b][a++]='o';
8126                    A->seq_al[b][a++]='-';
8127                    A->seq_al[b][a++]='-';
8128                  }
8129                else if ( r=='z')
8130                  {
8131                    A->seq_al[b][a++]='x';
8132                    A->seq_al[b][a++]='-';
8133                    A->seq_al[b][a++]='-';
8134                  }
8135                else
8136                  {
8137                    A->seq_al[b][a++]=r;
8138                    A->seq_al[b][a++]='-';
8139                    A->seq_al[b][a++]='-';
8140                  }
8141              }   
8142          compress_aln (A);
8143          
8144          return A;
8145        }
8146
8147
8148 int seq2tblastx_db (char *out,Sequence *S, int strand)
8149 {
8150   //strand : same values as in ncbi blastall
8151   //1: direct
8152   //2:revers
8153   //3: both
8154   int a, b,d, l;
8155   char *prot, *pprot;
8156   int min_exon_len=5;
8157   FILE *fp;
8158   fp=vfopen (out, "w");
8159   for (a=0; a<S->nseq; a++)
8160     {
8161       for (b=-3; b<=3; b++)
8162         {
8163           int f;
8164           int dl;
8165
8166           dl=strlen (S->seq[a]);
8167           if (b==0)continue; 
8168           else if ( strand==1 && b<0)continue;//only direct strand
8169           else if ( strand==2 && b>0)continue;//only reverse strand
8170           else if (b<0)
8171             {
8172               S->seq[a]=complement_string (S->seq[a]);
8173               f=b*-1;
8174             }
8175           else
8176             f=b;
8177           prot=translate_dna_seq (S->seq[a], f, 'X', NULL);
8178           upper_string (prot);
8179          
8180           l=strlen (prot);
8181           pprot=prot;
8182           for (pprot=prot,d=0; d<=l; d++)
8183             {
8184               if (prot[d]=='\0')
8185                 {
8186                   prot[d]='\0';
8187                   if ( strlen (pprot)>min_exon_len)
8188                     {
8189                       int start, end;
8190                       end=(d)*3+(f-1);
8191                       start=(end-(strlen (pprot))*3)+1;
8192                       fprintf (fp, ">%s__%c__%d__%d__%d\n%s\n", S->name[a],(b>0)?'d':'r',start,end,dl, pprot);
8193                       pprot=prot+d+1;
8194                     }
8195                 }
8196             }
8197           vfree (prot);
8198           if (b<0) S->seq[a]=complement_string (S->seq[a]);
8199         }
8200     }
8201   vfclose (fp);
8202   return EXIT_SUCCESS;
8203 }
8204
8205 int get_longest_frame (char *in_seq, int mode)
8206 {
8207   char *prot, *seq;
8208   int a;
8209   int max_l=0, l;
8210   int best_frame=0;
8211   int nf;
8212   
8213   seq=vcalloc (strlen (in_seq)+1, sizeof (char));
8214   prot=vcalloc (strlen (in_seq)+1, sizeof (char));
8215   sprintf ( seq, "%s", in_seq);
8216   
8217   if ( mode == 3)nf=3;
8218   else if ( mode == 4) nf=6;
8219   
8220   for (a=0; a<nf; a++)
8221     {
8222       int f;
8223       if (a==3)seq=complement_string (seq);
8224       f=(a>=3)?a-3:a;
8225       prot=translate_dna_seq ( seq,f,'\0', prot);
8226       l=strlen (prot);
8227       if (l>=max_l){max_l=l;best_frame=a;}
8228     }
8229   vfree (seq);
8230   vfree (prot);
8231   return best_frame;
8232 }
8233
8234 Alignment *clean_gdna_aln (Alignment *A)
8235        {
8236            int a, b, c, r1, r2,s, p, n, tn;
8237            int *col;
8238            static int **mat;
8239            Alignment *T=NULL;
8240            int **score;
8241            char *buffer;
8242            
8243
8244            /*Viterbi Parameters*/
8245            int AL=0;        /*Allowed Transition*/
8246            int F=-1000000; /*Forbiden Transition*/
8247            int SPLICE_PENALTY=100;
8248            int ORF1=0, ORF2=1, ORF3=2, NC=3;
8249            
8250            int state, pstate, best_e, best_pstate_p,best_state_p, best_pstate_v, best_state_v, v;
8251            int nstate=4;
8252            int **transitions;
8253            int e;
8254            int **v_tab_p;
8255            int **v_tab;
8256            int * is_dna;
8257            
8258            best_state_p=best_state_v=best_pstate_p=best_pstate_v=best_e=0;
8259            buffer=vcalloc ( 100000, sizeof (char));
8260            is_dna=vcalloc ( A->nseq, sizeof (int));
8261            score=declare_int ( A->nseq+1, A->len_aln);
8262
8263
8264            if ( !mat)mat=read_matrice("pam250mt");
8265            T=copy_aln (A, T);
8266            col=vcalloc ( A->nseq, sizeof (int));
8267            
8268            for (a=0; a<= A->len_aln; a++)
8269                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];}
8270
8271            for ( a=0; a< A->nseq; a++)
8272                {
8273                    sprintf ( buffer, "%s", A->seq_al[a]);
8274                    ungap (buffer);
8275                    is_dna[a]=strm ( get_string_type (buffer), "DNA");
8276                }
8277            
8278
8279            for (a=0; a< A->len_aln-2; a++)
8280                {
8281                for (b=0; b< A->nseq; b++)
8282                        {
8283                        if (is_dna[b])col[b]=translate_dna_codon (A->seq_al[b]+a, 'x');
8284                        else col[b]=tolower ( A->seq_al[b][a]);   
8285                        }
8286
8287                for (n=0,tn=0,b=0; b< A->nseq; b++)
8288                    for ( c=b; c< A->nseq; c++   )
8289                        {
8290                            r1=col[b];
8291                            r2=col[c];
8292                            
8293                            if (r1=='x' || r2=='x'){score[A->nseq][a]=F;break;}
8294                            else if (r1=='-' && r2=='-');
8295                            else if (r1=='-' || r2=='-');
8296                            else 
8297                                {
8298                                
8299                                    if ( is_dna[b] && is_dna[c])score[A->nseq][a]+= mat[r1-'A'][r2-'A'];
8300                                    else score[A->nseq][a]+=mat[r1-'A'][r2-'A']* (A->nseq*A->nseq);
8301                                }
8302                            n+=( !is_gap(r1) && !is_gap(r2));
8303                            score[A->nseq][a]=(((tn!=0)?score[A->nseq][a]/tn:0));
8304                        }
8305                
8306                }
8307
8308            /*initialisation*/
8309
8310            transitions=declare_int ( nstate, nstate);
8311            v_tab=declare_int ( A->len_aln+2, nstate       );
8312            v_tab_p=declare_int ( A->len_aln+2, nstate       );
8313
8314            for (a=0; a<nstate;a++)
8315                for (b=0; b<nstate;b++)
8316                      {transitions[a][b]=F;}
8317
8318            transitions[ORF1][ORF2]=AL;
8319            transitions[ORF2][ORF3]=AL;
8320            transitions[ORF3][ORF1]=AL;     
8321            
8322            transitions[ORF3][NC]  =AL-SPLICE_PENALTY;
8323            transitions[NC][ORF1]  =AL-SPLICE_PENALTY;
8324
8325
8326            for ( s=0; s<A->nseq; s++)
8327                {
8328                for ( p=0; p<=A->len_aln; p++){for (state=0; state< nstate; state++)v_tab_p[p][state]=-1; }
8329                for (p=1+2; p<= A->len_aln; p++)
8330                    {
8331
8332                    for (state=0; state< nstate; state++)
8333                        {
8334                            
8335                            if ( state==NC){e=-best_e;}
8336                            else
8337                               {
8338                                   e=score[A->nseq][(p-1)-state];
8339                                   if ( state==0)best_e=e;
8340                                   else best_e=MAX(e, best_e);
8341                               }
8342
8343                            for ( pstate=0; pstate<nstate; pstate++)
8344                                {
8345                                    v=e+transitions[pstate][state]+v_tab[p-1][pstate];
8346                                    if (pstate==0 ||(v>best_pstate_v) )
8347                                       {
8348                                        best_pstate_v=v;
8349                                        best_pstate_p=pstate;
8350                                       }
8351                                }
8352                         
8353                            v_tab[p][state]=best_pstate_v;
8354                            v_tab_p[p][state]=best_pstate_p;
8355                            if (state==0 ||best_pstate_v>best_state_v )
8356                               {
8357                                best_state_p=state; 
8358                                best_state_v=best_pstate_v;
8359                               }
8360                        }
8361
8362                    }
8363
8364                
8365        
8366                for (p=0; p< A->len_aln; p++)T->seq_al[s][p]='.';
8367                for (p=A->len_aln; p>0; p--)
8368                    {
8369                        
8370                        if ( best_state_p==0)T->seq_al[s][p-1]=translate_dna_codon (A->seq_al[s]+(p-1), 'x');
8371                        else if ( best_state_p==1 || best_state_p==2)T->seq_al[s][p-1]='-';
8372                       
8373                       
8374                        
8375                        best_state_p=v_tab_p[p][best_state_p];
8376                        
8377                    }
8378                }
8379            
8380            
8381
8382            vfree (col);
8383            return T;
8384        }
8385
8386 Alignment *clean_cdna_aln (Alignment *A)
8387        {
8388          /*Given an alignmnet of nucleotides
8389            Returns the same alignmnent whith non coding nucleotides replaced with dots
8390            
8391            at each position, the emission probability is the sum of pair of the substitution of amino-acids
8392          */
8393          
8394            int a, b, c,s, p;
8395            static int **mat;
8396            int   *emission;
8397            float em1, em2;
8398            char *buffer;
8399            Alignment *B=NULL;
8400
8401
8402            
8403
8404            /*Viterbi Parameters*/
8405            int AL=0;        /*Allowed Transition*/
8406            int F=-1000000; /*Forbiden Transition*/
8407            int PENALTY=30;
8408            int NC, C1,C2, C3, START, END;
8409            int nstate=0;
8410            int state=0,best_state=0, score=0, best_score=0;
8411            int p_state;
8412            int e=0;
8413            int **score_tab;
8414            int **state_tab;
8415          
8416            int **transitions;
8417            int n;
8418            int r1, r2, r3;
8419
8420            NC=nstate++;
8421            C1=nstate++;
8422            C2=nstate++;
8423            C3=nstate++;
8424            START=nstate++;
8425            END=nstate++;
8426
8427            
8428            B=copy_aln (A, B);
8429            buffer=vcalloc ( 100000, sizeof (char));
8430            emission=vcalloc (A->len_aln, sizeof (int));
8431
8432            if ( !mat)
8433              {
8434                mat=read_matrice("pam250mt");
8435              }
8436
8437            /*Computation of the emission proba for the coding state*/
8438
8439
8440            for (a=0; a< A->len_aln; a++)
8441              {
8442
8443                /*First component: % occupancy of the column*/
8444                em1=0;
8445                for ( b=0; b< A->nseq; b++) em1+=!is_gap(translate_dna_codon (A->seq_al[b]+a, '-'));
8446                em1=em1/(float)A->nseq;
8447                
8448                /*Second Component: % similarity within column*/
8449                em2=0;
8450                for (n=0,b=0; b< A->nseq-1; b++)
8451                  {
8452                    r1=translate_dna_codon (A->seq_al[b]+a, '-');
8453                    
8454                    for (c=b+1; c<A->nseq; c++)
8455                      {
8456                        r2=translate_dna_codon (A->seq_al[c]+a, '-');
8457                        if (is_gap(r2) || is_gap(r1));
8458                        else
8459                          {
8460                            n++;
8461                            em2+=((mat[r1-'A'][r2-'A'])>1)?1:0;
8462                          }
8463                      }
8464                  }
8465                em2=em2/(float)((n==0)?1:n);
8466                
8467                
8468                emission[a]=(em1*100);
8469
8470              }
8471            
8472          
8473
8474            /*initialisation*/
8475
8476            transitions=declare_int ( nstate, nstate);
8477            score_tab=declare_int ( A->len_aln+2, nstate       );
8478            state_tab=declare_int ( A->len_aln+2, nstate       );
8479
8480            for (a=0; a<nstate;a++)
8481                for (b=0; b<nstate;b++)
8482                      {transitions[a][b]=F;}
8483
8484            
8485            transitions[START][C1]=AL;
8486            transitions[START][NC]=AL;
8487            transitions[C3][END]=AL;
8488            transitions[NC][END]=AL;
8489            transitions[C1 ][C2 ]=AL;
8490            transitions[C2 ][C3 ]=AL;
8491            transitions[C3 ][C1 ]=AL;
8492            transitions[C3 ][NC ]=AL-PENALTY;
8493            transitions[NC ][C1 ]=AL-PENALTY;
8494            transitions[NC][NC]=AL-PENALTY;
8495            
8496            
8497                    
8498            for ( s=0; s< A->nseq; s++)
8499              {
8500              for ( p=0; p<=A->len_aln; p++){for (state=0; state< nstate; state++){score_tab[p][state]=F;state_tab[p][state]=-1;} }
8501              score_tab[0][START]=0;
8502              
8503              for (p=1; p<= A->len_aln; p++)
8504                {
8505                  for (state=0; state< nstate; state++)
8506                    {
8507                      if ( state==START || state==END)continue;
8508                      else if      ( state==NC)  e=-10;
8509                      else if ( state==C1)
8510                        {
8511                          e=emission[p-1];
8512                        }
8513                      else if ( state ==C2)
8514                        {
8515                          if ( p-2<0)e=F;
8516                          else e=emission[p-2];
8517                        }
8518                      else if ( state==C3)
8519                        {
8520                          if ( p-3<0)e=F;
8521                          else e=emission[p-3];
8522                        }
8523                      
8524                      for (p_state=0; p_state<nstate; p_state++)
8525                        {
8526                          
8527                          if (e==F)score=F;
8528                          else 
8529                            {
8530                              score=(score_tab[p-1][p_state]==F)?F:(e+transitions[p_state][state]+score_tab[p-1][p_state]);
8531                            }
8532                          
8533                          if(p_state==0 || score>best_score){ best_score=score;best_state=p_state;}
8534                          
8535                        }
8536                      
8537                      score_tab[p][state]=best_score;
8538                      state_tab[p][state]=best_state;
8539                      
8540                    }
8541                }
8542              
8543              best_score=best_state=UNDEFINED;
8544              for (state=0; state<nstate; state++)
8545                {
8546                  if (state==START || state==END)continue;
8547                  e=transitions[state][END];
8548                  if (e==F || score_tab[p-1][state]==F)continue;
8549                  
8550                  if (best_score==UNDEFINED || score_tab[p-1][state]>best_score)
8551                    {
8552                      best_score=score_tab[p-1][state]+e; 
8553                      best_state=state;
8554                    }
8555                  
8556                }
8557              
8558              for (p=A->len_aln; p>0;)
8559                {
8560                  B->seq_al[s][p-1]=best_state+'0';
8561                  best_state=state_tab[p][best_state];
8562                  p--;
8563                }
8564              }
8565
8566            for ( a=0; a< A->nseq; a++)
8567              for ( b=0; b< A->len_aln;)
8568                {
8569                  s=B->seq_al[a][b];
8570                  if ( s==C1+'0')
8571                    {
8572                      r1=A->seq_al[a][b];
8573                      r2=A->seq_al[a][b+1];
8574                      r3=A->seq_al[a][b+2];
8575                      
8576
8577                      if ( is_gap(r1) ||is_gap(r2) ||  is_gap(r3))
8578                        {
8579                          A->seq_al[a][b]=(is_gap(r1))?'~':'.';
8580                          A->seq_al[a][b+1]=(is_gap(r2))?'~':'.';
8581                          A->seq_al[a][b+2]=(is_gap(r3))?'~':'.';
8582                        }
8583                      b+=3;
8584                    }
8585                  else if ( s==NC+'0')
8586                    {
8587                      A->seq_al[a][b]=(is_gap(A->seq_al[a][b]))?'~':'.';
8588                      b++;
8589                    }
8590                  else 
8591                    {
8592                      fprintf (stderr, "\nPROBLEM: [%d %d]->%d", a, b, s-'0');
8593                    }
8594                }
8595                  
8596
8597            free_aln (B);
8598            free_int (transitions, -1);
8599            free_int (score_tab, -1);
8600            free_int (state_tab, -1);
8601            vfree (emission);
8602            vfree (buffer);
8603            
8604            return A;
8605        }
8606
8607
8608
8609
8610 Alignment *translate_splice_dna_aln (Alignment *A, Alignment *ST)
8611        {
8612            int a, b, c, r1, r2,s, p, n, tn;
8613            int *col;
8614            static int **mat;
8615            Alignment *T=NULL;
8616            int **score;
8617            
8618            /*Viterbi Parameters*/
8619            int AL=0;        /*Allowed Transition*/
8620            int F=-1000000; /*Forbiden Transition*/
8621            int ORF1=0, ORF2=1, ORF3=2,SPL1=3, SPL2=4, SPL3=5, SPL4=6, NC=7;
8622            int SPLICE_PENALTY;
8623            int frame1, frame2, frame3, best_frame;
8624            int nstate=8;
8625            char r;
8626            
8627
8628
8629            int state=0, pstate=0, best_pstate_p=0,best_state_p=0, best_pstate_v=0, best_state_v=0, v=0;
8630            
8631            int **transitions;
8632            int e=0;
8633            int **v_tab_p;
8634            int **v_tab;
8635
8636            score=declare_int ( A->nseq+1, A->len_aln);
8637
8638
8639            if ( !mat)mat=read_matrice("pam250mt");
8640            T=copy_aln (A, T);
8641            col=vcalloc ( A->nseq, sizeof (int));
8642            
8643            for (a=0; a<= A->len_aln; a++)
8644                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];}
8645
8646            
8647            
8648
8649            for (a=0; a< A->len_aln-2; a++)
8650                {
8651                for (b=0; b< A->nseq; b++)
8652                        {
8653                        col[b]=translate_dna_codon (A->seq_al[b]+a, 'x');
8654                        }
8655                
8656                for (n=0,tn=0,b=0; b< A->nseq-1; b++)
8657                    for ( c=b+1; c< A->nseq; c++, tn++   )
8658                        {
8659                            r1=col[b];
8660                            r2=col[c];
8661                            
8662                            if (r1=='x' || r2=='x')score[A->nseq][a]=F;
8663                            else if (r1=='-' && r2=='-');
8664                            else if (r1=='-' || r2=='-');
8665                            else 
8666                                {
8667                                    score[A->nseq][a]+= mat[r1-'A'][r2-'A'];
8668                                    
8669                                }
8670                            n+=( !is_gap(r1) && !is_gap(r2));
8671                        }   
8672                score[A->nseq][a]=(((tn!=0)?score[A->nseq][a]/tn:0));
8673                  
8674                }
8675                
8676            /*initialisation*/
8677
8678            transitions=declare_int ( nstate, nstate);
8679            v_tab=declare_int ( A->len_aln+2, nstate*nstate);
8680            v_tab_p=declare_int ( A->len_aln+2, nstate*nstate);
8681
8682            for (a=0; a<nstate;a++)
8683              for (b=0; b<nstate;b++)
8684                {transitions[a][b]=F;}
8685
8686            SPLICE_PENALTY=-1000;
8687
8688            transitions[ORF1][ORF2]    =AL;
8689            transitions[ORF1][SPL1]    =AL-SPLICE_PENALTY;
8690            
8691            transitions[ORF2][ORF3]    =AL;
8692            transitions[ORF2][SPL1]    =AL-SPLICE_PENALTY;
8693            
8694            transitions[ORF3][ORF1]    =AL;
8695            transitions[ORF3][SPL1]    =AL-SPLICE_PENALTY;
8696            
8697            transitions[ORF3][ORF1]    =AL;
8698            transitions[ORF3][SPL1]    =AL-SPLICE_PENALTY;
8699            
8700            transitions[ORF3][NC]=AL-100;
8701            transitions[NC][ORF1]=AL-100;
8702
8703
8704            transitions[SPL1][SPL2]=AL;
8705            transitions[SPL2][NC  ]=AL-SPLICE_PENALTY;
8706            transitions[NC  ][NC  ]=AL;
8707            transitions[NC  ][SPL3]=AL-SPLICE_PENALTY;
8708            transitions[SPL3][SPL4]=AL;
8709            transitions[SPL4][ORF1]=AL;
8710            transitions[SPL4][ORF2]=AL;
8711            transitions[SPL4][ORF3]=AL;
8712            
8713
8714            for ( s=0; s<A->nseq; s++)
8715                {
8716                for ( p=0; p<=A->len_aln; p++){for (state=0; state< nstate; state++)v_tab_p[p][state]=-1; }
8717                for (p=1+2; p<= A->len_aln; p++)
8718                    {
8719                     frame1=score[A->nseq][(p-1)];
8720                     frame2=score[A->nseq][(p-1)-1];
8721                     frame3=score[A->nseq][(p-1)-2];  
8722                     best_frame=best_int (3, 1, &a, frame1, frame2, frame3);
8723                     for (state=0; state< nstate; state++)
8724                        {
8725                          r=tolower (A->seq_al[s][p-1]);
8726                          r=(r=='u')?'t':r;
8727                          
8728                          if      (state==ORF1)e=frame1;
8729                          else if (state==ORF2)e=frame2;
8730                          else if (state==ORF3)e=frame3;
8731                          else if (state==SPL1)e=(r=='g')?best_frame:F;
8732                          else if (state==SPL2)e=(r=='t')?best_frame:F;
8733                          else if (state==SPL3)e=(r=='a')?best_frame:F;
8734                          else if (state==SPL4)e=(r=='g')?best_frame:F;
8735                          else if (state==NC)e=-best_frame;
8736                          for ( pstate=0; pstate<nstate; pstate++)
8737                                {
8738                                    v=e+transitions[pstate][state]+v_tab[p-1][pstate];
8739                                    if (pstate==0 ||(v>best_pstate_v) ){best_pstate_v=v;best_pstate_p=pstate;}
8740                                }
8741                         
8742                            v_tab[p][state]=best_pstate_v;
8743                            v_tab_p[p][state]=best_pstate_p;
8744                            if (state==0 ||best_pstate_v>best_state_v ){best_state_p=state; best_state_v=best_pstate_v;}
8745                        }
8746                    }
8747
8748                
8749        
8750                for (p=0; p< A->len_aln; p++)T->seq_al[s][p]='.';
8751                for (p=A->len_aln; p>0; p--)
8752                    {
8753                        if ( best_state_p==0)T->seq_al[s][p-1]=toupper(translate_dna_codon (A->seq_al[s]+(p-1), 'x'));
8754                        else if ( best_state_p>=SPL1  && best_state_p<=SPL4)T->seq_al[s][p-1]='-';
8755                        best_state_p=v_tab_p[p][best_state_p];
8756                    }
8757                }
8758            
8759            
8760
8761            vfree (col);
8762            return T;
8763        }
8764
8765 Alignment * mutate_cdna_aln ( Alignment *A)
8766 {
8767     int a, b, c, n;
8768     int n1, n2, r1, r2;
8769     int **pos, ps;
8770     int neutral_substitution=50;
8771     int random_substitution=0;
8772     int random_deletion=0;
8773     int amino_acid_deletion=0;
8774     int amino_acid_substitution=0;
8775     char nuc_list[]="agct";
8776     char *new_codon;
8777
8778     neutral_substitution=atoi(get_env_variable ("NEUTRAL_SUBSTITUTION",IS_FATAL));
8779     random_substitution =atoi(get_env_variable ("RANDOM_SUBSTITUTION", IS_FATAL));
8780     random_deletion     =atoi(get_env_variable ("RANDOM_DELETION", IS_FATAL));
8781     amino_acid_deletion =atoi(get_env_variable ("AMINO_ACID_DELETION", IS_FATAL));
8782     amino_acid_substitution =atoi(get_env_variable ("AMINO_ACID_SUBSTITUTION", IS_FATAL));
8783     
8784     
8785     if (A->S)free_sequence ( A->S, (A->S)->nseq);
8786     A->S=aln2seq(A);
8787
8788     addrandinit(time (NULL));
8789
8790     
8791     pos=aln2pos_simple ( A, A->nseq);
8792     
8793     /* 1 Apply neutral substitutions    */
8794     
8795     if ( neutral_substitution)
8796         {
8797         for (  c=0; c< neutral_substitution; c++)
8798             {
8799             for (  a=0; a< A->nseq; a++)
8800                 {
8801                     
8802                     for ( b=0; b< A->len_aln; b++)
8803                         {
8804                         
8805                         if (pos[a][b]<=0)continue; 
8806                         ps=MAX(0,pos[a][b]-(pos[a][b]-1)%3-1);
8807
8808
8809                         n1=(A->S)->seq[a][pos[a][b]-1];
8810                         r1=translate_dna_codon ( (A->S)->seq[a]+ps, 'o');
8811                         
8812                         n2=nuc_list[(int)addrand((unsigned long) 4)];
8813                         (A->S)->seq[a][pos[a][b]-1]=n2;
8814                         r2=translate_dna_codon ( (A->S)->seq[a]+ps, 'o');
8815                         
8816                         
8817                         if ( r1==r2 && r1!='o')A->seq_al[a][b]=n2;
8818                         
8819                         else (A->S)->seq[a][pos[a][b]-1]=n1;
8820                         }
8821                 }
8822             }
8823         }
8824
8825     /* 2 Apply         substitutions    */
8826      if ( random_substitution)
8827         {
8828         for (  a=0; a< A->nseq; a++)
8829             {
8830                 for ( b=0; b< A->len_aln; b++)
8831                     {
8832                     if (pos[a][b]<=0)continue; 
8833                     if (addrand ((unsigned long) 100)>random_substitution)continue; 
8834                     
8835                     n1=nuc_list[(int)addrand((unsigned long)4)];
8836                     (A->S)->seq[a][pos[a][b]-1]=n1;
8837                     A->seq_al[a][b]=n1;
8838                     }
8839             }
8840         }
8841     
8842     /* 3 Apply amino acid substitutions */
8843       if ( amino_acid_substitution)
8844         {
8845         for (  a=0; a< A->nseq; a++)
8846             {
8847                 for ( b=0; b< A->len_aln; b+=3)
8848                     {
8849                     if (pos[a][b]<=0)continue; 
8850                     if (addrand ((unsigned long) 100)>amino_acid_substitution)continue; 
8851                     ps=MAX(0,pos[a][b]-(pos[a][b]-1)%3-1);
8852                     
8853                     r1=translate_dna_codon ( (A->S)->seq[a]+ps, 'o');
8854                     new_codon=mutate_amino_acid(r1, "clustalw_col");
8855                     
8856                     for ( c=ps; c<ps+3; c++)(A->S)->seq[a][c]=new_codon[c-ps];
8857                     }
8858                 for ( b=0; b< A->len_aln; b++)
8859                     {
8860                     if (pos[a][b]<=0)continue; 
8861                     else A->seq_al[a][b]=(A->S)->seq[a][pos[a][b]-1];
8862                     }
8863             }
8864         }  
8865     /* 3 Apply amino acid deletions     */
8866      if ( amino_acid_deletion)
8867         {
8868         for (  a=0; a< A->nseq; a++)
8869             {
8870                 for ( b=0; b< A->len_aln; b+=3)
8871                     {
8872                     if (pos[a][b]<=0)continue; 
8873                     if (addrand ((unsigned long) 1000)>amino_acid_deletion)continue; 
8874                     ps=MAX(0,pos[a][b]-(pos[a][b]-1)%3-1);
8875                     n=addrand ((unsigned long) 4)+1;
8876                     
8877                     for ( c=ps; c<ps+(3*n) && c<A->len_aln; c++)(A->S)->seq[a][c]='-';
8878                     }
8879                 for ( b=0; b< A->len_aln; b++)
8880                     {
8881                     if (pos[a][b]<=0)continue; 
8882                     else A->seq_al[a][b]=(A->S)->seq[a][pos[a][b]-1];
8883                     }
8884             }
8885         }
8886     /* 4 Apply amino acid insertions    */
8887
8888 /*FRAMESHIFT MUTATIONS*/
8889     /* 5 Apply nucleotide deletions*/
8890      if ( random_deletion)
8891         {
8892         for (  a=0; a< A->nseq; a++)
8893             {
8894                 for ( b=0; b< A->len_aln; b++)
8895                     {
8896                     if (pos[a][b]<=0)continue; 
8897                     if (addrand ((unsigned long) 1000)>random_deletion)continue; 
8898                     
8899                     n1='-';
8900                     (A->S)->seq[a][pos[a][b]-1]=n1;
8901                     A->seq_al[a][b]=n1;
8902                     }
8903             }
8904         }
8905     /* 6 Apply nucleotide deletions*/
8906      free_int (pos, -1);
8907    return A;
8908
8909 }    
8910     
8911 Alignment* clean_est  ( Alignment *A)
8912         {
8913           /*Rules are as follow:
8914             Internal Gap > 30% Requences ----> -
8915             Best Residue < 50% Residues  ----> 'N'
8916           */
8917           int a, b,c;
8918           int best;
8919           int tot;
8920
8921           for ( a=0; a< A->len_aln; a++)
8922             {
8923               
8924               for (tot=0, b=0; b<4; b++)tot+=(A->P)->count[b][a];
8925               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]);
8926               
8927               if ( tot==0)
8928                 {
8929                   fprintf ( stderr, "\nWARNING: POSITION WITH NO INFORMATION [clean_est:%s]", PROGRAM);
8930                   A->seq_al[0][a]='-';
8931                 }
8932               else if (((A->P)->count[4][a]*100)/tot >30)A->seq_al[0][a]='-';
8933               else if ( (best*100)/tot<50)A->seq_al[0][a]='n';
8934               
8935             }
8936         return A;
8937         }
8938            
8939     
8940
8941 char **make_symbols ( char *name, int *n)
8942     {
8943     char **symbol;
8944
8945     symbol=declare_char ( STRING, STRING);
8946     
8947     if ( strcmp (name, "3d_ali")==0)
8948         {
8949         sprintf ( symbol[0], "gih");
8950         sprintf ( symbol[1], "eb");
8951         sprintf ( symbol[2], "x");
8952         sprintf ( symbol[3], "#l");
8953         n[0]=4;
8954         }
8955     
8956     else if ( strcmp (name, "all")==0)
8957         {
8958           int a, i;
8959           for ( i=0,a=0; a<26; a++)
8960             {
8961               sprintf ( symbol[i++], "%c%c", 'a'+a, 'a'+a);
8962               sprintf ( symbol[i++], "%c%c", 'A'+a, 'A'+a);
8963             }
8964           sprintf ( symbol[i++], "--");
8965           n[0]=i;
8966         }
8967     
8968     else if ( strcmp (name, "set1")==0)
8969         {
8970         sprintf ( symbol[0], "ilvmfywhktcagH");
8971         sprintf ( symbol[1], "reqdnsP");
8972         sprintf ( symbol[2], "--");
8973         sprintf ( symbol[3], "#l");
8974         n[0]=4;
8975         }
8976     else if ( strcmp (name, "set2")==0)
8977         {
8978         n[0]=0;
8979         sprintf ( symbol[n[0]++], "gsacT");
8980         sprintf ( symbol[n[0]++], "ndtvpS");
8981         sprintf ( symbol[n[0]++], "ilkreqL");
8982         sprintf ( symbol[n[0]++], "--");
8983         sprintf ( symbol[n[0]++],"#l"); 
8984         }
8985     else if ( strcmp ( name, "any")==0)
8986         {
8987         sprintf ( symbol[0], "*x");
8988         n[0]=1;
8989         }
8990
8991
8992
8993
8994     return symbol;
8995     }
8996
8997 char *testdna2gene (char *dna)
8998 {
8999   int *w,a,l;
9000   l=strlen (dna);
9001   HERE ("%s", dna);
9002   w=vcalloc(l+1, sizeof (int));
9003   for (a=0; a<l; a++)
9004     {
9005       w[a]=isupper (dna[a])?1:-1;
9006     }
9007   dna=dna2gene (dna,w);
9008   vfree (w);
9009   return dna;
9010 }
9011
9012 Sequence *dnaseq2geneseq (Sequence *S, int **w)
9013 {
9014   Sequence *PS;
9015   int a;
9016   char *p;
9017
9018   PS=duplicate_sequence (S);
9019   for (a=0; a<S->nseq; a++)
9020     {
9021       p=dna2gene (S->seq[a], w[a]);
9022       if (strstr (p, "F"))
9023         {
9024           HERE ("----FRAMESHIFT: %s", S->name[a]);
9025         }
9026       vfree (PS->seq[a]);
9027       PS->len[a]=strlen(p);
9028       PS->seq[a]=vcalloc (PS->len[a]+1, sizeof (char));
9029       sprintf ( PS->seq[a], "%s", p);
9030       vfree (p);
9031     }
9032   PS=reset_sequence_len (PS);
9033   return PS;
9034 }
9035
9036 char *dna2gene (char *dna, int *w)
9037 {
9038   int a, b, c, ns,l,od;
9039   int I1, I2, I3, START, NCE, NCS;
9040   int C1, S1_1, S2_1, S3_1, S4_1,NC1;
9041   int C2, S1_2, S2_2, S3_2, S4_2,NC2;
9042   int C3, S1_3, S2_3, S3_3, S4_3,NC3;
9043   int ST;
9044   int st;
9045   
9046   double p_C1, p_C2;
9047   double **C1_mat, **C2_mat;
9048   double *tb, **sc_mat, **tb_mat;
9049   double **em, **trans;
9050   double avg_w=0;
9051   
9052   char **string;
9053   double forbiden   =-100000;
9054   double frameshift1;
9055   double frameshift2;
9056   double exon_penalty;
9057   double exon_reward;
9058   double nostop_penalty;
9059   double shiftw;
9060   int frameshift_symbol='F';
9061   char *out_dna;
9062   int max=0;
9063
9064   char *three_dna;
9065
9066   three_dna=translate_dna_seq_on3frame (dna, 'x', NULL);
9067   lower_string(three_dna);
9068   l=strlen (dna);
9069   for (a=0; a<l; a++){max=MAX(max,w[a]);avg_w+=(double)w[a];}
9070   avg_w/=(double)l;
9071   shiftw=avg_w*-2;
9072   
9073   exon_penalty=-100*avg_w;
9074   exon_reward=avg_w;
9075   nostop_penalty=-100 *avg_w;
9076   frameshift1=forbiden;
9077   frameshift2=frameshift1;
9078   
9079   out_dna=vcalloc ( 2*strlen (dna)+1, sizeof (char));
9080   sprintf (out_dna, "%s", dna);
9081   ns=0;
9082   START=ns++;  I1=ns++;I2=ns++;I3=ns++;NCE=ns++;NCS=ns++;
9083   
9084   C1=ns++; S1_1=ns++;S2_1=ns++;NC1=ns++;S3_1=ns++;S4_1=ns++;
9085   C2=ns++; S1_2=ns++;S2_2=ns++;NC2=ns++;S3_2=ns++;S4_2=ns++;
9086   C3=ns++; S1_3=ns++;S2_3=ns++;NC3=ns++;S3_3=ns++;S4_3=ns++;
9087   ST=ns++;
9088   
9089   string=declare_char ( ns+1, 10);
9090   sprintf (string [S1_1], "S1_1");
9091   sprintf (string [S2_1], "S2_1");
9092   sprintf (string [S3_1], "S3_1");  
9093   sprintf (string [S4_1], "S4_1");
9094  
9095   sprintf (string [S1_2], "S1_2");
9096   sprintf (string [S2_2], "S2_2");
9097   sprintf (string [S3_2], "S3_2");  
9098   sprintf (string [S4_2], "S4_2");
9099   
9100   sprintf (string [S1_3], "S1_3");
9101   sprintf (string [S2_3], "S2_3");
9102   sprintf (string [S3_3], "S3_3");  
9103   sprintf (string [S4_3], "S4_3");
9104   
9105   sprintf (string [START], "START");
9106   sprintf (string [NCE], "NCE");
9107   sprintf (string [NCS], "NCS");
9108   sprintf (string [NC1], "NC1");
9109   sprintf (string [NC2], "NC2");
9110   sprintf (string [NC3], "NC3");
9111   sprintf (string [I1], "I1");
9112   sprintf (string [I2], "I2");
9113   sprintf (string [I3], "I3");
9114  
9115   sprintf (string [C1], "C1");
9116   sprintf (string [C2], "C2");
9117   sprintf (string [C3], "C3");
9118   
9119   sprintf (string [ST], "STOP");
9120
9121   trans=declare_double(ns,ns);
9122   em=declare_double   (ns,256);
9123   tb=vcalloc ( l+1, sizeof (double));
9124   sc_mat=declare_double (l+1, ns);
9125   tb_mat=declare_double (l+1, ns);
9126   C1_mat=declare_double (l+1, ns);
9127   C2_mat=declare_double (l+1, ns);
9128   
9129   for (a=0; a<ns; a++)
9130     {
9131       for (b=0; b<ns; b++)trans[a][b]=forbiden;
9132       for (b=0; b<256; b++)em[a][b]=forbiden;
9133     }
9134  
9135   trans[START][I1]=0;
9136   trans[START][NCS]=0;
9137   trans[NCS][NCS]=0;
9138   trans[NCS][NCE]=0;//allow sequence entirely non coding
9139   
9140   trans[NCS][I1]=0;
9141   trans[I1][I2]=0;
9142   trans[I2][I3]=0;
9143   trans[I3][C1]=0;
9144   
9145   trans[C1][C2]=0;
9146   trans[C1][C3]=frameshift1;
9147   trans[C1][C1]=frameshift2;
9148   
9149   trans[C2][C3]=0;
9150   trans[C2][C1]=frameshift1;
9151   trans[C2][C2]=frameshift2;
9152  
9153   
9154   trans[C3][C1]=exon_reward;
9155   trans[C3][C2]=frameshift1;
9156   trans[C3][C3]=frameshift2;
9157   
9158   trans[C3][NCE]=nostop_penalty;
9159   trans[C3][ST] =0; //normal termination
9160   trans[ST][NCE]=0;
9161   trans[NCE][NCE]=0;
9162   
9163   trans[C1][S1_1]=exon_penalty;
9164   trans[S1_1][S2_1]=0;
9165   trans[S2_1][NC1]=0;
9166   trans[NC1][NC1]=0;
9167   trans[NC1][S3_1]=0;
9168   trans[S3_1][S4_1]=0;
9169   trans[S4_1][C2]=0;
9170   trans[S4_1][C3]=frameshift1;
9171   trans[S4_1][C1]=frameshift2;
9172   
9173
9174   trans[C2][S1_2]=exon_penalty;
9175   trans[S1_2][S2_2]=0;
9176   trans[S2_2][NC2]=0;
9177   trans[NC2][NC2]=0;
9178   trans[NC2][S3_2]=0;
9179   trans[S3_2][S4_2]=0;
9180   trans[S4_2][C3]=0;
9181   trans[S4_2][C1]=frameshift1;
9182   trans[S4_2][C2]=frameshift2;
9183   
9184   trans[C3][S1_3]=exon_penalty;
9185   trans[S1_3][S2_3]=0;
9186   trans[S2_3][NC3]=0;
9187   trans[NC3][NC3]=0;
9188   trans[NC3][S3_3]=0;
9189   trans[S3_3][S4_3]=0;
9190   trans[S4_3][C1]=0;
9191   trans[S4_3][C2]=frameshift1;
9192   trans[S4_3][C3]=frameshift2;
9193   
9194   em[I1]['A']=1;
9195   em[I2]['T']=1;
9196   em[I3]['G']=1;
9197
9198   em[S1_1]['G']=1;
9199   em[S2_1]['T']=1;
9200   em[S3_1]['A']=1;
9201   em[S4_1]['G']=1;
9202
9203   em[S1_2]['G']=1;
9204   em[S2_2]['T']=1;
9205   em[S3_2]['A']=1;
9206   em[S4_2]['G']=1;
9207
9208   em[S1_3]['G']=1;
9209   em[S2_3]['T']=1;
9210   em[S3_3]['A']=1;
9211   em[S4_3]['G']=1;
9212  
9213
9214   for (a=0; a<ns; a++)sc_mat[0][a]=tb_mat[0][a]=forbiden;
9215   sc_mat[0][START]=tb_mat[0][START]=0;
9216   
9217   for (a=1; a<=l ;a++)
9218     {
9219       int r;
9220       r=toupper (dna[a-1]);
9221       for (b=0; b<ns; b++)
9222         {
9223           double best_sc,e,lw;
9224           int best_t;
9225           
9226           lw=(double)w[a-1]+shiftw;
9227
9228           if (b==ST && three_dna[a-1]=='x')e=0;
9229           else if (b==C1 || b == C2 || b== C3)e=lw;
9230           else if ( b==NC1|| b==NC2 || b==NC3 || b==NCE || b==NCS)e=-lw;
9231           else e=em[b][r];
9232           
9233           if (e==forbiden)
9234           {
9235                 sc_mat[a][b]=forbiden;
9236                 tb_mat[a][b]=0;
9237           }
9238           else
9239              {
9240                for (best_sc=forbiden,best_t=0,c=0; c<ns; c++)
9241                  {
9242                    double tr, sc, p_sc;
9243                    tr  = trans[c][b];
9244                    p_sc=sc_mat[a-1][c];
9245
9246                    //Frameshift handling
9247                    
9248                    if ( tr== forbiden || p_sc==forbiden);
9249                    else if (tr!=forbiden)
9250                      {
9251                        if     (b==C2 && c!=C1 && c!=S4_1){p_C1='N'; p_C2=r;  }
9252                        else if(b==C3 && c!=C2 && c!=S4_2){p_C1='N'; p_C2='N';}
9253                        else
9254                          {
9255                            p_C1=C1_mat[a-1][c];
9256                            p_C2=C2_mat[a-1][c];
9257                          }
9258                        
9259                        if (b==C3 && is_stop (p_C1, p_C2,r)){tr=forbiden;}
9260                        else
9261                          {
9262                            sc=tr+e+p_sc;
9263                            if (c==0 || sc>best_sc)
9264                              {
9265                                best_sc=sc;
9266                                best_t =c;
9267                              }
9268                          }
9269                      }
9270                  }
9271           
9272                C1_mat[a][b]=(b==C1)?r:C1_mat[a-1][best_t];
9273                C2_mat[a][b]=(b==C2)?r:C2_mat[a-1][best_t];
9274                sc_mat[a][b]=best_sc;
9275                tb_mat [a][b]=best_t;
9276              }
9277          
9278         }
9279     }
9280   
9281   a=l;
9282   b=NCE;
9283   c=sc_mat[a][NCE];
9284   
9285   while (a>0)
9286     {
9287 //      HERE ("**%d [%s] %c in %d", b,string[b], dna[a-1], a);
9288       tb[a]=b;
9289       b=tb_mat[a][b];
9290       a--;
9291     }
9292
9293   od=0;
9294   st=0;
9295   for (a=0;a<l;a++)
9296     {
9297       int r,t,pt, coding;
9298       t =tb[a+1];
9299       pt=tb[a ];
9300       r=dna[a];
9301       if ( st || t==ST)st++;
9302       coding=(t==C1 || t==C2 || t==C3 || t==I1 || t==I2 || t==I3 ||(st && st<=3))?1:0;
9303
9304       
9305       
9306       if      (t==C1 && (pt==C2 || pt==S4_2)){out_dna[od++]=frameshift_symbol;}
9307       else if (t==C1 && (pt==C1 || pt==S4_1)){out_dna[od++]=frameshift_symbol;out_dna[od++]=frameshift_symbol;}
9308      
9309       else if (t==C2 && (pt==C3 || pt==S4_3)){out_dna[od++]=frameshift_symbol;}
9310       else if (t==C2 && (pt==C2 || pt==S4_2)){out_dna[od++]=frameshift_symbol;out_dna[od++]=frameshift_symbol;}
9311       
9312       else if (t==C3 && (pt==C1 || pt==S4_1)){out_dna[od++]=frameshift_symbol;}
9313       else if (t==C3 && (pt==C3 || pt==S4_3)){out_dna[od++]=frameshift_symbol;out_dna[od++]=frameshift_symbol;}
9314       
9315       if (coding)out_dna[od++]=toupper (r);
9316       else out_dna[od++]=tolower(r);
9317       
9318     }
9319
9320   free_double (tb_mat, -1);
9321   free_double (sc_mat, -1);
9322   free_double (trans, -1);
9323   free_double (em, -1);
9324   free_double (C1_mat, -1);
9325   free_double (C2_mat, -1);
9326   vfree (tb);
9327   vfree (three_dna);
9328   return out_dna;
9329 }   
9330
9331    
9332 int res_weights2avg(Sequence *R, int **w)
9333 {
9334   int a, b;
9335   double avg=0;
9336   int n=0;
9337   
9338   for (a=0; a<R->nseq; a++)
9339     for (b=0; b<R->len[a]; b++){avg+=w[a][b];n++;}
9340   return avg/n;
9341 }
9342 int res_weights2min(Sequence *R, int **w)
9343 {
9344   int a,b;
9345   int min=w[0][0];
9346   
9347   for (a=0; a<R->nseq; a++)
9348     for (b=0; b<R->len[a]; b++)min=MIN(min,(w[a][b]));
9349   return min;
9350 }
9351 int res_weights2max(Sequence *R, int **w)
9352 {
9353   int a, b;
9354   int max=w[0][0];
9355   
9356   for (a=0; a<R->nseq; a++)
9357     for (b=0; b<R->len[a]; b++)max=MAX(max,(w[a][b]));
9358   return max;
9359 }
9360 int scan_res_weights4ac (Sequence *R, int **w, int start, int end, int step)
9361 {
9362   int best_t, a;
9363   float best_ac=0;
9364   float *count;
9365   float *acc;
9366   int avg;
9367
9368   avg=res_weights2avg(R,w);
9369   best_t=start;
9370   for (a=start; a<=end; a+=step)
9371     {
9372      
9373       count=res_weights2accuracy_counts (R,w,a,NULL);
9374       acc=counts2accuracy (count);
9375       
9376       if (acc[3]>best_ac)
9377         {
9378           best_ac=acc[3];
9379           best_t=a;
9380         }
9381       vfree (count);vfree (acc);
9382     }
9383   count=res_weights2accuracy_counts (R,w,best_t,NULL);
9384   acc=counts2accuracy (count);
9385   fprintf (stderr, "\nBest_T: %d ", best_t);
9386   display_accuracy (count,stderr);
9387   
9388   count=res_weights2accuracy_counts (R,w,2*avg,NULL);
9389   acc=counts2accuracy (count);
9390   fprintf (stderr, " Avg_T: %d ", 2*avg);
9391   display_accuracy (count,stderr);
9392   
9393   vfree (acc); vfree (count);
9394   return best_t;
9395 }
9396 int ** shift_res_weights ( Sequence *R, int **w, int shift)
9397 {
9398   int a, b;
9399   for (a=0; a<R->nseq; a++)
9400     for (b=0; b<R->len[a]; b++)
9401       w[a][b]+=shift;
9402   return w;
9403 }
9404 float *res_weights2accuracy_counts ( Sequence *R, int **w,int T, float *result)
9405 {
9406   int a, b, coding,pcoding;
9407   
9408   if (!result)result=vcalloc (4, sizeof (float));
9409   
9410   for (a=0; a<R->nseq; a++)
9411     {
9412       for (b=0; b<R->len[a]; b++)
9413         {
9414           coding=(isupper(R->seq[a][b]))?1:0;
9415           pcoding=(w[a][b]>T)?1:0;
9416           if      (  coding &&  pcoding)result[0]++;//TP
9417           else if ( !coding && !pcoding)result[1]++;//TN
9418           else if ( !coding &&  pcoding)result[2]++;//FP
9419           else if (  coding && !pcoding)result[3]++;//FN
9420         }
9421     }
9422   return result;
9423 }
9424
9425 void genepred_seq2accuracy_counts4all (Sequence *R, Sequence *T)
9426 {
9427   int a,b;
9428   float *result =vcalloc (4, sizeof (float));
9429
9430   fprintf ( stderr, "\n");
9431
9432   for (a=0; a<R->nseq; a++)
9433   {
9434     fprintf ( stderr, "gene: %s ", R->name[a]);
9435     for (b=0; b<T->nseq; b++)
9436     {
9437       if ( strm (R->name[a], T->name[b]) && hasupper(R->seq[a]))
9438       {
9439         vfree (display_accuracy (genepred2accuracy_counts (R->seq[a], T->seq[b], NULL),stderr));
9440         break;
9441       }
9442     }
9443   }
9444   vfree(result);
9445 }
9446       
9447 float* genepred_seq2accuracy_counts (Sequence *R, Sequence *T,float *result)
9448 {
9449   int a,b;
9450   
9451   if (!result)result=vcalloc (4, sizeof (float));
9452
9453   for (a=0; a<R->nseq; a++)
9454     for (b=0; b<T->nseq; b++)
9455       if ( strm (R->name[a], T->name[b]) && hasupper(R->seq[a]))
9456         genepred2accuracy_counts (R->seq[a], T->seq[b], result);
9457   return result;
9458 }
9459
9460 float* genepred2accuracy_counts      (char *ref,  char *target , float *result)
9461 {
9462   char *ref2, *target2;
9463   int l,a;
9464   if ( !result) result=vcalloc (4, sizeof (float));
9465   ref2=vcalloc ( strlen (ref)+1, sizeof (char));
9466   sprintf ( ref2, "%s", ref);
9467   
9468   target2=vcalloc ( strlen (target)+1, sizeof (char));
9469   sprintf ( target2, "%s", target);
9470   
9471   remove_charset (ref2, "Ff");
9472   remove_charset (target2, "Ff");
9473
9474   if ( strlen (target2) != strlen (ref2))
9475     {fprintf (stderr, "ERROR: Gene and target have different length [FATAL]\n"); myexit (EXIT_FAILURE);}
9476   
9477   l=strlen (ref2);
9478   for (a=0; a<l; a++)
9479     {
9480       int coding, pcoding;
9481       coding =isupper (ref2[a]);
9482       pcoding=isupper (target2[a]);
9483       if      ( coding  &&  pcoding)result[0]++;//TP
9484       else if ( !coding && !pcoding)result[1]++;//TN
9485       else if ( !coding &&  pcoding)result[2]++;//FP
9486       else if (  coding && !pcoding)result[3]++;//FN
9487     }
9488   
9489   vfree (ref2);
9490   vfree (target2);
9491   return result;
9492  }
9493
9494 int is_stop( char r1, char r2, char r3)
9495 {
9496   char codon[4];
9497   
9498   if (!r2 || !r3) return 0;
9499   else if (tolower (r1)=='n' || tolower(r2)=='n' || tolower(r3)=='n') return 0;
9500   else 
9501     {
9502       sprintf (codon, "%c%c%c", tolower(r1), tolower(r2), tolower(r3));
9503       if (translate_dna_codon (codon, 'x')=='x')return 1;
9504       else return 0;
9505     }
9506 }
9507
9508
9509 char * translate_dna_seq_on3frame (  char *dna_seq, char stop, char *prot)
9510        {
9511           int a, l;
9512           char *buf;
9513
9514           l=strlen (dna_seq);
9515           if ( prot==NULL)prot=vcalloc ( l+2, sizeof (char));
9516            
9517            buf=vcalloc (l+4, sizeof (char));
9518            sprintf (buf, "%s", dna_seq);
9519            lower_string ( buf);
9520            for ( a=0; a< l; a++)buf[a]=(buf[a]=='t')?'u':buf[a];
9521            
9522            for (a=0; a< l; a++)
9523                prot[a]=translate_dna_codon (buf+a, stop);
9524            vfree (buf);
9525            prot[a]='\0';
9526
9527            return prot;
9528        }
9529 char * translate_dna_seq ( char *dna_seq, int frame, char stop, char *prot)
9530        {
9531          //frame: 1->3
9532            int a, b, l;
9533            char *buf;
9534
9535            l=strlen (dna_seq);
9536            if ( prot==NULL)prot=vcalloc ( l, sizeof (char));
9537            frame--;
9538            buf=vcalloc (l+4, sizeof (char));
9539            sprintf (buf, "%s", dna_seq);
9540            lower_string ( buf);
9541            for ( a=0; a< l; a++)buf[a]=(buf[a]=='t')?'u':buf[a];
9542            
9543            
9544            for ( b=0,a=0+frame; a< l; a+=3,b++)
9545              {
9546
9547                prot[b]=translate_dna_codon (buf+a, stop);
9548              }
9549            vfree (buf);
9550            prot[b]='\0';
9551            upper_string (buf);
9552            return prot;
9553        }
9554 char * back_translate_dna_codon ( char aa, int deterministic)
9555         {
9556         static char *r;
9557         int choice;
9558         
9559         vsrand(0);
9560         if ( r==NULL)r=vcalloc (4, sizeof (char));
9561         
9562         if (!is_gap(aa))aa=tolower(aa);
9563         
9564         if (is_gap(aa))sprintf (r, "---");
9565         else if ( aa>=0 && aa<=9)
9566           {
9567             sprintf (r, "%d%d%d", aa, aa,aa);
9568           }
9569         else if ( aa>='0' && aa<='9')
9570           {
9571              sprintf (r, "%c%c%c", aa, aa,aa);
9572           }
9573         else if ( aa=='a')
9574           {
9575             choice=(deterministic)?0:rand()%4;
9576             if      ( choice==0)sprintf (r, "gca");
9577             else if ( choice==1)sprintf (r, "gcg");
9578             else if ( choice==2)sprintf (r, "gcc");
9579             else if ( choice==3)sprintf (r, "gct");
9580           }
9581         else if ( aa=='c')
9582           {
9583            choice=(deterministic)?0:rand()%2;
9584             if      ( choice==0)sprintf (r, "tgc");
9585             else if ( choice==1)sprintf (r, "tgt");
9586           } 
9587         else if ( aa=='d')
9588           {
9589           choice=(deterministic)?0:rand()%2;
9590           if ( choice==0)sprintf (r, "gac");
9591           else if ( choice==1)sprintf (r, "gat");
9592           }
9593         
9594         else if ( aa=='e')
9595           {
9596             choice=(deterministic)?0:rand()%2;
9597             if ( choice==0)sprintf (r, "gaa");
9598             else sprintf (r, "gag");
9599           }
9600         else if ( aa=='f')
9601           {
9602             choice=(deterministic)?0:rand()%2;
9603             if ( choice==0)sprintf (r, "ttc");
9604             else sprintf (r, "ttt");
9605           }
9606         else if ( aa=='g')
9607           {
9608             choice=(deterministic)?0:rand()%4;
9609             if  ( choice==0)     sprintf (r, "gga");
9610             else if ( choice==1) sprintf (r, "ggg");
9611             else if ( choice==2) sprintf (r, "ggc");
9612             else if ( choice==3) sprintf (r, "ggt");
9613           }     
9614         else if ( aa=='h')
9615           {
9616             choice =rand()%2;
9617             if ( choice==0)sprintf (r, "cac");
9618             else sprintf (r, "cat");
9619           }
9620         else if ( aa=='i')
9621           {
9622             choice=(deterministic)?0:rand()%3;
9623             if  ( choice==0)     sprintf (r, "ata");
9624             else if ( choice==1) sprintf (r, "atc");
9625             else if ( choice==2) sprintf (r, "att");
9626           }     
9627         else if ( aa=='k')
9628           {
9629             choice=(deterministic)?0:rand()%2;
9630             if  ( choice==0)     sprintf (r, "aaa");
9631             else if ( choice==1) sprintf (r, "aag");
9632             
9633           }
9634         else if ( aa=='l')
9635           {
9636             choice=(deterministic)?0:rand()%6;
9637             if  ( choice==0)     sprintf (r, "cta");
9638             else if ( choice==1) sprintf (r, "ctg");
9639             else if ( choice==2) sprintf (r, "ctc");
9640             else if ( choice==3) sprintf (r, "ctt");
9641             else if ( choice==4) sprintf (r, "tta");
9642             else if ( choice==5) sprintf (r, "ttg");        
9643           }     
9644         else if ( aa=='m')sprintf ( r, "atg");
9645         else if ( aa=='n')
9646           {
9647             choice=(deterministic)?0:rand()%2;
9648             if  ( choice==0)     sprintf (r, "aac");
9649             else if ( choice==1) sprintf (r, "aat");
9650           }     
9651         else if ( aa=='p')
9652           {
9653             choice=(deterministic)?0:rand()%4;
9654             if  ( choice==0)     sprintf (r, "cca");
9655             else if ( choice==1) sprintf (r, "ccg");
9656             else if ( choice==2) sprintf (r, "ccc");
9657             else if ( choice==3) sprintf (r, "cct");
9658           }     
9659         else if ( aa=='q')
9660           {
9661             choice=(deterministic)?0:rand()%2;
9662             if  ( choice==0)     sprintf (r, "caa");
9663             else if ( choice==1) sprintf (r, "cag");
9664           }
9665         else if ( aa=='r')
9666           {
9667             choice=(deterministic)?0:rand()%6;
9668             if  ( choice==0)     sprintf (r, "cga");
9669             else if ( choice==1) sprintf (r, "cgg");
9670             else if ( choice==2) sprintf (r, "cgc");
9671             else if ( choice==3) sprintf (r, "cgt");
9672             else if ( choice==4) sprintf (r, "aga");
9673             else if ( choice==5) sprintf (r, "agg");
9674             
9675           }
9676         else if ( aa=='s')
9677           {
9678             choice=(deterministic)?0:rand()%6;
9679             if  ( choice==0)     sprintf (r, "tca");
9680             else if ( choice==1) sprintf (r, "tcg");
9681             else if ( choice==2) sprintf (r, "tcc");
9682             else if ( choice==3) sprintf (r, "tct");
9683             else if ( choice==4) sprintf (r, "agt");
9684             else if ( choice==5) sprintf (r, "agc");
9685             
9686           }
9687         else if ( aa=='t')
9688           {
9689             choice=(deterministic)?0:rand()%4;
9690             if  ( choice==0)     sprintf (r, "aca");
9691             else if ( choice==1) sprintf (r, "acg");
9692             else if ( choice==2) sprintf (r, "acc");
9693             else if ( choice==3) sprintf (r, "act");
9694           }
9695         else if ( aa=='v')
9696           {
9697             choice=(deterministic)?0:rand()%4;
9698             if  ( choice==0)     sprintf (r, "gta");
9699             else if ( choice==1) sprintf (r, "gtg");
9700             else if ( choice==2) sprintf (r, "gtc");
9701             else if ( choice==3) sprintf (r, "gtt");
9702           }
9703         else if ( aa=='w')
9704           {
9705             sprintf (r, "tgg");
9706           }
9707         else if ( aa=='y')
9708           {
9709              choice=(deterministic)?0:rand()%2;
9710             if  ( choice==0)     sprintf (r, "tac");
9711             else if ( choice==1) sprintf (r, "tat");
9712           }
9713         else
9714           {
9715             sprintf (r, "nnn");
9716           }
9717         return r;
9718                 
9719         }
9720 int translate_dna_codon ( char *sequence, char stop)
9721         {
9722         char seq[4];
9723         int b;
9724         int upper;
9725         int ret;
9726         
9727         if ( strlen (sequence)<1) return 'n';
9728         upper=isupper (sequence[0])?1:0;
9729         
9730         if ( (b=strlen (sequence))<3)
9731           ret='x';
9732         else
9733           {
9734             seq[0]=tolower(sequence[0]);
9735             seq[1]=tolower(sequence[1]); 
9736             seq[2]=tolower(sequence[2]);
9737             seq[3]='\0';
9738             
9739             seq[0]=(seq[0]=='u')?'t':seq[0];
9740             seq[1]=(seq[1]=='u')?'t':seq[1];
9741             seq[2]=(seq[2]=='u')?'t':seq[2];
9742             if ( strm5(seq, "gca", "gcg", "gcc", "gct","gcn"))ret='a';
9743             else if ( strm2(seq, "tgc","tgt"))ret='c';
9744             else if ( strm2(seq, "gac","gat"))ret='d';
9745             else if ( strm2(seq, "gaa","gag"))ret='e';
9746             else if ( strm2(seq, "ttc","ttt"))ret='f';
9747             else if ( strm5(seq, "gga","ggg","ggc", "ggt", "ggn"))ret='g';
9748             else if ( strm2(seq, "cac","cat"))ret='h';
9749             else if ( strm3(seq, "ata","atc","att"))ret='i';
9750             else if ( strm2(seq, "aaa","aag"))ret= 'k';
9751             else if ( strm6(seq, "cta","ctg","ctc", "ctt", "tta", "ttg"))ret='l';
9752             else if ( strm (seq, "ctn"))ret='l';
9753             else if ( strm (seq, "atg"))ret='m';
9754             else if ( strm2(seq, "aac","aat"))ret= 'n';
9755             else if ( strm5(seq, "cca","ccg","ccc", "cct","ccn"))ret='p';
9756             else if ( strm2(seq, "cag","caa"))ret='q';
9757             else if ( strm6(seq, "cga","cgg","cgc", "cgt","aga","agg"))ret='r';
9758             else if ( strm (seq, "cgn"))ret= 'r';
9759             else if ( strm6(seq, "tca","tcg","tcc", "tct","agc","agt"))ret='s';
9760             else if ( strm (seq, "ccn"))ret='s';
9761             else if ( strm5(seq, "aca","acg","acc", "act", "acn"))ret='t';
9762             else if ( strm5(seq, "gta","gtg","gtc", "gtt", "gtn"))ret='v';
9763             else if ( strm (seq, "tgg"))ret='w';
9764             else if ( strm2(seq, "tac","tat"))ret='y';
9765             else if ( strm3(seq, "tag","taa","tga"))ret=stop;
9766             else ret='x';
9767             
9768             ret= (upper)?toupper(ret):ret;
9769           }
9770         return ret;
9771         }
9772
9773 int extend_seqaln (Sequence *S, Alignment *A)
9774 {
9775   char **s;
9776   int n,a;
9777   if (S){s=S->seq;n=S->nseq;}
9778   else if (A){s=A->seq_al;n=A->nseq;}
9779   else return 0;
9780   
9781   for (a=0; a<n;a++){extend_seq(s[a]);}
9782   return 1;
9783 }
9784 int unextend_seqaln (Sequence *S, Alignment *A)
9785 {
9786   char **s;
9787   int n, a;
9788   if (S){s=S->seq;n=S->nseq;}
9789   else if (A){s=A->seq_al;n=A->nseq;}
9790   else return 0;
9791   
9792   for (a=0; a<n;a++){unextend_seq(s[a]);}
9793   return 1;
9794 }
9795     
9796
9797 char *extend_seq (char *seq)
9798 {
9799   char *buf, *ebuf;
9800   int l, lb, a, b, upper,v;
9801   char r1, r2;
9802   
9803   l=strlen (seq);
9804   buf =vcalloc ( l+1, sizeof (char));
9805   ebuf=vcalloc ( l+1, sizeof (char));
9806   sprintf (  buf, "%s", seq);
9807   sprintf ( ebuf, "%s", seq);
9808   
9809   ungap ( buf);
9810   ungap (ebuf);
9811   lb=strlen (buf);
9812   
9813   for (a=0; a<lb-1; a++)
9814     {
9815       r1=buf[a];
9816       r2=buf[a+1];
9817       
9818       upper=(isupper(r1))?1:0;
9819       r1=tolower(r1);
9820       r2=tolower(r2);
9821     
9822       r1=(r1=='u')?'t':r1;
9823       r2=(r2=='u')?'t':r2;
9824             
9825       if (r1=='x' || r1=='n')v='x';
9826       else if (r2=='n' || r2=='x')v=r1;
9827       
9828       else if (r1=='a' && r2=='a')v='d';
9829       else if (r1=='a' && r2=='c')v='e';
9830       else if (r1=='a' && r2=='g')v='f';
9831       else if (r1=='a' && r2=='t')v='h';
9832       
9833       else if (r1=='c' && r2=='a')v='i';
9834       else if (r1=='c' && r2=='c')v='k';
9835       else if (r1=='c' && r2=='g')v='l';
9836       else if (r1=='c' && r2=='t')v='m';
9837       
9838       else if (r1=='g' && r2=='a')v='n';
9839       else if (r1=='g' && r2=='c')v='p';
9840       else if (r1=='g' && r2=='g')v='q';
9841       else if (r1=='g' && r2=='t')v='r';
9842       
9843       else if (r1=='t' && r2=='a')v='s';
9844       else if (r1=='t' && r2=='c')v='v';
9845       else if (r1=='t' && r2=='g')v='w';
9846       else if (r1=='t' && r2=='t')v='y';
9847       else
9848         {
9849           v='j';
9850         }
9851       ebuf[a]=(upper)?toupper(v):v;
9852     }
9853   
9854   for (b=0,a=0; a<l; a++)
9855     {
9856       if ( !is_gap(seq[a]))seq[a]=ebuf[b++];
9857     }
9858   vfree (ebuf);
9859   vfree (buf);
9860   return seq;
9861 }
9862 char *unextend_seq (char *seq)
9863 {
9864   char *buf, *ebuf;
9865   int l, lb, a, b, upper,v;
9866   char r1, r2;
9867   
9868   l=strlen (seq);
9869   buf =vcalloc ( l+1, sizeof (char));
9870   ebuf=vcalloc ( l+1, sizeof (char));
9871   sprintf (  buf, "%s", seq);
9872   sprintf ( ebuf, "%s", seq);
9873   
9874   ungap ( buf);
9875   ungap (ebuf);
9876   lb=strlen (buf);
9877   
9878   for (a=0; a<lb-1; a++)
9879     {
9880       r1=buf[a];
9881       upper=(isupper(r1))?1:0;
9882       r1=tolower(r1);
9883       r1=(r1=='u')?'t':r1;
9884       
9885       if (r1=='x')v='n';
9886       else if (r1=='d' || r1=='e' || r1 == 'f' || r1 == 'h')v='a';
9887       else if (r1=='i' || r1=='k' || r1 == 'l' || r1 == 'm')v='c';
9888       else if (r1=='n' || r1=='p' || r1 == 'q' || r1 == 'r')v='g';
9889       else if (r1=='s' || r1=='v' || r1 == 'w' || r1 == 'y')v='t';
9890       else v='j';
9891             
9892       ebuf[a]=(upper)?toupper(v):v;
9893     }
9894   
9895   for (b=0,a=0; a<l; a++)
9896     {
9897       if ( !is_gap(seq[a]))seq[a]=ebuf[b++];
9898     }
9899   vfree (ebuf);
9900   vfree (buf);
9901   return seq;
9902 }
9903
9904   
9905
9906 Alignment * mutate_aln ( Alignment *A, char *r)
9907 {
9908   int a, b, c, mut,type, ratio;
9909   char alp[30];
9910   int alp_size;
9911   Sequence *S;
9912   Alignment*B;
9913   int n_mut, tot;
9914
9915   vsrand(0);
9916   if ( r[0]=='\0')ratio=0.01*RAND_MAX;
9917   else ratio=atof(r)*RAND_MAX;
9918
9919   S=aln2seq(A);
9920   S=get_sequence_type(S);
9921   
9922
9923
9924   if ( strm(S->type, "DNA") ||  strm(S->type, "RNA"))sprintf (alp, "AGCT");
9925   else if (  strm(S->type, "PROTEIN"))sprintf (alp, "ACDEFGHIKLMNPQRSTVWY");
9926
9927   alp_size=strlen(alp);
9928
9929   B=copy_aln (A,NULL);
9930   B=realloc_aln(B, B->len_aln*2+1);
9931
9932   for ( a=0, b=0; a< A->len_aln; a++, b+=2)
9933     {
9934       for ( c=0; c< A->nseq; c++)
9935         {
9936           B->seq_al[c][b]=tolower(A->seq_al[c][a]);
9937           B->seq_al[c][b+1]='~';
9938         }      
9939     }
9940
9941   for ( c=0; c< A->nseq; c++)B->seq_al[c][b]='\0';
9942   B->len_aln=A->len_aln*2;
9943   
9944
9945  
9946   tot=n_mut=0;
9947   for (a=0; a< B->len_aln; a+=2)
9948     for ( b=0; b<B->nseq; b++)
9949       {
9950         if ( is_gap(B->seq_al[b][a]))continue;
9951         mut=((rand()%RAND_MAX)>ratio)?0:1;
9952         tot++;
9953         n_mut+=mut;
9954
9955         if (mut)
9956           {
9957             type=rand()%2;
9958             if (type==0)/*deletion*/
9959               {
9960                 B->seq_al[b][a]='.';
9961               }
9962             else if ( type==1)
9963               {
9964                 B->seq_al[b][a+1]=alp[rand()%alp_size];
9965               }
9966             else if (type==2)
9967               {
9968                 B->seq_al[b][a]=alp[rand()%alp_size];
9969               }
9970             
9971           }
9972       }
9973   ungap_aln (B);
9974   
9975   
9976   free_sequence (S, S->nseq);
9977   free_aln (A);
9978   return B;
9979   
9980 }
9981
9982 char* mutate_amino_acid ( char aa, char *mode)
9983
9984      {
9985          int a, b, c, d;
9986          char nucleotide[]="agct";
9987          char amino_acid[]="acdefghiklmnpqrstvwy";
9988          static char **triplet;
9989          static char **cw_col;
9990          int ng_cw_col;
9991          static int **amino_acid_list;
9992          static int *lu;
9993          char a1, a2;
9994          char *mat;
9995          
9996          aa=tolower(aa);
9997          declare_name(mat);
9998          if ( !mode)sprintf (mat, "clustalw_col");
9999          else sprintf (mat, "%s", mode);
10000          if (!triplet)
10001             {
10002                 triplet=declare_char ( 64, 4);
10003                 for (d=0, a=0; a< 4;a++)
10004                     for ( b=0; b< 4; b++)
10005                         for ( c=0; c< 4; c++, d++)
10006                             {
10007                                 triplet[d][0]=nucleotide[a];
10008                                 triplet[d][1]=nucleotide[b];
10009                                 triplet[d][2]=nucleotide[c];
10010                             }
10011             }
10012          if ( !cw_col)cw_col=make_group_aa ( &ng_cw_col,mat);
10013          if ( !amino_acid_list)
10014             {
10015                 amino_acid_list=declare_int ( 20, 65);
10016                 for ( a=0; a< 20; a++)
10017                     for ( b=0; b< 64; b++)
10018                         {
10019                             a1=translate_dna_codon ( triplet[b], 'x');
10020                             a2=amino_acid[a];
10021                             for ( d=0; d< ng_cw_col; d++)
10022                                 if ( is_in_set ( a1, cw_col[d]) && is_in_set ( a2, cw_col[d]))
10023                                    {
10024                                        amino_acid_list[a][++amino_acid_list[a][0]]=b;
10025                                    }
10026                         }
10027                 lu=vcalloc ( 26, sizeof (int));
10028                 for ( a=0; a<20; a++)
10029                     {
10030                         lu[amino_acid[a]-'a']=a;
10031                     }
10032                 /*
10033                 for ( a=0; a< 20; a++)
10034                     {
10035                         fprintf ( stderr, "\n%c", amino_acid[a]);
10036                         for ( b=1; b<=amino_acid_list[a][0]; b++)
10037                             fprintf ( stderr, "\n\t%s %c", triplet[amino_acid_list[a][b]], translate_dna_codon (triplet[amino_acid_list[a][b]], 'x'));
10038                     }
10039                 */                  
10040             }
10041         
10042          return triplet [addrand((unsigned long)amino_acid_list[lu[aa-'a']][0])+1];
10043      }                  
10044                                  
10045 /**************************************************************************************************/
10046 /********************************                      ********************************************/
10047 /********************************    PROCESSING        ********************************************/
10048 /********************************                      ********************************************/
10049
10050
10051          
10052 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)
10053      {
10054        Sequence  *COOR=NULL, *NS=NULL,*BUFS=NULL, *OUT_S=NULL;
10055        Constraint_list *CL;
10056        char *s;
10057        int value,upper_value, lower_value, start, end, a, b,c;
10058        int *count_table=NULL;
10059        char *action;
10060        Sequence_data_struc *D1; 
10061        Sequence_data_struc *D2;
10062        Sequence_data_struc *DST;
10063        int s1, s2, r1, r2;
10064        static int clean_flag;
10065        Alignment *BUF;
10066        
10067        /*Switches*/
10068
10069        action=action_list[0];
10070
10071        if (action[0]=='2')
10072          {
10073            
10074            D1=D2in;
10075            D2=D1in;
10076            DST=DSTin;
10077            action++;
10078          }
10079        else if ( action[0]=='1')
10080          {
10081            D1=D1in;
10082            D2=D2in;
10083            DST=DSTin;
10084            action++;
10085          }
10086        else if ( action[0]=='3')
10087          {
10088            D1=DSTin;
10089            D2=D1in;
10090            DST=DSTin;
10091            action++;
10092          }
10093        else
10094          {
10095            D1=D1in;
10096            D2=D2in;
10097            DST=DSTin;
10098          }
10099        if (!D1->A)D1->A=copy_aln (D1in->A, NULL); 
10100          
10101        if (  strm(action, "seqnos"))
10102          {
10103           (D1->A)->output_res_num=1;
10104          } 
10105        else if ( strm (action,"aln2bootstrap"))
10106          {
10107            (D1->A)=aln2bootstrap (D1->A, ATOI_ACTION (1));
10108            D1->S=aln2seq (D1->A);
10109          }
10110        else if ( strm (action,"aln2sample"))
10111          {
10112            (D1->A)=aln2sample (D1->A, ATOI_ACTION (1));
10113            D1->S=aln2seq (D1->A);
10114          }
10115        else if ( strm (action,"aln2random_aln"))
10116          {
10117            (D1->A)=aln2random_aln (D1->A, ACTION (1));
10118            D1->S=aln2seq (D1->A);
10119          }
10120        else if ( strm (action, "or_scan"))
10121          {
10122            HERE ("OR SCAN");
10123            D1->A=or_scan(D1->A, D2->A, ACTION(1));
10124            D1->S=aln2seq (D1->A);
10125          }
10126        else if ( strm (action, "or_sar"))
10127          {
10128            D1->A=or_sar(D1->A, D2->A, ACTION(1), PRINT);
10129            D1->S=aln2seq (D1->A);
10130          }
10131        else if ( strm ( action, "sar2subsar"))
10132          {
10133            /*in->sequences
10134              in2->sar data
10135            */
10136            Alignment *subA, *subS;
10137            
10138            if ( n_actions==1)
10139              {
10140                fprintf ( stderr, "\nin=aln, in2=sar sar2subsar [filter value compound1 compound2...] | [jack1] | [file]\n");
10141                myexit (EXIT_FAILURE);
10142              }
10143            
10144            sarset2subsarset ( D1->A, D2->A, &subA, &subS, main_read_aln (action_list[2], NULL));
10145            D1->A=subA;D2->A=subS;
10146          }
10147        else if ( strm (action, "display_sar"))
10148          {
10149            D1->A=display_sar (D1->A, D2->A, action_list[1]);
10150          }
10151        else if ( strm ( action, "sar2simpred"))
10152          {
10153            /*in->sequences
10154              in2->sar data
10155            */
10156            sar2simpred ( D1->A, D2->A, action_list[1], action_list[2], atoi(action_list[3]), atoi (action_list[4]));
10157          }
10158        else if ( strm ( action, "sar2simpred2"))
10159          {
10160            /*in->sequences
10161              in2->sar data
10162            */
10163            if ( n_actions!=5)
10164              {
10165                fprintf ( stderr, "\nERROR: +sar2simpred2 seqnamesfile posfile compound limit");
10166                myexit (EXIT_FAILURE);
10167              }
10168            sar2simpred2 ( D1->A, D2->A, action_list[1], action_list[2], action_list[3], atoi (action_list[4]));
10169          }
10170         else if ( strm ( action, "sar_analyze"))
10171          {
10172            /*in->sequences
10173              in2->sar data
10174            */
10175            sar_analyze ( D1->A, D2->A,action_list[1]);
10176          }
10177         else if ( strm ( action, "simple_sar_predict"))
10178           {
10179             //displays each column with ist score;
10180             simple_sar_predict (D1->A, D2->A,ACTION(1));
10181             myexit (EXIT_SUCCESS);
10182           }
10183         else if ( strm ( action, "display_sar_analyze"))
10184           {
10185             //displays each column with ist score;
10186             display_simple_sar_analyze_col (D1->A, D2->A,ACTION(1));
10187             myexit (EXIT_SUCCESS);
10188           }
10189        else if ( strm ( action, "display_sar_analyze_pc"))
10190           {
10191             //displays each column with ist score;
10192             display_simple_sar_analyze_pair_col (D1->A, D2->A,ACTION(1));
10193             myexit (EXIT_SUCCESS);
10194           }
10195        else if ( strm ( action, "weight2sar"))
10196          {
10197            /*in->sequences
10198              in2->sar data
10199            */
10200            if ( n_actions!=3)
10201              {
10202                fprintf ( stderr, "\nERROR: +weight2sar <weight_file> <limit>");
10203                myexit (EXIT_FAILURE);
10204              }
10205            D1->A=weight2sar ( D1->A,D2->A, action_list[1], atoi(action_list[2]));
10206            
10207          }
10208         else if ( strm ( action, "sar_weight"))
10209          {
10210            /*in->sequences
10211              in2->sar data
10212            */
10213            if ( n_actions!=3)
10214              {
10215                fprintf ( stderr, "\nERROR: +sar_weight <sar_analyze> <compound>");
10216                myexit (EXIT_FAILURE);
10217              }
10218            D1->A=aln2weighted_sar_score ( D1->A,D2->A, action_list[1], action_list[2]);
10219            D1->S=aln2seq ( D1->A);
10220          }
10221        
10222        else if ( strm (action, "name2unique_name"))
10223          {
10224            char *tmp1, *tmp2;
10225            char command[1000];
10226            tmp1=vtmpnam (NULL); tmp2=vtmpnam (NULL);
10227            
10228            output_fasta_aln (tmp1,D1->A);
10229            free_aln (D1->A);free_sequence (D1->S, -1);
10230            sprintf ( command, "fasta_aln2fasta_aln_unique_name.pl %s >%s", tmp1, tmp2);
10231            my_system ( command);
10232            D1->S=get_fasta_sequence ( tmp2, NULL);
10233            D1->A=seq2aln (D1->S,NULL, 1);
10234          }
10235        else if ( strm (action, "rm_tag") || strm (action, "rm_template"))
10236          {
10237
10238            char **temp_name=NULL,**temp_list=NULL, temp_nseq=0;
10239            int z;
10240                         
10241            if ( D1 && D1->A){temp_name=(D1->A)->name;temp_nseq=(D1->A)->nseq;}
10242            else if ( D1 && D1->S){temp_name=(D1->S)->name;temp_nseq=(D1->S)->nseq;}
10243            temp_list=rm_name_tag (temp_name,temp_nseq, NULL);
10244            if ( n_actions>1 && strm (action_list[1], "template"))
10245               {
10246               
10247                for ( z=0; z<temp_nseq; z++)
10248                 {
10249                 if (temp_list[z][0])
10250                         {fprintf (stdout, "%s\n", temp_list[z]);}
10251                 }
10252                 myexit (EXIT_SUCCESS);
10253               } 
10254          }
10255        else if (strm (action, "add_template") || strm (action, "swap_header"))
10256          {
10257            D1->S=seq2template_seq (D1->S, action_list[1], NULL);
10258             D1->A=seq2aln(D1->S, NULL, 1);
10259          }
10260        else if ( strm ( action, "seq2year"))
10261          {
10262            D1->S=seq2year (D1->S, (n_actions>1)?atoi(action_list[1]):1);
10263            D1->A=seq2aln(D1->S, NULL, 1);
10264          }
10265        else if ( strm (action, "swap_lib_header"))
10266          {
10267            Sequence *S;
10268            S=main_read_seq (action_list[1]);
10269            (D1->CL)->S=S;
10270            
10271          }
10272        else if ( strm (action, "weight_lib"))
10273          {
10274            int l;
10275            int w;
10276            w=atoi (action_list[1]);
10277            if ( D1->CL)
10278              {
10279                int s1, s2,r1,r2;
10280                Sequence *S=(D1->CL)->S;
10281                int ***r=(D1->CL)->residue_index;
10282                
10283                for (s1=0; s1<S->nseq; s1++)
10284                  for (r1=1; r1<=S->len[s1]; r1++)
10285                    for (b=1; b<r[s1][r1][0]; b+=3)
10286                      {
10287                        r[s1][r1][b+2]=w;
10288                      }
10289              }
10290          }
10291        else if ( strm (action, "struc2nb"))
10292          {
10293            int c;
10294            for ( c=0; c< (D1->S)->nseq; c++)
10295              {
10296                struclist2nb ((D1->S)->name[c],(D1->S)->seq[c], (D1->S)->seq_comment[c], atof(action_list[1]),ACTION(2),ACTION(3) );
10297              }
10298            myexit (EXIT_SUCCESS);
10299          }
10300        
10301      
10302       
10303        else if ( strm(action, "seq2contacts"))
10304          {
10305            int z;
10306            D1->S=swap_header (D1->S, D2->S); 
10307            for ( z=0; z< (D1->S)->nseq; z++)sprintf ( (D1->A)->name[z], "%s", (D1->S)->name[z]);
10308            DST->S=seq2contacts (D1->S, atof (action_list[1]));
10309            DST->A=copy_aln (D1->A, NULL);
10310            thread_seq_struc2aln ( DST->A,DST->S);
10311            for (z=0; z< (D1->S)->nseq; z++)
10312            (DST->A)->S=D1->S;
10313            
10314          }
10315        else if ( strm(action, "struc2contacts"))
10316          {
10317            char *seq;
10318            if ( atof (action_list[3])>0)
10319              {
10320                seq=map_contacts  (action_list[1], action_list[2], atof (action_list[3]));
10321                fprintf ( stderr, "\n>%s %s\n%s",action_list[1], action_list[2],seq);
10322              }
10323            else 
10324              print_contacts  (action_list[1], action_list[2], atof (action_list[3]));
10325            
10326            myexit (EXIT_SUCCESS);
10327          }
10328        else if ( strm(action, "treelist_prune")|| strm(action, "prune_treelist"))
10329          {
10330            Sequence *TS;
10331            if (D2 && D2->S)TS=D2->S;
10332            else TS=treelist2sub_seq((D1->S),ATOI_ACTION(1));
10333            treelist2prune_treelist ( D1->S,TS, NULL);
10334            D1->A=seq2aln (D1->S, NULL, NO_PAD);
10335          }
10336        else if ( strm (action, "tree2unresolved_nodes"))
10337          {
10338            int ns;
10339            int *l;
10340            ns=tree2nseq (D1->T);
10341            l=vcalloc (ns, sizeof (int));
10342            tree2nnode_unresolved (D1->T, l);
10343            for ( a=0; a<ns; a++)if (l[a])fprintf ( stdout, "SIZE: %d COUNT: %d\n", a, l[a]); 
10344            vfree (l);
10345            myexit (EXIT_SUCCESS);
10346          }
10347        else if ( strm(action, "tree_prune") || strm(action, "prune_tree"))
10348          {
10349            D1->T=main_prune_tree ( D1->T, D2->S);
10350          }
10351        else if ( strm ( action, "tree2seq"))
10352          {
10353            D1->S=tree2seq(D1->T, NULL);
10354            D1->A=seq2aln (D1->S, D1->A, 1);
10355            (D1->A)->len_aln=1;
10356            for ( a=0; a< (D1->A)->nseq; a++)sprintf ( (D1->A)->seq_al[a], "sequence");
10357          }
10358        else if ( strm (action, "seq2dpatree"))
10359          {
10360            D1->T= seq2dpa_tree(D1->S,"ktup");
10361          }
10362        else if ( strm (action, "tree2dpatree"))
10363          {
10364            D1->T= tree2dpa_tree(D1->T,(D2 && D2->A)?D2->A:D1->A, (n_actions==1)?"idmat":action_list[1]);
10365          }
10366        else if ( strm (action, "tree2group"))
10367          {
10368            vfclose (tree2group (D1->T, (tree2seq(D1->T,NULL)), atoi(action_list[1]), atoi(action_list[2]),(n_actions==4)?action_list[3]:NULL, stdout));    
10369            myexit (EXIT_SUCCESS);
10370          }
10371        else if ( strm(action, "unroot"))
10372          {
10373            D1->T=unroot_tree(D1->T);
10374          }
10375      
10376        
10377        else if ( strm(action, "treelist2group")|| strm(action, "treelist2groups") )
10378          {
10379            Sequence *TS;
10380            
10381            if (D2 && D2->S)TS=D2->S;
10382            else TS=treelist2seq((D1->S));
10383            treelist2groups (D1->S, TS, ACTION(1), stdout);
10384            myexit (EXIT_SUCCESS);
10385            
10386            //      treelist2groups (D1->S,(D2)?D2->S:NULL, ACTION(1), stdout );
10387            //exit (EXIT_SUCCESS);
10388          }
10389        else if ( strm(action, "splits2tree"))
10390           {
10391           
10392            D1->T=split2tree ((D2)?D2->T:NULL,D1->S, ACTION(1));
10393            
10394          }
10395        else if ( strm(action, "count_splits"))
10396          {
10397           
10398            count_splits ((D2)?D2->T:NULL,D1->S, ACTION(1));
10399            myexit (EXIT_SUCCESS);
10400          }
10401         else if ( strm(action, "count_groups"))
10402          {
10403            count_tree_groups (D1->S, ACTION(1));
10404          }
10405        else if ( strm (action, "tree2dist"))
10406          {
10407            int ta, tb, ***td;
10408            Sequence *TS;
10409            
10410            TS=(D2)?D2->S:NULL;
10411            td=tree2dist (D1->T,TS, NULL);
10412            if (!TS)TS=tree2seq(D1->T, NULL);
10413            for (ta=0; ta<TS->nseq; ta++)
10414              {
10415                fprintf ( stdout, "%-15s ",TS->name[ta]);
10416                for ( tb=0; tb<TS->nseq; tb++)
10417                  {
10418                    int n=0;
10419                    if ( ACTION(1) && strm (ACTION(1), "length"))n=1;
10420                    
10421                    fprintf (stdout, " %4d", td [n][ta][tb]);
10422                  }
10423                fprintf ( stdout, "\n");
10424              }
10425            myexit (EXIT_SUCCESS);
10426          }
10427        else if ( strm (action, "treelist2lti"))
10428          {
10429            Sequence *TS;
10430            if (D2 && D2->S)TS=D2->S;
10431            else TS=treelist2sub_seq((D1->S),ATOI_ACTION(2));
10432            treelist2lti (D1->S,TS, (int)ATOI_ACTION(1), stdout );
10433            myexit (0);
10434          }       
10435        else if ( strm (action,"treelist2frame"))
10436          {
10437            Sequence *TS;
10438            if (D2 && D2->S)TS=D2->S;
10439            else TS=treelist2sub_seq((D1->S),ATOI_ACTION(1));
10440            treelist2frame (D1->S, TS);
10441            myexit (EXIT_SUCCESS);
10442          }
10443        
10444        else if ( strm (action, "treelist2seq"))
10445          {
10446            D1->S=treelist2sub_seq (D1->S,ATOI_ACTION(1));
10447            D1->A=seq2aln(D1->S, NULL, 1);
10448          }       
10449        else if ( strm (action, "treelist2leafgroup"))
10450          {
10451            treelist2leafgroup (D1->S, (D2)?D2->S:NULL, ACTION(1));
10452            myexit (0);
10453          }       
10454        else if ( strm(action, "treelist2splits"))
10455          {
10456            if (D1->T)D1->S=add_file2file_list ((D1->T)->file, NULL);
10457            treelist2splits (D1->S, (D2)?D2->S:NULL);
10458          }
10459       
10460        else if ( strm(action, "treelist2dmat"))
10461          {
10462            treelist2dmat (D1->S);
10463          }
10464        else if ( strm(action, "tree2node") )
10465          {
10466            print_node_list ( D1->T,(DST)?DST->S:NULL);
10467            myexit (EXIT_SUCCESS);
10468          }
10469        else if ( strm(action, "tree_cmp_list") )
10470          {
10471            D1->T=main_compare_trees_list ( D1->T, D2->S, stdout);         
10472          }
10473        else if ( strm(action, "tree_cmp") || strm (action, "tree_compare"))
10474          {
10475            D1->T=main_compare_trees ( D1->T, D2->T, stdout);       
10476          }
10477        else if ( strm (action, "tree_scan"))
10478          {
10479            D1->T=tree_scan (D1->A, D2->T, ACTION(1), ACTION(2));
10480          }
10481        else if ( strm (action, "split_cmp"))
10482          {
10483            main_compare_splits (D1->T, D2->T, ACTION(1), stdout);
10484          }
10485        
10486        else if ( strm(action, "node_sort"))
10487          {
10488            node_sort ( action_list[1], D1->T);
10489            myexit (EXIT_SUCCESS);
10490          }
10491        
10492        else if ( strm ( action, "avg_bootstrap"))
10493          {
10494            display_avg_bootstrap (D1->T);
10495            myexit (EXIT_SUCCESS);
10496          }
10497        else if ( strm (action, "genepred2acc"))
10498          {
10499            //D2->S=reference
10500            //D1->S=prediction
10501            vfree (display_accuracy (genepred_seq2accuracy_counts (D2->S, D1->S, NULL),stderr));
10502            myexit (EXIT_SUCCESS);
10503          }
10504        else if ( strm (action, "tree_cog_cmp"))
10505          {
10506            main_compare_cog_tree (D1->T,action_list[1]);
10507            myexit (EXIT_SUCCESS);
10508          }
10509        else if ( strm (action, "tree_aln_cmp"))
10510          {
10511            main_compare_aln_tree (D1->T, D2->A, stdout);
10512            myexit (EXIT_SUCCESS);
10513          }
10514        else if ( strm(action, "change_bootstrap"))
10515          {
10516            D1->T=reset_boot_tree ( D1->T, (n_actions>=2)?atoi(action_list[1]):0);
10517          }
10518        else if ( strm(action, "change_distances"))
10519          {
10520            D1->T=reset_dist_tree ( D1->T, (n_actions>=2)?atof(action_list[1]):0.00);
10521          }
10522        
10523        else if ( strm(action, "aln2tree"))
10524          {
10525            D1->T=tree_compute (D1->A, n_actions-1, action_list+1);
10526          }
10527        else if ( strm(action, "similarities2tree"))
10528          {
10529            D1->T=similarities_file2tree (ACTION(1));
10530          }
10531        
10532        else if (  strm(action, "original_seqnos"))
10533          {
10534           (D1->A)->output_res_num=2;
10535          }
10536        
10537        else if ( strm (action, "aln2pred"))
10538          {
10539            aln2pred (D1->A, D2->A, ACTION (1));
10540            myexit (EXIT_SUCCESS);
10541          }
10542        else if ( strm(action, "evaluate"))
10543          {
10544            Alignment *A;
10545
10546
10547            DST->A=copy_aln (D1->A, NULL);
10548            DST->S=aln2seq(DST->A);
10549            if (n_actions>1 && strm (  action_list[1], "categories"))
10550              {
10551                CL=declare_constraint_list ( DST->S,NULL, NULL, 0,NULL, read_matrice("pam250mt"));
10552                DST->A=  main_coffee_evaluate_output(DST->A, CL, "categories");
10553              }
10554            else if (n_actions>1 && strm (  action_list[1], "sar"))
10555              {
10556                CL=declare_constraint_list ( DST->S,NULL, NULL, 0,NULL, read_matrice("pam250mt"));
10557                DST->A=  main_coffee_evaluate_output(DST->A, CL, "sar");
10558              }
10559            else if (n_actions>1 && strstr (  action_list[1], "boxshade"))
10560              {
10561                char color_mode[1000];
10562                sprintf (color_mode,"boxshade_%d", atoi(ACTION2(2,"30")));
10563                CL=declare_constraint_list ( DST->S,NULL, NULL, 0,NULL, read_matrice("pam250mt"));
10564                DST->A=  main_coffee_evaluate_output(DST->A, CL, color_mode);
10565              }
10566            else
10567              {
10568                CL=declare_constraint_list ( DST->S,NULL, NULL, 0,NULL, read_matrice((n_actions==1)?"pam250mt":action_list[1]));
10569                DST->A=  main_coffee_evaluate_output(DST->A, CL, "matrix");
10570              }
10571            
10572            DST->S=aln2seq ( DST->A);
10573            
10574            A=D1->A;
10575            
10576            sprintf ( A->name[A->nseq], "cons");
10577            sprintf ( A->seq_al[A->nseq], "%s", aln2cons_seq_mat (A, "idmat"));
10578           
10579          }
10580        else if ( strm (action, "sp_evaluate"))
10581          {
10582            fprintf ( stdout, "SP Score: %.2f", sum_pair ((DST && DST->A)?DST->A:D1->A,ACTION(1),atoi(ACTION2(2,"0")),atoi(ACTION2(3,"0"))));
10583            myexit (EXIT_SUCCESS);
10584          }
10585        else if ( strm (action, "lat_evaluate"))
10586          {
10587            float score;
10588            score=lat_sum_pair ( D1->A, action_list[1]);
10589            fprintf ( stdout, "\nLAT_SCORE: %.2f", score);
10590            myexit (EXIT_SUCCESS);
10591            
10592          }
10593        else if ( strm (action, "add_scale"))
10594          {
10595            D1->A=aln2scale (D1->A, ACTION(1));
10596          }
10597        else if ( strm (action, "RNAfold_cmp"))
10598          {
10599            D1->A=compare_RNA_fold (D1->A, D2->A);
10600          }
10601        else if ( strm (action, "aln2alifold"))
10602          {
10603            D1->A=aln2alifold (D1->A);
10604            D1->S=aln2seq ( D1->A);
10605          }
10606                  
10607
10608        else if ( strm (action, "add_alifold"))
10609          {
10610            D1->A=add_alifold2aln (D1->A, (D2)?D2->A:NULL);
10611          
10612          }
10613        else if ( strm (action, "alifold2analyze"))
10614          {
10615            D1->A=alifold2analyze (D1->A, (D2)?D2->A:NULL, ACTION(1));
10616            D1->S=aln2seq(D1->A);
10617          }
10618        else if ( strm (action, "aln2conservation"))
10619          {
10620            D1->A=aln2conservation ( D1->A, ATOI_ACTION (1), ACTION (2));
10621            myexit (EXIT_FAILURE);
10622          }
10623        else if ( strm (action, "aln2cons"))
10624          {
10625            char *cons_seq;
10626            char *cons_name;
10627            cons_name=vcalloc (100, sizeof (char));
10628            sprintf(cons_name, "%s", (n_actions<=2)?"Cons":action_list[2]);
10629            cons_seq=aln2cons_seq_mat (D1->A, (n_actions==1)?"blosum62mt":action_list[1]);
10630            free_aln (D1->A);free_sequence(D1->S, -1);
10631            D1->S=fill_sequence_struc (1, &cons_seq, &cons_name);
10632            /*keep the gaps*/
10633            (D1->S)->len[0]=strlen (cons_seq); sprintf ( (D1->S)->seq[0], "%s", cons_seq);
10634            D1->A=seq2aln (D1->S, NULL, KEEP_GAP);
10635            vfree (cons_name);vfree (cons_seq);
10636          }
10637        else if ( strm (action, "seq2filter"))
10638          {
10639            D1->S=seq2filter ( D1->S, atoi(action_list[1]), atoi(action_list[2]));
10640                               
10641          }
10642        else if ( strm (action, "aln2resindex"))
10643          {
10644            //-in: aln, file: ref_seq ref_res target_seq
10645            //-in2 target sequences
10646            aln2resindex (D1->A, (D2)?D2->A:NULL, stdout);
10647            myexit (EXIT_SUCCESS);
10648          }
10649        else if (strm(action, "keep_name"))
10650          {
10651            RAD->keep_name=1-RAD->keep_name;
10652          }
10653         else if (strm(action, "use_consensus") ||strm(action, "use_cons") )
10654          {
10655            RAD->use_consensus=1-RAD->use_consensus;
10656          }
10657        else if ( strm(action, "ungap"))
10658          {
10659            seq2aln (D1->S, D1->A, 1);
10660          }
10661        else if ( strm2(action, "rmgap", "rm_gap"))
10662          {
10663
10664            ungap_aln_n (D1->A, (n_actions==1)?100:atoi(action_list[1]));
10665            //free_sequence ( D1->S, (D1->S)->nseq);
10666            D1->S=aln2seq ( D1->A);
10667            (D1->A)->S=D1->S;
10668          }
10669        else if ( strm(action, "rmgap_col"))
10670          {
10671            D1->A=remove_gap_column ( D1->A,action_list[1]);
10672          }
10673        else if ( strm(action,"random"))
10674          {
10675            
10676            D1->A= make_random_aln(NULL,(n_actions==1)?1:atoi(action_list[1]),(n_actions==2)?100:atoi(action_list[2]),"acdefghiklmnpqrstvwy");
10677            
10678            D1->S=aln2seq ( D1->A);
10679          }
10680             
10681        else if ( strm(action, "landscape"))
10682           {
10683             
10684             set_landscape_msa ((n_actions==1)?0:atoi(action_list[1]));
10685           }
10686        else if ( strm(action, "clean_maln"))
10687           {
10688             if ( !DST) 
10689                    {
10690                    fprintf ( stderr,"\n[You Need an evaluation File: Change the output format][FATAL:%s]\n", PROGRAM);  
10691                    myexit(EXIT_FAILURE);
10692                    }
10693             (DST->A)=aln2number (DST->A);
10694             D1->A=clean_maln(D1->A, DST->A,(n_actions==1)?1:atoi(action_list[1]),(n_actions==1)?1:atoi(action_list[2]));   
10695           }
10696        else if ( strm (action, "extract"))
10697          {
10698            
10699            COOR=get_pir_sequence  (RAD->coor_file, NULL);
10700            D1->S=extract_sub_seq ( COOR, D1->S);
10701            free_aln (D1->A);
10702            D1->A=declare_Alignment(D1->S);
10703            seq2aln (D1->S, D1->A, RAD->rm_gap);
10704            free_sequence (COOR, COOR->nseq);
10705          }
10706        else if ( strm (action, "reorder_column"))
10707          {
10708
10709
10710           
10711                Alignment *RO1, *RO2;
10712                Sequence *OUT_S;
10713                int s;
10714                
10715                RO1=rotate_aln (D1->A,NULL);
10716                if (ACTION(1) && strm (ACTION(1), "tree"))
10717                  {
10718                    D1->T=tree_compute (RO1,n_actions-2, action_list+2);
10719                     OUT_S=tree2seq(D1->T, NULL);
10720                     RO1=reorder_aln(RO1, OUT_S->name, OUT_S->nseq);
10721                   }
10722                else if ( ACTION(1) && strm (ACTION(1), "random"))
10723                  {
10724                    RO1=reorder_aln ( RO1, NULL, RO1->nseq);
10725                  }
10726                
10727                RO2=rotate_aln (RO1, NULL);
10728                for (s=0; s< RO2->nseq; s++)
10729                  sprintf ( RO2->name[s], "%s", (D1->A)->name[s]);
10730                free_aln (RO1);
10731                free_aln (D1->A);
10732                D1->A=RO2;
10733                D1->S=aln2seq(D1->A);
10734          }
10735                                
10736        else if ( strm (action, "reorder"))
10737          {
10738            
10739            if ( n_actions==2 && strm (action_list[1], "random"))
10740              {
10741                D1->A=reorder_aln ( D1->A, NULL, (D1->A)->nseq);
10742              }
10743            else if (n_actions==2 && strm (action_list[1], "invert"))
10744              {
10745                char **nname;
10746                int z, y;
10747               
10748                nname=declare_char ((D1->A)->nseq, 100);
10749                for ( z=0,y=(D1->A)->nseq-1; z<(D1->A)->nseq; z++, y--)
10750                  {
10751                    sprintf (nname[z], "%s",(D1->A)->name[y]);
10752                  }
10753
10754                D1->A=reorder_aln ( D1->A, nname, (D1->A)->nseq);
10755                free_char (nname, -1);
10756              }
10757            else if (n_actions==2 && strm (action_list[1], "scramble"))
10758              {
10759                D1->A=aln2scramble_seq(D1->A);
10760              }
10761
10762            else if ( n_actions==2 && strm (action_list[1], "tree"))
10763              {
10764
10765                OUT_S=tree2seq (D2->T, NULL);
10766                D1->A=reorder_aln(D1->A, OUT_S->name, OUT_S->nseq);
10767                free_sequence (D1->S,(D1->S)->nseq);
10768                D1->S=aln2seq (D1->A);
10769              }
10770            else
10771              {
10772                (D2->A)->S=aln2seq (D2->A);
10773                (D1->A)->S=aln2seq (D1->A);
10774                OUT_S=trim_aln_seq_name(D2->A, D1->A);
10775                D1->A=reorder_aln(D1->A, OUT_S->name, OUT_S->nseq);
10776                free_sequence (D1->S,(D1->S)->nseq);
10777                D1->S=aln2seq (D1->A);
10778              }
10779          }
10780        else if ( strm (action, "aln2replicate"))
10781          {
10782            aln2N_replicate (D1->A, ACTION(1), ACTION(2));
10783          }
10784        else if ( strm (action, "paralogous_cat"))
10785          {
10786            D1->A=orthologous_concatenate_aln (D1->A,D2->S, ACTION (1));
10787          }
10788        
10789        else if ( strm (action, "cat_aln"))
10790          {
10791            /*D1->A=aln_cat ( D1->A, D2 ->A);*/
10792
10793            if (D2 && D2->A && !ACTION(1))
10794              D1->A=concatenate_aln (D1->A, D2->A, ACTION(1));
10795            else if (ACTION(1) && is_aln(ACTION(1)))
10796              {
10797                  Alignment *B;
10798                  int n=1;
10799
10800                  while (ACTION(n))
10801                    {
10802
10803                      B=main_read_aln (ACTION(n), NULL);
10804                      D1->A=concatenate_aln (D1->A, B, NULL);
10805                      n++;
10806                    }
10807                  D1->S=aln2seq(D1->A);
10808              }
10809                      
10810            else
10811              {
10812                Alignment *A, *B;
10813                
10814                A=main_read_aln ((D1->A)->name[0], NULL);
10815                
10816                for ( a=1; a<(D1->A)->nseq; a++)
10817                  {
10818                    B=main_read_aln ((D1->A)->name[a], NULL);
10819                    A=concatenate_aln (A, B, ACTION(1));
10820                    
10821                  }
10822                D1->A=A;
10823                D1->S=aln2seq(D1->A);
10824              }
10825          }
10826            
10827        else if ( strm ( action, "msalist2cat_pwaln"))
10828          {
10829            int a, b, c;
10830            int sim, min, max;
10831            
10832            if (n_actions!=3)
10833              {
10834                min=0;
10835                max=100;
10836              }
10837            else
10838              {
10839                min=atoi(action_list[1]);
10840                max=atoi(action_list[2]);
10841              }
10842            
10843            fprintf ( stdout, ">A\n");
10844            for (a=0;a<(D1->S)->nseq; a++)
10845              {
10846                Alignment *A;
10847                HERE ("process %s",  (D1->S)->name[a]);
10848                A=main_read_aln((D1->S)->name[a],NULL);
10849                for (b=0; b<A->nseq-1; b++)
10850                  {
10851                    for ( c=b+1; c<A->nseq; c++)
10852                      {
10853                        sim=get_seq_sim (A->seq_al[b], A->seq_al[c], "-", "");
10854                        if (sim>=min && sim<=max)fprintf (stdout, "xxx%s", A->seq_al[b]);
10855                      }
10856                  }
10857                free_aln (A);
10858              }
10859            fprintf ( stdout, "\n>B\n");
10860            for (a=0;a<(D1->S)->nseq; a++)
10861              {
10862                Alignment *A;
10863                HERE ("process %s",  (D1->S)->name[a]);
10864                A=main_read_aln((D1->S)->name[a],NULL);
10865                for (b=0; b<A->nseq-1; b++)
10866                  {
10867                    for ( c=b+1; c<A->nseq; c++)
10868                      {
10869                        sim=get_seq_sim (A->seq_al[b], A->seq_al[c], "-", "");
10870                        if (sim>=min && sim<=max)fprintf (stdout, "xxx%s", A->seq_al[c]);
10871                      }
10872                  }
10873                free_aln (A);
10874              }
10875            
10876            fprintf ( stdout, "\n");
10877            myexit (EXIT_SUCCESS);
10878          }
10879        
10880        else if ( strm (action, "collapse_tree"))
10881          {
10882            D1->T=tree2collapsed_tree (D1->T, n_actions-1, action_list+1);
10883          }
10884        else if ( strm (action, "collapse_aln"))
10885          {
10886            D1->A=aln2collapsed_aln (D1->A, n_actions-1, action_list+1);
10887          }
10888        else if ( strm (action, "extract_aln"))
10889          {
10890            D1->A=aln2sub_aln_file (D1->A, n_actions-1, action_list+1);
10891            myexit (EXIT_SUCCESS);
10892          }
10893       
10894        
10895       
10896        else if ( strm (action, "remove_aa"))
10897          {
10898            int pos,len, n;
10899            pos=atoi(action_list[1]);
10900            len=atoi(action_list[2]);
10901            n=atoi (action_list[3]);
10902            if ( atoi (action_list[4])==1)len=-len; 
10903            if (pos && n>1) 
10904              {
10905                fprintf ( stderr, "\nWARNING: rm_aa, position (pos) and iteration number (n) simulatneously defined. Iteration number reset to 1 [%s]\n", PROGRAM);
10906                n=1;
10907              }
10908            for ( a=0; a< n; a++)
10909              D1->A=probabilistic_rm_aa (D1->A, pos, len);
10910          }
10911        else if ( strm (action, "remove_nuc"))
10912          {
10913            int pos;
10914            pos=atoi(action_list[1]);
10915            
10916            if ( pos>3 || pos<1)
10917              printf_exit (EXIT_FAILURE, stderr, "Remove_nuc: indicate a number between 1 and 3\n");
10918            
10919            pos--;
10920            for ( c=0,a=0; a<(D1->A)->len_aln; a++, c++)
10921              {
10922                if (c==3)c=0;
10923                for (b=0; b<(D1->A)->nseq; b++)
10924                  {
10925                  if (c==pos)
10926                    {
10927                      (D1->A)->seq_al[b][a]='-';
10928                    }
10929                  }
10930              }
10931            
10932            D1->S=aln2seq (D1->A);
10933          }
10934        
10935        else if (strm ( action, "conserved_positions"))
10936          {
10937            Alignment *A;
10938            int  a, b, c;
10939            int *cache=NULL;
10940            
10941
10942            A=D1->A;
10943            for ( a=0; a< A->nseq && !cache; a++)
10944              {
10945                if ( strm (action_list[1], A->name[a]))
10946                  {
10947                    cache=vcalloc ( A->len_aln+1, sizeof (int));
10948                    for ( c=0,b=0; b<A->len_aln; b++)
10949                      {
10950                        if ( is_gap (A->seq_al[a][b]))cache[b]=-1;
10951                        else cache[b]=++c;
10952                      }
10953                  }
10954              }
10955
10956            for ( a=0; a< A->len_aln; a++)
10957              {
10958                r1=A->seq_al[0][a];
10959                if ( is_gap(r1))continue;
10960                for ( c=0,b=0; b<A->nseq; b++)
10961                  {
10962                    r2=A->seq_al[b][a];
10963                    c+=(r1==r2)?1:0;
10964                  }
10965                if ( (c*100)/A->nseq>=atoi(action_list[2]))
10966                  fprintf ( stdout, "COL: %d Res: %c %s %d\n", a+1, r1, action_list[1], cache[a]+atoi(action_list[3]));
10967              }
10968            myexit (EXIT_FAILURE);
10969          }
10970        else if (strm ( action, "extract_block") )
10971          {
10972            
10973            BUF=copy_aln (D1->A, NULL);
10974            if ( check_file_exists(action_list[1]))
10975              BUF=extract_aln3(BUF,action_list[1]);
10976            else
10977              BUF=extract_aln2(BUF,atoi(action_list[2]),atoi(action_list[3]),action_list[1]);
10978            D1->A=copy_aln (BUF,D1->A);
10979           
10980          }
10981        else if ( strm ( action, "extract_pos_list"))
10982          {
10983            D1->A=alnpos_list2block (D1->A, n_actions-1, action_list+1);
10984          }
10985        else if ( strm ( action, "seq2msa"))
10986          {
10987            D1->A=simple_progressive_aln ( D1->S, NULL, NULL, action_list[1]);
10988          }
10989        else if ( strm ( action, "realign_block") )
10990          {
10991            D1->A=realign_block ( D1->A, atoi (action_list[1]), atoi (action_list[2]), (n_actions==4)?action_list[3]:NULL);
10992          }
10993        else if ( strm (action, "extract_seq"))   
10994          {
10995            int is_file;
10996            if ( check_file_exists (action_list[1])&& format_is_fasta (action_list[1]))
10997              {
10998                is_file=1;
10999                BUFS=main_read_seq (action_list[1]);
11000                action_list=BUFS->name;
11001                n_actions=BUFS->nseq;           
11002              }
11003            else
11004              {
11005                is_file=0;
11006                action_list++;
11007                n_actions--;
11008              }
11009            
11010            for ( a=0; a< n_actions;)
11011              {
11012                s=action_list[a];
11013                
11014                if ( n_actions==1 || is_file==1)
11015                  {
11016                    start=1;
11017                    end=0;
11018                    a+=1;
11019                  }
11020                else
11021                  {
11022
11023                    start=(strm2 (s,"#","*"))?1:(atoi(action_list[a+1]));
11024                    end=  (strm2 (action_list[a+2],"#","*"))?0:(atoi(action_list[a+2]));
11025                    a+=3;
11026                  }
11027                
11028                if ( strm2 (s, "#", "*"))
11029                  {
11030                    OUT_S=extract_one_seq((D1->A)->name[0],start, end, D1->A, RAD->keep_name);
11031                    for (b=1; b< (D1->A)->nseq; b++)
11032                      {
11033                       NS=extract_one_seq((D1->A)->name[b],start, end, D1->A, RAD->keep_name);
11034                       if (count_n_res_in_array(NS->seq[0], -1))
11035                         OUT_S=add_sequence ( NS,OUT_S, 0);
11036                      }
11037                  }
11038                else
11039                  {
11040                   if ( a==1)OUT_S=extract_one_seq(s,start, end, D1->A, RAD->keep_name);
11041                   else
11042                     {
11043                       NS=extract_one_seq(s,start, end, D1->A, RAD->keep_name); 
11044                       OUT_S=add_sequence ( NS,OUT_S, 0);
11045                     }
11046                  }
11047              }
11048            D1->S=OUT_S;
11049            free_aln (D1->A);
11050            D1->A=declare_Alignment(D1->S);
11051            seq2aln (D1->S, D1->A, RAD->rm_gap);
11052          }
11053        
11054        else if ( strm (action, "extract_seq_list"))      
11055          {
11056            if ( check_file_exists (action_list[1]) && format_is_fasta (action_list[1]))
11057              {
11058               
11059                BUFS=main_read_seq (action_list[1]);
11060                action_list=BUFS->name;
11061                n_actions=BUFS->nseq;           
11062              }
11063            else
11064              {
11065                action_list++;
11066                n_actions--;
11067              }
11068            
11069            for ( a=0; a< n_actions;a++)
11070              {
11071             
11072                NS=extract_one_seq(action_list[a],1,0, D1->A, KEEP_NAME); 
11073                OUT_S=add_sequence ( NS,OUT_S, 0);
11074             
11075              }
11076            
11077            D1->S=OUT_S;
11078            free_aln (D1->A);
11079            D1->A=declare_Alignment(D1->S);
11080            seq2aln (D1->S, D1->A, RAD->rm_gap);
11081          }
11082        else if ( strm (action, "remove_seq") || strm (action, "rm_seq"))         
11083          {
11084            char *buf;
11085            char **list;
11086            int n;
11087            int l;
11088            
11089            list=declare_char ((D1->S)->nseq, 200);
11090            
11091            buf=vcalloc ((D1->S)->max_len+1, sizeof (char));
11092            for ( n=0,a=0; a< (D1->A)->nseq; a++)
11093              {
11094                
11095                sprintf (buf, "%s", (D1->S)->seq[a]);
11096                ungap (buf);
11097                l=strlen(buf);
11098                
11099                for (c=1, b=1; b< n_actions; b++)
11100                  {
11101                    if ( strm (action_list[b], (D1->S)->name[a])){(D1->S)->seq[a]=NULL;break;}
11102                    else if ( strm (action_list[b], "empty") && l==0)
11103                      {
11104                        fprintf ( stderr, "WARNING: Sequence %s does not contain any residue: automatically removed from the set [WARNING:%s]\n",(D1->S)->name[a], PROGRAM);
11105                        (D1->S)->seq[a]=NULL;break;
11106                      }
11107                    else if ( strm (action_list[b], "unique"))
11108                      {
11109                        if ( name_is_in_list ((D1->S)->name[a], list,n, 100)!=-1)
11110                          {
11111                            (D1->S)->seq[a]=NULL;break;
11112                          }
11113                        else
11114                          {
11115                            sprintf ( list[n++], "%s", (D1->S)->name[a]);
11116                          }
11117                      }
11118                  }
11119              }
11120            D1->S=duplicate_sequence (D1->S);
11121            free_aln (D1->A);
11122            free_char ( list, -1);
11123            D1->A=declare_Alignment(D1->S);
11124            seq2aln (D1->S, D1->A, RAD->rm_gap);
11125          }
11126        
11127        else if (  strm (action, "aln2overaln")|| strm (action,"overaln_param"))
11128          {
11129            //mode (lower|number|uanlign) Penalty (0-100) Thresold (0-9)
11130            int  p1,p2,p3,f, t;
11131            char *s;
11132            int eb=0;
11133            char clean_mode[100];
11134            OveralnP *F;
11135
11136            F=vcalloc (1, sizeof (OveralnP));
11137            if ( D2 && D2->A)
11138              {
11139                D1->A=mark_exon_boundaries (D1->A, D2->A);
11140                eb=1;
11141              }
11142            else if ( (s=get_string_variable ("exon_boundaries")))
11143              {
11144               Sequence *S;
11145               Alignment *EB;
11146               EB=seq2aln(S=main_read_seq(s),NULL, 0);
11147               D1->A=mark_exon_boundaries (D1->A, EB);
11148               free_sequence (S, S->nseq); free_aln (EB);
11149               eb=1; 
11150              }
11151            
11152
11153            if (ACTION(1)==NULL)sprintf (F->mode, "lower");
11154            else if (strstr (ACTION(1), "h"))
11155              {
11156                fprintf ( stdout, "aln2unalign lower|number|unalign|uanlign2 F P1 P2 P3 T\n");
11157                myexit (EXIT_SUCCESS);
11158              }
11159            else sprintf (F->mode, "%s", ACTION(1));
11160            
11161            F->t=ATOI_ACTION(2);
11162            F->f=ATOI_ACTION(3);
11163            F->p1=ATOI_ACTION(4); 
11164            F->p2=ATOI_ACTION(5);
11165            F->p3=ATOI_ACTION(6);
11166            F->p3=ATOI_ACTION(7);
11167            
11168            if (int_variable_isset ("overaln_target"))f=get_int_variable ("overaln_target");
11169            if (int_variable_isset ("overaln_threshold"))t=get_int_variable ("overaln_threshold");
11170            if (eb)sprintf (F->model, "fsa2");
11171            else   sprintf (F->model, "fsa1");
11172            
11173            D1->A=aln2clean_pw_aln (D1->A, F);
11174            
11175          }
11176        else if (  strm (action, "unalign_groups"))
11177          {
11178            //unalign everything in lower case
11179            unalign_aln_2 (D1->A, NULL, 0);
11180          }
11181        else if (  strm (action,"aln2unalign"))
11182          {
11183            Alignment *SA;
11184            Sequence *SS;
11185            SA=copy_aln (D1->A, NULL);
11186            SS=aln2seq(SA);
11187            
11188            thread_seq_struc2aln (SA, SS);
11189            D1->A=unalign_aln (D1->A,SA, ATOI_ACTION(1));
11190            D1->S=aln2seq ( D1->A);
11191          }
11192        else if (  strm (action, "clean_cdna"))
11193          { 
11194            Alignment *A;
11195            A=D1->A;
11196            for (a=0; a< A->nseq; a++)
11197              {
11198                char *d, *buf, f;
11199                
11200                d=A->seq_al[a];
11201                f=get_longest_frame (d, 3);
11202                buf=vcalloc ( strlen (d)+1, sizeof (char));
11203                sprintf (buf, "%s", d+f);
11204                sprintf (d, "%s", buf);
11205                vfree (buf);
11206              }
11207          }
11208        else if ( strm (action, "clean_cdna2"))
11209          {
11210            D1->A=clean_cdna_aln ( D1->A);
11211            free_sequence ( D1->S, (D1->S)->nseq);
11212            D1->S=aln2seq ( D1->A);
11213          }
11214        else if ( strm  (action, "aln2short_aln"))
11215            {
11216              D1->A=aln2short_aln (D1->A, action_list[1], action_list[2], atoi(action_list[3]));
11217              free_sequence ( D1->S, (D1->S)->nseq);
11218              D1->S=aln2seq ( D1->A);
11219            }
11220        else if ( strm ( action, "complement"))
11221          {
11222            D1->A=complement_aln (D1->A);
11223            free_sequence ( D1->S, (D1->S)->nseq);
11224            D1->S=aln2seq ( D1->A);
11225          }
11226        else if ( strm ( action, "extend"))
11227          {
11228            extend_seqaln( NULL,D1->A);
11229            free_sequence ( D1->S, (D1->S)->nseq);
11230            D1->S=aln2seq ( D1->A);
11231          }
11232        else if ( strm ( action, "unextend"))
11233          {
11234            unextend_seqaln( NULL,D1->A);
11235            free_sequence ( D1->S, (D1->S)->nseq);
11236            D1->S=aln2seq ( D1->A);
11237          }
11238        else if ( strm ( action, "translate"))
11239          {
11240            D1->A=translate_dna_aln( D1->A,(n_actions==1)?0:atoi(action_list[1]));
11241            free_sequence ( D1->S, (D1->S)->nseq);
11242            D1->S=aln2seq ( D1->A);
11243          }
11244        else if (strm2 ( action, "back_translate","backtranslate"))
11245          {
11246           D1->A=back_translate_dna_aln( D1->A);
11247           free_sequence ( D1->S, (D1->S)->nseq);
11248           D1->S=aln2seq ( D1->A);
11249          }
11250        else if (strm ( action, "rotate"))
11251          {
11252            D1->A=rotate_aln( D1->A, action_list[1]);
11253            free_sequence ( D1->S, (D1->S)->nseq);
11254            D1->S=aln2seq ( D1->A);
11255          }
11256        else if (strm ( action, "invert"))
11257          {
11258           D1->A=invert_aln( D1->A);
11259           free_sequence ( D1->S, (D1->S)->nseq);
11260           D1->S=aln2seq ( D1->A);
11261          }
11262        else if (strm ( action, "test_dna2gene"))
11263          {
11264            testdna2gene ((D1->S)->seq[0]);
11265          }
11266        else if (strm ( action, "code_dna_aln"))
11267          {
11268           D1->A=code_dna_aln( D1->A);
11269           free_sequence ( D1->S, (D1->S)->nseq);
11270           D1->S=aln2seq ( D1->A);
11271          }
11272        
11273        else if ( strm ( action, "mutate"))
11274          {
11275            D1->A=mutate_aln( D1->A,(n_actions==1)?"0":action_list[1]);
11276            free_sequence ( D1->S, (D1->S)->nseq);
11277            D1->S=aln2seq (D1->A);
11278          }
11279        else if ( strm ( action, "thread_profile_on_msa"))
11280          {
11281            (D1->A)->S=NULL;
11282            D1->A=thread_profile_files2aln (D1->A, action_list[1], NULL);
11283            D1->S=aln2seq(D1->A);
11284          }
11285        else if ( strm ( action, "thread_dna_on_prot_aln"))
11286           {
11287             D1->A=thread_dnaseq_on_prot_aln (D1->S, D2->A);
11288             free_sequence (D1->S,(D1->S)->nseq);
11289             D1->S=aln2seq (D1->A); 
11290           }
11291        else if ( strm ( action, "thread_struc_on_aln"))
11292          {
11293            thread_seq_struc2aln ( D2->A, D1->S);
11294            D1->A=copy_aln(D2->A, D1->A);
11295            free_sequence ( D1->S, (D1->S)->nseq);
11296            D1->S=aln2seq (D1->A);
11297          }
11298        else if ( strm (action, "sim_filter"))
11299          {
11300            D1->A=sim_filter (D1->A, action_list[1], ACTION (2));
11301            free_sequence (D1->S,(D1->S)->nseq);
11302            D1->S=aln2seq (D1->A); 
11303          }
11304        else if ( strm (action, "seq2blast"))
11305          {
11306            D1->A=seq2blast (D1->S);
11307            free_sequence (D1->S,(D1->S)->nseq);
11308            D1->S=aln2seq (D1->A); 
11309          }
11310            
11311        else if ( strm (action, "trim"))
11312          {
11313            D1->A=simple_trimseq (D1->A,(D2)?D2->A:NULL, action_list[1], ACTION (2), NULL);
11314            
11315            free_sequence (D1->S,(D1->S)->nseq);
11316            D1->S=aln2seq (D1->A); 
11317          }
11318        
11319        else if (strm ( action, "trimTC"))
11320          {
11321            value=(n_actions==1)?10:atoi(action_list[1]);
11322         
11323            D1->A=tc_trimseq(D1->A,D1->S,action_list[1]);
11324            free_sequence (D1->S,(D1->S)->nseq);
11325            D1->S=aln2seq (D1->A); 
11326          }
11327        else if (strm ( action, "trimTC2"))
11328          {
11329            char *group_file;
11330            Alignment *B=NULL;
11331            char trim_mode[100];
11332            if ( n_actions==1 || !(strm (action_list[1], "NSEQ") ||strm (action_list[1], "MINID")) )
11333              {
11334                fprintf ( stderr, "\nTrimTC2 <NSEQ | MINID>  <number sequences| minimum identity> (<matrix>)\n");
11335                myexit (EXIT_FAILURE);
11336              }
11337            sprintf (trim_mode, "%s", action_list[1]);action_list+=2; n_actions-=2;
11338            
11339            if ( strm ( trim_mode, "NSEQ"))
11340              {
11341                group_file=tree2Ngroup( (D1)?D1->A:NULL, (D2)?D2->T:NULL, atoi (action_list[0]), vtmpnam(NULL), (n_actions==1)?"idmat":action_list[1]);
11342              }
11343            else
11344              {
11345                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]);
11346              }
11347            
11348            B=copy_aln (D1->A, B);
11349            B=aln2sub_aln_file (B,1,&group_file);
11350            B=aln2sub_seq (B, 1, &group_file);
11351            D1->A=extract_sub_aln2 (D1->A, B->nseq, B->name);
11352          }
11353        else if ( strm (action, "chain"))
11354          {
11355            D1->A=seq2seq_chain (D1->A,D2->A, ACTION(2));
11356          }
11357        
11358        
11359        else if (strm ( action, "master_trim"))
11360          {
11361            value=(n_actions==1)?10:atoi(action_list[1]);
11362         
11363            D1->A=master_trimseq(D1->A,D1->S,action_list[1]);
11364            free_sequence (D1->S,(D1->S)->nseq);
11365            D1->S=aln2seq (D1->A); 
11366          }
11367         else if ( strm (action, "force_aln"))    
11368          {
11369            char ***rlist=NULL;
11370            int count=0;
11371
11372            if ( n_actions==2)
11373              {
11374                if (!is_lib_02(action_list[1]))
11375                  {
11376                    fprintf ( stderr, "\nERROR: force_aln requires files in TC_LIB_FORMAT_02 [FATAL:%s]", PROGRAM);
11377                    myexit (EXIT_FAILURE);
11378                  }
11379                else
11380                    rlist=file2list (action_list[1], " ");
11381              }
11382            else
11383              {
11384                rlist=declare_arrayN(3, sizeof (char),3,7, 10);
11385                
11386                strcat (rlist[1][1],action_list[1]);strcat (rlist[1][3],action_list[2]);
11387                strcat (rlist[1][4],action_list[3]);strcat (rlist[1][6],action_list[4]);
11388                sprintf ( rlist[2][0], "-1");
11389              }
11390            count=1;
11391            while (rlist[count] && atoi(rlist[count][0])!=-1)
11392              {
11393                char st1[100], st2[100], st3[100], st4[100];
11394                
11395                sprintf ( st1, "%s", rlist[count][1]);sprintf ( st2, "%s", rlist[count][3]);
11396                sprintf ( st3, "%s", rlist[count][4]);sprintf ( st4, "%s", rlist[count][6]);
11397                fprintf ( stderr, "\nFORCE: %s %s %s %s", st1, st2, st3, st4);
11398                
11399                if (is_number (st1))s1=atoi (st1)-1;
11400                else s1=name_is_in_list (st1,(D1->A)->name, (D1->A)->nseq, 100);
11401                if ( s1<0 || s1>= (D1->A)->nseq)crash ("wrong sequence index");
11402                r1=atoi (st2)-1;
11403                
11404                if (is_number (st3))s2=atoi (st3)-1;
11405                else s2=name_is_in_list (st3,(D1->A)->name, (D1->A)->nseq, 100);
11406                if ( s2<0 || s2>= (D1->A)->nseq)crash ("wrong sequence index");
11407                r2=atoi (st4)-1;
11408                
11409                (D1->A)=add_constraint2aln ((D1->A), s1, r1, s2, r2);
11410                count++;
11411              }
11412            fprintf ( stderr, "\n");
11413            free_arrayN((void*)rlist,3);
11414          }
11415       
11416         else if (strm ( action, "grep"))
11417           {
11418             D1->A=grep_seq (D1->A, ACTION(1),ACTION(2), ACTION(3));
11419             if (D1->A==NULL) myexit (EXIT_SUCCESS);
11420             else D1->S=aln2seq (D1->A); 
11421           }
11422                       
11423         else if (strm (action, "find"))
11424           {
11425             int r, l;
11426             char *search_string;
11427
11428             search_string=vcalloc ( 30, sizeof (char));
11429             if ( strm (action_list[1], "lower"))sprintf ( search_string, "abcdefghijklmnopqrstuvwxyz");
11430             else if ( strm ( action_list[1], "upper"))sprintf ( search_string, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
11431             else 
11432               {
11433                 vfree (search_string);search_string=vcalloc ( strlen (action_list[1])+1, sizeof (char));
11434                 sprintf (search_string, "%s", action_list[1]);
11435               }
11436             
11437             for (a=0; a<(D1->A)->nseq; a++)
11438               for ( l=0,b=0; b< (D1->A)->len_aln; b++)
11439                 {
11440                   r=(D1->A)->seq_al[a][b];
11441                   l+=!is_gap(r);
11442                   if ( r!='\0' && strrchr (search_string, r))
11443                     {
11444                       /*fprintf ( stdout, "%-15s res %c alnpos %4d seqpos %4d\n", (D1->A)->name[a], r, b+1, l);*/
11445                       fprintf ( stdout, "%s %d %d\n", (D1->A)->name[a], l, l+1);
11446                     }
11447                 }
11448             myexit (EXIT_SUCCESS);
11449           }
11450         else if ( strm (action, "merge_annotation"))
11451           {
11452             D1->A=merge_annotation (D1->A, DST?DST->A:NULL, ACTION(1));
11453             D1->S=aln2seq (D1->A); 
11454           }
11455         else if ( strm  (action, "color_residue"))
11456           {
11457             int i;
11458             Alignment *A;
11459             A=D1->A;
11460             
11461             DST->A=copy_aln (D1->A, NULL);
11462             DST->S=aln2seq (DST->A);
11463             for (a=0; a< (DST->S)->nseq; a++)ungap ((DST->S)->seq[a]);
11464             
11465             if (n_actions>2)
11466               {
11467                 for (a=1; a<n_actions; a+=3)
11468                   {
11469                     i=name_is_in_list(action_list[a], (D1->A)->name, (D1->A)->nseq, 100);
11470                     if (i!=-1)
11471                       {
11472                         (DST->S)->seq[i][atoi(action_list[a+1])-1]='0'+atoi(action_list[a+2])-1;
11473                       }
11474                     else fprintf (stderr, "\nWARNING: Could not find Sequence %s", action_list[a]);
11475                   }
11476               }
11477             else
11478               {
11479                 char name[1000];
11480                 int pos, val;
11481                 FILE *fp;
11482                 
11483                 fp=vfopen (action_list[1], "r");
11484                 while (fscanf (fp, "%s %d %d\n", name, &pos, &val)==3)
11485                   {
11486                 
11487                      i=name_is_in_list(name, (D1->A)->name, (D1->A)->nseq, 100);
11488                      if (i!=-1)(DST->S)->seq[i][pos-1]='0'+val; 
11489                      else fprintf (stderr, "\nWARNING: Could not find Sequence %s", action_list[a]);
11490                   }
11491                 vfclose (fp);
11492               }
11493             DST->A=seq2aln (DST->S, NULL, 1);
11494           }
11495        else if ( strm  (action, "edit_residue"))
11496           {
11497             FILE *fp;
11498             int i, pos;
11499             int **p;
11500             char mod[100], name[100];
11501             Alignment *A;           
11502             
11503             A=D1->A;
11504             
11505             p=aln2inv_pos (A);
11506             if (n_actions>2)
11507               {
11508                 for (a=1; a<n_actions; a+=3)
11509                   {
11510                     
11511                     i=name_is_in_list(action_list[a], (D1->A)->name, (D1->A)->nseq, 100);
11512                     if (i!=-1)
11513                       {
11514                         pos=atoi(action_list[a+1]);
11515                         
11516                         pos=p[i][pos]-1;
11517                         sprintf (mod, "%s", action_list[a+2]);
11518                         if ( strm (mod, "upper"))(D1->A)->seq_al[i][pos]=toupper((D1->A)->seq_al[i][pos]);
11519                         else if ( strm (mod, "lower"))(D1->A)->seq_al[i][pos]=tolower((D1->A)->seq_al[i][pos]);
11520                         else (D1->A)->seq_al[i][pos]=mod[0];
11521                       }
11522                     else fprintf (stderr, "\nWARNING: Could not find Sequence %s", action_list[a]);
11523                     
11524                   }
11525               }
11526             else
11527               {
11528                 fp=vfopen (action_list[1], "r");
11529                 while (fscanf (fp, "%s %d %s\n", name, &pos, mod)==3)
11530                   {
11531                 
11532                      i=name_is_in_list(name, (D1->A)->name, (D1->A)->nseq, 100);
11533                      if (i!=-1)
11534                        {
11535                          pos=p[i][pos]-1;
11536                          if ( strm (mod, "upper"))(D1->A)->seq_al[i][pos]=toupper(A->seq_al[i][pos]);
11537                          else if ( strm (mod, "lower"))A->seq_al[i][pos]=tolower(A->seq_al[i][pos]);
11538                          else A->seq_al[i][pos]=mod[0];
11539                        }
11540                       else fprintf(stderr, "\nWARNING: Could not find Sequence %s", action_list[1]);
11541                   }
11542                 vfclose (fp);
11543               }
11544             D1->S=aln2seq (D1->A);
11545           }
11546        else if ( strm (action, "clean_flag"))
11547          {
11548            clean_flag=1-clean_flag;
11549          }
11550        else if ( strm  (action, "aln2case"))
11551          {
11552            D1->A=aln2case_aln (D1->A, ACTION(1), ACTION(2));
11553            D1->S=aln2seq(D1->A);
11554          }
11555        
11556        else if ( strm5 (action, "convert","upper","lower", "keep", "switchcase"))
11557          {
11558            b=1;
11559
11560            if ( n_actions>1 && is_number (action_list[b]))
11561              {
11562                lower_value=upper_value=atoi(action_list[b++]);
11563              }
11564            else if ( n_actions>1 && strm (action_list[b], "gap"))
11565              {
11566                DST=vcalloc (1,sizeof(Sequence_data_struc));
11567                DST->A=aln2gap_cache (D1->A,0);
11568                lower_value=0;
11569                upper_value=0;
11570                b++;
11571              }
11572            else if (n_actions>1 && action_list[b] && action_list[b][0]=='[')
11573              
11574              {
11575                lower_value=atoi(strtok (action_list[b]+1, "-[]"));
11576                upper_value=atoi(strtok (NULL, "-[]"));
11577              
11578                b++;
11579              }
11580            else
11581              {
11582                lower_value=upper_value=-1;
11583              }
11584
11585            if ( n_actions >b ||strm (action, "keep") )
11586              {
11587                if ( !RAD->symbol_list)RAD->symbol_list=declare_char (STRING, STRING);
11588                RAD->n_symbol=0;
11589                if ( strm (action, "keep") )sprintf ( RAD->symbol_list[RAD->n_symbol++], "#-");
11590                else
11591                  {
11592                    for (a=b; a< n_actions; a++)
11593                      {
11594                        sprintf ( RAD->symbol_list[RAD->n_symbol], "%s", action_list[a]);
11595                        RAD->n_symbol++;
11596                      }
11597                  }
11598              }
11599            
11600            for ( value=0; value<=9; value++)
11601              {
11602                if ( lower_value==-1)value=-1;
11603                
11604                if ( (value>=lower_value && value<=upper_value)|| value==-1)
11605                  {
11606                    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);
11607                    else if (strm(action,"upper"))D1->A=filter_aln_lower_upper (D1->A, DST?DST->A:NULL,RAD->use_consensus,value);
11608                    else if (strm(action,"lower"))D1->A=filter_aln_upper_lower (D1->A, DST?DST->A:NULL,RAD->use_consensus,value);
11609                    else if (strm(action,"switchcase"))D1->A=filter_aln_switchcase (D1->A, DST?DST->A:NULL,RAD->use_consensus,value);
11610                  }
11611                else
11612                  {
11613                    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);
11614                  }
11615                if (value==-1)break;
11616
11617              }
11618
11619            /*free_sequence (D1->S,(D1->S)->nseq);*/
11620            if (!D1->S)D1->S=aln2seq (D1->A); 
11621          } 
11622         else if ( strm ( action, "count_pairs"))
11623           {
11624             int a, b,c,v, **matrix;
11625             Alignment *A;
11626             matrix=declare_int (300,300);
11627             A=D1->A;
11628             for ( a=0; a< A->nseq-1; a++)
11629               for (b=0; b< A->nseq; b++)
11630                 for (c=0; c<A->len_aln; c++)
11631                   matrix[(int)A->seq_al[a][c]][(int)A->seq_al[b][c]]++;
11632             for ( a=0; a<255; a++)
11633               for ( b=a; b<256; b++)
11634                 {
11635                   v=matrix[a][b]+matrix[b][a];
11636                   if (v)fprintf ( stdout, "\n%c %c %d", a, b, v);
11637                 }
11638             myexit (EXIT_SUCCESS);
11639           }
11640         else if ( strm (action, "count_misc"))
11641           {
11642             count_misc (D1->A, (!D2)?NULL:D2->A);
11643           }
11644        else if ( strm (action, "count"))
11645          {
11646            b=1;
11647            if ( n_actions>1 && is_number (action_list[b]))
11648              {
11649                lower_value=upper_value=atoi(action_list[b++]);
11650              }
11651            else if (n_actions>1 && action_list[b] && action_list[b] && action_list[b][0]=='[')
11652              
11653              {
11654                lower_value=atoi(strtok (action_list[b]+1, "-[]"));
11655                upper_value=atoi(strtok (NULL, "-[]"));
11656              
11657                b++;
11658              }
11659            else
11660              {
11661                lower_value=upper_value=-1;
11662              }
11663            if ( n_actions >b)
11664              {
11665                if ( !RAD->symbol_list)RAD->symbol_list=declare_char (STRING, STRING);
11666                RAD->n_symbol=0;
11667                for (a=b; a< n_actions; a++)
11668                  {
11669                    sprintf ( RAD->symbol_list[RAD->n_symbol], "%s", action_list[a]);
11670                    RAD->n_symbol++;
11671                  }
11672              }
11673            for ( value=lower_value; value<=upper_value; value++)
11674              {
11675                count_table=count_in_aln (D1->A, DST?DST->A:NULL,value,RAD->n_symbol, RAD->symbol_list, count_table);           
11676              }
11677            for ( a=0; a<RAD->n_symbol; a++)
11678              {
11679                fprintf ( stdout, "%s %d\n", RAD->symbol_list[a], count_table[a]);
11680              }
11681            free_sequence (D1->S,(D1->S)->nseq);
11682            D1->S=aln2seq (D1->A); 
11683            vfree(count_table);
11684            myexit(EXIT_SUCCESS);
11685          }
11686        else if ( strm (action, "species_weight"))
11687          {
11688            seq_weight2species_weight (D1->A, D2->S);
11689            exit (0);
11690          }
11691        else if ( strm (action, "aln2voronoi"))
11692          {
11693            aln2voronoi_weights (D1->A);
11694            
11695          }
11696        else if ( strm (action, "msa_weight"))
11697          {
11698            int random_value;
11699            char command [LONG_STRING];
11700            char aln_name[FILENAMELEN];
11701            char tree_name[FILENAMELEN];
11702            char dist_matrix_name[FILENAMELEN];
11703            char weight_name[FILENAMELEN]; 
11704            char method_4_msa_weights[1000];
11705            
11706            if ( n_actions==1)
11707              {
11708                fprintf ( stderr, "\nError: msa_weight requires a weight_method");
11709              }
11710            
11711            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);
11712            
11713            /*1 Computation of the tree and the distance matrix*/
11714            random_value=addrand ((unsigned long) 100000)+1;
11715            sprintf (aln_name, "%d.aln", random_value);
11716            sprintf (tree_name, "%d.ph", random_value);
11717            sprintf (dist_matrix_name, "%d.dst", random_value);
11718            sprintf (weight_name, "%d.weight", random_value);
11719            output_fasta_aln (aln_name, D1->A);
11720           
11721            sprintf ( command, "clustalw -infile=%s -tree -outputtree=dist %s", aln_name, TO_NULL_DEVICE);
11722            my_system ( command);
11723            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); 
11724            my_system ( command);
11725            
11726            (D1->A)->S=aln2seq (D1->A);
11727            ((D1->A)->S)->W=read_seq_weight ( (D1->A)->name, (D1->A)->nseq,weight_name);    
11728            vremove (weight_name);
11729            vremove (aln_name);
11730            vremove (tree_name);
11731            vremove (dist_matrix_name);     
11732          }
11733        else if ( strm (action, "pavie_seq2random_seq"))
11734          {
11735            D1->S=pavie_seq2random_seq (D1->S, action_list[1]);
11736            D1->A=seq2aln (D1->S,NULL,1);
11737          }
11738        else if ( strm ( action, "pavie_seq2noisy_seq"))
11739          {
11740            /*<amount of noise: 0-100> (<alp>)*/
11741            
11742            D1->S=pavie_seq2noisy_seq (D1->S, atoi(action_list[1]),ACTION(2));
11743            D1->A=seq2aln (D1->S,NULL,1);
11744          }
11745        else if ( strm (action, "pavie_seq2pavie_mat"))
11746          {
11747
11748            pavie_seq2trained_pavie_mat ( D1->S, (n_actions==2)?action_list[1]:NULL);
11749            myexit (EXIT_SUCCESS);
11750          }
11751        else if ( strm (action, "pavie_seq2pavie_aln"))
11752          {
11753
11754            pavie_seq2pavie_aln ( D1->S, action_list[1], ACTION(2));
11755            myexit (EXIT_SUCCESS);
11756          }
11757        else if ( strm (action, "pavie_seq2pavie_dm"))
11758          {
11759             if (strstr (ACTION2(2,""), "_MSA_"))
11760               D1->S=aln2seq_main(D1->A, KEEP_GAP);
11761            
11762                    
11763            pavie_seq2pavie_aln ( D1->S, action_list[1],(n_actions==3)?action_list[2]:"_MATDIST_");
11764            myexit (EXIT_SUCCESS);
11765          }
11766        else if ( strm (action, "pavie_seq2pavie_msa"))
11767          {
11768            D1->A=pavie_seq2pavie_msa ( D1->S, action_list[1], (n_actions==3)?action_list[2]:NULL);
11769          }
11770        else if ( strm (action, "pavie_seq2pavie_tree"))
11771          {
11772            D1->T=pavie_seq2pavie_tree ( D1->S, action_list[1], (n_actions==3)?action_list[2]:NULL);
11773          }
11774        else if ( strm (action, "pavie_seq2pavie_sort"))
11775          {
11776            D1->A=pavie_seq2pavie_sort ( D1->S, action_list[1], (n_actions==3)?action_list[2]:NULL);
11777          }
11778       
11779        else if ( strm (action, "aln2mat_diaa"))
11780          {
11781            aln2mat_diaa (D1->S);
11782          }
11783        else if ( strm (action, "aln2mat"))
11784          {
11785            aln2mat(D1->S);
11786          }
11787        
11788        else if ( strm (action, "seq2latmat"))
11789          {
11790            seq2latmat ( D1->S, "stdout");
11791            myexit (EXIT_SUCCESS);
11792          }
11793        else if ( strm (action , "rm_target_pdb"))
11794          {
11795            int i, j;
11796            char *buf;
11797            
11798            for (i=0; i< (D1->A)->nseq; i++)
11799              {
11800                j=1;buf=(D1->A)->name[i];
11801                while (buf[j]!='_' && buf[j-1]!='_' && buf[j]!='\0')j++;
11802                buf[j]='\0';
11803              }
11804          }
11805        else if ( strm ( action, "mat2cmp"))
11806          {
11807            double *r;
11808            r=mat2cmp (D1->M, D2->M);
11809            fprintf ( stdout, "\nMATRIX COMPARISON: R=%.3f R2=%.3f On %d pairs of values\n", (float)r[0], (float)r[1], (int)r[2]);
11810            myexit (EXIT_SUCCESS);
11811          }
11812 //Special modes
11813        else if ( strm ( action, "overaln_list"))
11814          {
11815            float *re, tre=0,sn, tsn=0, sp, tsp=0;
11816            int p1,p2,p3, t, f;
11817            FILE *fp;
11818            char fname [100];
11819            Alignment **LA;
11820            Alignment **LB;
11821            
11822            HERE ("F P1 P2 P3 T");
11823
11824            t=ATOI_ACTION(1);
11825            f=ATOI_ACTION(2);
11826            p1=ATOI_ACTION(3);
11827            p2=ATOI_ACTION(4);
11828            p3=ATOI_ACTION(5);
11829            
11830            
11831            
11832            LA=vcalloc ((D1->A)->nseq, sizeof (Alignment*));
11833            LB=vcalloc ((D2->A)->nseq, sizeof (Alignment*));
11834            for (a=0; a<(D1->A)->nseq; a++)
11835              {
11836                 LA[a]=main_read_aln ((D1->A)->name[a], NULL);
11837                 LB[a]=main_read_aln ((D2->A)->name[a], NULL);
11838              }
11839         
11840            for ( a=0; a<(D1->A)->nseq; a++)
11841              {
11842                Alignment *A, *B;
11843                A=LA[a];
11844                B=LB[a];
11845                re=analyze_overaln (A, B, "_case_l_",t,f,p1,p2,p3);
11846                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);
11847                tsn+=re[0];
11848                tsp+=re[1];
11849                tre+=re[2];
11850                vfree(re);
11851              }
11852            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);
11853            
11854            myexit (0);
11855          }
11856        else if ( strm ( action, "overaln_list_scan"))
11857          {
11858            float *re, tre=0, tsn=0, tsp;
11859            int p1,p2, p3, t, f;
11860            FILE *fp;
11861            char fname [100];
11862            Alignment **LA;
11863            Alignment **LB;
11864            
11865            if ( ACTION(1))sprintf ( fname, "%s", ACTION(1));
11866            else sprintf ( fname, "scan_results.txt");
11867            
11868            fprintf ( stdout, "SCAN Results will be ouput in %s\n", fname);
11869            
11870            
11871            LA=vcalloc ((D1->A)->nseq, sizeof (Alignment*));
11872            LB=vcalloc ((D2->A)->nseq, sizeof (Alignment*));
11873            for (a=0; a<(D1->A)->nseq; a++)
11874              {
11875                 LA[a]=main_read_aln ((D1->A)->name[a], NULL);
11876                 LB[a]=main_read_aln ((D2->A)->name[a], NULL);
11877              }
11878            for (f=32; f<=40; f++)
11879              {
11880                for (p1=90; p1<=100; p1+=5)
11881                  {
11882                    for ( t=1; t<=3; t++)
11883                      {
11884                        for (p2=0; p2<=40; p2+=5)
11885                          {
11886                            for (p3=0;p3<=0;p3+=5)
11887                              {
11888                                tre=tsn=tsp=0;
11889                                for ( a=0; a<(D1->A)->nseq; a++)
11890                                  {
11891                                    Alignment *A, *B;
11892                                    A=LA[a];
11893                                    B=LB[a];
11894                                    re=analyze_overaln (A, B, "_case_l_",t,f,p1,p2,p3);
11895                                    
11896                                    tsn+=re[0];
11897                                    tsp+=re[1];
11898                                    tre+=re[2];
11899                                    vfree (re);
11900                                  }
11901                                fp=vfopen (fname, "a");
11902                                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);
11903                                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);
11904                                vfclose (fp);
11905                              }
11906                          }
11907                      }
11908                  }
11909              }
11910            myexit (0);
11911          }
11912        else if ( strm ( action, "overaln"))//Evaluate the capacity to predict over-aligned regions
11913          {
11914            OveralnP *F;
11915            F=vcalloc (1, sizeof (OveralnP));
11916            //al1: ref
11917            //al2: alignment
11918            //ATOI(1): P (0-100)
11919            //ATOI(2): T (0-9)
11920            
11921            float *r;
11922            DST=vcalloc (1,sizeof(Sequence_data_struc));
11923            DST->A=aln2gap_cache (D1->A,0);
11924            lower_value=0;
11925            upper_value=0;
11926            D1->A=filter_aln_upper_lower (D1->A, DST->A, 0, 0);
11927            
11928            sprintf (F->mode, "%s", ((s=get_string_variable ("overaln_mode")))?s:"lower");
11929            if (!strm (F->mode, "lower") && !strstr (F->mode, "unalign"))printf_exit (EXIT_FAILURE,stderr,"\nERROR: unknown overal_mode in overal output [%s] [FATAL:%s]", F->mode, PROGRAM);
11930            
11931            if (int_variable_isset ("overaln_threshold"))F->t=get_int_variable ("overaln_threshold");
11932            if (int_variable_isset ("overaln_target"))F->f=get_int_variable ("overaln_target");
11933            if (int_variable_isset ("overaln_P1"))F->f=get_int_variable ("overaln_P1");
11934            if (int_variable_isset ("overaln_P1"))F->f=get_int_variable ("overaln_P2");
11935            if (int_variable_isset ("overaln_P1"))F->f=get_int_variable ("overaln_P3");
11936            if (int_variable_isset ("overaln_P1"))F->f=get_int_variable ("overaln_P4");//F P1 P2 P3 T;
11937            
11938            D2->A=aln2clean_pw_aln (D2->A, F);
11939            r=aln2pred (D1->A, D2->A,"case_l_");
11940            fprintf ( stdout, "sn %.2f sp %.2f re %.2f\n", r[0], r[1], r[2]);
11941            myexit (0);
11942          }
11943        
11944            
11945 //JM_START
11946        else if ( strm ( action, "aln2hitMat"))
11947          {
11948                 aln2hitMat(D1->A, ACTION(1));
11949                 myexit (EXIT_SUCCESS);
11950          }
11951 //JM_END
11952        
11953        else
11954          {
11955            fprintf ( stderr, "\nWARNING: ACTION %s UNKNOWN and IGNORED\n", action);
11956          }
11957      
11958      }
11959
11960
11961 void aln2mat_diaa (Sequence *S)
11962 {
11963   int a, aa1, aa2, aa3, aa4;
11964   int s1, s2, p;
11965   Alignment *A;
11966   
11967   int ****m;
11968   int **c;
11969   int naa=0;
11970   int count=0;
11971   double Delta=0.00001;
11972   int *alp;
11973   int tot,u;
11974   double observed, expected, f_diaa1, f_diaa2, v;
11975   
11976
11977   alp=vcalloc (256, sizeof (int));
11978   for (a=0; a<26; a++)alp[a+'a']=1;
11979   alp['b']=0;
11980   alp['j']=0;
11981   alp['o']=0;
11982   alp['u']=0;
11983   alp['x']=0;
11984   alp['z']=0;
11985
11986   m=declare_arrayN (4,sizeof (int),26,26,26,26);
11987   c=declare_arrayN  (2,sizeof (int),26,26);
11988   
11989   for ( a=0; a< S->nseq; a++)
11990     {
11991       fprintf ( stderr, "%s\n", S->name[a]);
11992       A=main_read_aln (S->name[a],NULL);
11993       for (s1=0; s1<A->nseq; s1++)lower_string (A->seq_al[s1]);
11994       
11995       for ( s1=0; s1<A->nseq-1; s1++)
11996         for (s2=s1+1; s2<A->nseq; s2++)
11997           {
11998             for (p=0; p<A->len_aln-1; p++)
11999               {
12000                 
12001                 u =alp[aa1=A->seq_al[s1][p]];
12002                 u+=alp[aa2=A->seq_al[s1][p+1]];
12003                 u+=alp[aa3=A->seq_al[s2][p]];
12004                 u+=alp[aa4=A->seq_al[s2][p+1]];
12005         
12006                 if ( u==4)
12007                   {
12008                     aa1-='a';aa2-='a';aa3-='a'; aa4-='a';
12009                     
12010                     c[aa1][aa2]++;
12011                     c[aa3][aa4]++;
12012                     m[aa1][aa2][aa3][aa4]++;
12013                     count+=2;
12014                   }
12015               }
12016           }
12017       free_aln (A);
12018     }
12019   fprintf ( stdout, "# DIAA_MATRIX_FORMAT_01\n");
12020   naa=26;
12021   for (aa1=0; aa1<naa; aa1++)
12022     for (aa2=0; aa2<naa; aa2++)
12023       for (aa3=0; aa3<naa; aa3++)
12024         for (aa4=0; aa4<naa;aa4++) 
12025           {
12026             u =alp[aa1+'a'];
12027             u+=alp[aa2+'a'];
12028             u+=alp[aa3+'a'];
12029             u+=alp[aa4+'a'];
12030             
12031             if ( u==4)
12032               {
12033                 tot=m[aa1][aa2][aa3][aa4]+m[aa3][aa4][aa1][aa2];
12034                 observed=((double)tot)/(double)((double)count/(double)2);
12035                 f_diaa1=(double)c[aa1][aa2]/(double)count;
12036                 f_diaa2=(double)c[aa3][aa4]/(double)count;
12037                 
12038                 expected=f_diaa1*f_diaa2;
12039                 if (expected<Delta)v=0;
12040                 else if (observed<Delta)v=-100;
12041                 else 
12042                   {
12043                     v=log(observed/expected)*10;
12044                   }
12045             // 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);
12046                 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]);
12047               }
12048           }
12049   myexit (EXIT_SUCCESS);
12050 }
12051 void aln2mat (Sequence *S)
12052 {
12053   int a, aa1, aa3;
12054   int s1, s2, p;
12055   Alignment *A;
12056   int **mat;
12057   int **m;
12058   int *c;
12059   int naa=0;
12060   int count=0;
12061   double Delta=0.00001;
12062   int *alp;
12063   int tot,u;
12064   double observed, expected, f_diaa1, f_diaa2, v;
12065   char *balp;
12066
12067   balp=vcalloc ( 256, sizeof (char));
12068   for (a=0; a<strlen (BLAST_AA_ALPHABET); a++)balp[BLAST_AA_ALPHABET[a]]=a;
12069   
12070   mat=declare_int (256, 256);
12071   alp=vcalloc (256, sizeof (int));
12072   for (a=0; a<26; a++)alp[a+'a']=1;
12073   alp['b']=0;
12074   alp['j']=0;
12075   alp['o']=0;
12076   alp['u']=0;
12077   alp['x']=0;
12078   alp['z']=0;
12079
12080   m=declare_arrayN (2,sizeof (int),26,26);
12081   c=declare_arrayN  (1,sizeof (int),26);
12082   
12083   for ( a=0; a< S->nseq; a++)
12084     {
12085       fprintf ( stderr, "%s\n", S->name[a]);
12086       A=main_read_aln (S->name[a],NULL);
12087       for (s1=0; s1<A->nseq; s1++)lower_string (A->seq_al[s1]);
12088       
12089       for ( s1=0; s1<A->nseq-1; s1++)
12090         for (s2=s1+1; s2<A->nseq; s2++)
12091           {
12092             for (p=0; p<A->len_aln-1; p++)
12093               {
12094                 
12095                 u =alp[aa1=A->seq_al[s1][p]];
12096                 u+=alp[aa3=A->seq_al[s2][p]];
12097                 
12098                 if ( u==2)
12099                   {
12100                     aa1-='a';aa3-='a';
12101                     
12102                     c[aa1]++;
12103                     c[aa3]++;
12104                     m[aa1][aa3]++;
12105                     count+=2;
12106                   }
12107               }
12108           }
12109       free_aln (A);
12110     }
12111   fprintf ( stdout, "# MONOAA_MATRIX_FORMAT_01\n");
12112   naa=26;
12113   for (aa1=0; aa1<naa; aa1++)
12114       for (aa3=0; aa3<naa; aa3++)
12115           {
12116             u =alp[aa1+'a'];
12117             u+=alp[aa3+'a'];
12118             
12119             if ( u==2)
12120               {
12121                 tot=m[aa1][aa3]+m[aa3][aa1];
12122                 observed=((double)tot)/(double)((double)count/(double)2);
12123                 f_diaa1=(double)c[aa1]/(double)count;
12124                 f_diaa2=(double)c[aa3]/(double)count;
12125                 
12126                 expected=f_diaa1*f_diaa2;
12127                 if (expected<Delta)v=0;
12128                 else if (observed<Delta)v=-100;
12129                 else 
12130                   {
12131                     v=log(observed/expected)/(log(2)/2);
12132                   }
12133             // 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);
12134                 //fprintf ( stdout, "%c %c %d %d\n", aa1+'A', aa3+'A', (int)v, m[aa1][aa3]+ m[aa3][aa1]);
12135                 mat[aa1][aa3]=(int)v;
12136               }
12137           }
12138   output_blast_mat (mat, "stdout");
12139   myexit (EXIT_SUCCESS);
12140 }           
12141         
12142
12143 int **seq2latmat ( Sequence *S, char *fname)
12144 {
12145   int a, b, r0, r1;
12146   char *aa;
12147   int naa;
12148   int *count, tot;
12149   int **mat;
12150   double observed, expected;
12151   FILE *fp;
12152
12153   fp=vfopen (fname, "w");
12154   
12155   count=vcalloc ( 256, sizeof (int));
12156   mat=declare_int (256, 256);
12157   
12158   naa=strlen ( BLAST_AA_ALPHABET);
12159   aa=vcalloc ( naa+2, sizeof (char));
12160   sprintf ( aa, "%s", BLAST_AA_ALPHABET);
12161   lower_string (aa);
12162   
12163   for ( tot=0,a=0; a< S->nseq; a++)
12164     {
12165       ungap (S->seq[a]);
12166       for ( b=1; b<S->len[a]; b++)
12167         {
12168           r0=tolower(S->seq[a][b-1]);
12169           r1=tolower(S->seq[a][b]);
12170           
12171           mat[r0][r1]++;
12172           //count[r0]++;
12173           count[r1]++;
12174           tot++;
12175         }
12176     }
12177   for ( a=0; a< naa; a++)
12178     for (b=0; b< naa; b++)
12179       {
12180         if ( aa[a]=='*' || aa[b]=='*');
12181         else
12182           {
12183             expected=((double)count[(int)aa[a]]/(double)tot)* ((double)count[(int)aa[b]]/(double)tot)*(double)tot;
12184             observed=((double)mat[(int)aa[a]][(int)aa[b]]);
12185         
12186             /*
12187               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);  
12188               fprintf ( stderr, "\n%d", mat[aa[a]][aa[b]]);
12189               fprintf ( stderr, "\n%d", mat[aa[a]][aa[b]]);
12190             */
12191             mat[(int)aa[a]][(int)aa[b]]=(expected==0 || observed==0)?0:((int)10*log((observed/expected)));
12192           }
12193       }
12194  
12195   fprintf (fp,"# BLAST_MATRIX FORMAT\n#ALPHABET=%s\n#TRANSITION MATRIX TRAINED ON %d Sequence\n#", BLAST_AA_ALPHABET, S->nseq);
12196   for (a=0; a< naa; a++)fprintf ( fp, "%3c ", toupper(aa[a]));
12197   fprintf (fp,"\n");
12198   for (a=0; a< naa; a++)
12199     {
12200       
12201       fprintf (fp, "%c", toupper(aa[a]));
12202       for ( b=0; b< naa; b++)
12203         {
12204           fprintf (fp, "%3d ", mat[(int)aa[a]][(int)aa[b]]);
12205         }
12206       fprintf ( fp, "\n");
12207     }
12208   vfclose (fp);
12209   vfree (count);
12210   vfree (aa);
12211   
12212   return mat;
12213 }
12214  
12215 double* mat2cmp ( int **mat1, int **mat2)
12216 {
12217   int a, b, n, x, y;
12218   double **list, *r;
12219   if ( !mat1 || !mat2)
12220     {
12221       fprintf ( stderr, "\nERROR: mat2cmp needs two matrices [FATAL:%s]", PROGRAM);
12222       myexit (EXIT_FAILURE);
12223     }
12224   
12225   for (n=0, a=0; a< 256; a++)
12226     for ( b=0; b<256; b++)
12227       {
12228         x=mat1[a][b];
12229         y=mat2[a][b];
12230         if ( x|| y)n++;
12231       }
12232   if ( n==0) return 0;
12233   list=declare_double (n, 2);
12234   
12235   for (n=0, a=0; a<256; a++)
12236     for ( b=0; b<256; b++)
12237       {
12238         x=mat1[a][b];
12239         y=mat2[a][b];
12240         if ( x || y)
12241           {
12242             list[n][0]=x;
12243             list[n][1]=y;
12244             n++;
12245           }
12246       }
12247   r=return_r (list, n);
12248   free_double(list, -1);
12249   return r;
12250 }
12251    
12252 int ** read_blast_matrix ( char *mat_name)
12253         {
12254         FILE *fp;
12255         int n_aa,aa1, aa2;
12256         int a, b, c;
12257         int **matrix;
12258         int value;
12259         char sbuf[VERY_LONG_STRING];
12260         char buf[2];
12261         char alp[257];
12262
12263         matrix=declare_int (256,256);
12264         vfree ( matrix[30]);
12265         matrix[30]=vcalloc(10000, sizeof (int));
12266         fp=vfopen ( mat_name, "r");
12267         while ( (c=fgetc(fp))=='#' || isspace(c) )
12268           {
12269             char *p;
12270             fgets ( sbuf, VERY_LONG_STRING, fp);
12271             if ( (p=strstr (sbuf, "ALPHABET")))
12272               sscanf (p, "ALPHABET=%s", alp);
12273         }
12274         ungetc(c, fp);
12275         lower_string (alp);
12276         n_aa=strlen (alp);
12277
12278         for ( a=0; a< n_aa; a++)
12279             {
12280             fscanf ( fp, "%s ", buf);
12281
12282             aa1=tolower(buf[0]);
12283             
12284             if ( aa1!=alp[a])
12285                 {
12286                 fprintf ( stderr, "\nParsing_error when reading blast_matrix %s:\n%c %c",mat_name, aa1,alp[a]);
12287                 fprintf ( stderr, "\n%c ", fgetc(fp));
12288                 myexit (EXIT_FAILURE);
12289                 }
12290             for ( b=0; b<n_aa; b++)
12291                 {
12292                 aa2=tolower ((char) alp[b]);
12293                 fscanf ( fp, "%d ", &value);
12294                 if (is_gap(aa1) || is_gap(aa2))
12295                   {
12296                     int c1, c2;
12297                     c1=(is_gap(aa1))?GAP_CODE:aa1;
12298                     c2=(is_gap(aa2))?GAP_CODE:aa2;
12299                     if ( c1==GAP_CODE && c2==GAP_CODE)
12300                       matrix[c1][c2]=value;
12301                     else if ( c1==GAP_CODE)
12302                       {
12303                         matrix[c1][tolower(c2)]=value;
12304                         matrix[c1][toupper(c2)]=value;
12305                       }
12306                     else
12307                       {
12308                         matrix[tolower(c1)][c2]=value;
12309                         matrix[toupper(c1)][c2]=value;
12310                       }
12311                   }
12312                 else if ( aa1!='*' && aa2!='*')
12313                   {
12314                     matrix[tolower(aa1)-'A'][tolower(aa2)-'A']=value;
12315                     matrix[toupper(aa1)-'A'][toupper(aa2)-'A']=value;
12316                     matrix[tolower(aa1)-'A'][toupper(aa2)-'A']=value;
12317                     matrix[toupper(aa1)-'A'][tolower(aa2)-'A']=value;
12318                   }
12319                 }
12320             fscanf(fp, "\n");
12321             }
12322         fclose (fp);
12323
12324         return matrix;
12325         }
12326
12327 int output_blast_mat (int **mat, char *fname)
12328 {
12329   return output_mat(mat, fname, BLAST_AA_ALPHABET, 'a');
12330   
12331 }
12332 int output_header_mat (int **mat, char *fname, char *alp)
12333 {
12334   int a, b,l;
12335   FILE *fp;
12336   int naa;
12337
12338   char raa[]="ABCDEFGHIKLMNPQRSTVWXYZ";
12339   char *aa;
12340   
12341   naa=strlen (raa);
12342   aa=vcalloc ( naa+2, sizeof (char));
12343   sprintf ( aa, "%s",raa);
12344   lower_string (aa);
12345   
12346   fp=vfopen (fname, "w");
12347   fprintf ( fp, "int new_mat[]={\n");
12348   l=strlen (aa);
12349   for (a=0; a<naa; a++)
12350     {
12351       for (b=0; b<=a; b++)
12352         {
12353           fprintf (fp, "%3d, ", mat[aa[a]-'a'][aa[b]-'a']);
12354         }
12355       fprintf (fp, "\n");
12356     }
12357   fprintf ( fp, "}");
12358   vfclose (fp);
12359 }
12360 int output_mat (int **mat, char *fname, char *alp, int offset)
12361 {
12362   char *aa;
12363   int a,b, naa;
12364   FILE *fp;
12365
12366
12367   
12368   naa=strlen (alp);
12369   aa=vcalloc ( naa+2, sizeof (char));
12370   sprintf ( aa, "%s",alp);
12371   lower_string (aa);
12372   if (!(fp=vfopen (fname, "w")))return 0;
12373   fprintf (fp,"# BLAST_MATRIX FORMAT\n#ALPHABET=%s\n  ",alp);
12374   for (a=0; a< naa; a++)fprintf ( fp, "%5c ", toupper(aa[a]));
12375   fprintf (fp,"\n");
12376   for (a=0; a< naa; a++)
12377     {
12378       
12379       fprintf (fp, "%c", toupper(aa[a]));
12380       for ( b=0; b< naa; b++)
12381         {
12382           if (aa[a]!='*' && aa[b]!='*')
12383             fprintf (fp, " %5d", mat[aa[a]-offset][aa[b]-offset]);
12384           else 
12385             fprintf (fp, " %5d", 0);
12386         }
12387       fprintf ( fp, "\n");
12388     }
12389   vfree (aa);
12390   vfclose (fp);
12391   return 1;
12392 }               
12393
12394 void output_pavie_mat (int **mat, char *fname, double gep, char *alp)
12395 {
12396   int n, a, b;
12397   FILE *fp;
12398   
12399   n=strlen (alp);
12400   fp=vfopen (fname, "w");
12401   fprintf (fp,"# PAVIE_MATRIX FORMAT\n#ALPHABET=%s\n",alp);
12402   
12403   for(a=0; a< n; a++)
12404      {
12405        for ( b=a; b<n; b++)
12406         {         
12407           fprintf (fp, "%c %c %.3f\n", toupper(alp[a]), toupper(alp[b]), (float)mat[alp[a]-'A'][alp[b]-'A']/PAVIE_MAT_FACTOR);
12408         }
12409      }
12410    if ( gep!=UNDEFINED)fprintf ( fp, "- - %.3f\n", gep/PAVIE_MAT_FACTOR);
12411    vfclose(fp);
12412  }
12413
12414 int ** read_pavie_matrix ( char *mat_name)
12415         {
12416         FILE *fp;
12417         int c, n_aa;
12418         char aa1, aa2;
12419         float v;
12420         int **matrix;
12421         char sbuf[VERY_LONG_STRING];
12422         char alp[257];
12423         int gep=UNDEFINED;
12424         
12425         matrix=declare_int (256,256);
12426         
12427         
12428         fp=vfopen ( mat_name, "r");
12429         while ( (c=fgetc(fp))=='#' || isspace(c) )
12430           {
12431             fgets ( sbuf, VERY_LONG_STRING, fp);
12432             if ( sscanf (sbuf, "ALPHABET=%s", alp)==1);
12433           }
12434         ungetc(c, fp);
12435         
12436         n_aa=strlen (alp);
12437         while ( fgets ( sbuf, VERY_LONG_STRING, fp)!=NULL)
12438             {
12439               aa1=aa2='Z';
12440               if (sscanf (sbuf, "%c %c %f",&aa1, &aa2, &v)==3)
12441                 {
12442                   v*=PAVIE_MAT_FACTOR;
12443                   if (aa1=='-' && aa2=='-')gep=v;
12444                   else
12445                     {
12446                       matrix[tolower(aa1)-'A'][tolower(aa2)-'A']=v;
12447                       matrix[toupper(aa1)-'A'][toupper(aa2)-'A']=v;
12448                       matrix[tolower(aa1)-'A'][toupper(aa2)-'A']=v;
12449                       matrix[toupper(aa1)-'A'][tolower(aa2)-'A']=v;
12450                       
12451                       matrix[tolower(aa2)-'A'][tolower(aa1)-'A']=v;
12452                       matrix[toupper(aa2)-'A'][toupper(aa1)-'A']=v;
12453                       matrix[tolower(aa2)-'A'][toupper(aa1)-'A']=v;
12454                       matrix[toupper(aa2)-'A'][tolower(aa1)-'A']=v;
12455                     }             
12456               }
12457             }
12458         if ( gep!=UNDEFINED)
12459           {
12460             int a;
12461             for (a=0; a< n_aa; a++)
12462               {
12463                 if (!matrix[tolower(alp[a])-'A'][GAP_CODE])
12464                   {
12465                     matrix[tolower(alp[a])-'A'][GAP_CODE]=gep;
12466                     matrix[toupper(alp[a])-'A'][GAP_CODE]=gep;
12467                   }
12468               }
12469           }
12470         vfclose (fp);
12471         return matrix;
12472         }
12473
12474 Sequence *seq2year ( Sequence *S, int modulo)
12475 {
12476   int a, b, y;
12477   int first;
12478   char *s;
12479   char new_channel[100];
12480   
12481   sprintf( new_channel, "_agechannel%d",modulo);
12482   
12483   for ( a=0; a<S->nseq; a++)
12484     {
12485       if (S->seq_comment[a] && (s=strstr(S->seq_comment[a], "_FIRSTYEAR")))
12486         {
12487           sscanf (s, "_FIRSTYEAR%d_", &first);
12488         }      
12489       else first=1;
12490      
12491       for ( y=first,b=0; b<S->len[a]; b++)
12492         {
12493           if ( !is_gap(S->seq[a][b]))
12494             {
12495               S->seq[a][b]='a'+((y/modulo))%10;
12496               y++;
12497             }
12498         }
12499       if ( (s=strstr ( S->name[a], "_agechannel")))
12500            {
12501              sprintf ( s, "%s", new_channel);
12502            }
12503       else strcat (S->name[a], new_channel);
12504     }
12505   return S;
12506 }
12507
12508 Sequence* output_n_pavie_age_channel (Sequence *S, char *name, int n)
12509 {
12510   int x, a;
12511   if (!n)n=2;
12512   
12513
12514   for ( x=1,a=0; a< n; a++, x*=10)
12515     {
12516       S=output_pavie_age_channel(S, name,x);
12517     }
12518 return S;
12519 }
12520   
12521
12522
12523
12524 Sequence* output_pavie_age_channel (Sequence *S, char *name, int modulo)
12525   {
12526     Alignment *A;
12527     FILE *fp;
12528     static int display;
12529     char mat_list_name[100];
12530     char seq_list[1000];
12531     char mat_name[1000];
12532     char *tmp;
12533     
12534     sprintf ( mat_list_name, "%s_pavie_age_matrix.mat_list", name);
12535     sprintf (seq_list, "%s_age_channel.fasta",name);
12536     
12537     if ( display==0 )
12538       {
12539         if (check_file_exists(seq_list))vremove (seq_list);
12540         if (check_file_exists(mat_list_name))vremove (mat_list_name);
12541       }
12542     sprintf (mat_name, "%s_age_mat_mod%d.mat",name, modulo);
12543     output_age_matrix ( mat_name, modulo);
12544       
12545     fp=vfopen  ( mat_list_name,"a"); 
12546     fprintf ( fp, "%s\n", mat_name);
12547     vfclose ( fp);
12548     
12549     S=seq2year (S,modulo);
12550     A=seq2aln (S, NULL, KEEP_GAP);
12551     output_fasta_seq (tmp=vtmpnam (NULL),A);
12552     file_cat ( tmp, seq_list);
12553         
12554     if ( display==0)
12555       {
12556         display_output_filename ( stdout, "AGE_MAT_LIST", "MAT_LIST", mat_list_name, CHECK);
12557         display_output_filename ( stdout, "AGE_SEQ", "FASTA", seq_list, CHECK);
12558         display=1;
12559       }
12560     fprintf ( stderr, "\nModulo:%d years", modulo); 
12561     fprintf ( stderr, "\n");
12562     free_aln (A);
12563     return S;
12564   }
12565 //
12566 // Name MAnipulation
12567 //
12568
12569 Alignment *clean_aln (Alignment *A)
12570 {
12571   if ( A)
12572     {
12573       A->seq_comment=clean_string (A->nseq, A->seq_comment);
12574       A->aln_comment=clean_string (A->nseq, A->aln_comment);
12575       A->name=translate_names(A->nseq, A->name);
12576       (A->S)=clean_sequence ((A->S));
12577     }
12578   return A;
12579 }
12580 Sequence *clean_sequence ( Sequence *S)
12581 {
12582   if ( !S) return S;
12583   
12584   S->seq_comment=clean_string (S->nseq, S->seq_comment);
12585   S->name=translate_names(S->nseq, S->name);
12586   return S;
12587 }
12588 char ** translate_names (int n, char **name)
12589 {
12590   int a;
12591   for ( a=0; a<n; a++)
12592     name[a]=translate_name(name[a]);
12593   return name;
12594 }
12595 char * translate_name ( char *name)
12596         {
12597
12598         int len;
12599         int a;
12600         char buf[1000];
12601         
12602         len=strlen (name);
12603                 
12604         //if ( name[0]=='\'')return name;
12605         
12606         for ( a=0; a<len; a++)
12607                 {
12608                 if ( isspace(name[a]))name[a]='\0';
12609                 else if (strchr (";(),:#><'�", name[a]))name[a]='_';
12610                 
12611                 }
12612         sprintf (buf,"%s",decode_name (name, DECODE));
12613         if ( strlen (buf)>read_array_size_new ((char *)name))
12614           {
12615             name=vrealloc (name, sizeof (char)*(strlen (buf)+1));
12616           }
12617         sprintf (name, "%s", buf);
12618         
12619         return name;
12620         }
12621 char *decode_name (char *name, int mode)
12622 {
12623   static char ***name_list;
12624   static int n;
12625   static char tag[100];
12626   int a;
12627   
12628   if (mode==CLEAN)
12629     {
12630       for (a=0; a<n; a++)
12631         {
12632           vfree (name_list[a][0]);
12633           vfree (name_list[a][1]);
12634           vfree (name_list[a]);
12635         }
12636       vfree (name_list);
12637       tag[0]='\0';
12638     }
12639   
12640   //spacial modes
12641   if ( mode == CODELIST)
12642     {
12643       char *file;
12644       file=vtmpnam (NULL);
12645       for (a=0; a< n; a++)
12646         printf_file(file, "a", "#CODE: %s <=> %s\n", name_list[a][0], name_list[a][1]);
12647       return file;
12648     }
12649   if (mode ==DECODE && name_list==NULL)return name;
12650   if ( name==NULL) return name;
12651   
12652         
12653   
12654   if (!tag[0])
12655     {
12656       vsrand (0);
12657       sprintf ( tag, "TCTAG_%d",rand ()%100000);
12658     }
12659   
12660   if ( mode==CODE)
12661     {
12662       for (a=0; a< n; a++)
12663         if ( strm (name, name_list[a][0]))return name_list[a][1];
12664      
12665
12666       name_list=realloc (name_list, sizeof (char**)*(n+1));
12667       name_list[n]=vcalloc (2, sizeof (char*));
12668       name_list[n][0]=vcalloc (strlen (name)+1, sizeof (char));
12669       name_list[n][1]=vcalloc (100, sizeof (char));
12670       sprintf ( name_list[n][0], "%s", name);
12671       sprintf ( name_list[n][1], "%s_%d", tag,n+1);
12672       return name_list[n++][1];
12673     }
12674   else if ( mode ==DECODE)
12675     {
12676       char *p;
12677       int i;
12678       if ( !(p=after_strstr (name, tag)))return name;
12679       else 
12680         {
12681           sscanf (p, "_%d", &i);
12682           return name_list[i-1][0];
12683         }
12684     }
12685   else 
12686     {
12687       printf_exit (EXIT_FAILURE, stderr,"Unknown Mode for Decode_name [FATAL:%s]", PROGRAM);
12688     }
12689   return NULL;
12690 }
12691       
12692
12693 FILE * display_sequences_names (Sequence *S, FILE *fp, int check_pdb_status, int print_templates)
12694         {
12695             int a;
12696             int max_len;
12697             char *r;
12698             
12699             if ( !S)
12700                {
12701                    fprintf (fp,"\nERROR: NO SEQUENCE READ [FATAL:%s]\n", PROGRAM); myexit (EXIT_FAILURE);
12702                }
12703             for ( a=0, max_len=0; a< S->nseq; a++)max_len=MAX(max_len, strlen (S->name[a]));
12704             fprintf ( fp, "\nINPUT SEQUENCES: %d SEQUENCES  [%s]", S->nseq,(S->type)?S->type:"Unknown type");
12705             for ( a=0; a< S->nseq; a++)
12706                 {
12707                   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);
12708                     if (check_pdb_status)
12709                       {
12710                         if ((r=seq_is_pdb_struc (S, a)))fprintf (fp, " Struct Yes PDBID %s", get_pdb_id(r));
12711                         else fprintf (fp, " Struct No");
12712                         /*
12713                         if (is_pdb_struc (S->name[a])||is_pdb_struc (S->file[a]) )fprintf (fp, " Struct Yes");
12714                         else fprintf (fp, " Struct No");
12715                         */
12716                       }
12717                     else fprintf (fp, " Struct Unchecked");
12718                     if ( print_templates)fp=display_sequence_templates (S, a, fp);
12719                     
12720
12721                 }
12722             fprintf ( fp, "\n");
12723             return fp;
12724             
12725         }
12726 Sequence *add_file2file_list (char *name, Sequence *S)
12727 {
12728   
12729   if (!S) S=declare_sequence (1,1,10);
12730   else S=realloc_sequence   (S,S->nseq+1,0);S->nseq=0;
12731  
12732   sprintf ( S->name[S->nseq++], "%s", name);
12733   return S;
12734
12735 }
12736 #ifdef DO_PHECOMP
12737 int parse_phecomp_data (char *in, char *out)
12738 {
12739   static char *buffer;
12740   in=quick_find_token_in_file (in, "[EXPERIMENT HEADER]");
12741   while (fgets (buffer,fp,MAX_LINE_LENGTH));
12742 }
12743 FILE * quick_find_token_in_file  (FILE *fp, char *token)
12744 {
12745   //returns fp pointing to the begining of the line FOLLOWING the line containing token 
12746   static char *buffer;
12747   if (!line) line=vcalloc (MAX_LINE_LENGTH+1, sizeof (char)); 
12748   while (fgets (buffer,MAX_LINE_LENGTH, fp)!=NULL)
12749     if (strstr (buffer,token))return fp;
12750   vfclose (fp);
12751   return NULL;
12752 }
12753
12754 int * file2cage (char *file, int cage)
12755 {
12756   
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766 #endif
12767 /******************************COPYRIGHT NOTICE*******************************/
12768 /*© Centro de Regulacio Genomica */
12769 /*and */
12770 /*Cedric Notredame */
12771 /*Fri Feb 18 08:27:45 CET 2011 - Revision 596. */
12772 /*All rights reserved.*/
12773 /*This file is part of T-COFFEE.*/
12774 /**/
12775 /*    T-COFFEE is free software; you can redistribute it and/or modify*/
12776 /*    it under the terms of the GNU General Public License as published by*/
12777 /*    the Free Software Foundation; either version 2 of the License, or*/
12778 /*    (at your option) any later version.*/
12779 /**/
12780 /*    T-COFFEE is distributed in the hope that it will be useful,*/
12781 /*    but WITHOUT ANY WARRANTY; without even the implied warranty of*/
12782 /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the*/
12783 /*    GNU General Public License for more details.*/
12784 /**/
12785 /*    You should have received a copy of the GNU General Public License*/
12786 /*    along with Foobar; if not, write to the Free Software*/
12787 /*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA*/
12788 /*...............................................                                                                                      |*/
12789 /*  If you need some more information*/
12790 /*  cedric.notredame@europe.com*/
12791 /*...............................................                                                                                                                                     |*/
12792 /**/
12793 /**/
12794 /*      */
12795 /******************************COPYRIGHT NOTICE*******************************/