#!/usr/bin/env python

# Standard packages
import sys
import argparse

# Third-party packages
from toil.job import Job

# Package methods
from ddb import configuration
from ddb_ngsflow import gatk
from ddb_ngsflow import pipeline
from ddb_ngsflow.rna import star
from ddb_ngsflow.rna import bowtie
from ddb_ngsflow.rna import cufflinks


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('-s', '--samples_file', help="Input configuration file for samples")
    parser.add_argument('-c', '--configuration', help="Configuration file for various settings")
    Job.Runner.addToilOptions(parser)
    args = parser.parse_args()
    args.logLevel = "INFO"

    sys.stdout.write("Parsing configuration data\n")
    config = configuration.configure_runtime(args.configuration)

    sys.stdout.write("Parsing sample data\n")
    samples = configuration.configure_samples(args.samples_file, config)

    # Workflow Graph definition. The following workflow definition should create a valid Directed Acyclic Graph (DAG)
    root_job = Job.wrapJobFn(pipeline.spawn_batch_jobs, cores=1)

    # Per sample jobs
    for sample in samples:
        # Alignment and Refinement Stages
        flags = ("local", "unstranded", "removeNonCanonical", "2-stage", "cufflinks")

        align_job = Job.wrapJobFn(star.star_unpaired, config, sample, samples, flags,
                                  cores=int(config['star']['num_cores']),
                                  memory="{}G".format(config['star']['max_mem']))

        samples[sample]['star'] = "{}.star.Aligned.sortedByCoord.out.bam".format(sample)
        samples[sample]['unmapped_fastq'] = "{}.star.Unmapped.out.mate1".format(sample)

        # bowtie_job = Job.wrapJobFn(bowtie.bowtie_unpaired, config, sample, samples, flags,
        #                            cores=int(config['bowtie']['num_cores']),
        #                            memory="{}G".format(config['bowtie']['max_mem']))

        input_bams = list()
        input_bams.append(samples[sample]['star'])
        input_bams.append(samples[sample]['bowtie'])

        merge_job = Job.wrapJobFn(gatk.merge_sam, config, sample, input_bams,
                               cores=int(config['picard-merge']['num_cores']),
                               memory="{}G".format(config['picard-merge']['max_mem']))

        cufflinks_job = Job.wrapJobFn(cufflinks.cufflinks, config, sample, merge_job.rv(),
                                   cores=int(config['cufflinks']['num_cores']),
                                   memory="{}G".format(config['cufflinks']['max_mem']))

        # Create workflow from created jobs
        root_job.addChild(align_job)
        # align_job.addChild(bowtie_job)
        # bowtie_job.addChild(merge_job)
        align_job.addChild(merge_job)
        merge_job.addChild(cufflinks_job)

    cuffmerge_job = Job.wrapJobFn(cufflinks.cuffmerge, config, "blah", samples, "manifest.txt",
                                  cores=int(config['cuffmerge']['num_cores']),
                                  memory="{}G".format(config['cuffmerge']['max_mem']))

    root_job.addFollowOn(cuffmerge_job)

    for sample in samples:
        cuffquant_job = Job.wrapJobFn(cufflinks.cuffquant, config, sample, samples,
                                      cores=int(config['cuffquant']['num_cores']),
                                      memory="{}G".format(config['cuffquant']['max_mem']))
        cuffmerge_job.addChild(cuffquant_job)

    # Start workflow execution
    Job.Runner.startToil(root_job, args)

@@ -2336,32 +2336,104 @@
 %5D%5B'max_mem'%5D))%0A%0A
+        samples%5Bsample%5D%5B'bam'%5D = %22%7B%7D.merged.sorted.bam%22.format(sample)%0A%0A
         cufflink
@@ -2495,24 +2495,20 @@
 le, 
-merge_job.rv(),%0A
+samples,%0A   
     
@@ -2575,32 +2575,35 @@
 %5B'num_cores'%5D),%0A
+   
                 

Fix setting bam, file for cufflinks