<?php
// $Id: ncbi_wrapper.php,v 1.1.1.1 2005/05/19 10:31:10 rdmpage Exp $
/**
* @brief Wrapper to talk to the NCBI's taxonomy database.
*
*/
class NCBIWrapper extends Wrapper {
var $xpath;
function NCBIWrapper ()
{
$this->server = "eutils.ncbi.nlm.nih.gov";
$this->authority = "ncbi.nlm.nih.gov";
$this->namespace = "TaxId";
$this->StartXML();
}
/**
* @brief Try to connect to NCBI server
*
* Simply tries to open an HTTP connection to eutils.ncbi.nlm.nih.gov.
* @return true if service is live, false otherwise
*/ function IsAlive ()
{
$query = "http://";
$query .= $this->server;
$result = true;
$http = new Net_HTTP_Client();
if ($config['proxy_name'] != '')
{
$http->setProxy ($config['proxy_name'], $config['proxy_port']);
}
$http->Connect($query, 80 ) or $result = false;
$http->Disconnect();
return $result;
}
/**
* @brief Return details for a single record in NCBI
*
* @param id NCBI TAXID of taxon name to retrieve
* @return Result in my XML format
*/
function GetDataForID ($id)
{
$result = "";
global $config;
$ncbi_xml = $this->GetDataFromCache ("GetDataForID", "xml", $id);
if ($ncbi_xml == "")
{
// Get fresh data
$query = "http://";
$query .= $this->server;
$query .= "/entrez/eutils/esummary.fcgi?db=taxonomy";
$query .= "&tool=TNS";
$query .= "&id=" . $id;
$http = new Net_HTTP_Client();
if ($config['proxy_name'] != '')
{
$http->setProxy ($config['proxy_name'], $config['proxy_port']);
}
if (!$http->Connect($query, 80 ))
{
$this->Error ("server", "Can't connect");
$this->EndXML();
return $this->xml;
}
$this->StartTimer();
$status = $http->Get( $query );
$this->StopTimer();
if( $status != 200 )
{
$this->Error ("GET", $http->getStatusMessage());
$this->EndXML();
return $this->xml;
}
$ncbi_xml = $http->getBody();
$http->Disconnect();
$this->StoreDataInCache ("GetDataForID", "xml", $id, $ncbi_xml);
}
//echo $this->xml;
// Tell XSLT processor the time used to search this service
$params = array("timeused" => $this->time_used);
$xslt_file = "xsl/ncbi_taxon.xsl";
if ($config['sabcmd'] != '')
{
// Transform using external sablot processor
$xpresult = XSLT_Buffer ($ncbi_xml, $xslt_file,'', $params);
// If we have an error then bail out
if (strpos ( $xpresult,"[code:" ))
{
$this->Error ("XSLT", $xpresult);
$this->EndXML();
return $this->xml;
}
}
else
{
// Use XSLT extension
$xslt_processor = xslt_create();
$xslt = join ("", file($xslt_file));
$arg_buffer = array("/xml" => $ncbi_xml, "/xslt" => $xslt);
$xp = xslt_create() or die ("Could not create XSLT processor");
if (!($xpresult = xslt_process($xp, "arg:/xml", "arg:/xslt", NULL, $arg_buffer, $params)))
{
echo "An error occurred: " . xslt_error($xp) . "(error code " . xslt_errno($xp) . ")";
}
xslt_free($xp);
}
$result = $xpresult;
//echo $result;
return $result;
}
/**
* @brief Search for name and return an XML document listing the names
*
* We talk to ITIS using its URL API and get XML back from the server.
* This is then transformed into our own format.
*
* @param name The taxon name to search for
* @param qualifier Kind of search (default is exact)
* @param max_results The maximum number of records to return (default is 1)
*/
function NameSearch ($name, $qualifier = EXACT, $max_results = 1)
{
$result = "";
global $config;
$id = nameToSafe ($name);
$this->StartTimer();
$ncbi_xml = $this->GetDataFromCache ("NameSearch", "xml", $id);
if ($ncbi_xml == "")
{
$query = "http://";
$query .= $this->server;
$query .= "/entrez/eutils/esearch.fcgi?db=taxonomy";
$query .= "&tool=TNS";
if ($qualifier == EXACT)
{
}
else
{
}
$query .= "&term=";
$query .= ereg_replace (" " ,"+" ,$name );
$query .= "&retmode=xml";
//echo $query;
$http = new Net_HTTP_Client();
if ($config['proxy_name'] != '')
{
$http->setProxy ($config['proxy_name'], $config['proxy_port']);
}
if (!$http->Connect($query, 80 ))
{
$this->Error ("server", "Can't connect");
$this->EndXML();
return $this->xml;
}
$status = $http->Get( $query );
if( $status != 200 )
{
$this->Error ("GET", $http->getStatusMessage());
$this->EndXML();
return $this->xml;
}
// Get the NCBI XML
$ncbi_xml = $http->getBody();
$http->Disconnect();
$this->StoreDataInCache ("NameSearch", "xml", $id, $ncbi_xml);
}
$this->StopTimer();
$this->ReportTimeUsed();
// $xml has the result returned from NCBI
//echo $ncbi_xml;
// Extract info we need using XPath
$xpath = new XPath();
$xpath->importFromString($ncbi_xml);
$nodeCollection = $xpath->match("//eSearchResult/IdList/Id");
foreach($nodeCollection as $node)
{
$id = $xpath->getData($node);
// Get data for this id to build a more informative answer
$this->xml .= "<taxon authority=\"$this->authority\" namespace=\"$this->namespace\" id=\"$id\">\n";
$this->xml .= " <name>$name</name>\n";
$this->xml .= " <url>http://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id=$id</url>\n";
$this->xml .= "</taxon>\n";
}
$this->EndXML();
return $this->xml;
}
/* function GetStandardData ($id)
{
// 1. Get data from server
// 2. extract all info and store in a hash
// 3. call a method to convert array to XML document
// to do: store result in a cache
}
function GetTimeToGet()
{
}
*/
}
?>