Comparing version 0.0.4 to 0.1.0
var gulp = require('gulp'); | ||
var mocha = require('gulp-mocha'); | ||
var gutil = require('gulp-util'); | ||
var eslint = require('gulp-eslint'); | ||
@@ -9,10 +10,20 @@ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
gulp.task('lint', function() { | ||
return gulp.src(['**/*.js','!node_modules/**']) | ||
// eslint() attaches the lint output to the "eslint" property | ||
// of the file object so it can be used by other modules. | ||
.pipe(eslint()) | ||
// eslint.format() outputs the lint results to the console. | ||
// Alternatively use eslint.formatEach() (see Docs). | ||
.pipe(eslint.format()); | ||
}); | ||
gulp.task('test', function() { | ||
return gulp.src(['test/*.js'], { read: false }) | ||
.pipe(mocha({ reporter: 'list' })) | ||
.on('error', gutil.log); | ||
return gulp.src(['test/*.js'], { read: false }) | ||
.pipe(mocha({ reporter: 'list' })) | ||
.on('error', gutil.log); | ||
}); | ||
gulp.task('test-watch', function() { | ||
gulp.watch(['./**/*.js'], ['test']); | ||
gulp.watch(['./**/*.js'], ['test']); | ||
}); |
73
index.js
@@ -1,72 +0,3 @@ | ||
var _ = require('underscore'); | ||
var createGateway = require('./createGateway'); | ||
'use strict'; | ||
module.exports = { | ||
pubmedSearch : function(query, args) { | ||
var defaults = { | ||
start : 0, | ||
end : 10 | ||
} | ||
var settings = _.extend(defaults, args); | ||
var search = createGateway({ | ||
method : 'esearch', | ||
params : { | ||
db : 'pubmed', | ||
term : query, | ||
retstart : settings.start, | ||
retmax : settings.end - settings.start | ||
} | ||
}); | ||
var summary = createGateway({ | ||
method : 'esummary', | ||
params : { | ||
db : 'pubmed' | ||
} | ||
}); | ||
return new Promise(function(resolve, reject) { | ||
var data = {}; | ||
search.get().then(function(document) { | ||
data.count = document.count(); | ||
var ids = document.ids(); | ||
if ( ! ids.length ) { | ||
data.papers = []; | ||
resolve(data); | ||
} | ||
summary.addIds(ids); | ||
summary.get().then(function(document) { | ||
data.papers = document.summaries(); | ||
resolve(data); | ||
}, function(err) { | ||
reject(err); | ||
}); | ||
}, function(err) { | ||
reject(err); | ||
}) | ||
}); | ||
}, | ||
getAbstract : function(pmids, single) { | ||
var gateway = createGateway({ | ||
method : 'efetch', | ||
responseType : 'xml', | ||
params : { | ||
db : 'pubmed' | ||
} | ||
}); | ||
gateway.addIds(pmids); | ||
return gateway.get().then(function(document) { | ||
return document.abstracts(single); | ||
}); | ||
} | ||
} | ||
module.exports = require('./src/controllers'); |
{ | ||
"name": "node-ncbi", | ||
"version": "0.0.4", | ||
"version": "0.1.0", | ||
"description": "Access and parse the NCBI eUtils API in Node or the Browser", | ||
@@ -13,2 +13,5 @@ "keywords": [ | ||
"main": "index.js", | ||
"scripts": { | ||
"start": "node ./bin/repl" | ||
}, | ||
"author": { | ||
@@ -24,2 +27,3 @@ "name": "Casey Ydenberg", | ||
"dependencies": { | ||
"gulp-util": "^3.0.7", | ||
"popsicle": "^0.5.12", | ||
@@ -30,6 +34,9 @@ "underscore": "^1.8.3", | ||
"devDependencies": { | ||
"gulp": "^3.9.0", | ||
"gulp": "^3.9.1", | ||
"gulp-eslint": "^2.0.0", | ||
"gulp-mocha": "^2.1.2", | ||
"mocha": "^2.2.5" | ||
} | ||
"mocha": "^2.2.5", | ||
"open": "0.0.5" | ||
}, | ||
"license": "MIT" | ||
} |
# node-ncbi | ||
A nodejs wrapper for the NCBI eUtils API. You can use it to search PubMed or other databases and get the results as a JavaScript object. | ||
A nodejs wrapper for the NCBI eUtils. You can use it to search PubMed or other databases and get the results as a JavaScript object. | ||
[Read the full documentation of the API](http://www.ncbi.nlm.nih.gov/books/NBK25500/). | ||
[Read the full documentation of eUtils](http://www.ncbi.nlm.nih.gov/books/NBK25500/). | ||
@@ -15,72 +15,94 @@ ## Getting started | ||
### PubMed Searches | ||
```js | ||
ncbi.pubmedSearch('actin').then(function(results) { | ||
let pubmedSearch = ncbi.createSearch('actin'); | ||
pubmedSearch.search().then((results) => { | ||
console.log(results); | ||
}) | ||
}); | ||
``` | ||
Will display `{count: 101882, papers: {...}}` | ||
Will log an array of objects. The objects represent PubMed "summaries" containing title, authors, journal and citation information, etc.. | ||
By default, 10 results will be retrieved at a time. To get the next set of results: | ||
```js | ||
ncbi.getAbstract(6365930, true).then(function(result) { | ||
console.log(result); | ||
}); | ||
pubmedSearch.nextPage().then( ... ); | ||
``` | ||
will display the abstract, where 6365930 is a PubMed ID number, a true indicates that only a single result is expected. | ||
## Advanced usage | ||
To change the number of results retrieved at a time: | ||
node-ncbi consists of three components: a gateway, a parser, and a few functions that wrap around them to create simple interfaces. | ||
```js | ||
let pubmedSearch = ncbi.createSearch('actin', { | ||
resultsPerPage: 100 | ||
}); | ||
``` | ||
### Gateway | ||
###Getting the details of a paper | ||
```js | ||
var gateway = require('node-ncbi/createGateway')({ | ||
method: 'esearch', | ||
responseType: 'json', | ||
params: { | ||
term: 'actin' | ||
}, | ||
test: false | ||
}); | ||
var paper = ncbi.createCitation(20517925); | ||
``` | ||
`method` may be esearch, esummary, efetch, etc. | ||
where the only argument is a PMID (PubMed ID #). | ||
The following methods are available: | ||
`responseType` may be json or xml. Note that efetch only returns XML. | ||
- `abstract()` - get the abstract | ||
- `summary()` - get the "summary" - an object of fields containing title, authors, citation info, etc. | ||
- `cites()` - papers which this paper cites. | ||
- `citedBy()` - papers which cite this paper (only includes citing papers in PubMed central) | ||
- `similar()` - papers similar to this one (similarity is calculated on NCBI's side of the API, not ours). | ||
Params can be [any of the parameters that the eUtils API accepts](http://www.ncbi.nlm.nih.gov/books/NBK25500/), as key-value pairs. | ||
All methods return a promise accessible by `.then()`. Except for `.abstract()` the parameter passed to the callback is a PubMed summary or an array of summaries. | ||
**Methods** | ||
## Contributing | ||
`gateway.addParams({parameterKey: parameterValue})` - add new parameters after instantiating the object. | ||
I'd love to get PRs improving the code or expanding the search methods beyond PubMed. | ||
`gateway.addIds(ids)` - special way to add a list of IDs as a parameter; an array of ids will be concatenated into a string. | ||
You can build for development by navigating to the project folder and running `npm install`. You'll also need to have gulp installed globally `npm install -g gulp`. | ||
`gateway.send().then(function(response) {...})` - send off the request. The entire, unfiltered response will be the first argument of the callback passed to `then`. | ||
### Overview | ||
`gateway.get().then(function(doc) {...})` - send of the request and created a parser object. The parser object will be the first argument of the callback passed to `then`. | ||
The module consists of three main parts: a Gateway class that controls access to the API, a set of Document parsers for finding the required information in the returned documents, and Controllers for tying the two together. | ||
###Parser### | ||
Since many of the exposed methods require accessing the API multiple times (ie - perform a search and get ID numbers, then find the individual documents by sending those ID numbers) controllers configure as many gateways and parsers as needed to accomplish a particular task. | ||
Gateways are instantiated with an object literal as follows: | ||
```js | ||
var doc = require('node-ncbi/createNcbiDataDocument')(data); | ||
let gateway = Gateway({ | ||
documentType: 'esearch' | 'esummary' | 'elink' | 'efetch' (default: 'esearch'), | ||
responseType: 'json' | 'xml' (default: 'json'), | ||
params: {} (set of parameters for the API) | ||
test: false | ||
}); | ||
``` | ||
Data may be JSON or XML (or a JavaScript object). It will stored in the property `doc.record`. | ||
See the [API documentaion](http://www.ncbi.nlm.nih.gov/books/NBK25500/) for more information on document types and available parameters. | ||
**Methods** | ||
A set of PubMed IDs can be added like so: | ||
`doc.count()` - return the count (total results) from a search. | ||
```js | ||
gateway.addIds([1111111, 2222222]); | ||
``` | ||
`doc.ids()` - return all IDs (such as PubMed ID numbers) from a search. | ||
The most important method is `gateway.send()` which returns a Promise resolving to the appropriate parser for the returned document type. The parser methods are pretty self-explanatory and are named for the type of information that they will return. | ||
`doc.summaries(single)` - return the complete record of each summary found in an esummary search. If *single* is false, return an array, otherwise a single object. | ||
### REPL | ||
`doc.abstracts(single)` - return the abstract from one or more results, as a string. If *single* is false, return an array, otherwise a single string. | ||
To help with creating Gateways are seeing the data structures returned by the API, node-ncbi provides a custom REPL. Start it with `npm start`. You can then run `url({object})` or `open({object})` where {object} is an object literal for creating gateways as described above. `url` will log the URL needed to access eUtils while `open` will open that URL in a browser. This can help to see the actual data which is useful to create new parsers. | ||
### License | ||
### Unit tests | ||
Copyright (c) 2015 Casey A. Ydenberg | ||
The Gateway and the Parsers are tested independetly in `test/test.js`. Run with `gulp test`. | ||
### ESLint | ||
Run with `gulp lint`. | ||
## License | ||
Copyright (c) 2016 Casey A. Ydenberg | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
@@ -87,0 +109,0 @@ of this software and associated documentation files (the "Software"), to deal |
@@ -0,0 +0,0 @@ { |
@@ -0,0 +0,0 @@ { |
@@ -0,1 +1,5 @@ | ||
"use strict"; | ||
/*eslint-env mocha */ | ||
//test the tests | ||
@@ -10,19 +14,11 @@ var assert = require("assert") | ||
}) | ||
}) | ||
}); | ||
var createGateway = require('../src/gateways'); | ||
describe('Search Gateway', function() { | ||
var createGateway = require('../createGateway'); | ||
describe('Gateway', function() { | ||
var search = createGateway({ | ||
method : 'esearch', | ||
params : { | ||
db : 'pubmed', | ||
term : 'ydenberg ca' | ||
}, | ||
test : true | ||
}); | ||
describe('generateUrl', function() { | ||
var search = createGateway.pubmedSearch('ydenberg ca', 0, 10); | ||
describe('generateUrl search', function() { | ||
it('should build a valid search url from parameters', function() { | ||
assert.equal(search.generateUrl(), 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=ydenberg%20ca&retmode=json'); | ||
assert.equal(search.generateUrl(), 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=ydenberg%20ca&retstart=0&retmax=10&retmode=json'); | ||
}); | ||
@@ -33,9 +29,20 @@ }); | ||
describe('Links gateway', function() { | ||
describe('generateUrl links', function() { | ||
var links = createGateway.pubmedLinks(22588722); | ||
it('should build a valid link url from parameters', function() { | ||
assert.equal(links.generateUrl(), 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/elink.fcgi?db=pubmed&dbfrom=pubmed&cmd=neighbor&retmode=json&id=22588722'); | ||
}); | ||
}); | ||
}); | ||
function getDoc(filename, callback) { | ||
fs = require('fs'); | ||
path = require('path'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
fs.readFile(path.join(__dirname, 'docs', filename), 'UTF-8', callback); | ||
} | ||
var createParser = require('../createNcbiDataDocument'); | ||
var createParser = require('../src/documents'); | ||
describe('parser', function() { | ||
@@ -46,3 +53,3 @@ | ||
getDoc('search.json', function(err, contents) { | ||
var parser = createParser(contents); | ||
var parser = createParser(contents, 'esearch'); | ||
assert.equal(parser.count(), 9); | ||
@@ -57,3 +64,3 @@ done(); | ||
getDoc('search.json', function(err, contents) { | ||
var parser = createParser(contents); | ||
var parser = createParser(contents, 'esearch'); | ||
assert.equal(parser.ids().length, 9); | ||
@@ -68,3 +75,3 @@ done(); | ||
getDoc('summary.json', function(err, contents) { | ||
var parser = createParser(contents); | ||
var parser = createParser(contents, 'esummary'); | ||
var summaries = parser.summaries(); | ||
@@ -80,3 +87,3 @@ assert.equal(summaries.length, 9); | ||
getDoc('fetch.xml', function(err, contents) { | ||
var parser = createParser(contents); | ||
var parser = createParser(contents, 'efetch'); | ||
assert.ok( parser.abstracts(true) ); | ||
@@ -88,2 +95,32 @@ done(); | ||
describe('citedBy', function() { | ||
it('should find all of the papers that have cited this one', function(done) { | ||
getDoc('elink.json', function(err, contents) { | ||
var parser = createParser(contents, 'elink'); | ||
assert.equal(parser.citedBy().length, 2); | ||
done(); | ||
}); | ||
}) | ||
}); | ||
describe('cites', function() { | ||
it('should find all the papers this paper cites', function(done) { | ||
getDoc('elink.json', function(err, contents) { | ||
var parser = createParser(contents, 'elink'); | ||
assert.ok(parser.cites()); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('similar', function() { | ||
it('should find all the papers PubMed flags as similar to this one', function(done) { | ||
getDoc('elink.json', function(err, contents) { | ||
var parser = createParser(contents, 'elink'); | ||
assert.ok(parser.similar()); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
82336
23
0
2146
124
4
5
2
+ Addedgulp-util@^3.0.7
+ Addedansi-gray@0.1.1(transitive)
+ Addedansi-regex@2.1.1(transitive)
+ Addedansi-styles@2.2.1(transitive)
+ Addedansi-wrap@0.1.0(transitive)
+ Addedarray-differ@1.0.0(transitive)
+ Addedarray-uniq@1.0.3(transitive)
+ Addedbeeper@1.1.1(transitive)
+ Addedchalk@1.1.3(transitive)
+ Addedclone@1.0.4(transitive)
+ Addedclone-stats@0.0.1(transitive)
+ Addedcolor-support@1.1.3(transitive)
+ Addeddateformat@2.2.0(transitive)
+ Addedduplexer2@0.0.2(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedfancy-log@1.3.3(transitive)
+ Addedglogg@1.0.2(transitive)
+ Addedgulp-util@3.0.8(transitive)
+ Addedgulplog@1.0.0(transitive)
+ Addedhas-ansi@2.0.0(transitive)
+ Addedhas-gulplog@0.1.0(transitive)
+ Addedlodash._basecopy@3.0.1(transitive)
+ Addedlodash._basetostring@3.0.1(transitive)
+ Addedlodash._basevalues@3.0.0(transitive)
+ Addedlodash._getnative@3.9.1(transitive)
+ Addedlodash._isiterateecall@3.0.9(transitive)
+ Addedlodash._reescape@3.0.0(transitive)
+ Addedlodash._reevaluate@3.0.0(transitive)
+ Addedlodash._reinterpolate@3.0.0(transitive)
+ Addedlodash._root@3.0.1(transitive)
+ Addedlodash.escape@3.2.0(transitive)
+ Addedlodash.isarguments@3.1.0(transitive)
+ Addedlodash.isarray@3.0.4(transitive)
+ Addedlodash.keys@3.1.2(transitive)
+ Addedlodash.restparam@3.6.1(transitive)
+ Addedlodash.template@3.6.2(transitive)
+ Addedlodash.templatesettings@3.1.1(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedmultipipe@0.1.2(transitive)
+ Addedobject-assign@3.0.0(transitive)
+ Addedparse-node-version@1.0.1(transitive)
+ Addedreadable-stream@1.1.14(transitive)
+ Addedreplace-ext@0.0.1(transitive)
+ Addedsparkles@1.0.1(transitive)
+ Addedstrip-ansi@3.0.1(transitive)
+ Addedsupports-color@2.0.0(transitive)
+ Addedthrough2@2.0.5(transitive)
+ Addedtime-stamp@1.1.0(transitive)
+ Addedvinyl@0.5.3(transitive)