blib/lib/MOBY/RDF/Ontologies/Objects.pm untested

filter: Statementstmt 0.0% 0 / 190 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 277 CC 16 SCAR 29.9
CC
16
Cov
0%
createByName
72.0
_processDatatype
20.0
createAll
12.0
CRAP 19.9
1 #-----------------------------------------------------------------
2 # MOBY::RDF::Ontologies::Objects
3 # Author: Edward Kawas <edward.kawas@gmail.com>,
4 # For copyright and disclaimer see below.
5 #
6 # $Id: Objects.pm,v 1.5 2008/09/02 13:12:46 kawas Exp $
7 #-----------------------------------------------------------------
8  
9 package MOBY::RDF::Ontologies::Objects;
10  
11 use RDF::Core;
12 use RDF::Core::Storage::Memory;
13 use RDF::Core::Model;
14 use RDF::Core::Literal;
15 use RDF::Core::Statement;
16 use RDF::Core::Model::Serializer;
17 use RDF::Core::NodeFactory;
18  
19 use MOBY::Config;
20  
21 use MOBY::RDF::Predicates::DC_PROTEGE;
22 use MOBY::RDF::Predicates::MOBY_PREDICATES;
23 use MOBY::RDF::Predicates::OMG_LSID;
24 use MOBY::RDF::Predicates::RDF;
25 use MOBY::RDF::Predicates::RDFS;
26  
27 use MOBY::RDF::Utils;
28  
29 use MIME::Base64;
30 use CGI;
31 use strict;
32  
33 use vars qw /$VERSION/;
34 $VERSION = sprintf "%d.%02d", q$Revision: 1.5 $ =~ /: (\d+)\.(\d+)/;
35  
36 #-----------------------------------------------------------------
37 # load all modules needed for my attributes
38 #-----------------------------------------------------------------
39  
40 =head1 NAME
41  
42 MOBY::RDF::Ontologies::Objects - Create RDF/OWL for Moby datatypes
43  
44 =head1 SYNOPSIS
45  
46 use MOBY::RDF::Ontologies::Objects;
47 my $x = MOBY::RDF::Ontologies::Objects->new;
48  
49 # get RDF for all datatypes in unformatted XML
50 my $rdf_all = $x->createAll({ prettyPrint => 'no' });
51  
52 # get RDF for a specific datatype as formatted XML
53 my $rdf = $x->createByName( { term => 'DNASequence' });
54  
55 =head1 DESCRIPTION
56  
57 This module creates RDF/XML for the Objects ontology.
58  
59 =cut
60  
61 =head1 AUTHORS
62  
63 Edward Kawas (edward.kawas [at] gmail [dot] com)
64  
65 =cut
66  
67 #-----------------------------------------------------------------
68  
69 =head1 SUBROUTINES
70  
71 =cut
72  
73 #-----------------------------------------------------------------
74 # new
75 #-----------------------------------------------------------------
76 sub new {
77 my ($class) = @_;
78  
79 # create an object
80 my $self = bless {}, ref($class) || $class;
81
82 # save some information retrieved from mobycentral.config
83 my $CONF = MOBY::Config->new;
84 $self->{uri} = $CONF->{mobyobject}->{resourceURL} || 'http://biomoby.org/RESOURCES/MOBY-S/Objects/';
85 $self->{uri} = $self->{uri} . "/" unless $self->{uri} =~ m/^.*(\/{1})$/;
86
87 $self->{uri_comp} = $self->{uri};
88 $self->{uri_comp} =~ s/\/MOBY\-S\//\/MOBY_SUB_COMPONENT\//;
89  
90 $self->{query_all} = <<END;
91 SELECT ot1.object_type, rt.relationship_type, ot2.object_type, rt.object2_articlename, ot1.description, ot1.object_lsid, ot1.authority, ot1.contact_email
92 FROM object as ot1, object_term2term as rt, object as ot2
93 WHERE ot1.object_id = rt.object1_id and ot2.object_id = rt.object2_id
94 ORDER BY ot1.object_type
95 END
96  
97 # done
98 $self->{query} = <<END;
99 SELECT ot1.object_type, rt.relationship_type, ot2.object_type, rt.object2_articlename, ot1.description, ot1.object_lsid, ot1.authority, ot1.contact_email
100 FROM object as ot1, object_term2term as rt, object as ot2
101 WHERE ot1.object_id = rt.object1_id and ot2.object_id = rt.object2_id and ot1.object_type = ?
102 ORDER BY ot1.object_type
103 END
104  
105 # done
106 return $self;
107 }
108  
109 #-----------------------------------------------------------------
110 # createAll
111 #-----------------------------------------------------------------
112  
113 =head2 createAll
114  
115 Return a string of RDF in XML that represents all of the
116 datatypes in the objects ontology.
117  
118 This routine consumes a hash as input with keys:
119 prettyPrint: whether (yes) or not (no) to output 'pretty print' formatted XML. Defaults to 'yes'.
120  
121 =cut
122  
123 sub createAll {
124 my ( $self, $hash ) = @_;
125  
126 # set up an RDF model
127 my $storage = new RDF::Core::Storage::Memory;
128 my $model = new RDF::Core::Model( Storage => $storage );
129  
130 my $prettyPrint = $hash->{prettyPrint} ? $hash->{prettyPrint} : 'yes';
131  
132 # add root of ontology
133 $self->_addOntologyRoot($model);
134  
135 my $db = MOBY::Config->new()-> getDataAdaptor( source => "mobyobject" )->dbh;
136 my $sth = $db->prepare( $self->{query_all} );
137 $sth->execute;
138  
139 # returns an array of hash references
140 while ( my $ref = $sth->fetchrow_arrayref ) {
141 my $subject = $$ref[0];
142 my $relationship = $$ref[1];
143 my $object = $$ref[2];
144 my $articlename = $$ref[3] || '';
145 my $description = $$ref[4];
146 my $lsid = $$ref[5];
147 my $authority = $$ref[6];
148 my $email = $$ref[7];
149  
150 my $resource = new RDF::Core::Resource( $self->{uri}, $subject );
151 $self->_processDatatype(
152 $model, $resource, $subject, $relationship,
153 $object, $articlename, $description, $lsid,
154 $authority, $email
155 );
156  
157 }
158 $sth->finish();
159 $db->disconnect();
160 my $xml = '';
161 my $serializer = new RDF::Core::Model::Serializer(
162 Model => $model,
163 Output => \$xml,
164 BaseURI => 'URI://BASE/',
165 );
166 $serializer->serialize;
167 return new MOBY::RDF::Utils->prettyPrintXML( { xml => $xml } )
168 unless $prettyPrint eq 'no';
169 return $xml;
170 }
171  
172 =head2 createByName
173  
174 Return a string of RDF in XML that represents a specific datatype in the
175 Objects ontology. This sub routine takes one argument, 'term',
176 that represents the ontology term that you would like to create RDF
177 for. For example, the term 'DNASequence' would return RDF describing
178 DNASequence and all of its parents and container relationships.
179  
180 This routine consumes a hash as input with keys:
181 term: the node to retrieve B<required>
182 prettyPrint: whether (yes) or not (no) to output 'pretty print' formatted XML. Defaults to 'yes'.
183  
184 =cut
185  
186 sub createByName {
187 my ( $self, $hash ) = @_;
188 die "No term specified!" unless $hash->{term};
189  
190 # set up the term that we care about
191 my $term = $hash->{term};
192 my $prettyPrint = $hash->{prettyPrint} ? $hash->{prettyPrint} : 'yes';
193  
194 my $termExists = 0;
195
196 $termExists++ if $term eq "Object";
197  
198 # set up an RDF model
199 my $storage = new RDF::Core::Storage::Memory;
200 my $model = new RDF::Core::Model( Storage => $storage );
201  
202 # add root of ontology
203 $self->_addOntologyRoot($model);
204 my $node_factory = new RDF::Core::NodeFactory();
205  
206 do {
207 my $db = MOBY::Config->new()-> getDataAdaptor( source => "mobyobject" )->dbh;
208 my $sth = $db->prepare( $self->{query} );
209 $sth->execute( ($term) );
210  
211 # this line is here, because there are some datatypes in the ontology not rooted at 'Object' and while illegal, this would kill this code.
212 $term = '';
213  
214 #base case
215 $term = '' if $term eq 'Object';
216  
217 # returns an array of hash references
218 while ( my $ref = $sth->fetchrow_arrayref ) {
219 $termExists++;
220 my $subject = $$ref[0];
221 my $relationship = $$ref[1];
222 my $object = $$ref[2];
223 my $articlename = $$ref[3] || '';
224 my $description = $$ref[4];
225 my $lsid = $$ref[5];
226 my $authority = $$ref[6];
227 my $email = $$ref[7];
228 my $resource = new RDF::Core::Resource( $self->{uri}, $subject );
229 $self->_processDatatype(
230 $model, $resource, $subject, $relationship,
231 $object, $articlename, $description, $lsid,
232 $authority, $email
233 );
234 do {
235 $term = $object;
236 } if $relationship =~ m/.*\:isa$/;
237  
238 }
239 $sth->finish();
240 $db->disconnect();
241  
242 } while ( $term && $term ne '' );
243 my $xml = '';
244 my $serializer = new RDF::Core::Model::Serializer(
245 Model => $model,
246 Output => \$xml,
247 BaseURI => 'URI://BASE/',
248 );
249 $serializer->serialize;
250  
251 # dont output anything unless term exists!
252 unless ( $termExists > 0 ) {
253 $xml = <<END;
254 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"></rdf:RDF>
255 END
256 }
257 return new MOBY::RDF::Utils->prettyPrintXML( { xml => $xml } )
258 unless $prettyPrint eq 'no';
259 return $xml;
260 }
261  
262  
263 #####-----------------------Private Routines--------------------------#####
264  
265 sub _addOntologyRoot {
266 my ( $self, $model ) = @_;
267 my $resource = new RDF::Core::Resource( $self->{uri}, "Object" );
268  
269 # $model->addStmt(
270 # new RDF::Core::Statement(
271 # $resource,
272 # $resource->new( MOBY::RDF::Predicates::RDF->type ),
273 # new RDF::Core::Resource( MOBY::RDF::Predicates::OWL->Class )
274 # )
275 # );
276 $model->addStmt(
277 new RDF::Core::Statement(
278 $resource,
279 $resource->new( MOBY::RDF::Predicates::RDFS->label ),
280 new RDF::Core::Literal("Object")
281 )
282 );
283 $model->addStmt(
284 new RDF::Core::Statement(
285 $resource,
286 $resource->new( MOBY::RDF::Predicates::RDFS->comment ),
287 new RDF::Core::Literal(
288 "a base object class consisting of a namespace and an identifier"
289 )
290 )
291 );
292 $model->addStmt(
293 new RDF::Core::Statement(
294 $resource,
295 $resource->new( MOBY::RDF::Predicates::DC_PROTEGE->publisher ),
296 new RDF::Core::Literal("openinformatics.com")
297 )
298 );
299 $model->addStmt(
300 new RDF::Core::Statement(
301 $resource,
302 $resource->new( MOBY::RDF::Predicates::DC_PROTEGE->identifier ),
303 new RDF::Core::Literal(
304 "urn:lsid:biomoby.org:objectclass:Object:2001-09-21T16-00-00Z"
305 )
306 )
307 );
308 $model->addStmt(
309 new RDF::Core::Statement(
310 $resource,
311 $resource->new( MOBY::RDF::Predicates::DC_PROTEGE->creator ),
312 new RDF::Core::Literal("jason\@openinformatics.com")
313 )
314 );
315  
316 }
317  
318 sub _processDatatype {
319  
320 my (
321 $self, $model, $resource, $subject,
322 $relationship, $object, $articlename, $description,
323 $lsid, $authority, $email
324 )
325 = @_;
326  
327 # $model->addStmt(
328 # new RDF::Core::Statement(
329 # $resource,
330 # $resource->new( MOBY::RDF::Predicates::RDF->type ),
331 # new RDF::Core::Resource( MOBY::RDF::Predicates::OWL->Class )
332 # )
333 # );
334 $model->addStmt(
335 new RDF::Core::Statement(
336 $resource,
337 $resource->new( MOBY::RDF::Predicates::RDFS->label ),
338 new RDF::Core::Literal($subject)
339 )
340 );
341 $model->addStmt(
342 new RDF::Core::Statement(
343 $resource,
344 $resource->new( MOBY::RDF::Predicates::RDFS->comment ),
345 new RDF::Core::Literal($description)
346 )
347 );
348 $model->addStmt(
349 new RDF::Core::Statement(
350 $resource,
351 $resource->new( MOBY::RDF::Predicates::DC_PROTEGE->publisher ),
352 new RDF::Core::Literal($authority)
353 )
354 );
355 $model->addStmt(
356 new RDF::Core::Statement(
357 $resource,
358 $resource->new( MOBY::RDF::Predicates::DC_PROTEGE->identifier ),
359 new RDF::Core::Literal($lsid)
360 )
361 );
362 $model->addStmt(
363 new RDF::Core::Statement(
364 $resource,
365 $resource->new( MOBY::RDF::Predicates::DC_PROTEGE->creator ),
366 new RDF::Core::Literal($email)
367 )
368 );
369  
370 # add subclassof, if applicable
371 do {
372 my $parent = new RDF::Core::Resource( $self->{uri}, $object );
373 $model->addStmt(
374 new RDF::Core::Statement(
375 $resource,
376 $resource->new( MOBY::RDF::Predicates::RDFS->subClassOf ),
377 $parent
378 )
379 );
380 } if $relationship =~ m/.*\:isa$/;
381  
382 # add hasa container relationships
383 do {
384 my $container =
385 new RDF::Core::Resource( $self->{uri_comp},
386 $subject . "_" . CGI::escape(encode_base64("$articlename")) );
387 $model->addStmt(
388 new RDF::Core::Statement(
389 $container,
390 $container->new( MOBY::RDF::Predicates::RDF->type ),
391 new RDF::Core::Resource( $self->{uri}, $object )
392 )
393 );
394 $model->addStmt(
395 new RDF::Core::Statement(
396 $resource,
397 $resource->new( MOBY::RDF::Predicates::MOBY_PREDICATES->hasa ),
398 $container
399 )
400 );
401 $model->addStmt(
402 new RDF::Core::Statement(
403 $container,
404 $container->new(
405 MOBY::RDF::Predicates::MOBY_PREDICATES->articleName
406 ),
407 new RDF::Core::Literal($articlename)
408 )
409 );
410 $self->_processISAs( $model, $object );
411  
412 } if $relationship =~ m/.*\:hasa$/;
413  
414 # add has container relationship
415 do {
416 my $container =
417 new RDF::Core::Resource( $self->{uri_comp},
418 $subject . "_" . CGI::escape (encode_base64("$articlename")));
419 $model->addStmt(
420 new RDF::Core::Statement(
421 $container,
422 $container->new( MOBY::RDF::Predicates::RDF->type ),
423 new RDF::Core::Resource( $self->{uri}, $object )
424 )
425 );
426  
427 $model->addStmt(
428 new RDF::Core::Statement(
429 $resource,
430 $resource->new( MOBY::RDF::Predicates::MOBY_PREDICATES->has ),
431 $container
432 )
433 );
434 $model->addStmt(
435 new RDF::Core::Statement(
436 $container,
437 $container->new(
438 MOBY::RDF::Predicates::MOBY_PREDICATES->articleName
439 ),
440 new RDF::Core::Literal($articlename)
441 )
442 );
443 $self->_processISAs( $model, $object );
444  
445 } if $relationship =~ m/.*\:has$/;
446 }
447  
448 sub _processISAs {
449 my ( $self, $model, $term ) = @_;
450  
451 my $termExists = 0;
452
453
454 do {
455 my $db = MOBY::Config->new()-> getDataAdaptor( source => "mobyobject" )->dbh;
456 my $sth = $db->prepare( $self->{query} );
457 $sth->execute( ($term) );
458  
459 # this line is here, because there are some datatypes in the ontology not rooted at 'Object' and while illegal, this would kill this code.
460 $term = '';
461  
462 #base case
463 $term = '' if $term eq 'Object';
464  
465 # returns an array of hash references
466 while ( my $ref = $sth->fetchrow_arrayref ) {
467 $termExists++;
468 my $subject = $$ref[0];
469 my $relationship = $$ref[1];
470 my $object = $$ref[2];
471 my $articlename = $$ref[3] || '';
472 my $description = $$ref[4];
473 my $lsid = $$ref[5];
474 my $authority = $$ref[6];
475 my $email = $$ref[7];
476 my $resource = new RDF::Core::Resource( $self->{uri}, $subject );
477 $self->_processDatatype(
478 $model, $resource, $subject, $relationship,
479 $object, $articlename, $description, $lsid,
480 $authority, $email
481 );
482 do {
483 $term = $object;
484 } if $relationship =~ m/.*\:isa$/;
485  
486 }
487 $sth->finish();
488 $db->disconnect();
489  
490 } while ( $term && $term ne '' );
491 }
492  
493 1;
494 __END__