-
Notifications
You must be signed in to change notification settings - Fork 9
/
OntologySearch.gs
331 lines (280 loc) · 12.8 KB
/
OntologySearch.gs
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// OntoMaton is a component of the ISA software suite (http://www.isa-tools.org)
//
// License:
// OntoMaton is licensed under the Common Public Attribution License version 1.0 (CPAL)
//
// EXHIBIT A. CPAL version 1.0
// “The contents of this file are subject to the CPAL version 1.0 (the “License”);
// you may not use this file except in compliance with the License. You may obtain a
// copy of the License at http://isatab.sf.net/licenses/OntoMaton-license.html.
// The License is based on the Mozilla Public License version 1.1 but Sections
// 14 and 15 have been added to cover use of software over a computer network and
// provide for limited attribution for the Original Developer. In addition, Exhibit
// A has been modified to be consistent with Exhibit B.
//
// Software distributed under the License is distributed on an “AS IS” basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.
//
// The Original Code is OntoMaton.
// The Original Developer is the Initial Developer. The Initial Developer of the
// Original Code is the ISA Team (Eamonn Maguire, [email protected];
// Philippe Rocca-Serra, [email protected]; Susanna-Assunta Sansone, [email protected]; Alejandra Gonzalez-Beltran, [email protected]
// http://www.isa-tools.org). All portions of the code written by the ISA Team are
// Copyright (c) 2007-2020 ISA Team. All Rights Reserved.
//
// EXHIBIT B. Attribution Information
// Attribution Copyright Notice: Copyright (c) 2007-2020 ISA Team
// Attribution Phrase: Developed by the ISA Team
// Attribution URL: http://www.isa-tools.org
// Graphic Image provided in the Covered Code as file: http://isatab.sf.net/assets/img/tools/ontomaton-part-of-isatools.png
// Display of Attribution Information is required in Larger Works which are defined in the CPAL as a work which combines Covered Code or portions thereof with code not governed by the terms of the CPAL.
function showOntologySearch() {
var html = HtmlService.createHtmlOutputFromFile('Ontology-Search-Template')
.setTitle('OntoMaton - Ontology Search & Tagging')
.setWidth(300);
SpreadsheetApp.getUi()
.showSidebar(html);
}
function runSearch(service, term) {
return performSearch(service,term);
}
function performSearch(service, term) {
switch (service) {
case "bioportal":
return searchBioPortal(term);
case "lov":
return searchLOV(term);
case "ols":
return searchOLS(term);
default:
return searchLOV(term);
}
}
function searchBioPortal(term) {
try {
var ontologies = getBioPortalOntologies();
// only perform a search if there is a difference
var restriction = findRestrictionForCurrentColumn("BioPortal");
if (term.length > 2) {
// todo check if particular column has a restriction in the hidden sheet. If so, restrict the search.
var searchString = "http://data.bioontology.org/search?q=" + term + "&display_context=false";
if (restriction) {
if (restriction.branch)
searchString += "&ontology=" + restriction.ontologyId + "&subtree=" + restriction.branch + "&apikey=fd88ee35-6995-475d-b15a-85f1b9dd7a42";
else
searchString += "&ontologies=" + restriction.ontologyId + "&apikey=fd88ee35-6995-475d-b15a-85f1b9dd7a42";
}
// we cache results and try to retrieve them on every new execution.
var cacheResult = fetchFromCache(searchString);
var text, key;
if (cacheResult != undefined) {
return JSON.parse(cacheResult);
// SpreadsheetApp.getActiveSpreadsheet().toast("Terms retrieved from cache.", "Cache accessed", -1);
} else {
text = UrlFetchApp.fetch(searchString).getContentText();
var doc = JSON.parse(text);
var searchResultBeans = doc.collection;
var ontologyDictionary = {};
for (var resultIndex in searchResultBeans) {
var result = searchResultBeans[resultIndex];
var ontologyLabel = result.links.ontology.substring(result.links.ontology.lastIndexOf("/") + 1);
if (ontologyDictionary[ontologyLabel] == undefined) {
ontologyDictionary[ontologyLabel] = {"ontology-name": ontologies[ontologyLabel].name, "terms": []};
}
//key for the cache
key = ontologyLabel +":" + result["@id"]
var ontology_record = {
"label": result.prefLabel,
"id": key,
"ontology-label": ontologyLabel,
"ontology-name": ontologies[ontologyLabel].name,
"accession": result["@id"], //result.links.self,
"ontology": result.links.ontology,
"url": result["@id"] //result.links.ui
};
var ontology_record_string = JSON.stringify(ontology_record);
storeInCache(key, ontology_record_string);
ontologyDictionary[ontologyLabel].terms.push(ontology_record);
}
storeInCache(searchString, JSON.stringify(ontologyDictionary));
return ontologyDictionary;
}
} else {
throw 'Please enter a term with 3 or more characters.';
}
} catch (e) {
throw e;
}
return {};
}
function searchLOV(term) {
try {
// only perform a search if there is a difference
var restriction = findRestrictionForCurrentColumn("LOV");
var vocabularies = getLinkedOpenVocabularies();
var url = "http://lov.okfn.org/dataset/lov/api/v2/search?q=" + term;
var vocabShortname;
var vocabURI;
if (restriction.source) {
vocabShortname = restriction.ontologyId.replace(/\s/g, ""); //string trim
if (vocabularies[vocabShortname]) {
vocabURI = vocabularies[vocabShortname].uri;
if (vocabURI)
url = "http://lov.okfn.org/dataset/lov/api/v2/search?q=" + term + "&voc=" + vocabURI;
} else {
url = "";
}
}
if (url) {
// we cache results and try to retrieve them on every new execution.
var cacheResult = fetchFromCache(url);
var text, label;
if (cacheResult != null) {
text = cacheResult;
} else {
text = UrlFetchApp.fetch(url).getContentText();
}
var ontologyDictionary = {};
var parsed_result = JSON.parse(text);
if (!parsed_result.error) {
var results = parsed_result.results;
for (var i in results) {
var uriPrefixed = results[i]['prefixedName'][0];
label = uriPrefixed.split(':')[1];
var key = uriPrefixed;
var uri = results[i].uri[0];
var vocabularyPrefix = results[i]['vocabulary.prefix'][0];
if (vocabularyPrefix != null) {
var vocab_record = vocabularies[vocabularyPrefix];
var ontologyLabel = vocab_record.name + "\n (" + vocab_record.uri + ")";
if (ontologyDictionary[ontologyLabel] === undefined) {
ontologyDictionary[ontologyLabel] = {"ontology-name": vocab_record.name, "terms": []};
}
var ontology_record = {
"label": label,
"id": key,
"ontology-label": vocabularyPrefix,
"ontology-name": vocab_record.name,
"accession": uri,//vocab_record.uri,
"ontology": vocab_record.uri,
"details": "",
"url":uri
};
var ontology_record_string = JSON.stringify(ontology_record);
storeInCache(key, ontology_record_string);
ontologyDictionary[ontologyLabel].terms.push(ontology_record);
} //vocabularyPrefix not null
}//for
//storeInCache(url, JSON.stringify(text));
storeInCache(url, text);
return ontologyDictionary;
}// no error
}
} catch (e) {
Logger.log(e);
throw e;
}
return {};
}
/**
* @method
* @name searchOLS
* @param{string} term
*/
function searchOLS(term) {
try {
var restriction = findRestrictionForCurrentColumn("OLS");
var ontologies = getOLSOntologies();
var url = OLS_API_BASE_URI + '/search';
var queryObj = {
q: term,
rows: OLS_PAGINATION_SIZE,
start: 0,
ontology: restriction ? restriction.ontologyId : undefined
};
var queryString = jsonToQueryString(queryObj);
url += '?' + queryString;
var cacheResult = fetchFromCache(url);
if (cacheResult) {
return JSON.parse(cacheResult);
}
var text = UrlFetchApp.fetch(url).getContentText(), json = JSON.parse(text), ontologyDict = {};
var docs = json.response && json.response.docs;
if (!docs || docs.length === 0) {
throw "No Result found.";
}
docs.forEach(function(elem) {
var ontology = ontologies[elem.ontology_name], ontologyLabel = elem.ontology_prefix, record;
if (!ontologyDict[ontologyLabel]) {
ontologyDict[ontologyLabel] = {"ontology-name": elem.ontology_name, "terms": []};
record = {
label: elem.label,
id: elem.id,
'ontology-label': elem.ontology_prefix,
'ontology-name': elem.ontology_name,
accession: elem.iri,//elem.obo_id,
ontology: elem.ontology_name,
details: '',
url: elem.iri
};
storeInCache(record.id, JSON.stringify(record));
ontologyDict[ontologyLabel].terms.push(record);
}
});
storeInCache(url, JSON.stringify(ontologyDict));
// Logger.log(ontologyDict);
return ontologyDict;
}
catch(e) {
Logger.log(e);
throw(e);
}
}
function handleTermInsertion(term_id) {
try {
var sheet = SpreadsheetApp.getActiveSheet();
var selectedRange = sheet.getActiveSelection();
var textTerm = fetchFromCache(term_id);
var term = JSON.parse(textTerm);
var ontologyObject = {
"term": term["label"],
"accession": term_id,
"ontologyId": term["ontology-label"],
"ontologyVersion": term["ontology"],
"ontologyDescription": term["ontology-name"],
"url": term["url"]
}
// figure out whether the Term Source REF and Term Accession Number columns exist, if they do exist at all. Insertion technique will vary
// depending on the file being looked at.
var sourceAndAccessionPositions = getSourceAndAccessionPositionsForTerm(selectedRange.getColumn());
// add all terms into a separate sheet with all their information.
if (sourceAndAccessionPositions.sourceRef != undefined && sourceAndAccessionPositions.accession != undefined) {
insertOntologySourceInformationInInvestigationBlock(ontologyObject);
}
for (var row = selectedRange.getRow(); row <= selectedRange.getLastRow(); row++) {
// if the currently selected column is an ISA defined ontology term, then we should insert the source and accession in subsequent
// columns and add the ontology source information to the investigation file if it doesn't already exist.
if (sourceAndAccessionPositions.sourceRef != undefined && sourceAndAccessionPositions.accession != undefined) {
sheet.getRange(row, selectedRange.getColumn()).setValue(ontologyObject.term);
sheet.getRange(row, sourceAndAccessionPositions.sourceRef).setValue(ontologyObject.ontologyId);
sheet.getRange(row, sourceAndAccessionPositions.accession).setValue(ontologyObject.url);
} else {
var isDefaultInsertionMechanism = loadPreferences();
var selectedColumn = selectedRange.getColumn();
var nextColumn = selectedColumn + 1;
if (!isDefaultInsertionMechanism) {
sheet.getRange(row, selectedColumn).setValue(ontologyObject.term);
sheet.getRange(row, nextColumn).setValue(ontologyObject.url);
} else {
sheet.getRange(row, selectedColumn).setFormula('=HYPERLINK("' + ontologyObject.url + '","' + ontologyObject.term + '")')
}
}
}
insertTermInformationInTermSheet(ontologyObject);
}
catch(err) {
Logger.log(err);
throw err;
}
}