-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
365 lines (302 loc) · 11.1 KB
/
app.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
import { parseBands } from "./parsers/bands.js";
import { parseChromosomes } from "./parsers/chromosomes.js";
import { parseHg19Centromeres, parseHg38Centromeres } from "./parsers/centromeres.js";
import { vcfToJson, vcfSamples, vcfQuality } from "./testing/annotate_json.js";
import { getOverlappedGenes, getGeneAssociations } from "./testing/dbHelpers.js";
import sqlite3 from 'sqlite3';
import express from 'express';
const app = express();
const port = 7477;
// let prefix = './'; //development
let prefix = '/' //production
//will need to set the cors headers to allow all
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
});
//json
app.use(express.json());
app.get('/', (req, res) => {
res.send('sv.iobio Backend Running...');
}
);
//the bands endpoint expects a build hg19 or hg38 returns the bands
//The gzipped files are in our data folder
app.get('/bands', async (req, res) => {
const build = req.query.build;
let url;
if (!build || (build !== 'hg19' && build !== 'hg38')) {
res.status(400).send('valid build query parameter is required');
} else {
if (build === 'hg19') {
url = `${prefix}data/cytoBand_hg19.txt.gz`;
} else {
url = `${prefix}data/cytoBand_hg38.txt.gz`;
}
//Use the parseBands function to get the bands
try {
const bands = await parseBands(url);
res.send(bands);
} catch (e) {
res.status(500).send(e.message);
}
}
});
//the chromosomes endpoint expects a build hg19 or hg38 returns the chromosomes
//The files are in our data folder and are not gzipped
app.get('/chromosomes', async (req, res) => {
const build = req.query.build;
let url;
if (!build || (build !== 'hg19' && build !== 'hg38')) {
res.status(400).send('valid build query parameter is required');
} else {
if (build === 'hg19') {
url = `${prefix}data/chromosomes_hg19.txt`;
} else {
url = `${prefix}data/chromosomes_hg38.txt`;
}
//Use the parseChromosomes function to get the chromosomes
try {
const chromosomes = await parseChromosomes(url);
res.send(chromosomes);
} catch (e) {
res.status(500).send(e.message);
}
}
});
//the centromeres endpoint expects a build hg19 or hg38 returns the centromeres
//The files are in our data folder
app.get('/centromeres', async (req, res) => {
const build = req.query.build;
let url;
if (!build || (build !== 'hg19' && build !== 'hg38')) {
res.status(400).send('valid build query parameter is required');
} else {
if (build === 'hg19') {
url = `${prefix}data/gaps_ref_hg19.txt.gz`;
} else {
url = `${prefix}data/centromeres_hg38.txt`;
}
//Use the parseHg19Centromeres or parseHg38Centromeres function to get the centromeres
try {
let centromeres;
if (build === 'hg19') {
centromeres = await parseHg19Centromeres(url);
} else {
centromeres = await parseHg38Centromeres(url);
}
res.send(centromeres);
} catch (e) {
res.status(500).send(e.message);
}
}
});
app.get('/genes', (req, res) => {
let build = req.query.build;
let source = req.query.source;
let sourceText = '';
if (!build || (build !== 'hg19' && build !== 'hg38')) {
res.status(400).send('Valid build query parameter is required');
return;
}
if (source === 'refseq') {
sourceText = ' AND source = "refseq"';
}
let query = '';
if (build === 'hg19') {
query = 'SELECT * FROM genes WHERE build = "GRCh37"' + sourceText; //this is okay because I made this here it didnt come from the request
} else if (build === 'hg38') {
query = 'SELECT * FROM genes WHERE build = "GRCh38"' + sourceText;
}
const db = new sqlite3.Database(`${prefix}data/geneinfo.db/gene.iobio.db`);
db.all(query, [], (err, rows) => {
db.close(); // Close the database after fetching the data
if (err) {
res.status(500).send(err.message);
return;
}
//for each row we want to have this become a json object where the gene_symbol is the key
let geneMap = {};
rows.forEach(row => {
geneMap[row.gene_symbol] = row;
});
res.send(geneMap);
});
});
app.get('/genes/region', async (req, res) => {
let build = req.query.build;
let source = req.query.source;
let startChr = req.query.startChr
let startPos = req.query.startPos
let endChr = req.query.endChr
let endPos = req.query.endPos
let sourceText = ''
if (!build | !source | !startChr | !startPos | !endChr | !endPos) {
res.status(400).send('Endpoint requires a start chr & position as well as an end chr & position. Typical build and source are also required');
return;
}
const db = new sqlite3.Database(`${prefix}data/geneinfo.db/gene.iobio.db`);
let geneMap = await getOverlappedGenes(build, source, startChr, startPos, endChr, endPos, sourceText, db);
db.close();
res.send(geneMap);
})
app.post('/sv/info/batch', async (req, res) => {
let build = req.query.build;
let source = req.query.source;
//the request body will have the variants: which is an array of objects with chromosome, startPos, endPos
let variants = req.body.variants;
if (!build | !source) {
res.status(400).send('Endpoint requires typical build and source parameters');
return;
}
const geneDb = new sqlite3.Database(`${prefix}data/geneinfo.db/gene.iobio.db`);
const hpoDb = new sqlite3.Database(`${prefix}data/hpo.db`);
let mappedVariants = [];
for (let variant of variants) {
let chromosome = `chr${variant.chromosome}`;
let geneMap = await getOverlappedGenes(build, source, chromosome, variant.start, chromosome, variant.end, source, geneDb);
let genes = Object.keys(geneMap);
let { phenToGene, diseaseToGene } = await getGeneAssociations(genes, hpoDb);
for (let gene_name of genes) {
if (!geneMap.hasOwnProperty(gene_name)) {
continue;
}
geneMap[gene_name].phenotypes = phenToGene[gene_name] || {};
geneMap[gene_name].diseases = diseaseToGene[gene_name] || {};
}
variant.overlappedGenes = geneMap;
mappedVariants.push(variant);
}
geneDb.close();
hpoDb.close();
res.send(mappedVariants);
});
//We will also want to get phenotypes for a given gene
app.get('/geneAssociations', async (req, res) => {
let genes = req.query.genes ? req.query.genes.split(',') : [];
if (!genes) {
res.status(400).send('A list of genes is a required parameter for this endpoint');
return;
}
const db = new sqlite3.Database(`${prefix}data/hpo.db`);
const { phenToGene, diseaseToGene } = await getGeneAssociations(genes, db);
db.close();
res.send({phenToGene, diseaseToGene});
})
app.get('/transcripts', (req, res) => {
let genes = req.query.genes ? req.query.genes.split(',') : [];
if (!genes) {
res.status(400).send('A list of genes is a required parameter for this endpoint');
return;
}
const db = new sqlite3.Database(`${prefix}data/geneinfo.db/gene.iobio.db`);
let placeholders = genes.map(() => '?').join(',');
let query = `SELECT * FROM transcripts WHERE gene_name IN (${placeholders}) AND source = 'gencode' AND is_mane_select = 'true'`;
db.all(query, genes, (err, rows) => {
db.close(); // Close the database after fetching the data
if (err) {
res.status(500).send(err.message);
return;
}
let transcriptMap = {};
rows.forEach(row => {
transcriptMap[row.gene_name] = row;
//features come in as a string so we need to parse them as they are really an array of objects
if (row.features) {
transcriptMap[row.gene_name].features = JSON.parse(row.features);
}
});
res.send(transcriptMap);
});
});
app.get('/phenotypeGenes', (req, res) => {
let phenotypes = req.query.phenotypes ? req.query.phenotypes.split(',') : [];
if (!phenotypes) {
res.status(400).send('A list of phenotypes is a required parameter for this endpoint');
return;
}
const db = new sqlite3.Database(`${prefix}data/hpo.db`);
let placeholders = phenotypes.map(() => '?').join(',');
let geneQuery = `SELECT term_to_gene.*, genes.gene_symbol FROM term_to_gene JOIN genes ON term_to_gene.gene_id = genes.gene_id WHERE term_to_gene.term_id IN (${placeholders})`;
db.all(geneQuery, phenotypes, (err, rows) => {
db.close(); // Close the database after fetching the data
if (err) {
res.status(500).send(err.message);
return;
}
let geneMap = {};
rows.forEach(row => {
if (geneMap.hasOwnProperty(row.gene_symbol)){
geneMap[row.gene_symbol][row.term_id] = row;
} else {
geneMap[row.gene_symbol] = {};
geneMap[row.gene_symbol][row.term_id] = row;
}
});
res.send(geneMap);
});
});
app.get('/dataFromVcf', async (req, res) => {
let vcfPath = req.query.vcfPath;
let sampleName = req.query.sampleName;
if (!vcfPath) {
res.status(400).send('Valid vcfPath query parameter is required');
return;
}
let json;
try {
if (!sampleName) {
json = vcfToJson(vcfPath, (jsonOutput) => {
res.send(jsonOutput);
});
} else {
json = vcfToJson(vcfPath, (jsonOutput) => {
res.send(jsonOutput);
}, sampleName);
}
} catch (e) {
res.status(500).send(e.message);
}
});
app.get('/vcfSamples', async (req, res) => {
let vcfPath = req.query.vcfPath;
if (!vcfPath) {
res.status(400).send('Valid vcfPath query parameter is required');
return;
}
let json;
try {
json = vcfSamples(vcfPath, (jsonOutput) => {
res.send(jsonOutput);
});
} catch (e) {
res.status(500).send(e.message);
}
});
app.get('/vcfQuality', async (req, res) => {
let vcfPath = req.query.vcfPath;
let sampleName = req.query.sampleName;
if (!vcfPath) {
res.status(400).send('Valid vcfPath query parameter is required');
return;
}
let json;
try {
if (sampleName) {
json = vcfQuality(vcfPath, (jsonOutput) => {
res.send(jsonOutput);
}, sampleName);
} else {
json = vcfQuality(vcfPath, (jsonOutput) => {
res.send(jsonOutput);
});
}
} catch (e) {
res.status(500).send(e.message);
}
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
export default app;