This repository has been archived by the owner on Jan 3, 2020. It is now read-only.
forked from zotero/translators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNCBI Nucleotide.js
227 lines (196 loc) · 5.7 KB
/
NCBI Nucleotide.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
{
"translatorID": "5e385e77-2f51-41b4-a29b-908e23d5d3e8",
"label": "NCBI Nucleotide",
"creator": "Martin Fenner",
"target": "^https?://(www\\.)?ncbi\\.nlm\\.nih\\.gov/nuccore/",
"minVersion": "3.0",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcs",
"lastUpdated": "2014-11-15 11:25:19"
}
/**
Copyright (c) 2014 Martin Fenner
This program is free software: you can redistribute it and/or
modify it under the terms of the GNU Affero General Public License
as published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public
License along with this program. If not, see
<http://www.gnu.org/licenses/>.
*/
function detectWeb(doc, url) {
// use item type journalArticle until item type dataset is supported in Zotero
if (getIds(doc)) return "journalArticle";
// search results
if (url.indexOf("/?term=") != -1 && getSearchResults(doc, true)) return "multiple";
}
function doWeb(doc, url) {
if (detectWeb(doc, url) == "multiple") {
getSelectedJSON(doc);
} else {
var ids = getIds(doc);
getJSON(ids);
}
}
function getSearchResults(doc, checkOnly) {
var results = doc.getElementsByClassName('rslt');
if (!results.length) return false;
var items = {};
var found = false;
for (var i = 0; i < results.length; i++) {
var title = results[i].firstChild.textContent;
// parse the id (a number) from the second link
var links = results[i].getElementsByTagName("a");
var id = links[1].href.match(/[\d]+/);
if (id && title) {
if (checkOnly) return true;
found = true;
items["_" + id[0]] = ZU.trimInternal(title);
}
}
return found ? items : false;
}
function getSelectedJSON(doc) {
var items = getSearchResults(doc);
var ids = [];
Zotero.selectItems(items, function(selectedItems) {
if (!selectedItems) return true;
for (var i in selectedItems) {
ids.push(i.substr(1));
}
getJSON(ids);
});
}
function getIds(doc) {
var ids = ZU.xpathText(doc, '/html/head/meta[@name="ncbi_uidlist"]/@content')
if (!ids) return false;
return ids.split(",");
}
function getJSON(ids) {
var baseURL = "//eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=nucleotide&retmode=json&id=";
var jsonURL = baseURL + encodeURIComponent(ids.join(","));
Z.debug(jsonURL);
ZU.doGet(jsonURL, parseJSON);
}
function parseJSON(text) {
try {
var obj = JSON.parse(text).result;
var uids = obj.uids;
} catch (e) {
Z.debug(text.substr(0, 100));
throw e;
}
for (var i = 0; i < uids.length; i++) {
var uid = uids[i];
var data = obj[uid];
var item = new Z.Item("journalArticle");
item.title = data.title;
item.callNumber = data.accessionversion;
item.archive = "NCBI Nucleotide Database";
item.archiveLocation = uid;
// indicate that this is in fact a dataset
item.extra = "{:itemType: dataset}";
var version = data.accessionversion.match(/\.(\d{1,2})$/);
if (version && version[1] != '1') {
item.extra += "\n{:version: " + version[1] + "}";
}
item.url = "http://www.ncbi.nlm.nih.gov/nuccore/" + encodeURIComponent(data.accessionversion);
item.date = data.updatedate;
if (data.organism) item.tags.push(data.organism);
if (data.moltype) item.tags.push(data.moltype);
if (data.sourcedb) item.tags.push(data.sourcedb);
item.attachments.push({
url: "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=nucleotide&retmode=gb&rettype=text&id=" + encodeURIComponent(uid),
title: "Nucleotide sequence (gb)",
mimeType: "chemical/x-genbank",
snapshot: false
});
// http://www.ncbi.nlm.nih.gov/About/disclaimer.html
item.rights = "Public domain";
item.language = "en-US";
item.complete();
}
}
/** BEGIN TEST CASES **/
var testCases = [
{
"type": "web",
"url": "http://www.ncbi.nlm.nih.gov/nuccore/?term=brca1",
"items": "multiple"
},
{
"type": "web",
"url": "http://www.ncbi.nlm.nih.gov/nuccore/I01425.1",
"items": [
{
"itemType": "journalArticle",
"title": "Sequence 1 from Patent US 4849348",
"creators": [],
"date": "1993/03/05",
"archive": "NCBI Nucleotide Database",
"archiveLocation": "270186",
"callNumber": "I01425.1",
"extra": "{:itemType: dataset}",
"language": "en-US",
"libraryCatalog": "NCBI Nucleotide",
"rights": "Public domain",
"url": "http://www.ncbi.nlm.nih.gov/nuccore/I01425.1",
"attachments": [
{
"title": "Nucleotide sequence (gb)",
"mimeType": "chemical/x-genbank",
"snapshot": false
}
],
"tags": [
"dna",
"insd"
],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "http://www.ncbi.nlm.nih.gov/nuccore/NM_078524.4",
"items": [
{
"itemType": "journalArticle",
"title": "Drosophila melanogaster smad on X (Smox), transcript variant A, mRNA",
"creators": [],
"date": "2014/08/05",
"archive": "NCBI Nucleotide Database",
"archiveLocation": "665390239",
"callNumber": "NM_078524.4",
"extra": "{:itemType: dataset}\n{:version: 4}",
"language": "en-US",
"libraryCatalog": "NCBI Nucleotide",
"rights": "Public domain",
"url": "http://www.ncbi.nlm.nih.gov/nuccore/NM_078524.4",
"attachments": [
{
"title": "Nucleotide sequence (gb)",
"mimeType": "chemical/x-genbank",
"snapshot": false
}
],
"tags": [
"Drosophila melanogaster",
"refseq",
"rna"
],
"notes": [],
"seeAlso": []
}
]
}
]
/** END TEST CASES **/