forked from bimberlabinternal/BimberLabKeyModules
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbstractVariantTransform.java
More file actions
317 lines (281 loc) · 11 KB
/
AbstractVariantTransform.java
File metadata and controls
317 lines (281 loc) · 11 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
package org.labkey.mgap.columnTransforms;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;
import org.junit.Assert;
import org.junit.Test;
import org.labkey.api.collections.CaseInsensitiveHashMap;
import org.labkey.api.data.Results;
import org.labkey.api.data.Selector;
import org.labkey.api.data.SimpleFilter;
import org.labkey.api.data.StopIteratingException;
import org.labkey.api.data.TableInfo;
import org.labkey.api.data.TableSelector;
import org.labkey.api.di.columnTransform.ColumnTransform;
import org.labkey.api.exp.api.DataType;
import org.labkey.api.exp.api.ExpData;
import org.labkey.api.exp.api.ExperimentService;
import org.labkey.api.pipeline.PipeRoot;
import org.labkey.api.pipeline.PipelineJobException;
import org.labkey.api.pipeline.PipelineService;
import org.labkey.api.query.BatchValidationException;
import org.labkey.api.query.FieldKey;
import org.labkey.api.query.QueryService;
import org.labkey.api.util.FileUtil;
import org.labkey.api.util.PageFlowUtil;
import org.labkey.mgap.etl.EtlQueueManager;
import org.labkey.mgap.mGAPManager;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by bimber on 5/15/2017.
*/
abstract public class AbstractVariantTransform extends ColumnTransform
{
private transient TableInfo _outputFilesTableInfo = null;
private transient Map<String, Integer> _genomeIdMap = null;
@Override
public void reset()
{
_outputFilesTableInfo = null;
_genomeIdMap = null;
}
protected Map<String, Integer> getGenomeIdMap()
{
if (_genomeIdMap == null)
{
_genomeIdMap = getGenomeMap();
}
return _genomeIdMap;
}
private Map<String, Integer> getGenomeMap()
{
TableInfo ti = QueryService.get().getUserSchema(getContainerUser().getUser(), getContainerUser().getContainer(), "sequenceanalysis").getTable("reference_libraries");
final Map<String, Integer> genomeMap = new HashMap<>();
new TableSelector(ti, PageFlowUtil.set("rowid", "name")).forEachResults(new Selector.ForEachBlock<Results>()
{
@Override
public void exec(Results rs) throws SQLException, StopIteratingException
{
genomeMap.put(rs.getString("name"), rs.getInt("rowid"));
}
});
return genomeMap;
}
protected TableInfo getOutputFilesTableInfo()
{
if (_outputFilesTableInfo == null)
{
_outputFilesTableInfo = QueryService.get().getUserSchema(getContainerUser().getUser(), getContainerUser().getContainer(), "sequenceanalysis").getTable("outputfiles");
}
return _outputFilesTableInfo;
}
protected String getGenomeIdField()
{
return "genomeId/Name";
}
protected Integer getLibraryId()
{
String name = (String)getInputValue(getGenomeIdField());
Map<String, Integer> genomeMap = getGenomeIdMap();
if (name == null || genomeMap == null)
{
return null;
}
return genomeMap.get(name);
}
protected Integer getOrCreateOutputFile(Object dataFileUrl, Object folderName, String name)
{
try
{
if (dataFileUrl == null)
{
throw new IllegalArgumentException("DataFileUrl was null");
}
URI uri = new URI(String.valueOf(dataFileUrl));
File f = new File(uri);
if (!f.exists())
{
if (!EtlQueueManager.get().isFileInQueue(getContainerUser().getContainer(), f))
{
getStatusLogger().error("File not found: " + uri);
return null;
}
else
{
getStatusLogger().debug("File is in ETL queue: " + f.getPath());
}
}
File subDir = getLocalSubdir(folderName);
File localCopy = doFileCopy(f, subDir, name);
if (localCopy == null)
{
// TODO
}
//first create the ExpData
ExpData d = ExperimentService.get().getExpDataByURL(localCopy, getContainerUser().getContainer());
if (d == null)
{
d = ExperimentService.get().createData(getContainerUser().getContainer(), new DataType("Variant Catalog"));
d.setDataFileURI(localCopy.toURI());
d.setName(localCopy.getName());
d.save(getContainerUser().getUser());
}
//then the outputfile
TableSelector ts = new TableSelector(getOutputFilesTableInfo(), PageFlowUtil.set("rowid"), new SimpleFilter(FieldKey.fromString("dataId"), d.getRowId()), null);
if (ts.exists())
{
getStatusLogger().info("existing record found for outputfile: " + d.getDataFileUrl());
return ts.getObject(Integer.class);
}
else
{
Map<String, Object> row = new CaseInsensitiveHashMap<>();
row.put("category", getOutputFileCategory());
row.put("dataid", d.getRowId());
row.put("name", name == null ? "mGAP Variants, Version: " + getInputValue("version") : name);
row.put("description", getDescription());
row.put("library_id", getLibraryId());
row.put("container", getContainerUser().getContainer().getId());
row.put("created", new Date());
row.put("createdby", getContainerUser().getUser().getUserId());
row.put("modified", new Date());
row.put("modifiedby", getContainerUser().getUser().getUserId());
List<Map<String, Object>> rows = getOutputFilesTableInfo().getUpdateService().insertRows(getContainerUser().getUser(), getContainerUser().getContainer(), List.of(row), new BatchValidationException(), null, new HashMap<>());
getStatusLogger().info("created outputfile: " + rows.get(0).get("rowid"));
return (Integer)rows.get(0).get("rowid");
}
}
catch (Exception e)
{
getStatusLogger().error("Error syncing file: " + dataFileUrl, e);
}
return null;
}
protected File getLocalSubdir(Object folderName) throws PipelineJobException
{
PipeRoot pr = PipelineService.get().getPipelineRootSetting(getContainerUser().getContainer());
File baseDir = new File(pr.getRootPath(), mGAPManager.DATA_DIR_NAME);
if (!baseDir.exists())
{
baseDir.mkdirs();
}
String folderNameString = StringUtils.trimToNull(String.valueOf(folderName));
if (folderNameString == null)
{
throw new PipelineJobException("Unable to find folderName");
}
File subdir = new File(baseDir, folderNameString);
if (!subdir.exists())
{
subdir.mkdirs();
}
return subdir;
}
protected File doFileCopy(File f, File subdir, @Nullable String name) throws PipelineJobException
{
getStatusLogger().info("preparing to copy file: " + f.getPath() + ", with name: " + name);
if (f.getName().equals("write.lock"))
{
return LuceneIndexTransform.doLuceneCopy(f, subdir, name, getStatusLogger(), getContainerUser().getContainer());
}
//Copy file locally, plus index if exists:
File localCopy = new File(subdir, name == null || f.getName().startsWith("mGap.v") ? f.getName() : FileUtil.makeLegalName(name).replaceAll(" ", "_") + ".vcf.gz");
if (f.equals(localCopy))
{
getStatusLogger().debug("Attempting to copy file that is already a child of the target: " + f.getPath(), new Exception());
return localCopy;
}
boolean doCopy = true;
if (localCopy.exists())
{
getStatusLogger().info("file exists: " + localCopy.getPath());
if (localCopy.lastModified() >= f.lastModified())
{
doCopy = false;
}
else
{
getStatusLogger().info("source file has been modified, deleting copy and re-syncing: " + localCopy.getPath());
try
{
Files.delete(localCopy.toPath());
if (localCopy.exists())
{
throw new PipelineJobException("Unable to delete file: " + localCopy.getPath());
}
}
catch (IOException e)
{
throw new PipelineJobException("Unable to delete file: " + localCopy.getPath(), e);
}
}
}
else
{
getStatusLogger().info("existing file not found: " + localCopy.getPath());
}
if (doCopy)
{
getStatusLogger().info("Creating symlink: " + f.getPath() + " / " + localCopy.getPath());
try
{
if (!Files.isReadable(f.toPath()))
{
throw new PipelineJobException("Unable to read file: " + f.getPath());
}
if (localCopy.exists())
{
throw new PipelineJobException("File should have been deleted: " + localCopy.getPath());
}
Files.createSymbolicLink(localCopy.toPath(), f.toPath());
}
catch (IOException e)
{
throw new PipelineJobException("Failed to create symlink: " + localCopy.getPath(), e);
}
}
File index = new File(f.getPath() + ".tbi");
if (index.exists())
{
File indexLocal = new File(localCopy.getPath() + ".tbi");
if (doCopy && indexLocal.exists())
{
getStatusLogger().info("deleting local copy of index since file will be re-copied: " + indexLocal.getPath());
indexLocal.delete();
}
if (!indexLocal.exists())
{
getStatusLogger().info("Creating symlink copy of VCF index: " + index.getPath() + " / " + indexLocal.getPath());
try
{
Files.createSymbolicLink(indexLocal.toPath(), index.toPath());
}
catch (IOException e)
{
getStatusLogger().error("Failed to create symlink: " + indexLocal.getPath(), e);
}
}
else
{
getStatusLogger().info("Local index already exists: " + indexLocal.getPath());
}
}
return localCopy;
}
protected String getOutputFileCategory()
{
return "VCF File";
}
protected String getDescription()
{
return "mGAP Release";
}
}