Snakemake: How to save and access sample details in config.yml file?

强颜欢笑 提交于 2019-12-04 01:58:01

问题


Can anybody help me understand if it is possible to access sample details from a config.yml file when the sample names are not written in the snakemake workflow? This is so I can re-use the workflow for different projects and only adjust the config file. Let me give you an example:

I have four samples that belong together and should be analyzed together. They are called sample1-4. Every sample comes with some more information but to keep it simple here lets say its just a name tag such as S1, S2, etc.

My config.yml file could look like this:

samples: ["sample1","sample2","sample3","sample4"]

sample1:
  tag: "S1"
sample2:
  tag: "S2"
sample3:
  tag: "S3"
sample4:
  tag: "S4"

And here is an example of the snakefile that we use:

configfile: "config.yaml"

rule final: 
  input: expand("{sample}.txt", sample=config["samples"])

rule rule1:
  output:  "{sample}.txt"
  params:  tag=config["{sample}"]["tag"]
  shell:   """
           touch {output}
           echo {params.tag} > {output}

What rule1 is trying to do is create a file named after each sample as saved in the samples variable in the config file. So far no problem. Then, I would like to print the sample tag into that file. As the code is written above, running snakemake will fail because config["{sample}"] will literally look for the {sample} variable in the config file which doesn't exist because instead I need it to be replaced with the current sample that the rule is run for, e.g. sample1.

Does anybody know if this is somehow possible to do, and if yes, how I could do it?

Ideally I'd like to compress the information even more (see below) but that's further down the road.

samples:
    sample1:
        tag: "S1"
    sample2:
        tag: "S2"
    sample3:
        tag: "S3"
    sample4:
        tag: "S4"

回答1:


I would suggest using a tab-delimited file in order to store samples information.

sample.tab:

Sample     Tag      
1          S1   
2          S2

You could store the path to this file in the config file, and read it in your Snakefile.

config.yaml:

sample_file: "sample.tab"

Snakefile:

configfile: "config.yaml"

sample_file = config["sample_file"]

samples = read_table(sample_file)['Sample']
tags    = read_table(sample_file)['Tag']

This way your can re-use your workflow for any number of samples, with any number of columns.

Apart from that, in Snakemake usually you can escape curly brackets by doubling them, maybe you could try that.

Good luck!




回答2:


In the params section, you need to provide a function of wildcards. The following modification of your workflow seems to work:

configfile: "config.yaml"

rule final: 
    input: expand("{sample}.txt", sample=config["samples"])

rule rule1:
    output:
        "{sample}.txt"
    params:
        tag = lambda wildcards: config[wildcards.sample]["tag"]
    shell:
        """
        touch {output}
        echo {params.tag} > {output}
        """


来源:https://stackoverflow.com/questions/48174374/snakemake-how-to-save-and-access-sample-details-in-config-yml-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!