【问题标题】:Snakemake rule to write a new text file from input variables (Snakemake syntax)从输入变量写入新文本文件的 Snakemake 规则(Snakemake 语法)
【发布时间】:2019-03-07 22:57:03
【问题描述】:

我有一个功能齐全的 Snakemake 工作流程,但我想添加一个规则,其中输入变量在新生成的输出文本文件中作为新行写出。简单总结一下,我在下面包含了相关代码:

OUTPUTDIR = config["outputDIR"] 
SAMPLEID = list(SAMPLE_TABLE.Sample_Name)
# Above 2 lines are functional in other parts of script.

rule all:
  input:
    manifest = OUTPUTDIR + "/manifest.txt"

rule write_manifest:
  input:
    sampleid = SAMPLEID,
    loc_r1 = expand("{base}/trimmed/{sample}_1.trimmed.fastq.gz", base = OUTPUTDIR, sample = SAMPLELIST),
    loc_r2 = expand("{base}/trimmed/{sample}_2.trimmed.fastq.gz", base = OUTPUTDIR, sample = SAMPLELIST)
  output:
    OUTPUTDIR + "/manifest.txt"
  shell:
    """
    echo "{input.sampleid},{input.loc_r1},forward" >> {output}
    echo "{input.sampleid},{input.loc_r2},reverse" >> {output}
    """

我的问题是 Snakemake 正在读取文件,我需要它来打印它检测到的文件路径或示例 ID。 语法帮助?

所需的输出文件需要如下所示:

depth1,$PWD/raw_seqs_dir/Test01_full_L001_R1_001.fastq.gz,forward
depth1,$PWD/raw_seqs_dir/Test01_full_L001_R2_001.fastq.gz,reverse
depth2,$PWD/raw_seqs_dir/Test02_full_L001_R1_001.fastq.gz,forward
depth2,$PWD/raw_seqs_dir/Test02_full_L001_R2_001.fastq.gz,reverse

尝试使用 echo 编写此内容。

错误信息:

Building DAG of jobs...
MissingInputException in [write_manifest]:
Missing input files for rule write_manifest:
sample1
sample2
sample3

更新: 通过将 sampleid 添加到参数:

rule write_manifest:
  input:
    loc_r1 = expand("{base}/trimmed/{sample}_{suf}_1.trimmed.fastq.gz", base = SCRATCHDIR, sample = SAMPLE$
    loc_r2 = expand("{base}/trimmed/{sample}_{suf}_2.trimmed.fastq.gz", base = SCRATCHDIR, sample = SAMPLE$
  output:
    OUTPUTDIR + "/manifest.txt"
  params:
    sampleid = SAMPLEID
  shell:
    """
    echo "{params.sampleid},{input.loc_r1},forward" >> {output}
    echo "{params.sampleid},{input.loc_r2},reverse" >> {output}
    """

我的输出看起来像这样(这是不正确的)

sample1 sample2 sample3,$PWD/tmp/dir/sample1.fastq $PWD/tmp/dir/sample2.fastq $PWD/tmp/dir/sample3.fastq,forward
sample1 sample2 sample3,$PWD/tmp/dir/sample1.fastq $PWD/tmp/dir/sample2.fastq $PWD/tmp/dir/sample3.fastq,reverse

这仍然不是我想要的,我需要它看起来像下面想要的输出。我可以这样写,以便 Snakemake 循环遍历每个样本/输入/参数吗? 所需的输出文件需要如下所示:

depth1,$PWD/raw_seqs_dir/Test01_full_L001_R1_001.fastq.gz,forward
depth1,$PWD/raw_seqs_dir/Test01_full_L001_R2_001.fastq.gz,reverse
depth2,$PWD/raw_seqs_dir/Test02_full_L001_R1_001.fastq.gz,forward
depth2,$PWD/raw_seqs_dir/Test02_full_L001_R2_001.fastq.gz,reverse

【问题讨论】:

  • My issue is that Snakemake is reading in files, and I need it to print the file path or sample id that is it detecting instead. - 你能澄清一下这个说法吗?
  • 我更新了问题以显示所需的输出,这应该澄清。我想使用 echo 或其他方式将包含 3 个用逗号分隔的字符串的行打印到新的文本文件(称为 manifest.txt)中(显示在 echo 语句旁边的引号中)。
  • 问题/障碍是什么?是不是当你有新样本时,snakemake 不运行这个规则?
  • Snakemake 给我一个“MissingInputException”错误并说我缺少“SAMPLEID”的输入文件,但 SAMPLEID 只是一个字符串列表(例如“sample1”等)所以我不想让 Snakemake 读取文件,我需要它按原样读取 SAMPLEID。我再次更新了问题以显示错误消息
  • 其实我已经想通了!我需要将 SAMPLEID 添加到参数,而不是输入。但是,它添加了所有内容,然后用逗号分隔,我仍然需要弄清楚如何为每一行列出每个样本和相关文件。 Echo 可能需要循环遍历?

标签: shell snakemake


【解决方案1】:

您需要在参数中使用通配符sample 而不是变量SAMPLEID。这将在执行时使用特定于该规则的正确示例 ID。

params:
    sample = '{sample}'
shell:
    """
    echo "{params.sample},{input.loc_r1},forward" >> {output}
    echo "{params.sample},{input.loc_r2},reverse" >> {output}
    """

【讨论】:

  • 是的,这更接近于几乎解决方案,但请注意两点。 (1) {sample} != {sampleid} 在我的例子中。 (2) 输出仍然没有产生正确的结果。在我上面的问题中看到,所有的sampleid都是列表,然后是所有的输入,然后转发,然后重复。我想要的结果是为每个样本一次打印一行(想要的结果如上所示)
猜你喜欢
  • 2021-12-29
  • 2021-09-29
  • 1970-01-01
  • 2022-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多