semtools 0.1.2 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +4 -1
- data/bin/onto2json.rb +9 -3
- data/bin/semtools.rb +446 -0
- data/bin/strsimnet.rb +1 -2
- data/external_data/ontologies.txt +4 -0
- data/lib/semtools/math_methods.rb +137 -129
- data/lib/semtools/ontology.rb +2550 -2032
- data/lib/semtools/sim_handler.rb +1 -1
- data/lib/semtools/version.rb +1 -1
- data/lib/semtools.rb +0 -1
- data/semtools.gemspec +3 -0
- metadata +48 -3
data/lib/semtools/ontology.rb
CHANGED
@@ -1,2041 +1,2559 @@
|
|
1
|
+
require 'expcalc'
|
1
2
|
require 'json'
|
3
|
+
require 'colorize'
|
2
4
|
|
3
5
|
|
4
6
|
class Ontology
|
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
|
-
|
1001
|
-
|
1002
|
-
|
1003
|
-
|
1004
|
-
|
1005
|
-
|
1006
|
-
|
1007
|
-
|
1008
|
-
|
1009
|
-
|
1010
|
-
|
1011
|
-
|
1012
|
-
|
1013
|
-
|
1014
|
-
|
1015
|
-
|
1016
|
-
|
1017
|
-
|
1018
|
-
|
1019
|
-
|
1020
|
-
|
1021
|
-
|
1022
|
-
|
1023
|
-
|
1024
|
-
|
1025
|
-
|
1026
|
-
|
1027
|
-
|
1028
|
-
|
1029
|
-
|
1030
|
-
|
1031
|
-
|
1032
|
-
|
1033
|
-
|
1034
|
-
|
1035
|
-
|
1036
|
-
|
1037
|
-
|
1038
|
-
|
1039
|
-
|
1040
|
-
|
1041
|
-
|
1042
|
-
|
1043
|
-
|
1044
|
-
|
1045
|
-
|
1046
|
-
|
1047
|
-
|
1048
|
-
|
1049
|
-
|
1050
|
-
|
1051
|
-
|
1052
|
-
|
1053
|
-
|
1054
|
-
|
1055
|
-
|
1056
|
-
|
1057
|
-
|
1058
|
-
|
1059
|
-
|
1060
|
-
|
1061
|
-
|
1062
|
-
|
1063
|
-
|
1064
|
-
|
1065
|
-
|
1066
|
-
|
1067
|
-
|
1068
|
-
|
1069
|
-
|
1070
|
-
|
1071
|
-
|
1072
|
-
|
1073
|
-
|
1074
|
-
|
1075
|
-
|
1076
|
-
|
1077
|
-
|
1078
|
-
|
1079
|
-
|
1080
|
-
|
1081
|
-
|
1082
|
-
|
1083
|
-
|
1084
|
-
|
1085
|
-
|
1086
|
-
|
1087
|
-
|
1088
|
-
|
1089
|
-
|
1090
|
-
|
1091
|
-
|
1092
|
-
|
1093
|
-
|
1094
|
-
|
1095
|
-
|
1096
|
-
|
1097
|
-
|
1098
|
-
|
1099
|
-
|
1100
|
-
|
1101
|
-
|
1102
|
-
|
1103
|
-
|
1104
|
-
|
1105
|
-
|
1106
|
-
|
1107
|
-
|
1108
|
-
|
1109
|
-
|
1110
|
-
|
1111
|
-
|
1112
|
-
|
1113
|
-
|
1114
|
-
|
1115
|
-
|
1116
|
-
|
1117
|
-
|
1118
|
-
|
1119
|
-
|
1120
|
-
|
1121
|
-
|
1122
|
-
|
1123
|
-
|
1124
|
-
|
1125
|
-
|
1126
|
-
|
1127
|
-
|
1128
|
-
|
1129
|
-
|
1130
|
-
|
1131
|
-
|
1132
|
-
|
1133
|
-
|
1134
|
-
|
1135
|
-
|
1136
|
-
|
1137
|
-
|
1138
|
-
|
1139
|
-
|
1140
|
-
|
1141
|
-
|
1142
|
-
|
1143
|
-
|
1144
|
-
|
1145
|
-
|
1146
|
-
|
1147
|
-
|
1148
|
-
|
1149
|
-
|
1150
|
-
|
1151
|
-
|
1152
|
-
|
1153
|
-
|
1154
|
-
|
1155
|
-
|
1156
|
-
|
1157
|
-
|
1158
|
-
|
1159
|
-
|
1160
|
-
|
1161
|
-
|
1162
|
-
|
1163
|
-
|
1164
|
-
|
1165
|
-
|
1166
|
-
|
1167
|
-
|
1168
|
-
|
1169
|
-
|
1170
|
-
|
1171
|
-
|
1172
|
-
|
1173
|
-
|
1174
|
-
|
1175
|
-
|
1176
|
-
|
1177
|
-
|
1178
|
-
|
1179
|
-
|
1180
|
-
|
1181
|
-
|
1182
|
-
|
1183
|
-
|
1184
|
-
|
1185
|
-
|
1186
|
-
|
1187
|
-
|
1188
|
-
|
1189
|
-
|
1190
|
-
|
1191
|
-
|
1192
|
-
|
1193
|
-
|
1194
|
-
|
1195
|
-
|
1196
|
-
|
1197
|
-
|
1198
|
-
|
1199
|
-
|
1200
|
-
|
1201
|
-
|
1202
|
-
|
1203
|
-
|
1204
|
-
|
1205
|
-
|
1206
|
-
|
1207
|
-
|
1208
|
-
|
1209
|
-
|
1210
|
-
|
1211
|
-
|
1212
|
-
|
1213
|
-
|
1214
|
-
|
1215
|
-
|
1216
|
-
|
1217
|
-
|
1218
|
-
|
1219
|
-
|
1220
|
-
|
1221
|
-
|
1222
|
-
|
1223
|
-
|
1224
|
-
|
1225
|
-
|
1226
|
-
|
1227
|
-
|
1228
|
-
|
1229
|
-
|
1230
|
-
|
1231
|
-
|
1232
|
-
|
1233
|
-
|
1234
|
-
|
1235
|
-
|
1236
|
-
|
1237
|
-
|
1238
|
-
|
1239
|
-
|
1240
|
-
|
1241
|
-
|
1242
|
-
|
1243
|
-
|
1244
|
-
|
1245
|
-
|
1246
|
-
|
1247
|
-
|
1248
|
-
|
1249
|
-
|
1250
|
-
|
1251
|
-
|
1252
|
-
|
1253
|
-
|
1254
|
-
|
1255
|
-
|
1256
|
-
|
1257
|
-
|
1258
|
-
|
1259
|
-
|
1260
|
-
|
1261
|
-
|
1262
|
-
|
1263
|
-
|
1264
|
-
|
1265
|
-
|
1266
|
-
|
1267
|
-
|
1268
|
-
|
1269
|
-
|
1270
|
-
|
1271
|
-
|
1272
|
-
|
1273
|
-
|
1274
|
-
|
1275
|
-
|
1276
|
-
|
1277
|
-
|
1278
|
-
|
1279
|
-
|
1280
|
-
|
1281
|
-
|
1282
|
-
|
1283
|
-
|
1284
|
-
|
1285
|
-
|
1286
|
-
|
1287
|
-
|
1288
|
-
|
1289
|
-
|
1290
|
-
|
1291
|
-
|
1292
|
-
|
1293
|
-
|
1294
|
-
|
1295
|
-
|
1296
|
-
|
1297
|
-
|
1298
|
-
|
1299
|
-
|
1300
|
-
|
1301
|
-
|
1302
|
-
|
1303
|
-
|
1304
|
-
|
1305
|
-
|
1306
|
-
|
1307
|
-
|
1308
|
-
|
1309
|
-
|
1310
|
-
|
1311
|
-
|
1312
|
-
|
1313
|
-
|
1314
|
-
|
1315
|
-
|
1316
|
-
|
1317
|
-
|
1318
|
-
|
1319
|
-
|
1320
|
-
|
1321
|
-
|
1322
|
-
|
1323
|
-
|
1324
|
-
|
1325
|
-
|
1326
|
-
|
1327
|
-
|
1328
|
-
|
1329
|
-
|
1330
|
-
|
1331
|
-
|
1332
|
-
|
1333
|
-
|
1334
|
-
|
1335
|
-
|
1336
|
-
|
1337
|
-
|
1338
|
-
|
1339
|
-
|
1340
|
-
|
1341
|
-
|
1342
|
-
|
1343
|
-
|
1344
|
-
|
1345
|
-
|
1346
|
-
|
1347
|
-
|
1348
|
-
|
1349
|
-
|
1350
|
-
|
1351
|
-
|
1352
|
-
|
1353
|
-
|
1354
|
-
|
1355
|
-
|
1356
|
-
|
1357
|
-
|
1358
|
-
|
1359
|
-
|
1360
|
-
|
1361
|
-
|
1362
|
-
|
1363
|
-
|
1364
|
-
|
1365
|
-
|
1366
|
-
|
1367
|
-
|
1368
|
-
|
1369
|
-
|
1370
|
-
|
1371
|
-
|
1372
|
-
|
1373
|
-
|
1374
|
-
|
1375
|
-
|
1376
|
-
|
1377
|
-
|
1378
|
-
|
1379
|
-
|
1380
|
-
|
1381
|
-
|
1382
|
-
|
1383
|
-
|
1384
|
-
|
1385
|
-
|
1386
|
-
|
1387
|
-
|
1388
|
-
|
1389
|
-
|
1390
|
-
|
1391
|
-
|
1392
|
-
|
1393
|
-
|
1394
|
-
|
1395
|
-
|
1396
|
-
|
1397
|
-
|
1398
|
-
|
1399
|
-
|
1400
|
-
|
1401
|
-
|
1402
|
-
|
1403
|
-
|
1404
|
-
|
1405
|
-
|
1406
|
-
|
1407
|
-
|
1408
|
-
|
1409
|
-
|
1410
|
-
|
1411
|
-
|
1412
|
-
|
1413
|
-
|
1414
|
-
|
1415
|
-
|
1416
|
-
|
1417
|
-
|
1418
|
-
|
1419
|
-
|
1420
|
-
|
1421
|
-
|
1422
|
-
|
1423
|
-
|
1424
|
-
|
1425
|
-
|
1426
|
-
|
1427
|
-
|
1428
|
-
|
1429
|
-
|
1430
|
-
|
1431
|
-
|
1432
|
-
|
1433
|
-
|
1434
|
-
|
1435
|
-
|
1436
|
-
|
1437
|
-
|
1438
|
-
|
1439
|
-
|
1440
|
-
|
1441
|
-
|
1442
|
-
|
1443
|
-
|
1444
|
-
|
1445
|
-
|
1446
|
-
|
1447
|
-
|
1448
|
-
|
1449
|
-
|
1450
|
-
|
1451
|
-
|
1452
|
-
|
1453
|
-
|
1454
|
-
|
1455
|
-
|
1456
|
-
|
1457
|
-
|
1458
|
-
|
1459
|
-
|
1460
|
-
|
1461
|
-
|
1462
|
-
|
1463
|
-
|
1464
|
-
|
1465
|
-
|
1466
|
-
|
1467
|
-
|
1468
|
-
|
1469
|
-
|
1470
|
-
|
1471
|
-
|
1472
|
-
|
1473
|
-
|
1474
|
-
|
1475
|
-
|
1476
|
-
|
1477
|
-
|
1478
|
-
|
1479
|
-
|
1480
|
-
|
1481
|
-
|
1482
|
-
|
1483
|
-
|
1484
|
-
|
1485
|
-
|
1486
|
-
|
1487
|
-
|
1488
|
-
|
1489
|
-
|
1490
|
-
|
1491
|
-
|
1492
|
-
|
1493
|
-
|
1494
|
-
|
1495
|
-
|
1496
|
-
|
1497
|
-
|
1498
|
-
|
1499
|
-
|
1500
|
-
|
1501
|
-
|
1502
|
-
|
1503
|
-
|
1504
|
-
|
1505
|
-
|
1506
|
-
|
1507
|
-
|
1508
|
-
|
1509
|
-
|
1510
|
-
|
1511
|
-
|
1512
|
-
|
1513
|
-
|
1514
|
-
|
1515
|
-
|
1516
|
-
|
1517
|
-
|
1518
|
-
|
1519
|
-
|
1520
|
-
|
1521
|
-
|
1522
|
-
|
1523
|
-
|
1524
|
-
|
1525
|
-
|
1526
|
-
|
1527
|
-
|
1528
|
-
|
1529
|
-
|
1530
|
-
|
1531
|
-
|
1532
|
-
|
1533
|
-
|
1534
|
-
|
1535
|
-
|
1536
|
-
|
1537
|
-
|
1538
|
-
|
1539
|
-
|
1540
|
-
|
1541
|
-
|
1542
|
-
|
1543
|
-
|
1544
|
-
|
1545
|
-
|
1546
|
-
|
1547
|
-
|
1548
|
-
|
1549
|
-
|
1550
|
-
|
1551
|
-
|
1552
|
-
|
1553
|
-
|
1554
|
-
|
1555
|
-
|
1556
|
-
|
1557
|
-
|
1558
|
-
|
1559
|
-
|
1560
|
-
|
1561
|
-
|
1562
|
-
|
1563
|
-
|
1564
|
-
|
1565
|
-
|
1566
|
-
|
1567
|
-
|
1568
|
-
|
1569
|
-
|
1570
|
-
|
1571
|
-
|
1572
|
-
|
1573
|
-
|
1574
|
-
|
1575
|
-
|
1576
|
-
|
1577
|
-
|
1578
|
-
|
1579
|
-
|
1580
|
-
|
1581
|
-
|
1582
|
-
|
1583
|
-
|
1584
|
-
|
1585
|
-
|
1586
|
-
|
1587
|
-
|
1588
|
-
|
1589
|
-
|
1590
|
-
|
1591
|
-
|
1592
|
-
|
1593
|
-
|
1594
|
-
|
1595
|
-
|
1596
|
-
|
1597
|
-
|
1598
|
-
|
1599
|
-
|
1600
|
-
|
1601
|
-
|
1602
|
-
|
1603
|
-
|
1604
|
-
|
1605
|
-
|
1606
|
-
|
1607
|
-
|
1608
|
-
|
1609
|
-
|
1610
|
-
|
1611
|
-
|
1612
|
-
|
1613
|
-
|
1614
|
-
|
1615
|
-
|
1616
|
-
|
1617
|
-
|
1618
|
-
|
1619
|
-
|
1620
|
-
|
1621
|
-
|
1622
|
-
|
1623
|
-
|
1624
|
-
|
1625
|
-
|
1626
|
-
|
1627
|
-
|
1628
|
-
|
1629
|
-
|
1630
|
-
|
1631
|
-
|
1632
|
-
|
1633
|
-
|
1634
|
-
|
1635
|
-
|
1636
|
-
|
1637
|
-
|
1638
|
-
|
1639
|
-
|
1640
|
-
|
1641
|
-
|
1642
|
-
|
1643
|
-
|
1644
|
-
|
1645
|
-
|
1646
|
-
|
1647
|
-
|
1648
|
-
|
1649
|
-
|
1650
|
-
|
1651
|
-
|
1652
|
-
|
1653
|
-
|
1654
|
-
|
1655
|
-
|
1656
|
-
|
1657
|
-
|
1658
|
-
|
1659
|
-
|
1660
|
-
|
1661
|
-
|
1662
|
-
|
1663
|
-
|
1664
|
-
|
1665
|
-
|
1666
|
-
|
1667
|
-
|
1668
|
-
|
1669
|
-
|
1670
|
-
|
1671
|
-
|
1672
|
-
|
1673
|
-
|
1674
|
-
|
1675
|
-
|
1676
|
-
|
1677
|
-
|
1678
|
-
|
1679
|
-
|
1680
|
-
|
1681
|
-
|
1682
|
-
|
1683
|
-
|
1684
|
-
|
1685
|
-
|
1686
|
-
|
1687
|
-
|
1688
|
-
|
1689
|
-
|
1690
|
-
|
1691
|
-
|
1692
|
-
|
1693
|
-
|
1694
|
-
|
1695
|
-
|
1696
|
-
|
1697
|
-
|
1698
|
-
|
1699
|
-
|
1700
|
-
|
1701
|
-
|
1702
|
-
|
1703
|
-
|
1704
|
-
|
1705
|
-
|
1706
|
-
|
1707
|
-
|
1708
|
-
|
1709
|
-
|
1710
|
-
|
1711
|
-
|
1712
|
-
|
1713
|
-
|
1714
|
-
|
1715
|
-
|
1716
|
-
|
1717
|
-
|
1718
|
-
|
1719
|
-
|
1720
|
-
|
1721
|
-
|
1722
|
-
|
1723
|
-
|
1724
|
-
|
1725
|
-
|
1726
|
-
|
1727
|
-
|
1728
|
-
|
1729
|
-
|
1730
|
-
|
1731
|
-
|
1732
|
-
|
1733
|
-
|
1734
|
-
|
1735
|
-
|
1736
|
-
|
1737
|
-
|
1738
|
-
|
1739
|
-
|
1740
|
-
|
1741
|
-
|
1742
|
-
|
1743
|
-
|
1744
|
-
|
1745
|
-
|
1746
|
-
|
1747
|
-
|
1748
|
-
|
1749
|
-
|
1750
|
-
|
1751
|
-
|
1752
|
-
|
1753
|
-
|
1754
|
-
|
1755
|
-
|
1756
|
-
|
1757
|
-
|
1758
|
-
|
1759
|
-
|
1760
|
-
|
1761
|
-
|
1762
|
-
|
1763
|
-
|
1764
|
-
|
1765
|
-
|
1766
|
-
|
1767
|
-
|
1768
|
-
|
1769
|
-
|
1770
|
-
|
1771
|
-
|
1772
|
-
|
1773
|
-
|
1774
|
-
|
1775
|
-
|
1776
|
-
|
1777
|
-
|
1778
|
-
|
1779
|
-
|
1780
|
-
|
1781
|
-
|
1782
|
-
|
1783
|
-
|
1784
|
-
|
1785
|
-
|
1786
|
-
|
1787
|
-
|
1788
|
-
|
1789
|
-
|
1790
|
-
|
1791
|
-
|
1792
|
-
|
1793
|
-
|
1794
|
-
|
1795
|
-
|
1796
|
-
|
1797
|
-
|
1798
|
-
|
1799
|
-
|
1800
|
-
|
1801
|
-
|
1802
|
-
|
1803
|
-
|
1804
|
-
|
1805
|
-
|
1806
|
-
|
1807
|
-
|
1808
|
-
|
1809
|
-
|
1810
|
-
|
1811
|
-
|
1812
|
-
|
1813
|
-
|
1814
|
-
|
1815
|
-
|
1816
|
-
|
1817
|
-
|
1818
|
-
|
1819
|
-
|
1820
|
-
|
1821
|
-
|
1822
|
-
|
1823
|
-
|
1824
|
-
|
1825
|
-
|
1826
|
-
|
1827
|
-
|
1828
|
-
|
1829
|
-
|
1830
|
-
|
1831
|
-
|
1832
|
-
|
1833
|
-
|
1834
|
-
|
1835
|
-
|
1836
|
-
|
1837
|
-
|
1838
|
-
|
1839
|
-
|
1840
|
-
|
1841
|
-
|
1842
|
-
|
1843
|
-
|
1844
|
-
|
1845
|
-
|
1846
|
-
|
1847
|
-
|
1848
|
-
|
1849
|
-
|
1850
|
-
|
1851
|
-
|
1852
|
-
|
1853
|
-
|
1854
|
-
|
1855
|
-
|
1856
|
-
|
1857
|
-
|
1858
|
-
|
1859
|
-
|
1860
|
-
|
1861
|
-
|
1862
|
-
|
1863
|
-
|
1864
|
-
|
1865
|
-
|
1866
|
-
|
1867
|
-
|
1868
|
-
|
1869
|
-
|
1870
|
-
|
1871
|
-
|
1872
|
-
|
1873
|
-
|
1874
|
-
|
1875
|
-
|
1876
|
-
|
1877
|
-
|
1878
|
-
|
1879
|
-
|
1880
|
-
|
1881
|
-
|
1882
|
-
|
1883
|
-
|
1884
|
-
|
1885
|
-
|
1886
|
-
|
1887
|
-
|
1888
|
-
|
1889
|
-
|
1890
|
-
|
1891
|
-
|
1892
|
-
|
1893
|
-
|
1894
|
-
|
1895
|
-
|
1896
|
-
|
1897
|
-
|
1898
|
-
|
1899
|
-
|
1900
|
-
|
1901
|
-
|
1902
|
-
|
1903
|
-
|
1904
|
-
|
1905
|
-
|
1906
|
-
|
1907
|
-
|
1908
|
-
|
1909
|
-
|
1910
|
-
|
1911
|
-
|
1912
|
-
|
1913
|
-
|
1914
|
-
|
1915
|
-
|
1916
|
-
|
1917
|
-
|
1918
|
-
|
1919
|
-
|
1920
|
-
|
1921
|
-
|
1922
|
-
|
1923
|
-
|
1924
|
-
|
1925
|
-
|
1926
|
-
|
1927
|
-
|
1928
|
-
|
1929
|
-
|
1930
|
-
|
1931
|
-
|
1932
|
-
|
1933
|
-
|
1934
|
-
|
1935
|
-
|
1936
|
-
|
1937
|
-
|
1938
|
-
|
1939
|
-
|
1940
|
-
|
1941
|
-
|
1942
|
-
|
1943
|
-
|
1944
|
-
|
1945
|
-
|
1946
|
-
|
1947
|
-
|
1948
|
-
|
1949
|
-
|
1950
|
-
|
1951
|
-
|
1952
|
-
|
1953
|
-
|
1954
|
-
|
1955
|
-
|
1956
|
-
|
1957
|
-
|
1958
|
-
|
1959
|
-
|
1960
|
-
|
1961
|
-
|
1962
|
-
|
1963
|
-
|
1964
|
-
|
1965
|
-
|
1966
|
-
|
1967
|
-
|
1968
|
-
|
1969
|
-
|
1970
|
-
|
1971
|
-
|
1972
|
-
|
1973
|
-
|
1974
|
-
|
1975
|
-
|
1976
|
-
|
1977
|
-
|
1978
|
-
|
1979
|
-
|
1980
|
-
|
1981
|
-
|
1982
|
-
|
1983
|
-
|
1984
|
-
|
1985
|
-
|
1986
|
-
|
1987
|
-
|
1988
|
-
|
1989
|
-
|
1990
|
-
|
1991
|
-
|
1992
|
-
|
1993
|
-
|
1994
|
-
|
1995
|
-
|
1996
|
-
|
1997
|
-
|
1998
|
-
|
1999
|
-
|
2000
|
-
|
2001
|
-
|
2002
|
-
|
2003
|
-
|
2004
|
-
|
2005
|
-
|
2006
|
-
|
2007
|
-
|
7
|
+
#########################################################
|
8
|
+
# AUTHOR NOTES
|
9
|
+
#########################################################
|
10
|
+
|
11
|
+
# 1 - Store @profiles as @stanzas[:instances]
|
12
|
+
# 2 - Items values (not keys) are imported as strings, not as symbols (maybe add a flag which indicates if values are, or not, symbols?)
|
13
|
+
|
14
|
+
|
15
|
+
#############################################
|
16
|
+
# FIELDS
|
17
|
+
#############################################
|
18
|
+
# Handled class variables
|
19
|
+
# => @@basic_tags :: hash with main OBO structure tags
|
20
|
+
# => @@allowed_calcs :: hash with allowed ICs and similaritites calcs
|
21
|
+
# => @@symbolizable_ids :: tags which can be symbolized
|
22
|
+
# => @@tags_with_trailing_modifiers :: tags which can include extra info after specific text modifiers
|
23
|
+
#
|
24
|
+
# Handled object variables
|
25
|
+
# => @header :: file header (if is available)
|
26
|
+
# => @stanzas :: OBO stanzas {:terms,:typedefs,:instances}
|
27
|
+
# => @ancestors_index :: hash of ancestors per each term handled with any structure relationships
|
28
|
+
# => @descendants_index :: hash of descendants per each term handled with any structure relationships
|
29
|
+
# => @alternatives_index :: has of alternative IDs (include alt_id and obsoletes)
|
30
|
+
# => @obsoletes_index :: hash of obsoletes and it's new ids
|
31
|
+
# => @special_tags :: set of special tags to be expanded (:is_a, :obsolete, :alt_id)
|
32
|
+
# => @structureType :: type of ontology structure depending on ancestors relationship. Allowed: {atomic, sparse, circular, hierarchical}
|
33
|
+
# => @ics :: already calculated ICs for handled terms and IC types
|
34
|
+
# => @meta :: meta_information about handled terms like [ancestors, descendants, struct_freq, observed_freq]
|
35
|
+
# => @max_freqs :: maximum freqs found for structural and observed freqs
|
36
|
+
# => @dicts :: bidirectional dictionaries with three levels <key|value>: 1º) <tag|hash2>; 2º) <(:byTerm/:byValue)|hash3>; 3º) dictionary <k|v>
|
37
|
+
# => @profiles :: set of terms assigned to an ID
|
38
|
+
# => @profilesDict :: set of profile IDs assigned to a term
|
39
|
+
# => @items :: hash with items relations to terms
|
40
|
+
# => @removable_terms :: array of terms to not be considered
|
41
|
+
# => @term_paths :: metainfo about parental paths of each term
|
42
|
+
|
43
|
+
@@basic_tags = {ancestors: [:is_a], obsolete: :is_obsolete, alternative: [:replaced_by,:consider,:alt_id]}
|
44
|
+
@@allowed_calcs = {ics: [:resnik, :resnik_observed, :seco, :zhou, :sanchez], sims: [:resnik, :lin, :jiang_conrath]}
|
45
|
+
@@symbolizable_ids = [:id, :alt_id, :replaced_by, :consider]
|
46
|
+
@@tags_with_trailing_modifiers = [:is_a, :union_of, :disjoint_from, :relationship, :subsetdef, :synonymtypedef, :property_value]
|
47
|
+
@@multivalue_tags = [:alt_id, :is_a, :subset, :synonym, :xref, :intersection_of, :union_of, :disjoint_from, :relationship, :replaced_by, :consider, :subsetdef, :synonymtypedef, :property_value, :remark]
|
48
|
+
@@symbolizable_ids.concat(@@tags_with_trailing_modifiers)
|
49
|
+
|
50
|
+
#############################################
|
51
|
+
# CONSTRUCTOR
|
52
|
+
#############################################
|
53
|
+
|
54
|
+
# Instantiate a OBO_Handler object
|
55
|
+
# ===== Parameters
|
56
|
+
# +file+:: with info to be loaded (.obo ; .json)
|
57
|
+
# +load_file+:: activate load process automatically
|
58
|
+
# +removable_terms+: term to be removed from calcs
|
59
|
+
# +build+: flag to launch metainfo calculation
|
60
|
+
# +file_format+: force format type despite file extension. Can be :obo or :json
|
61
|
+
def initialize(file: nil, load_file: false, removable_terms: [], build: true, file_format: nil)
|
62
|
+
# Initialize object variables
|
63
|
+
@header = nil
|
64
|
+
@stanzas = {terms: {}, typedefs: {}, instances: {}}
|
65
|
+
@ancestors_index = {}
|
66
|
+
@descendants_index = {}
|
67
|
+
@alternatives_index = {}
|
68
|
+
@obsoletes_index = {}
|
69
|
+
@structureType = nil
|
70
|
+
@ics = Hash[@@allowed_calcs[:ics].map{|ictype| [ictype, {}]}]
|
71
|
+
@meta = {}
|
72
|
+
@special_tags = @@basic_tags.clone
|
73
|
+
@max_freqs = {:struct_freq => -1.0, :observed_freq => -1.0, :max_depth => -1.0}
|
74
|
+
@dicts = {}
|
75
|
+
@profiles = {}
|
76
|
+
@profilesDict = {}
|
77
|
+
@items = {}
|
78
|
+
@removable_terms = []
|
79
|
+
@term_paths = {}
|
80
|
+
add_removable_terms(removable_terms) if !removable_terms.empty?
|
81
|
+
load_file = true unless file.nil? # This should remove load_file argument, keep it for old scripts
|
82
|
+
# Load if proceeds
|
83
|
+
if load_file
|
84
|
+
fformat = file_format
|
85
|
+
fformat = File.extname(file) if fformat.nil? && !file.nil?
|
86
|
+
if fformat == :obo || fformat == ".obo"
|
87
|
+
load(file, build: build)
|
88
|
+
elsif fformat == :json || fformat == ".json"
|
89
|
+
self.read(file, build: build)
|
90
|
+
elsif !fformat.nil?
|
91
|
+
warn 'Format not allowed. Loading process will not be performed'
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
|
97
|
+
#############################################
|
98
|
+
# CLASS METHODS
|
99
|
+
#############################################
|
100
|
+
|
101
|
+
# Expand a (starting) term using a specific tag and return all extended terms into an array and
|
102
|
+
# the relationship structuture observed (hierarchical or circular). If circular structure is
|
103
|
+
# foumd, extended array will be an unique vector without starting term (no loops).
|
104
|
+
# +Note+: we extremly recomend use get_related_ids_by_tag function instead of it (directly)
|
105
|
+
# ===== Parameters
|
106
|
+
# +start+:: term where start to expand
|
107
|
+
# +terms+:: set to be used to expand
|
108
|
+
# +target_tag+:: tag used to expand
|
109
|
+
# +eexpansion+:: already expanded info
|
110
|
+
# +split_info_char+:: special regex used to split info (if it is necessary)
|
111
|
+
# +split_info_indx+:: special index to take splitted info (if it is necessary)
|
112
|
+
# +alt_ids+:: set of alternative IDs
|
113
|
+
# ===== Returns
|
114
|
+
# A vector with the observed structure (string) and the array with extended terms.
|
115
|
+
def self.get_related_ids(start_id, terms, target_tag, related_ids = {}, alt_ids = {})
|
116
|
+
# Take start_id term available info and already accumulated info
|
117
|
+
current_associations = related_ids[start_id]
|
118
|
+
current_associations = [] if current_associations.nil?
|
119
|
+
return [:no_term,[]] if terms[start_id].nil?
|
120
|
+
id_relations = terms[start_id][target_tag]
|
121
|
+
return [:source,[]] if id_relations.nil?
|
122
|
+
|
123
|
+
# Prepare auxiliar variables
|
124
|
+
struct = :hierarchical
|
125
|
+
|
126
|
+
# Study direct extensions
|
127
|
+
id_relations = id_relations.clone
|
128
|
+
while id_relations.length > 0
|
129
|
+
id = id_relations.shift
|
130
|
+
id = alt_ids[id].first if alt_ids.include?(id) # NOTE: if you want to persist current ID instead source ID, re-implement this
|
131
|
+
|
132
|
+
# Handle
|
133
|
+
if current_associations.include?(id) # Check if already have been included into this expansion
|
134
|
+
struct = :circular
|
135
|
+
else
|
136
|
+
current_associations << id
|
137
|
+
if related_ids.include?(id) # Check if current already has been expanded
|
138
|
+
current_associations = current_associations | related_ids[id]
|
139
|
+
if current_associations.include?(start_id) # Check circular case
|
140
|
+
struct = :circular
|
141
|
+
[id, start_id].each{|repeated| current_associations.delete(repeated)}
|
142
|
+
end
|
143
|
+
else # Expand
|
144
|
+
related_ids[start_id] = current_associations
|
145
|
+
structExp, current_related_ids = self.get_related_ids(id, terms, target_tag, related_ids, alt_ids) # Expand current
|
146
|
+
current_associations = current_associations | current_related_ids
|
147
|
+
struct = :circular if structExp == :circular # Check struct
|
148
|
+
if current_associations.include?(start_id) # Check circular case
|
149
|
+
struct = :circular
|
150
|
+
current_associations.delete(start_id)
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
related_ids[start_id] = current_associations
|
156
|
+
|
157
|
+
return struct, current_associations
|
158
|
+
end
|
159
|
+
|
160
|
+
|
161
|
+
# Expand terms using a specific tag and return all extended terms into an array and
|
162
|
+
# the relationship structuture observed (hierarchical or circular). If circular structure is
|
163
|
+
# foumd, extended array will be an unique vector without starting term (no loops)
|
164
|
+
# ===== Parameters
|
165
|
+
# +terms+:: set to be used to expand
|
166
|
+
# +target_tag+:: tag used to expand
|
167
|
+
# +split_info_char+:: special regex used to split info (if it is necessary)
|
168
|
+
# +split_info_indx+:: special index to take splitted info (if it is necessary)
|
169
|
+
# +alt_ids+:: set of alternative IDs
|
170
|
+
# +obsoletes+:: integer with the number of obsolete IDs. used to calculate structure type.
|
171
|
+
# ===== Returns
|
172
|
+
# A vector with the observed structure (string) and the hash with extended terms
|
173
|
+
def self.get_related_ids_by_tag(terms:,target_tag:, alt_ids: {}, obsoletes: 0)
|
174
|
+
# Define structure type
|
175
|
+
structType = :hierarchical
|
176
|
+
related_ids = {}
|
177
|
+
terms.each do |id, tags|
|
178
|
+
# Check if target tag is defined
|
179
|
+
if !tags[target_tag].nil?
|
180
|
+
# Obtain related terms
|
181
|
+
set_structure, _ = self.get_related_ids(id, terms, target_tag, related_ids, alt_ids)
|
182
|
+
# Check structure
|
183
|
+
structType = :circular if set_structure == :circular
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
# Check special case
|
188
|
+
structType = :atomic if related_ids.length <= 0
|
189
|
+
structType = :sparse if related_ids.length > 0 && ((terms.length - related_ids.length - obsoletes) >= 2)
|
190
|
+
# Return type and hash with related_ids
|
191
|
+
return structType, related_ids
|
192
|
+
end
|
193
|
+
|
194
|
+
|
195
|
+
# Class method to transform string with <tag : info> into hash structure
|
196
|
+
# ===== Parameters
|
197
|
+
# +attributes+:: array tuples with info to be transformed into hash format
|
198
|
+
# ===== Returns
|
199
|
+
# Attributes stored into hash structure
|
200
|
+
def self.info2hash(attributes, split_char = " ! ", selected_field = 0)
|
201
|
+
# Load info
|
202
|
+
info_hash = {}
|
203
|
+
# Only TERMS multivalue tags (future add Typedefs and Instance)
|
204
|
+
# multivalue_tags = [:alt_id, :is_a, :subset, :synonym, :xref, :intersection_of, :union_of, :disjoint_from, :relationship, :replaced_by, :consider]
|
205
|
+
attributes.each do |tag, value|
|
206
|
+
value.gsub!(/{source=[\\\":A-Za-z0-9\/\.\-, =]+} /, '') if tag == 'is_a' # To delete "source" attributes in is_a tag of MONDO ontology
|
207
|
+
# Check
|
208
|
+
raise EncodingError, 'Info element incorrect format' if (tag.nil?) || (value.nil?)
|
209
|
+
# Prepare
|
210
|
+
tag = tag.lstrip.to_sym
|
211
|
+
value.lstrip!
|
212
|
+
value = value.split(split_char)[selected_field].to_sym if @@tags_with_trailing_modifiers.include?(tag)
|
213
|
+
|
214
|
+
# Store
|
215
|
+
query = info_hash[tag]
|
216
|
+
if !query.nil? # Tag already exists
|
217
|
+
if !query.kind_of?(Array) # Check that tag is multivalue
|
218
|
+
raise('Attempt to concatenate plain text with another. The tag is not declared as multivalue. [' + tag.to_s + '](' + query + ')')
|
219
|
+
else
|
220
|
+
query << value # Add new value to tag
|
221
|
+
end
|
222
|
+
else # New entry
|
223
|
+
if @@multivalue_tags.include?(tag)
|
224
|
+
info_hash[tag] = [value]
|
225
|
+
else
|
226
|
+
info_hash[tag] = value
|
227
|
+
end
|
228
|
+
end
|
229
|
+
end
|
230
|
+
self.symbolize_ids(info_hash)
|
231
|
+
return info_hash
|
232
|
+
end
|
233
|
+
|
234
|
+
|
235
|
+
# Class method to load an OBO format file (based on OBO 1.4 format). Specially focused on load
|
236
|
+
# the Header, the Terms, the Typedefs and the Instances.
|
237
|
+
# ===== Parameters
|
238
|
+
# +file+:: OBO file to be loaded
|
239
|
+
# ===== Returns
|
240
|
+
# Hash with FILE, HEADER and STANZAS info
|
241
|
+
def self.load_obo(file) #TODO: Send to obo_parser class
|
242
|
+
raise("File is not defined") if file.nil?
|
243
|
+
# Data variables
|
244
|
+
header = ''
|
245
|
+
stanzas = {terms: {}, typedefs: {}, instances: {}}
|
246
|
+
# Auxiliar variables
|
247
|
+
infoType = 'Header'
|
248
|
+
currInfo = []
|
249
|
+
stanzas_flags = %w[[Term] [Typedef] [Instance]]
|
250
|
+
# Read file
|
251
|
+
File.open(file).each do |line|
|
252
|
+
line.chomp!
|
253
|
+
next if line.empty?
|
254
|
+
fields = line.split(':', 2)
|
255
|
+
# Check if new instance is found
|
256
|
+
if stanzas_flags.include?(line)
|
257
|
+
header = self.process_entity(header, infoType, stanzas, currInfo)
|
258
|
+
# Update info variables
|
259
|
+
currInfo = []
|
260
|
+
infoType = line.gsub!(/[\[\]]/, '')
|
261
|
+
next
|
262
|
+
end
|
263
|
+
# Concat info
|
264
|
+
currInfo << fields
|
265
|
+
end
|
266
|
+
# Store last loaded info
|
267
|
+
header = self.process_entity(header, infoType, stanzas, currInfo) if !currInfo.empty?
|
268
|
+
|
269
|
+
# Prepare to return
|
270
|
+
finfo = {:file => file, :name => File.basename(file, File.extname(file))}
|
271
|
+
return finfo, header, stanzas
|
272
|
+
end
|
273
|
+
|
274
|
+
|
275
|
+
# Handle OBO loaded info and stores it into correct container and format
|
276
|
+
# ===== Parameters
|
277
|
+
# +header+:: container
|
278
|
+
# +infoType+:: current ontology item type detected
|
279
|
+
# +stanzas+:: container
|
280
|
+
# +currInfo+:: info to be stored
|
281
|
+
# ===== Returns
|
282
|
+
# header newly/already stored
|
283
|
+
def self.process_entity(header, infoType, stanzas, currInfo)
|
284
|
+
info = self.info2hash(currInfo)
|
285
|
+
# Store current info
|
286
|
+
if infoType.eql?('Header')
|
287
|
+
header = info
|
288
|
+
else
|
289
|
+
id = info[:id]
|
290
|
+
case infoType
|
291
|
+
when 'Term'
|
292
|
+
stanzas[:terms][id] = info
|
293
|
+
when 'Typedef'
|
294
|
+
stanzas[:typedefs][id] = info
|
295
|
+
when 'Instance'
|
296
|
+
stanzas[:instances][id] = info
|
297
|
+
end
|
298
|
+
end
|
299
|
+
return header
|
300
|
+
end
|
301
|
+
|
302
|
+
|
303
|
+
# Symboliza all values into hashs using symbolizable tags as keys
|
304
|
+
# ===== Parameters
|
305
|
+
# +item_hash+:: hash to be checked
|
306
|
+
def self.symbolize_ids(item_hash)
|
307
|
+
@@symbolizable_ids.each do |tag|
|
308
|
+
query = item_hash[tag]
|
309
|
+
if !query.nil?
|
310
|
+
if query.kind_of?(Array)
|
311
|
+
query.map!{|item| item.to_sym}
|
312
|
+
else
|
313
|
+
item_hash[tag] = query.to_sym if !query.nil?
|
314
|
+
end
|
315
|
+
end
|
316
|
+
end
|
317
|
+
end
|
318
|
+
|
319
|
+
|
320
|
+
#
|
321
|
+
# ===== Parameters
|
322
|
+
# +root+:: main term to expand
|
323
|
+
# +ontology+:: to be cutted
|
324
|
+
# +clone+:: if true, given ontology object will not be mutated
|
325
|
+
# +remove_up+:: if true, stores only the root term given an it descendants. If false, only root ancestors will be stored
|
326
|
+
# ===== Returns
|
327
|
+
# An Ontology object with terms after cut the ontology.
|
328
|
+
def self.mutate(root, ontology, clone: true, remove_up: true)
|
329
|
+
ontology = ontology.clone if clone
|
330
|
+
# Obtain affected IDs
|
331
|
+
descendants = ontology.descendants_index[root]
|
332
|
+
descendants << root # Store itself to do not remove it
|
333
|
+
# Remove unnecesary terms
|
334
|
+
ontology.stanzas[:terms] = ontology.stanzas[:terms].select{|id,v| remove_up ? descendants.include?(id) : !descendants.include?(id)}
|
335
|
+
ontology.ics = Hash[@@allowed_calcs[:ics].map{|ictype| [ictype, {}]}]
|
336
|
+
ontology.max_freqs = {:struct_freq => -1.0, :observed_freq => -1.0, :max_depth => -1.0}
|
337
|
+
ontology.dicts = {}
|
338
|
+
ontology.removable_terms = []
|
339
|
+
ontology.term_paths = {}
|
340
|
+
# Recalculate metadata
|
341
|
+
ontology.build_index
|
342
|
+
ontology.add_observed_terms_from_profiles
|
343
|
+
# Finish
|
344
|
+
return ontology
|
345
|
+
end
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
#############################################
|
350
|
+
# GENERAL METHODS
|
351
|
+
#############################################
|
352
|
+
|
353
|
+
# Include removable terms to current removable terms list
|
354
|
+
# ===== Parameters
|
355
|
+
# +terms+:: terms array to be concatenated
|
356
|
+
def add_removable_terms(terms)
|
357
|
+
terms = terms.map{|term| term.to_sym}
|
358
|
+
@removable_terms.concat(terms)
|
359
|
+
end
|
360
|
+
|
361
|
+
|
362
|
+
# Include removable terms to current removable terms list loading new
|
363
|
+
# terms from a one column plain text file
|
364
|
+
# ===== Parameters
|
365
|
+
# +file+:: to be loaded
|
366
|
+
def add_removable_terms_from_file(file)
|
367
|
+
File.open(excluded_codes_file).each do |line|
|
368
|
+
line.chomp!
|
369
|
+
@removable_terms << line.to_sym
|
370
|
+
end
|
371
|
+
end
|
372
|
+
|
373
|
+
|
374
|
+
# Increase observed frequency for a specific term
|
375
|
+
# ===== Parameters
|
376
|
+
# +term+:: term which frequency is going to be increased
|
377
|
+
# +increas+:: frequency rate to be increased. Default = 1
|
378
|
+
# ===== Return
|
379
|
+
# true if process ends without errors, false in other cases
|
380
|
+
def add_observed_term(term:,increase: 1.0)
|
381
|
+
# Check
|
382
|
+
raise ArgumentError, "Term given is NIL" if term.nil?
|
383
|
+
return false unless @stanzas[:terms].include?(term)
|
384
|
+
return false if @removable_terms.include?(term)
|
385
|
+
if @alternatives_index.include?(term)
|
386
|
+
alt_id = @alternatives_index[term]
|
387
|
+
@meta[alt_id] = {:ancestors => -1.0,:descendants => -1.0,:struct_freq => 0.0,:observed_freq => 0.0} if @meta[alt_id].nil?
|
388
|
+
@meta[term] = @meta[alt_id]
|
389
|
+
end
|
390
|
+
# Check if exists
|
391
|
+
@meta[term] = {:ancestors => -1.0,:descendants => -1.0,:struct_freq => 0.0,:observed_freq => 0.0} if @meta[term].nil?
|
392
|
+
# Add frequency
|
393
|
+
@meta[term][:observed_freq] = 0 if @meta[term][:observed_freq] == -1
|
394
|
+
@meta[term][:observed_freq] += increase
|
395
|
+
# Check maximum frequency
|
396
|
+
@max_freqs[:observed_freq] = @meta[term][:observed_freq] if @max_freqs[:observed_freq] < @meta[term][:observed_freq]
|
397
|
+
return true
|
398
|
+
end
|
399
|
+
|
400
|
+
|
401
|
+
# Increase the arbitrary frequency of a given term set
|
402
|
+
# ===== Parameters
|
403
|
+
# +terms+:: set of terms to be updated
|
404
|
+
# +increase+:: amount to be increased
|
405
|
+
# +transform_to_sym+:: if true, transform observed terms to symbols. Default: false
|
406
|
+
# ===== Return
|
407
|
+
# true if process ends without errors and false in other cases
|
408
|
+
def add_observed_terms(terms:, increase: 1.0, transform_to_sym: false)
|
409
|
+
# Check
|
410
|
+
raise ArgumentError, 'Terms array given is NIL' if terms.nil?
|
411
|
+
raise ArgumentError, 'Terms given is not an array' if !terms.is_a? Array
|
412
|
+
# Add observations
|
413
|
+
if transform_to_sym
|
414
|
+
checks = terms.map{|id| self.add_observed_term(term: id.to_sym,increase: increase)}
|
415
|
+
else
|
416
|
+
checks = terms.map{|id| self.add_observed_term(term: id,increase: increase)}
|
417
|
+
end
|
418
|
+
return checks
|
419
|
+
end
|
420
|
+
|
421
|
+
|
422
|
+
# Compare to terms sets
|
423
|
+
# ===== Parameters
|
424
|
+
# +termsA+:: set to be compared
|
425
|
+
# +termsB+:: set to be compared
|
426
|
+
# +sim_type+:: similitude method to be used. Default: resnik
|
427
|
+
# +ic_type+:: ic type to be used. Default: resnik
|
428
|
+
# +bidirectional+:: calculate bidirectional similitude. Default: false
|
429
|
+
# ===== Return
|
430
|
+
# similitude calculated
|
431
|
+
def compare(termsA, termsB, sim_type: :resnik, ic_type: :resnik, bidirectional: true, store_mica: false)
|
432
|
+
# Check
|
433
|
+
raise ArgumentError, "Terms sets given are NIL" if termsA.nil? | termsB.nil?
|
434
|
+
raise ArgumentError, "Set given is empty. Aborting similarity calc" if termsA.empty? | termsB.empty?
|
435
|
+
micasA = []
|
436
|
+
# Compare A -> B
|
437
|
+
termsA.each do |tA|
|
438
|
+
micas = []
|
439
|
+
termsB.each do |tB|
|
440
|
+
if store_mica
|
441
|
+
value = @mica_index.dig(tA, tB)
|
442
|
+
else
|
443
|
+
value = nil
|
444
|
+
end
|
445
|
+
if value.nil?
|
446
|
+
value = self.get_similarity(tA, tB, type: sim_type, ic_type: ic_type)
|
447
|
+
if store_mica
|
448
|
+
value = true if value.nil? # We use true to save that the operation was made but there is not mica value
|
449
|
+
add2nestHash(@mica_index, tA, tB, value)
|
450
|
+
end
|
451
|
+
end
|
452
|
+
micas << value if value.class == Float
|
453
|
+
end
|
454
|
+
if !micas.empty?
|
455
|
+
micasA << micas.max # Obtain maximum value
|
456
|
+
else
|
457
|
+
micasA << 0
|
458
|
+
end
|
459
|
+
end
|
460
|
+
means_sim = micasA.inject{ |sum, el| sum + el }.fdiv(micasA.size)
|
461
|
+
# Compare B -> A
|
462
|
+
if bidirectional
|
463
|
+
means_simA = means_sim * micasA.size
|
464
|
+
means_simB = self.compare(termsB, termsA, sim_type: sim_type, ic_type: ic_type, bidirectional: false, store_mica: store_mica) * termsB.size
|
465
|
+
means_sim = (means_simA + means_simB).fdiv(termsA.size + termsB.size)
|
466
|
+
end
|
467
|
+
# Return
|
468
|
+
return means_sim
|
469
|
+
end
|
470
|
+
|
471
|
+
def add2nestHash(h, key1, key2, val)
|
472
|
+
query1 = h[key1]
|
473
|
+
if query1.nil?
|
474
|
+
h[key1] = {key2 => val}
|
475
|
+
else
|
476
|
+
query1[key2] = val
|
477
|
+
end
|
478
|
+
end
|
479
|
+
|
480
|
+
# Compare internal stored profiles against another set of profiles. If an external set is not provided, internal profiles will be compared with itself
|
481
|
+
# ===== Parameters
|
482
|
+
# +external_profiles+:: set of external profiles. If nil, internal profiles will be compared with itself
|
483
|
+
# +sim_type+:: similitude method to be used. Default: resnik
|
484
|
+
# +ic_type+:: ic type to be used. Default: resnik
|
485
|
+
# +bidirectional+:: calculate bidirectional similitude. Default: false
|
486
|
+
# ===== Return
|
487
|
+
# Similitudes calculated
|
488
|
+
def compare_profiles(external_profiles: nil, sim_type: :resnik, ic_type: :resnik, bidirectional: true)
|
489
|
+
profiles_similarity = {} #calculate similarity between patients profile
|
490
|
+
profiles_ids = @profiles.keys
|
491
|
+
if external_profiles.nil?
|
492
|
+
comp_ids = profiles_ids
|
493
|
+
comp_profiles = @profiles
|
494
|
+
main_ids = comp_ids
|
495
|
+
main_profiles = comp_profiles
|
496
|
+
else
|
497
|
+
comp_ids = external_profiles.keys
|
498
|
+
comp_profiles = external_profiles
|
499
|
+
main_ids = profiles_ids
|
500
|
+
main_profiles = @profiles
|
501
|
+
end
|
502
|
+
# Compare
|
503
|
+
@mica_index = {}
|
504
|
+
while !main_ids.empty?
|
505
|
+
curr_id = main_ids.shift
|
506
|
+
current_profile = main_profiles[curr_id]
|
507
|
+
comp_ids.each do |id|
|
508
|
+
profile = comp_profiles[id]
|
509
|
+
value = compare(current_profile, profile, sim_type: sim_type, ic_type: ic_type, bidirectional: bidirectional, store_mica: true)
|
510
|
+
query = profiles_similarity[curr_id]
|
511
|
+
if query.nil?
|
512
|
+
profiles_similarity[curr_id] = {id => value}
|
513
|
+
else
|
514
|
+
query[id] = value
|
515
|
+
end
|
516
|
+
end
|
517
|
+
end
|
518
|
+
return profiles_similarity
|
519
|
+
end
|
520
|
+
|
521
|
+
|
522
|
+
# Expand alternative IDs arround all already stored terms
|
523
|
+
# ===== Parameters
|
524
|
+
# +alt_tag+:: tag used to expand alternative IDs
|
525
|
+
# ===== Returns
|
526
|
+
# true if process ends without errors and false in other cases
|
527
|
+
def get_index_alternatives(alt_tag: @@basic_tags[:alternative].last)
|
528
|
+
# Check input
|
529
|
+
raise('stanzas terms empty') if @stanzas[:terms].empty?
|
530
|
+
# Take all alternative IDs
|
531
|
+
alt_ids2add = {}
|
532
|
+
@stanzas[:terms].each do |id, tags|
|
533
|
+
if id == tags[:id] # Avoid simulated alternative terms
|
534
|
+
# id = tags[:id] # Take always real ID in case of alternative terms simulted
|
535
|
+
alt_ids = tags[alt_tag]
|
536
|
+
if !alt_ids.nil?
|
537
|
+
alt_ids = alt_ids - @removable_terms - [id]
|
538
|
+
# Update info
|
539
|
+
alt_ids.each do |alt_term|
|
540
|
+
@alternatives_index[alt_term] = id
|
541
|
+
alt_ids2add[alt_term] = @stanzas[:terms][id] if !@stanzas[:terms].include?(alt_term)
|
542
|
+
@ancestors_index[alt_term] = @ancestors_index[id] if !@ancestors_index[id].nil?
|
543
|
+
end
|
544
|
+
end
|
545
|
+
end
|
546
|
+
end
|
547
|
+
@stanzas[:terms].merge!(alt_ids2add)
|
548
|
+
end
|
549
|
+
|
550
|
+
|
551
|
+
# Executes basic expansions of tags (alternatives, obsoletes and parentals) with default values
|
552
|
+
# ===== Returns
|
553
|
+
# true if eprocess ends without errors and false in other cases
|
554
|
+
def build_index()
|
555
|
+
self.get_index_obsoletes
|
556
|
+
self.get_index_alternatives
|
557
|
+
self.get_index_child_parent_relations
|
558
|
+
@alternatives_index.each{|k,v| @alternatives_index[k] = self.extract_id(v)}
|
559
|
+
## @alternatives_index.map {|k,v| @alternatives_index[k] = self.stanzas[:terms][v][:id] if k == v} unless self.stanzas[:terms].empty?
|
560
|
+
@alternatives_index.compact!
|
561
|
+
@obsoletes_index.each{|k,v| @obsoletes_index[k] = self.extract_id(v)}
|
562
|
+
@obsoletes_index.compact!
|
563
|
+
@ancestors_index.each{|k,v| @ancestors_index[k] = v.map{|t| self.extract_id(t)}.compact}
|
564
|
+
@ancestors_index.compact!
|
565
|
+
@descendants_index.each{|k,v| @descendants_index[k] = v.map{|t| self.extract_id(t)}.compact}
|
566
|
+
@descendants_index.compact!
|
567
|
+
self.get_index_frequencies
|
568
|
+
self.calc_dictionary(:name)
|
569
|
+
self.calc_dictionary(:synonym, select_regex: /\"(.*)\"/)
|
570
|
+
self.calc_term_levels(calc_paths: true)
|
571
|
+
end
|
572
|
+
|
573
|
+
|
574
|
+
# Calculates regular frequencies based on ontology structure (using parentals)
|
575
|
+
# ===== Returns
|
576
|
+
# true if everything end without errors and false in other cases
|
577
|
+
def get_index_frequencies()
|
578
|
+
# Check
|
579
|
+
if @ancestors_index.empty?
|
580
|
+
warn('ancestors_index object is empty')
|
581
|
+
else
|
582
|
+
# Per each term, add frequencies
|
583
|
+
@stanzas[:terms].each do |id, tags|
|
584
|
+
if @alternatives_index.include?(id)
|
585
|
+
alt_id = @alternatives_index[id]
|
586
|
+
query = @meta[alt_id] # Check if exist
|
587
|
+
if query.nil?
|
588
|
+
query = {ancestors: 0.0, descendants: 0.0, struct_freq: 0.0, observed_freq: 0.0}
|
589
|
+
@meta[alt_id] = query
|
590
|
+
end
|
591
|
+
@meta[id] = query
|
592
|
+
# Note: alternative terms do not increase structural frequencies
|
593
|
+
else # Official term
|
594
|
+
query = @meta[id] # Check if exist
|
595
|
+
if query.nil?
|
596
|
+
query = {ancestors: 0.0, descendants: 0.0, struct_freq: 0.0, observed_freq: 0.0}
|
597
|
+
@meta[id] = query
|
598
|
+
end
|
599
|
+
# Store metadata
|
600
|
+
query[:ancestors] = @ancestors_index.include?(id) ? @ancestors_index[id].count{|anc| !@alternatives_index.include?(anc)}.to_f : 0.0
|
601
|
+
query[:descendants] = @descendants_index.include?(id) ? @descendants_index[id].count{|desc| !@alternatives_index.include?(desc)}.to_f : 0.0
|
602
|
+
query[:struct_freq] = query[:descendants] + 1.0
|
603
|
+
# Update maximums
|
604
|
+
@max_freqs[:struct_freq] = query[:struct_freq] if @max_freqs[:struct_freq] < query[:struct_freq]
|
605
|
+
@max_freqs[:max_depth] = query[:descendants] if @max_freqs[:max_depth] < query[:descendants]
|
606
|
+
end
|
607
|
+
end
|
608
|
+
end
|
609
|
+
end
|
610
|
+
|
611
|
+
|
612
|
+
# Expand obsoletes set and link info to their alternative IDs
|
613
|
+
# ===== Parameters
|
614
|
+
# +obs_tags+:: tags to be used to find obsoletes
|
615
|
+
# +alt_tags+:: tags to find alternative IDs (if are available)
|
616
|
+
# +reset_obsoletes+:: flag to indicate if obsoletes set must be reset. Default: true
|
617
|
+
# ===== Returns
|
618
|
+
# true if process ends without errors and false in other cases
|
619
|
+
def get_index_obsoletes(obs_tag: @@basic_tags[:obsolete], alt_tags: @@basic_tags[:alternative])
|
620
|
+
if @stanzas[:terms].empty?
|
621
|
+
warn('stanzas terms empty')
|
622
|
+
else
|
623
|
+
# Check obsoletes
|
624
|
+
@stanzas[:terms].each do |id, term_tags|
|
625
|
+
next if term_tags.nil?
|
626
|
+
next if self.is_alternative?(id)
|
627
|
+
query = term_tags[obs_tag]
|
628
|
+
if !query.nil? && query == 'true' # Obsolete tag presence
|
629
|
+
next if !@obsoletes_index[id].nil? # Already stored
|
630
|
+
# Check if alternative value is available
|
631
|
+
alt_ids = alt_tags.map{|alt| term_tags[alt]}.compact
|
632
|
+
if !alt_ids.empty?
|
633
|
+
alt_id = alt_ids.first.first #FIRST tag, FIRST id
|
634
|
+
# Store
|
635
|
+
@alternatives_index[id] = alt_id
|
636
|
+
@obsoletes_index[id] = alt_id
|
637
|
+
end
|
638
|
+
end
|
639
|
+
end
|
640
|
+
end
|
641
|
+
end
|
642
|
+
|
643
|
+
|
644
|
+
# Expand parentals set and link all info to their alternative IDs. Also launch frequencies process
|
645
|
+
# ===== Parameters
|
646
|
+
# +tag+:: tag used to expand parentals
|
647
|
+
# +split_info_char+:: special regex used to split info (if it is necessary)
|
648
|
+
# +split_info_indx+:: special index to take splitted info (if it is necessary)
|
649
|
+
# ===== Returns
|
650
|
+
# true if process ends without errors and false in other cases
|
651
|
+
def get_index_child_parent_relations(tag: @@basic_tags[:ancestors][0])
|
652
|
+
# Check
|
653
|
+
if @stanzas[:terms].nil?
|
654
|
+
warn('stanzas terms empty')
|
655
|
+
else
|
656
|
+
# Expand
|
657
|
+
structType, parentals = self.class.get_related_ids_by_tag(terms: @stanzas[:terms],
|
658
|
+
target_tag: tag,
|
659
|
+
alt_ids: @alternatives_index,
|
660
|
+
obsoletes: @obsoletes_index.length)
|
661
|
+
# Check
|
662
|
+
raise('Error expanding parentals') if (structType.nil?) || parentals.nil?
|
663
|
+
# Prepare ancestors structure
|
664
|
+
anc = {}
|
665
|
+
des = {}
|
666
|
+
parentals.each do |id, parents|
|
667
|
+
parents = parents - @removable_terms
|
668
|
+
anc[id] = parents
|
669
|
+
parents.each do |anc_id| # Add descendants
|
670
|
+
if !des.include?(anc_id)
|
671
|
+
des[anc_id] = [id]
|
672
|
+
else
|
673
|
+
des[anc_id] << id
|
674
|
+
end
|
675
|
+
end
|
676
|
+
end
|
677
|
+
# Store alternatives
|
678
|
+
# @alternatives_index.each do |id,alt|
|
679
|
+
# anc[id] = anc[alt] if anc.include?(alt)
|
680
|
+
# des[id] = des[alt] if des.include?(alt)
|
681
|
+
# end
|
682
|
+
# Check structure
|
683
|
+
if ![:atomic,:sparse].include? structType
|
684
|
+
structType = structType == :circular ? :circular : :hierarchical
|
685
|
+
end
|
686
|
+
# Store
|
687
|
+
@ancestors_index = anc
|
688
|
+
@descendants_index = des
|
689
|
+
@structureType = structType
|
690
|
+
end
|
691
|
+
# Finish
|
692
|
+
end
|
693
|
+
|
694
|
+
|
695
|
+
# Find ancestors of a given term
|
696
|
+
# ===== Parameters
|
697
|
+
# +term+:: to be checked
|
698
|
+
# +filter_alternatives+:: if true, remove alternatives from final results
|
699
|
+
# ===== Returns
|
700
|
+
# an array with all ancestors of given term or false if parents are not available yet
|
701
|
+
def get_ancestors(term, filter_alternatives = false)
|
702
|
+
return self.get_familiar(term, true, filter_alternatives)
|
703
|
+
end
|
704
|
+
|
705
|
+
|
706
|
+
# Find descendants of a given term
|
707
|
+
# ===== Parameters
|
708
|
+
# +term+:: to be checked
|
709
|
+
# +filter_alternatives+:: if true, remove alternatives from final results
|
710
|
+
# ===== Returns
|
711
|
+
# an array with all descendants of given term or false if parents are not available yet
|
712
|
+
def get_descendants(term, filter_alternatives = false)
|
713
|
+
return self.get_familiar(term, false, filter_alternatives)
|
714
|
+
end
|
715
|
+
|
716
|
+
|
717
|
+
# Find ancestors/descendants of a given term
|
718
|
+
# ===== Parameters
|
719
|
+
# +term+:: to be checked
|
720
|
+
# +return_ancestors+:: return ancestors if true or descendants if false
|
721
|
+
# +filter_alternatives+:: if true, remove alternatives from final results
|
722
|
+
# ===== Returns
|
723
|
+
# an array with all ancestors/descendants of given term or nil if parents are not available yet
|
724
|
+
def get_familiar(term, return_ancestors = true, filter_alternatives = false)
|
725
|
+
# Find into parentals
|
726
|
+
familiars = return_ancestors ? @ancestors_index[term] : @descendants_index[term]
|
727
|
+
if !familiars.nil?
|
728
|
+
familiars = familiars.clone
|
729
|
+
if filter_alternatives
|
730
|
+
familiars.reject!{|fm| @alternatives_index.include?(fm)}
|
731
|
+
end
|
732
|
+
else
|
733
|
+
familiars = []
|
734
|
+
end
|
735
|
+
return familiars
|
736
|
+
end
|
737
|
+
|
738
|
+
|
739
|
+
# Obtain IC of an specific term
|
740
|
+
# ===== Parameters
|
741
|
+
# +term+:: which IC will be calculated
|
742
|
+
# +type+:: of IC to be calculated. Default: resnik
|
743
|
+
# +force+:: force re-calculate the IC. Do not check if it is already calculated
|
744
|
+
# +zhou_k+:: special coeficient for Zhou IC method
|
745
|
+
# ===== Returns
|
746
|
+
# the IC calculated
|
747
|
+
def get_IC(termRaw, type: :resnik, force: false, zhou_k: 0.5)
|
748
|
+
term = termRaw.to_sym
|
749
|
+
curr_ics = @ics[type]
|
750
|
+
# Check
|
751
|
+
raise ArgumentError, "IC type specified (#{type}) is not allowed" if !@@allowed_calcs[:ics].include?(type)
|
752
|
+
# Check if it's already calculated
|
753
|
+
return curr_ics[term] if (curr_ics.include? term) && !force
|
754
|
+
# Calculate
|
755
|
+
ic = - 1
|
756
|
+
term_meta = @meta[term]
|
757
|
+
case type # https://arxiv.org/ftp/arxiv/papers/1310/1310.8059.pdf ||| https://sci-hub.st/https://doi.org/10.1016/j.eswa.2012.01.082
|
758
|
+
###########################################
|
759
|
+
#### STRUCTURE BASED METRICS
|
760
|
+
###########################################
|
761
|
+
# Shortest path
|
762
|
+
# Weighted Link
|
763
|
+
# Hirst and St-Onge Measure
|
764
|
+
# Wu and Palmer
|
765
|
+
# Slimani
|
766
|
+
# Li
|
767
|
+
# Leacock and Chodorow
|
768
|
+
###########################################
|
769
|
+
#### INFORMATION CONTENT METRICS
|
770
|
+
###########################################
|
771
|
+
when :resnik # Resnik P: Using Information Content to Evaluate Semantic Similarity in a Taxonomy
|
772
|
+
# -log(Freq(x) / Max_Freq)
|
773
|
+
ic = -Math.log10(term_meta[:struct_freq].fdiv(@max_freqs[:struct_freq]))
|
774
|
+
when :resnik_observed
|
775
|
+
# -log(Freq(x) / Max_Freq)
|
776
|
+
ic = -Math.log10(term_meta[:observed_freq].fdiv(@max_freqs[:observed_freq]))
|
777
|
+
# Lin
|
778
|
+
# Jiang & Conrath
|
779
|
+
|
780
|
+
###########################################
|
781
|
+
#### FEATURE-BASED METRICS
|
782
|
+
###########################################
|
783
|
+
# Tversky
|
784
|
+
# x-similarity
|
785
|
+
# Rodirguez
|
786
|
+
|
787
|
+
###########################################
|
788
|
+
#### HYBRID METRICS
|
789
|
+
###########################################
|
790
|
+
when :seco, :zhou # SECO:: An intrinsic information content metric for semantic similarity in WordNet
|
791
|
+
# 1 - ( log(hypo(x) + 1) / log(max_nodes) )
|
792
|
+
ic = 1 - Math.log10(term_meta[:struct_freq]).fdiv(Math.log10(@stanzas[:terms].length - @alternatives_index.length))
|
793
|
+
if :zhou # New Model of Semantic Similarity Measuring in Wordnet
|
794
|
+
# k*(IC_Seco(x)) + (1-k)*(log(depth(x))/log(max_depth))
|
795
|
+
@ics[:seco][term] = ic # Special store
|
796
|
+
ic = zhou_k * ic + (1.0 - zhou_k) * (Math.log10(term_meta[:descendants]).fdiv(Math.log10(@max_freqs[:max_depth])))
|
797
|
+
end
|
798
|
+
when :sanchez # Semantic similarity estimation in the biomedical domain: An ontology-basedinformation-theoretic perspective
|
799
|
+
ic = -Math.log10((term_meta[:descendants].fdiv(term_meta[:ancestors]) + 1.0).fdiv(@max_freqs[:max_depth] + 1.0))
|
800
|
+
# Knappe
|
801
|
+
end
|
802
|
+
curr_ics[term] = ic
|
803
|
+
return ic
|
804
|
+
end
|
805
|
+
|
806
|
+
|
807
|
+
# Calculates and return resnik ICs (by ontology and observed frequency) for observed terms
|
808
|
+
# ===== Returns
|
809
|
+
# two hashes with resnik and resnik_observed ICs for observed terms
|
810
|
+
def get_observed_ics_by_onto_and_freq
|
811
|
+
# Chech there are observed terms
|
812
|
+
if @profiles.empty?
|
813
|
+
resnik = {}
|
814
|
+
resnik_observed = {}
|
815
|
+
else
|
816
|
+
# Calc ICs for all terms
|
817
|
+
observed_terms = @profiles.values.flatten.uniq
|
818
|
+
observed_terms.each{ |term| get_IC(term)}
|
819
|
+
observed_terms.each{ |term| get_IC(term, type: :resnik_observed)}
|
820
|
+
resnik = @ics[:resnik].select{|k,v| observed_terms.include?(k)}
|
821
|
+
resnik_observed = @ics[:resnik_observed].select{|k,v| observed_terms.include?(k)}
|
822
|
+
end
|
823
|
+
return resnik.clone, resnik_observed.clone
|
824
|
+
end
|
825
|
+
|
826
|
+
|
827
|
+
# Find the IC of the Most Index Content shared Ancestor (MICA) of two given terms
|
828
|
+
# ===== Parameters
|
829
|
+
# +termA+:: term to be cheked
|
830
|
+
# +termB+:: term to be checked
|
831
|
+
# +ic_type+:: IC formula to be used
|
832
|
+
# ===== Returns
|
833
|
+
# the IC of the MICA(termA,termB)
|
834
|
+
def get_ICMICA(termA, termB, ic_type = :resnik)
|
835
|
+
term, ic = self.get_MICA(termA, termB, ic_type)
|
836
|
+
return term.nil? ? nil : ic
|
837
|
+
end
|
838
|
+
|
839
|
+
|
840
|
+
# Find the Most Index Content shared Ancestor (MICA) of two given terms
|
841
|
+
# ===== Parameters
|
842
|
+
# +termA+:: term to be cheked
|
843
|
+
# +termB+:: term to be checked
|
844
|
+
# +ic_type+:: IC formula to be used
|
845
|
+
# ===== Returns
|
846
|
+
# the MICA(termA,termB) and it's IC
|
847
|
+
def get_MICA(termA, termB, ic_type = :resnik)
|
848
|
+
termA = @alternatives_index[termA] if @alternatives_index.include?(termA)
|
849
|
+
termB = @alternatives_index[termB] if @alternatives_index.include?(termB)
|
850
|
+
mica = [nil,-1.0]
|
851
|
+
# Special case
|
852
|
+
if termA.eql?(termB)
|
853
|
+
ic = self.get_IC(termA, type: ic_type)
|
854
|
+
mica = [termA, ic]
|
855
|
+
else
|
856
|
+
# Obtain ancestors (include itselfs too)
|
857
|
+
anc_A = self.get_ancestors(termA)
|
858
|
+
anc_B = self.get_ancestors(termB)
|
859
|
+
if !(anc_A.empty? && anc_B.empty?)
|
860
|
+
anc_A << termA
|
861
|
+
anc_B << termB
|
862
|
+
(anc_A & anc_B).each do |anc| # Find MICA in shared ancestors
|
863
|
+
ic = self.get_IC(anc, type: ic_type)
|
864
|
+
mica = [anc,ic] if ic > mica[1]
|
865
|
+
end
|
866
|
+
end
|
867
|
+
end
|
868
|
+
return mica
|
869
|
+
end
|
870
|
+
|
871
|
+
|
872
|
+
# Calculate similarity between two given terms
|
873
|
+
# ===== Parameters
|
874
|
+
# +termsA+:: to be compared
|
875
|
+
# +termsB+:: to be compared
|
876
|
+
# +type+:: similitude formula to be used
|
877
|
+
# +ic_type+:: IC formula to be used
|
878
|
+
# ===== Returns
|
879
|
+
# the similarity between both sets or false if frequencies are not available yet
|
880
|
+
def get_similarity(termA, termB, type: :resnik, ic_type: :resnik)
|
881
|
+
# Check
|
882
|
+
raise ArgumentError, "SIM type specified (#{type}) is not allowed" if !@@allowed_calcs[:sims].include?(type)
|
883
|
+
sim = nil
|
884
|
+
mica, sim_res = get_MICA(termA, termB, ic_type)
|
885
|
+
if !mica.nil?
|
886
|
+
case type
|
887
|
+
when :resnik
|
888
|
+
sim = sim_res
|
889
|
+
when :lin
|
890
|
+
sim = (2.0 * sim_res).fdiv(self.get_IC(termA,type: ic_type) + self.get_IC(termB,type: ic_type))
|
891
|
+
when :jiang_conrath # This is not a similarity, this is a disimilarity (distance)
|
892
|
+
sim = (self.get_IC(termA, type: ic_type) + self.get_IC(termB, type: ic_type)) - (2.0 * sim_res)
|
893
|
+
end
|
894
|
+
end
|
895
|
+
return sim
|
896
|
+
end
|
897
|
+
|
898
|
+
|
899
|
+
# Method used to load information stored into an OBO file and store it into this object.
|
900
|
+
# If a file is specified by input parameter, current @file value is updated
|
901
|
+
# ===== Parameters
|
902
|
+
# +file+:: optional file to update object stored file
|
903
|
+
def load(file, build: true)
|
904
|
+
_, header, stanzas = self.class.load_obo(file)
|
905
|
+
@header = header
|
906
|
+
@stanzas = stanzas
|
907
|
+
self.remove_removable()
|
908
|
+
# @removable_terms.each{|removableID| @stanzas[:terms].delete(removableID)} if !@removable_terms.empty? # Remove if proceed
|
909
|
+
self.build_index() if build
|
910
|
+
end
|
911
|
+
|
912
|
+
#
|
913
|
+
def remove_removable()
|
914
|
+
@removable_terms.each{|removableID| @stanzas[:terms].delete(removableID)} if !@removable_terms.empty? # Remove if proceed
|
915
|
+
end
|
916
|
+
|
917
|
+
|
918
|
+
# Exports an OBO_Handler object in json format
|
919
|
+
# ===== Parameters
|
920
|
+
# +file+:: where info will be stored
|
921
|
+
def write(file)
|
922
|
+
# Take object stored info
|
923
|
+
obj_info = {header: @header,
|
924
|
+
stanzas: @stanzas,
|
925
|
+
ancestors_index: @ancestors_index,
|
926
|
+
descendants_index: @descendants_index,
|
927
|
+
alternatives_index: @alternatives_index,
|
928
|
+
obsoletes_index: @obsoletes_index,
|
929
|
+
structureType: @structureType,
|
930
|
+
ics: @ics,
|
931
|
+
meta: @meta,
|
932
|
+
special_tags: @special_tags,
|
933
|
+
max_freqs: @max_freqs,
|
934
|
+
dicts: @dicts,
|
935
|
+
profiles: @profiles,
|
936
|
+
profilesDict: @profilesDict,
|
937
|
+
items: @items,
|
938
|
+
removable_terms: @removable_terms,
|
939
|
+
term_paths: @term_paths}
|
940
|
+
# Convert to JSON format & write
|
941
|
+
File.open(file, "w") { |f| f.write obj_info.to_json }
|
942
|
+
end
|
943
|
+
|
944
|
+
|
945
|
+
def is_number? string
|
946
|
+
true if Float(string) rescue false
|
947
|
+
end
|
948
|
+
|
949
|
+
|
950
|
+
# Read a JSON file with an OBO_Handler object stored
|
951
|
+
# ===== Parameters
|
952
|
+
# +file+:: with object info
|
953
|
+
# +file+:: if true, calculate indexes. Default: true
|
954
|
+
# ===== Return
|
955
|
+
# OBO_Handler internal fields
|
956
|
+
def read(file, build: true)
|
957
|
+
# Read file
|
958
|
+
jsonFile = File.open(file)
|
959
|
+
jsonInfo = JSON.parse(jsonFile.read, :symbolize_names => true)
|
960
|
+
# Pre-process (Symbolize some hashs values)
|
961
|
+
if !jsonInfo[:header].nil?
|
962
|
+
aux = jsonInfo[:header].map do |entry,info|
|
963
|
+
if info.kind_of?(Array) && @@symbolizable_ids.include?(entry)
|
964
|
+
[entry,info.map{|item| item.to_sym}]
|
965
|
+
else
|
966
|
+
[entry,info]
|
967
|
+
end
|
968
|
+
end
|
969
|
+
jsonInfo[:header] = aux.to_h
|
970
|
+
end
|
971
|
+
jsonInfo[:stanzas][:terms].map{|id,info| self.class.symbolize_ids(info)} # STANZAS
|
972
|
+
jsonInfo[:stanzas][:typedefs].map{|id,info| self.class.symbolize_ids(info)}
|
973
|
+
jsonInfo[:stanzas][:instances].map{|id,info| self.class.symbolize_ids(info)}
|
974
|
+
# Optional
|
975
|
+
jsonInfo[:alternatives_index] = jsonInfo[:alternatives_index].map{|id,value| [id, value.to_sym]}.to_h unless jsonInfo[:alternatives_index].nil?
|
976
|
+
jsonInfo[:ancestors_index].map {|id,family_arr| family_arr.map!{|item| item.to_sym}} unless jsonInfo[:ancestors_index].nil?
|
977
|
+
jsonInfo[:descendants_index].map {|id,family_arr| family_arr.map!{|item| item.to_sym}} unless jsonInfo[:descendants_index].nil?
|
978
|
+
jsonInfo[:obsoletes_index] = jsonInfo[:obsoletes_index].map{|id,value| [id, value.to_sym]}.to_h unless jsonInfo[:obsoletes_index].nil?
|
979
|
+
jsonInfo[:dicts] = jsonInfo[:dicts].each do |flag, dictionaries|
|
980
|
+
next if dictionaries.nil?
|
981
|
+
# Special case: byTerm
|
982
|
+
dictionaries[:byTerm] = dictionaries[:byTerm].map do |term, value|
|
983
|
+
if !term.to_s.scan(/\A[-+]?[0-9]*\.?[0-9]+\Z/).empty? # Numeric dictionary
|
984
|
+
[term.to_s.to_i, value.map{|term| term.to_sym}]
|
985
|
+
elsif value.is_a? Numeric # Numeric dictionary
|
986
|
+
[term.to_sym, value]
|
987
|
+
elsif value.kind_of?(Array) && flag == :is_a
|
988
|
+
[term.to_sym, value.map{|v| v.to_sym}]
|
989
|
+
else
|
990
|
+
[term.to_sym, value]
|
991
|
+
end
|
992
|
+
end
|
993
|
+
dictionaries[:byTerm] = dictionaries[:byTerm].to_h
|
994
|
+
# By value
|
995
|
+
dictionaries[:byValue] = dictionaries[:byValue].map do |value, term|
|
996
|
+
if value.is_a? Numeric # Numeric dictionary
|
997
|
+
[value, term.to_sym]
|
998
|
+
elsif term.is_a? Numeric # Numeric dictionary
|
999
|
+
[value.to_s.to_sym, term]
|
1000
|
+
elsif flag == :is_a
|
1001
|
+
[value.to_sym, term.map{|v| v.to_sym}]
|
1002
|
+
elsif term.kind_of?(Array)
|
1003
|
+
[value.to_sym, term.map{|t| t.to_sym}]
|
1004
|
+
else
|
1005
|
+
[value.to_s, term.to_sym]
|
1006
|
+
end
|
1007
|
+
end
|
1008
|
+
dictionaries[:byValue] = dictionaries[:byValue].to_h
|
1009
|
+
end
|
1010
|
+
if !jsonInfo[:profiles].nil?
|
1011
|
+
jsonInfo[:profiles].map{|id,terms| terms.map!{|term| term.to_sym}}
|
1012
|
+
jsonInfo[:profiles].keys.map{|id| jsonInfo[:profiles][id.to_s.to_i] = jsonInfo[:profiles].delete(id) if self.is_number?(id.to_s)}
|
1013
|
+
end
|
1014
|
+
jsonInfo[:profilesDict].map{|term,ids| ids.map!{|id| id.to_sym if !id.is_a?(Numeric)}} unless jsonInfo[:profilesDict].nil?
|
1015
|
+
jsonInfo[:removable_terms] = jsonInfo[:removable_terms].map{|term| term.to_sym} unless jsonInfo[:removable_terms].nil?
|
1016
|
+
jsonInfo[:special_tags] = jsonInfo[:special_tags].each do |k, v|
|
1017
|
+
next if v.nil?
|
1018
|
+
if v.kind_of?(Array)
|
1019
|
+
jsonInfo[:special_tags][k] = v.map{|tag| tag.to_sym}
|
1020
|
+
else
|
1021
|
+
jsonInfo[:special_tags][k] = v.to_sym
|
1022
|
+
end
|
1023
|
+
end
|
1024
|
+
jsonInfo[:items].each{|k,v| jsonInfo[:items][k] = v.map{|item| item.to_sym}} unless jsonInfo[:items].nil?
|
1025
|
+
jsonInfo[:term_paths].each{|term,info| jsonInfo[:term_paths][term][:paths] = info[:paths].map{|path| path.map{|t| t.to_sym}}} unless jsonInfo[:term_paths].nil?
|
1026
|
+
|
1027
|
+
# Store info
|
1028
|
+
@header = jsonInfo[:header]
|
1029
|
+
@stanzas = jsonInfo[:stanzas]
|
1030
|
+
@ancestors_index = jsonInfo[:ancestors_index]
|
1031
|
+
@descendants_index = jsonInfo[:descendants_index]
|
1032
|
+
@alternatives_index = jsonInfo[:alternatives_index]
|
1033
|
+
@obsoletes_index = jsonInfo[:obsoletes_index]
|
1034
|
+
jsonInfo[:structureType] = jsonInfo[:structureType].to_sym unless jsonInfo[:structureType].nil?
|
1035
|
+
@structureType = jsonInfo[:structureType]
|
1036
|
+
@ics = jsonInfo[:ics]
|
1037
|
+
@meta = jsonInfo[:meta]
|
1038
|
+
@special_tags = jsonInfo[:special_tags]
|
1039
|
+
@max_freqs = jsonInfo[:max_freqs]
|
1040
|
+
@dicts = jsonInfo[:dicts]
|
1041
|
+
@profiles = jsonInfo[:profiles]
|
1042
|
+
@profilesDict = jsonInfo[:profilesDict]
|
1043
|
+
@items = jsonInfo[:items]
|
1044
|
+
@removable_terms = jsonInfo[:removable_terms]
|
1045
|
+
@term_paths = jsonInfo[:term_paths]
|
1046
|
+
|
1047
|
+
self.build_index() if build
|
1048
|
+
end
|
1049
|
+
|
1050
|
+
|
1051
|
+
# Check if a given ID is stored as term into this object
|
1052
|
+
# ===== Parameters
|
1053
|
+
# +id+:: to be checked
|
1054
|
+
# ===== Return
|
1055
|
+
# True if term is allowed or false in other cases
|
1056
|
+
def exists? id
|
1057
|
+
return stanzas[:terms].include?(id)
|
1058
|
+
end
|
1059
|
+
|
1060
|
+
|
1061
|
+
# This method assumes that a text given contains an allowed ID. And will try to obtain it splitting it
|
1062
|
+
# ===== Parameters
|
1063
|
+
# +text+:: to be checked
|
1064
|
+
# ===== Return
|
1065
|
+
# The correct ID if it can be found or nil in other cases
|
1066
|
+
def extract_id(text, splitBy: ' ')
|
1067
|
+
if self.exists?(text)
|
1068
|
+
return text
|
1069
|
+
else
|
1070
|
+
splittedText = text.to_s.split(splitBy).first.to_sym
|
1071
|
+
return self.exists?(splittedText) ? splittedText : nil
|
1072
|
+
end
|
1073
|
+
end
|
1074
|
+
|
1075
|
+
|
1076
|
+
# Generate a bidirectinal dictionary set using a specific tag and terms stanzas set
|
1077
|
+
# This functions stores calculated dictionary into @dicts field.
|
1078
|
+
# This functions stores first value for multivalue tags
|
1079
|
+
# This function does not handle synonyms for byValue dictionaries
|
1080
|
+
# ===== Parameters
|
1081
|
+
# +tag+:: to be used to calculate dictionary
|
1082
|
+
# +select_regex+:: gives a regfex that can be used to modify value to be stored
|
1083
|
+
# +substitute_alternatives+:: flag used to indicate if alternatives must, or not, be replaced by it official ID
|
1084
|
+
# +store_tag+:: flag used to store dictionary. If nil, mandatory tag given will be used
|
1085
|
+
# +multiterm+:: if true, byValue will allows multi-term linkage (array)
|
1086
|
+
# +self_type_references+:: if true, program assumes that refrences will be between Ontology terms, and it term IDs will be checked
|
1087
|
+
# ===== Return
|
1088
|
+
# void. And stores calcualted bidirectional dictonary into dictionaries main container
|
1089
|
+
def calc_dictionary(tag, select_regex: nil, substitute_alternatives: true, store_tag: nil, multiterm: false, self_type_references: false)
|
1090
|
+
tag = tag.to_sym
|
1091
|
+
store_tag = tag if store_tag.nil?
|
1092
|
+
if @stanzas[:terms].empty?
|
1093
|
+
warn('Terms are not already loaded. Aborting dictionary calc')
|
1094
|
+
else
|
1095
|
+
byTerm = {}
|
1096
|
+
byValue = {}
|
1097
|
+
# Calc per term
|
1098
|
+
@stanzas[:terms].each do |term, tags|
|
1099
|
+
referenceTerm = term
|
1100
|
+
if @alternatives_index.include?(term) && substitute_alternatives # Special case
|
1101
|
+
referenceTerm = @alternatives_index[term] if !@obsoletes_index.include?(@alternatives_index[term])
|
1102
|
+
end
|
1103
|
+
queryTag = tags[tag]
|
1104
|
+
if !queryTag.nil?
|
1105
|
+
# Pre-process
|
1106
|
+
if !select_regex.nil?
|
1107
|
+
if queryTag.kind_of?(Array)
|
1108
|
+
queryTag = queryTag.map{|value| value.scan(select_regex).first}
|
1109
|
+
queryTag.flatten!
|
1110
|
+
else
|
1111
|
+
queryTag = queryTag.scan(select_regex).first
|
1112
|
+
end
|
1113
|
+
queryTag.compact!
|
1114
|
+
end
|
1115
|
+
if queryTag.kind_of?(Array) # Store
|
1116
|
+
if !queryTag.empty?
|
1117
|
+
if byTerm.include?(referenceTerm)
|
1118
|
+
byTerm[referenceTerm] = (byTerm[referenceTerm] + queryTag).uniq
|
1119
|
+
else
|
1120
|
+
byTerm[referenceTerm] = queryTag
|
1121
|
+
end
|
1122
|
+
if multiterm
|
1123
|
+
queryTag.each do |value|
|
1124
|
+
byValue[value] = [] if byValue[value].nil?
|
1125
|
+
byValue[value] << referenceTerm
|
1126
|
+
end
|
1127
|
+
else
|
1128
|
+
queryTag.each{|value| byValue[value] = referenceTerm}
|
1129
|
+
end
|
1130
|
+
end
|
1131
|
+
else
|
1132
|
+
if byTerm.include?(referenceTerm)
|
1133
|
+
byTerm[referenceTerm] = (byTerm[referenceTerm] + [queryTag]).uniq
|
1134
|
+
else
|
1135
|
+
byTerm[referenceTerm] = [queryTag]
|
1136
|
+
end
|
1137
|
+
if multiterm
|
1138
|
+
byValue[queryTag] = [] if byValue[queryTag].nil?
|
1139
|
+
byValue[queryTag] << referenceTerm
|
1140
|
+
else
|
1141
|
+
byValue[queryTag] = referenceTerm
|
1142
|
+
end
|
1143
|
+
end
|
1144
|
+
end
|
1145
|
+
end
|
1146
|
+
|
1147
|
+
# Check self-references
|
1148
|
+
if self_type_references
|
1149
|
+
byTerm.map do |term, references|
|
1150
|
+
corrected_references = references.map do |t|
|
1151
|
+
checked = self.extract_id(t)
|
1152
|
+
if checked.nil?
|
1153
|
+
t
|
1154
|
+
else
|
1155
|
+
byValue[checked] = byValue.delete(t) if checked != t && byValue[checked].nil? # Update in byValue
|
1156
|
+
checked
|
1157
|
+
end
|
1158
|
+
end
|
1159
|
+
byTerm[term] = corrected_references.uniq
|
1160
|
+
end
|
1161
|
+
end
|
1162
|
+
|
1163
|
+
# Check order
|
1164
|
+
byTerm.map do |term,values|
|
1165
|
+
if self.exists?(term)
|
1166
|
+
referenceValue = @stanzas[:terms][term][tag]
|
1167
|
+
if !referenceValue.nil?
|
1168
|
+
if !select_regex.nil?
|
1169
|
+
if referenceValue.kind_of?(Array)
|
1170
|
+
referenceValue = referenceValue.map{|value| value.scan(select_regex).first}
|
1171
|
+
referenceValue.flatten!
|
1172
|
+
else
|
1173
|
+
referenceValue = referenceValue.scan(select_regex).first
|
1174
|
+
end
|
1175
|
+
referenceValue.compact!
|
1176
|
+
end
|
1177
|
+
if self_type_references
|
1178
|
+
if referenceValue.kind_of?(Array)
|
1179
|
+
aux = referenceValue.map{|t| self.extract_id(t)}
|
1180
|
+
else
|
1181
|
+
aux = self.extract_id(referenceValue)
|
1182
|
+
end
|
1183
|
+
aux.compact! unless aux.nil?
|
1184
|
+
referenceValue = aux unless aux.nil?
|
1185
|
+
end
|
1186
|
+
referenceValue = [referenceValue] if !referenceValue.kind_of?(Array)
|
1187
|
+
byTerm[term] = referenceValue + (values - referenceValue)
|
1188
|
+
end
|
1189
|
+
end
|
1190
|
+
end
|
1191
|
+
|
1192
|
+
# Store
|
1193
|
+
@dicts[store_tag] = {byTerm: byTerm, byValue: byValue}
|
1194
|
+
end
|
1195
|
+
end
|
1196
|
+
|
1197
|
+
|
1198
|
+
# Calculates :is_a dictionary without alternatives substitution
|
1199
|
+
def calc_ancestors_dictionary
|
1200
|
+
self.calc_dictionary(:is_a, substitute_alternatives: false, self_type_references: true, multiterm: true)
|
1201
|
+
end
|
1202
|
+
|
1203
|
+
|
1204
|
+
# Translate a given value using an already calcualted dictionary
|
1205
|
+
# ===== Parameters
|
1206
|
+
# +toTranslate+:: value to be translated using dictiontionary
|
1207
|
+
# +tag+:: used to generate the dictionary
|
1208
|
+
# +byValue+:: boolean flag to indicate if dictionary must be used values as keys or terms as keys. Default: values as keys = true
|
1209
|
+
# ===== Return
|
1210
|
+
# translation
|
1211
|
+
def translate(toTranslate, tag, byValue: true)
|
1212
|
+
dict = byValue ? @dicts[tag][:byValue] : @dicts[tag][:byTerm]
|
1213
|
+
toTranslate = get_main_id(toTranslate) if !byValue
|
1214
|
+
return dict[toTranslate]
|
1215
|
+
end
|
1216
|
+
|
1217
|
+
|
1218
|
+
# Translate a name given
|
1219
|
+
# ===== Parameters
|
1220
|
+
# +name+:: to be translated
|
1221
|
+
# ===== Return
|
1222
|
+
# translated name or nil if it's not stored into this ontology
|
1223
|
+
def translate_name(name)
|
1224
|
+
term = self.translate(name, :name)
|
1225
|
+
term = self.translate(name, :synonym) if term.nil?
|
1226
|
+
return term
|
1227
|
+
end
|
1228
|
+
|
1229
|
+
|
1230
|
+
# Translate several names and return translations and a list of names which couldn't be translated
|
1231
|
+
# ===== Parameters
|
1232
|
+
# +names+:: array to be translated
|
1233
|
+
# ===== Return
|
1234
|
+
# two arrays with translations and names which couldn't be translated respectively
|
1235
|
+
def translate_names(names)
|
1236
|
+
translated = []
|
1237
|
+
rejected = []
|
1238
|
+
names.each do |name|
|
1239
|
+
tr = self.translate_name(name)
|
1240
|
+
if tr.nil?
|
1241
|
+
rejected << name
|
1242
|
+
else
|
1243
|
+
translated << tr
|
1244
|
+
end
|
1245
|
+
end
|
1246
|
+
return translated, rejected
|
1247
|
+
end
|
1248
|
+
|
1249
|
+
|
1250
|
+
# Translates a given ID to it assigned name
|
1251
|
+
# ===== Parameters
|
1252
|
+
# +id+:: to be translated
|
1253
|
+
# ===== Return
|
1254
|
+
# main name or nil if it's not included into this ontology
|
1255
|
+
def translate_id(id)
|
1256
|
+
name = self.translate(id, :name, byValue: false)
|
1257
|
+
return name.nil? ? nil : name.first
|
1258
|
+
end
|
1259
|
+
|
1260
|
+
|
1261
|
+
# Translates several IDs and returns translations and not allowed IDs list
|
1262
|
+
# ===== Parameters
|
1263
|
+
# +ids+:: to be translated
|
1264
|
+
# ===== Return
|
1265
|
+
# two arrays with translations and names which couldn't be translated respectively
|
1266
|
+
def translate_ids(ids)
|
1267
|
+
translated = []
|
1268
|
+
rejected = []
|
1269
|
+
ids.each do |term_id|
|
1270
|
+
tr = self.translate_id(term_id.to_sym)
|
1271
|
+
if !tr.nil?
|
1272
|
+
translated << tr
|
1273
|
+
else
|
1274
|
+
rejected << tr
|
1275
|
+
end
|
1276
|
+
end
|
1277
|
+
return translated, rejected
|
1278
|
+
end
|
1279
|
+
|
1280
|
+
|
1281
|
+
# ===== Returns
|
1282
|
+
# the main ID assigned to a given ID. If it's a non alternative/obsolete ID itself will be returned
|
1283
|
+
# ===== Parameters
|
1284
|
+
# +id+:: to be translated
|
1285
|
+
# ===== Return
|
1286
|
+
# main ID related to a given ID. Returns nil if given ID is not an allowed ID
|
1287
|
+
def get_main_id(id)
|
1288
|
+
return nil if !@stanzas[:terms].include? id
|
1289
|
+
new_id = id
|
1290
|
+
mainID = @alternatives_index[id]
|
1291
|
+
new_id = mainID if !mainID.nil? & !@obsoletes_index.include?(mainID)
|
1292
|
+
return new_id
|
1293
|
+
end
|
1294
|
+
|
1295
|
+
|
1296
|
+
# Check a pull of IDs and return allowed IDs removing which are not official terms on this ontology
|
1297
|
+
# ===== Parameters
|
1298
|
+
# +ids+:: to be checked
|
1299
|
+
# ===== Return
|
1300
|
+
# two arrays whit allowed and rejected IDs respectively
|
1301
|
+
def check_ids(ids, substitute: true)
|
1302
|
+
checked_codes = []
|
1303
|
+
rejected_codes = []
|
1304
|
+
ids.each do |id|
|
1305
|
+
if @stanzas[:terms].include? id
|
1306
|
+
if substitute
|
1307
|
+
checked_codes << self.get_main_id(id)
|
1308
|
+
else
|
1309
|
+
checked_codes << id
|
1310
|
+
end
|
1311
|
+
else
|
1312
|
+
rejected_codes << id
|
1313
|
+
end
|
1314
|
+
end
|
1315
|
+
return checked_codes, rejected_codes
|
1316
|
+
end
|
1317
|
+
|
1318
|
+
|
1319
|
+
# Stores a given profile with an specific ID. If ID is already assigend to a profile, it will be replaced
|
1320
|
+
# ===== Parameters
|
1321
|
+
# +id+:: assigned to profile
|
1322
|
+
# +terms+:: array of terms
|
1323
|
+
# +substitute+:: subsstitute flag from check_ids
|
1324
|
+
def add_profile(id, terms, substitute: true)
|
1325
|
+
warn("Profile assigned to ID (#{id}) is going to be replaced") if @profiles.include? id
|
1326
|
+
correct_terms, rejected_terms = self.check_ids(terms, substitute: substitute)
|
1327
|
+
if !rejected_terms.empty?
|
1328
|
+
warn('Given terms contains erroneus IDs. These IDs will be removed')
|
1329
|
+
end
|
1330
|
+
if id.is_a? Numeric
|
1331
|
+
@profiles[id] = correct_terms
|
1332
|
+
else
|
1333
|
+
@profiles[id.to_sym] = correct_terms
|
1334
|
+
end
|
1335
|
+
end
|
1336
|
+
|
1337
|
+
|
1338
|
+
# Method used to store a pull of profiles
|
1339
|
+
# ===== Parameters
|
1340
|
+
# +profiles+:: array/hash of profiles to be stored. If it's an array, numerical IDs will be assigned starting at 1
|
1341
|
+
# +calc_metadata+:: if true, launch calc_profiles_dictionary process
|
1342
|
+
# +reset_stored+:: if true, remove already stored profiles
|
1343
|
+
# +substitute+:: subsstitute flag from check_ids
|
1344
|
+
def load_profiles(profiles, calc_metadata: true, reset_stored: false, substitute: false)
|
1345
|
+
self.reset_profiles if reset_stored
|
1346
|
+
# Check
|
1347
|
+
if profiles.kind_of?(Array)
|
1348
|
+
profiles.each_with_index do |items, i|
|
1349
|
+
self.add_profile(i, items.map {|item| item.to_sym}, substitute: substitute)
|
1350
|
+
end
|
1351
|
+
else # Hash
|
1352
|
+
if !profiles.keys.select{|id| @profiles.include?(id)}.empty?
|
1353
|
+
warn('Some profiles given are already stored. Stored version will be replaced')
|
1354
|
+
end
|
1355
|
+
profiles.each{|id, prof| self.add_profile(id, prof, substitute: substitute)}
|
1356
|
+
end
|
1357
|
+
|
1358
|
+
self.add_observed_terms_from_profiles(reset: true)
|
1359
|
+
|
1360
|
+
if calc_metadata
|
1361
|
+
self.calc_profiles_dictionary
|
1362
|
+
end
|
1363
|
+
end
|
1364
|
+
|
1365
|
+
|
1366
|
+
# Internal method used to remove already stored profiles and restore observed frequencies
|
1367
|
+
def reset_profiles
|
1368
|
+
# Clean profiles storage
|
1369
|
+
@profiles = {}
|
1370
|
+
# Reset frequency observed
|
1371
|
+
@meta.each{|term,info| info[:observed_freq] = 0}
|
1372
|
+
@max_freqs[:observed_freq] = 0
|
1373
|
+
end
|
1374
|
+
|
1375
|
+
|
1376
|
+
# ===== Returns
|
1377
|
+
# profiles assigned to a given ID
|
1378
|
+
# ===== Parameters
|
1379
|
+
# +id+:: profile ID
|
1380
|
+
# ===== Return
|
1381
|
+
# specific profile or nil if it's not stored
|
1382
|
+
def get_profile(id)
|
1383
|
+
return @profiles[id]
|
1384
|
+
end
|
1385
|
+
|
1386
|
+
|
1387
|
+
# ===== Returns
|
1388
|
+
# an array of sizes for all stored profiles
|
1389
|
+
# ===== Return
|
1390
|
+
# array of profile sizes
|
1391
|
+
def get_profiles_sizes()
|
1392
|
+
return @profiles.map{|id,terms| terms.length}
|
1393
|
+
end
|
1394
|
+
|
1395
|
+
|
1396
|
+
# ===== Returns
|
1397
|
+
# mean size of stored profiles
|
1398
|
+
# ===== Parameters
|
1399
|
+
# +round_digits+:: number of digits to round result. Default: 4
|
1400
|
+
# ===== Returns
|
1401
|
+
# mean size of stored profiles
|
1402
|
+
def get_profiles_mean_size(round_digits: 4)
|
1403
|
+
sizes = self.get_profiles_sizes
|
1404
|
+
return sizes.inject(0){|sum, n| sum + n}.fdiv(@profiles.length).round(round_digits)
|
1405
|
+
end
|
1406
|
+
|
1407
|
+
|
1408
|
+
# Calculates profiles sizes and returns size assigned to percentile given
|
1409
|
+
# ===== Parameters
|
1410
|
+
# +perc+:: percentile to be returned
|
1411
|
+
# +increasing_sort+:: flag to indicate if sizes order must be increasing. Default: false
|
1412
|
+
# ===== Returns
|
1413
|
+
# values assigned to percentile asked
|
1414
|
+
def get_profile_length_at_percentile(perc=50, increasing_sort: false)
|
1415
|
+
prof_lengths = self.get_profiles_sizes.sort
|
1416
|
+
prof_lengths.reverse! if !increasing_sort
|
1417
|
+
n_profiles = prof_lengths.length
|
1418
|
+
percentile_index = ((perc * (n_profiles - 1)).fdiv(100) - 0.5).round # Take length which not overpass percentile selected
|
1419
|
+
percentile_index = 0 if percentile_index < 0 # Special case (caused by literal calc)
|
1420
|
+
return prof_lengths[percentile_index]
|
1421
|
+
end
|
1422
|
+
|
1423
|
+
|
1424
|
+
# Translate a given profile to terms names
|
1425
|
+
# ===== Parameters
|
1426
|
+
# +prof+:: array of terms to be translated
|
1427
|
+
# ===== Returns
|
1428
|
+
# array of translated terms. Can include nils if some IDs are not allowed
|
1429
|
+
def profile_names(prof)
|
1430
|
+
return prof.map{|term| self.translate_id(term)}
|
1431
|
+
end
|
1432
|
+
|
1433
|
+
|
1434
|
+
# Trnaslates a bunch of profiles to it sets of term names
|
1435
|
+
# ===== Parameters
|
1436
|
+
# +profs+:: array of profiles
|
1437
|
+
# +asArray+:: flag to indicate if results must be returned as: true => an array of tuples [ProfID, ArrayOdNames] or ; false => hashs of translations
|
1438
|
+
# ===== Returns
|
1439
|
+
# translated profiles
|
1440
|
+
def translate_profiles_ids(profs = [], asArray: true)
|
1441
|
+
profs = @profiles if profs.empty?
|
1442
|
+
profs = profs.each_with_index.map{|terms, index| [index, terms]}.to_h if profs.kind_of?(Array)
|
1443
|
+
profs_names = profs.map{|id, terms| [id, self.profile_names(terms)]}.to_h
|
1444
|
+
return asArray ? profs_names.values : profs_names
|
1445
|
+
end
|
1446
|
+
|
1447
|
+
|
1448
|
+
# Includes as "observed_terms" all terms included into stored profiles
|
1449
|
+
# ===== Parameters
|
1450
|
+
# +reset+:: if true, reset observed freqs alreeady stored befor re-calculate
|
1451
|
+
def add_observed_terms_from_profiles(reset: false)
|
1452
|
+
@meta.each{|term, freqs| freqs[:observed_freq] = -1} if reset
|
1453
|
+
@profiles.each{|id, terms| self.add_observed_terms(terms: terms)}
|
1454
|
+
end
|
1455
|
+
|
1456
|
+
|
1457
|
+
# Get a term frequency
|
1458
|
+
# ===== Parameters
|
1459
|
+
# +term+:: term to be checked
|
1460
|
+
# +type+:: type of frequency to be returned. Allowed: [:struct_freq, :observed_freq]
|
1461
|
+
# ===== Returns
|
1462
|
+
# frequency of term given or nil if term is not allowed
|
1463
|
+
def get_frequency(term, type: :struct_freq)
|
1464
|
+
queryFreq = @meta[term]
|
1465
|
+
return queryFreq.nil? ? nil : queryFreq[type]
|
1466
|
+
end
|
1467
|
+
|
1468
|
+
|
1469
|
+
# Geys structural frequency of a term given
|
1470
|
+
# ===== Parameters
|
1471
|
+
# +term+:: to be checked
|
1472
|
+
# ===== Returns
|
1473
|
+
# structural frequency of given term or nil if term is not allowed
|
1474
|
+
def get_structural_frequency(term)
|
1475
|
+
return self.get_frequency(term, type: :struct_freq)
|
1476
|
+
end
|
1477
|
+
|
1478
|
+
|
1479
|
+
# Gets observed frequency of a term given
|
1480
|
+
# ===== Parameters
|
1481
|
+
# +term+:: to be checked
|
1482
|
+
# ===== Returns
|
1483
|
+
# observed frequency of given term or nil if term is not allowed
|
1484
|
+
def get_observed_frequency(term)
|
1485
|
+
return self.get_frequency(term, type: :observed_freq)
|
1486
|
+
end
|
1487
|
+
|
1488
|
+
|
1489
|
+
# Calculates frequencies of stored profiles terms
|
1490
|
+
# ===== Parameters
|
1491
|
+
# +ratio+:: if true, frequencies will be returned as ratios between 0 and 1.
|
1492
|
+
# +literal+:: if true, literal terms will be used to calculate frequencies instead translate alternative terms
|
1493
|
+
# +asArray+:: used to transform returned structure format from hash of Term-Frequency to an array of tuples [Term, Frequency]
|
1494
|
+
# +translate+:: if true, term IDs will be translated to
|
1495
|
+
# ===== Returns
|
1496
|
+
# stored profiles terms frequencies
|
1497
|
+
def get_profiles_terms_frequency(ratio: true, literal: true, asArray: true, translate: true)
|
1498
|
+
n_profiles = @profiles.length
|
1499
|
+
if literal
|
1500
|
+
freqs = {}
|
1501
|
+
@profiles.each do |id, terms|
|
1502
|
+
terms.each do |literalTerm|
|
1503
|
+
if freqs.include?(literalTerm)
|
1504
|
+
freqs[literalTerm] += 1
|
1505
|
+
else
|
1506
|
+
freqs[literalTerm] = 1
|
1507
|
+
end
|
1508
|
+
end
|
1509
|
+
end
|
1510
|
+
if (ratio || translate)
|
1511
|
+
aux_keys = freqs.keys
|
1512
|
+
aux_keys.each do |term|
|
1513
|
+
freqs[term] = freqs[term].fdiv(n_profiles) if ratio
|
1514
|
+
if translate
|
1515
|
+
tr = self.translate_id(term)
|
1516
|
+
freqs[tr] = freqs.delete(term) if !tr.nil?
|
1517
|
+
end
|
1518
|
+
end
|
1519
|
+
end
|
1520
|
+
if asArray
|
1521
|
+
freqs = freqs.map{|term, freq| [term, freq]}
|
1522
|
+
freqs.sort!{|h1, h2| h2[1] <=> h1[1]}
|
1523
|
+
end
|
1524
|
+
else # Freqs translating alternatives
|
1525
|
+
freqs = @meta.select{|id, freqs| freqs[:observed_freq] > 0}.map{|id, freqs| [id, ratio ? freqs[:observed_freq].fdiv(n_profiles) : freqs[:observed_freq]]}
|
1526
|
+
freqs = freqs.to_h if !asArray
|
1527
|
+
if translate
|
1528
|
+
freqs = freqs.map do |term, freq|
|
1529
|
+
tr = self.translate_id(term)
|
1530
|
+
tr.nil? ? [term, freq] : [tr, freq]
|
1531
|
+
end
|
1532
|
+
end
|
1533
|
+
if asArray
|
1534
|
+
freqs = freqs.map{|term, freq| [term, freq]}
|
1535
|
+
freqs.sort!{|h1, h2| h2[1] <=> h1[1]}
|
1536
|
+
else
|
1537
|
+
freqs = freqs.to_h
|
1538
|
+
end
|
1539
|
+
end
|
1540
|
+
return freqs
|
1541
|
+
end
|
1542
|
+
|
1543
|
+
|
1544
|
+
# Clean a given profile returning cleaned set of terms and removed ancestors term.
|
1545
|
+
# ===== Parameters
|
1546
|
+
# +prof+:: array of terms to be checked
|
1547
|
+
# ===== Returns
|
1548
|
+
# two arrays, first is the cleaned profile and second is the removed elements array
|
1549
|
+
def remove_ancestors_from_profile(prof)
|
1550
|
+
ancestors = prof.map{|term| self.get_ancestors(term)}.flatten.uniq
|
1551
|
+
redundant = prof.select{|term| ancestors.include?(term)}
|
1552
|
+
return prof - redundant, redundant
|
1553
|
+
end
|
1554
|
+
|
1555
|
+
|
1556
|
+
# Remove alternative IDs if official ID is present. DOES NOT REMOVE synonyms or alternative IDs of the same official ID
|
1557
|
+
# ===== Parameters
|
1558
|
+
# +prof+:: array of terms to be checked
|
1559
|
+
# ===== Returns
|
1560
|
+
# two arrays, first is the cleaned profile and second is the removed elements array
|
1561
|
+
def remove_alternatives_from_profile(prof)
|
1562
|
+
alternatives = prof.select{|term| @alternatives_index.include?(term)}
|
1563
|
+
redundant = alternatives.select{|alt_id| prof.include?(@alternatives_index[alt_id])}
|
1564
|
+
return prof - redundant, redundant
|
1565
|
+
end
|
1566
|
+
|
1567
|
+
|
1568
|
+
# Remove alternatives (if official term is present) and ancestors terms of a given profile
|
1569
|
+
# ===== Parameters
|
1570
|
+
# +profile+:: profile to be cleaned
|
1571
|
+
# +remove_alternatives+:: if true, clenaed profiles will replace already stored profiles
|
1572
|
+
# ===== Returns
|
1573
|
+
# cleaned profile
|
1574
|
+
def clean_profile(profile, remove_alternatives: true)
|
1575
|
+
warn('Estructure is circular, behaviour could not be which is expected') if @structureType == :circular
|
1576
|
+
terms_without_ancestors, _ = self.remove_ancestors_from_profile(profile)
|
1577
|
+
if remove_alternatives
|
1578
|
+
terms_without_ancestors_and_alternatices, _ = self.remove_alternatives_from_profile(terms_without_ancestors)
|
1579
|
+
else
|
1580
|
+
terms_without_ancestors_and_alternatices = terms_without_ancestors
|
1581
|
+
end
|
1582
|
+
return terms_without_ancestors_and_alternatices
|
1583
|
+
end
|
1584
|
+
|
1585
|
+
def clean_profile_hard(profile, options = {})
|
1586
|
+
profile, _ = check_ids(profile)
|
1587
|
+
profile = profile.select{|t| !is_obsolete?(t)}
|
1588
|
+
if !options[:term_filter].nil?
|
1589
|
+
profile.select! {|term| get_ancestors(term).include?(options[:term_filter])}
|
1590
|
+
end
|
1591
|
+
profile = clean_profile(profile.uniq)
|
1592
|
+
return profile
|
1593
|
+
end
|
1594
|
+
|
1595
|
+
# Remove terms from a given profile using hierarchical info and scores set given
|
1596
|
+
# ===== Parameters
|
1597
|
+
# +profile+:: profile to be cleaned
|
1598
|
+
# +scores+:: hash with terms by keys and numerical values (scores)
|
1599
|
+
# +byMax+:: if true, maximum scored term will be keeped, if false, minimum will be keeped
|
1600
|
+
# +remove_without_score+:: if true, terms without score will be removed. Default: true
|
1601
|
+
# ===== Returns
|
1602
|
+
# cleaned profile
|
1603
|
+
def clean_profile_by_score(profile, scores, byMax: true, remove_without_score: true)
|
1604
|
+
scores = scores.sort_by{|term,score| score}.to_h
|
1605
|
+
keep = profile.map do |term|
|
1606
|
+
if scores.include?(term)
|
1607
|
+
parentals = [self.get_ancestors(term), self.get_descendants(term)].flatten
|
1608
|
+
targetable = parentals.select{|parent| profile.include?(parent)}
|
1609
|
+
if targetable.empty?
|
1610
|
+
term
|
1611
|
+
else
|
1612
|
+
targetable << term
|
1613
|
+
targets = scores.select{|term,score| targetable.include?(term)}.to_h
|
1614
|
+
byMax ? targets.keys.last : targets.keys.first
|
1615
|
+
end
|
1616
|
+
elsif remove_without_score
|
1617
|
+
nil
|
1618
|
+
else
|
1619
|
+
term
|
1620
|
+
end
|
1621
|
+
end
|
1622
|
+
return keep.compact.uniq
|
1623
|
+
end
|
1624
|
+
|
1625
|
+
|
1626
|
+
# Remove alternatives (if official term is present) and ancestors terms of stored profiles
|
1627
|
+
# ===== Parameters
|
1628
|
+
# +store+:: if true, clenaed profiles will replace already stored profiles
|
1629
|
+
# +remove_alternatives+:: if true, clenaed profiles will replace already stored profiles
|
1630
|
+
# ===== Returns
|
1631
|
+
# a hash with cleaned profiles
|
1632
|
+
def clean_profiles(store: false, remove_alternatives: true)
|
1633
|
+
cleaned_profiles = {}
|
1634
|
+
@profiles.each{ |id, terms| cleaned_profiles[id] = self.clean_profile(terms, remove_alternatives: remove_alternatives)}
|
1635
|
+
@profiles = cleaned_profiles if store
|
1636
|
+
return cleaned_profiles
|
1637
|
+
end
|
1638
|
+
|
1639
|
+
|
1640
|
+
# Calculates number of ancestors present (redundant) in each profile stored
|
1641
|
+
# ===== Returns
|
1642
|
+
# array of parentals for each profile
|
1643
|
+
def parentals_per_profile
|
1644
|
+
cleaned_profiles = self.clean_profiles(remove_alternatives: false)
|
1645
|
+
parentals = @profiles.map{ |id, terms| terms.length - cleaned_profiles[id].length}
|
1646
|
+
return parentals
|
1647
|
+
end
|
1648
|
+
|
1649
|
+
|
1650
|
+
def get_profile_redundancy()
|
1651
|
+
profile_sizes = self.get_profiles_sizes
|
1652
|
+
parental_terms_per_profile = self.parentals_per_profile# clean_profiles
|
1653
|
+
parental_terms_per_profile = parental_terms_per_profile.map{|item| item[0]}
|
1654
|
+
profile_sizes, parental_terms_per_profile = profile_sizes.zip(parental_terms_per_profile).sort_by{|i| i.first}.reverse.transpose
|
1655
|
+
return profile_sizes, parental_terms_per_profile
|
1656
|
+
end
|
1657
|
+
|
1658
|
+
def compute_term_list_and_childs()
|
1659
|
+
suggested_childs = {}
|
1660
|
+
total_terms = 0
|
1661
|
+
terms_with_more_specific_childs = 0
|
1662
|
+
@profiles.each do |id, terms|
|
1663
|
+
total_terms += terms.length
|
1664
|
+
more_specific_childs = self.get_childs_table(terms, true)
|
1665
|
+
terms_with_more_specific_childs += more_specific_childs.select{|hpo_record| !hpo_record.last.empty?}.length #Exclude phenotypes with no childs
|
1666
|
+
suggested_childs[id] = more_specific_childs
|
1667
|
+
end
|
1668
|
+
return suggested_childs, terms_with_more_specific_childs.fdiv(total_terms)
|
1669
|
+
end
|
1670
|
+
|
1671
|
+
# Calculates mean IC of a given profile
|
1672
|
+
# ===== Parameters
|
1673
|
+
# +prof+:: profile to be checked
|
1674
|
+
# +ic_type+:: ic_type to be used
|
1675
|
+
# +zhou_k+:: special coeficient for Zhou IC method
|
1676
|
+
# ===== Returns
|
1677
|
+
# mean IC for a given profile
|
1678
|
+
def get_profile_mean_IC(prof, ic_type: :resnik, zhou_k: 0.5)
|
1679
|
+
return prof.map{|term| self.get_IC(term, type: ic_type, zhou_k: zhou_k)}.inject(0){|sum,x| sum + x}.fdiv(prof.length)
|
1680
|
+
end
|
1681
|
+
|
1682
|
+
|
1683
|
+
# Calculates resnik ontology, and resnik observed mean ICs for all profiles stored
|
1684
|
+
# ===== Returns
|
1685
|
+
# two hashes with Profiles and IC calculated for resnik and observed resnik respectively
|
1686
|
+
def get_profiles_resnik_dual_ICs
|
1687
|
+
struct_ics = {}
|
1688
|
+
observ_ics = {}
|
1689
|
+
@profiles.each do |id, terms|
|
1690
|
+
struct_ics[id] = self.get_profile_mean_IC(terms, ic_type: :resnik)
|
1691
|
+
observ_ics[id] = self.get_profile_mean_IC(terms, ic_type: :resnik_observed)
|
1692
|
+
end
|
1693
|
+
return struct_ics.clone, observ_ics.clone
|
1694
|
+
end
|
1695
|
+
|
1696
|
+
|
1697
|
+
# Calculates ontology structural levels for all ontology terms
|
1698
|
+
# ===== Parameters
|
1699
|
+
# +calc_paths+:: calculates term paths if it's not already calculated
|
1700
|
+
# +shortest_path+:: if true, level is calculated with shortest path, largest path will be used in other cases
|
1701
|
+
def calc_term_levels(calc_paths: false, shortest_path: true)
|
1702
|
+
if @term_paths.empty?
|
1703
|
+
if calc_paths
|
1704
|
+
self.calc_term_paths
|
1705
|
+
else
|
1706
|
+
warn('Term paths are not already loaded. Aborting dictionary calc')
|
1707
|
+
end
|
1708
|
+
end
|
1709
|
+
if !@term_paths.empty?
|
1710
|
+
byTerm = {}
|
1711
|
+
byValue = {}
|
1712
|
+
# Calc per term
|
1713
|
+
@term_paths.each do |term, info|
|
1714
|
+
level = shortest_path ? info[:shortest_path] : info[:largest_path]
|
1715
|
+
if level.nil?
|
1716
|
+
level = -1
|
1717
|
+
else
|
1718
|
+
level = level.round(0)
|
1719
|
+
end
|
1720
|
+
byTerm[term] = level
|
1721
|
+
queryLevels = byValue[level]
|
1722
|
+
if queryLevels.nil?
|
1723
|
+
byValue[level] = [term]
|
1724
|
+
else
|
1725
|
+
byValue[level] << term
|
1726
|
+
end
|
1727
|
+
end
|
1728
|
+
@dicts[:level] = {byTerm: byValue, byValue: byTerm} # Note: in this case, value has multiplicity and term is unique value
|
1729
|
+
# Update maximum depth
|
1730
|
+
@max_freqs[:max_depth] = byValue.keys.max
|
1731
|
+
end
|
1732
|
+
end
|
1733
|
+
|
1734
|
+
|
1735
|
+
# Check if a term given is marked as obsolete
|
1736
|
+
def is_obsolete? term
|
1737
|
+
return @obsoletes_index.include?(term)
|
1738
|
+
end
|
1739
|
+
|
1740
|
+
# Check if a term given is marked as alternative
|
1741
|
+
def is_alternative? term
|
1742
|
+
return @alternatives_index.include?(term)
|
1743
|
+
end
|
1744
|
+
|
1745
|
+
# Find paths of a term following it ancestors and stores all possible paths for it and it's parentals.
|
1746
|
+
# Also calculates paths metadata and stores into @term_paths
|
1747
|
+
def calc_term_paths(only_main_terms=false)
|
1748
|
+
self.calc_ancestors_dictionary if @dicts[:is_a].nil? # Calculate direct parentals dictionary if it's not already calculated
|
1749
|
+
visited_terms = {} # PEDRO: To keep track of visited data, hash accesions are fast than array includes. I don't understant why use this variable instead of check @term_paths to see if the data is calculated
|
1750
|
+
@term_paths = {}
|
1751
|
+
if [:hierarchical, :sparse].include? @structureType
|
1752
|
+
@stanzas[:terms].each do |term, t_attributes|
|
1753
|
+
if !only_main_terms && (self.is_obsolete?(term) || self.is_alternative?(term)) # Special case (obsoletes)
|
1754
|
+
special_term = term
|
1755
|
+
term = self.is_obsolete?(term) ? @obsoletes_index[term] : @alternatives_index[term]
|
1756
|
+
@term_paths[term] = {total_paths: 0, largest_path: 0, shortest_path: 0, paths: []} if !@term_paths.include?(term)
|
1757
|
+
@term_paths[special_term] = @term_paths[term]
|
1758
|
+
visited_terms[special_term] = true
|
1759
|
+
end
|
1760
|
+
if !visited_terms.include?(term)
|
1761
|
+
# PEDRO: This code is very similar to expand_path method, but cannot be replaced by it (test fail). We must work to use this method here
|
1762
|
+
path_attr = @term_paths[term]
|
1763
|
+
if path_attr.nil?
|
1764
|
+
path_attr = {total_paths: 0, largest_path: 0, shortest_path: 0, paths: []} # create new path data
|
1765
|
+
@term_paths[term] = path_attr #save path data container
|
1766
|
+
end
|
1767
|
+
parentals = @dicts[:is_a][:byTerm][term]
|
1768
|
+
if parentals.nil?
|
1769
|
+
path_attr[:paths] << [term]
|
1770
|
+
else
|
1771
|
+
parentals.each do |direct_parental|
|
1772
|
+
self.expand_path(direct_parental)
|
1773
|
+
new_paths = @term_paths[direct_parental][:paths]
|
1774
|
+
path_attr[:paths].concat(new_paths.map{|path| path.clone.unshift(term)})
|
1775
|
+
end
|
1776
|
+
end
|
1777
|
+
anc = @ancestors_index[term].each{|anc| visited_terms[anc] = true} if @ancestors_index.include?(term)
|
1778
|
+
visited_terms[term] = true
|
1779
|
+
end
|
1780
|
+
# Update metadata
|
1781
|
+
path_attr = @term_paths[term]
|
1782
|
+
path_attr[:total_paths] = path_attr[:paths].length
|
1783
|
+
paths_sizes = path_attr[:paths].map{|path| path.length}
|
1784
|
+
path_attr[:largest_path] = paths_sizes.max
|
1785
|
+
path_attr[:shortest_path] = paths_sizes.min
|
1786
|
+
end
|
1787
|
+
else
|
1788
|
+
warn('Ontology structure must be hierarchical or sparse to calculate term levels. Aborting paths calculation')
|
1789
|
+
end
|
1790
|
+
end
|
1791
|
+
|
1792
|
+
|
1793
|
+
# Recursive function whic finds paths of a term following it ancestors and stores all possible paths for it and it's parentals
|
1794
|
+
# ===== Parameters
|
1795
|
+
# +curr_term+:: current visited term
|
1796
|
+
# +visited_terms+:: already expanded terms
|
1797
|
+
def expand_path(curr_term)
|
1798
|
+
if !@term_paths.include?(curr_term)
|
1799
|
+
path_attr = {total_paths: 0, largest_path: 0, shortest_path: 0, paths: []}
|
1800
|
+
@term_paths[curr_term] = path_attr
|
1801
|
+
direct_parentals = @dicts[:is_a][:byTerm][curr_term]
|
1802
|
+
if direct_parentals.nil? # No parents :: End of recurrence
|
1803
|
+
path_attr[:paths] << [curr_term]
|
1804
|
+
else # Expand and concat
|
1805
|
+
direct_parentals.each do |ancestor|
|
1806
|
+
path_attr_parental = @term_paths[ancestor]
|
1807
|
+
if path_attr_parental.nil? # Calculate new paths
|
1808
|
+
self.expand_path(ancestor)
|
1809
|
+
new_paths = @term_paths[ancestor][:paths]
|
1810
|
+
else # Use direct_parental paths already calculated
|
1811
|
+
new_paths = path_attr_parental[:paths]
|
1812
|
+
end
|
1813
|
+
path_attr[:paths].concat(new_paths.map{|path| path.clone.unshift(curr_term)})
|
1814
|
+
end
|
1815
|
+
end
|
1816
|
+
end
|
1817
|
+
end
|
1818
|
+
|
1819
|
+
|
1820
|
+
# Gets ontology levels calculated
|
1821
|
+
# ===== Returns
|
1822
|
+
# ontology levels calculated
|
1823
|
+
def get_ontology_levels
|
1824
|
+
return @dicts[:level][:byTerm].clone # By term, in this case, is Key::Level, Value::Terms
|
1825
|
+
end
|
1826
|
+
|
1827
|
+
|
1828
|
+
# Gets ontology level of a specific term
|
1829
|
+
# ===== Returns
|
1830
|
+
# Term level
|
1831
|
+
def get_term_level(term)
|
1832
|
+
return @dicts[:level][:byValue][term]
|
1833
|
+
end
|
1834
|
+
|
1835
|
+
# nil, term not found, [] term exists but not has parents
|
1836
|
+
def get_parental_path(term, which_path = :shortest_path, level = 0)
|
1837
|
+
path = nil
|
1838
|
+
path_attr = @term_paths[term]
|
1839
|
+
if !path_attr.nil?
|
1840
|
+
path_length = path_attr[which_path]
|
1841
|
+
all_paths = path_attr[:paths]
|
1842
|
+
if all_paths.empty?
|
1843
|
+
path = []
|
1844
|
+
else
|
1845
|
+
path = all_paths.select{|pt| pt.length == path_length}.first.clone
|
1846
|
+
if level > 0 # we want the term and his ascendants until a specific level
|
1847
|
+
n_parents = path_length - level
|
1848
|
+
path = path[0..n_parents]
|
1849
|
+
end
|
1850
|
+
path.shift # Discard the term itself
|
1851
|
+
end
|
1852
|
+
end
|
1853
|
+
return path
|
1854
|
+
end
|
1855
|
+
|
1856
|
+
# Return ontology levels from profile terms
|
1857
|
+
# ===== Returns
|
1858
|
+
# hash of term levels (Key: level; Value: array of term IDs)
|
1859
|
+
def get_ontology_levels_from_profiles(uniq = true) # TODO: remove uniq and check dependencies
|
1860
|
+
profiles_terms = @profiles.values.flatten
|
1861
|
+
profiles_terms.uniq! if uniq
|
1862
|
+
term_freqs_byProfile = {}
|
1863
|
+
profiles_terms.each do |term|
|
1864
|
+
query = term_freqs_byProfile[term]
|
1865
|
+
if query.nil?
|
1866
|
+
term_freqs_byProfile[term] = 1
|
1867
|
+
else
|
1868
|
+
term_freqs_byProfile[term] += 1
|
1869
|
+
end
|
1870
|
+
end
|
1871
|
+
levels_filtered = @dicts[:level][:byTerm].map{|level, terms| [level,terms.map{|t| profiles_terms.include?(t) ? Array.new(term_freqs_byProfile[t], t) : nil}.flatten.compact]}.select{|level, filteredTerms| !filteredTerms.empty?}.to_h
|
1872
|
+
return levels_filtered
|
1873
|
+
end
|
1874
|
+
|
1875
|
+
def get_profile_ontology_distribution_tables
|
1876
|
+
cohort_ontology_levels = get_ontology_levels_from_profiles(uniq=false)
|
1877
|
+
uniq_cohort_ontology_levels = get_ontology_levels_from_profiles
|
1878
|
+
hpo_ontology_levels = get_ontology_levels
|
1879
|
+
total_ontology_terms = hpo_ontology_levels.values.flatten.length
|
1880
|
+
total_cohort_terms = cohort_ontology_levels.values.flatten.length
|
1881
|
+
total_uniq_cohort_terms = uniq_cohort_ontology_levels.values.flatten.length
|
1882
|
+
|
1883
|
+
ontology_levels = []
|
1884
|
+
distribution_percentage = []
|
1885
|
+
hpo_ontology_levels.each do |level, terms|
|
1886
|
+
cohort_terms = cohort_ontology_levels[level]
|
1887
|
+
uniq_cohort_terms = uniq_cohort_ontology_levels[level]
|
1888
|
+
if cohort_terms.nil? || uniq_cohort_terms.nil?
|
1889
|
+
num = 0
|
1890
|
+
u_num = 0
|
1891
|
+
else
|
1892
|
+
num = cohort_terms.length
|
1893
|
+
u_num = uniq_cohort_terms.length
|
1894
|
+
end
|
1895
|
+
ontology_levels << [level, terms.length, num]
|
1896
|
+
distribution_percentage << [
|
1897
|
+
level,
|
1898
|
+
(terms.length.fdiv(total_ontology_terms)*100).round(3),
|
1899
|
+
(num.fdiv(total_cohort_terms)*100).round(3),
|
1900
|
+
(u_num.fdiv(total_uniq_cohort_terms)*100).round(3)
|
1901
|
+
]
|
1902
|
+
end
|
1903
|
+
ontology_levels.sort! { |x,y| x.first <=> y.first }
|
1904
|
+
distribution_percentage.sort! { |x,y| x.first <=> y.first }
|
1905
|
+
return ontology_levels, distribution_percentage
|
1906
|
+
end
|
1907
|
+
|
1908
|
+
def get_dataset_specifity_index(mode)
|
1909
|
+
ontology_levels, distribution_percentage = get_profile_ontology_distribution_tables
|
1910
|
+
if mode == 'uniq'
|
1911
|
+
observed_distribution = 3
|
1912
|
+
elsif mode == 'weigthed'
|
1913
|
+
observed_distribution = 2
|
1914
|
+
end
|
1915
|
+
max_terms = distribution_percentage.map{|row| row[1]}.max
|
1916
|
+
maxL = nil
|
1917
|
+
distribution_percentage.each do |level_info|
|
1918
|
+
maxL = level_info.first if level_info[1] == max_terms
|
1919
|
+
end
|
1920
|
+
diffL = distribution_percentage.map{|l| [l[0], l[observed_distribution] - l[1]]}
|
1921
|
+
diffL.select!{|dL| dL.last > 0}
|
1922
|
+
lowSection = diffL.select{|dL| dL.first <= maxL}
|
1923
|
+
highSection = diffL.select{|dL| dL.first > maxL}
|
1924
|
+
dsi = nil
|
1925
|
+
if highSection.empty?
|
1926
|
+
dsi = 0
|
1927
|
+
else
|
1928
|
+
accumulated_weigth = 0
|
1929
|
+
accumulated_weigthed_diffL = 0
|
1930
|
+
hss = get_weigthed_level_contribution(highSection, maxL, ontology_levels.length - maxL)
|
1931
|
+
lss = get_weigthed_level_contribution(lowSection, maxL, maxL)
|
1932
|
+
dsi = hss.fdiv(lss)
|
1933
|
+
end
|
1934
|
+
return dsi
|
1935
|
+
end
|
1936
|
+
|
1937
|
+
def get_weigthed_level_contribution(section, maxL, nLevels)
|
1938
|
+
accumulated_weigthed_diffL = 0
|
1939
|
+
section.each do |level, diff|
|
1940
|
+
weightL = maxL - level
|
1941
|
+
if weightL >= 0
|
1942
|
+
weightL += 1
|
1943
|
+
else
|
1944
|
+
weightL = weightL.abs
|
1945
|
+
end
|
1946
|
+
accumulated_weigthed_diffL += diff * weightL
|
1947
|
+
end
|
1948
|
+
weigthed_contribution = accumulated_weigthed_diffL.fdiv(nLevels)
|
1949
|
+
return weigthed_contribution
|
1950
|
+
end
|
1951
|
+
|
1952
|
+
|
1953
|
+
# Calculate profiles dictionary with Key= Term; Value = Profiles
|
1954
|
+
def calc_profiles_dictionary
|
1955
|
+
if @profiles.empty?
|
1956
|
+
warn('Profiles are not already loaded. Aborting dictionary calc')
|
1957
|
+
else
|
1958
|
+
byTerm = {} # Key: Terms
|
1959
|
+
# byValue -- Key: Profile == @profiles
|
1960
|
+
@profiles.each do |id, terms|
|
1961
|
+
terms.each do |term|
|
1962
|
+
if byTerm.include?(term)
|
1963
|
+
byTerm[term] << id
|
1964
|
+
else
|
1965
|
+
byTerm[term] = [id]
|
1966
|
+
end
|
1967
|
+
end
|
1968
|
+
end
|
1969
|
+
@profilesDict = byTerm
|
1970
|
+
end
|
1971
|
+
end
|
1972
|
+
|
1973
|
+
|
1974
|
+
# Gets profiles dictionary calculated
|
1975
|
+
# ===== Return
|
1976
|
+
# profiles dictionary (clone)
|
1977
|
+
def get_terms_linked_profiles
|
1978
|
+
return @profilesDict.clone
|
1979
|
+
end
|
1980
|
+
|
1981
|
+
|
1982
|
+
# Get related profiles to a given term
|
1983
|
+
# ===== Parameters
|
1984
|
+
# +term+:: to be checked
|
1985
|
+
# ===== Returns
|
1986
|
+
# profiles which contains given term
|
1987
|
+
def get_term_linked_profiles(term)
|
1988
|
+
return @profilesDict[term]
|
1989
|
+
end
|
1990
|
+
|
1991
|
+
|
1992
|
+
# Gets metainfo table from a set of terms
|
1993
|
+
# ===== Parameters
|
1994
|
+
# +terms+:: IDs to be expanded
|
1995
|
+
# +filter_alternatives+:: flag to be used in get_descendants method
|
1996
|
+
# ===== Returns
|
1997
|
+
# an array with triplets [TermID, TermName, DescendantsNames]
|
1998
|
+
def get_childs_table(terms, filter_alternatives = false)
|
1999
|
+
expanded_terms = []
|
2000
|
+
terms.each do |t|
|
2001
|
+
expanded_terms << [[t, self.translate_id(t)], self.get_descendants(t, filter_alternatives).map{|child| [child, self.translate_id(child)]}]
|
2002
|
+
end
|
2003
|
+
return expanded_terms
|
2004
|
+
end
|
2005
|
+
|
2006
|
+
|
2007
|
+
# Store specific relations hash given into ITEMS structure
|
2008
|
+
# ===== Parameters
|
2009
|
+
# +relations+:: hash to be stored
|
2010
|
+
# +remove_old_relations+:: substitute ITEMS structure instead of merge new relations
|
2011
|
+
# +expand+:: if true, already stored keys will be updated with the unique union of both sets
|
2012
|
+
def load_item_relations_to_terms(relations, remove_old_relations = false, expand = false)
|
2013
|
+
@items = {} if remove_old_relations
|
2014
|
+
if !relations.select{|term, items| !@stanzas[:terms].include?(term)}.empty?
|
2015
|
+
warn('Some terms specified are not stored into this ontology. These not correct terms will be stored too')
|
2016
|
+
end
|
2017
|
+
if !remove_old_relations
|
2018
|
+
if !relations.select{|term, items| @items.include?(term)}.empty? && !expand
|
2019
|
+
warn('Some terms given are already stored. Stored version will be replaced')
|
2020
|
+
end
|
2021
|
+
end
|
2022
|
+
if expand
|
2023
|
+
@items = self.concatItems(@items,relations)
|
2024
|
+
# relations.each do |k,v| # MUST UPDATE THIS USING A CONCAT SPECIFIC FUNCTION
|
2025
|
+
# if @items.keys.include?(k)
|
2026
|
+
# if v.kind_of?(Array)
|
2027
|
+
# @items[k] = (@items[k] + v).uniq
|
2028
|
+
# elsif v.kind_of?(Hash)
|
2029
|
+
# @items.merge!(relations) do |k, oldV, newV|
|
2030
|
+
# if oldV.kind_of?(Array)
|
2031
|
+
# return (oldV + newV).uniq
|
2032
|
+
# else
|
2033
|
+
# oldV = [oldV,newV]
|
2034
|
+
# end
|
2035
|
+
# end
|
2036
|
+
# elsif @items[k].kind_of?(Array) # We suppose a single value/object from here
|
2037
|
+
# @items[k] = (@items[k] + [v]).uniq
|
2038
|
+
# else
|
2039
|
+
# @items[k] = [@items[k],v]
|
2040
|
+
# end
|
2041
|
+
# else
|
2042
|
+
# @items[k] = v
|
2043
|
+
# end
|
2044
|
+
# end
|
2045
|
+
else
|
2046
|
+
@items.merge!(relations)
|
2047
|
+
end
|
2048
|
+
end
|
2049
|
+
|
2050
|
+
# Internal function to concat two elements.
|
2051
|
+
# ===== Parameters
|
2052
|
+
# +itemA+:: item to be concatenated
|
2053
|
+
# +itemB+:: item to be concatenated
|
2054
|
+
# ===== Returns
|
2055
|
+
# Concatenated objects
|
2056
|
+
def concatItems(itemA,itemB)
|
2057
|
+
# A is Array :: RETURN ARRAY
|
2058
|
+
# A_array : B_array
|
2059
|
+
# A_array : B_hash => NOT ALLOWED
|
2060
|
+
# A_array : B_single => NOT ALLOWED
|
2061
|
+
# A is Hash :: RETURN HASH
|
2062
|
+
# A_hash : B_array => NOT ALLOWED
|
2063
|
+
# A_hash : B_hash
|
2064
|
+
# A_hash : B_single => NOT ALLOWED
|
2065
|
+
# A is single element => RETURN ARRAY
|
2066
|
+
# A_single : B_array
|
2067
|
+
# A_single : B_hash => NOT ALLOWED
|
2068
|
+
# A_single : B_single
|
2069
|
+
concatenated = nil
|
2070
|
+
if itemA.kind_of?(Array) && itemB.kind_of?(Array)
|
2071
|
+
concatenated = (itemA + itemB).uniq
|
2072
|
+
elsif itemA.kind_of?(Hash) && itemB.kind_of?(Hash)
|
2073
|
+
concatenated = itemA.merge(itemB) do |k, oldV, newV|
|
2074
|
+
self.concatItems(oldV,newV)
|
2075
|
+
end
|
2076
|
+
elsif itemB.kind_of?(Array)
|
2077
|
+
concatenated = ([itemA] + itemB).uniq
|
2078
|
+
elsif ![Array, Hash].include?(itemB.class)
|
2079
|
+
concatenated = [itemA,itemB].uniq
|
2080
|
+
end
|
2081
|
+
return concatenated
|
2082
|
+
end
|
2083
|
+
|
2084
|
+
|
2085
|
+
# Assign a dictionary already calculated as a items set.
|
2086
|
+
# ===== Parameters
|
2087
|
+
# +dictID+:: dictionary ID to be stored (:byTerm will be used)
|
2088
|
+
def set_items_from_dict(dictID, remove_old_relations = false)
|
2089
|
+
@items = {} if remove_old_relations
|
2090
|
+
if !@dicts[dictID].nil?
|
2091
|
+
@items.merge(@dicts[dictID][:byTerm])
|
2092
|
+
else
|
2093
|
+
warn('Specified ID is not calculated. Dict will not be added as a items set')
|
2094
|
+
end
|
2095
|
+
end
|
2096
|
+
|
2097
|
+
|
2098
|
+
# This method computes childs similarity and impute items to it parentals. To do that Item keys must be this ontology allowed terms.
|
2099
|
+
# Similarity will be calculated by text extact similarity unless an ontology object will be provided. In this case, MICAs will be used
|
2100
|
+
# ===== Parameters
|
2101
|
+
# +ontology+:: (Optional) ontology object which items given belongs
|
2102
|
+
# +minimum_childs+:: minimum of childs needed to infer relations to parental. Default: 2
|
2103
|
+
# +clean_profiles+:: if true, clena_profiles ontology method will be used over inferred profiles. Only if an ontology object is provided
|
2104
|
+
# ===== Returns
|
2105
|
+
# void and update items object
|
2106
|
+
def expand_items_to_parentals(ontology: nil, minimum_childs: 2, clean_profiles: true)
|
2107
|
+
# Check item keys
|
2108
|
+
if @items.empty?
|
2109
|
+
warn('Items have been not provided yet')
|
2110
|
+
return nil
|
2111
|
+
end
|
2112
|
+
targetKeys = @items.keys.select{|k| self.exists?(k)}
|
2113
|
+
if targetKeys.length == 0
|
2114
|
+
warn('Any item key is allowed')
|
2115
|
+
return nil
|
2116
|
+
elsif targetKeys.length < @items.keys.length
|
2117
|
+
warn('Some item keys are not allowed')
|
2118
|
+
end
|
2119
|
+
|
2120
|
+
# Expand to parentals
|
2121
|
+
targetKeys << targetKeys.map{|t| self.get_ancestors(t, true)}
|
2122
|
+
targetKeys.flatten!
|
2123
|
+
targetKeys.uniq!
|
2124
|
+
|
2125
|
+
# Obtain levels (go from leaves to roots)
|
2126
|
+
levels = targetKeys.map{|term| self.get_term_level(term)}
|
2127
|
+
levels.compact!
|
2128
|
+
levels.uniq!
|
2129
|
+
levels.sort!
|
2130
|
+
levels.reverse!
|
2131
|
+
levels.shift # Leaves are not expandable
|
2132
|
+
|
2133
|
+
# Expand from leaves to roots
|
2134
|
+
levels.map do |lvl|
|
2135
|
+
curr_keys = targetKeys.select{|k| self.get_term_level(k) == lvl}
|
2136
|
+
curr_keys.map do |term_expand|
|
2137
|
+
to_infer = []
|
2138
|
+
# Obtain childs
|
2139
|
+
childs = self.get_descendants(term_expand,true).select{|t| !@items[t].nil?}
|
2140
|
+
# Expand
|
2141
|
+
if childs.length > 0 && minimum_childs == 1 # Special case
|
2142
|
+
to_infer = childs.map{|c| @items[c]}.flatten.compact.uniq
|
2143
|
+
elsif childs.length >= minimum_childs
|
2144
|
+
to_infer = Hash.new(0)
|
2145
|
+
# Compare
|
2146
|
+
while childs.length > 1
|
2147
|
+
curr_term = childs.shift
|
2148
|
+
childs.each do |compare_term|
|
2149
|
+
pivot_items = @items[curr_term]
|
2150
|
+
compare_items = @items[compare_term]
|
2151
|
+
if ontology.nil? # Exact match
|
2152
|
+
pivot_items.map do |pitem|
|
2153
|
+
if compare_items.include?(pitem)
|
2154
|
+
to_infer[pitem] += 2
|
2155
|
+
end
|
2156
|
+
end
|
2157
|
+
else # Find MICAs
|
2158
|
+
local_infer = Hash.new(0)
|
2159
|
+
pivot_items.map do |pitem|
|
2160
|
+
micas = compare_items.map{|citem| ontology.get_MICA(pitem, citem)}
|
2161
|
+
maxmica = micas[0]
|
2162
|
+
micas.each{|mica| maxmica = mica if mica.last > maxmica.last}
|
2163
|
+
local_infer[maxmica.first] += 1
|
2164
|
+
end
|
2165
|
+
compare_items.map do |citem|
|
2166
|
+
micas = pivot_items.map{|pitem| ontology.get_MICA(pitem, citem)}
|
2167
|
+
maxmica = micas[0]
|
2168
|
+
micas.each{|mica| maxmica = mica if mica.last > maxmica.last}
|
2169
|
+
local_infer[maxmica.first] += 1
|
2170
|
+
end
|
2171
|
+
local_infer.each{|t,freq| to_infer[t] += freq if freq >= 2}
|
2172
|
+
end
|
2173
|
+
end
|
2174
|
+
end
|
2175
|
+
# Filter infer
|
2176
|
+
to_infer = to_infer.select{|k,v| v >= minimum_childs}
|
2177
|
+
end
|
2178
|
+
# Infer
|
2179
|
+
if to_infer.length > 0
|
2180
|
+
@items[term_expand] = [] if @items[term_expand].nil?
|
2181
|
+
if to_infer.kind_of?(Array)
|
2182
|
+
@items[term_expand] = (@items[term_expand] + to_infer).uniq
|
2183
|
+
else
|
2184
|
+
@items[term_expand] = (@items[term_expand] + to_infer.keys).uniq
|
2185
|
+
end
|
2186
|
+
@items[term_expand] = ontology.clean_profile(@items[term_expand]) if clean_profiles && !ontology.nil?
|
2187
|
+
elsif !@items.include?(term_expand)
|
2188
|
+
targetKeys.delete(term_expand)
|
2189
|
+
end
|
2190
|
+
end
|
2191
|
+
end
|
2192
|
+
end
|
2193
|
+
|
2194
|
+
|
2195
|
+
# Return direct ancestors/descendants of a given term
|
2196
|
+
# ===== Parameters
|
2197
|
+
# +term+:: which are requested
|
2198
|
+
# +relation+:: can be :ancestor or :descendant
|
2199
|
+
# +remove_alternatives+:: if true, alternatives will be removed
|
2200
|
+
# ===== Returns
|
2201
|
+
# Direct ancestors/descendants of given term or nil if any error occurs
|
2202
|
+
def get_direct_related(term, relation, remove_alternatives: false)
|
2203
|
+
if @dicts[:is_a].nil?
|
2204
|
+
warn("Hierarchy dictionary is not already calculated. Returning nil")
|
2205
|
+
return nil
|
2206
|
+
end
|
2207
|
+
target = nil
|
2208
|
+
case relation
|
2209
|
+
when :ancestor
|
2210
|
+
target = :byTerm
|
2211
|
+
when :descendant
|
2212
|
+
target = :byValue
|
2213
|
+
else
|
2214
|
+
warn('Relation type not allowed. Returning nil')
|
2215
|
+
end
|
2216
|
+
return nil if target.nil?
|
2217
|
+
query = @dicts[:is_a][target][term]
|
2218
|
+
return query if query.nil?
|
2219
|
+
query, _ = remove_alternatives_from_profile(query) if remove_alternatives
|
2220
|
+
return query
|
2221
|
+
end
|
2222
|
+
|
2223
|
+
|
2224
|
+
# Return direct ancestors of a given term
|
2225
|
+
# ===== Parameters
|
2226
|
+
# +term+:: which ancestors are requested
|
2227
|
+
# +remove_alternatives+:: if true, alternatives will be removed
|
2228
|
+
# ===== Returns
|
2229
|
+
# Direct ancestors of given term or nil if any error occurs
|
2230
|
+
def get_direct_ancentors(term, remove_alternatives: false)
|
2231
|
+
return self.get_direct_related(term, :ancestor, remove_alternatives: remove_alternatives)
|
2232
|
+
end
|
2233
|
+
|
2234
|
+
# Return direct descendants of a given term
|
2235
|
+
# ===== Parameters
|
2236
|
+
# +term+:: which descendants are requested
|
2237
|
+
# +remove_alternatives+:: if true, alternatives will be removed
|
2238
|
+
# ===== Returns
|
2239
|
+
# Direct descendants of given term or nil if any error occurs
|
2240
|
+
def get_direct_descendants(term, remove_alternatives: false)
|
2241
|
+
return self.get_direct_related(term, :descendant, remove_alternatives: remove_alternatives)
|
2242
|
+
end
|
2243
|
+
|
2244
|
+
def each(att = false)
|
2245
|
+
@stanzas[:terms].each do |id, tags|
|
2246
|
+
next if @alternatives_index.include?(id)
|
2247
|
+
if att
|
2248
|
+
yield(id, tags)
|
2249
|
+
else
|
2250
|
+
yield(id)
|
2251
|
+
end
|
2252
|
+
end
|
2253
|
+
end
|
2254
|
+
|
2255
|
+
def list_term_attributes
|
2256
|
+
terms = []
|
2257
|
+
each do |code|
|
2258
|
+
terms << [code, translate_id(code), get_term_level(code)]
|
2259
|
+
end
|
2260
|
+
return terms
|
2261
|
+
end
|
2262
|
+
|
2263
|
+
#============================================================================
|
2264
|
+
#============================================================================
|
2265
|
+
|
2266
|
+
# NO IDEA WHAT THIS DOES. DON'T USE THIS METHODS IS NOT CHECKED
|
2267
|
+
# ===== Parameters
|
2268
|
+
# ++::
|
2269
|
+
# ===== Returns
|
2270
|
+
# ...
|
2271
|
+
def compute_relations_to_items(external_item_list, total_items, mode, thresold)
|
2272
|
+
terms_levels = list_terms_per_level_from_items
|
2273
|
+
#puts terms_levels.inspect.yellow
|
2274
|
+
connect_familiars!(terms_levels)
|
2275
|
+
#puts terms_levels.inspect.blue
|
2276
|
+
item_list_with_transf_parental = get_item_list_parental(terms_levels)
|
2277
|
+
results = []
|
2278
|
+
if mode == :elim
|
2279
|
+
results = compute_relations_elim(terms_levels, external_item_list, total_items, thresold, item_list_with_transf_parental)
|
2280
|
+
elsif mode == :weight
|
2281
|
+
results = compute_relations_weight(terms_levels, external_item_list, total_items, item_list_with_transf_parental)
|
2282
|
+
end
|
2283
|
+
return results
|
2284
|
+
end
|
2285
|
+
|
2286
|
+
def get_item_list_parental(terms_levels)
|
2287
|
+
transfered_list = {}
|
2288
|
+
parent_dict = @dicts[:is_a][:byTerm]
|
2289
|
+
levels = terms_levels.keys.sort
|
2290
|
+
while levels.length > 1
|
2291
|
+
level = levels.pop
|
2292
|
+
terms_levels[level].each do |term|
|
2293
|
+
parents = parent_dict[term]
|
2294
|
+
if parents.nil?
|
2295
|
+
next
|
2296
|
+
elsif parents.length == 1
|
2297
|
+
parent = parents.first
|
2298
|
+
else
|
2299
|
+
parent = (parents | terms_levels[level - 1]).first
|
2300
|
+
end
|
2301
|
+
term_it = @items[term]
|
2302
|
+
parent_it = @items[parent]
|
2303
|
+
curr_it = transfered_list[term]
|
2304
|
+
parent_all_items = merge_groups([term_it, parent_it, curr_it])
|
2305
|
+
transfered_list[parent] = parent_all_items if !parent_all_items.empty?
|
2306
|
+
term_all_items = merge_groups([term_it, curr_it])
|
2307
|
+
transfered_list[term] = term_all_items if !term_all_items.empty?
|
2308
|
+
end
|
2309
|
+
end
|
2310
|
+
terms_levels[levels.first].each do |term| # Rescue lower level terms that not have children so they cannot receive items
|
2311
|
+
transfered_list[term] = @items[term] if transfered_list[term].nil?
|
2312
|
+
end
|
2313
|
+
return transfered_list
|
2314
|
+
end
|
2315
|
+
|
2316
|
+
def merge_groups(groups)
|
2317
|
+
return groups.compact.inject([]){|it, a| it | a}
|
2318
|
+
end
|
2319
|
+
|
2320
|
+
def list_terms_per_level_from_items
|
2321
|
+
terms_levels = {}
|
2322
|
+
@items.each do |term, items|
|
2323
|
+
level = self.get_term_level(term)
|
2324
|
+
query = terms_levels[level]
|
2325
|
+
if query.nil?
|
2326
|
+
terms_levels[level] = [term]
|
2327
|
+
else
|
2328
|
+
query << term
|
2329
|
+
end
|
2330
|
+
end
|
2331
|
+
return terms_levels
|
2332
|
+
end
|
2333
|
+
|
2334
|
+
def connect_familiars!(terms_levels)
|
2335
|
+
levels = terms_levels.keys.sort
|
2336
|
+
while levels.length > 1 # Process when current level has a parental level
|
2337
|
+
level = levels.pop
|
2338
|
+
parental_level = level - 1
|
2339
|
+
parental_terms = terms_levels[parental_level]
|
2340
|
+
if parental_terms.nil? # The queried parent level not exists but there is a parental level above of the non existant
|
2341
|
+
parental_terms = [] # Initialize required parental level
|
2342
|
+
terms_levels[parental_level] = parental_terms
|
2343
|
+
levels << parental_level
|
2344
|
+
end
|
2345
|
+
terms_levels[level].each do |term|
|
2346
|
+
path_info = @term_paths[term]
|
2347
|
+
shortest_path_length = path_info[:shortest_path]
|
2348
|
+
path = path_info[:paths].select{|p| p.length == shortest_path_length}.first
|
2349
|
+
parental = path[1] # the first elements is the term itself
|
2350
|
+
parental_terms << parental if !parental_terms.include?(parental)
|
2351
|
+
end
|
2352
|
+
end
|
2353
|
+
end
|
2354
|
+
|
2355
|
+
def compute_relations_elim(terms_levels, external_item_list, total_items, thresold, item_list)
|
2356
|
+
results = []
|
2357
|
+
penalized_terms = {}
|
2358
|
+
levels = terms_levels.keys.sort
|
2359
|
+
levels.reverse_each do |level|
|
2360
|
+
terms_levels[level].each do |term|
|
2361
|
+
associated_items = item_list[term]
|
2362
|
+
items_to_remove = penalized_terms[term]
|
2363
|
+
items_to_remove = [] if items_to_remove.nil?
|
2364
|
+
pval = get_fisher_exact_test(
|
2365
|
+
external_item_list - items_to_remove,
|
2366
|
+
associated_items - items_to_remove,
|
2367
|
+
#((associated_items | external_item_list) - items_to_remove).length
|
2368
|
+
total_items
|
2369
|
+
)
|
2370
|
+
if pval <= thresold
|
2371
|
+
parents = get_ancestors(term) # Save the items for each parent term to remove them later in the fisher test
|
2372
|
+
parents.each do |prnt|
|
2373
|
+
query = penalized_terms[prnt]
|
2374
|
+
if query.nil?
|
2375
|
+
penalized_terms[prnt] = item_list[term].clone # We need a new array to store the following iterations
|
2376
|
+
else
|
2377
|
+
query.concat(item_list[term])
|
2378
|
+
end
|
2379
|
+
end
|
2380
|
+
end
|
2381
|
+
results << [term, pval]
|
2382
|
+
end
|
2383
|
+
end
|
2384
|
+
return results
|
2385
|
+
end
|
2386
|
+
|
2387
|
+
def compute_relations_weight(terms_levels, external_item_list, total_items, item_list)
|
2388
|
+
pvals = {}
|
2389
|
+
item_weigths_per_term = Hash.new { |hash, key| Hash.new(1) } #https://mensfeld.pl/2016/09/ruby-hash-default-value-be-cautious-when-you-use-it/
|
2390
|
+
levels = terms_levels.keys.sort
|
2391
|
+
levels.reverse_each do |level|
|
2392
|
+
terms_levels[level].each do |term|
|
2393
|
+
associated_items = item_list[term]
|
2394
|
+
#initialize observed items in item_weigths_per_term list
|
2395
|
+
add_items_to_weigthed_list(term, associated_items, item_weigths_per_term) if !associated_items.nil?
|
2396
|
+
children = @dicts[:is_a][:byValue][term]
|
2397
|
+
if children.nil?
|
2398
|
+
children = []
|
2399
|
+
else
|
2400
|
+
children = children.select{|ch| item_weigths_per_term[ch].length > 0} # Only use children with items associated to them OR transfered to them
|
2401
|
+
end
|
2402
|
+
computeTermSig(term, children, external_item_list, total_items, pvals, item_weigths_per_term)
|
2403
|
+
end
|
2404
|
+
end
|
2405
|
+
return pvals.to_a
|
2406
|
+
end
|
2407
|
+
|
2408
|
+
def add_items_to_weigthed_list(term, associated_items, weigthed_list)
|
2409
|
+
term_weigthing = weigthed_list[term]
|
2410
|
+
associated_items.each{|ai| term_weigthing[ai] = 1}
|
2411
|
+
weigthed_list[term] = term_weigthing
|
2412
|
+
end
|
2413
|
+
|
2414
|
+
def computeTermSig(term, children, external_item_list, total_items, pvals, item_weigths_per_term)
|
2415
|
+
#puts term.to_s.red
|
2416
|
+
#puts @term_paths[term].inspect
|
2417
|
+
#puts @dicts[:is_a][:byValue][term].inspect.light_blue
|
2418
|
+
associated_items = item_weigths_per_term[term].keys
|
2419
|
+
pval = get_fisher_exact_test(external_item_list, associated_items, total_items,
|
2420
|
+
'two_sided', item_weigths_per_term[term], true)
|
2421
|
+
pvals[term] = pval
|
2422
|
+
if children.length > 0
|
2423
|
+
rates = {}
|
2424
|
+
sig_child = 0
|
2425
|
+
children.each do |child|
|
2426
|
+
ratio = sigRatio(pvals[child], pval)
|
2427
|
+
rates[child] = ratio
|
2428
|
+
sig_child += 1 if ratio >= 1
|
2429
|
+
end
|
2430
|
+
if sig_child == 0 # CASE 1
|
2431
|
+
children.each do |child|
|
2432
|
+
current_ratio = rates[child]
|
2433
|
+
query_child = item_weigths_per_term[child]
|
2434
|
+
query_child.transform_values!{|weight| weight * current_ratio}
|
2435
|
+
pvals[child] = get_fisher_exact_test(external_item_list, item_weigths_per_term[child].keys, total_items,
|
2436
|
+
'two_sided', item_weigths_per_term[child], true)
|
2437
|
+
end
|
2438
|
+
else
|
2439
|
+
ancs = get_ancestors(term, filter_alternatives = true)
|
2440
|
+
ancs << term
|
2441
|
+
rates.each do |ch, ratio|# CASE 2
|
2442
|
+
if ratio >= 1 # The child is better than parent
|
2443
|
+
ancs.each do |anc|
|
2444
|
+
query_anc = item_weigths_per_term[anc]
|
2445
|
+
associated_items.each do |item|
|
2446
|
+
query_anc[item] /= ratio # /= --> query_anc[item]/ratio
|
2447
|
+
end
|
2448
|
+
end
|
2449
|
+
end
|
2450
|
+
end
|
2451
|
+
computeTermSig(term, children - rates.keys, external_item_list, total_items, pvals, item_weigths_per_term)
|
2452
|
+
end
|
2453
|
+
end
|
2454
|
+
end
|
2455
|
+
|
2456
|
+
def sigRatio(pvalA, pvalB)
|
2457
|
+
return Math.log(pvalA)/Math.log(pvalB)
|
2458
|
+
end
|
2459
|
+
|
2460
|
+
def profile_stats
|
2461
|
+
stats = Hash.new(0)
|
2462
|
+
data = @profiles.values.map{|ont_ids| ont_ids.size}
|
2463
|
+
stats[:average] = data.sum().fdiv(data.size)
|
2464
|
+
sum_devs = data.sum{|element| (element - stats[:avg]) ** 2}
|
2465
|
+
stats[:variance] = sum_devs.fdiv(data.size)
|
2466
|
+
stats[:standardDeviation] = stats[:variance] ** 0.5
|
2467
|
+
stats[:max] = data.max
|
2468
|
+
stats[:min] = data.min
|
2469
|
+
|
2470
|
+
stats[:count] = data.size
|
2471
|
+
data.each do |value|
|
2472
|
+
stats[:countNonZero] += 1 if value != 0
|
2473
|
+
end
|
2474
|
+
|
2475
|
+
stats[:q1] = data.get_quantiles(0.25)
|
2476
|
+
stats[:median] = data.get_quantiles(0.5)
|
2477
|
+
stats[:q3] = data.get_quantiles(0.75)
|
2478
|
+
return stats
|
2479
|
+
|
2480
|
+
end
|
2481
|
+
|
2482
|
+
#============================================================================
|
2483
|
+
#============================================================================
|
2484
|
+
|
2485
|
+
# Check if a given ID is a removable (blacklist) term.
|
2486
|
+
# +DEPRECATED+ use is_removable? instead
|
2487
|
+
# ===== Parameters
|
2488
|
+
# +id+:: to be checked
|
2489
|
+
# ===== Returns
|
2490
|
+
# true if given term is a removable (blacklist) term or false in other cases
|
2491
|
+
def is_removable(id)
|
2492
|
+
warn "[DEPRECATION] `is_removable` is deprecated. Please use `is_removable?` instead."
|
2493
|
+
return @removable_terms.include?(id.to_sym)
|
2494
|
+
end
|
2495
|
+
|
2496
|
+
# Check if a given ID is a removable (blacklist) term
|
2497
|
+
# ===== Parameters
|
2498
|
+
# +id+:: to be checked
|
2499
|
+
# ===== Returns
|
2500
|
+
# true if given term is a removable (blacklist) term or false in other cases
|
2501
|
+
def is_removable? id
|
2502
|
+
return @removable_terms.include?(id.to_sym)
|
2503
|
+
end
|
2504
|
+
|
2505
|
+
############################################
|
2506
|
+
# SPECIAL METHODS
|
2507
|
+
#############################################
|
2508
|
+
def ==(other)
|
2509
|
+
self.header == other.header &&
|
2510
|
+
self.stanzas == other.stanzas &&
|
2511
|
+
self.ancestors_index == other.ancestors_index &&
|
2512
|
+
self.alternatives_index == other.alternatives_index &&
|
2513
|
+
self.obsoletes_index == other.obsoletes_index &&
|
2514
|
+
self.structureType == other.structureType &&
|
2515
|
+
self.ics == other.ics &&
|
2516
|
+
self.meta == other.meta &&
|
2517
|
+
self.dicts == other.dicts &&
|
2518
|
+
self.profiles == other.profiles &&
|
2519
|
+
self.profilesDict == other.profilesDict &&
|
2520
|
+
(self.items.keys - other.items.keys).empty? &&
|
2521
|
+
self.removable_terms == other.removable_terms &&
|
2522
|
+
self.special_tags == other.special_tags &&
|
2523
|
+
self.items == other.items &&
|
2524
|
+
self.term_paths == other.term_paths &&
|
2525
|
+
self.max_freqs == other.max_freqs
|
2008
2526
|
end
|
2009
2527
|
|
2010
2528
|
|
2011
2529
|
def clone
|
2012
|
-
|
2013
|
-
|
2014
|
-
|
2015
|
-
|
2016
|
-
|
2017
|
-
|
2018
|
-
|
2019
|
-
|
2020
|
-
|
2021
|
-
|
2022
|
-
|
2023
|
-
|
2024
|
-
|
2025
|
-
|
2026
|
-
|
2027
|
-
|
2028
|
-
|
2029
|
-
|
2030
|
-
|
2031
|
-
|
2032
|
-
end
|
2033
|
-
|
2034
|
-
|
2035
|
-
|
2036
|
-
|
2037
|
-
|
2038
|
-
|
2039
|
-
|
2040
|
-
|
2530
|
+
copy = Ontology.new
|
2531
|
+
copy.header = self.header.clone
|
2532
|
+
copy.stanzas[:terms] = self.stanzas[:terms].clone
|
2533
|
+
copy.stanzas[:typedefs] = self.stanzas[:typedefs].clone
|
2534
|
+
copy.stanzas[:instances] = self.stanzas[:instances].clone
|
2535
|
+
copy.ancestors_index = self.ancestors_index.clone
|
2536
|
+
copy.descendants_index = self.descendants_index.clone
|
2537
|
+
copy.alternatives_index = self.alternatives_index.clone
|
2538
|
+
copy.obsoletes_index = self.obsoletes_index.clone
|
2539
|
+
copy.structureType = self.structureType.clone
|
2540
|
+
copy.ics = self.ics.clone
|
2541
|
+
copy.meta = self.meta.clone
|
2542
|
+
copy.dicts = self.dicts.clone
|
2543
|
+
copy.profiles = self.profiles.clone
|
2544
|
+
copy.profilesDict = self.profilesDict.clone
|
2545
|
+
copy.items = self.items.clone
|
2546
|
+
copy.removable_terms = self.removable_terms.clone
|
2547
|
+
copy.term_paths = self.term_paths.clone
|
2548
|
+
copy.max_freqs = self.max_freqs.clone
|
2549
|
+
return copy
|
2550
|
+
end
|
2551
|
+
|
2552
|
+
|
2553
|
+
#############################################
|
2554
|
+
# ACCESS CONTROL
|
2555
|
+
#############################################
|
2556
|
+
|
2557
|
+
attr_reader :file, :header, :stanzas, :ancestors_index, :descendants_index, :special_tags, :alternatives_index, :obsoletes_index, :structureType, :ics, :max_freqs, :meta, :dicts, :profiles, :profilesDict, :items, :removable_terms, :term_paths
|
2558
|
+
attr_writer :file, :header, :stanzas, :ancestors_index, :descendants_index, :special_tags, :alternatives_index, :obsoletes_index, :structureType, :ics, :max_freqs, :meta, :dicts, :profiles, :profilesDict, :items, :removable_terms, :term_paths
|
2041
2559
|
end
|