@sjcrh/proteinpaint-server 2.63.0 → 2.63.3-3

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.63.0",
3
+ "version": "2.63.3-3",
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",
@@ -47,7 +47,7 @@
47
47
  "@typescript-eslint/eslint-plugin": "^5.60.0",
48
48
  "babel-loader": "^8.2.2",
49
49
  "esbuild": "^0.19.12",
50
- "glob": "^7.2.3",
50
+ "glob": "^10.4.1",
51
51
  "node-watch": "^0.7.1",
52
52
  "nodemon": "^2.0.19",
53
53
  "prettier": "^2.8.8",
@@ -0,0 +1,110 @@
1
+ import fs from "fs";
2
+ import { spawn } from "child_process";
3
+ import { Readable } from "stream";
4
+ import path from "path";
5
+ import serverconfig from "../src/serverconfig.js";
6
+ const api = {
7
+ endpoint: "genesetEnrichment",
8
+ methods: {
9
+ all: {
10
+ init,
11
+ request: {
12
+ typeId: "genesetEnrichmentRequest"
13
+ },
14
+ response: {
15
+ typeId: "genesetEnrichmentResponse"
16
+ // will combine this with type checker
17
+ //valid: (t) => {}
18
+ }
19
+ }
20
+ }
21
+ };
22
+ function init({ genomes }) {
23
+ return async (req, res) => {
24
+ try {
25
+ const results = await run_genesetEnrichment_analysis(req.query, genomes);
26
+ res.send(results);
27
+ } catch (e) {
28
+ res.send({ status: "error", error: e.message || e });
29
+ }
30
+ };
31
+ }
32
+ async function run_genesetEnrichment_analysis(q, genomes) {
33
+ if (!genomes[q.genome].termdbs)
34
+ throw "termdb database is not available for " + q.genome;
35
+ const genesetenrichment_input = {
36
+ genes: q.genes,
37
+ fold_change: q.fold_change,
38
+ db: genomes[q.genome].termdbs.msigdb.cohort.db.connection.name,
39
+ gene_set_group: q.geneSetGroup
40
+ };
41
+ const gsea_output = await run_gsea(
42
+ path.join(serverconfig.binpath, "../python/src", "gsea.py"),
43
+ "/" + JSON.stringify(genesetenrichment_input)
44
+ // "/" is needed for python to accept the bracket "{" as a bracket
45
+ );
46
+ let result;
47
+ for (const line of gsea_output.split("\n")) {
48
+ if (line.startsWith("result: ")) {
49
+ result = JSON.parse(line.replace("result: ", ""));
50
+ } else {
51
+ console.log(line);
52
+ }
53
+ }
54
+ return result;
55
+ }
56
+ async function run_gsea(path2, data) {
57
+ try {
58
+ await fs.promises.stat(path2);
59
+ } catch (e) {
60
+ throw `${path2} does not exist`;
61
+ }
62
+ return new Promise((resolve, reject) => {
63
+ const _stdout = [];
64
+ const _stderr = [];
65
+ let python_path = "python3";
66
+ if (serverconfig.py_path)
67
+ python_path = serverconfig.py_path;
68
+ const sp = spawn(python_path, [path2]);
69
+ if (data) {
70
+ try {
71
+ const input = data.endsWith("\n") ? data : data + "\n";
72
+ Readable.from(input).pipe(sp.stdin);
73
+ } catch (e) {
74
+ sp.kill();
75
+ let errmsg = e;
76
+ const stderr = _stderr.join("").trim();
77
+ if (stderr)
78
+ errmsg += `
79
+ python stderr: ${stderr}`;
80
+ reject(errmsg);
81
+ }
82
+ }
83
+ sp.stdout.on("data", (data2) => _stdout.push(data2));
84
+ sp.stderr.on("data", (data2) => _stderr.push(data2));
85
+ sp.on("error", (err) => reject(err));
86
+ sp.on("close", (code) => {
87
+ const stdout = _stdout.join("").trim();
88
+ const stderr = _stderr.join("").trim();
89
+ if (code !== 0) {
90
+ let errmsg = `python process exited with non-zero status code=${code}`;
91
+ if (stdout)
92
+ errmsg += `
93
+ python stdout: ${stdout}`;
94
+ if (stderr)
95
+ errmsg += `
96
+ python stderr: ${stderr}`;
97
+ reject(errmsg);
98
+ }
99
+ if (stderr) {
100
+ const errmsg = `python process emitted standard error
101
+ python stderr: ${stderr}`;
102
+ reject(errmsg);
103
+ }
104
+ resolve(stdout);
105
+ });
106
+ });
107
+ }
108
+ export {
109
+ api
110
+ };