Tutorial: How to make your snakefiles self-documenting
5
gravatar for Botond Sipos
3.4 years ago by
Botond Sipos1.7k
United Kingdom
Botond Sipos1.7k wrote:

I wrote a gist about how to make snakefiles self-documenting:

# This snippet add the "self-documenting" feature to snakemake files. 
# It is inspired by the Makefile in https://github.com/audreyr/cookiecutter-pypackage

# Add the follwing to your snakefile or include: "self_document.snake"

import re
def generate_help(sfile):
    """Parse out target and help message from file."""
    handler = open(sfile, "r")
    for line in handler:
        match = re.match(r'^rule\s+([a-zA-Z_-]+):.*?## (.*)$$', line)
        if match:
            target, help = match.groups()
            print("%-20s %s" % (target, help))

rule help: ## print list of all targets with help
    input:
        workflow.included
    run:
        print("--------------------------------------------")
        [generate_help(sfile) for sfile in input]
        print("--------------------------------------------")

# Then simply add a comment to your rule line preceded by '##'. Invoking snakemake help will print all targets and their descriptions.
snakemake tutorial • 2.7k views
ADD COMMENTlink modified 3.1 years ago by Eric Lim1.6k • written 3.4 years ago by Botond Sipos1.7k
5
gravatar for Eric Lim
3.4 years ago by
Eric Lim1.6k
Stoke Therapeutics, Inc
Eric Lim1.6k wrote:

Thanks for posting. This can also be accomplished using native snakemake's objects, workflow and rule with docstring.

rule help:
  """
  Print list of all targets with help.
  """
  run:
    for rule in workflow.rules:
      print( rule.name )
      print( rule.docstring )

rule do_something:
  """
  Do something.
  """
  run:
    pass

You can then write a function to prettify the outputs.

ADD COMMENTlink modified 13 months ago by WouterDeCoster43k • written 3.4 years ago by Eric Lim1.6k
1

Yeah, this might be a better way to do it :)

ADD REPLYlink written 3.4 years ago by Botond Sipos1.7k
1
gravatar for Eric Lim
3.1 years ago by
Eric Lim1.6k
Stoke Therapeutics, Inc
Eric Lim1.6k wrote:

Not sure if this is a newly added functionality, but I just realized one can simply do snakemake -l or snakemake --lt to display docstrings for each rule.

This is better than what I've been doing:

rule print_help:
"""Print help menu.
"""
run:
    tbl = PrettyTable(['Rules', 'Description'])
    tbl.align = 'l'
    for rule in workflow.rules:
        if not rule.name.startswith('_'):
            tbl.add_row([rule.name, rule.docstring.splitlines()[0]])
    print(tbl)
ADD COMMENTlink written 3.1 years ago by Eric Lim1.6k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1686 users visited in the last hour