old_contents: '''
  Individual stages of the pipeline implemented as functions from
  input files to output files.
  
  The run_stage function knows everything about submitting jobs and, given
  the state parameter, has full access to the state of the pipeline, such 
  as config, options, DRMAA and the logger.
  '''
  
  from utils import safe_make_dir
  from runner import run_stage
  
  class Stages(object):
      def __init__(self, state):
          self.state = state
  
      def fastqc(self, fastq_in, dir_out):
          '''Quality check fastq file using fastqc'''
          safe_make_dir(dir_out)
          command = "fastqc --quiet -o {dir} {fastq}".format(dir=dir_out, fastq=fastq_in)
          run_stage(self.state, 'fastqc', command)
      
      def index_reference_bwa(self, reference_in, index_file_out):
          '''Index the reference genome using BWA'''
          command = "bwa index -a bwtsw {ref}".format(ref=reference_in)
          run_stage(self.state, 'index_reference_bwa', command)
      
      def index_reference_samtools(self, reference_in, index_file_out):
          '''Index the reference genome using samtools'''
          command = "samtools faidx {ref}".format(ref=reference_in)
          run_stage(self.state, 'index_reference_samtools', command)
      
      def align_bwa(self, inputs, bam_out, sample):
          '''Align the paired end fastq files to the reference genome using bwa'''
          fastq_read1_in, [fastq_read2_in, reference_in] = inputs
          # Get the read group information for this sample from the configuration file
          read_group = state.config.get_read_group(sample)
          # Get the number of cores to request for the job, this translates into the
          # number of threads to give to bwa's -t option
          cores = state.config.get_stage_option('align_bwa', 'cores')
          # Run bwa and pipe the output through samtools view to generate a BAM file
          command = 'bwa mem -t {cores} -R "{read_group}" {reference} {fastq_read1} {fastq_read2} ' \
                    '| samtools view -S -b - > {bam}' \
                    .format(cores=cores,
                        read_group=read_group,
                        fastq_read1=fastq_read1_in,
                        fastq_read2=fastq_read2_in,
                        reference=reference_in,
                        bam=bam_out)
          run_stage(self.state, 'align_bwa', command)
  
new_contents: '''
  Individual stages of the pipeline implemented as functions from
  input files to output files.
  
  The run_stage function knows everything about submitting jobs and, given
  the state parameter, has full access to the state of the pipeline, such 
  as config, options, DRMAA and the logger.
  '''
  
  from utils import safe_make_dir
  from runner import run_stage
  
  class Stages(object):
      def __init__(self, state):
          self.state = state
  
      def fastqc(self, fastq_in, dir_out):
          '''Quality check fastq file using fastqc'''
          safe_make_dir(dir_out)
          command = "fastqc --quiet -o {dir} {fastq}".format(dir=dir_out, fastq=fastq_in)
          run_stage(self.state, 'fastqc', command)
      
      def index_reference_bwa(self, reference_in, index_file_out):
          '''Index the reference genome using BWA'''
          command = "bwa index -a bwtsw {ref}".format(ref=reference_in)
          run_stage(self.state, 'index_reference_bwa', command)
      
      def index_reference_samtools(self, reference_in, index_file_out):
          '''Index the reference genome using samtools'''
          command = "samtools faidx {ref}".format(ref=reference_in)
          run_stage(self.state, 'index_reference_samtools', command)
      
      def align_bwa(self, inputs, bam_out, sample):
          '''Align the paired end fastq files to the reference genome using bwa'''
          fastq_read1_in, [fastq_read2_in, reference_in] = inputs
          # Get the read group information for this sample from the configuration file
          read_group = self.state.config.get_read_group(sample)
          # Get the number of cores to request for the job, this translates into the
          # number of threads to give to bwa's -t option
          cores = self.state.config.get_stage_option('align_bwa', 'cores')
          # Run bwa and pipe the output through samtools view to generate a BAM file
          command = 'bwa mem -t {cores} -R "{read_group}" {reference} {fastq_read1} {fastq_read2} ' \
                    '| samtools view -S -b - > {bam}' \
                    .format(cores=cores,
                        read_group=read_group,
                        fastq_read1=fastq_read1_in,
                        fastq_read2=fastq_read2_in,
                        reference=reference_in,
                        bam=bam_out)
          run_stage(self.state, 'align_bwa', command)
  
subject: Fix bug in align_bwa caused by incorrect reference to state