new version of tcoffee 8.99 not yet compiled for ia32 linux (currently compiled for...
[jabaws.git] / binaries / src / tcoffee / t_coffee_source / util_job_handling.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <math.h>
4 #include <stdarg.h>
5 #include <string.h>
6
7 #include "io_lib_header.h"
8 #include "util_lib_header.h"
9 #include "define_header.h"
10 #include "dp_lib_header.h"
11
12
13
14 Job_TC* print_lib_job ( Job_TC *job,char *string, ...)
15 {
16   va_list ap;
17   char **list;
18   char **value;
19   char **name;
20   int a, np, n;
21     
22   char bname[100];
23   char bval[100];
24   
25   list=string2list2(string, " =");
26   n=atoi (list[0]);
27   
28
29   name =vcalloc ( (n-1)/2, sizeof (char*));
30   value=vcalloc ( (n-1)/2, sizeof (char*));
31   
32   
33   va_start (ap, string);
34   for (a=1, np=0; a<n; a++)
35     {
36       if (list[a][0]=='%' && list[a][1]=='s')
37         {
38           value[np++]=duplicate_string (va_arg (ap,char *));
39         }
40       else if (list[a][0]=='%')
41         {
42           if ( list[a][1]=='d')sprintf (bval, "%d", va_arg (ap,int));
43           else if ( list[a][1]=='f')sprintf (bval, "%lf",(float) va_arg (ap,double));
44           else if ( list[a][1]=='p')sprintf (bval, "%ld", (long)va_arg (ap,void *));
45           value[np++]=duplicate_string (bval);
46         }
47       else
48         {
49           sprintf (bname, "%s", list[a]);
50           name[np]=duplicate_string (bname);
51         }
52     }
53   free_arrayN((void**)list, 2);
54   
55   va_end (ap);
56   
57   return print_lib_job2 ( job,np,name,value);
58 }
59
60 static int njobs;
61 Job_TC *print_lib_job2 ( Job_TC* job, int n, char **name, char **value)
62 {
63   int a;
64   
65   if ( job==NULL)
66     {
67       job=vcalloc ( 1, sizeof (Job_TC));
68       job->pl=vcalloc (100, sizeof (char*));job->pl[job->np++]=(void*)job->pl;
69       job->jobid=njobs++;
70     }
71   
72   for ( a=0; a< n; a++)
73     {
74       int string=0;
75       if ( strstr(name[a], "control") && !job->control){job->control=vcalloc ( 1, sizeof (Job_control_TC));job->pl[job->np++]=(void*)job->control;}
76       else if ( strstr(name[a], "io") && !job->io){job->io=vcalloc ( 1, sizeof (Job_io_TC));job->pl[job->np++]=(void*)job->io;}
77       else if ( strstr(name[a], "param") && !job->param){job->param=vcalloc ( 1, sizeof (Job_param_TC));job->pl[job->np++]=(void*)job->param;}
78       
79       if (           strm (name[a], "control"))                {job->control=(struct Job_control_TC*)atol(value[a]);string=0;}
80       else if (      strm (name[a], "control->submitF"))       {(job->control) ->submitF=(struct Job_TC *(*)(struct Job_TC *))atol(value[a]);string=0;}
81       else if (      strm (name[a], "control->retrieveF"))     {(job->control) ->retrieveF=(struct Job_TC *(*)(struct Job_TC *))atol(value[a]);string=0;}
82       else if (      strm (name[a], "control->mode"))   {(job->control)->mode=value[a];string=1;}
83       
84       else if ( strm (name[a], "param"))            {job->param=(struct Job_param_TC*)atol(value[a]);string=0;}
85       else if ( strm (name[a], "param->method"))    {job->pl[job->np++]=((job->param)->method)=value[a];string=1;}
86       else if ( strm (name[a], "param->TCM"))       {(job->param)->TCM= (TC_method *) atol(value[a]) ;string=0;}
87       else if ( strm (name[a], "param->aln_c"))     {job->pl[job->np++]=(job->param)->aln_c=value[a] ;string=1;}
88       else if ( strm (name[a], "param->seq_c"))     {job->pl[job->np++]=(job->param)->seq_c=value[a] ;string=1;}
89                            
90       
91       else if (           strm (name[a], "io"))                {job->io=(struct Job_io_TC*)atol(value[a]);string=0;}
92       else if ( strm (name[a], "io->out")) {job->pl[job->np++]=(job->io)->out=value[a] ;string=1;}
93       else if ( strm (name[a], "io->in" )) {job->pl[job->np++]=(job->io)->in =value[a] ;string=1;}
94       else if ( strm (name[a], "io->CL"))  {(job->io)->CL=(Constraint_list*)atol (value[a]); string=0;}
95       else
96         {
97           fprintf ( stderr, "ERROR: print_lib_job2: %s is unknown [FATAL:%s]", name[a], PROGRAM);
98           myexit (EXIT_FAILURE);
99         }
100       if ( string==0) vfree ( value[a]);
101     }
102   vfree ( value);
103   free_arrayN ((void **)name, 2);
104   return job;
105 }
106
107
108
109 /*Stack Manipulation*/
110 Job_TC *queue_cat  (Job_TC *P, Job_TC *C)
111 {
112   if ( !P && !C) return NULL;
113   else if (!P || P->jobid==-1)
114     {
115       vfree (P);
116       C->p=NULL;
117       return C;
118     }
119   else
120     {
121       P->c=C;
122       if (C)C->p=P;
123       return queue2last(P);
124     }
125   return NULL;
126 }
127 Job_TC *free_queue  (Job_TC *job)
128 {
129   return NULL;
130   if (!job) return job;
131   else
132     {
133       job=queue2last(job);
134       while ( job)
135         {
136           job=free_job (job);
137         }
138       return job;
139     }
140 }
141 Job_TC *free_job  (Job_TC *job)
142   {
143     int a;
144     Job_TC *p;
145     
146     if ( !job ) return job;
147     else
148       {
149         for ( a=job->np-1; a>=0; a--)
150           vfree ( job->pl[a]);
151         p=job->p;
152         job->p=job->c=NULL;
153         vfree (job);
154         return p;
155       }
156     return NULL;
157   }
158 Job_TC * queue2heap (Job_TC*job)
159 {
160
161   while (job && job->p)
162     job=job->p;
163   return job;
164 }
165 Job_TC * queue2last (Job_TC*job)
166 {
167
168   while (job && job->c)
169     {
170       job=job->c;
171     }
172   return job;
173 }
174
175 int queue2n (Job_TC*job)
176 {
177   int n=0;
178
179
180   job=queue2last (job);
181   while (job && job->p)
182     {
183       n++;
184       job=job->p;
185
186     }
187   return n;
188 }
189
190 Job_TC * descend_queue (Job_TC*job)
191 {
192
193   if (!job ||!job->c)return job;
194   else
195     {
196       (job->c)->p=job;
197       job=job->c;
198     }
199   return job;
200 }
201
202 Job_TC* delete_job (Job_TC *job)
203 {
204   Job_TC *p, *c;
205   
206   p=job->p;
207   c=job->c;
208   free_job (job);
209   
210   return queue_cat (p, c);
211 }
212
213 Job_TC*** split_job_list (Job_TC *job, int ns)
214 {
215   int a,u,n,nj,split;
216   Job_TC*** jl;
217   Job_TC *ljob;
218   //retun a pointer to ns splits for joblist
219   
220   
221   if (ns==0)return NULL;
222   job=queue2heap(job);
223   jl=vcalloc(ns+1, sizeof (Job_TC**));
224   jl[0]=vcalloc (2, sizeof (Job_TC*));
225   
226   nj=queue2n(job);
227  
228   if   (nj==0)return NULL;
229   else split=(nj/ns)+1;
230    
231   n=a=u=0;
232   jl[a][0]=job;
233   while (job)
234     {
235       ljob=job;
236       if (n==split && a<ns)
237         {
238           jl[a][1]=job;
239           if (a<ns-1)
240             {
241               jl[a+1]=vcalloc (2, sizeof (Job_TC*));
242               jl[a+1][0]=job;
243               u++;
244             }
245           a++;
246           n=0;
247         }
248       n++;
249       job=job->c;
250     }
251
252   /*Display job details when debugging
253   a=0;
254   while (jl[a])
255     {
256       Job_TC *start,* end;
257       int todo=0;
258       
259       start=job=jl[a][0];
260       end=jl[a][1];
261       while (job!=end){todo++;job=job->c;}
262       job=start;
263       while (job!=end)
264         {
265           HERE ("--- %d %d %s",a, job, job->param->aln_c);
266           job=job->c;
267         }
268       a++;
269     }
270   
271     a=0;
272     while (jl[a]){HERE ("**** %d %d ", jl [a][0], jl[a][1]);a++;}
273     myexit (0);
274   */
275   return jl;
276 }
277
278
279
280   
281 /*Job Control*/
282 Job_TC* submit_job ( Job_TC *job)
283 {
284   
285   if (!(job->control)->mode ||!(job->control)->mode[0] || 1==1)
286     {
287       return (job->control)->submitF (job);
288     }
289   else
290     {
291       fprintf ( stderr, "\n%s is an unkown mode for posting jobs [FATAL:%s]",(job->control)->mode, PROGRAM);
292       myexit (EXIT_FAILURE);
293       return NULL;
294     }
295   
296 }
297
298 Job_TC* retrieve_job ( Job_TC *job)
299 {
300   if (!(job->control)->mode ||!(job->control)->mode[0] || 1==1)
301     {
302       return (job->control)->retrieveF (job);
303     }
304   else
305     {
306       fprintf ( stderr, "\n%s is an unkown mode for posting jobs [FATAL:%s]",(job->control)->mode, PROGRAM);
307       myexit (EXIT_FAILURE);
308       return NULL;
309     }
310 }
311 int **n2splits (int splits, int tot)
312 {
313   int **l;
314   int a,b,delta;
315   
316   if (splits==0)return NULL;
317   else if ( tot==0)return NULL;
318   else
319     {
320       
321       l=declare_int (splits,2);
322       delta=tot/splits;
323       
324       for (a=0,b=0; a<splits && b<tot; a++,b+=delta)
325         {
326           l[a][0]=b;
327           l[a][1]=MIN((b+delta),tot);
328         }
329       l[splits-1][1]=MAX((l[splits-1][1]),tot);
330       return l;
331     }
332 }
333   
334 /******************************COPYRIGHT NOTICE*******************************/
335 /*© Centro de Regulacio Genomica */
336 /*and */
337 /*Cedric Notredame */
338 /*Fri Feb 18 08:27:45 CET 2011 - Revision 596. */
339 /*All rights reserved.*/
340 /*This file is part of T-COFFEE.*/
341 /**/
342 /*    T-COFFEE is free software; you can redistribute it and/or modify*/
343 /*    it under the terms of the GNU General Public License as published by*/
344 /*    the Free Software Foundation; either version 2 of the License, or*/
345 /*    (at your option) any later version.*/
346 /**/
347 /*    T-COFFEE is distributed in the hope that it will be useful,*/
348 /*    but WITHOUT ANY WARRANTY; without even the implied warranty of*/
349 /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the*/
350 /*    GNU General Public License for more details.*/
351 /**/
352 /*    You should have received a copy of the GNU General Public License*/
353 /*    along with Foobar; if not, write to the Free Software*/
354 /*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA*/
355 /*...............................................                                                                                      |*/
356 /*  If you need some more information*/
357 /*  cedric.notredame@europe.com*/
358 /*...............................................                                                                                                                                     |*/
359 /**/
360 /**/
361 /*      */
362 /******************************COPYRIGHT NOTICE*******************************/