我正在编写一个类的函数来将 sam 文件转换为 bam 文件,然后对其进行排序并在 python 中对其进行索引(我知道在 bash 中很容易实现,但了解更多信息并没有什么坏处)。
类的另一个函数已经在定义的目录 (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