forked from SciLifeLab/external_kpis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
external_kpis.js
1524 lines (1357 loc) · 52.4 KB
/
external_kpis.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
// Lookup table for organisations
var organisationNames = {
"GU": "Göteborgs universitet",
"KI": "Karolinska institutet",
"KTH": "Kungliga tekniska högskolan",
"KS": "Karolinska universitetssjukh.",
"LU": "Lunds universitet",
"LiU": "Linköpings universitet",
"Lnu": "Linnéuniversitetet i Kalmar",
"NRM": "Naturhistoriska riksmuséet",
"SH": "Södertörns högskola",
"SLU-Alnarp": "Sv. lantbruksuniv. - Alnarp",
"SLU-Uppsala": "Sv. lantbruksuniv. - Uppsala",
"SLU-Umea": "Sv. lantbruksuniv. - Umeå",
"SMI": "Smittskyddsinstitutet",
"SU": "Stockholms universitet",
"SVA": "Statens veterinärmed. anstalt",
"UU": "Uppsala universitet",
"UmU": "Umeå universitet",
}
// Lookup table for applications
// Maps our application names to categories agreed with Uppsala
//
// Does not catch the Epigenetics category (Methylation, Bishulphite, MedIP, RRBS, ...)
var applicationNames = {
"Amplicon": "Target re-seq",
"cDNA": "RNA-seq",
"ChIP-seq": "",
"Custom capture": "Target re-seq",
"de novo": "de novo seq",
"Exome capture": "Target re-seq",
"Mate-pair": "de novo seq",
"Metagenome": "Metagenomics",
"miRNA-seq": "",
"RNA-seq (mRNA)": "RNA-seq",
"RNA-seq (RiboZero)": "RNA-seq",
"RNA-seq (Ribominus)": "RNA-seq",
"RNA-seq (total RNA)": "RNA-seq",
"Special": "Other",
"WG re-seq": "Whole genome re-seq",
"WG re-seq (IGN)": "Whole genome re-seq (hum)",
"Finished library": "",
}
/* **** Helper functions **** */
/**
* Sort function for delivery time bins
* @param {Object} a Time bin object
* @param {Object} b Time bin object
* @returns {Number} A negative number if a should be sorted before b, a positive number if vice versa, otherwise 0
*/
function sortCats(a, b) {
var order = [];
order["0-6 w"] = 1;
order["6-12 w"] = 2;
order["12-24 w"] = 3;
order["24-52 w"] = 4;
order["Not closed"] = 5;
return order[a.key] - order[b.key];
}
/**
* Sort function for d3.pie to sort Finished library bin last
* @param {Object} a Application bin object
* @param {Object} b Application bin object
* @returns {Number} A negative number if a should be sorted before b, a positive number if vice versa, otherwise 0
*/
function sortFinLibLast(a, b) {
// Strangely, the order of Objects in the array in the d3.pie object will not change, only the
// location where the arc for Finished library will be drawn.
// That's why there is another sort function (sortPieDataFinLibLast) below to then be able
// to sort the transformed array of a d3.pie object so that the Finished library bin object
// comes last when we feed that to the pie and legend drawing code (to get the colours to be the same)
// 'Finished library' last
if (a.key == "Finished library") {
return 1;
} else if (b.key == "Finished library") {
return -1;
}
// 'Other' second to last
if (a.key == "Other") {
return 1;
} else if (b.key == "Other") {
return -1;
}
if (a.key.toLowerCase() < b.key.toLowerCase()) {
return -1;
} else if (a.key.toLowerCase() > b.key.toLowerCase()) {
return 1;
}
return 0;
}
/**
* Sort function for d3.pie-transformed data array to sort Finished library bin Object last
* @param {Object} a "d3.pie.arc" object
* @param {Object} b "d3.pie.arc" object
* @returns {Number} A negative number if a should be sorted before b, a positive number if vice versa, otherwise 0
*/
function sortPieDataFinLibLast(a, b) {
// See comment above in sortFinLibLast function
// 'Finished library' last
if (a.data.key == "Finished library") {
return 1;
} else if (b.data.key == "Finished library") {
return -1;
}
// 'Other' second to last
if (a.data.key == "Other") {
return 1;
} else if (b.data.key == "Other") {
return -1;
}
if (a.data.key.toLowerCase() < b.data.key.toLowerCase()) {
return -1;
} else if (a.data.key.toLowerCase() > b.data.key.toLowerCase()) {
return 1;
}
return 0;
}
/**
* 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;
}
/**
* 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;
}
/**
* Creates data set with project counts per time bin since a specified start date
* @param {Object} json A parsed json stream
* @param {Date} startDate A Date object to specify start of date range to include
* @returns {Object} An object with project counts per time bin
*/
function makeDeltimeDataset(json, startDate) {
var data = [];
var rows = json["rows"];
//console.log(rows);
var dformat = d3.time.format("%Y-%m-%d");
var startDateStr;
if (startDate) {
startDateStr = dformat(startDate);
//console.log("start date: " + startDateStr);
}
//console.log(rows);
var nums = new Object;
var total = 0;
for (var i = 0; i < rows.length; i++) {
var ka = rows[i]["key"];
var bin = ka[0]; // bin name
var v = rows[i]["value"];
var qd = v["Queue date"];
//console.log(bin + ", " + qd);
if(bin == null) { continue; }
//if(bin == null) {
// if(qd == "0000-00-00") { continue; }
// bin = "Not closed";
//}
if(startDateStr != undefined && qd < startDateStr) {
//console.log("skipping");
continue;
} //skip if queue date is before startDate
if(nums[bin]) {
nums[bin]++;
} else {
nums[bin] = 1;
}
total++;
}
for(bin in nums) {
data.push( {"key": bin, "value": nums[bin], "percent": Math.round(100 * nums[bin]/total) } );
}
return data.sort(sortCats);
}
/**
* Creates data set with project counts per application since a specified start date
* @param {Object} json A parsed json stream
* @param {Date} startDate A Date object to specify start of date range to include
* @returns {Object} An object with project counts per application
*/
function makeApplProjDataset(json, startDate) {
var data = [];
var rows = json["rows"];
//console.log(rows);
var dformat = d3.time.format("%Y-%m-%d");
var startDateStr;
if (startDate) {
startDateStr = dformat(startDate);
//console.log("start date: " + startDateStr);
}
//console.log(rows);
var nums = new Object;
for (var i = 0; i < rows.length; i++) {
var application = rows[i]["key"];
// Check lookup table if it belongs to a category or should have a more verbose name
if(applicationNames[application]) { application = applicationNames[application]; }
var v = rows[i]["value"];
var od = v[1];
//console.log(bin + ", " + qd);
if(startDateStr != undefined && od < startDateStr) {
//console.log("skipping");
continue;
} //skip if queue date is before startDate
if(nums[application]) {
nums[application]++;
} else {
nums[application] = 1;
}
}
for(application in nums) {
data.push( {"key": application, "value": nums[application]} );
}
return data;
}
/**
* Creates data set with sample counts per application since a specified start date
* @param {Object} json A parsed json stream
* @param {Date} startDate A Date object to specify start of date range to include
* @param {boolean} includeFinishedLibrary Whether Finished Library projects should be included or not
* @returns {Object} An object with sample counts per application
*/
function makeApplSampleDataset(json, startDate, includeFinishedLibrary) {
var data = [];
var rows = json["rows"];
//console.log(rows);
var dformat = d3.time.format("%Y-%m-%d");
var startDateStr;
if (startDate) {
startDateStr = dformat(startDate);
//console.log("start date: " + startDateStr);
}
//console.log(rows);
var nums = new Object;
for (var i = 0; i < rows.length; i++) {
var application = rows[i]["key"];
// Check lookup table if it belongs to a category or should have a more verbose name
if(applicationNames[application]) { application = applicationNames[application]; }
var v = rows[i]["value"];
var od = v[1];
var samples = v[0];
//console.log(bin + ", " + qd);
if(startDateStr != undefined && od < startDateStr) {
//console.log("skipping");
continue;
} //skip if queue date is before startDate
if (!includeFinishedLibrary && application == "Finished library") {
samples = 0; // set to a value to get Finished libraries as a category for the legend
}
if(nums[application]) {
nums[application] += samples;
} else {
nums[application] = samples;
}
}
for(application in nums) {
data.push( {"key": application, "value": nums[application]} );
}
return data;
}
/**
* Creates data set with read(-pair) counts per lane since a specified start date and sequencing mode
* @param {Object} json A parsed json stream
* @param {Date} startDate A Date object to specify start of date range to include
* @param {string} filter The sequencing mode for which data should be extracted
* @returns {Object} An array of lane read(-pair) counts in millions
*/
function makeReadsDataset(json, startDate, filter) {
var rows = json.rows;
var values = [];
var dformat = d3.time.format("%y%m%d");
var startDateStr;
if (startDate) {
startDateStr = dformat(startDate);
//console.log("start date: " + startDateStr);
}
for(var i = 0; i < rows.length; i++) {
var mode = rows[i]["key"][0];
var fc_id= rows[i]["key"][1];
var date = fc_id.split("_")[0];
if(startDateStr != undefined && date < startDateStr) {
continue;
}
if(filter != undefined) {
if (mode != filter) { continue; }
}
//console.log("mode: " + mode);
var val = rows[i]["value"];
if(val != null) { values.push(val/1e6); }
}
return values;
}
/**
* Creates data set with number of projects per organisation since a specified start date
* @param {Object} json A parsed json stream
* @param {Date} startDate A Date object to specify start of date range to include
* @returns {Array} An array of organisation bin objects
*/
function makeAffiliationDataset(json, startDate) {
var rows = json.rows;
//console.log(rows);
var nums = new Object;
for (var i = 0; i < rows.length; i++) {
var date;
if(startDate != undefined) {
date = new Date(rows[i]["key"][0]);
if (date < startDate) { continue; }
}
var aff = rows[i]["key"][1];
// Check lookup table if there is a longer name
if(organisationNames[aff]) { aff = organisationNames[aff]; }
if(nums[aff]) {
nums[aff]++;
} else {
nums[aff] = 1;
}
}
var data = [];
for(a in nums) {
if(a != "null") {
data.push( {"key": a, "value": nums[a]} );
}
}
data.sort(function (a,b) {
return a.key > b.key?1:-1;
});
if(nums["null"]) { // Call this category Other, even if not really true
data.push( {"key": "Other", "value": nums["null"]} );
}
//console.log(data);
return data;
}
// Data set generators for queue viz
/**
* 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 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);
// 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 " + 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 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) {
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") {
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 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 (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) {
projects[pid] = { queueDate: queueDate, arrivalDate: arrivalDate }
}
}
}
//console.log(pfBins);
// 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"] };
projArr.push(o);
}
dataArray.push(projArr);
}
// change to sort by application
dataArray.sort(sortByApplication); // by application & queue date - arrival date - project ID
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"];
}
}
//console.log(dataArray);
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;
}
/**
* Draws a "half" pie chart of delivery time bins
* @param {Object} dataset Parsed data
*/
function drawDelTimes(dataset) {
var w = 350;
var h = 200;
var outerRadius = w / 2;
var innerRadius = w / 10;
var startA = - Math.PI/2;
var endA = Math.PI/2
var arc = d3.svg.arc()
.innerRadius(innerRadius)
.outerRadius(outerRadius);
var pie = d3.layout.pie()
.value(function(d) {
return d.value;
})
.sort(null)
.startAngle(startA)
.endAngle(endA)
;
//Easy colors accessible via a 20-step ordinal scale
//var color = d3.scale.category10();
var color = d3.scale.category20();
//var color = d3.scale.category20b();
//var color = d3.scale.category20c();
//Create SVG element
var svg = d3.select("#delivery_times_plot")
.append("svg")
.attr("width", w)
.attr("height", h);
//Set up groups
var arcs = svg.selectAll("g.arc")
.data(pie(dataset))
.enter()
.append("g")
.attr("class", "arc")
.attr("transform", "translate(" + outerRadius + "," + (outerRadius + 20) + ")");
//Draw arc paths
arcs.append("path")
.attr("fill", function(d, i) {
return color(i);
})
.attr("d", arc);
//Labels
arcs.filter(function(d) { return d.endAngle - d.startAngle > .15; }).append("text") //Skip labels for smaller arcs
.attr("transform", function(d) {
return "translate(" + arc.centroid(d) + ")";
})
.attr("text-anchor", "middle")
.text(function(d) {
return d.data.key;
});
// Add a magnitude value to the larger arcs, translated to the arc centroid.
arcs.filter(function(d) { return d.endAngle - d.startAngle > .2; }).append("svg:text")
.attr("dy", "1.1em")
.attr("text-anchor", "middle")
.attr("transform", function(d) { //set the label's origin to the center of the arc
//we have to make sure to set these before calling arc.centroid
d.outerRadius = outerRadius; // Set Outer Coordinate
d.innerRadius = outerRadius/2; // Set Inner Coordinate
return "translate(" + (arc.centroid(d)) + ")";
})
.style("fill", "White")
.style("font", "bold 12px Arial")
.text(function(d) { return "(" + d.data.value + " / " + d.data.percent + "%" + ")"; });
}
/**
* Draws a pie chart of number of projects per application
* @param {Object} dataset Parsed data
*/
function drawApplProj(dataset) {
var w = 300;
var h = 300;
padding = w - 250;
var outerRadius = (w - padding) / 2;
var innerRadius = 0;
var labelr = outerRadius + 10;
var startA = 0;
var endA = Math.PI * 2;
var arc = d3.svg.arc()
.innerRadius(innerRadius)
.outerRadius(outerRadius);
var pie = d3.layout.pie()
.value(function(d) {
return d.value;
})
//.sort(null)
.sort(sortFinLibLast) // Sort the indata so that Finished library will be last
.startAngle(startA)
.endAngle(endA)
;
//Easy colors accessible via a 10-step ordinal scale
//var color = d3.scale.category10();
//var color = d3.scale.category20();
//var color = d3.scale.category20b();
var color = d3.scale.category20c();
//Create SVG element
var svg = d3.select("#application_projects")
.append("svg")
.attr("width", w)
.attr("height", h);
//Set up groups
var arcs = svg.selectAll("g.arc")
.data(pie(dataset).sort(sortPieDataFinLibLast)) // Sort the data so that Finished library
// will be last (see comment in sortFinLibLast
// and sortPieDataFinLibLast functions)
.enter()
.append("g")
.attr("class", "arc")
.attr("transform", "translate(" + w/2 + "," + (outerRadius + 20) + ")")
;
//Draw arc paths
arcs.append("path")
.attr("fill", function(d, i) {