Page MenuHomec4science

hello_world_pipeline.snake
No OneTemporary

File Metadata

Created
Wed, Aug 28, 07:13

hello_world_pipeline.snake

##########################################
## Basic hello world Snakemake pipeline ##
##########################################
#
# @author: Balazs Laurenczy
# @date: 2017
########
# INIT #
########
import os, logging
from timeit import default_timer as t
from datetime import datetime
# set up logging
logging.basicConfig(level = logging.INFO, format = '#%(asctime)s | %(levelname)-7s | %(filename)-20s: %(message)s')
logging.info('Starting Snakemake pipeline ...')
# get config parameters
N_FILES = int(config.get('n', 4))
OUT_FOLDER = config.get('output_folder', 'output')
if OUT_FOLDER[:-1] != '/': OUT_FOLDER += '/' # add ending slash to folder path
REF_FOLDER = config.get('ref_folder', 'ref_output')
if REF_FOLDER[:-1] != '/': REF_FOLDER += '/' # add ending slash to folder path
#########
# RULES #
#########
rule all:
input:
expand(OUT_FOLDER + 'helloworld_{w}.log', w = range(N_FILES))
run:
logging.info('Pipeline completed.')
rule write_hello:
output:
OUT_FOLDER + 'hello_{w}.log'
shell:
'echo "hello{wildcards.w}" > {output}'
rule write_world:
output:
OUT_FOLDER + 'world_{w}.log'
shell:
'echo "world{wildcards.w}" > {output}'
rule write_hello_world:
input:
hello = OUT_FOLDER + 'hello_{w}.log',
world = OUT_FOLDER + 'world_{w}.log'
output:
OUT_FOLDER + 'helloworld_{w}.log'
shell:
'cat {input.hello} {input.world} > {output}'
onsuccess:
logging.info('Pipeline completed successfully.')
if not os.path.exists(REF_FOLDER):
exit('Cannot check validity of pipeline as there is no reference folder at path "{}". Aborting.'.format(REF_FOLDER))
# test if output folders are identical
shell('cd {OUT_FOLDER} && find . -regex ".*.log" -type f -exec md5sum {{}} \; | sort -k2 > content.md5')
try:
shell('diff {OUT_FOLDER}content.md5 {REF_FOLDER}content.md5')
logging.info('Pipeline is valid.'.format(REF_FOLDER))
except Exception:
logging.error('Some differences (see above) exist between the current pipeline\'s output ' +
'and the reference output at path "{}". Pipeline is *not* valid.'.format(REF_FOLDER))
onerror:
logging.info('Pipeline failed.')

Event Timeline