blib/lib/BioX/Workflow/Drake.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 5 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 94 CC 5 SCAR 28.7
CC
5
Cov
0%
process_template
30.0
BEGIN
2.0
BEGIN
2.0
CRAP 17.6
1 package BioX::Workflow::Drake;
2  
3 use Data::Dumper;
4 use Data::Pairs;
5  
6 use Moose;
7 extends 'BioX::Workflow';
8  
9 use Interpolation E => 'eval';
10  
11 =head1 NAME
12  
13 BioX::Workflow::Writer::Drake - A very opinionated template based bioinformatics workflow writer for Drake.
14  
15 =head1 SYNOPSIS
16  
17 The main documentation for this module is at L<BioX::Workflow>. This module extends Workflow in order to add functionality for outputing workflows in drake format.
18  
19 biox-workflow-drake.pl --workflow workflow.yml > workflow.drake
20 drake --workflow workflow.drake #with other functionality such as --jobs for asynchronous output, etc.
21  
22 More information about Drake can be found here L<https://github.com/Factual/drake>.
23  
24 =head2 Default Variables
25  
26 BioX::Workflow::Drake assumes your INPUT/OUTPUT and indir/outdirs are
27 linked.
28  
29 This means the output from step1 is the input for step2.
30  
31 You can override this behavior by either declaring any of these values, or in the global
32 variables set auto_input: 0, disable automatic indir/outdir naming with
33 auto_name: 0, and disable automatically naming outdirectories by rule names with
34 enforce_struct: 0.
35  
36  
37 =head2 Example
38  
39 =head3 workflow.yml
40  
41 ---
42 global:
43 - indir: /home/user/workflow
44 - outdir: /home/user/workflow/output
45 - file_rule: (.csv)$
46 rules:
47 - backup:
48 local:
49 - INPUT: "{$self->indir}/{$sample}.csv"
50 - OUTPUT: "{$self->outdir}/{$sample}.csv"
51 - thing: "other thing"
52 process: |
53 cp $INPUT $OUTPUT
54 - grep_VARA:
55 local:
56 - OUTPUT: "{$self->outdir}/{$sample}.grep_VARA.csv"
57 process: |
58 echo "Working on {$self->{indir}}/{$sample.csv}"
59 grep -i "VARA" {$self->indir}/{$sample}.csv >> {$self->outdir}/{$sample}.grep_VARA.csv \
60 || touch {$self->OUTPUT}
61 - grep_VARB:
62 local:
63 - OUTPUT: "{$self->outdir}/{$sample}.grep_VARA.grep_VARB.csv"
64 process: |
65 grep -i "VARB" {$self->indir}/{$sample}.grep_VARA.csv >> {$self->outdir}/{$sample}.grep_VARA.grep_VARB.csv || touch {$self->OUTPUT}
66  
67 =head3 Notes on the drake.yml
68  
69 Drake will stop everything if you're job returns with an exit code of anything
70 besides 0. For this reason we have the last command have a command1 || command2
71 syntax, so that even if we don't grep any "VARB" from the file the workflow
72 could continue.
73  
74 =head3 Run it with default setup
75  
76 biox-workflow-drake.pl --workflow workflow.yml > workflow.full.drake
77  
78 =head3 Output with default setup
79  
80 I don't want to inlcude the whole file, but you get the idea
81  
82 ;
83 ; Generated at: 2015-06-21T11:01:24
84 ; This file was generated with the following options
85 ; --workflow drake.yml
86 ; --min 1
87 ;
88  
89 ;
90 ; Samples: test1, test2
91 ;
92 ;
93 ; Starting Workflow
94 ;
95  
96 ;
97 ; Starting backup
98 ;
99  
100  
101 ;
102 ; Variables
103 ; Indir: /home/guests/jir2004/workflow
104 ; Outdir: /home/guests/jir2004/workflow/output/backup
105 ; Local Variables:
106 ; INPUT: {$self->indir}/{$sample}.csv
107 ; OUTPUT: {$self->outdir}/{$sample}.csv
108 ; thing: other thing
109 ;
110  
111 /home/guests/jir2004/workflow/output/backup/$[SAMPLE].csv <- /home/guests/jir2004/workflow/$[SAMPLE].csv
112 cp $INPUT $OUTPUT
113  
114  
115 ;
116 ; Ending backup
117 ;
118  
119  
120 ;
121 ; Starting grep_VARA
122 ;
123  
124  
125 Run drake
126  
127 drake --workflow workflow.full.drake
128  
129 The following steps will be run, in order:
130 1: /home/user/workflow/output/backup/test1.csv <- /home/user/workflow/test1.csv [timestamped]
131 2: /home/user/workflow/output/backup/test2.csv <- /home/user/workflow/test2.csv [timestamped]
132 3: /home/user/workflow/output/grep_vara/test1.grep_VARA.csv <- /home/user/workflow/output/backup/test1.csv [projected timestamped]
133 4: /home/user/workflow/output/grep_vara/test2.grep_VARA.csv <- /home/user/workflow/output/backup/test2.csv [projected timestamped]
134 5: /home/user/workflow/output/grep_varb/test1.grep_VARA.grep_VARB.csv <- /home/user/workflow/output/grep_vara/test1.grep_VARA.csv [projected timestamped]
135 6: /home/user/workflow/output/grep_varb/test2.grep_VARA.grep_VARB.csv <- /home/user/workflow/output/grep_vara/test2.grep_VARA.csv [projected timestamped]
136 Confirm? [y/n] y
137 Running 6 steps with concurrence of 1...
138  
139 --- 0. Running (timestamped): /home/user/workflow/output/backup/test1.csv <- /home/user/workflow/test1.csv
140 --- 0: /home/user/workflow/output/backup/test1.csv <- /home/user/workflow/test1.csv -> done in 0.02s
141  
142 --- 1. Running (timestamped): /home/user/workflow/output/backup/test2.csv <- /home/user/workflow/test2.csv
143 --- 1: /home/user/workflow/output/backup/test2.csv <- /home/user/workflow/test2.csv -> done in 0.01s
144  
145 --- 2. Running (timestamped): /home/user/workflow/output/grep_vara/test1.grep_VARA.csv <- /home/user/workflow/output/backup/test1.csv
146 Working on /home/user/workflow/output/backup/test1csv
147 --- 2: /home/user/workflow/output/grep_vara/test1.grep_VARA.csv <- /home/user/workflow/output/backup/test1.csv -> done in 0.01s
148  
149 --- 3. Running (timestamped): /home/user/workflow/output/grep_vara/test2.grep_VARA.csv <- /home/user/workflow/output/backup/test2.csv
150 Working on /home/user/workflow/output/backup/test2csv
151 --- 3: /home/user/workflow/output/grep_vara/test2.grep_VARA.csv <- /home/user/workflow/output/backup/test2.csv -> done in 0.01s
152  
153 --- 4. Running (timestamped): /home/user/workflow/output/grep_varb/test1.grep_VARA.grep_VARB.csv <- /home/user/workflow/output/grep_vara/test1.grep_VARA.csv
154 --- 4: /home/user/workflow/output/grep_varb/test1.grep_VARA.grep_VARB.csv <- /home/user/workflow/output/grep_vara/test1.grep_VARA.csv -> done in 0.01s
155  
156 --- 5. Running (timestamped): /home/user/workflow/output/grep_varb/test2.grep_VARA.grep_VARB.csv <- /home/user/workflow/output/grep_vara/test2.grep_VARA.csv
157 --- 5: /home/user/workflow/output/grep_varb/test2.grep_VARA.grep_VARB.csv <- /home/user/workflow/output/grep_vara/test2.grep_VARA.csv -> done in 0.08s
158 Done (6 steps run).
159  
160  
161 =head3 Run in minified mode
162  
163 As an alternative you can run this with the --min option, which instead of
164 printing out each workflow prints out only one, and creates a run-workflow.sh
165 which has all of your environmental variables.
166  
167 This option is preferable if running on an HPC cluster with many nodes.
168  
169 This WILL break with use of --resample, either local or global. You need to
170 split up your workflows as opposed to using the --resample option.
171  
172 biox-workflow-drake.pl --workflow workflow.yml --min 1 > workflow.drake #This also creates the run-workflow.sh in the same directory
173 ./run-workflow.sh
174  
175 cat drake.log #Here is the log for the first run
176  
177 2015-06-21 14:02:47,543 INFO Running 3 steps with concurrence of 1...
178 2015-06-21 14:02:47,568 INFO
179 2015-06-21 14:02:47,570 INFO --- 0. Running (timestamped): /home/user/workflow/output/backup/test1.csv <- /home/user/workflow/test1.csv
180 2015-06-21 14:02:47,592 INFO --- 0: /home/user/workflow/output/backup/test1.csv <- /home/user/workflow/test1.csv -> done in 0.02s
181  
182 #So on and so forth
183  
184 If you look in the example directory you will see a few png files, these are outputs of the drake workflow.
185  
186 =cut
187  
188 =head1 Acknowledgements
189  
190 This module was originally developed at and for Weill Cornell Medical
191 College in Qatar within ITS Advanced Computing Team. With approval from
192 WCMC-Q, this information was generalized and put on github, for which
193 the authors would like to express their gratitude.
194  
195 =head1 Inline Code Documentation
196  
197 You shouldn't need these, but if you do here they are.
198  
199 =head2 Attributes
200  
201 =cut
202  
203 =head3 full
204  
205 Print the whole workflow hardcoded. This is the default
206  
207 =cut
208  
209 has 'full' => (
210 is => 'rw',
211 isa => 'Bool',
212 default => 1,
213 );
214  
215 =head3 min
216  
217 Print the workflow as 2 files.
218  
219 Run the drake things
220  
221 drake --vars "SAMPLE=$sample" --workflow/workflow.drake
222  
223 workflow.drake
224  
225 Our regular file
226  
227 =cut
228  
229 has 'min' => (
230 is => 'rw',
231 isa => 'Bool',
232 default => 0,
233 );
234  
235 =head2 Subroutines
236  
237 Subroutines
238  
239 =head3 before run
240  
241 Must initialize some variables
242  
243 =cut
244  
245 before 'run' => sub{
246 my($self) = shift;
247  
248 if($self->min){
249 $self->full(0);
250 }
251 $self->wait(0);
252 $self->comment_char(';');
253 };
254  
255 =head3 after get_samples
256  
257 Things to do if we decide to do a min version
258  
259 =cut
260  
261 after 'get_samples' => sub{
262 my($self) = shift;
263  
264 return unless $self->min;
265  
266 open(my $fh, '>', 'run-workflow.sh') or die print "Could not open file $!\n";
267  
268 print $fh "#!/bin/bash\n\n";
269  
270 foreach my $sample (@{$self->samples}){
271 print $fh <<EOF;
272 drake --vars "SAMPLE=$sample" --workflow workflow.drake
273 EOF
274 }
275  
276 close $fh;
277  
278 chmod 0777, 'run-workflow.sh';
279  
280 $self->samples(["\$SAMPLE"]);
281 };
282  
283 =head3 write_process
284  
285 Fill in the template with the process
286  
287 =cut
288  
289 before 'write_process' => sub{
290 my($self) = shift;
291  
292 if($self->local_attr){
293 if((! $self->local_attr->get_values('INPUT')) && ! $self->local_attr->get_values('OUTPUT') ){
294 print "$self->{comment_char} There is no INPUT or OUTPUT!\n";
295 }
296 }
297  
298 my @tmp = split("\n", $self->process);
299 $self->process(join("\n\t", @tmp));
300 };
301  
302  
303 sub process_template{
304 my($self, $data) = @_;
305  
306 my($tmp, $template, $newprocess, $INPUT, $OUTPUT);
307  
308 $self->INPUT($self->local_attr->get_values('INPUT')) unless $self->INPUT;
309 $self->OUTPUT($self->local_attr->get_values('OUTPUT'));
310  
311 #Get the INPUT template
312 if($self->INPUT){
313 $INPUT = $self->INPUT;
314 $tmp = "$E{$self->INPUT}";
315 $template = $self->make_template($tmp);
316 $self->INPUT($template->fill_in(HASH => $data));
317 }
318  
319 #Get the output template
320 if($self->OUTPUT){
321 $OUTPUT = $self->OUTPUT;
322 $tmp = "$E{$self->OUTPUT}";
323 $template = $self->make_template($tmp);
324 $self->OUTPUT($template->fill_in(HASH => $data));
325 }
326  
327 #This is exactly the same as the previous except for this one statement bah
328 $tmp = "$E{$self->OUTPUT} <- $E{$self->INPUT}\n\t";
329 if($self->min){
330 $tmp =~ s/\$SAMPLE/\$[SAMPLE]/g;
331 }
332 $newprocess = $tmp.$self->process;
333 #
334  
335 $template = $self->make_template($newprocess);
336 $template->fill_in(HASH => $data, OUTPUT => \*STDOUT);
337  
338 print "\n\n";
339  
340 $self->INPUT($self->local_attr->get_values('INPUT'));
341 $self->OUTPUT($self->local_attr->get_values('OUTPUT'));
342  
343 }
344  
345 __PACKAGE__->meta->make_immutable;
346  
347  
348 1;