简体   繁体   中英

How to access Snakemake parameters inside Snakefile?

I'm trying to figure out how to access the Snakemake parameters inside my Snakefile, eg the set target rule (either specified by user or first defined rule). I was able to find some settings in the environment using dir() , but not the target rule. Is that even accessible?

EDIT : I would like to print an overview of the most important parameters at the beginning of the run, including the user ID, path to config file, and also the target rule (since our workflow has multiple endpoints/target rules, I find it useful to have it logged somewhere).

After some more digging, I found the workflow object that contains some useful information, however the first_rule is set to None . I would've expected that property to be set to the target rule but perhaps I just haven't understood the purpose of it yet, and/or no information should be extracted from the workflow object.

I know that I can use the --report feature to get a lot of information - and we do use it - but would like to be independent from it.

Thank you!

From the updated question it does seem like workflow could be useful. Specifically, one option is to iterate over all the rules in a workflow (see related answer ), extracting information of interest during the iterations. Specifically for the target rule information, running snakemake -j 1 on the following Snakefile seems to output the required information:

rule all:
    input:
        "a.txt",
        "b.txt",


rule a:
    output:
        "a.txt",
    shell:
        """
        echo "a" > {output}
        """


rule b:
    output:
        "b.txt",
    default_target: True,
    shell:
        """
        echo "b" > {output}
        """

print(workflow.default_target)
# will print b

One caution in terms of printing , if you are doing cluster submission or your rules have shell commands that are sensitive to incoming data, it might be better to log information into a file vs. printing to stdout. At least I had experience with some non-obvious bugs that eventually could be traced to me printing in the Snakefile.

Update: this is a hack for an edge case, but in case one is interested in modifying dynamically the default_target , it might be possible to do something like below (this is only pseudocode, not tested):

# assume that config contains the name of the desired target rule
default_target = config["default_target"]

for n, r in enumerate(workflow.rules):
    if r.name == default_target:
        r.default_target = True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM