/
12ontology.t
187 lines (171 loc) · 6.18 KB
/
12ontology.t
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
# -*-Perl-*-
# $Id$
BEGIN {
use lib 't';
use Bio::Root::Test;
test_begin(-tests => 740);
use_ok('DBTestHarness');
use_ok('Bio::OntologyIO');
}
$biosql = DBTestHarness->new("biosql");
ok $biosql;
$db = $biosql->get_DBAdaptor();
ok $db;
my $ontio = Bio::OntologyIO->new(-file => test_input_file('sofa.ontology'),
-format => 'soflat');
ok ($ontio);
my $ont = $ontio->next_ontology();
ok ($ont);
$ont->name("My Test Ontology"); # avoid clashes
# insert by inserting all relationships (there are no isolated terms in SOFA)
foreach my $rel ($ont->get_relationships()) {
# change ID prefix to avoid clashes
foreach my $term_meth ("subject_term","object_term") {
my $id = $rel->$term_meth->identifier();
next if $id =~ /^MYTO/;
$rel->$term_meth->identifier("MYTO:".substr($id,3)) if $id;
}
# make persistent and insert
my $prel = $db->create_persistent($rel);
ok ($prel->create());
ok ($prel->primary_key());
}
# now get the ontology back from the database
my $dbont = Bio::Ontology::Ontology->new(-name => "My Test Ontology");
$dbont = $db->get_object_adaptor($dbont)->find_by_unique_key($dbont);
ok ($dbont);
ok ($dbont->primary_key());
# set up the query to get all relationships
my $queryrels =
Bio::DB::Query::BioQuery->new(
-datacollections => ["Bio::Ontology::OntologyI=>Bio::Ontology::RelationshipI"],
-where => ["Bio::Ontology::OntologyI::primary_key = ".
$dbont->primary_key()
]
);
my $reladp = $db->get_object_adaptor("Bio::Ontology::RelationshipI");
my $qres = $reladp->find_by_query($queryrels);
while(my $rel = $qres->next_object()) {
is ($rel->ontology->name, "My Test Ontology");
$dbont->add_term($rel->subject_term);
$dbont->add_term($rel->object_term);
#$dbont->add_term($rel->predicate_term);
$dbont->add_relationship($rel);
}
# now query the ontology
my ($term) = $dbont->find_terms(-identifier => "MYTO:0000233");
ok ($term);
is ($term->identifier, "MYTO:0000233");
is ($term->name, "processed_transcript");
@rels = $dbont->get_relationships($term);
is (scalar(@rels), 5);
@relset = grep { $_->predicate_term->name eq "IS_A"; } @rels;
is (scalar(@relset), 3);
@relset = grep { $_->object_term->identifier eq "MYTO:0000233"; } @rels;
is (scalar(@relset), 4);
# check for correct storage and retrieval of synonyms and dbxrefs
($term) = $dbont->find_terms(-identifier => "MYTO:0000203");
ok ($term);
is ($term->name, "untranslated_region");
my @syns = $term->get_synonyms();
is (scalar(@syns), 1);
is ($syns[0], "UTR");
# modify, update, and re-retrieve to check with multiple synonyms, and with
# dbxrefs (this version of SOFA doesn't come with any dbxrefs)
$term->add_synonym("junk DNA");
$term->add_dbxref(-dbxrefs => [Bio::Annotation::DBLink->new(-database => "MYDB",
-primary_id => "yaddayadda")]);
ok ($term->store());
$term = $term->adaptor->find_by_primary_key($term->primary_key);
ok ($term);
# now test
@syns = $term->get_synonyms();
is (scalar(@syns), 2);
is (scalar(grep { $_ eq "junk DNA"; } @syns), 1);
is (scalar($term->get_dbxrefs()), 1);
#
# test the transitive closure computations
#
my $ontadp = $db->get_object_adaptor("Bio::Ontology::OntologyI");
my $ontname = "My BioSQL Predicate Ontology";
my $id_pred = Bio::Ontology::Term->new(-name => "identity",
-ontology => $ontname);
my $superpred = Bio::Ontology::Term->new(-name => "PART_OF",
-ontology => $ontname);
my $subcl_pred = Bio::Ontology::Term->new(-name => "implies",
-ontology => $ontname);
ok ($ontadp->compute_transitive_closure($ont,
-truncate => 1,
-predicate_superclass => $superpred,
-subclass_predicate => $subcl_pred,
-identity_predicate => $id_pred));
#
# now query and test the results
# set up the query to get all relationships
$query = Bio::DB::Query::BioQuery->new(
-datacollections => ["Bio::Ontology::OntologyI=>Bio::Ontology::PathI o",
"Bio::Ontology::TermI=>Bio::Ontology::PathI tsubj::subject",
"Bio::Ontology::TermI=>Bio::Ontology::PathI tobj::object",
],
-where => ["o.name = 'My Test Ontology'",
"tobj.name = 'gene'",
"tsubj.name = 'exon'"]
);
my $pathadp = $db->get_object_adaptor("Bio::Ontology::PathI");
$qres = $pathadp->find_by_query($query);
my $n = 0;
while(my $path = $qres->next_object()) {
is ($path->ontology->name, "My Test Ontology");
is ($path->subject_term->name, "exon");
is ($path->object_term->name, "gene");
is ($path->predicate_term->name, "PART_OF");
$n++;
}
is ($n, 1);
# test for distance zero paths
$query = Bio::DB::Query::BioQuery->new(
-datacollections => [
"Bio::Ontology::OntologyI=>Bio::Ontology::PathI o",
"Bio::Ontology::TermI=>Bio::Ontology::PathI tpred::predicate",
],
-where => ["o.name = 'My Test Ontology'",
"tpred.name = 'identity'"]
);
$qres = $pathadp->find_by_query($query);
$n = 0;
while(my $path = $qres->next_object()) {
is ($path->ontology->name, "My Test Ontology");
is ($path->subject_term->name, $path->object_term->name);
is ($path->predicate_term->name, "identity");
is ($path->predicate_term->ontology->name, "My BioSQL Predicate Ontology");
is ($path->distance, 0);
$n++ if $path->subject_term->identifier; # don't count hard-coded but
# unused relationship types
}
is ($n, 86);
#
# test removal of relationships
#
$ont = Bio::Ontology::Ontology->new(-name => "My Test Ontology");
ok ($reladp->remove_all_relationships($ont));
# try to find any relationships
$reladp = $db->get_object_adaptor("Bio::Ontology::RelationshipI");
$qres = $reladp->find_by_query($queryrels);
ok ($qres);
$n = 0;
while ($qres->next_object()) {
$n++;
}
is ($n, 0);
# there should still be terms though
my $dbterm = Bio::Ontology::Term->new(-identifier => "MYTO:0000233");
$dbterm = $db->get_object_adaptor($dbterm)->find_by_unique_key($dbterm);
ok ($dbterm);
ok ($dbterm->primary_key);
ok ($dbterm->ontology);
is ($dbterm->ontology->name, "My Test Ontology");
is ($dbterm->identifier, "MYTO:0000233");
($term) = $dbont->find_terms(-identifier => "MYTO:0000233");
ok ($term);
is ($dbterm->name, $term->name);
is (scalar($dbterm->get_synonyms()), scalar($term->get_synonyms()));