-
Notifications
You must be signed in to change notification settings - Fork 2
/
stack.js
1618 lines (1429 loc) · 61.6 KB
/
stack.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
/* **** Helper functions **** */
/**
* Sort function for layer objects to sort by platform.<br>
* HiSeq before MiSeq - then on Queue date
* @param {Object} a Layer object
* @param {Object} b Layer object
* @returns {Number} A negative number if a should be sorted before b, a positive number if vice versa, otherwise 0
*/
function sortByPlatform (a, b) {
aPf = "";
bPf = "";
for (var i = 0; i < a.length; i++) {
if(a[i]["y"] != 0) { aPf = a[i]["x"]; aQ = a[i]["queueDate"]; }
if(b[i]["y"] != 0) { bPf = b[i]["x"]; bQ = b[i]["queueDate"]; }
}
if(aPf < bPf) return -1;
if(aPf > bPf) return 1;
if(aQ < bQ ) return -1;
if(aQ > bQ ) return 1;
return 0;
}
/**
* Sort function for layer objects to sort by application.<br>
* DNA/RNA/SeqCap/Other - then on Queue date
* @param {Object} a Layer object
* @param {Object} b Layer object
* @returns {Number} A negative number if a should be sorted before b, a positive number if vice versa, otherwise 0
*/
function sortByApplication (a, b) {
var map = {
"DNA": 0,
"RNA": 1,
"SeqCap": 2,
"Other": 3
}
var aAppl;
var bAppl;
for (var i = 0; i < a.length; i++) {
if(a[i]["y"] != 0) { aAppl = map[a[i]["x"]]; aQ = a[i]["queueDate"]; aArr = a[i]["arrivalDate"]; aPid = a[i]["pid"];}
if(b[i]["y"] != 0) { bAppl = map[b[i]["x"]]; bQ = b[i]["queueDate"]; bArr = b[i]["arrivalDate"]; bPid = b[i]["pid"];}
}
if(aAppl < bAppl) return -1;
if(aAppl > bAppl) return 1;
if(aQ < bQ ) return -1;
if(aQ > bQ ) return 1;
if(aArr < bArr ) return -1;
if(aArr > bArr ) return 1;
if(aPid < bPid ) return -1;
if(aPid > bPid ) return 1;
return 0;
}
function getFirstInQueue(data) {
var qD = "9999-99-99";
var arrD = "9999-99-99";
var firstPid;
for (var i = 0; i < data.length; i++) {
if (data[i][0]["queueDate"] < qD) {
qD = data[i][0]["queueDate"];
arrD = data[i][0]["arrivalDate"];
firstPid = data[i][0]["pid"];
} else if (data[i][0]["queueDate"] == qD) {
if (data[i][0]["arrivalDate"] < arrD) {
arrD = data[i][0]["arrivalDate"];
firstPid = data[i][0]["pid"];
} else if (data[i][0]["arrivalDate"] == arrD) {
if (data[i][0]["pid"] < firstPid) {
firstPid = data[i][0]["pid"];
}
}
}
}
return firstPid;
}
/**
* Calculates number of projects per domain (x value) in a layer object data set
* @param {Array} dataset Array of array of layer objects
* @param {Array} domain Array of domain names (x values)
* @returns {Object} An object with counts per domain
*/
function numProjects(dataset, domain) {
var num_projects = {};
for (var i = 0; i < domain.length; i++) {
num_projects[domain[i]] = 0;
}
//console.log(num_projects);
for(var i = 0; i < dataset.length; i++) {
for (var j = 0; j < dataset[i].length; j++) {
var obj = dataset[i][j];
if(obj.y != 0) { num_projects[obj.x]++; }
}
}
//console.log(num_projects);
return num_projects;
}
/**
* Calculates number of units (worksets or lanes) per domain (x value) in a layer object data set
* @param {Array} dataset Array of array of layer objects
* @param {Array} domain Array of domain names (x values)
* @param {String} [unit="lanes"] Name of unit. Specify "samples" to get number of worksets, otherwise number of lanes
* @returns {Object} An object with counts per domain.
*/
function numUnits(dataset, domain, unit) {
var num_u = {};
for (var i = 0; i < domain.length; i++) {
num_u[domain[i]] = 0;
}
//console.log(num_u);
for(var i = 0; i < dataset.length; i++) {
for (var j = 0; j < dataset[i].length; j++) {
var obj = dataset[i][j];
if(obj.y != 0) { num_u[obj.x] += obj.y; }
}
}
for (c in num_u) {
if (unit == "samples") { // convert samples to worksets
if (num_u[c] != 0) {
num_u[c] = Math.ceil(num_u[c]/96);
}
} else { // round off lane counts to one digit
num_u[c] = parseFloat(num_u[c]).toFixed(1);
}
}
//console.log(num_u);
return num_u;
}
/**
* Calculates the total y value for all stacked bars per domain (x value) in a layer object data set
* @param {Array} dataset Array of array of layer objects
* @param {Array} domain Array of domain names (x values)
* @returns {Object} An object with counts per domain
*/
function totalY(dataset, domain) {
var tot = {};
for (var i = 0; i < domain.length; i++) {
tot[domain[i]] = 0;
}
//console.log(num_projects);
for(var i = 0; i < dataset.length; i++) {
for (var j = 0; j < dataset[i].length; j++) {
var obj = dataset[i][j];
tot[obj.x] += obj.y;
}
}
//console.log(tot);
return tot;
}
/* **** Data transformation functions **** */
/**
* Generates a dataset for Rec Ctrl project load for stacked bar chart drawing
* @param {Object} json A parsed json stream object at sample level
* @param {Date} cmpDate A Date object to specify load date
* @returns {Array} An array of arrays of "layer" objects
*/
function generateRecCtrlStackDataset(json, cmpDate) {
var dateFormat = d3.time.format("%Y-%m-%d");
var cmpDateStr = dateFormat(cmpDate); // Turn cmp date into a string to compare to dates in data
var dataArray = [];
var rows = json["rows"];
var applBins = {};
var cat = ["DNA", "RNA", "SeqCap", "Other", "FinLib", "Application"];
for (i = 0; i < cat.length; i++) {
//console.log("adding " + cat[i]);
applBins[cat[i]] = { count: 0 };// initialize count for each category
}
//console.log(applBins);
var projects = {};
// loop through each sample and add upp num of projects
for (var i = 0; i < rows.length; i++) {
var k = rows[i]["key"];
//console.log(k)
var pid = k[0];
var type = k[1];
var appl = k[2];
var sampleID = k[4];
// skip projects that are labeled as neither Production or Application
if (type == null ) { continue; }
if(appl == null) { appl = "Other";}
//console.log(sampleID);
var applCat = "";
if(type == "Application") {
applCat = "Application"
} else if (appl.indexOf("capture") != -1) {
applCat = "SeqCap";
} else if (appl == "Amplicon" ||
appl == "de novo" ||
appl == "Metagenome" ||
appl == "WG re-seq") {
applCat = "DNA";
} else if (appl == "RNA-seq (total RNA)") {
applCat = "RNA";
} else if (appl == "Finished library") {
applCat = "FinLib";
} else {
applCat = "Other";
}
var v = rows[i]["value"];
// skip aborted projects
var aborted_date = v["Aborted date"];
if (aborted_date != "0000-00-00") {
//console.log("Skipping " + keys[0]);
continue;
}
// Skip aborted or finished *samples*
if (v["Status"] == "Aborted" || v["Status"] == "Finished" ) {
continue;
}
// skip closed projects
var closeDate = v["Close date"];
if(closeDate != "0000-00-00") { continue; }
// skip projects that have passed later process steps
var libQCDate = v["QC library finished"];
var seqDoneDate = v["All samples sequenced"];
if (appl != "Finished library" && libQCDate != "0000-00-00") {
continue;
}
if (seqDoneDate != "0000-00-00") {
continue;
}
var rawDataDelivered = v["All raw data delivered"];
if (rawDataDelivered != "0000-00-00") { continue; }
var arrDate = v["Arrival date"];
var queueDate = v["Queue date"];
if (arrDate != "0000-00-00" &&
arrDate <= cmpDateStr &&
queueDate == "0000-00-00") {
// initialize a value for the project if it doesn't exist in applBins
if(applBins[applCat][pid] == undefined) { //this is only done once for each project
applBins[applCat][pid] = 1; // just add a value for the project
applBins[applCat]["count"]++;
//console.log(pid + ", " + type + ", " + appl + ", " + applCat);
}
//if(projects[pid] == undefined) { // is this needed?
// projects[pid] = { cat: applCat, arrivalDate: arrDate, queueDate: queueDate}
//}
}
}
//console.log(pfBins);
// put into "layer structure", sort & then add up y0's
//console.log(applBins);
var projArr = [];
for (i = 0; i < cat.length; i++) {
var o = { x: "RecCtrl", y: applBins[cat[i]]["count"], cat: cat[i] };
var proj;
for(var p in applBins[cat[i]]) {
if(p.indexOf("P") == -1) { continue; }
if (proj == undefined) {
proj = p;
} else {
proj += ", " + p;
}
}
o["projects"] = proj;
proj = undefined;
projArr.push(o);
}
tot = 0;
for (var i = 0; i < projArr.length; i++) {
projArr[i]["y0"] = tot;
tot += projArr[i]["y"];
}
return [projArr];
}
/**
* Generates a dataset for Libprep queue lane load for stacked bar chart drawing
* @param {Object} json A parsed json stream object at sample level
* @param {Date} cmpDate A Date object to specify load date
* @returns {Array} An array of arrays of "layer" objects
*/
function generateQueueLaneLPStackDataset(json, cmpDate, ptype) {
var dateFormat = d3.time.format("%Y-%m-%d");
var cmpDateStr = dateFormat(cmpDate); // Turn cmp date into a string to compare to dates in data
var dataArray = [];
var rows = json["rows"];
var pfBins = {};
var projects = {};
ptype = typeof ptype !== 'undefined' ? ptype : "Production"; //Default is production, unless something else is given
// loop through each sample and add upp lane load per project
for (var i = 0; i < rows.length; i++) {
//console.log("looping through json array: 1");
var k = rows[i]["key"];
var pid = k[0];
var type = k[1];
var appl = k[2];
if (type != ptype) { continue; } // only matching ptype projectsprojects (Application or Production) will be returned.
if (appl == "Finished library") { continue; } // need seq start date to be able to handle fin lib projects
// Determine which platform
var pf = k[3];
var otherPf = "MiSeq";
if (pf != "MiSeq") {
pf = "HiSeq";
} else {
otherPf = "HiSeq";
}
var v = rows[i]["value"];
// skip aborted projects
var aborted_date = v["Aborted date"];
if (aborted_date != "0000-00-00") {
//console.log("Skipping " + keys[0]);
continue;
}
// Skip aborted or finished *samples*
if (v["Status"] == "Aborted" || v["Status"] == "Finished" ) {
continue;
}
// skip closed projects
var closeDate = v["Close date"];
if(closeDate != "0000-00-00") { continue; }
// skip samples already done, but where dates are missing in lims
var libQCDate = v["QC library finished"];
if (libQCDate != "0000-00-00") { continue; }
var seqStartDate = v["Sequencing start"];
if (seqStartDate != "0000-00-00") { continue; }
var seqFinishedDate = v["All samples sequenced"];
if (seqFinishedDate != "0000-00-00") { continue; }
var rawDataDelivered = v["All raw data delivered"];
if (rawDataDelivered != "0000-00-00") { continue; }
var queueDate = v["Queue date"];
var prepStartDate = v["Lib prep start"];
// this is for libprep projects
if (queueDate != "0000-00-00" &&
queueDate <= cmpDateStr &&
prepStartDate == "0000-00-00") {
//console.log(pf + ", " + pid + ", " + v["Lanes"]);
// create bins for the platforms if they don't exist
if(pfBins[pf] == undefined) {
pfBins[pf] = {};
}
if(pfBins[otherPf] == undefined) {
pfBins[otherPf] = {};
}
// initialize a value for the project if it doesn't exist in pfBins
if(pfBins[pf][pid] == undefined) {
pfBins[pf][pid] = 0;
}
if(pfBins[otherPf][pid] == undefined) {
pfBins[otherPf][pid] = 0;
}
// add on lane load for this particular project
pfBins[pf][pid] += v["Lanes"];
if(projects[pid] == undefined) {
projects[pid] = { queueDate: queueDate }
}
}
}
//console.log(pfBins);
// remove proj name??????
// put into "layer structure", sort & then add up y0's
for (var projID in pfBins["HiSeq"]) {
var hO = { x: "HiSeq", y: pfBins["HiSeq"][projID], pid: projID, queueDate: projects[projID]["queueDate"] };
var mO = { x: "MiSeq", y: pfBins["MiSeq"][projID], pid: projID, queueDate: projects[projID]["queueDate"] };
dataArray.push([hO, mO]);
}
dataArray.sort(sortByPlatform);
var tot = { HiSeq: 0, MiSeq: 0};
for (var i = 0; i < dataArray.length; i++) {
for (var j = 0; j < dataArray[i].length; j++) {
var pf = dataArray[i][j]["x"];
dataArray[i][j]["y0"] = tot[pf];
tot[pf] += dataArray[i][j]["y"];
}
}
//console.log(dataArray);
if (dataArray.length == 0 ) {
dataArray = [
[
{ x: "HiSeq", y: 0, y0: 0, pid: "Px", projName: "empty", queueDate: "0000-00-00"},
{ x: "MiSeq", y: 0, y0: 0, pid: "Px", projName: "empty", queueDate: "0000-00-00"}
]
];
}
return dataArray;
}
/**
* Generates a dataset for Finished lib queue lane load for stacked bar chart drawing
* @param {Object} json A parsed json stream object at sample level
* @param {Date} cmpDate A Date object to specify load date
* @returns {Array} An array of arrays of "layer" objects
*/
function generateQueueLaneFLStackDataset(json, cmpDate, ptype) {
var dateFormat = d3.time.format("%Y-%m-%d");
var cmpDateStr = dateFormat(cmpDate); // Turn cmp date into a string to compare to dates in data
// time limits in days to use for project bar colouring
var timeLimit1 = 10; // to lable yellow
var timeLimit2 = 20; // to lable red
var dataArray = [];
var rows = json["rows"];
var pfBins = {};
var projects = {};
ptype = typeof ptype !== 'undefined' ? ptype : "Production"; //Default is production, unless something else is given
// loop through each sample and add upp lane load per project
for (var i = 0; i < rows.length; i++) {
//console.log("looping through json array: 1");
var k = rows[i]["key"];
var pid = k[0];
var type = k[1];
var appl = k[2];
if (type != ptype){ continue; } // only ptype are of interest - is this true??
if (appl != "Finished library") { continue; } // skip fin lib projects
// Determine which platform
var pf = k[3];
var otherPf = "MiSeq";
if (pf != "MiSeq") {
pf = "HiSeq";
} else {
otherPf = "HiSeq";
}
var v = rows[i]["value"];
// skip aborted projects
var aborted_date = v["Aborted date"];
if (aborted_date != "0000-00-00") {
//console.log("Skipping " + k[0]);
continue;
}
// Skip aborted or finished *samples*
if (v["Status"] == "Aborted" || v["Status"] == "Finished" ) {
continue;
}
// skip closed projects
var closeDate = v["Close date"];
if(closeDate != "0000-00-00") { continue; }
// skip samples already done, but where dates are missing in lims
var seqStartDate = v["Sequencing start"];
if (seqStartDate != "0000-00-00") { continue; }
var seqFinishedDate = v["All samples sequenced"];
if (seqFinishedDate != "0000-00-00") { continue; }
var rawDataDelivered = v["All raw data delivered"];
if (rawDataDelivered != "0000-00-00") { continue; }
var queueDate = v["Queue date"];
//var prepStartDate = v["Lib prep start"];
var seqStartDate = v["Sequencing start"];
// this is for libprep projects
if (queueDate != "0000-00-00" &&
queueDate <= cmpDateStr &&
seqStartDate == "0000-00-00") {
//console.log(pf + ", " + pid + ", " + v["Lanes"]);
// create bins for the platforms if they don't exist
if(pfBins[pf] == undefined) {
pfBins[pf] = {};
}
if(pfBins[otherPf] == undefined) {
pfBins[otherPf] = {};
}
// initialize a value for the project if it doesn't exist in pfBins
if(pfBins[pf][pid] == undefined) {
pfBins[pf][pid] = 0;
}
if(pfBins[otherPf][pid] == undefined) {
pfBins[otherPf][pid] = 0;
}
// add on lane load for this particular project
pfBins[pf][pid] += v["Lanes"];
if(projects[pid] == undefined) {
// set a limit indicator based on how long the project has been going on
// the limit variables are set at the beginning of this function
// these limits can be used to set colouring of project bars
var ongoingTime = daydiff(new Date(queueDate), cmpDate);
var passedTimeLimit = 0;
if (ongoingTime > timeLimit2) {
passedTimeLimit = 2;
} else if (ongoingTime > timeLimit1) {
passedTimeLimit = 1;
}
//console.log(pid + " passedTimeLimit: " + passedTimeLimit)
projects[pid] = { queueDate: queueDate, passedTimeLimit: passedTimeLimit }
//projects[pid] = { queueDate: queueDate}
}
}
}
//console.log(pfBins);
// put into "layer structure", sort & then add up y0's
for (var projID in pfBins["HiSeq"]) {
var hO = { x: "HiSeq", y: pfBins["HiSeq"][projID], pid: projID, queueDate: projects[projID]["queueDate"], passedTimeLimit: projects[projID]["passedTimeLimit"] };
var mO = { x: "MiSeq", y: pfBins["MiSeq"][projID], pid: projID, queueDate: projects[projID]["queueDate"], passedTimeLimit: projects[projID]["passedTimeLimit"] };
dataArray.push([hO, mO]);
}
dataArray.sort(sortByPlatform);
var tot = { HiSeq: 0, MiSeq: 0};
for (var i = 0; i < dataArray.length; i++) {
for (var j = 0; j < dataArray[i].length; j++) {
var pf = dataArray[i][j]["x"];
dataArray[i][j]["y0"] = tot[pf];
tot[pf] += dataArray[i][j]["y"];
}
}
//console.log(dataArray);
if (dataArray.length == 0 ) {
dataArray = [
[
{ x: "HiSeq", y: 0, y0: 0, pid: "Px", projName: "empty", queueDate: "0000-00-00"},
{ x: "MiSeq", y: 0, y0: 0, pid: "Px", projName: "empty", queueDate: "0000-00-00"}
]
];
}
return dataArray;
}
/**
* Generates a dataset for Libprep queue sample load for stacked bar chart drawing
* @param {Object} json A parsed json stream object at sample level
* @param {Date} cmpDate A Date object to specify load date
* @returns {Array} An array of arrays of "layer" objects
*/
function generateQueueSampleStackDataset(json, cmpDate, ptype) {
var dateFormat = d3.time.format("%Y-%m-%d");
var cmpDateStr = dateFormat(cmpDate); // Turn cmp date into a string to compare to dates in data
// time limits in days to use for project bar colouring
var timeLimit1 = 10; // to lable yellow
var timeLimit2 = 20; // to lable red
var dataArray = [];
var rows = json["rows"];
var applBins = {};
var cat = ["DNA", "RNA", "SeqCap", "Other"];
for (i = 0; i < cat.length; i++) {
//console.log("adding " + cat[i]);
applBins[cat[i]] = {};
}
//console.log(applBins);
ptype = typeof ptype !== 'undefined' ? ptype : "Production"; //Default is production, unless something else is given
// array to capture libprep start dates
var libPrepStartDates = [];
var projects = {};
// loop through each sample and add upp lane load per project
for (var i = 0; i < rows.length; i++) {
var k = rows[i]["key"];
//console.log(k)
var pid = k[0];
var type = k[1];
var appl = k[2];
var sampleID = k[4];
if (type != ptype){ continue; } // only ptype projects of interest
if (appl == "Finished library") { continue; } // fin lib projects not of interest
if(appl == null) { appl = "Other";}
//console.log(sampleID);
var applCat = "";
if (appl.indexOf("capture") != -1) {
applCat = "SeqCap";
} else if (appl == "Amplicon" ||
appl == "de novo" ||
appl == "Metagenome" ||
appl == "WG re-seq" ||
appl == "WG re-seq (IGN)"
) {
applCat = "DNA";
} else if (appl == "RNA-seq (total RNA)") {
applCat = "RNA";
} else {
applCat = "Other";
}
var v = rows[i]["value"];
// skip aborted projects
var aborted_date = v["Aborted date"];
if (aborted_date != "0000-00-00") {
//console.log("Skipping " + keys[0]);
continue;
}
// Skip aborted or finished *samples*
if (v["Status"] == "Aborted" || v["Status"] == "Finished" ) {
continue;
}
// skip closed projects
var closeDate = v["Close date"];
if(closeDate != "0000-00-00") { continue; }
// skip samples already done, but where dates are missing in lims
var libQCDate = v["QC library finished"];
if (libQCDate != "0000-00-00") { continue; }
var seqStartDate = v["Sequencing start"];
if (seqStartDate != "0000-00-00") { continue; }
var seqFinishedDate = v["All samples sequenced"];
if (seqFinishedDate != "0000-00-00") { continue; }
var rawDataDelivered = v["All raw data delivered"];
if (rawDataDelivered != "0000-00-00") { continue; }
var arrivalDate = v["Arrival date"];
var queueDate = v["Queue date"];
var prepStartDate = v["Lib prep start"];
libPrepStartDates.push(prepStartDate);
// this is for libprep projects
if (queueDate != "0000-00-00" &&
queueDate <= cmpDateStr &&
prepStartDate == "0000-00-00") {
//console.log("To add - app cat: " + applCat + ", pid: " + pid + ", sample: " + sampleID);
// initialize a value for the project for all applications if it doesn't exist in applBins
if(applBins[applCat][pid] == undefined) {
//for (c in cat) {
// applBins[c][pid] = 0;
//}
for (var j = 0; j < cat.length; j++) {
applBins[cat[j]][pid] = 0;
}
}
// add sample load for this particular project
applBins[applCat][pid] += 1;
if(projects[pid] == undefined) {
// set a limit indicator based on how long the project has been going on
// the limit variables are set at the beginning of this function
// these limits can be used to set colouring of project bars
var ongoingTime = daydiff(new Date(queueDate), cmpDate);
var passedTimeLimit = 0;
if (ongoingTime > timeLimit2) {
passedTimeLimit = 2;
} else if (ongoingTime > timeLimit1) {
passedTimeLimit = 1;
}
//console.log(pid + " passedTimeLimit: " + passedTimeLimit)
projects[pid] = { queueDate: queueDate, arrivalDate: arrivalDate, passedTimeLimit: passedTimeLimit }
}
}
}
//console.log(pfBins);
// get the last prep start date
// filter function to remove duplicates
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
var libPrepStartDates = libPrepStartDates.filter( onlyUnique );
libPrepStartDates.sort();
var ps = libPrepStartDates.pop(); // last date
while (ps > cmpDateStr ) { // continue pop'ing until last date is less than comparison date
ps = libPrepStartDates.pop();
}
var lastLibPrepStart = ps;
var daysSincePrepStart = daydiff(new Date(lastLibPrepStart), cmpDate);
// put into "layer structure", sort & then add up y0's
for (var projID in applBins["DNA"]) {
var projArr = [];
//for (c in cat) {
for (i = 0; i < cat.length; i++) {
//var o = { x: cat[i], y: applBins[cat[i]][projID], pid: projID, queueDate: projects[projID]["queueDate"], arrivalDate: projects[projID]["arrivalDate"] };
var o = { x: cat[i], y: applBins[cat[i]][projID], pid: projID, queueDate: projects[projID]["queueDate"], arrivalDate: projects[projID]["arrivalDate"], passedTimeLimit: projects[projID]["passedTimeLimit"] };
projArr.push(o);
}
dataArray.push(projArr);
}
// change to sort by application
dataArray.sort(sortByApplication); // by application & queue date - arrival date - project ID
var firstInQueuePid = getFirstInQueue(dataArray);
//console.log("first in queue pid: " + firstInQueuePid);
var tot = { DNA: 0, RNA: 0, SeqCap: 0, Other: 0};
for (var i = 0; i < dataArray.length; i++) {
for (var j = 0; j < dataArray[i].length; j++) {
var a = dataArray[i][j]["x"];
dataArray[i][j]["y0"] = tot[a];
tot[a] += dataArray[i][j]["y"];
//if (i == 0) { // add info about time since last libprep start for the project first in queue
if (dataArray[i][j]["pid"] == firstInQueuePid) { // add info about time since last libprep start for the project first in queue
dataArray[i][j]["lastLibPrep"] = lastLibPrepStart;
dataArray[i][j]["daysSincePrepStart"] = daysSincePrepStart;
}
}
}
//console.log(dataArray);
//return pfBins;
if (dataArray.length == 0 ) {
dataArray = [
[
{ x: "DNA", y: 0, y0: 0, pid: "Px", projName: "empty", queueDate: "0000-00-00"},
{ x: "RNA", y: 0, y0: 0, pid: "Px", projName: "empty", queueDate: "0000-00-00"},
{ x: "SeqCap", y: 0, y0: 0, pid: "Px", projName: "empty", queueDate: "0000-00-00"},
{ x: "Other", y: 0, y0: 0, pid: "Px", projName: "empty", queueDate: "0000-00-00"}
]
];
}
return dataArray;
}
/**
* Generates a dataset for Libprep ongoing sample load for stacked bar chart drawing
* @param {Object} json A parsed json stream object at sample level
* @param {Date} cmpDate A Date object to specify load date
* @returns {Array} An array of arrays of "layer" objects
*/
function generateLibprepSampleLoadDataset(json, cmpDate, ptype) {
var dateFormat = d3.time.format("%Y-%m-%d");
var cmpDateStr = dateFormat(cmpDate); // Turn cmp date into a string to compare to dates in data
// time limits in days to use for project bar colouring
var timeLimit1 = 42; // to lable yellow
var timeLimit2 = 63; // to lable red
var dataArray = [];
var rows = json["rows"];
var applBins = {};
var cat = ["DNA", "RNA", "SeqCap", "Other"];
for (i = 0; i < cat.length; i++) {
//console.log("adding " + cat[i]);
applBins[cat[i]] = {};
}
//console.log(applBins);
ptype = typeof ptype !== 'undefined' ? ptype : "Production"; //Default is production, unless something else is given
var projects = {};
// loop through each sample and add upp lane load per project
for (var i = 0; i < rows.length; i++) {
var k = rows[i]["key"];
var pid = k[0];
var type = k[1];
var appl = k[2];
var sampleID = k[4];
if (appl == "Finished library") { continue; } // fin lib projects not of interest
if (type != ptype) { continue; } // only projects of ptype are of interest
if(appl == null) { appl = "Other";}
//console.log(sampleID);
var applCat = "";
if (appl.indexOf("capture") != -1) {
applCat = "SeqCap";
} else if (appl == "Amplicon" ||
appl == "de novo" ||
appl == "Metagenome" ||
appl == "WG re-seq") {
applCat = "DNA";
} else if (appl == "RNA-seq (total RNA)") {
applCat = "RNA";
} else {
applCat = "Other";
}
var v = rows[i]["value"];
// skip aborted projects
var aborted_date = v["Aborted date"];
if (aborted_date != "0000-00-00") {
//console.log("Skipping " + keys[0]);
continue;
}
// Skip aborted or finished *samples*
if (v["Status"] == "Aborted" || v["Status"] == "Finished" ) {
continue;
}
// skip closed projects
var closeDate = v["Close date"];
if(closeDate != "0000-00-00") { continue; }
// skip samples already done, but where dates are missing in lims
var seqStartDate = v["Sequencing start"];
if (seqStartDate != "0000-00-00") { continue; }
var seqFinishedDate = v["All samples sequenced"];
if (seqFinishedDate != "0000-00-00") { continue; }
var rawDataDelivered = v["All raw data delivered"];
if (rawDataDelivered != "0000-00-00") { continue; }
var queueDate = v["Queue date"];
var prepStartDate = v["Lib prep start"];
var libQCDate = v["QC library finished"];
//console.log("app cat: " + applCat + ", pid: " + pid + ", sample: " + sampleID + ", " + prepStartDate + "-" + libQCDate);
// this is for libprep projects
if (prepStartDate != "0000-00-00" &&
prepStartDate <= cmpDateStr &&
libQCDate == "0000-00-00") {
//console.log("To add - app cat: " + applCat + ", pid: " + pid + ", sample: " + sampleID);
// initialize a value for the project for all applications if it doesn't exist in applBins
if(applBins[applCat][pid] == undefined) {
for (var j = 0; j < cat.length; j++) {
applBins[cat[j]][pid] = 0;
}
}
// add sample load for this particular project
applBins[applCat][pid] += 1;
if(projects[pid] == undefined) {
// set a limit indicator based on how long the project has been going on
// the limit variables are set at the beginning of this function
// these limits can be used to set colouring of project bars
var ongoingTime = daydiff(new Date(queueDate), cmpDate);
var passedTimeLimit = 0;
if (ongoingTime > timeLimit2) {
passedTimeLimit = 2;
} else if (ongoingTime > timeLimit1) {
passedTimeLimit = 1;
}
projects[pid] = { queueDate: queueDate, pid: pid, passedTimeLimit: passedTimeLimit }
//projects[pid] = { queueDate: queueDate, pid: pid}
}
}
}
//console.log(pfBins);
// put into "layer structure", sort & then add up y0's
for (var projID in applBins["DNA"]) {
var projArr = [];
for (i = 0; i < cat.length; i++) {
//var o = { x: cat[i], y: applBins[cat[i]][projID], pid: projID, projName: projects[projID]["projName"], queueDate: projects[projID]["queueDate"] };
var o = { x: cat[i], y: applBins[cat[i]][projID], pid: projID, projName: projects[projID]["projName"], queueDate: projects[projID]["queueDate"], passedTimeLimit: projects[projID]["passedTimeLimit"] };
projArr.push(o);
}
dataArray.push(projArr);
}
// change to sort by application
dataArray.sort(sortByApplication);
var tot = { DNA: 0, RNA: 0, SeqCap: 0, Other: 0};
for (var i = 0; i < dataArray.length; i++) {
for (var j = 0; j < dataArray[i].length; j++) {
var a = dataArray[i][j]["x"];
dataArray[i][j]["y0"] = tot[a];
tot[a] += dataArray[i][j]["y"];
}
}
if (dataArray.length == 0 ) {
dataArray = [
[
{ x: "DNA", y: 0, y0: 0, pid: "Px", projName: "empty", queueDate: "0000-00-00"},
{ x: "RNA", y: 0, y0: 0, pid: "Px", projName: "empty", queueDate: "0000-00-00"},
{ x: "SeqCap", y: 0, y0: 0, pid: "Px", projName: "empty", queueDate: "0000-00-00"},
{ x: "Other", y: 0, y0: 0, pid: "Px", projName: "empty", queueDate: "0000-00-00"}
]
];
}
return dataArray;
}
/**
* Generates a dataset for Libprep ongoing lane load for stacked bar chart drawing
* @param {Object} json A parsed json stream object at sample level
* @param {Date} cmpDate A Date object to specify load date
* @returns {Array} An array of arrays of "layer" objects
*/
function generateLibprepLaneLoadDataset(json, cmpDate, ptype) {
var dateFormat = d3.time.format("%Y-%m-%d");
var cmpDateStr = dateFormat(cmpDate); // Turn cmp date into a string to compare to dates in data
var dataArray = [];
var rows = json["rows"];
var pfBins = {};
var projects = {};
ptype = typeof ptype !== 'undefined' ? ptype : "Production"; //Default is production, unless something else is given
for (var i = 0; i < rows.length; i++) {
var k = rows[i]["key"];
var pid = k[0];
var type = k[1];
var appl = k[2];
if (appl == "Finished library") { continue; } // skip fin lib projects
if (type != ptype) { continue; } // only ptype projects are of interest
// Determine which platform
var pf = k[3];
var otherPf = "MiSeq";
if (pf != "MiSeq") {
pf = "HiSeq";
} else {
otherPf = "HiSeq";
}
var v = rows[i]["value"];
// skip aborted projects
var aborted_date = v["Aborted date"];
if (aborted_date != "0000-00-00") {
//console.log("Skipping " + keys[0]);
continue;
}
// Skip aborted or finished *samples*
if (v["Status"] == "Aborted" || v["Status"] == "Finished" ) {
continue;
}
// skip closed projects
var closeDate = v["Close date"];
if(closeDate != "0000-00-00") { continue; }
// skip samples already done, but where dates are missing in lims
var seqStartDate = v["Sequencing start"];
if (seqStartDate != "0000-00-00") { continue; }
var seqFinishedDate = v["All samples sequenced"];
if (seqFinishedDate != "0000-00-00") { continue; }
var rawDataDelivered = v["All raw data delivered"];
if (rawDataDelivered != "0000-00-00") { continue; }
var queueDate = v["Queue date"];
var prepStartDate = v["Lib prep start"];
//console.log(prepStartDate);
var libQCDate = v["QC library finished"];
// this is for libprep projects
if (prepStartDate != "0000-00-00" &&
prepStartDate <= cmpDateStr &&
libQCDate == "0000-00-00") {
//console.log(pf + ", " + pid + ", " + v["Lanes"]);
// create bins for the platforms if they don't exist
if(pfBins[pf] == undefined) {
pfBins[pf] = {};
}
if(pfBins[otherPf] == undefined) {
pfBins[otherPf] = {};
}
// initialize a value for the project if it doesn't exist in pfBins
if(pfBins[pf][pid] == undefined) {
pfBins[pf][pid] = 0;
}
if(pfBins[otherPf][pid] == undefined) {
pfBins[otherPf][pid] = 0;
}
// add on lane load for this particular project
pfBins[pf][pid] += v["Lanes"];
if(projects[pid] == undefined) {
projects[pid] = { queueDate: queueDate, pid: pid}
}
}
}
//console.log(pfBins);
// put into "layer structure", sort & then add up y0's
for (var projID in pfBins["HiSeq"]) {
var hO = { x: "HiSeq", y: pfBins["HiSeq"][projID], pid: projID, projName: projects[projID]["projName"], queueDate: projects[projID]["queueDate"] };
var mO = { x: "MiSeq", y: pfBins["MiSeq"][projID], pid: projID, projName: projects[projID]["projName"], queueDate: projects[projID]["queueDate"] };
dataArray.push([hO, mO]);
}
dataArray.sort(sortByPlatform);
var tot = { HiSeq: 0, MiSeq: 0};
for (var i = 0; i < dataArray.length; i++) {
for (var j = 0; j < dataArray[i].length; j++) {