Created
August 26, 2011 03:46
-
-
Save drlivingston/1172638 to your computer and use it in GitHub Desktop.
example AO annotation serialization in RDF using Perl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Data::UUID; | |
#use RDF::Trine::Serializer::Turtle; | |
#use RDF::Trine::Serializer::Notation3; | |
use RDF::Trine::Serializer::NTriples; | |
use RDF::Trine::Namespace qw(rdf); | |
my $foaf = RDF::Trine::Namespace->new('http://xmlns.com/foaf/0.1/' ); | |
my $ao = RDF::Trine::Namespace->new('http://purl.org/ao/core/'); | |
my $aot = RDF::Trine::Namespace->new('http://purl.org/ao/types/'); | |
my $aos = RDF::Trine::Namespace->new('http://purl.org/ao/selectors/'); | |
my $aof = RDF::Trine::Namespace->new('http://purl.org/ao/foaf/'); | |
my $pav = RDF::Trine::Namespace->new('http://purl.org/pav/'); | |
my $ex = RDF::Trine::Namespace->new('http://example/'); | |
sub resource { | |
return RDF::Trine::Node::Resource->new(shift); | |
} | |
sub literal { | |
return RDF::Trine::Node::Literal->new(shift); | |
} | |
sub literalString { | |
return RDF::Trine::Node::Literal->new(shift, shift); | |
} | |
sub statement { | |
return RDF::Trine::Statement->new(shift, shift, shift); | |
} | |
sub newPrefixURI { | |
my $ug = new Data::UUID; | |
my $name = (shift) . "_" . $ug->create_hex(); | |
return $ex->$name; | |
} | |
sub newAnnotationURI { | |
return newPrefixURI("A"); | |
} | |
sub newSpanURI { | |
return newPrefixURI("SPAN"); | |
} | |
sub addStatementsToModel { | |
my $model = shift; | |
my $statements = shift; | |
foreach my $statement(@$statements) { | |
$model->add_statement(statement(@$statement)); | |
} | |
return $model; | |
} | |
sub addAnnotation { | |
my %params = @_; | |
my $model = $params{model}; | |
my $A = newAnnotationURI(); | |
addStatementsToModel($model, | |
[[$A, $rdf->type, $ao->Annotation], | |
[$A, $pav->createdBy, $ex->foo], | |
[$A, $pav->createdOn, $ex->now]]); | |
return {model => $model, annotation => $A}; | |
} | |
sub addDocument { | |
my %params = @_; | |
my $model = $params{model}; | |
my $pmid = $params{pmid}; | |
my $pmidURI = resource("http://www.ncbi.nlm.nih.gov/pubmed/" . $pmid); | |
print $pmidURI; | |
## my $document = $params{document}; | |
addStatementsToModel($model, | |
[[$pmidURI, $rdf->type, $foaf->Document]]); | |
return {(%params, document => $pmidURI)}; | |
} | |
sub addDocumentVersion { | |
my %params = @_; | |
my $model = $params{model}; | |
my $dateAccessed = $params{dateAccessed}; | |
my $document = $params{document}; | |
my $DV = newPrefixURI("DocVersion"); | |
addStatementsToModel($model, | |
[[$DV, $rdf->type, $ao->SourceDocument], | |
[$DV, $pav->sourceAccessedOn, $ex->now], | |
[$DV, $pav->retrievedFrom, $document]]); | |
return {(%params, documentVersion => $DV)}; | |
} | |
sub addContext { | |
my %params = @_; | |
my $model = $params{model}; | |
my $start = $params{start}; | |
my $end = $params{end}; | |
my $A = $params{annotation}; | |
my $D = $params{document}; | |
my $DV = $params{documentVersion}; | |
my $S = newSpanURI(); | |
addStatementsToModel($model, | |
[[$A, $ao->hasContext, $S], | |
[$S, $rdf->type, $aos->StartEndTextSelector], | |
[$S, $aos->start, literal($start)], | |
[$S, $aos->end, literal($end)]]); | |
if ($DV) { | |
addStatementsToModel($model, | |
[[$S, $ao->onSourceDocument, $DV]]); | |
} else { | |
addStatementsToModel($model, | |
[[$S, $aof->onDocument, $D]]); | |
} | |
return %params; | |
} | |
my $RDF; | |
my $model; | |
my $progressiveOutput; | |
my $file_name = 'annotation.ntriple'; | |
open ($RDF, ">>$file_name") || die ("Could not open $file_name!"); | |
my $serializer = RDF::Trine::Serializer::NTriples->new(); | |
#other serializer formats | |
#RDF::Trine::Serializer::Turtle->new(namespaces => $namespaces); | |
#RDF::Trine::Serializer::Notation3->new(namespaces => $namespaces); | |
# TODO LOOP HERE | |
$model = RDF::Trine::Model->temporary_model; | |
$progressiveOutput = addAnnotation(model => $model); | |
$progressiveOutput = addDocument((%$progressiveOutput, pmid=> 18276894)); | |
$progressiveOutput = | |
addDocumentVersion((%$progressiveOutput, dateAccessed=>"now")); | |
$progressiveOutput = addContext((%$progressiveOutput, start => 5, end => 10)); | |
$serializer->serialize_model_to_file($RDF, $model); | |
#for test | |
#print $serializer->serialize_model_to_string($model); | |
#END LOOP | |
close ($RDF); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment