blib/lib/BioX/Workflow.pm

filter: Statementstmt 27.8% 84 / 302 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 53.1% 17 / 32 Podpod 53.8% 7 / 13 totaltotal 24.6% 108 / 438 CC 52 SCAR 44.6
CC
52
Cov
22%
dothings
306.0
write_pipeline
90.0
write_process
72.0
CRAP 86.1
↑ Summary blib/lib/BioX/Workflow/Drake.pm »
1 package BioX::Workflow;
2  
3 1 ▶use 5.008_005;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 our $VERSION = '0.07';
5  
6 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Find::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Path qw(make_path remove_tree);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use List::Compare;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use YAML::XS 'LoadFile';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use String::CamelCase qw(camelize decamelize wordsplit);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Class::Load ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Interpolation E => 'eval';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Text::Template qw(fill_in_file fill_in_string);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Data::Pairs;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use Carp::Always;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 extends 'BioX::Wrapper';
25 1 with 'MooseX::Getopt::Usage';
26 1 with 'MooseX::Getopt::Usage::Role::Man';
27 1 with 'MooseX::SimpleConfig';
28  
29 # For pretty man pages!
30 1 $ENV{TERM}='xterm-256color';
31  
32 =encoding utf-7
33  
34 =head1 NAME
35  
36 BioX::Workflow - A very opinionated template based workflow writer.
37  
38 =head1 SYNOPSIS
39  
40 Most of the functionality can be accessed through the biox-workflow.pl script.
41  
42 biox-workflow.pl --workflow /path/to/workflow.yml
43  
44 This module was written with Bioinformatics workflows in mind, but should be extensible to any sort of workflow or pipeline.
45  
46 =head1 Philosophy
47  
48 Most bioinformatics workflows involve starting with a set of samples, and
49 processing those samples in one or more steps. Also, most bioinformatics
50 workflows are bash based, and no one wants to reinvent the wheel to rewrite
51 their code in perl/python/whatever.
52  
53 For example with our samples test1.vcf and test2.vcf, we want to bgzip and
54 annotate using snpeff, and then parse the output using vcf-to-table.pl
55 (shameless plug for BioX::Wrapper::Annovar).
56  
57 BioX::Workflow assumes your have a set of inputs, known as samples,
58 and these inputs will carry on through your pipeline. There are some exceptions
59 to this, which we will explore with the resample option.
60  
61 It also makes several assumtions about your output structure. It assumes you
62 have each of your processes/rules outputting to a distinct directory.
63  
64 These directories will be created and automatically named based on your process
65 name. You can disable this and make your own out directories by either
66 specifiying auto_name: 1 in your global, in any of the local rules to disable
67 it for that rule, or by specifying an outdirectory.
68  
69 =head1 A Simple Example
70  
71 Here is a very simple example that searches a directory for *.csv files and creates an outdir /home/user/workflow/output if one doesn't exist.
72  
73 Create the /home/user/workflow/workflow.yml
74  
75 ---
76 global:
77 - indir: /home/guests/jir2004/workflow
78 - outdir: /home/guests/jir2004/workflow/output
79 - file_rule: (.csv)$
80 rules:
81 - backup:
82 process: cp {$self->indir}/{$sample}.csv {$self->outdir}/{$sample}.csv
83 - grep_VARA:
84 process: |
85 echo "Working on {$self->{indir}}/{$sample.csv}"
86 grep -i "VARA" {$self->indir}/{$sample}.csv >> {$self->outdir}/{$sample}.grep_VARA.csv
87 - grep_VARB:
88 process: |
89 grep -i "VARB" {$self->indir}/{$sample}.grep_VARA.csv >> {$self->outdir}/{$sample}.grep_VARA.grep_VARB.csv
90  
91 If we step through the whole process
92  
93 cd /home/user/workflow
94  
95 #Create test1.csv with some lines
96 echo "This is VARA" >> test1.csv
97 echo "This is VARB" >> test1.csv
98 echo "This is VARC" >> test1.csv
99  
100 #Create test2.csv with some lines
101 echo "This is VARA" >> test2.csv
102 echo "This is VARB" >> test2.csv
103 echo "This is VARC" >> test2.csv
104 echo "This is some data I don't want" >> test2.csv
105  
106 Run the script to create out directory structure and workflow bash script
107  
108 biox-workflow.pl --workflow workflow.yml > workflow.sh
109  
110 =head2 Look at the directory structure
111  
112 /home/user/workflow/
113 test1.csv
114 test2.csv
115 /output
116 /backup
117 /grep_vara
118 /grep_varb
119  
120 =head2 Run the workflow
121  
122 Assuming you saved your output to workflow.sh if you run ./workflow.sh you will get the following.
123  
124 /home/user/workflow/
125 test1.csv
126 test2.csv
127 /output
128 /backup
129 test1.csv
130 test2.csv
131 /grep_vara
132 test1.grep_VARA.csv
133 test2.grep_VARA.csv
134 /grep_varb
135 test1.grep_VARA.grep_VARB.csv
136 test2.grep_VARA.grep_VARB.csv
137  
138  
139 =head2 A closer look at workflow.sh
140  
141 This top part here is the metadata. It tells you the options used to run the script.
142  
143 #
144 # This file was generated with the following options
145 # --workflow config.yml
146 #
147  
148 If --verbose is enabled, and it is by default, you'll see some variables printed out for your benefit
149  
150 #
151 # Variables
152 # Indir: /home/user/workflow
153 # Outdir: /home/user/workflow/output/backup
154 # Samples: test1 test2
155 #
156  
157 Here is out first rule, named backup. As you can see our $self->outdir is automatically named 'backup', relative to the globally defined outdir.
158  
159 #
160 # Starting backup
161 #
162  
163 cp /home/user/workflow/test1.csv /home/user/workflow/output/backup/test1.csv
164 cp /home/user/workflow/test2.csv /home/user/workflow/output/backup/test2.csv
165  
166 wait
167  
168 #
169 # Ending backup
170 #
171  
172 Notice the 'wait' command. If running your outputted workflow through any of the HPC::Runner scripts, the wait signals to wait until all previous processes have ended before beginning the next one.
173  
174 For instance, if running this as
175  
176 slurmrunner.pl --infile workflow.sh
177 #OR
178 mcerunner.pl --infile workflow.sh
179  
180 The "cp blahblahblah" commands would run in parallel, and the next rule would not begin until those processes have finished.
181  
182 Here is some verbose output for the next rule.
183  
184 #
185 # Variables
186 # Indir: /home/guests/jir2004/workflow/output
187 # Outdir: /home/guests/jir2004/workflow/output/grep_vara
188 # Samples: test1 test2
189 #
190  
191 And here is the actual work.
192  
193 #
194 # Starting grep_VARA
195 #
196  
197 echo "Working on $self->indir/test1csv"
198 grep -i "VARA" /home/guests/jir2004/workflow/output/test1.csv >> /home/guests/jir2004/workflow/output/grep_vara/test1.grep_VARA.csv
199  
200 echo "Working on $self->indir/test2csv"
201 grep -i "VARA" /home/guests/jir2004/workflow/output/test2.csv >> /home/guests/jir2004/workflow/output/grep_vara/test2.grep_VARA.csv
202  
203 wait
204  
205 #
206 # Ending grep_VARA
207 #
208  
209 So on and so forth.
210  
211 #
212 # Variables
213 # Indir: /home/guests/jir2004/workflow/output
214 # Outdir: /home/guests/jir2004/workflow/output/grep_varb
215 # Samples: test1 test2
216 #
217  
218  
219 #
220 # Starting grep_VARB
221 #
222  
223 grep -i "VARB" /home/guests/jir2004/workflow/output/test1.csv >> /home/guests/jir2004/workflow/output/grep_varb/test1.grep_VARB.csv
224  
225 grep -i "VARB" /home/guests/jir2004/workflow/output/test2.csv >> /home/guests/jir2004/workflow/output/grep_varb/test2.grep_VARB.csv
226  
227 wait
228  
229 #
230 # Ending grep_VARB
231 #
232  
233 #
234 # Workflow Finished
235 #
236  
237 =head1 Customizing your output and special variables
238  
239 BioX::Workflow uses a few conventions and special variables. As you
240 probably noticed these are indir, outdir, infiles, and file_rule. In addition
241 sample is the currently scoped sample. Infiles is not used by default, but is
242 simply a store of all the original samples found when the script is first run,
243 before any processes. In the above example the $self->infiles would evaluate as
244 ['test1.csv', 'test2.csv'].
245  
246 Variables are interpolated using L<Interpolation> and L<Text::Template>. All
247 variables, unless explictly defined with "$my variable = "stuff"" in your
248 process key, must be referenced with $self, and surrounded with brackets {}.
249 Instead of $self->outdir, it should be {$self->outdir}. It is also possible to
250 define variables with other variables in this way. Everything is referenced
251 with $self in order to dynamically pass variables to Text::Template. The sample
252 variable, $sample, is the exception because it is defined in the loop. In
253 addition you can create an OUTPUT/OUTPUT variables to clean up your process
254 code.
255  
256 ---
257 global:
258 - ROOT: /home/user/workflow
259 - indir: {$self->ROOT}
260 - outdir: {$self->indir}/output
261 rules:
262 - backup:
263 local:
264 - OUTPUT: {$self->indir}/{$sample}.in
265 - OUTPUT: {$self->outdir}/{$sample}.out
266  
267 Your variables must be defined in an appropriate order.
268  
269 =head2 Local and Global Variables
270  
271 Global variables will always be available, but can be overwritten by local
272 variables contained in your rules.
273  
274 ---
275 global:
276 - indir: /home/user/example-workflow
277 - outdir: /home/user/example-workflow/gemini-wrapper
278 - file_rule: (.vcf)$|(.vcf.gz)$
279 - some_variable: {$self->indir}/file_to_keep_handy
280 rules:
281 - backup:
282 local:
283 - ext: "backup"
284 process: cp {$self->indir}/{$sample}.csv {$self->outdir}/{$sample}.{$self->ext}.csv
285  
286 =head2 Rules
287  
288 Rules are processed in the order they appear.
289  
290 Before any rules are processed, first the samples are found. These are grepped using File::Basename, the indir, and the file_rule variable. The
291 default is to get rid of the everything after the final '.' .
292  
293 =head2 Overriding Processes
294  
295 By default your process is evaluated as
296  
297 foreach my $sample (@{$self->samples}){
298 #Get the value from the process key.
299 }
300  
301 If instead you would like to use the infiles, or some other random process that has nothing to do with your samples, you can override the process
302 template. Make sure to use the previously defined $OUT. For more information see the L<Text::Template> man page.
303  
304 rules:
305 - backup:
306 outdir: {$self->ROOT}/datafiles
307 override: 1
308 process: |
309 $OUT .= wget {$self->some_globally_defined_parameter}
310 {
311 foreach my $infile (@{$self->infiles}){
312 $OUT .= "dostuff $infile";
313 }
314 }
315  
316  
317  
318 =head2 Directory Structure
319  
320 BioX::Workflow will create a directory structure based on your rule name, decamelized, and your globally defined outdir.
321  
322 /path/to/outdir
323 /rule1
324 /rule2
325 /rule3
326  
327 If you don't like this you can globally disable auto_name (auto_name: 0), or simply defined indir or outdir within your global variables. If using the
328 second method it is probably a good idea to also defined a ROOT_DIR in your global variables.
329  
330 =head2 Other variables
331  
332 A quick overview of other samples
333  
334 =head3 Resampling
335  
336 I can only think of a few examples where one would want to perform a resample, since the files probably won't exist, but one example is if you want to
337 check for uncompressed files, compress them, and then carry on with your life.
338  
339 ---
340 global:
341 - indir: /home/user/gemini
342 - outdir: /home/user/gemini/gemini-wrapper
343 - file_rule: (.vcf)$|(.vcf.gz)$
344 - infile:
345 rules:
346 - bgzip:
347 local:
348 - file_rule: (.vcf)$
349 - resample: 1
350 before_meta: bgzip and tabix index uncompressed files
351 after_meta: finished bgzip
352 process: bgzip {$self->{indir}}/{$sample}.vcf && tabix {$self->{indir}}/{$sample}.vcf.gz
353 - normalize_snpeff:
354 local:
355 - indir: /home/user
356 - file_rule: (.vcf.gz)$
357 - resample: 1
358 process: |
359 bcftools view {$self->indir}/{$sample}.vcf.gz | sed 's/ID=AD,Number=./ID=AD,Number=R/' \
360 | vt decompose -s - \
361 | vt normalize -r $REFGENOME - \
362 | java -Xmx4G -jar $SNPEFF/snpEff.jar -c \$SNPEFF/snpEff.config -formatEff -classic GRCh37.75 \
363 | bgzip -c > \
364 {$self->{outdir}}/{$sample}.norm.snpeff.gz && tabix {$self->{outdir}}/{$sample}.norm.snpeff.gz
365  
366 The bgzip rule would first run a resample looking for only files ending in .vcf, and compress them. The following rule, normalize_snpeff, looks again
367 in the indir (which we set here otherwise it would have been the previous rules outdir), and resamples based on the .vcf.gz extension.
368  
369 =head1 In Code Documenation
370  
371 You shouldn't really need to look here unless you have some reason to do some serious hacking.
372  
373 =head2 Attributes
374  
375 Moose attributes. Technically any of these can be changed, but may break everything.
376  
377 =head3 resample
378  
379 Boolean value get new samples based on indir/file_rule or no
380  
381 Samples are found at the beginning of the workflow, based on the global indir variable and the file_find.
382  
383 Chances are you don't want to set resample to try, because these files probably won't exist outside of the indirectory until the pipeline is run.
384  
385 One example of doing so, shown in the gemini.yml in the examples directory, is looking for uncompressed files, .vcf extension, compressing them, and
386 then resampling based on the .vcf.gz extension.
387  
388 =cut
389  
390 1 has 'resample' => (
391 is => 'rw',
392 isa => 'Bool',
393 default => 0,
394 predicate => 'has_resample',
395 clearer => 'clear_resample',
396 );
397  
398 =head2 find_by_dir
399  
400 Use this option when you sample names are by directory
401 The default is to find samples by filename
402  
403 /SAMPLE1
404 SAMPLE1_r1.fastq.gz
405 SAMPLE1_r2.fastq.gz
406 /SAMPLE2
407 SAMPLE2_r1.fastq.gz
408 SAMPLE2_r2.fastq.gz
409  
410 =cut
411  
412 1 has 'find_by_dir' => (
413 is => 'rw',
414 isa => 'Bool',
415 default => 0,
416 documentation => q{Use this option when you sample names are directories},
417 );
418  
419 =head2 by_sample_outdir
420  
421 outdir/
422 /outdir/SAMPLE1
423 /rule1
424 /rule2
425 /rule3
426 /outdir/SAMPLE2
427 /rule1
428 /rule2
429 /rule3
430  
431 Instead of
432  
433 /outdir
434 /rule1
435 /rule2
436  
437 This feature is not particularly well supported, and may break when mixed with other methods, particularly --resample
438  
439 =cut
440  
441 1 has 'by_sample_outdir' => (
442 is => 'rw',
443 isa => 'Bool',
444 default => 0,
445 documentation => q{When you want your output by sample},
446 );
447  
448 =head3 auto_name
449  
450 Auto_name - Create outdirectory based on rulename
451  
452 global:
453 - outdir: /home/user/workflow/processed
454 rule:
455 normalize:
456 process:
457 dostuff {$self->indir}/{$sample}.in >> {$self->outdir}/$sample.out
458  
459 Would create your directory structure /home/user/workflow/processed/normalize (if it doesn't exist)
460  
461 =cut
462  
463 1 has 'auto_name' => (
464 is => 'rw',
465 isa => 'Bool',
466 default => 1,
467 );
468  
469 =head3 auto_input
470  
471 This is similar to the auto_name function in the BioX::Workflow.
472 Instead this says each input should be the previous output.
473  
474 =cut
475  
476 1 has 'auto_input' => (
477 is => 'rw',
478 isa => 'Bool',
479 default => 1,
480 );
481  
482 =head3 enforce_struct
483  
484 Enforce a particular workflow where the outdirectory (outdir) from the previous rule is the indirectory for the current
485  
486 =cut
487  
488 1 has 'enforce_struct' => (
489 is => 'rw',
490 isa => 'Bool',
491 default => 1,
492 );
493  
494 =head3 verbose
495  
496 Output some more things
497  
498 =cut
499  
500 1 has 'verbose' => (
501 is => 'rw',
502 isa => 'Bool',
503 default => 1,
504 );
505  
506 =head3 wait
507  
508 Print "wait" at the end of each rule
509  
510 =cut
511  
512 1 has 'wait' => (
513 is => 'rw',
514 isa => 'Bool',
515 default => 1,
516 );
517  
518  
519 =head3 override_process
520  
521 local:
522 - override: 1
523  
524 =cut
525  
526 1 has 'override_process' => (
527 is => 'rw',
528 isa => 'Bool',
529 default => 0,
530 );
531  
532 =head3 indir outdir
533  
534 =cut
535  
536 has ['indir', 'outdir'] => (
537 is => 'rw',
538 isa => 'Str',
539 0 ▶ default => sub {getcwd();},
Subroutine __ANON__: not called CC=1 SCAR=6.9
540 1 );
541  
542 =head3 Input Output
543  
544 Special variables that can have input/output
545  
546 =cut
547  
548 1 has 'OUTPUT' =>(
549 is => 'rw',
550 isa => 'Str|Undef',
551 predicate => 'has_OUTPUT',
552 clearer => 'clear_OUTPUT',
553 );
554  
555 1 has 'OUTPUT' =>(
556 is => 'rw',
557 isa => 'Str|Undef',
558 predicate => 'has_OUTPUT',
559 clearer => 'clear_OUTPUT',
560 );
561  
562 =head3 file_rule
563  
564 Rule to find files
565  
566 =cut
567  
568 has 'file_rule' =>(
569 is => 'rw',
570 isa => 'Str',
571 0 ▶ default => sub { return "\\.[^.]*"; }
Subroutine __ANON__: not called CC=1 SCAR=6.9
572 1 );
573  
574  
575 1 has 'yaml' => (
576 is => 'rw',
577 );
578  
579 =head3 attr
580  
581 attributes read in from runtime
582  
583 =cut
584  
585 1 has 'attr' => (
586 is => 'rw',
587 isa => 'Data::Pairs',
588 );
589  
590 =head3 global_attr
591  
592 Attributes defined in the global section of the yaml file
593  
594 =cut
595  
596 1 has 'global_attr' => (
597 is => 'rw',
598 isa => 'Data::Pairs',
599 );
600  
601 =head3 local_attr
602  
603 Attributes defined in the rules->rulename->local section of the yaml file
604  
605 =cut
606  
607 1 has 'local_attr' => (
608 is => 'rw',
609 isa => 'Data::Pairs',
610 );
611  
612 =head3 local_rule
613  
614 =cut
615  
616 1 has 'local_rule' => (
617 is => 'rw',
618 isa => 'HashRef'
619 );
620  
621 =head3 infiles
622  
623 Infiles to be processed
624  
625 =cut
626  
627 1 has 'infiles' => (
628 is => 'rw',
629 isa => 'ArrayRef',
630 );
631  
632 =head3 samples
633  
634 =cut
635  
636 1 has 'samples' => (
637 is => 'rw',
638 isa => 'ArrayRef',
639 );
640  
641 =head3 process
642  
643 Do stuff
644  
645 =cut
646  
647 1 has 'process' => (
648 is => 'rw',
649 isa => 'Str',
650 );
651  
652 =head3 key
653  
654 Do stuff
655  
656 =cut
657  
658 1 has 'key' => (
659 is => 'rw',
660 isa => 'Str',
661 );
662  
663 =head3 workflow
664  
665 Path to workflow workflow. This must be a YAML file.
666  
667 =cut
668  
669 1 has 'workflow' => (
670 is => 'rw',
671 isa => 'Str',
672 required => 1,
673 );
674  
675 =head3 rule_based
676  
677 This is the default. The outer loop are the rules, not the samples
678  
679 =cut
680  
681 1 has 'rule_based' => (
682 is => 'rw',
683 isa => 'Bool',
684 default => 1,
685 );
686  
687 =head3 sample_based
688  
689 Default Value. The outer loop is samples, not rules. Must be set in your global values or on the command line --sample_based 1
690  
691 If you ever have resample: 1 in your config you should NOT set this value to true!
692  
693 =cut
694  
695 1 has 'sample_based' => (
696 is => 'rw',
697 isa => 'Bool',
698 default => 0,
699 );
700  
701  
702 =head2 Subroutines
703  
704 Subroutines can also be overriden and/or extended in the usual Moose fashion.
705  
706 =head3 run
707  
708 Starting point.
709  
710 =cut
711  
712 sub run {
713 0 ▶ my($self) = shift;
Subroutine run: not called CC=3 SCAR=24.8
Pod documented
714  
715 0 print "#!/bin/bash\n\n";
716  
717 0 $self->print_opts;
718  
719 0 my $array = LoadFile($self->workflow);
720  
721 0 $self->yaml($array);
722  
723 0 $self->class_load;
724  
725 0 $self->global_attr(Data::Pairs->new($array->{global}));
726  
727 0 $self->attr($self->global_attr);
728  
729 0 $self->create_attr;
730 0 $self->eval_attr;
731  
732 0 $self->make_outdir;
733  
734 0 $self->get_samples;
735  
736 0 ▶ if($self->verbose){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose) 0 0 0
737 0 print "$self->{comment_char}\n";
738 0 print "$self->{comment_char} Starting Workflow\n";
739 0 print "$self->{comment_char}\n";
740 }
741  
742 0 $self->write_pipeline;
743  
744 0 ▶ if($self->verbose){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose) 0 0 0
745 0 print "$self->{comment_char}\n";
746 0 print "$self->{comment_char} Ending Workflow\n";
747 0 print "$self->{comment_char}\n";
748 }
749 }
750  
751 =head3 make_outdir
752  
753 Set initial indir and outdir
754  
755 =cut
756  
757 sub make_outdir {
758 0 ▶ my($self) = @_;
Subroutine make_outdir: not called CC=2 SCAR=17.9
Pod documented
759  
760 0 ▶ make_path($self->outdir) if ! -d $self->outdir;
Branch: 1 branch
BranchTrueFalseTotal
if not -d $self->outdir 0 0 0
761 }
762  
763 =head3 get_samples
764  
765 Get basename of the files. Can add optional rules.
766  
767 sample.vcf.gz and sample.vcf would be sample if the file_rule is (.vcf)$|(.vcf.gz)$
768  
769 Also gets the full path to infiles
770  
771 Instead of doing
772  
773 foreach my $sample (@$self->samples){
774 dostuff
775 }
776  
777 Could have
778  
779 foreach my $infile (@$self->infiles){
780 dostuff
781 }
782  
783 =cut
784  
785 sub get_samples{
786 0 ▶ my($self) = shift;
Subroutine get_samples: not called CC=3 SCAR=24.8
Pod documented
787 0 my(@whole, @basename, $text);
788  
789 0 $text = $self->file_rule;
790  
791 0 ▶ if($self->find_by_dir){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->find_by_dir) { } 0 0 0
792 0 @whole = find(directory => name => qr/$text/, maxdepth => 1, in => $self->indir);
793 0 @basename = map { basename($_) } @whole ;
794 }
795 else{
796 0 @whole = find(file => name => qr/$text/, maxdepth => 1, in => $self->indir);
797 0 @basename = map { my @tmp = fileparse($_, qr/$text/); $tmp[0] } @whole ;
798 }
799  
800 0 $self->samples(\@basename);
801 0 $self->infiles(\@whole);
802  
803 0 ▶ if($self->verbose){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose) 0 0 0
804 0 print "$self->{comment_char}\n";
805 0 print "$self->{comment_char} Samples: ",join(", ", @{$self->samples})."\n";
806 0 print "$self->{comment_char}\n";
807 }
808 }
809  
810 =head3 load
811  
812 Load classes defined in yaml with Class::Load
813  
814 =cut
815  
816 sub class_load {
817 0 ▶ my($self) = shift;
Subroutine class_load: not called CC=3 SCAR=24.8
Pod undocumented
818  
819 0 ▶ return unless $self->yaml->{use};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->yaml->{"use"} 0 0 0
820  
821 0 my $modules = $self->yaml->{use};
822  
823 0 foreach my $m (@$modules){
824 0 load_class($m);
825 }
826 }
827  
828 =head3 make_template
829  
830 Make the template for interpolating strings
831  
832 =cut
833  
834 sub make_template{
835 0 ▶ my($self, $input) = @_;
Subroutine make_template: not called CC=1 SCAR=6.9
Pod documented
836  
837 0 my $template = Text::Template->new( TYPE => 'STRING',
838 SOURCE => "$E{$input}",
839 );
840  
841 0 return $template;
842 }
843  
844 =head3 create_attr
845  
846 make attributes
847  
848 =cut
849  
850 sub create_attr{
851 0 ▶ my($self) = shift;
Subroutine create_attr: not called CC=5 SCAR=34.0
Pod documented
852  
853 0 my $meta = __PACKAGE__->meta;
854  
855 0 $meta->make_mutable;
856  
857 0 my %seen = ();
858  
859 0 for my $attr ( $meta->get_all_attributes ) {
860 0 $seen{$attr->name} = 1;
861 }
862  
863 # Data Pairs is so much prettier
864 0 my @keys = $self->attr->get_keys();
865  
866 0 foreach my $k (@keys){
867 0 my($v) = $self->attr->get_values($k);
868  
869 0 ▶ if(! exists $seen{$k}){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $seen{$k}) 0 0 0
870 0 $meta->add_attribute($k => (is => 'rw', predicate => "has_$k", clearer => "clear_$k"));
871 }
872 0 ▶ $self->$k($v) if $v;
Branch: 1 branch
BranchTrueFalseTotal
if $v 0 0 0
873 }
874  
875 0 $meta->make_immutable;
876 }
877  
878 sub eval_attr {
879 0 ▶ my $self = shift;
Subroutine eval_attr: not called CC=5 SCAR=34.0
Pod undocumented
880 0 my $sample = shift;
881  
882 0 $DB::single=2;
883 0 my @keys = $self->attr->get_keys();
884  
885 0 foreach my $k (@keys){
886 0 my($v) = $self->attr->get_values($k);
887  
888 0 ▶ next unless $v;
Branch: 1 branch
BranchTrueFalseTotal
unless $v 0 0 0
889  
890 0 my $template = $self->make_template($v);
891 0 my $text;
892 0 ▶ if($sample){
Branch: 1 branch
BranchTrueFalseTotal
if ($sample) { } 0 0 0
893 0 $text = $template->fill_in(HASH => {self => \$self, sample => $sample});
894 }
895 else{
896 0 $text = $template->fill_in(HASH => {self => \$self});
897 }
898  
899 0 $self->$k($text);
900 }
901  
902 0 ▶ $self->make_outdir if $self->attr->exists('OUTPUT')
Branch: 1 branch
BranchTrueFalseTotal
if $self->attr->exists("OUTPUT") 0 0 0
903 }
904  
905 sub clear_attr {
906 0 ▶ my $self = shift;
Subroutine clear_attr: not called CC=3 SCAR=24.8
Pod undocumented
907  
908 0 my @keys = $self->attr->get_keys();
909  
910 0 foreach my $k (@keys){
911 0 my($v) = $self->attr->get_values($k);
912 0 ▶ next unless $v;
Branch: 1 branch
BranchTrueFalseTotal
unless $v 0 0 0
913  
914 0 my $clear = "clear_$k";
915 0 $self->$clear;
916 }
917 }
918  
919 sub write_pipeline{
920 0 ▶ my($self) = shift;
Subroutine write_pipeline: not called CC=9 SCAR=45.0
Pod undocumented
921  
922 0 my $process = $self->yaml->{rules};
923  
924 0 ▶ die print "Where are the rules?\n" unless $process;
Branch: 1 branch
BranchTrueFalseTotal
unless $process 0 0 0
925  
926 # This is untested with resampling!
927 0 ▶ if($self->sample_based){
Branch: 2 branches
BranchTrueFalseTotal
if ($self->sample_based) { } 0 0 0
elsif ($self->rule_based) { } 0 0 0
928 #Store the samples
929 0 my $sample_store = $self->samples;
930  
931 0 foreach my $sample (@$sample_store){
932 0 $self->samples([$sample]);
933 0 foreach my $p (@{$process}){
934 0 ▶ next unless $p;
Branch: 1 branch
BranchTrueFalseTotal
unless $p 0 0 0
935 0 $self->local_rule($p);
936 0 $self->dothings;
937 }
938 }
939 }
940 elsif($self->rule_based){
941 0 foreach my $p (@{$process}){
942 0 ▶ next unless $p;
Branch: 1 branch
BranchTrueFalseTotal
unless $p 0 0 0
943 0 $self->local_rule($p);
944 0 $self->dothings;
945 }
946 }
947 else{
948 0 die print "Workflow must be rule based or sample based!\n";
949 }
950 }
951  
952 sub dothings {
953 0 ▶ my($self) = shift;
Subroutine dothings: not called CC=17 SCAR=57.2
Pod undocumented
954  
955  
956 #$DB::single=2;
957 0 my(@keys, $pairs,$camel_key, $key, $process_outdir);
958  
959 0 $self->local_attr(Data::Pairs->new([]));
960  
961 0 @keys = keys %{$self->local_rule};
962  
963 #TODO make these more informative messages
964 0 ▶ return unless @keys;
Branch: 1 branch
BranchTrueFalseTotal
unless @keys 0 0 0
965  
966 0 ▶ if($#keys > 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($#keys > 0) 0 0 0
967 0 die print "We have a problem! There should only be one key. Please see the documentation!\n";
968 }
969  
970 0 $key = $keys[0];
971 0 $self->key($key);
972 #$camel_key = decamelize($key);
973 0 $camel_key= $key;
974  
975 0 ▶ if($self->auto_name){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->auto_name) 0 0 0
976 0 $self->outdir($self->outdir."/$camel_key");
977 0 $process_outdir = $self->outdir;
978 0 ▶ $self->make_outdir() unless $self->by_sample_outdir;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->by_sample_outdir 0 0 0
979 }
980  
981 0 ▶ if (exists $self->local_rule->{$key}->{override} && $self->local_rule->{$key}->{override} == 1){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->local_rule->{$key}{"override"} and $self->local_rule->{$key}{"override"} == 1) 0 0 0
Condition0%0/3
exists $self->local_rule->{$key}{"override"} and $self->local_rule->{$key}{"override"} == 1
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->local_rule->{$key}{"override"} and $self->local_rule->{$key}{"override"} == 1
exists $self->local_rule->{$key}{"override"} $self->local_rule->{$key}{"override"} == 1
Truth table0%0/3
A and B
A= exists $self->local_rule->{$key}{"override"}B= $self->local_rule->{$key}{"override"} == 1
ABresult
0X0
100
111
982 0 $self->override_process(1);
983 }
984  
985 0 ▶ if(exists $self->local_rule->{$key}->{local}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->local_rule->{$key}{"local"}) 0 0 0
986 0 $self->local_attr(Data::Pairs->new($self->local_rule->{$key}->{local}));
987 0 $self->attr($self->local_attr);
988 0 $self->create_attr;
989 }
990  
991 0 ▶ if(! exists $self->local_rule->{$key}->{process}){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->local_rule->{$key}{"process"}) 0 0 0
992 0 die print "There is no process key! Dying...\n";
993 }
994  
995 0 $self->process($self->local_rule->{$key}->{process});
996  
997 0 $self->write_rule_meta('before_meta');
998  
999 0 ▶ if($self->resample){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->resample) 0 0 0
1000 0 $self->get_samples;
1001 }
1002  
1003 0 ▶ if($self->verbose){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose) 0 0 0
1004 0 print "\n\n$self->{comment_char}\n";
1005 0 print "$self->{comment_char} Variables \n";
1006 0 print "$self->{comment_char} Indir: ".$self->indir."\n";
1007 0 print "$self->{comment_char} Outdir: ".$self->outdir."\n";
1008  
1009 0 ▶ if(exists $self->local_rule->{$key}->{local}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->local_rule->{$key}{"local"}) 0 0 0
1010  
1011 0 print "$self->{comment_char} Local Variables:\n";
1012  
1013 0 ▶ if($self->auto_input && $self->has_OUTPUT){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->auto_input and $self->has_OUTPUT) 0 0 0
Condition0%0/3
$self->auto_input and $self->has_OUTPUT
!ll&&!rl&&r
000
MC/DC0%0/2
$self->auto_input and $self->has_OUTPUT
$self->auto_input $self->has_OUTPUT
Truth table0%0/3
A and B
A= $self->auto_inputB= $self->has_OUTPUT
ABresult
0X0
100
111
1014 0 $self->local_attr->set('OUTPUT' => $self->OUTPUT);
1015 }
1016  
1017 0 my @keys = $self->local_attr->get_keys();
1018  
1019 0 foreach my $k (@keys){
1020 0 my($v) = $self->local_attr->get_values($k);
1021 0 print "$self->{comment_char}\t$k: ".$v."\n";
1022 }
1023 }
1024  
1025 0 ▶ if($self->resample){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->resample) 0 0 0
1026 0 print "$self->{comment_char} Resampling Samples: ",join(", ", @{$self->samples})."\n";
1027 }
1028 0 print "$self->{comment_char}\n\n";
1029 }
1030  
1031 0 $self->write_process();
1032  
1033 #Write after meta
1034 0 $self->write_rule_meta('after_meta');
1035  
1036 #Set bools back to false and reinitialize global vars
1037 0 $self->resample(0);
1038 0 $self->clear_attr;
1039 0 $self->attr($self->global_attr);
1040 0 $self->eval_attr;
1041 0 $self->local_attr(Data::Pairs->new([]));
1042  
1043 0 ▶ if($self->enforce_struct){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->enforce_struct) 0 0 0
1044 0 $self->indir($process_outdir);
1045 }
1046 }
1047  
1048 =head2 write_rule_meta
1049  
1050 =cut
1051  
1052 sub write_rule_meta{
1053 0 ▶ my($self, $meta) = @_;
Subroutine write_rule_meta: not called CC=4 SCAR=30.0
Pod documented
1054  
1055 0 ▶ if(exists $self->local_rule->{$self->{key}}->{$meta}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->local_rule->{$self->{'key'}}{$meta}) { } 0 0 0
1056 0 $DB::single=2;
1057 0 print "\n$self->{comment_char}\n";
1058 0 print "$self->{comment_char} ".$self->local_rule->{$self->key}->{after_meta}."\n";
1059 0 print "$self->{comment_char}\n\n";
1060 }
1061 else{
1062 0 print "\n$self->{comment_char}\n";
1063 0 ▶ if($meta eq "before_meta"){
Branch: 2 branches
BranchTrueFalseTotal
if ($meta eq 'before_meta') { } 0 0 0
elsif ($meta eq 'after_meta') { } 0 0 0
1064 0 print "$self->{comment_char} Starting $self->{key}\n";
1065 }
1066 elsif($meta eq "after_meta"){
1067 0 print "$self->{comment_char} Ending $self->{key}\n";
1068 }
1069 0 print "$self->{comment_char}\n\n";
1070 }
1071  
1072  
1073 }
1074  
1075 =head3 write_process
1076  
1077 Fill in the template with the process
1078  
1079 =cut
1080  
1081 1 has 'pkey' => (
1082 is => 'rw',
1083 isa => 'Str|Undef',
1084 predicate => 'has_pkey'
1085 );
1086  
1087 sub write_process{
1088 0 ▶ my($self) = @_;
Subroutine write_process: not called CC=8 SCAR=42.8
Pod documented
1089  
1090 0 my($template, $tmp, $newprocess, $sample, $origout, $origin);
1091  
1092 0 $origout = $self->outdir;
1093 0 $origin = $self->indir;
1094  
1095 0 $DB::single = 2;
1096  
1097 0 ▶ if(!$self->override_process){
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->override_process) { } 0 0 0
1098 0 foreach my $sample (@{$self->samples}){
1099 0 ▶ if($self->by_sample_outdir){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->by_sample_outdir) 0 0 0
1100 0 my($tt, $key);
1101 0 $tt = $self->outdir;
1102 0 $key = $self->key;
1103 0 $tt =~ s/$key/$sample\/$key/;
1104 0 $self->outdir($tt);
1105 0 $self->make_outdir;
1106 0 $DB::single=2;
1107  
1108 0 ▶ if($self->has_pkey){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_pkey) { } 0 0 0
1109 0 $tt = $self->indir;
1110 0 $key = $self->key;
1111 0 $tt =~ s/$key/$sample\/$key/;
1112 0 $self->indir($tt);
1113 }
1114 else{
1115 0 $tt = $self->indir;
1116 0 $tt = "$tt/$sample";
1117 0 $self->indir($tt);
1118 }
1119 }
1120 0 $self->eval_attr($sample);
1121 0 my $data = {self => \$self, sample => $sample};
1122 0 $DB::single=2;
1123 0 $self->process_template($data);
1124 0 $self->outdir($origout);
1125 0 $self->indir($origin);
1126 }
1127 }
1128 else{
1129 0 $self->eval_attr;
1130 0 my $data = {self => \$self};
1131 0 $self->process_template($data);
1132 }
1133  
1134 0 ▶ if($self->wait){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->wait) 0 0 0
1135 0 print "\nwait\n";
1136 }
1137  
1138  
1139 0 $DB::single=2;
1140 0 ▶ if($self->auto_input && $self->local_attr->exists('OUTPUT')){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->auto_input and $self->local_attr->exists('OUTPUT')) { } 0 0 0
Condition0%0/3
$self->auto_input and $self->local_attr->exists("OUTPUT")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->auto_input and $self->local_attr->exists("OUTPUT")
$self->auto_input $self->local_attr->exists("OUTPUT")
Truth table0%0/3
A and B
A= $self->auto_inputB= $self->local_attr->exists("OUTPUT")
ABresult
0X0
100
111
1141 0 $DB::single=2;
1142 0 my($tmp, $indir, $outdir) = ($self->local_attr->get_values('OUTPUT'), $self->indir, $self->outdir);
1143 0 $tmp =~ s/$outdir/$indir/;
1144 0 $self->OUTPUT($tmp);
1145 0 $DB::single=2;
1146 }
1147 else{
1148 0 $self->OUTPUT('');
1149 }
1150 0 $self->OUTPUT('');
1151  
1152 0 $self->pkey($self->key);
1153 }
1154  
1155 sub process_template{
1156 0 ▶ my($self, $data) = @_;
Subroutine process_template: not called CC=1 SCAR=6.9
Pod undocumented
1157  
1158 0 my($tmp, $template);
1159  
1160 0 $template = $self->make_template($self->process);
1161 0 $DB::single=2;
1162 0 $template->fill_in(HASH => $data, OUTPUT => \*STDOUT);
1163  
1164 #$self->OUTPUT($self->local_attr->get_values('OUTPUT')) if $self->local_attr->exists('OUTPUT');
1165 #$self->OUTPUT($self->local_attr->get_values('OUTPUT')) if $self->local_attr->exists('OUTPUT');
1166  
1167 0 print "\n\n";
1168 }
1169  
1170  
1171 1 __PACKAGE__->meta->make_immutable;
1172  
1173 1 1;
1174  
1175 __END__
1176  
1177  
1178 =head1 DESCRIPTION
1179  
1180 BioX::Workflow - A very opinionated template based workflow writer.
1181  
1182 =head1 AUTHOR
1183  
1184 Jillian Rowe E<lt>jillian.e.rowe@gmail.comE<gt>
1185  
1186 =head1 Acknowledgements
1187  
1188 Before version 0.03
1189  
1190 This module was originally developed at and for Weill Cornell Medical
1191 College in Qatar within ITS Advanced Computing Team. With approval from
1192 WCMC-Q, this information was generalized and put on github, for which
1193 the authors would like to express their gratitude.
1194  
1195 As of version 0.03:
1196  
1197 This modules continuing development is supported by NYU Abu Dhabi in the Center for Genomics and Systems Biology.
1198 With approval from NYUAD, this information was generalized and put on bitbucket, for which
1199 the authors would like to express their gratitude.
1200  
1201 =head1 COPYRIGHT
1202  
1203 Copyright 2015- Weill Cornell Medical College in Qatar
1204  
1205 =head1 LICENSE
1206  
1207 This library is free software; you can redistribute it and/or modify
1208 it under the same terms as Perl itself.
1209  
1210 =head1 SEE ALSO
1211  
1212 =cut
↑ Summary blib/lib/BioX/Workflow/Drake.pm »