-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNextFlowController.java
More file actions
353 lines (317 loc) · 14.1 KB
/
NextFlowController.java
File metadata and controls
353 lines (317 loc) · 14.1 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package org.labkey.nextflow;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Logger;
import org.labkey.api.action.ApiResponse;
import org.labkey.api.action.ApiSimpleResponse;
import org.labkey.api.action.FormViewAction;
import org.labkey.api.action.MutatingApiAction;
import org.labkey.api.action.SpringActionController;
import org.labkey.api.admin.AdminUrls;
import org.labkey.api.data.PropertyManager;
import org.labkey.api.data.PropertyStore;
import org.labkey.api.pipeline.PipeRoot;
import org.labkey.api.pipeline.PipelineProvider;
import org.labkey.api.pipeline.PipelineService;
import org.labkey.api.pipeline.PipelineStatusUrls;
import org.labkey.api.pipeline.browse.PipelinePathForm;
import org.labkey.api.security.AdminConsoleAction;
import org.labkey.api.security.RequiresPermission;
import org.labkey.api.security.permissions.AdminOperationsPermission;
import org.labkey.api.security.permissions.InsertPermission;
import org.labkey.api.security.permissions.ReadPermission;
import org.labkey.api.security.permissions.SiteAdminPermission;
import org.labkey.api.util.Button;
import org.labkey.api.util.DOM;
import org.labkey.api.util.FileUtil;
import org.labkey.api.util.HtmlString;
import org.labkey.api.util.PageFlowUtil;
import org.labkey.api.util.Path;
import org.labkey.api.util.URLHelper;
import org.labkey.api.util.element.Select;
import org.labkey.api.util.logging.LogHelper;
import org.labkey.api.view.HtmlView;
import org.labkey.api.view.JspView;
import org.labkey.api.view.NavTree;
import org.labkey.api.view.UnauthorizedException;
import org.labkey.api.view.ViewBackgroundInfo;
import org.labkey.nextflow.pipeline.NextFlowPipelineJob;
import org.labkey.nextflow.pipeline.NextFlowProtocol;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import static org.labkey.api.util.DOM.Attribute.checked;
import static org.labkey.api.util.DOM.Attribute.hidden;
import static org.labkey.api.util.DOM.Attribute.method;
import static org.labkey.api.util.DOM.Attribute.name;
import static org.labkey.api.util.DOM.Attribute.type;
import static org.labkey.api.util.DOM.Attribute.value;
import static org.labkey.api.util.DOM.DIV;
import static org.labkey.api.util.DOM.INPUT;
import static org.labkey.api.util.DOM.LK.FORM;
import static org.labkey.api.util.DOM.UL;
import static org.labkey.api.util.DOM.at;
import static org.labkey.nextflow.NextFlowManager.NEXTFLOW_CONFIG;
public class NextFlowController extends SpringActionController
{
private static final DefaultActionResolver _actionResolver = new DefaultActionResolver(NextFlowController.class);
public static final String NAME = "nextflow";
protected static final Logger LOG = LogHelper.getLogger(NextFlowPipelineJob.class, "LabKey UI and API for NextFlow usage");
public NextFlowController()
{
setActionResolver(_actionResolver);
}
@RequiresPermission(SiteAdminPermission.class)
public static class DeleteNextFlowConfigurationAction extends MutatingApiAction<Object>
{
@Override
public ApiResponse execute(Object form, BindException errors)
{
if (!getContainer().isRoot())
{
throw new UnauthorizedException();
}
PropertyStore store = PropertyManager.getEncryptedStore();
store.deletePropertySet(NEXTFLOW_CONFIG);
return new ApiSimpleResponse("success", true);
}
}
@AdminConsoleAction
@RequiresPermission(AdminOperationsPermission.class)
public static class NextFlowConfigurationAction extends FormViewAction<NextFlowConfiguration>
{
@Override
public void validateCommand(NextFlowConfiguration target, Errors errors)
{
}
@Override
public ModelAndView getView(NextFlowConfiguration newConfig, boolean reshow, BindException errors)
{
NextFlowConfiguration existingConfig = NextFlowManager.get().getConfiguration();
if (existingConfig != null)
{
if (StringUtils.isEmpty(newConfig.getNextFlowConfigFilePath()))
{
newConfig.setNextFlowConfigFilePath(existingConfig.getNextFlowConfigFilePath());
}
if (StringUtils.isEmpty(newConfig.getAccountName()))
{
newConfig.setAccountName(existingConfig.getAccountName());
}
if (StringUtils.isEmpty(newConfig.getIdentity()))
{
newConfig.setIdentity(existingConfig.getIdentity());
}
if (StringUtils.isEmpty(newConfig.getCredential()))
{
newConfig.setCredential(existingConfig.getCredential());
}
if (StringUtils.isEmpty(newConfig.getS3BucketPath()))
{
newConfig.setS3BucketPath(existingConfig.getS3BucketPath());
}
if (StringUtils.isEmpty(newConfig.getApiKey()))
{
newConfig.setApiKey(existingConfig.getApiKey());
}
}
return new JspView<>("/org/labkey/nextflow/nextFlowConfiguration.jsp", newConfig, errors);
}
@Override
public boolean handlePost(NextFlowConfiguration newConfig, BindException errors)
{
NextFlowConfiguration existingConfig = NextFlowManager.get().getConfiguration();
if (existingConfig != null)
{
if (StringUtils.isEmpty(newConfig.getApiKey()))
{
newConfig.setApiKey(existingConfig.getApiKey());
}
if (StringUtils.isEmpty(newConfig.getCredential()))
{
newConfig.setCredential(existingConfig.getCredential());
}
}
NextFlowManager.get().saveConfig(newConfig, errors);
return !errors.hasErrors();
}
@Override
public URLHelper getSuccessURL(NextFlowConfiguration nextFlowConfiguration)
{
return PageFlowUtil.urlProvider(AdminUrls.class).getAdminConsoleURL();
}
@Override
public void addNavTrail(NavTree root)
{
root.addChild("Admin Console", PageFlowUtil.urlProvider(AdminUrls.class).getAdminConsoleURL());
root.addChild("Configure NextFlow");
}
}
public static class EnabledForm
{
Boolean _enabled;
public Boolean getEnabled()
{
return _enabled;
}
public void setEnabled(Boolean enabled)
{
_enabled = enabled;
}
}
@RequiresPermission(ReadPermission.class)
public static class BeginAction extends FormViewAction<EnabledForm>
{
@Override
public void validateCommand(EnabledForm target, Errors errors)
{
}
@Override
public ModelAndView getView(EnabledForm form, boolean reshow, BindException errors)
{
if (getUser().hasSiteAdminPermission())
{
Boolean status = NextFlowManager.get().getEnabledState(getContainer());
boolean inheritedStatus = NextFlowManager.get().isEnabled(getContainer().getParent());
return new HtmlView("Enable or Disable NextFlow",
FORM(at(method, "POST"),
DIV(INPUT(at(type, "radio", name, "enabled", value, Boolean.TRUE.toString(), (status == Boolean.TRUE ? checked : null), null)),
"Enabled"),
DIV(INPUT(at(type, "radio", name, "enabled", value, Boolean.FALSE.toString(), (status == Boolean.FALSE ? checked : null), null)),
"Disabled"),
DIV(INPUT(at(type, "radio", name, "enabled", value, "", (status == null ? checked : null), null)),
getContainer().isRoot() ?
"Unset" :
"Inherited from " + getContainer().getParent().getPath() + " (currently " + (inheritedStatus ? "enabled" : "disabled") + ")"),
new Button.ButtonBuilder("Save").submit(true).build(), " ",
new Button.ButtonBuilder("Cancel").href(getContainer().getStartURL(getUser())).build()));
}
else
{
return new HtmlView("NextFlow Integration Status",
DIV("NextFlow integration is " + (NextFlowManager.get().isEnabled(getContainer()) ? "enabled" : "disabled") + " in this " + (getContainer().isProject() ? "project" : "folder") + ".")
);
}
}
@Override
public boolean handlePost(EnabledForm form, BindException errors)
{
NextFlowManager.get().saveEnabledState(getContainer(), form.getEnabled());
return true;
}
@Override
public void addNavTrail(NavTree root)
{
root.addChild("NextFlow Integration Status");
}
@Override
public URLHelper getSuccessURL(EnabledForm o)
{
return getContainer().getStartURL(getUser());
}
}
@Getter @Setter
public static class AnalyzeForm extends PipelinePathForm
{
private boolean launch = false;
private String configFile;
}
@RequiresPermission(InsertPermission.class)
public class NextFlowRunAction extends FormViewAction<AnalyzeForm>
{
@Override
public void validateCommand(AnalyzeForm o, Errors errors)
{
if (!NextFlowManager.get().isEnabled(getContainer()))
{
errors.reject(ERROR_MSG, "NextFlow is not enabled");
}
}
@Override
public ModelAndView getView(AnalyzeForm o, boolean b, BindException errors)
{
List<File> selectedFiles = o.getValidatedFiles(getContainer(), false);
if (selectedFiles.isEmpty())
{
return new HtmlView(HtmlString.of("Couldn't find input file(s)"));
}
// NextFlow operates on the full directory so show the list to the user, regardless of what they selected
// from the file listing
File inputDir = selectedFiles.get(0).getParentFile();
File[] inputFiles = inputDir.listFiles(new PipelineProvider.FileTypesEntryFilter(NextFlowProtocol.INPUT_TYPES));
if (inputFiles == null || inputFiles.length == 0)
{
return new HtmlView(HtmlString.of("Couldn't find input file(s)"));
}
NextFlowConfiguration config = NextFlowManager.get().getConfiguration();
if (config.getNextFlowConfigFilePath() != null)
{
File configDir = new File(config.getNextFlowConfigFilePath());
if (configDir.isDirectory())
{
File[] configFiles = configDir.listFiles();
if (configFiles != null && configFiles.length > 0)
{
return new HtmlView("NextFlow Runner", DIV(
FORM(at(method, "POST"),
INPUT(at(hidden, true, name, "launch", value, true)),
Arrays.stream(o.getFile()).map(f -> INPUT(at(hidden, true, name, "file", value, f))).toList(),
"Files: ",
UL(Arrays.stream(inputFiles).map(File::getName).map(DOM::LI)),
"Config: ",
new Select.SelectBuilder().name("configFile").addOptions(Arrays.stream(configFiles).filter(f -> f.isFile() && f.getName().toLowerCase().endsWith(".config")).map(File::getName).sorted(String.CASE_INSENSITIVE_ORDER).toList()).build(),
DOM.BR(),
new Button.ButtonBuilder("Start NextFlow").submit(true).build())));
}
}
}
return new HtmlView(HtmlString.of("Couldn't find NextFlow config file(s)"));
}
@Override
public boolean handlePost(AnalyzeForm form, BindException errors) throws Exception
{
if (!form.isLaunch())
{
return false;
}
NextFlowConfiguration config = NextFlowManager.get().getConfiguration();
File configDir = new File(config.getNextFlowConfigFilePath());
File configFile = FileUtil.appendPath(configDir, Path.parse(form.getConfigFile()));
if (!configFile.exists())
{
errors.reject(ERROR_MSG, "Config file does not exist");
}
else
{
List<File> inputFiles = form.getValidatedFiles(getContainer());
if (inputFiles.isEmpty())
{
errors.reject(ERROR_MSG, "No input files");
}
else
{
ViewBackgroundInfo info = getViewBackgroundInfo();
PipeRoot root = PipelineService.get().findPipelineRoot(info.getContainer());
NextFlowPipelineJob job = NextFlowPipelineJob.create(info, root, configFile.toPath(), inputFiles.stream().map(File::toPath).toList());
PipelineService.get().queueJob(job);
LOG.info("NextFlow job queued: {}", job.getJsonJobInfo(false));
}
}
return !errors.hasErrors();
}
@Override
public URLHelper getSuccessURL(AnalyzeForm o)
{
return PageFlowUtil.urlProvider(PipelineStatusUrls.class).urlBegin(getContainer());
}
@Override
public void addNavTrail(NavTree navTree)
{
navTree.addChild("NextFlow Runner");
}
}
}