Commit 2c4798ad authored by jlopez's avatar jlopez
Browse files

Add generation workflow snakefile

parent fb1c6549
#!/usr/bin/python3
# This script will take an workflow and generate snakefile
# Accepts 1 parameter:
# Usage: ./generate_workflow_snakefile.py workflow_name
from tools import *
import sys
PATH_OUTPUT = "./"
PATH_INPUT = "./workflows/"
def generate(name):
result = "# File generate with generate_workflow_snakefile.py\n\n"
yaml = read_yaml(PATH_INPUT+name+"/"+name+".yaml")
# imports
result += "import os\n"
result += "import re\n"
result += "import snakemake.utils\n"
result += "import csv\n"
result += "\n"
# Wildcards
result += "#############\n"
result += "# Wildcards #\n"
result += "#############\n"
result += "\n"
result += "SAMPLES = config[\"samples\"]\n"
result += "STEPS = config[\"steps\"]\n"
result += "PREPARE_REPORT_OUTPUTS = config[\"prepare_report_outputs\"]\n"
result += "PREPARE_REPORT_SCRIPTS = config[\"prepare_report_scripts\"]\n"
result += "OUTPUTS = config[\"outputs\"]\n"
result += "PARAMS_INFO = config[\"params_info\"]\n"
result += "config = config[\"params\"]\n"
result += "\n"
# intputs
result += "##########\n"
result += "# Inputs #\n"
result += "##########\n"
result += "\n"
result += "# Generic input functions\n"
result += "## get raw_reads\n"
result += "def raw_reads():\n"
result += "\tinputs = dict()\n"
result += "\tif (config[\"SeOrPe\"] == \"PE\"):\n"
result += "\t\tinputs[\"read\"] = config['sample_dir']+'/{sample}_R1'+config[\"sample_suffix\"]\n"
result += "\t\tinputs[\"read2\"] = config['sample_dir']+'/{sample}_R2'+config[\"sample_suffix\"]\n"
result += "\telse:\n"
result += "\t\tinputs[\"read\"] = config['sample_dir']+'/{sample}'+config[\"sample_suffix\"]\n"
result += "\treturn inputs\n"
result += "\n"
result += "# Tools inputs functions\n"
if "steps" in yaml:
for step in yaml["steps"]:
if "tools" in step:
for tool in step["tools"]:
if tool != "null":
result += "def " + tool + "_inputs():\n"
result += "\tinputs = dict()\n"
result += "\t# TODO Complete inputs\n"
result += "\t# inputs = raw_reads()\n"
result += "\treturn inputs\n"
result += "\n"
result += "\n"
result += "{import global_functions}\n"
result += "\n"
# outputs
result += "###########\n"
result += "# Outputs #\n"
result += "###########\n"
result += "\n"
result += "def step_outputs(step):\n"
result += "\toutputs = list()\n"
if "steps" in yaml:
for step in yaml["steps"]:
if "name" in step:
result += "\tif (step == \"" + step["name"] + "\"):\n"
result += "\t\t# TODO Complete step\n"
result += "\t\tpass\n"
result += "\t\t\n"
result += "\tif (step == \"all\"):\n"
result += "\t\toutputs = list(rules.multiqc.output)\n"
result += "\n"
result += "\treturn outputs\n"
result += "\n"
result += "# get outputs for each choosen tools\n"
result += "def workflow_outputs(step):\n"
result += "\toutputs = list()\n"
result += "\toutputs.extend(step_outputs(step))\n"
result += "\treturn outputs\n"
result += "\n"
# rules
result += "#########\n"
result += "# Rules #\n"
result += "#########\n"
result += "\n"
result += "{import rules}\n"
result += "{import global_rules}\n"
# print(result)
path_snakefile = PATH_INPUT + "/" + name + "/" + name + ".snakefile"
f = open(path_snakefile, "w")
f.write(result)
f.close()
def main():
if len(sys.argv) == 2:
generate(sys.argv[1])
else:
exit("""Needs 1 argument: workflow_name
Usage: ./generate_workflow_snakefile.py workflow_name """)
if __name__ == "__main__":
# execute only if run as a script
main()
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment