0d24c42a4a3e5aac2488c5f532137d3ffbfa9103
[jabaws.git] / binaries / src / tcoffee / t_coffee_source / util_constraints_list.c
1 #include <stdlib.h>
2
3 #include <stdio.h>
4 #include <math.h>
5 #include <stdarg.h>
6 #include <string.h>
7 #include <ctype.h>
8 #include <sched.h>
9
10
11 #include "io_lib_header.h"
12 #include "util_lib_header.h"
13 #include "define_header.h"
14 #include "dp_lib_header.h"
15 static int entry_len;
16 int compare_constraint_list_entry ( const void*vx, const void*vy);
17 int compare_constraint_list_entry4bsearch ( const void*vx, const void*vy);
18 /*********************************************************************/
19 /*                                                                   */
20 /*                         PRODUCE IN LIST                           */
21 /*                                                                   */
22 /*                                                                   */
23 /*********************************************************************/
24 Constraint_list * make_test_lib(Constraint_list *CL);
25 Constraint_list *fork_line_produce_list   ( Constraint_list *CL, Sequence *S, char * method,char *weight,char *mem_mode,Job_TC *job, FILE *loca_stderr);        
26 Constraint_list *fork_cell_produce_list   ( Constraint_list *CL, Sequence *S, char * method,char *weight,char *mem_mode,Job_TC *job, FILE *local_stderr);
27 Constraint_list *nfork_produce_list   ( Constraint_list *CL, Sequence *S, char * method,char *weight,char *mem_mode,Job_TC *job, FILE *local_stderr);
28 Constraint_list *fork_subset_produce_list   ( Constraint_list *CL, Sequence *S, char * method,char *weight,char *mem_mode, Job_TC *job, FILE *local_stderr);
29 int job2first_seq(Job_TC *job);
30 Constraint_list *produce_list   ( Constraint_list *CL, Sequence *S, char * method,char *weight,char *mem_mode)  
31 {
32   Job_TC *job=NULL;
33   FILE *local_stderr;
34   int njob;
35   
36   store_seq_type (S->type);
37   if ( CL==NULL)CL=declare_constraint_list ( S,NULL, NULL, 0,(strm(mem_mode, "disk"))?vtmpfile():NULL, NULL);
38   local_stderr=(CL->local_stderr!=NULL)?CL->local_stderr:stderr;
39   
40   CL->local_stderr=vfopen("/dev/null", "w");
41   job=queue2heap(method2job_list ( method,S,weight, CL->lib_list,CL->DM, CL));
42   njob=queue2n(job)+1;
43   
44   if ( get_nproc()==1 || njob==1)return  nfork_produce_list (CL, S, method, weight, mem_mode,job, local_stderr);
45   else if (strstr ( CL->multi_thread, "jobcells"))return fork_cell_produce_list (CL, S, method, weight, mem_mode,job,local_stderr);
46   else if (strstr ( CL->multi_thread, "joblines"))return fork_line_produce_list (CL, S, method, weight, mem_mode,job, local_stderr);
47   else if (strstr ( CL->multi_thread, "jobs"))return fork_subset_produce_list (CL, S, method, weight, mem_mode,job, local_stderr); //Recommended default
48   else return nfork_produce_list (CL, S, method, weight, mem_mode,job, local_stderr);
49 }
50 int job2first_seq(Job_TC *job)
51 {
52   int *seqlist;
53   int r;
54   
55   if (!job) return -1;
56   else if ( !job->param)return -1;
57   else if ( !(job->param)->seq_c) return -1;
58   seqlist=string2num_list ((job->param)->seq_c);
59   if (seqlist[0]<2)r=-1;
60   else r=seqlist[2];
61   vfree (seqlist);
62   return r;
63 }
64
65 Constraint_list *fork_subset_produce_list   ( Constraint_list *CL, Sequence *S, char * method,char *weight,char *mem_mode, Job_TC *job, FILE *local_stderr)
66 {
67   //forks lines of the matrix
68   int a,b;
69   Job_TC *heap,*end,*start, ***jl;
70   TC_method *M;
71   int n_elements_in, n_new_elements;
72   char **pid_tmpfile;
73   int   *pid_list;
74   int pid,npid, njob;
75   int nproc, max_nproc, submited;
76   int cseq, seq, nlines;
77   int n_aln;
78   
79   max_nproc=nproc=get_nproc();
80   max_nproc*=2;
81   /*OUT_MODE:
82     A->  alignment provided in a file
83     L->  list      provided in a file
84     aln-> alignment computed and provided in a file
85     list->list      computed and provided in a file
86   */
87     
88     
89   
90   
91   if ( job->jobid==-1)
92     {
93       M=(job->param)->TCM;
94       fprintf (local_stderr, "\n\tMethod %s: No Suitable Sequences [Type: %s]\n", method,M->seq_type); 
95       return CL;
96     }
97   
98   job=queue2heap (job_list2multi_thread_job_list (job, CL->multi_thread, CL));
99   heap=job;
100   njob=queue2n (job);
101   n_elements_in=CL->ne;
102   M=(job->param)->TCM;
103   if (M)M->PW_CL=method2pw_cl ( M, CL);
104   pid_tmpfile=vcalloc (MAX(njob,get_nproc())+1, sizeof (char*));
105   pid_list   =vcalloc (MAX_N_PID, sizeof (int *));
106   
107   fprintf ( local_stderr, "\n\tMulti Core Mode: %d processors [subset]\n", get_nproc());
108   
109   jl=split_job_list(job,get_nproc());
110   a=npid=0;
111   
112   a=npid=0;
113   while (jl[a])
114     {
115  
116       start=job=jl[a][0];
117       end=jl[a][1];
118       pid_tmpfile[a]=vtmpnam(NULL);
119       pid=vfork();
120     
121       if (pid==0)//child process
122         {
123           FILE *fp;
124           int done, todo, t;
125           
126           initiate_vtmpnam(NULL);
127           fp=vfopen (pid_tmpfile[a],"a");
128           todo=0;
129           while (job!=end){todo++;job=job->c;}
130           job=start;
131           
132           done=0;
133           while (job!=end)
134             {
135               if (a==0)output_completion ( local_stderr,done,todo,1, "Submit   Job");
136               job=print_lib_job (job, "io->CL=%p control->submitF=%p control->retrieveF=%p control->mode=%s",duplicate_constraint_list4lib_computation (CL),submit_lib_job, retrieve_lib_job, CL->multi_thread );       
137               job=submit_job (job);
138               retrieve_job (job);
139               t=((job->io)->CL)->ne*((job->io)->CL)->entry_len;
140               for (b=0; b<t; b++)fprintf ( fp, "%d ", ((job->io)->CL)->L[b]);
141               job=job->c;
142               done++;
143             }
144           vfclose (fp);
145           exit (EXIT_SUCCESS);
146         }
147       else
148         {
149           pid_list[pid]=npid;
150           set_pid(pid);
151           npid++;
152           a++;
153         }
154     }
155  
156   //wait for all processes to finish
157   for (a=0; a<npid; a++)
158     {
159       pid=vwait(NULL);
160       CL=read_constraint_list_raw_file (CL,pid_tmpfile[pid_list[pid]]);
161       remove(pid_tmpfile[pid_list[pid]]);
162     }
163   
164   vfree (pid_list);
165   vfree (pid_tmpfile);
166   
167   job=heap;
168   while (job)   job=delete_job (job);
169   n_new_elements=CL->ne - n_elements_in;
170   compact_list (CL, n_elements_in,n_new_elements, "best");
171   compact_list (CL, 0, CL->ne, "default");
172   CL->local_stderr=local_stderr;
173   free_queue  (heap);
174   return CL;
175 }
176
177
178 Constraint_list *fork_line_produce_list   ( Constraint_list *CL, Sequence *S, char * method,char *weight,char *mem_mode, Job_TC *job, FILE *local_stderr)
179 {
180   //forks lines of the matrix
181   int a,b;
182   Job_TC *heap;
183   TC_method *M;
184   int n_elements_in, n_new_elements;
185   char **pid_tmpfile;
186   int   *pid_list;
187   int pid,npid, njob;
188   int nproc, max_nproc, submited;
189   int cseq, seq, nlines;
190   
191   max_nproc=nproc=get_nproc();
192   max_nproc*=2;
193   /*OUT_MODE:
194     A->  alignment provided in a file
195     L->  list      provided in a file
196     aln-> alignment computed and provided in a file
197     list->list      computed and provided in a file
198   */
199     
200     
201   
202   
203   if ( job->jobid==-1)
204     {
205       M=(job->param)->TCM;
206       fprintf (local_stderr, "\n\tMethod %s: No Suitable Sequences [Type: %s]\n", method,M->seq_type); 
207       return CL;
208     }
209   
210   
211   job=queue2heap (job_list2multi_thread_job_list (job, CL->multi_thread, CL));
212   
213   heap=job;
214   M=(job->param)->TCM;
215   if (M)M->PW_CL=method2pw_cl ( M, CL);
216   n_elements_in=CL->ne;    
217   
218   /* Cf. parse method for possible out_mode flags*/
219   
220   njob=queue2n(job)+1;
221   pid_tmpfile=vcalloc (njob, sizeof (char*));
222  
223   pid_list   =vcalloc (MAX_N_PID, sizeof (int *));
224   fprintf ( local_stderr, "\n\tMulti Core Mode: %d processors [jobline]\n", get_nproc());
225  
226   
227   //count the number of lines
228   cseq=-1;
229   nlines=0;
230   while (job)
231     {
232       nlines++;
233       seq=job2first_seq(job);
234       if ( seq!=cseq)
235         {
236           cseq=seq;
237           while (job && cseq==job2first_seq(job))job=job->c;
238         }
239     }
240   job=heap;
241    
242   npid=submited=0;
243   cseq=-1;
244   while (job)
245     {
246       seq=job2first_seq(job);
247       if ( seq!=cseq)
248         {
249             cseq=seq;
250             pid_tmpfile[npid]=vtmpnam(NULL);
251             
252             pid=vfork();
253             
254             if (pid==0)//Child Process
255               {
256                 initiate_vtmpnam(NULL);
257                 while (job && cseq==job2first_seq(job))
258                   {
259                     job=print_lib_job (job, "io->CL=%p control->submitF=%p control->retrieveF=%p control->mode=%s",duplicate_constraint_list4lib_computation (CL),submit_lib_job, retrieve_lib_job, CL->multi_thread ); 
260                     job=submit_job (job);
261                     retrieve_job (job);
262                     constraint_list2raw_file ((job->io)->CL,pid_tmpfile[npid], "a");
263                     job=job->c;
264                   }
265                 exit (EXIT_SUCCESS);
266               }
267             else //parent process
268               {
269                 
270                 pid_list[pid]=npid;
271                 set_pid (pid);
272                 npid++;
273                 submited++;
274                 if (submited>max_nproc)
275                   {
276                     //wait for nproc
277                     for (a=0; a<nproc; a++)
278                       {
279                         int index;
280                         local_stderr=output_completion ( local_stderr,npid,nlines,1, "Processed   Job");
281                         pid=vwait(NULL);
282                         index=pid_list[pid];
283                         CL=read_constraint_list_raw_file (CL,pid_tmpfile[index]);
284                         remove (pid_tmpfile[index]);
285                         submited--;
286                       }
287                   }
288               }
289           }
290       else
291           {
292             job=job->c;
293           }
294     }
295   
296   for (a=0; a<submited; a++)
297     {
298       int index;
299       local_stderr=output_completion ( local_stderr,npid-(submited-a),nlines,1, "Processed   Job");
300       pid=vwait(NULL);
301       index=pid_list[pid];
302       CL=read_constraint_list_raw_file (CL,pid_tmpfile[index]);
303       remove (pid_tmpfile[index]);
304     }
305   
306   vfree (pid_list);
307   vfree (pid_tmpfile);
308   
309   n_new_elements=CL->ne - n_elements_in;
310   compact_list (CL, n_elements_in,n_new_elements, "best");
311   compact_list (CL, 0, CL->ne, "default");
312   CL->local_stderr=local_stderr;
313   
314   free_queue  (heap);
315   
316   return CL;
317 }
318
319 Constraint_list *fork_cell_produce_list   ( Constraint_list *CL, Sequence *S, char * method,char *weight,char *mem_mode, Job_TC *job, FILE *local_stderr)
320     {
321       //forks cells of the matrix
322       int a,b, n;
323       Job_TC *heap;
324       TC_method *M;
325       int n_elements_in, n_new_elements;
326       int *pid_list;
327       char **pid_tmpfile;
328       int pid,npid, njob;
329       int nproc, max_nproc;
330       int submited;
331       
332       max_nproc=nproc=get_nproc();
333             
334       /*OUT_MODE:
335               A->  alignment provided in a file
336               L->  list      provided in a file
337               aln-> alignment computed and provided in a file
338               list->list      computed and provided in a file
339     */
340     
341     
342       if ( job->jobid==-1)
343       {
344         M=(job->param)->TCM;
345         fprintf (local_stderr, "\n\tMethod %s: No Suitable Sequences [Type: %s]\n", method,M->seq_type); 
346         return CL;
347       }
348
349       
350     job=queue2heap (job_list2multi_thread_job_list (job, CL->multi_thread, CL));
351     
352     heap=job;
353     M=(job->param)->TCM;
354     if (M)M->PW_CL=method2pw_cl ( M, CL);
355     n_elements_in=CL->ne;    
356     
357     /* Cf. parse method for possible out_mode flags*/
358     
359     njob=queue2n(job)+1;
360     pid_tmpfile=vcalloc (njob, sizeof (char*));
361     pid_list   =vcalloc (MAX_N_PID, sizeof (int *));
362     
363     fprintf ( local_stderr, "\n\tMulti Core Mode: %d processors:\n", get_nproc());
364     npid=0;
365     submited=0;
366     while (job)
367       {
368         job=print_lib_job (job, "io->CL=%p control->submitF=%p control->retrieveF=%p control->mode=%s",duplicate_constraint_list4lib_computation (CL),submit_lib_job, retrieve_lib_job, CL->multi_thread );     
369         pid_tmpfile[npid]=vtmpnam(NULL);
370         pid=vfork ();
371         if ( pid==0)
372           {
373             initiate_vtmpnam (NULL);
374             job=submit_job (job);
375             retrieve_job (job);
376             constraint_list2raw_file ((job->io)->CL,pid_tmpfile[npid], "w");
377             exit (EXIT_SUCCESS);
378           }
379         else 
380           {
381             job=job->c;
382             pid_list[pid]=npid;
383             set_pid(pid);
384             npid++;
385             submited++;
386             
387             if (submited>max_nproc)
388               {
389                 for (a=0; a<nproc; a++)
390                 {
391                   int index;
392                   local_stderr=output_completion ( local_stderr,npid,njob,1, "Processed   Job");
393                   pid=vwait(NULL);
394                   index=pid_list[pid];
395                   CL=read_constraint_list_raw_file (CL,pid_tmpfile[index]);
396                   remove (pid_tmpfile[index]);
397                   submited--;
398                 }
399               }
400           }
401       }
402
403     for (a=0; a<submited; a++)
404       {
405         int index;
406         local_stderr=output_completion ( local_stderr,npid-a, npid,1, "Processed   Job");
407         pid=vwait(NULL);
408         index=pid_list[pid];
409         CL=read_constraint_list_raw_file (CL,pid_tmpfile[index]);
410         remove (pid_tmpfile[index]);
411       }
412     vfree (pid_list);
413     vfree (pid_tmpfile);
414     
415     n_new_elements=CL->ne - n_elements_in;
416     compact_list (CL, n_elements_in,n_new_elements, "best");
417     compact_list (CL, 0, CL->ne, "default");
418     CL->local_stderr=local_stderr;
419     
420     free_queue  (heap);
421     
422     return CL;
423     }
424 Constraint_list *nfork_produce_list   ( Constraint_list *CL, Sequence *S, char * method,char *weight,char *mem_mode, Job_TC *job, FILE *local_stderr)
425     {
426     int b;
427     int n_aln;
428     Job_TC *heap;
429     TC_method *M;
430     int n_elements_in, n_new_elements;
431         
432     
433     /*OUT_MODE:
434               A->  alignment provided in a file
435               L->  list      provided in a file
436               aln-> alignment computed and provided in a file
437               list->list      computed and provided in a file
438     */
439     
440     
441     if ( job->jobid==-1)
442       {
443         M=(job->param)->TCM;
444         fprintf (local_stderr, "\n\tMethod %s: No Suitable Sequences [Type: %s]\n", method,M->seq_type); 
445         return CL;
446       }
447
448     
449     job=queue2heap (job_list2multi_thread_job_list (job, CL->multi_thread, CL));
450     
451     heap=job;
452     n_aln=queue2n (job);
453     M=(job->param)->TCM;
454
455     if (M)M->PW_CL=method2pw_cl ( M, CL);
456
457    
458     n_elements_in=CL->ne;    
459     
460     
461     /* Cf. parse method for possible out_mode flags*/
462     
463    
464        
465     b=0;
466
467     while (job)
468       {
469         local_stderr=output_completion ( local_stderr, b, n_aln+1,1, "Submit   Job");
470         job=print_lib_job (job, "io->CL=%p control->submitF=%p control->retrieveF=%p control->mode=%s",duplicate_constraint_list4lib_computation (CL),submit_lib_job, retrieve_lib_job, CL->multi_thread );     
471         job=submit_job (job);
472         if (retrieve_job (job)) 
473           {
474             CL=merge_constraint_list    ((job->io)->CL, CL, "default");
475             free_constraint_list4lib_computation ( (job->io)->CL);      
476           }
477         job=job->c;
478         b++;
479       }
480     job=heap;
481     fprintf ( local_stderr, "\n");
482     
483     while (job) job=delete_job (job);
484   
485     n_new_elements=CL->ne - n_elements_in;
486     compact_list (CL, n_elements_in,n_new_elements, "best");
487     compact_list (CL, 0, CL->ne, "default");
488     CL->local_stderr=local_stderr;
489     
490     free_queue  (heap);
491     
492     return CL;
493     }
494
495
496 Job_TC *job_list2multi_thread_job_list (Job_TC* ojob, char *mt, Constraint_list *CL)
497 {
498   FILE *fp=NULL;
499   int mtv, n, nj;
500   char *met_file, *seq_file, *lib_file, *lib_list;
501   char common[1000], command[1000], T_file[100];
502   Job_TC *njob, *heap;
503   TC_method *TCM;
504  
505   
506   mtv=(mt==NULL)?0:atoi (mt);
507   if ( !mtv || mtv==1)return ojob;
508   
509   HERE ("***"); exit (0);
510   heap=ojob;
511   nj=(queue2n(ojob)/mtv)+1;
512   nj=(nj==0)?1:nj;
513   
514   met_file=vtmpnam (NULL);  
515   TC_method2method_file ((ojob->param)->TCM,met_file=vtmpnam (NULL));  
516   
517   TCM=method_file2TC_method (method_name2method_file ("tc2"));
518   sprintf (T_file,"%s",(CL->S)->template_file);
519   
520   HERE ("2");
521
522   seq_file=vtmpnam (NULL);
523   sprintf ( common, "%s -in M%s S%s -lib_only ", TCM->executable, met_file, seq_file);
524   if ( T_file[0] && check_file_exists (T_file))
525     {
526       strcat (common, " -template_file ");strcat (common, T_file); strcat ( common , " ");
527     }
528
529   njob=vcalloc ( 1, sizeof (Job_TC));
530   njob->jobid=-1;
531   n=0;
532
533   HERE ("3");
534
535   while ( ojob && n<nj)
536     {
537       if (n==0)
538         {
539           lib_file=vtmpnam ( NULL);
540           lib_list=vtmpnam ( NULL);
541           fp=vfopen (lib_list, "w");
542           sprintf ( command, "%s -out_lib=%s -lib_list=%s -quiet", common, lib_file, lib_list);
543           njob->c=print_lib_job (NULL, "param->TCM=%p param->method=%s param->aln_c=%s io->in=%s io->out=%s",TCM, "tc2", command,seq_file, lib_file);
544           
545           njob=queue_cat (njob, njob->c);
546         }
547
548       fprintf ( fp, "%s\n", (ojob->param)->seq_c);
549       ojob=ojob->c;
550       if ( (++n)==nj){vfclose (fp);n=0;}
551     }
552   HERE ("4");
553   if (fp && n)vfclose (fp);
554   free_queue  (heap);
555   return njob;
556 }
557
558
559
560   
561   
562   
563   
564 Job_TC *retrieve_lib_job ( Job_TC *job)
565 {
566   Job_param_TC *p;
567   Job_io_TC *io;
568   TC_method *M;
569
570   p=job->param;
571   io=job->io;
572   M=(job->param)->TCM;
573   
574   
575   if ( job->status==EXIT_SUCCESS)
576     {
577       static char *log_output;
578       static int log;
579
580       if ( !M) return job;
581       else if (strm2(M->out_mode, "aln", "A"))
582         {
583           io->CL=read_constraint_list (io->CL,io->out,"aln","disk",M->weight);
584         }
585       else if (strm2(M->out_mode, "lib","L"))
586         {
587           io->CL=read_constraint_list (io->CL,io->out,"lib","disk",M->weight);
588         }
589       return job;
590     }
591   else
592     return NULL;
593 }
594
595 int add_method_output2method_log (char *l,char *command,Alignment *A, Constraint_list *CL, char *io_file)
596 {
597   static int   header;
598   static int log;
599   static char *file, *log_file;
600   static int set;
601   
602   if ( set && log_file==NULL && l==NULL) return 0;
603   if (!set ){log_file=get_string_variable ("method_log");if (log_file && strm (log_file, "no"))log_file=NULL; set=1;}
604   if (!file)file=vtmpnam (NULL);
605   
606   if ( l);
607   else if (!l && log_file) l=log_file;
608   else return 0;
609   
610   
611   if (!header){printf_file ( l, "w", "# CONC_MSF_FORMAT_01\n");header=1;}
612   if (command)printf_file (l,  "a", "%s\n#----------------------------------------------\n#%s\n", TC_REC_SEPARATOR,command);
613   
614   
615   if ( A)
616     {
617      
618       io_file=file;
619       output_fasta_aln (io_file, A);
620     }
621   else if (CL)
622     {
623       io_file=file;
624       vfclose (save_constraint_list ( CL, 0, CL->ne,io_file, NULL, "ascii",CL->S));
625     }
626   else
627     file_cat (io_file,l);
628   
629     
630   return 1;
631 }
632
633
634       
635 Job_TC *submit_lib_job ( Job_TC *job)
636 {
637   Job_param_TC *p;
638   Job_io_TC *io;
639   TC_method *M;
640   static char *l;
641   static int log;
642   
643   p=job->param;
644   io=job->io;
645   M=(job->param)->TCM;
646   add_method_output2method_log (NULL, p->aln_c, NULL, NULL, NULL);
647   if ( getenv4debug ("DEBUG_LIBRARY"))fprintf ( stderr, "\n[DEBUG_LIBRARY:produce_list] Instruction: %s\n", p->aln_c);
648   
649   if ( !M)
650     {
651       return job;
652     }
653   else if (strm4 (M->out_mode,"A", "L", "aln", "lib"))
654     {   
655
656       seq_list2in_file ( M, (io->CL)->S, p->seq_c, io->in);
657       
658       my_system (p->aln_c);/*Multi Threaded jobs must be sent here*/
659       
660       add_method_output2method_log (NULL,NULL, NULL, NULL, io->out);
661       
662       if (!evaluate_sys_call_io (io->out,p->aln_c, "") || (strm (M->out_mode, "aln") && !(is_aln (io->out) || is_seq(io->out))) ) 
663         {
664           add_warning (stderr, "\nFAILED TO EXECUTE:%s [SERIOUS:%s]", p->aln_c, PROGRAM);     
665           job->status=EXIT_FAILURE;
666           //myexit (EXIT_FAILURE);
667         return job;
668         }
669     }
670   else if ( strm2 (M->out_mode, "fA", "fL"))
671     {
672       io->CL= seq2list(job);
673       if (!io->CL)
674         {
675           add_warning (stderr, "\nFAILED TO EXECUTE:%s [SERIOUS:%s]", p->aln_c, PROGRAM);     
676           job->status=EXIT_FAILURE;
677         }
678     }
679   else
680     {
681       fprintf ( stderr, "\nERROR: Unknown out_mode=%s for method[FATAL:%s]\n", M->out_mode, M->executable);
682       myexit (EXIT_FAILURE);
683     }
684   
685 return job;
686 }
687
688
689
690 Job_TC* method2job_list ( char *method_name,Sequence *S, char *weight, char *lib_list, Distance_matrix *DM, Constraint_list *CL)
691     {
692       int preset_method;  
693       static char *fname, *bufS, *bufA;
694       char *in,*out;
695       TC_method *method;
696       char aln_mode[100];
697       char out_mode[100];
698       Job_TC *job;
699       int hijack_P_jobs=1;
700       
701     /*A method can be:
702       1- a pre computed alignment out_mode=A
703       2- a precomputed Library    out_mode=L
704       3- a method producing an alignment out_mode=aln
705       4- a method producing an alignment out_mode=list
706       5- a function producing an alignment out_mode=faln
707       6- a function producing a library    out_mode=flist
708     */
709     
710       if ( !fname)
711         {
712           fname=vcalloc ( 1000, sizeof (char));
713           bufS=vcalloc ( S->nseq*10, sizeof (char));
714         }
715       
716     /*Make sure that fname is a method*/
717     
718
719     sprintf(fname, "%s", method_name);
720     
721     if ( fname[0]=='A' || fname[0]=='L')
722         {
723           method=method_file2TC_method("no_method");
724           sprintf ( method->out_mode, "%c", fname[0]);
725           
726           if (!strm (weight, "default"))sprintf ( method->weight, "%s", weight);
727           
728           return print_lib_job(NULL,"param->out=%s param->TCM=%p",fname+1, method);
729         }    
730     else if ( fname[0]=='M' && is_in_pre_set_method_list (fname+1))
731         {       
732           preset_method=1;
733           fname++;
734         }
735     else if ( is_in_pre_set_method_list (fname))
736         {
737         preset_method=1;        
738         }
739     else 
740         {
741           char buf[1000];         
742           if ( check_file_exists ( fname));
743           else if (fname[0]=='M' && check_file_exists(fname+1));
744           else
745             {
746               sprintf ( buf, "%s/%s", get_methods_4_tcoffee(), fname);
747               if( check_file_exists(buf)){sprintf ( fname, "%s", buf);}
748               else
749                 {
750                   fprintf ( stderr, "\n%s (%s) is not a valid method [FATAL:%s]\n", buf,fname, PROGRAM);
751                   myexit (EXIT_FAILURE);
752                 }
753             }
754         }
755     
756
757     method=method_file2TC_method(fname);
758     job=print_lib_job (NULL, "param->TCM=%p", method);
759     job->jobid=-1;
760     
761     if (!strm (weight, "default"))sprintf ( method->weight, "%s", weight);
762     
763     sprintf ( aln_mode, "%s", method->aln_mode);
764     sprintf ( out_mode, "%s", method->out_mode);
765     
766
767     if (lib_list && lib_list[0])
768       {
769         static char **lines, **list=NULL;
770         int a,i, x, n, nl;
771         
772
773         
774         if ( lines) free_char (lines, -1);
775
776
777         if ( strstr (lib_list, "prune"))
778           {
779             lines=file2lines (list2prune_list (S,DM->similarity_matrix));
780           }
781         else
782           {
783           lines=file2lines (lib_list);
784           }
785                 
786         nl=atoi (lines[0]);
787         for (a=1; a<nl; a++)
788           {
789
790             if (list) free_char (list, -1);
791             if (isblanc (lines[a]))continue;
792
793             list=string2list (lines[a]);n=atoi(list[1]);
794             if ( n> 2 && strm (aln_mode, "pairwise"))continue;
795             if ( n==2 && strm (aln_mode, "multiple"))continue;
796
797             for (i=2; i<n+2; i++)
798               {
799               if ( is_number (list[i]));
800               else if ((x=name_is_in_list (list[i], S->name, S->nseq, 100))!=-1)sprintf(list[i], "%d", x);
801               else
802                 {
803                   add_warning ( stderr, "\nWARNING: %s is not part of the sequence dataset \n", list[i]);
804                   continue;
805                 }
806               }
807             sprintf ( bufS, "%s", list[1]);         
808             for ( i=2; i<n+2; i++) {strcat (bufS, " ");strcat ( bufS, list[i]);}
809                     
810            
811             bufA=make_aln_command (method, in=vtmpnam(NULL),out=vtmpnam(NULL));
812             
813             if (strrchr(bufA, '>')==NULL)strcat (bufA,TO_NULL_DEVICE);
814             if ( check_seq_type ( method, bufS, S))
815               {
816                 job->c=print_lib_job (NULL, "param->TCM=%p param->method=%s param->aln_c=%s param->seq_c=%s io->in=%s io->out=%s ", method, fname, bufA, bufS, in, out);              
817                 job=queue_cat (job, job->c);
818               }
819             vfree (bufA);
820            
821           }
822       }
823     else if ( strcmp (aln_mode, "multiple")==0)
824       {
825         int d;
826         char buf[1000];
827         
828         sprintf (bufS, "%d",S->nseq);
829         for (d=0; d< S->nseq; d++)
830           {
831             sprintf ( buf," %d",d);
832             strcat ( bufS, buf);
833           }
834         
835         bufA=make_aln_command (method, in=vtmpnam(NULL),out=vtmpnam(NULL));
836         
837         if (strrchr(bufA, '>')==NULL)strcat (bufA,TO_NULL_DEVICE);
838         
839         if ( check_seq_type ( method, bufS, S))
840           {
841             
842             job->c=print_lib_job (NULL, "param->TCM=%p param->method=%s param->aln_c=%s param->seq_c=%s io->in=%s io->out=%s ", method, fname, bufA, bufS, in, out, S->template_file);        
843             
844             job=queue_cat (job, job->c);
845             
846           }
847         vfree (bufA);
848         
849       }
850     else if ( strstr(aln_mode, "pairwise"))
851       {
852         
853         int do_mirror, do_self, x, y, id;
854         do_mirror=(strstr(aln_mode, "m_"))?1:0;
855         do_self=(strstr(aln_mode, "s_"))?1:0;
856
857         
858         for (x=0; x< S->nseq; x++)
859           for ( y=(do_mirror)?0:x; y< S->nseq; y++)
860             {
861               
862               id=DM->similarity_matrix[x][y];
863               
864               if ( x==y && !do_self);
865               else if ( !is_in_range(id,method->minid, method->maxid));  
866               else
867                 {
868                   sprintf (bufS, "2 %d %d",x,y);
869                   bufA=make_aln_command (method,in=vtmpnam(NULL),out=vtmpnam (NULL));
870                   
871                   if (strrchr(bufA, '>')==NULL)strcat (bufA, TO_NULL_DEVICE);
872                   if (check_seq_type (method, bufS, S))
873                     {
874                       job->c=print_lib_job (job->c, "param->TCM=%p param->method=%s param->aln_c=%s param->seq_c=%s io->in=%s io->out=%s ",method,fname,bufA, bufS, in, out);
875                       job=queue_cat (job, job->c);
876                     }
877                   else if ( method->seq_type[0]=='P' && hijack_P_jobs)
878                     {
879                       //Hijack _P_ jobs without enough templates
880                       static TC_method *proba_pairM;
881                       
882                       fprintf (stderr, "\n\t Information: Method %s cannot be applied to [%s vs %s]. Use proba_pair instead", method, (CL->S)->name[x], (CL->S)->name [y]);
883                       if (!proba_pairM)
884                         {
885                           proba_pairM=method_file2TC_method(method_name2method_file ("proba_pair"));
886                           proba_pairM->PW_CL=method2pw_cl(proba_pairM, CL);
887                         }
888                       job->c=print_lib_job (job->c, "param->TCM=%p param->method=%s param->aln_c=%s param->seq_c=%s io->in=%s io->out=%s ",proba_pairM,fname,bufA, bufS, in, out);
889                       job=queue_cat (job, job->c);
890                     }
891
892                   vfree (bufA);
893                 }
894             }
895       }
896
897     return job;
898     }
899
900 int check_seq_type (TC_method *M, char *list,Sequence *S)
901 {
902   char t1, t2;
903   int s1, s2, n1, nseq, ntype, i;
904   int *slist;
905   Template *T1, *T2;
906
907
908   slist=string2num_list (list);
909   
910   nseq=slist[1];
911   ntype=strlen (M->seq_type);
912   t1=M->seq_type[0];
913   t2=M->seq_type[1];
914   n1=0;
915
916   /*Profiles and Sequences MUST NOT be distinguished so that sequences and profiles can easily be aligned*/
917   if ( tolower(t1)=='r')t1='S';
918   if ( tolower(t2)=='r')t2='S';
919   
920
921   if ( strm ( M->aln_mode, "pairwise") && nseq>2)n1=0;
922   else if (strm ( M->aln_mode, "multiple" ) && ntype>1)n1=0;
923   else if (ntype==1)
924     {
925       
926       for (n1=0, i=0; i<nseq; i++)
927         {
928           s1=slist[i+2];
929           T1=S->T[s1];
930           
931           n1+=(strchr (T1->seq_type,t1) || check_profile_seq_type (S, s1, t1))?1:0;
932         }
933       n1=(n1==nseq)?1:0;
934     }
935   else if (ntype==2)
936     {
937       int s1_has_t1;
938       int s1_has_t2;
939       int s2_has_t1;
940       int s2_has_t2;
941       
942       s1=slist[2];
943       s2=slist[3];
944       T1=(S->T[s1]);
945       T2=(S->T[s2]);
946       
947       s1_has_t1=(strchr ( T1->seq_type, t1) || check_profile_seq_type (S, s1, t1))?1:0;
948       s1_has_t2=(strchr ( T1->seq_type, t2) || check_profile_seq_type (S, s1, t2))?1:0;
949       s2_has_t1=(strchr ( T2->seq_type, t1) || check_profile_seq_type (S, s2, t1))?1:0;
950       s2_has_t2=(strchr ( T2->seq_type, t2) || check_profile_seq_type (S, s2, t2))?1:0;
951       n1=((s1_has_t1 && s2_has_t2) || (s1_has_t2 && s2_has_t1))?1:0;
952     }
953   
954   vfree (slist);
955   return n1;
956 }
957
958 int check_profile_seq_type (Sequence *S, int i, char t)
959 {
960   Alignment *A;
961   Template *T;
962   int a;
963   
964   /*returns 1 if the sequence S is associated with a profile containing the right sequence*/
965   A=seq2R_template_profile (S, i);
966   if (A==NULL || A->S==NULL) return 0;
967   for ( a=0; a< A->nseq; a++)
968     {
969
970       T=(A->S)->T[a];
971       if ( T && strchr( T->seq_type,t))return 1;
972       
973     }
974   return 0;
975 }
976
977
978 char **method_list2method4dna_list ( char **list, int n)
979 {
980   int a;
981   static char *buf;
982   
983   if ( !buf)buf=vcalloc ( 1000, sizeof (char));
984   
985   if ( !list || n==0)return list;
986   buf=vcalloc ( 1000, sizeof (char));
987     
988   for ( a=0; a< n; a++)
989     {
990       
991       sprintf ( buf,"%s",list[a]);
992       
993       if ( strm ( list[a], "4dna"));
994       else 
995         {
996           
997           char **para ;
998           int b;
999           
1000           para=string2list2 (list[a], "@");
1001           sprintf ( buf, "%s4dna", para[1]);
1002           if (method_name2method_file (buf) || method_name2method_file(buf+1))
1003             {
1004               sprintf ( list[a],"%s", buf);
1005               for (b=2; b< atoi (para[0]); b++)
1006                 {
1007                   strcat (list[a], "@");
1008                   strcat (list[a], para[b]);
1009                 }
1010             }
1011           
1012           free_char (para, -1);
1013         }
1014     }
1015   return list;
1016 }
1017  
1018 int is_in_pre_set_method_list ( char *method)
1019 {
1020   char *new_name;
1021
1022  
1023  
1024
1025   new_name=method_name2method_file (method);
1026  
1027   if ( !new_name) return 0;
1028   else
1029     {
1030
1031       sprintf ( method, "%s", new_name);
1032       return 1;
1033     }
1034 }
1035 char *** display_method_names (char *mode, FILE *fp)
1036 {
1037   char ***list, ***l2;
1038   int n=0, a, ml1=0, ml2=0, ml3=0;
1039   int status;
1040   
1041   
1042   
1043   
1044
1045   
1046   list=produce_method_file (NULL);
1047   l2=declare_arrayN(3,sizeof (char), 1000, 10, 100);
1048   
1049   fprintf ( fp, "\n#######   Compiling the list of available methods ... (will take a few seconds)\n");
1050   a=0;
1051   while (list[a])
1052     {
1053       
1054
1055       sprintf (l2[a][0], "%s", method_file_tag2value (list[a][1],"ADDRESS"));
1056       sprintf (l2[a][1], "%s", method_file_tag2value (list[a][1],"PROGRAM"));
1057       sprintf (l2[a][2], "%s", method_file_tag2value (list[a][1],"ALN_MODE"));
1058       sprintf (l2[a][3], "%s", method_file_tag2value (list[a][1],"SEQ_TYPE"));
1059       ml1=MAX((strlen (list[a][0])),ml1);
1060       ml2=MAX((strlen (l2  [a][0])),ml2);
1061       ml3=MAX((strlen (l2  [a][1])),ml3);
1062       l2[a][4][0]= check_program_is_installed (l2[a][1],NULL,NULL,l2[a][0],NO_REPORT);
1063       if (l2[a][4][0])l2[a][5]=pg2path (l2[a][1]);
1064       a++;
1065     }
1066   
1067   fprintf ( fp, "\n#######   Methods For which an Interface is available in T-Coffee\n");
1068   fprintf ( fp, "You must install the packages yourself when required (use the provided address)\n");
1069   fprintf ( fp, "Contact us if you need an extra method to be added [%s]\n", EMAIL);
1070   
1071   
1072   fprintf ( fp, "\n****** Pairwise Sequence Alignment Methods:\n");
1073   fprintf ( fp, "--------------------------------------------\n");
1074   a=0;
1075   while (list[a])
1076     {
1077       if ( strm (l2[a][2], "pairwise") && !strstr (l2[a][3], "P"))
1078         fprintf ( fp, "%-*s %-*s [pg: %*s is %s Installed][%s]\n", ml1,list[a][0],ml2, l2[a][0], ml3,l2[a][1], (l2[a][4][0]==0)?"NOT":"",(l2[a][5])?l2[a][5]:"");
1079       a++;
1080     }
1081   
1082   fprintf ( fp, "\n****** Pairwise Structural Alignment Methods:\n");
1083     fprintf ( fp, "--------------------------------------------\n");
1084   a=0;
1085   while (list[a])
1086     {
1087       if ( strm (l2[a][2], "pairwise") && strstr (l2[a][3], "P"))
1088         fprintf ( fp, "%-*s %-*s [pg: %*s is %s Installed][%s]\n", ml1,list[a][0],ml2, l2[a][0], ml3,l2[a][1], (l2[a][4][0]==0)?"NOT":"",(l2[a][5])?l2[a][5]:"");
1089       a++;
1090     }
1091   fprintf ( fp, "\n****** Multiple Sequence Alignment Methods:\n");
1092   fprintf ( fp, "--------------------------------------------\n");
1093   a=0;
1094   while (list[a])
1095     {
1096       if ( strm (l2[a][2], "multiple"))
1097         fprintf ( fp, "%-*s %-*s [pg: %*s is %s Installed][%s]\n", ml1,list[a][0],ml2, l2[a][0], ml3,l2[a][1], (l2[a][4][0]==0)?"NOT":"",(l2[a][5])?l2[a][5]:"");
1098       a++;
1099     }
1100   fprintf ( fp, "\n#######   Prediction Methods available to generate Templates\n");
1101   fprintf ( fp, "-------------------------------------------------------------\n");
1102   a=0;
1103   while (list[a])
1104     {
1105       if ( strm (l2[a][2], "predict"))
1106         fprintf ( fp, "%-*s %-*s [pg: %*s is %s Installed][%s]\n", ml1,list[a][0],ml2, l2[a][0], ml3,l2[a][1], (l2[a][4][0]==0)?"NOT":"",(l2[a][5])?l2[a][5]:"");
1107       a++;
1108     }
1109   fprintf ( fp, "\n\n\nAll these Methods are supported by T-Coffee, but you HAVE to install them yourself [use the provided address]\n\n");
1110   fprintf ( fp, "\nThese methods were selected because they are freeware opensource, easy to install and well supported");
1111   fprintf ( fp, "\nContact us if you need an extra method to be added [%s]\n", EMAIL);
1112  
1113     return l2;
1114 }
1115   
1116 char* method_name2method_file (char *method)
1117     {
1118       char *fname=NULL;
1119       char ***mlist, *p;
1120       char address[100];
1121       char program[100];
1122       int a;
1123         
1124       if ( check_file_exists (method) || (toupper(method[0])==method[0] && check_file_exists (method+1)))return NULL;
1125  
1126       if ( (p=strstr (method, "@"))!=NULL && !strstr (method, "em@"))p[0]='\0';
1127       mlist=produce_method_file (method);
1128       
1129             
1130       
1131       a=0;
1132       while (mlist[a])
1133       {
1134         if ( lstrstr (method,mlist[a][0])){fname=mlist[a][1];break;}
1135         else {a++;}
1136       }
1137       if (p)p[0]='@';
1138       if ( fname==NULL) return NULL;
1139       else 
1140         {
1141           sprintf (address, "%s", method_file_tag2value (fname,"ADDRESS"));
1142           sprintf (program, "%s", method_file_tag2value (fname,"PROGRAM"));
1143           check_program_is_installed (program,NULL,NULL,address,INSTALL_OR_DIE);
1144           if ( (method=strstr (method, "EP@"))!=NULL)
1145             {
1146               int a;
1147               char **list;
1148               FILE *fp;
1149               list=string2list2 ( method, "@");
1150               fp=vfopen (fname, "a");
1151               for ( a=2; a<atoi (list[0]); a+=2)
1152                 {
1153                   fprintf (fp, "%s %s\n", list[a], list[a+1]);
1154                   
1155                 }
1156               
1157               vfclose (fp);
1158               free_char (list, -1);
1159             }
1160           return fname;
1161         }
1162     }
1163 char *method_file_tag2value (char *method, char *tag)
1164 {
1165   FILE *fp;
1166   char c, *p;
1167   static char *buf, *line;
1168   if (!buf) buf=vcalloc ( 1000, sizeof (char));
1169   else buf[0]='\0';
1170   
1171   if (!line)
1172     {
1173       line=vcalloc (LONG_STRING+1, sizeof ( char));
1174     }
1175   
1176   fp=vfopen (method, "r");
1177   while ( (c=fgetc (fp))!=EOF)
1178     {
1179       ungetc ( c, fp);
1180       fgets ( line,LONG_STRING, fp);
1181       
1182       if ( (line && (line[0]=='*' || line[0]=='#' || line[0] == '$'|| line[0]==' ' || line[0]=='\0' )));
1183       else if ( (p=strstr (line, tag    ))) 
1184         
1185         {
1186           sscanf (p+strlen (tag), " %s"   , buf);
1187           vfclose (fp);
1188           return buf;
1189         }
1190     }
1191   vfclose ( fp);
1192   return buf;
1193 }
1194 struct TC_method * method_file2TC_method( char *method)
1195 {
1196   TC_method *m;
1197   static char *subcommand;
1198   static char *line;
1199   char *p;
1200   FILE *fp;
1201   int c; 
1202   //<EXECUTABLE><PARAM1><IN_FLAG><seq_file><PARAM2><OUT_FLAG><outname><PARAM>
1203   if (!line)
1204     {
1205       line=vcalloc (LONG_STRING+1, sizeof ( char));
1206       subcommand=vcalloc ( LONG_STRING, sizeof (char));
1207     }
1208
1209   m=vcalloc ( 1, sizeof (TC_method));
1210   
1211   /*set default parameter values*/
1212   m->gop=m->gep=UNDEFINED;
1213   sprintf (m->seq_type, "S");
1214   sprintf (m->weight, "sim");
1215   m->minid=0;
1216   m->maxid=100;
1217   
1218   fp=vfopen (method, "r");
1219   while ( (c=fgetc (fp))!=EOF)
1220     {
1221       ungetc ( c, fp);
1222       fgets ( line,LONG_STRING, fp);
1223       
1224       
1225       line=substitute (line, "\n", " ");
1226       line=substitute (line, "%s", " ");
1227       line=substitute (line, "%e", "=");
1228       line=substitute (line, "%m", "-");
1229       
1230       if ( (line && (line[0]=='*' || line[0]=='#' || line[0] == '$'|| line[0]==' ' || line[0]=='\0' )))subcommand[0]='\0';
1231       //Parse PARAM, PARM1 and PARAM2 first because they may contain keywords
1232       else if ( (p=strstr (line, "PARAM1"     )))
1233         {
1234           sprintf (subcommand, " %s ", p+6);
1235           strcat ( m->param1, subcommand);
1236         }
1237       else if ( (p=strstr (line, "PARAM2"     )))
1238         {
1239           sprintf (subcommand, " %s ", p+6);
1240           strcat ( m->param2, subcommand);
1241         }
1242       else if ( (p=strstr (line, "PARAM"     )))
1243         {
1244           sprintf (subcommand, " %s ", p+5);
1245           strcat ( m->param, subcommand);
1246         }else if ( (p=strstr (line, "EXECUTABLE2" ))) 
1247         {
1248           sscanf (p, "EXECUTABLE2 %s", m->executable2);
1249         }
1250       else if ( (p=strstr (line, "EXECUTABLE" ))) 
1251         {
1252           sscanf (p, "EXECUTABLE %s", m->executable);
1253         }
1254       else if ( (p=strstr (line, "IN_FLAG2"    ))) sscanf (p, "IN_FLAG2 %s"   , m->in_flag2);
1255       else if ( (p=strstr (line, "IN_FLAG"    ))) sscanf (p, "IN_FLAG %s"   , m->in_flag);
1256       else if ( (p=strstr (line, "OUT_FLAG"   ))) sscanf (p, "OUT_FLAG %s"  , m->out_flag);
1257       else if ( (p=strstr (line, "OUT_MODE"   ))) sscanf (p, "OUT_MODE %s"  , m->out_mode);
1258       else if ( (p=strstr (line, "ALN_MODE"   ))) sscanf (p, "ALN_MODE %s"  , m->aln_mode);
1259       else if ( (p=strstr (line, "SEQ_TYPE"   ))) sscanf (p, "SEQ_TYPE %s"  , m->seq_type);
1260       else if ( (p=strstr (line, "WEIGHT"     ))) sscanf (p, "WEIGHT %s"  , m->weight);
1261       else if ( (p=strstr (line, "MATRIX"     ))){ sscanf (p, "MATRIX %s"  , m->matrix);}
1262       else if ( (p=strstr (line, "GOP"        ))) sscanf (p, "GOP %d"  , &m->gop);
1263       else if ( (p=strstr (line, "GEP"        ))) sscanf (p, "GEP %d"  , &m->gep);
1264       else if ( (p=strstr (line, "MAXID"      ))) sscanf (p, "MAXID %d"  , &m->maxid);
1265       else if ( (p=strstr (line, "MINID"      ))) sscanf (p, "MINID %d"  , &m->minid);
1266      
1267     }
1268   vfclose ( fp);
1269
1270
1271
1272   return m;
1273 }
1274
1275 int TC_method2method_file( struct TC_method*m,char *fname )
1276 {
1277   FILE *fp;
1278   if ( !m) return 0;
1279   fp=vfopen ( fname, "w");
1280   if ( m->executable[0])fprintf (fp, "EXECUTABLE %s\n", m->executable);
1281   if (m->in_flag[0])fprintf (fp, "IN_FLAG %s\n", m->in_flag);
1282   if (m->out_flag[0])fprintf (fp, "OUT_FLAG %s\n", m->out_flag);
1283   if (m->out_mode[0])fprintf (fp, "OUT_MODE %s\n", m->out_mode);
1284   if (m->aln_mode[0])fprintf (fp, "ALN_MODE %s\n", m->aln_mode);
1285   if (m->seq_type)fprintf (fp, "SEQ_TYPE %s\n", m->seq_type);
1286   if (m->weight[0])fprintf (fp, "WEIGHT %s\n", m->weight);
1287   if (m->matrix[0])fprintf (fp, "MATRIX %s\n", m->matrix);
1288   if (m->gop!=UNDEFINED)fprintf (fp, "GOP %d\n", m->gop);
1289   if (m->gep!=UNDEFINED)fprintf (fp, "GEP %d\n", m->gep);
1290   if (m->minid!=0  )fprintf (fp, "MINID %d\n", m->minid);
1291   if (m->maxid!=100)fprintf (fp, "MAXID %d\n", m->maxid);
1292   if (m->param[0])fprintf (fp, "PARAM %s\n", m->param);
1293   if (m->param1[0])fprintf (fp, "PARAM1 %s\n", m->param1);
1294   if (m->param2[0])fprintf (fp, "PARAM1 %s\n", m->param2);
1295   if (m->in_flag2[0])fprintf (fp, "IN_FLAG2 %s\n", m->in_flag2);
1296   
1297   vfclose ( fp);
1298   return 1;
1299 }
1300
1301 char *make_aln_command(TC_method *m, char *seq, char *aln)
1302     {
1303       char *command;
1304       char buf[1000];
1305     
1306       //      sprintf ( buf, "%s %s %s%s %s%s %s", m->executable, m->param1, m->in_flag, seq,m->param2, m->out_flag,aln, m->param);
1307       sprintf ( buf, "%s %s %s%s %s %s%s %s", m->executable, m->param1, m->in_flag, seq,m->param2, m->out_flag,aln, m->param);
1308       command=vcalloc ( strlen (buf)+100, sizeof (char));
1309       sprintf ( command, "%s", buf);
1310       
1311
1312       command=substitute (command, "&bnsp", " ");
1313       command=substitute (command, "no_name", "");
1314
1315       return command;
1316     }
1317         
1318
1319      
1320      
1321
1322 /*********************************************************************/
1323 /*                                                                   */
1324 /*                         WRITE IN LIST                             */
1325 /*                                                                   */
1326 /*                                                                   */
1327 /*********************************************************************/
1328
1329
1330 int dump_constraint_list (Constraint_list *CL)
1331 {
1332   int *L;
1333   int a, b;
1334   if (!CL->L) return 0;
1335   L=CL->L;
1336   CL->L=NULL;
1337   CL->fp=tmpfile ();
1338   
1339   for ( a=0; a<CL->ne; a++)
1340     for (b=0; b<CL->entry_len; b++)
1341       vwrite_clist (CL, a, b, L[a*CL->entry_len+b]);
1342   
1343   vfree (CL->L); CL->L=NULL;
1344   return 1;
1345 }
1346
1347 int vread_clist ( Constraint_list *CL, int a, int b )
1348     {
1349     int x;
1350     
1351     if ( a>= CL->ne)
1352       {
1353         return UNDEFINED_2;
1354         
1355       }
1356     else if (CL->fp)
1357        {       
1358          
1359          fseek (CL->fp, a*CL->el_size*CL->entry_len+b*CL->el_size, SEEK_SET);
1360          fread (&x, CL->el_size, 1, CL->fp);
1361          
1362          return x;
1363        }
1364     else if ( CL->L)
1365        {
1366          return CL->L[a*CL->entry_len+b];
1367        }
1368     else if (CL->M)
1369        {
1370        return (CL->M)[a][b];
1371        }
1372     else 
1373        {
1374        return UNDEFINED_2; 
1375        
1376        }
1377     return UNDEFINED_2;
1378     }
1379 int vwrite_clist ( Constraint_list *CL, int a, int b, CLIST_TYPE x)
1380     {
1381
1382       CL->seq_indexed=0;
1383       CL->residue_indexed=0;
1384
1385
1386       if (CL->fp)
1387         {
1388           fseek (CL->fp, a*CL->el_size*CL->entry_len+b*CL->el_size, SEEK_SET);
1389           fwrite(&x, CL->el_size, 1, CL->fp);
1390         }
1391       else if (!CL->M)
1392         {
1393           int i,l;
1394           
1395           
1396           i=a*CL->entry_len+b;
1397           if (CL->L)
1398             {
1399               Memcontrol *p;
1400               p=(Memcontrol *)CL->L;
1401               p-=2;
1402               l=(int)p[0].size/p[0].size_element;
1403               //read_size_int (CL->L,sizeof (int));
1404             }
1405           else 
1406             {
1407               l=CL->chunk;
1408               (CL->L)=vcalloc (l,sizeof (int));
1409             }
1410           
1411           if (l<=i)
1412             {
1413               l+=CL->chunk;
1414               (CL->L)=vrealloc ( (CL->L),l*sizeof (int));
1415             }
1416           (CL->L)[i]=x;
1417 #ifdef gagaga
1418           if (((a*CL->entry_len)+b)>=(CL->entry_len*CL->max_L_len))
1419             {
1420               
1421               if (!(CL->L))
1422                 {
1423                   (CL->L)=vcalloc ((CL->chunk+a)*CL->entry_len+b, sizeof (int));
1424                 }
1425               else
1426                 {
1427                   CL->max_L_len+=CL->chunk;
1428                   (CL->L)=vrealloc ( (CL->L),CL->entry_len*sizeof (int)*CL->max_L_len+b);
1429                 }
1430             }
1431           
1432             (CL->L)[i]=x;
1433 #endif
1434        }
1435     return x;
1436     }
1437
1438
1439 /*********************************************************************/
1440 /*                                                                   */
1441 /*                        INDEXING FUNCTIONS                         */
1442 /*                                                                   */
1443 /*                                                                   */
1444 /*********************************************************************/
1445
1446 Constraint_list *index_res_constraint_list ( Constraint_list *CL, int field)
1447         {
1448         /*
1449           function documentation: start
1450           Constraint_list *index_res_constraint_list ( Constraint_list *CL, int field)
1451           This function reorganises the content of the CL->L matrix, so that a single look up gives
1452           the constraints associated with any residue
1453           
1454           1-if CL->residue_indexed=1 return
1455                  2-CL->residue_index[Seq X][Res Y of Seq X][0]=Z
1456                  Z=Number of residues matching X(Y)*3+1
1457                  CL->residue_index[Seq X][Res Y of Seq X][0]=Z
1458                  CL->residue_index[Seq X][Res Y of Seq X][c+0]=seq W
1459                  CL->residue_index[Seq X][Res Y of Seq X][c+1]=res V
1460                  CL->residue_index[Seq X][Res Y of Seq X][c+2]=weight W(V) Vs X(Y)               
1461           
1462           NOTE: Works All right with a sequence against itself
1463           NOTE: Any modification of CL->L should result in residue_indexed to be reset    
1464           function documentation: end
1465         */
1466         int a, b, s1, s2, r1, r2, w;
1467
1468
1469
1470
1471
1472           
1473
1474         if ( CL->residue_indexed && CL->residue_field==field);
1475         else
1476            {
1477
1478              if ( CL->residue_index)
1479                   {
1480                     for ( a=0; a< (CL->S)->nseq; a++)
1481                       for ( b=0; b<= (CL->S)->len[a]; b++)
1482                           {
1483                               vfree(CL->residue_index[a][b]);
1484                               CL->residue_index[a][b]=vcalloc_nomemset (1, sizeof (int));
1485                               CL->residue_index[a][b][0]=1;
1486                           }    
1487                   }
1488                else if ( !CL->residue_index)
1489                   {
1490                  
1491
1492                     CL->residue_index=vcalloc_nomemset ( (CL->S)->nseq, sizeof (int**));
1493                     for ( a=0; a< (CL->S)->nseq; a++)
1494                           {
1495                         
1496                             CL->residue_index[a]=vcalloc_nomemset ( ((CL->S)->len[a]+1), sizeof (int*));
1497                               for ( b=0; b<= (CL->S)->len[a]; b++)
1498                                   {
1499                                     CL->residue_index[a][b]=vcalloc_nomemset (1,sizeof (int));
1500                                   CL->residue_index[a][b][0]=1;
1501                                   }
1502                           }
1503                   }
1504                for (a=0;a<CL->ne; a++)
1505                   {
1506                     
1507                   s1=vread_clist (CL, a, SEQ1);
1508                   s2=vread_clist (CL, a, SEQ2);
1509                   r1=vread_clist (CL, a, R1);
1510                   r2=vread_clist (CL, a, R2);
1511                   w=vread_clist (CL, a, field);
1512                   
1513         
1514                   CL->residue_index[s1][r1][0]+=3;
1515                   CL->residue_index[s1][r1]=vrealloc ( CL->residue_index[s1][r1], CL->residue_index[s1][r1][0]*sizeof (int));
1516                   CL->residue_index[s1][r1][CL->residue_index[s1][r1][0]-3]=s2;
1517                   CL->residue_index[s1][r1][CL->residue_index[s1][r1][0]-2]=r2;
1518                   CL->residue_index[s1][r1][CL->residue_index[s1][r1][0]-1]=w;
1519
1520                   CL->residue_index[s2][r2][0]+=3;
1521                   CL->residue_index[s2][r2]=vrealloc ( CL->residue_index[s2][r2], CL->residue_index[s2][r2][0]*sizeof (int));
1522                   CL->residue_index[s2][r2][CL->residue_index[s2][r2][0]-3]=s1;
1523                   CL->residue_index[s2][r2][CL->residue_index[s2][r2][0]-2]=r1;
1524                   CL->residue_index[s2][r2][CL->residue_index[s2][r2][0]-1]=w;
1525                   
1526                   }
1527            CL->residue_indexed=1;
1528            CL->residue_field=field;
1529            }
1530         return CL;
1531         }
1532       
1533 Constraint_list *index_constraint_list ( Constraint_list *CL)
1534         {
1535           /*
1536           Function Documentation: start
1537           Constraint_list *index_constraint_list ( Constraint_list *CL);
1538           Indexes a constraint list
1539              1-Checks the flag seq_indexed
1540              2-if flag set to 0
1541                CL->start_index[seq1][seq2] indicatse the first position for seq1 Vs seq2
1542                CL->end_index[seq1][seq3]   indicatse the last  position for seq1 Vs seq2
1543           Any modif to CL->L should cause the flag 1 to be set to 0;
1544           Function Documentation: end
1545         */
1546         int a, csA, csB, sA, sB;
1547
1548
1549         if ( CL->seq_indexed);
1550         else
1551             {
1552             if ( CL->start_index!=NULL)free_int ( CL->start_index,-1);
1553             CL->start_index=declare_int ( (CL->S)->nseq, (CL->S)->nseq);
1554             
1555             if ( CL->end_index!=NULL)free_int ( CL->end_index,-1);
1556             CL->end_index=declare_int ( (CL->S)->nseq, (CL->S)->nseq);
1557             
1558             csA=vread_clist (CL, 0, SEQ1);
1559             csB=vread_clist (CL, 0, SEQ2);
1560
1561             CL->start_index[csA][csB]=0;
1562             CL->start_index[csB][csA]=0;
1563             for ( a=1; a<CL->ne; a++)
1564                 {
1565                 sA=vread_clist (CL, a, SEQ1);
1566                 sB=vread_clist (CL, a, SEQ2);
1567                 if (sA!=csA || sB!=csB)
1568                    {
1569                    CL->end_index[csA][csB]=a;
1570                    CL->end_index[csB][csA]=a;
1571                    csA=sA;
1572                    csB=sB;
1573                    CL->start_index[csA][csB]=a;
1574                    CL->start_index[csB][csA]=a;
1575                    }
1576                 }               
1577             CL->end_index[csB][csA]=CL->ne;
1578             CL->end_index[csA][csB]=CL->ne;
1579             CL->seq_indexed=1;
1580             }
1581         return CL;
1582         }
1583
1584
1585 char ** reindex_constraint_list (char **profile, int np,char **list, int *inL, Sequence *S)
1586 {
1587   int a, nl=0, l;
1588   int ***cache, **index, *entry;
1589   char **nlist, **cons, *nlib_name;
1590   Sequence *NS;
1591   Alignment **P, *A;
1592   Constraint_list *CL, *NCL;
1593   
1594
1595   l=inL[0];
1596   /*1: Pre-Process the profiles*/
1597   cache=vcalloc (np, sizeof (int**));
1598   cons=vcalloc  (np, sizeof (char*));
1599   P=vcalloc (np, sizeof (Alignment *));
1600
1601   for ( a=0; a< np; a++)
1602     {
1603       int e,b,c, **ca;
1604       A=P[a]=main_read_aln (profile[a], NULL);
1605       
1606       ca=cache[a]=declare_int (A->nseq, A->len_aln+1);
1607       cons[a]=aln2cons_seq_mat ( A, "blosum62mt");
1608       for (b=0;b<A->nseq; b++)
1609         {
1610           for (e=0,c=0; c<A->len_aln; c++)
1611             {
1612               if (is_gap(A->seq_al[b][c]));
1613               else ca[b][++e]=c+1;
1614             }
1615         }
1616     }
1617
1618   /*2: Index The Sequences*/
1619   index=declare_int (S->nseq,2);
1620   for (a=0; a<S->nseq; a++)index[a][0]=index[a][1]=-1;
1621   for (a=0; a< S->nseq; a++)
1622     {
1623       char *name;
1624       int p, b;
1625       name=S->name[a];
1626       for (b=0; b< np; b++)
1627         {
1628           if ((p=name_is_in_list(name, (P[b])->name, (P[b])->nseq, MAXNAMES))!=-1) 
1629             {
1630               index[a][0]=b;
1631               index[a][1]=p;
1632             }
1633         }
1634     }
1635
1636   /*3: Read the primary Library*/
1637   
1638   nlist=declare_char (read_array_size_new ((void *)list), read_array_size_new ((void *)list[0]));
1639   for (nl=0,a=0; a< l; a++)
1640     if ( list[a][0]=='L' || list[a][0]=='A')
1641       nlist[nl++]=list[a];
1642   
1643   CL=declare_constraint_list ( S,NULL, NULL, 0,NULL, NULL);
1644   CL=read_n_constraint_list (nlist,nl,NULL,NULL,"sim",S->type,stdout, CL, "S");   
1645   vfree (nlist);
1646   
1647   NS=fill_sequence_struc (np, cons, profile);
1648   NCL=declare_constraint_list ( NS,NULL, NULL, 0,NULL, NULL);
1649   entry=vcalloc ( CL->entry_len, CL->el_size);
1650   for (a=0; a<CL->ne; a++)
1651     {
1652       int s1, s2, r1, r2, ps1, ps2, pps1, pps2;
1653
1654       entry=extract_entry (entry, a, CL);
1655       s1=entry[SEQ1]; s2=entry[SEQ2];
1656       r1=entry[R1]; r2=entry[R2];
1657       ps1=index[s1][0];ps2=index[s2][0];
1658       pps1=index[s1][1];pps2=index[s2][1];
1659
1660       if (ps1==ps2 || ps1==-1 || ps2==-1)continue;
1661       entry[SEQ1]=ps1;
1662       entry[SEQ2]=ps2;
1663     
1664       entry[R1]=cache[ps1][pps1][r1];
1665       entry[R2]=cache[ps2][pps2][r2];
1666     
1667       add_entry2list (entry, NCL);
1668       
1669     }
1670
1671   compact_list (NCL, 0, NCL->ne, "default");
1672   nlib_name=vtmpnam(NULL);
1673   vfclose(save_constraint_list ( NCL, 0, NCL->ne,nlib_name, NULL, "lib",NCL->S));
1674   
1675   
1676   nlist=declare_char (read_array_size_new ((void *)list), read_array_size_new ((void *)list[0]));
1677   for (nl=0,a=0; a< l; a++)
1678     if ( list[a][0]!='L' && list[a][0]!='A')
1679       sprintf (nlist[nl++], "%s",list[a]);
1680   sprintf (nlist[nl++], "L%s", nlib_name);
1681   
1682   free_arrayN ((void *)cache, 3);
1683   free_arrayN ((void *)cons, 2);
1684   free_arrayN ((void *)index, 2);
1685   vfree (entry);
1686   for (a=0;a<np; a++)free_aln (P[a]);
1687   vfree (P);
1688   free_constraint_list_full (CL);
1689   free_constraint_list_full (NCL);
1690   
1691   inL[0]=nl;
1692   return nlist;
1693 }
1694   
1695
1696 Constraint_list * progressive_index_res_constraint_list ( Alignment *A, int *ns, int **ls, Constraint_list *CL)
1697 {
1698   int **index_seq;
1699   int **index_res;  
1700   static Constraint_list *NCL;
1701   
1702   int a, b, c, d,s,l, max_len;
1703   int s1, s2, r1, r2;
1704   int *L, *NL;
1705   char *seq;
1706   Sequence *S, *NS;
1707
1708
1709   
1710   if ( NCL==NULL)
1711     {
1712       NCL=duplicate_constraint_list (CL);
1713     }
1714   
1715   S=CL->S;
1716   NS=NCL->S;
1717   
1718   L=CL->L;
1719   NL=NCL->L;
1720   
1721   
1722   
1723   if (NCL->residue_index)
1724     {
1725       free_arrayN((void*)NCL->residue_index, 3);
1726       NCL->residue_index=NULL;
1727     }
1728
1729   for ( a=0; a< S->nseq; a++)
1730     {
1731       sprintf ( NS->seq[a], "%s", S->seq[a]);
1732       NS->len[a]=strlen (S->seq[a]);
1733     }
1734   
1735   
1736   for ( a=0; a<2; a++)
1737     {
1738       s=A->order[ls[a][0]][0];
1739       vfree (NS->seq[s]);
1740       NS->seq[s]=sub_aln2cons_seq_mat (A, ns[a], ls[a], "blosum62mt");
1741       NS->len[s]=strlen (NS->seq[s]);
1742     }
1743   for ( max_len=0, a=0; a<S->nseq; a++)max_len=MAX(max_len, S->len[a]);
1744   index_seq=declare_int ( NS->nseq, max_len+1);
1745   index_res=declare_int ( NS->nseq, max_len+1);
1746   
1747
1748   for ( a=0; a< 2; a++)
1749     {
1750       for (b=0; b< ns[a]; b++)
1751         {
1752           s=A->order[ls[a][b]][0];
1753           seq=A->seq_al[ls[a][b]];
1754           l=strlen (seq);
1755
1756           for ( d=0, c=0; c<l; c++)
1757             {
1758               if ( !is_gap(seq[c]))
1759                 {
1760                   d++;
1761                   
1762                   index_res[s][d]=c+1;
1763                   index_seq[s][d]=A->order[ls[a][0]][0];
1764                 }
1765             }
1766         }
1767     }
1768
1769   for ( a=0; a< CL->ne; a++)
1770     {
1771       s1=L[a*CL->entry_len+SEQ1];
1772       s2=L[a*CL->entry_len+SEQ2];
1773       r1=L[a*CL->entry_len+R1];
1774       r2=L[a*CL->entry_len+R2];
1775       
1776       if ( index_res[s1][r1])
1777         {
1778           NL[a*CL->entry_len+SEQ1]=index_seq[s1][r1];
1779           NL[a*CL->entry_len+R1]=index_res[s1][r1];
1780         }
1781       else
1782         {
1783           NL[a*CL->entry_len+SEQ1]=s1;
1784           NL[a*CL->entry_len+R1]=r1;
1785         }
1786       
1787       if ( index_res[s2][r2])
1788         {
1789           NL[a*CL->entry_len+SEQ2]=index_seq[s2][r2];
1790           NL[a*CL->entry_len+R2]=index_res[s2][r2];
1791         }
1792       else
1793         {
1794           NL[a*CL->entry_len+SEQ2]=s2;
1795           NL[a*CL->entry_len+R2]=r2;
1796         }
1797     }
1798
1799   NCL->ne=CL->ne;
1800   NCL->residue_indexed=0;
1801   
1802   NCL=compact_list (NCL, 0, NCL->ne, "best");
1803   
1804   NCL=index_res_constraint_list (NCL, WE);
1805   
1806   free_int ( index_res, -1); free_int (index_seq, -1);
1807   
1808   return NCL;
1809 }
1810 /*********************************************************************/
1811 /*                                                                   */
1812 /*                         LIST EXTENTION                            */
1813 /*                                                                   */
1814 /*                                                                   */
1815 /*********************************************************************/
1816 Constraint_list *extend_list_pair (Constraint_list *CL,char *store_mode, int s1, int s2)
1817         {
1818         static Sequence *S;
1819         Constraint_list *CLout;
1820         /*
1821           function documentation: start
1822           Constraint_list *extend_list_pair (Constraint_list *CL,char *store_mode, int s1, int s2)
1823           This function takes a pair of sequences s1, s2 and perrforms the extention
1824           It returns the incoming list CL, with CL->L[s1][s2] now extended
1825           See main documentation for store_mode
1826           function documentation: end
1827         */
1828
1829         if ( S==NULL)S=declare_sequence ((CL->S)->min_len, (CL->S)->max_len,(CL->S)->nseq);     
1830         sprintf ( S->name[0], "%s",(CL->S)->name[s1]);
1831         sprintf ( S->name[1],"%s",(CL->S)->name[s2]);
1832         S->nseq=2;
1833         
1834         CLout=extend_list (CL, store_mode, CL->extend_clean_mode, CL->extend_compact_mode,CL->do_self, S);
1835         return CLout;           
1836         }
1837 Constraint_list *extend_list (Constraint_list *CLin, char *store_mode,char *clean_mode, char *compact_mode,int do_self, Sequence *DO_LIST)
1838         {
1839         int a, b, c, d, e, f;
1840         int wA, wC,w, rA, rC, miscA, miscC, misc;
1841         static int **posA;
1842         static int **posC;
1843         int start_ba, end_ba, start_bc, end_bc, start_ac, end_ac;
1844         int len;
1845         int lenA=0;
1846         int lenC=0;
1847         int *translation;
1848         Constraint_list *CLout=NULL;
1849
1850
1851         /*Do not extend if the List is a Matrix*/
1852         if ( !CLin->L && CLin->M)
1853            {
1854            CLin->extend_jit=0;
1855            return CLin;
1856            }
1857         
1858         translation=vcalloc ( (CLin->S)->nseq, sizeof (int));
1859         for ( a=0; a<(CLin->S)->nseq; a++)
1860             {
1861             translation[a]=name_is_in_list ((CLin->S)->name[a],DO_LIST->name, DO_LIST->nseq, 100);
1862             translation[a]++;/* set translation to -1+1=0 if seq not in list*/
1863             }
1864         
1865         CLout=declare_constraint_list (CLin->S, NULL,NULL,0, strm("disk", store_mode)?tmpfile():NULL, NULL);
1866                 
1867         for ( a=0; a<(CLin->S)->nseq-(1-do_self); a++)
1868                 {               
1869                 fprintf (CLin->local_stderr, "\nSeq %3d: %5d", a+1,CLout->ne);
1870                 for ( c=a+(1-do_self); c<(CLin->S)->nseq; c++)
1871                         {
1872                         if ( translation[a] && translation[c])
1873                                {                               
1874                                get_bounds (CLin,a, c, &start_ac, &end_ac);                                                    
1875                                for ( d=start_ac; d<end_ac; d++)
1876                                    {
1877                                    for ( e=0; e< CLin->entry_len; e++)
1878                                        vwrite_clist(CLout,CLout->ne, e, vread_clist(CLin,d, e));
1879                                    CLout->ne++;
1880                                    }
1881                                
1882                                for ( b=0; b<(CLin->S)->nseq; b++)
1883                                    {
1884                                    len=strlen ( (CLin->S)->seq[b]);
1885                                    
1886                                    get_bounds (CLin,b, a, &start_ba, &end_ba);  
1887                                    posA=fill_pos_matrix (CLin,start_ba, end_ba, len, posA, &lenA,(b>a));
1888                                    
1889                                    if ((c!=b && a!=b) ||(do_self==1))
1890                                        {
1891                                    
1892                                        get_bounds (CLin, b, c, &start_bc, &end_bc);
1893                                        posC=fill_pos_matrix (CLin, start_bc, end_bc, len, posC, &lenC, (b>c));
1894                                        
1895                                        for (d=1; d<=len; d++)
1896                                            {
1897                                            if ( posA[d][1]==0 || posC[d][1]==0);
1898                                            else
1899                                                {
1900                                                for (e=2; e<=posA[d][1]+1; e+=(CLin->entry_len-4)) 
1901                                                    for ( f=2; f<=posC[d][1]+1; f+=(CLin->entry_len-4))
1902                                                        {
1903                                                        wA   =posA[d][e+1];
1904                                                        miscA=posA[d][e+2];
1905
1906                                                        wC   =posC[d][f+1];
1907                                                        miscC=posC[d][f+2];
1908
1909                                                        rA=posA[d][e];
1910                                                        rC=posC[d][f];
1911                                                        
1912                                                        w   =MIN(wA,wC);
1913                                                        
1914                                                        misc=MAX(miscA, miscC);
1915                                                        
1916                                                        vwrite_clist( CLout, CLout->ne, SEQ1, a);
1917                                                        vwrite_clist( CLout, CLout->ne, SEQ2, c);
1918                                                        vwrite_clist( CLout, CLout->ne, R1  ,rA);
1919                                                        vwrite_clist( CLout, CLout->ne, R2  ,rC);
1920                                                        vwrite_clist( CLout, CLout->ne, WE  , w);
1921                                                        vwrite_clist( CLout, CLout->ne, CONS, 1);
1922                                                        vwrite_clist( CLout, CLout->ne, MISC,misc);
1923                                                        CLout->ne++;
1924                                                        }
1925                                                }
1926                                            }
1927                                        }
1928                                    }
1929                 
1930                                CLout=compact_list (CLout,0,CLout->ne,"mirror_sum");
1931                                CLout=clean ( clean_mode,CLout, 0, CLout->ne);
1932                                }
1933                         }
1934                 }
1935
1936         
1937         vfree (translation);
1938         return CLout;
1939         }
1940 void get_bounds (Constraint_list *CL, int s1, int s2, int *start, int *end)
1941         {
1942
1943         CL=index_constraint_list (CL);
1944         
1945         if ( s1>s2)SWAP(s1, s2);
1946         
1947         start[0]=CL->start_index[s1][s2];
1948         end  [0]=CL->end_index  [s1][s2];
1949         }
1950
1951         
1952 int ** fill_pos_matrix (Constraint_list *CL, int beg, int end, int slen, int **pos, int *len, int mirrored)
1953         {
1954         int small_chunck;
1955         int a, r1,r2;
1956         
1957
1958         small_chunck=2*CL->entry_len;
1959
1960         if ( pos==NULL)
1961                 {
1962                 pos=declare_int (slen+1, small_chunck);
1963                 for ( a=0; a<=slen; a++)pos[a][0]=small_chunck;
1964                 len[0]=slen+1;
1965                 }
1966         else if ( len[0]<=slen)
1967                 {
1968                 free_int ( pos, len[0]);
1969                 pos=declare_int (slen+1, small_chunck);
1970                 for ( a=0; a<=slen; a++)pos[a][0]=small_chunck;
1971                 len[0]=slen+1;
1972                 }
1973         else
1974                 {
1975                 for ( a=0; a<=slen; a++)pos[a][1]=0;
1976                 }
1977                         
1978                         
1979         
1980         
1981         for ( a=beg; a<end; a++)
1982                 {
1983                 
1984                 if (!mirrored)     {r1=vread_clist (CL, a, R1);r2=vread_clist (CL, a, R2);}
1985                 else if ( mirrored){r1=vread_clist (CL, a, R2);r2=vread_clist (CL, a, R1);}
1986
1987                if ( ((pos[r1][1])+(CL->entry_len))>pos[r1][0])
1988                         {
1989                         pos[r1]=vrealloc (pos[r1], (pos[r1][0]+small_chunck)*sizeof (int));
1990                         pos[r1][0]+=small_chunck;
1991                         }
1992                 pos[r1][pos[r1][1]+2]=r2;
1993                 pos[r1][pos[r1][1]+3]=vread_clist(CL,a,WE);
1994                 pos[r1][pos[r1][1]+4]=vread_clist(CL,a,MISC);
1995                 pos[r1][1]+=(CL->entry_len-4);          
1996                 }
1997         return pos;
1998         }
1999 Constraint_list * evaluate_constraint_list_reference ( Constraint_list *CL)
2000         {
2001             static CLIST_TYPE *entry;
2002             int a, b, c, s1, s2, r1, r2, w;
2003             int ***max_res;
2004             
2005             
2006
2007             if ( CL->M)
2008               {
2009                 CL->max_value=CL->max_ext_value=20;
2010                 
2011               }
2012             else 
2013               {
2014
2015                 CL->max_value=CL->max_ext_value=0;
2016                 max_res=vcalloc ( (CL->S)->nseq, sizeof (int**));
2017                         
2018                 for ( a=0; a< (CL->S)->nseq; a++)
2019                   {
2020                     max_res[a]=vcalloc ( strlen ((CL->S)->seq[a])+1, sizeof (int*));
2021                     for ( b=0; b<=(CL->S)->len[a]; b++)
2022                       {
2023                       max_res[a][b]=vcalloc ( (CL->S)->nseq+1, sizeof (int));
2024                       }
2025                   }
2026                 
2027                 for ( a=0; a< CL->ne; a++)
2028                   {
2029                     entry=extract_entry ( entry, a, CL);
2030                     s1=entry[SEQ1];
2031                     s2=entry[SEQ2];
2032                     r1=entry[R1];
2033                     r2=entry[R2];
2034                     w= entry[WE];
2035                     if ( w==UNDEFINED || ( (CL->moca) && (CL->moca)->forbiden_residues  && ((CL->moca)->forbiden_residues[s1][r1]==UNDEFINED || (CL->moca)->forbiden_residues[s2][r2]==UNDEFINED)));
2036                     else
2037                       {
2038                         
2039                         max_res[s1][r1][s2]+=w;
2040                         max_res[s2][r2][s1]+=w;
2041                         CL->max_value=MAX(w, CL->max_value);
2042                       }
2043                   }
2044                 
2045                 for ( a=0; a< (CL->S)->nseq; a++)
2046                   for ( b=1; b<=(CL->S)->len[a]; b++)
2047                     {
2048                       for ( c=0; c< (CL->S)->nseq; c++)
2049                         {
2050                           max_res[a][b][(CL->S)->nseq]+= max_res[a][b][c];
2051                         }
2052                       CL->max_ext_value=MAX(max_res[a][b][c],CL->max_ext_value);
2053                     }           
2054                 
2055                 for ( a=0; a< (CL->S)->nseq; a++)
2056                   {
2057                     for ( b=0; b<=(CL->S)->len[a]; b++)
2058                       vfree ( max_res[a][b]);
2059                     vfree (max_res[a]);
2060                   }
2061                 CL->max_ext_value=MAX(1,CL->max_ext_value);
2062                 vfree ( max_res);
2063               }
2064             
2065             if (CL->normalise)
2066               {
2067                 
2068                 CL->nomatch=(CL->nomatch*CL->normalise)/CL->max_ext_value;
2069               }
2070             
2071             return CL;
2072         }
2073                                
2074 /*********************************************************************/
2075 /*                                                                   */
2076 /*                         ENTRY MANIPULATION                        */
2077 /*                                                                   */
2078 /*                                                                   */
2079 /*********************************************************************/
2080 Constraint_list * add_list_entry2list (Constraint_list *CL, int n_para, ...)
2081         {
2082         int a;
2083         int *entry;
2084         int field, val;
2085         va_list ap;
2086
2087         if (n_para>LIST_N_FIELDS)
2088            {
2089                crash ("Too Many Fields in List [FATAL/add_list_entry2list]");
2090            }
2091         
2092         va_start (ap,n_para);
2093         entry=vcalloc (CL->entry_len, sizeof (int));
2094
2095         for ( a=0; a<n_para; a++)
2096             {
2097             field=va_arg(ap, int);
2098             val  =va_arg(ap, CLIST_TYPE);
2099             entry[field]=val;
2100             }
2101         va_end (ap);
2102         add_entry2list(entry, CL);
2103         vfree(entry);
2104         return CL;
2105         }
2106
2107 Constraint_list *add_entry2list ( CLIST_TYPE *entry, Constraint_list *CL)
2108         {
2109           return insert_entry2list (entry, CL->ne++, CL);
2110         }
2111 Constraint_list* insert_entry2list(CLIST_TYPE * entry, int pos, Constraint_list *CL)
2112         {
2113         int a;
2114         for ( a=0; a< CL->entry_len; a++)
2115             vwrite_clist ( CL,pos, a,entry[a]);
2116         return CL;
2117         }
2118 CLIST_TYPE* extract_entry(CLIST_TYPE * entry, int pos, Constraint_list *CL)
2119         {
2120         int a;
2121         
2122         if ( entry==NULL)entry=vcalloc ( CL->entry_len, CL->el_size);
2123         
2124         for (a=0; a< CL->entry_len; a++)entry[a]=vread_clist(CL, pos, a);
2125         return entry;
2126         }
2127
2128         
2129 /*********************************************************************/
2130 /*                                                                   */
2131 /*                         SEARCH IN LIST (ARRAY AND FILE)           */
2132 /*                                                                   */
2133 /*                                                                   */
2134 /*********************************************************************/
2135 FILE * compare_list (FILE *OUT, Constraint_list *CL1,Constraint_list *CL2)
2136         {
2137         int a;
2138         float nw_score=0;
2139         float w_score=0;
2140         int *l;
2141
2142         CLIST_TYPE  *entry=NULL;
2143         int p;
2144         
2145         entry_len=CL1->entry_len;
2146         qsort ( (void *)CL1->L, CL1->ne, CL1->entry_len*sizeof (int), compare_constraint_list_entry);
2147         qsort ( (void *)CL2->L, CL2->ne, CL2->entry_len*sizeof (int), compare_constraint_list_entry);
2148         
2149         entry=vcalloc ( CL1->entry_len, CL1->el_size);
2150         for ( a=0; a<CL1->ne; a++)
2151                 {
2152                 entry=extract_entry (entry,a,CL1);
2153                 if ((l=main_search_in_list_constraint (entry,&p,4,CL2))!=NULL)
2154                         {
2155                         vwrite_clist ( CL2, p,MISC, 1);
2156                         vwrite_clist ( CL1, a,MISC, 1);
2157                         nw_score++;
2158                         w_score+=l[WE];
2159                         }
2160                 }
2161         fprintf ( OUT, "%-15s:%d pairs (Evaluated matrix), %d pairs in the other (%s)\n", CL2->list_name, CL2->ne, CL1->ne, CL1->list_name);
2162         fprintf ( OUT, "%-15s:%d pairs\n", CL1->list_name, CL1->ne);
2163         fprintf ( OUT, "Acurracy=%.2f%%\n", (nw_score/CL1->ne)*MAXID);
2164         fprintf ( OUT, "Sensitiv=%.2f%%\n\n", (nw_score/CL2->ne)*MAXID);
2165         return OUT;
2166         }
2167
2168
2169 CLIST_TYPE *main_search_in_list_constraint ( int *key,int *p,int k_len,Constraint_list *CL)
2170         {
2171           
2172         
2173           CLIST_TYPE *l=NULL;
2174           int start, end;
2175
2176           CL=index_constraint_list (CL);
2177           
2178           start=CL->start_index[key[SEQ1]][key[SEQ2]];
2179           end  =CL->end_index  [key[SEQ1]][key[SEQ2]];
2180           
2181         
2182                   
2183           entry_len=CL->entry_len;
2184           l=bsearch (key, (CL->L)+(start*CL->entry_len), (end-start), sizeof (int)*CL->entry_len,  compare_constraint_list_entry4bsearch);
2185           p[0]=CL->L-l;
2186           return l;
2187         }
2188 CLIST_TYPE return_max_constraint_list ( Constraint_list *CL, int field)
2189         {
2190         CLIST_TYPE max=0;
2191         int a;
2192         for ( a=0; a< CL->ne; a++)max=MAX( vread_clist(CL,a,field), max);
2193         return max;
2194         }
2195         
2196  /*********************************************************************/
2197 /*                                                                   */
2198 /*                                                                   */
2199 /*      LIST SORTING                                                 */
2200 /*                                                                   */
2201 /*                                                                   */
2202 /*                                                                   */
2203 /*********************************************************************/
2204 Constraint_list *sort_constraint_list_inv (Constraint_list *CL, int start, int len)
2205         {
2206         CL=sort_constraint_list   (CL, start,len);
2207         
2208
2209         CL=invert_constraint_list (CL, start,len);
2210         if ( start+len==CL->ne)
2211             {      
2212             while (vread_clist(CL,CL->ne-1, SEQ1)==-1)CL->ne--;
2213             }
2214         
2215
2216         return CL;
2217         }
2218
2219 Constraint_list *invert_constraint_list (Constraint_list *CL, int start,int len)
2220         {
2221         int a, b, c;
2222         CLIST_TYPE tp;
2223         
2224         
2225         for ( a=start, b=start+len-1; a<=b; a++, b--)
2226             {
2227             for (c=0; c< CL->entry_len; c++)
2228                 {
2229                 tp=vread_clist(CL, a, c);
2230                 vwrite_clist(CL,a, c, vread_clist(CL, b, c));
2231                 vwrite_clist(CL,b, c, tp);
2232                 }
2233             }
2234         return CL;
2235         }
2236         
2237 Constraint_list * sort_constraint_list(Constraint_list *CL, int start, int len)
2238         {
2239
2240         CL=sort_constraint_list_on_n_fields (CL, start, len, 0, CL->entry_len);
2241
2242         return CL;
2243         }
2244
2245 Constraint_list * sort_constraint_list_on_n_fields (Constraint_list *CL, int start, int len, int first_field, int n_fields)
2246         {
2247           entry_len=CL->entry_len;
2248           
2249           if (CL->fp)
2250             {      
2251               rewind( CL->fp);
2252               fseek      ( CL->fp, start*CL->el_size*CL->entry_len , SEEK_SET);
2253               hsort_list_file ( CL->fp,        len, CL->el_size, CL->entry_len,first_field,n_fields);
2254             }
2255           else if ( CL->L)
2256             {
2257               qsort ( (void *)CL->L, CL->ne, CL->entry_len*sizeof (int), compare_constraint_list_entry);
2258               //hsort_list_array ((void**)(CL->L)+start, len, CL->el_size, CL->entry_len,first_field,n_fields);
2259             }
2260           return CL;
2261         }
2262 int compare_constraint_list_entry4bsearch ( const void*vx, const void*vy)
2263 {
2264   int a;
2265   const int *x=vx, *y=vy;
2266   for (a=SEQ1; a<=R2; a++)
2267     {
2268       if (x[a]<y[a])return -1;
2269       else if (x[a]>y[a]) return 1;
2270     }
2271   return 0;
2272 }
2273 int compare_constraint_list_entry ( const void*vx, const void*vy)
2274 {
2275   int a;
2276   const int *x=vx, *y=vy;
2277   
2278   for (a=SEQ1; a<=WE; a++)
2279     {
2280       if (x[a]<y[a])return -1;
2281       else if (x[a]>y[a]) return 1;
2282     }
2283   return 0;
2284 }
2285 /*********************************************************************/
2286 /*                                                                   */
2287 /*                         LIST PARSING                              */
2288 /*                                                                   */
2289 /*                                                                   */
2290 /*********************************************************************/ 
2291 Constraint_list*  fork_read_n_constraint_list(char **fname,int n_list, char *in_mode,char *mem_mode,char *weight_mode, char *type,FILE *local_stderr, Constraint_list *CL, char *seq_source);
2292 Constraint_list* nfork_read_n_constraint_list(char **fname,int n_list, char *in_mode,char *mem_mode,char *weight_mode, char *type,FILE *local_stderr, Constraint_list *CL, char *seq_source);
2293
2294 Constraint_list* read_n_constraint_list(char **fname,int n_list, char *in_mode,char *mem_mode,char *weight_mode, char *type,FILE *local_stderr, Constraint_list *CL, char *seq_source)
2295 {
2296   
2297   if ( get_nproc()==1 || n_list<=2)return nfork_read_n_constraint_list(fname,n_list, in_mode,mem_mode,weight_mode,type,local_stderr, CL, seq_source);
2298   else if ( strstr (CL->multi_thread, "methods"))
2299     return fork_read_n_constraint_list(fname,n_list, in_mode,mem_mode,weight_mode,type,local_stderr, CL, seq_source);
2300   else
2301     return nfork_read_n_constraint_list(fname,n_list, in_mode,mem_mode,weight_mode,type,local_stderr, CL, seq_source);
2302 }
2303 Constraint_list* fork_read_n_constraint_list(char **fname,int n_list, char *in_mode,char *mem_mode,char *weight_mode, char *type,FILE *local_stderr, Constraint_list *CL, char *seq_source)
2304     {
2305     int a, b;
2306     Sequence *S;
2307     char **tmp_list;
2308     int*proclist;
2309     int nproc, ns;
2310
2311     nproc=get_nproc();
2312     
2313     proclist=vcalloc (65536, sizeof (int));
2314     tmp_list=vcalloc (n_list+1, sizeof (char*));
2315     for (a=0; a<n_list; a++)tmp_list[a]=vtmpnam(NULL);
2316     
2317     if (!(CL->S) && (S=read_seq_in_n_list (fname, n_list,type,seq_source))==NULL)
2318         {
2319         fprintf ( stderr, "\nNO SEQUENCE WAS SPECIFIED[FATAL]\n");
2320         myexit(EXIT_FAILURE);
2321         }
2322     else if (CL->S==NULL)
2323       {
2324         CL->S=S;
2325       }
2326    
2327    /*CHECK IF THERE IS A MATRIX AND GET RID OF OTHER METHODS*/
2328     for (b=0, a=0; a< n_list; a++)if (is_matrix(fname[a]) ||is_matrix(fname[a]+1) )b=a+1;
2329
2330     if ( b)
2331        {
2332         if ( b==1);
2333         else sprintf ( fname[0], "%s", fname[b-1]);
2334         n_list=1;
2335         return nfork_read_n_constraint_list(fname,n_list, in_mode,mem_mode,weight_mode, type,local_stderr, CL, seq_source);
2336         }   
2337       
2338     if (!CL)CL=declare_constraint_list ( S,NULL, NULL, 0,(strm(mem_mode, "disk"))?tmpfile():NULL, NULL);    
2339
2340     if (CL->ne)
2341       {
2342         constraint_list2raw_file(CL,tmp_list[n_list], "w");
2343         CL->ne=0;
2344       }
2345
2346     CL->local_stderr=local_stderr;
2347     fprintf ( local_stderr, "\n\tMulti Core Mode: %d processors:\n", nproc);
2348     for (ns=0,a=0; a< n_list; a++)
2349       {
2350         int pid;
2351         ns++;
2352         pid=vfork ();   
2353         if ( pid==0)
2354           {
2355             int in;
2356             initiate_vtmpnam (NULL);
2357             CL->local_stderr=vfopen("/dev/null", "w");
2358             in=CL->ne;
2359             CL=read_constraint_list (CL, fname[a], in_mode, mem_mode,weight_mode);      
2360             if (CL->ne>in)constraint_list2raw_file (CL,tmp_list[a], "w");
2361             exit (EXIT_SUCCESS);
2362           }
2363         else
2364           {
2365           
2366             set_pid (pid);
2367             fprintf ( local_stderr, "\n\t--- Process Method/Library/Aln %s", fname[a], ns); 
2368             proclist[pid]=a;
2369             if (ns>=nproc)
2370               {
2371                 b=proclist[vwait(NULL)];
2372                 fprintf (local_stderr, "\n\txxx Retrieved %s",fname[a]);
2373                 if (tmp_list[b] && check_file_exists (tmp_list[b]))
2374                   {
2375                     CL=read_constraint_list_raw_file (CL,tmp_list[b]);
2376                     compact_list (CL, 0, CL->ne, (strm (weight_mode, "cons")?"cons":"default"));
2377                   }
2378                 ns--;
2379               }
2380           }
2381       }
2382     
2383     while (ns)
2384       {
2385         int pid2;
2386         pid2=vwait(NULL);
2387         a=proclist[pid2];
2388         fprintf (local_stderr, "\n\txxx Retrieved %s",fname[a]);
2389         if (tmp_list[a] && check_file_exists (tmp_list[a]))
2390           {
2391             CL=read_constraint_list_raw_file (CL,tmp_list[a]);
2392             compact_list (CL, 0, CL->ne, (strm (weight_mode, "cons")?"cons":"default"));
2393           }
2394         ns--;
2395       }
2396     fprintf ( local_stderr, "\n\n\tAll Methods Retrieved\n");
2397     
2398     if (tmp_list[n_list] && check_file_exists (tmp_list[n_list]))
2399           {
2400             CL=read_constraint_list_raw_file (CL,tmp_list[n_list]);
2401             compact_list (CL, 0, CL->ne, (strm (weight_mode, "cons")?"cons":"default"));
2402           }
2403     
2404     CL->local_stderr=local_stderr;
2405     CL=evaluate_constraint_list_reference (CL);
2406     vfree (proclist);
2407     vfree (tmp_list);
2408     return CL;
2409     }
2410 Constraint_list* nfork_read_n_constraint_list(char **fname,int n_list, char *in_mode,char *mem_mode,char *weight_mode, char *type,FILE *local_stderr, Constraint_list *CL, char *seq_source)
2411     {
2412     int a, b;
2413     Sequence *S;
2414
2415     
2416     if (!(CL->S) && (S=read_seq_in_n_list (fname, n_list,type,seq_source))==NULL)
2417         {
2418         fprintf ( stderr, "\nNO SEQUENCE WAS SPECIFIED[FATAL]\n");
2419         myexit(EXIT_FAILURE);
2420         }
2421     else if (CL->S==NULL)
2422       {
2423         CL->S=S;
2424       }
2425    
2426    /*CHECK IF THERE IS A MATRIX AND GET RID OF OTHER METHODS*/
2427     for (b=0, a=0; a< n_list; a++)if (is_matrix(fname[a]) ||is_matrix(fname[a]+1) )b=a+1;
2428
2429     if ( b)
2430        {
2431         if ( b==1);
2432         else sprintf ( fname[0], "%s", fname[b-1]);
2433         n_list=1;
2434
2435         }   
2436     
2437     if (!CL)CL=declare_constraint_list ( S,NULL, NULL, 0,(strm(mem_mode, "disk"))?tmpfile():NULL, NULL);    
2438     CL->local_stderr=local_stderr;
2439     fprintf ( CL->local_stderr,"\nREAD/MAKE LIBRARIES:[%d]\n",n_list );
2440
2441     CL=read_constraint_list (CL, fname[0], in_mode, mem_mode,weight_mode);
2442     compact_list (CL, 0, CL->ne, "default");
2443     for ( a=1; a< n_list; a++)
2444         {       
2445         CL=read_constraint_list (CL, fname[a], in_mode, mem_mode,weight_mode);  
2446         if (strm (weight_mode, "cons"))compact_list (CL, 0, CL->ne, "cons");
2447         else
2448           compact_list (CL, 0, CL->ne, "default");
2449         }
2450     CL->local_stderr=local_stderr;
2451     
2452     CL=evaluate_constraint_list_reference (CL);
2453     
2454     return CL;
2455     }
2456 Constraint_list* read_constraint_list(Constraint_list *CL,char *in_fname,char *in_mode, char *mem_mode,char *weight_mode)
2457         {
2458         Sequence *SL=NULL, *TS=NULL;
2459         int a;
2460         Constraint_list *SUBCL=NULL;
2461         static char *read_mode;
2462         char *fname;
2463          
2464         fname=in_fname; 
2465         if ( !read_mode)read_mode=vcalloc ( STRING, sizeof (char));
2466         
2467         if ( is_lib_list (in_fname))sprintf ( read_mode, "lib_list");
2468         else if ( in_mode)sprintf (read_mode, "%s", in_mode);     
2469         else if ( fname[0]=='A'){sprintf ( read_mode, "aln");fname++;}
2470         else if ( fname[0]=='L'){sprintf ( read_mode, "lib");fname++;}
2471         else if ( fname[0]=='M'){sprintf ( read_mode, "method");fname++;}
2472         else if ( fname[0]=='S'){sprintf ( read_mode, "sequence");return CL;}
2473         else if ( fname[0]=='P'){sprintf ( read_mode, "pdb")     ;return CL;}
2474         else if ( fname[0]=='R'){sprintf ( read_mode, "profile") ;return CL;}
2475         else if ( fname[0]=='X'){sprintf ( read_mode, "matrix");++fname;}    
2476         else if ( fname[0]=='W'){sprintf ( read_mode, "structure");fname++;}
2477
2478         else
2479                  {
2480                      fprintf ( stderr, "\nERROR: The descriptor %s could not be identified as a file or a method.[FATAL]\nIf it is a method file please indicate it with M%s\n", fname, fname);
2481                      myexit (EXIT_SUCCESS);
2482                  }
2483
2484         fprintf (CL->local_stderr, "\n\t%s [%s]\n", fname, read_mode);
2485         
2486         
2487         if ( strm (read_mode, "lib_list"))
2488           {
2489             int n, a;
2490             char **l;
2491             l=read_lib_list (fname, &n);
2492             for ( a=0; a<n; a++)
2493               CL=read_constraint_list (CL,l[a],in_mode, mem_mode,weight_mode);
2494             free_char (l,-1);
2495           }
2496         else if (strm(read_mode, "binary"))
2497               {
2498                 fprintf ( stderr, "\nERROR: Library %s: binary mode is not any more supported [FATAL:%s]\n", fname,PROGRAM);
2499                 myexit (EXIT_FAILURE);
2500               }
2501         else if ( strm (fname, "make_test_lib"))
2502           {
2503             CL=make_test_lib (CL);
2504           }
2505         else if ( strm2 (read_mode,"ascii","lib"))
2506               {
2507                 SUBCL=read_constraint_list_file(NULL, fname);
2508               }
2509            else if (strm(read_mode, "method"))
2510               {
2511                 CL=produce_list ( CL, CL->S, fname,weight_mode,mem_mode);
2512               }
2513            else if (strm(read_mode, "matrix"))
2514               {
2515               CL->L=NULL;
2516               CL->extend_jit=0;
2517               CL->M=read_matrice ( fname);
2518               }
2519            else if ( strm ( read_mode, "structure"))
2520                 {
2521                 if ( CL->ne>0)
2522                         {
2523                         fprintf ( stderr, "\nERROR: Wstructure must come before Mmethod or Aaln [FATAL:%s]",PROGRAM);
2524                         myexit (EXIT_FAILURE);
2525                         }
2526                 
2527                 if ( !(CL->STRUC_LIST))
2528                         {
2529                         CL->STRUC_LIST=declare_sequence (1,1,10000);
2530                         (CL->STRUC_LIST)->nseq=0;
2531                         }
2532                 SL=CL->STRUC_LIST;
2533                 
2534                 if ( check_file_exists(fname))
2535                         {
2536                         TS=main_read_seq ( fname);
2537                         for (a=0; a<TS->nseq; a++)sprintf (SL->name[SL->nseq++], "%s", TS->name[a]);
2538                         free_sequence (TS, TS->nseq);
2539                         }
2540                 else
2541                         { 
2542                         sprintf (SL->name[SL->nseq++], "%s", fname);
2543                         }
2544                 }
2545            else if (strm (read_mode, "aln"))
2546               {
2547                 SUBCL=aln_file2constraint_list ( fname,SUBCL,weight_mode);
2548               }
2549            else 
2550               {
2551                 SUBCL=read_constraint_list_file(SUBCL, fname);
2552               }
2553         
2554         if (SUBCL)
2555           {
2556             CL=merge_constraint_list    (SUBCL, CL, "default");
2557             free_constraint_list_full (SUBCL);
2558           }
2559         
2560         return CL;
2561         }
2562
2563 #define is_seq_source(Symbol,Mode,SeqMode)            (Symbol==Mode && (SeqMode==NULL || strm (SeqMode, "ANY") || (SeqMode[0]!='_' && strchr (SeqMode,Symbol)) || (SeqMode[0]=='_' && !strchr (SeqMode,Symbol)))) 
2564 Sequence * read_seq_in_n_list(char **fname, int n, char *type, char *SeqMode)
2565         {
2566         int nseq=0;
2567         int a, b;
2568         Alignment *A;
2569         char **sequences=NULL;
2570         char **seq_name=NULL;
2571         Sequence *S=NULL;
2572         Sequence *S1;
2573         char mode;
2574
2575
2576         
2577         /*THE TYPE OF EACH FILE MUST BE INDICATED*/
2578         /*SeqMode indicates the type of file that can be used as sequence sources*/
2579         /*
2580           ANY: any mode
2581           SL: only sequences from Libraries and Sequences
2582           _A: anything BUT sequences from A(lignments)  
2583         */
2584
2585         if ( n==0)
2586            {
2587              fprintf ( stderr, "\nERROR: NO IN FILE [FATAL:%s]\n", PROGRAM);
2588              myexit (EXIT_FAILURE);
2589            }
2590         else
2591            {       
2592            for ( a=0; a< n ; a++)
2593                {
2594                static char *buf;
2595                char *lname;
2596                if (buf)vfree (buf);
2597                 
2598                buf=name2type_name(fname[a]);mode=buf[0];lname=buf+1;
2599               
2600                if (is_seq_source ('A', mode, SeqMode))
2601                   {
2602                   
2603
2604                   A=main_read_aln (lname,NULL);
2605                   S1=aln2seq(A);
2606                   S1=seq2unique_name_seq (S1);
2607                   if ((S=merge_seq ( S1, S))==NULL){fprintf ( stderr, "\nERROR: Sequence Error in %s [FATAL:%s]\n",lname, PROGRAM); myexit(EXIT_FAILURE);} 
2608                   free_aln (A);
2609                   free_sequence (S1, S1->nseq);
2610                   }
2611                else if ( is_seq_source ('R', mode, SeqMode))
2612                   {
2613                     S=add_prf2seq (lname, S);
2614
2615                   }
2616                else if (is_seq_source ('P', mode, SeqMode))
2617                   {     
2618                     int i;
2619                     
2620                     S1=get_pdb_sequence (lname);
2621                     if (S1==NULL)
2622                       {
2623                         add_warning ( stderr, "\nWarning: Could not use PDB: %s", lname);
2624                       }
2625                     else
2626                       {
2627                         if ((S=merge_seq ( S1, S))==NULL){fprintf ( stderr, "\nERROR: Sequence Error in %s [FATAL:%s]\n",lname, PROGRAM); myexit(EXIT_FAILURE);} 
2628                         i=name_is_in_list (S1->name[0], S->name, S->nseq, 100);
2629                         (S->T[i])->P=fill_P_template (S->name[i], lname, S);
2630                       }
2631                     free_sequence (S1, S1->nseq);
2632                   }
2633                else if ( mode=='M');
2634                else if ( mode=='X');
2635                else if ( mode=='W');
2636                
2637                else if (is_seq_source ('S', mode, SeqMode))
2638                   {
2639                   /*1 Try with my routines (read t_coffee and MSF)*/ 
2640                     if ( (A=main_read_aln ( lname, NULL))!=NULL)  
2641                      {
2642                      
2643                      S1=aln2seq(A);
2644                      free_aln(A);
2645                      }            
2646                   else
2647                      { 
2648                      S1=main_read_seq (lname);
2649                      }
2650                   
2651                   for ( b=0; b< S1->nseq; b++)ungap(S1->seq[b]);
2652                   S1=seq2unique_name_seq (S1);
2653
2654                   
2655                   if ((S=merge_seq ( S1, S))==NULL){fprintf ( stderr, "\nSequence Error in %s [FATAL:%s]\n",lname,PROGRAM); myexit(EXIT_FAILURE);} 
2656                   
2657                   free_sequence (S1,S1->nseq);
2658                   
2659                   }
2660                else if (is_seq_source ('L', mode, SeqMode))
2661                   {
2662                   
2663                     read_seq_in_list (lname,&nseq,&sequences,&seq_name);             
2664                   
2665                     S1=fill_sequence_struc ( nseq, sequences, seq_name);
2666                   
2667                   for ( b=0; b< S1->nseq; b++)sprintf ( S1->file[b], "%s", lname);
2668                   nseq=0;free_char (sequences, -1); free_char ( seq_name, -1);
2669                   sequences=NULL;
2670                   seq_name=NULL;
2671                   S1=seq2unique_name_seq (S1);
2672
2673                   if ((S=merge_seq( S1, S))==NULL){fprintf ( stderr, "\nSequence Error in %s [FATAL:%s]\n",lname,PROGRAM); myexit(EXIT_FAILURE);} 
2674                   free_sequence(S1, S1->nseq);
2675                   }
2676               
2677                else if ( !strchr ( "ALSMXPRWG", mode))
2678                   {
2679                       fprintf ( stderr, "\nERROR: %s is neither a file nor a method [FATAL:%s]\n", lname, PROGRAM);
2680                       crash ("");
2681
2682                   }
2683                }
2684            
2685            S=remove_empty_sequence (S);
2686           
2687
2688            if ( type && type[0] )sprintf ( S->type, "%s", type);
2689            else S=get_sequence_type (S);
2690          
2691            if ( strm (S->type, "PROTEIN_DNA"))
2692               {
2693                   for ( a=0; a< S->nseq; a++)
2694                       {
2695                           if (strm ( get_string_type ( S->seq[a]), "DNA") ||strm ( get_string_type ( S->seq[a]), "RNA")  );
2696                           else if ( strm ( get_string_type ( S->seq[a]), "PROTEIN"))
2697                               {
2698                                   S->seq[a]=thread_aa_seq_on_dna_seq (S->seq[a]);
2699                                   S->len[a]=strlen (S->seq[a]);
2700                                   S->max_len=MAX(S->max_len, S->len[a]);
2701                               }
2702                       }
2703               }
2704
2705            
2706
2707            return S;
2708            }
2709
2710           
2711         
2712         return NULL;
2713         }
2714
2715 int read_cpu_in_list ( char *fname)
2716         {
2717         FILE *fp;
2718         int c;
2719         int cpu=0;
2720
2721         fp=vfopen ( fname, "r");
2722         while ( (c=fgetc(fp))!='#');
2723         while ( (c=fgetc(fp))!='C' && c!=EOF);
2724         if ( c=='C')fscanf( fp, "PU %d\n", &cpu);
2725         vfclose ( fp);
2726         return cpu;
2727         }
2728 char * expand_constraint_list_file ( char *file)
2729 {
2730   char *new_file;
2731   FILE *IN, *OUT;
2732   int a, b, c, n;
2733   char **list;
2734   static char *buf;
2735
2736   if ( !grep_function ( "'+BLOCK+'", file))return file;
2737
2738   new_file=vtmpnam (NULL);
2739   IN=vfopen ( file,"r");
2740   OUT=vfopen (new_file, "w");
2741  
2742   while ( (c=fgetc (IN))!=EOF)
2743     {
2744       ungetc (c, IN);
2745       buf=vfgets (buf, IN);
2746       if ( !strstr (buf, "+BLOCK+"))
2747         fprintf (OUT, "%s", buf);
2748       else
2749         {
2750           list=string2list (buf);
2751           n=atoi (list[2]);
2752           
2753           for (a=0; a< n; a++)
2754             {
2755               fprintf ( OUT, "%5d %5d ",atoi(list[3])+a, atoi(list[4])+a);
2756               for (b=5; b<atoi(list[0]); b++) fprintf ( OUT, "%s ", list[b]);
2757               fprintf (OUT, "\n");
2758             }
2759           free_char (list, -1);
2760         }
2761     }
2762   vfclose (IN); vfclose (OUT);
2763   return new_file;
2764 }
2765           
2766
2767 Constraint_list * make_test_lib(Constraint_list *CL)
2768 {
2769   int a, b, c, l1;
2770   Sequence *S;
2771   HERE ("cncn: making artificial lib");//Keep this warning alive
2772   S=CL->S;
2773  
2774   fprintf ( stderr, "\n");
2775   for (a=0; a< S->nseq-1; a++)
2776     {
2777       fprintf ( stderr, "[%d-%d]", a, CL->ne);
2778       for ( b=a+1; b< S->nseq; b++)
2779         {
2780           l1=MIN(S->len[a], S->len[b]);
2781           l1=MIN(10, l1);
2782           for (c=0; c<l1; c++)
2783             {
2784               vwrite_clist( CL,CL->ne,0,a);
2785               vwrite_clist( CL,CL->ne,1,b);
2786               vwrite_clist( CL,CL->ne,2,c+1);
2787               vwrite_clist( CL,CL->ne,3,c+1);
2788               vwrite_clist( CL,CL->ne,4,100);
2789               CL->ne++;
2790             }
2791         }
2792     }
2793   return CL;
2794 }
2795 Constraint_list * read_constraint_list_file(Constraint_list *CL, char *fname)
2796         {
2797         int a, c,e,n,z;
2798         int seq_len, sn;
2799         int s1, s2;
2800         FILE *fp;
2801         static char *name;
2802         char *sequence;
2803         static char *mat;
2804         static char *dp_mode;
2805         int max_nseq=0;
2806         static int *sn_list;
2807         static int line=2;
2808         int list_nseq;
2809         static CLIST_TYPE *entry;
2810         Sequence *S;
2811         Sequence *small_S;
2812         int seq_1_to_n=0;
2813         Alignment *B=NULL;
2814         char *buf;
2815         int lline;
2816         char *stripped_file1;
2817         char *stripped_file;
2818
2819         
2820
2821         
2822              
2823         stripped_file1=strip_file_from_comments ("!", fname);
2824         stripped_file=expand_constraint_list_file (stripped_file1);
2825         small_S=read_seq_in_n_list (&fname, 1,NULL, NULL); 
2826         
2827         if ( !CL)
2828           {
2829             CL=declare_constraint_list ( small_S,NULL, NULL, 0,NULL, NULL);  
2830             CL->S=small_S;
2831           }
2832         
2833         small_S=read_seq_in_n_list (&fname, 1, (CL->S)->type, NULL);
2834         
2835         B=seq2aln (small_S, NULL, 1); 
2836         B=fix_aln_seq  ( B, (CL->S));
2837         
2838         if ( CL->S!=small_S)free_sequence (small_S, B->nseq);
2839                 
2840         lline=measure_longest_line_in_file (fname)+1;
2841                 
2842         if ( !mat) mat=vcalloc (STRING, sizeof (char));
2843         if ( !dp_mode) dp_mode=vcalloc (STRING, sizeof (char));
2844         fp=vfopen (fname, "r");
2845         while((c=fgetc(fp))!='#')if ( c=='\n')max_nseq++;
2846         vfclose (fp);
2847         
2848         buf=vcalloc (lline, sizeof (char));     
2849         sequence=vcalloc (lline, sizeof (char));
2850         if ( !name)name=vcalloc ( 100, sizeof (char));
2851         if ( !entry)entry=vcalloc ( CL->entry_len, CL->el_size);
2852         if ( !sn_list)sn_list=vcalloc (max_nseq, sizeof (int));
2853         else 
2854           {
2855             sn_list=vrealloc (sn_list, max_nseq*sizeof (int));
2856           }
2857         S=CL->S;
2858
2859         seq_1_to_n=((fp=find_token_in_file (fname, NULL, "SEQ_1_TO_N"))!=NULL);
2860         vfclose (fp);
2861         if ( sn_list==NULL)sn_list=vcalloc (max_nseq, sizeof (int));
2862
2863         /*Read Constraint list*/
2864         fp=vfopen(stripped_file,"r");
2865         fscanf ( fp, "%d\n", &list_nseq);
2866         for ( a=0; a<list_nseq; a++)
2867                 {
2868                 fscanf ( fp, "%s %d %s\n", name, &seq_len, sequence);
2869                 line++;
2870                 lower_string (sequence);
2871                 
2872                 if ((sn=name_is_in_list (name,S->name, S->nseq, 100))==-1){continue;}
2873                 else
2874                   {
2875                     sn_list[a]=sn;
2876                   }
2877                 }
2878         
2879         while ((c=fgetc(fp))!=EOF)
2880           {
2881             ungetc(c, fp);
2882             if ( c=='#')
2883                         {
2884                         fscanf ( fp, "#%d %d\n", &s1, &s2);line++;
2885                         /*Check If the sequence numbering is legal*/
2886                         if ( seq_1_to_n){s1--; s2--;}
2887                         
2888                         if (s1<0 || s2 <0)
2889                           {
2890                             fprintf (stderr, "ERROR: Wrong Sequence Numbering in %s [FATAL:%s]\n",fname, PROGRAM); 
2891                             myexit (EXIT_FAILURE);
2892                           }
2893                         
2894
2895                         
2896                         s1=sn_list[s1];
2897                         s2=sn_list[s2];
2898
2899                         while (isdigit((c=fgetc(fp))))
2900                                 {
2901
2902                                 for ( z=0; z<  CL->entry_len; z++)entry[z]=0;
2903                                 ungetc(c, fp);                          
2904                                 n=0;
2905                                 entry[n++]=s1;
2906                                 entry[n++]=s2;
2907                                 while ( (c=fgetc(fp))!='\n')
2908                                   {
2909                                    
2910                                         if ( isspace (c));
2911                                         else 
2912                                                 {
2913                                                 ungetc(c, fp);
2914                                                 fscanf ( fp, "%d", &entry[n]);
2915                                                 n++;
2916                                                 }
2917                                         
2918                                         if ( n>CL->entry_len)
2919                                                 {
2920                                                 add_warning ( stderr, "\nWARNING:PARSING ERROR #1 (Too many Fields) IN %s AT LINE %d: C=%c n=%d\n", fname,line, c,n);
2921                                                 for ( e=2; e<LIST_N_FIELDS; e++)
2922                                                         fprintf ( stderr, "%d ", entry[e]);
2923                                         
2924                                                 myexit (EXIT_FAILURE);
2925                                                 }
2926                                         }
2927                                 if (c=='\n')line++;
2928                                  
2929                                 if ( n<=CONS)entry[CONS]=1;
2930                                 
2931                                   
2932                                 /*Check The legality of the entry*/
2933                                 if ( n>0 && n<3)
2934                                         {
2935                                         add_warning ( stderr, "\nWARNING:PARSING ERROR #2 IN %s (Not enough Fields) AT LINE %d: C=%c\n", fname,line-1, c);
2936                                         for ( e=2; e<LIST_N_FIELDS; e++)
2937                                                 fprintf ( stderr, "%d ",entry[e]);
2938                                         
2939                                         myexit (EXIT_FAILURE);
2940                                         }
2941                                 
2942                                 entry[R1]=(B->seq_cache)?B->seq_cache[entry[SEQ1]][entry[R1]]:entry[R1];
2943                                 entry[R2]=(B->seq_cache)?B->seq_cache[entry[SEQ2]][entry[R2]]:entry[R2];
2944                                 
2945                                 if ( entry[R1] && entry[R2])
2946                                   {
2947                                     if ( entry[R1]<=0 || entry[R1]>(CL->S)->len[s1])
2948                                       {
2949                                         fprintf ( stderr, "\nERROR: Seq1=%d (len=%d, name=%s), Seq2=%d (len=%d, name=%s), Res1 %d, Res2 %d\n", entry[SEQ1]+1,(CL->S)->len[s1],(CL->S)->name[s1], entry[SEQ2]+1,(CL->S)->len[s2],(CL->S)->name[s2],entry[R1], entry[R2]);
2950                                         fprintf ( stderr, "\nERROR: Library %s, line %d, Field 1: Bad residue numbering (%d)[FATAL:%s]\n", fname, line-1,entry[R1], PROGRAM);
2951                                         myexit (EXIT_FAILURE);
2952                                       }
2953                                     else if (entry[R2]<=0 || entry[R2]>(CL->S)->len[s2])
2954                                       {
2955                                         fprintf ( stderr, "\nERROR: Seq1=%d (len=%d, name=%s), Seq2=%d (len=%d, name=%s), Res1 %d, Res2 %d\n", entry[SEQ1]+1,(CL->S)->len[s1],(CL->S)->name[s1], entry[SEQ2]+1,(CL->S)->len[s2],(CL->S)->name[s2],entry[R1], entry[R2]);
2956                                         
2957                                         fprintf ( stderr, "\nERROR: Seq1: %d, Seq2 %d, Res1 %d, Res2 %d\n", entry[SEQ1], entry[SEQ2], entry[R1], entry[R2]);
2958                                         fprintf ( stderr, "\nERROR: Library %s, line %d, Field 2: Bad residue numbering (%d)[FATAL:%s]\n", fname, line-1, entry[R2],PROGRAM);
2959                                         myexit (EXIT_FAILURE);
2960                                       }
2961                                     fscanf ( fp, "\n");
2962                                     if ( (entry[SEQ1]>entry[SEQ2])|| (entry[SEQ1]==entry[SEQ2] && entry[R1]>entry[R2]))
2963                                       {
2964                                         SWAP(entry[SEQ1],entry[SEQ2]);
2965                                         SWAP(entry[R1], entry[R2]);
2966                                       }
2967                                   
2968                                     for ( z=0; z< CL->entry_len; z++)vwrite_clist( CL,CL->ne, z, entry[z]);
2969                                     
2970                                     CL->ne++;
2971                                   }
2972                                 }
2973                          ungetc ( c, fp);
2974                          
2975                          }
2976             else if ( c=='!' || c=='C' || c=='\n' || c=='\r'){while ((c=fgetc(fp))!='\n' && c!=EOF && c!='\r');}
2977             else
2978               {
2979                 fprintf ( stderr, "\n\n PARSING ERROR 3 IN %s AT LINE %d: [%c] \n[read_constraint_list_file]", fname,line,c);                   
2980                 while ((c=fgetc(fp))!='\n' && c!=EOF)fprintf ( stderr, "%c", c);
2981                 fprintf ( stderr, "\n");
2982                 printf_system ( "cp %s faulty_library.tc_lib", fname);
2983                 myexit (EXIT_FAILURE);
2984               }
2985             if ( c==EOF)ungetc(c, fp);
2986           }
2987
2988         free_aln (B);
2989         vfree(buf);
2990         vfree(sequence);
2991         vfclose (fp);    
2992         remove(stripped_file);
2993         return CL;
2994         } 
2995
2996 Constraint_list * read_constraint_list_raw_file(Constraint_list *CL, char *fname)
2997 {
2998   FILE *fp;
2999   int n=0, v;
3000   static int *entry;
3001
3002   if ( !entry)entry=vcalloc (CL->entry_len, sizeof (int));
3003   fp=vfopen (fname, "r");
3004   
3005   while (fscanf (fp, "%d ", &v)==1)
3006     {
3007       if (n==CL->entry_len)
3008         {
3009           add_entry2list (entry, CL);
3010           n=0;
3011         }
3012       entry[n++]=v;
3013     }
3014    if (n==CL->entry_len)
3015         {
3016           add_entry2list (entry, CL);
3017           n=0;
3018         }
3019    vfclose (fp);
3020    return CL;
3021 }
3022            
3023 Constraint_list * fast_read_constraint_list_file(Constraint_list *CL, char *in_fname)
3024         {
3025           Sequence *NS;
3026           int **index;
3027           int *list=NULL;
3028           int c;
3029           FILE *fp;
3030           CLIST_TYPE *entry=NULL;
3031           char *buf=NULL, *buf2;
3032           char *fname;
3033           int i;
3034           
3035
3036           
3037           fname=expand_constraint_list_file (in_fname);
3038           
3039           if (!CL)
3040             {
3041               return read_constraint_list_file (CL,fname);
3042             }
3043           
3044           entry=vcalloc (sizeof (int), CL->entry_len);
3045           NS=read_seq_in_n_list (&fname, 1,NULL, NULL);
3046           index=index_seq_name(NS,CL->S);
3047           
3048
3049           /*Read Constraint list*/
3050           fp=vfopen(fname,"r");
3051           i=0;
3052           while (i<=NS->nseq)
3053             {
3054               buf=vfgets ( buf, fp);
3055               if (buf[0]!='!')i++;
3056             }
3057           
3058           while ( (c=fgetc (fp))!='#' && c!=EOF);
3059           if (c==EOF)
3060             {
3061               vfclose (fp);
3062               vfree (list);
3063               free_sequence (NS,-1);
3064               vfree (entry);
3065               vfree (buf);
3066               add_warning (stderr, "Warning: incomplete library [%s]",PROGRAM); 
3067               return CL;
3068             }
3069           ungetc (c, fp);
3070           
3071           
3072
3073           while ((buf2=vfgets ( buf, fp))!=NULL)
3074             {
3075               if (buf2[0]=='!')continue;
3076               buf=buf2;
3077               
3078               list=string2num_list2 (buf, " #\n");
3079               
3080               if (buf[0]=='#')
3081                 {
3082                   sscanf ( buf, "#%d %d", &entry[SEQ1], &entry[SEQ2]);
3083                   entry[SEQ1]=index[entry[SEQ1]-1][0];
3084                   entry[SEQ2]=index[entry[SEQ2]-1][0];
3085                   
3086                 }
3087               else
3088                 {
3089                   sscanf (buf, "%d %d %d %d %d", &entry[R1], &entry[R2], &entry[WE], &entry[CONS], &entry[MISC]);
3090                   
3091                   
3092                   
3093                   
3094                   
3095                   
3096                   if ( (entry[SEQ1]>entry[SEQ2])|| (entry[SEQ1]==entry[SEQ2] && entry[R1]>entry[R2]))
3097                     {
3098                       SWAP(entry[SEQ1],entry[SEQ2]);SWAP(entry[R1], entry[R2]);
3099                     }
3100                   add_entry2list (entry, CL);
3101                 }
3102             }
3103           vfclose (fp);
3104           
3105           
3106           
3107           vfree (list);
3108           free_sequence (NS,-1);
3109           vfree (entry);
3110           vfree (buf);
3111           
3112           return CL;
3113         }       
3114   
3115 int read_seq_in_list ( char *fname,  int *nseq, char ***sequences, char ***seq_name)
3116         {
3117           int a;
3118         int seq_len, sn;
3119
3120         FILE *fp;
3121         char name[1000];
3122         char *sequence; 
3123         static int max_nseq;
3124         static int *sn_list;
3125         int list_nseq;
3126         int lline;
3127
3128
3129         
3130         fp=vfopen (fname, "r");
3131         fp=skip_commentary_line_in_file ('!', fp);
3132         fscanf (fp, "%d\n", &max_nseq);
3133         for ( lline=0,a=0; a<max_nseq; a++)
3134           {
3135             int l;
3136             fscanf (fp, "%*s %d %*s\n", &l);
3137             lline=MAX(lline, l);
3138           }
3139         vfclose (fp);
3140         sequence=vcalloc (lline, sizeof (char)+1);
3141         
3142         
3143         
3144         if ( seq_name[0]==NULL)
3145                 {
3146                 seq_name[0]= declare_char (max_nseq,0);
3147                 sequences[0]=declare_char (max_nseq,0);         
3148                 }
3149         if ( sn_list==NULL)sn_list=vcalloc ( max_nseq, sizeof (int));
3150         else sn_list=vrealloc (sn_list, max_nseq*sizeof (int));
3151
3152         fp=vfopen (fname,"r");
3153         fp=skip_commentary_line_in_file ('!', fp);
3154         if (fscanf ( fp, "%d\n", &list_nseq)!=1)return 0;
3155         for ( a=0; a<max_nseq; a++)
3156           {
3157             fp=skip_commentary_line_in_file ('!', fp);
3158             
3159             fscanf ( fp, "%s %d %s\n", name, &seq_len, sequence);
3160             lower_string (sequence);
3161                 
3162             if ((sn=name_is_in_list (name, seq_name[0], nseq[0], 100))==-1)
3163               {
3164                 seq_name[0][nseq[0]]=vcalloc (strlen (name)+1, sizeof (char));
3165                 sprintf (seq_name[0][nseq[0]], "%s", name);
3166                 sequences[0][nseq[0]]=vcalloc (strlen (sequence)+1, sizeof (char));
3167                 sprintf (sequences[0][nseq[0]], "%s", sequence);
3168                 sn_list[a]=nseq[0];
3169                 nseq[0]++;
3170               }
3171             else
3172               {
3173                 sn_list[a]=sn;
3174               }
3175           }
3176         vfclose (fp);
3177         vfree (sequence);
3178         return 1;
3179         }
3180
3181
3182 /*********************************************************************/
3183 /*                                                                   */
3184 /*                        EXTENDED LIST OUTPUT                       */
3185 /*                                                                   */
3186 /*                                                                   */
3187 /*********************************************************************/ 
3188
3189 FILE * save_extended_constraint_list      (  Constraint_list *CL, char *mode, FILE *fp) 
3190 {
3191   int a, b;
3192   
3193   if ( !fp)fp=stdout;
3194   
3195     
3196   fp=save_list_header (fp,CL);
3197   
3198   
3199   for ( a=0; a< (CL->S)->nseq; a++)
3200     {
3201       for ( b=a; b<(CL->S)->nseq; b++)
3202         {
3203
3204           if ( a==b && !CL->do_self)continue;
3205           fp=save_extended_constraint_list_pair(CL, mode, (CL->S)->name[a], (CL->S)->name[b], fp);
3206         }
3207     }
3208   fprintf (fp, "! SEQ_1_TO_N\n");
3209   return fp;
3210 }
3211   
3212
3213 FILE * save_extended_constraint_list_pair (  Constraint_list *CL, char *mode, char* seq1, char * seq2,FILE *fp)
3214       {
3215         int a, b, t;
3216         int s1, s2, score;
3217         char *p;
3218         
3219         
3220         if ((p=strstr (mode, "THR"))!=NULL)t=atoi(p+3);
3221         else t=0;
3222         
3223         s1=name_is_in_list (seq1,(CL->S)->name, (CL->S)->nseq, 100);
3224         s2=name_is_in_list (seq2,(CL->S)->name, (CL->S)->nseq, 100);
3225         
3226         if ( s1==-1)
3227           {
3228             fprintf ( stderr, "Output Error: %s is not a sequence [FATAL:%s]\n", seq1, PROGRAM);
3229             crash ("");
3230           }
3231         if ( s2==-1)
3232           {
3233             fprintf ( stderr, "Output Error: %s is not a sequence [FATAL:%s]\n", seq2, PROGRAM);
3234             crash ("");
3235           }
3236
3237         if ( strstr (mode, "pair"))fprintf (fp, "# 1 2\n");
3238         else if ( strstr (mode, "lib"))fprintf (fp, "# %d %d\n", s1+1, s2+1);
3239         
3240         for ( a=0; a<(CL->S)->len[s1]; a++)
3241                 {
3242                   for ( b=0; b<(CL->S)->len[s2]; b++)
3243                     {
3244                       if ( a>=b && s1==s2)continue;
3245                       if ( strstr (mode, "pc"))score=residue_pair_extended_list_pc (CL, s1,a+1, s2, b+1);
3246                       else if ( strstr (mode, "raw"))score=residue_pair_extended_list_raw (CL, s1,a+1, s2, b+1);
3247                       else 
3248                         score=CL->evaluate_residue_pair (CL, s1,a+1, s2, b+1);
3249                       
3250                       if (score<=t) continue;
3251                       fprintf (fp, "%5d %5d %5d \n", a+1, b+1, score);
3252                       
3253                     }             
3254                 }
3255         return fp;
3256       }
3257
3258         
3259 /*********************************************************************/
3260 /*                                                                   */
3261 /*                         LIST OUTPUT                               */
3262 /*                                                                   */
3263 /*                                                                   */
3264 /*********************************************************************/         
3265 #ifdef MMMMMMMM
3266 FILE *save_extended_constraint_list ( Constraint_list *CL,Sequence *S, char *fname)
3267 {
3268   int a, b, c, d;
3269   int *tr, *ns;
3270   int **pos0, **l_s;
3271   int epsilon=0;
3272   Alignment *A;
3273   FILE *fp;
3274
3275   fp=vfopen (fname, "w");
3276   fp=save_sub_list_header(fp, S->nseq, S->name, CL);
3277   
3278   tr=vcalloc (S->nseq+1, sizeof (int));
3279   for ( b=0,a=0; a< S->nseq; a++)
3280     {
3281       int i;
3282       if ( (i=name_is_in_list(S->name[a],(CL->S)->name,(CL->S)->nseq, 100))==-1)
3283         {
3284           printf_exit (EXIT_FAILURE, stderr, "\nERROR: Sequence %s is not part of the sequence dataset [FATAL:%s]", S->name[a], PROGRAM);
3285           
3286         }
3287       else 
3288         {
3289           tr[a]=i;
3290         }
3291     }
3292
3293   A=declare_aln (S);
3294   pos0=vcalloc ( S->nseq, sizeof (int*));
3295   for (a=0; a<S->nseq; a++)
3296     {
3297       int l;
3298       l=strlen (S->seq[a]);
3299       A->seq_al[a]=S->seq[a];
3300       pos0[a]=vcalloc (l+1, sizeof (int));
3301       for (b=0; b<l; b++)pos[a][b]=b+1;
3302     }
3303   l_s=declare_int (2,2);
3304   ns=vcalloc ( 2, sizeof (int));
3305   
3306       
3307   for ( a=0; a< S->nseq-1; a++)
3308     for ( b=a+1; b<S->nseq; b++)
3309       {
3310         int pos_i, pos_j, s;
3311         l_s[0]=tr[a];l_s[1]=tr[b];
3312         for ( pos_i=0; pos_i< S->len[a]; pos_i++)
3313           for (pos_j=0; pos_j<S->len[b]; pos_j++)
3314             {
3315               s=(CL->get_dp_cost) ( A, pos0, ns[0], l_s[0], i-1, pos0, ns[1], l_s[1],pos_j-1, CL);
3316               if (s>epsilon)fprintf (fp, "%d %d %d", i, j, s);
3317             }
3318       }
3319   return fp;
3320 }
3321 #endif   
3322
3323 FILE * save_constraint_list ( Constraint_list *CL,int start, int len, char *fname, FILE *fp,char *mode, Sequence *S)
3324         {
3325         int a, b;
3326         static int* translation;
3327
3328         
3329         
3330         
3331         if ( fp==NULL)
3332            {
3333            if ( translation!=NULL)vfree(translation);
3334            translation=vcalloc ( (CL->S)->nseq+1, sizeof (int));
3335            for ( b=0,a=0; a< (CL->S)->nseq; a++)
3336                {
3337                if ( name_is_in_list((CL->S)->name[a],S->name,S->nseq, 100)==-1)
3338                   {
3339                   (CL->S)->len[a]=-1;
3340                   translation [a]=-1;
3341                   }
3342                else 
3343                   {
3344                   translation[a]=b++;
3345                   }
3346                }
3347            
3348            }
3349         if (strm2(mode, "lib","ascii"))
3350            {
3351            if ( fp==NULL)fp=vfopen ( fname, "w");
3352            fp=save_list_header (fp,CL);
3353            fp=save_constraint_list_ascii(fp, CL, 0, CL->ne, translation);
3354            }
3355         else if (strm(mode, "binary"))
3356            {
3357            if ( fp==NULL)fp=vfopen ( fname, "wb");
3358            fp=save_constraint_list_bin  (fp, CL, 0, CL->ne, translation);
3359            }
3360         else
3361             {
3362             fprintf (stderr,"\nUNKOWN MODE FOR OUTPUT: %s [FATAL]\n",mode);
3363             crash ("");
3364             }
3365         return fp;
3366         }
3367                     
3368 FILE * save_sub_list_header ( FILE *OUT, int n, char **name, Constraint_list *CL)
3369         {
3370         int a,b;
3371         int nseq=0;
3372         
3373         
3374
3375         for ( a=0; a<(CL->S)->nseq; a++)
3376           for ( b=0; b<n; b++)
3377             if (strm (name[b] ,  (CL->S)->name[a]))
3378               nseq+=((CL->S)->len[a]!=-1);
3379                 
3380         fprintf ( OUT, "! TC_LIB_FORMAT_01\n%d\n",nseq);
3381         for ( a=0; a<n; a++)
3382           for ( b=0; b<(CL->S)->nseq; b++)
3383             if (strm (name[a] ,  (CL->S)->name[b]))
3384                 if ((CL->S)->len[b]!=-1) fprintf ( OUT, "%s %d %s\n", (CL->S)->name[b], (CL->S)->len[b],(CL->S)->seq[b]);
3385         
3386         return OUT;                     
3387         }          
3388 FILE * save_list_header ( FILE *OUT,Constraint_list *CL)
3389         {       
3390         int a;
3391         int nseq=0;
3392         
3393         for ( a=0; a<(CL->S)->nseq; a++)nseq+=((CL->S)->len[a]!=-1);
3394         
3395
3396         fprintf ( OUT, "! TC_LIB_FORMAT_01\n%d\n",nseq);
3397         for ( a=0; a<(CL->S)->nseq; a++)
3398                 if ((CL->S)->len[a]!=-1) 
3399                   {
3400                     fprintf ( OUT, "%s %d %s\n", (CL->S)->name[a], (CL->S)->len[a],(CL->S)->seq[a]);
3401                     
3402                   }
3403         return OUT;                     
3404         }
3405
3406 FILE *save_list_footer (FILE *OUT,Constraint_list *CL)
3407       {
3408         if ( CL->cpu)fprintf (OUT, "! CPU %d\n",get_time());
3409         fprintf (OUT, "! SEQ_1_TO_N\n");
3410         return OUT;
3411       }
3412 int constraint_list2raw_file ( Constraint_list *CL, char *fname, char *mode)
3413 {
3414   FILE *fp;
3415   if ( !CL || !CL->ne || !fname){return 0;}
3416   
3417   fp=vfopen (fname,mode);
3418   fp=save_raw_constraint_list (fp, CL, 0, CL->ne, NULL);
3419   vfclose (fp);
3420   return CL->ne;
3421   }
3422 FILE * save_raw_constraint_list   ( FILE *fp,Constraint_list *CL, int start,int len, int *translation)
3423 {
3424   int a, b;
3425   for ( b=0; b<CL->entry_len*CL->ne; b++)
3426     {
3427       fprintf ( fp, "%d ", CL->L[b]);
3428     }
3429
3430   fprintf (fp, "\n");
3431   return fp;
3432 }
3433 FILE * save_constraint_list_ascii ( FILE *OUT,Constraint_list *CL, int start,int len, int *translation)
3434         {       
3435         int a, b, s1, s2;
3436         CLIST_TYPE x1, x2;
3437
3438         if (len==start && CL->cpu!=-1)
3439             {
3440             fprintf (OUT, "! CPU %d\n",get_time());
3441             return OUT;
3442             }
3443         else
3444             {
3445             
3446             s1=translation[vread_clist(CL,start,SEQ1)];
3447             s2=translation[vread_clist(CL,start,SEQ2)];
3448            
3449         
3450             if ( s1!=-1 && s2!=-1)fprintf ( OUT, "#%d %d\n", s1+1, s2+1);
3451             for ( a=start; a<(len+start); a++)
3452                    {
3453                    x1=translation[vread_clist(CL,a,SEQ1)];
3454                    x2=translation[vread_clist(CL,a,SEQ2)];
3455                    if ( x1==-1 || x2==-1);
3456                    else 
3457                        {
3458                        if ( x1!=s1 || x2!=s2)
3459                           {
3460                           s1=x1;
3461                           s2=x2;
3462                           fprintf ( OUT, "#%d %d\n", s1+1, s2+1);
3463                           }
3464                        for ( b=2; b<CL->entry_len; b++) fprintf ( OUT, "%5d ", vread_clist(CL, a, b));
3465                        fprintf (OUT, "\n");
3466                        }
3467                    }
3468             }
3469         return save_list_footer (OUT, CL);
3470                         
3471         }
3472 FILE * save_constraint_list_bin ( FILE *OUT,Constraint_list *CL, int start,int len, int *translation)
3473         {       
3474         int a, b;
3475
3476         CLIST_TYPE x1, x2;
3477         
3478
3479         if (len==start && CL->cpu!=-1)
3480             {
3481             
3482             return OUT;
3483             }
3484         else
3485             {
3486             for ( a=start; a<(len+start); a++)
3487                    {
3488                    x1=translation[vread_clist(CL,a,SEQ1)];
3489                    x2=translation[vread_clist(CL,a,SEQ2)];
3490                    if ( x1==-1 || x2==-1);
3491                    else 
3492                        {
3493                        for ( b=2; b<CL->entry_len; b++)
3494                            {
3495                            x1=vread_clist(CL,a,b);
3496                            fwrite (&x1, CL->el_size, 1, OUT);
3497                            }
3498                        }
3499                    }
3500             }
3501         return OUT;                     
3502         }
3503
3504 /*********************************************************************/
3505 /*                                                                   */
3506 /*                         LIST CONVERTION                           */
3507 /*                                                                   */
3508 /*                                                                   */
3509 /*********************************************************************/         
3510 Constraint_list * filter_constraint_list (Constraint_list *CL, int field, int T)
3511 {
3512   int a,b,c;
3513   if (!CL || !CL->L)return CL;
3514   for (a=0, b=0; a<CL->ne; a++)
3515     {
3516       if (CL->L[a*CL->entry_len+field]<T);
3517       else
3518         {
3519           for (c=0; c<CL->entry_len; c++)CL->L[b*CL->entry_len+c]=CL->L[a*CL->entry_len+c];
3520           b++;
3521         }
3522     }
3523   CL->ne=b;
3524   CL->residue_indexed=0;
3525   return CL;
3526 }
3527 int constraint_list_is_connected ( Constraint_list *CL)
3528 {
3529   int a, b, c, s1, s2;
3530   int *connexions;
3531   int rv =1;
3532   Sequence*S;
3533
3534   if (!CL->ne) return 1;
3535   
3536   S=CL->S;
3537   
3538   connexions=vcalloc ((CL->S)->nseq+1, sizeof (int));
3539   for ( a=0; a< CL->ne; a++)
3540     {
3541       s1=CL->L[a*CL->entry_len+SEQ1];
3542       s2=CL->L[a*CL->entry_len+SEQ2];
3543       connexions[s1]++;
3544       connexions[s2]++;
3545     }
3546   for (a=0; a<S->nseq; a++)
3547     {
3548       if (!connexions[a])
3549         {
3550           add_warning ( stderr, "ERROR: Sequence %s is not connected\n", (CL->S)->name[a]);
3551           rv=0;
3552         }
3553     }
3554   return rv;
3555 }
3556 Constraint_list * nfork_relax_constraint_list (Constraint_list *CL);
3557 Constraint_list * fork_relax_constraint_list (Constraint_list *CL);
3558 Constraint_list * relax_constraint_list (Constraint_list *CL)
3559 {
3560   if ( get_nproc()==1)return  nfork_relax_constraint_list (CL);
3561   else if (strstr ( CL->multi_thread, "relax"))return fork_relax_constraint_list (CL);
3562   else return nfork_relax_constraint_list (CL);
3563 }
3564
3565 Constraint_list * fork_relax_constraint_list (Constraint_list *CL)
3566 {
3567   int a, s1, s2, r1, r2,n;
3568   int score;
3569   int thr;
3570   int chunk, npid, job,pid;
3571   FILE *fp;
3572   char **pid_tmpfile;
3573   int * pid_list;
3574   int in;
3575
3576   in=CL->ne;
3577   if (!CL || !CL->L)return CL;
3578
3579   fprintf ( CL->local_stderr, "\nLibrary Relaxation: Multi_proc [%d] ", get_nproc());
3580   
3581   if ((chunk=CL->ne/get_nproc())==0)chunk=get_nproc();
3582   
3583  
3584   pid_tmpfile=vcalloc ((CL->ne/chunk)+1, sizeof (char*));
3585   pid_list   =vcalloc (MAX_N_PID, sizeof (int *));
3586   
3587   for (npid=0,job=0; job<CL->ne; job+=chunk)
3588     {
3589       pid_tmpfile[npid]=vtmpnam(NULL);
3590       pid=vfork ();
3591       if (pid==0)
3592         {
3593           int s,e;
3594           
3595           initiate_vtmpnam (NULL);
3596           s=job;
3597           e=MIN((s+chunk),CL->ne);
3598           fp=vfopen (pid_tmpfile[npid], "w");
3599           for (a=s; a<e; a++)
3600             {
3601               if (job==0)output_completion (CL->local_stderr,a,chunk,1, "Submit   Job");
3602               s1=CL->L[a*CL->entry_len+SEQ1];
3603               s2=CL->L[a*CL->entry_len+SEQ2];
3604               
3605               r1=CL->L[a*CL->entry_len+R1];
3606               r2=CL->L[a*CL->entry_len+R2];
3607               score=residue_pair_extended_list_pc (CL,s1, r1,s2, r2);
3608               CL->L[a*CL->entry_len+WE]=score;
3609               fprintf (fp, "%d %d ", a, score);
3610             }
3611           vfclose (fp);
3612           exit (EXIT_SUCCESS);
3613         }
3614       else
3615         {
3616           pid_list[pid]=npid;
3617           set_pid (pid);
3618           npid++;
3619         }
3620     }
3621   
3622   for (a=0; a<npid; a++)
3623     {
3624       int i;
3625       int j=0;
3626       
3627       pid=vwait (NULL);
3628       fp=vfopen (pid_tmpfile[pid_list[pid]], "r");
3629       while (fscanf (fp, "%d %d ",&i, &score)==2){CL->L[i*CL->entry_len+WE]=score;j++;}
3630       vfclose (fp);
3631       remove(pid_tmpfile[pid_list[pid]]);
3632     }
3633   
3634   vfree (pid_list);
3635   vfree (pid_tmpfile);
3636   
3637   thr=10;
3638     
3639   for (n=0,a=0; a< CL->ne; a++)
3640     {
3641       score=CL->L[a*CL->entry_len+WE];
3642       
3643       if (score<=thr);
3644       else
3645         {
3646           CL->L[n*CL->entry_len+SEQ1]=CL->L[a*CL->entry_len+SEQ1];
3647           CL->L[n*CL->entry_len+SEQ2]=CL->L[a*CL->entry_len+SEQ2];
3648           CL->L[n*CL->entry_len+R1]=CL->L[a*CL->entry_len+R1];
3649           CL->L[n*CL->entry_len+R2]=CL->L[a*CL->entry_len+R2];
3650           CL->L[n*CL->entry_len+WE]=score;
3651           n++;
3652         }
3653       
3654     }
3655  
3656   CL->L=vrealloc (CL->L, n*CL->entry_len*sizeof (int));
3657   CL->ne=n;
3658   
3659   fprintf ( CL->local_stderr, "\nTotal Relaxation: [%d]--->[%d] Entries\n",in, CL->ne); 
3660   CL->residue_indexed=0;
3661   return CL;
3662   
3663 }               
3664   
3665 Constraint_list * nfork_relax_constraint_list (Constraint_list *CL)
3666 {
3667   int a, s1, s2, r1, r2;
3668   int max, score, n;
3669   int thr;
3670   
3671   if (!CL || !CL->L)return CL;
3672
3673   fprintf ( CL->local_stderr, "\nLibrary Relaxation:[%d] ", CL->ne);
3674   for (max=0,a=0; a<CL->ne; a++)
3675     {
3676       
3677       s1=CL->L[a*CL->entry_len+SEQ1];
3678       s2=CL->L[a*CL->entry_len+SEQ2];
3679
3680       r1=CL->L[a*CL->entry_len+R1];
3681       r2=CL->L[a*CL->entry_len+R2];
3682       
3683       score=residue_pair_extended_list_pc (CL,s1, r1,s2, r2);
3684       //HERE ("%d %d",  CL->L[a*CL->entry_len+WE],score);
3685       CL->L[a*CL->entry_len+WE]=score;
3686        
3687     }
3688
3689   thr=10;
3690     
3691   for (n=0,a=0; a< CL->ne; a++)
3692     {
3693       score=CL->L[a*CL->entry_len+WE];
3694       
3695       if (score<=thr);
3696       else
3697         {
3698           CL->L[n*CL->entry_len+SEQ1]=CL->L[a*CL->entry_len+SEQ1];
3699           CL->L[n*CL->entry_len+SEQ2]=CL->L[a*CL->entry_len+SEQ2];
3700           CL->L[n*CL->entry_len+R1]=CL->L[a*CL->entry_len+R1];
3701           CL->L[n*CL->entry_len+R2]=CL->L[a*CL->entry_len+R2];
3702           CL->L[n*CL->entry_len+WE]=score;
3703           n++;
3704         }
3705       
3706     }
3707   CL->L=vrealloc (CL->L, n*CL->entry_len*sizeof (int));
3708   CL->ne=n;
3709   
3710   fprintf ( CL->local_stderr, "--->[%d]\n", CL->ne); 
3711   CL->residue_indexed=0;
3712   return CL;
3713   
3714 }               
3715
3716 Constraint_list * shrink_constraint_list (Constraint_list *CL)
3717 {
3718   int a, b, n, tot;
3719   Constraint_list *CL2;
3720   Alignment *A, *B;
3721   int *ns, **ls;
3722
3723   ns=vcalloc (2, sizeof (int));
3724   ls=declare_int ((CL->S)->nseq, 2);
3725   
3726   A=seq2aln (CL->S,NULL, RM_GAP);
3727   B=seq2aln (CL->S,NULL, RM_GAP);
3728   CL2=declare_constraint_list (CL->S,NULL, NULL, 0,NULL, NULL);  
3729   n=(CL->S)->nseq;
3730   tot=((n*n)-n)/2;
3731   fprintf ( CL->local_stderr, "\n\n\tSHRINK Constraint List [%d element(s)]", CL->ne);
3732   for (n=0,a=0; a<(CL->S)->nseq-1; a++)
3733     for (b=a+1; b<(CL->S)->nseq; b++, n++)
3734       {
3735         output_completion (CL->local_stderr,n, tot, 100, "slow_pair");
3736         ns[0]=ns[1]=1;
3737         ls[0][0]=a;
3738         ls[1][0]=b;
3739         ungap (A->seq_al[a]);
3740         ungap (A->seq_al[b]);
3741         linked_pair_wise (A, ns, ls, CL);
3742         B->seq_al[0]=A->seq_al[a];
3743         B->seq_al[1]=A->seq_al[b];
3744         sprintf (B->name[0], "%s", A->name[a]);
3745         sprintf (B->name[1], "%s", A->name[b]);
3746         B->nseq=2;
3747         B->len_aln=strlen (B->seq_al[0]);
3748         CL2=aln2constraint_list (B, CL2, "sim");
3749       }
3750   vfree(CL->L);
3751   CL->L=CL2->L;
3752   CL->ne=CL2->ne;
3753   return CL;
3754 }
3755   
3756
3757 Constraint_list *aln_file2constraint_list (char *alname, Constraint_list *CL,char *weight_mode)
3758         {
3759         Alignment *A;
3760         A=main_read_aln ( alname, NULL);
3761                 
3762         CL=aln2constraint_list (A, CL, weight_mode);
3763         free_aln (A);
3764         return CL;
3765         }
3766
3767 int *seqpair2weight (int s1, int s2, Alignment *A,Constraint_list *CL, char *weight_mode, int *weight)
3768 {
3769   int *col;
3770   int a,c, ref_weight;
3771   
3772   
3773   if ( !weight)weight=vcalloc (MAX(2,A->len_aln), sizeof (int));
3774   
3775   weight[0]=FORBIDEN;
3776   if ( weight_mode==NULL || strcmp (weight_mode, "no")==0 || is_number (weight_mode))
3777     {
3778       
3779       if (is_number (weight_mode))ref_weight=atoi(weight_mode);
3780       else ref_weight=1;
3781       weight[1]=ref_weight;
3782       
3783     }
3784   else if ( strstr ( weight_mode, "cons"))
3785     {
3786       ref_weight=weight[1]=1000;
3787     }
3788   else if ( strstr ( weight_mode, "OW"))
3789     {
3790       int ow;
3791       sscanf ( weight_mode, "OW%d", &ow);
3792       weight[1]=ow*get_seq_sim ( A->seq_al[s1], A->seq_al[s2], "-", NULL);
3793       
3794     }
3795   else if ( strncmp ( weight_mode, "len",3)==0)
3796     {
3797       weight[1]=A->len_aln;
3798     }
3799   else if ( strnm ( weight_mode, "sim", 3) || strm (weight_mode, "default"))
3800     {
3801       
3802       ref_weight=get_seq_sim ( A->seq_al[s1], A->seq_al[s2], "-", (strm (weight_mode, "default"))?NULL:(weight_mode+3));
3803       weight[1]=ref_weight;
3804
3805     }
3806   else if ( strnm ( weight_mode, "subset", 6))
3807     {
3808       ref_weight=get_seq_sim ( A->seq_al[s1], A->seq_al[s2], "-",NULL);
3809       weight[1]=ref_weight;
3810     }
3811   
3812   else if ( strncmp (weight_mode, "winsim", 6)==0)
3813     {
3814       weight=get_seq_winsim ( A->seq_al[s1], A->seq_al[s2], "-", weight_mode+6, weight);
3815     }
3816   else if (  strncmp ( weight_mode, "cdna", 4)==0)
3817     {
3818       ref_weight=get_seq_sim ( A->seq_al[s1], A->seq_al[s2], "-", weight_mode+4);
3819       col=vcalloc ( A->len_aln+1, sizeof (int));
3820       if (A->cdna_cache)
3821         for ( a=0; a<=A->len_aln; a++)col[a]=A->cdna_cache[0][a];
3822       else
3823         for ( a=0; a<=A->len_aln; a++)col[a]=1;
3824       for ( c=0; c< A->len_aln; c++)weight[c]=ref_weight*col[c];
3825       vfree (col);
3826     }
3827   else if ( strm ( weight_mode, "pdb"))
3828     {
3829       if ( !(A->CL) || !(A->CL)->T)
3830         {
3831           fprintf ( stderr, "\nCould not find the PDB structure: [FATAL:%s]\n", PROGRAM);
3832           crash ("");
3833         }
3834     }
3835   else if ( strm (weight_mode, "overaln"))
3836     {
3837       ref_weight=get_seq_sim ( A->seq_al[s1], A->seq_al[s2], "-","idmat");
3838       //weight=pw_aln2clean_aln_weight (A->seq_al[s1], A->seq_al[s2], ref_weight,0, 0, 0, 0, NULL);
3839       printf_exit (EXIT_FAILURE, stderr,"ERROR: mode overaln not currently supported [FATAL:%s]", PROGRAM);
3840     }
3841   else
3842     {
3843       fprintf ( stderr, "\nERROR: Weight Mode %s is unknown [FATAL:%s]", weight_mode, PROGRAM);
3844       crash ("");
3845     }
3846   return weight;
3847 }
3848 Constraint_list *aln2constraint_list      (Alignment *A, Constraint_list *CL,char *in_weight_mode)
3849         {
3850         Constraint_list *CLB=NULL;
3851         int a, b, c,nres1, nres2;
3852         int *weight=NULL;
3853         int s1, s2;
3854         int fixed_nres1, fixed_nres2;
3855         int do_pdb=0;
3856         int pdb_weight=0;
3857         int set_misc;
3858         char*alp=NULL;
3859         char *p, *s;
3860         char weight_mode [100];
3861         
3862         
3863         sprintf ( weight_mode , "%s", (!in_weight_mode || strm (in_weight_mode, "default"))?"sim":in_weight_mode);
3864           
3865         if ( !A) return CL;     
3866         
3867         if ( !CL)
3868           {
3869             Sequence *S;
3870             S=aln2seq (A);
3871             CL=declare_constraint_list (S,NULL, NULL, 0,NULL, NULL);  
3872             CL->S=S;
3873           }
3874         CLB=(Constraint_list *)A->CL;
3875         
3876
3877         do_pdb=(strstr ( weight_mode, "pdb"))?1:0;
3878         if ( (p=strstr (weight_mode, "_subset_")))
3879           {
3880             alp=strchr (weight_mode, '_')+1;
3881             p[0]='\0';
3882           }
3883
3884         
3885         for ( a=0; a<A->nseq-1; a++)
3886                 
3887                 {
3888                 for (set_misc=0,b=a+1; b< A->nseq; b++)
3889                         {       
3890                         s1=name_is_in_list (A->name[a], (CL->S)->name, (CL->S)->nseq, 100);
3891                         s2=name_is_in_list (A->name[b], (CL->S)->name, (CL->S)->nseq, 100);
3892                         
3893                         if ( s1==-1 || s2==-1)
3894                           {
3895                             if ( getenv4debug ("DEBUG_LIBRARY"))fprintf ( stderr, "\n[DEBUG_LIBRARY:aln2constraint_list]Could use a pair of constraints");
3896                           }
3897                         else if ( s1!=-1 && s2!=-1)
3898                             {
3899                               int use_pair;
3900                               
3901                               weight=seqpair2weight (a, b, A, CL, weight_mode, weight);
3902                               
3903                               for (nres1=A->order[a][1], nres2=A->order[b][1], c=0; c< A->len_aln; c++)
3904                                 {
3905                                   int isgop1, isgop2;
3906                                   
3907                                   isgop1=is_gop(c, A->seq_al[a]);
3908                                   isgop2=is_gop(c, A->seq_al[b]);
3909                                   nres1+=!is_gap(A->seq_al[a][c]);
3910                                   nres2+=!is_gap(A->seq_al[b][c]);
3911                                   
3912                                   if ( strm ( weight_mode, "pdb") && CLB)
3913                                     {
3914                                       
3915                                       pdb_weight=MAX(0,(CLB->evaluate_residue_pair)(CLB,0, nres1,1,nres2));
3916                                     }
3917                                 
3918                                 use_pair=1;
3919                                 use_pair=use_pair && !is_gap(A->seq_al[a][c]);
3920                                 use_pair=use_pair && !is_gap(A->seq_al[b][c]);
3921                                 use_pair=use_pair && A->seq_al[b][c]!=UNDEFINED_RESIDUE;
3922                                 use_pair=use_pair && A->seq_al[a][c]!=UNDEFINED_RESIDUE;
3923                                 use_pair=use_pair && !(do_pdb && pdb_weight==0);
3924                                 use_pair=use_pair && ((weight[0]==FORBIDEN)?weight[1]:weight[c]);
3925                                   
3926                                 if (alp)use_pair=use_pair && is_in_set (A->seq_al[b][c], alp) && is_in_set (A->seq_al[a][c], alp);
3927                                 
3928                                 /*if ( !is_gap(A->seq_al[a][c]) && !is_gap(A->seq_al[b][c]) && A->seq_al[b][c]!=UNDEFINED_RESIDUE && A->seq_al[a][c]!=UNDEFINED_RESIDUE && !(do_pdb && pdb_weight==0))*/
3929                                 if (use_pair)
3930                                         {
3931                                         
3932                                           fixed_nres1=(!A->seq_cache)?nres1:A->seq_cache[s1][nres1];
3933                                           fixed_nres2=(!A->seq_cache)?nres2:A->seq_cache[s2][nres2];
3934                                           
3935                                           
3936                                           if ( fixed_nres1==-1 || fixed_nres2==-1)
3937                                             {
3938                                               fprintf ( stderr, "\nPB: Sequence %s, Residue %d : Cache=%d",A->name[a], nres1,fixed_nres1 );
3939                                               fprintf ( stderr, "\nPB: Sequence %s, Residue %d : Cache=%d",A->name[b], nres2,fixed_nres2 );
3940                                               
3941                                               myexit(EXIT_FAILURE);
3942                                             }
3943                                           
3944                                           if ( fixed_nres1 && fixed_nres2)
3945                                             {
3946                                              
3947                                               
3948
3949                                               /*
3950                                                 This code was uncommented to make profile2seq simpler
3951                                                 Must check how this affects other functions
3952                                               
3953                                               vwrite_clist (CL,CL->ne, SEQ1, (s1<s2)?s1:s2);
3954                                               vwrite_clist (CL,CL->ne, SEQ2, (s1<s2)?s2:s1);
3955                                               vwrite_clist (CL,CL->ne, R1,   (s1<s2)?fixed_nres1:fixed_nres2);
3956                                               vwrite_clist (CL,CL->ne, R2,   (s1<s2)?fixed_nres2:fixed_nres1);
3957                                               */
3958
3959                                               vwrite_clist (CL,CL->ne, SEQ1, s1);
3960                                               vwrite_clist (CL,CL->ne, SEQ2, s2);
3961                                               vwrite_clist (CL,CL->ne, R1,fixed_nres1);
3962                                               vwrite_clist (CL,CL->ne, R2,fixed_nres2);
3963                                               
3964                                               if (do_pdb)
3965                                                 {
3966
3967                                                   vwrite_clist (CL,CL->ne, WE,(NORM_F/MAXID)*pdb_weight );
3968                                                 }
3969                                               else
3970                                                 {
3971                                                   
3972                                                   vwrite_clist (CL,CL->ne, WE,(NORM_F/MAXID)*((weight[0]==FORBIDEN)?weight[1]:weight[c]) );
3973                                                   
3974                                                 }
3975                                               vwrite_clist (CL,CL->ne, CONS,1);
3976                                               if (!set_misc)
3977                                                 {
3978                                                   vwrite_clist (CL,CL->ne, MISC,A->len_aln);
3979                                                   set_misc=1;
3980                                                 }
3981                                               else 
3982                                                 {
3983                                                   vwrite_clist (CL,CL->ne, MISC,0);
3984                                                 }
3985                                               CL->ne++;
3986                                               /*
3987                                               if (isgop1)
3988                                                 {
3989
3990                                                    vwrite_clist (CL,CL->ne, SEQ1, s1);
3991                                                    vwrite_clist (CL,CL->ne, SEQ2, s1);
3992                                                    vwrite_clist (CL,CL->ne, R1,fixed_nres1);
3993                                                    vwrite_clist (CL,CL->ne, R2,fixed_nres1);
3994                                                    vwrite_clist (CL,CL->ne, WE, (weight[0]==FORBIDEN)?weight[1]:weight[c] );
3995                                                    vwrite_clist (CL,CL->ne, CONS,1);
3996                                                    CL->ne++;
3997                                                 }
3998                                               if (isgop2)
3999                                                 {
4000                                                   
4001                                                    vwrite_clist (CL,CL->ne, SEQ1, s2);
4002                                                    vwrite_clist (CL,CL->ne, SEQ2, s2);
4003                                                    vwrite_clist (CL,CL->ne, R1,fixed_nres2);
4004                                                    vwrite_clist (CL,CL->ne, R2,fixed_nres2);
4005                                                    vwrite_clist (CL,CL->ne, WE, (weight[0]==FORBIDEN)?weight[1]:weight[c] );
4006                                                    vwrite_clist (CL,CL->ne, CONS,1);
4007                                                    CL->ne++;
4008                                                 }
4009                                               */
4010                                             }
4011                                               
4012                                         }
4013                                 }
4014                             }
4015                         }
4016                 }
4017         
4018         vfree (weight);
4019         if (A->A) 
4020           {
4021             return aln2constraint_list (A->A, CL, weight_mode);
4022           }
4023         else
4024           return CL;
4025        } 
4026 double **list2mat (Constraint_list *CLin,int s1,int s2, double *min, double *max)
4027         {
4028         double ** mat;
4029         int a, r1, r2;
4030         int min_def=0;
4031         Constraint_list *CL;
4032         static Sequence *S;
4033
4034         
4035         int row, column;
4036         if ( S==NULL)S=declare_sequence ((CLin->S)->min_len, (CLin->S)->max_len,(CLin->S)->nseq);       
4037         sprintf ( S->name[0], "%s",(CLin->S)->name[s1]);
4038         sprintf ( S->name[1],"%s",(CLin->S)->name[s2]);
4039         S->nseq=2;
4040         
4041         row   =(CLin->S)->len[s1];
4042         column=(CLin->S)->len[s2];
4043         
4044         if ( CLin->extend_jit)  
4045             CL=extend_list(CLin,"mem",CLin->extend_clean_mode, CLin->extend_compact_mode, CLin->do_self, S);
4046         else
4047             CL=CLin;
4048
4049
4050         min[0]=max[0];
4051         mat=declare_double ( row, column);
4052
4053         for ( a=0; a<CL->ne; a++)
4054             {
4055             r1=vread_clist(CL,a,R1)-1;
4056             r2=vread_clist(CL,a,R2)-1;
4057             if ( vread_clist(CL,a,SEQ1)==s1 &&vread_clist(CL,a,SEQ2)==s2)
4058                 {
4059                 mat[r1][r2]=(double)vread_clist(CL,a,WE);
4060                 if (min_def==0)
4061                    {
4062                    min_def=1;
4063                    min[0]=mat[r1][r2];
4064                    max[0]=mat[r1][r2];
4065                    }
4066                 else
4067                    {
4068                    min[0]=(min[0]<mat[r1][r2])?min[0]:mat[r1][r2];
4069                    max[0]=(max[0]>mat[r1][r2])?max[0]:mat[r1][r2];
4070                    }
4071                 }
4072             else if (vread_clist(CL,a,SEQ2)==s1 &&vread_clist(CL,a,SEQ1)==s2) 
4073                 {
4074                 mat[r2][r1]=(double)vread_clist(CL,a,WE);
4075                         if (min_def==0)
4076                    {
4077                    min_def=1;
4078                    min[0]=mat[r2][r1];
4079                    max[0]=mat[r2][r1];
4080                    }
4081                 else
4082                    {
4083                    min[0]=(min[0]<mat[r2][r1])?min[0]:mat[r2][r1];
4084                    max[0]=(max[0]>mat[r2][r1])?max[0]:mat[r2][r1];
4085                    }
4086                 }
4087             }
4088         return mat;
4089         }
4090
4091 Constraint_list * constraint_list2bin_file(Constraint_list *clist)
4092         {
4093         int a,b;
4094         
4095         clist->fp=tmpfile();
4096         for ( a=0; a< clist->ne; a++)
4097             for ( b=0; b<clist->entry_len; b++)
4098                 {
4099                 fwrite (&clist->L[a*clist->entry_len+b],clist->el_size, 1,clist->fp);
4100                 }
4101         return clist;
4102         }
4103
4104 FILE * bin_file2constraint_list ( Constraint_list *CL, FILE *fp, char *name)
4105         {
4106         int a, b, s1, s2;
4107         CLIST_TYPE *entry;
4108         
4109         if ( fp==NULL)fp=vfopen ( name, "w");
4110         entry=vcalloc ( CL->entry_len, CL->el_size);
4111         fprintf ( fp, "%d\n", (CL->S)->nseq);
4112         for ( a=0; a< (CL->S)->nseq; a++)fprintf (fp, "%s %d %s\n", (CL->S)->name[a], (CL->S)->len[a], (CL->S)->seq[a]);
4113         
4114
4115         rewind ( CL->fp);
4116         fread(entry, CL->el_size, CL->entry_len, CL->fp);
4117         s1=entry[SEQ1];
4118         s2=entry[SEQ2];
4119         fprintf (fp, "#%d %d\n", s1, s2);
4120         for ( b=2; b< CL->entry_len; b++)fprintf (fp, "%5d ",entry[b]);
4121         fprintf (fp, "\n");
4122         for ( a=1; a< (CL->ne); a++)
4123             {
4124             fread(entry, CL->el_size, CL->entry_len, CL->fp);
4125             if ( entry[SEQ1]!=s1 || entry[SEQ2]!=s2)
4126                {
4127                s1=entry[SEQ1];
4128                s2=entry[SEQ2];
4129                fprintf (fp, "#%d %d\n", s1, s2);
4130                }
4131             for ( b=2; b< CL->entry_len; b++)fprintf (fp, "%5d ",entry[b]);
4132             fprintf (fp, "\n");
4133             }
4134         fprintf (fp, "! CPU %d\n",get_time());
4135         
4136         return fp;
4137         }
4138 int **list2residue_total_weight ( Constraint_list *CL)
4139         {
4140           /*Returns 
4141             tot_weight[nseq][maxlen]
4142             where each residue is associated with the total of its weights in CL
4143             ####IMPORTANT
4144             
4145                   -the numbering of the residues  goes from 1 to L:
4146                   -the numbering of the sequences goes from 0 to N-1:
4147           */
4148
4149         int **tot_weight;
4150         int s1, s2, r1, r2, w, a;
4151
4152
4153         tot_weight=declare_int ( (CL->S)->nseq, (CL->S)->max_len+1);
4154         for ( a=0; a<CL->ne; a++)
4155             {
4156             r1=vread_clist(CL,a,R1)-1;
4157             r2=vread_clist(CL,a,R2)-1;
4158             s1=vread_clist(CL,a,SEQ1);
4159             s2=vread_clist(CL,a,SEQ2);
4160             w=vread_clist(CL,a,WE);
4161             
4162             tot_weight[s1][r1]+=w;
4163             tot_weight[s2][r2]+=w;
4164             }
4165         return tot_weight;
4166         }
4167
4168 int **list2residue_total_extended_weight ( Constraint_list *CL)
4169         {
4170           /*Returns 
4171             tot_extended_weight[nseq][maxlen]
4172             where each residue is associated with the total of its weights in CL
4173             ####IMPORTANT
4174             
4175                   -the numbering of the residues  goes from 1 to L:
4176                   -the numbering of the sequences goes from 0 to N-1:
4177           */
4178
4179         static int **tot_extended_weight;
4180         int s1, s2, r1, r2, w;
4181         
4182         if (CL->residue_indexed && tot_extended_weight);
4183         else
4184           {
4185             if (tot_extended_weight) free_int (tot_extended_weight, -1);
4186             if (CL->residue_indexed==0)index_res_constraint_list (CL,WE);
4187             
4188
4189             tot_extended_weight=declare_int ( (CL->S)->nseq, (CL->S)->max_len+1);
4190             
4191             for ( s1=0; s1< (CL->S)->nseq-1; s1++)
4192               for ( s2=s1+1; s2< (CL->S)->nseq; s2++)
4193                 for (r1=1; r1<=(CL->S)->len[s1]; r1++)
4194                   for (r2=1; r2<=(CL->S)->len[s2]; r2++)
4195                     {
4196                       w=(CL->evaluate_residue_pair)( CL, s1, r1, s2, r2);
4197                       tot_extended_weight[s1][r1]+=w;
4198                       tot_extended_weight[s2][r2]+=w;
4199                     }
4200           }
4201         return tot_extended_weight;
4202         }
4203 int **list2residue_partial_extended_weight ( Constraint_list *CL)
4204         {
4205           /*Returns 
4206             tot_extended_weight[nseq][maxlen]
4207             where each residue is associated with the total of its weights in CL
4208             ####IMPORTANT
4209             
4210                   -the numbering of the residues  goes from 1 to L:
4211                   -the numbering of the sequences goes from 0 to N-1:
4212           */
4213
4214         int **tot_extended_weight;
4215         int s1, s2, r1, r2, w1, w2, a;
4216
4217
4218         tot_extended_weight=declare_int ( (CL->S)->nseq, (CL->S)->max_len+1);
4219         for ( a=0; a<CL->ne; a++)
4220             {
4221             r1=vread_clist(CL,a,R1);
4222             r2=vread_clist(CL,a,R2);
4223             s1=vread_clist(CL,a,SEQ1);
4224             s2=vread_clist(CL,a,SEQ2);
4225             w1=(CL->evaluate_residue_pair)( CL, s1, r1, s2, r2);
4226             w2=(CL->evaluate_residue_pair)( CL, s2, r2, s1, r1);
4227             if ( w1!=w2)fprintf ( stderr, "*");
4228
4229             tot_extended_weight[s1][r1]+=w1;
4230             tot_extended_weight[s2][r2]+=w2;
4231             }
4232         return tot_extended_weight;
4233         }
4234   
4235
4236 /*******************************************************************************************/
4237 /*                                                                                         */
4238 /*                                                                                         */
4239 /*                              clean functions                                            */
4240 /*                                                                                         */
4241 /*                                                                                         */
4242 /*                                                                                         */
4243 /*******************************************************************************************/
4244 Constraint_list *clean ( char *clean_mode,Constraint_list *CL,int start, int len)
4245         {
4246         
4247         if ( strm ( clean_mode, "shadow"))   CL=clean_shadow (CL,start,len);    
4248         else if ( strm5( clean_mode, "","NO","no","No","default"));
4249         else    add_warning ( CL->local_stderr, "\nWARNING: The %s CLEANING MODE DOES NOT EXIST\n", clean_mode);
4250         
4251         return CL;
4252         }
4253
4254
4255 Constraint_list * clean_shadow ( Constraint_list *CL, int start, int len)
4256         {
4257         int s1, s2, r1, a, b, end;
4258         int max, min;
4259                 
4260         s1=vread_clist (CL, start, SEQ1);
4261         s2=vread_clist (CL, start, SEQ2);
4262         r1=vread_clist (CL, start, R1);
4263         
4264         
4265         for ( a=start; a<(start+len);)
4266                 {
4267                 
4268                 max=min=vread_clist (CL, a, WE);
4269                 while ( a<CL->ne && vread_clist (CL, a, SEQ1)==s1 && vread_clist (CL, a, SEQ2)==s2 && vread_clist (CL, a, R1)==r1)
4270                         {
4271                         max=(vread_clist (CL, a, WE)>max)?vread_clist (CL, a, WE):max;
4272                         min=(vread_clist (CL, a, WE)<min)?vread_clist (CL, a, WE):min;
4273                         a++;
4274                         }
4275                 end=a;
4276                 
4277                 if ((end-start)>1)
4278                         {
4279                         for ( b=start; b<end; b++)
4280                                 if ( vread_clist (CL, b, WE)<max)vwrite_clist (CL, b, SEQ1,-1);
4281                         }
4282                 start=end;
4283                 if ( start<CL->ne)
4284                         {
4285                         s1=vread_clist (CL, start, SEQ1);
4286                         s2=vread_clist (CL, start, SEQ2);
4287                         r1=vread_clist (CL, start, R1);
4288                         }
4289                 }
4290         CL=sort_constraint_list_inv (CL, start, (CL->ne-start));
4291         CL=sort_constraint_list (CL,start,(CL->ne-start)  );    
4292
4293         return CL;
4294         }
4295 /*********************************************************************/
4296 /*                                                                   */
4297 /*                         LIST FUNCTIONS                            */
4298 /*                                                                   */
4299 /*                                                                   */
4300 /*********************************************************************/ 
4301
4302 static Constraint_list *fast_merge_constraint_list   ( Constraint_list *SL, Constraint_list *ML, char *mode);
4303 static Constraint_list *slow_merge_constraint_list   ( Constraint_list *SL, Constraint_list *ML, char *mode);
4304
4305 Constraint_list *merge_constraint_list   ( Constraint_list *SL, Constraint_list *ML, char *mode)
4306 {
4307
4308
4309   if ( !ML) 
4310     {
4311       return SL;
4312     }
4313   else if ( SL->S == ML->S)
4314     {
4315       return fast_merge_constraint_list (SL, ML, mode);
4316     }
4317   else
4318     {
4319       return slow_merge_constraint_list (SL, ML, mode);
4320     }
4321 }
4322
4323 Constraint_list *fast_merge_constraint_list   ( Constraint_list *SL, Constraint_list *ML, char *mode)
4324 {
4325   int l;
4326
4327   l=ML->ne+SL->ne;
4328
4329   ML->max_L_len=l;
4330   ML->seq_indexed=0;
4331   ML->residue_indexed=0;
4332   if (ML->ne==0 || !ML->L)ML->L=vcalloc (l*ML->entry_len, sizeof (int));
4333   else ML->L=vrealloc (ML->L, sizeof (int)*l*ML->entry_len);
4334   
4335   memcpy (ML->L+(ML->ne*ML->entry_len), SL->L, SL->ne*sizeof (int)*SL->entry_len);
4336  
4337   ML->ne+=SL->ne;
4338   return ML;
4339   
4340 }
4341
4342 Constraint_list *slow_merge_constraint_list   ( Constraint_list *SL, Constraint_list *ML, char *mode)
4343 {
4344   int a, s1, s2;
4345   Sequence *S1, *S2;
4346   int **name_index;
4347   int **seq_index;
4348   CLIST_TYPE *entry=NULL;
4349
4350   if ( !ML)return SL;
4351   
4352   S1=SL->S;S2=ML->S;
4353   
4354   name_index=index_seq_name(S1,S2);
4355   seq_index=index_seq_res (S1,S2, name_index);
4356   
4357   for ( a=0; a< SL->ne; a++)
4358     {
4359       
4360       entry=extract_entry ( entry, a, SL);
4361       //HERE ("BEF: %d %d %d %d", entry[SEQ1], entry[SEQ2], entry[R1], entry[R2]); 
4362       s1=entry[SEQ1]; s2=entry[SEQ2];
4363  
4364       if ( S1==S2)
4365         {
4366          add_entry2list(entry, ML);
4367         }
4368       else if (name_index[s1][0]==-1 || name_index[s2][0]==-1 || !seq_index[s1] || !seq_index[s2]);
4369       else 
4370         {
4371           int r1, r2;
4372           r1=seq_index[s1][entry[R1]-1];
4373           r2=seq_index[s2][entry[R2]-1];
4374         
4375           entry[SEQ1]=name_index[s1][0];
4376           entry[SEQ2]=name_index[s2][0];
4377           if ( r1!=-1 && r2!=-1 && entry[SEQ1]!=entry[SEQ2])
4378             {
4379               entry[R1]=r1+1; entry[R2]=r2+1;
4380               add_entry2list(entry, ML);
4381             }
4382           //HERE ("AFT: %d %d %d %d", entry[SEQ1], entry[SEQ2], entry[R1], entry[R2]); 
4383         }
4384     }
4385   free_int (name_index, -1);
4386   free_int ( seq_index, -1);
4387   vfree ( entry);
4388   return ML;
4389 }
4390   
4391 Constraint_list *modify_weight( Constraint_list *CL,int start, int end,  char *modify_mode)
4392         {
4393         int a;
4394         CLIST_TYPE x;
4395         
4396         if ( strm(modify_mode, "default"))return CL;
4397         for ( a=start; a<end; a++)
4398             {
4399             x=vread_clist(CL, a, WE);
4400             
4401             if (strm2 (modify_mode,"sc_eq_cons", "we_eq_cons"))
4402                 if(x!=UNDEFINED)
4403                     vwrite_clist(CL, a, WE,  vread_clist(CL, a, CONS));
4404             
4405             if (strm2(modify_mode,"sc_eq_wePcons","sc_eq_consPwe"))
4406                 if(x!=UNDEFINED)
4407                      vwrite_clist(CL, a, WE, vread_clist(CL, a, CONS)*x);               
4408             }
4409         return CL;
4410         }
4411         
4412 Constraint_list *compact_list (Constraint_list *CL, int start, int len, char *compact_mode)
4413         {
4414         int a;
4415         int r1, r2, rr1, rr2, s1, rs1, s2, rs2, ra;
4416         CLIST_TYPE x;
4417         int debug_compact=0;
4418
4419         if (debug_compact)fprintf ( stderr, "\n[In: %d %s start=%d len=%d", CL->ne, compact_mode, start, len);
4420
4421         if ( len==0  || strm3(compact_mode, "no", "No", "NO"))return CL;
4422         else if ( strm2(compact_mode,"mirror","mirror_sum"));
4423         else if ( strm4(compact_mode, "default","shrink","shrink_best","shrink_worst"))
4424                 {
4425                 
4426                 for ( a=start; a<(start+len) ; a++)
4427                     {
4428                     
4429                     if ( vread_clist(CL, a, SEQ1)> vread_clist(CL, a, SEQ2) ||\
4430                        ( vread_clist(CL, a, SEQ1)==vread_clist(CL, a, SEQ2) &&\
4431                          vread_clist(CL, a, R1)  > vread_clist(CL, a, R2)     ))
4432                         
4433                         
4434                         {
4435                         s1=vread_clist(CL, a, SEQ1);
4436                         s2=vread_clist(CL, a, SEQ2);
4437                         r1=vread_clist(CL, a, R1);
4438                         r2=vread_clist(CL, a, R2);
4439                         vwrite_clist(CL, a, SEQ1,s2);
4440                         vwrite_clist(CL, a, SEQ2,s1);
4441                         vwrite_clist(CL, a, R1,r2);
4442                         vwrite_clist(CL, a, R2,r1);
4443                         }
4444                     }
4445                 }
4446         
4447         if (debug_compact)fprintf ( stderr, "\n[2: %d %s start=%d len=%d", CL->ne, compact_mode, start, len);
4448
4449         sort_constraint_list ( CL, start, len);
4450         
4451         rs1=vread_clist(CL, start, SEQ1);       
4452         rs2=vread_clist(CL, start, SEQ2);
4453         rr1=vread_clist(CL, start, R1);
4454         rr2=vread_clist(CL, start, R2);
4455         ra=start;
4456
4457         if (debug_compact)fprintf ( stderr, "\n[3: %d %s start=%d len=%d", CL->ne, compact_mode, start, len);
4458
4459
4460         if ( (rs1==rs2) && (rr1==rr2))vwrite_clist(CL, start, SEQ1,-1);         
4461         for ( a=start+1; a<(start+len); a++)
4462                 {
4463                 s1=vread_clist(CL, a, SEQ1);
4464                 s2=vread_clist(CL, a, SEQ2);
4465                 r1=vread_clist(CL, a, R1);
4466                 r2=vread_clist(CL, a, R2);
4467                 
4468                 //if ( (s1==s2) && (r1==r2))vwrite_clist(CL, a, SEQ1, -1);
4469                 if ( s1==rs1 && s2==rs2 && r1==rr1 && r2==rr2)
4470                         {
4471                         x=vread_clist(CL, ra, WE);
4472                         if (strm ( compact_mode, "shrink"));
4473                         else if (strm( compact_mode,"default"))//default is set to best
4474                           vwrite_clist(CL, ra, WE,MAX(vread_clist(CL, a, WE),x));
4475                         else if ( strm ( compact_mode,"mirror_sum"))    
4476                           vwrite_clist(CL, ra, WE, vread_clist(CL, a, WE)+x);
4477                         else if (strm2 ( compact_mode,"best", "shrink_best"))
4478                           vwrite_clist(CL, ra, WE,MAX(vread_clist(CL, a, WE),x));
4479                         else if (strm2 ( compact_mode, "worst","shrink_worst"))
4480                           vwrite_clist(CL, ra, WE,MIN(vread_clist(CL, a, WE), vread_clist(CL, a, WE)));
4481                 
4482                         if (  strm(compact_mode, "shrink"));
4483                         else
4484                             {
4485                             vwrite_clist(CL, ra, CONS, vread_clist(CL, ra, CONS)+ vread_clist(CL, a, CONS));
4486                             vwrite_clist(CL, ra, MISC, vread_clist(CL, ra, MISC)+ vread_clist(CL, a, MISC));
4487                             }
4488                         vwrite_clist(CL,a, SEQ1, -1);
4489                         
4490                         }
4491                 else
4492                         {
4493                         rs1=s1;
4494                         rs2=s2;
4495                         rr1=r1;
4496                         rr2=r2;
4497                         ra=a;
4498                         }
4499                 }
4500         
4501         
4502         sort_constraint_list_inv(CL,0,CL->ne);
4503         
4504         sort_constraint_list    (CL,0,CL->ne);
4505         
4506         
4507         if ( strm3 (compact_mode, "consPwe", "wePcons","cons"))
4508                 {
4509                 for ( a=start; a<(start+len); a++)
4510                         {
4511                         if ( strm2(compact_mode,"consPwe", "wePcons"))
4512                             vwrite_clist(CL, a, WE, vread_clist(CL,a,WE)* vread_clist(CL,a,CONS));
4513                         else if  (strm (compact_mode, "cons"))
4514                              vwrite_clist(CL, a, WE, vread_clist(CL,a,CONS)*100);
4515                         }
4516                 }
4517         if (debug_compact)fprintf ( stderr, "....OUT: %d]\n", CL->ne);  
4518         return CL;
4519         }
4520
4521
4522 Constraint_list *rescale_list_simple (Constraint_list *CL,int start, int len,int new_min, int new_max)
4523         {
4524         int a, min, max;
4525         double x;
4526         /*Rescales between 0 and max2
4527           Any value above max1 is set to max1 first and then to max2
4528         */
4529
4530
4531
4532         min=max=vread_clist ( CL,start, WE);
4533         
4534         for ( a=start; a<(start+len); a++)
4535                 {
4536                 x=(double)vread_clist ( CL,a, WE);
4537                 if ( x>max)max=(int)x;
4538                 if ( x< min)min=(int)x;
4539                 }
4540         
4541         fprintf ( CL->local_stderr, "\n[%d-%d]=>[%d-%d]", min, max, new_min, new_max);
4542
4543         for ( a=start; a<(start+len); a++)
4544             {
4545               
4546             x=vread_clist(CL,a, WE);
4547            
4548             if ((max-min)==0)x=100;
4549             else x=(((x-min)/(max-min))*new_max)+new_min;
4550             
4551             vwrite_clist(CL, a, WE,(CLIST_TYPE) x);
4552             }
4553         return CL;
4554         }
4555 Constraint_list *rescale_list (Constraint_list *CL,int start, int len,int max1, int max2)
4556         {
4557         int a, min_val, max_val;
4558         CLIST_TYPE x;
4559         
4560         /*Rescales between 0 and max2
4561           Any value above max1 is set to max1 first and then to max2
4562         */
4563
4564
4565         min_val=0;
4566         max_val=max1;
4567
4568         
4569         
4570         for ( a=start; a<len; a++)
4571                 {
4572                 if (vread_clist ( CL,a, WE)>max1)vwrite_clist(CL, a, WE, max1);
4573                 }
4574         
4575         for ( a=start; a<len; a++)
4576             {
4577             x=vread_clist(CL,a, WE);
4578             vwrite_clist(CL, a, WE, (((x-min_val)*max2)/(max_val-min_val)));
4579             }
4580         return CL;
4581         }
4582
4583
4584 Constraint_list* filter_list (Constraint_list *CL, int start, int len,int T)
4585         {
4586         int a;
4587         int field;
4588         
4589         if (T==0)return CL;
4590         
4591         field=WE;
4592         if (T>0)field=WE;
4593         else if ( T<0)
4594                 {
4595                 field=CONS;
4596                 T=-T;
4597                 }
4598         
4599         
4600         for ( a=start; a<len; a++)
4601             if (vread_clist(CL, a, field)<=T)vwrite_clist(CL,a,SEQ1,-1);
4602         
4603         CL=sort_constraint_list_inv (CL, 0, CL->ne);
4604         CL=sort_constraint_list     (CL, 0, CL->ne);
4605         return CL;
4606         }
4607
4608
4609
4610
4611
4612 Constraint_list *undefine_list (Constraint_list *CL)
4613       {
4614       int a, b;
4615       int undefined_flag;
4616       
4617       for ( a=0;a<CL->ne; a++)
4618           {
4619           for ( b=0, undefined_flag=0; b< LIST_N_FIELDS; b++)
4620               {
4621
4622               if ( vread_clist (CL, a, b)==UNDEFINED)undefined_flag=1;    
4623               if ( undefined_flag)
4624                   { 
4625                   for ( b=0; b< LIST_N_FIELDS; b++)
4626                       if ( b!=SEQ1 && b!=SEQ2 && b!=R1 && b!=R2)
4627                           {
4628                           vwrite_clist(CL, a, b, UNDEFINED);
4629                           }
4630                   }
4631               }
4632           }
4633       return CL;
4634       }
4635
4636 int ** seq2defined_residues ( Sequence *S, Constraint_list *CL)
4637 {
4638   int **seq_count;
4639   int *entry=NULL;
4640   int a;
4641   
4642   seq_count=declare_int (S->nseq, S->max_len+1);
4643   for (a=0; a< CL->ne; a++)
4644       {
4645         entry=extract_entry(entry, a, CL);
4646         seq_count[entry[SEQ1]][entry[R1]]++;
4647         seq_count[entry[SEQ2]][entry[R2]]++;
4648       }
4649   vfree (entry);
4650   return seq_count;
4651 }
4652
4653 int ** aln2defined_residues ( Alignment *A, Constraint_list *CL)
4654 {
4655   
4656   int **seq_count;
4657   int **aln_count;
4658   int **pos;
4659   int a,ra, b;
4660   
4661   pos=aln2pos_simple(A, A->nseq);
4662   seq_count=seq2defined_residues(CL->S, CL);
4663   aln_count=declare_int (A->nseq, A->len_aln);
4664   for (a=0; a< A->nseq; a++)
4665     {
4666       ra=name_is_in_list(A->name[a], (CL->S)->name, (CL->S)->nseq, 100);
4667       if ( ra==-1) continue;
4668       for ( b=0; b<A->len_aln; b++)
4669         if (pos[a][b]>0 && seq_count[ra][pos[a][b]]>0)aln_count[a][b]=1;
4670     }
4671
4672   free_int (seq_count, -1);
4673   free_int (pos,-1);
4674   return aln_count;
4675 }
4676
4677 /*********************************************************************/
4678 /*                                                                   */
4679 /*                        DEBUG CONSTRAINT_LIST                       */
4680 /*                                                                   */
4681 /*                                                                   */
4682 /*********************************************************************/   
4683 void check_seq_pair_in_list(Constraint_list *CLin,int seq1, int seq2)
4684       {
4685       int a, s1, s2, r1, r2;
4686            
4687       for ( a=0; a< CLin->ne; a++)
4688           {
4689           s1=vread_clist(CLin,a,SEQ1);
4690           s2=vread_clist(CLin,a,SEQ2);
4691           if ( s1==seq1 && s2==seq2)
4692              {
4693              r1=vread_clist(CLin,a,R1);
4694              r2=vread_clist(CLin,a,R2);
4695              fprintf ( stderr, "\n[%d][%d %d] [%d %d]",a,s1, r1, s2, r2);
4696              }
4697           }
4698       }
4699
4700 void print_CL_mem(Constraint_list *CL, char *function)
4701      {
4702       fprintf ( stderr,"%s\n", function);
4703      if ( CL->fp==NULL && CL->L==NULL) fprintf ( stderr, "\n\tNOTHING");
4704      if ( CL->fp)fprintf ( stderr, "\n\tFILE SET");
4705      if ( CL->L)fprintf ( stderr, "\n\tMEM SET\n");
4706      }
4707
4708 int constraint_list_is_sorted ( Constraint_list *CL)
4709      {
4710      int a,b, x1, x2;
4711      for ( a=0; a< CL->ne-1; a++)
4712          {
4713          for ( b=0; b< CL->entry_len; b++)
4714              {
4715              x1=vread_clist( CL, a, b);
4716              x2=vread_clist( CL, a+1,b);
4717              if ( x1<x2)break;
4718              else if ( x1>x2)
4719                  {
4720                  fprintf ( stderr, "\n[%d][%d]=>%d\n[%d][%d]=>%d\n\n",a, b, x1, a+1, b, x2);
4721                  return 0;
4722                  }
4723              
4724              }
4725          }
4726      return 1;
4727      }
4728 /*********************************************************************/
4729 /*                                                                   */
4730 /*                        PRUNE CONSTRAINT_LIST                     */
4731 /*                                                                   */
4732 /*                                                                   */
4733 /*********************************************************************/
4734 char * list2prune_list_old ( Sequence *S, int **sm)
4735 {
4736   int a, b, i;
4737   char *aln, *seq, *file;
4738   FILE *fp;
4739   Sequence *subS;
4740   Alignment *A;
4741   
4742   aln=vtmpnam (NULL);
4743   seq=vtmpnam (NULL);
4744   file=vtmpnam (NULL);
4745   
4746   output_fasta_seq (seq, A=seq2aln (S,NULL,RM_GAP));
4747   free_aln (A);
4748   
4749   printf_system ( "t_coffee %s -in Xblosum62mt -outfile=%s -msa_mode iterative_tree_aln", seq, aln);
4750   printf_system  ( "t_coffee -other_pg seq_reformat -in %s -action +trim _aln_n5 -output fasta_seq > %s",aln, seq );
4751   subS=main_read_seq ( seq);
4752
4753   fp=vfopen (file, "w");
4754   for ( a=0; a< subS->nseq; a++)
4755     {
4756       i=name_is_in_list (subS->name[a], S->name, S->nseq, 100);
4757       if ( i==-1) continue;
4758       for ( b=0; b<S->nseq; b++)
4759         if (i!=b)fprintf ( fp, "\n2 %d %d",i, b);
4760     }
4761   vfclose (fp);
4762   return file;
4763 }
4764
4765 char * list2prune_list ( Sequence *S, int **sm)
4766 {
4767   int a, b, c;
4768   int **mat, *used, *keep;
4769   int nk=0, n=0;
4770   int ns=4;
4771   char *file;
4772   FILE *fp;
4773   
4774   n=S->nseq;
4775   
4776   if (get_string_variable("prune_lib_mode"))
4777     ns=atoi(get_string_variable("prune_lib_mode"));
4778   
4779   HERE ("NS=%d", ns);
4780   if (ns==0)ns=n;
4781   else if (ns<0)ns=-(n*ns)/100;
4782   else if (ns>=n)ns=n;
4783   
4784   
4785   HERE ("NS=%d", ns);
4786   
4787
4788   keep=vcalloc (n, sizeof (int));
4789   used=vcalloc (n, sizeof (int));
4790   mat=declare_int (n, n);
4791   file=vtmpnam (NULL);
4792  
4793   //1-Identify the seed sequence: the one on average the further away from the rest
4794   for (a=0; a<n;a++)
4795     {
4796       mat[a][0]=a;
4797       for (b=0; b<n; b++)
4798         {
4799           mat[a][1]+=sm[a][b];
4800         }
4801       sort_int_inv (mat, 2, 1, 0, n-1);
4802     }
4803   
4804   keep[nk++]=mat[0][0];
4805   used[mat[0][0]]=1;
4806  
4807   for (a=1; a<ns; a++)
4808     {
4809       for (c=0; c<n; c++)
4810         {
4811           mat[c][0]=c;
4812           if (used[c]){mat[c][1]=n*1000;continue;}
4813           for (b=0; b<nk; b++)
4814             {
4815               mat[c][1]+=sm[c][keep[b]];
4816             }
4817         }
4818       sort_int (mat, 2, 1, 0,n-1);
4819       keep[nk++]=mat[0][0];
4820       used[mat[0][0]]=1;
4821     }
4822  
4823   fp=vfopen (file, "w");
4824   for ( a=0; a<nk; a++)
4825     {
4826       for (b=0; b<S->nseq; b++)
4827          if (keep[a]!=b)
4828            {
4829              fprintf ( fp, "\n2 %d %d", keep[a], b);
4830            }
4831     }
4832    vfclose (fp);
4833    vfree (keep); vfree (used);free_int (mat, -1);
4834    
4835    return file;
4836 }
4837   
4838
4839 char * list2prune_list_old3 ( Sequence *S, int **sm)
4840 {
4841   int **keep;
4842   int a,b,c,n,s1,s2, tot, nseq;
4843
4844   char *file;
4845   FILE *fp;
4846   nseq=S->nseq;
4847   
4848   keep=declare_int (nseq, nseq);
4849
4850   for (a=0; a< nseq; a++)
4851     for (b=a+1; b< nseq; b++)
4852       {
4853         int bc=0,bsim=0;
4854         s1=sm[a][b];
4855         for (c=0; c< nseq; c++)
4856           {
4857             if ( c==a || c==b) continue;
4858             s2=MIN(sm[a][c], sm[c][b]);
4859             
4860             if (s2>s1 && s2>bsim)
4861               {
4862                 bsim=s2;
4863                 bc=c;
4864               }
4865           }
4866         if ( bsim) 
4867           {
4868             keep[a][bc]=1;
4869             keep[bc][a]=1;
4870             keep[b][bc]=1;
4871             keep[bc][b]=1;
4872           }
4873       }
4874
4875   file=vtmpnam (NULL);
4876   fp=vfopen (file, "w");
4877   
4878   for (n=0,tot=0,a=0; a< nseq; a++)
4879     for ( b=a+1; b< nseq; b++)
4880       {
4881         if (keep[a][b])
4882           fprintf (fp, "\n 2 %d %d", a, b);
4883       }
4884   vfclose (fp);
4885   return file;
4886 }
4887   
4888 /*********************************************************************/
4889 /*                                                                   */
4890 /*                        WEIGHT CONSTRAINT_LIST                     */
4891 /*                                                                   */
4892 /*                                                                   */
4893 /*********************************************************************/
4894
4895 Constraint_list *weight_constraint_list(Constraint_list * CL, char *seq_weight)
4896
4897     {
4898       Weights *W;
4899      
4900       if ( CL->ne==0)return CL;
4901       else if ( strm(seq_weight, "t_coffee")) W=compute_t_coffee_weight(CL);
4902       else if (check_file_exists (seq_weight))
4903         {
4904           W=read_seq_weight ((CL->S)->name, (CL->S)->nseq, seq_weight);
4905         }
4906       else 
4907         {
4908           int a;
4909           W=declare_weights((CL->S)->nseq);
4910           sprintf ( W->mode, "no_seq_weight");
4911           for ( a=0; a<(CL->S)->nseq; a++)
4912             {
4913               sprintf ( W->seq_name[a], "%s", (CL->S)->name[a]);
4914               W->SEQ_W[a]=1;
4915             }
4916           CL->W=W;
4917           return CL;
4918         }
4919      
4920       CL=re_weight_constraint_list (CL,W);
4921      
4922       CL->W=W;
4923       
4924      
4925       return CL;
4926       
4927       
4928     }
4929       
4930
4931
4932 Weights* compute_t_coffee_weight(Constraint_list * CL)
4933     {
4934       int a, b;
4935       float p, d;
4936       Weights *W;
4937       int nseq;
4938       
4939
4940       
4941
4942
4943       if (!CL->L)return NULL;
4944       
4945       nseq=(CL->S)->nseq;
4946       W=declare_weights(nseq);
4947       sprintf ( W->mode, "t_coffee");
4948       for ( a=0; a< nseq; a++)
4949          {
4950            sprintf ( W->seq_name[a], "%s", (CL->S)->name[a]);
4951            W->SEQ_W[a]=1;
4952          }
4953      
4954       
4955       for (a=0; a< (CL->S)->nseq-1; a++)
4956         for ( b=a+1; b< (CL->S)->nseq; b++)
4957           {
4958             if ( b==a){d=1;}
4959             else if ( !(CL->S)->len[b] || !(CL->S)->len[a])d=1;
4960             else
4961               {
4962                 d=((float)(CL->DM)->similarity_matrix[a][b]/MAXID)*10;
4963               }
4964             p=pow(d,3);
4965             
4966             W->SEQ_W[a]+=p;
4967             W->SEQ_W[b]+=p;
4968             
4969           }
4970       
4971       for ( p=0,b=0; b< (CL->S)->nseq; b++)
4972            {
4973              if ((CL->S)->len[b]==0)W->SEQ_W[b]=0;
4974              else W->SEQ_W[b]=2/W->SEQ_W[b];
4975              p+=W->SEQ_W[b];
4976            }
4977       for ( b=0; b< (CL->S)->nseq; b++)
4978         {
4979           W->SEQ_W[b]=W->SEQ_W[b]*((float)W->nseq/p);
4980         }
4981       
4982       
4983       return W;
4984     }
4985       
4986 Constraint_list *re_weight_constraint_list(Constraint_list * CL,Weights *W)
4987     {
4988       int a;
4989       float w;
4990       float *weight;
4991       int sA, sB;
4992
4993
4994
4995       weight=W->SEQ_W;
4996
4997       if (!CL->L)return CL;
4998  
4999       
5000       
5001       for ( a=0; a< CL->ne; a++)
5002            {
5003              sA=CL->L[a*CL->entry_len+SEQ1];
5004              sB=CL->L[a*CL->entry_len+SEQ2];
5005              
5006              w=MIN(weight[sA], weight[sB]);
5007              
5008              CL->L[a*CL->entry_len+WE]*=w;
5009            } 
5010       CL=evaluate_constraint_list_reference (CL);
5011       return CL;
5012     }
5013
5014 Distance_matrix* cl2distance_matrix    (Constraint_list *CL, Alignment *A, char *in_mode, char *in_sim_mode, int print)
5015 {
5016
5017   char mode[100];
5018   char sim_mode [100];
5019   
5020  
5021
5022   if ( !CL)return NULL;
5023   sprintf ( mode, "%s", (CL && in_mode==NULL)?CL->distance_matrix_mode:in_mode);
5024   sprintf ( sim_mode, "%s", (CL && in_sim_mode==NULL)?CL->distance_matrix_sim_mode:in_sim_mode);
5025   
5026   if ( !CL->DM ||!strm ((CL->DM)->mode, mode) || !strm ((CL->DM)->sim_mode, sim_mode) || A )
5027     {
5028       return seq2distance_matrix (CL, A, mode, sim_mode, print);
5029     }
5030   else
5031     {
5032       
5033       return CL->DM;
5034     }
5035 }
5036
5037   
5038 Distance_matrix *seq2distance_matrix (Constraint_list *CL, Alignment *A,char *mode, char *sim_mode, int print)
5039 {
5040   /*Compute the distance matrix associated with the Constraint List and the sequences*/
5041   /*Computation only occurs if the similiraty matrix is undefined : CL->similarity_matrix*/
5042   /*Undefine  CL->similarity_matrix to force computation*/
5043
5044   int a, b;
5045   Alignment *B;
5046   Constraint_list *NCL;
5047   float score=0;
5048   int *ns;
5049   int **l_s;
5050   float id;
5051   int max_name=0;
5052   int  id_score;
5053   static float **g_matrix;
5054   float ref=0;
5055   int n_coor=0;
5056   Distance_matrix *DM;
5057   int **sim_table=NULL;
5058
5059   //mode: computation mode
5060   //sim_mode: mode for computing the similarity
5061   
5062   //Composite modes
5063   
5064   if (strm (mode, "ktup2"))
5065     {
5066       B=seq2aln ( CL->S, NULL, 1);
5067       B=very_fast_aln (B, B->nseq,NULL);
5068       sprintf ( CL->distance_matrix_mode, "aln");
5069       DM=cl2distance_matrix (CL, B, NULL, NULL, 1);
5070       sprintf ( CL->distance_matrix_mode, "ktup2");
5071       sprintf ( DM->mode, "%s", mode);
5072       sprintf ( DM->sim_mode, "%s", sim_mode);
5073       free_aln (B);
5074       return DM;
5075     }
5076   
5077   if ( !CL) return NULL;
5078   else
5079     {
5080       for ( max_name=0,a=0; a<  (CL->S)->nseq; a++)max_name=MAX(strlen ((CL->S)->name[a]), max_name);
5081  
5082
5083       if ( CL->DM)DM=CL->DM;
5084       else
5085         {
5086           DM=vcalloc ( 1, sizeof (Distance_matrix));
5087           DM->nseq=(CL->S)->nseq;
5088           DM->similarity_matrix=declare_int ( (CL->S)->nseq, (CL->S)->nseq);
5089           DM->distance_matrix  =declare_int ( (CL->S)->nseq, (CL->S)->nseq);
5090           DM->score_similarity_matrix=declare_int ( (CL->S)->nseq, (CL->S)->nseq);
5091         }
5092       
5093       sprintf ( DM->mode, "%s", mode);
5094       sprintf ( DM->sim_mode, "%s", sim_mode);
5095       
5096       NCL=duplicate_constraint_list_soft (CL);
5097       NCL->pw_parameters_set=1;
5098
5099       if (!A)
5100         {
5101           if ( CL->tree_aln)B=CL->tree_aln;
5102           else B=seq2aln ( NCL->S, NULL, 1);
5103         }
5104       else
5105         {
5106           B=copy_aln (A, NULL);
5107           B=reorder_aln (B, (CL->S)->name, (CL->S)->nseq);
5108         }
5109       
5110       if ( strm (mode, "very_fast"))
5111         {
5112           sprintf ( NCL->dp_mode, "very_fast_pair_wise");
5113           NCL->evaluate_residue_pair=evaluate_matrix_score;
5114           if ( strm ((CL->S)->type, "DNA") ||strm ((CL->S)->type, "RNA")  )
5115                {
5116                  NCL->M=read_matrice ("idmat");
5117                  NCL->gop=-10;
5118                  NCL->gep=-1;
5119                  CL->ktup=6;
5120                }
5121              else
5122                {
5123                  NCL->M=read_matrice ("blosum62mt");
5124                  NCL->gop=get_avg_matrix_mm (NCL->M, AA_ALPHABET)*10;
5125                  NCL->gep=-1;
5126                  CL->ktup=2;
5127                }
5128              NCL->use_fragments=1;
5129              CL->diagonal_threshold=6;
5130         }
5131       
5132       else if ( strm (mode, "ktup"))
5133         {
5134           
5135           NCL->ktup=6;
5136           sim_table=ktup_dist_mat((CL->S)->seq,(CL->S)->nseq,NCL->ktup, (CL->S)->type);
5137         }
5138      
5139       
5140       else if (strm (mode, "aln"))
5141         {
5142            
5143            sim_table=aln2sim_mat (A, sim_mode);
5144         }
5145       else if ( strm (mode, "fast") || strm ("idscore", mode))
5146            {
5147              sprintf ( NCL->dp_mode, "myers_miller_pair_wise");
5148              NCL->evaluate_residue_pair=evaluate_matrix_score;
5149              if ( strm ((CL->S)->type, "DNA") || strm ((CL->S)->type, "RNA"))
5150                {
5151                  NCL->M=read_matrice ("idmat");
5152                  NCL->gop=-10;
5153                  NCL->gep=-1;
5154                }
5155              else
5156                {
5157                  NCL->M=read_matrice ("blosum62mt");
5158                  NCL->gop=get_avg_matrix_mm (NCL->M, AA_ALPHABET)*10;
5159                  NCL->gep=-1;
5160                }
5161            }
5162       else if ( strm (mode, "cscore"))
5163         {
5164           if (!CL || !CL->L || CL->ne==0)
5165             return seq2distance_matrix (CL, A,"idscore",sim_mode, print);
5166         }
5167       else if ( strm (mode, "geometric") );
5168       else if (strm (mode, "slow"));
5169       else if (strm (mode, "clustalw"));
5170       else if (strm (mode, "no"))
5171         print=1;
5172       else if (strm (mode, "random"))
5173         print=1;
5174       else
5175         {
5176           fprintf ( stderr, "\nError: %s is an unknown distance_matrix_mode [FATAL:%s]", mode,PROGRAM);
5177           crash ("");
5178         }
5179       
5180       //Special Geometric Mode
5181       if ( strm (NCL->distance_matrix_mode, "geometric"))
5182         {
5183           free_arrayN(g_matrix, 2);
5184           g_matrix=declare_float ((CL->S)->nseq, 3);
5185           n_coor=MIN(3,((CL->S)->nseq));
5186
5187           for ( a=0; a<(CL->S)->nseq; a++)
5188             {
5189               for (b=0; b<n_coor; b++)
5190                 {
5191                 B=align_two_sequences ((CL->S)->seq[a], (CL->S)->seq[b], "pam250mt", -10, -1, "fasta_pair_wise");
5192                 g_matrix[a][b]=get_seq_sim ( B->seq_al[0], B->seq_al[1], "-", NULL);
5193                 free_aln(B);B=NULL;
5194                 }
5195             }
5196           ref=(float)sqrt((double)(10000*n_coor));
5197         }
5198   
5199       
5200       ns=vcalloc ( 2, sizeof(int));
5201       l_s=declare_int ( 2, 1);
5202       ns[0]=ns[1]=1;
5203       l_s[0][0]=0;
5204       l_s[1][0]=1;
5205       
5206       if (CL->local_stderr && print>0)fprintf ( (CL->local_stderr), "\nCOMPUTE PAIRWISE SIMILARITY [dp_mode: %s] [distance_matrix_mode: %s][Similarity Measure: %s] \n", NCL->dp_mode,mode, sim_mode);  
5207       
5208       for (a=0; a< (CL->S)->nseq; a++)
5209         {
5210         if (CL->local_stderr && print>0)fprintf ( (CL->local_stderr), "\n\tSeq: %s", (CL->S)->name[a]);
5211         for ( b=a; b< (CL->S)->nseq; b++)
5212           {
5213             if ( b==a){DM->similarity_matrix[a][b]=MAXID;}
5214             else
5215               {
5216                 l_s[0][0]=a;
5217                 l_s[1][0]=b;
5218                 if ( !strm(mode, "ktup2") && ! strm (mode, "geometric"))
5219                      {
5220                        ungap ( B->seq_al[a]);
5221                        ungap ( B->seq_al[b]);
5222                      }
5223                 
5224                 if ( strm (mode, "slow"))
5225                   {
5226
5227                     B->score_aln=pair_wise (B, ns, l_s,NCL);
5228                                     
5229                     id=get_seq_sim ( B->seq_al[a], B->seq_al[b], "-", sim_mode);
5230                     if ( CL->L)
5231                       {
5232                         score=(int)(((float)B->score_aln)/(B->len_aln*SCORE_K));
5233                         score=(int)(CL->L && CL->normalise)?((score*MAXID)/(CL->normalise)):(score);
5234                       }
5235                     else if ( CL->M)score=id;
5236                     
5237                     
5238                     if ( score>MAXID)score=(int)(CL->L)?sub_aln2sub_aln_score (B, CL, CL->evaluate_mode, ns, l_s):id;
5239                      
5240                   }
5241                 else if ( strm2 (mode,"fast", "very_fast"))
5242                   {
5243                     B->score_aln=pair_wise (B, ns, l_s,NCL);
5244                     id=get_seq_sim ( B->seq_al[a], B->seq_al[b], "-", sim_mode);
5245                     score=(int)(id)*SCORE_K;
5246                   }
5247                 else if ( strm (mode, "cscore"))
5248                   {
5249                      ungap ( B->seq_al[a]);
5250                      ungap ( B->seq_al[b]);
5251                      score=(int)linked_pair_wise (B, ns, l_s, NCL);
5252                      
5253                      
5254                      score/=(B->len_aln*SCORE_K);
5255                      id=score/SCORE_K;
5256                   }
5257                 else if ( strm (mode, "idscore"))
5258                   {
5259                     score=id=idscore_pairseq (B->seq_al[a], B->seq_al[b], NCL->gop, NCL->gep, NCL->M, sim_mode);
5260                     //HERE ("%s %d %d ->%d", sim_mode, a, b, (int)id);
5261                   }
5262                 else if (strm (mode, "ktup"))
5263                   {
5264                     id=sim_table[a][b];
5265                     score=id*SCORE_K;
5266                     
5267                   }
5268                 else if (strm (mode, "aln"))
5269                   {
5270                     score=id=sim_table[a][b];
5271                     score*=SCORE_K;
5272                   }
5273                 
5274                 else if ( strm (mode, "geometric"))
5275                   {
5276                     id=get_geometric_distance (g_matrix,n_coor, a, b, "euclidian");                 
5277                     id=MAXID*(1-((id/ref)));    
5278                     score=(int)(id)*SCORE_K;
5279                   }
5280                 else if ( strm (mode, "no"))
5281                   {
5282                     id=100;
5283                     score=id*SCORE_K;
5284                   }
5285                 else if ( strm (mode, "random"))
5286                   {
5287                     id=rand()%100;
5288                     score=id*SCORE_K;
5289                   }
5290                 else
5291                   {
5292                     id=B->score_aln=pair_wise (B, ns, l_s,NCL);
5293                     score=id*SCORE_K;
5294                   }
5295                 /*Sim mat*/
5296                 DM->similarity_matrix[a][b]=DM->similarity_matrix[b][a]=(int)(id);
5297                 /*Dist mat*/
5298                 DM->distance_matrix[a][b]=DM->distance_matrix[b][a]=MAXID-(int)(id);
5299                 /*Score mat*/
5300                 
5301                 
5302                 DM->score_similarity_matrix[a][b]=DM->score_similarity_matrix[b][a]=(int)score;
5303                 id_score=id;
5304                 if (CL->local_stderr && print>1) fprintf (CL->local_stderr, "\n\t%-*s %-*s identity=%3d%% score=%3d", max_name,(CL->S)->name[a], max_name,(CL->S)->name[b], id_score, (int)score);
5305               }
5306           }
5307         }
5308       vfree (ns);
5309       free_int(l_s, -1);
5310       
5311     }
5312
5313
5314   if (CL->local_stderr) fprintf (CL->local_stderr, "\n");
5315   free_constraint_list (NCL);
5316  
5317       
5318
5319   if (!CL->tree_aln)
5320     {
5321       free_aln (B);
5322     }
5323   
5324   free_int (sim_table, -1);
5325   
5326
5327
5328   return DM;
5329 }
5330 /*********************************************************************/
5331 /*                                                                   */
5332 /*                        RNA FUNCTIONS                              */
5333 /*                                                                   */
5334 /*                                                                   */
5335 /*********************************************************************/
5336 char * seq2rna_lib ( Sequence *S, char *name)
5337 {
5338   int a;
5339   FILE *fp;
5340
5341
5342   if (!name)name=vtmpnam (NULL);
5343   fp=vfopen (name, "w");
5344   for ( a=0; a<S->nseq; a++)
5345     {
5346       
5347       fprintf (fp, "%s\n", rna_struc2rna_lib(S->name[a], S->seq[a], NULL));
5348     }
5349   vfclose (fp);
5350   
5351   return name;
5352 }
5353
5354 Constraint_list *read_rna_lib ( Sequence *S, char *fname)
5355 {
5356   Constraint_list *R;
5357   char **list;
5358   int n=0,a;
5359   
5360
5361   if (check_file_exists (fname))
5362     {
5363
5364       list=read_lib_list ( fname, &n);
5365     }
5366   else
5367     {
5368       X_template *F;
5369
5370       list=vcalloc (S->nseq, sizeof (char*));
5371       for ( a=0; a<S->nseq; a++)
5372         {
5373           if ((F=seq_has_template (S, a, "_F_")))
5374             {
5375               list[n++]=F->template_file;
5376             }
5377         }
5378     }
5379
5380   R=declare_constraint_list ( S,NULL, NULL, 0,NULL, NULL); 
5381       
5382   for (a=0; a< n; a++)
5383     {
5384
5385       if (list[a])R=fast_read_constraint_list_file (R, list[a]);
5386     }
5387
5388   R=index_res_constraint_list (R,WE);
5389   
5390   return R;
5391 }
5392
5393 Constraint_list * rna_lib_extension ( Constraint_list *CL, Constraint_list *R)
5394 {
5395   CLIST_TYPE  *entry=NULL;
5396   int a,b,c,n1,n2, ne,s1, s2, r1, r2,w;
5397   int list1[100], list2[100];
5398   
5399   
5400   entry=vcalloc ( CL->entry_len, CL->el_size);
5401   ne=CL->ne;
5402   
5403
5404   for ( a=0; a<ne; a++)
5405     {
5406
5407       
5408       extract_entry (entry, a, CL);
5409       s1=entry[SEQ1];
5410       s2=entry[SEQ2];
5411       r1=entry[R1];
5412       r2=entry[R2];
5413       w=entry[WE];
5414       
5415       n1=n2=0;
5416       list1[n1++]=r1;
5417       for (b=1; b<R->residue_index[s1][r1][0]; b+=3)
5418         {
5419           list1[n1++]=R->residue_index[s1][r1][b+1];
5420         }
5421       list2[n2++]=r2;
5422       for (b=1; b<R->residue_index[s2][r2][0]; b+=3)
5423         {
5424           list2[n2++]=R->residue_index[s2][r2][b+1];
5425         }
5426         
5427       for (b=1; b<n1; b++)
5428         for (c=1; c<n2; c++)
5429         {
5430
5431           
5432           entry[R1]=list1[b];
5433           entry[R2]=list2[c];
5434           add_entry2list ( entry,CL);
5435         }
5436     }
5437   
5438   return CL;
5439 }
5440
5441 char *** produce_method_file ( char *method)
5442 {
5443   static char ***list;
5444   int n=0;
5445   FILE *fp;
5446
5447   if (!list)list=declare_arrayN(3, sizeof (char),200,2, 100);
5448   
5449
5450   /*
5451   sprintf (list[n][0], "t_coffee");
5452   sprintf (list[n][1], "%s", vtmpnam(NULL));
5453   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
5454   fprintf ( fp, "EXECUTABLE t_coffee\n");
5455   fprintf ( fp, "ADDRESS    built_in");
5456   fprintf ( fp, "ALN_MODE   any\n");
5457   fprintf ( fp, "OUT_MODE   L\n");
5458   fprintf ( fp, "IN_FLAG    no_name\n");
5459   fprintf ( fp, "OUT_FLAG   no_name\n");
5460   fprintf ( fp, "SEQ_TYPE   S\n");
5461   fprintf ( fp, "ADDRESS    %s\n", ADDRESS_BUILT_IN);
5462   fprintf ( fp, "PROGRAM    %s\n", PROGRAM_BUILT_IN);
5463   
5464   vfclose (fp);}           
5465   */
5466   /*Space holder method to analyze very large dATASETS*/
5467   sprintf (list[n][0], "test_pair");
5468   sprintf (list[n][1], "%s", vtmpnam(NULL));
5469   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
5470   fprintf ( fp, "EXECUTABLE test_pair\n");
5471   fprintf ( fp, "DOC Fast alignmnents on the best diagonals\n"); 
5472   fprintf ( fp, "ALN_MODE   pairwise\n");
5473   fprintf ( fp, "OUT_MODE   fL\n");
5474   fprintf ( fp, "IN_FLAG    no_name\n");
5475   fprintf ( fp, "OUT_FLAG   no_name\n");
5476   fprintf ( fp, "SEQ_TYPE   S\n");
5477   fprintf ( fp, "ADDRESS    %s\n", ADDRESS_BUILT_IN);
5478   fprintf ( fp, "PROGRAM    %s\n", PROGRAM_BUILT_IN);
5479   vfclose (fp);}           
5480
5481   sprintf (list[n][0], "fast_pair");
5482   sprintf (list[n][1], "%s", vtmpnam(NULL));
5483   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
5484   fprintf ( fp, "EXECUTABLE fast_pair\n");
5485   fprintf ( fp, "DOC Fast alignmnents on the best diagonals\n"); 
5486   fprintf ( fp, "ALN_MODE   pairwise\n");
5487   fprintf ( fp, "OUT_MODE   fL\n");
5488   fprintf ( fp, "IN_FLAG    no_name\n");
5489   fprintf ( fp, "OUT_FLAG   no_name\n");
5490   fprintf ( fp, "SEQ_TYPE   S\n");
5491   if (strm (retrieve_seq_type(), "DNA") || strm (retrieve_seq_type(), "RNA"))
5492     {
5493       fprintf ( fp, "MATRIX dna_idmat\n");
5494       fprintf ( fp, "GOP -50\n");
5495       fprintf ( fp, "GEP -1\n");
5496     }
5497   fprintf ( fp, "ADDRESS    %s\n", ADDRESS_BUILT_IN);
5498   fprintf ( fp, "PROGRAM    %s\n", PROGRAM_BUILT_IN);
5499   vfclose (fp);}           
5500
5501   sprintf (list[n][0], "exon3_pair");
5502   sprintf (list[n][1], "%s", vtmpnam(NULL));
5503   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
5504   fprintf ( fp, "EXECUTABLE exon3_pair\n");
5505   fprintf ( fp, "ALN_MODE   pairwise\n");
5506   fprintf ( fp, "OUT_MODE   fL\n");
5507   fprintf ( fp, "IN_FLAG    no_name\n");
5508   fprintf ( fp, "OUT_FLAG   no_name\n"); 
5509   fprintf ( fp, "SEQ_TYPE   G\n");
5510   fprintf ( fp, "MATRIX     exon2mt\n");
5511   fprintf ( fp, "GOP        0\n");
5512   fprintf ( fp, "GEP        -1\n");
5513   fprintf ( fp, "ADDRESS    %s\n", ADDRESS_BUILT_IN);
5514   fprintf ( fp, "PROGRAM    %s\n", PROGRAM_BUILT_IN);
5515   vfclose (fp);}           
5516   
5517   sprintf (list[n][0], "exon2_pair");
5518   sprintf (list[n][1], "%s", vtmpnam(NULL));
5519   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
5520   fprintf ( fp, "EXECUTABLE exon2_pair\n");
5521   fprintf ( fp, "ALN_MODE   pairwise\n");
5522   fprintf ( fp, "OUT_MODE   fL\n");
5523   fprintf ( fp, "IN_FLAG    no_name\n");
5524   fprintf ( fp, "OUT_FLAG   no_name\n"); 
5525   fprintf ( fp, "SEQ_TYPE   G\n");
5526   fprintf ( fp, "MATRIX     exon2mt\n");
5527   fprintf ( fp, "GOP        -10\n");
5528   fprintf ( fp, "GEP        -1\n");
5529   fprintf ( fp, "ADDRESS    %s\n", ADDRESS_BUILT_IN);
5530   fprintf ( fp, "PROGRAM    %s\n", PROGRAM_BUILT_IN);
5531   vfclose (fp);}           
5532   
5533   sprintf (list[n][0], "exon_pair");
5534   sprintf (list[n][1], "%s", vtmpnam(NULL));
5535   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
5536   fprintf ( fp, "EXECUTABLE exon_pair\n");
5537   fprintf ( fp, "ALN_MODE   pairwise\n");
5538   fprintf ( fp, "OUT_MODE   fL\n");
5539   fprintf ( fp, "IN_FLAG    no_name\n");
5540   fprintf ( fp, "OUT_FLAG   no_name\n"); 
5541   fprintf ( fp, "SEQ_TYPE   G\n");
5542   fprintf ( fp, "MATRIX     exon2mt\n");
5543   fprintf ( fp, "GOP        -10\n");
5544   fprintf ( fp, "GEP        -1\n");
5545   fprintf ( fp, "ADDRESS    %s\n", ADDRESS_BUILT_IN);
5546   fprintf ( fp, "PROGRAM    %s\n", PROGRAM_BUILT_IN);
5547   vfclose (fp);}           
5548   
5549
5550   sprintf (list[n][0], "clean_slow_pair");
5551   sprintf (list[n][1], "%s", vtmpnam(NULL));
5552   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
5553   fprintf ( fp, "DOC regular dynamic Programming\n");
5554   fprintf ( fp, "EXECUTABLE slow_pair\n");
5555   fprintf ( fp, "ALN_MODE   pairwise\n");
5556   fprintf ( fp, "OUT_MODE   fL\n");
5557   fprintf ( fp, "IN_FLAG    no_name\n");
5558   fprintf ( fp, "OUT_FLAG   no_name\n");
5559   fprintf ( fp, "SEQ_TYPE   S\n");
5560   fprintf ( fp, "WEIGHT     clean\n");
5561   if ( strm ( retrieve_seq_type(), "DNA") || strm (retrieve_seq_type(), "RNA"))
5562     {
5563       fprintf ( fp, "MATRIX   dna_idmat\n");
5564       fprintf ( fp, "GOP   -10\n");
5565       fprintf ( fp, "GEP   -1\n");
5566     }
5567   fprintf ( fp, "ADDRESS    %s\n", ADDRESS_BUILT_IN);
5568   fprintf ( fp, "PROGRAM    %s\n", PROGRAM_BUILT_IN);
5569   vfclose (fp);}
5570
5571   sprintf (list[n][0], "slow_pair");
5572   sprintf (list[n][1], "%s", vtmpnam(NULL));
5573   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
5574   fprintf ( fp, "DOC regular dynamic Programming\n");
5575   fprintf ( fp, "EXECUTABLE slow_pair\n");
5576   fprintf ( fp, "ALN_MODE   pairwise\n");
5577   fprintf ( fp, "OUT_MODE   fL\n");
5578   fprintf ( fp, "IN_FLAG    no_name\n");
5579   fprintf ( fp, "OUT_FLAG   no_name\n");
5580   fprintf ( fp, "SEQ_TYPE   S\n");
5581   if ( strm ( retrieve_seq_type(), "DNA") || strm (retrieve_seq_type(), "RNA"))
5582     {
5583       fprintf ( fp, "MATRIX   dna_idmat\n");
5584       fprintf ( fp, "GOP   -10\n");
5585       fprintf ( fp, "GEP   -1\n");
5586     }
5587   fprintf ( fp, "ADDRESS    %s\n", ADDRESS_BUILT_IN);
5588   fprintf ( fp, "PROGRAM    %s\n", PROGRAM_BUILT_IN);
5589   vfclose (fp);}
5590
5591   sprintf (list[n][0], "biphasic_pair");
5592   sprintf (list[n][1], "%s", vtmpnam(NULL));
5593   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
5594   fprintf ( fp, "DOC bi-phasic dynamic Programming\n");
5595   fprintf ( fp, "EXECUTABLE biphasic_pair\n");
5596   fprintf ( fp, "ALN_MODE   pairwise\n");
5597   fprintf ( fp, "OUT_MODE   fL\n");
5598   fprintf ( fp, "IN_FLAG    no_name\n");
5599   fprintf ( fp, "OUT_FLAG   no_name\n");
5600   fprintf ( fp, "SEQ_TYPE   S\n");
5601   if ( strm ( retrieve_seq_type(), "DNA") || strm (retrieve_seq_type(), "RNA"))
5602     {
5603       fprintf ( fp, "MATRIX   dna_idmat\n");
5604       fprintf ( fp, "GOP   -10\n");
5605       fprintf ( fp, "GEP   -1\n");
5606     }
5607   fprintf ( fp, "ADDRESS    %s\n", ADDRESS_BUILT_IN);
5608   fprintf ( fp, "PROGRAM    %s\n", PROGRAM_BUILT_IN);
5609   vfclose (fp);}
5610   
5611   sprintf (list[n][0], "proba_pair");
5612   sprintf (list[n][1], "%s", vtmpnam(NULL));
5613   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
5614   fprintf ( fp, "DOC Probabilistic pairwise alignment\n");
5615   fprintf ( fp, "EXECUTABLE proba_pair\n");
5616   fprintf ( fp, "ALN_MODE   pairwise\n");
5617   fprintf ( fp, "OUT_MODE   fL\n");
5618   fprintf ( fp, "IN_FLAG    no_name\n");
5619   fprintf ( fp, "OUT_FLAG   no_name\n");
5620   fprintf ( fp, "SEQ_TYPE   S\n");
5621   if ( strm ( retrieve_seq_type(), "DNA") || strm (retrieve_seq_type(), "RNA"))
5622     {
5623       fprintf ( fp, "GOP   %d\n",CODE4DNA);//code for DNA
5624     }
5625   else
5626     {
5627       fprintf ( fp, "GOP   %d\n",CODE4PROTEINS);//Code for Proteins
5628     }
5629   fprintf ( fp, "ADDRESS    %s\n", ADDRESS_BUILT_IN);
5630   fprintf ( fp, "PROGRAM    %s\n", PROGRAM_BUILT_IN);
5631   vfclose (fp);}           
5632   
5633   
5634   sprintf (list[n][0], "best_pair4prot");
5635   sprintf (list[n][1], "%s", vtmpnam(NULL));
5636   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
5637   fprintf ( fp, "DOC Combination of the best template methods\n");
5638   fprintf ( fp, "EXECUTABLE best_pair4prot\n");
5639   fprintf ( fp, "ALN_MODE   pairwise\n");
5640   fprintf ( fp, "OUT_MODE   fL\n");
5641   fprintf ( fp, "IN_FLAG    no_name\n");
5642   fprintf ( fp, "OUT_FLAG   no_name\n");
5643   fprintf ( fp, "SEQ_TYPE   S\n");
5644   fprintf ( fp, "ADDRESS    %s\n", ADDRESS_BUILT_IN);
5645   fprintf ( fp, "PROGRAM    %s\n", PROGRAM_BUILT_IN);
5646    if (strm (retrieve_seq_type(), "DNA") || strm (retrieve_seq_type(), "RNA"))
5647     {
5648       printf_exit (EXIT_FAILURE, stderr, "\nERROR: The mode best_pair4prot is only suited for Proteins [FATAL:%s]\n", PROGRAM);
5649     }
5650   vfclose (fp);}           
5651
5652   
5653   sprintf (list[n][0], "best_pair4rna");
5654   sprintf (list[n][1], "%s", vtmpnam(NULL));
5655   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
5656           fprintf ( fp, "DOC Combination of the best template methods\n");
5657           fprintf ( fp, "EXECUTABLE best_pair4rna\n");
5658           fprintf ( fp, "ALN_MODE   pairwise\n");
5659           fprintf ( fp, "OUT_MODE   fL\n");
5660           fprintf ( fp, "IN_FLAG    no_name\n");
5661           fprintf ( fp, "OUT_FLAG   no_name\n");
5662           fprintf ( fp, "SEQ_TYPE   S\n");
5663           fprintf ( fp, "ADDRESS    %s\n", ADDRESS_BUILT_IN);
5664           fprintf ( fp, "PROGRAM    %s\n", PROGRAM_BUILT_IN);
5665 //        if (strm(retrieve_seq_type(), "RNA"))
5666 //        {
5667 //                printf_exit (EXIT_FAILURE, stderr, "\nERROR: The mode best_pair4rna is only suited for RNA [FATAL:%s]\n", PROGRAM);
5668 //        }
5669           vfclose (fp);}
5670   
5671   
5672   //Llaign ID PAIR
5673   sprintf (list[n][0], "lalign_id_pair");
5674   sprintf (list[n][1], "%s", vtmpnam(NULL));
5675   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
5676   fprintf ( fp, "DOC local alignment reporting the N best pairwise local alignments\n");
5677   fprintf ( fp, "EXECUTABLE lalign_id_pair\n");
5678   fprintf ( fp, "ALN_MODE   pairwise\n");
5679   fprintf ( fp, "OUT_MODE   fL\n");
5680   fprintf ( fp, "IN_FLAG    no_name\n");
5681   fprintf ( fp, "OUT_FLAG   no_name\n");
5682   if ( strm (retrieve_seq_type(), "DNA") || strm (retrieve_seq_type(), "RNA"))
5683     {
5684       fprintf ( fp, "MATRIX     dna_idmat\n");
5685       fprintf ( fp, "GOP   -10\n");
5686       fprintf ( fp, "GEP   -1\n");
5687     }
5688   else
5689     {
5690       fprintf ( fp, "MATRIX     blosum50mt\n");
5691       fprintf ( fp, "GOP   -10\n");
5692       fprintf ( fp, "GEP   -4\n");
5693     }
5694   fprintf ( fp, "MAXID  100\n");
5695   fprintf ( fp, "SEQ_TYPE   S\n");
5696   fprintf ( fp, "ADDRESS    %s\n", ADDRESS_BUILT_IN);
5697   fprintf ( fp, "PROGRAM    %s\n", PROGRAM_BUILT_IN);
5698   vfclose (fp);}
5699   
5700   sprintf (list[n][0], "align_pdbpair");
5701   sprintf (list[n][1], "%s", vtmpnam(NULL));
5702   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
5703   fprintf ( fp, "EXECUTABLE align_pdb_pair\n");
5704   fprintf ( fp, "ALN_MODE   pairwise\n");
5705   fprintf ( fp, "OUT_MODE   fL\n");
5706   fprintf ( fp, "IN_FLAG    no_name\n");
5707   fprintf ( fp, "OUT_FLAG   no_name\n");
5708   fprintf ( fp, "SEQ_TYPE   P\n");
5709   fprintf ( fp, "ADDRESS    %s\n", ADDRESS_BUILT_IN);
5710   fprintf ( fp, "PROGRAM    %s\n", PROGRAM_BUILT_IN);
5711   vfclose (fp);}           
5712   
5713   sprintf (list[n][0], "lalign_pdbpair");
5714   sprintf (list[n][1], "%s", vtmpnam(NULL));
5715   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
5716   fprintf ( fp, "EXECUTABLE lalign_pdb_pair\n");
5717   fprintf ( fp, "ALN_MODE   pairwise\n");
5718   fprintf ( fp, "OUT_MODE   fL\n");
5719   fprintf ( fp, "IN_FLAG    no_name\n");
5720   fprintf ( fp, "OUT_FLAG   no_name\n");
5721   fprintf ( fp, "SEQ_TYPE   P\n");
5722   fprintf ( fp, "ADDRESS    %s\n", ADDRESS_BUILT_IN);
5723   fprintf ( fp, "PROGRAM    %s\n", PROGRAM_BUILT_IN);
5724   vfclose (fp);}           
5725
5726   sprintf (list[n][0], "ktup_msa");
5727   sprintf (list[n][1], "%s", vtmpnam(NULL));
5728   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
5729     fprintf ( fp, "EXECUTABLE ktup_msa\n");
5730     fprintf ( fp, "ALN_MODE   multiple\n");
5731     fprintf ( fp, "OUT_MODE   fL\n");
5732     fprintf ( fp, "IN_FLAG    no_name\n");
5733     fprintf ( fp, "OUT_FLAG   no_name\n");
5734     fprintf ( fp, "SEQ_TYPE   s\n");
5735     fprintf ( fp, "ADDRESS    %s\n", ADDRESS_BUILT_IN);
5736     fprintf ( fp, "PROGRAM    %s\n", PROGRAM_BUILT_IN);
5737     vfclose (fp);}         
5738   
5739   sprintf (list[n][0], "seq_pair");
5740   sprintf (list[n][1], "%s", vtmpnam(NULL));
5741   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
5742   fprintf ( fp, "EXECUTABLE seq_pair\n");
5743   fprintf ( fp, "ALN_MODE   pairwise\n");
5744   fprintf ( fp, "OUT_MODE   fL\n");
5745   fprintf ( fp, "IN_FLAG    no_name\n");
5746   fprintf ( fp, "OUT_FLAG   no_name\n");
5747   fprintf ( fp, "SEQ_TYPE   s\n");
5748   fprintf ( fp, "ADDRESS    %s\n", ADDRESS_BUILT_IN);
5749   fprintf ( fp, "PROGRAM    %s\n", PROGRAM_BUILT_IN);
5750   vfclose (fp);}           
5751   
5752   sprintf (list[n][0], "extern_pdbpair");
5753   sprintf (list[n][1], "%s", vtmpnam(NULL));
5754   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
5755   fprintf ( fp, "EXECUTABLE tc_P_generic_method.pl\n");
5756   fprintf ( fp, "ALN_MODE   pairwise\n");
5757   fprintf ( fp, "OUT_MODE   A\n");
5758   fprintf ( fp, "IN_FLAG    no_name\n");
5759   fprintf ( fp, "OUT_FLAG   no_name\n");
5760   fprintf ( fp, "SEQ_TYPE   P\n");
5761   fprintf ( fp, "ADDRESS    %s\n", ADDRESS_BUILT_IN);
5762   fprintf ( fp, "PROGRAM    %s\n", PROGRAM_BUILT_IN);
5763   vfclose (fp);}           
5764   
5765   sprintf (list[n][0], "externprofile_pair");
5766   sprintf (list[n][1], "%s", vtmpnam(NULL));
5767   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
5768   fprintf ( fp, "EXECUTABLE tc_R_generic_method\n");
5769   fprintf ( fp, "ALN_MODE   pairwise\n");
5770   fprintf ( fp, "OUT_MODE   A\n");
5771   fprintf ( fp, "IN_FLAG    no_name\n");
5772   fprintf ( fp, "OUT_FLAG   no_name\n");
5773   fprintf ( fp, "SEQ_TYPE   R\n");
5774   fprintf ( fp, "ADDRESS    %s\n", ADDRESS_BUILT_IN);
5775   fprintf ( fp, "PROGRAM    %s\n", PROGRAM_BUILT_IN);
5776   vfclose (fp);}           
5777   
5778   sprintf (list[n][0], "thread_pair");
5779   sprintf (list[n][1], "%s", vtmpnam(NULL));
5780   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
5781   /*This runs thread_pair@EP@EXECUTABLE2@threpg*/
5782   fprintf ( fp, "EXECUTABLE thread_pair\n");
5783   fprintf ( fp, "EXECUTABLE2 t_coffee\n");
5784   fprintf ( fp, "ALN_MODE    pairwise\n");
5785   fprintf ( fp, "OUT_MODE   fL\n");
5786   fprintf ( fp, "IN_FLAG    -infile=\n");
5787   fprintf ( fp, "IN_FLAG2   -pdbfile1=\n");
5788   fprintf ( fp, "OUT_FLAG   -outfile=\n");
5789   fprintf ( fp, "SEQ_TYPE   Ps\n");
5790   fprintf ( fp, "ADDRESS    %s\n", ADDRESS_BUILT_IN);
5791   fprintf ( fp, "PROGRAM    %s\n", PROGRAM_BUILT_IN);
5792   vfclose (fp);}           
5793    
5794   sprintf (list[n][0], "fugue_pair");
5795   sprintf (list[n][1], "%s", vtmpnam(NULL));
5796   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
5797   /*This runs thread_pair@EP@EXECUTABLE2@threpg*/
5798   fprintf ( fp, "EXECUTABLE thread_pair\n");
5799   fprintf ( fp, "EXECUTABLE2 fugueali\n");
5800   fprintf ( fp, "ALN_MODE    pairwise\n");
5801   fprintf ( fp, "OUT_MODE   fL\n");
5802   fprintf ( fp, "IN_FLAG    -infile=\n");
5803   fprintf ( fp, "IN_FLAG2   -pdbfile1=\n");
5804   fprintf ( fp, "OUT_FLAG   -outfile=\n");
5805   fprintf ( fp, "SEQ_TYPE   Ps\n");
5806   fprintf ( fp, "ADDRESS    %s\n", FUGUE_ADDRESS);
5807   fprintf ( fp, "PROGRAM    %s\n", FUGUE_4_TCOFFEE);
5808   vfclose (fp);}           
5809   
5810   sprintf (list[n][0], "pdb_pair");
5811   sprintf (list[n][1], "%s", vtmpnam(NULL));
5812   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
5813   fprintf ( fp, "EXECUTABLE pdb_pair\n");
5814   fprintf ( fp, "EXECUTABLE2 t_coffee\n");
5815   fprintf ( fp, "ALN_MODE   pairwise\n");
5816   fprintf ( fp, "OUT_MODE   fL\n");
5817   fprintf ( fp, "IN_FLAG    -pdbfile1=\n");
5818   fprintf ( fp, "IN_FLAG2   -pdbfile2=\n");
5819   fprintf ( fp, "OUT_FLAG   -outfile=\n");
5820   fprintf ( fp, "SEQ_TYPE   P\n");
5821   fprintf ( fp, "ADDRESS    %s\n", ADDRESS_BUILT_IN);
5822   fprintf ( fp, "PROGRAM    %s\n", PROGRAM_BUILT_IN);
5823   vfclose (fp);}           
5824   
5825   sprintf (list[n][0], "hh_pair");
5826   sprintf (list[n][1], "%s", vtmpnam(NULL));
5827   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
5828   fprintf ( fp, "EXECUTABLE profile_pair\n");
5829   fprintf ( fp, "EXECUTABLE2 hhalign\n");
5830   fprintf ( fp, "ALN_MODE   pairwise\n");
5831   fprintf ( fp, "OUT_MODE   fL\n");
5832   fprintf ( fp, "IN_FLAG    -profile1=\n");
5833   fprintf ( fp, "IN_FLAG2   -profile2=\n");
5834   fprintf ( fp, "OUT_FLAG   -outfile=\n");
5835   fprintf ( fp, "SEQ_TYPE   R\n");
5836   fprintf ( fp, "ADDRESS    %s\n", ADDRESS_BUILT_IN);
5837   fprintf ( fp, "PROGRAM    %s\n", PROGRAM_BUILT_IN);
5838   fprintf ( fp, "SUPPORTED  NO");
5839   vfclose (fp);}           
5840   
5841   sprintf (list[n][0], "profile_pair");
5842   sprintf (list[n][1], "%s", vtmpnam(NULL));
5843   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w"); 
5844   fprintf ( fp, "EXECUTABLE profile_pair\n");
5845   fprintf ( fp, "EXECUTABLE2 clustalw\n");
5846   fprintf ( fp, "ALN_MODE   pairwise\n");
5847   fprintf ( fp, "OUT_MODE   fL\n");
5848   fprintf ( fp, "IN_FLAG    -profile1=\n");
5849   fprintf ( fp, "IN_FLAG2   -profile2=\n");
5850   fprintf ( fp, "OUT_FLAG   -outfile=\n");
5851   fprintf ( fp, "SEQ_TYPE   R\n");
5852   fprintf ( fp, "ADDRESS    %s\n", ADDRESS_BUILT_IN);
5853   fprintf ( fp, "PROGRAM    %s\n", PROGRAM_BUILT_IN);
5854   fprintf ( fp, "SUPPORTED  NO");
5855   vfclose (fp);}           
5856   
5857
5858   //Switch to TM_align if SAP is not installed
5859    //Intercept sap 
5860   if (method && strm (method, "sap_pair") && !check_program_is_installed (SAP_4_TCOFFEE,NULL,NULL,SAP_ADDRESS,INSTALL))
5861     {
5862       static int issued;
5863       if (!issued)
5864         {
5865           add_warning (stderr, "\n******************** WARNING: ****************************************\nSAP is not installed\nTMalign will be used instead\ntmalign is FASTER than SAP and *almost* as accurate\n**********************************************************************\n");
5866           issued=1;
5867         }
5868       
5869       sprintf (list[n][0], "sap_pair");
5870       sprintf (list[n][1], "%s", vtmpnam(NULL));
5871       n++;
5872       
5873       fp=vfopen (list[n-1][1], "w"); 
5874       fprintf ( fp, "DOC: TM-Align: pairwise structural aligner [%s]\n", TMALIGN_ADDRESS);
5875       fprintf ( fp, "EXECUTABLE pdb_pair\n");
5876       fprintf ( fp, "EXECUTABLE2 TMalign\n" );
5877       fprintf ( fp, "ALN_MODE   pairwise\n");
5878       fprintf ( fp, "OUT_MODE   fL\n");
5879       fprintf ( fp, "IN_FLAG    -pdbfile1=\n");
5880       fprintf ( fp, "IN_FLAG2   -pdbfile2=\n");
5881       fprintf ( fp, "OUT_FLAG   -outfile=\n");
5882       fprintf ( fp, "ADDRESS    %s\n", TMALIGN_ADDRESS);
5883       fprintf ( fp, "PROGRAM    %s\n", TMALIGN_4_TCOFFEE);
5884       fprintf ( fp, "SEQ_TYPE   P\n");
5885       vfclose (fp);        
5886     }
5887   else
5888     {
5889       
5890       sprintf (list[n][0], "sap_pair");
5891       sprintf (list[n][1], "%s", vtmpnam(NULL));
5892       n++;
5893       if (method==NULL || strm (method, list[n-1][0]))
5894         {
5895           fp=vfopen (list[n-1][1], "w"); 
5896           fprintf ( fp, "DOC: sap: pairwise structural aligner [%s]\n", SAP_ADDRESS);
5897           fprintf ( fp, "EXECUTABLE sap_pair\n");
5898           fprintf ( fp, "ALN_MODE   pairwise\n");
5899           fprintf ( fp, "OUT_MODE   fL\n");
5900           fprintf ( fp, "IN_FLAG    no_name\n");
5901           fprintf ( fp, "OUT_FLAG   no_name\n");
5902           fprintf ( fp, "WEIGHT     100\n");
5903           fprintf ( fp, "SEQ_TYPE   P\n");
5904           fprintf ( fp, "ADDRESS    %s\n", SAP_ADDRESS);
5905           fprintf ( fp, "PROGRAM    %s\n", SAP_4_TCOFFEE);
5906           vfclose (fp);
5907         }          
5908     }
5909         sprintf (list[n][0], "sara_pair");
5910         sprintf (list[n][1], "%s", vtmpnam(NULL));
5911         n++;
5912         if (method==NULL || strm (method, list[n-1][0])){
5913                 fp=vfopen (list[n-1][1], "w"); 
5914                 fprintf ( fp, "DOC: SARA: pairwise structural RNA aligner [%s]\n", ADDRESS_BUILT_IN);
5915                 fprintf ( fp, "EXECUTABLE rna_pair\n");
5916                 fprintf ( fp, "EXECUTABLE2 runsara.py\n" );
5917                 fprintf ( fp, "ALN_MODE   pairwise\n");
5918                 fprintf ( fp, "OUT_MODE   fL\n");
5919                 fprintf ( fp, "IN_FLAG    -pdbfile1=\n");
5920                 fprintf ( fp, "IN_FLAG2   -pdbfile2=\n");
5921                 fprintf ( fp, "OUT_FLAG   -outfile=\n");
5922                 fprintf ( fp, "SEQ_TYPE   P\n");
5923                 fprintf ( fp, "ADDRESS    %s\n", ADDRESS_BUILT_IN);
5924                 fprintf ( fp, "PROGRAM    %s\n", PROGRAM_BUILT_IN);
5925                 vfclose (fp);
5926         }
5927         
5928   sprintf (list[n][0], "dali_pair");
5929   sprintf (list[n][1], "%s", vtmpnam(NULL));
5930   n++;
5931   if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w"); 
5932   fprintf ( fp, "DOC: Dalilite: pairwise structural aligner [%s]\n", DALILITEc_ADDRESS);
5933   fprintf ( fp, "EXECUTABLE pdbid_pair\n");
5934   fprintf ( fp, "EXECUTABLE2 dalilite\n" );
5935   fprintf ( fp, "ALN_MODE   pairwise\n");
5936   fprintf ( fp, "OUT_MODE   fL\n");
5937   fprintf ( fp, "IN_FLAG    -pdbfile1=\n");
5938   fprintf ( fp, "IN_FLAG2   -pdbfile2=\n");
5939   fprintf ( fp, "OUT_FLAG   -outfile=\n");
5940   fprintf ( fp, "SEQ_TYPE   P\n");
5941   fprintf ( fp, "ADDRESS    %s\n", DALILITEc_ADDRESS);
5942   fprintf ( fp, "PROGRAM    %s\n", DALILITEc_4_TCOFFEE);
5943   vfclose (fp);}           
5944
5945   sprintf (list[n][0], "mustang_pair");
5946   sprintf (list[n][1], "%s", vtmpnam(NULL));
5947   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w"); 
5948   fprintf ( fp, "DOC: Mustang: pairwise structural aligner [%s]\n", MUSTANG_ADDRESS);
5949   fprintf ( fp, "EXECUTABLE pdb_pair\n");
5950   fprintf ( fp, "EXECUTABLE2 mustang\n" );
5951   fprintf ( fp, "ALN_MODE   pairwise\n");
5952   fprintf ( fp, "OUT_MODE   fL\n");
5953   fprintf ( fp, "IN_FLAG    -pdbfile1=\n");
5954   fprintf ( fp, "IN_FLAG2   -pdbfile2=\n");
5955   fprintf ( fp, "OUT_FLAG   -outfile=\n");
5956   fprintf ( fp, "SEQ_TYPE   P\n");
5957   fprintf ( fp, "ADDRESS    %s\n", MUSTANG_ADDRESS);
5958   fprintf ( fp, "PROGRAM    %s\n", MUSTANG_4_TCOFFEE);
5959   vfclose (fp);}           
5960   
5961   sprintf (list[n][0], "TMalign_pair");
5962   sprintf (list[n][1], "%s", vtmpnam(NULL));
5963   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w"); 
5964   fprintf ( fp, "DOC: TM-Align: pairwise structural aligner [%s]\n", TMALIGN_ADDRESS);
5965   fprintf ( fp, "EXECUTABLE pdb_pair\n");
5966   fprintf ( fp, "EXECUTABLE2 TMalign\n" );
5967   fprintf ( fp, "ALN_MODE   pairwise\n");
5968   fprintf ( fp, "OUT_MODE   fL\n");
5969   fprintf ( fp, "IN_FLAG    -pdbfile1=\n");
5970   fprintf ( fp, "IN_FLAG2   -pdbfile2=\n");
5971   fprintf ( fp, "OUT_FLAG   -outfile=\n");
5972   fprintf ( fp, "ADDRESS    %s\n", TMALIGN_ADDRESS);
5973   fprintf ( fp, "PROGRAM    %s\n", TMALIGN_4_TCOFFEE);
5974   fprintf ( fp, "SEQ_TYPE   P\n");
5975   vfclose (fp);}           
5976   
5977   sprintf (list[n][0], "cdna_fast_pair");
5978   sprintf (list[n][1], "%s", vtmpnam(NULL));
5979   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w"); 
5980   fprintf ( fp, "EXECUTABLE cdna_fast_pair\n");
5981   fprintf ( fp, "ALN_MODE   pairwise\n");
5982   fprintf ( fp, "OUT_MODE   fL\n");
5983   fprintf ( fp, "IN_FLAG    no_name\n");
5984   fprintf ( fp, "OUT_FLAG   no_name\n");
5985   fprintf ( fp, "SEQ_TYPE   S\n");
5986   fprintf ( fp, "ADDRESS    %s\n", ADDRESS_BUILT_IN);
5987   fprintf ( fp, "PROGRAM    %s\n", PROGRAM_BUILT_IN);
5988   vfclose (fp);}           
5989   
5990   sprintf (list[n][0], "cdna_cfast_pair");
5991   sprintf (list[n][1], "%s", vtmpnam(NULL));
5992   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
5993   fprintf ( fp, "EXECUTABLE cdna_cfast_pair\n");
5994   fprintf ( fp, "ALN_MODE   pairwise\n");
5995   fprintf ( fp, "OUT_MODE   fL\n");
5996   fprintf ( fp, "IN_FLAG    no_name\n");
5997   fprintf ( fp, "OUT_FLAG   no_name\n");
5998   fprintf ( fp, "SEQ_TYPE   S\n");
5999   fprintf ( fp, "ADDRESS    %s\n", ADDRESS_BUILT_IN);
6000   fprintf ( fp, "PROGRAM    %s\n", PROGRAM_BUILT_IN);
6001   vfclose (fp);}           
6002   
6003   sprintf (list[n][0], "blast_msa");
6004   sprintf (list[n][1], "%s", vtmpnam(NULL));
6005   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w"); 
6006   fprintf ( fp, "DOC: BLAST multiple Aligner [%s]\n", NCBIBLAST_ADDRESS);
6007   fprintf ( fp, "EXECUTABLE seq_msa\n");
6008   fprintf ( fp, "EXECUTABLE2 blastpgp\n" );
6009   fprintf ( fp, "ALN_MODE   multiple\n");
6010   fprintf ( fp, "OUT_MODE   fL\n");
6011   fprintf ( fp, "IN_FLAG    -infile=\n");
6012   fprintf ( fp, "OUT_FLAG   -outfile=\n");
6013   fprintf ( fp, "SEQ_TYPE   S\n");
6014   fprintf ( fp, "ADDRESS    %s\n", NCBIBLAST_ADDRESS);
6015   fprintf ( fp, "PROGRAM    %s\n", NCBIBLAST_4_TCOFFEE);
6016   vfclose (fp);}           
6017   
6018   sprintf (list[n][0], "clustalw2_pair");
6019   sprintf (list[n][1], "%s", vtmpnam(NULL));
6020   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
6021   fprintf ( fp, "DOC: clustalw [%s]\n", CLUSTALW2_ADDRESS);
6022   fprintf ( fp, "EXECUTABLE clustalw\n");
6023   fprintf ( fp, "ALN_MODE   pairwise\n");
6024   fprintf ( fp, "OUT_MODE   aln\n");
6025   fprintf ( fp, "IN_FLAG    %sINFILE=\n",CWF);
6026   fprintf ( fp, "OUT_FLAG   %sOUTFILE=\n",CWF);
6027   fprintf ( fp, "PARAM      %sOUTORDER=INPUT %sNEWTREE=cw.tmp.dnd %salign\n",CWF,CWF,CWF);
6028   fprintf ( fp, "SEQ_TYPE   S\n");
6029   fprintf ( fp, "ADDRESS    %s\n", CLUSTALW2_ADDRESS);
6030   fprintf ( fp, "PROGRAM    %s\n", CLUSTALW2_4_TCOFFEE);
6031   vfclose (fp);}           
6032   
6033   sprintf (list[n][0], "clustalw2_msa");
6034   sprintf (list[n][1], "%s", vtmpnam(NULL));
6035   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
6036   fprintf ( fp, "DOC clustalw[%s]\n", CLUSTALW2_ADDRESS);
6037   fprintf ( fp, "EXECUTABLE clustalw2\n");
6038   fprintf ( fp, "ALN_MODE   multiple\n");
6039   fprintf ( fp, "OUT_MODE   aln\n");
6040   fprintf ( fp, "IN_FLAG    %sINFILE=\n",CWF);
6041   fprintf ( fp, "OUT_FLAG   %sOUTFILE=\n", CWF);
6042   fprintf ( fp, "PARAM      %sOUTORDER=INPUT %sNEWTREE=cw.tmp.dnd %salign\n",CWF,CWF,CWF);
6043   fprintf ( fp, "SEQ_TYPE   S\n");
6044   fprintf ( fp, "ADDRESS    %s\n", CLUSTALW2_ADDRESS);
6045   fprintf ( fp, "PROGRAM    %s\n", CLUSTALW2_4_TCOFFEE);
6046   vfclose (fp);}           
6047  
6048   sprintf (list[n][0], "clustalw_pair");
6049   sprintf (list[n][1], "%s", vtmpnam(NULL));
6050   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
6051   fprintf ( fp, "DOC: clustalw [%s]\n", CLUSTALW_ADDRESS);
6052   fprintf ( fp, "EXECUTABLE clustalw\n");
6053   fprintf ( fp, "ALN_MODE   pairwise\n");
6054   fprintf ( fp, "OUT_MODE   aln\n");
6055   fprintf ( fp, "IN_FLAG    %sINFILE=\n", CWF);
6056   fprintf ( fp, "OUT_FLAG   %sOUTFILE=\n",CWF);
6057   fprintf ( fp, "PARAM      %sOUTORDER=INPUT %sNEWTREE=cw.tmp.dnd %salign\n",CWF,CWF,CWF);
6058   fprintf ( fp, "SEQ_TYPE   S\n");
6059   fprintf ( fp, "ADDRESS    %s\n", CLUSTALW_ADDRESS);
6060   fprintf ( fp, "PROGRAM    %s\n", CLUSTALW_4_TCOFFEE);
6061   vfclose (fp);}           
6062   
6063   sprintf (list[n][0], "clustalw_msa");
6064   sprintf (list[n][1], "%s", vtmpnam(NULL));
6065   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
6066   fprintf ( fp, "DOC clustalw[%s]\n", CLUSTALW_ADDRESS);
6067   fprintf ( fp, "EXECUTABLE clustalw\n");
6068   fprintf ( fp, "ALN_MODE   multiple\n");
6069   fprintf ( fp, "OUT_MODE   aln\n");
6070   fprintf ( fp, "IN_FLAG    %sINFILE=\n", CWF);
6071   fprintf ( fp, "OUT_FLAG   %sOUTFILE=\n", CWF);
6072   fprintf ( fp, "PARAM      %sOUTORDER=INPUT %sNEWTREE=cw.tmp.dnd %salign\n",CWF,CWF,CWF);
6073   fprintf ( fp, "SEQ_TYPE   S\n");
6074   fprintf ( fp, "ADDRESS    %s\n", CLUSTALW_ADDRESS);
6075   fprintf ( fp, "PROGRAM    %s\n", CLUSTALW_4_TCOFFEE);
6076   vfclose (fp);}           
6077   
6078   sprintf (list[n][0], "mafftdef_pair");
6079   sprintf (list[n][1], "%s", vtmpnam(NULL));
6080   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
6081     fprintf ( fp, "EXECUTABLE mafft\n");
6082   fprintf ( fp, "DOC Mafft [%s]\n", MAFFT_ADDRESS);
6083   fprintf ( fp, "ALN_MODE   pairwise\n");
6084   fprintf ( fp, "OUT_MODE   aln\n");
6085   fprintf ( fp, "PARAM1  \n");
6086   fprintf ( fp, "IN_FLAG    &bnsp\n");
6087   fprintf ( fp, "OUT_FLAG   >\n");
6088   fprintf ( fp, "PARAM      &bnsp2>/dev/null\n"); 
6089   fprintf ( fp, "SEQ_TYPE   S\n");
6090   fprintf ( fp, "ADDRESS    %s\n", MAFFT_ADDRESS);
6091   fprintf ( fp, "PROGRAM    %s\n", MAFFT_4_TCOFFEE);
6092   vfclose (fp);}           
6093   
6094
6095   sprintf (list[n][0], "mafftdef_msa");
6096   sprintf (list[n][1], "%s", vtmpnam(NULL));
6097   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
6098     fprintf ( fp, "EXECUTABLE mafft\n");
6099   fprintf ( fp, "DOC Mafft [%s]\n", MAFFT_ADDRESS);
6100   fprintf ( fp, "ALN_MODE   multiple\n");
6101   fprintf ( fp, "OUT_MODE   aln\n");
6102   fprintf ( fp, "PARAM1 --localpair --maxiterate 1000 \n");
6103   fprintf ( fp, "IN_FLAG    &bnsp\n");
6104   fprintf ( fp, "OUT_FLAG   >\n");
6105   fprintf ( fp, "PARAM      &bnsp2>/dev/null\n"); 
6106   fprintf ( fp, "SEQ_TYPE   S\n");
6107   fprintf ( fp, "ADDRESS    %s\n", MAFFT_ADDRESS);
6108   fprintf ( fp, "PROGRAM    %s\n", MAFFT_4_TCOFFEE);
6109   vfclose (fp);}           
6110   
6111   sprintf (list[n][0], "mafft_pair");
6112   sprintf (list[n][1], "%s", vtmpnam(NULL));
6113   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
6114     fprintf ( fp, "EXECUTABLE mafft\n");
6115   fprintf ( fp, "DOC Mafft [%s]\n", MAFFT_ADDRESS);
6116   fprintf ( fp, "ALN_MODE   pairwise\n");
6117   fprintf ( fp, "OUT_MODE   aln\n");
6118   fprintf ( fp, "PARAM1 --localpair --maxiterate 1000 \n");
6119   fprintf ( fp, "IN_FLAG    &bnsp\n");
6120   fprintf ( fp, "OUT_FLAG   >\n");
6121   fprintf ( fp, "PARAM      &bnsp2>/dev/null\n"); 
6122   fprintf ( fp, "SEQ_TYPE   S\n");
6123   fprintf ( fp, "ADDRESS    %s\n", MAFFT_ADDRESS);
6124   fprintf ( fp, "PROGRAM    %s\n", MAFFT_4_TCOFFEE);
6125   vfclose (fp);}           
6126   
6127   sprintf (list[n][0], "mafft_msa");
6128   sprintf (list[n][1], "%s", vtmpnam(NULL));
6129   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
6130    fprintf ( fp, "EXECUTABLE mafft\n");
6131   fprintf ( fp, "DOC Mafft [%s]\n", MAFFT_ADDRESS);
6132   fprintf ( fp, "ALN_MODE   multiple\n");
6133   fprintf ( fp, "OUT_MODE   aln\n");
6134   fprintf ( fp, "PARAM1 --localpair --maxiterate 1000 \n");
6135   fprintf ( fp, "IN_FLAG    &bnsp\n");
6136   fprintf ( fp, "OUT_FLAG   >\n");
6137   fprintf ( fp, "PARAM      &bnsp2>/dev/null\n"); 
6138   fprintf ( fp, "SEQ_TYPE   S\n");
6139   fprintf ( fp, "ADDRESS    %s\n", MAFFT_ADDRESS);
6140   fprintf ( fp, "PROGRAM    %s\n", MAFFT_4_TCOFFEE);
6141   vfclose (fp);}           
6142
6143   sprintf (list[n][0], "mafftjtt_pair");
6144   sprintf (list[n][1], "%s", vtmpnam(NULL));
6145   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
6146   fprintf ( fp, "DOC Mafft [%s]\n", MAFFT_ADDRESS);
6147   fprintf ( fp, "EXECUTABLE mafft \n");
6148   fprintf ( fp, "ALN_MODE   pairwise\n");
6149   fprintf ( fp, "OUT_MODE   aln\n");
6150   fprintf ( fp, "IN_FLAG    &bnsp\n");
6151   fprintf ( fp, "OUT_FLAG   >\n");
6152   fprintf ( fp, "PARAM1 --jtt 250 --localpair --maxiterate 1000 \n");
6153   fprintf ( fp, "PARAM      &bnsp2>/dev/null\n"); 
6154   fprintf ( fp, "SEQ_TYPE   S\n");
6155   fprintf ( fp, "ADDRESS    %s\n", MAFFT_ADDRESS);
6156   fprintf ( fp, "PROGRAM    %s\n", MAFFT_4_TCOFFEE);
6157   vfclose (fp);}           
6158
6159   sprintf (list[n][0], "mafftjtt_msa");
6160   sprintf (list[n][1], "%s", vtmpnam(NULL));
6161   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
6162   fprintf ( fp, "DOC Mafft [%s]\n", MAFFT_ADDRESS);
6163   fprintf ( fp, "EXECUTABLE mafft \n");
6164   
6165   fprintf ( fp, "ALN_MODE   multiple\n");
6166   fprintf ( fp, "OUT_MODE   aln\n");
6167   fprintf ( fp, "IN_FLAG    &bnsp\n");
6168   fprintf ( fp, "OUT_FLAG   >\n");
6169   fprintf ( fp, "PARAM1 --jtt 250 --localpair --maxiterate 1000 \n");
6170   fprintf ( fp, "PARAM      &bnsp2>/dev/null\n"); 
6171   fprintf ( fp, "SEQ_TYPE   S\n");
6172   fprintf ( fp, "ADDRESS    %s\n", MAFFT_ADDRESS);
6173   fprintf ( fp, "PROGRAM    %s\n", MAFFT_4_TCOFFEE);
6174   vfclose (fp);}           
6175         
6176   sprintf (list[n][0], "mafftgins_pair");
6177   sprintf (list[n][1], "%s", vtmpnam(NULL));
6178   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
6179    fprintf ( fp, "EXECUTABLE mafft\n");
6180   fprintf ( fp, "DOC Mafft [%s]\n", MAFFT_ADDRESS);
6181   fprintf ( fp, "ALN_MODE   pairwise\n");
6182   fprintf ( fp, "OUT_MODE   aln\n");
6183   fprintf ( fp, "PARAM1 --globalpair --maxiterate 1000 \n");
6184   fprintf ( fp, "IN_FLAG    &bnsp\n");
6185   fprintf ( fp, "OUT_FLAG   >\n");
6186   fprintf ( fp, "PARAM      &bnsp2>/dev/null\n"); 
6187   fprintf ( fp, "SEQ_TYPE   S\n");
6188   fprintf ( fp, "ADDRESS    %s\n", MAFFT_ADDRESS);
6189   fprintf ( fp, "PROGRAM    %s\n", MAFFT_4_TCOFFEE);
6190   vfclose (fp);}           
6191
6192   sprintf (list[n][0], "mafftgins_msa");
6193   sprintf (list[n][1], "%s", vtmpnam(NULL));
6194   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
6195    fprintf ( fp, "EXECUTABLE mafft\n");
6196   fprintf ( fp, "DOC Mafft [%s]\n", MAFFT_ADDRESS);
6197   fprintf ( fp, "ALN_MODE   multiple\n");
6198   fprintf ( fp, "OUT_MODE   aln\n");
6199   fprintf ( fp, "PARAM1 --globalpair --maxiterate 1000 \n");
6200   fprintf ( fp, "IN_FLAG    &bnsp\n");
6201   fprintf ( fp, "OUT_FLAG   >\n");
6202   fprintf ( fp, "PARAM      &bnsp2>/dev/null\n"); 
6203   fprintf ( fp, "SEQ_TYPE   S\n");
6204   fprintf ( fp, "ADDRESS    %s\n", MAFFT_ADDRESS);
6205   fprintf ( fp, "PROGRAM    %s\n", MAFFT_4_TCOFFEE);
6206   vfclose (fp);}           
6207   
6208   sprintf (list[n][0], "dialigntx_pair");
6209   sprintf (list[n][1], "%s", vtmpnam(NULL));
6210   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
6211   fprintf ( fp, "DOC dialign-tx [%s]\n", DIALIGNTX_ADDRESS);
6212   fprintf ( fp, "EXECUTABLE dialign-tx\n");
6213   fprintf ( fp, "ALN_MODE   pairwise\n");
6214   fprintf ( fp, "OUT_MODE   aln\n");
6215   if ( isdir (DIALIGNTX_DIR))
6216     fprintf ( fp, "PARAM1 %s \n", DIALIGNTX_DIR);
6217   else
6218     fprintf ( fp, "PARAM1 %s \n", get_mcoffee_4_tcoffee());
6219   fprintf ( fp, "IN_FLAG    &bnsp\n");
6220   fprintf ( fp, "OUT_FLAG   &bnsp\n");
6221   fprintf ( fp, "PARAM      >/dev/null&bnsp2>/dev/null\n"); 
6222   fprintf ( fp, "SEQ_TYPE   S\n");
6223   fprintf ( fp, "ADDRESS    %s\n", DIALIGNTX_ADDRESS);
6224   fprintf ( fp, "PROGRAM    %s\n", DIALIGNTX_4_TCOFFEE);
6225   vfclose (fp);}           
6226   
6227   sprintf (list[n][0], "dialigntx_msa");
6228   sprintf (list[n][1], "%s", vtmpnam(NULL));
6229   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
6230   fprintf ( fp, "DOC dialign-tx [%s]\n", DIALIGNTX_ADDRESS);
6231   fprintf ( fp, "EXECUTABLE dialign-tx\n");
6232   fprintf ( fp, "ALN_MODE   multiple\n");
6233   fprintf ( fp, "OUT_MODE   aln\n");
6234   if ( isdir (DIALIGNTX_DIR))
6235     fprintf ( fp, "PARAM1 %s \n", DIALIGNTX_DIR);
6236   else
6237     fprintf ( fp, "PARAM1 %s \n", get_mcoffee_4_tcoffee());
6238   fprintf ( fp, "IN_FLAG    &bnsp\n");
6239   fprintf ( fp, "OUT_FLAG   &bnsp\n");
6240   fprintf ( fp, "PARAM      >/dev/null&bnsp2>/dev/null\n"); 
6241   fprintf ( fp, "SEQ_TYPE   S\n");
6242   fprintf ( fp, "ADDRESS    %s\n", DIALIGNTX_ADDRESS);
6243   fprintf ( fp, "PROGRAM    %s\n", DIALIGNTX_4_TCOFFEE);
6244   vfclose (fp);}           
6245   
6246   sprintf (list[n][0], "dialignt_pair");
6247   sprintf (list[n][1], "%s", vtmpnam(NULL));
6248   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
6249   fprintf ( fp, "DOC dialign-tx [%s]\n", DIALIGNT_ADDRESS);
6250   fprintf ( fp, "EXECUTABLE dialign-tx\n");
6251   fprintf ( fp, "ALN_MODE   pairwise\n");
6252   fprintf ( fp, "OUT_MODE   aln\n");
6253   if ( isdir (DIALIGNT_DIR))
6254     fprintf ( fp, "PARAM1 %s \n", DIALIGNT_DIR);
6255   else
6256     fprintf ( fp, "PARAM1 %s \n", get_mcoffee_4_tcoffee());
6257   fprintf ( fp, "IN_FLAG    &bnsp\n");
6258   fprintf ( fp, "OUT_FLAG   &bnsp\n");
6259   fprintf ( fp, "PARAM      >/dev/null&bnsp2>/dev/null\n"); 
6260   fprintf ( fp, "SEQ_TYPE   S\n");
6261   fprintf ( fp, "ADDRESS    %s\n", DIALIGNT_ADDRESS);
6262   fprintf ( fp, "PROGRAM    %s\n", DIALIGNT_4_TCOFFEE);
6263
6264   vfclose (fp);}           
6265   
6266   sprintf (list[n][0], "dialignt_msa");
6267   sprintf (list[n][1], "%s", vtmpnam(NULL));
6268   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
6269   fprintf ( fp, "DOC dialign-tx [%s]\n", DIALIGNT_ADDRESS);
6270   fprintf ( fp, "EXECUTABLE dialign-tx\n");
6271   fprintf ( fp, "ALN_MODE   multiple\n");
6272   fprintf ( fp, "OUT_MODE   aln\n");  
6273   if ( isdir (DIALIGNT_DIR))
6274     fprintf ( fp, "PARAM1 %s \n", DIALIGNT_DIR);
6275   else
6276     fprintf ( fp, "PARAM1 %s \n", get_mcoffee_4_tcoffee());
6277   fprintf ( fp, "IN_FLAG    &bnsp\n");
6278   fprintf ( fp, "OUT_FLAG   &bnsp\n");
6279   fprintf ( fp, "PARAM      >/dev/null&bnsp2>/dev/null\n"); 
6280   fprintf ( fp, "SEQ_TYPE   S\n");
6281   fprintf ( fp, "ADDRESS    %s\n", DIALIGNT_ADDRESS);
6282   fprintf ( fp, "PROGRAM    %s\n", DIALIGNT_4_TCOFFEE);
6283  
6284   vfclose (fp);}           
6285   
6286   sprintf (list[n][0], "poa_pair");
6287   sprintf (list[n][1], "%s", vtmpnam(NULL));
6288   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
6289   fprintf ( fp, "DOC Partial Order Graph Alignment [%s]\n", POA_ADDRESS);
6290   fprintf ( fp, "EXECUTABLE poa\n");
6291   fprintf ( fp, "ALN_MODE   pairwise\n");
6292   fprintf ( fp, "OUT_MODE   aln\n");
6293   fprintf ( fp, "PARAM1 -toupper \n");
6294   fprintf ( fp, "IN_FLAG    -read_fasta&bnsp\n");
6295   fprintf ( fp, "OUT_FLAG   -clustal&bnsp\n");
6296   if (file_exists (POA_DIR, POA_FILE1))
6297       fprintf ( fp, "PARAM      %s/%s&bnsp2>/dev/null\n",POA_DIR,POA_FILE1); 
6298   else
6299     fprintf ( fp, "PARAM      %s/%s&bnsp2>/dev/null\n", get_mcoffee_4_tcoffee(), POA_FILE1); 
6300   fprintf ( fp, "SEQ_TYPE   S\n");
6301   fprintf ( fp, "ADDRESS    %s\n", POA_ADDRESS);
6302   fprintf ( fp, "PROGRAM    %s\n",POA_4_TCOFFEE);
6303
6304   vfclose (fp);}           
6305   
6306   sprintf (list[n][0], "poa_msa");
6307   sprintf (list[n][1], "%s", vtmpnam(NULL));
6308   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
6309   fprintf ( fp, "DOC Partial Order Graph Alignment [%s]\n", POA_ADDRESS);
6310   fprintf ( fp, "EXECUTABLE poa\n");
6311   fprintf ( fp, "ALN_MODE   multiple\n");
6312   fprintf ( fp, "OUT_MODE   aln\n");
6313   fprintf ( fp, "PARAM1 -toupper \n");
6314   fprintf ( fp, "IN_FLAG    -read_fasta&bnsp\n");
6315   fprintf ( fp, "OUT_FLAG   -clustal&bnsp\n");
6316   if (file_exists (POA_DIR, POA_FILE1))
6317     fprintf ( fp, "PARAM      %s/%s&bnsp2>/dev/null\n",POA_DIR,POA_FILE1); 
6318   else
6319     fprintf ( fp, "PARAM      %s/%s&bnsp2>/dev/null\n", get_mcoffee_4_tcoffee(), POA_FILE1); 
6320   fprintf ( fp, "SEQ_TYPE   S\n");
6321   fprintf ( fp, "ADDRESS    %s\n", POA_ADDRESS);
6322   fprintf ( fp, "PROGRAM    %s\n",POA_4_TCOFFEE);
6323   vfclose (fp);}           
6324   
6325   sprintf (list[n][0], "probcons_pair");
6326   sprintf (list[n][1], "%s", vtmpnam(NULL));
6327   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
6328   fprintf ( fp, "DOC probcons [%s]\n", PROBCONS_ADDRESS);
6329   fprintf ( fp, "ALN_MODE   pairwise\n");
6330   fprintf ( fp, "OUT_MODE   aln\n");
6331   fprintf ( fp, "IN_FLAG    &bnsp\n");
6332   fprintf ( fp, "OUT_FLAG   >\n");
6333   fprintf ( fp, "PARAM      &bnsp2>/dev/null\n"); 
6334   fprintf ( fp, "SEQ_TYPE   S\n");
6335   if ( strm (retrieve_seq_type(), "DNA") || strm (retrieve_seq_type(), "RNA"))fprintf ( fp, "EXECUTABLE probconsRNA\n");
6336   else fprintf ( fp, "EXECUTABLE probcons\n");
6337   fprintf ( fp, "ADDRESS    %s\n", PROBCONS_ADDRESS);
6338   fprintf ( fp, "PROGRAM    %s\n",PROBCONS_4_TCOFFEE);
6339   vfclose (fp);}           
6340
6341   sprintf (list[n][0], "probcons_msa");
6342   sprintf (list[n][1], "%s", vtmpnam(NULL));
6343   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
6344   fprintf ( fp, "DOC probcons [%s]\n", PROBCONS_ADDRESS);
6345   fprintf ( fp, "ALN_MODE   multiple\n");
6346   fprintf ( fp, "OUT_MODE   aln\n");
6347   fprintf ( fp, "IN_FLAG    &bnsp\n");
6348   fprintf ( fp, "OUT_FLAG   >\n");
6349   fprintf ( fp, "PARAM      &bnsp2>/dev/null\n"); 
6350   fprintf ( fp, "SEQ_TYPE   S\n");
6351   if ( strm (retrieve_seq_type(), "DNA") || strm (retrieve_seq_type(), "RNA"))fprintf ( fp, "EXECUTABLE probconsRNA\n");
6352   else fprintf ( fp, "EXECUTABLE probcons\n");
6353   fprintf ( fp, "ADDRESS    %s\n", PROBCONS_ADDRESS);
6354   fprintf ( fp, "PROGRAM    %s\n",PROBCONS_4_TCOFFEE);
6355   vfclose (fp);}           
6356   
6357   sprintf (list[n][0], "probconsRNA_pair");
6358   sprintf (list[n][1], "%s", vtmpnam(NULL));
6359   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
6360   fprintf ( fp, "DOC probcons [%s]\n", PROBCONSRNA_ADDRESS);
6361   fprintf ( fp, "ALN_MODE   pairwise\n");
6362   fprintf ( fp, "OUT_MODE   aln\n");
6363   fprintf ( fp, "IN_FLAG    &bnsp\n");
6364   fprintf ( fp, "OUT_FLAG   >\n");
6365   fprintf ( fp, "PARAM      &bnsp2>/dev/null\n"); 
6366   fprintf ( fp, "SEQ_TYPE   S\n");
6367   fprintf ( fp, "EXECUTABLE probconsRNA\n");
6368   fprintf ( fp, "ADDRESS    %s\n", PROBCONSRNA_ADDRESS);
6369   fprintf ( fp, "PROGRAM    %s\n",PROBCONSRNA_4_TCOFFEE);
6370   vfclose (fp);}           
6371
6372   sprintf (list[n][0], "probconsRNA_msa");
6373   sprintf (list[n][1], "%s", vtmpnam(NULL));
6374   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
6375   fprintf ( fp, "DOC probcons [%s]\n", PROBCONSRNA_ADDRESS);
6376   fprintf ( fp, "ALN_MODE   multiple\n");
6377   fprintf ( fp, "OUT_MODE   aln\n");
6378   fprintf ( fp, "IN_FLAG    &bnsp\n");
6379   fprintf ( fp, "OUT_FLAG   >\n");
6380   fprintf ( fp, "PARAM      &bnsp2>/dev/null\n"); 
6381   fprintf ( fp, "SEQ_TYPE   S\n");
6382   fprintf ( fp, "EXECUTABLE probconsRNA\n");
6383   fprintf ( fp, "ADDRESS    %s\n", PROBCONSRNA_ADDRESS);
6384   fprintf ( fp, "PROGRAM    %s\n",PROBCONSRNA_4_TCOFFEE);
6385   vfclose (fp);}           
6386   
6387   
6388
6389   sprintf (list[n][0], "muscle_pair");
6390   sprintf (list[n][1], "%s", vtmpnam(NULL));
6391   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
6392   fprintf ( fp, "DOC Muscle [%s]\n", MUSCLE_ADDRESS);
6393   fprintf ( fp, "EXECUTABLE muscle\n");
6394   fprintf ( fp, "ALN_MODE   pairwise\n");
6395   fprintf ( fp, "OUT_MODE   aln\n");
6396   fprintf ( fp, "IN_FLAG    -in&bnsp\n");
6397   fprintf ( fp, "OUT_FLAG   -out&bnsp\n");
6398   fprintf ( fp, "SEQ_TYPE   S\n");
6399   fprintf ( fp, "ADDRESS    %s\n", MUSCLE_ADDRESS);
6400   fprintf ( fp, "PROGRAM    %s\n", MUSCLE_4_TCOFFEE);  
6401   vfclose (fp);}           
6402   
6403   sprintf (list[n][0], "muscle_msa");
6404   sprintf (list[n][1], "%s", vtmpnam(NULL));
6405    n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
6406      fprintf ( fp, "DOC Muscle [%s]\n", MUSCLE_ADDRESS);
6407      fprintf ( fp, "EXECUTABLE muscle\n");
6408      fprintf ( fp, "ALN_MODE   multiple\n");
6409      fprintf ( fp, "OUT_MODE   aln\n");
6410      fprintf ( fp, "IN_FLAG    -in&bnsp\n");
6411      fprintf ( fp, "OUT_FLAG   -out&bnsp\n");
6412      fprintf ( fp, "SEQ_TYPE   S\n");
6413      fprintf ( fp, "ADDRESS    %s\n", MUSCLE_ADDRESS);
6414      fprintf ( fp, "PROGRAM    %s\n", MUSCLE_4_TCOFFEE);  
6415      vfclose (fp);}        
6416    
6417   sprintf (list[n][0], "t_coffee_pair");
6418   sprintf (list[n][1], "%s", vtmpnam(NULL));
6419   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
6420     fprintf ( fp, "EXECUTABLE t_coffee\n");
6421     fprintf ( fp, "DOC T-Coffee [%s]\n", TCOFFEE_ADDRESS);
6422     fprintf ( fp, "ALN_MODE   pairwise\n");
6423     fprintf ( fp, "OUT_MODE   aln\n");
6424     fprintf ( fp, "IN_FLAG    -infile&bnsp\n");
6425     fprintf ( fp, "OUT_FLAG   -outfile&bnsp\n");
6426     fprintf ( fp, "SEQ_TYPE   S\n");
6427     fprintf ( fp, "ADDRESS    %s\n", TCOFFEE_ADDRESS);
6428     fprintf ( fp, "PROGRAM    %s\n", TCOFFEE_4_TCOFFEE);  
6429     vfclose (fp);}         
6430   
6431   sprintf (list[n][0], "t_coffee_msa");
6432   sprintf (list[n][1], "%s", vtmpnam(NULL));
6433   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w");
6434     fprintf ( fp, "EXECUTABLE t_coffee\n");
6435     fprintf ( fp, "DOC T-Coffee [%s]\n", TCOFFEE_ADDRESS);
6436     fprintf ( fp, "ALN_MODE   multiple\n");
6437     fprintf ( fp, "OUT_MODE   aln\n");
6438     fprintf ( fp, "IN_FLAG    -infile&bnsp\n");
6439     fprintf ( fp, "OUT_FLAG   -outfile&bnsp\n");
6440     fprintf ( fp, "SEQ_TYPE   S\n");
6441     fprintf ( fp, "ADDRESS    %s\n", TCOFFEE_ADDRESS);
6442     fprintf ( fp, "PROGRAM    %s\n", TCOFFEE_4_TCOFFEE);  
6443     vfclose (fp);}         
6444   
6445   sprintf (list[n][0], "pcma_pair");
6446   sprintf (list[n][1], "%s", vtmpnam(NULL));
6447   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w"); 
6448   fprintf ( fp, "DOC PCMA [%s]\n", PCMA_ADDRESS);
6449   fprintf ( fp, "EXECUTABLE pcma\n");
6450   fprintf ( fp, "ALN_MODE   pairwise\n");
6451   fprintf ( fp, "OUT_MODE   aln\n");
6452   fprintf ( fp, "IN_FLAG    -infile=\n");
6453   fprintf ( fp, "OUT_FLAG   -outfile=\n");
6454   fprintf ( fp, "SEQ_TYPE   S\n");
6455   fprintf ( fp, "ADDRESS    %s\n", PCMA_ADDRESS);
6456   fprintf ( fp, "PROGRAM    %s\n", PCMA_4_TCOFFEE);  
6457   vfclose (fp);}           
6458   
6459   sprintf (list[n][0], "pcma_msa");
6460   sprintf (list[n][1], "%s", vtmpnam(NULL));
6461   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w"); 
6462   fprintf ( fp, "DOC PCMA [%s]\n", PCMA_ADDRESS);
6463   fprintf ( fp, "EXECUTABLE pcma\n");
6464   fprintf ( fp, "ALN_MODE   multiple\n");
6465   fprintf ( fp, "OUT_MODE   aln\n");
6466   fprintf ( fp, "IN_FLAG    -infile=\n");
6467   fprintf ( fp, "OUT_FLAG   -outfile=\n");
6468   fprintf ( fp, "SEQ_TYPE   S\n");
6469   fprintf ( fp, "ADDRESS    %s\n", PCMA_ADDRESS);
6470   fprintf ( fp, "PROGRAM    %s\n", PCMA_4_TCOFFEE);  
6471   vfclose (fp);}           
6472   
6473   sprintf (list[n][0], "kalign_pair");
6474   sprintf (list[n][1], "%s", vtmpnam(NULL));
6475   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w"); 
6476     fprintf ( fp, "EXECUTABLE kalign\n");
6477   fprintf ( fp, "DOC kalign [%s]\n", KALIGN_ADDRESS);
6478   fprintf ( fp, "ALN_MODE   pairwise\n");
6479   fprintf ( fp, "OUT_MODE   aln\n");
6480   fprintf ( fp, "IN_FLAG    -i&bnsp\n");
6481   fprintf ( fp, "OUT_FLAG   -o&bnsp\n");
6482   fprintf ( fp, "SEQ_TYPE   S\n");
6483   fprintf ( fp, "ADDRESS    %s\n", KALIGN_ADDRESS);
6484   fprintf ( fp, "PROGRAM    %s\n", KALIGN_4_TCOFFEE);  
6485   vfclose (fp);}              
6486
6487   sprintf (list[n][0], "kalign_msa");
6488   sprintf (list[n][1], "%s", vtmpnam(NULL));
6489   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w"); 
6490     fprintf ( fp, "EXECUTABLE kalign\n");
6491   fprintf ( fp, "DOC kalign [%s]\n", KALIGN_ADDRESS);
6492   fprintf ( fp, "ALN_MODE   multiple\n");
6493   fprintf ( fp, "OUT_MODE   aln\n");
6494   fprintf ( fp, "IN_FLAG    -i&bnsp\n");
6495   fprintf ( fp, "OUT_FLAG   -o&bnsp\n");
6496   fprintf ( fp, "SEQ_TYPE   S\n");
6497   fprintf ( fp, "ADDRESS    %s\n", KALIGN_ADDRESS);
6498   fprintf ( fp, "PROGRAM    %s\n", KALIGN_4_TCOFFEE);  
6499   vfclose (fp);}              
6500
6501   sprintf (list[n][0], "amap_pair");
6502   sprintf (list[n][1], "%s", vtmpnam(NULL));
6503   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w"); 
6504     fprintf ( fp, "EXECUTABLE amap\n");
6505   fprintf ( fp, "DOC amap [%s]\n", AMAP_ADDRESS);
6506   fprintf ( fp, "ALN_MODE   pairwise\n");
6507   fprintf ( fp, "OUT_MODE   aln\n");
6508   fprintf ( fp, "IN_FLAG    &bnsp\n");
6509   fprintf ( fp, "OUT_FLAG   >\n");
6510   fprintf ( fp, "PARAM      &bnsp2>/dev/null\n"); 
6511   fprintf ( fp, "SEQ_TYPE   S\n");
6512   fprintf ( fp, "ADDRESS    %s\n", AMAP_ADDRESS);
6513   fprintf ( fp, "PROGRAM    %s\n", AMAP_4_TCOFFEE);  
6514   vfclose (fp);}           
6515
6516   sprintf (list[n][0], "amap_msa");
6517   sprintf (list[n][1], "%s", vtmpnam(NULL));
6518   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w"); 
6519     fprintf ( fp, "EXECUTABLE amap\n");
6520   fprintf ( fp, "DOC amap [%s]\n", AMAP_ADDRESS);
6521   fprintf ( fp, "ALN_MODE   multiple\n");
6522   fprintf ( fp, "OUT_MODE   aln\n");
6523   fprintf ( fp, "IN_FLAG    &bnsp\n");
6524   fprintf ( fp, "OUT_FLAG   >\n");
6525   fprintf ( fp, "PARAM      &bnsp2>/dev/null\n"); 
6526   fprintf ( fp, "SEQ_TYPE   S\n");
6527   fprintf ( fp, "ADDRESS    %s\n", AMAP_ADDRESS);
6528   fprintf ( fp, "PROGRAM    %s\n", AMAP_4_TCOFFEE);  
6529   vfclose (fp);}           
6530   
6531   sprintf (list[n][0], "proda_pair");
6532   sprintf (list[n][1], "%s", vtmpnam(NULL));
6533   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w"); 
6534   fprintf ( fp, "DOC proda [%s]\n", PRODA_ADDRESS);
6535   fprintf ( fp, "EXECUTABLE proda\n");
6536   fprintf ( fp, "ALN_MODE   pairwise\n");
6537   fprintf ( fp, "OUT_MODE   aln\n");
6538   fprintf ( fp, "IN_FLAG    &bnsp\n");
6539   fprintf ( fp, "OUT_FLAG   >\n");
6540   fprintf ( fp, "PARAM      &bnsp2>/dev/null\n"); 
6541   fprintf ( fp, "SEQ_TYPE   S\n");
6542   fprintf ( fp, "ADDRESS    %s\n", PRODA_ADDRESS);
6543   fprintf ( fp, "PROGRAM    %s\n", PRODA_4_TCOFFEE);  
6544   vfclose (fp);}           
6545   
6546   sprintf (list[n][0], "proda_msa");
6547   sprintf (list[n][1], "%s", vtmpnam(NULL));
6548   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w"); 
6549   fprintf ( fp, "DOC proda [%s]\n", PRODA_ADDRESS);
6550   fprintf ( fp, "EXECUTABLE proda\n");
6551   fprintf ( fp, "ALN_MODE   multiple\n");
6552   fprintf ( fp, "OUT_MODE   aln\n");
6553   fprintf ( fp, "IN_FLAG    &bnsp\n");
6554   fprintf ( fp, "OUT_FLAG   >\n");
6555   fprintf ( fp, "PARAM      &bnsp2>/dev/null\n"); 
6556   fprintf ( fp, "SEQ_TYPE   S\n");
6557   fprintf ( fp, "ADDRESS    %s\n", PRODA_ADDRESS);
6558   fprintf ( fp, "PROGRAM    %s\n", PRODA_4_TCOFFEE);  
6559   vfclose (fp);}           
6560   
6561   sprintf (list[n][0], "prank_pair");
6562   sprintf (list[n][1], "%s", vtmpnam(NULL));
6563   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w"); 
6564   fprintf ( fp, "DOC prank [%s]\n", PRANK_ADDRESS);
6565   fprintf ( fp, "EXECUTABLE tc_generic_method.pl\n");
6566   fprintf ( fp, "ALN_MODE  pairwise\n");
6567   fprintf ( fp, "OUT_MODE  aln\n");
6568   fprintf ( fp, "PARAM -method=%s -mode=seq_msa -tmpdir=%s\n",(getenv("PRANK_4_TCOFFEE"))?getenv("PRANK_4_TCOFFEE"):PRANK_4_TCOFFEE, get_tmp_4_tcoffee());
6569   fprintf ( fp, "IN_FLAG -infile=\n");
6570   fprintf ( fp, "OUT_FLAG -outfile=\n");
6571   fprintf ( fp, "SEQ_TYPE   S\n");
6572   fprintf ( fp, "ADDRESS    %s\n", PRANK_ADDRESS);
6573   fprintf ( fp, "PROGRAM    %s\n", PRANK_4_TCOFFEE);  
6574   vfclose (fp);}
6575   
6576   sprintf (list[n][0], "prank_msa");
6577   sprintf (list[n][1], "%s", vtmpnam(NULL));
6578   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w"); 
6579   fprintf ( fp, "DOC prank [%s]\n", PRANK_ADDRESS);
6580   fprintf ( fp, "EXECUTABLE tc_generic_method.pl\n");
6581   fprintf ( fp, "ALN_MODE  multiple\n");
6582   fprintf ( fp, "OUT_MODE  aln\n");
6583   fprintf ( fp, "PARAM -method=%s -mode=seq_msa -tmpdir=%s\n",(getenv("PRANK_4_TCOFFEE"))?getenv("PRANK_4_TCOFFEE"):PRANK_4_TCOFFEE, get_tmp_4_tcoffee());
6584   fprintf ( fp, "IN_FLAG -infile=\n");
6585   fprintf ( fp, "OUT_FLAG -outfile=\n");
6586   fprintf ( fp, "SEQ_TYPE   S\n");
6587   fprintf ( fp, "ADDRESS    %s\n", PRANK_ADDRESS);
6588   fprintf ( fp, "PROGRAM    %s\n", PRANK_4_TCOFFEE);  
6589   vfclose (fp);}
6590
6591   
6592   sprintf (list[n][0], "em");
6593   sprintf (list[n][1], "%s", vtmpnam(NULL));
6594   n++;
6595   if (method==NULL || lstrstr (method,"em@"))
6596     {
6597       
6598       fp=vfopen (list[n-1][1], "w"); 
6599       if (method)
6600         {
6601           char **l2;
6602           l2=string2list2 ( method, "@");
6603           fprintf ( fp, "PARAM -method=%s -mode=seq_msa -tmpdir=%s\n",l2[2], get_tmp_4_tcoffee());
6604           fprintf ( fp, "ALN_MODE  %s\n", l2[3]);
6605           free_char (l2, -1);
6606         }
6607       fprintf ( fp, "EXECUTABLE tc_generic_method.pl\n");
6608       fprintf ( fp, "OUT_MODE  aln\n");
6609       fprintf ( fp, "IN_FLAG -infile=\n");
6610       fprintf ( fp, "OUT_FLAG -outfile=\n");
6611       fprintf ( fp, "SEQ_TYPE   S\n");
6612       fprintf ( fp, "ADDRESS    %s\n", ADDRESS_BUILT_IN);
6613       fprintf ( fp, "PROGRAM    %s\n", PROGRAM_BUILT_IN);
6614       vfclose (fp);
6615     }
6616   
6617   sprintf (list[n][0], "consan_pair");
6618   sprintf (list[n][1], "%s", vtmpnam(NULL));
6619   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w"); 
6620   fprintf ( fp, "DOC consan (sfold) RNA pairwise sequence aligner [%s]\n", CONSAN_ADDRESS);
6621   fprintf ( fp, "EXECUTABLE fasta_seq2consan_aln.pl \n");
6622   fprintf ( fp, "ALN_MODE   pairwise\n");
6623   fprintf ( fp, "OUT_MODE   aln\n");
6624   fprintf ( fp, "IN_FLAG    -i&bnsp\n");
6625   fprintf ( fp, "OUT_FLAG   -o&bnsp\n");
6626   fprintf ( fp, "PARAM      -d&bnsp%s&bnsp2>/dev/null\n",get_mcoffee_4_tcoffee()); 
6627   fprintf ( fp, "SEQ_TYPE   S\n");
6628   fprintf ( fp, "ADDRESS    %s\n", CONSAN_ADDRESS);
6629   fprintf ( fp, "PROGRAM    %s\n", CONSAN_4_TCOFFEE);  
6630   vfclose (fp);}
6631   
6632   sprintf (list[n][0], "RNAplfold");
6633   sprintf (list[n][1], "%s", vtmpnam(NULL));
6634   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w"); 
6635   fprintf ( fp, "EXECUTABLE RNAplfold \n");
6636   fprintf ( fp, "ALN_MODE   predict\n");
6637   fprintf ( fp, "SEQ_TYPE   RNA\n");
6638   fprintf ( fp, "ADDRESS    %s\n", RNAPLFOLD_ADDRESS);
6639   fprintf ( fp, "PROGRAM    %s\n", RNAPLFOLD_4_TCOFFEE);  
6640   vfclose (fp);}
6641
6642   sprintf (list[n][0], "HMMtop");
6643   sprintf (list[n][1], "%s", vtmpnam(NULL));
6644   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w"); 
6645   fprintf ( fp, "EXECUTABLE HMMtop \n");
6646   fprintf ( fp, "ALN_MODE   predict\n");
6647   fprintf ( fp, "SEQ_TYPE   PROTEIN\n");
6648   fprintf ( fp, "ADDRESS    %s\n", HMMTOP_ADDRESS);
6649   fprintf ( fp, "PROGRAM    %s\n", HMMTOP_4_TCOFFEE);  
6650   vfclose (fp);}
6651
6652   sprintf (list[n][0], "GOR4");
6653   sprintf (list[n][1], "%s", vtmpnam(NULL));
6654   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w"); 
6655   fprintf ( fp, "EXECUTABLE GORIV \n");
6656   fprintf ( fp, "ALN_MODE   predict\n");
6657   fprintf ( fp, "SEQ_TYPE   PROTEIN\n");
6658   fprintf ( fp, "ADDRESS    %s\n", GOR4_ADDRESS);
6659   fprintf ( fp, "PROGRAM    %s\n", GOR4_4_TCOFFEE);  
6660   vfclose (fp);}
6661
6662   sprintf (list[n][0], "wublast_client");
6663   sprintf (list[n][1], "%s", vtmpnam(NULL));
6664   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w"); 
6665   fprintf ( fp, "EXECUTABLE wublast.pl \n");
6666   fprintf ( fp, "ALN_MODE   predict\n");
6667   fprintf ( fp, "SEQ_TYPE   PROTEIN\n");
6668   fprintf ( fp, "ADDRESS    %s\n", EBIWUBLASTc_ADDRESS);
6669   fprintf ( fp, "PROGRAM    %s\n", EBIWUBLASTc_4_TCOFFEE);  
6670   vfclose (fp);}
6671
6672   sprintf (list[n][0], "blastpgp_client");
6673   sprintf (list[n][1], "%s", vtmpnam(NULL));
6674   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w"); 
6675   fprintf ( fp, "EXECUTABLE blastpgp.pl \n");
6676   fprintf ( fp, "ALN_MODE   predict\n");
6677   fprintf ( fp, "SEQ_TYPE   PROTEIN\n");
6678     
6679   fprintf ( fp, "ADDRESS    %s\n", EBIBLASTPGPc_ADDRESS);
6680   fprintf ( fp, "PROGRAM    %s\n", EBIBLASTPGPc_4_TCOFFEE);  
6681   vfclose (fp);}
6682   
6683   sprintf (list[n][0], "ncbi_netblast");
6684   sprintf (list[n][1], "%s", vtmpnam(NULL));
6685   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w"); 
6686   fprintf ( fp, "EXECUTABLE blastcl3 \n");
6687   fprintf ( fp, "ALN_MODE   predict\n");
6688   fprintf ( fp, "SEQ_TYPE   PROTEIN\n");
6689     
6690   fprintf ( fp, "ADDRESS    %s\n", NCBIWEBBLAST_ADDRESS);
6691   fprintf ( fp, "PROGRAM    %s\n", NCBIWEBBLAST_4_TCOFFEE);  
6692   vfclose (fp);}
6693   
6694   sprintf (list[n][0], "local_ncbiblast");
6695   sprintf (list[n][1], "%s", vtmpnam(NULL));
6696   n++;if (method==NULL || strm (method, list[n-1][0])){fp=vfopen (list[n-1][1], "w"); 
6697   fprintf ( fp, "EXECUTABLE blastall \n");
6698   fprintf ( fp, "ALN_MODE   predict\n");
6699   fprintf ( fp, "SEQ_TYPE   PROTEIN\n");
6700     
6701   fprintf ( fp, "ADDRESS    %s\n", NCBIBLAST_ADDRESS);
6702   fprintf ( fp, "PROGRAM    %s\n", NCBIBLAST_4_TCOFFEE);  
6703   vfclose (fp);}
6704
6705   list[n]=NULL;
6706   return list;
6707 }
6708   
6709
6710
6711 /*********************************COPYRIGHT NOTICE**********************************/
6712 /*© Centro de Regulacio Genomica */
6713 /*and */
6714 /*Cedric Notredame */
6715 /*Tue Oct 27 10:12:26 WEST 2009. */
6716 /*All rights reserved.*/
6717 /*This file is part of T-COFFEE.*/
6718 /**/
6719 /*    T-COFFEE is free software; you can redistribute it and/or modify*/
6720 /*    it under the terms of the GNU General Public License as published by*/
6721 /*    the Free Software Foundation; either version 2 of the License, or*/
6722 /*    (at your option) any later version.*/
6723 /**/
6724 /*    T-COFFEE is distributed in the hope that it will be useful,*/
6725 /*    but WITHOUT ANY WARRANTY; without even the implied warranty of*/
6726 /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the*/
6727 /*    GNU General Public License for more details.*/
6728 /**/
6729 /*    You should have received a copy of the GNU General Public License*/
6730 /*    along with Foobar; if not, write to the Free Software*/
6731 /*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA*/
6732 /*...............................................                                                                                      |*/
6733 /*  If you need some more information*/
6734 /*  cedric.notredame@europe.com*/
6735 /*...............................................                                                                                                                                     |*/
6736 /**/
6737 /**/
6738 /*      */
6739 /*********************************COPYRIGHT NOTICE**********************************/