Skip to content

Commit

Permalink
(bionode#33) [Feature] Do the ncbi valid db checks at lib level
Browse files Browse the repository at this point in the history
Do the db filter at lib level

Add tests to valid-dbs

Fix identation

Istanbul should use all the test files for coverage
  • Loading branch information
Istar-Eldritch committed May 5, 2017
1 parent 840fb34 commit 9149535
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 49 deletions.
84 changes: 38 additions & 46 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ var JSONStream = require('JSONStream')
var split = require('split2')
var ncbi = require('./')
var insight = require('./lib/anonymous-tracking')
var validDbs = require('./lib/valid-dbs')

var argv = require('yargs')
.strict()
.demandCommand(1)
Expand Down Expand Up @@ -75,63 +77,53 @@ bionode-ncbi plink tax sra -s --pretty`
.boolean('pretty')
.describe('pretty', 'Print human readable output instead of NDJSON')
.choices('dlsource', ['assembly', 'sra'])
.choices('db', [
'gquery', 'assembly', 'bioproject', 'biosample', 'biosystems', 'books',
'clinvar', 'clone', 'cdd', 'gap', 'dbvar', 'nucest', 'gene', 'genome', 'gds',
'geoprofiles', 'nucgss', 'gtr', 'homologene', 'medgen', 'mesh', 'ncbisearch',
'nlmcatalog', 'nuccore', 'omim', 'pmc', 'popset', 'probe', 'protein',
'proteinclusters', 'pcassay', 'pccompound', 'pcsubstance', 'pubmed',
'pubmedhealth', 'snp', 'sparcle', 'sra', 'structure', 'taxonomy', 'toolkit',
'toolkitall', 'toolkitbook', 'toolkitbookgh', 'unigene'
])
.example('databases available',
`gquery (All Databases), assembly, bioproject, biosample, biosystems, \
books, clinvar, clone, cdd (Conserved Domains), gap (dbGaP), dbvar, \
nucest (EST), gene, genome, gds (GEO DataSets), geoprofiles (GEO Profiles), \
nucgss (GSS), gtr (GTR), homologene, medgen, mesh, \
ncbisearch (NCBI Web Site), nlmcatalog, nuccore (Nucleotide), omim, pmc, \
popset, probe, protein, proteinclusters, pcassay (PubChem BioAssay), \
pccompound (PubChem Compound), pcsubstance (PubChem Substance), \
pubmed, pubmedhealth, snp, sparcle, sra, structure, \
taxonomy, toolkit, toolkitall, toolkitbook, toolkitbookgh, unigene`
)
.example('databases available', validDbs.printDbs())
.example(`DEBUG mode: export DEBUG='*'`)
.argv

if (argv.dlsource) { argv.db = argv.dlsource }

insight.track('ncbi', 'cli')

var ncbiStream = ncbi[argv._[0]](argv)
try {
var ncbiStream = ncbi[argv._[0]](argv)

var jsonStream
if (argv.pretty) {
jsonStream = JSONStream.stringify(false, null, null, 2)
} else {
jsonStream = JSONStream.stringify(false)
}
var jsonStream
if (argv.pretty) {
jsonStream = JSONStream.stringify(false, null, null, 2)
} else {
jsonStream = JSONStream.stringify(false)
}

ncbiStream.pipe(jsonStream).pipe(process.stdout)
ncbiStream.pipe(jsonStream).pipe(process.stdout)

if (argv.stdin) {
insight.track('ncbi', 'stdin')
process.stdin.setEncoding('utf8')
if (argv.stdin) {
insight.track('ncbi', 'stdin')
process.stdin.setEncoding('utf8')

process.stdin
.pipe(split())
.pipe(JSONStream.parse())
.pipe(ncbiStream)
process.stdin
.pipe(split())
.pipe(JSONStream.parse())
.pipe(ncbiStream)

process.stdin.on('end', function () {
ncbiStream.end()
})
}
process.stdin.on('end', function () {
ncbiStream.end()
})
}

process.stdout.on('error', function (err) {
if (err.code === 'EPIPE') { process.exit(0) }
})
process.stdout.on('error', function (err) {
if (err.code === 'EPIPE') { process.exit(0) }
})

ncbiStream.on('error', function (error) {
console.error(error.message)
process.exit()
})
ncbiStream.on('error', function (error) {
console.error(error.message)
process.exit()
})
} catch (err) {
if (err instanceof validDbs.InvalidDbError) {
console.error(err.message)
console.log('Run "bionode-ncbi --help" to check the available dbs')
} else {
console.error(err)
}
}
7 changes: 7 additions & 0 deletions lib/bionode-ncbi.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ var cheerio = require('cheerio')
var fasta = require('bionode-fasta')
var insight = require('./anonymous-tracking')

var validDbs = require('./valid-dbs')
var InvalidDbError = validDbs.InvalidDbError

var ncbi = exports

var PROXY = typeof window !== 'undefined' ? 'http://cors.inb.io/' : ''
Expand Down Expand Up @@ -111,6 +114,10 @@ ncbi.search = function (db, term, cb) {
var opts = typeof db === 'string' ? { db, term } : db
cb = typeof term === 'function' ? term : cb

if (Object.keys(validDbs.dbs).indexOf(opts.db) < 0) {
throw new InvalidDbError('The database "' + opts.db + '" is not a valid ncbi database')
}

var stream = pumpify.obj(
createAPISearchUrl(opts.db, opts.term),
requestStream(true),
Expand Down
72 changes: 72 additions & 0 deletions lib/valid-dbs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

var dbs = {
gquery: 'All Databases',
assembly: 'Assembly',
bioproject: 'BioProject',
biosample: 'BioSample',
biosystems: 'BioSystems',
books: 'Books',
clinvar: 'ClinVar',
clone: 'Clone',
cdd: 'Conserved Domains',
gap: 'dbGaP',
dbvar: 'dbVar',
nucest: 'EST',
gene: 'Gene',
genome: 'Genome',
gds: 'GEO DataSets',
geoprofiles: 'GEO Profiles',
nucgss: 'GSS',
gtr: 'GTR',
homologene: 'HomoloGene',
medgen: 'MedGen',
mesh: 'MeSH',
ncbisearch: 'NCBI Web Site',
nlmcatalog: 'NLM Catalog',
nuccore: 'Nucleotide',
omim: 'OMIM',
pmc: 'PMC',
popset: 'PopSet',
probe: 'Probe',
protein: 'Protein',
proteinclusters: 'Protein Clusters',
pcassay: 'PubChem BioAssay',
pccompound: 'PubChem Compound',
pcsubstance: 'PubChem Substance',
pubmed: 'PubMed',
pubmedhealth: 'PubMed Health',
snp: 'SNP',
sparcle: 'Sparcle',
sra: 'SRA',
structure: 'Structure',
taxonomy: 'Taxonomy',
toolkit: 'ToolKit',
toolkitall: 'ToolKitAll',
toolkitbook: 'ToolKitBook',
toolkitbookgh: 'ToolKitBookgh',
unigene: 'UniGene'
}

function printDbs (dbsObject) {
dbsObject = dbsObject || dbs

var keys = Object.keys(dbsObject)
return keys.reduce((acc, k, i) => {
acc = acc + k + ' (' + dbsObject[k] + ')'
if (i < keys.length - 1) {
acc = acc + '\n'
}
return acc
}, '')
}

function InvalidDbError (msg) {
this.name = 'InvalidDbError'
this.message = msg
}

InvalidDbError.prototype = new Error('Invalid database')

module.exports.dbs = dbs
module.exports.InvalidDbError = InvalidDbError
module.exports.printDbs = printDbs
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@
"bionode-ncbi": "cli.js"
},
"scripts": {
"test": "standard && dependency-check . && node test/*.js | tap-spec",
"test": "standard && dependency-check . && tape test/**/*.js | tap-spec",
"test-browser": "browserify test/*.js -d | testling -x 'open -a \"Google Chrome\"' | tap-spec",
"coverage": "standard && dependency-check . && istanbul cover test/bionode-ncbi.js --report lcovonly -- | tap-spec && rm -rf ./coverage",
"coveralls": "istanbul cover test/bionode-ncbi.js --report lcovonly -- | tap-spec && cat ./coverage/lcov.info | ./node_modules/.bin/coveralls && rm -rf ./coverage",
"coverage": "standard && dependency-check . && istanbul cover tape test/**/*.js --report lcovonly -- | tap-spec && rm -rf ./coverage",
"coveralls": "istanbul cover tape test/**/*.js --report lcovonly -- | tap-spec && cat ./coverage/lcov.info | ./node_modules/.bin/coveralls && rm -rf ./coverage",
"build-browser": "browserify -r ./index.js:bionode-ncbi | uglifyjs > bionode-ncbi.min.js",
"build-docs": "docco ./lib/bionode-ncbi.js"
},
Expand Down
27 changes: 27 additions & 0 deletions test/valid-dbs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var tape = require('tape')
var tapeNock = require('tape-nock')
var validDbs = require('../lib/valid-dbs')
var ncbi = require('../lib/bionode-ncbi')

var test = tapeNock(tape)

test('valid-dbs printDbs', t => {
var dummy = {fakedb: 'Fake!', another: 'Another'}

var expected = 'fakedb (Fake!)\nanother (Another)'

t.equals(validDbs.printDbs(dummy), expected, 'printDbs returns the expected string')

t.end()
})

// TODO move this test to a suite just for bionode-ncbi search
test('bionode-ncbi search', t => {
t.plan(1)

try {
ncbi.search('invalid', 'human')
} catch (err) {
t.assert(err instanceof validDbs.InvalidDbError, 'call search with wrong db throws InvalidDbError')
}
})

0 comments on commit 9149535

Please sign in to comment.