def samtools_index(self): if not self.has_been_sorted: raise Exception( 'BAM must be sorted before it can be indexed' ) with namedpipe.temp_named_pipe() as ( bam_pipe ), namedpipe.temp_named_pipe() as ( index_pipe ): with subprocess.Popen( ( 'sh', '-c', ( 'cat {0} & ' 'samtools index {1} {0} &' 'cat > {1}' ) .format( index_pipe.name, bam_pipe.name ) ), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=self.log ) as samtools_index: self.index, _ = samtools_index.communicate(input=self.bam)
def get_fasta(self): with namedpipe.temp_named_pipe() as peaks_pipe: with subprocess.Popen( ( 'sh', '-c', ( 'bedtools getfasta -bed {0} -fi {1} & cat > {0}' .format(peaks_pipe.name, self.reference_genome_path) ) ), stdin=subprocess.PIPE, stdout=subprocess.PIPE ) as bedtools_getfasta: self.fasta, _ = bedtools_getfasta.communicate(input=self.peaks)
def bwa_aln(self, sequence_alignment): """Perform sequence alignment using the bwa aln algorithm Single-end and paired end reads are handled appropriately based on the type of the SequenceAlignment's raw reads path Parameters ---------- sequence_alignment : SequenceAlignment a SequenceAlignemnt object Returns ------- bytes A BAM file """ if not isinstance(sequence_alignment.raw_reads_path, str): with namedpipe.temp_named_pipe() as ( sai_pipe_0), namedpipe.temp_named_pipe() as (sai_pipe_1): with subprocess.Popen( ('sh', '-c', ('bwa sampe {0} {1} {2} {3} {4} & ' 'bwa aln -t {5} -q {6} -l {7} -k {8} {0} {3} > ' '{1} & ' 'bwa aln -t {5} -q {6} -l {7} -k {8} {0} {4} > ' '{2} & ').format( self.reference_genome_path, sai_pipe_0.name, sai_pipe_1.name, sequence_alignment.raw_reads_path[0], sequence_alignment.raw_reads_path[1], math.floor(sequence_alignment.processes / 2), self.trim_qual, self.seed_len, self.max_seed_diff)), stdout=subprocess.PIPE, stderr=sequence_alignment.log) as bwa_aln_sampe: with subprocess.Popen( ('samtools', 'view', '-Sbq', str(sequence_alignment.phred_quality_score), '-@', str(sequence_alignment.processes)), stdin=bwa_aln_sampe.stdout, stdout=subprocess.PIPE, stderr=sequence_alignment.log) as samtools_view: return samtools_view.communicate()[0] else: with namedpipe.temp_named_pipe() as sai_pipe: with subprocess.Popen( ('sh', '-c', ('bwa samse {0} {1} {2} & ' 'bwa aln -t {3} -q {4} -l {5} -k {6} {0} {2} > {1}; ' ).format(self.reference_genome_path, sai_pipe.name, sequence_alignment.raw_reads_path, sequence_alignment.processes, self.trim_qual, self.seed_len, self.max_seed_diff)), stdout=subprocess.PIPE, stderr=sequence_alignment.log) as bwa_aln_samse: with subprocess.Popen( ('samtools', 'view', '-bhq', str(sequence_alignment.phred_quality_score), '-@', str(sequence_alignment.processes)), stdin=bwa_aln_samse.stdout, stdout=subprocess.PIPE, stderr=sequence_alignment.log) as samtools_view: return samtools_view.communicate()[0]
def bwa_aln(self): if isinstance(self.sa.raw_reads_path, tuple): with namedpipe.temp_named_pipe() as ( sai_pipe_0 ), namedpipe.temp_named_pipe() as ( sai_pipe_1 ): with subprocess.Popen( ( 'sh', '-c', ( 'bwa sampe {0} {1} {2} {3} {4} & ' 'bwa aln -t {5} -q {6} {0} {3} > {1} & ' 'bwa aln -t {5} -q {6} {0} {4} > {2} & ' ) .format( self.reference_genome_path, sai_pipe_0.path, sai_pipe_1.path, self.sa.raw_reads_path[0], self.sa.raw_reads_path[1], math.floor(self.sa.processes / 2), self.trims_reads_by ) ), stdout=subprocess.PIPE, stderr=self.sa.log ) as bwa_aln_sampe: with subprocess.Popen( ( 'samtools', 'view', '-Sbq', str(self.sa.phred_quality_score), '-@', str(self.sa.processes) ), stdin=bwa_aln_sampe.stdout, stdout=subprocess.PIPE, stderr=self.sa.log ) as samtools_view: self.sa.bam, _ = samtools_view.communicate() elif isinstance(self.sa.raw_reads_path, str): with namedpipe.temp_named_pipe() as sai_pipe: with subprocess.Popen( ( 'sh', '-c', ( 'bwa samse {0} {1} {2} & ' 'bwa aln -t {3} -q {4} {0} {2} > {1}; ' ) .format( self.reference_genome_path, sai_pipe.name, self.sa.raw_reads_path, self.sa.processes, self.trims_reads_by ) ), stdout=subprocess.PIPE, stderr=self.sa.log ) as bwa_aln_samse: with subprocess.Popen( ( 'samtools', 'view', '-bhq', str(self.sa.phred_quality_score), '-@', str(self.sa.processes) ), stdin=bwa_aln_samse.stdout, stdout=subprocess.PIPE, stderr=self.sa.log ) as samtools_view: self.sa.bam, _ = samtools_view.communicate()