biosails / BioX-Workflow-Command

Other
0 stars 0 forks source link

Add Template::Mustache as a templating option #26

Closed jerowe closed 7 years ago

jerowe commented 7 years ago

@nizardrou this should make you happy. ;-)

Workflows that do not follow the standard for $sample (@samples) have always been a bit of a pain. This was helped along some by iterators, but its still not great, especially if your iterator is not something very simple.

I added Template::Mustache as an optional template engine. It is the perl implementation of Mustache , a logicless templating system that is really good at iterating over loops.

Some preprocessing to get info into a datastructure (array of hashes in this case). This could also be done in the users language of choice and read into biox as a yaml or json configuration using the '.*_config' type.

    - preprocess_dna:
        local:
          - override_process: 1
        process: |
            {
              my $dna_list = [];
              my @dna_files = glob($self->melt_dna_zips);

              foreach my $dna (@dna_files) {
                my $parse = fileparse($dna, '.zip');
                my $dirname = dirname($dna);
                my $dna_hash = {
                    file => $dna,
                    dirname => $dirname,
                    parse => $parse,
                  };
                push(@{$dna_list}, $dna_hash);
              }
              $self->stash->{DNA} = $dna_list;
              ($SILENTLY);
            }

Normally with nested loops what we would have would be this:

        process: |
            {
               ##Begin DNA For Loop
               foreach my $dna (@{$self->stash->{DNA}}) {
                  $OUT .= "#TASK tags=".$sample."_".$dna->{parse}."\n";
                  $OUT .= "java -Xmx20G -jar MELT.jar   Genotype". " \\\n";
                  $OUT .= "\t". "-h  " . $self->INPUT->[0]     . " \\\n";
                  $OUT .= "\t". "-l  " . $self->INPUT->[1]     . " \\\n";
                  $OUT .= "\t". "-t  " . $dna->{file}            . " \\\n";
                  $OUT .= "\t". "-w  " . $dna->{dirname}         . "\n\n";
               }
               ##End DNA For Loop
            }

But with the addition of the Mustache template it becomes this:

            - process_mustache: |
               {{#stash.DNA}}
               #TASK tags={{sample}}_{{parse}}
               java -Xmx20G -jar MELT.jar Genotype \
                  -h {{INPUT.0}} \
                  -l {{INPUT.1}} \
                  -t {{{file}}} \
                  -w {{{dirname}}} \
                  -c COVERAGE
                {{/stash.DNA}}
        process: |
            {
              $self->render_mustache($self->process_mustache, 1);
            }