#!/usr/bin/perl
## Version 1.0 ##
## Author: Christopher Villareal cvillareal@gmail.com ##
## Conventions
## $p_ is for a method/function parameter; $r_ is for a return value;
## p is for a pointer (ex. $pTwig); n for an integer; ph for a pointer to an HASH Table;
## pa for a pointer to an ARRAY; a for an ARRAY; h for an HASH
## ToDo: change over to using an sql database for dynamic generation ##
########################################################################################
## This script will take your xml file created by the vbi program and parse them into
## individual html files denoted by /$organism/ by BioobjectID or InteractionID
## for hyperlinking too by the curators with the master picture
## Put the picture into the w/e folder, but I would suggest something like
## /var/www/html/organism/index.html and then link by ../$bioobjID
## Modules ##
use strict;
use XML::Twig; # Handle the complicated handlers, and parse xml
use XML::Simple; # Use to insert parsed xml data into a html document
use DBI;
use CGI;
use Data::Dumper;
## End Modules ##
## Global Variables ## Some are necessary so their contents are available after subroutines##
my $pCGI = new CGI;
print $pCGI->header;
#my $organism = $pCGI->param('organism');
# my $sInput = $pCGI->param('input');
my $sAllBioObjectID;
my $sAllBRefs;
my $sAllBDesc;
my $sAllBFun;
my $sAllBName;
my $sAllBLoc;
my $sAllBType;
my $sAllIID;
my $sAllIRefs;
my $sAllICofac;
my $sAllIDesc;
my $sAllIType;
my %ref2pmid;
## End Global Variables ##
select(STDOUT); $| = 1; # make STDOUT unbuffered (so prints are done imediately)
my $parse = "RiftValleyFever_Pathway_pronet83.xml";
# Building the Twig Object and parse it using the insertCitations Method
my $pTwig = new XML::Twig('TwigRoots' => {'ProNet' => \&createHTML});
if( !defined($pTwig) )
{
print "Err: Can't get XML::Twig Object !\n";
exit -2;
}
$pTwig->parsefile($parse);
print "All done \:\) \n";
exit;
sub getBIOOB
{
print "Grabbing all BioObject's Information \n";
my $p_phXML = shift;
my $paXMLBIOOB = getInfosFromXML($p_phXML, 'ProNet', 'BioObject');
## Creating all the arrays we're going to need
my @aBioObjectID = ();
my @aBRefs = ();
my @aBDesc = ();
my @aBName = ();
my @aBType = ();
my @aBLoc = ();
my @aBFun = ();
## this foreach is pretty fun, its using the dumped data from $Var1 to then parse through and group what I want
## therefore, this can be extended to more functionality and even picture/hyperlink creations
## but I would suggest using a database if you were to do that and then you can have dynamic generation!
foreach my $phBioObject (@{$paXMLBIOOB})
{
push @aBioObjectID, $phBioObject->{'BioObjectID'};
push @aBRefs, $phBioObject->{'Refs'};
push @aBDesc, $phBioObject->{'Description'};
push @aBName, $phBioObject->{'Name'};
push @aBType, $phBioObject->{'Type'};
push @aBLoc, $phBioObject->{'Location'};
push @aBFun, $phBioObject->{'Function'};
}
## Now that I've grabbed everything, for this subroutine, I am going to finish it off by joining them all into a single
## placeholder than can then be foreached and split to make the htmls file lines
$sAllBioObjectID = join('<<|>>', @aBioObjectID);
$sAllBRefs = join('<<|>>', @aBRefs);
$sAllBDesc = join('<<|>>', @aBDesc);
$sAllBName = join('<<|>>', @aBName);
$sAllBType = join('<<|>>', @aBType);
$sAllBLoc = join('<<|>>', @aBLoc);
$sAllBFun = join('<<|>>', @aBFun);
print "Done grabbing all BioObject information\n";
}
sub getINTERACTIONS
{
print "Grabbing all Interaction's Information\n";
my $p_phXML = shift;
my $paXMLINTERACTIONS = getInfosFromXML($p_phXML, 'ProNet', 'Interaction');
my @aIID = ();
my @aIRefs = ();
my @aICofac = ();
my @aIDesc = ();
my @aIType = ();
foreach my $phInteractions (@{$paXMLINTERACTIONS})
{
push @aIID, $phInteractions->{'InteractionID'};
push @aIRefs, $phInteractions->{'Refs'};
push @aICofac, $phInteractions->{'Co-factors'};
push @aIDesc, $phInteractions->{'Description'};
push @aIType, $phInteractions->{'Type'};
}
$sAllIID = join('<<|>>', @aIID);
$sAllIRefs = join('<<|>>', @aIRefs);
$sAllICofac = join('<<|>>', @aICofac);
$sAllIDesc = join('<<|>>', @aIDesc);
$sAllIType = join('<<|>>', @aIType);
print "Done grabbing all interaction information\n";
}
sub getPMID
{
my $p_phXML = shift;
my $paXMLPMID = getInfosFromXML($p_phXML, 'ProNet', 'DataSource', 'JournalArticle');
foreach my $phPMID (@{$paXMLPMID})
{
my $tempvar=$phPMID->{'PubMedID'};
$ref2pmid{$phPMID->{'RefID'}}=$tempvar;
}
print Dumper(%ref2pmid);
}
sub getInfosFromXML
{
my $p_phXML = shift;
my @p_aKeyList = @_;
my $r_paXMLInfos;
my $phTmpXML = $p_phXML;
my @aKeyListSaved = @p_aKeyList;
my $bReturnTmpXML = 1;
foreach my $sKey (@p_aKeyList)
{
shift @aKeyListSaved;
if( defined($phTmpXML) )
{
if( ref($phTmpXML) eq 'HASH' && defined($phTmpXML->{$sKey}) )
{
if( ref($phTmpXML->{$sKey}) eq 'HASH' )
{
$phTmpXML = $phTmpXML->{$sKey};
}
elsif( ref($phTmpXML->{$sKey}) eq 'ARRAY' )
{
foreach my $puTmp (@{$phTmpXML->{$sKey}})
{
if( !defined($r_paXMLInfos) )
{
$r_paXMLInfos = [];
}
my $paXMLInfos = getInfosFromXML($puTmp, @aKeyListSaved);
if( defined($paXMLInfos) )
{
push @{$r_paXMLInfos}, @{$paXMLInfos};
}
}
}
else
{
if( !defined($r_paXMLInfos) )
{
$r_paXMLInfos = [];
}
push @{$r_paXMLInfos}, $phTmpXML->{$sKey};
$bReturnTmpXML = 0;
last;
}
}
elsif( ref($phTmpXML) eq 'HASH' && !defined($phTmpXML->{$sKey}) )
{
$bReturnTmpXML = 0;
}
}
}
if( !defined($r_paXMLInfos) )
{
if( $bReturnTmpXML eq 1 )
{
$r_paXMLInfos = [$phTmpXML];
}
else
{
$r_paXMLInfos = [];
}
}
return $r_paXMLInfos;
}
sub createHTML
{
my $p_pTwig = shift;
my $p_pProNet = shift;
# Save XML ProNet into string
my $sProNet = $p_pProNet->sprint;
# release/purge Twig Datas (no more needed as we work on the $sProNet XML directly using XML::Simple
$p_pTwig->purge;
# Getting an XML::Simple Object from the ProNet XML (using sprint), which makes things simple to parse...
my $pXS = new XML::Simple();
my $phXML = eval{$pXS->XMLin($sProNet, 'forcearray' => 0)};
if( $@ )
{
print "Err: $@\n";
return;
}
if( !defined($phXML) )
{
return;
}
# Call subroutines to call all the parsed information
getBIOOB($phXML) || die "Could not get BioObjects";
getINTERACTIONS($phXML) || die "Could not get InteractionObjects";
getPMID($phXML) || die "Could not get citation information";
# Stuff those into new arrays after being simplified
# Check from HASH(xxxx) to NULL
my @aAllBioObjectID = split ("<<|>>",$sAllBioObjectID);
#my $nBioObjID;
my @aAllBRefs = split ("<<|>>",$sAllBRefs);
my $nBRefs;
# <<|>>Odonnel<<|>>Grubman Frys<|>>
# <JournalArticle RefID="Odonnell" PubMedID="15956593">
# <<|>>15956593<<|>>15956593 1558648<<|>>
my @aAllBDesc = split ("<<|>>",$sAllBDesc);
#my $nBDesc;
my @aAllBFun = split ("<<|>>",$sAllBFun);
#my $nBFun;
my @aAllBName = split ("<<|>>",$sAllBName);
#my $nBName;
my @aAllBLoc = split ("<<|>>",$sAllBLoc);
#my $nBLoc;
my @aAllBType = split ("<<|>>",$sAllBType);
#my $nBType;
my @aAllIID = split ("<<|>>",$sAllIID);
#my $nIID;
my @aAllIRefs = split ("<<|>>",$sAllIRefs);
#my $nIRefs;
my @aAllICofac = split ("<<|>>",$sAllICofac);
#my $nICofac;
my @aAllIDesc = split ("<<|>>",$sAllIDesc);
#my $nIDesc;
my @aAllIType = split ("<<|>>",$sAllIType);
#my $nIType;
# let's create the webpages now
print "Now creating all the html files needed in ... \n";
# Before using the script, make sure this points to the correct area
for (my $i=0;$i<@aAllBioObjectID; $i++) {
# Change here to be able to tell the script where is should make the files too
open (BIO,">/home/cvillareal/Desktop/kelly/RiftValleyFever/$aAllBioObjectID[$i].html") || die "Could not write Bio files!";
print BIO "<html>";
print BIO "<body>";
print BIO "<p>Name: $aAllBName[$i]</p>";
print BIO "<p>Type: $aAllBType[$i]</p>";
print BIO "<p>Location: $aAllBLoc[$i]</p>";
print BIO "<p>Fuction: $aAllBFun[$i]</p>";
print BIO "<p>Description: $aAllBDesc[$i]</p>";
foreach my $nRef(split (" ",$aAllBRefs[$i])){
print BIO "<p><a href=\"http\:\/\/eutils\.ncbi\.nlm\.nih\.gov\/entrez\/eutils\/elink.fcgi\?dbfrom\=pubmed\&id\=$ref2pmid{$nRef}\&retmode\=ref\&cmd\=prlinks\">$nRef</a></p>";
}
print BIO "</body>";
print BIO "</html>";
}
for (my $b=0;$b<@aAllIID; $b++) {
open (INTER, ">/home/cvillareal/Desktop/kelly/RiftValleyFever/$aAllIID[$b].html") || "Could not write interaction files!";
print INTER "<html>";
print INTER "<body>";
print INTER "<p>ID: $aAllIID[$b]</p>";
print INTER "<p>Type: $aAllIType[$b]";
print INTER "<p>Description: $aAllIDesc[$b]";
print INTER "<p>CoFactors: $aAllICofac[$b]";
foreach my $iRef(split (" ",$aAllIRefs[$b])){
print INTER "<p><a href=\"http\:\/\/eutils\.ncbi\.nlm\.nih\.gov\/entrez\/eutils\/elink.fcgi\?dbfrom\=pubmed\&id\=$ref2pmid{$iRef}\&retmode\=ref\&cmd\=prlinks\">$iRef</a></p>";
}
print BIO "</body>";
print BIO "</html>";
}