-
Notifications
You must be signed in to change notification settings - Fork 5
/
nextflow_include_command.py
149 lines (129 loc) · 4.83 KB
/
nextflow_include_command.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import re
from typing import Iterator
import sys
from pathlib import Path
import sublime
import sublime_plugin
regex_process = re.compile(r'^process +(\w+) *\{\s*$')
regex_functions = re.compile(r'^def +(\w+) *\([^\)]*\) *\{\s*$')
def find_processes(path: Path) -> Iterator[str]:
with open(path) as f:
for l in f:
m = regex_process.match(l)
if m:
yield m.group(1)
def find_functions(path: Path) -> Iterator[str]:
with open(path) as f:
for l in f:
m = regex_functions.match(l)
if m:
yield m.group(1)
def relative_path(script_path: Path, import_path: Path) -> str:
for i, parent_path in enumerate(script_path.parents):
try:
if i == 0:
return './' + str(import_path.relative_to(parent_path))
else:
return '../'*i + str(import_path.relative_to(parent_path))
except ValueError:
continue
return str(import_path)
class NextflowIncludeInsertProcessCommand(sublime_plugin.TextCommand):
def run(self, edit, **kwargs):
process = kwargs.get('process', None)
module_path = kwargs.get('module_path', None)
if process and module_path:
view = self.view
if len(view.selection) > 1:
return
region = view.selection[0]
point = region.a
row, col = view.rowcol(point)
if col != 0:
return
module_path = Path(module_path)
module_path = module_path.parent / module_path.stem
script_path = Path(view.file_name())
view.insert(edit, point, "include { " + process + " } from '" + relative_path(script_path, module_path) + "' addParams( options: modules['" + process.lower() + "'] )")
class NextflowIncludeProcessCommand(sublime_plugin.TextCommand):
def run(self, edit, **kwargs):
view = self.view
if len(view.selection) > 1:
return
region = view.selection[0]
point = region.a
row, col = view.rowcol(point)
if col != 0:
return
folders = view.window().folders()
if not view.file_name():
return
if not folders:
return
root_dir = Path(folders[0])
nf_files = [(proc, str(p.absolute())) for p in root_dir.rglob('*.nf') for proc in find_processes(p)]
def on_select(x: int):
if x == -1:
return
proc, nf_path = nf_files[x]
root = str(root_dir.absolute())
if nf_path.startswith(root):
view.run_command(
'nextflow_include_insert_process',
dict(process=proc,
module_path=nf_path
)
)
view.window().show_quick_panel(nf_files, on_select=on_select)
class NextflowIncludeInsertFunctionsCommand(sublime_plugin.TextCommand):
def run(self, edit, **kwargs):
funcs = kwargs.get('funcs', None)
module_path = kwargs.get('module_path', None)
if funcs and module_path:
view = self.view
if len(view.selection) > 1:
return
region = view.selection[0]
point = region.a
row, col = view.rowcol(point)
if col != 0:
return
module_path = Path(module_path)
module_path = module_path.parent / module_path.stem
script_path = Path(view.file_name())
view.insert(edit, point, "include { " + '; '.join(funcs) + " } from '" + relative_path(script_path, module_path) + "'")
class NextflowIncludeFunctionsCommand(sublime_plugin.TextCommand):
def run(self, edit, **kwargs):
view = self.view
if len(view.selection) > 1:
return
region = view.selection[0]
point = region.a
row, col = view.rowcol(point)
if col != 0:
return
folders = view.window().folders()
if not view.file_name():
return
if not folders:
return
root_dir = Path(folders[0])
nf_files = []
for p in root_dir.rglob('*.nf'):
abspath = str(p.absolute())
funcs = list(find_functions(p))
if funcs:
nf_files.append((funcs, abspath))
def on_select(x: int):
if x == -1:
return
funcs, nf_path = nf_files[x]
root = str(root_dir.absolute())
if nf_path.startswith(root):
view.run_command(
'nextflow_include_insert_functions',
dict(funcs=funcs,
module_path=nf_path
)
)
view.window().show_quick_panel([('; '.join(x), y) for x,y in nf_files], on_select=on_select)