This file is indexed.

/usr/share/perl5/Bio/Ontology/SimpleOntologyEngine.pm is in libbio-perl-perl 1.6.924-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
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
#
# BioPerl module for Bio::Ontology::SimpleOntologyEngine
#
# Please direct questions and support issues to <bioperl-l@bioperl.org>
#
# Cared for by Peter Dimitrov <dimitrov@gnf.org>
#
# Copyright Peter Dimitrov
# (c) Peter Dimitrov, dimitrov@gnf.org, 2002.
# (c) GNF, Genomics Institute of the Novartis Research Foundation, 2002.
#
# You may distribute this module under the same terms as perl itself.
# Refer to the Perl Artistic License (see the license accompanying this
# software package, or see http://www.perl.com/language/misc/Artistic.html)
# for the terms under which you may use, modify, and redistribute this module.
#
# THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# POD documentation - main docs before the code

=head1 NAME

Bio::Ontology::SimpleOntologyEngine - Implementation of OntologyEngineI interface

=head1 SYNOPSIS

  my $soe = Bio::Ontology::SimpleOntologyEngine->new;

=head1 DESCRIPTION

This is a "simple" implementation of Bio::Ontology::OntologyEngineI.

=head1 FEEDBACK

=head2 Mailing Lists

User feedback is an integral part of the evolution of this and other
Bioperl modules. Send your comments and suggestions preferably to
the Bioperl mailing list.  Your participation is much appreciated.

  bioperl-l@bioperl.org                  - General discussion
  http://bioperl.org/wiki/Mailing_lists  - About the mailing lists

=head2 Support

Please direct usage questions or support issues to the mailing list:

I<bioperl-l@bioperl.org>

rather than to the module maintainer directly. Many experienced and
reponsive experts will be able look at the problem and quickly
address it. Please include a thorough description of the problem
with code and data examples if at all possible.

=head2 Reporting Bugs

Report bugs to the Bioperl bug tracking system to help us keep track
of the bugs and their resolution. Bug reports can be submitted via
the web:

  https://github.com/bioperl/bioperl-live/issues

=head1 AUTHOR - Peter Dimitrov

Email dimitrov@gnf.org

=head1 CONTRIBUTORS

Hilmar Lapp, hlapp at gmx.net

=head1 APPENDIX

The rest of the documentation details each of the object methods.
Internal methods are usually preceded with a _

=cut

# Let the code begin...

package Bio::Ontology::SimpleOntologyEngine;
use strict;
use Carp;
use Bio::Ontology::RelationshipFactory;
use Data::Dumper;

use base qw(Bio::Root::Root Bio::Ontology::OntologyEngineI);

=head2 new

 Title   : new
 Usage   : $soe = Bio::Ontology::SimpleOntologyEngine->new;
 Function: Initializes the ontology engine.
 Example : $soe = Bio::Ontology::SimpleOntologyEngine->new;
 Returns : Object of class SimpleOntologyEngine.
 Args    :

=cut

sub new {
    my ( $class, @args ) = @_;
    my $self = $class->SUPER::new(@args);

    #   my %param = @args;

    $self->_term_store(                  {} );
    $self->_relationship_store(          {} );
    $self->_inverted_relationship_store( {} );
    $self->_relationship_type_store(     {} );
    $self->_instantiated_terms_store(    {} );

    # set defaults for the factories
    $self->relationship_factory(
        Bio::Ontology::RelationshipFactory->new( -type => "Bio::Ontology::Relationship" ) );
    return $self;
}

=head2 _instantiated_terms_store

 Title   : _instantiated_terms_store
 Usage   : $obj->_instantiated_terms_store($newval)
 Function:
 Example :
 Returns : hash
 Args    : empty hash

=cut

sub _instantiated_terms_store {
    my ( $self, $value ) = @_;

    if ( defined $value ) {
        $self->{'_instantiated_terms_store'} = $value;
    }
    return $self->{'_instantiated_terms_store'};
}

=head2 mark_instantiated

 Title   : mark_instantiated
 Usage   : $self->mark_instantiated(TermI terms): TermI
 Function: Marks TermI objects as fully instantiated,
           allowing for proper counting of the number of terms in the term store.
           The TermI objects has to be already stored in the term store in order
           to be marked.
 Example : $self->mark_instantiated($term);
 Returns : its argument or throws an exception if a term is not
           in the term store.
 Args    : array of objects of class TermI.

=cut

sub mark_instantiated {
    my ( $self, @terms ) = @_;

    foreach my $term (@terms) {
        $self->throw( "term " . $term->identifier . " not in the term store\n" )
            if !defined $self->_term_store->{ $term->identifier };
        $self->_instantiated_terms_store->{ $term->identifier } = 1;
    }

    return @terms;
}

=head2 mark_uninstantiated

 Title   : mark_uninstantiated
 Usage   : $self->mark_uninstantiated(TermI terms): TermI
 Function: Marks TermI objects as not fully instantiated,
 Example : $self->mark_uninstantiated($term);
 Returns : its argument or throws an exception if a term is not
           in the term store(if the term is not marked it does nothing).
 Args    : array of objects of class TermI.

=cut

sub mark_uninstantiated {
    my ( $self, @terms ) = @_;

    foreach my $term (@terms) {
        $self->throw( "term " . $term->identifier . " not in the term store\n" )
            if !defined $self->_term_store->{ $term->identifier };
        delete $self->_instantiated_terms_store->{ $term->identifier }
            if defined $self->_instantiated_terms_store->{ $term->identifier };
    }

    return @terms;
}

=head2 _term_store

 Title   : term_store
 Usage   : $obj->_term_store($newval)
 Function:
 Example :
 Returns : reference to an array of Bio::Ontology::TermI objects
 Args    : reference to an array of Bio::Ontology::TermI objects

=cut

sub _term_store {
    my ( $self, $value ) = @_;

    if ( defined $value ) {
        if ( defined $self->{'_term_store'} ) {
            $self->throw("_term_store already defined\n");
        } else {
            $self->{'_term_store'} = $value;
        }
    }

    return $self->{'_term_store'};
}

=head2 add_term

 Title   : add_term
 Usage   : add_term(TermI term): TermI
 Function: Adds TermI object to the ontology engine term store.
 Marks the term fully instantiated by default.
 Example : $soe->add_term($term)
 Returns : its argument.
 Args    : object of class TermI.

=cut

sub add_term {
    my ( $self, $term ) = @_;
    my $term_store = $self->_term_store;

    if ( defined $term_store->{ $term->identifier } && $self->_instantiated_terms_store->{ $term->identifier }) {
        $self->throw( "term " . $term->identifier . " already defined\n" );
    } else {
        $term_store->{ $term->identifier } = $term;
        $self->_instantiated_terms_store->{ $term->identifier } = 1;
    }

    return $term;
}

=head2 get_term_by_identifier

 Title   : get_term_by_identifier
 Usage   : get_term_by_identifier(String id): TermI
 Function: Retrieves terms from the term store by their identifier
           field, or an empty list if not there.
 Example : $term = $soe->get_term_by_identifier("IPR000001");
 Returns : An array of zero or more Bio::Ontology::TermI objects.
 Args    : An array of identifier strings

=cut

sub get_term_by_identifier {
    my ( $self, @ids ) = @_;
    my @ans = ();

    foreach my $id (@ids) {
        my $term = $self->_term_store->{$id};
        push @ans, $term if defined $term;
    }

    return @ans;
}

=head2 _get_number_rels

 Title   : get_number_rels
 Usage   :
 Function:
 Example :
 Returns :
 Args    :

=cut

sub _get_number_rels {
    my ($self) = @_;
    my $num_rels = 0;

    foreach my $entry ( $self->_relationship_store ) {
        $num_rels += scalar keys %$entry;
    }
    return $num_rels;
}

=head2 _get_number_terms

 Title   : _get_number_terms
 Usage   :
 Function:
 Example :
 Returns :
 Args    :

=cut

sub _get_number_terms {
    my ($self) = @_;

    return scalar $self->_filter_unmarked( values %{ $self->_term_store } );

}

=head2 _relationship_store

 Title   : _storerelationship_store
 Usage   : $obj->relationship_store($newval)
 Function:
 Example :
 Returns : reference to an array of Bio::Ontology::TermI objects
 Args    : reference to an array of Bio::Ontology::TermI objects

=cut

sub _relationship_store {
    my ( $self, $value ) = @_;

    if ( defined $value ) {
        if ( defined $self->{'_relationship_store'} ) {
            $self->throw("_relationship_store already defined\n");
        } else {
            $self->{'_relationship_store'} = $value;
        }
    }

    return $self->{'_relationship_store'};
}

=head2 _inverted_relationship_store

 Title   : _inverted_relationship_store
 Usage   :
 Function:
 Example :
 Returns : reference to an array of Bio::Ontology::TermI objects
 Args    : reference to an array of Bio::Ontology::TermI objects

=cut

sub _inverted_relationship_store {
    my ( $self, $value ) = @_;

    if ( defined $value ) {
        if ( defined $self->{'_inverted_relationship_store'} ) {
            $self->throw("_inverted_relationship_store already defined\n");
        } else {
            $self->{'_inverted_relationship_store'} = $value;
        }
    }

    return $self->{'_inverted_relationship_store'};
}

=head2 _relationship_type_store

 Title   : _relationship_type_store
 Usage   : $obj->_relationship_type_store($newval)
 Function:
 Example :
 Returns : reference to an array of Bio::Ontology::RelationshipType objects
 Args    : reference to an array of Bio::Ontology::RelationshipType objects

=cut

sub _relationship_type_store {
    my ( $self, $value ) = @_;

    if ( defined $value ) {
        if ( defined $self->{'_relationship_type_store'} ) {
            $self->throw("_relationship_type_store already defined\n");
        } else {
            $self->{'_relationship_type_store'} = $value;
        }
    }

    return $self->{'_relationship_type_store'};
}

=head2 _add_relationship_simple

 Title   : _add_relationship_simple
 Usage   :
 Function:
 Example :
 Returns :
 Args    :

=cut

sub _add_relationship_simple {
    my ( $self, $store, $rel, $inverted ) = @_;

    my $subject = $rel->subject_term
        or $self->throw('cannot add relationship, relationship has no subject_term');
    my $object  = $rel->object_term
        or $self->throw('cannot add relationship, relationship has no object_term');

    my ( $parent_id, $child_id ) = ( $object->identifier, $subject->identifier );
    ( $parent_id, $child_id ) = ( $child_id, $parent_id ) if $inverted;

    if (     defined $store->{$parent_id}
         &&  defined $store->{$parent_id}->{$child_id}
         && $store->{$parent_id}->{$child_id}->name ne $rel->predicate_term->name
     ) {
        $self->throw( "relationship "
                . $rel->predicate_term->name
                . " between "
                . $parent_id . " and "
                . $child_id
                . " already defined as "
                . $store->{$parent_id}->{$child_id}->name
                . "\n" );
    }

    # all is well if we get here
    $store->{$parent_id}->{$child_id} = $rel->predicate_term;
}

=head2 add_relationship

 Title   : add_relationship
 Usage   : add_relationship(RelationshipI relationship): RelationshipI
 Function: Adds a relationship object to the ontology engine.
 Example :
 Returns : Its argument.
 Args    : A RelationshipI object.

=cut

sub add_relationship {
    my ( $self, $rel ) = @_;

    $self->_add_relationship_simple( $self->_relationship_store,          $rel, 0 );
    $self->_add_relationship_simple( $self->_inverted_relationship_store, $rel, 1 );
    $self->_relationship_type_store->{ $self->_unique_termid( $rel->predicate_term ) } =
        $rel->predicate_term;

    return $rel;
}

=head2 get_relationships

 Title   : get_relationships
 Usage   : get_relationships(): RelationshipI
 Function: Retrieves all relationship objects.
 Example :
 Returns : Array of RelationshipI objects
 Args    :

=cut

sub get_relationships {
    my $self = shift;
    my $term = shift;
    my @rels;
    my $store   = $self->_relationship_store;
    my $relfact = $self->relationship_factory();

    my @parent_ids = $term
        ?

        # if a term is supplied then only get the term's parents
        ( map { $_->identifier(); } $self->get_parent_terms($term) )
        :

        # otherwise use all parent ids
        ( keys %{$store} );

    # add the term as a parent too if one is supplied
    push( @parent_ids, $term->identifier ) if $term;

    foreach my $parent_id (@parent_ids) {
        my $parent_entry = $store->{$parent_id};

        # if a term is supplied, add a relationship for the parent to the term
        # except if the parent is the term itself (we added that one before)
        if ( $term && ( $parent_id ne $term->identifier() ) ) {
            my @parent_terms = $self->get_term_by_identifier($parent_id);
            foreach my $parent_term (@parent_terms) {
                push(
                    @rels,
                    $relfact->create_object(
                        -object_term    => $parent_term,
                        -subject_term   => $term,
                        -predicate_term => $parent_entry->{ $term->identifier },
                        -ontology       => $term->ontology()
                    )
                );
            }

        } else {

            # otherwise, i.e., no term supplied, or the parent equals the
            # supplied term
            my @parent_terms = $term ? ($term) : $self->get_term_by_identifier($parent_id);
            foreach my $child_id ( keys %$parent_entry ) {
                my $rel_info = $parent_entry->{$child_id};
                my ($subj_term) = $self->get_term_by_identifier($child_id);

                foreach my $parent_term (@parent_terms) {
                    push(
                        @rels,
                        $relfact->create_object(
                            -object_term    => $parent_term,
                            -subject_term   => $subj_term,
                            -predicate_term => $rel_info,
                            -ontology       => $parent_term->ontology
                        )
                    );
                }
            }
        }
    }

    return @rels;
}

=head2 get_all_relationships

 Title   : get_all_relationships
 Usage   : get_all_relationships(): RelationshipI
 Function: Retrieves all relationship objects.
 Example :
 Returns : Array of RelationshipI objects
 Args    :

=cut

sub get_all_relationships {
    return shift->get_relationships();
}

=head2 get_predicate_terms

 Title   : get_predicate_terms
 Usage   : get_predicate_terms(): TermI
 Function: Retrives all relationship types stored in the engine
 Example :
 Returns : reference to an array of Bio::Ontology::RelationshipType objects
 Args    :

=cut

sub get_predicate_terms {
    my ($self) = @_;

    return values %{ $self->_relationship_type_store };
}

=head2 _is_rel_type

 Title   : _is_rel_type
 Usage   :
 Function:
 Example :
 Returns :
 Args    :

=cut

sub _is_rel_type {
    my ( $self, $term, @rel_types ) = @_;

    foreach my $rel_type (@rel_types) {
        if ( $rel_type->identifier || $term->identifier ) {
            return 1 if $rel_type->identifier eq $term->identifier;
        } else {
            return 1 if $rel_type->name eq $term->name;
        }
    }

    return 0;
}

=head2 _typed_traversal

 Title   : _typed_traversal
 Usage   :
 Function:
 Example :
 Returns :
 Args    :

=cut

sub _typed_traversal {
    my ( $self, $rel_store, $level, $term_id, @rel_types ) = @_;
    return if !defined( $rel_store->{$term_id} );
    my %parent_entry = %{ $rel_store->{$term_id} };
    my @children     = keys %parent_entry;

    my @ans;

    if ( @rel_types > 0 ) {
        @ans = ();

        foreach my $child_id (@children) {
            push @ans, $child_id
                if $self->_is_rel_type( $rel_store->{$term_id}->{$child_id}, @rel_types );
        }
    } else {
        @ans = @children;
    }
    if ( $level < 1 ) {
        my @ans1 = ();

        foreach my $child_id (@ans) {
            push @ans1, $self->_typed_traversal( $rel_store, $level - 1, $child_id, @rel_types )
                if defined $rel_store->{$child_id};
        }
        push @ans, @ans1;
    }

    return @ans;
}

=head2 get_child_terms

 Title   : get_child_terms
 Usage   : get_child_terms(TermI term, TermI predicate_terms): TermI
           get_child_terms(TermI term, RelationshipType predicate_terms): TermI
 Function: Retrieves all child terms of a given term, that satisfy a
           relationship among those that are specified in the second
           argument or undef otherwise. get_child_terms is a special
           case of get_descendant_terms, limiting the search to the
           direct descendants.
 Example :
 Returns : Array of TermI objects.
 Args    : First argument is the term of interest, second is the list of
           relationship type terms.

=cut

sub get_child_terms {
    my ( $self, $term, @relationship_types ) = @_;

    $self->throw("must provide TermI compliant object")
        unless defined($term) && $term->isa("Bio::Ontology::TermI");

    return $self->_filter_unmarked(
        $self->get_term_by_identifier(
            $self->_typed_traversal(
                $self->_relationship_store, 1, $term->identifier, @relationship_types
            )
        )
    );
}

=head2 get_descendant_terms

 Title   : get_descendant_terms
 Usage   : get_descendant_terms(TermI term, TermI rel_types): TermI
           get_child_terms(TermI term, RelationshipType predicate_terms): TermI
 Function: Retrieves all descendant terms of a given term, that
           satisfy a relationship among those that are specified in
           the second argument or undef otherwise. Uses
           _typed_traversal to find all descendants.

 Example :
 Returns : Array of TermI objects.
 Args    : First argument is the term of interest, second is the list of
           relationship type terms.

=cut

sub get_descendant_terms {
    my ( $self, $term, @relationship_types ) = @_;

    $self->throw("must provide TermI compliant object")
        unless defined($term) && $term->isa("Bio::Ontology::TermI");

    return $self->_filter_unmarked(
        $self->_filter_repeated(
            $self->get_term_by_identifier(
                $self->_typed_traversal(
                    $self->_relationship_store, 0, $term->identifier, @relationship_types
                )
            )
        )
    );
}

=head2 get_parent_terms

 Title   : get_parent_terms
 Usage   : get_parent_terms(TermI term, TermI predicate_terms): TermI
           get_child_terms(TermI term, RelationshipType predicate_terms): TermI
 Function: Retrieves all parent terms of a given term, that satisfy a
           relationship among those that are specified in the second
           argument or undef otherwise. get_parent_terms is a special
           case of get_ancestor_terms, limiting the search to the
           direct ancestors.

 Example :
 Returns : Array of TermI objects.
 Args    : First argument is the term of interest, second is the list of relationship type terms.

=cut

sub get_parent_terms {
    my ( $self, $term, @relationship_types ) = @_;
    $self->throw("term must be a valid object, not undef") unless defined $term;

    return $self->_filter_unmarked(
        $self->get_term_by_identifier(
            $self->_typed_traversal(
                $self->_inverted_relationship_store,
                1, $term->identifier, @relationship_types
            )
        )
    );
}

=head2 get_ancestor_terms

 Title   : get_ancestor_terms
 Usage   : get_ancestor_terms(TermI term, TermI predicate_terms): TermI
           get_child_terms(TermI term, RelationshipType predicate_terms): TermI
 Function: Retrieves all ancestor terms of a given term, that satisfy
           a relationship among those that are specified in the second
           argument or undef otherwise. Uses _typed_traversal to find
           all ancestors.

 Example :
 Returns : Array of TermI objects.
 Args    : First argument is the term of interest, second is the list
           of relationship type terms.

=cut

sub get_ancestor_terms {
    my ( $self, $term, @relationship_types ) = @_;
    $self->throw("term must be a valid object, not undef") unless defined $term;

    return $self->_filter_unmarked(
        $self->_filter_repeated(
            $self->get_term_by_identifier(
                $self->_typed_traversal(
                    $self->_inverted_relationship_store, 0,
                    $term->identifier,                   @relationship_types
                )
            )
        )
    );
}

=head2 get_leaf_terms

 Title   : get_leaf_terms
 Usage   : get_leaf_terms(): TermI
 Function: Retrieves all leaf terms from the ontology. Leaf term is a term w/o descendants.
 Example : @leaf_terms = $obj->get_leaf_terms()
 Returns : Array of TermI objects.
 Args    :

=cut

sub get_leaf_terms {
    my ($self) = @_;
    my @leaf_terms;

    foreach my $term ( values %{ $self->_term_store } ) {
        push @leaf_terms, $term
            if !defined $self->_relationship_store->{ $term->identifier }
                && defined $self->_instantiated_terms_store->{ $term->identifier };
    }

    return @leaf_terms;
}

=head2 get_root_terms

 Title   : get_root_terms
 Usage   : get_root_terms(): TermI
 Function: Retrieves all root terms from the ontology. Root term is a term w/o descendants.
 Example : @root_terms = $obj->get_root_terms()
 Returns : Array of TermI objects.
 Args    :

=cut

sub get_root_terms {
    my ($self) = @_;
    my @root_terms;

    foreach my $term ( values %{ $self->_term_store } ) {
        push @root_terms, $term
            if !defined $self->_inverted_relationship_store->{ $term->identifier }
                && defined $self->_instantiated_terms_store->{ $term->identifier };
    }

    return @root_terms;
}

=head2 _filter_repeated

 Title   : _filter_repeated
 Usage   : @lst = $self->_filter_repeated(@old_lst);
 Function: Removes repeated terms
 Example :
 Returns : List of unique TermI objects
 Args    : List of TermI objects

=cut

sub _filter_repeated {
    my ( $self, @args ) = @_;
    my %h;

    foreach my $element (@args) {
        $h{ $element->identifier } = $element if !defined $h{ $element->identifier };
    }

    return values %h;
}

=head2 get_all_terms

 Title   : get_all_terms
 Usage   : get_all_terms(): TermI
 Function: Retrieves all terms currently stored in the ontology.
 Example : @all_terms = $obj->get_all_terms()
 Returns : Array of TermI objects.
 Args    :

=cut

sub get_all_terms {
    my ($self) = @_;

    return $self->_filter_unmarked( values %{ $self->_term_store } );
}

=head2 find_terms

 Title   : find_terms
 Usage   : ($term) = $oe->find_terms(-identifier => "SO:0000263");
 Function: Find term instances matching queries for their attributes.

           This implementation can efficiently resolve queries by
           identifier.

 Example :
 Returns : an array of zero or more Bio::Ontology::TermI objects
 Args    : Named parameters. The following parameters should be recognized
           by any implementations:

              -identifier    query by the given identifier
              -name          query by the given name

=cut

sub find_terms {
    my ( $self, @args ) = @_;
    my @terms;

    my ( $id, $name ) = $self->_rearrange( [qw(IDENTIFIER NAME)], @args );

    if ( defined($id) ) {
        @terms = $self->get_term_by_identifier($id);
    } else {
        @terms = $self->get_all_terms();
    }
    if ( defined($name) ) {
        @terms = grep { $_->name() eq $name; } @terms;
    }
    return @terms;
}

=head2 relationship_factory

 Title   : relationship_factory
 Usage   : $fact = $obj->relationship_factory()
 Function: Get/set the object factory to be used when relationship
           objects are created by the implementation on-the-fly.

 Example :
 Returns : value of relationship_factory (a Bio::Factory::ObjectFactoryI
           compliant object)
 Args    : on set, a Bio::Factory::ObjectFactoryI compliant object

=cut

sub relationship_factory {
    my $self = shift;

    return $self->{'relationship_factory'} = shift if @_;
    return $self->{'relationship_factory'};
}

=head2 term_factory

 Title   : term_factory
 Usage   : $fact = $obj->term_factory()
 Function: Get/set the object factory to be used when term objects are
           created by the implementation on-the-fly.

           Note that this ontology engine implementation does not
           create term objects on the fly, and therefore setting this
           attribute is meaningless.

 Example :
 Returns : value of term_factory (a Bio::Factory::ObjectFactoryI
           compliant object)
 Args    : on set, a Bio::Factory::ObjectFactoryI compliant object

=cut

sub term_factory {
    my $self = shift;

    if (@_) {
        $self->warn(
            "setting term factory, but " . ref($self) . " does not create terms on-the-fly" );
        return $self->{'term_factory'} = shift;
    }
    return $self->{'term_factory'};
}

=head2 _filter_unmarked

 Title   : _filter_unmarked
 Usage   : _filter_unmarked(TermI terms): TermI
 Function: Removes the uninstantiated terms from the list of terms
 Example :
 Returns : array of fully instantiated TermI objects
 Args    : array of TermI objects

=cut

sub _filter_unmarked {
    my ( $self, @terms ) = @_;
    my @filtered_terms = ();

    if ( scalar(@terms) >= 1 ) {
        foreach my $term (@terms) {
            push @filtered_terms, $term
                if defined $self->_instantiated_terms_store->{ $term->identifier };
        }
    }

    return @filtered_terms;
}

=head2 remove_term_by_id

 Title   : remove_term_by_id
 Usage   : remove_term_by_id(String id): TermI
 Function: Removes TermI object from the ontology engine using the
           string id as an identifier. Current implementation does not
           enforce consistency of the relationships using that term.
 Example : $term = $soe->remove_term_by_id($id);
 Returns : Object of class TermI or undef if not found.
 Args    : The string identifier of a term.

=cut

sub remove_term_by_id {
    my ( $self, $id ) = @_;

    if ( $self->get_term_by_identifier($id) ) {
        my $term = $self->{_term_store}->{$id};
        delete $self->{_term_store}->{$id};
        return $term;
    } else {
        $self->warn("Term with id '$id' is not in the term store");
        return;
    }
}

=head2 to_string

 Title   : to_string
 Usage   : print $sv->to_string();
 Function: Currently returns formatted string containing the number of
           terms and number of relationships from the ontology engine.
 Example : print $sv->to_string();
 Returns :
 Args    :

=cut

sub to_string {
    my ($self) = @_;
    my $s = "";

    $s .= "-- # Terms:\n";
    $s .= scalar( $self->get_all_terms ) . "\n";
    $s .= "-- # Relationships:\n";
    $s .= $self->_get_number_rels . "\n";

    return $s;
}

=head2 _unique_termid

 Title   : _unique_termid
 Usage   :
 Function: Returns a string that can be used as ID using fail-over
           approaches.

           If the identifier attribute is not set, it uses the
           combination of name and ontology name, provided both are
           set. If they are not, it returns the name alone.

           Note that this is a private method. Call from inheriting
           classes but not from outside.

 Example :
 Returns : a string
 Args    : a Bio::Ontology::TermI compliant object

=cut

sub _unique_termid {
    my $self = shift;
    my $term = shift;

    return $term->identifier() if $term->identifier();
    my $id = $term->ontology->name() if $term->ontology();
    if ($id) {
        $id .= '|';
    } else {
        $id = '';
    }
    $id .= $term->name();
}

#################################################################
# aliases
#################################################################

*get_relationship_types = \&get_predicate_terms;

1;