如何设置目标文件由文件内容决定的snakemake规则?

问题描述 投票:2回答:1

我想根据条形码信息将sam文件拆分成多个sam文件。查询条形码信息列在另一个文件中。

$ cat barcode.list
ATGCATGC
TTTTAAAA
GGGGCCCC
CGCGATGA
AAGGTTCC
....

下面的简单bash脚本可以实现目标。

barcode_list=./A_barcode.csv
input_bam=./A_input.bam
splited_dir=./splited_sam/A
filtered_dir="./filterd_sam/A"

mkdir -p ${splited_dir} ${splited_dir}
header=$(samtools view -H ${input_bam})
samtools view {input.bam} | LC_ALL=C fgrep -f <(cat ${barcode_list}) | awk -v header="${header}" -v outdir="${splited_dir}" '{barcode=substr($0,index($0, "\tCB:Z:")+6,18);if (!header_printed[barcode]++) {print $0 >> outdir"/"barcode".sam"}}'
for sam in ${output_dir};do samtools view -q 30 -m 1 ${sam} -O bam -o ${filtered_dir}/$(basename ${sam} "sam")"bam";done 

注意:只有barcode_list文件和input_bam文件中的条形码才会被记录到新文件中。

但我想将脚本重写为sankemake管道以便更好地扩展。我试过的解决方案如下所示。

我不知道如何在所有规则的最后一步中分配输入文件名,在本例中为rule all。因为它们是由input_baminput_barcode文件决定的。同时,在不了解splited_sam文件名的情况下,我也无法通过下一步。

SAMPLES = ["A", "B", "C", "D"]
# BARCODE = ???

rule all:
  input:
    splited_sam_dir = expand("splited_sam/{sample}", sample=SAMPLES)

rule split_sam:
  input:
    bar = "{sample}_barcode.csv",
    bam = "{sample}_input.bam"
  output:
    splited_sam_dir = "splited_sam/{sample}"
  shell:
    """
    header=$(samtools view -H {input.bam})
    samtools view {input.bam} | LC_ALL=C fgrep -f <(cat {input.bar}) | awk -v header="$header" -v outdir="{output.splited_sam_dir}" '{{barcode=substr($0,index($0, "\tCB:Z:")+6,18);if (!header_printed[barcode]++) {{print $0 >> outdir"/"barcode".sam"}}}}
    """
rule filter_sam:
  # ??? don't know the input file name...
python bash workflow bioinformatics snakemake
1个回答
0
投票

我认为您需要将“split_sam”定义为检查点规则,请参阅the doc on checkpoints。一旦执行检查点规则,将重新计算依赖于此规则输出的所有规则的DAG。

© www.soinside.com 2019 - 2024. All rights reserved.