nextflow.py 2.68 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from string import Template
import os.path
import os

analysis_template = Template ('''
process ${id} {
    input:
    file fasta from for_${id}

    output:
    file "$${fasta}.${id}.results" into ${id}_results

    script:
    """
    ${analysis_script} --fasta $$fasta --output $${fasta}.${id}.results ${params}
    """
}
''')
convert_template = Template ('''
process convert_${id}_to_json {
    input:
    file result from ${id}_results

    output:
    file "$${result}.json" into ${id}_json

    script:
    """
    ${converter_script} --result $$result --output $${result}.json
    """
}
''')
input_template = Template('''    file ${id}_result from ${id}_json''')
join_jsons_template = Template('''
process join_documents {

    input:
    ${inputs}

    output:
    file "joined.json" into joined_json

    script:
    """
    join_json_files.py --output joined.json *.json
    """
}
''')
split_jsons_template = Template('''
process split_documents {

    publishDir "${output}", mode: 'copy'

    input:
    file "input/json.json" from joined_json

    output:
    file "*.json" into result_documents

    script:
    """
    split_json_into_separate_files.py --json 'input/json.json' --output .
    """
}
''')

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)
    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):
82
    old_cwd = os.getcwd()
83
    os.chdir(execution['directory'])
84
85
    os.system('nextflow run ' + execution['directory'] + '/main.nf --fasta ' + execution['fasta'] + ' --output ' + execution['output'])
    os.chdir(old_cwd)
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

def generate_nextflow_script(execution):
    modules = execution['modules']

    fragments = []
    fragments.append('''params.fasta = "example/proteins.fas"
Channel.fromPath(params.fasta).set{fasta}''')
    target_channels = ["for_"+m['id'] for m in modules]
    fragments.append('fasta.into{'+';'.join(target_channels)+';}')

    for m in modules:
        fragments.append(analysis_template.substitute(m))
        fragments.append(convert_template.substitute(m))

    json_inputs = []
    for m in modules:
        json_inputs.append(input_template.substitute(m))

    fragments.append(join_jsons_template.substitute({'inputs': '\n'.join(json_inputs)}))
    fragments.append(split_jsons_template.substitute(execution))
        
    nextflow_script = '\n'.join(fragments)
    return nextflow_script