forked from bimberlabinternal/BimberLabKeyModules
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSivStudiesCustomizer.java
More file actions
568 lines (495 loc) · 26.9 KB
/
SivStudiesCustomizer.java
File metadata and controls
568 lines (495 loc) · 26.9 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
package org.labkey.sivstudies.query;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Logger;
import org.labkey.api.collections.CaseInsensitiveHashSet;
import org.labkey.api.data.AbstractTableInfo;
import org.labkey.api.data.BaseColumnInfo;
import org.labkey.api.data.ColumnInfo;
import org.labkey.api.data.Container;
import org.labkey.api.data.JdbcType;
import org.labkey.api.data.SQLFragment;
import org.labkey.api.data.TableInfo;
import org.labkey.api.data.WrappedColumn;
import org.labkey.api.ldk.table.AbstractTableCustomizer;
import org.labkey.api.query.ExprColumn;
import org.labkey.api.query.FieldKey;
import org.labkey.api.query.LookupForeignKey;
import org.labkey.api.query.QueryDefinition;
import org.labkey.api.query.QueryException;
import org.labkey.api.query.QueryForeignKey;
import org.labkey.api.query.QueryService;
import org.labkey.api.query.UserSchema;
import org.labkey.api.security.User;
import org.labkey.api.studies.StudiesService;
import org.labkey.api.studies.query.ResultsOORDisplayColumn;
import org.labkey.api.study.Dataset;
import org.labkey.api.study.DatasetTable;
import org.labkey.api.util.logging.LogHelper;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
public class SivStudiesCustomizer extends AbstractTableCustomizer
{
private static final Logger _log = LogHelper.getLogger(SivStudiesCustomizer.class, "Table customization for the SIV Studies module");
public static final String ID_COL = "Id";
public static final String DATE_COL = "Date";
@Override
public void customize(TableInfo tableInfo)
{
StudiesService.get().getStudiesTableCustomizer().customize(tableInfo);
if (tableInfo instanceof DatasetTable ds)
{
performDatasetCustomization(ds);
}
else if ("chemistryPivot".equalsIgnoreCase(tableInfo.getName()) | "hematologyPivot".equalsIgnoreCase(tableInfo.getName()))
{
if (tableInfo instanceof AbstractTableInfo ati)
{
appendDemographicsColumns(ati);
}
}
}
public void performDatasetCustomization(DatasetTable ds)
{
if (ds instanceof AbstractTableInfo ati)
{
_log.debug("Customizing dataset: {}", ds.getName());
if (!ds.getDataset().isDemographicData())
{
appendAgeAtTimeCol(ds.getUserSchema(), ati, ID_COL, DATE_COL);
appendPvlColumns(ds, ID_COL, DATE_COL);
appendSivChallengeColumns(ati, ID_COL, DATE_COL);
appendArtColumns(ds, ID_COL, DATE_COL);
}
appendDemographicsColumns(ati);
addNumericValuesTrigger(ati);
if ("viralLoads".equalsIgnoreCase(ds.getName()))
{
customizeViralLoads(ati);
}
if ("assignment".equalsIgnoreCase(ds.getName()))
{
ati.addTriggerFactory(StudiesService.get().getStudiesTriggerFactory());
ati.addTriggerFactory(new AutoCreateDemographicsTrigger.Factory());
}
}
else
{
_log.error("Expected DatasetTable to be instanceof AbstractTableInfo: " + ds.getName());
}
}
private ColumnInfo getPkCol(TableInfo ti)
{
Set<String> pks = new CaseInsensitiveHashSet(ti.getPkColumnNames());
if (pks.size() == 1)
{
return ti.getColumn(pks.iterator().next());
}
else if (pks.contains("lsid"))
{
return ti.getColumn("lsid");
}
else if (pks.contains("objectId"))
{
return ti.getColumn("objectId");
}
else if (pks.contains("Id"))
{
return ti.getColumn("Id");
}
else if (pks.contains("subjectId"))
{
return ti.getColumn("subjectId");
}
return null;
}
private void appendAgeAtTimeCol(UserSchema demographicsSchema, AbstractTableInfo ds, final String subjectColName, final String dateColName)
{
String name = "ageAtTime";
if (ds.getColumn(name, false) != null)
return;
final ColumnInfo pkCol = getPkCol(ds);
if (pkCol == null)
return;
final ColumnInfo idCol = ds.getColumn(subjectColName);
if (idCol == null)
return;
if (ds.getColumn(dateColName) == null)
return;
final String targetSchemaName = ds.getUserSchema().getName();
final Container targetSchemaContainer = ds.getUserSchema().getContainer();
final User u = ds.getUserSchema().getUser();
final String schemaName = ds.getPublicSchemaName();
final String queryName = ds.getName();
final String demographicsPath = demographicsSchema.getContainer().getPath();
WrappedColumn col = new WrappedColumn(pkCol, name);
col.setLabel("Age At Time");
col.setReadOnly(true);
col.setIsUnselectable(true);
col.setUserEditable(false);
col.setFk(new LookupForeignKey(){
@Override
public TableInfo getLookupTableInfo()
{
String name = queryName + "_ageAtTime";
UserSchema targetSchema = ds.getUserSchema().getDefaultSchema().getUserSchema(targetSchemaName);
QueryDefinition qd = QueryService.get().createQueryDef(u, targetSchemaContainer, targetSchema, name);
qd.setSql("SELECT\n" +
"c." + pkCol.getFieldKey().toSQLString() + ",\n" +
"\n" +
"CAST(\n" +
"CASE\n" +
"WHEN d.birth is null or c." + dateColName + " is null\n" +
" THEN null\n" +
"WHEN (d.death IS NOT NULL AND d.death < c." + dateColName + ") THEN\n" +
" ROUND(CONVERT(age_in_months(d.birth, d.death), DOUBLE) / 12, 1)\n" +
"ELSE\n" +
" ROUND(CONVERT(age_in_months(d.birth, CAST(c." + dateColName + " as DATE)), DOUBLE) / 12, 1)\n" +
"END AS float) as AgeAtTime,\n" +
"\n" +
"CAST(\n" +
"CASE\n" +
"WHEN d.birth is null or c." + dateColName + " is null\n" +
" THEN null\n" +
"WHEN (d.death IS NOT NULL AND d.death < c." + dateColName + ") THEN\n" +
" floor(age(d.birth, d.death))\n" +
"ELSE\n" +
" floor(age(d.birth, CAST(c." + dateColName + " as DATE)))\n" +
"END AS float) as AgeAtTimeYearsRounded,\n" +
"\n" +
"CAST(\n" +
"CASE\n" +
"WHEN d.birth is null or c." + dateColName + " is null\n" +
" THEN null\n" +
"WHEN (d.death IS NOT NULL AND d.death < c." + dateColName + ") THEN\n" +
" CONVERT(TIMESTAMPDIFF('SQL_TSI_DAY',d.birth, d.death), INTEGER)\n" +
"ELSE\n" +
" CONVERT(TIMESTAMPDIFF('SQL_TSI_DAY',d.birth, CAST(c." + dateColName + " AS DATE)), INTEGER)\n" +
"END AS float) as AgeAtTimeDays,\n" +
"\n" +
"CAST(\n" +
"CASE\n" +
"WHEN d.birth is null or c." + dateColName + " is null\n" +
" THEN null\n" +
"WHEN (d.death IS NOT NULL AND d.death < c." + dateColName + ") THEN\n" +
" CONVERT(age_in_months(d.birth, d.death), INTEGER)\n" +
"ELSE\n" +
" CONVERT(age_in_months(d.birth, CAST(c." + dateColName + " AS DATE)), INTEGER)\n" +
"END AS float) as AgeAtTimeMonths,\n" +
"FROM \"" + schemaName + "\".\"" + queryName + "\" c " +
"LEFT JOIN \"" + demographicsPath + "\".study.demographics d ON (d.Id = c." + idCol.getFieldKey().toSQLString() + ")"
);
qd.setIsTemporary(true);
List<QueryException> errors = new ArrayList<>();
TableInfo ti = qd.getTable(errors, true);
if (!errors.isEmpty())
{
_log.warn("Error creating age at time lookup table for: " + schemaName + "." + queryName + " in container: " + targetSchema.getContainer().getPath());
for (QueryException e : errors)
{
_log.warn(e.getMessage(), e);
}
}
if (ti != null)
{
((BaseColumnInfo)ti.getColumn(pkCol.getName())).setHidden(true);
((BaseColumnInfo)ti.getColumn(pkCol.getName())).setKeyField(true);
((BaseColumnInfo)ti.getColumn("AgeAtTime")).setLabel("Age At Time (Years)");
((BaseColumnInfo)ti.getColumn("AgeAtTimeDays")).setLabel("Age At Time (Days)");
((BaseColumnInfo)ti.getColumn("AgeAtTimeMonths")).setLabel("Age At Time (Months)");
((BaseColumnInfo)ti.getColumn("AgeAtTimeYearsRounded")).setLabel("Age At Time (Years, Rounded)");
}
return ti;
}
});
ds.addColumn(col);
}
private void appendDemographicsColumns(AbstractTableInfo parentTable)
{
if (parentTable.getColumn("mhcGenotypes") == null)
{
BaseColumnInfo colInfo = getWrappedIdCol(parentTable.getUserSchema(), "demographicsMHC", parentTable, "mhcGenotypes");
colInfo.setLabel("MHC Genotypes");
parentTable.addColumn(colInfo);
}
if (parentTable.getColumn("projects") == null)
{
BaseColumnInfo colInfo = getWrappedIdCol(parentTable.getUserSchema(), "demographicsProjects", parentTable, "projects");
colInfo.setLabel("Project Summary");
parentTable.addColumn(colInfo);
}
if (parentTable.getColumn("immunizations") == null)
{
BaseColumnInfo colInfo = getWrappedIdCol(parentTable.getUserSchema(), "demographicsImmunizations", parentTable, "immunizations");
colInfo.setLabel("Immunization Summary");
parentTable.addColumn(colInfo);
}
if (parentTable.getColumn("interventions") == null)
{
BaseColumnInfo colInfo = getWrappedIdCol(parentTable.getUserSchema(), "demographicsInterventions", parentTable, "interventions");
colInfo.setLabel("Interventions");
parentTable.addColumn(colInfo);
}
if (parentTable.getColumn("outcomes") == null)
{
BaseColumnInfo colInfo = getWrappedIdCol(parentTable.getUserSchema(), "demographicsOutcomes", parentTable, "outcomes");
colInfo.setLabel("Outcomes");
parentTable.addColumn(colInfo);
}
if (parentTable.getColumn("sivART") == null)
{
BaseColumnInfo colInfo = getWrappedIdCol(parentTable.getUserSchema(), "demographicsChallengeAndArt", parentTable, "sivART");
colInfo.setLabel("SIV/ART Dates");
parentTable.addColumn(colInfo);
}
if (parentTable.getColumn("pvlInfo") == null)
{
BaseColumnInfo colInfo = getWrappedIdCol(parentTable.getUserSchema(), "demographicsPVL", parentTable, "pvlInfo");
colInfo.setLabel("PVL Info");
parentTable.addColumn(colInfo);
}
}
private void appendPvlColumns(DatasetTable ds, String subjectColName, String dateColName)
{
final String name = "viralLoad";
if (ds.getColumn(name) != null)
{
return;
}
Dataset vl = ds.getDataset().getStudy().getDatasetByName("viralloads");
if (vl == null)
{
return;
}
if (ds instanceof AbstractTableInfo ti)
{
ColumnInfo subjectCol = ti.getColumn(subjectColName);
ColumnInfo dateCol = ti.getColumn(dateColName);
final String tableName = vl.getDomain().getStorageTableName();
SQLFragment sql = new SQLFragment("(SELECT CASE WHEN count(t.result) = 1 THEN max(t.result) ELSE null END as expr FROM studydataset." + tableName + " t WHERE t.participantid = " + ExprColumn.STR_TABLE_ALIAS + ".participantid AND CAST(t.date AS DATE) = CAST(" + ExprColumn.STR_TABLE_ALIAS + ".date AS DATE) AND t.sampletype = 'Plasma' AND t.target = 'SIV')");
ExprColumn newCol = new ExprColumn(ti, name, sql, JdbcType.DOUBLE, subjectCol, dateCol);
newCol.setDescription("Displays the viral load from this timepoint, if present");
newCol.setLabel("SIV PVL (copies/mL)");
newCol.setDisplayColumnFactory(ResultsOORDisplayColumn::new);
ti.addColumn(newCol);
String nameOOR = name + "OORIndicator";
SQLFragment sqlOOR = new SQLFragment("(SELECT CASE WHEN count(t.result) = 1 THEN max(t.resultOORIndicator) ELSE null END as expr FROM studydataset." + tableName + " t WHERE t.participantid = " + ExprColumn.STR_TABLE_ALIAS + ".participantid AND CAST(t.date AS DATE) = CAST(" + ExprColumn.STR_TABLE_ALIAS + ".date AS DATE) AND t.sampletype = 'Plasma' AND t.target = 'SIV')");
ExprColumn newColOOR = new ExprColumn(ti, nameOOR, sqlOOR, JdbcType.VARCHAR, subjectCol, dateCol);
newColOOR.setDescription("For the corresponding PVL, this indicates if the value is out-of-range");
newColOOR.setLabel("SIV PVL OOR Indicator");
newColOOR.setHidden(true);
ti.addColumn(newColOOR);
String nameHasPvl = name + "HasPvl";
SQLFragment sqlHasPvl = new SQLFragment("(SELECT CASE WHEN count(*) > 0 THEN " + ti.getSqlDialect().getBooleanTRUE() + " ELSE " + ti.getSqlDialect().getBooleanFALSE() + " END as expr FROM studydataset." + tableName + " t WHERE t.participantid = " + ExprColumn.STR_TABLE_ALIAS + ".participantid AND t.sampletype = 'Plasma' AND t.target = 'SIV')");
ExprColumn newColHasPvl = new ExprColumn(ti, nameHasPvl, sqlHasPvl, JdbcType.DOUBLE, subjectCol, dateCol);
newColHasPvl.setDescription("Displays whether this animal has any positive SIV PVLs");
newColHasPvl.setLabel("Has Positive SIV PVL?");
ti.addColumn(newColHasPvl);
}
}
private void appendSivChallengeColumns(AbstractTableInfo targetTable, String subjectColName, String dateColName)
{
String name = "timePostSivChallenge";
if (targetTable.getColumn(name, false) != null)
return;
final ColumnInfo pkCol = getPkCol(targetTable);
if (pkCol == null)
return;
final ColumnInfo idCol = targetTable.getColumn(subjectColName);
if (idCol == null)
return;
final ColumnInfo dateCol = targetTable.getColumn(dateColName);
if (dateCol == null)
return;
final String targetSchemaName = targetTable.getUserSchema().getName();
final Container targetSchemaContainer = targetTable.getUserSchema().getContainer();
final User u = targetTable.getUserSchema().getUser();
final String schemaName = targetTable.getPublicSchemaName();
final String queryName = targetTable.getName();
WrappedColumn col = new WrappedColumn(pkCol, name);
col.setLabel("SIV Challenge");
col.setReadOnly(true);
col.setIsUnselectable(true);
col.setUserEditable(false);
col.setFk(new LookupForeignKey(){
@Override
public TableInfo getLookupTableInfo()
{
// TODO: mock?
String name = queryName + "_sivChallenge";
UserSchema targetSchema = targetTable.getUserSchema().getDefaultSchema().getUserSchema(targetSchemaName);
QueryDefinition qd = QueryService.get().createQueryDef(u, targetSchemaContainer, targetSchema, name);
qd.setSql("SELECT t.*,\n" +
"CASE\n" +
"WHEN t.daysPostInfection IS NULL THEN NULL\n" +
"WHEN t.daysPostInfection <= 28 THEN (CAST(t.daysPostInfection AS VARCHAR) || ' DPI')\n" +
"ELSE (CAST(t.weeksPostInfection AS VARCHAR) || ' WPI')\n" +
"END as timePostInfection\n" +
"FROM (SELECT\n" +
"max(ad.date) as infectionDate,\n" +
// NOTE: CAST() is used to ensure whole numbers
"CONVERT(TIMESTAMPDIFF('SQL_TSI_DAY', CAST(max(ad.date) AS DATE), CAST(c." + dateColName + " AS DATE)), INTEGER) as daysPostInfection,\n" +
"CONVERT((CONVERT(TIMESTAMPDIFF('SQL_TSI_DAY', CAST(max(ad.date) AS DATE), CAST(c." + dateColName + " AS DATE)), INTEGER) / 7), INTEGER) as weeksPostInfection,\n" +
"ROUND(CONVERT(TIMESTAMPDIFF('SQL_TSI_DAY', CAST(max(ad.date) AS DATE), CAST(c." + dateColName + " AS DATE)), DOUBLE) / 7.0, 1) as weeksPostInfectionDecimal,\n" +
"CONVERT(age_in_months(CAST(max(ad.date) AS DATE), CAST(c." + dateColName + " AS DATE)), DOUBLE) as monthsPostInfection,\n" +
"c." + pkCol.getFieldKey().toString() + "\n" +
"FROM \"" + schemaName + "\".\"" + queryName + "\" c " +
"JOIN studies.subjectAnchorDates ad ON (ad.subjectId = c." + idCol.getFieldKey().toSQLString() + ")\n" +
"WHERE ad.eventLabel = 'SIV Infection'\n" +
"GROUP BY c.date, c." + pkCol.getFieldKey().toString() + "\n" +
"HAVING count(DISTINCT c.date) = 1) t"
);
qd.setIsTemporary(true);
List<QueryException> errors = new ArrayList<>();
TableInfo ti = qd.getTable(errors, true);
if (!errors.isEmpty())
{
_log.warn("Error creating sivChallenge lookup table for: " + schemaName + "." + queryName + " in container: " + targetSchema.getContainer().getPath());
for (QueryException e : errors)
{
_log.warn(e.getMessage(), e);
}
}
if (ti != null)
{
((BaseColumnInfo)ti.getColumn(pkCol.getName())).setHidden(true);
((BaseColumnInfo)ti.getColumn(pkCol.getName())).setKeyField(true);
((BaseColumnInfo)ti.getColumn("infectionDate")).setLabel("Infection Date");
((BaseColumnInfo)ti.getColumn("daysPostInfection")).setLabel("Days Post-Infection");
((BaseColumnInfo)ti.getColumn("weeksPostInfection")).setLabel("Weeks Post-Infection");
((BaseColumnInfo)ti.getColumn("monthsPostInfection")).setLabel("Months Post-Infection");
BaseColumnInfo tpi = ((BaseColumnInfo)ti.getColumn("timePostInfection"));
tpi.setLabel("Time Post-Infection");
tpi.setSortFieldKeys(Arrays.asList(FieldKey.fromString("daysPostInfection")));
}
return ti;
}
});
targetTable.addColumn(col);
}
private void appendArtColumns(DatasetTable ds, String subjectColName, String dateColName)
{
String name = "artInformation";
if (ds.getColumn(name) != null)
return;
final ColumnInfo pkCol = getPkCol(ds);
if (pkCol == null)
return;
final ColumnInfo idCol = ds.getColumn(subjectColName);
if (idCol == null)
return;
final ColumnInfo dateCol = ds.getColumn(dateColName);
if (dateCol == null)
return;
Dataset treatments = ds.getDataset().getStudy().getDatasetByName("treatments");
if (treatments == null)
{
return;
}
final String targetSchemaName = ds.getUserSchema().getName();
final Container targetSchemaContainer = ds.getUserSchema().getContainer();
final User u = ds.getUserSchema().getUser();
final String schemaName = ds.getPublicSchemaName();
final String queryName = ds.getName();
WrappedColumn col = new WrappedColumn(pkCol, name);
col.setLabel("ART Information");
col.setReadOnly(true);
col.setIsUnselectable(true);
col.setUserEditable(false);
col.setFk(new LookupForeignKey(){
@Override
public TableInfo getLookupTableInfo()
{
String name = queryName + "_artData";
UserSchema targetSchema = ds.getUserSchema().getDefaultSchema().getUserSchema(targetSchemaName);
QueryDefinition qd = QueryService.get().createQueryDef(u, targetSchemaContainer, targetSchema, name);
qd.setSql("SELECT\n" +
"min(tr.date) as artInitiation,\n" +
"CONVERT(TIMESTAMPDIFF('SQL_TSI_DAY', CAST(min(tr.date) AS DATE), CAST(c." + dateColName + " AS DATE)), INTEGER) as daysPostArtInitiation,\n" +
"CONVERT(TIMESTAMPDIFF('SQL_TSI_DAY', CAST(min(tr.date) AS DATE), CAST(c." + dateColName + " AS DATE)) / 7, INTEGER) as weeksPostArtInitiation,\n" +
"CONVERT(age_in_months(CAST(min(tr.date) AS DATE), CAST(c." + dateColName + " AS DATE)), FLOAT) as monthsPostArtInitiation,\n" +
"max(tr.enddate) as artRelease,\n" +
"CONVERT(CASE WHEN max(tr.enddate) IS NULL THEN NULL ELSE TIMESTAMPDIFF('SQL_TSI_DAY', CAST(max(tr.enddate) AS DATE), CAST(c." + dateColName + " AS DATE)) END, INTEGER) as daysPostArtRelease,\n" +
"CONVERT(CASE WHEN max(tr.enddate) IS NULL THEN NULL ELSE TIMESTAMPDIFF('SQL_TSI_DAY', CAST(max(tr.enddate) AS DATE), CAST(c." + dateColName + " AS DATE)) END / 7, INTEGER) as weeksPostArtRelease,\n" +
"CONVERT(CASE WHEN max(tr.enddate) IS NULL THEN NULL ELSE age_in_months(CAST(max(tr.enddate) AS DATE), CAST(c." + dateColName + " AS DATE)) END, FLOAT) as monthsPostArtRelease,\n" +
"CAST(CASE WHEN CAST(min(tr.date) AS DATE) <= CAST(c." + dateCol.getFieldKey().toString() + " AS DATE) AND CAST(max(coalesce(tr.enddate, now())) AS DATE) >= CAST(c." + dateCol.getFieldKey().toString() + " AS DATE) THEN 'Y' ELSE null END as VARCHAR) as onArt,\n" +
"GROUP_CONCAT(DISTINCT tr.treatment) AS artTreatment,\n" +
"c." + pkCol.getFieldKey().toString() + "\n" +
"FROM \"" + schemaName + "\".\"" + queryName + "\" c " +
// TODO: consider whether this should include all dates
"JOIN study.treatments tr ON (tr.category = 'ART' AND CAST(tr.date AS DATE) <= CAST(c." + dateCol.getFieldKey().toString() + " AS DATE) AND tr.Id = c." + idCol.getFieldKey().toSQLString() + ")\n" +
"GROUP BY c." + dateCol.getFieldKey().toString() + ", c." + pkCol.getFieldKey().toString() + "\n" +
"HAVING COUNT(DISTINCT tr.date) = 1"
);
qd.setIsTemporary(true);
List<QueryException> errors = new ArrayList<>();
TableInfo ti = qd.getTable(errors, true);
if (!errors.isEmpty())
{
_log.warn("Error creating artData lookup table for: " + schemaName + "." + queryName + " in container: " + targetSchema.getContainer().getPath());
for (QueryException e : errors)
{
_log.warn(e.getMessage(), e);
}
}
if (ti != null)
{
((BaseColumnInfo)ti.getColumn(pkCol.getName())).setHidden(true);
((BaseColumnInfo)ti.getColumn(pkCol.getName())).setKeyField(true);
((BaseColumnInfo)ti.getColumn("artInitiation")).setLabel("ART Initiation");
((BaseColumnInfo)ti.getColumn("artRelease")).setLabel("ART Release");
((BaseColumnInfo)ti.getColumn("daysPostArtInitiation")).setLabel("Days Post-ART Initiation");
((BaseColumnInfo)ti.getColumn("weeksPostArtInitiation")).setLabel("Weeks Post-ART Initiation");
((BaseColumnInfo)ti.getColumn("monthsPostArtInitiation")).setLabel("Months Post-ART Initiation");
((BaseColumnInfo)ti.getColumn("daysPostArtRelease")).setLabel("Days Post-ART Release");
((BaseColumnInfo)ti.getColumn("weeksPostArtRelease")).setLabel("Weeks Post-ART Release");
((BaseColumnInfo)ti.getColumn("monthsPostArtRelease")).setLabel("Months Post-ART Release");
((BaseColumnInfo)ti.getColumn("artTreatment")).setLabel("ART Treatment(s)");
((BaseColumnInfo)ti.getColumn("onArt")).setLabel("Overlaps ART?");
}
return ti;
}
});
if (ds instanceof AbstractTableInfo ati)
{
ati.addColumn(col);
}
}
// TODO: was on ART or not??
private BaseColumnInfo getWrappedIdCol(UserSchema targetQueryUserSchema, String targetQueryName, AbstractTableInfo demographicsTable, String colName)
{
WrappedColumn col = new WrappedColumn(demographicsTable.getColumn(ID_COL), colName);
col.setReadOnly(true);
col.setIsUnselectable(true);
col.setUserEditable(false);
col.setFk(new QueryForeignKey(demographicsTable.getUserSchema(), null, targetQueryUserSchema, null, targetQueryName, ID_COL, ID_COL));
return col;
}
private void addNumericValuesTrigger(AbstractTableInfo ati)
{
// This behavior conflicts with ViralLoadsTriggerFactory
if ("viralLoads".equalsIgnoreCase(ati.getName()))
{
return;
}
List<NumericValuesTrigger.StringTransformer> stringTransformers = new ArrayList<>();
if ("immunizations".equalsIgnoreCase(ati.getName()))
{
stringTransformers.add((ti, row, stringValue, propName, errors) -> {
if ("quantity".equalsIgnoreCase(propName) & ("Supernatant".equalsIgnoreCase(stringValue) | "Supernatent".equalsIgnoreCase(stringValue)))
{
row.put("quantity", null);
String comments = row.get("comments") == null ? null : StringUtils.trimToNull(String.valueOf(row.get("comments")));
comments = (comments == null ? "" : comments + ", ") + "Quantity: Supernatant";
row.put("comments", comments);
}
});
}
ati.addTriggerFactory(new NumericValuesTrigger.Factory(stringTransformers));
}
private void customizeViralLoads(AbstractTableInfo ati)
{
ati.addTriggerFactory(new ViralLoadsTriggerFactory());
}
}