forked from bimberlabinternal/BimberLabKeyModules
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPerformManualIdrStepsTask.java
More file actions
286 lines (245 loc) · 11.5 KB
/
PerformManualIdrStepsTask.java
File metadata and controls
286 lines (245 loc) · 11.5 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
package org.labkey.sivstudies.etl;
import org.apache.xmlbeans.XmlException;
import org.jetbrains.annotations.NotNull;
import org.labkey.api.collections.CaseInsensitiveHashMap;
import org.labkey.api.data.CompareType;
import org.labkey.api.data.SimpleFilter;
import org.labkey.api.data.TableInfo;
import org.labkey.api.data.TableSelector;
import org.labkey.api.di.TaskRefTask;
import org.labkey.api.pipeline.PipelineJob;
import org.labkey.api.pipeline.PipelineJobException;
import org.labkey.api.pipeline.RecordedActionSet;
import org.labkey.api.query.BatchValidationException;
import org.labkey.api.query.DuplicateKeyException;
import org.labkey.api.query.FieldKey;
import org.labkey.api.query.InvalidKeyException;
import org.labkey.api.query.QueryService;
import org.labkey.api.query.QueryUpdateServiceException;
import org.labkey.api.util.PageFlowUtil;
import org.labkey.api.writer.ContainerUser;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
public class PerformManualIdrStepsTask implements TaskRefTask
{
protected ContainerUser _containerUser;
@Override
public RecordedActionSet run(@NotNull PipelineJob pipelineJob) throws PipelineJobException
{
pruneSivChallenges(pipelineJob);
updateVaccineInformation(pipelineJob);
updateChallengeAnchorDates(pipelineJob);
updateArtInitiationAnchorDates(pipelineJob);
return new RecordedActionSet();
}
private void pruneSivChallenges(PipelineJob pipelineJob) throws PipelineJobException
{
TableInfo ti = QueryService.get().getUserSchema(_containerUser.getUser(), _containerUser.getContainer(), "study").getTable("treatments");
Map<String, Set<Date>> existingRecords = new HashMap<>();
new TableSelector(ti, PageFlowUtil.set("Id", "date", "lsid"), new SimpleFilter(FieldKey.fromString("treatment"), "SIV - Unknown", CompareType.NEQ_OR_NULL).addCondition(FieldKey.fromString("category"), "SIV Infection"), null).forEachResults(rs -> {
String id = rs.getString(FieldKey.fromString("Id"));
if (!existingRecords.containsKey(id))
{
existingRecords.put(id, new HashSet<>());
}
existingRecords.get(id).add(rs.getDate(FieldKey.fromString("date")));
});
final List<Map<String, Object>> toDelete = new ArrayList<>();
new TableSelector(ti, PageFlowUtil.set("Id", "date", "lsid"), new SimpleFilter(FieldKey.fromString("treatment"), "SIV - Unknown"), null).forEachResults(rs -> {
String id = rs.getString(FieldKey.fromString("Id"));
if (!existingRecords.containsKey(id))
{
return;
}
if (existingRecords.get(id).contains(rs.getDate(FieldKey.fromString("date"))))
{
toDelete.add(new CaseInsensitiveHashMap<>(Map.of("lsid", rs.getString(FieldKey.fromString("lsid")))));
}
});
if (!toDelete.isEmpty())
{
pipelineJob.getLogger().info("Deleting " + toDelete.size() + " SIV challenge records");
try
{
ti.getUpdateService().deleteRows(_containerUser.getUser(), _containerUser.getContainer(), toDelete, null, null);
}
catch (SQLException | BatchValidationException | QueryUpdateServiceException | InvalidKeyException e)
{
throw new PipelineJobException(e);
}
}
}
private static final Pattern mir126_RE = Pattern.compile("miR[- ]{0,1}126", Pattern.CASE_INSENSITIVE);
private static final Pattern mir142_RE = Pattern.compile("miR[- ]{0,1}142", Pattern.CASE_INSENSITIVE);
private static final Pattern mir126_142_RE = Pattern.compile("miR[- ]{0,1}142[ ,-]{1,2}126", Pattern.CASE_INSENSITIVE);
private void updateVaccineInformation(PipelineJob pipelineJob) throws PipelineJobException
{
TableInfo ti = QueryService.get().getUserSchema(_containerUser.getUser(), _containerUser.getContainer(), "study").getTable("immunizations");
final List<Map<String, Object>> toUpdate = new ArrayList<>();
new TableSelector(ti, PageFlowUtil.set("lsid", "treatment", "backbone", "antigens")).forEachResults(rs -> {
String treatment = rs.getString(FieldKey.fromString("treatment"));
if (treatment == null)
{
return;
}
String backbone = rs.getString(FieldKey.fromString("backbone"));
Map<String, Object> updatedRow = new HashMap<>();
if (backbone != null && backbone.contains("68-1"))
{
if (mir126_142_RE.matcher(treatment).find())
{
updatedRow.put("backbone", "68-1 MHC-1A-only");
}
else if (mir126_RE.matcher(treatment).find() && !treatment.contains("142"))
{
updatedRow.put("backbone", "68-1 MHC-E-only");
}
else if (mir142_RE.matcher(treatment).find() && !treatment.contains("126"))
{
updatedRow.put("backbone", "68-1 MHC-II-only");
}
else if (treatment.contains("RhCMV FL"))
{
updatedRow.put("backbone", "68-1 FL");
}
else if (treatment.contains("RhCMV") && treatment.contains("d186-189"))
{
updatedRow.put("backbone", "68-1 d186-189");
}
}
if (treatment.toUpperCase().contains("MOCK"))
{
updatedRow.put("isMock", true);
}
if (!updatedRow.isEmpty())
{
updatedRow.put("lsid", rs.getString(FieldKey.fromString("lsid")));
toUpdate.add(updatedRow);
}
});
if (!toUpdate.isEmpty())
{
pipelineJob.getLogger().info("Updating " + toUpdate.size() + " immunization records");
try
{
BatchValidationException bve = new BatchValidationException();
List<Map<String, Object>> oldKeys = toUpdate.stream().map(x -> (Map<String, Object>)new CaseInsensitiveHashMap<>(Map.of("lsid", x.get("lsid")))).toList();
ti.getUpdateService().updateRows(_containerUser.getUser(), _containerUser.getContainer(), toUpdate, oldKeys, bve, null, null);
if (bve.hasErrors())
{
throw bve;
}
}
catch (SQLException | BatchValidationException | QueryUpdateServiceException | InvalidKeyException e)
{
throw new PipelineJobException(e);
}
}
}
private void updateChallengeAnchorDates(PipelineJob pipelineJob) throws PipelineJobException
{
updateAnchorDates(pipelineJob, "SIV Infection", "SIV Infection", "date");
}
private void updateArtInitiationAnchorDates(PipelineJob pipelineJob) throws PipelineJobException
{
updateAnchorDates(pipelineJob, "ART Initiation", "ART", "date");
updateAnchorDates(pipelineJob, "ART End", "ART", "enddate");
}
private void updateAnchorDates(PipelineJob pipelineJob, String eventType, String treatmentCategory, String sourceDateField) throws PipelineJobException
{
TableInfo treatments = QueryService.get().getUserSchema(_containerUser.getUser(), _containerUser.getContainer(), "study").getTable("treatments");
TableInfo ad = QueryService.get().getUserSchema(_containerUser.getUser(), _containerUser.getContainer(), "studies").getTable("subjectAnchorDates");
Map<String, Set<Date>> existingRecords = new HashMap<>();
new TableSelector(ad, PageFlowUtil.set("subjectId", "date", "rowid"), new SimpleFilter(FieldKey.fromString("eventLabel"), eventType), null).forEachResults(rs -> {
String id = rs.getString(FieldKey.fromString("subjectId"));
if (!existingRecords.containsKey(id))
{
existingRecords.put(id, new HashSet<>());
}
existingRecords.get(id).add(rs.getDate(FieldKey.fromString("date")));
});
final Map<String, Set<Date>> sourceRecords = new HashMap<>();
final List<Map<String, Object>> toInsert = new ArrayList<>();
new TableSelector(treatments, PageFlowUtil.set("Id", sourceDateField, "objectId"), new SimpleFilter(FieldKey.fromString("category"), treatmentCategory), null).forEachResults(rs -> {
String id = rs.getString(FieldKey.fromString("Id"));
Date date = rs.getDate(FieldKey.fromString(sourceDateField));
if (date == null)
{
return;
}
if (!sourceRecords.containsKey(id))
{
sourceRecords.put(id, new HashSet<>());
}
sourceRecords.get(id).add(date);
if (!sourceRecords.containsKey(id) || !sourceRecords.get(id).contains(date))
{
toInsert.add(new CaseInsensitiveHashMap<>(Map.of(
"subjectId", id,
"date", date,
"category", eventType,
"sourceRecord", rs.getString(FieldKey.fromString("objectId"))
)));
}
});
if (!toInsert.isEmpty())
{
pipelineJob.getLogger().info("Inserting " + toInsert.size() + " " + eventType + " anchor date records");
try
{
BatchValidationException bve = new BatchValidationException();
ad.getUpdateService().insertRows(_containerUser.getUser(), _containerUser.getContainer(), toInsert, bve, null, null);
if (bve.hasErrors())
{
throw bve;
}
}
catch (SQLException | BatchValidationException | QueryUpdateServiceException | DuplicateKeyException e)
{
throw new PipelineJobException(e);
}
}
final List<Map<String, Object>> toDelete = new ArrayList<>();
new TableSelector(ad, PageFlowUtil.set("subjectId", "date", "rowid"), new SimpleFilter(FieldKey.fromString("eventLabel"), eventType), null).forEachResults(rs -> {
String id = rs.getString(FieldKey.fromString("subjectId"));
Date date = rs.getDate(FieldKey.fromString("date"));
if (!sourceRecords.containsKey(id) | !sourceRecords.get(id).contains(date))
{
toDelete.add(new CaseInsensitiveHashMap<>(Map.of("rowid", rs.getInt(FieldKey.fromString("rowId")))));
}
});
if (!toDelete.isEmpty())
{
pipelineJob.getLogger().info("Deleting " + toDelete.size() + " " + eventType + " anchor date records");
try
{
ad.getUpdateService().deleteRows(_containerUser.getUser(), _containerUser.getContainer(), toDelete, null, null);
}
catch (SQLException | BatchValidationException | QueryUpdateServiceException | InvalidKeyException e)
{
throw new PipelineJobException(e);
}
}
}
@Override
public List<String> getRequiredSettings()
{
return List.of();
}
@Override
public void setSettings(Map<String, String> map) throws XmlException
{
}
@Override
public void setContainerUser(ContainerUser containerUser)
{
_containerUser = containerUser;
}
}