-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNextFlowRunTask.java
More file actions
251 lines (223 loc) · 8.37 KB
/
NextFlowRunTask.java
File metadata and controls
251 lines (223 loc) · 8.37 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
package org.labkey.nextflow.pipeline;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.labkey.api.data.ContainerManager;
import org.labkey.api.data.DbSequence;
import org.labkey.api.data.DbSequenceManager;
import org.labkey.api.exp.XarFormatException;
import org.labkey.api.pipeline.AbstractTaskFactory;
import org.labkey.api.pipeline.AbstractTaskFactorySettings;
import org.labkey.api.pipeline.PipelineJob;
import org.labkey.api.pipeline.PipelineJobException;
import org.labkey.api.pipeline.PipelineValidationException;
import org.labkey.api.pipeline.RecordedAction;
import org.labkey.api.pipeline.RecordedActionSet;
import org.labkey.api.pipeline.WorkDirectoryTask;
import org.labkey.api.security.SecurityManager;
import org.labkey.api.targetedms.TargetedMSService;
import org.labkey.api.util.FileType;
import org.labkey.nextflow.NextFlowConfiguration;
import org.labkey.nextflow.NextFlowManager;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;
public class NextFlowRunTask extends WorkDirectoryTask<NextFlowRunTask.Factory>
{
public static final String SPECTRA_INPUT_ROLE = "Spectra";
public static final String ACTION_NAME = "NextFlow";
private static final DbSequence INVOCATION_SEQUENCE = DbSequenceManager.get(ContainerManager.getRoot(), NextFlowRunTask.class.getName());
public NextFlowRunTask(Factory factory, PipelineJob job)
{
super(factory, job);
}
@Override
public @NotNull RecordedActionSet run() throws PipelineJobException
{
Logger log = getJob().getLogger();
// NextFlow requires a unique job name for every execution. Increment a counter to append as a suffix to
// ensure uniqueness
long invocationCount = INVOCATION_SEQUENCE.next();
INVOCATION_SEQUENCE.sync();
NextFlowPipelineJob.LOG.info("Starting to execute NextFlow: {}", getJob().getJsonJobInfo(invocationCount));
SecurityManager.TransformSession session = null;
boolean success = false;
try
{
NextFlowConfiguration config = NextFlowManager.get().getConfiguration();
if (config == null)
{
throw new PipelineJobException("No NextFlow configuration found");
}
// Use the configured API key if set
String apiKey = config.getApiKey();
if (apiKey == null)
{
session = SecurityManager.createTransformSession(getJob().getUser());
apiKey = session.getApiKey();
}
// Need to pass to the main process directly in the future to allow concurrent execution for different users
ProcessBuilder secretsPB = new ProcessBuilder("nextflow", "secrets", "set", "PANORAMA_API_KEY", apiKey);
log.info("Setting secrets");
File dir = getJob().getLogFile().getParentFile();
getJob().runSubProcess(secretsPB, dir);
ProcessBuilder executionPB = new ProcessBuilder(getArgs(invocationCount));
getJob().runSubProcess(executionPB, dir);
log.info("Job Finished");
NextFlowPipelineJob.LOG.info("Finished executing NextFlow: {}", getJob().getJsonJobInfo(invocationCount));
RecordedAction action = new RecordedAction(ACTION_NAME);
for (Path inputFile : getJob().getInputFilePaths())
{
action.addInput(inputFile.toFile(), SPECTRA_INPUT_ROLE);
}
addOutputs(action, getJob().getLogFilePath().getParent().resolve("reports"), log);
addOutputs(action, getJob().getLogFilePath().getParent().resolve("results"), log);
success = true;
return new RecordedActionSet(action);
}
catch (IOException e)
{
throw new PipelineJobException(e);
}
finally
{
if (session != null)
{
session.close();
}
if (!success)
{
NextFlowPipelineJob.LOG.info("Failed executing NextFlow: {}", getJob().getJsonJobInfo(invocationCount));
}
}
}
private void addOutputs(RecordedAction action, Path path, Logger log) throws IOException
{
// Skip results.sky.zip files - it's the template document. We want the file output doc that includes
// the replicate analysis
if (Files.isRegularFile(path) && !path.endsWith("results.sky.zip"))
{
action.addOutput(path.toFile(), "Output", false);
if (path.toString().toLowerCase().endsWith(".sky.zip"))
{
try
{
log.info("Queueing import for {}", path);
// Make sure that the TargetedMS runs get wrapped with their experiment run counterparts
TargetedMSService.get().importSkylineDocument(getJob().getInfo(), path);
}
catch (XarFormatException | PipelineValidationException e)
{
log.error("Error queuing import of Skyline document", e);
}
}
}
else if (Files.isDirectory(path))
{
try (Stream<Path> listing = Files.list(path))
{
for (Path child : listing.toList())
{
addOutputs(action, child, log);
}
}
}
}
private boolean hasAwsSection(Path configFile) throws PipelineJobException
{
try (InputStream in = Files.newInputStream(configFile);
InputStreamReader isReader = new InputStreamReader(in, StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(isReader))
{
String line;
while ((line = reader.readLine()) != null)
{
line = line.trim();
// Ignore comments
if (!line.startsWith("//"))
{
if (line.startsWith("aws"))
{
return true;
}
}
}
return false;
}
catch (IOException e)
{
throw new PipelineJobException(e);
}
}
private @NotNull List<String> getArgs(long invocationCount) throws PipelineJobException
{
NextFlowConfiguration config = NextFlowManager.get().getConfiguration();
Path configFile = getJob().getConfig();
boolean aws = hasAwsSection(configFile);
List<String> args = new ArrayList<>(Arrays.asList("nextflow", "run", "-resume", "-r", "main"));
if (aws)
{
args.add("-profile");
args.add("aws");
}
args.add("mriffle/nf-skyline-dia-ms");
if (aws)
{
String s3BucketPath = config.getS3BucketPath();
String s3Path = "s3://" + s3BucketPath;
args.add("-bucket-dir");
args.add(s3Path);
}
args.add("-c");
args.add(configFile.toAbsolutePath().toString());
args.add("-name");
args.add(getJob().getNextFlowRunName(invocationCount));
return args;
}
@Override
public NextFlowPipelineJob getJob()
{
return (NextFlowPipelineJob) super.getJob();
}
public static class Factory extends AbstractTaskFactory<AbstractTaskFactorySettings, Factory>
{
public Factory()
{
super(NextFlowRunTask.class);
}
@Override
public NextFlowRunTask createTask(PipelineJob job)
{
return new NextFlowRunTask(this, job);
}
@Override
public List<FileType> getInputTypes()
{
return Collections.emptyList();
}
@Override
public List<String> getProtocolActionNames()
{
return List.of(ACTION_NAME);
}
@Override
public String getStatusName()
{
return "NextFlow Run";
}
@Override
public boolean isJobComplete(PipelineJob job)
{
return false;
}
}
}