Commit 23f7e3bd authored by Lukas Jelonek's avatar Lukas Jelonek
Browse files

Implemented nextflow script generation and execution

parent 54e269f0
#!/usr/bin/env python3
import argparse
import os
from config import load_config
config = load_config()
import copy
def main():
parser = argparse.ArgumentParser(description='Make bioinformatic observations on aminoacid sequences')
......@@ -17,41 +17,73 @@ def main():
analyze_parser.add_argument('--fasta', '-f', required=True, help='A fasta file with aminoacid sequences')
analyze_parser.add_argument('--output', '-o', required=True, help='The output directory for the json documents')
analyze_parser.add_argument('--profile', '-p', default='fast', help='The profile to use')
analyze_parser.add_argument('--live', '-l', action='store_true', help='Report results as they are computed, not only at the end of the computation')
analyze_parser.add_argument('--config', '-c', help='The config to use')
analyze_parser.set_defaults(func=analyze)
args = parser.parse_args()
args.func(args)
def info(args):
show_analyses()
def analyze(args):
print()
modules = get_modules_for_profile(args.profile)
generate_nextflow_script(modules)
config = load_config()
args.func(args, config)
def info(args, config):
show_analyses(config)
def analyze(args, config):
execution = generate_execution(config, args)
setup_execution_directory(execution)
execute_analysis(execution)
def generate_execution(config, args):
execution = {}
execution['mode'] = 'live' if args.live else 'complete'
execution['bin_path'] = config['app']['bin_path']
execution['fasta'] = os.path.abspath(args.fasta)
execution['output'] = os.path.abspath(args.output)
execution['directory'] = 'tmp' # TODO replace by random tmp directory or make it configurable (cli option)
modules = generate_execution_modules_for_profile(config, args.profile)
execution['modules'] = modules
return execution
def setup_execution_directory(execution):
directory = execution['directory']
if not os.path.exists(directory):
os.mkdir(directory)
if not os.path.isdir(directory):
exit()
nextflow_script = generate_nextflow_script(execution['modules'])
with open(directory + '/main.nf', 'w') as script_file:
script_file.write(nextflow_script)
if not os.path.exists(directory+'/bin'):
os.symlink(execution['bin_path'], directory + '/bin')
def execute_analysis(execution):
os.chdir(execution['directory'])
os.system('nextflow run main.nf --fasta ' + execution['fasta'] + ' --output ' + execution['output'])
def get_modules_for_profile(profile):
def generate_execution_modules_for_profile(config, profile):
# find profile by name
p = [x for x in config['profiles'] if x['name'] == profile][0]
# find all modules for profile from modules config section
m = [x for x in config['modules'] if x['name'] in p['modules']]
import copy
modules = copy.deepcopy(m)
# generate unique ids for module
# generate unique ids for each module
for module in modules:
module['id'] = module['name']
return modules
def generate_nextflow_script(modules):
print('''params.file = "example/proteins.fas"
Channel.fromPath(param.file).set{fasta}''')
fragments = []
fragments.append('''params.fasta = "example/proteins.fas"
Channel.fromPath(params.fasta).set{fasta}''')
target_channels = ["for_"+m['id'] for m in modules]
print('fasta.join{'+';'.join(target_channels)+'}')
fragments.append('fasta.into{'+';'.join(target_channels)+';}')
from string import Template
# TODO add parameters to analysis_script
analysis_template = Template ('''
process ${id} {
input:
......@@ -81,10 +113,14 @@ process convert_${id}_to_json {
}
''')
for m in modules:
print(analysis_template.substitute(m))
print(convert_template.substitute(m))
fragments.append(analysis_template.substitute(m))
fragments.append(convert_template.substitute(m))
# TODO add postprocessing
nextflow_script = '\n'.join(fragments)
return nextflow_script
def show_analyses():
def show_analyses(config):
print('Profiles:')
for profile in config['profiles']:
print(' {0:<20} - {1}'.format(profile['name'], profile['info']))
......
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