def map(self): if self.to_map: if self.single_end: if self.gtf != 'blank': cmd_args = ['tophat', '-p', '6', '-o', self.output, '-r', self.mean, '--mate-std-dev', self.sd, '-G', self.gtf, '--library-type', self.library_type, self.species, self.input1] else: cmd_args = ['tophat', '-p', '6', '-o', self.output, '-r', self.mean, '--mate-std-dev', self.sd, '--library-type', self.library_type, self.species, self.input1] else: if self.gtf != 'blank': cmd_args = ['tophat', '-p', '10', '-o', self.output, '-r', self.mean, '--mate-std-dev', self.sd, '-G', self.gtf, '--library-type', self.library_type, self.species, self.input1, self.input2] else: cmd_args = ['tophat2', '-o', self.output, '-p', '6', '-r', self.mean, '--mate-std-dev', self.sd, '--library-type', self.library_type, self.species, self.input1, self.input2] #print self.errorlog print "Mapping with tophat: " + " ".join(cmd_args[1:]) errorlog = open(self.errorlog, 'w') tophat = Popen(cmd_args, stderr=errorlog) tophat.wait() errorlog.close() sam.proc([self.output + "/accepted_hits.bam", "False"])
def main(argv): sam_dir = "/media/storage2/data/sam/" bam_dir = "/media/storage2/data/bam/" bed_dir = "/media/storage2/data/bed/" ## take Illmina fastq file parser = argparse.ArgumentParser(description="Map fastq files.") parser.add_argument('-i', '--input_fastq', nargs=2, metavar='fastq', required=True, dest='fastq_files', help='read1 and read3 of library') parser.add_argument('-n', '--index', dest='index', required=True, help='index number of library') parser.add_argument('-r', '--mate-inner-dist', metavar='mean', required=True, dest='mean', help='mean size of library (minus adaptors)') parser.add_argument('-s', '--mate-std-dev', metavar='sd', required=True, dest='sd', help='standard deviation of library') parser.add_argument('-g', '--GTF', metavar='gtf', dest='gtf', default='blank', help='reference gtf file to map reads to') parser.add_argument('--stranded', action='store_true', required=False, dest='strand', default=False, help="indicate if library contains strand information") args = parser.parse_args() input1 = argv[1] input2 = argv[2] index = argv[3] input_prefix = input1.split("_")[0] + "_" + index print input_prefix #input_filtered = input_prefix + "_filt.fastq" #cmd_args = ['fastq_quality_filter', '-q', '20', '-p', '80', # '-i', input_fastq, '-o', input_filtered] #filt = Popen(cmd_args) #filt.wait() ## Run bowtie samfile = sam_dir + input_prefix + ".sam" if not os.path.exists(samfile): cmd_args = ['bowtie', '-S', '-m', '1', '-p', '6', '-I', '100', '-X', '1500', '--chunkmbs', '256', 'mm9', '-1', input1, '-2', input2, samfile] print "Mapping with bowtie: " + " ".join(cmd_args[1:]) bowtie = Popen(cmd_args) bowtie.wait() ## SAM to BAM bamfile = bam_dir + input_prefix + ".bam" if not os.path.exists(bamfile): sam.sam2bam(samfile, bamfile) ## Remove unmapped, duplicates, sort, index sam.proc(bam_dir, input_prefix) ## Output BED print "Writing BED..." bam = bam_dir + input_prefix + "_sort.bam" bed = bed_dir + input_prefix + ".bed" sam.bam2bed(bam, bed, pe = True)
def sam2bam(self): if not os.path.exists(self.bamfile): try: sam.sam2bam(self.samfile, self.bamfile, self.errorlog) except IOError: self.errorlog.write("Can't open SAM for conversion.\n") return except: # pdb.set_trace() self.errorlog.write("SAM to BAM conversion failed\n") return else: self.errorlog.write("SAM to BAM completed successfully.\n") self.errorlog.write("Removing SAM...\n") os.remove(self.samfile) try: sam.proc([self.bamfile, "False", self.errorlog]) except IOError: self.errorlog.write("BAM processing failed: IOError.\n") else: self.errorlog.write("BAM processing completed successfully.\n")
def proc(self): ret = sam.proc([self.bamfile, "False"])
def sam2bam(self): if not os.path.exists(self.bamfile): sam.sam2bam(self.samfile, self.bamfile) sam.proc([self.bamfile, "True"])