Snakemake:在声明中使用另一个扩展变量的值

时间:2019-03-22 09:43:05

标签: snakemake

是否可以在扩展函数中使用另一个扩展变量的值?例如,如果我有以下config.yaml

patients:
  patientA:
    - "sampleA_A"
    - "sampleA_B"
    - "sampleA_C"
  patientB:
    - "sampleB_A"
    - "sampleB_B"
    - "sampleB_C"

还有这个Snakefile

configfile: "config.yaml"
patient_samples = expand("{patient}_{sample}.txt", patient = "patientA", sample = config["patients"]["patientA"])
print(patient_samples)

这将产生:

['patientA_sampleA_A.txt', 'patientA_sampleA_B.txt', 'patientA_sampleA_C.txt']

但是,如果我想遍历患者并使其与样本匹配,我希望能够使用expand Patient变量的值来检索患者的样本:

configfile: "config.yaml"
PATIENTS = ["patientA", "patientB"]
patient_samples = expand("{patient}_{sample}.txt", patient = PATIENTS, sample = config["patients"][patient])
print(patient_samples)

但这不起作用,因为它会产生以下错误:

NameError in line 4 of /Users/fongchan/Downloads/Snakefile:
name 'patient' is not defined
  File "/Users/fongchan/Downloads/Snakefile", line 4, in <module>

是否可以引用扩展patient值,以便可以在config变量中使用它来获取相应的样本?

在此先感谢

1 个答案:

答案 0 :(得分:0)

如果您完全离开expand函数并自己构建patient_samples列表,也许会更容易。例如。像这样:

patient_samples= []
for patient in config["patients"]:
    for sample in config["patients"][patient]:
        patient_samples.append("{patient}_{sample}.txt".format(patient= patient, sample= sample))