@sjcrh/proteinpaint-server 2.106.0 → 2.107.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sjcrh/proteinpaint-server",
3
- "version": "2.106.0",
3
+ "version": "2.107.0",
4
4
  "type": "module",
5
5
  "description": "a genomics visualization tool for exploring a cohort's genotype and phenotype data",
6
6
  "main": "src/app.js",
@@ -61,7 +61,7 @@
61
61
  "@sjcrh/augen": "2.87.0",
62
62
  "@sjcrh/proteinpaint-rust": "2.99.0",
63
63
  "@sjcrh/proteinpaint-shared": "2.106.0",
64
- "@sjcrh/proteinpaint-types": "2.106.0",
64
+ "@sjcrh/proteinpaint-types": "2.107.0",
65
65
  "@types/express": "^5.0.0",
66
66
  "@types/express-session": "^1.18.1",
67
67
  "better-sqlite3": "^9.4.1",
package/routes/burden.js CHANGED
@@ -15,6 +15,7 @@ const api = {
15
15
  }
16
16
  }
17
17
  };
18
+ const MAXBOOTNUM = 20;
18
19
  function init({ genomes }) {
19
20
  return async function handler(req, res) {
20
21
  try {
@@ -27,72 +28,137 @@ function init({ genomes }) {
27
28
  throw `invalid q.genome=${req.query.dslabel}`;
28
29
  if (!ds.cohort.cumburden?.files)
29
30
  throw `missing ds.cohort.cumburden.files`;
30
- const estimates = await getBurdenEstimates(req, ds);
31
- const { keys, rows } = formatPayload(estimates);
32
- res.send({ status: "ok", keys, rows });
31
+ if (!ds.cohort?.cumburden?.db)
32
+ throw `missing ds.cohort.cumburden.db`;
33
+ if (!ds.cohort?.cumburden?.bootsubdir)
34
+ throw `missing ds.cohort.cumburden.bootsubdir`;
35
+ const result = await getBurdenResult(q, ds.cohort.cumburden);
36
+ if (!q.showCI) {
37
+ res.send({
38
+ status: "ok",
39
+ /*estimate: result.estimate,*/
40
+ ...formatPayload(result.estimate)
41
+ });
42
+ } else {
43
+ if (!result.ci95)
44
+ await compute95ci(result, ds.cohort.cumburden);
45
+ res.send({
46
+ status: "ok",
47
+ /*ci95: result.ci95,*/
48
+ ...formatPayload(result.ci95)
49
+ });
50
+ }
33
51
  } catch (e) {
34
52
  res.send({ status: "error", error: e.message || e });
35
53
  }
36
54
  };
37
55
  }
38
- async function getBurdenEstimates(q, ds) {
39
- for (const k in q.query) {
40
- q.query[k] = Number(q.query[k]);
56
+ async function getBurdenResult(q, cumburden) {
57
+ const { id, jsonInput } = normalizeInput(q, cumburden);
58
+ let result = cumburden.db.connection.prepare("SELECT * FROM estimates WHERE id=?").get(id);
59
+ if (!result) {
60
+ result = { id, status: null, input: jsonInput };
61
+ const estJson = await run_R(path.join(serverconfig.binpath, "utils", "burden-main.R"), jsonInput, []);
62
+ const estimate = JSON.parse(estJson);
63
+ const ages = Object.keys(estimate[0]).filter((k) => k.startsWith("["));
64
+ const overall = { chc: 0 };
65
+ for (const age of ages) {
66
+ overall[age] = [0];
67
+ for (const est of estimate)
68
+ overall[age][0] += est[age];
69
+ }
70
+ estimate.push(overall);
71
+ const burden = {};
72
+ for (const est of estimate) {
73
+ burden[est.chc] = est;
74
+ }
75
+ cumburden.db.connection.prepare("INSERT INTO estimates (id, input, status, estimate) VALUES (?, ?, ?, ?)").run([result.id, jsonInput, 0, JSON.stringify(burden)]);
76
+ result.status = 0;
77
+ result.estimate = burden;
41
78
  }
42
- const data = Object.assign({}, defaults, q.query);
43
- const { fit, surv, sample } = ds.cohort.cumburden.files;
44
- if (!fit || !surv || !sample)
45
- throw `missing one or more of ds.cohort.burden.files.{fit, surv, sample}`;
46
- const args = [
47
- `${serverconfig.tpmasterdir}/${fit}`,
48
- `${serverconfig.tpmasterdir}/${surv}`,
49
- `${serverconfig.tpmasterdir}/${sample}`
50
- ];
51
- const estimates = JSON.parse(
52
- await run_R(path.join(serverconfig.binpath, "utils", "burden.R"), JSON.stringify(data), args)
53
- );
54
- return estimates;
79
+ for (const [k, v] of Object.entries(result)) {
80
+ if (k !== "id" && typeof v == "string")
81
+ result[k] = JSON.parse(v);
82
+ }
83
+ return result;
55
84
  }
56
- function formatPayload(estimates) {
57
- const rawKeys = Object.keys(estimates[0]);
58
- const outKeys = [];
59
- const keys = [];
60
- for (const k of rawKeys) {
61
- if (k == "chc") {
62
- keys.push(k);
63
- outKeys.push(k);
64
- } else {
65
- const age = Number(k.slice(1).split(",")[0]);
66
- if (age <= 60 && age % 2 == 0) {
67
- keys.push(k);
68
- outKeys.push(`burden${age}`);
85
+ function normalizeInput(q, cumburden) {
86
+ const keys = Object.keys(q).filter((k) => k in defaultInputValues).sort();
87
+ const id = keys.map((k) => q[k]).join("-");
88
+ const normalized = {};
89
+ for (const k of keys)
90
+ normalized[k] = q[k];
91
+ normalized.datafiles = {
92
+ dir: path.join(serverconfig.tpmasterdir, cumburden.dir),
93
+ files: cumburden.files,
94
+ boosubdir: cumburden.bootsubdir
95
+ };
96
+ normalized.binpath = serverconfig.binpath;
97
+ const jsonInput = JSON.stringify(normalized);
98
+ return { id, jsonInput };
99
+ }
100
+ async function compute95ci(result, cumburden) {
101
+ try {
102
+ if (!result.input)
103
+ throw "result{} does not have .input";
104
+ const input = structuredClone(result.input);
105
+ input.burden = Object.values(result.estimate).filter((est) => est.chc !== 0);
106
+ const lowup = await run_R(path.join(serverconfig.binpath, "utils", "burden-ci95.R"), JSON.stringify(input), []);
107
+ const { low, up, overall } = JSON.parse(lowup);
108
+ const ci95 = { 0: {} };
109
+ for (const est of Object.values(result.estimate)) {
110
+ if (!ci95[est.chc])
111
+ ci95[est.chc] = {};
112
+ const lower = low.find((l) => l.chc === est.chc);
113
+ const upper = up.find((u) => u.chc === est.chc);
114
+ for (const [age, val] of Object.entries(est)) {
115
+ if (!age.startsWith("["))
116
+ continue;
117
+ const burden = est.chc === 0 ? overall[0][age] : val;
118
+ ci95[est.chc][age] = [burden, lower[age], upper[age]];
69
119
  }
70
120
  }
121
+ result.ci95 = ci95;
122
+ } catch (e) {
123
+ console.log(e);
71
124
  }
125
+ await cumburden.db.connection.prepare(`UPDATE estimates SET ci95=? WHERE id=?`).run(JSON.stringify(result.ci95), result.id);
126
+ }
127
+ function sortNumericValue(a, b) {
128
+ return a < b ? -1 : 1;
129
+ }
130
+ function formatPayload(estimates) {
131
+ const rawKeys = Object.keys(estimates["1"]).filter((k) => k.startsWith("["));
132
+ const renamedKeys = rawKeys.map((k) => `burden${k.split(",")[0].slice(1)}`);
133
+ const outKeys = ["chc", ...renamedKeys];
72
134
  const rows = [];
73
- for (const v of estimates) {
74
- rows.push(keys.map((k) => v[k]));
135
+ for (const [chc, burdenByAge] of Object.entries(estimates)) {
136
+ const arr = [chc];
137
+ for (const age of rawKeys)
138
+ arr.push(Array.isArray(burdenByAge[age]) ? burdenByAge[age] : [burdenByAge[age]]);
139
+ rows.push(arr);
75
140
  }
76
141
  return { keys: outKeys, rows };
77
142
  }
78
- const defaults = Object.freeze({
143
+ const defaultInputValues = Object.freeze({
144
+ // showCI: false, do not track so it's not computed as part of unique ID
79
145
  diaggrp: 5,
80
- sex: 0,
146
+ sex: 1,
81
147
  white: 1,
82
- agedx: 1,
148
+ agedx: 6,
83
149
  // chemotherapy
84
150
  steriod: 0,
85
151
  bleo: 0,
86
- vcr: 0,
87
- //12, // Vincristine
88
- etop: 0,
89
- //2500, // Etoposide
152
+ vcr: 12,
153
+ // Vincristine
154
+ etop: 2500,
155
+ // Etoposide
90
156
  itmt: 0,
91
157
  // Intrathecal methothrexate_grp: 0,
92
- ced: 0,
93
- //1.6, // Cyclophosphamide, 0.7692 mean 7692.
94
- cisp: 0,
95
- //300, // Cisplatin
158
+ ced: 1.6,
159
+ // Cyclophosphamide, 0.7692 mean 7692.
160
+ cisp: 300,
161
+ // Cisplatin
96
162
  dox: 0,
97
163
  // Anthracycline, 3 mean 300 ml/m2
98
164
  carbo: 0,
@@ -100,14 +166,11 @@ const defaults = Object.freeze({
100
166
  hdmtx: 0,
101
167
  // High-Dose Methotrexate
102
168
  // radiation
103
- brain: 0,
104
- //5.4,
105
- chest: 0,
106
- //2.4,
169
+ brain: 5.4,
170
+ chest: 2.4,
107
171
  heart: 0,
108
172
  pelvis: 0,
109
- abd: 0
110
- //2.4
173
+ abd: 2.4
111
174
  });
112
175
  export {
113
176
  api