在使用pysam模块时,我尝试将stdout重定向到文件,如下所示:
bam=sys.argv[1]
samfile = pysam.AlignmentFile(bam, "rb")
for alignment in samfile:
reverse=isReverse(alignment.flag)
if (reverse):
outfile = pysam.AlignmentFile("-", "w", template=samfile)
with open('reverse.sam', 'w') as f:
with (f):
for s in samfile:
print('HEY')
outfile.write(s)
当“print('HEY')”写入reverse.sam时,“outfile.write(s)”不是。我应该怎么做呢? 谢谢 标记
答案 0 :(得分:0)
不应该
for alignment in samfile.fetch()
? 什么
type (alignment)
得到? 你有什么打算用额外的
with (f)
上下文? :)从你发布的内容来看,外部环境已经没有意义了。
答案 1 :(得分:0)
你走了。
bam_file = sys.argv[1] # or '-' (if reading from stdin) or 'path/to/your/input.bam'
sam_file = 'path/to/your/output.sam' # or '-' (if writing to stdout) or 'path/to/your/output.bam' (for bam file)
infile = pysam.AlignmentFile(bam_file, "rb")
outfile = pysam.AlignmentFile(sam_file, "w", template=infile) # or "wb" if writing to bam file
i = 0
for alignment in infile:
if alignment.is_reverse:
outfile.write(alignment)
i += 1
outfile.close()
infile.close()
print("Wrote {} alignments.".format(i))