|
| 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 | +require_once(__DIR__.'/../../php-lib/bio2rdfapi.php'); |
| 25 | +require_once(__DIR__.'/../../php-lib/xmlapi.php'); |
| 26 | + |
| 27 | +/** |
| 28 | + * MIRIAM DB RDFizer |
| 29 | + * @version 1.0 |
| 30 | + * @author Michel Dumontier |
| 31 | + * @description http://www.ebi.ac.uk/miriam/ |
| 32 | +*/ |
| 33 | +class MIRIAMParser extends Bio2RDFizer |
| 34 | +{ |
| 35 | + private $version = null; |
| 36 | + |
| 37 | + function __construct($argv) { |
| 38 | + parent::__construct($argv,"miriam"); |
| 39 | + parent::addParameter('files',true,'all','all','files to process'); |
| 40 | + parent::addParameter('download_url',false,null,'http://www.ebi.ac.uk/miriam/main/export/xml/'); |
| 41 | + parent::addParameter('overwrite',false,'true|false','false','overwrite existing files with download option'); |
| 42 | + parent::initialize(); |
| 43 | + } |
| 44 | + |
| 45 | + function Run() |
| 46 | + { |
| 47 | + echo "processing miriam database"; |
| 48 | + |
| 49 | + // directory shortcuts |
| 50 | + $ldir = $this->getParameterValue('indir'); |
| 51 | + $odir = $this->getParameterValue('outdir'); |
| 52 | + |
| 53 | + // download and set the read file |
| 54 | + $file = 'miriam.xml'; |
| 55 | + $rfile = $this->getParameterValue("download_url"); |
| 56 | + $lfile = $ldir.$file; |
| 57 | + if(!file_exists($lfile) || $this->getParameterValue("download") == "true") { |
| 58 | + utils::downloadSingle($rfile,$lfile); |
| 59 | + } |
| 60 | + |
| 61 | + parent::setReadFile($lfile); |
| 62 | + |
| 63 | + // set the write file |
| 64 | + $outfile = "miriam.".parent::getParameterValue('output_format'); |
| 65 | + $gz = (strstr(parent::getParameterValue('output_format'),".gz") === FALSE)?false:true; |
| 66 | + parent::setWriteFile(parent::getParameterValue("outdir").$outfile,$gz); |
| 67 | + $this->parse(); |
| 68 | + parent::WriteRDFBufferToWriteFile(); |
| 69 | + $this->getWriteFile()->Close(); |
| 70 | + |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + function parse() |
| 75 | + { |
| 76 | + // convert into json |
| 77 | + $lfile = parent::getReadFile()->getFileName(); |
| 78 | + $xml = simplexml_load_file($lfile); |
| 79 | + $json = json_encode($xml); |
| 80 | + $db = json_decode($json,TRUE); |
| 81 | + |
| 82 | + // miriam metadata |
| 83 | + // $attributes = $db['@attributes']; |
| 84 | + foreach($db['datatype'] AS $item) { |
| 85 | + $this->parseItem($item); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + function parseItem($item) |
| 90 | + { |
| 91 | + $id = $item['@attributes']['id']; |
| 92 | + $label = $item['name']; |
| 93 | + |
| 94 | + parent::addRDF( |
| 95 | + parent::describeIndividual($id, $item['name'], parent::getVoc()."Entry"). |
| 96 | + parent::describeClass(parent::getVoc()."Entry","MIRIAM database entry"). |
| 97 | + parent::triplifyString($id, parent::getVoc()."namespace", $item['namespace']) |
| 98 | + ); |
| 99 | + |
| 100 | + if(isset($item['@attributes'])) { |
| 101 | + foreach($item['@attributes'] AS $k => $v) { |
| 102 | + parent::addRDF( |
| 103 | + parent::triplifyString($id, parent::getVoc().$k, $v) |
| 104 | + ); |
| 105 | + } |
| 106 | + } |
| 107 | + if(isset($item['comment'])) parent::addRDF(parent::triplifyString($id, parent::getVoc()."comment", $item['comment'])); |
| 108 | + if(isset($item['definition'])) parent::addRDF(parent::triplifyString($id, parent::getVoc()."definition", $item['definition'])); |
| 109 | + if(isset($item['synonyms'])) { |
| 110 | + $mylist = null; |
| 111 | + if(is_array($item['synonyms']['synonym'])) $mylist = $item['synonyms']['synonym']; |
| 112 | + else $mylist[] = $item['synonyms']['synonym']; |
| 113 | + foreach($mylist AS $myitem) { |
| 114 | + parent::addRDF( |
| 115 | + parent::triplifyString($id, "skos:altLabel", $myitem) |
| 116 | + ); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + if(isset($item['uris'])) { |
| 121 | + foreach($item['uris']['uri'] AS $uri) { |
| 122 | + parent::addRDF( |
| 123 | + parent::triplifyString($id, parent::getVoc()."uri", $uri) |
| 124 | + ); |
| 125 | + } |
| 126 | + } |
| 127 | + if(isset($item['resources'])) { |
| 128 | + $mylist = null; |
| 129 | + if(!isset($item['resources']['resource']['dataEntry'])) $mylist = $item['resources']['resource']; |
| 130 | + else $mylist[] = $item['resources']['resource']; |
| 131 | + foreach($mylist AS $myitem) { |
| 132 | + $rid = $myitem['@attributes']['id']; |
| 133 | + parent::addRDF( |
| 134 | + parent::describeIndividual($rid, $myitem['dataInfo'], parent::getVoc()."Resource"). |
| 135 | + parent::describeClass(parent::getVoc()."Resource", "MIRIAM Resource"). |
| 136 | + parent::triplify($rid, parent::getVoc()."url", $myitem['dataResource']). |
| 137 | + parent::triplifyString($rid, parent::getVoc()."urlTemplate", $myitem['dataEntry']). |
| 138 | + parent::triplifyString($rid, parent::getVoc()."organization", is_array($myitem['dataInstitution'])?"":$myitem['dataInstitution']). |
| 139 | + parent::triplifyString($rid, parent::getVoc()."location", is_array($myitem['dataLocation'])?"":$myitem['dataLocation']). |
| 140 | + parent::triplify($id, parent::getVoc()."resource", $rid) |
| 141 | + ); |
| 142 | + } |
| 143 | + } |
| 144 | + if(isset($item['tags'])) { |
| 145 | + $i = $item['tags']['tag']; |
| 146 | + $mylist = null; |
| 147 | + if(!is_array($i)) $mylist[] = $i; |
| 148 | + else $mylist = $i; |
| 149 | + foreach($mylist AS $myitem) { |
| 150 | + parent::addRDF( |
| 151 | + parent::triplifyString($id, parent::getvoc()."tag", $myitem) |
| 152 | + ); |
| 153 | + }} |
| 154 | + |
| 155 | + if(isset($item['documentations'])) { |
| 156 | + $i = $item['documentations']['documentation']; |
| 157 | + $mylist = null; |
| 158 | + if(!is_array($i)) $mylist[] = $i; |
| 159 | + else $mylist = $i; |
| 160 | + foreach($mylist AS $myitem) { |
| 161 | + if(strstr($myitem, "pubmed")) $uri = "pubmed:".substr($myitem, strrpos($myitem, ":")+1); |
| 162 | + else if(strstr($myitem, "doi")) $uri = "http://dx.doi.org/".substr($myitem, strpos($myitem, "doi:")); |
| 163 | + else $uri = $myitem; |
| 164 | + |
| 165 | + parent::addRDF( |
| 166 | + parent::triplify($id, parent::getvoc()."documentation", $uri) |
| 167 | + ); |
| 168 | + }} |
| 169 | + |
| 170 | + if(isset($item['restrictions'])) { |
| 171 | + $mylist = null; |
| 172 | + if(!isset($item['restrictions']['restriction']['statement'])) $mylist = $item['restrictions']['restriction']; |
| 173 | + else $mylist[] = $item['restrictions']['restriction']; |
| 174 | + foreach($mylist AS $i => $myitem) { |
| 175 | + $rid = parent::getRes().str_replace(":","",$id)."_".($i+1); |
| 176 | + $a = $myitem['@attributes']; |
| 177 | + $rid_type = parent::getVoc().'restriction_type_'.$a['type']; |
| 178 | + |
| 179 | + parent::addRDF( |
| 180 | + parent::describeIndividual($rid, $a['desc'], parent::getVoc()."Restriction"). |
| 181 | + parent::describeClass(parent::getVoc()."Restriction", "Resource Restriction"). |
| 182 | + parent::triplify($rid, "rdf:type", $rid_type). |
| 183 | + parent::describeClass($rid_type, $a['desc'], parent::getVoc()."Restriction"). |
| 184 | + parent::triplifyString($rid, "dct:description", $myitem['statement']). |
| 185 | + parent::triplify($rid, "foaf:page", isset($myitem['link'])?$myitem['link']:""). |
| 186 | + parent::triplify($id, parent::getVoc()."restriction", $rid) |
| 187 | + ); |
| 188 | + }} |
| 189 | + |
| 190 | + /* |
| 191 | + <annotation> |
| 192 | + <format name="SBML"> |
| 193 | + <elements> |
| 194 | + <element>reaction</element> |
| 195 | + <element>event</element> |
| 196 | + <element>rule</element> |
| 197 | + <element>species</element> |
| 198 | + </elements> |
| 199 | + </format> |
| 200 | + */ |
| 201 | + if(isset($item['annotation'])) { |
| 202 | + $mylist = null; |
| 203 | + if(!isset($item['annotation']['format']['elements'])) $mylist = $item['annotation']['format']; |
| 204 | + else $mylist[] = $item['annotation']['format']; |
| 205 | + foreach($mylist AS $i => $myitem) { |
| 206 | + $name = $myitem['@attributes']['name']; |
| 207 | + $myid = str_replace("MIR:",parent::getRes(), $id)."_annotation_".($i+1)."_".urlencode($name); |
| 208 | + parent::addRDF( |
| 209 | + parent::describeIndividual($myid, "$label used by $name", parent::getVoc()."ValueSet"). |
| 210 | + parent::describeClass(parent::getVoc()."ValueSet", "MIRIAM Value Set"). |
| 211 | + parent::triplifyString($myid, parent::getVoc()."used-in", $name). |
| 212 | + parent::triplify($myid, parent::getVoc()."uses", $id) |
| 213 | + ); |
| 214 | + |
| 215 | + $b = $myitem['elements']['element']; |
| 216 | + $mylist2 = null; |
| 217 | + if(!is_array($b)) $mylist2[] = $b; |
| 218 | + else $mylist2 = $b; |
| 219 | + foreach($mylist2 AS $i => $e) { |
| 220 | + parent::addRDF( |
| 221 | + parent::triplifyString($myid, parent::getVoc()."used-for", $e) |
| 222 | + ); |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + } |
| 227 | + |
| 228 | + } |
| 229 | + |
| 230 | +} |
| 231 | + |
| 232 | +?> |
0 commit comments