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