-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSequenceAnalysisUpgradeCode.java
More file actions
297 lines (266 loc) · 11.8 KB
/
SequenceAnalysisUpgradeCode.java
File metadata and controls
297 lines (266 loc) · 11.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
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
package org.labkey.sequenceanalysis;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.biojava.nbio.core.exceptions.CompoundNotFoundException;
import org.biojava.nbio.core.sequence.DNASequence;
import org.labkey.api.collections.CaseInsensitiveHashMap;
import org.labkey.api.data.CompareType;
import org.labkey.api.data.Container;
import org.labkey.api.data.ContainerManager;
import org.labkey.api.data.DeferredUpgrade;
import org.labkey.api.data.SimpleFilter;
import org.labkey.api.data.Table;
import org.labkey.api.data.TableInfo;
import org.labkey.api.data.TableSelector;
import org.labkey.api.data.UpgradeCode;
import org.labkey.api.exp.api.ExpData;
import org.labkey.api.exp.api.ExperimentService;
import org.labkey.api.module.ModuleContext;
import org.labkey.api.query.FieldKey;
import org.labkey.api.sequenceanalysis.RefNtSequenceModel;
import org.labkey.api.util.PageFlowUtil;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* User: bimber
* Date: 1/31/13
* Time: 8:29 PM
*/
public class SequenceAnalysisUpgradeCode implements UpgradeCode
{
private static final Logger _log = LogManager.getLogger(SequenceAnalysisUpgradeCode.class);
/** called at 12.277-12.278 */
@SuppressWarnings({"UnusedDeclaration"})
@DeferredUpgrade
public void migrateSequenceField(final ModuleContext moduleContext)
{
TableInfo ti = SequenceAnalysisSchema.getTable(SequenceAnalysisSchema.TABLE_REF_NT_SEQUENCES);
// original installs used SQL scripts to populate data into the ref NT table. with the
// new storage scheme this is not ideal, so simply delete these for new installs
try
{
if (moduleContext.isNewInstall())
{
TableSelector ts = new TableSelector(ti, PageFlowUtil.set("rowid"));
List<Integer> rowIds = ts.getArrayList(Integer.class);
SequenceAnalysisManager.get().deleteRefNtSequenceWithoutUserSchema(rowIds);
return;
}
TableSelector ts = new TableSelector(ti);
List<RefNtSequenceModel> nts = ts.getArrayList(RefNtSequenceModel.class);
_log.info(nts.size() + " total sequences to migrate");
for (RefNtSequenceModel nt : nts)
{
String seq = nt.getLegacySequence();
if (StringUtils.trimToNull(seq) != null)
{
_log.info("writing sequence: " + nt.getName());
nt.createFileForSequence(moduleContext.getUpgradeUser(), seq, null);
}
}
}
catch (Exception e)
{
_log.error("Error upgrading sequenceanalysis module", e);
}
}
/** called at 12.292-12.293 and 12.293-12.294*/
@SuppressWarnings({"UnusedDeclaration"})
@DeferredUpgrade
public void migrateLibraryTracks(final ModuleContext moduleContext)
{
if (moduleContext.isNewInstall())
return;
try
{
TableInfo ti = SequenceAnalysisSchema.getTable(SequenceAnalysisSchema.TABLE_LIBRARY_TRACKS);
TableSelector ts = new TableSelector(ti, PageFlowUtil.set("fileid", "library_id", "container"));
List<Map> dataIds = ts.getArrayList(Map.class);
_log.info(dataIds.size() + " total tracks to migrate");
for (Map<String, Object> map : dataIds)
{
Integer libraryId = (Integer)map.get("library_id");
Container container = ContainerManager.getForId(map.get("container").toString());
if (container == null)
{
continue;
}
File libraryDir = new File(SequenceAnalysisManager.get().getReferenceLibraryDir(container), libraryId.toString());
File trackDir = new File(libraryDir, "tracks");
if (!trackDir.exists())
{
trackDir.mkdirs();
}
ExpData d = ExperimentService.get().getExpData((Integer)map.get("fileid"));
if (d != null && d.getFile() != null)
{
if (!d.getFile().getParentFile().getName().equals("tracks"))
{
File dest = new File(trackDir, d.getName());
_log.info("moving file: " + d.getFile().getPath() + " to " + dest.getPath());
FileUtils.moveFile(d.getFile(), dest);
d.setDataFileURI(dest.toURI());
d.save(moduleContext.getUpgradeUser());
}
else
{
_log.info("track file is already in proper location: " + d.getFile().getName());
}
}
else
{
_log.warn("dataId not found for track with dataId: " + map.get("fileid"));
}
}
//migrate chain files
TableInfo chainTable = SequenceAnalysisSchema.getTable(SequenceAnalysisSchema.TABLE_CHAIN_FILES);
TableSelector ts2 = new TableSelector(chainTable, PageFlowUtil.set("chainFile", "genomeId1", "container"));
List<Map> chainFiles = ts2.getArrayList(Map.class);
_log.info(chainFiles.size() + " total chain files to migrate");
for (Map<String, Object> map : chainFiles)
{
Integer libraryId = (Integer)map.get("genomeId1");
Container container = ContainerManager.getForId(map.get("container").toString());
File libraryDir = new File(SequenceAnalysisManager.get().getReferenceLibraryDir(container), libraryId.toString());
File chainDir = new File(libraryDir, "chainFiles");
if (!chainDir.exists())
{
chainDir.mkdirs();
}
ExpData d = ExperimentService.get().getExpData((Integer)map.get("chainFile"));
if (d != null && d.getFile() != null)
{
if (!d.getFile().getParentFile().getName().equals("chainFiles"))
{
File dest = new File(chainDir, d.getName());
_log.info("moving file: " + d.getFile().getPath() + " to " + dest.getPath());
FileUtils.moveFile(d.getFile(), dest);
d.setDataFileURI(dest.toURI());
d.save(moduleContext.getUpgradeUser());
}
else
{
_log.info("chain file is already in proper location: " + d.getFile().getName());
}
}
else
{
_log.warn("dataId not found for chain file with dataId: " + map.get("chainFile"));
}
}
}
catch (Exception e)
{
_log.error("Error upgrading sequenceanalysis module", e);
}
}
/** called at 12.306-12.307*/
@SuppressWarnings({"UnusedDeclaration"})
@DeferredUpgrade
public void appendSequenceLength(final ModuleContext moduleContext)
{
if (moduleContext.isNewInstall())
return;
SequenceAnalysisManager.get().apppendSequenceLength(moduleContext.getUpgradeUser(), _log);
}
/** called at 12.321-12.322*/
@SuppressWarnings({"UnusedDeclaration"})
@DeferredUpgrade
public void updateBarcodeRC(final ModuleContext moduleContext)
{
TableInfo ti = SequenceAnalysisSchema.getTable(SequenceAnalysisSchema.TABLE_BARCODES);
TableSelector ts = new TableSelector(ti, PageFlowUtil.set("tag_name", "sequence"), new SimpleFilter(FieldKey.fromString("reverse_complement"), null, CompareType.ISBLANK), null);
if (ts.exists())
{
List<Map<String, Object>> toUpdate = new ArrayList<>();
ts.forEachResults(rs -> {
Map<String, Object> r = new CaseInsensitiveHashMap<>();
r.put("tag_name", rs.getString(FieldKey.fromString("tag_name")));
if (StringUtils.isEmpty(rs.getString(FieldKey.fromString("sequence"))))
{
return;
}
try
{
String[] sequences = rs.getString(FieldKey.fromString("sequence")).split(",");
List<String> rcs = new ArrayList<>();
for (String s : sequences)
{
DNASequence seq = new DNASequence(s);
rcs.add(seq.getReverseComplement().getSequenceAsString());
}
r.put("reverse_complement", StringUtils.join(rcs, ","));
toUpdate.add(r);
}
catch (CompoundNotFoundException e)
{
_log.error("Unable to reverse complement barcode: " + rs.getString(FieldKey.fromString("sequence")), e);
}
});
toUpdate.forEach(row -> {
Table.update(moduleContext.getUpgradeUser(), ti, row, row.get("tag_name"));
});
}
}
/** called at 12.331-12.332*/
@SuppressWarnings({"UnusedDeclaration"})
@DeferredUpgrade
public void migrateSequenceDirs(final ModuleContext moduleContext)
{
try
{
TableInfo ti = SequenceAnalysisSchema.getTable(SequenceAnalysisSchema.TABLE_REF_NT_SEQUENCES);
TableSelector ts = new TableSelector(ti);
List<RefNtSequenceModel> nts = ts.getArrayList(RefNtSequenceModel.class);
_log.info(nts.size() + " total sequences to migrate");
int processed = 0;
for (RefNtSequenceModel nt : nts)
{
processed++;
if (processed % 1000 == 0)
{
_log.info("{} of {} sequence files migrated", processed, nts.size());
}
ExpData legacyExpData = ExperimentService.get().getExpData(nt.getSequenceFile());
if (legacyExpData == null)
{
_log.error("Missing ExpData for NT sequence: {}", nt.getSequenceFile());
continue;
}
File legacyFile = legacyExpData.getFile();
if (!legacyFile.exists())
{
_log.error("Missing file for NT sequence: {}", legacyFile.getPath());
continue;
}
if (!RefNtSequenceModel.BASE_DIRNAME.equals(legacyFile.getParentFile().getName()))
{
_log.error("Sequence appears to have already been migrated, this might indicate a retry after a failed move: {}", legacyFile.getPath());
continue;
}
File newLocation = nt.getExpectedSequenceFile();
if (!newLocation.getParentFile().exists())
{
newLocation.getParentFile().mkdirs();
}
if (newLocation.exists())
{
_log.error("Target location for migrated sequence file exists, this might indicate a retry after a filed move: {}", newLocation.getPath());
continue;
}
FileUtils.copyFile(legacyFile, newLocation);
legacyExpData.setDataFileURI(newLocation.toURI());
legacyExpData.save(moduleContext.getUpgradeUser());
legacyFile.delete();
}
}
catch (Exception e)
{
_log.error("Error upgrading sequenceanalysis module", e);
}
}
}