Code optimization. Can force a skip ISA checking for small speed boost
[bioperl-live.git] / t / obo_parser.t
blobd37ef5ce0d4dbeb27fd3fa754aee59ca77806425
1 # -*-Perl-*- Test Harness script for Bioperl
2 # $Id$
4 use strict;
6 BEGIN {
7     use lib 't/lib';
8     use BioperlTest;
9     
10     test_begin(-tests => 45,
11                            -requires_module => 'Graph');
12         
13         use_ok('Bio::OntologyIO');
14         use_ok('Bio::Ontology::RelationshipType');
17 my $IS_A    = Bio::Ontology::RelationshipType->get_instance( "IS_A" );
18 my $PART_OF = Bio::Ontology::RelationshipType->get_instance( "PART_OF" );
20 my $parser = Bio::OntologyIO->new(
21                       -format    => "obo",
22                       -file      => test_input_file('so.obo'));
24 my $ont = $parser->next_ontology();
25 ok ($ont);
26 is ($ont->name(), "sequence");
28 my @roots = $ont->get_root_terms();
29 is (scalar(@roots), 1);
30 is ($roots[0]->name(), "Sequence_Ontology");
31 is ($roots[0]->identifier(), "SO:0000000");
33 my @terms = $ont->get_child_terms($roots[0]);
34 is (scalar(@terms), 5);
35 my ($term) = grep { $_->name() eq "variation_operation"; } @terms;
36 ok $term;
37 ($term) = grep { $_->name() eq "sequence_attribute"; } @terms;
38 ok $term;
39 ($term) = grep { $_->name() eq "consequences_of_mutation"; } @terms;
40 ok $term;
41 ($term) = grep { $_->name() eq "chromosome_variation"; } @terms;
42 ok $term;
43 ($term) = grep { $_->name() eq "located_sequence_feature"; } @terms;
44 ok $term;
46 @terms = $ont->get_child_terms($terms[0]);
47 is (scalar(@terms), 5);
48 ($term) = grep { $_->name() eq "translocate"; } @terms;
49 ok $term;
50 ($term) = grep { $_->name() eq "delete"; } @terms;
51 ok $term;
52 ($term) = grep { $_->name() eq "insert"; } @terms;
53 ok $term;
54 ($term) = grep { $_->name() eq "substitute"; } @terms;
55 ok $term;
56 ($term) = grep { $_->name() eq "invert"; } @terms;
57 ok $term;
59 my $featterm = $terms[0];
60 @terms = $ont->get_child_terms($featterm);
61 is (scalar(@terms), 2);
63 # substitution has two parents, see whether this is handled
64 @terms = $ont->find_terms(-name => "substitution");
65 $term =  $terms[0];
66 is ($term->name(), "substitution");
68 # search using obo terms;
69 @terms = $ont->find_identically_named_terms($term);
70 is scalar @terms, 1;
71 @terms = $ont->find_identical_terms($term);
72 is scalar @terms, 1;
73 @terms = $ont->find_similar_terms($term);
74 is scalar @terms, 7;
76 @terms = $ont->get_ancestor_terms($term);
77 is (scalar(@terms), 6);
78 is (scalar(grep { $_->name() eq "region"; } @terms), 1);
79 is (scalar(grep { $_->name() eq "sequence_variant"; } @terms), 1);
81 # processed_transcript has part-of and is-a children
83 @terms = $ont->find_terms(-name => "processed_transcript");;
84 $term = $terms[0];
86 @terms = $ont->get_child_terms($term);
87 is (scalar(@terms), 5);
88 @terms = $ont->get_child_terms($term, $PART_OF);
89 is (scalar(@terms), 2);
90 @terms = $ont->get_child_terms($term, $IS_A);
91 is (scalar(@terms), 3);
92 @terms = $ont->get_child_terms($term, $PART_OF, $IS_A);
93 is (scalar(@terms), 5);
95 # TF_binding_site has 2 parents and different relationships in the two
96 # paths up (although the relationships to its two parents are of the
97 # same type, namely is-a)
98 @terms = $ont->find_terms(-name => "TF_binding_site");;
99 $term = $terms[0];
101 @terms = $ont->get_parent_terms($term);
102 is (scalar(@terms), 2);
103 my ($pterm) = grep { $_->name eq "regulatory_region"; } @terms;
104 ok $pterm;
105 @terms = $ont->get_parent_terms($term, $PART_OF);
106 is (scalar(@terms), 0);
107 @terms = $ont->get_parent_terms($term, $IS_A);
108 is (scalar(@terms), 2);
109 @terms = $ont->get_parent_terms($term, $PART_OF, $IS_A);
110 is (scalar(@terms), 2);
113 # pull out all relationships
114 my @rels = $ont->get_relationships();
115 my @relset = grep { $_->object_term->name eq "Sequence_Ontology"; } @rels;
116 is (scalar(@relset), 5);
117 @relset = grep { $_->subject_term->name eq "Sequence_Ontology"; } @rels;
118 is (scalar(@relset), 0);
120 # relationships for a specific term only
121 ($term) = $ont->find_terms(-identifier => "SO:0000082");
122 ok ($term);
123 is ($term->identifier, "SO:0000082");
124 is ($term->name, "processed_transcript_attribute");
125 @rels = $ont->get_relationships($term);
126 is (scalar(@rels), 5);
127 @relset = grep { $_->predicate_term->name eq "IS_A"; } @rels;
128 is (scalar(@relset), 5);
129 @relset = grep { $_->object_term->identifier eq "SO:0000082"; } @rels;
130 is (scalar(@relset), 4);
131 @relset = grep { $_->subject_term->identifier eq "SO:0000082"; } @rels;
132 is (scalar(@relset), 1);