mf-from-google-sheet
Advanced tools
Comparing version 0.8.0 to 0.9.1
@@ -6,3 +6,3 @@ { | ||
}, | ||
"version": "0.8.0", | ||
"version": "0.9.1", | ||
"description": "Retrieve and format molecular formula from google sheet", | ||
@@ -25,5 +25,5 @@ "main": "src/index.js", | ||
"dependencies": { | ||
"mf-generator": "^0.8.0", | ||
"mf-parser": "^0.8.0", | ||
"mf-utilities": "^0.8.0", | ||
"mf-generator": "^0.9.1", | ||
"mf-parser": "^0.9.1", | ||
"mf-utilities": "^0.9.1", | ||
"papaparse": "^4.3.6", | ||
@@ -30,0 +30,0 @@ "request": "^2.83.0", |
@@ -7,11 +7,17 @@ 'use strict'; | ||
test('Test getReferenceList with existing', async () => { | ||
var result = await mfFromGoogleSheet('https://googledocs.cheminfo.org/spreadsheets/d/15Kuc5MeOhvm4oeTMvEuP1rWdRFiVWosxXhYwAmuf3Uo/export?format=tsv'); | ||
expect(result).toHaveLength(1684); | ||
expect(result[0].mw).toBeCloseTo(5.963091372400585, 5); | ||
expect(result[0].em).toBeCloseTo(5.95304980662, 5); | ||
expect(result[0].ms.em).toBeCloseTo(5.95250122671093, 5); | ||
var result = await mfFromGoogleSheet( | ||
'https://googledocs.cheminfo.org/spreadsheets/d/15Kuc5MeOhvm4oeTMvEuP1rWdRFiVWosxXhYwAmuf3Uo/export?format=tsv' | ||
); | ||
expect(result).toHaveLength(1684); | ||
expect(result[0].mw).toBeCloseTo(5.963091372400585, 5); | ||
expect(result[0].em).toBeCloseTo(5.95304980662, 5); | ||
expect(result[0].ms.em).toBeCloseTo(5.95250122671093, 5); | ||
}); | ||
test('Test getReferenceList with non existing document', () => { | ||
return expect(mfFromGoogleSheet('https://googledocs.cheminfo.org/spreadsheets/d/15Kuc5MeOhvm4xxxVWosxXhYwAmuf3Uo/export?format=tsv')).rejects.toThrow('404'); | ||
expect( | ||
mfFromGoogleSheet( | ||
'https://googledocs.cheminfo.org/spreadsheets/d/15Kuc5MeOhvm4xxxVWosxXhYwAmuf3Uo/export?format=tsv' | ||
) | ||
).rejects.toThrow('404'); | ||
}); |
187
src/index.js
'use strict'; | ||
const fetchText = require('./util/fetchText'); | ||
var Papa = require('papaparse'); | ||
@@ -8,108 +7,110 @@ var generateMFs = require('mf-generator'); | ||
const fetchText = require('./util/fetchText'); | ||
async function mfFromGoogleSheet(url, options = {}) { | ||
let { | ||
urlReferences | ||
} = options; | ||
let { | ||
urlReferences | ||
} = options; | ||
if (urlReferences) { | ||
let results = await Promise.all([ | ||
fetchText(url), | ||
fetchText(urlReferences) | ||
]); | ||
return parse(results[0], results[1]); | ||
} else { | ||
let result = await fetchText(url); | ||
return parse(result); | ||
} | ||
if (urlReferences) { | ||
let results = await Promise.all([ | ||
fetchText(url), | ||
fetchText(urlReferences) | ||
]); | ||
return parse(results[0], results[1]); | ||
} else { | ||
let result = await fetchText(url); | ||
return parse(result); | ||
} | ||
function parse(tsv, tsvReferences) { | ||
var parsed = Papa.parse(tsv, | ||
{ | ||
delimiter: '\t', | ||
header: true | ||
} | ||
); | ||
var fields = parsed.meta.fields; | ||
var infoFields = fields.filter((a) => !['mf', 'modif', 'ESI', 'MALDI', 'positive', 'negative'].includes(a)); | ||
var formulas = parsed.data; | ||
function parse(tsv, tsvReferences) { | ||
var parsed = Papa.parse(tsv, | ||
{ | ||
delimiter: '\t', | ||
header: true | ||
} | ||
); | ||
var fields = parsed.meta.fields; | ||
var infoFields = fields.filter((a) => !['mf', 'modif', 'ESI', 'MALDI', 'positive', 'negative'].includes(a)); | ||
var formulas = parsed.data; | ||
if (tsvReferences) { | ||
var referencesArray = Papa.parse(tsvReferences, | ||
{ | ||
delimiter: '\t', | ||
header: true | ||
} | ||
).data; | ||
if (tsvReferences) { | ||
var referencesArray = Papa.parse(tsvReferences, | ||
{ | ||
delimiter: '\t', | ||
header: true | ||
} | ||
).data; | ||
var references = {}; | ||
referencesArray.forEach( | ||
function (r) { | ||
references[r.label] = r; | ||
} | ||
); | ||
var references = {}; | ||
referencesArray.forEach( | ||
function (r) { | ||
references[r.label] = r; | ||
} | ||
); | ||
} | ||
var results = []; | ||
for (var formula of formulas) { | ||
if (tsvReferences) { | ||
// we add references | ||
var refs = formula.references.split(/[ ,]+/); | ||
formula.references = []; | ||
for (let ref of refs) { | ||
formula.references.push( | ||
references[ref] | ||
); | ||
} | ||
} | ||
// we need to calculate all the possibilities | ||
try { | ||
var mfs = generateMFs([formula.mf], { ionizations: formula.modif }); | ||
for (var mf of mfs) { | ||
mf.info = {}; | ||
for (let infoField of infoFields) { | ||
mf.info[infoField] = formula[infoField]; | ||
} | ||
if (!formula.ESI && !formula.MALDI && !formula.positive && !formula.negative) { | ||
mf.filter = { | ||
ESI: true, | ||
MALDI: true, | ||
positive: true, | ||
negative: true | ||
}; | ||
} else { | ||
mf.filter = { | ||
ESI: formula.ESI === 'X' ? true : false, | ||
MALDI: formula.MALDI === 'X' ? true : false, | ||
positive: formula.positive === 'X' ? true : false, | ||
negative: formula.negative === 'X' ? true : false | ||
}; | ||
} | ||
mf.mf = (new MF(mf.mf)).toMF(); | ||
results.push(mf); | ||
} | ||
} catch (e) { | ||
// eslint-disable-next-line no-console | ||
console.warn('Non parsable molecular formula: ', formula.mf, formula.modif, e.toString()); | ||
} | ||
var results = []; | ||
for (var formula of formulas) { | ||
if (tsvReferences) { | ||
// we add references | ||
var refs = formula.references.split(/[ ,]+/); | ||
formula.references = []; | ||
for (let ref of refs) { | ||
formula.references.push( | ||
references[ref] | ||
); | ||
} | ||
} | ||
// we need to calculate all the possibilities | ||
try { | ||
var mfs = generateMFs([formula.mf], { ionizations: formula.modif }); | ||
for (var mf of mfs) { | ||
mf.info = {}; | ||
for (let infoField of infoFields) { | ||
mf.info[infoField] = formula[infoField]; | ||
} | ||
if (!formula.ESI && !formula.MALDI && !formula.positive && !formula.negative) { | ||
mf.filter = { | ||
ESI: true, | ||
MALDI: true, | ||
positive: true, | ||
negative: true | ||
}; | ||
} else { | ||
mf.filter = { | ||
ESI: formula.ESI === 'X' ? true : false, | ||
MALDI: formula.MALDI === 'X' ? true : false, | ||
positive: formula.positive === 'X' ? true : false, | ||
negative: formula.negative === 'X' ? true : false | ||
}; | ||
} | ||
mf.mf = (new MF(mf.mf)).toMF(); | ||
results.push(mf); | ||
} | ||
} catch (e) { | ||
// eslint-disable-next-line no-console | ||
console.warn('Non parsable molecular formula: ', formula.mf, formula.modif, e.toString()); | ||
} | ||
} | ||
results = results.filter(function (a) { | ||
return a.ms.em !== 0; | ||
}); | ||
results = results.filter(function (a) { | ||
return a.ms.em !== 0; | ||
}); | ||
results.sort(function (a, b) { | ||
return a.ms.em - b.ms.em; | ||
}); | ||
results.sort(function (a, b) { | ||
return a.ms.em - b.ms.em; | ||
}); | ||
var uniqueResults = [results[0]]; | ||
for (var i = 1; i < results.length; i++) { | ||
if (results[i - 1].ms.em !== results[i].ms.em) { | ||
uniqueResults.push(results[i]); | ||
} | ||
} | ||
var uniqueResults = [results[0]]; | ||
for (var i = 1; i < results.length; i++) { | ||
if (results[i - 1].ms.em !== results[i].ms.em) { | ||
uniqueResults.push(results[i]); | ||
} | ||
} | ||
return uniqueResults; | ||
} | ||
return uniqueResults; | ||
} | ||
} | ||
@@ -116,0 +117,0 @@ |
@@ -6,3 +6,3 @@ 'use strict'; | ||
module.exports = function fetchText(url) { | ||
return request(url).promise(); | ||
return request(url).promise(); | ||
}; |
@@ -6,4 +6,4 @@ 'use strict'; | ||
module.exports = async function fetchTextBrowser(url) { | ||
const result = await fetch(url); | ||
return result.text(); | ||
const result = await fetch(url); | ||
return result.text(); | ||
}; |
132
6785
+ Addedatom-sorter@0.9.1(transitive)
+ Addedchemical-elements@0.9.1(transitive)
+ Addedchemical-groups@0.9.1(transitive)
+ Addedmf-finder@0.9.1(transitive)
+ Addedmf-generator@0.9.1(transitive)
+ Addedmf-matcher@0.9.1(transitive)
+ Addedmf-parser@0.9.1(transitive)
+ Addedmf-utilities@0.9.1(transitive)
- Removedatom-sorter@0.8.0(transitive)
- Removedchemical-elements@0.7.0(transitive)
- Removedchemical-groups@0.8.0(transitive)
- Removedmf-finder@0.8.0(transitive)
- Removedmf-generator@0.8.0(transitive)
- Removedmf-matcher@0.8.0(transitive)
- Removedmf-parser@0.8.0(transitive)
- Removedmf-utilities@0.8.0(transitive)
Updatedmf-generator@^0.9.1
Updatedmf-parser@^0.9.1
Updatedmf-utilities@^0.9.1