2017-04-10 40 views
1

我正在编写一个脚本,它是snakemake和python代码的组合,可以自动化大量文件。更确切地说,我正致力于使用BWA MEM将读取与成对末端读取(http://bio-bwa.sourceforge.net/bwa.shtml)进行对齐。在脚本的第一部分,我重复了我的文件中的名称列表(它们是fastq bunzipped文件),然后将它们相应地排序在列表中。下面是一些文件的快速浏览:子进程:无法将'_io.BufferedReader'对象隐式转换为str

['NG-8653_1A_lib95899_4332_7_1', 'NG-8653_1A_lib95899_4332_7_2', 'NG-8653_1B_lib95900_4332_7_1', 'NG-8653_1B_lib95900_4332_7_2', 'NG-8653_1N_lib95898_4332_7_1', 'NG-8653_1N_lib95898_4332_7_2']

正如你所看到的,读的排序由二两(1A _... 1和1A ..._ 2等)。现在使用子进程,我想通过使用bunzip2对它们进行解压缩,然后通过标准输入将它们传递给bwa mem。 bwa mem命令将fastq格式文件转换为.sam文件,然后我使用samtools将它们转换为.bam格式。这里的脚本到目前为止:

import re, os, subprocess, bz2 

WDIR = "/home/alaa/Documents/snakemake" 
workdir: WDIR 
SAMPLESDIR = "/home/alaa/Documents/snakemake/fastq/" 
REF = "/home/alaa/Documents/inputs/reference/hg19_ref_genome.fa" 

FILE_FASTQ = glob_wildcards("fastq/{samples}.fastq.bz2") 
LIST_FILE_SAMPLES = [] 

for x in FILE_FASTQ[0]: 
    LIST_FILE_SAMPLES.append(x) 

LIST_FILE_SAMPLES = sorted(LIST_FILE_SAMPLES) 
print(LIST_FILE_SAMPLES) 

rule fastq_to_bam: 
    run: 
     for x in range(0, len(LIST_FILE_SAMPLES), 2): 
      # get the name of the sample (1A, 1B ...) 
      samp = "" 
      samp += LIST_FILE_SAMPLES[x].split("_")[1] 

      # get the corresponding read (1 or 2) 
      r1 = SAMPLESDIR + LIST_FILE_SAMPLES[x] + ".fastq.bz2" 
      r2 = SAMPLESDIR + LIST_FILE_SAMPLES[x+1] + ".fastq.bz2" 

      # gunzipping the files and pipping them 
      p1 = subprocess.Popen(['bunzip2', '-kc', r1], stdout=subprocess.PIPE) 
      p2 = subprocess.Popen(['bunzip2', '-kc', r2], stdout=subprocess.PIPE)   


      # now write the output file to .bam format after aligning them 
      with open("sam/" + samp + ".bam", "w") as stdout: 
       fastq2sam = subprocess.Popen(["bwa", "mem", "-T 1", REF, p1.stdout, p2.stdout], stdout=subprocess.PIPE) 
       fastq2samOutput = subprocess.Popen(["samtools", "view", "-Sb", "-"], shell = True, stdin=fastq2sam.stdout, stdout=stdout) 

我试图通过逐行尝试调试脚本。将bunzip2写入输出文件时,它工作正常。现在,如果我试图管它,我得到一个错误:

Error in job fastq_to_bam while creating output file . 
RuleException: 
TypeError in line 39 of /home/alaa/Documents/snakemake/Snakefile: 
Can't convert '_io.BufferedReader' object to str implicitly 
    File "/home/alaa/Documents/snakemake/Snakefile", line 39, in __rule_fastq_to_bam 
    File "/usr/lib/python3.5/subprocess.py", line 947, in __init__ 
    File "/usr/lib/python3.5/subprocess.py", line 1490, in _execute_child 
    File "/usr/lib/python3.5/concurrent/futures/thread.py", line 55, in run 
Exiting because a job execution failed. Look above for error message 
Will exit after finishing currently running jobs. 
Exiting because a job execution failed. Look above for error message 

你能告诉我什么是脚本的问题?自从今天上午我试图寻找这个问题,我似乎无法弄清楚。任何帮助深表感谢。提前致谢。

编辑1:

从@bli和@Johannes阅读更多有关反馈后,我做了这么远:

import re, os, subprocess, bz2, multiprocessing 
from os.path import join 
from contextlib import closing 

WDIR = "/home/alaa/Documents/snakemake" 
workdir: WDIR 
SAMPLESDIR = "fastq/" 
REF = "/home/alaa/Documents/inputs/reference/hg19_ref_genome.fa" 


FILE_FASTQ = glob_wildcards("fastq/{samples, NG-8653_\d+[a-zA-Z]+_.+}") 
LIST_FILE_SAMPLES = [] 

for x in FILE_FASTQ[0]: 
    LIST_FILE_SAMPLES.append("_".join(x.split("_")[0:5])) 

LIST_FILE_SAMPLES = sorted(LIST_FILE_SAMPLES) 
print(LIST_FILE_SAMPLES) 


rule final: 
    input: 
     expand('bam/' + '{sample}.bam', sample = LIST_FILE_SAMPLES) 

rule bunzip_fastq: 
    input: 
     r1 = SAMPLESDIR + '{sample}_1.fastq.bz2', 
     r2 = SAMPLESDIR + '{sample}_2.fastq.bz2' 
    output: 
     o1 = SAMPLESDIR + '{sample}_r1.fastq.gz', 
     o2 = SAMPLESDIR + '{sample}_r2.fastq.gz' 
    shell: 
     """ 
     bunzip2 -kc < {input.r1} | gzip -c > {output.o1} 
     bunzip2 -kc < {input.r2} | gzip -c > {output.o2} 
     """ 

rule fastq_to_bam: 
    input: 
     r1 = SAMPLESDIR + '{sample}_r1.fastq.gz', 
     r2 = SAMPLESDIR + '{sample}_r2.fastq.gz', 
     ref = REF 
    output: 
     'bam/' + '{sample}.bam' 
    shell: 
     """ 
     bwa mem {input.ref} {input.r1} {input.r2} | samtools -b > {output} 
     """ 

感谢很多的帮助!我想我可以从这里管理。

最好的问候, 阿拉

+0

我第二次JohannesKöster在他的回答中发表了评论。你可能会考虑为bunzip做一个单独的规则,在这个规则中你可以使用“shell”部分,而不必用'subprocess'手动运行。然后,将此规则的输出作为映射规则的输入(并删除该循环并改用通配符)。 – bli

回答

2

你的问题是在这里:

["bwa", "mem", "-T 1", REF, p1.stdout, p2.stdout] 

p1.stdoutp2.stdoutBufferedReader类型,但subprocess.Popen预计字符串列表。您可能想要使用的是p1.stdout.read()

但是,请注意,您的方法不是使用Snakemake的惯用方式,实际上,脚本中没有任何内容真正使用Snakemake的功能。

对于Snakemake,你宁愿有一条规则,它会处理带有bwa mem的单个示例,以fastq作为输入并将bam存储为输出。请参阅Snakemake官方教程中的this example。它完全符合你在这里完成的任务,但是用更少的必要样板。只需让Snakemake完成这项工作,不要试图自己重新实现它。

+0

谢谢您的回复Johannes。您的评论是有帮助的。而我所发布的只是剧本的开始。我完全理解Snakemake的工作原理,因为我有进一步的下游规则! 再次感谢您的帮助。我仍在研究该脚本,并在完成后发布我的解决方法。 – Zen

相关问题