forked from bigbio/proma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.gs
370 lines (299 loc) · 14.5 KB
/
Utils.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
var OLS_API_BASE_URI = "http://www.ebi.ac.uk/ols/api";
var OLS_PAGINATION_SIZE = 500; // MAX pagination size
/**
* @description serializes an object to a queryString
*/
function jsonToQueryString(json) {
return Object.keys(json).map(function(key) {
var value = json[key] == null ? '' : Array.isArray(json[key]) ? json[key].join() : json[key];
return encodeURIComponent(key) + '=' + encodeURIComponent(value);
}).join('&');
}
function getSourceAndAccessionPositionsForTerm(column) {
// we will look at positions 1 & 2 after the column to determine if the source and accession columns
// particular to ISAtab are present.
var sheet = SpreadsheetApp.getActiveSheet();
var sourceObject = new Object();
for (var columnIndex = column + 1; columnIndex <= column + 2; columnIndex++) {
if (sheet.getRange(1, columnIndex).getValue() == "Term Source REF") {
sourceObject.sourceRef = columnIndex;
}
if (sheet.getRange(1, columnIndex).getValue() == "Term Accession Number") {
sourceObject.accession = columnIndex;
}
}
return sourceObject;
}
function createOntologyObjectFromString(ontologyString) {
// processes the ontology string into an ontology object to be used for population of ISA sections.
var ontologyDetails = ontologyString.split("::");
var ontologyObject = new Object();
// BioPortal -> cy5::http://purl.obolibrary.org/obo/CHEBI_37989::1123::47203::Ontology for Biomedical Investigations
// LOV -> swpo:hasStreetAddress::http://sw-portal.deri.org/ontologies/swportal#hasStreetAddress
ontologyObject.term = ontologyDetails[0];
ontologyObject.accession = ontologyDetails[1];
ontologyObject.ontologyId = ontologyDetails[2];
ontologyObject.conceptId = ontologyDetails[3];
ontologyObject.ontologyVersion = ontologyDetails[4];
ontologyObject.ontologyDescription = ontologyDetails[5];
// we will have an additional freeText variable from the auto tagger value
if (ontologyDetails.length > 5) {
ontologyObject.freeText = ontologyDetails[6];
}
return ontologyObject;
}
function insertOntologySourceInformationInInvestigationBlock(ontologyObject) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var investigationSheet;
var ontologySectionStartIndex;
// find investigation file
for (var sheet in sheets) {
if (sheets[sheet].getName().indexOf("i_") > -1) {
// we've now got the investigation file.
// insert the ontology source information if it doesn't already exist here.
investigationSheet = sheets[sheet];
break;
}
}
// find section we're looking for.
if (investigationSheet != undefined) {
// we need to find the section, which should always be at position 1 in the investigation file.
// However, we should be careful and allow for automated discovery of the location.
for (var row = 1; row <= investigationSheet.getLastRow(); row++) {
if (investigationSheet.getRange(row, 1).getValue() == "ONTOLOGY SOURCE REFERENCE") {
ontologySectionStartIndex = row;
break;
}
}
}
if (ontologySectionStartIndex != undefined) {
// now we can proceed to adding the information about the ontology source, if it doesn't already exist.
var locationInformation = getIndexesToInsertInto(investigationSheet, ontologySectionStartIndex, ontologyObject.ontologyId);
investigationSheet.getRange(locationInformation.sourceName, locationInformation.insertionPoint).setValue(ontologyObject.ontologyId);
investigationSheet.getRange(locationInformation.sourceFile, locationInformation.insertionPoint).setValue("");
investigationSheet.getRange(locationInformation.sourceVersion, locationInformation.insertionPoint).setValue(ontologyObject.ontologyVersion);
investigationSheet.getRange(locationInformation.sourceDescription, locationInformation.insertionPoint).setValue(ontologyObject.ontologyDescription);
}
}
function getIndexesToInsertInto(sheet, ontologySectionIndex, sourceName) {
// will find either the place where this source name currently resides (to update it) or the place to insert all other terms.
var locationInformation = new Object();
// default.
locationInformation.insertionPoint = 2;
sheet.insertColumns(sheet.getLastColumn(), 1);
for (var rowIndex = ontologySectionIndex + 1; rowIndex <= ontologySectionIndex + 4; rowIndex++) {
var value = sheet.getRange(rowIndex, 1).getValue();
if (value == "Term Source Name") {
locationInformation.sourceName = rowIndex;
for (var columnIndex = 2; columnIndex <= sheet.getMaxColumns(); columnIndex++) {
// if the value is empty, or equals the current source name, then add it.
if (sheet.getRange(rowIndex, columnIndex).getValue() == "" || sheet.getRange(rowIndex, columnIndex).getValue() == sourceName) {
locationInformation.insertionPoint = columnIndex;
break;
}
}
} else if (value == "Term Source File") {
locationInformation.sourceFile = rowIndex;
} else if (value == "Term Source Version") {
locationInformation.sourceVersion = rowIndex;
} else if (value == "Term Source Description") {
locationInformation.sourceDescription = rowIndex;
}
}
return locationInformation;
}
function insertTermInformationInTermSheet(ontologyObject) {
var previousSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var activeRange = previousSheet.getActiveRange();
var termSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Terms");
if(termSheet == undefined) {
termSheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet("Terms");
SpreadsheetApp.getActiveSpreadsheet().toast("The Term sheet is used to maintain information about all the ontologies you have entered.",
"Term Sheet Added Automatically", -1);
termSheet.getRange(1, 1).setValue("Term Name");
termSheet.getRange(1, 2).setValue("Term URI");
termSheet.getRange(1, 3).setValue("Ontology Source");
termSheet.getRange(1, 4).setValue("Ontology URI");
termSheet.getRange(1, 5).setValue("Ontology Full Name");
// Go back to the previous sheet...
SpreadsheetApp.setActiveSheet(previousSheet)
SpreadsheetApp.getActiveSheet().setActiveRange(activeRange);
}
if(termSheet != undefined) {
// we have a term sheet, so we can enter information about the term here.
var insertionRow = findNextBlankRow(termSheet);
termSheet.getRange(insertionRow, 1).setValue(ontologyObject.term);
termSheet.getRange(insertionRow, 2).setValue(ontologyObject.url);
termSheet.getRange(insertionRow, 3).setValue(ontologyObject.ontologyId);
termSheet.getRange(insertionRow, 4).setValue(ontologyObject.ontologyVersion);
termSheet.getRange(insertionRow, 5).setValue(ontologyObject.ontologyDescription);
}
}
function insertSDRFInRecordSheet(modification,parameter,ontologyObject){
var previousSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var activeRange = previousSheet.getActiveRange();
var RecordSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Record");
if(RecordSheet == undefined) {
RecordSheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet("Record");
RecordSheet.getRange(1, 1).setValue("Parameter Type");
RecordSheet.getRange(1, 2).setValue("Paremeter Value")
RecordSheet.getRange(1, 3).setValue("Term Name");
RecordSheet.getRange(1, 4).setValue("Ontology Source");
RecordSheet.getRange(1, 5).setValue("Term URI");
SpreadsheetApp.setActiveSheet(previousSheet)
SpreadsheetApp.getActiveSheet().setActiveRange(activeRange);
}
if(RecordSheet != undefined) {
// we have a term sheet, so we can enter information about the term here.
var insertionRow = findNextBlankRow(RecordSheet);
if(parameter == 'modification parameters'){
RecordSheet.getRange(insertionRow, 1).setValue("modification parameters");
}
else if(parameter == 'instrument'){
RecordSheet.getRange(insertionRow, 1).setValue("instrument");
}
else if(parameter == 'cleavage agent details'){
RecordSheet.getRange(insertionRow, 1).setValue("cleavage agent details");
}
else if(parameter == 'label'){
RecordSheet.getRange(insertionRow, 1).setValue("label");
}
RecordSheet.getRange(insertionRow, 2).setValue(modification);
RecordSheet.getRange(insertionRow, 3).setValue(ontologyObject.term);
RecordSheet.getRange(insertionRow, 4).setValue(ontologyObject.ontologyId);
RecordSheet.getRange(insertionRow, 5).setValue(ontologyObject.url);
}
}
function findNextBlankRow(sheet) {
return sheet.getLastRow()+1;
}
function findRestrictionForCurrentColumn(service) {
var restriction = new Object();
restriction.ontologyId = "";
restriction.source = "";
restriction.branch = "";
restriction.version = "";
try {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var restrictionSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Restrictions");
var activeSheet = SpreadsheetApp.getActiveSheet();
var selectedRange = activeSheet.getActiveSelection();
var columnName = activeSheet.getRange(1, selectedRange.getColumn()).getValue();
var transposedColumnName = activeSheet.getRange(selectedRange.getRow(), 1).getValue();
if (restrictionSheet != undefined) {
for (var row = 1; row <= restrictionSheet.getLastRow(); row++) {
var restrictionColumn = restrictionSheet.getRange(row, 1).getValue();
var serviceColumn = restrictionSheet.getRange(row, 6).getValue();
if (restrictionColumn == columnName && serviceColumn == service) {
restriction.ontologyId = restrictionSheet.getRange(row, 2).getValue();
restriction.branch = restrictionSheet.getRange(row, 3).getValue();
restriction.version = restrictionSheet.getRange(row, 4).getValue();
restriction.service = restrictionSheet.getRange(row, 6).getValue();
return restriction;
}
if(restrictionColumn == transposedColumnName && serviceColumn == service) {
restriction.ontologyId = restrictionSheet.getRange(row, 2).getValue();
restriction.branch = restrictionSheet.getRange(row, 3).getValue();
restriction.version = restrictionSheet.getRange(row, 4).getValue();
restriction.service = restrictionSheet.getRange(row, 6).getValue();
return restriction;
}
}
}
} catch(e) {
throw e;
} finally {
return restriction;
}
}
function fetchFromCache(searchString) {
var cache = CacheService.getPrivateCache();
var cachedContent = cache.get(searchString);
if (cachedContent != null) {
return cachedContent;
}
}
function storeInCache(searchString, content) {
try {
var cache = CacheService.getPrivateCache();
cache.put(searchString, content, 1500); // caches result for 25 minutes
} catch(e) {
}
}
function createLabel(app, text, fontfamily, fontweight, fontsize, color) {
var label = app.createLabel(text);
label.setStyleAttribute("font-family", fontfamily).setStyleAttribute("font-weight", fontweight)
.setStyleAttribute("font-size", fontsize).setStyleAttribute("color", color);
return label;
}
function itemDefinitionHandler(e) {
var app = UiApp.getActiveApplication();
var value = e.parameter.source;
var term = value.substring(0,value.indexOf("::"));
var definition = value.substring(value.lastIndexOf("::") + 2);
SpreadsheetApp.getActiveSpreadsheet().toast("Definition: " + definition, term, -1);
return app;
}
function itemDefinitionHandlerLOV(e) {
var app = UiApp.getActiveApplication();
var value = e.parameter.source;
var term = value.substring(0,value.indexOf("::"));
var definition = value.substring(value.lastIndexOf("::") + 2);
if(definition == "") {
definition = "No definition available for this term.";
}
SpreadsheetApp.getActiveSpreadsheet().toast(definition, term, -1);
return app;
}
/**
The cache items have a limited size. This method splits up large
results and can put them back together again
cache is an instance of CacheService
cache key will be something like lov or bioportal
toStore is the text to keep
**/
function splitResultAndCache(cache, cacheKey, toStore) {
var fragments = 1;
var value_length = 10000;
// two hour expiration time.
var expiration_time_secs = 7200;
if(toStore.length > value_length) {
fragments = Math.floor(toStore.length/value_length + (toStore.length%value_length > 0 ? 1 : 0));
}
var fragmentCount = 0;
while(fragmentCount < fragments) {
var string_fragment = toStore.substring(fragmentCount * value_length, (fragmentCount + 1) *value_length);
cache.put(cacheKey + "_" + fragmentCount, string_fragment, value_length);
fragmentCount++;
}
cache.put(cacheKey + "_fragments", fragmentCount, expiration_time_secs);
}
function getCacheResultAndMerge(cache, cacheKey) {
var fragments = cache.get(cacheKey + "_fragments");
var fullResult = "";
var fragmentCount = 0;
while(fragmentCount < fragments) {
fullResult += cache.get(cacheKey + "_" + fragmentCount);
fragmentCount++;
}
return fullResult;
}
function getTextFromHtml(html) {
return getTextFromNode(Xml.parse(html, true).getElement());
}
function getTextFromNode(x) {
switch(x.toString()) {
case 'XmlText': return x.toXmlString();
case 'XmlElement': return x.getNodes().map(getTextFromNode).join('');
default: return '';
}
}
function replaceControlCharacters(text) {
try {
var escaped_string = text.replace(/\\n/g, "\\n");
return escaped_string;
} catch (e){
return text;
}
}