vsm-dictionary-ensembl
Advanced tools
Comparing version 1.0.6 to 1.0.7
{ | ||
"name": "vsm-dictionary-ensembl", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "Implementation of a VSM-dictionary that uses the EBI search RESTful Web Services to interact with the Ensembl genome database", | ||
@@ -5,0 +5,0 @@ "main": "src/DictionaryEnsembl.js", |
@@ -64,2 +64,15 @@ # vsm-dictionary-ensembl | ||
Note that if we receive an error response from the EBI Search servers (see the | ||
URL requests for `getEnties` and `getEntryMatchesForString` below) that is not a | ||
JSON string that we can parse, we formulate the error as a JSON object ourselves | ||
in the following format: | ||
``` | ||
{ | ||
status: <number>, | ||
error: <response> | ||
} | ||
``` | ||
where the *response* from the server is JSON stringified. | ||
### Map Ensembl to DictInfo VSM object | ||
@@ -96,3 +109,3 @@ | ||
From the above URL, we provide a brief description for each sub-part: | ||
For the above URL, we provide a brief description for each sub-part: | ||
- The first part refers to the EBI Search's main REST endpoint: https://www.ebi.ac.uk/ebisearch/ws/rest/ | ||
@@ -156,3 +169,3 @@ - The second part refers to the **domain** of search (*ensembl_gene*) | ||
entry/match object property takes the combined value of the `species`, the gene | ||
names (`name`, `gene_name`, `gene_synonym`) and the `description` (in that order). | ||
names (`gene_synonym`, `name`) and the `description` (in that order). | ||
The reason behind this is that the `description` is sometimes the same for different | ||
@@ -159,0 +172,0 @@ genes when `optimap: false` and thus not distinguishable, so we had to provide |
const Dictionary = require('vsm-dictionary'); | ||
const { getLastPartOfURL, fixedEncodeURIComponent, removeDuplicates } = require('./fun'); | ||
const { getLastPartOfURL, fixedEncodeURIComponent, | ||
removeDuplicates, isJSONString } = require('./fun'); | ||
@@ -50,12 +51,5 @@ module.exports = class DictionaryEnsembl extends Dictionary { | ||
} else { | ||
// keep only the domain-specific dictID(s) | ||
let idList = options.filter.id.filter(dictID => | ||
dictID.trim() === this.ensemblDictID | ||
); | ||
if (idList.length === 0) { | ||
return cb(null, {items: []}); | ||
} else { | ||
return cb(null, res); | ||
} | ||
return (options.filter.id.includes(this.ensemblDictID)) | ||
? cb(null, res) | ||
: cb(null, { items: [] }); | ||
} | ||
@@ -65,11 +59,5 @@ } | ||
getEntries(options, cb) { | ||
if (this.hasProperFilterDictIDProperty(options)) { | ||
// keep only the domain-specific dictID(s) | ||
let idList = options.filter.dictID.filter(dictID => | ||
dictID.trim() === this.ensemblDictID | ||
); | ||
if (idList.length === 0) { | ||
return cb(null, { items: [] }); | ||
} | ||
if (this.hasProperFilterDictIDProperty(options) | ||
&& !options.filter.dictID.includes(this.ensemblDictID)) { | ||
return cb(null, { items: [] }); | ||
} | ||
@@ -86,3 +74,3 @@ | ||
// When requesting specific list of ids, do sorting and triming | ||
// When requesting specific list of ids, do sorting and trimming | ||
let arr = entryObjArray; | ||
@@ -105,11 +93,5 @@ if (this.hasProperFilterIDProperty(options)) { | ||
if (this.hasProperFilterDictIDProperty(options)) { | ||
// keep only the domain-specific dictID(s) | ||
let idList = options.filter.dictID.filter(dictID => | ||
dictID.trim() === this.ensemblDictID | ||
); | ||
if (idList.length === 0) { | ||
return cb(null, { items: [] }); | ||
} | ||
if (this.hasProperFilterDictIDProperty(options) | ||
&& !options.filter.dictID.includes(this.ensemblDictID)) { | ||
return cb(null, { items: [] }); | ||
} | ||
@@ -385,4 +367,9 @@ | ||
if (req.readyState === 4) { | ||
if (req.status !== 200) | ||
cb(JSON.parse(req.responseText)); | ||
if (req.status !== 200) { | ||
let response = req.responseText; | ||
isJSONString(response) | ||
? cb(JSON.parse(response)) | ||
: cb(JSON.parse('{ "status": ' + req.status | ||
+ ', "errors": [' + JSON.stringify(response) + ']}')); | ||
} | ||
else { | ||
@@ -389,0 +376,0 @@ try { |
module.exports = { getLastPartOfURL, fixedEncodeURIComponent, | ||
getElementsInParentheses, getStringBeforeFirstSeparator, removeDuplicates }; | ||
getElementsInParentheses, getStringBeforeFirstSeparator, | ||
removeDuplicates, isJSONString }; | ||
@@ -34,2 +35,11 @@ function getLastPartOfURL(entryId) { | ||
return [...new Set(arr)]; | ||
} | ||
function isJSONString(str) { | ||
try { | ||
let json = JSON.parse(str); | ||
return (json && typeof json === 'object'); | ||
} catch (e) { | ||
return false; | ||
} | ||
} |
const { getLastPartOfURL, fixedEncodeURIComponent, getElementsInParentheses, | ||
getStringBeforeFirstSeparator, removeDuplicates } = require('./fun'); | ||
getStringBeforeFirstSeparator, removeDuplicates, isJSONString } = require('./fun'); | ||
const chai = require('chai'); chai.should(); | ||
const expect = chai.expect; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
describe('fun.js', () => { | ||
const getIDPath = path.join(__dirname, '..', 'resources', 'id.json'); | ||
const getMelanomaPath = path.join(__dirname, '..', 'resources', 'melanoma.json'); | ||
const getIDStr = fs.readFileSync(getIDPath, 'utf8'); | ||
const getMatchesForMelanomaStr = fs.readFileSync(getMelanomaPath, 'utf8'); | ||
describe('getLastPartOfURL', () => { | ||
@@ -85,2 +94,23 @@ it('returns the last part of a URL', cb => { | ||
}); | ||
describe('isJSONString', () => { | ||
it('returns true or false whether the given string is a JSON string or ' + | ||
'not!', cb => { | ||
expect(isJSONString('')).to.equal(false); | ||
expect(isJSONString('melanoma')).to.equal(false); | ||
expect(isJSONString('<h1>Not Found</h1>')).to.equal(false); | ||
expect(isJSONString([])).to.equal(false); | ||
expect(isJSONString({})).to.equal(false); | ||
expect(isJSONString('This is not a JSON string.')).to.equal(false); | ||
expect(isJSONString('{}')).to.equal(true); | ||
expect(isJSONString('[]')).to.equal(true); | ||
expect(isJSONString('["foo","bar",{"foo":"bar"}]')).to.equal(true); | ||
expect(isJSONString('{"myCount": null}')).to.equal(true); | ||
expect(isJSONString(getIDStr)).to.equal(true); | ||
expect(isJSONString(getMatchesForMelanomaStr)).to.equal(true); | ||
cb(); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is too big to display
155139
1343
191
2