You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Snakemake中如何生成规则的自定义输入列表?替代expand方法

Snakemake 实现指定样本组合的all规则

一、按顺序一一配对样本

如果需要让sample_1和sample_2的元素按索引一一对应(如foo配spam、bar配ham),可以利用Snakemake的expand函数结合zip参数,避免生成全量笛卡尔积组合。

示例代码:

sample_1 = ["foo", "bar", "baz"]
sample_2 = ["spam", "ham", "eggs"]

rule all:
    input:
        # zip参数指定按顺序配对两个列表的元素
        expand("{first_sample}_{second_sample}.frob", zip, first_sample=sample_1, second_sample=sample_2)

rule frobnicate:
    input:
        assembly = "{first_sample}_{second_sample}.txt"
    output:
        frobnicated = "{first_sample}_{second_sample}.frob"
    # 这里替换为你的实际处理命令
    shell:
        "cat {input} > {output}"

执行后,all规则只会触发foo_spam.frob、bar_ham.frob、baz_eggs.frob这三个目标的生成。

二、自定义复杂样本组合

如果需要更灵活的配对(比如非顺序配对、条件筛选配对),可以直接定义目标组合列表,再传递给all规则。

1. 手动指定任意配对

直接定义包含目标样本对的元组列表,再生成对应的输出路径:

# 自定义需要处理的样本对,每个元组对应(first_sample, second_sample)
custom_pairs = [("foo", "ham"), ("baz", "spam"), ("bar", "eggs")]

rule all:
    input:
        # 直接通过列表推导式生成目标文件路径
        [f"{s1}_{s2}.frob" for s1, s2 in custom_pairs]

rule frobnicate:
    input:
        assembly = "{first_sample}_{second_sample}.txt"
    output:
        frobnicated = "{first_sample}_{second_sample}.frob"
    shell:
        "cat {input} > {output}"

2. 按条件筛选配对

如果需要基于规则筛选样本组合(比如只保留包含特定字符的样本对),可以用列表推导式生成符合条件的配对:

sample_1 = ["foo", "bar", "baz"]
sample_2 = ["spam", "ham", "eggs"]

# 筛选条件:sample_1元素含"ar",sample_2元素含"am"
filtered_pairs = [(s1, s2) for s1 in sample_1 for s2 in sample_2 if "ar" in s1 and "am" in s2]

rule all:
    input:
        [f"{s1}_{s2}.frob" for s1, s2 in filtered_pairs]

rule frobnicate:
    input:
        assembly = "{first_sample}_{second_sample}.txt"
    output:
        frobnicated = "{first_sample}_{second_sample}.frob"
    shell:
        "cat {input} > {output}"

执行后会生成bar_spam.frob和bar_ham.frob两个目标。

内容的提问来源于stack exchange,提问作者KeyboardCat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 07:45:51