Popen samtools没有运行

时间:2017-08-09 23:03:42

标签: subprocess samtools

我正在编写一个类的函数来将sam文件转换为bam文件,然后对它进行排序并在python中对其进行索引(我知道它很容易在bash中实现,但是了解更多内容并没有什么坏处。) / p>

已经由该类的另一个函数在已定义的目录(cwd)中生成了一个sam文件。

以下是代码:

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的

1 个答案:

答案 0 :(得分:0)

参考here.

这是因为'>'重定向。我将代码修改为:

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

它有效。