forked from geneontology/noctua
-
Notifications
You must be signed in to change notification settings - Fork 1
/
epione.js
569 lines (482 loc) · 13.7 KB
/
epione.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
////
//// Greek Goddess of soothing pain.
////
//// Optional companion file monitor for Minerva. Should be turned on
//// before Minerva is started to make sure they're on the same page.
////
//// Requires custom document type for GOlr: "noctua_model_meta". Git
//// operations are not going to be initially implemented, in parens.
////
//// - On initial start
//// - wipes document type "noctua_model_meta"
//// - scans and reads all files
//// - adds all files to GOlr
//// (- if there have been any uncommited changes, commit and push)
//// - Watches for file alterations
//// - On update/creation
//// - re-read document (scan single doc)
//// - Solr clobber (or is it delete and re-add?)
//// (- if add: commit add to git)
//// (- if change: commit change to git)
//// (- push commit)
//// - On deletion
//// - remove that ID from GOlr (model_id + document_category)
//// (- commit deletion to git)
//// (- push commit)
//// - Periodic
//// - ?Send optimize command to Solr?
////
//// Requires custom document type for GOlr.
//// amigo/metadata/noctua-model-meta-config.yaml
////
//// Usage: node epione.js --monitor A/B/ --golr http://localhost:8080/solr --users C/D
////
//// Note that the currently used FAM is a little slow, taking on the
//// order of seconds.
////
var document_category = 'noctua_model_meta';
var golr_location = null;
// Std utils.
var fs = require('fs');
var us = require('underscore');
var ustr = require('underscore.string');
//var Q = require('q');
var yaml = require('yamljs');
var url = require('url');
// TODO: External interaction.
//var watch = require('watch');
//var git = require('git');
var solr = require('solr-client');
var watch = require('watch');
//var N3 = require('n3');
//var parser = N3.Parser();
var wait = require('wait.for');
// CLI handling.
var argv = require('minimist')(process.argv.slice(2));
//console.dir(argv);
///
/// Helpers and aliases.
///
var each = us.each;
var quote_re = /\\\"/gi;
function ll(arg1){
console.log('epione [' + (new Date()).toJSON() + ']: ', arg1);
}
function _die(message){
console.error('EPIONE [' + (new Date()).toJSON() + ']: ' + message);
process.exit(-1);
}
// Until we get a parser, we just want to compile at startup.
var matcher = {
/* jshint ignore:start */
ontology: /Ontology\: \<([^]*)\>/,
title: /\<http\:\/\/purl\.org\/dc\/elements\/1\.1\/title\>\ \"([^]*)\"\^\^xsd\:string/,
date: /\<http\:\/\/purl\.org\/dc\/elements\/1\.1\/date\>\ \"([^]*)\"\^\^xsd\:string/,
contributor: /\<http\:\/\/purl\.org\/dc\/elements\/1\.1\/contributor\>\ \"([^]*)\"\^\^xsd\:string/,
state: /\<http\:\/\/geneontology\.org\/lego\/modelstate\>\ \"([^]*)\"\^\^xsd\:string/,
comment: /rdfs\:comment\ \"([^]*)\"\^\^xsd\:string/,
owl_blob_json: /\<http\:\/\/geneontology\.org\/lego\/json\-model\>\ \"([^]*)\"\^\^xsd\:string/
/* jshint ignore:end */
};
var match_types = us.keys(matcher);
function _pull_line(str){
var ret = null;
if( str ){
// Want to break quickly, so for loop and break.
for( var i = 0; i < match_types.length; i++ ){
var type = match_types[i];
var regexp = matcher[type];
var match = str.match(regexp);
if( ! match ){
//
}else{
ret = {};
ret[type] = match[1];
break;
}
}
}
return ret;
}
// File reading/parsing.
// TODO; is there a turtle parser? Do this by hand?
// TODO: async code here?
function _filename_to_json(fname){
var ret = {};
// TODO.
var buf = fs.readFileSync(fname);
if( buf ){
var str = buf.toString();
var lines = str.split(/\n/);
var cache = {};
each(lines, function(line){
var pulled = _pull_line(line);
if( pulled ){
each(pulled, function(val, key){
if( typeof(cache[key]) === 'undefined' ){
cache[key] = {};
}
cache[key][val] = true;
});
}
});
each(cache, function(set, key){
ret[key] = us.keys(set);
});
// Check that we have an ID.
if( ret['ontology'] && ret['ontology'].length !== 1 ){
ret = null;
}
// Grrr--only Manchester seems to no exist for JS:
// http://www.w3.org/community/rdfjs/wiki/Comparison_of_RDFJS_libraries#Parsing_libraries
// // TODO: PARSING
// parser.parse(str, function(error, triple, prefixes){
// if( triple ){
// ll(triple.subject, triple.predicate, triple.object, '.');
// }else{
// ll("# That's all, folks!", prefixes);
// }
// });
}
return ret;
}
// Return whether or not the file likely seems like a model.
//var check_regexp = /\d{16}$/g;
var model_regexp = /\/([0-9a-f]{16})$/;
function _file_okay_p(fname){
var ret = false;
if( model_regexp.test(fname) ){
ret = true;
}
return ret;
}
// Return a list of all files in a given dir.
// TODO: Recursive?
function _all_files(dir){
var ret = [];
ret = fs.readdirSync(dir);
return ret;
}
//
function _file_to_payload(fname){
var deliverable = null;
var blob = _filename_to_json(fname);
if( ! blob || us.isEmpty(blob) ){
ll('Could NOT process: ' + fname);
}else{
ll('Read and processed: ' + fname);
// Reuse contributor in lookup if possible.
var ckeys = blob['contributor'];
var clabels = [];
each(ckeys, function(ckey){
if( keyed_users[ckey] ){
clabels.push(keyed_users[ckey]);
}
});
// Model date should be last date.
var model_date = null;
var all_dates = blob['date'];
if( ! us.isEmpty(all_dates) ){
//model_date = all_dates.sort()[0]; // whoops - earliest
model_date = all_dates.sort()[all_dates.length -1];
}
var comment = null;
if( blob['comment'] ){
comment = blob['comment'].join(' / ');
}
// Just the one, hopefully.
var owl_blob_json = null;
if( blob['owl_blob_json'] && blob['owl_blob_json'][0] ){
//ll( blob['owl_blob_json'][0] );
// Get rid of extra escaping that slips in.
//owl_blob_json = blob['owl_blob_json'][0];
owl_blob_json = blob['owl_blob_json'][0].replace(quote_re, '"');
//ll( owl_blob_json );
}
var title = '???';
if( blob['title'] ){
title = blob['title'].join(', ');
}
var state = '???';
if( blob['state'] ){
state = blob['state'].join(', ');
}
var mid = blob['ontology'][0];
// Transform to final Solr model.
deliverable = {
id: document_category + '_' + mid,
document_category: document_category,
annotation_unit: mid,
//?//annotation_unit_searchable: mid,
annotation_unit_label: title,
annotation_unit_label_searchable: title,
contributor: clabels,
contributor_searchable: clabels,
model_date: model_date,
model_date_searchable: model_date,
model_state: state,
//?//model_state_searchable: state,
// TODO: Do not load comments until upstream is fixed:
// See ticket: https://github.com/geneontology/noctua/issues/182
comment: comment,
comment_searchable: comment,
// Models.
owl_blob_json: owl_blob_json
};
//ll(deliverable);
return deliverable;
}
}
// Add/clobber a document into GOlr.
// true on success, false otherwise
function _golr_add_and_commit_docs(payloads, success_callback){
// Add everything we have back in.
solr_client.add(payloads, function(err, obj){
if(err){
_die('Could not do adds to server: ' + err);
}else{
//
solr_client.commit(function(err, obj){
if(err){
_die('Could not commit adds to GOlr server: ' +
err);
}else{
ll('All docs added; completed spin-up.');
// Run callback if there.
if( success_callback ){
success_callback(payloads);
}
}
});
}
});
}
//
function _golr_add_by_filename(fname, success_callback){
var payload = _file_to_payload(fname);
// Add everything we have back in.
solr_client.add(payload, function(err, obj){
if(err){
_die('Could not add ' + fname + ' to server: ' + err);
}else{
//
solr_client.commit(function(err, obj){
if(err){
_die('Could not commit add to GOlr server: ' + err);
}else{
ll('Completed spin-up.');
// Run callback if there.
if( success_callback ){
success_callback(fname);
}
}
});
}
});
}
function _golr_delete_by_filename(fname, success_callback){
// Recreate ID.
var mid = null;
var match = fname.match(model_regexp);
if( ! match ){
_die('Unable to reconstruct model ID.');
}else{
mid = match[1];
}
var full_id = document_category + '_gomodel:' + fname;
//
solr_client.deleteByID(full_id, function(err, obj){
if(err){
_die('Could not delete doc on GOlr server: ' + err);
}else{
//ll(obj);
solr_client.commit(function(err, obj){
if(err){
_die('Could not commit delete on GOlr server: ' + err);
}else{
ll('Deleted gomodel:' + full_id + ' from GOlr server.');
// Run callback if there.
if( success_callback ){
success_callback(fname);
}
}
});
}
});
}
//
function _watchers(){
// function _on_change(event, fname){
// // event
// if( event === 'rename' ){ // is a create/rename event
// // For renames events, we only care about deletion since
// // the creation event will also throw a change.
// if( ! fs.existsSync(fname) ){
// ll('deleted: ' + fname);
// }
// }else{ // change event
// ll('delete and re-add: ' + fname);
// }
// }
// // TODO:
// ll('Starting watchers...');
// var watch_opt = {
// persistent: true,
// recursive: false
// };
// fs.watch(monitor_dir, {}, _on_change);
// watch.watchTree(monitor_dir, function (f, curr, prev) {
// if (typeof f === "object" && prev === null && curr === null) {
// ll('Watcher is watching...');
// } else if (prev === null) {
// // f is a new file
// ll('new: ' + f);
// } else if (curr.nlink === 0) {
// // f was removed
// if( ! fs.existsSync(f) ){
// ll('deleted: ' + f);
// //ll('deleted: ' + fname);
// }
// } else {
// // f was changed
// ll('changed: ' + f);
// }
// });
//
watch.createMonitor(monitor_dir, function (monitor){
monitor.on("created", function (f, stat){
//ll('new: ' + f);
if( ! _file_okay_p(f) ){
ll('Skipping questionable file (creation): ' + f);
}else{
_golr_add_by_filename(f);
}
});
monitor.on("changed", function (f, curr, prev){
//ll('changed: ' + f);
if( ! _file_okay_p(f) ){
ll('Skipping questionable file (changed): ' + f);
}else{
_golr_delete_by_filename(f, _golr_add_by_filename);
}
});
monitor.on("removed", function (f, stat){
//ll('deleted: ' + f);
if( ! _file_okay_p(f) ){
ll('Skipping questionable file (removal): ' + f);
}else{
_golr_delete_by_filename(f);
}
});
});
ll('Watchers probably standing watch.');
}
///
/// CLI handling, environment setup, and initialization of clients.
///
// What directory will we monitor/operate on.
var monitor_dir = argv['m'] || argv['monitor'];
if( ! monitor_dir ){
_die('Option (m|monitor) is required.');
// Make sure extant, etc.
var dstats = fs.statSync(monitor_dir);
if( ! dstats.isDirectory() ){
_die('Option (m|monitor) is not a directory: ' + monitor_dir);
}
}else{
ll('Will monitor: ' + monitor_dir);
}
// What is our GOlr target.
golr_location = argv['g'] || argv['golr'];
if( ! golr_location ){
_die('Option (g|golr) is required.');
// TODO: make sure extant, etc.
}else{
ll('Will operate on GOlr instance at: ' + golr_location);
}
// Pull in and keyify the contributor information.
var user_fname = argv['u'] || argv['users'];
if( ! user_fname ){
_die('Option (u|users) is required.');
// Make sure extant, etc.
var fstats = fs.statSync(user_fname);
if( ! fstats.isFile() ){
_die('Option (u|users) is not a file: ' + user_fname);
}
}else{
ll('Will pull user info from: ' + user_fname);
}
var keyed_users = {};
var user_entries = yaml.load(user_fname);
each(user_entries, function(entry){
var nick = entry['nickname'];
if( nick ){
var xref = entry['xref'];
var uri = entry['uri'];
if( xref ){ keyed_users[xref] = nick; }
if( uri ){ keyed_users[uri] = nick; }
}
});
///
/// Startup.
///
// Set the actual target.
var u = url.parse(golr_location);
var client_opts = {
solrVersion: '3.6',
host: u.hostname,
port: u.port,
path: ustr(u.path).rtrim('/').value()
};
//ll(client_opts);
var solr_client = solr.createClient(client_opts);
//var solr_client = solr.createClient(u.hostname, u.port, '', u.path);
// Not in our version?
// // Ping solr server. Only continue if.
// solr_client.ping(function(err, obj){
// if(err){
// _die('Could not make contact with GOlr server!');
// }else{
// ll(obj);
// }
// });
///
/// Start by flushing the current contents.
///
var flush_query = 'document_category:' + document_category;
//solr_client.deleteByQuery(flush_query, function(err, obj){
//var myObjData = wait.forMethod(
solr_client.deleteByQuery(flush_query, function(err, obj){
if(err){
_die('Could not flush GOlr server: ' + err);
}else{
//ll(obj);
solr_client.commit(function(err, obj){
if(err){
_die('Could not commit flush of GOlr server: ' + err);
}else{
//ll('2');
//ll(obj);
///
/// Do inital adds.
///
// Gather all of the payloads before committing.
var payloads = [];
var all_file_names = _all_files(monitor_dir);
each(all_file_names, function(fname){
var filename = monitor_dir + fname;
if( ! _file_okay_p(filename) ){
ll('Skipping questionable file: ' + filename);
}else{
var payload = _file_to_payload(filename);
if( payload ){
payloads.push(payload);
}
}
});
// Add and commit everything, then start the eatchers.
_golr_add_and_commit_docs(payloads, _watchers);
}
});
}
});