#!/usr/bin/perl -w
my
$ns
= RDF::NS->new(
'20131205'
);
print
"Content-Type: text/turtle\n\n"
;
unless
(
$ENV
{
'PATH_INFO'
}){
printProfilerInfo();
exit
;
}
my
(
$blank
,
$profile_schema
,
$position
) =
split
'/'
,
$ENV
{
'PATH_INFO'
};
my
(
$model_to_return
,
$current_position
);
if
(
$profile_schema
=~ /DragonDB_Allele_ProfileAlleleDescriptions/){
(
$model_to_return
,
$current_position
) = alleleDescriptions(
$sortedURLs
,
$position
);
}
elsif
(
$profile_schema
=~ /DragonDB_Allele_ProfileImagesEDAM/){
(
$model_to_return
,
$current_position
) = alleleImagesEDAM(
$sortedURLs
,
$position
);
}
elsif
(
$profile_schema
=~ /DragonDB_Allele_ProfileImagesSIO/){
(
$model_to_return
,
$current_position
) = alleleImagesSIO(
$sortedURLs
,
$position
);
}
createPagination(
$model_to_return
,
scalar
(
@$sortedURLs
),
$current_position
);
serializeThis(
$model_to_return
);
exit
;
sub
createPagination {
my
(
$model
,
$total
,
$current
) =
@_
;
my
$this_script
=
$ENV
{
'REQUEST_URI'
} ||
"/DragonDB_LDF_Profiler"
;
my
$server
=
$ENV
{
'SERVER_NAME'
} ||
"biordf.org"
;
my
$path
=
$ENV
{
'PATH_INFO'
} ||
"/DragonDB_Allele_ProfileImagesSIO/100"
;
my
$this_page
=
"http://$server/$this_script/$path"
;
$path
=~ s/\d+$//;
my
$baseURL
=
"http://$server/$this_script/$path"
;
if
(
$current
> 0) {
my
$prevpage
=
$current
- 11;
$prevpage
= 0
if
$prevpage
< 0;
my
$prevURL
=
$baseURL
.
$prevpage
;
my
$statement
= statement(
$this_page
,
$ns
->hydra(
'previousPage'
),
$prevURL
);
$model
->add_statement(
$statement
);
}
if
(
$total
-
$current
> 0) {
my
$nextpage
=
$current
;
my
$nextURL
=
$baseURL
.
$nextpage
;
my
$statement
= statement(
$this_page
,
$ns
->hydra(
'nextPage'
),
$nextURL
);
$model
->add_statement(
$statement
);
}
}
sub
getLDPRecordURLs {
my
$LDPServer
=
shift
;
my
$store
= RDF::Trine::Store::Memory->new();
my
$model
= RDF::Trine::Model->new(
$store
);
my
$parser
= RDF::Trine::Parser->new(
'turtle'
);
$parser
->parse_url_into_model(
$LDPServer
,
$model
);
my
@rows
=
$query
->execute(
$model
);
my
@sortedurls
=
map
{
$_
->{
'o'
}->value} (
sort
{
$a
->{
'o'
}->value cmp
$b
->{
'o'
}->value}
@rows
);
return
\
@sortedurls
;
}
sub
getTrineModelForURL {
my
(
$url
) =
shift
;
my
$store
= RDF::Trine::Store::Memory->new();
my
$model
= RDF::Trine::Model->new(
$store
);
my
$parser
= RDF::Trine::Parser->new(
'turtle'
);
$parser
->parse_url_into_model(
$url
,
$model
);
return
$model
;
}
sub
alleleDescriptions {
my
(
$URLs
,
$position
) =
@_
;
$position
||=0;
my
@URLs
=
@$URLs
[
$position
..
scalar
(
@$URLs
)];
my
$model
= createFreshTrineModel();
my
$counter
= 0;
foreach
my
$url
(
@URLs
){
$counter
++;
last
if
(
$counter
> 5) ;
my
$LDPmodel
= getTrineModelForURL(
$url
);
my
$query
= RDF::Query->new("SELECT ?s ?url where {
my
@rows
=
$query
->execute(
$LDPmodel
);
my
$xmlrecord
=
$rows
[0]->{
'url'
}->value;
my
$alleleURL
=
$rows
[0]->{
's'
}->value;
my
$xml
= get(
$xmlrecord
);
my
(
$gene
,
$desc
,
$images
) = extractDataFromXMLRecord(
$xml
);
my
$statement
;
$statement
= statement(
$alleleURL
,
$ns
->rdf(
'type'
),
$ns
->obo(
'SO_0001023'
));
$model
->add_statement(
$statement
);
$statement
=
""
;
$statement
= statement(
$ns
->obo(
'SO_0001023'
),
$ns
->rdfs(
'label'
),
"Allele"
);
$model
->add_statement(
$statement
);
$statement
=
""
;
if
(
$desc
) {
$statement
= statement(
$alleleURL
,
$ns
->dc(
'description'
),
$desc
);
$model
->add_statement(
$statement
);
$statement
=
""
;
}
if
(
$gene
) {
$statement
= statement(
$alleleURL
,
$ns
->obo(
'so_variant_of'
),
$gene
);
$model
->add_statement(
$statement
);
$statement
=
""
;
$statement
= statement(
$gene
,
$ns
->rdf(
'type'
),
$ns
->obo(
'SO_0000704'
));
$model
->add_statement(
$statement
);
$statement
=
""
;
$statement
= statement(
$ns
->obo(
'SO_0000704'
),
$ns
->rdfs(
'label'
),
"Gene"
);
$model
->add_statement(
$statement
);
$statement
=
""
;
}
}
return
(
$model
, (
$position
+
$counter
));
}
sub
alleleImagesEDAM {
my
(
$URLs
,
$position
) =
@_
;
$position
||=0;
my
@URLs
=
@$URLs
[
$position
..
scalar
(
@$URLs
)];
my
$model
= createFreshTrineModel();
my
$counter
= 0;
foreach
my
$url
(
@URLs
){
$counter
++;
last
if
(
$counter
> 5) ;
my
$LDPmodel
= getTrineModelForURL(
$url
);
my
$query
= RDF::Query->new("SELECT ?s ?url where {
my
@rows
=
$query
->execute(
$LDPmodel
);
my
$xmlrecord
=
$rows
[0]->{
'url'
}->value;
my
$alleleURL
=
$rows
[0]->{
's'
}->value;
my
$xml
= get(
$xmlrecord
);
my
(
$gene
,
$desc
,
$images
) = extractDataFromXMLRecord(
$xml
);
my
$statement
;
$statement
= statement(
$alleleURL
,
$ns
->rdf(
'type'
),
$ns
->obo(
'SO_0001023'
));
$model
->add_statement(
$statement
);
$statement
= statement(
$ns
->obo(
'SO_0001023'
),
$ns
->rdfs(
'label'
),
"Allele"
);
$model
->add_statement(
$statement
);
foreach
my
$imageURL
(
@$images
){
$statement
= statement(
$alleleURL
,
$ns
->sio(
'SIO_000205'
),
$imageURL
);
$model
->add_statement(
$statement
);
$model
->add_statement(
$statement
);
$model
->add_statement(
$statement
);
}
}
return
(
$model
, (
$position
+
$counter
));
}
sub
alleleImagesSIO {
my
(
$URLs
,
$position
) =
@_
;
$position
||=0;
my
@URLs
=
@$URLs
[
$position
..
scalar
(
@$URLs
)];
my
$model
= createFreshTrineModel();
my
$counter
= 0;
foreach
my
$url
(
@URLs
){
$counter
++;
last
if
(
$counter
> 5) ;
my
$LDPmodel
= getTrineModelForURL(
$url
);
my
$query
= RDF::Query->new("SELECT ?s ?url where {
my
@rows
=
$query
->execute(
$LDPmodel
);
my
$xmlrecord
=
$rows
[0]->{
'url'
}->value;
my
$alleleURL
=
$rows
[0]->{
's'
}->value;
my
$xml
= get(
$xmlrecord
);
my
(
$gene
,
$desc
,
$images
) = extractDataFromXMLRecord(
$xml
);
my
$statement
;
$statement
= statement(
$alleleURL
,
$ns
->rdf(
'type'
),
$ns
->obo(
'SO_0001023'
));
$model
->add_statement(
$statement
);
$statement
= statement(
$ns
->obo(
'SO_0001023'
),
$ns
->rdfs(
'label'
),
"Allele"
);
$model
->add_statement(
$statement
);
foreach
my
$imageURL
(
@$images
){
$statement
= statement(
$alleleURL
,
$ns
->sio(
'SIO_000205'
),
$imageURL
);
$model
->add_statement(
$statement
);
$statement
= statement(
$imageURL
,
$ns
->rdf(
'type'
),
$ns
->sio(
'SIO_000081'
));
$model
->add_statement(
$statement
);
$statement
= statement(
$ns
->sio(
'SIO_000081'
),
$ns
->rdfs(
'label'
),
"Image"
);
$model
->add_statement(
$statement
);
}
}
return
(
$model
, (
$position
+
$counter
));
}
sub
extractDataFromXMLRecord {
my
$xml
=
shift
;
my
$xp
= XML::XPath->new(
xml
=>
$xml
);
my
$geneid
=
$xp
->getNodeText(
'/Allele/Source/gene/@value'
);
my
$nodeset
=
$xp
->find(
'/Allele/Description/Phenotype/@value'
);
my
$desc
;
foreach
my
$node
(
$nodeset
->get_nodelist) {
$desc
.=
$node
->getData .
"\n"
;
}
my
$imgnodeset
=
$xp
->find(
'/Allele/Phenotype_picture/@value'
);
my
@images
;
foreach
my
$node
(
$imgnodeset
->get_nodelist) {
my
$imgname
= uri_escape(
$node
->getData);
push
@images
,
$img_url
;
}
return
(
$geneURL
,
$desc
, \
@images
);
}
sub
statement {
my
(
$s
,
$p
,
$o
) =
@_
;
unless
(
ref
(
$s
) =~ /Trine/){
$s
=~ s/[\<\>]//g;
$s
= RDF::Trine::Node::Resource->new(
$s
);
}
unless
(
ref
(
$p
) =~ /Trine/){
$p
=~ s/[\<\>]//g;
$p
= RDF::Trine::Node::Resource->new(
$p
);
}
unless
(
ref
(
$o
) =~ /Trine/){
$o
=~ s/[\<\>]//g;
$o
= RDF::Trine::Node::Resource->new(
$o
);
}
elsif
(
$o
=~ /\D/) {
$o
= RDF::Trine::Node::Literal->new(
$o
);
}
else
{
$o
= RDF::Trine::Node::Literal->new(
$o
);
}
}
my
$statement
= RDF::Trine::Statement->new(
$s
,
$p
,
$o
);
return
$statement
;
}
sub
serializeThis{
my
$model
=
shift
;
my
$serializer
= RDF::Trine::Serializer->new(
'turtle'
);
print
$serializer
->serialize_model_to_string(
$model
);
}
sub
printProfilerInfo {
my
$ns
= RDF::NS->new(
'20131205'
);
my
$model
= createFreshTrineModel();
my
$stm
= statement(
$ns
->proj(
'DragonDB_Allele_ProfileAlleleDescriptions'
),
$ns
->rdf(
'type'
),
$ns
->fair(
'dataProjectorDescriptor'
));
$model
->add_statement(
$stm
);
$stm
= statement(
$ns
->proj(
'DragonDB_Allele_ProfileImagesSIO'
),
$ns
->rdf(
'type'
),
$ns
->fair(
'dataProjectorDescriptor'
));
$model
->add_statement(
$stm
);
$stm
= statement(
$ns
->proj(
'DragonDB_Allele_ProfileImagesEDAM'
),
$ns
->rdf(
'type'
),
$ns
->fair(
'dataProjectorDescriptor'
));
$model
->add_statement(
$stm
);
$stm
= statement(
$ns
->proj(
'DragonDB_Allele_ProfileAlleleDescriptions'
),
$ns
->fair(
'projectsSource'
),
"http://antirrhinum.net"
);
$model
->add_statement(
$stm
);
$model
->add_statement(
$stm
);
$model
->add_statement(
$stm
);
$model
->add_statement(
$stm
);
$stm
= statement(
$ns
->proj(
'DragonDB_Allele_ProfileImagesSIO'
),
$ns
->fair(
'projectsSource'
),
"http://antirrhinum.net"
);
$model
->add_statement(
$stm
);
$model
->add_statement(
$stm
);
$model
->add_statement(
$stm
);
$model
->add_statement(
$stm
);
$stm
= statement(
$ns
->proj(
'DragonDB_Allele_ProfileImagesEDAM'
),
$ns
->fair(
'projectsSource'
),
"http://antirrhinum.net"
);
$model
->add_statement(
$stm
);
$model
->add_statement(
$stm
);
$model
->add_statement(
$stm
);
$model
->add_statement(
$stm
);
serializeThis(
$model
);
}
sub
createFreshTrineModel {
my
$store
= RDF::Trine::Store::Memory->new();
my
$model
= RDF::Trine::Model->new(
$store
);
return
$model
;
}