| 1 | package SADI::Simple::Utils; | ||
| 2 | { | ||
| 3 | $SADI::Simple::Utils::VERSION = '0.15'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use File::Spec; | ||
| 10 | use RDF::Trine::Model 0.135; | ||
| 11 | use RDF::Trine::Parser 0.135; | ||
| 12 | use RDF::Trine::Serializer 0.135; | ||
| 13 | use File::Spec::Functions; | ||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | SADI::Simple::Utils - internal utils for manipulating RDF | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | There are no public methods in this module. | ||
| 24 | |||
| 25 | =cut | ||
| 26 | |||
| 27 | sub rdfxml_to_n3 | ||
| 28 | { | ||
| 29 | my ($self, $rdfxml) = @_; | ||
| 30 | |||
| 31 | $rdfxml = $self | ||
| 32 | unless ref($self) =~ m/^SADI::Simple::Utils/ or $self =~ /^SADI::Simple::Utils/; | ||
| 33 | |||
| 34 | my $model = RDF::Trine::Model->temporary_model; | ||
| 35 | my $parser = RDF::Trine::Parser->new('rdfxml'); | ||
| 36 | |||
| 37 | eval { $parser->parse_into_model(undef, $rdfxml, $model) }; | ||
| 38 | |||
| 39 | die "failed to convert RDF/XML to TTL, error parsing RDF/XML: $@" if $@; | ||
| 40 | |||
| 41 | my $serializer = RDF::Trine::Serializer->new('turtle'); | ||
| 42 | |||
| 43 | return $self->serialize_model($model, 'text/rdf+n3'); | ||
| 44 | } | ||
| 45 | |||
| 46 | sub n3_to_rdfxml | ||
| 47 | { | ||
| 48 | my ($self, $n3) = @_; | ||
| 49 | |||
| 50 | $n3 = $self | ||
| 51 | unless ref($self) =~ m/^SADI::Simple::Utils/ or $self =~ /^SADI::Simple::Utils/; | ||
| 52 | |||
| 53 | my $model = RDF::Trine::Model->temporary_model; | ||
| 54 | my $parser = RDF::Trine::Parser->new('turtle'); | ||
| 55 | |||
| 56 | eval { $parser->parse_into_model(undef, $n3, $model) }; | ||
| 57 | |||
| 58 | die "failed to convert N3 to RDF/XML, error parsing N3: $@" if $@; | ||
| 59 | |||
| 60 | my $serializer = RDF::Trine::Serializer->new('rdfxml'); | ||
| 61 | |||
| 62 | return $self->serialize_model($model, 'text/rdf+n3'); | ||
| 63 | } | ||
| 64 | |||
| 65 | my @N3_MIME_TYPES = ( | ||
| 66 | 'text/rdf+n3', | ||
| 67 | 'text/n3', | ||
| 68 | 'application/x-turtle', | ||
| 69 | ); | ||
| 70 | |||
| 71 | sub serialize_model | ||
| 72 | { | ||
| 73 | my ($self, $model, $mime_type) = @_; | ||
| 74 | |||
| 75 | unless(ref($self) =~ m/^SADI::Simple::Utils/ or $self =~ /^SADI::Simple::Utils/) { | ||
| 76 | ($model, $mime_type) = @_; | ||
| 77 | } | ||
| 78 | |||
| 79 | my $serializer; | ||
| 80 | |||
| 81 | if (grep($_ eq $mime_type, @N3_MIME_TYPES)) { | ||
| 82 | $serializer = RDF::Trine::Serializer->new('turtle'); | ||
| 83 | } elsif (lc($mime_type) eq "application/n-quads") { | ||
| 84 | $serializer = RDF::Trine::Serializer::NQuads->new(); | ||
| 85 | #$self->add_nanopub_metadata($model); | ||
| 86 | } else { | ||
| 87 | $serializer = RDF::Trine::Serializer->new('rdfxml'); | ||
| 88 | } | ||
| 89 | |||
| 90 | return $serializer->serialize_model_to_string($model); | ||
| 91 | |||
| 92 | } | ||
| 93 | |||
| 94 | sub get_standard_content_type | ||
| 95 | { | ||
| 96 | my ($self, $content_type) = @_; | ||
| 97 | |||
| 98 | unless(ref($self) =~ m/^SADI::Simple::Utils/ or $self =~ /^SADI::Simple::Utils/) { | ||
| 99 | ($content_type) = @_; | ||
| 100 | } | ||
| 101 | |||
| 102 | my $standard_content_type = 'application/rdf+xml'; | ||
| 103 | |||
| 104 | if (defined $content_type) { | ||
| 105 | $standard_content_type = 'text/rdf+n3' if $content_type =~ m|text/rdf\+n3|gi; | ||
| 106 | $standard_content_type = 'text/rdf+n3' if $content_type =~ m|text/n3|gi; | ||
| 107 | } | ||
| 108 | |||
| 109 | return $standard_content_type; | ||
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | sub add_nanopub_metadata { | ||
| 114 | my ($self) = @_; | ||
| 115 | |||
| 116 | # | ||
| 117 | #@prefix : <http://www.example.org/mynanopub/>. | ||
| 118 | #@prefix ex: <http://www.example.org/>. | ||
| 119 | #@prefix np: <http://www.nanopub.org/nschema#>. | ||
| 120 | #@prefix dct: <http://purl.org/dc/terms/>. | ||
| 121 | #@prefix go: <http://purl.obolibrary.org/obo/>. | ||
| 122 | #@prefix up: <http://purl.uniprot.org/core/> . | ||
| 123 | #@prefix pav: <http://swan.mindinformatics.org/ontologies/1.2/pav/> | ||
| 124 | #@prefix xsd: <http://www.w3.org/2001/XMLSchema#>. | ||
| 125 | #{ | ||
| 126 | #:nanopub1 np:hasAssertion :G1; | ||
| 127 | #np:hasProvenance :G2; | ||
| 128 | #np:hasSupporting :G3. | ||
| 129 | #:G1 a np:Assertion. | ||
| 130 | #:G2 a np:Provenance. | ||
| 131 | #:G3 a np:Supporting. | ||
| 132 | #} | ||
| 133 | #:G1 { | ||
| 134 | #<http://purl.uniprot.org/uniprot/O76074> | ||
| 135 | #up:classifiedWith go:GO_0000287, go:GO_0005737, go:GO_0007165, | ||
| 136 | #go:GO_0008270, go:GO_0009187, go:GO_0030553. | ||
| 137 | #} | ||
| 138 | #:G2 { | ||
| 139 | #:nanopub1 pav:versionNumber "1.1" | ||
| 140 | #:nanopub1 pav:previousVersion "1.0". | ||
| 141 | #:nanopub1 dct:created "2009-09-03"^^xsd:date. | ||
| 142 | #:nanopub1 dct:creator ex:JohnSmith. | ||
| 143 | #:nanopub1 dct:rightsHolder ex:SomeOrganization. | ||
| 144 | #:nanopub1 up:citation <http://bio2rdf.org/medline:99320215>. | ||
| 145 | #} | ||
| 146 | |||
| 147 | |||
| 148 | } | ||
| 149 | |||
| 150 | |||
| 151 | 1; |