-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.js
828 lines (724 loc) · 28.5 KB
/
Code.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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
var serverURL = 'http://intentparser.sd2e.org';
var versionString = '3.2';
function onOpen() {
const ui = DocumentApp.getUi();
const tablesMenu = ui.createMenu('Create table templates');
tablesMenu.addItem('Controls', 'createControlsTable');
tablesMenu.addItem('Measurements', 'createTableMeasurements');
tablesMenu.addItem('Parameters', 'createParameterTable');
const tableHelpMenu = ui.createMenu('Tables');
tableHelpMenu.addItem('Controls', 'reportControlsInfo');
tableHelpMenu.addItem('Lab', 'reportLabInfo');
tableHelpMenu.addItem('Measurements', 'reportMeasurementsInfo');
tableHelpMenu.addItem('Parameters', 'reportParametersInfo');
const runExperimentMenu = ui.createMenu('Run Experiment');
runExperimentMenu.addItem('with Structured Request', 'executeExperiment');
runExperimentMenu.addItem('with OPIL', 'executeOpilExperiment');
const helpMenu = ui.createMenu('Help');
helpMenu.addSubMenu(tableHelpMenu);
helpMenu.addItem('About', 'showHelp');
const menu = ui.createMenu('Parse Intent');
menu.addItem('Add to SynBioHub', 'addToSynBioHub');
menu.addItem('Analyze from cursor', 'sendAnalyzeFromCursor');
menu.addItem('Analyze from top', 'sendAnalyzeFromTop');
menu.addItem('Calculate samples for measurements table', 'calculateSamples');
menu.addItem('Import Experimental Protocol template', 'experimentalProtocol');
menu.addItem('Generate OPIL', 'sendOpilRequest');
menu.addItem('Generate Report', 'sendGenerateReport');
menu.addItem('Generate Structured Request', 'sendGenerateStructuredRequest');
menu.addItem('Report Experiment Status', 'reportExperimentStatus');
menu.addSubMenu(runExperimentMenu);
menu.addItem('Suggest Additions by Spelling from cursor', 'addBySpellingFromCursor');
menu.addItem('Suggest Additions by Spelling from top', 'addBySpelling');
menu.addItem('Update experimental results', 'updateExperimentalResults');
menu.addItem('Validate Structured Request', 'sendValidateStructuredRequest');
menu.addSubMenu(tablesMenu);
menu.addItem('File Issues', 'reportIssues');
menu.addSubMenu(helpMenu);
menu.addToUi();
}
function experimentalProtocol(){
let doc = DocumentApp.getActiveDocument();
let cursorPosition = doc.getCursor();
if(cursorPosition == null) {
// Cursor position is null, so assume a selection
const selectionRange = doc.getSelection();
const rangeElement = selectionRange.getRangeElements()[0];
// Extract element and offset from end of selection
var el = rangeElement.getElement();
} else {
// Select element and off set from current position
var el = cursorPosition.getElement();
}
const childIndex = doc.getBody().getChildIndex(el);
const data = {'childIndex' : childIndex, 'tableType' : 'experimentProtocols'};
sendPost('/createTableTemplate', data);
}
function reportParametersInfo(){
var docId = DocumentApp.getActiveDocument().getId();
const data = {'tableType': 'parameter',
'documentId': docId};
var options = {
'method' : 'post',
'contentType': 'application/json',
'payload' : JSON.stringify(data)
};
var response = UrlFetchApp.fetch(serverURL + '/tableInformation', options);
param_info = response.getContentText();
showSidebar(param_info);
}
function reportControlsInfo(){
html_content = '<h2> Controls Table </h2>\n' +
'<p><b>Description</b>: control definition based on time, strain, contents, etc.</p>\n' +
'<b>Required fields:</b>\n' +
'<ul>\n' +
'\t<li><a href="https://schema.catalog.sd2e.org/schemas/control_type.json"> <b>Type</b></a>: an expected type for this control. <i>Example:</i> HIGH_FITC</li>\n' +
'\t<li><b>Strains</b>: a list of one or more text values representing a strain listed in the SBOL Dictionary lab name. <i>Example:</i> B_subtilis_comKS_mCherry_1x</li>\n' +
'</ul>\n' +
'<b>Optional fields:</b>\n' +
'<ul>\n' +
'\t<li><b>Channel</b>: a text value representing FCS channel. <i>Example:</i> BL1-A</li>\n' +
'\t<li><b>Contents</b>: a list of one or more text values representing the content of a control. A content can come in the form of a name or a name followed by a value, followed by a timepoint unit. <i>Example:</i> beta_estradiol or beta_estradiol 0.05 micromole</li>\n' +
'\t<li><b><a href="https://schema.catalog.sd2e.org/schemas/time_unit.json">Timepoints</a></b>: a list of one or more text values representing point in a time series. <i>Example:</i> 2, 4 hours</li>\n' +
'</ul>';
showSidebar(html_content);
}
function reportLabInfo(){
html_content = '<h2> Lab Table </h2>\n' +
'<p><b>Description</b>: information linked to a lab.</p>\n' +
'<b>Required fields:</b>\n' +
'<ul>\n' +
'\t<li><a href="https://schema.catalog.sd2e.org/schemas/lab.json"> <b>Lab</b></a>: a text value representing the lab that performed this experiment. <i>Example:</i> TACC</li>\n' +
'</ul>\n' +
'<b>Optionalfields:</b>\n' +
'<ul>\n' +
'\t<li><b>Experiment_id</b>: a text identifier, namespaced performer, for the experiment <i>Example:</i> 123</li>\n' +
'</ul>'
showSidebar(html_content);
}
function reportMeasurementsInfo(){
html_content = '<h2> Measurements Table </h2>\n' +
'<p><b>Description</b>: measurements expected to be produced for a run, broken down by measurement type and sample conditions</p>\n' +
'<b>Required fields:</b>\n' +
'<ul>\n' +
'\t<li><a href="https://schema.catalog.sd2e.org/schemas/measurement_type.json"> <b>Measurement Type</b></a>: an expected file type for this measurement. <i>Example:</i> RNA_SEQ</li>\n' +
'\t<li><a href="https://schema.catalog.sd2e.org/schemas/filetype_label.json"> <b>File Type</b></a>: a list of one or more expected file type for this measurement. <i>Example:</i> MSWORD, SPREADSHEET</li>\n' +
'</ul>\n' +
'<b>Optional fields:</b>\n' +
'<ul>\n' +
'\t<li><b>Batch</b>: a list of one or more numerical values representing the batches a measurement belongs to. <i>Example:</i> 1, 2, 3</li>\n' +
'\t<li><b>Controls</b>: a list of Control Table captions for representing expected control elements for this run <i>Example:</i> Table 1, Table 2</li>\n' +
'\t<li><b>Ods</b>: a list of one or more numerical values representing expected optical densities for this measurement. <i>Example:</i> 5</li>\n' +
'\t<li><b>Replicates</b>: a list of one or more numerical values representing expected number of replicates. <i>Example:</i> 6</li>\n' +
'\t<li><b>Strains</b>: a list of one or more string values representing expected strains for this measurement. Strains listed in this field must have a hyperlink that references to a SBH URI. <i>Example:</i> UWBF_6390</li>\n' +
'\t<li><a href="https://schema.catalog.sd2e.org/schemas/temperature.json"><b>Temperatures</b></a>: a list of one or more numerical values followed by a temperature unit representing expected temperatures for this measurement. <i>Example</i>: 30 celsius</li>\n' +
'\t<li><a href="https://schema.catalog.sd2e.org/schemas/time_unit.json"><b>Timepoints</b></a>: a list of one or more numerical values followed by a timepoint unit representing expected timepoints for this run. <i>Example:</i> 0, 4, 8, 12, 16 hour</li>\n' +
'\t<li><b>Column_id</b>: a list of one or more numerical values to signify which column of which run received which inducer concentration. <i>Example:</i> 2</li>\n' +
'\t<li><b>Row_id</b>: a list of one or more numerical values signify which row of which run received which inducer concentration. <i>Example:</i> 1</li>\n' +
'\t<li><b>lab_id</b>: a list of one or more text values to specify lab ids. <i>Example:</i> abc</li>\n' +
'\t<li><b>Number of Negative Controls</b>: a list of integers. <i>Example:</i> 1, 2, 3</li>\n' +
'\t<li><b>Use RNAse Inhibitor in Reaction</b>: a list of boolean values. <i>Example:</i> True, False</li>\n' +
'\t<li><b>DNA Reaction Concentration</b>: a list of integers. <i>Example:</i> 1, 2, 3</li>\n' +
'\t<li><b>Template DNA</b>: a list of string. <i>Example:</i> a, b, c</li>\n' +
'</ul>';
showSidebar(html_content);
}
function showHelp() {
var helpHTML = '\
<p>\
Intent Parser version %s\
</p>\
<p>\
The purpose of the intent parser add-on is to create a suite of tools that aids the user in creating well documented experimental plans.\
</p>\
<p>\
Several different actions help users link terms to SynbioHub, to create a better trail of documentation. \
The first action is <b><i>analyzing</i></b> the document, which searches for terms present in the SD2 spreadsheet dictionary and offers to insert links to the SynbioHub definition of those terms. \
Additionally, the document can be scanned for terms that do not exist in the <b><i>spelling</i></b> dictionary and suggest them as terms to possibly additions into the SD2 spreadsheet dictionary or to have a link manually added. \
Users can also highlight arbitrary terms and <b><i>add</i></b> a definition to SynbioHub for that term. \
This <b><i>add</i></b> dialog will also query SynbioHub for matches to the selected term and links to those terms can be added.\
</p>\
<p>\
Intent parser can also help create structured requests from the experimental request document. \
This works by creating a table for the measurements which can be parsed. \
The first step in this is creating a measurements table template, using the <b><i>create table templates</i></b> file menu option. \
The measurements table template dialog will ask a few questions about the table, and then insert the table template into the document. \
Users will need to enter the reagent names in the blank columns and fill in the rest of the table. \
Each table cell can accept a comma separated list of values and units should be specified where appropriate. \
If only one value in the comma-separated list has a unit, that unit will be used for all entries in the list. \
For instance, the entry "0, 4, 8, 12 hour" will use the unit of hour for each entry. \
Once the measurements table is complete, a structured request can be generated with the <b><i>Generate Structured Request</i></b> file menu option, which will create a json file that can be saved for later use. \
Additionally, the <b><i>Validate Structured Request</i></b> option can be used to generate and validate a structured request. \
If the request fails validation, an error message will be printed which indicates that the request failed validation, and why. \
</p>\
<p>\
Problems? <a href="https://gitlab.sd2e.org/sd2program/experimental-intent-parser/issues" target=_blank>File and issue</a>\
</p>\
';
verFormattedHTML = Utilities.formatString(helpHTML, versionString);
showModalDialog(verFormattedHTML, 'Help', 600, 600);
}
function validate_uri(uri) {
try {
var response = UrlFetchApp.fetch(uri);
if (response.getResponseCode() == 200) {
return true;
}
else {
return false;
}
}
catch (e) {
return false;
}
}
function enterLinkPrompt(title, msg) {
var ui = DocumentApp.getUi();
var result = ui.prompt(title, msg, ui.ButtonSet.OK_CANCEL);
// Process the user's response.
var button = result.getSelectedButton();
var text = result.getResponseText();
while (button == ui.Button.OK) {
if (validate_uri(text)) {
return [true, text];
}
else { // If URI is invalid, reprompt
var result = ui.prompt('Entered URI was invalid!\n' + title, msg, ui.ButtonSet.OK_CANCEL);
button = result.getSelectedButton();
text = result.getResponseText();
}
}
return [false, text];
}
function executeExperiment() {
sendPost('/run_experiment');
}
function executeOpilExperiment() {
sendPost('/run_opil_experiment');
}
function reportExperimentStatus() {
let doc = DocumentApp.getActiveDocument();
let cursorPosition = doc.getCursor();
if(cursorPosition == null) {
// Cursor position is null, so assume a selection
const selectionRange = doc.getSelection();
const rangeElement = selectionRange.getRangeElements()[0];
// Extract element and offset from end of selection
var el = rangeElement.getElement();
}
else {
// Select element and off set from current position
var el = cursorPosition.getElement();
}
const childIndex = doc.getBody().getChildIndex(el);
const data = {'childIndex' : childIndex};
sendPost('/experiment_status', data);
}
function sendMessage(message) {
var request = {'message': message};
var requestJSON = JSON.stringify(request);
var options = {'method' : 'post',
'payload' : requestJSON };
UrlFetchApp.fetch(serverURL + '/message', options);
}
function buttonClick(buttonName) {
sendPost('/buttonClick', {'buttonId' : buttonName});
}
function processActions(response) {
if( typeof(response.actions) == 'undefined' ) {
return;
}
var actions = response.actions;
waitForMoreActions = false;
for( var actionKey in actions) {
var actionDesc = actions[actionKey];
switch(actionDesc['action']) {
case 'highlightText':
var paragraphIndex = actionDesc['paragraph_index'];
var offset = actionDesc['offset'];
var endOffset = actionDesc['end_offset'];
highlightDocText(paragraphIndex, offset, endOffset);
break;
case 'linkText':
var paragraphIndex = actionDesc['paragraph_index'];
var offset = actionDesc['offset'];
var endOffset = actionDesc['end_offset'];
var url = actionDesc['url'];
linkDocText(paragraphIndex, offset, endOffset, url);
break;
case 'calculateSamples':
var tableIds = actionDesc['tableIds'];
var sampleIndices = actionDesc['sampleIndices'];
var sampleValues = actionDesc['sampleValues'];
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var tables = body.getTables();
for (var tIdx = 0; tIdx < tableIds.length; tIdx++) {
sampleColIdx = sampleIndices[tIdx];
var numRows = tables[tableIds[tIdx]].getNumRows();
// Samples column doesn't exist
if (sampleColIdx < 0){
// Create new column for samples
var numCols = tables[tableIds[tIdx]].getRow(0).getNumCells();
tables[tableIds[tIdx]].getRow(0).appendTableCell("samples");
for (var rowIdx = 1; rowIdx < numRows; rowIdx++)
{
tables[tableIds[tIdx]].getRow(rowIdx).appendTableCell();
}
sampleColIdx = numCols;
}
for (var rowIdx = 1; rowIdx < numRows; rowIdx++) {
var tableCell = tables[tableIds[tIdx]].getRow(rowIdx).getCell(sampleColIdx);
tableCell.setText(sampleValues[tIdx][rowIdx - 1]);
}
}
break
case 'addTable':
try{
var childIndex = actionDesc['cursorChildIndex'];
var tableData = actionDesc['tableData'];
var colSizes = actionDesc['colSizes'];
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var newTable = body.insertTable(childIndex, tableData);
var headerRow = newTable.getRow(0);
// Reset formatting
var tableStyle = {};
tableStyle[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.LEFT;
tableStyle[DocumentApp.Attribute.FONT_SIZE] = 11;
tableStyle[DocumentApp.Attribute.FONT_SIZE] = 11;
tableStyle[DocumentApp.Attribute.BOLD] = false;
tableStyle[DocumentApp.Attribute.ITALIC] = false;
tableStyle[DocumentApp.Attribute.BACKGROUND_COLOR] = '#FFFFFF';
tableStyle[DocumentApp.Attribute.FOREGROUND_COLOR] = '#000000';
newTable.setAttributes(tableStyle);
var style = {};
style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER;
style[DocumentApp.Attribute.FONT_SIZE] = 11;
style[DocumentApp.Attribute.BOLD] = true;
headerRow.setAttributes(style);
for(var idx=0; idx < colSizes.length; ++idx) {
newTable.setColumnWidth(idx, colSizes[idx] * 7);
}
if (actionDesc['tableType'] == 'measurements') {
labTableData = actionDesc['tableLab'];
var newLabTable = body.insertTable(childIndex, labTableData);
newLabTable.setAttributes(tableStyle);
}
}
catch (err){
console.log(err);
}
break
case 'updateExperimentResults':
var headerIdx = actionDesc['headerIdx'];
var contentIdx = actionDesc['contentIdx'];
var expData = actionDesc['expData'];
var expLinks = actionDesc['expLinks'];
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
if (headerIdx != -1 && contentIdx != -1) {
var paragraphs = body.getParagraphs();
para = paragraphs[contentIdx];
para.setText('\n');
for (var i = 0; i < expData.length; i++) {
for (var p = 0; p < expData[i].length; p++) {
newTxt = para.appendText(expData[i][p]);
if (i < expLinks.length && expLinks[i][p] != '') {
newTxt.setLinkUrl(expLinks[i][p]);
} else {
newTxt.setLinkUrl('');
}
}
}
} else {
var header_para = body.appendParagraph('Experiment Results');
header_para.setHeading(DocumentApp.ParagraphHeading.HEADING2);
para = body.appendParagraph('\n');
for (var i = 0; i < expData.length; i++) {
for (var p = 0; p < expData[i].length; p++) {
newTxt = para.appendText(expData[i][p]);
if (i < expLinks.length && expLinks[i][p] != '') {
newTxt.setLinkUrl(expLinks[i][p]);
} else {
newTxt.setLinkUrl('');
}
}
}
}
break;
case 'showSidebar':
showSidebar(actionDesc['html']);
break;
case 'showProgressbar':
showSidebar(actionDesc['html']);
var p = PropertiesService.getDocumentProperties();
p.setProperty("analyze_progress", '0');
waitForMoreActions = true;
break;
case 'updateProgress':
waitForMoreActions = true;
var p = PropertiesService.getDocumentProperties();
p.setProperty("analyze_progress", actionDesc['progress']);
break;
case 'reportContent':
processReportContent(actionDesc['report']);
break;
case 'showModalDialog':
showModalDialog(actionDesc['html'], actionDesc['title'],
actionDesc['width'], actionDesc['height']);
break;
default:
break;
}
}
return waitForMoreActions;
}
function showSidebar(html) {
var user = Session.getActiveUser();
var userEmail = user.getEmail();
var ui = DocumentApp.getUi();
var htmlOutput = HtmlService.createHtmlOutput(html);
ui.showSidebar(htmlOutput);
}
function showModalDialog(html, title, width, height) {
var ui = DocumentApp.getUi();
var htmlOutput = HtmlService.createHtmlOutput(html);
htmlOutput.setWidth(width);
htmlOutput.setHeight(height);
ui.showModalDialog(htmlOutput, title);
}
function highlightDocText(paragraphIndex, offset, endOffset) {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var paragraph = body.getParagraphs()[paragraphIndex];
var docText = paragraph.editAsText();
var selectionRange = doc.newRange();
selectionRange.addElement(docText, offset, endOffset);
doc.setSelection(selectionRange.build());
}
function linkDocText(paragraphIndex, offset, endOffset, url) {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var paragraph = body.getParagraphs()[paragraphIndex];
var docText = paragraph.editAsText();
docText.setLinkUrl(offset, endOffset, url);
}
function sendPost(resource, data) {
var docId = DocumentApp.getActiveDocument().getId();
var user = Session.getActiveUser();
var userEmail = user.getEmail();
var request = {
'documentId': docId,
'user': user,
'userEmail': userEmail
};
if( typeof(data) != 'undefined' ) {
request['data'] = data;
}
var requestJSON = JSON.stringify(request);
var options = {
'method' : 'post',
'payload' : requestJSON,
'contentType': 'application/json'
};
shouldProcessActions = true;
while (shouldProcessActions) {
response = UrlFetchApp.fetch(serverURL + resource, options);
var responseText = response.getContentText();
var responseOb = JSON.parse(responseText);
shouldProcessActions = processActions(responseOb);
}
return responseOb.results;
}
//Identifies a paragraph with an array of hierarchy indicies
function identifyParagraph(element) {
var foundParagraph = true;
var identity = [];
var parent = element.getParent();
while(parent != null) {
elementType = element.getType();
var idx = parent.getChildIndex(element);
identity.push(idx);
element = parent;
parent = element.getParent();
}
return identity.reverse();
}
function identity2str(identity) {
str = '';
for(i=0; i<identity.length; ++i) {
val = identity[i];
str += '' + val + '.';
}
return str;
}
function compareIdentities(identity1, identity2) {
for(var idx=0; idx<identity1.length; ++idx) {
if(idx >= identity2.length) {
// identity2 is smaller
// This compare function returns true here because
// identity1 is a parent of identity2
return 0;
}
if(identity1[idx] < identity2[idx]) {
// identity1 is smaller
return -1;
}
if(identity1[idx] > identity2[idx]) {
// identity1 is larger
return 1;
}
}
if(identity2.length > identity1.length) {
// identity1 is smaller
return -1;
}
// identity1 and identity2 are equal
return 0;
}
//Find a paragraph identified by an array or hierarchy
//indicies using a binary search
function findParagraph(identity, paragraphList) {
if(paragraphList.length < 4) {
// If the list size is less than 4, do a brute force
// search
for(var idx=0; idx<paragraphList.length; ++idx) {
var pCompare = paragraphList[idx];
var valIdentity = identifyParagraph(pCompare);
if(compareIdentities(identity, valIdentity) == 0) {
return idx;
}
}
return null;
}
// Use the middle element to decide whether to search
// the first half of entries of the second half of
// entries
var middle = Math.floor(paragraphList.length / 2);
var middleElement = paragraphList[ middle ];
var middleIdentity = identifyParagraph(middleElement);
if(compareIdentities(identity, middleIdentity) < 0) {
var newList = paragraphList.slice(0, middle);
var startIndex = 0;
} else {
var newList = paragraphList.slice(middle,
paragraphList.length);
startIndex = middle;
}
return startIndex + findParagraph(identity, newList);
}
//Finds TEXT element under element
function findTEXT(element) {
var elType = element.getType();
if(elType == elType.TEXT) {
return element;
}
if(typeof element.getNumChildren != 'function') {
return null;
}
for(var i=0; i<element.getNumChildren(); ++i) {
var child = element.getChild(i);
var result = findTEXT(child);
if(result != null) {
return result;
}
}
return null;
}
//Find the cursor location
function findCursor() {
var doc = DocumentApp.getActiveDocument();
var cursorPosition = doc.getCursor();
if(cursorPosition == null) {
// Cursor position is null, so assume a selection
selectionRange = doc.getSelection();
rangeElement = selectionRange.getRangeElements()[0];
// Extract element and offset from end of selection
var el = rangeElement.getElement();
var offset = rangeElement.getEndOffsetInclusive();
} else {
// Select element and off set from current position
var el = cursorPosition.getElement();
var offset = cursorPosition.getOffset();
}
var elementType = el.getType();
// Handle special case of cursor at the end of a paragraph
// Paragraphs appear to only have an offset of 0 or 1 (beginning or end)
// If we are at the end of a paragraph, we want to instead use the end of the text element in the paragraph
if(elementType != elementType.TEXT && offset > 0) {
var textElement = findTEXT(el);
if(textElement != null) {
var length = textElement.getText().length;
offset = length - 1;
}
}
return getLocation(el, offset);
}
function getLocation(el, offset) {
var doc = DocumentApp.getActiveDocument();
// Get the ordared list of paragraphs
var plist = doc.getBody().getParagraphs();
// Identify the element by its location in the
// document hierarchy.
identity = identifyParagraph(el);
// Find the index in plist of the paragraph with the
// same
var result = findParagraph(identity, plist);
if(result == null) {
return null;
} else {
return {'paragraphIndex': result,
'offset': offset};
}
}
function updateExperimentalResults() {
sendPost('/updateExperimentalResults');
}
function calculateSamples() {
sendPost('/calculateSamples');
}
function sendAnalyzeFromTop() {
sendPost('/analyzeDocument');
}
function sendAnalyzeFromCursor() {
var cursorLocation = findCursor();
sendPost('/analyzeDocument', cursorLocation);
}
function sendGenerateReport() {
var docId = DocumentApp.getActiveDocument().getId();
var html = '';
html += '<script>\n';
html += 'function onSuccess() {\n';
html += ' google.script.host.close()\n';
html += '}\n';
html += '</script>\n';
html += '\n';
html += '<p>';
html += '<center>';
html += 'Download Report ';
html += '<a href=' + serverURL + '/document_report/d/';
html += docId + ' target=_blank>here</a>';
html += '</p>';
html += '\n';
html += '<input id=okButton Button value="Done" ';
html += 'type="button" onclick="onSuccess()" />\n';
html += '</center>';
showModalDialog(html, 'Download', 300, 100);
}
function reportIssues(){
helpHTML = '\
<p>Something unexpected happen with the intent-parser plugin?</p> \
<p>Want to request a feature support?</p> \
<p>Send a bug report <a href="https://gitlab.sd2e.org/sd2program/experimental-intent-parser/issues" target=_blank>here</a>.</p> \
';
verFormattedHTML = Utilities.formatString(helpHTML, versionString);
showModalDialog(verFormattedHTML, 'Issues', 400, 200);
}
function sendOpilRequest(){
sendPost('/generateOpilRequest', getBookmarks());
}
function sendValidateStructuredRequest() {
sendPost('/validateStructuredRequest', getBookmarks());
}
function sendGenerateStructuredRequest() {
sendPost('/generateStructuredRequest', getBookmarks());
}
function getBookmarks(){
var doc = DocumentApp.getActiveDocument();
var bookmarks = doc.getBookmarks();
var result = [];
for(var bookmark of bookmarks){
var bookmark_id = bookmark.getId();
var bookmark_text = bookmark.getPosition().getElement().asText().getText();
result.push({id:bookmark_id, text: bookmark_text});
}
return {'bookmarks': result};
}
function addToSynBioHub() {
var doc = DocumentApp.getActiveDocument();
selectionRange = doc.getSelection();
if(selectionRange == null) {
return;
}
// Cursor position is null, so assume a selection
var selectionRange = doc.getSelection();
var rangeElements = selectionRange.getRangeElements();
var firstElement = rangeElements[0];
var lastElement = rangeElements[ rangeElements.length - 1 ];
// Extract element and offset from end of selection
var startEl = firstElement.getElement();
var startOffset = firstElement.getStartOffset();
var startLocation = getLocation(startEl, startOffset);
var endEl = lastElement.getElement();
var endOffset = lastElement.getEndOffsetInclusive();
var endLocation = getLocation(endEl, endOffset);
var selection = {'start': startLocation,
'end': endLocation};
sendPost('/addToSynBioHub', selection);
}
function addBySpelling() {
sendPost('/addBySpelling');
}
function addBySpellingFromCursor() {
const cursorLocation = findCursor();
sendPost('/addBySpelling', cursorLocation);
}
function submitForm(formData) {
return sendPost('/submitForm', formData);
}
function postFromClient(postInfo) {
return sendPost(postInfo.resource, postInfo.data);
}
function createControlsTable(){
let doc = DocumentApp.getActiveDocument();
let cursorPosition = doc.getCursor();
if(cursorPosition == null) {
// Cursor position is null, so assume a selection
const selectionRange = doc.getSelection();
const rangeElement = selectionRange.getRangeElements()[0];
// Extract element and offset from end of selection
var el = rangeElement.getElement();
}
else {
// Select element and off set from current position
var el = cursorPosition.getElement();
}
const childIndex = doc.getBody().getChildIndex(el);
const data = {'childIndex' : childIndex, 'tableType' : 'controls'};
sendPost('/createTableTemplate', data);
}
function createTableMeasurements() {
let doc = DocumentApp.getActiveDocument();
let cursorPosition = doc.getCursor();
if(cursorPosition == null) {
// Cursor position is null, so assume a selection
const selectionRange = doc.getSelection();
const rangeElement = selectionRange.getRangeElements()[0];
// Extract element and offset from end of selection
var el = rangeElement.getElement();
} else {
// Select element and off set from current position
var el = cursorPosition.getElement();
}
const childIndex = doc.getBody().getChildIndex(el);
const data = {'childIndex' : childIndex, 'tableType' : 'measurements'};
sendPost('/createTableTemplate', data);
}
function createParameterTable(){
let doc = DocumentApp.getActiveDocument();
let cursorPosition = doc.getCursor();
if(cursorPosition == null) {
// Cursor position is null, so assume a selection
const selectionRange = doc.getSelection();
const rangeElement = selectionRange.getRangeElements()[0];
// Extract element and offset from end of selection
var el = rangeElement.getElement();
} else {
// Select element and off set from current position
var el = cursorPosition.getElement();
}
const childIndex = doc.getBody().getChildIndex(el);
const data = {'childIndex' : childIndex, 'tableType' : 'parameters'};
sendPost('/createTableTemplate', data);
}