-
Notifications
You must be signed in to change notification settings - Fork 4
/
pubmed.js
111 lines (94 loc) · 2.74 KB
/
pubmed.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* @module importer/entrez/pubmed
*/
const Ajv = require('ajv');
const { checkSpec } = require('../util');
const { fetchByIdList, uploadRecord, preLoadCache: preLoadAnyCache } = require('./util');
const ajv = new Ajv();
const { pubmed: SOURCE_DEFN } = require('../sources');
const DB_NAME = 'pubmed';
const CACHE = {};
const recordSpec = ajv.compile({
properties: {
fulljournalname: { type: 'string' },
sortdate: { type: 'string' },
sortpubdate: { type: 'string' },
title: { type: 'string' },
uid: { pattern: '^\\d+$', type: 'string' },
},
required: ['uid', 'title'],
type: 'object',
});
/**
* Given an record record retrieved from pubmed, parse it into its equivalent
* GraphKB representation
*/
const parseRecord = (record) => {
checkSpec(recordSpec, record);
const parsed = {
name: record.title,
sourceId: record.uid,
url: `${SOURCE_DEFN.url}/${record.uid}`,
};
if (record.fulljournalname) {
parsed.journalName = record.fulljournalname;
}
// sortpubdate: '1992/06/01 00:00'
if (record.sortpubdate) {
const match = /^(\d\d\d\d)\//.exec(record.sortpubdate);
if (match) {
parsed.year = parseInt(match[1], 10);
}
} else if (record.sortdate) {
const match = /^(\d\d\d\d)\//.exec(record.sortdate);
if (match) {
parsed.year = parseInt(match[1], 10);
}
}
return parsed;
};
const createDisplayName = sourceId => `pmid:${sourceId}`;
/**
* Given some list of pubmed IDs, return if cached,
* If they do not exist, grab from the pubmed api
* and then upload to GraphKB
*
* @param {ApiConnection} api connection to GraphKB
* @param {Array.<string>} idList list of pubmed IDs
*/
const fetchAndLoadByIds = async (api, idListIn, opt = {}) => {
const pmcIds = idListIn.filter(id => /^pmc\d+$/i.exec(id)).map(id => id.replace(/^pmc/i, ''));
const records = await fetchByIdList(
idListIn.filter(id => !/^pmc\d+$/i.exec(id)),
{
cache: CACHE, db: DB_NAME, parser: parseRecord,
},
);
records.push(...await fetchByIdList(
pmcIds,
{
cache: CACHE, db: 'pmc', dbfrom: DB_NAME, parser: parseRecord,
},
));
return Promise.all(records.map(
async record => uploadRecord(api, record, {
...opt,
cache: CACHE,
createDisplayName,
sourceDefn: SOURCE_DEFN,
target: 'Publication',
}),
));
};
const preLoadCache = async api => preLoadAnyCache(
api,
{
cache: CACHE, sourceDefn: SOURCE_DEFN, target: 'Publication',
},
);
module.exports = {
SOURCE_DEFN,
fetchAndLoadByIds,
parseRecord,
preLoadCache,
};