对于Snakemake工作流程,我需要处理许多BAM文件中的标签,并希望通过脚本(using the Snakemake script: directive)将它们进行管道处理。我这样做的具体方法是使用pysam stream processing。
infile = pysam.AlignmentFile("-", "rb")
outfile = pysam.AlignmentFile("-", "wb", template=infile)
for s in infile:
(flowcell, lane) = s.query_name.split(':')[0:2]
rg_id = ".".join([flowcell, lane])
s.set_tag('RG',rg_id,'Z')
outfile.write(s)
该脚本独立运行良好,但是我无法弄清楚如何通过snakemake script
指令对其进行集成。
我更喜欢这种方式来最大程度地减少IO和RAM的使用。
编辑:依靠直接加载来修复RG标签。
# parameters passed from snakemake
bam_file = snakemake.input[0]
fixed_bam_file = snakemake.output[0]
bamfile = pysam.AlignmentFile(bam_file, "rb")
fixed_bamfile = pysam.AlignmentFile(fixed_bam_file, "wb", template = bamfile)
for i, read in enumerate(bamfile.fetch()):
(flowcell, lane) = read.query_name.split(':')[0:2]
rg_id = ".".join([flowcell, lane])
read.set_tag('RG',rg_id,'Z')
fixed_bamfile.write(read)
if not (i % 100000):
print("Updated the read group for {} reads to {}".format(i, rg_id))
bamfile.close()
fixed_bamfile.close()
编辑:Snakemakes run:
和shell:
伪指令设置workdir:
目录,而script:
伪指令相对于执行Snakefile的目录进行操作(保持所有内容良好,整洁)。因此,将流处理器置于script:
下的问题。
答案 0 :(得分:1)
使用shell
代替script
指令:
rule all:
input:
expand('{sample}_edited.bam'), sample=['a', 'b', 'c']
rule somename:
input:
'{sample}.bam'
output:
'{sample}_edited.bam'
shell:
'''
cat {input} > python edit_bam.py > {output}
'''
答案 1 :(得分:1)
@Krischan,看来您已经找到了解决方案,如果可以的话,可以将其发布为答案。
或者,您可以使用对象{workflow}
获取Snakefile的目录,然后从那里构造python脚本的路径。如果您的目录结构是:
./
├── Snakefile
├── data
│ └── sample.bam
└── scripts
└── edit_bam.py
Snakefile看起来像:
rule all:
input:
'test.tmp',
rule one:
input:
'sample.bam',
output:
'test.tmp',
shell:
r"""
cat {input} \
| {workflow.basedir}/scripts/edit_bam.py > {output}
"""
由snakemake -d data ...
似乎workflow
对象未记录,但请检查此线程Any way to get the full path of the Snakefile within the Snakefile?