jbjares2

Untitled

May 1st, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 28.07 KB | None | 0 0
  1. <?php
  2. /**
  3. Copyright (C) 2012 Michel Dumontier
  4.  
  5. Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. this software and associated documentation files (the "Software"), to deal in
  7. the Software without restriction, including without limitation the rights to
  8. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  9. of the Software, and to permit persons to whom the Software is furnished to do
  10. so, subject to the following conditions:
  11.  
  12. The above copyright notice and this permission notice shall be included in all
  13. copies or substantial portions of the Software.
  14.  
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22. */
  23.  
  24. /**
  25.  * DrugBank RDFizer
  26.  * @version 2.0
  27.  * @author Michel Dumontier
  28.  * @author Dana Klassen
  29.  * @description
  30. */
  31. require_once(__DIR__.'../../../libs/php-lib/bio2rdfapi.php');
  32. require_once(__DIR__.'../../../libs/php-lib/xmlapi.php');
  33.  
  34. class DrugBankParser extends Bio2RDFizer
  35. {    
  36.     function __construct($argv) {
  37.         parent::__construct($argv,"drugbank");
  38.         parent::addParameter('files', true, 'all|drugbank','all','Files to convert');
  39.         parent::addParameter('download_url',false,null,'http://www.drugbank.ca/system/downloads/current/');
  40.         parent::initialize();
  41.     }
  42.  
  43.     function Run()
  44.     {
  45.         $indir        = parent::getParameterValue('indir');
  46.         $outdir       = parent::getParameterValue('outdir');
  47.         $download_url = parent::getParameterValue('download_url');
  48.  
  49.         if(parent::getParameterValue('files') == 'all') {
  50.             $files = explode("|",parent::getParameterList('files'));
  51.             array_shift($files);
  52.         } else {
  53.             $files = explode("|",parent::getParameterValue('files'));
  54.         }
  55.  
  56.         if(parent::getParameterValue("id_list")) {
  57.             $this->id_list = array_flip(explode(",",parent::getParameterValue('id_list')));
  58.         }
  59.        
  60.         $go_cache_file = parent::getParameterValue('indir')."go.cache.json";
  61.         #unlink($go_cache_file);
  62.         if(!file_exists($go_cache_file) or parent::getParameterValue('download') == true) {
  63.             $this->getGO();
  64.             file_put_contents($go_cache_file,json_encode($this->go));
  65.         } else {
  66.             // read the file
  67.             $this->go = json_decode( file_get_contents($go_cache_file), true);     
  68.         }
  69.  
  70.         $dataset_description = '';
  71.         foreach($files AS $f) {
  72.             if($f == 'drugbank') {
  73.                 $file = 'drugbank.xml.zip';
  74.                 $lname = 'drugbank';
  75.             }
  76.             $fnx = 'parse_'.$f;
  77.  
  78.             $rfile = parent::getParameterValue('download_url').$file;
  79.             $lfile = parent::getParameterValue('indir').$file;
  80.             $cfile = $lname.".".parent::getParameterValue('output_format');
  81.  
  82.             // download
  83.             if(!file_exists($lfile) || parent::getParameterValue('download') == true) {
  84.                 utils::downloadSingle($rfile,$lfile);
  85.             }
  86.  
  87.             // setup the write
  88.             $gz = (strstr(parent::getParameterValue('output_format'),".gz") === FALSE)?false:true;
  89.             parent::setWriteFile($outdir.$cfile, $gz);
  90.             echo $outdir.$cfile;
  91.             if(file_exists($indir.$file)) {
  92.                 // call the parser
  93.                 echo "processing $file ...".PHP_EOL;
  94.                 $this->$fnx($indir,$file);
  95.                 echo "done".PHP_EOL;
  96.                 parent::clear();
  97.             }
  98.             parent::getWriteFile()->close();
  99.  
  100.             // dataset description
  101.             $ouri = parent::getGraphURI();
  102.             parent::setGraphURI(parent::getDatasetURI());
  103.  
  104.             $source_version = parent::getDatasetVersion();
  105.             $bVersion = parent::getParameterValue('bio2rdf_release');
  106.             $prefix = parent::getPrefix();
  107.             $date = date ("Y-m-d\TH:i:sP");
  108.             // dataset description
  109.             $source_file = (new DataResource($this))
  110.             ->setURI($rfile)
  111.             ->setTitle("DrugBank ($file)")
  112.             ->setRetrievedDate( date ("Y-m-d\TH:i:sP", filemtime($indir.$file)))
  113.             ->setFormat("application/xml")
  114.             ->setFormat("application/zip")
  115.             ->setPublisher("http://drugbank.ca")
  116.             ->setHomepage("http://drugbank.ca")
  117.             ->setRights("use")
  118.             ->setRights("by-attribution")
  119.             ->setRights("no-commercial")
  120.             ->setLicense("http://www.drugbank.ca/about")
  121.             ->setDataset("http://identifiers.org/drugbank/");
  122.  
  123.             $output_file = (new DataResource($this))
  124.             ->setURI("http://download.bio2rdf.org/release/$bVersion/$prefix/$cfile")
  125.             ->setTitle("Bio2RDF v$bVersion RDF version of $prefix v$source_version")
  126.             ->setSource($source_file->getURI())
  127.             ->setCreator("https://github.com/bio2rdf/bio2rdf-scripts/blob/master/drugbank/drugbank.php")
  128.             ->setCreateDate($date)
  129.             ->setHomepage("http://download.bio2rdf.org/release/$bVersion/$prefix/$prefix.html")
  130.             ->setPublisher("http://bio2rdf.org")           
  131.             ->setRights("use-share-modify")
  132.             ->setRights("by-attribution")
  133.             ->setRights("restricted-by-source-license")
  134.             ->setLicense("http://creativecommons.org/licenses/by/3.0/")
  135.             ->setDataset(parent::getDatasetURI());
  136.  
  137.             $gz = (strstr(parent::getParameterValue('output_format'),".gz") === FALSE)?false:true;
  138.             if($gz) $output_file->setFormat("application/gzip");
  139.             if(strstr(parent::getParameterValue('output_format'),"nt")) $output_file->setFormat("application/n-triples");
  140.             else $output_file->setFormat("application/n-quads");
  141.  
  142.             parent::writeToReleaseFile($source_file->toRDF().$output_file->toRDF());
  143.             parent::setGraphURI($ouri);
  144.         }
  145.         parent::closeReleaseFile();
  146.     }
  147.  
  148.  
  149.     function parse_drugbank($ldir,$infile)
  150.     {
  151.         $xml = new CXML($ldir.$infile);
  152.         while($xml->parse("drug") == TRUE) {
  153.             if(isset($this->id_list) and count($this->id_list) == 0) break;
  154.             $this->parseDrugEntry($xml);
  155.         }
  156.         unset($xml);
  157.     }
  158.    
  159.        
  160.     function NSMap($source)
  161.     {
  162.         $source = strtolower($source);
  163.         switch($source) {
  164.             case 'uniprotkb':
  165.             case 'uniprot accession':
  166.                 return 'uniprot';
  167.             case "pubchem compound":
  168.                 return 'pubchemcompound';
  169.             case 'pubchem substance':
  170.                 return 'pubchemsubstance';
  171.             case 'drugs product database (dpd)':
  172.                 return 'dpd';
  173.             case 'kegg compound':
  174.             case 'kegg drug':
  175.                 return 'kegg';
  176.             case 'national drug code directory':
  177.                 return 'ndc';
  178.             case 'guide to pharmacology':
  179.                 return 'gtp';
  180.             case 'human protein reference database (hprd)':
  181.                 return 'hprd';
  182.             case 'genbank gene database':
  183.                 return 'genbank';
  184.             case 'genbank protein database':
  185.                 return 'gi';
  186.             case 'hugo gene nomenclature committee (hgnc)':
  187.                 return 'hgnc';
  188.                
  189.             default:
  190.                 return strtolower($source);
  191.         }
  192.     }
  193.    
  194.     function parsePartnerEntry($did, $pid, $x)
  195.     {
  196.         $debug = false;
  197.         if(!isset($x->polypeptide)) {
  198.             return;
  199.         }
  200.         $partner = $x->polypeptide;
  201.        
  202.         foreach($partner->children() AS $k1 => $v1) {
  203.             // get the direct values
  204.             $tv1 = trim($v1);
  205.             $nc  = count($v1->children());
  206.            
  207.             if($debug) echo "tv1 $nc $k1 $tv1".PHP_EOL;
  208.             if($nc == 0) {
  209.                 if($debug) echo "k1 $k1 $v1".PHP_EOL;
  210.                 parent::addRDF(
  211.                     parent::triplifyString($pid,parent::getVoc().$k1,(string)$v1)
  212.                 );
  213.                
  214.                 if($k1 == "organism") {
  215.                     $taxid = $v1->attributes()->{'ncbi-taxonomy-id'};
  216.                     parent::addRDF(
  217.                         parent::triplify($pid, parent::getVoc()."x-taxonomy", "taxonomy:$taxid")
  218.                     );
  219.                 }
  220.                 continue;
  221.             }
  222.  
  223.             foreach($v1->children() AS $k2 => $v2) {
  224.                 $tv2 = trim($v2);
  225.                 $nc  = count($v2->children());
  226.                
  227.                 if($debug) echo "tv2 $nc $k2 $tv2".PHP_EOL;
  228.                 if($nc == 0) {
  229.                     if($debug) echo "k2 $k2 $v2".PHP_EOL;
  230.                     parent::addRDF(
  231.                         parent::triplifyString($pid,parent::getVoc().$k2,(string)$v2)
  232.                     );
  233.                     continue;
  234.                 }
  235.                
  236.                 if($k2 == 'external-identifier') {
  237.                     $ns = $this->NSMap($v2->resource);
  238.                     $id = (string) $v2->identifier;
  239.                     $id = str_replace(array(" "),"",$id);
  240.                     $id = str_replace(array("HGNC:","GNC:"),"",$id);
  241.                     parent::addRDF(
  242.                         parent::triplify($pid, parent::getVoc()."x-$ns","$ns:$id")
  243.                     );
  244.                     if($ns == "uniprot") {
  245.                         parent::addRDF(
  246.                             parent::triplify("$ns:$id","skos:exactMatch","http://purl.uniprot.org/uniprot/$id")
  247.                         );
  248.                     }
  249.                 } else if($k2 == 'pfam') {
  250.                     parent::addRDF(
  251.                         parent::triplify($pid, parent::getVoc()."x-pfam","pfam:"."".$v2->identifier)
  252.                     );
  253.                 } else if($k2 == "go-classifier") {
  254.                     $e = array_search($v2->description, $this->go);
  255.                     if($e !== FALSE) {
  256.                         parent::addRDF(    
  257.                             parent::triplify($pid, parent::getVoc()."go-".$v2->category, $e)
  258.                         );             
  259.                     } else {
  260.                         parent::addRDF(            
  261.                             parent::triplifyString($pid, parent::getVoc()."go-".$v2->category, $v2->description)
  262.                         );
  263.                     }
  264.                 } else {
  265.                     trigger_error("no handler for $k2",E_USER_WARNING);
  266.     /*                  parent::addRDF(
  267.                             parent::triplifyString($pid, parent::getVoc().$k3, $v4)
  268.                         );
  269.     */
  270.                 }
  271.             }
  272.         }
  273.     }
  274.  
  275.    
  276.     function parsePartnerRelation($did, &$x,$type)
  277.     {  
  278.         $id = (string)$x->id;
  279.         $pid = "drugbank:".$id;
  280.         $lid = parent::getRes().substr($did,strpos($did,":")+1)."_".$id; // local pivot to keep the action between the drug and target
  281.         $parent = parent::getVoc().ucfirst(str_replace(" ","-",$type));
  282.         $name = (string) $x->name;
  283.         $knownaction = (string) $x->{'known-action'};
  284.         $knownaction = $knownaction=="unknown"?'':$knownaction;
  285.  
  286.         parent::addRDF(
  287.             parent::describeIndividual($pid,$name,$parent).
  288.             parent::describeClass($parent,ucfirst($type)).
  289.             parent::triplify($did,parent::getVoc().$type,$pid).
  290.             parent::describeIndividual($lid,"$did to $pid relation",parent::getVoc().ucfirst("$type-Relation")).
  291.             parent::describeClass(parent::getVoc().ucfirst("$type-Relation"), ucfirst("$type Relation")).
  292.             parent::triplify($lid,parent::getVoc()."drug",$did).
  293.             parent::triplify($lid,parent::getVoc().$type,$pid).
  294.             parent::triplifyString($lid, parent::getVoc()."known-action", $knownaction)
  295.         );
  296.        
  297.         // the main elements for the relation are actions, known-action, references,
  298.         foreach($x->actions AS $actions => $action) {
  299.             if(!trim($action)) continue;
  300.             $aid = str_replace(array(" ","/"),"-",$action->action);
  301.             $auri = parent::getVoc().$aid;
  302.             parent::addRDF(
  303.                 parent::describeIndividual($auri,$action->action,parent::getVoc()."Action").
  304.                 parent::describeClass(parent::getVoc()."Action","Action").
  305.                 parent::triplify($lid,parent::getVoc()."action",$auri)
  306.             ); 
  307.         }
  308.         if(isset($x->references)) {
  309.             foreach( explode("\n",$x->references) AS $ref) {
  310.                 preg_match_all("/pubmed\/([0-9]+)/",$ref,$m);
  311.                 foreach($m[1] AS $pmid) {
  312.                     parent::addRDF(
  313.                         parent::triplify($lid,parent::getVoc()."reference","pubmed:".$pmid)
  314.                     );
  315.                 }
  316.             }
  317.         }
  318.        
  319.         $this->parsePartnerEntry($did, $pid, $x);
  320.     }
  321.  
  322.     /**
  323.     * @description check if a type has already been defined and add appropriate RDF
  324.     * NOTE:: Should be moved into bio2rdfapi.php
  325.     */
  326.     function typify($id,$tid,$subclass,$label)
  327.     {
  328.         if(!isset($this->defined[$tid])) {
  329.              $this->defined[$tid] = '';
  330.              parent::addRDF(
  331.                  parent::describeClass($tid,$label,$this->getVoc().$subclass)
  332.              );
  333.         }
  334.         parent::addRDF(
  335.             parent::triplify($id,$this->getVoc().strtolower($subclass),$tid)
  336.         );
  337.     }
  338.  
  339.     function parseDrugEntry(&$xml)
  340.     {  
  341.         $declared    = null; // a list of all the entities declared
  342.         $counter     = 1;
  343.         $x           = $xml->GetXMLRoot();
  344.         $dbid        = (string) $x->{"drugbank-id"};
  345.         $did         = "drugbank:".$dbid;
  346.         $name        = (string)$x->name;
  347.         $type        = ucfirst((string)str_replace(" ","-",$x->attributes()->type));
  348.         $type_label  = ucfirst($x->attributes()->type);
  349.         $description = null;
  350.  
  351.         if(isset($this->id_list)) {
  352.             if(!isset($this->id_list[$dbid])) return;
  353.             unset($this->id_list[$dbid]);
  354.         }
  355.  
  356.         echo "Processing $dbid".PHP_EOL;
  357.         if(isset($x->description) && $x->description != '') {
  358.             $description = trim((string)$x->description);
  359.         }
  360.  
  361.         parent::addRDF(
  362.             parent::describeIndividual($did, $name, parent::getVoc()."Drug",$name, $description).
  363.             parent::describeClass(parent::getVoc()."Drug","Drug").
  364.             parent::triplify($did,"owl:sameAs","http://identifiers.org/drugbank/".$dbid).
  365.             parent::triplify($did,"rdfs:seeAlso","http://www.drugbank.ca/drugs/".$dbid).
  366.             parent::triplify($did,"rdf:type", parent::getVoc().$type).
  367.             parent::describeClass(parent::getVoc().$type, $type_label)
  368.         );
  369.  
  370.         foreach($x->{'drugbank-id'} AS $id) {
  371.             parent::addRDF(
  372.                 parent::triplifyString($did, parent::getVoc()."drugbank-id", $id)
  373.             );
  374.         }
  375.         if(isset($x->{'cas-number'})) {
  376.             parent::addRDF(
  377.                 parent::triplify($did, parent::getVoc()."x-cas", "cas:".$x->{'cas-number'})
  378.             );
  379.         }
  380.  
  381.         $literals = array(
  382.             "indication",
  383.             "pharmacodynamics",
  384.             "mechanism-of-action",
  385.             "toxicity",
  386.             "biotransformation",
  387.             "absorption",
  388.             "half-life",
  389.             "protein-binding",
  390.             "route-of-elimination",
  391.             "volume-of-distribution",
  392.             "clearance"
  393.         );
  394.  
  395.         foreach($literals AS $l) {
  396.             if(isset($x->$l) and $x->$l != '') {
  397.                 $lid = parent::getRes().md5($l.$x->$l);
  398.                 parent::addRDF(
  399.                     parent::describeIndividual($lid,"$l for $did",parent::getVoc().ucfirst($l), "$l for $did",$x->$l).
  400.                     parent::describeClass(parent::getVoc().ucfirst($l),ucfirst(str_replace("-"," ",$l))).
  401.                     parent::triplify($did,parent::getVoc().$l,$lid)
  402.                 );
  403.             }
  404.         }
  405.  
  406.        // TODO:: Replace the next two lines
  407.         $this->AddList($x,$did,"groups","group",parent::getVoc()."group");
  408.         $this->AddList($x,$did,"categories","category",parent::getVoc()."category");
  409.  
  410.         if(isset($x->classification)) {
  411.             foreach($x->classification->children() AS $k => $v) {
  412.                 $cid = parent::getRes().md5($v);
  413.                 parent::addRDF(
  414.                     parent::describeIndividual($cid, $v, parent::getVoc()."Drug-Classification-Category").
  415.                     parent::describeClass(parent::getVoc()."Drug-Classification-Category","Drug Classification Category").
  416.                     parent::triplify($did, parent::getVoc()."drug-classification-category", $cid)
  417.                 );
  418.             }
  419.         }
  420.  
  421.         $this->addLinkedResource($x, $did, 'atc-codes','atc-code','atc');
  422.         $this->addLinkedResource($x, $did, 'ahfs-codes','ahfs-code','ahfs');
  423.  
  424.         // taxonomy
  425.         $this->AddText($x,$did,"taxonomy","kingdom",parent::getVoc()."kingdom");
  426.  
  427.         // substructures
  428.         $this->AddText($x,$did,"taxonomy","substructures",parent::getVoc()."substructure", "substructure");
  429.  
  430.         // synonyms
  431.         $this->AddCategory($x,$did,"synonyms","synonym",parent::getVoc()."synonym");
  432.  
  433.         // brand names
  434.         $this->AddCategory($x,$did,"international-brands","international-brand",parent::getVoc()."brand");
  435.  
  436.         // salt
  437.         if(isset($x->salts->salt)) {
  438.             foreach($x->salts->salt AS $s) {
  439.                 $sid = parent::getPrefix().':'.$s->{'drugbank-id'};
  440.                 parent::addRDF(
  441.                     parent::describeIndividual($sid, $s->name, parent::getVoc()."Salt").
  442.                     parent::describeClass(parent::getVoc()."Salt", "Salt").
  443.                     parent::triplify($did, parent::getVoc()."salt", $sid).
  444.                     parent::triplify($sid, parent::getVoc()."x-cas", "cas:".$s->{'cas-number'}).
  445.                     parent::triplify($sid, parent::getVoc()."x-inchikey", "inchikey:".$s->{'inchikey'})
  446.                 );
  447.             }
  448.         }
  449.  
  450.         // mixtures
  451.         // <mixtures><mixture><name>Cauterex</name><ingredients>dornase alfa + fibrinolysin + gentamicin sulfate</ingredients></mixture>
  452.         if(isset($x->mixtures)) {
  453.             $id = 0;
  454.             foreach($x->mixtures->mixture AS $item) {
  455.                 if(isset($item)) {
  456.                     $o = $item;
  457.                     $mid = parent::getRes().md5(str_replace(" ","-",$o->name[0]));
  458.  
  459.                     parent::addRDF(
  460.                         parent::triplify($did,parent::getVoc()."mixture",$mid).
  461.                         parent::describeIndividual($mid,$o->name[0],parent::getVoc()."Mixture").
  462.                         parent::describeClass(parent::getVoc()."Mixture","mixture").
  463.                         parent::triplifyString($mid,$this->getVoc()."ingredients","".$o->ingredients[0])
  464.                     );
  465.  
  466.                     $a = explode(" + ",$o->ingredients[0]);
  467.                     foreach($a AS $b) {
  468.                         $b = trim($b);
  469.                         $iid = parent::getRes().str_replace(" ","-",$b);
  470.                         parent::addRDF(
  471.                             parent::describeClass($iid,$b, parent::getVoc()."Ingredient").
  472.                             parent::describeClass(parent::getVoc()."Ingredient","Ingredient").
  473.                             parent::triplify($mid,parent::getVoc()."ingredient",$iid)
  474.                         );
  475.                     }
  476.                 }
  477.             }
  478.         }
  479.  
  480.         // packagers
  481.         // <packagers><packager><name>Cardinal Health</name><url>http://www.cardinal.com</url></packager>
  482.         if(isset($x->packagers)) {
  483.             foreach($x->packagers AS $items) {
  484.                 if(isset($items->packager)) {
  485.                     foreach($items->packager AS $item) {
  486.                         $pid = parent::getRes().md5($item->name);
  487.  
  488.                         parent::addRDF(
  489.                             parent::triplify($did,parent::getVoc()."packager",$pid)
  490.                         );
  491.                         if(!isset($defined[$pid])) {
  492.                             $defined[$pid] = '';
  493.                             parent::addRDF(
  494.                                 parent::describe($pid,"".$item->name[0])
  495.                             );
  496.  
  497.                             if(strstr($item->url,"http://") && $item->url != "http://BASF Corp."){
  498.                                 parent::addRDF(
  499.                                     $this->triplify($pid,"rdfs:seeAlso","".$item->url[0])
  500.                                 );
  501.                             }
  502.                         }
  503.                     }
  504.                 }
  505.             }
  506.         }    
  507.  
  508.         // manufacturers
  509.         $this->AddText($x,$did,"manufacturers","manufacturer",parent::getVoc()."manufacturer"); // @TODO RESOURCE
  510.  
  511.         // prices
  512.         if(isset($x->prices->price)) {
  513.             foreach($x->prices->price AS $product) {
  514.                 $pid = parent::getRes().md5($product->description);
  515.                 parent::addRDF(
  516.                     parent::describeIndividual($pid,$product->description,parent::getVoc()."Pharmaceutical",$product->description).
  517.                     parent::describeClass(parent::getVoc()."Pharmaceutical","pharmaceutical").
  518.                     parent::triplifyString($pid,parent::getVoc()."price","".$product->cost,"xsd:float").
  519.                     parent::triplify($did, parent::getVoc()."product", $pid)
  520.                 );
  521.  
  522.                 $uid = parent::getVoc().md5($product->unit);
  523.                 parent::addRDF(
  524.                     parent::describeIndividual($uid,$product->unit,parent::getVoc()."Unit",$product->unit).
  525.                     parent::describeClass(parent::getVoc()."Unit","unit").
  526.                     parent::triplify($pid,parent::getVoc()."form",$uid)
  527.                 );
  528.             }
  529.         }
  530.  
  531.         // dosages <dosages><dosage><form>Powder, for solution</form><route>Intravenous</route><strength></strength></dosage>
  532.         if(isset($x->dosages->dosage)) {
  533.             foreach($x->dosages->dosage AS $dosage) {
  534.                 $id = parent::getRes().md5($dosage->strength.$dosage->form.$dosage->route);
  535.                 $label = (($dosage->strength != '')?$dosage->strength." ":"").$dosage->form." form with ".$dosage->route. " route";
  536.  
  537.                 parent::addRDF(
  538.                     parent::describeIndividual($id,$label,parent::getVoc()."Dosage").
  539.                     parent::describeClass(parent::getVoc()."Dosage","Dosage").
  540.                     parent::triplify($did, parent::getVoc()."dosage", $id)
  541.                 );
  542.  
  543.                 $rid = parent::getVoc().md5($dosage->route);
  544.                 $this->typify($id,$rid,"Route","".$dosage->route);
  545.  
  546.                 $fid =  parent::getVoc().md5($dosage->form);
  547.                 $this->typify($id,$fid,"Form","".$dosage->form);
  548.  
  549.                 if($dosage->strength != '') {
  550.                     parent::addRDF(
  551.                         parent::triplifyString($id, parent::getVoc()."strength", $dosage->strength)
  552.                     );
  553.                 }
  554.             }
  555.         }
  556.  
  557.         // experimental-properties
  558.         $props = array("experimental-properties","calculated-properties");
  559.         foreach($props AS $prop) {
  560.             $subtype = substr($prop,0,strpos("-",$prop));
  561.             if(isset($x->{$prop})) {
  562.                 foreach($x->{$prop} AS $properties) {
  563.                     foreach($properties AS $property) {
  564.                         $type  = (string) $property->kind;
  565.                         $value = (string) $property->value;
  566.                         $type_uri = parent::getVoc().ucfirst(str_replace(" ","-",$type));
  567.  
  568.                         $id = parent::getRes().$prop."-".$dbid."-".($counter++);
  569.                         $label = $property->kind.": $value".($property->source == ''?'':" from ".$property->source);
  570.                         parent::addRDF(
  571.                             parent::describeIndividual($id,$label,$type_uri).
  572.                             parent::describeClass($type_uri,$type,parent::getVoc().ucfirst($prop)).
  573.                             parent::describeClass(parent::getVoc().ucfirst($prop),str_replace("-"," ",$prop)).
  574.                             parent::triplifyString($id,$this->getVoc()."value",$value).
  575.                             parent::triplify($did,$this->getVoc().$prop,$id)
  576.                         );
  577.  
  578.                         // Source
  579.                         if(isset($property->source)) {
  580.                             foreach($property->source AS $source) {
  581.                                 $s = (string) $source;
  582.                                 if($s == '') continue;
  583.                                 $sid = parent::getRes().md5($s);
  584.                                 parent::addRDF(
  585.                                     parent::describeIndividual($sid,$s,parent::getVoc()."Source").
  586.                                         parent::describeClass(parent::getVoc()."Source","Source").
  587.                                         parent::triplify($id,parent::getVoc()."source",$sid)
  588.                                 );
  589.                             }
  590.                         }      
  591.                     }
  592.                 }
  593.             }
  594.         }
  595.  
  596.    
  597.         // identifiers
  598.         // <patents><patent><number>RE40183</number><country>United States</country><approved>1996-04-09</approved>        <expires>2016-04-09</expires>
  599.         if(isset($x->patents->patent)) {
  600.             foreach($x->patents->patent AS $patent) {
  601.                 $id = "uspto:".$patent->number;
  602.  
  603.                 parent::addRDF(
  604.                     parent::triplify($did,$this->getVoc()."patent",$id).
  605.                     parent::describeIndividual($id,$patent->country." patent ".$patent->number,$this->getVoc()."Patent").
  606.                     parent::describeClass(parent::getVoc()."Patent","patent").
  607.                     parent::triplifyString($id,$this->getVoc()."approved","".$patent->approved).
  608.                     parent::triplifyString($id,$this->getVoc()."expires","".$patent->expires)
  609.                 );
  610.  
  611.                 $cid = parent::getRes().md5($patent->country);
  612.                 $this->typify($id,$cid,"Country","".$patent->country);
  613.             }
  614.         }
  615.    
  616.         // partners
  617.         $partners = array('target','enzyme','transporter','carrier');
  618.         foreach($partners AS $partner) {
  619.             $plural = $partner.'s';
  620.             if(isset($x->$plural)) {
  621.                 foreach($x->$plural AS $list) {
  622.                     foreach($list->$partner AS $item) {  
  623.                         $this->parsePartnerRelation($did,$item,$partner);
  624.                         parent::writeRDFBufferToWriteFile();
  625.                     }
  626.                 }
  627.             }
  628.         }
  629.    
  630.        
  631.         // drug-interactions
  632.         $y = (int) substr($dbid,2);
  633.         if(isset($x->{"drug-interactions"})) {
  634.             foreach($x->{"drug-interactions"} AS $ddis) {
  635.                 foreach($ddis->{"drug-interaction"} AS $ddi) {
  636.                     $dbid2 = $ddi->{'drugbank-id'};
  637.                     if($dbid < $dbid2) { // don't repeat
  638.                         $ddi_id = parent::getRes().$dbid."_".$dbid2;
  639.                         parent::addRDF(
  640.                             parent::triplify("drugbank:".$dbid,parent::getVoc()."ddi-interactor-in","".$ddi_id).
  641.                             parent::triplify("drugbank:".$dbid2,parent::getVoc()."ddi-interactor-in","".$ddi_id).
  642.                             parent::describeIndividual($ddi_id,"DDI between $name and ".$ddi->name." - ".$ddi->description,parent::getVoc()."Drug-Drug-Interaction").
  643.                             parent::describeClass(parent::getVoc()."Drug-Drug-Interaction","drug-drug interaction")
  644.                         );
  645.                     }
  646.                 }
  647.             }
  648.         }
  649.  
  650.         // food-interactions
  651.         $this->AddText($x,$did,"food-interactions","food-interaction",parent::getVoc()."food-interaction");
  652.  
  653.         // affected-organisms
  654.         $this->AddCategory($x,$did,"affected-organisms","affected-organism",parent::getVoc()."affected-organism");
  655.  
  656.         //  <external-identifiers>
  657.         if(isset($x->{"external-identifiers"})) {
  658.             foreach($x->{"external-identifiers"} AS $objs) {
  659.                 foreach($objs AS $obj) {
  660.                     $ns = $this->NSMap($obj->resource);
  661.                     $id = $obj->identifier;
  662.                     if($ns == "genecards") $id = str_replace(array(" "),array("_"),$id);
  663.  
  664.                     parent::addRDF(
  665.                         parent::triplify($did,parent::getVoc()."x-$ns","$ns:$id")
  666.                     );
  667.                     if($ns == "pubchemcompound") {
  668.                         parent::addRDF(
  669.                             parent::triplify("$ns:$id","skos:exactMatch","http://rdf.ncbi.nlm.nih.gov/pubchem/compound/$id")
  670.                         );
  671.                     }
  672.  
  673.                 }
  674.             }
  675.         }
  676.         // <external-links>
  677.         if(isset($x->{"external-links"})) {
  678.             foreach($x->{"external-links"}->{'external-link'} AS $el) {
  679.                 if(strpos($el->url,'http') !== false) {
  680.                     parent::addRDF(
  681.                         parent::triplify($did,"rdfs:seeAlso","".$el->url)
  682.                     );
  683.                 }
  684.             }
  685.         }
  686.         parent::writeRDFBufferToWriteFile();
  687.     }
  688.  
  689.     function AddLinkedResource(&$x, $id, $list_name,$item_name,$ns)
  690.     {
  691.         if(isset($x->$list_name)) {
  692.             foreach($x->$list_name AS $item) {
  693.                 if(isset($item->$item_name) && ($item->$item_name != '')) {
  694.                     if($item_name == "atc-code") {
  695.                         $l = $ns.":".$item->$item_name->attributes()->code;
  696.                     } else {
  697.                         $l = $ns.":".$item->$item_name;
  698.                     }
  699.                     $this->addRDF($this->triplify($id,parent::getVoc()."x-$ns",trim($l)));
  700.                 }
  701.             }
  702.         }
  703.     }
  704.  
  705.     function AddText(&$x, $id, $list_name,$item_name,$predicate, $list_item_name = null)
  706.     {
  707.         if(isset($x->$list_name)) {
  708.             foreach($x->$list_name AS $item) {
  709.                 if(isset($item->$item_name) && ($item->$item_name != '')) {
  710.                     $l = $item->$item_name;
  711.                     if(isset($l->$list_item_name)) {
  712.                         foreach($l->$list_item_name AS $k) {
  713.                             $kid = parent::getRes().md5($k);
  714.                             $this->addRDF(
  715.                                 $this->describeIndividual($kid,"$item_name for $id",parent::getVoc().ucfirst($item_name)).
  716.                                 $this->describeClass(parent::getVoc().ucfirst($item_name),$item_name).
  717.                                 $this->triplifyString($kid,"rdf:value",$k).
  718.                                 $this->triplify($id,$predicate,$kid)
  719.                             );
  720.                         }
  721.                     } else {
  722.                         $kid = parent::getRes().md5($l);
  723.                         $this->addRDF(
  724.                             $this->describeIndividual($kid,"$item_name for $id",parent::getVoc().ucfirst($item_name)).
  725.                             $this->describeClass(parent::getVoc().ucfirst($item_name),$item_name).
  726.                             $this->triplifyString($kid,"rdf:value",$l).
  727.                             $this->triplify($id,$predicate,$kid)
  728.                         );
  729.                     }
  730.                 }
  731.             }
  732.         }
  733.     }
  734.    
  735.     function AddCategory(&$x, $id, $list_name, $item_name, $predicate, $list_item_name = null)
  736.     {
  737.         if(isset($x->$list_name)) {
  738.             foreach($x->$list_name AS $item) {
  739.                 if(isset($item->$item_name) && ($item->$item_name != '')) {
  740.                     $l = $item->$item_name;
  741.                     $att = ($l->attributes());
  742.                     foreach($l AS $key => $item_value) {
  743.                         $kid = parent::getvoc().md5($item_value);
  744.                         $this->addRDF(
  745.                             $this->describeIndividual($kid,"".$item_value,parent::getVoc().ucfirst($item_name)).
  746.                             $this->describeClass(parent::getVoc().ucfirst($item_name),ucfirst("".$item_name)).
  747.                             $this->triplify($id,$predicate,$kid)
  748.                         );
  749.                         foreach($att AS $ka => $va) {
  750.                             parent::addRDF(
  751.                                 $this->triplifyString($kid, parent::getVoc().$ka, "".$va)
  752.                             );
  753.                         }
  754.                     }
  755.                     $kid = parent::getvoc().md5($l->asXML());
  756.                     foreach($l->children() AS $k2 => $v2) {
  757.                         $this->addRDF(
  758.                             $this->describeIndividual($kid,($k2=="name"?$v2:$predicate),parent::getVoc().ucfirst($k2)).
  759.                             $this->describeClass(parent::getVoc().ucfirst($k2),ucfirst("".$v2)).
  760.                             $this->triplifyString($kid, parent::getVoc().$k2, $v2).
  761.                             $this->triplify($id, $predicate, $kid)
  762.                         );
  763.                     }
  764.                 }
  765.             }
  766.         }
  767.     }
  768.  
  769.     function AddList(&$x, $id, $list_name, $item_name, $predicate, $list_item_name = null)
  770.     {
  771.         if(isset($x->$list_name)) {
  772.             foreach($x->$list_name->$item_name AS $k => $item) {
  773.                 if(isset($item->$item_name)) {
  774.                     foreach($item->$item_name AS $k => $v) {
  775.                         $mylist[] = ''.$v;
  776.                     }
  777.                 } else $mylist[] = ''.$item;
  778.             }
  779.         }
  780.         if(isset($mylist)) {
  781.             foreach($mylist AS $item) {
  782.                 $label = ''.$item;
  783.                 $kid = parent::getVoc().ucfirst(str_replace(" ","-",$label)); // generate a new identifier for the list item
  784.                 $this->addRDF(
  785.                     $this->describeIndividual($kid,$label,parent::getVoc().ucfirst($item_name)).
  786.                     $this->describeClass(parent::getVoc().ucfirst($item_name),ucfirst($item_name)).
  787.                     $this->triplify($id,$predicate,$kid)
  788.                 );
  789.             }
  790.         }
  791.     }
  792.  
  793.     function getGO()
  794.     {
  795.         $this->go = null;
  796.        
  797.         $server = "http://bio2rdf.org/sparql";
  798.         $sparql = "PREFIX dct: <http://purl.org/dc/terms/>
  799. SELECT distinct ?id ?title
  800. {
  801. ?go a <http://bio2rdf.org/go_vocabulary:Resource> .
  802. ?go dct:identifier ?id.
  803. ?go dct:title ?title .
  804. } ";
  805.         $url = $server."?query=".urlencode($sparql)."&format=".urlencode("text/tab-separated-values");
  806.  
  807.         $results = file_get_contents($url);
  808.         if($results === FALSE) {
  809.             trigger_error("Unable to get Gene Ontology labels",E_USER_WARNING);
  810.             return false;
  811.         }
  812.         $list = explode("\n",$results);
  813.         array_shift($list); array_pop($list);  // remove first and last
  814.        
  815.         foreach($list AS $v) {
  816.             $b = explode("\t",str_replace('"','',$v));
  817.             $this->go[$b[0]] = $b[1];
  818.         }
  819.         return true;
  820.     }
  821.    
  822. } // end class
  823.  
  824. ?>
Advertisement
Add Comment
Please, Sign In to add comment