2017-08-09 56 views
-1

我正在寫一個類將sam文件轉換爲bam文件,然後對它進行排序並在python中進行索引(我知道它很容易在bash中實現,但它並不會傷害到學習更多)。Popen samtools沒有運行

sam文件已經在定義的目錄(cwd)中由該類的另一個函數生成了。

下面是代碼:

def sam2bam(self): 
    """ 
    return a sorted and index bam file in the working directory 
    """ 


    if str(self.fq)[7:9] == '24': 
     # create bam 
     comnd = "samtools view -bS " + str(self.fq)[0:10] + ".sam > " + str(self.fq)[0:10] + ".bam" 

     print("Converting sam to bam ... (executed command: {})\n".format(comnd)) 
     comnd_input = shlex.split(comnd) 

     with open((str(self.fq)[0:10] + '.bam'), 'w') as f: 
      Popen(comnd_input, stdout = f, stderr = PIPE, cwd = 'G24/' + str(self.fq)) 


     # sort bam  
     comnd2 = "samtools sort " + str(self.fq)[0:10] + ".bam -o " + str(self.fq)[0:10] + ".sorted.bam" 
     print('Sorting bam ... (executed command: {}\n)'.format(comnd2)) 

     comnd_input2 = shlex.split(comnd2) 
     with open((str(self.fq)[0:10] + 'sorted.bam'), 'w') as f_sort: 
      Popen(comnd_input2, stdout = f_sort, stderr = PIPE, cwd = 'G24/' + str(self.fq)) 

     # index bam 
     comnd3 = "samtools index " + str(self.fq)[0:10] + ".sorted.bam" 
     print('Indexing bam ... (executed command: {}\n)'.format(comnd3)) 

     comnd_input3 = shlex.split(comnd3) 

     with open((str(self.fq)[0:10] + 'sorted.bam'), 'w') as f_index: 
      Popen(comnd_input3, stdout = f_index, stderr = PIPE, cwd = 'G24/' + str(self.fq)) 

但沒有出來

我想:

Popen(comnd_input, stdout = PIPE, stderr = PIPE, cwd = 'G24/' + str(self.fq)) 

還沒有任何。嘗試將其保存爲變量並執行.communicate(),沒有任何結果。

所以我不知道我做了什麼錯誤?

感謝,

XP的

回答

0

here.

這是因爲 '>' 重定向的。我修改了代碼爲:

Popen(comnd, stdout = PIPE, stderr = PIPE, shell = True, cwd = 'G14/' + str(self.fq)) 

它工作。