-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
1125 lines (989 loc) · 39.8 KB
/
server.js
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Author: Dany Fu ([email protected])
const ax = require("axios");
const rateLimit = require("axios-rate-limit");
const axios = rateLimit(ax.create(), { maxRequests: 5, perMilliseconds: 1000});
const csv = require("fast-csv");
const fs = require("fs");
const argv = require("minimist")(process.argv.slice(2));
const config = require("config");
const path = require("path");
const constants = require("./constants.js");
const timeout = 5000; //ms
/*****************
* Logging
* @type {winston}
*****************/
const { createLogger, format, transports } = require('winston');
const { combine, timestamp, printf } = format;
const myFormat = printf(({ level, message, timestamp }) => {
return `${timestamp} <${level}> ${message} </${level}>`;
});
let today = new Date().toLocaleDateString("en-US", {timeZone: "America/New_York"});
let todayFormatted = today.substring(0, 10).replace(/\//g, "-");
const logger = createLogger({
format: combine(
timestamp({
format: 'MM/DD/YYYY, hh:mm:ss A Z'
}),
myFormat
),
level: 'info',
transports: [
// - Write all logs with level `error` and below to `error.log`
new transports.File({ filename: `logs/error-${todayFormatted}.log`, level: 'error' }),
// - Write all logs with level `info` and below to `combined.log`
new transports.File({ filename: `logs/combined-${todayFormatted}.log` }),
],
});
/*******************
* HELPER FUNCTIONS
*******************/
function equalsIgnoringCase(text, other) {
return text.localeCompare(other, undefined, { sensitivity: 'accent' }) === 0;
}
function isEmpty(str) {
return (!str || str.trim().length === 0);
}
function isOdd(num) {
return num % 2 === 1;
}
function isEven(num) {
return num % 2 === 0;
}
function getNumAttempts(patientSample){
let data = patientSample.data? patientSample.data[0] : patientSample;
return data.meta.find(m => m.key === constants.META.NUM_ATTEMPTS).value;
}
function getPerformed(patientSample){
let data = patientSample.data? patientSample.data[0] : patientSample;
return data.meta.find(m => m.key === constants.META.PERFORMED).value;
}
function getSampleID(patientSample){
return patientSample.data? patientSample.data[0].sampleID : patientSample.sampleID;
}
function getSampleTypeID(patientSample){
return patientSample.data? patientSample.data[0].sampleTypeID : patientSample.sampleTypeID;
}
function getChildren(pooledSample){
return pooledSample.data? pooledSample.data[0].children : pooledSample.children;
}
function getValidChildren(sampleObj, sampleBC){
let children = getChildren(sampleObj);
// check that all children are of COVID-19 sampleType
const nonCOVID = children.some(child => child.sampleTypeID !== config.get('covidSampleTypeID'));
if(nonCOVID){
logger.error(`Not all children of pooled sample ${sampleBC} are of sample type COVID-19 Sample.
Results for sampleBC ${sampleBC} NOT processed.`);
return null;
}
return children.filter(child => getPerformed(child) === constants.POOLED.POOLED);
}
function getParentID(patientSample){
return patientSample.data? patientSample.data[0].parentSampleID : patientSample.parentSampleID;
}
function isChild(patientSample){
return getSampleTypeID(patientSample) === config.get("covidSampleTypeID")
&& getPerformed(patientSample) === constants.POOLED.POOLED
&& getParentID(patientSample) !== "0";
}
function isParent(pooledSample){
return getSampleTypeID(pooledSample) === config.get("pooledSampleTypeID");
}
function isValidParent(pooledSample){
return getSampleTypeID(pooledSample) === config.get("pooledSampleTypeID") &&
pooledSample.data[0].children.length > 0 &&
pooledSample.data[0].children.length < 6;
}
function getMetasForSample(sampleObj, indMetas, poolMetas){
return getSampleTypeID(sampleObj) === config.get('covidSampleTypeID')? indMetas : poolMetas;
}
function stringToArray(str){
str = str.replace(/'|"/g, "");
return str.replace(/^\[|\]$/g, "").split(",");
}
function isControl(sample){
return Object.values(constants.CONTROLS).some(v => sample.includes(v))
}
function getWarningWells(data){
const regex = /(.*),.*,WARNING/g;
let found = [...data.matchAll(regex)];
if(found.length){
return Array.from(found, f => f[1]);
}
return null;
}
function isWellCall(data){
const regex = /\[Well Call\]/g;
return regex.exec(data);
}
function getqPCRUser(data) {
const regex = /# User Name: (.*)/g;
let found = regex.exec(data);
if(found){
return found[1];
}
return null;
}
function getqPCRSN(data) {
const regex = /# Instrument Serial Number: (.*)/g;
let found = regex.exec(data);
if(found){
return found[1];
}
return null;
}
function createMetaObj({key, value, type, metaID}={}){
return {
"key": key,
"value": value,
"sampleDataType": type,
"sampleTypeMetaID": metaID
}
}
function isValidSampleType(sampleObj, sampleBC){
const sampleTypeID = getSampleTypeID(sampleObj);
if (sampleTypeID !== config.get('covidSampleTypeID') && sampleTypeID !== config.get('pooledSampleTypeID')){
logger.error(`Unrecognized SampleTypeID ${sampleTypeID}. SAMPLE BC:${sampleBC} NOT PROCESSED.`);
return false;
}
return true;
}
function isValidLogSample(sampleObj, sampleBC){
const sampleTypeID = getSampleTypeID(sampleObj);
if (sampleTypeID === config.get('covidSampleTypeID') && getPerformed(sampleObj) === constants.POOLED.POOLED){
logger.error(`SAMPLE BC:${sampleBC} is a COVID-19 Sample and performed as "pooled" and thus should not appear in the log.
SAMPLE BC:${sampleBC} NOT PROCESSED.`);
return false;
}
return true;
}
/******************
* ELAB API CALLS
******************/
/**
* Updates an array of meta fields for a sample
* @param sampleID The unique ID of the sample generated by eLabs
* @param metaArray an array of meta objects
* @param retries Retry API call up to 5 (default) times
* @returns {Promise<void>}
*/
async function updateMetas(sampleID, metaArray, retries=5){
return axios
.put(`${config.get('endpoints.samples')}/${sampleID}/metas`, metaArray, {timeout: timeout})
.then((res) => {
if(res.status === 200){
logger.info(`Batch update sample: ${sampleID}, statusCode: ${res.status}.`);
} else {
if(retries > 0){
logger.error(`Error occurred during metas update for sampleID ${sampleID}, statusCode: ${res.status}. Trying again.`);
return updateMetas(sampleID, metaArray, retries - 1);
} else {
logger.error(`Failed to batch update sample after 5 attempts; Error: ${res.data}.
SAMPLE ID:${sampleID} NOT CORRECTLY PROCESSED.`);
process.exitCode = 8;
return null;
}
}
})
.catch((error) => {
if(retries > 0){
logger.error(`Error occurred during metas update for sampleID ${sampleID}. Trying again.`);
return updateMetas(sampleID, metaArray, retries - 1);
} else {
if (ax.isCancel(error)) {
logger.error(`Request cancelled after 5 attempts, ${error}`);
}
else if(error.response){
logger.error(`Failed to batch update sample after 5 attempts: ${sampleID}; Status: ${error.response.status}.
StatusText: ${error.response.statusText}. Error Message: ${error.response.data}.
SAMPLE ID:${sampleID} NOT CORRECTLY PROCESSED.`);
} else {
logger.error(`Failed to batch update meta field after 5 attempts. Possible internal network error. Try again later.`);
logger.error(`Error dump: ${error}`);
}
process.exitCode = 8;
return null;
}
});
}
/**
* Find a sample with the given barcode (which is also its name)
* Returns null if more than one sample can be found with the same barcode
* @param barcode Name of the sample
* @param prefetch Boolean, returns error codes for Hamilton if true
* @param retries Retry API call up to 5 (default) times
* @returns {Promise<void>} Sample object with all custom fields if found, else Null
*/
async function getPatientSample(barcode, prefetch=false, retries=5){
let endpoint = `${config.get('endpoints.samples')}` +
`?$expand=meta%2Cchildren&name=${barcode}`;
return axios.get(endpoint, {timeout: timeout})
.then((res) => {
if(res.status === 200){
if(res.data.data.length === 0){
logger.error(`Sample for barcode ID ${barcode} not found. SAMPLE BC:${barcode} NOT PROCESSED.`);
process.exitCode = 8;
return (prefetch? constants.PREFETCH_CODES.NOT_FOUND : null);
}
else if(res.data.data.length === 1){
logger.info(`Got sample with barcode ${barcode}, statusCode: ${res.status}`);
return res.data;
} else {
logger.error(`More than one sample found with name ${barcode}. SAMPLE BC:${barcode} NOT PROCESSED.`);
process.exitCode = 8;
return (prefetch? constants.PREFETCH_CODES.DUPLICATE : null);
}
} else{
if(retries > 0){
logger.error(`Error occurred during getting sample ${barcode}, statusCode: ${res.status}. Trying again.`);
return getPatientSample(barcode, prefetch, retries-1);
} else {
logger.error(res.data);
process.exitCode = 8;
return (prefetch? constants.PREFETCH_CODES.ERROR : null);
}
}
})
.catch((error) => {
if(retries > 0){
logger.error(`Error occurred while getting sample ${barcode}. Trying again.`);
return getPatientSample(barcode, prefetch, retries-1);
} else {
if (ax.isCancel(error)) {
logger.error(`Request cancelled after 5 attempts, ${error}`);
}
else if(error.response){
logger.error(`Failed to get sample after 5 attempts. Status: ${error.response.status}.
StatusText: ${error.response.statusText}. Error Message: ${error.response.data}.
SAMPLE BC:${barcode} NOT PROCESSED.`);
} else {
logger.error(`Failed to get sample after 5 attempts. Possible internal network error. Try again later.`);
logger.error(`Error dump: ${error}`);
}
process.exitCode = 8;
return (prefetch? 'ERROR' : null);
}
});
}
/**
* Get all the meta fields for SampleType
* @param sampleTypeID either COVID-19 Sample or Pooled COVID-19 Sample, see config for IDs
* @param retries Retry API call up to 5 (default) times
* @returns {Promise<T>}
*/
async function getSampleTypeMetas(sampleTypeID, retries=5){
return axios.get(`${config.get('endpoints.sampleTypes')}/${sampleTypeID}/meta`, {timeout: timeout})
.then((res) => {
if(res.status === 200){
logger.info(`Got SampleType, statusCode: ${res.status}`);
return res.data.data;
} else {
if(retries > 0){
logger.error(`Error occurred while getting SampleType, statusCode: ${res.status}. Trying again`);
return getSampleTypeMetas(retries-1);
} else {
logger.error(`Failed to get SampleType after 5 attempts. Status code: ${res.status}`);
process.exitCode = 8;
return null;
}
}
})
.catch((error) => {
if(retries > 0){
logger.error("Error occurred while getting SampleType. Trying again");
return getSampleTypeMetas(retries-1);
} else {
if (ax.isCancel(error)) {
logger.error(`Request to fetch SampleType cancelled after 5 attempts, ${error}`);
}
else if(error.response){
logger.error(`Failed to find SampleType after 5 attempts. Status: ${error.response.status}.
StatusText: ${error.response.statusText}. Error Message: ${error.response.data}.`);
} else {
logger.error(`Failed to get SampleType after 5 attempts. Possible internal network error. Try again later.`);
logger.error(`Error dump: ${error}`);
}
process.exitCode = 8;
return null;
}
});
}
/******************************
* HAMILTON RELATED FUNCTIONS
******************************/
/**
* Update Covid-19 Sample with the plate barcode and well number
* from the Sample Prep Hamilton and update status
* @param metas Array of meta fields associated with a SampleType
* @param destBC Output plate barcode
* @param destWellNum Output well number
* @param user Technician initials
* @param serialNum Serial number of the Hamilton robot
* @returns {*} Array of meta objects to be updated
*/
function samplePrepTracking(metas, destBC, destWellNum, user, serialNum){
let metaArray = [];
const dwBC = metas.find(m => m.key === constants.META.DEEPWELL_BC);
metaArray.push(createMetaObj({
key: constants.META.DEEPWELL_BC,
value: destBC,
type: dwBC.sampleDataType,
metaID: dwBC.sampleTypeMetaID})); //update RNA Plate Barcode
const dwWN = metas.find(m => m.key === constants.META.DEEPWELL_WELL_NUM);
metaArray.push(createMetaObj({
key: constants.META.DEEPWELL_WELL_NUM,
value: destWellNum,
type: dwWN.sampleDataType,
metaID: dwWN.sampleTypeMetaID})); //update RNA Plate Well Location
const status = metas.find(m => m.key === constants.META.STATUS);
metaArray.push(createMetaObj({
key: constants.META.STATUS,
value: constants.STATUS_VAL.SAMPLE_PREP_DONE,
type: status.sampleDataType,
metaID: status.sampleTypeMetaID})); //update status to "Sample Transferred To 96-Well Plate"
const userMeta = metas.find(meta => meta.key === constants.META.SAMPLE_PREP_TECH);
metaArray.push(createMetaObj({
key: constants.META.SAMPLE_PREP_TECH,
value: user,
type: userMeta.sampleDataType,
metaID: userMeta.sampleTypeMetaID})); //update initials of "Sample Aliquot User"
const snMeta = metas.find(meta => meta.key === constants.META.SAMPLE_PREP_SN);
metaArray.push(createMetaObj({
key: constants.META.SAMPLE_PREP_SN,
value: serialNum,
type: snMeta.sampleDataType,
metaID: snMeta.sampleTypeMetaID})); //update "Sample Aliquot Instrument SN"
return metaArray;
}
/**
* Updates Covid-19 Sample with the plate barcode and well number
* from the RNA Extraction Hamilton and update status
* @param metas Array of meta fields associated with a SampleType
* @param destBC Output plate barcode
* @param destWellNum Output well number
* @param user Technician initials
* @param serialNum Serial number of the Hamilton robot
* @returns {*} Array of meta objects to be updated
*/
function rnaExtractionTracking(metas, destBC, destWellNum, user, serialNum){
let metaArray = [];
const rnaBC = metas.find(m => m.key === constants.META.RNA_PLATE_BC);
metaArray.push(createMetaObj({
key: constants.META.RNA_PLATE_BC,
value: destBC,
type: rnaBC.sampleDataType,
metaID: rnaBC.sampleTypeMetaID})); //update RNA Plate Barcode
const rnaWN = metas.find(m => m.key === constants.META.RNA_PLATE_WELL_NUM);
metaArray.push(createMetaObj({
key: constants.META.RNA_PLATE_WELL_NUM,
value: destWellNum,
type: rnaWN.sampleDataType,
metaID: rnaWN.sampleTypeMetaID})); //update RNA Plate Well Location
const status = metas.find(m => m.key === constants.META.STATUS);
metaArray.push(createMetaObj({
key: constants.META.STATUS,
value: constants.STATUS_VAL.RNA_DONE,
type: status.sampleDataType,
metaID: status.sampleTypeMetaID})); //update status to "RNA Extracted"
const userMeta = metas.find(meta => meta.key === constants.META.EXTRACTION_TECH);
metaArray.push(createMetaObj({
key: constants.META.EXTRACTION_TECH,
value: user,
type: userMeta.sampleDataType,
metaID: userMeta.sampleTypeMetaID})); //update initials of "RNA Extraction User"
const snMeta = metas.find(meta => meta.key === constants.META.EXTRACTION_SN);
metaArray.push(createMetaObj({
key: constants.META.EXTRACTION_SN,
value: serialNum,
type: snMeta.sampleDataType,
metaID: snMeta.sampleTypeMetaID})); //update "RNA Extraction Instrument SN"
return metaArray;
}
/**
* Updates Covid-19 Sample with the plate barcode and well number
* from the qPCR Prep Hamilton and update status
* @param metas Array of meta fields associated with a SampleType
* @param destBC Output plate barcode
* @param destWellNum Output well number
* @param user Technician initials
* @param serialNum Serial number of the Hamilton robot
* @returns {*} Array of meta objects to be updated
*/
function qPCRPrepTracking(metas, destBC, destWellNum, user, serialNum){
let metaArray = [];
const pcrBC = metas.find(m => m.key === constants.META.QPCR_PLATE_BC);
metaArray.push(createMetaObj({
key: constants.META.QPCR_PLATE_BC,
value: destBC,
type: pcrBC.sampleDataType,
metaID: pcrBC.sampleTypeMetaID})); //update RNA Plate Barcode
const pcrWN = metas.find(m => m.key === constants.META.QPCR_PLATE_WELL_NUM);
metaArray.push(createMetaObj({
key: constants.META.QPCR_PLATE_WELL_NUM,
value: destWellNum,
type: pcrWN.sampleDataType,
metaID: pcrWN.sampleTypeMetaID})); //update RNA Plate Well Location
const status = metas.find(meta => meta.key === constants.META.STATUS);
metaArray.push(createMetaObj({
key: constants.META.STATUS,
value: constants.STATUS_VAL.QPCR_PREP_DONE,
type: status.sampleDataType,
metaID: status.sampleTypeMetaID})); //update status to "qPCR Plate Prepared"
const userMeta = metas.find(meta => meta.key === constants.META.QPCR_PREP_TECH);
metaArray.push(createMetaObj({
key: constants.META.QPCR_PREP_TECH,
value: user,
type: userMeta.sampleDataType,
metaID: userMeta.sampleTypeMetaID})); //update initials of "qPCR Prep User"
const snMeta = metas.find(meta => meta.key === constants.META.QPCR_PREP_SN);
metaArray.push(createMetaObj({
key: constants.META.QPCR_PREP_SN,
value: serialNum,
type: snMeta.sampleDataType,
metaID: snMeta.sampleTypeMetaID})); //update "qPCR Prep Instrument SN"
return metaArray;
}
/**
* Updates a Covid-19 Sample with the reagent lot number it was processed with
* @param metas Array of meta fields associated with a SampleType
* @param reagentNames Array of reagent names used
* @param reagentNums Array of reagent lot numbers, matches the names by index
* @returns {*} Array of meta objects to be updated
*/
function reagentTracking(metas, reagentNames, reagentNums){
reagentNames = stringToArray(reagentNames);
reagentNums = stringToArray(reagentNums);
let metaArray = [];
for (let i = 0; i < reagentNames.length; i++) {
const reagentMeta = metas.find(meta => meta.key === reagentNames[i]);
if(reagentMeta){
metaArray.push(createMetaObj({key: reagentNames[i],
value: reagentNums[i],
type: reagentMeta.sampleDataType,
metaID: reagentMeta.sampleTypeMetaID})); //Update reagent lot number
} else {
logger.error(`Reagent field ${reagentNames[i]} cannot be found.`);
process.exitCode = 8;
}
}
return metaArray;
}
/**
* Calls the appropriate update function based on Hamilton protocol
* @param metas Array of meta fields associated with a SampleType
* @param protocol A string indicating which robot the log originated from
* @param destBC Output plate barcode
* @param destWellNum Output well number
* @param user Technician initials
* @param serialNum Serial number of the Hamilton robot
* @returns {*} Array of meta objects to be updated
*/
function lineageTracking(metas, protocol, destBC, destWellNum, user, serialNum){
switch(protocol){
case constants.ORIGIN_VAL.SAMPLE_ALIQUOT:
return samplePrepTracking(metas, destBC, destWellNum, user, serialNum);
case constants.ORIGIN_VAL.RNA_EXTRACTION:
return rnaExtractionTracking(metas, destBC, destWellNum, user, serialNum);
case constants.ORIGIN_VAL.QPCR_PREP:
return qPCRPrepTracking(metas, destBC, destWellNum, user, serialNum);
}
}
/**
* Get data from each row of the Hamilton log and calls the appropriate function
* to update eLabs records
* @param csvRow
* @param indMetas Array of meta fields associated with the COVID-19 SampleType
* @param poolMetas Array of meta fields associated with the Pooled COVID-19 SampleType
* @returns {Promise<void>}
*/
async function hamiltonTracking(csvRow, indMetas, poolMetas){
let sampleBC = csvRow[constants.HAMILTON_LOG_HEADERS.SAMPLE_TUBE_BC];
let protocol = csvRow[constants.HAMILTON_LOG_HEADERS.PROTOCOL];
if (!protocol || (!(protocol in constants.ORIGIN_VAL))){
let protocolVals = Object.keys(constants.ORIGIN_VAL);
logger.error(`${protocol} is not recognized as a supported process. Must be one of the
${protocolVals.length} values: ${Object.keys(constants.ORIGIN_VAL)}.
SAMPLE BC:${sampleBC} NOT PROCESSED.`);
process.exitCode = 8;
return;
}
let sampleObj = await getPatientSample(sampleBC);
if(!sampleObj){
process.exitCode = 8;
return;
}
if(!isValidSampleType(sampleObj, sampleBC) || !isValidLogSample(sampleObj, sampleBC)){
process.exitCode = 8;
return;
}
let metas = getMetasForSample(sampleObj, indMetas, poolMetas);
let metaArray = []; //so we can update all the fields from the csv row in one API call
let reagentNames = csvRow[constants.HAMILTON_LOG_HEADERS.REAGENT_NAMES];
let reagentNums = csvRow[constants.HAMILTON_LOG_HEADERS.REAGENT_NUMS];
metaArray.push(...reagentTracking(metas, reagentNames, reagentNums));
let destBC = csvRow[constants.HAMILTON_LOG_HEADERS.DEST_BC];
let destWellNum = csvRow[constants.HAMILTON_LOG_HEADERS.DEST_WELL_NUM];
let user = csvRow[constants.HAMILTON_LOG_HEADERS.USER];
let serialNum = csvRow[constants.HAMILTON_LOG_HEADERS.SERIAL_NUM];
metaArray.push(...lineageTracking(metas, protocol, destBC, destWellNum, user, serialNum));
// if qPCR prep && sample is a parent, then update all children
if(protocol === constants.ORIGIN_VAL.QPCR_PREP && isParent(sampleObj)){
let children = getValidChildren(sampleObj, sampleBC);
if(!children){
process.exitCode = 8;
return;
}
for(const child of children){
let childMetaArr = [];
const childID = child.sampleID;
childMetaArr.push(...reagentTracking(indMetas, reagentNames, reagentNums));
childMetaArr.push(...lineageTracking(indMetas, protocol, destBC, destWellNum, user, serialNum));
await updateMetas(childID, childMetaArr);
}
}
let sampleID = getSampleID(sampleObj);
return Promise.resolve(updateMetas(sampleID, metaArray));
}
/********************************
* QUANTSTUDIO RELATED FUNCTIONS
********************************/
/**
* Increases number of attempt by 1 if allowed and returns the total number of attempts
* @param sampleObj data object of the sample from eLab
* @param metas Array of meta fields associated with a SampleType
* @returns {*} Meta object for number of attempts
*/
function increaseAttempt(sampleObj, metas){
// Check number of attempts
let numAttempt = getNumAttempts(sampleObj);
if (numAttempt < constants.MAX_ATTEMPTS){
++numAttempt; //Increase number of attempts by 1
}
const attemptMeta = metas.find(m => m.key === constants.META.NUM_ATTEMPTS);
return createMetaObj({key: constants.META.NUM_ATTEMPTS,
value: numAttempt,
type: attemptMeta.sampleDataType,
metaID: attemptMeta.sampleTypeMetaID});
}
/**
* Updates status and result of samples that belong to the plate which had a failed control
* @param metas Array of meta fields associated with a SampleType
* @param statusConst Either "Re-run qPCR" or "Re-run RNA Extraction"
* @param numAttempt Number of attempts for this sample
* @param isParent
* @returns {*} Array of meta objects to be updated
*/
function updateFailed(metas, statusConst, numAttempt, isParent=false){
let metaArray = [];
const result = metas.find(m => m.key === constants.META.RESULT);
const status = metas.find(m => m.key === constants.META.STATUS);
// If it's less than 5, we can rerun
if (numAttempt < constants.MAX_ATTEMPTS){
metaArray.push(createMetaObj({key: constants.META.RESULT,
value: constants.TEST_RESULT.WARNING,
type: result.sampleDataType,
metaID: result.sampleTypeMetaID})); //update Test Result to "Control Failed"
metaArray.push(createMetaObj({key: constants.META.STATUS,
value: statusConst,
type: status.sampleDataType,
metaID: status.sampleTypeMetaID})); // Update status to re-prep or re-extract
} else {
metaArray.push(createMetaObj({key: constants.META.RESULT,
value: constants.TEST_RESULT.INVALID,
type: result.sampleDataType,
metaID: result.sampleTypeMetaID})); //Update Test Result to "Invalid - recollect"
if(isParent){
metaArray.push(createMetaObj({key: constants.META.STATUS,
value: constants.STATUS_VAL.QPCR_COMPLETE,
type: status.sampleDataType,
metaID: status.sampleTypeMetaID})); //update status to "qPCR Completed"
} else {
metaArray.push(createMetaObj({key: constants.META.STATUS,
value: constants.STATUS_VAL.QPCR_DONE,
type: status.sampleDataType,
metaID: status.sampleTypeMetaID})); //update status to "Finished"
}
}
return metaArray;
}
/**
* Updates status and result of samples that did not fail its control sample
* @param metas Array of meta fields associated with a SampleType
* @param call Result of the well from the Call column of the qPCR output
* @param isParent Boolean for if the sample is a pooled parent
* @param isValidParent Boolean for if sample is pooled and have valid number of children
* @param isChild Boolean for if the sample is a pooled child
* @returns {*} Array of meta objects to be updated
*/
function updatePassed(metas, call, isParent=false, isValidParent=true, isChild=false){
let metaArray = [];
let callVal = constants.TEST_RESULT[call];
let isPos = call === 'POSITIVE';
let isInvalid = call === 'INVALID';
// if pooled sample is positive, map to Presumptive Positive
if(isPos && (isParent || isChild)){
callVal = constants.POOLED.POSITIVE;
}
const result = metas.find(m => m.key === constants.META.RESULT);
metaArray.push(createMetaObj({key: constants.META.RESULT,
value: callVal,
type: result.sampleDataType,
metaID: result.sampleTypeMetaID})); //update Test Result
let statusVal = constants.STATUS_VAL.QPCR_DONE;
if (isParent || (isChild && isInvalid) || (isChild && isPos)){
statusVal = constants.STATUS_VAL.QPCR_COMPLETE;
}
const status = metas.find(m => m.key === constants.META.STATUS);
metaArray.push(createMetaObj({key: constants.META.STATUS,
value: statusVal,
type: status.sampleDataType,
metaID: status.sampleTypeMetaID})); //update Status
// Flip children to Individual if the parent is invalid or positive
if(isChild && (isInvalid || isPos)){
const performed = metas.find(m => m.key === constants.META.PERFORMED);
metaArray.push(createMetaObj({key: constants.META.PERFORMED,
value: constants.POOLED.INDIVIDUAL,
type: performed.sampleDataType,
metaID: performed.sampleTypeMetaID}));
}
// flip tube condition to "damaged" if 0< children >6
if (isParent && !isValidParent){
const tube = metas.find(m => m.key === constants.META.TUBE_CONDITION);
metaArray.push(createMetaObj({key: constants.META.TUBE_CONDITION,
value: constants.TUBE_CONDITION.DAMAGED,
type: tube.sampleDataType,
metaID: tube.sampleTypeMetaID}));
}
return metaArray;
}
/**
* Helper function for building metas for test results
* @param sampleObj data object of the sample from eLab
* @param metas Array of meta fields associated with a SampleType
* @param failedWells Dictionary of all possible failed wells and their respective status
* @param user Initials of the technician who initiated the qPCR run
* @param serialNum Serial number of the qPCR machine
* @param wellNum Well number from the Well Position column of the qPCR output
* @param call Result of the well from the Call column of the qPCR output
* @returns {Array}
*/
function buildResultMetas(sampleObj, metas, failedWells, user, serialNum, wellNum, call){
let metaArray = [];
const userMeta = metas.find(meta => meta.key === constants.META.QPCR_TECH);
metaArray.push(createMetaObj({
key: constants.META.QPCR_TECH,
value: user,
type: userMeta.sampleDataType,
metaID: userMeta.sampleTypeMetaID})); //update initials of "qPCR User"
const snNumMeta = metas.find(meta => meta.key === constants.META.QPCR_SN);
metaArray.push(createMetaObj({
key: constants.META.QPCR_SN,
value: serialNum,
type: snNumMeta.sampleDataType,
metaID: snNumMeta.sampleTypeMetaID})); //update "qPCR SN"
let numAttemptMetaObj = increaseAttempt(sampleObj, metas);
metaArray.push(numAttemptMetaObj);
if (wellNum in failedWells){
metaArray.push(...updateFailed(metas, failedWells[wellNum], numAttemptMetaObj.value, isParent(sampleObj)));
} else {
metaArray.push(...updatePassed(metas, call, isParent(sampleObj), isValidParent(sampleObj), isChild(sampleObj)));
}
return metaArray;
}
/**
* Main function for updating data from the Well Call file
*
* Update "call" of the test, results can be POSITIVE, NEGATIVE, INVALID, INCONCLUSIVE, or WARNING
* INVALID or INCONCLUSIVE results both require recollection of sample
* WARNING occurs when controls failed, and can be reattempted up to 4 more times
* If controls fail during attempt #5, a recollection is required
* @param csvRow
* @param indMetas Array of meta fields associated with the COVID-19 SampleType
* @param poolMetas Array of meta fields associated with the Pooled COVID-19 SampleType
* @param failedWells Dictionary of all possible failed wells and their respective status
* @param user Initials of the technician who initiated the qPCR run
* @param serialNum Serial number of the qPCR machine
* @returns {Promise<void>}
*/
async function updateTestResult(csvRow, indMetas, poolMetas, failedWells, user, serialNum){
let sampleBC = csvRow[constants.QPCR_LOG_HEADERS.SAMPLE];
if (isControl(sampleBC)) {
return;
}
let sampleObj = await getPatientSample(sampleBC);
if(!sampleObj){
process.exitCode = 8;
return;
}
if(!isValidSampleType(sampleObj, sampleBC) || !isValidLogSample(sampleObj, sampleBC)){
process.exitCode = 8;
return;
}
let metas = getMetasForSample(sampleObj, indMetas, poolMetas);
let wellNum = csvRow[constants.QPCR_LOG_HEADERS.WELL];
let call = csvRow[constants.QPCR_LOG_HEADERS.CALL];
let metaArray = buildResultMetas(sampleObj, metas, failedWells, user, serialNum, wellNum, call);
// update its children, if pooled
if(isParent(sampleObj)){
let children = getValidChildren(sampleObj, sampleBC);
if(!children){
process.exitCode = 8;
return;
}
for(const child of children){
const childID = child.sampleID;
let childMetaArr = buildResultMetas(child, indMetas, failedWells, user, serialNum, wellNum, call);
await updateMetas(childID, childMetaArr);
}
}
let sampleID = getSampleID(sampleObj);
return Promise.resolve(await updateMetas(sampleID, metaArray));
}
/**
* Helper function for building metas for Cq targets
* @param metas
* @param target Name of the gene target
* @param cq CT value
* @returns {{key, value, sampleDataType, sampleTypeMetaID}}
*/
function buildTargetMeta(metas, target, cq){
const targetMeta = metas.find(m => m.key === constants.META[target]);
return createMetaObj({key: constants.META[target],
value: cq,
type: targetMeta.sampleDataType,
metaID: targetMeta.sampleTypeMetaID});
}
/**
* Main function for updating data from the Target Call file
*
* Updates PatientSample with CT values from the QuantStudio. Stores CT values
* internally until all 3 have been read, then writes them at once to eLab.
* @param csvRow
* @param indMetas Array of meta fields associated with the COVID-19 SampleType
* @param poolMetas Array of meta fields associated with the Pooled COVID-19 SampleType
* @param allSampleCTs dictionary of eLab sampleID to array of CT value meta objects
* @returns {Promise<void>}
*/
async function updateCTValues(csvRow, indMetas, poolMetas, allSampleCTs){
let sampleBC = csvRow[constants.QPCR_LOG_HEADERS.SAMPLE];
if (isControl(sampleBC)) {
return;
}
let sampleObj = await getPatientSample(sampleBC);
if(!sampleObj){
process.exitCode = 8;
return;
}
if(!isValidSampleType(sampleObj, sampleBC) || !isValidLogSample(sampleObj, sampleBC)){
process.exitCode = 8;
return;
}
let metas = getMetasForSample(sampleObj, indMetas, poolMetas);
if(!(sampleBC in allSampleCTs)){
allSampleCTs[sampleBC] = [];
}
let target = csvRow[constants.QPCR_LOG_HEADERS.TARGET];
let cq = csvRow[constants.QPCR_LOG_HEADERS.CQ];
allSampleCTs[sampleBC].push(buildTargetMeta(metas, target, cq));
// update record when all 3 records are processed
if(allSampleCTs[sampleBC].length === 3){
let sampleID = getSampleID(sampleObj);
await updateMetas(sampleID, allSampleCTs[sampleBC]);
// and update its children, if pooled
if(getSampleTypeID(sampleObj) === config.get('pooledSampleTypeID')){
let children = getValidChildren(sampleObj, sampleBC);
if(!children){
process.exitCode = 8;
return;
}
for(const child of children){
const childID = child.sampleID;
let childMetaArr = allSampleCTs[sampleBC].map(parentMeta =>
buildTargetMeta(indMetas, parentMeta["key"].substring(10, 12), parentMeta["value"])
);
await updateMetas(childID, childMetaArr);
}
}
}
return Promise.resolve(true);
}
/**
* Creates a dictionary of all *possible* wells in a 384 that match a failed control
* PCR_POS failure means all samples need to be re-prepped
* NTC/NEC failures mean that specific plate needs to be re-extracted (takes precedence)
* @param failedControls Array of control names that failed (warning)
* @returns {*} key: well position, value: status
*/
function getFailureWells(failedControls){
let failedWells = {};
for(let failed of failedControls){
if(!constants.CONTROL_WELLS.includes(failed)){
logger.error(`${failed} is not a valid Control well`);
process.exitCode = 8;
return null;
}
const plateRows = new Array( constants.PLATE384.ROW ).fill(1).map( (_, i) => String.fromCharCode(65 + i));
const plateCols = Array.from(Array(constants.PLATE384.COL), (_, i) => (i + 1).toString());
if(failed === constants.CONTROL_WELLS[0]){
// re-prep all the wells on the plate
for (let i = 0; i < plateRows.length; i++) {
for (let j = 0; j < plateCols.length; j++) {
failedWells[`${plateRows[i]}${plateCols[j]}`] = constants.STATUS_VAL.RE_QPCR;
}
}
} else{
let rIndex = plateRows.indexOf(failed[0]);
let cIndex = plateCols.indexOf(failed[1]);
let failedRows = isOdd(rIndex) ? plateRows.filter((_,i)=>isOdd(i)) : plateRows.filter((_,i)=>isEven(i));
let failedCols = isOdd(cIndex) ? plateCols.filter((_,i)=>isOdd(i)) : plateCols.filter((_,i)=>isEven(i));
for (let i = 0; i < failedRows.length; i++) {
for (let j = 0; j < failedCols.length; j++) {
failedWells[`${failedRows[i]}${failedCols[j]}`] = constants.STATUS_VAL.RE_EXTRACT;
}
}
}
}
return failedWells;
}
/**
* Returns the eLab ID for the given row's barcode
* or a string indicating error
* @param csvRow
* @returns {Promise<any>}
*/
async function getElabID(csvRow){
let sampleBC = csvRow[constants.HAMILTON_LOG_HEADERS.SAMPLE_TUBE_BC];
let sampleObj = await getPatientSample(sampleBC, true);
let sampleID;
if (typeof sampleObj === 'string'){
sampleID = sampleObj;
} else {
// check that no children made it onto Gandalf
sampleID = isValidLogSample(sampleObj, sampleBC)? getSampleID(sampleObj) : constants.PREFETCH_CODES.INDIV_POOLED;
}
return Promise.resolve(sampleID);
}
/**
* Handles parsing of all Hamilton and QuantStudio CSV output
* @param logfile Output from Hamilton or QuantStudio
* @param failedWells Dictionary of all possible failed wells and their respective status
* @param qPCRUser Required for Well Call file
* @param qPCRSerialNum Required for Well Call file
*/
async function parseCSV(logfile, failedWells, qPCRUser, qPCRSerialNum){
let allSampleCTs = {};
let write = false;
let promises = [];
let idRows = [[constants.HAMILTON_LOG_HEADERS.SAMPLE_TUBE_BC, constants.HAMILTON_LOG_HEADERS.ELAB_ID]];
let indMetas = await getSampleTypeMetas(config.get('covidSampleTypeID'));