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