-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathwayProcessor.bp3.js
1334 lines (1235 loc) · 41.7 KB
/
pathwayProcessor.bp3.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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* FUNCTIONS FOR ELEMENTARY BEHAVIORS
*/
var objectJoin = function(o, concat) {
var u = [];
Object.keys(o).forEach(function(I) {
u.push(I+'='+o[I]); });
return u.join(concat);
};
/**
* @author Sungyoung Lee
* @date 160623
* @remark BioPAX 3 compatible
* @brief Fetch 'canonical' name of BioPAX 3 entity,
* by searching <bp:standardName> first and then
* searching <bp:displayName> next if the former is not exists.
* @param[in] H_obj An {Element} object to get the name
* @return {null} if there is no available name
* @return {String} if the name has found
*/
pathwayProcessor.prototype.bp3getName = function(H_obj) {
/* Seek bp:standardName first */
var S_name = H_obj.getChildTextNS(this.bp, 'standardName');
if (!S_name)
S_name = H_obj.getChildTextNS(this.bp, 'displayName');
return !S_name ? null : S_name;
};
// xref ID -> db/id
var A_need2anno = {};
function dump_sub(_, e, lv, B_retString) {
if (undefined === B_retString) B_retString = false;
var F_1 = function(v){if(v.length!=1)throw new Exception("INCOMPATIBLE");};
var F_text = function(v){F_1(v);return v[0].textContent;};
var F_textN = function(v){var t=[];v.forEach(function(I){t.push(I.textContent);});return t.join("|");};
var F_resource = function(v){F_1(v);return v[0].getAttributeNS(_.rdf, 'resource');};
var F_resourceN = function(v){var t=[];v.forEach(function(I){t.push(I.getAttributeNS(_.rdf, 'resource'));});return t.join("|");};
var F_xrefN = function(v){var t=[];v.forEach(function(I){
var r = I.getAttributeNS(_.rdf, 'resource');
t.push(dump_sub(_, _.A_bp3entry[r], 0, true));
//t.push(_.A_bp3entry[r].tagName+'['+r+']');
});return t.join("|");};
var F_xref = function(v){F_1(v);
return dump_sub(_, _.A_bp3entry[v[0].getAttributeNS(_.rdf, 'resource')], 0, true);
var r = v[0].getAttributeNS(_.rdf, 'resource');
return _.A_bp3entry[r].tagName+'['+r+']'; };
var A_tagInfo = {
'bp:PublicationXref': {
print: '[XREF] [PUBLICATION ]', expect: {'bp:db':F_text,'bp:id':F_text},
optional: {'bp:year':F_text,'bp:author':F_textN,'bp:source':F_text,'bp:title':F_text},
returnOnly: true, needAnnotate: true, },
'bp:Protein': {
print: '[FUNC] [PROTEIN ]', expect: {'bp:displayName':F_text,'bp:entityReference':F_resource},
optional: {'bp:xref':F_xrefN,'bp:comment':F_text}, },
'bp:Complex': {
print: '[FUNC] [COMPLEX ]', expect: {'bp:component':F_resourceN},
optional: {'bp:xref':F_xrefN}, },
'bp:SmallMolecule': {
print: '[FUNC] [SMALLMOLCULE]', expect: {'bp:displayName':F_text,'bp:entityReference':F_resource},
optional: {'bp:comment':F_text,'bp:xref':F_xrefN}, },
'bp:BiochemicalReaction': {
print: '[FUNC] [BIOCHEMREACT]', expect: {},
optional: {'bp:left':F_resourceN,'bp:right':F_resourceN}, },
'bp:SmallMoleculeReference': {
print: '[ REF] [SMALLMOLCULE]', expect: {'bp:xref':F_xref}, },
'bp:ProteinReference': {
print: '[ REF] [PROTEIN ]', expect: {'bp:organism':F_resource},
optional: {'bp:xref':F_xref}, },
'bp:RelationshipXref': {
print: '[XREF] [RELATIONSHIP]', expect: {'bp:db':F_text,'bp:id':F_text},
returnOnly: true, needAnnotate: true, },
'bp:UnificationXref': {
print: '[XREF] [UNIFICATION ]', expect: {'bp:db':F_text,'bp:id':F_text},
returnOnly: true, needAnnotate: true, },
'bp:Pathway': {
print: '[PWAY]', expect: {'bp:organism':F_text,'bp:pathwayComponent':F_xrefN,'bp:displayName':F_text,'bp:comment':F_textN}, },
'bp:Catalysis': {
print: '[FUNC] [CATALYSIS ]', expect: {'bp:controller':F_resource,'bp:controlled':F_resource}, },
};
if (undefined === lv) lv = 0;
var str = ' '.repeat(lv);
switch (e.tagName) {
case 'bp:Pathway':
case 'bp:BiochemicalReaction':
case 'bp:Protein':
case 'bp:Catalysis':
case 'bp:SmallMolecule':
case 'bp:Complex':
case 'bp:SmallMoleculeReference':
case 'bp:ProteinReference':
case 'bp:UnificationXref':
case 'bp:PublicationXref':
case 'bp:RelationshipXref':
var T = A_tagInfo[e.tagName];
// Make an array of child tag names
// Cache childs by their tag name
var tmp = [];
var childs = [];
for (var j=0 ; j<e.children.length ; j++) {
if (childs[e.children[j].tagName] === undefined) childs[e.children[j].tagName] = [];
childs[e.children[j].tagName].push(e.children[j]);
tmp.push(e.children[j].tagName);
}
// Remove optional childs from the collected array
if (T.optional !== undefined) {
var O = Object.keys(T.optional);
tmp = tmp.filter(function(I) { return O.indexOf(I) == -1; });
}
// Child composition must be same
if (Array.from(new Set(tmp)).sort().toString() != Object.keys(T.expect).sort().toString()) {
var ret = str+' --> [ERR] '+T.print+' ['+e.id+']';
if (B_retString) return ret;
console.log(ret);
// Otherwise just print its children
for (var j=0 ; j<e.children.length ; j++) dump_sub(_, e.children[j], lv+1);
} else {
// Information match, extract information
tmp = {};
Object.keys(T.expect).forEach(function(I) {
tmp[I.split(':').pop()] = T.expect[I](childs[I]);
});
// Extract 'optional' information if needed
if (T.optional !== undefined) Object.keys(T.optional).forEach(function(I) {
if (childs[I] && childs[I].length)
tmp[I.split(':').pop()] = T.optional[I](childs[I]);
});
var ret = str+T.print+' ['+e.id+'] -> ' + objectJoin(tmp, ',');
if (B_retString) return ret;
// Do not print if returnOnly
if (undefined === T.returnOnly || !T.returnOnly)
console.log(ret);
// Add this entry to A_need2anno if needAnnotate == true
if (undefined !== T.needAnnotate && T.needAnnotate)
A_need2anno[e.id] = tmp;
}
break;
default:
var ret = str+e.tagName+'['+e.id+']';
if (B_retString) return ret;
console.log(ret);
for (var j=0 ; j<e.children.length ; j++)
dump_sub(_, e.children[j], lv+1);
break;
}
}
pathwayProcessor.prototype.dump = function() {
var o = this.A_bp3entry;
for (var i in o) if (o.hasOwnProperty(i)) dump_sub(this, o[i]);
// Now I have all annotations to cover
// Get the annotation (test)
console.log("t");
}
/**
* @author Sungyoung Lee
* @date 160618
* @remark BioPAX 3 compatible
* @brief Fetch 'target' entries with the given NS and localName
* According to BioPAX 3 standard, it automatically
* follows/caches the {Element}s to search,
* by following rdf:resource or searching the childNodes
* @param[in] H_obj An {Element} object to get the name
* @param[in] S_ns A namespace to match
* @param[in] S_name A localName to match
* @return {Array} of {Element}s found
*/
pathwayProcessor.prototype.bp3getData = function(H_obj, S_ns, S_name) {
// Initialize A_bp3entry if not exists
if (undefined === this.A_bp3entry) this.A_bp3entry = {};
if (undefined === this.A_bp3tag) this.A_bp3tag = {};
/* Get child {Element}s by the given NS and localName */
var A_elem = H_obj.getChildsByTagNameNS(S_ns, S_name);
/* For each child, invoke the sub-search function and vectorize the returns */
var A_ret = [];
for (var i=0 ; i<A_elem.length ; i++)
this.bp3getDatum(A_elem[i]).forEach(function(v) { A_ret.push(v); });
/* OK */
return A_ret;
};
pathwayProcessor.prototype.bp3getDatum = function(H_obj, S_tagName) {
// Have resource?
var tgt = [];
var res = H_obj.getAttributeNS(this.rdf, 'resource');
if (res) {
/* Have resource = returns an entity */
var _id = res.charAt(0) == '#' ? res.substring(1) : res;
var _tag = _id.replace(/[0-9]+/, "");
/* Cached? */
if (undefined === this.A_bp3entry[_id]) this.bp3cacheElementsByTagNames([_tag]);
/* Not cached yet? then it does not exists */
if (undefined === this.A_bp3entry[_id]) {
Lerror('Entity ['+_id+'] does not exists in the file');
return [];
}
/* Matches tagName? */
var tmp = this.A_bp3entry[_id];
if (undefined !== S_tagName && tmp.tagName != S_tagName) {
Lerror('Entity ['+_id+'] expected ['+S_tagName+'] but is ['+tmp.tagName+']');
return [];
}
/* OK */
tgt.push(tmp);
} else {
/* In this case, the entity/entities is/are directly defined below the tag */
this.bp3cacheElementsByChilds(H_obj);
for (var i=0 ; i<H_obj.childNodes.length ; i++) {
var I = H_obj.childNodes[i];
if (undefined === I.tagName || !I.tagName) continue;
if (I.namespaceURI != this.bp) continue;
var _id = I.getAttributeNS(this.rdf, 'ID');
/* Not cached yet? then it does not exists */
if (undefined === this.A_bp3entry[_id]) {
Lerror('SYSERR: Entity ['+_id+'] was not cached');
continue;
}
/* Matches tagName? */
var tmp = this.A_bp3entry[_id];
if (undefined !== S_tagName && tmp.tagName != S_tagName) continue;
/* OK */
tgt.push(tmp);
}
}
return tgt;
};
/**
* @author Sungyoung Lee
* @date 160619
* @remark BioPAX 3 compatible
* @remark the identifier that was not cached will not converted
* @brief Converts array/object of the identifiers into the cached object
* @param[in/out] {Argument} Mixture of array/object contains "id"s to convert into Element
* @return Nothing but the "arguments" will be converted internally
*/
pathwayProcessor.prototype.bp3convertID2element = function() {
var _ = this;
for (var i=0 ; i<arguments.length ; i++) {
var cur = arguments[i];
if (cur instanceof Array) for (var j=0 ; j<cur.length ; j++)
if (typeof cur[j] == 'string') {
var id = cur[j].charAt(0) == '#' ? cur[j].substring(1) : cur[j];
/* Allocate object if cached */
if (undefined !== _.A_bp3entry[id])
cur[j] = _.A_bp3entry[id];
else {
var tmp = _.bp3interpretNonElemAnno(id);
if (tmp === undefined)
Lwarn("["+id+"] has failed to convert");
cur[j] = tmp;
}
}
else if (cur instanceof Object) for (var j in cur) {
if (cur.hasOwnProperty(j) && typeof cur[j] == 'string') {
var id = cur[j].charAt(0) == '#' ? cur[j].substring(1) : cur[j];
/* Allocate object if cached */
if (undefined !== _.A_bp3entry[id])
cur[j] = _.A_bp3entry[id];
else {
var tmp = _.bp3interpretNonElemAnno(id);
if (tmp === undefined)
Lwarn("["+id+"] has failed to convert");
cur[j] = tmp;
}
}
}
}
}
/**
* @author Sungyoung Lee
* @date 171201
* @remark BioPAX 3 compatible
* @brief Interpret non-element type annotation (which is created by this script) into translatable form
* @param[in] {Argument} (Presumably) string which is an ID to interpret
* @return {Array} Interpretation result
*/
pathwayProcessor.prototype.bp3interpretNonElemAnno = function(id) {
if (id.indexOf(':') != -1) {
var db = id.split(':').shift();
var ret = id;
switch (db) {
case 'text':
break;
case 'PubMed':
break;
default:
Lwarn('['+db+'] annotation is not addressed');
break;
}
return ret;
} else return undefined;
}
/**
* @author Sungyoung Lee
* @date 160619
* @remark BioPAX 3 compatible
* @brief Extracts tags from the rdf:ID or rdf:about attributes
* @param[in] {Argument} Mixture of array/object contains rdf:ID or rdf:about
* @return {Array} Extracted tags
*/
pathwayProcessor.prototype.bp3extractTagsFromArray = function() {
var tags = {};
var F_proc = function(i) {
if (i.charAt(0) == '#') i = i.substring(1);
tags[i.replace(/[0-9]+/, "")] = true;
};
for (var i=0 ; i<arguments.length ; i++) {
var A_obj = arguments[i];
if (A_obj instanceof Array) A_obj.forEach(F_proc);
else for (var i in A_obj) if (A_obj.hasOwnProperty(i)) F_proc(i);
}
return Object.keys(tags);
};
/**
* BioPAX 3 compatible
* Get all elements by tagname and namespace
* @argument {Variable} Tag list qualified by BP namespace
* @return Array of Element objects whose tag matches to the input
*/
pathwayProcessor.prototype.bp3getElementsByTagNames = function() {
var A_ret = [];
var A_tag = [];
for (var i=0 ; i<arguments.length ; i++) A_tag.push(arguments[i]);
/* Cache if not cached */
this.bp3cacheElementsByTagNames(A_tag);
/* Return them */
for (var i=0 ; i<arguments.length ; i++) {
if (undefined === this.A_bp3tag[arguments[i]]) {
Lcmt('Tag ['+arguments[i]+'] has not cached, maybe not exists?');
for (var j in this.A_bp3tag) if (this.A_bp3tag.hasOwnProperty(j))
Lcmt(' ['+j+'] cached');
continue;
}
this.A_bp3tag[arguments[i]].forEach(function(v) { A_ret.push(v); });
}
return A_ret;
};
/**
* BioPAX 3 compatible
* Cache XML elements by tag names
* @argument {Array} A_tag A list of tags, "bp" namespace is assumed
* @return Nothing but this.A_bp3entry caches the information
*/
pathwayProcessor.prototype.bp3cacheElementsByTagNames = function(A_tag) {
var _ = this;
var xml = this.xml();
var N_anno = 0;
var F_procTag = function(v) {
// Do nothing if already cached
if (undefined !== _.A_bp3tag[v] ||
_.B_bp3allChecked && v != "XXXXXX") return;
_.A_bp3tag[v] = [];
// Get elements and store them by their rdf:ID
var A_get = xml.getElementsByTagNameNS(_.bp, v);
if (A_get.length == 0) {
_.B_bp3allChecked = true;
A_get = xml.getElementsByTagNameNS(_.bp, "*");
}
N_anno = 0;
for (var j=0 ; j<A_get.length ; j++) {
var _i = A_get[j].getAttributeNS(_.rdf, 'ID');
if (!_i) {
_i = A_get[j].getAttributeNS(_.rdf, 'about');
if (!_i) {
if (_.B_bp3allChecked) continue;
Lwarn('['+j+'] th element of tag ['+v+'] has neither rdf:ID or rdf:about');
continue;
}
}
A_get[j].id = _i;
N_anno++;
if (_.B_bp3allChecked) {
if (undefined === _.A_bp3tag[A_get[j].localName]) _.A_bp3tag[A_get[j].localName] = [];
_.A_bp3tag[A_get[j].localName].push(A_get[j]);
} else
_.A_bp3tag[v].push(A_get[j]);
_.A_bp3entry[_i] = A_get[j];
}
Lcmt('['+N_anno+'/'+A_get.length+'] elements cached/found by tag ['+(_.B_bp3allChecked?'ALL':v)+']');
};
// Initialize A_bp3entry if not exists
if (undefined === _.A_bp3entry) _.A_bp3entry = {};
if (undefined === _.A_bp3tag) _.A_bp3tag = {};
// For each tag
if (A_tag instanceof Array) A_tag.forEach(F_procTag);
else for (var v in A_tag) if (A_tag.hasOwnProperty(v))
F_procTag(v);
if (N_anno == 0 && A_tag[0] != 'XXXXXX') this.bp3cacheElementsByTagNames(["XXXXXX"]);
};
pathwayProcessor.prototype.bp3cacheElementsByChilds = function(H_obj) {
var xml = this.xml();
// Gather tags
var A_tag = {};
for (var i=0 ; i<H_obj.childNodes.length ; i++) {
var I = H_obj.childNodes[i];
if (undefined === I.tagName || !I.tagName) continue;
if (I.namespaceURI != this.bp) continue;
A_tag[I.localName] = true;
}
this.bp3cacheElementsByTagNames(A_tag);
};
/**
* BioPAX 3 compatible
* Grab all rdf:resource for given tags
*/
pathwayProcessor.prototype.bp3getResourcesByElement = function(A_elem) {
var ret = {};
for (var i=0 ; i<A_elem.length ; i++) {
var res = A_elem[i].getAttributeNS(this.rdf, 'resource');
if (res)
// Remove #
ret[res.substring(1)] = true;
}
return ret;
};
/*
* FUNCTIONS FOR BASIC BioPAX 3 PROCESSORS
*/
/**
* @author Sungyoung Lee
* @date 160621
* @remark BioPAX 3 compatible
* @brief Get the title of pathway according to the BioPAX 3 standard.
* @param[in] H_xml A {document} object of XML
* @return {String} the fetched title
*/
pathwayProcessor.prototype.bp3getTitle = function(H_xml) {
var H_pwInfo = H_xml.getElementsByTagNameNS(this.bp, 'Pathway')[0];
var S_name = this.bp3getName(H_pwInfo);
if (S_name === null) {
Lwarn("BIOPAX3 : Pathway have no name entry");
return "";
} else return S_name;
};
/**
* @author Sungyoung Lee
* @date 160619
* @remark BioPAX 3 compatible
* @remark Supports non-standard directions contain underline(_) character
* @brief Determines and returns the 'direction' of reactions in the BioPAX 3
* @param[in] H_obj An {Element} object to get the direction located inside
* @param[in] S_name Child localName contains the direction, its NS should BP
* @return {undefined} if there is no direction information
* @return {bool} true if L->R, false if R->L
*/
pathwayProcessor.prototype.bp3procDirection = function(H_obj, S_name) {
var B_L2R = undefined;
var S_dir = H_obj.getChildTextNS(this.bp, S_name);
switch (S_dir) {
case 'LEFT_TO_RIGHT':
Lwarn('Non-standard direction ['+S_dir+'] found');
case 'LEFT-TO-RIGHT': B_L2R = true; break;
case 'RIGHT_TO_LEFT':
Lwarn('Non-standard direction ['+S_dir+'] found');
case 'RIGHT-TO-LEFT': B_L2R = false; break;
default: break;
}
Ldebug("Step ["+H_obj.getAttributeNS(this.rdf, 'ID')+"] direction ["+(B_L2R===undefined?'???':(B_L2R?'L->R':'R->L'))+"]");
return B_L2R;
};
pathwayProcessor.prototype.bp3annotate = function() {
/* Collect information */
var annot = {};
var res = {};
var N_anno = 0; /* Number of annotated {Element}s */
/* {Function} to collect entries to annotate in the given {Element} */
var F_procAnnoFind = function(w) {
var _w = w.split(':');
if (_w.length < 2) return;
_w[1] = _w.slice(1).join(':');
Ldebug('Annotation ['+_w[0]+':'+_w[1]+'] found');
if (undefined === annot[_w[0]]) annot[_w[0]] = [];
annot[_w[0]].push(_w[1]);
};
/* {Function} to remap the annotations onto the given {Element} */
var F_procAnnoRemap = function(v, w) {
var _w = w.split(':');
_w[1] = _w.slice(1).join(':');
if (undefined !== res[_w[0]] && undefined !== res[_w[0]][_w[1]]) {
if (undefined === v.name) v.name = [];
v.name.push(res[_w[0]][_w[1]]);
v.symbol = res[_w[0]][_w[1]];
N_anno++;
}
};
/* Find entries to annotate */
this.A_nodes.forEach(function(v) {
if (undefined !== v.gene) v.gene.forEach(F_procAnnoFind);
if (undefined !== v.compound) v.compound.forEach(F_procAnnoFind);
});
for (var i in annot) if (annot.hasOwnProperty(i)) {
var v = annot[i];
res[i] = {};
Lcmt('Annotating ['+i+']... ['+v.length+'] entries');
$.ajax({
type:'post',
url:this.A_infoAnno[i][0],
dataType:'json',
data:{
keys:v.join(this.A_infoAnno[i][1])
},
async:false,
success:function(a) {
for (var j in a) {
Ldebug(i+" ["+j+"] -> ["+a[j]+"]");
res[i][j] = a[j];
}
}
});
}
/* Remap the annotated entries to Element */
this.A_nodes.forEach(function(v) {
if (undefined !== v.gene) v.gene.forEach(function(w) { F_procAnnoRemap(v, w); });
if (undefined !== v.compound) v.compound.forEach(function(w) { F_procAnnoRemap(v, w); });
});
Lcmt('['+N_anno+'] entires annotated');
};
/**
* @author Sungyoung Lee
* @date 160622
* @remark BioPAX 3 compatible
* @brief Check the given Element has BP namespace and the given localName and assigns "id" if true
* @param[in/out] {Element} to check
* @return Nothing but H_obj.id will be appended if passed
*/
pathwayProcessor.prototype.bp3check = function(H_obj, S_tag) {
/* Namespace should be BP */
if (H_obj.namespaceURI != this.bp) return false;
/* Tag should match */
if (H_obj.localName != S_tag) return false;
/* Add id if not exists */
if (undefined === H_obj.id || !H_obj.id)
H_obj.id = H_obj.getAttributeNS(this.rdf, 'ID');
return true;
};
/**
* @author Sungyoung Lee
* @date 160624
* @remark BioPAX 3 compatible
* @brief A callback for Xref default processor
* If S_name contains whitespace, the first word will be
* considered for returning db
* @param[in] S_name {String} of Xref -> db
* @param[in] w {Element} of Xref
* @return {String} consists of db:id, db is the first word
*/
pathwayProcessor.prototype.bp3procXrefDefault = function(S_name, w) {
var db = S_name.split(' ').shift().toLowerCase();
return db+':'+w.getChildTextNS(this.bp, 'id');
};
/*
* FUNCTIONS FOR BP-NAMESPACE ELEMENT PROCESSOR
*/
pathwayProcessor.prototype.bp3procPathway = function(H_pw) {
var _ = this;
_.A_bp3edge = [];
/* Grab all <bp:pathwayOrder> */
var _po = this.bp3getData(H_pw, this.bp, 'pathwayOrder');
_po.forEach(function(v) {
switch (v.tagName) {
case 'bp:BiochemicalPathwayStep':
/* Check the tag have <bp:stepDirection> */
var B_L2R = _.bp3procDirection(v, 'stepDirection');
/* For all childs of <bp:stepProcess> */
var A_sp = _.bp3getData(v, _.bp, 'stepProcess');
if (A_sp.length == 0) {
var A_sc = _.bp3getData(v, _.bp, 'stepConversion');
if (A_sc.length == 0)
Lerror("No interpretable step found in ["+v.id+"]");
else {
A_sc.forEach(function(w) {
_.bp3procBiochemicalReaction(w, B_L2R);
});
Lcmt('BiochemicalPathwayStep ['+v.id+'] has ['+A_sc.length+'] stepConversion');
}
break;
}
Lcmt('BiochemicalPathwayStep ['+v.id+'] has ['+A_sp.length+'] stepProcess');
A_sp.forEach(function(v) {
_.bp3procCatalysis(v, B_L2R);
});
break;
default:
Lwarn('Uncontrolled pathwayOrder ['+v.tagName+']');
break;
}
});
/* If no pathwayOrder, do pathwayComponent */
if (_po.length == 0) this.bp3getData(H_pw, this.bp, 'pathwayComponent').forEach(function(v) { switch (v.tagName) {
case 'bp:Catalysis':
_.bp3procCatalysis(v);
break;
case 'bp:BiochemicalReaction':
_.bp3procBiochemicalReaction(v);
break;
default:
Lerror('Invalid pathway component ['+v.tagName+']');
break;
} });
};
pathwayProcessor.prototype.bp3procCatalysis = function(H_ct, B_L2R) {
/* Check integrity */
if (!this.bp3check(H_ct, 'Catalysis')) return false;
var _ = this;
var res = [];
var typeCtrl = H_ct.getChildTextNS(_.bp, 'controlType');
var _ctrler = this.bp3getData(H_ct, this.bp, 'controller');
var ctrler = _.bp3procController(H_ct, _ctrler, B_L2R);
var _ctrled = this.bp3getData(H_ct, this.bp, 'controlled');
var ctrled = _.bp3procControlled(H_ct, _ctrled, B_L2R);
Lcmt("[REACTION] "+H_ct.tagName+" ["+ctrler.map(function(v) { return v.getAttributeNS(_.rdf, 'ID'); }).join(',')+"] " + typeCtrl + " ["+ctrled.map(function(v) { return v.getAttributeNS(_.rdf, 'ID'); }).join(',')+"]");
var er = {};
ctrler.forEach(function(I) {
switch (I.tagName) {
case 'bp:displayName':
/* displayName -> ??? */
er.displayName = I.textContent;
break;
case 'bp:Complex':
/* Complex -> ??? */
_.bp3getControllerComplex(I, er);
break;
case 'bp:xref':
/* Xref -> ??? */
_.bp3getControllerXref(I, er);
break;
default:
Lwarn('[bp:Catalysis] Invalid controller ['+I.tagName+']');
break;
}
});
for (var j in er) if (er.hasOwnProperty(j)) res.push(er[j]);
var ed = {};
ctrled.forEach(function(I) {
switch (I.tagName) {
case 'bp:displayName':
/* displayName -> ??? */
ed.displayName = I.textContent;
break;
case 'bp:BiochemicalReaction':
ed = _.bp3getNodeBiochemicalReaction(ctrled);
break;
default:
Lwarn('[bp:Catalysis] Invalid controlled ['+I.tagName+']');
break;
}
});
for (var j in ed) if (ed.hasOwnProperty(j)) res.push(ed[j]);
return true;
};
/**
* BioPAX 3 compatible
* Parses <bp:BiochemicalReaction>
* @argument {Element(bp:BiochemicalReaction)} A BiochemicalReaction element
* @argument {bool} No default value, L->R if true
*/
pathwayProcessor.prototype.bp3procBiochemicalReaction = function(H_br, B_L2R) {
var _ = this;
/* Check integrity */
if (!this.bp3check(H_br, 'BiochemicalReaction')) return false;
/* Check direction */
var B_subL2R = this.bp3procDirection(H_br, 'conversionDirection');
if (undefined === B_subL2R && undefined === B_L2R) {
/* 171129 assume L->R */
B_L2R = true;
//Lwarn('Invalid direction in bp3procBiochemicalReaction ['+H_br.id+']');
}
/* Check type */
H_br.type = 'indirect effect';
var H_it = this.bp3getData(H_br, this.bp, 'interactionType');
if (H_it.length == 1) {
var _it = H_it[0];
H_br.type = _it.getChildTextNS(this.bp, 'term');
Lcmt("BiochemReact ["+H_br.id+"] -> ["+H_br.type+"]");
} else
Lcmt("BiochemReact ["+H_br.id+"] do not have interactionType");
/* Get left & right */
var _L = this.bp3getData(H_br, this.bp, B_L2R?'left':'right');
var _R = this.bp3getData(H_br, this.bp, B_L2R?'right':'left');
/* Both have length? */
if (!_L.length || !_R.length) {
Lerror("Reaction ["+H_br.id+"] do not have left["+_L.length+"] nor right["+_R.length+"]");
return false;
}
Ldebug("BiochemicalReaction ["+H_br.id+"] direction ["+
(B_L2R?"L->R":"R->L")
+"]");
/* For both L and R, get rdf:resource */
var __L = [], __R = [];
for (var i=0 ; i<_L.length ; i++) __L.push(_L[i].id);
for (var i=0 ; i<_R.length ; i++) __R.push(_R[i].id);
Ldebug(" From ["+__L.join(',')+"]");
Ldebug(" To ["+__R.join(',')+"]");
H_br.L = _L;
H_br.R = _R;
H_br.L.forEach(function(v) {
H_br.R.forEach(function(w) {
_.A_bp3edge.push({
from: v.id,
to: w.id,
class: [ H_br.type ]
});
});
});
return true;
};
/* BioPAX 3 compatible */
pathwayProcessor.prototype.bp3procController = function(H_parent, A_ctrler, B_L2R) {
var _ = this;
var ret = [];
if (A_ctrler === undefined) {
Lerror("["+H_parent.id+"] undefined controller!");
return ret;
}
if (A_ctrler.length > 1)
Lwarn('Too many controller found');
var v = A_ctrler[0];
for (var i=0 ; i<v.childNodes.length ; i++) {
var I = v.childNodes[i];
I.anno = [];
switch (I.tagName) {
case 'bp:Complex':
// Get components
var q = I.getElementsByTagNameNS(this.bp, 'component');
I.anno.push(q[0].getAttributeNS(this.rdf, 'resource'));
ret.push(I);
break;
/* 171129 EntityReference */
case 'bp:entityReference':
break;
/* 171129 */
case 'bp:displayName':
I.anno.push('text:'+I.textContent);
ret.push(I);
break;
case 'bp:xref':
var tmp = _.bp3getData(I.parentElement, _.bp, 'xref')[0];
switch (tmp.tagName) {
case 'bp:PublicationXref':
var aDB = tmp.getElementsByTagNameNS(_.bp, 'db');
var aID = tmp.getElementsByTagNameNS(_.bp, 'id');
if (aDB.length != aID.length)
Lerror('PublicationXref DB and ID length unmatch');
else if (aDB.length == 0)
Lwarn('PublicationXref does not have publication information');
for (var i=0 ; i<aDB.length ; i++)
I.anno.push(aDB[i].textContent+':'+aID[i].textContent);
ret.push(I);
break;
default:
Lwarn('Errornous Xref type for Controller ['+tmp.tagName+']');
break;
} break;
case undefined: break;
default:
Lwarn('Errornous controller child node tag ['+I.tagName+']');
break;
}
}
/* Grab tags */
var tags = {};
ret.forEach(function(v) {
if (undefined === v.anno) return;
var tagsSub = _.bp3extractTagsFromArray(v.anno);
tagsSub.forEach(function(v) {
tags[v] = true;
});
});
// Get tags if not cached
_.bp3cacheElementsByTagNames(tags);
// Convert elements
ret.forEach(function(v) {
if (undefined === v.anno) return;
_.bp3convertID2element(v.anno);
});
return ret;
};
pathwayProcessor.prototype.bp3attachAnnotation = function(A_anno, ret) {
var _ = this;
for (var i=0 ; i<A_anno.length ; i++) {
var o = A_anno[i];
if (o instanceof Element)
ret[o.id] = o;
else {
var tmp = _.bp3interpretNonElemAnno(o);
if (tmp === undefined)
Lerror('Uninterpretable node exists in [anno] of bp:Complex');
}
}
}
pathwayProcessor.prototype.bp3getNodeComplex = function(A_elem) {
var _ = this;
var xml = this.xml();
var ret = {};
A_elem.forEach(function(I) {
// Tag check
if (I.tagName != 'bp:Complex') {
Lerror('['+I.tagName+'] is not a bp:Complex node');
return;
}
// Get components and return
_.bp3attachAnnotation(I.anno, ret);
});
return ret;
}
pathwayProcessor.prototype.bp3getControllerComplex = function(I, ret) {
var xml = this.xml();
// Tag check
if (I.tagName != 'bp:Complex') {
Lerror('['+I.tagName+'] is not a bp:Complex node');
return;
}
// Get components and return
this.bp3attachAnnotation(I.anno, ret);
return ret;
}
pathwayProcessor.prototype.bp3getControllerXref = function(I, ret) {
var xml = this.xml();
// Tag check
if (I.tagName != 'bp:xref') {
Lerror('['+I.tagName+'] is not a bp:xref node');
return;
}
// Get components and return
this.bp3attachAnnotation(I.anno, ret);
return ret;
}
pathwayProcessor.prototype.bp3getNodeBiochemicalReaction = function(A_elem) {
var xml = this.xml();
var ret = {};
var F_procComponentScan = function(v) {
if (v instanceof Element)
ret[v.id] = v;
else {
Lerror('Uninterpretable node [id='+v.id+'] exists in <bp:BiochemicalReaction>');
return {};
}
};
A_elem.forEach(function(I) {
// Tag check
if (I.tagName != 'bp:BiochemicalReaction') {
Lerror('['+v.tagName+'] is not a bp:BiochemicalReaction node');
return {};
}
// Get components and return
if (I.L === undefined || I.R === undefined)
Lwarn("LEFT/RIGHT relationship of bp:BiochemicalReaction not found, maybe premature initiation?");
if (I.L !== undefined) I.L.forEach(F_procComponentScan);
if (I.R !== undefined) I.R.forEach(F_procComponentScan);
});
return ret;
}
/* BioPAX 3 compatible */
pathwayProcessor.prototype.bp3procControlled = function(H_parent, A_ctrled, B_L2R) {
var ret = [];
var _ = this;
if (A_ctrled.length < 1)
Lwarn('No controlled found');
else if (A_ctrled.length > 1)
Lwarn('Too many controlled found');
var v = A_ctrled[0];
switch (v.tagName) {
case 'bp:BiochemicalReaction':
// Get components
_.bp3procBiochemicalReaction(v, B_L2R);
// var tags = _.bp3extractTagsFromArray(v.L, v.R);
// Get tags if not cached
// _.bp3cacheElementsByTagNames(tags);
// Convert elements
// _.bp3convertID2element(v.L, v.R);
ret.push(v);
break;
case undefined: break;
default:
Lwarn('Errornous controlled child node tag ['+v.tagName+']');
break;
}
return ret;
};
pathwayProcessor.prototype.bp3initEntries = function() {
var _ = this;
this.A_nodes.forEach(function(I) {
/* Insert to A_entry */
_.A_entry[I.id] = I;
});
};
pathwayProcessor.prototype.bp3procXref = function(w, proc) {
var S_ref = null;
var _db = w.getChildTextNS(this.bp, 'db').toLowerCase();
if (undefined === proc[_db])
Lnotice("Uncontrolled Xref ["+_db+":"+w.getChildTextNS(this.bp, 'id')+"] found");
else
S_ref = proc[_db].bind(this)(_db, w);
return S_ref;
}
pathwayProcessor.prototype.bp3getEdges = function(xml) {
var _ = this;
var xml = this.xml();
var ret = [];
_.dump();
this.A_bp3edge.forEach(function(I) {
ret.push(I);
});
return ret;
}
pathwayProcessor.prototype.bp3getNodes = function(xml) {
var _ = this;
this.bp3procPathway(xml.getElementsByTagNameNS(_.bp, 'Pathway')[0]);
var F_bp3annoDefault = function(v, A_ref) {
var S_ret = _.bp3getName(v);
if (S_ret === null) {
Lwarn('No available annotation found for protein ['+v.id+']');
} else {
A_ref.push(S_ret);
Lwarn('No supported annotation found for protein ['+v.id+'], use ['+S_ret+']');
}
};
var A_target = this.bp3getElementsByTagNames('Protein', 'SmallMolecule');
this.A_nodes = [];
A_target.forEach(function(v) { switch (v.tagName) {
case 'bp:SmallMolecule':
var str = _.bp3getData(v, _.bp, 'structure');
if (str.length) switch (str[0].getChildTextNS(_.bp, 'structureFormat')) {
case 'InChI':
var _key = str[0].getChildTextNS(_.bp, 'structureData');
$.ajax({
type:'post',
url:'stream.php?url='+encodeURIComponent(
'http://www.chemspider.com/InChI.asmx/InChIToCSID?inchi='+encodeURIComponent(_key)
),
dataType:'xml',
async:false,
success:function(a) {
if (a.childNodes[0].textContent.length) {
v.image = 'http://www.chemspider.com/ImagesHandler.ashx?id='+a.childNodes[0].textContent+'&w=250&h=250';
Lcmt('Structure image ['+v.image+']');
}
}