2016-01-15 22 views
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) 

雖然「打印(‘嗨’)」被寫入reverse.sam的「OUTFILE .write(s)「不是。我該怎麼做才能做到這一點?
感謝 馬克

回答

0

難道不應該寧可

for alignment in samfile.fetch() 

? 什麼

type (alignment) 

收益呢? 什麼是你的意圖與額外

with (f) 

上下文? :)從你發佈的內容來看,外部環境已經沒有意義了。

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)) 
相關問題