-
Notifications
You must be signed in to change notification settings - Fork 17
/
ReferenceDetails.js
557 lines (480 loc) · 16.9 KB
/
ReferenceDetails.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
////
//// A lot of the commented out stuff in the other completely gone here.
////
// Let jshint pass over over our external globals (browserify takes
// care of it all).
/* global jQuery */
/* global global_acc */
/* global global_live_search_query */
/* global global_live_search_filters */
/* global global_live_search_pins */
var us = require('underscore');
var bbop = require('bbop-core');
var widgets = require('bbop-widget-set');
var html = widgets.html;
// Config.
var amigo = new (require('amigo2-instance-data'))(); // no overload
var golr_conf = require('golr-conf');
var gconf = new golr_conf.conf(amigo.data.golr);
var sd = amigo.data.server;
var gserv = amigo.data.server.golr_base;
var gserv_download = amigo.data.server.golr_bulk_base;
var defs = amigo.data.definitions;
// Linker.
var linker = amigo.linker;
// Handler.
var handler = amigo.handler;
// Management.
var jquery_engine = require('bbop-rest-manager').jquery;
var golr_manager = require('bbop-manager-golr');
var golr_response = require('bbop-response-golr');
// And a basic response.
var rest_response = require('bbop-rest-response').base;
// // XML.
// var xpath = require('xpath');
// var dom = require('xmldom').DOMParser;
var jxon = require('jxon');
// Aliases.
var dlimit = defs.download_limit;
//
function UnableToMakeContact(msg){
// Clear.
jQuery('#info-area').empty();
jQuery('#results-area').empty();
// Reformat as needed.
jQuery('#top-panel').removeClass('panel-default');
jQuery('#top-panel').addClass('panel-danger');
jQuery('#results-area').removeClass('row');
// Apologize.
jQuery('#info-area').append(
['<p class="well">',
'Unable to contact NCBI E-utilities for ID: "' + global_acc + '".',
'<br />',
msg,
'</p>'].join(' '));
jQuery('#results-area').append('<p class="well">No results available.</p>');
}
//
function ReferenceDetailsInit(){
// Logger.
var logger = new bbop.logger();
logger.DEBUG = true;
function ll(str){ logger.kvetch('RX: ' + str); }
ll('');
ll('ReferenceDetails.js');
ll('ReferenceDetailsInit start...');
// Use jQuery UI to tooltip-ify doc.
var tt_args = {'position': {'my': 'left bottom', 'at': 'right top'}};
jQuery('.bbop-js-tooltip').tooltip(tt_args);
// // Tabify the layout if we can (may be in a non-tabby version).
// var dtabs = jQuery("#display-tabs");
// if( dtabs ){
// ll('Apply tabs...');
// jQuery("#display-tabs").tabs();
// jQuery("#display-tabs").tabs('option', 'active', 0);
// }
///
/// Manager setup.
///
var engine = new jquery_engine(golr_response);
engine.method('GET');
engine.use_jsonp(true);
var refman = new golr_manager(gserv, gconf, engine, 'async');
var confc = gconf.get_class('annotation');
// // Setup the annotation profile and make the annotation document
// // category and the current acc sticky in the filters.
// var refman_args = {
// 'linker': linker,
// 'handler': handler,
// 'spinner_shield_message' : 'Loading and using this widget may take a long time on some large filter sets. If it takes too long, please close it and further narrow your results using other facets or the text search.<br />Waiting...',
// 'spinner_search_source' : sd.image_base + '/waiting_ajax.gif'
// };
// var refman = new widget.search_pane(gserv, gconf,
// 'display-associations',
// refman_args);
// Set the manager profile.
refman.set_personality('annotation'); // profile in gconf
refman.include_highlighting(true);
// Two sticky filters.
refman.add_query_filter('document_category', 'annotation', ['*']);
refman.add_query_filter('reference', global_acc, ['*']);
// Experiment.
// Process incoming queries, pins, and filters (into
// manager)--the RESTy bookmarking API.
if( global_live_search_query ){ //has incoming query
ll("Try and use incoming query (set default): " +
global_live_search_query);
refman.set_comfy_query(global_live_search_query);
}
if( us.isArray(global_live_search_filters) ){ //has incoming filters
us.each(global_live_search_filters, function(filter){
refman.add_query_filter_as_string(filter, ['$']);
});
}
if( us.isArray(global_live_search_pins) ){ //has incoming pins
us.each(global_live_search_pins, function(pin){
refman.add_query_filter_as_string(pin, ['*']);
});
}
///
/// Major widget attachements to the manager.
///
// Attach filters to manager.
var hargs = {
meta_label: 'Total annotations: ',
// free_text_placeholder:
// 'Input text to filter against all remaining documents',
'display_free_text_p': false
};
var filters = new widgets.live_filters('accordion', refman, gconf, hargs);
filters.establish_display();
// Attach pager to manager.
var pager_opts = {
results_title: 'Total annotations: ',
};
var pager = new widgets.live_pager('pager', refman, pager_opts);
// Attach the results pane and download buttons to manager.
var default_fields = confc.field_order_by_weight('result');
var btmpl = widgets.display.button_templates;
var flex_download_button = btmpl.flexible_download_b3(
'Download <small>(up to ' + dlimit + ')</small>',
dlimit,
default_fields,
'annotation',
gconf,
gserv_download);
var results_opts = {
//'callback_priority': -200,
'user_buttons_div_id': pager.button_span_id(),
'user_buttons': [
flex_download_button
]
};
var results = new widgets.live_results('results', refman, confc,
handler, linker, results_opts);
// Add pre and post run spinner (borrow filter's for now).
refman.register('prerun', function(){
filters.spin_up();
});
refman.register('postrun', function(){
filters.spin_down();
});
refman.search();
///
/// Create a bookmark for searching annotations and
/// bioentities with this term. Generate links and activate
/// hidden stubs in the doc.
///
jQuery('#prob_related').removeClass('hidden');
// Get bookmark for annotations.
(function(){
// Ready bookmark.
var engine = new jquery_engine(golr_response);
engine.method('GET');
engine.use_jsonp(true);
var man = new golr_manager(gserv, gconf, engine, 'async');
man.set_personality('annotation');
man.add_query_filter('document_category', 'annotation', ['*']);
man.add_query_filter('reference', global_acc);
var lstate = man.get_filter_query_string();
var lurl = linker.url(lstate, 'search', 'annotation');
// Add it to the DOM.
jQuery('#prob_ann_href').attr('href', lurl);
jQuery('#prob_ann').removeClass('hidden');
})();
// Get bookmark for annotation download.
(function(){
// Ready bookmark.
var engine = new jquery_engine(golr_response);
engine.method('GET');
engine.use_jsonp(true);
var man = new golr_manager(gserv, gconf, engine, 'async');
man.set_personality('annotation');
man.add_query_filter('document_category', 'annotation', ['*']);
man.add_query_filter('reference', global_acc);
var dstate = man.get_download_url(defs.gaf_from_golr_fields, {
'rows': dlimit,
'encapsulator': '',
'golr_download_url': gserv_download
});
jQuery('#prob_ann_dl_href').attr('href', dstate);
jQuery('#prob_ann_dl').removeClass('hidden');
})();
//
ll('ReferenceDetailsInit done.');
}
// Embed the jQuery setup runner.
(function (){
jQuery(document).ready(function(){
if( ! global_acc ){
UnableToMakeContact('Response error.');
}else if( ! bbop.first_split(':', global_acc) ){
UnableToMakeContact('Response error.');
}else if( ! bbop.first_split(':', global_acc)[1] ){
UnableToMakeContact('Response error.');
}else if( bbop.first_split(':', global_acc)[1] === '' ){
UnableToMakeContact('Response error.');
}else{
var id_part = bbop.first_split(':', global_acc)[1];
// First, try and get the PubMed information for our item.
try {
// Setup.
var ncbi_engine = new jquery_engine(rest_response);
ncbi_engine.method('GET');
// They use CORS?
//ncbi_engine.use_jsonp(true);
// If error.
ncbi_engine.register('error', function(resp, man){
//console.log(resp.message());
//console.log(resp);
UnableToMakeContact('Response error--it\'s <b>possible</b> that you\'re experiencing <b>network issues</b>. <b>Please try again</b>.');
});
// Run.
ncbi_engine.register('success', function(resp, man){
var xml = resp.raw();
//console.log(xml);
var jx = null;
if( typeof xml === 'string' ){
jx = jxon.stringToJs(xml);
}else{
jx = jxon.xmlToJs(xml);
}
console.log(jx);
// Our operating object.
if( ! jx.PubmedArticleSet.PubmedArticle ){
UnableToMakeContact('<b>No such PubMed ID</b>.');
}else if( ! jx.PubmedArticleSet.PubmedArticle.MedlineCitation.Article ){
UnableToMakeContact('<b>No such Article</b>.');
}else{
var op = jx.PubmedArticleSet.PubmedArticle.MedlineCitation.Article;
// Title.
var title = 'n/a';
if( op.ArticleTitle ){
title = op.ArticleTitle;
}
// Date.
var date = 'n/a';
if( op.ArticleDate ){
var year = op.ArticleDate.Year || '???';
var month = op.ArticleDate.Month || '???';
var day = op.ArticleDate.Day || '???';
date = [year, month, day].join('-');
}
// Authors.
var authors = 'n/a';
if( op.AuthorList && op.AuthorList.Author ){
var acache = [];
us.each(op.AuthorList.Author, function(auth){
var name = '';
if( auth.ForeName ){ name += auth.ForeName; }
//if( auth.Initials ){ name+=' '+auth.Initials+'.'; }
if( auth.LastName ){ name +=' '+auth.LastName; }
acache.push(name);
});
if( ! us.isEmpty(acache) ){
authors = acache.join(', ');
}
}
// Abstract; list or string.
var abstract = 'n/a';
if( op.Abstract && op.Abstract.AbstractText ){
if( us.isArray(op.Abstract.AbstractText) ){
var abscache = [];
us.each(op.Abstract.AbstractText, function(abs){
if( abs._ ){ abscache.push(abs._); }
});
if( ! us.isEmpty(abscache) ){
abstract = abscache.join('<br />');
}
}else if( us.isString(op.Abstract.AbstractText) ){
abstract = op.Abstract.AbstractText;
}
}
// Render.
jQuery('#info-area').empty();
var info = [
'<dl class="dl-horizontal amigo-detail-info">',
// Title.
'<dt>Title</dt>',
'<dd>',
title,
'</dd>',
// Date.
'<dt>Date</dt>',
'<dd>',
date,
'</dd>',
// Author.
'<dt>Author(s)</dt>',
'<dd>',
authors,
'</dd>',
// Abstract.
'<dt>Abstract</dt>',
'<dd id="abstract">',
abstract,
'</dd>',
// // Related.
// '<dt id="prob_related" class="hidden">Related</dt>',
// '<dd id="prob_ann" class="hidden">',
// '<a id="prob_ann_href" href="#" class="btn btn-primary btn-xs">Link</a> to all direct and indirect <strong>annotations</strong> to ' + global_acc + '.',
// '</dd>',
// '<dd id="prob_ann_dl" class="hidden">',
// '<a id="prob_ann_dl_href" href="#" class="btn btn-primary btn-xs">Link</a> to all direct and indirect <strong>annotations download</strong> (limited to first 10,000) for ' + global_acc + '.',
// '</dd>',
// Pubmed.
'<dt>PubMed</dt>',
'<dd>',
'<a href="https://www.ncbi.nlm.nih.gov/pubmed/' + bbop.first_split(':', global_acc)[1] + '">' + global_acc + '</a>',
'</dd>',
// // Feedback.
// '<dt>Feedback</dt>',
// '<dd>',
// 'Contact the <a href="http://geneontology.org/form/contact-go" title="GO Helpdesk.">GO Helpdesk</a> if you find mistakes or have concerns about the data you find here.',
// '</dd>',
'</dl>'
];
jQuery('#info-area').append(info.join(' '));
_shrink_wrap('abstract');
// Powered by NCBI.
jQuery('#info-area').append([
'<p>',
'Powered by NCBI\'s <a href="https://eutils.ncbi.nlm.nih.gov">E-utilities</a>.',
'<br />',
'Please read NCBI\'s <a href="https://www.ncbi.nlm.nih.gov/home/about/policies.shtml">Disclaimer and Copyright notice.',
'</p>',
].join(' '));
// Get feedback from viewers.
jQuery('#info-area').append([
'<p>',
'<span class="glyphicon glyphicon-pencil"></span><b>Are you the author</b> (or have a deep understanding) of this paper? If so, <b>do these annotations look okay to you</b>? <a href="http://geneontology.org/form/contact-go">Let us know</a>!',
'</p>'
].join(' '));
// Start the normal GOlr services.
ReferenceDetailsInit();
}
});
// Trigger.
var url = 'https://eutils.ncbi.nlm.nih.gov';
var path = '/entrez/eutils/efetch.fcgi';
var pay = {
'tool': 'amigo_client_2.4.x',
'email': ['sjc', 'arb', 'on@', 'lbl', '.go', 'v'].join(''),
'retmode': 'XML',
'db': 'pubmed',
'id': id_part
};
var meth = 'GET';
ncbi_engine.start(url + path, pay, 'GET');
}catch (e) {
UnableToMakeContact('General error.');
}
}
});
})();
// Take and element, look at it's contents, if it's above a certain
// threshold, shrink with "more..." button, otherwise leave alone.
function _shrink_wrap(elt_id){
// Now take what we have, and wrap around some expansion code if
// it looks like it is too long.
var _trim_hash = {};
var _trimit = 100;
// Only want to compile once.
var ea_regexp = new RegExp("\<\/a\>", "i"); // detect an <a>
var br_regexp = new RegExp("\<br\ \/\>", "i"); // detect a <br />
function _trim_and_store( in_str ){
var retval = in_str;
// Let there be tests.
var list_p = br_regexp.test(retval);
var anchors_p = ea_regexp.test(retval);
// Try and break without breaking anchors, etc.
var tease = null;
if( ! anchors_p && ! list_p ){
// A normal string then...trim it!
//ll("\tT&S: easy normal text, go nuts!");
tease = new html.span(bbop.crop(retval, _trimit, '...'),
{'generate_id': true});
}else if( anchors_p && ! list_p ){
// It looks like it is a link without a break, so not
// a list. We cannot trim this safely.
//ll("\tT&S: single link so cannot work on!");
}else{
//ll("\tT&S: we have a list to deal with");
var new_str_list = retval.split(br_regexp);
if( new_str_list.length <= 3 ){
// Let's just ignore lists that are only three
// items.
//ll("\tT&S: pass thru list length <= 3");
}else{
//ll("\tT&S: contruct into 2 plus tag");
var new_str = '';
new_str = new_str + new_str_list.shift();
new_str = new_str + '<br />';
new_str = new_str + new_str_list.shift();
tease = new html.span(new_str, {'generate_id': true});
}
}
// If we have a tease (able to break down incoming string),
// assemble the rest of the packet to create the UI.
if( tease ){
// Setup the text for tease and full versions.
var bgen = function(lbl, dsc){
var b = new html.button(lbl, {
'generate_id': true,
'type': 'button',
'title': dsc || lbl,
//'class': 'btn btn-default btn-xs'
'class': 'btn btn-primary btn-xs'
});
return b;
};
var more_b = new bgen('more', 'Display the complete list');
var full = new html.span(retval, {'generate_id': true});
var less_b = new bgen('less', 'Display the truncated list');
// Store the different parts for later activation.
var tease_id = tease.get_id();
var more_b_id = more_b.get_id();
var full_id = full.get_id();
var less_b_id = less_b.get_id();
_trim_hash[tease_id] = [tease_id, more_b_id, full_id, less_b_id];
// New final string.
retval = tease.to_string() + " " +
more_b.to_string() + " " +
full.to_string() + " " +
less_b.to_string();
}
return retval;
}
var pre_html = jQuery('#' + elt_id).html();
if( pre_html && pre_html.length && (pre_html.length > _trimit * 2) ){
// Get the new value into the wild.
var new_str = _trim_and_store(pre_html);
if( new_str !== pre_html ){
jQuery('#' + elt_id).html(new_str);
// Bind the jQuery events to it.
// Add the roll-up/down events to the doc.
us.each(_trim_hash, function(val, key){
var tease_id = val[0];
var more_b_id = val[1];
var full_id = val[2];
var less_b_id = val[3];
// Initial state.
jQuery('#' + full_id ).hide();
jQuery('#' + less_b_id ).hide();
// Click actions to go back and forth.
jQuery('#' + more_b_id ).click(function(){
jQuery('#' + tease_id ).hide();
jQuery('#' + more_b_id ).hide();
jQuery('#' + full_id ).show('fast');
jQuery('#' + less_b_id ).show('fast');
});
jQuery('#' + less_b_id ).click(function(){
jQuery('#' + full_id ).hide();
jQuery('#' + less_b_id ).hide();
jQuery('#' + tease_id ).show('fast');
jQuery('#' + more_b_id ).show('fast');
});
});
}
}
}