Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ pubmed.search("actin", 0, 20).then((results) => {
});
```

You can also search by relevance (instead of publication date):

```javascript
pubmed.searchByRelevance("actin").then((results) => {
console.log(results);
});
```

### Looking up a specific paper

```javascript
Expand All @@ -84,6 +92,14 @@ In addition, following methods are available:

All methods return a promise accessible by `.then()`; the value retrieved is passed to the promise.

### Searching by PubMedCentral (PMC) ID number

```javascript
pmc.summary(3315798).then((paper) => {
console.log(paper);
});
```

## Contributing

I'd love to get PRs improving the code or expanding the search methods beyond PubMed.
Expand Down
86 changes: 48 additions & 38 deletions src/gateways/index.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,65 @@
'use strict';
"use strict";

const createGateway = require('./createGateway');
const createGateway = require("./createGateway");

/**
* Wrapper factory functions for the Gateway. Create and configure Gateways
* for a specific purpose.
*/
module.exports = {

/**
* pubmedSearch. Return a Gateway for a pubmed search, with start and end parameters.
* (ie first and last results.)
*/
pubmedSearch: function(query, page, resultsPerPage) {
pubmedSearch: function (query, page, resultsPerPage, sort) {
const start = page * resultsPerPage;
return createGateway({
utility: 'esearch',
utility: "esearch",
params: {
db: 'pubmed',
db: "pubmed",
term: query,
retstart: start,
retmax: resultsPerPage
}
retmax: resultsPerPage,
sort: sort,
},
});
},

/**
* pubmedSummary. Get a set of pubmedSummaries, specified by an array of pmid (pubmed identifiers.)
*/
pubmedSummary: function(ids) {
pubmedSummary: function (ids) {
return createGateway({
utility: "esummary",
params: {
db: "pubmed",
id: ids,
},
});
},

pmcSummary: function (ids) {
return createGateway({
utility: 'esummary',
params: {
db: 'pubmed',
id: ids
db: 'pmc',
id: ids,
}
});
})
},

/**
* Get a set of full/efetch documents from pubmed, specified by an array of pmids (pubmed identifiers).
* Note that efetch documents can only be returned via XML (or flatfile).
*/
pubmedRecord: function(ids) {
* Get a set of full/efetch documents from pubmed, specified by an array of pmids (pubmed identifiers).
* Note that efetch documents can only be returned via XML (or flatfile).
*/
pubmedRecord: function (ids) {
return createGateway({
utility: 'efetch',
utility: "efetch",
params: {
db: 'pubmed',
retmode: 'xml',
id: ids
}
db: "pubmed",
retmode: "xml",
id: ids,
},
});
},

Expand All @@ -59,25 +69,25 @@ module.exports = {
* similar articles, articles that cite this article, and articles cited by
* this article.
*/
pubmedLinks: function(id) {
pubmedLinks: function (id) {
return createGateway({
utility: 'elink',
utility: "elink",
params: {
db: 'pubmed',
dbfrom: 'pubmed',
cmd: 'neighbor',
id: id
}
db: "pubmed",
dbfrom: "pubmed",
cmd: "neighbor",
id: id,
},
});
},

pmcFullText: (id) => createGateway({
utility: 'efetch',
params: {
db: 'pmc',
retmode: 'xml',
id: id
}
})

}
pmcFullText: (id) =>
createGateway({
utility: "efetch",
params: {
db: "pmc",
retmode: "xml",
id: id,
},
}),
};
17 changes: 17 additions & 0 deletions src/pmc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const gateways = require('./gateways');
const queries = require('./queries');

module.exports = {
summary: function(pmc) {
return gateways.pmcSummary(pmc).resolve(data => {
const summaries = queries.summaries(data);
if (summaries.length) {
return summaries[0];
} else {
return null;
}
});
}
}
51 changes: 29 additions & 22 deletions src/pubmed.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,37 @@
const gateways = require('./gateways');
const queries = require('./queries');

function pubmedSearch(query, page, resultsPerPage, sort) {
let count;
page = page || 0;
resultsPerPage = resultsPerPage || 10;
sort = sort || 'most+recent'
return gateways.pubmedSearch(query, page, resultsPerPage).resolve(data => {
count = queries.count(data);
const pmids = queries.ids(data);
if (!pmids.length) {
return {
count: 0,
papers: []
};
}
return new Promise((resolve, reject) => {
this.summaries(pmids).then(summaries => {
resolve({
count: count,
papers: summaries
});
}).catch(err => reject(err));
});
});
}

module.exports = {

search: function(query, page, resultsPerPage) {
let count;
page = page || 0;
resultsPerPage = resultsPerPage || 10;
return gateways.pubmedSearch(query, page, resultsPerPage).resolve(data => {
count = queries.count(data);
const pmids = queries.ids(data);
if (!pmids.length) {
return {
count: 0,
papers: []
};
}
return new Promise((resolve, reject) => {
this.summaries(pmids).then(summaries => {
resolve({
count: count,
papers: summaries
});
}).catch(err => reject(err));
});
});
search: pubmedSearch,

searchByRelevance: function(query, page, resultsPerPage) {
return pubmedSearch(query, page, resultsPerPage, 'relevance');
},

summaries: function(pmids) {
Expand Down
6 changes: 3 additions & 3 deletions src/queries/summaries.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ const summaryQueries = {
raw: data,
title: data.title || '',
authors: summaryQueries.formatAuthors(data.authors) || '',
pubDate: data.sortpubdate
pubDate: data.sortpubdate || data.sortdate,
};
data.articleids.forEach((idObject) => {
if (idObject.idtype === 'pubmed') {
if (['pubmed', 'pmid'].includes(idObject.idtype)) {
//Change pubmed to pmid. Make sure it's an integer.
summary.pmid = parseInt(idObject.value, 10);
} else if (idObject.idtype === 'pmc') {
//Remove PMC from the beginning of the string and make sure it's an integer.
summary.pmc = parseInt(idObject.value.replace('PMC', ''));
summary.pmc = parseInt(idObject.value.replace('PMC', ''), 10);
} else if (idObject.idtype === 'doi') {
//Move DOI to the top level
summary.doi = idObject.value;
Expand Down
8 changes: 8 additions & 0 deletions test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {isPubmedSummary} = require('./helpers.js');
const assert = require('assert');

var pubmed = require('../src/pubmed');
var pmc = require('../src/pmc')

describe('Pubmed module', function() {
this.timeout(10000);
Expand Down Expand Up @@ -72,6 +73,13 @@ describe('Pubmed module', function() {
});
});

it('should retrieve a summary from pmc', function(done) {
pmc.summary(3315798).then(result => {
isPubmedSummary(result);
done();
});
});

it('should retrieve the full NLM XML of a paper', function(done) {
pubmed.fulltext(22323294).then(result => {
check.string(result);
Expand Down
11 changes: 11 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ describe('Links gateway', function() {

});

describe('PMC summary gateway', () => {
describe('generateUrl', () => {

it('should build a valid url from parameters', () => {
const fetch = createGateway.pmcSummary(3315798);
assert.equal(fetch.generateUrl(), 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?retmode=json&db=pmc&id=3315798');
});

});
});

describe('PMC full text gateway', () => {
describe('generateUrl', () => {

Expand Down
33 changes: 23 additions & 10 deletions types/node-ncbi.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
declare module "node-ncbi" {
interface Paper {
raw: object;
pubDate: string;
title: string;
authors: string;
pmid: number;
pmc?: number;
doi?: string;
}

module pubmed {
interface Paper {
raw: object;
pubDate: string;
title: string;
authors: string;
pmid: number;
pmc?: number;
doi?: string;
}

function search(
term: string,
page?: number,
Expand All @@ -19,6 +19,15 @@ declare module "node-ncbi" {
papers: Array<Paper>;
}>;

function searchByRelevance(
term: string,
page?: number,
limit?: number
): Promise<{
count: number;
papers: Array<Paper>;
}>;

function summary(pmid: number): Promise<Paper>;

function summaries(pmids: string): Promise<Paper[]>;
Expand All @@ -35,4 +44,8 @@ declare module "node-ncbi" {

function fullText(pmid: number): Promise<string>;
}

module pmc {
function summary(pmc: number): Promise<Paper>;
}
}