| 1 | #----------------------------------------------------------------- | ||
| 2 | # MOBY::RDF::Parsers::DatatypeParser | ||
| 3 | # Author: Edward Kawas <edward.kawas@gmail.com>, | ||
| 4 | # For copyright and disclaimer see below. | ||
| 5 | # | ||
| 6 | # $Id: DatatypeParser.pm,v 1.3 2008/11/25 18:05:44 kawas Exp $ | ||
| 7 | #----------------------------------------------------------------- | ||
| 8 | package MOBY::RDF::Parsers::DatatypeParser; | ||
| 9 | use strict; | ||
| 10 | |||
| 11 | # imports | ||
| 12 | use RDF::Core::Model::Parser; | ||
| 13 | use RDF::Core::Model; | ||
| 14 | use RDF::Core::Storage::Memory; | ||
| 15 | use RDF::Core::Resource; | ||
| 16 | |||
| 17 | use MOBY::RDF::Utils; | ||
| 18 | use MOBY::RDF::Predicates::DC_PROTEGE; | ||
| 19 | use MOBY::RDF::Predicates::MOBY_PREDICATES; | ||
| 20 | use MOBY::RDF::Predicates::OMG_LSID; | ||
| 21 | use MOBY::RDF::Predicates::RDF; | ||
| 22 | use MOBY::RDF::Predicates::RDFS; | ||
| 23 | |||
| 24 | use vars qw /$VERSION/; | ||
| 25 | $VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /: (\d+)\.(\d+)/; | ||
| 26 | |||
| 27 | =head1 NAME | ||
| 28 | |||
| 29 | DatatypeParser - An module for obtaining datatypes from the RDF Namespace ontology | ||
| 30 | |||
| 31 | =cut | ||
| 32 | |||
| 33 | =head1 SYNOPSIS | ||
| 34 | |||
| 35 | use MOBY::RDF::Parsers::DatatypeParser; | ||
| 36 | use Data::Dumper; | ||
| 37 | |||
| 38 | # construct a parser for datatypes | ||
| 39 | my $parser = MOBY::RDF::Parsers::DatatypeParser->new(); | ||
| 40 | |||
| 41 | # get all datatypes from a URL | ||
| 42 | my $namespace_href = $parser->getDatatypes('http://biomoby.org/RESOURCES/MOBY-S/Objects'); | ||
| 43 | |||
| 44 | # print out details regarding 'BasicGFFSequenceFeature' | ||
| 45 | print Dumper( $datatype_href->{'BasicGFFSequenceFeature'} ); | ||
| 46 | |||
| 47 | =cut | ||
| 48 | |||
| 49 | =head1 DESCRIPTION | ||
| 50 | |||
| 51 | This module contains the methods required to download and parse Namespace RDF into individual datatypes | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | =head1 AUTHORS | ||
| 56 | |||
| 57 | Edward Kawas (edward.kawas [at] gmail [dot] com) | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | #----------------------------------------------------------------- | ||
| 62 | |||
| 63 | =head1 SUBROUTINES | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | =head2 new | ||
| 68 | |||
| 69 | Contructs a new DatatypeParser. | ||
| 70 | |||
| 71 | Input: none. | ||
| 72 | |||
| 73 | Example: | ||
| 74 | |||
| 75 | MOBY::RDF::Parsers::DatatypeParser->new() | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | sub new { | ||
| 80 | my ($class) = @_; | ||
| 81 | |||
| 82 | # create an object | ||
| 83 | my $self = bless {}, ref($class) || $class; | ||
| 84 | |||
| 85 | # done | ||
| 86 | return $self; | ||
| 87 | } | ||
| 88 | |||
| 89 | =head2 getDatatypes | ||
| 90 | |||
| 91 | Downloads RDF from $url, parses it and returns a hashref of hashes. | ||
| 92 | |||
| 93 | The key into the hashref is a datatype name and the hash value | ||
| 94 | contains information on that datatype. | ||
| 95 | |||
| 96 | The keys for the inner hash are: | ||
| 97 | |||
| 98 | objectLSID => "urn:lsid:..." | ||
| 99 | description => "a human-readable description of the object" | ||
| 100 | contactEmail => "your@email.address" | ||
| 101 | authURI => "URI of the registrar of this object" | ||
| 102 | Relationships => { | ||
| 103 | relationshipType1 => [ | ||
| 104 | { | ||
| 105 | object => Object1, | ||
| 106 | articleName => ArticleName1, | ||
| 107 | lsid => lsid1 | ||
| 108 | }, | ||
| 109 | { | ||
| 110 | object => Object2, | ||
| 111 | articleName => ArticleName2, | ||
| 112 | lsid => lsid2 | ||
| 113 | } | ||
| 114 | ], | ||
| 115 | relationshipType2 => [ | ||
| 116 | { | ||
| 117 | object => Object3, | ||
| 118 | articleName => ArticleName3, | ||
| 119 | lsid => lsid3 | ||
| 120 | } | ||
| 121 | ] | ||
| 122 | } | ||
| 123 | |||
| 124 | The returned hashref is the same structure as the one returned by B<MOBY::Client::Central-E<gt>retrieveObjectDefinition> | ||
| 125 | |||
| 126 | Input: a scalar URL | ||
| 127 | |||
| 128 | Example: | ||
| 129 | |||
| 130 | my $parser = MOBY::RDF::Parsers::DatatypeParser->new(); | ||
| 131 | my $datatype_href = $parser->getDatatypes('http://biomoby.org/RESOURCES/MOBY-S/Objects'); | ||
| 132 | |||
| 133 | =cut | ||
| 134 | |||
| 135 | sub getDatatypes { | ||
| 136 | |||
| 137 | my ($self, $url) = @_; | ||
| 138 | my %hash = (); | ||
| 139 | return \%hash unless $url; | ||
| 140 | |||
| 141 | # download string from url | ||
| 142 | my $rdf = undef; | ||
| 143 | |||
| 144 | # 'try/catch' | ||
| 145 | eval { | ||
| 146 | $rdf = MOBY::RDF::Utils->new()->getHttpRequestByURL($url); | ||
| 147 | }; | ||
| 148 | return \%hash unless $rdf; | ||
| 149 | |||
| 150 | # create RDF model and populate | ||
| 151 | my $storage = new RDF::Core::Storage::Memory; | ||
| 152 | my $model = new RDF::Core::Model (Storage => $storage); | ||
| 153 | my %options = (Model => $model, | ||
| 154 | Source => $rdf, | ||
| 155 | SourceType => 'string', | ||
| 156 | BaseURI => "$url", | ||
| 157 | ); | ||
| 158 | my $parser = new RDF::Core::Model::Parser(%options); | ||
| 159 | $parser->parse; | ||
| 160 | |||
| 161 | # get information from the model | ||
| 162 | my $enumerator = $model->getStmts(undef, new RDF::Core::Resource( MOBY::RDF::Predicates::DC_PROTEGE->publisher ), undef); | ||
| 163 | my $statement = $enumerator->getFirst; | ||
| 164 | while (defined $statement) { | ||
| 165 | my $datatype = $statement->getSubject->getLocalValue; | ||
| 166 | my $authURI = $statement->getObject->getValue; | ||
| 167 | |||
| 168 | my $definition = $model->getObjects($statement->getSubject, new RDF::Core::Resource( MOBY::RDF::Predicates::RDFS->comment ) ); | ||
| 169 | $definition = "" unless $$definition[0]; | ||
| 170 | $definition = $$definition[0]->getValue if ref ($definition) eq 'ARRAY' and $$definition[0]; | ||
| 171 | |||
| 172 | my $email = $model->getObjects($statement->getSubject, new RDF::Core::Resource( MOBY::RDF::Predicates::DC_PROTEGE->creator ) ); | ||
| 173 | $email = "" unless $$email[0]; | ||
| 174 | $email = $$email[0]->getValue if ref ($email) eq 'ARRAY' and $$email[0]; | ||
| 175 | |||
| 176 | my $lsid = $model->getObjects($statement->getSubject, new RDF::Core::Resource( MOBY::RDF::Predicates::DC_PROTEGE->identifier ) ); | ||
| 177 | $lsid = "" unless $$lsid[0]; | ||
| 178 | $lsid = $$lsid[0]->getValue if ref ($lsid) eq 'ARRAY' and $$lsid[0]; | ||
| 179 | |||
| 180 | #process relationships | ||
| 181 | my $isa = undef; | ||
| 182 | do { | ||
| 183 | $isa = $model->getObjects($statement->getSubject, new RDF::Core::Resource( MOBY::RDF::Predicates::RDFS->subClassOf ) ); | ||
| 184 | $isa = "" unless $$isa[0]; | ||
| 185 | $isa = $$isa[0]->getLocalValue if ref ($isa) eq 'ARRAY' and $$isa[0]; | ||
| 186 | } unless $datatype eq 'Object'; | ||
| 187 | |||
| 188 | |||
| 189 | $hash{$datatype} = { | ||
| 190 | contactEmail => $email, | ||
| 191 | authURI => $authURI, | ||
| 192 | objectLSID => $lsid, | ||
| 193 | description => $definition, | ||
| 194 | }; | ||
| 195 | push @ {$hash{$datatype}{Relationships}{ISA}}, {object => $isa, lsid =>'', articleName =>''} if $isa; | ||
| 196 | |||
| 197 | #process has relationships | ||
| 198 | my $has = $model->getObjects($statement->getSubject, new RDF::Core::Resource( MOBY::RDF::Predicates::MOBY_PREDICATES->has ) ); | ||
| 199 | if ($has and ref ($has) eq 'ARRAY') { | ||
| 200 | for my $resource (@{$has}) { | ||
| 201 | my $type = $model->getObjects($resource, new RDF::Core::Resource( MOBY::RDF::Predicates::RDF->type ) ); | ||
| 202 | $type = "" unless $$type[0]; | ||
| 203 | $type= $$type[0]->getLocalValue if ref ($type) eq 'ARRAY' and $$type[0]; | ||
| 204 | |||
| 205 | my $name = $model->getObjects($resource, new RDF::Core::Resource( MOBY::RDF::Predicates::MOBY_PREDICATES->articleName ) ); | ||
| 206 | $name = "" unless $$name[0]; | ||
| 207 | $name= $$name[0]->getValue if ref ($name) eq 'ARRAY' and $$name[0]; | ||
| 208 | push @ {$hash{$datatype}{Relationships}{HAS}}, {object => $type, lsid =>'', articleName =>$name}; | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | #process hasa relationships | ||
| 213 | my $hasa = $model->getObjects($statement->getSubject, new RDF::Core::Resource( MOBY::RDF::Predicates::MOBY_PREDICATES->hasa ) ); | ||
| 214 | if ($hasa and ref ($hasa) eq 'ARRAY') { | ||
| 215 | for my $resource (@{$hasa}) { | ||
| 216 | my $type = $model->getObjects($resource, new RDF::Core::Resource( MOBY::RDF::Predicates::RDF->type ) ); | ||
| 217 | $type = "" unless $$type[0]; | ||
| 218 | $type= $$type[0]->getLocalValue if ref ($type) eq 'ARRAY' and $$type[0]; | ||
| 219 | |||
| 220 | my $name = $model->getObjects($resource, new RDF::Core::Resource( MOBY::RDF::Predicates::MOBY_PREDICATES->articleName ) ); | ||
| 221 | $name = "" unless $$name[0]; | ||
| 222 | $name= $$name[0]->getValue if ref ($name) eq 'ARRAY' and $$name[0]; | ||
| 223 | push @ {$hash{$datatype}{Relationships}{HASA}}, {object => $type, lsid =>'', articleName =>$name}; | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | $statement = $enumerator->getNext; | ||
| 228 | } | ||
| 229 | $enumerator->close; | ||
| 230 | |||
| 231 | # return hash | ||
| 232 | return \%hash; | ||
| 233 | |||
| 234 | } | ||
| 235 | |||
| 236 | 1; | ||
| 237 | __END__ |