2015-07-20 113 views
-1

新手問題在這裏;管道破壞返回

我有以下兩個函數 - 第一個,'read_fq_entry',從subprocess.Popen(該系統調用zcat並管道它)讀取行,並一次返回四行作爲列表。

第二個'stream_fq'從第一個獲取返回的輸出,並且暫時打印它。 (最終我將被隨機抽樣套系,並將其寫入文件

的問題是,在第一個函數的返回打破了管道 - 如何不做到這一點任何想法??

def read_fq_entry(process): 

     while (True): 
      name = process.stdout.readline()  
      seq = process.stdout.readline() 
      strand = process.stdout.readline() 
      qual = process.stdout.readline() 

      if (not name or name == '') : 
       return [] 

      return [name, seq, strand, qual] 


def stream_fq(infile1, infile2, Nr_of_reads): 


     proc1 = subprocess.Popen("zcat " + infile1, stdout=subprocess.PIPE, shell=True) 
     proc2 = subprocess.Popen("zcat " + infile2, stdout=subprocess.PIPE, shell=True) 

     while (True): 

      fq_1_entry = read_fq_entry(proc1) 
      fq_2_entry = read_fq_entry(proc2) 
      print fq_1_entry 
      print fq_2_entry 
+1

這可能是不相關的職業blem,但'not name or name =='''是多餘的,因爲當'name'是空字符串時'not name'的值爲True。將它縮短爲「if not name:」會有相同的效果。 – Kevin

+0

謝謝Kevin - 好的提示 – user3234810

+0

把'''stream_fq''寫成[coroutine](http://wla.berkeley.edu/~cs61a/fa11/lectures/streams.html#coroutines)和''send' ''線路。如果我在我的辦公桌上,我會用一個例子回答,希望這會讓你走。 – wwii

回答

1

return [name, seq, strand, qual]while True:循環中,你應該把它移到一個級別,也許改變return聲明爲yield語句。

+0

這沒有幫助。 OP想要一個發生器,但這是一個重大改寫。 – Kevin

+0

定義生成器 - 可根據需要重寫,尋找最佳解決方案。 – user3234810