0

在使用 pysam 模块时,我试图将标准输出重定向到一个文件,如下所示:

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)”不是。我应该怎么做呢?
谢谢马克

4

2 回答 2

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))
于 2016-03-15T22:25:42.803 回答
0

不应该是

for alignment in samfile.fetch() 

? 做什么

type (alignment)

屈服?你对额外的意图是什么

with (f) 

语境?:) 从您发布的内容来看,外部上下文已经没有意义了。

于 2016-01-15T03:57:19.357 回答