forked from BimberLab/DiscvrLabKeyModules
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSequenceRemoteIntegrationTests.java
More file actions
258 lines (215 loc) · 8.8 KB
/
SequenceRemoteIntegrationTests.java
File metadata and controls
258 lines (215 loc) · 8.8 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
package org.labkey.sequenceanalysis;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;
import org.json.JSONObject;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.labkey.api.pipeline.PipelineJob;
import org.labkey.api.pipeline.PipelineJobService;
import org.labkey.api.pipeline.PipelineService;
import org.labkey.api.pipeline.TaskId;
import org.labkey.api.pipeline.WorkDirectory;
import org.labkey.api.reader.Readers;
import org.labkey.api.util.FileUtil;
import org.labkey.api.writer.PrintWriters;
import org.labkey.sequenceanalysis.pipeline.AlignmentInitTask;
import org.labkey.sequenceanalysis.pipeline.PrepareAlignerIndexesTask;
import org.labkey.sequenceanalysis.pipeline.SequenceAlignmentJob;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Objects;
import static org.labkey.api.sequenceanalysis.pipeline.SequencePipelineService.SEQUENCE_TOOLS_PARAM;
public class SequenceRemoteIntegrationTests extends SequenceIntegrationTests.AbstractAnalysisPipelineTestCase
{
private static final String PROJECT_NAME = "SequenceRemoteIntegrationTests";
@Before
@Override
public void setUp() throws Exception
{
super.setUp();
}
@BeforeClass
public static void initialSetUp()
{
doInitialSetUp(PROJECT_NAME);
}
private File setupConfigDir(File outDir) throws IOException
{
File baseDir = FileUtil.appendName(outDir, "config");
if (baseDir.exists())
{
FileUtils.deleteDirectory(baseDir);
}
FileUtil.mkdirs(baseDir);
if (_sampleData == null)
{
throw new IOException("_sampleData was null");
}
File source = FileUtil.appendName(_sampleData, "remotePipeline");
if (!source.exists())
{
throw new IOException("Unable to find file: " + source.getPath());
}
FileUtils.copyFile(FileUtil.appendName(source, "sequenceanalysisConfig.xml"), FileUtil.appendName(baseDir, "sequenceanalysisConfig.xml"));
try (PrintWriter writer = PrintWriters.getPrintWriter(FileUtil.appendName(baseDir, "pipelineConfig.xml")); BufferedReader reader = Readers.getReader(FileUtil.appendName(source, "pipelineConfig.xml")))
{
String line;
while ((line = reader.readLine()) != null)
{
if (line.contains("@@SEQUENCEANALYSIS_TOOLS@@"))
{
String path = PipelineJobService.get().getConfigProperties().getSoftwarePackagePath(SEQUENCE_TOOLS_PARAM);
if (StringUtils.trimToNull(path) == null)
{
path = PipelineJobService.get().getAppProperties().getToolsDirectory();
}
path = path.replaceAll("\\\\", "/");
line = line.replaceAll("@@SEQUENCEANALYSIS_TOOLS@@", path);
}
else if (line.contains("@@WORK_DIR@@"))
{
line = line.replaceAll("@@WORK_DIR@@", outDir.getPath().replaceAll("\\\\", "/"));
}
writer.println(line);
}
}
return baseDir;
}
@AfterClass
public static void cleanup()
{
doCleanup(PROJECT_NAME);
}
@Override
protected String getProjectName()
{
return PROJECT_NAME;
}
@Test
public void BasicRemoteJob() throws Exception
{
File outDir = FileUtil.appendName(getPipelineRoot(_project), "clusterBootstrap");
if (outDir.exists())
{
FileUtils.deleteDirectory(outDir);
}
FileUtil.mkdirs(outDir);
executeJobRemote(outDir, null);
try
{
// Not ideal. This job runs extremely quickly. When the folder cleanup happens, it seems that some process is holding on
// to the sequence file created in setup, so delete fails on that file. Search should be disabled, but there might be another FileWatcher.
// This delay is designed to let that thread catch up.
Thread.sleep(10000);
}
catch (InterruptedException e)
{
throw new RuntimeException(e.getMessage(), e);
}
}
@Test
public void RunBwaRemote() throws Exception
{
if (!isExternalPipelineEnabled())
return;
String jobName = "TestBWAMem_" + System.currentTimeMillis();
JSONObject config = substituteParams(FileUtil.appendName(_sampleData, ALIGNMENT_JOB), jobName);
config.put("alignment", "BWA-Mem");
appendSamplesForAlignment(config, _readsets);
SequenceAlignmentJob job = SequenceAlignmentJob.createForReadsets(_project, _context.getUser(), "RemoteJob1", "Test of remote pipeline", config, config.getJSONArray("readsetIds"), false).get(0);
File outDir = FileUtil.appendName(getPipelineRoot(_project), "remoteBwa");
if (outDir.exists())
{
FileUtils.deleteDirectory(outDir);
}
FileUtil.mkdirs(outDir);
FileUtil.mkdirs(job.getLogFile().getParentFile());
_readsets.forEach(rs -> job.getSequenceSupport().cacheReadset(rs));
//Force the init task to run
job.setActiveTaskId(new TaskId(AlignmentInitTask.class));
AlignmentInitTask task = (AlignmentInitTask)job.getActiveTaskFactory().createTask(job);
WorkDirectory wd = job.getActiveTaskFactory().createWorkDirectory(job.getJobGUID(), job, job.getLogger());
assertNotNull("Sequence support is null", job.getSequenceSupport());
assertEquals("Readsets not cached", _readsets.size(), job.getSequenceSupport().getCachedReadsets().size());
task.setWorkDirectory(wd);
task.run();
//Now move to remote tasks
job.setActiveTaskId(new TaskId(PrepareAlignerIndexesTask.class));
File jobFile = FileUtil.appendName(outDir, "bwaRemote.job.json.txt");
job.writeToFile(jobFile);
executeJobRemote(outDir, jobFile);
//check outputs
try
{
PipelineJob job2 = PipelineJob.readFromFile(jobFile);
Assert.assertEquals("Incorrect status", PipelineJob.TaskStatus.complete, job2.getActiveTaskStatus());
File workingFasta = job.getTargetGenome().getWorkingFastaFile();
Assert.assertNotNull("Genome FASTA not set", workingFasta);
File idx = new File(workingFasta.getPath() + ".fai");
Assert.assertTrue("FASTA index not created, expected: " + idx.getPath(), idx.exists());
}
catch (AssertionError e)
{
writeJobLogToLog(job);
_log.info("Files in job folder: " + job.getLogFile().getParentFile().getPath());
for (File f : Objects.requireNonNull(job.getLogFile().getParentFile().listFiles()))
{
_log.info(f.getName());
}
throw e;
}
}
protected void executeJobRemote(File workDir, @Nullable File jobJson) throws IOException
{
List<String> args = PipelineService.get().getClusterStartupArguments();
File configDir = setupConfigDir(workDir);
args.add("-configdir=" + configDir.getPath());
if (jobJson != null)
{
args.add(jobJson.toURI().toString());
}
ProcessBuilder pb = new ProcessBuilder(args);
pb.directory(workDir);
_log.info("Executing job in '{}': {}", pb.directory().getAbsolutePath(), String.join(" ", pb.command()));
Process proc;
try
{
pb.redirectErrorStream(true);
proc = pb.start();
File logFile = FileUtil.appendName(workDir, "clusterBootstrap.txt");
try (BufferedReader procReader = Readers.getReader(proc.getInputStream());PrintWriter writer = PrintWriters.getPrintWriter(logFile))
{
String line;
while ((line = procReader.readLine()) != null)
{
writer.println(line);
}
}
proc.waitFor();
if (proc.exitValue() != 0)
{
try (BufferedReader reader = Readers.getReader(logFile))
{
_log.error("Output from ClusterBootstrap:");
String line;
while ((line = reader.readLine()) != null)
{
_log.error(line);
}
}
}
Assert.assertEquals("Non-zero exit from ClusterBootstrap", 0, proc.exitValue());
}
catch (InterruptedException | IOException e)
{
throw new RuntimeException(e);
}
}
}