def main(): parser = argparse.ArgumentParser( description="", formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('input', help="Use BAM or - for SAM STDIN") parser.add_argument('-o', '--output', help="output file") args = parser.parse_args() of = sys.stdout if args.output: if re.search('\.gz$', args.output): of = gzip.open(args.output, 'w') else: of = open(args.output, 'w') inf = sys.stdin if args.input != '-': cmd = "samtools view " + args.input p = Popen(cmd.split(), stdout=PIPE) inf = p.stdout for line in inf: v = SAM(line) for rng in [x.get_range() for x in v.get_target_transcript(68).exons]: of.write("\t".join([str(x) for x in rng.get_bed_array()]) + "\n") if args.input != '-': p.communicate() of.close()
def main(): parser = argparse.ArgumentParser(description="",formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('input',help="Use BAM or - for SAM STDIN") parser.add_argument('-o','--output',help="output file") args = parser.parse_args() of = sys.stdout if args.output: if re.search('\.gz$',args.output): of = gzip.open(args.output,'w') else: of = open(args.output,'w') inf = sys.stdin if args.input != '-': cmd = "samtools view "+args.input p = Popen(cmd.split(),stdout=PIPE) inf = p.stdout for line in inf: v = SAM(line) for rng in [x.get_range() for x in v.get_target_transcript(68).exons]: of.write("\t".join([str(x) for x in rng.get_bed_array()])+"\n") if args.input != '-': p.communicate() of.close()
def get_SAM(self, min_intron_size=68): from Bio.Format.Sam import SAM #ar is target then query qname = self.get_alignment_ranges()[0][1].chr flag = 0 if self.get_strand() == '-': flag = 16 rname = self.get_alignment_ranges()[0][0].chr pos = self.get_alignment_ranges()[0][0].start mapq = 255 cigar = self.construct_cigar(min_intron_size) rnext = '*' pnext = 0 tlen = self.get_target_range().length() seq = self.get_query_sequence() if not seq: seq = '*' qual = self.get_query_quality() if not qual: qual = '*' #seq = '*' #qual = '*' if self.get_strand() == '-': seq = rc(seq) qual = qual[::-1] ln = qname + "\t" + str(flag) + "\t" + rname + "\t" + \ str(pos) + "\t" + str(mapq) + "\t" + cigar + "\t" + \ rnext + "\t" + str(pnext) + "\t" + str(tlen) + "\t" + \ seq + "\t" + qual return SAM(ln, reference=self._reference)
def do_seq(seq,args,fname): cmd = 'samtools view '+args.input+' '+seq p = Popen(cmd.split(),stdout=PIPE) cmd = 'bedtools genomecov -i - -bg -g '+fname po2 = Popen(cmd.split(),stdin=PIPE,stdout=PIPE) cmd = 'sort -k 1,1 -k2,2n -k3,3n -S1G --parallel='+str(args.threads) if args.tempdir: cmd += ' -T '+args.tempdir po1 = Popen(cmd.split(),stdin=PIPE,stdout=po2.stdin) for line in p.stdout: sam = SAM(line) if not sam.is_aligned(): continue for ex in sam.get_target_transcript(min_intron=68).exons: bed = "\t".join([str(x) for x in ex.get_range().get_bed_array()]) po1.stdin.write(bed+"\n") po1.communicate() res = po2.communicate()[0] return res
def do_seq(seq, args, fname): cmd = 'samtools view ' + args.input + ' ' + seq p = Popen(cmd.split(), stdout=PIPE) cmd = 'bedtools genomecov -i - -bg -g ' + fname po2 = Popen(cmd.split(), stdin=PIPE, stdout=PIPE) cmd = 'sort -k 1,1 -k2,2n -k3,3n -S1G --parallel=' + str(args.threads) if args.tempdir: cmd += ' -T ' + args.tempdir po1 = Popen(cmd.split(), stdin=PIPE, stdout=po2.stdin) for line in p.stdout: sam = SAM(line) if not sam.is_aligned(): continue for ex in sam.get_target_transcript(min_intron=68).exons: bed = "\t".join([str(x) for x in ex.get_range().get_bed_array()]) po1.stdin.write(bed + "\n") po1.communicate() res = po2.communicate()[0] return res