forked from bigbio/proma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OntologySearch.gs
125 lines (111 loc) · 4.54 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
function showOntologySearch() {
var html = HtmlService.createHtmlOutputFromFile('Ontology-Search-Template')
.setTitle('PROMA - Ontology Search & Tagging')
.setWidth(300);
SpreadsheetApp.getUi()
.showSidebar(html);
}
function runSearch(term) {
return searchOLS(term);
}
/**
* @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;
}
}