forked from metaspace2020/sm-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
263 lines (229 loc) · 8.8 KB
/
utils.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
const slack = require('node-slack'),
jsondiffpatch = require('jsondiffpatch'),
winston = require('winston'),
moment = require('moment'),
{ PubSub } = require('graphql-subscriptions'),
{UserError} = require('graphql-errors'),
fetch = require('node-fetch');
const config = require('config');
const RESOL_POWER_PARAMS = {
'70K': {sigma: 0.00247585727028, fwhm: 0.00583019832869, pts_per_mz: 2019},
'100K': {sigma: 0.0017331000892, fwhm: 0.00408113883008, pts_per_mz: 2885},
'140K': {sigma: 0.00123792863514, fwhm: 0.00291509916435, pts_per_mz: 4039},
'200K': {sigma: 0.000866550044598, fwhm: 0.00204056941504, pts_per_mz: 5770},
'250K': {sigma: 0.000693240035678, fwhm: 0.00163245553203, pts_per_mz: 7212},
'280K': {sigma: 0.00061896431757, fwhm: 0.00145754958217, pts_per_mz: 8078},
'500K': {sigma: 0.000346620017839, fwhm: 0.000816227766017, pts_per_mz: 14425},
'750K': {sigma: 0.000231080011893, fwhm: 0.000544151844011, pts_per_mz: 21637},
'1000K': {sigma: 0.00017331000892, fwhm: 0.000408113883008, pts_per_mz: 28850},
};
function generateProcessingConfig(metadata) {
const polarity_dict = {'Positive': '+', 'Negative': '-'},
polarity = polarity_dict[metadata['MS_Analysis']['Polarity']],
instrument = metadata['MS_Analysis']['Analyzer'],
rp = metadata['MS_Analysis']['Detector_Resolving_Power'],
rp_mz = parseFloat(rp['mz']),
rp_resolution = parseFloat(rp['Resolving_Power']);
let rp200, params;
if (instrument === 'FTICR')
rp200 = rp_resolution * rp_mz / 200.0;
else if (instrument === 'Orbitrap')
rp200 = rp_resolution * Math.pow(rp_mz / 200.0, 0.5);
else
rp200 = rp_resolution;
if (rp200 < 85000) params = RESOL_POWER_PARAMS['70K'];
else if (rp200 < 120000) params = RESOL_POWER_PARAMS['100K'];
else if (rp200 < 195000) params = RESOL_POWER_PARAMS['140K'];
else if (rp200 < 265000) params = RESOL_POWER_PARAMS['250K'];
else if (rp200 < 390000) params = RESOL_POWER_PARAMS['280K'];
else if (rp200 < 625000) params = RESOL_POWER_PARAMS['500K'];
else if (rp200 < 875000) params = RESOL_POWER_PARAMS['750K'];
else params = RESOL_POWER_PARAMS['1000K'];
let m_opts = metadata['metaspace_options'];
let ppm = 3.0;
if ('ppm' in m_opts) {
ppm = m_opts['ppm'];
}
// TODO: move to proper metadata format supporting multiple molecular databases
let mdb_list;
let mdb_names = m_opts['Metabolite_Database'];
if (!Array.isArray(mdb_names))
mdb_list = [{'name': mdb_names}];
else
mdb_list = mdb_names.map( (name) => ({'name': name}) );
for (let default_moldb_name of config.defaults.moldb_names) {
if (mdb_list.filter(mdb => mdb.name === default_moldb_name).length === 0)
mdb_list.push({ "name": default_moldb_name});
}
// TODO: metadata format should support adduct specification
let adducts;
if (m_opts.hasOwnProperty('Adducts'))
adducts = m_opts['Adducts'];
else
adducts = config.defaults.adducts[polarity];
return {
"databases": mdb_list,
"isotope_generation": {
"adducts": adducts,
"charge": {
"polarity": polarity,
"n_charges": 1
},
"isocalc_sigma": Number(params['sigma'].toFixed(6)),
"isocalc_pts_per_mz": params['pts_per_mz']
},
"image_generation": {
"ppm": ppm,
"nlevels": 30,
"q": 99,
"do_preprocessing": false
}
};
}
function metadataChangeSlackNotify(user, datasetId, oldMetadata, newMetadata) {
const delta = jsondiffpatch.diff(oldMetadata, newMetadata),
diff = jsondiffpatch.formatters.jsonpatch.format(delta);
const slackConn = config.slack.webhook_url ? new slack(config.slack.webhook_url): null;
if (slackConn) {
let oldDSName = oldMetadata.metaspace_options.Dataset_Name || "";
let msg = slackConn.send({
text: `${user} edited metadata of ${oldDSName} (id: ${datasetId})` +
"\nDifferences:\n" + JSON.stringify(diff, null, 2),
channel: config.slack.channel
});
}
}
function metadataUpdateFailedSlackNotify(user, datasetId, e_msg) {
const slackConn = config.slack.webhook_url ? new slack(config.slack.webhook_url): null;
if (slackConn) {
let msg = slackConn.send({
text: `${user} tried to edit metadata (ds_id=${datasetId})\nError: ${e_msg}`,
channel: config.slack.channel
});
}
}
const logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({
level: config.log.level,
timestamp: function() {
return moment().format();
},
formatter: function(options) {
// TODO Lachlan: This custom formatter logs an empty string when given an error
// Copy the default formatter's behavior for when options.message is empty
// Return string will be passed to logger.
return options.timestamp() +' - '+ options.level.toUpperCase() +' - '+ (options.message ? options.message : '') +
(options.meta && Object.keys(options.meta).length ? '\n\t'+ JSON.stringify(options.meta) : '' );
}
})
]
});
const pubsub = new PubSub();
const dbConfig = () => {
const {host, database, user, password} = config.db;
return {
host, database, user, password,
max: 10, // client pool size
idleTimeoutMillis: 30000
};
};
let db = require('knex')({
client: 'pg',
connection: dbConfig(),
searchPath: 'knex,public'
});
function canUserViewEsDataset(dataset, user) {
return dataset._source.ds_is_public
|| (user != null && user.role === 'admin')
|| (user != null && user.email != null && user.email !== '' && dataset._source.ds_submitter_email === user.email);
}
function canUserViewPgDataset(dataset, user) {
return dataset.is_public
|| (user != null && user.role === 'admin')
|| (user != null && user.email != null && user.email !== ''
&& dataset.metadata.Submitted_By.Submitter.Email === user.email);
}
function pgDatasetsViewableByUser(user) {
// The returned callback can be used in `.from` and `.join` clauses, e.g. `.from(pgDatasetsViewableByUser(user))`
// This pattern is used to protect against accidentally breaking the filters by using a chained `.orWhere` clause
// More discussion here: https://github.com/tgriesser/knex/issues/1714
return q => {
let filter;
if (user && user.role === 'admin') {
filter = ds => true;
} else if (user && user.email) {
filter = ds => ds.where('is_public', true)
.orWhereRaw("metadata#>>'{Submitted_By,Submitter,Email}' = ?", [user.email]);
} else {
filter = ds => ds.where('is_public', true);
}
return q.from('dataset').where(filter).as('dataset');
};
}
async function assertUserCanViewDataset(datasetId, user) {
const records = await db.select().from('dataset').where('id', '=', datasetId);
if (records.length === 0)
throw new UserError(`No dataset with specified id: ${datasetId}`);
if (!canUserViewPgDataset(records[0], user)) {
throw new UserError(`You don't have permissions to view the dataset: ${datasetId}`);
}
}
function assertUserCanEditDataset(datasetId, user) {
return db.select().from('dataset').where('id', '=', datasetId)
.then(records => {
if (records.length === 0)
throw new UserError(`No dataset with specified id: ${datasetId}`);
if (user == null
|| !(user.role === 'admin'
|| user.email === records[0].metadata.Submitted_By.Submitter.Email)) {
throw new UserError(`You don't have permissions to edit the dataset: ${datasetId}`);
}
});
}
async function fetchDS({id, name}) {
// TODO Lachlan: Refactor this so that security is enforced by default
let records;
if (id !== undefined)
records = await db.select().from('dataset').where('id', '=', id);
else if (name !== undefined)
records = await db.select().from('dataset').where('name', '=', name);
else
throw new UserError(`'id' or 'name' must be provided`);
if (records.length === 0)
return undefined;
else if (records.length > 1)
throw new UserError(`More than one dataset found: '${id}' '${name}'`);
return records[0];
}
const deprecatedMolDBs = new Set(['HMDB', 'ChEBI', 'LIPID_MAPS', 'SwissLipids', 'COTTON_HMDB']);
async function fetchMolecularDatabases({hideDeprecated = true}) {
const host = config.services.moldb_service_host,
resp = await fetch(`http://${host}/v1/databases`),
body = await resp.json();
let molDBs = body['data'];
if (hideDeprecated) {
molDBs = molDBs.filter((molDB) => !deprecatedMolDBs.has(molDB.name));
}
return molDBs;
}
async function wait(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
module.exports = {
generateProcessingConfig,
metadataChangeSlackNotify,
metadataUpdateFailedSlackNotify,
canUserViewEsDataset,
canUserViewPgDataset,
pgDatasetsViewableByUser,
assertUserCanViewDataset,
assertUserCanEditDataset,
fetchDS,
fetchMolecularDatabases,
wait,
config,
logger,
pubsub,
db
};