forked from BimberLab/DiscvrLabKeyModules
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSequenceJobSupportImpl.java
More file actions
386 lines (319 loc) · 12.4 KB
/
SequenceJobSupportImpl.java
File metadata and controls
386 lines (319 loc) · 12.4 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package org.labkey.sequenceanalysis.pipeline;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import htsjdk.samtools.util.IOUtil;
import org.junit.Assert;
import org.junit.Test;
import org.labkey.api.collections.IntHashMap;
import org.labkey.api.collections.LongHashMap;
import org.labkey.api.exp.api.ExpData;
import org.labkey.api.exp.api.ExperimentService;
import org.labkey.api.pipeline.PipelineJob;
import org.labkey.api.pipeline.PipelineJobException;
import org.labkey.api.security.User;
import org.labkey.api.sequenceanalysis.SequenceAnalysisService;
import org.labkey.api.sequenceanalysis.model.AnalysisModel;
import org.labkey.api.sequenceanalysis.model.Readset;
import org.labkey.api.sequenceanalysis.pipeline.ReferenceGenome;
import org.labkey.api.sequenceanalysis.pipeline.SequenceAnalysisJobSupport;
import org.labkey.sequenceanalysis.SequenceAnalysisServiceImpl;
import org.labkey.sequenceanalysis.SequenceReadsetImpl;
import org.labkey.sequenceanalysis.model.AnalysisModelImpl;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by bimber on 2/10/2015.
*/
public class SequenceJobSupportImpl implements SequenceAnalysisJobSupport, Serializable
{
private final Map<Long, File> _cachedFilePaths = new LongHashMap<>();
private final List<SequenceReadsetImpl> _cachedReadsets = new ArrayList<>();
private final Map<Long, AnalysisModel> _cachedAnalyses = new LongHashMap<>();
private final Map<Integer, ReferenceGenome> _cachedGenomes = new IntHashMap<>();
private final Map<String, Serializable> _cachedObjects = new HashMap<>();
private transient boolean _modifiedSinceSerialize = false;
private static final int TEMPORARY_GENOME = -1;
public SequenceJobSupportImpl()
{
}
@Override
public SequenceReadsetImpl getCachedReadset(Long rowId)
{
if (rowId == null || rowId < 1)
{
throw new IllegalArgumentException("Attempting to access cached readset without a valid ID: " + rowId);
}
for (SequenceReadsetImpl rs : _cachedReadsets)
{
if (rowId.equals(rs.getRowId()))
{
return rs;
}
}
return null;
}
@Override
public AnalysisModel getCachedAnalysis(long rowId)
{
return _cachedAnalyses.get(rowId);
}
@Override
public void cacheReadset(long readsetId, User u)
{
cacheReadset(readsetId,u, false);
}
@Override
public void cacheReadset(long readsetId, User u, boolean allowReadsetsWithArchivedData)
{
SequenceReadsetImpl rs = SequenceAnalysisServiceImpl.get().getReadset(readsetId, u);
if (rs != null)
{
cacheReadset(rs, allowReadsetsWithArchivedData);
}
else
{
throw new IllegalArgumentException("Unable to find readset with rowid: " + readsetId);
}
}
public void cacheReadset(SequenceReadsetImpl m)
{
cacheReadset(m, false);
}
public void markModified()
{
_modifiedSinceSerialize = true;
}
public boolean isModifiedSinceSerialize()
{
return _modifiedSinceSerialize;
}
protected void writeToDisk(File json) throws IOException
{
try (OutputStream output = IOUtil.maybeBufferOutputStream(IOUtil.openFileForWriting(json)))
{
ObjectMapper objectMapper = PipelineJob.createObjectMapper();
objectMapper.writeValue(output, this);
_modifiedSinceSerialize = false;
}
}
public void cacheReadset(SequenceReadsetImpl m, boolean allowReadsetsWithArchivedData)
{
if (!allowReadsetsWithArchivedData && m.hasArchivedData())
{
throw new IllegalArgumentException("Readset has archived data, cannot be used for pipeline jobs", new Exception());
}
markModified();
m.cacheForRemoteServer();
if (m.existsInDatabase())
{
if (getCachedReadset(m.getRowId()) != null)
{
_cachedReadsets.remove(getCachedReadset(m.getRowId()));
}
}
else
{
_cachedReadsets.remove(m);
}
_cachedReadsets.add(m);
}
public void cacheAnalysis(AnalysisModelImpl m, PipelineJob job)
{
cacheAnalysis(m, job, false);
}
public void cacheAnalysis(AnalysisModelImpl m, PipelineJob job, boolean allowReadsetsWithArchivedData)
{
markModified();
if (m.getAlignmentFile() != null)
{
cacheExpData(m.getAlignmentData());
}
if (m.getReadset() != null)
{
SequenceReadsetImpl rs = SequenceAnalysisServiceImpl.get().getReadset(m.getReadset(), job.getUser());
cacheReadset(rs, allowReadsetsWithArchivedData);
}
if (m.getLibraryId() != null)
{
try
{
ReferenceGenome rg = SequenceAnalysisService.get().getReferenceGenome(m.getLibraryId(), job.getUser());
cacheGenome(rg);
cacheExpData(ExperimentService.get().getExpData(rg.getFastaExpDataId()));
}
catch (PipelineJobException e)
{
job.getLogger().error(e.getMessage(), e);
}
}
if (m.getReferenceLibrary() != null)
{
cacheExpData(ExperimentService.get().getExpData(m.getReferenceLibrary()));
}
_cachedAnalyses.put(m.getRowId(), m);
}
@Override
public void cacheGenome(ReferenceGenome m)
{
markModified();
Integer key = m.getGenomeId();
if (m.isTemporaryGenome())
{
key = TEMPORARY_GENOME;
}
if (getCachedGenome(key) != null)
{
_cachedGenomes.remove(key);
}
_cachedGenomes.put(key, m);
}
@Override
public List<Readset> getCachedReadsets()
{
return Collections.unmodifiableList(new ArrayList<Readset>(_cachedReadsets));
}
@Override
public List<AnalysisModel> getCachedAnalyses()
{
return Collections.unmodifiableList(new ArrayList<>(_cachedAnalyses.values()));
}
@Override
public ReferenceGenome getCachedGenome(int genomeId)
{
return _cachedGenomes.get(genomeId);
}
@Override
public Collection<ReferenceGenome> getCachedGenomes()
{
return Collections.unmodifiableCollection(_cachedGenomes.values());
}
@Override
public void cacheExpData(ExpData data)
{
markModified();
if (data != null)
{
_cachedFilePaths.put(data.getRowId(), data.getFile());
}
}
@Override
public File getCachedData(long dataId)
{
return _cachedFilePaths.get(dataId);
}
@Override
public Map<Long, File> getAllCachedData()
{
return Collections.unmodifiableMap(_cachedFilePaths);
}
@Override
public void cacheObject(String key, Serializable object)
{
markModified();
_cachedObjects.put(key, object);
}
@Override
public <T> T getCachedObject(String key, Class<T> clazz) throws PipelineJobException
{
if (_cachedObjects.get(key) != null && clazz.isAssignableFrom(_cachedObjects.get(key).getClass()))
{
return clazz.cast(_cachedObjects.get(key));
}
ObjectMapper mapper = PipelineJob.createObjectMapper();
return getCachedObject(key, mapper.getTypeFactory().constructType(clazz));
}
@Override
public <T> T getCachedObject(String key, JavaType type) throws PipelineJobException
{
if (_cachedObjects.get(key) == null)
{
return null;
}
try
{
ObjectMapper mapper = PipelineJob.createObjectMapper();
StringWriter writer = new StringWriter();
mapper.writeValue(writer, _cachedObjects.get(key));
return mapper.readValue(new StringReader(writer.toString()), type);
}
catch (IOException e)
{
throw new PipelineJobException(e);
}
}
public static class TestCase extends Assert
{
@Test
public void testSerializeWithMap() throws Exception
{
SequenceJobSupportImpl js1 = new SequenceJobSupportImpl();
js1._cachedAnalyses.put(1L, new AnalysisModelImpl());
js1._cachedGenomes.put(2, new ReferenceGenomeImpl());
SequenceReadsetImpl rs1 = new SequenceReadsetImpl();
rs1.setRowId(100);
js1._cachedReadsets.add(rs1);
js1._cachedFilePaths.put(4L, new File("/"));
HashMap<Integer, Integer> map = new HashMap<>();
map.put(1, 1);
js1._cachedObjects.put("cachedMap", map);
js1._cachedObjects.put("cachedInt", 1);
js1._cachedObjects.put("cachedString", "foo");
js1._cachedObjects.put("cachedLong", 2L);
ObjectMapper mapper = PipelineJob.createObjectMapper();
StringWriter writer = new StringWriter();
mapper.writeValue(writer, js1);
SequenceJobSupportImpl deserialized = mapper.readValue(new StringReader(writer.toString()), SequenceJobSupportImpl.class);
assertNotNull("Analysis map not serialized properly", deserialized._cachedAnalyses.get(1L));
assertNotNull("Genome map not serialized properly", deserialized._cachedGenomes.get(2));
assertEquals("Readset list not serialized properly", 1, deserialized._cachedReadsets.size());
assertEquals("Readset not deserialized with correct rowid", 100, deserialized._cachedReadsets.get(0).getRowId());
assertEquals("Readset not deserialized with correct readsetid", 100, deserialized._cachedReadsets.get(0).getReadsetId().intValue());
assertNotNull("File map not serialized properly", deserialized._cachedFilePaths.get(4L));
assertNotNull("Cached map not serialized properly", deserialized.getCachedObject("cachedMap",Map.class));
assertEquals(" Cached int not serialized properly", Integer.valueOf(1), deserialized.getCachedObject("cachedInt", Integer.class));
assertEquals(" Cached string not serialized properly", "foo", deserialized.getCachedObject("cachedString", String.class));
assertEquals(" Cached long not serialized properly", 2L, deserialized.getCachedObject("cachedLong", Long.class).longValue());
//NOTE: this is not serializing properly. the keys are serialized as Strings
Map serializedMap = deserialized.getCachedObject("cachedMap", mapper.getTypeFactory().constructParametricType(Map.class, Integer.class, Integer.class));
assertEquals("Map not serialized properly", 1, serializedMap.size());
//TODO: determine if we can coax jackson into serializing these properly
assertEquals("Object not serialized with correct key type", Integer.class, serializedMap.keySet().iterator().next().getClass());
assertNotNull("Map keys not serialized properly", serializedMap.get(1));
}
@Test
public void testReadsetCaching() {
SequenceJobSupportImpl js = new SequenceJobSupportImpl();
SequenceReadsetImpl rs1 = new SequenceReadsetImpl();
rs1.setRowId(100);
assertTrue(rs1.existsInDatabase());
SequenceReadsetImpl rs2 = new SequenceReadsetImpl();
assertFalse(rs2.existsInDatabase());
SequenceReadsetImpl rs3 = new SequenceReadsetImpl();
assertFalse(rs3.existsInDatabase());
js.cacheReadset(rs1);
assertEquals(1, js._cachedReadsets.size());
js.cacheReadset(rs1);
assertEquals(1, js._cachedReadsets.size());
js.cacheReadset(rs2);
assertEquals(2, js._cachedReadsets.size());
js.cacheReadset(rs3);
assertEquals(3, js._cachedReadsets.size());
js.cacheReadset(rs2);
assertEquals(3, js._cachedReadsets.size());
js._cachedReadsets.remove(rs2);
assertEquals(2, js._cachedReadsets.size());
js.cacheReadset(rs2);
assertEquals(3, js._cachedReadsets.size());
}
}
}