1<?php
2/*
3description : Access to NCBI using eSummary and eSearch
4author      : Ikuo Obataya
5email       : i.obataya[at]gmail_com
6lastupdate  : 2013-07-07
7license     : GPL 2 (http://www.gnu.org/licenses/gpl.html)
8*/
9if(!defined('DOKU_INC')) die();
10class ncbi{
11  var $HttpClient;
12  var $eSummaryURL = '';
13  var $eSearchURL  = '';
14  var $pubmedURL   = '';
15  function ncbi()
16  {
17    $this->HttpClient  = new DokuHTTPClient();
18    $this->eSummaryURL = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=%s&retmode=xml&id=%s';
19    $this->eSearchURL  = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=%s&term=%s';
20    $this->pubchemURL  = 'https://pubchem.ncbi.nlm.nih.gov/summary/summary.cgi?cid=%s&disopt=DisplayXML';
21    $this->pubmedURL   = 'https://www.ncbi.nlm.nih.gov/pubmed/%s';
22  }
23  /*
24   * Retrieve Summary XML
25   */
26  function SummaryXml($db,$id){
27    $url = sprintf($this->eSummaryURL,urlencode($db),urlencode($id));
28    $summary = $this->HttpClient->get($url);
29    if (preg_match("/error/i",$summary)){return NULL;}
30    return $summary;
31
32  }
33  /*
34   * Retrieve Search result
35   */
36  function SearchXml($db,$term){
37    $result = $this->HttpClient->get(sprintf($this->eSearchURL,urlencode($db),urlencode($term)));
38    if (preg_match("/error/i",$result)){return NULL;}
39    return $result;
40  }
41  /*
42   * Retrieve PubChem XML
43   */
44  function GetPubchemXml($cid){
45    $xml = $this->HttpClient->get(sprintf($this->pubchemURL,$cid));
46    if (preg_match("/error/i",$xml)){return NULL;}
47    return $xml;
48  }
49
50  /*
51   * Handle XML elements
52   */
53
54  function GetSummaryItem($item,&$xml){
55    preg_match('/"'.$item.'"[^>]*>([^<]+)/',$xml,$m);
56    return $m[1];
57  }
58
59  function GetSummaryItems($item,&$xml){
60    preg_match_all('/"'.$item.'"[^>]*>([^<]+)/',$xml,$m);
61    return $m[1];
62  }
63
64  function GetSearchItem($item,&$xml){
65     preg_match("/<".$item.">([^<]+?)</",$xml,$m);
66     return $m[1];
67  }
68
69  function GetSearchItems($item,&$xml){
70     preg_match_all("/<".$item.">([^<]+?)</",$xml,$m);
71     return $m[1];
72  }
73
74}
75?>
76