-
Notifications
You must be signed in to change notification settings - Fork 15
/
biopax2bio2rdf.php
344 lines (302 loc) · 11 KB
/
biopax2bio2rdf.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
/**
Copyright (C) 2012 Michel Dumontier
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
require_once(__DIR__.'/../arc2/ARC2.php'); // available on git @ https://github.com/semsol/arc2.git
/**
* BioPAX2Bio2RDF RDFizer library
* @version 1.0
* @author Michel Dumontier
*/
Class BioPAX2Bio2RDF
{
private $file = null;
private $buf = null;
private $biopax = null;
private $base_ns = null;
private $bio2rdf_ns = null;
private $dataset_uri = null;
private $declared = null;
private $rdfizer = null;
function __construct($rdfizer)
{
$this->setRdfizer($rdfizer);
}
function SetFile($file)
{
$this->file = $file;
return $this;
}
function SetBuffer($buf)
{
$this->buf = $buf;
return $this;
}
function SetBioPAXVersion($version)
{
if($version < 1 || $version > 3) {
trigger_error("Invalid BioPAX version $version");
return FALSE;
}
$this->biopax_ns = "http://www.biopax.org/release/biopax-level".$version.".owl#";
if($version == 1 || $version == 2) {
$this->biopax['db'] = $this->biopax_ns."DB";
$this->biopax['id'] = $this->biopax_ns."ID";
$this->biopax['xref'] = $this->biopax_ns."XREF";
$this->biopax['name'] = $this->biopax_ns."DISPLAY-NAME";
$this->biopax['term'] = $this->biopax_ns."TERM";
$this->biopax['comment'] = $this->biopax_ns."COMMENT";
$this->biopax['unificationXref'] = $this->biopax_ns."UnificationXref";
$this->biopax['relationshipXref'] = $this->biopax_ns."RelationshipXref";
$this->biopax['publicationXref'] = $this->biopax_ns."PublicationXref";
$this->biopax['Xref'] = $this->biopax_ns."Xref";
} else if($version == 3) {
// relations
$this->biopax['db'] = $this->biopax_ns."db";
$this->biopax['id'] = $this->biopax_ns."id";
$this->biopax['xref'] = $this->biopax_ns.'xref';
$this->biopax['name'] = $this->biopax_ns."displayName";
$this->biopax['term'] = $this->biopax_ns."term";
$this->biopax['comment'] = $this->biopax_ns."comment";
// types
$this->biopax['unificationXref'] = $this->biopax_ns."UnificationXref";
$this->biopax['relationshipXref'] = $this->biopax_ns."RelationshipXref";
$this->biopax['publicationXref'] = $this->biopax_ns."PublicationXref";
$this->biopax['Xref'] = $this->biopax_ns."Xref";
}
return $this;
}
function SetBaseNamespace($base_ns)
{
$this->base_ns = $base_ns;
return $this;
}
function SetBio2RDFNamespace($bio2rdf_ns)
{
$this->bio2rdf_ns = $bio2rdf_ns;
return $this;
}
function SetDatasetURI($dataset_uri)
{
$this->dataset_uri = $dataset_uri;
return $this;
}
function SetRDFizer($rdfizer){
$this->rdfizer = $rdfizer;
return $this;
}
function GetRDFizer(){
return $this->rdfizer;
}
function Parse()
{
$this->declared = null;
$parser = ARC2::getRDFXMLParser();
if(isset($this->file)) {
$parser->parse('file://'.$this->file);
} else if(isset($this->buf)) {
$parser->parse($this->base_ns, $this->buf);
} else {
trigger_error("Set a file or buffer to parse", E_USER_ERROR);
exit;
}
$rdf_type = $this->getRDFizer()->getRegistry()->GetFQURI("rdf:type", "provider-uri");
$url = $this->getRDFizer()->getRegistry()->GetFQURI("bio2rdf_vocabulary:url");
$triples = $parser->getTriples();
foreach($triples AS $i => $a) {
$s = str_replace(" ","",$a['s']);
if($a['s_type'] == 'bnode') {
// make a uri
$s = $this->bio2rdf_ns.substr($s,2);
}
$o['type'] = $a['o_type'];
$o['datatype'] = $a['o_datatype'];
$o['value'] = $a['o'];
if($a['o_type'] == 'uri') {
$o['value'] = str_replace(" ","",$a['o']);
}
if($a['o_type'] == 'bnode') {
$o['type'] = 'uri';
$o['value'] = $this->bio2rdf_ns.substr($a['o'],2);
}
$index[$s][$a['p']][] = $o;
}
if(!isset($index)) return '';
// generate the bio2rdf / identifiers.org xrefs
foreach($index AS $s => $p_list) {
foreach($p_list AS $p => $o_list) {
// find and reformat xrefs
if($p == $this->biopax['xref']) {
foreach($o_list AS $i => $o) {
$o_uri = $o['value'];
// check if we have the normalized id
if(isset($xrefs[$o_uri])) {
$new_uri = $xrefs[$o_uri];
} else {
$xref_obj = $index[$o_uri];
// generate the id
if(!isset($xrefs[$o_uri])) {
if(!isset($xref_obj[$this->biopax['id']][0]['value'])) {
continue;
}
$id_string = $xref_obj[$this->biopax['id']][0]['value'];
//check for PANTHER domain IDs which can contain colons
if(stristr($id_string, "pthr")){
$id = $id_string;
} else {
$this->getRDFizer()->getRegistry()->ParseQName($id_string,$db,$id);
}
if(!$db) {
if(isset($xref_obj[$this->biopax['db']][0]['value'])) {
$db = $xref_obj[$this->biopax['db']][0]['value'];
} else {
// badly formed biopax
continue;
}
}
if($db == "ICD") $db = "icd9";
$qname = $this->getRDFizer()->getRegistry()->MapQName("$db:$id");
$this->getRDFizer()->getRegistry()->ParseQName($qname,$db,$id);
if($db == "go") {
if(!is_numeric($id[0])) {
echo "skipping non-numeric GO identifier: $id".PHP_EOL;
continue;
}
}
$new_uri = $this->getRDFizer()->getRegistry()->getFQURI($qname);
// set the new uri
$xrefs[$o_uri] = $new_uri;
// add to the index
$o = '';
$o['value'] = $db;
$o['type'] = 'literal';
$o['datatype'] = '';
$index[$new_uri][$this->biopax['db']][] = $o;
$o = '';
$o['value'] = $id;
$o['type'] = 'literal';
$o['datatype'] = 'http://www.w3.org/2001/XMLSchema#string';
$index[$new_uri][$this->biopax['id']][] = $o;
$o = '';
$o['value'] = "$db:$id";
$o['type'] = 'literal';
$o['datatype'] = 'http://www.w3.org/2001/XMLSchema#string';
$index[$new_uri][$this->getRDFizer()->getRegistry()->GetFQURI("dc:identifier")][] = $o;
} else $new_uri = $xrefs[$o_uri];
// now determine the nature of the relation
$type = $xref_obj[$rdf_type][0]['value'];
if($type == $this->biopax['unificationXref']) {
$rel = $this->getRDFizer()->getRegistry()->GetFQURI("biopax_vocabulary:identical-to");
} elseif($type == $this->biopax['relationshipXref']) {
$rel = $this->getRDFizer()->getRegistry()->GetFQURI("biopax_vocabulary:related-to");
} elseif($type == $this->biopax['publicationXref']) {
$rel = $this->getRDFizer()->getRegistry()->GetFQURI("biopax_vocabulary:publication");
} elseif($type == $this->biopax['Xref']) {
$rel = $this->getRDFizer()->getRegistry()->GetFQURI("biopax_vocabulary:related-to");
}
// unset the old ref and put in the new one
unset($index[$o_uri]);
unset($index[$s][$p][$i]);
if(count($index[$s][$p]) == 0) {
unset($index[$s][$p]);
}
$o = '';
$o['value'] = $new_uri;
$o['type'] = 'uri';
$o['datatype'] = '';
$index[$s][$rel][] = $o;
} // if
} // foreach o_list
} // if
} // foreach p_list
} // foreach index
$rdf = '';
foreach($index AS $s => $p_list) {
preg_match("/http\:\/\/identifiers\.org\/([^\/]+)\/(.*)/",$s,$m);
if(isset($m[1])) {
$ns = $m[1];
$id = $m[2];
if($ns == "biomodels.db") {
$id = str_replace("/","_",$m[2]);
}
$this->getRDFizer()->getRegistry()->parseQName($id,$ns2,$id2);
if($ns2) {
$id = $id2;
}
$s = "http://identifiers.org/$ns/$id";
$s_uri = $this->getRDFizer()->getRegistry()->getFQURI("$ns:$id");
} else {
$s_uri = str_replace($this->base_ns,$this->bio2rdf_ns,$s);
if(!$s_uri) {
continue;
}
}
// check for bio2rdf
if(FALSE !== ($pos = strpos($s_uri,"http://bio2rdf.org/"))) {
$a = explode (":",substr($s_uri,19),2);
$s_uri = $a[0].":".$a[1];
}
if(isset($this->dataset_uri)) {
$rdf .= $this->getRDFizer()->triplify($s_uri,"void:inDataset",$this->dataset_uri);
}
if($s[0] != '_' && $s != $s_uri) $rdf .= $this->getRDFizer()->triplify($s_uri,"owl:sameAs",$s);
// add an rdfs:label
if(isset($p_list[$this->biopax['name']][0]['value'])) {
$label = $p_list[$this->biopax['name']][0]['value'];
if($label) $rdf .= $this->getRDFizer()->triplifyString($s_uri,"rdfs:label",$label);
}
if(isset($p_list[$this->biopax['term']][0]['value'])) {
$label = $p_list[$this->biopax['term']][0]['value'];
if($label) $rdf .= $this->getRDFizer()->triplifyString($s_uri,"rdfs:label",$label);
}
foreach($p_list AS $p => $o_list) {
foreach($o_list AS $o) {
if($o['type'] == 'uri') {
$o_uri = str_replace($this->base_ns,$this->bio2rdf_ns,$o['value']);
$rdf .= $this->getRDFizer()->triplify($s_uri,$p,$o_uri);
if(!isset($this->declared[$p])) {
$this->declared[$p] = '';
$rdf .= $this->getRDFizer()->triplify($p,"rdf:type", "owl:ObjectProperty");
}
} elseif($o['type'] == 'bnode') {
$rdf .= $this->getRDFizer()->triplify($s_uri,$p ,$o['value']);
if(!isset($this->declared[$p])) {
$this->declared[$p] = '';
$rdf .=$this->getRDFizer()->triplify($p,"rdf:type","owl:ObjectProperty");
}
} else if($o['type'] == 'literal') {
$literal = $this->getRDFizer()->SafeLiteral($o['value']);
if($literal == '') continue;
if(!isset($this->declared[$p])) {
$this->declared[$p] = '';
if($p == $this->biopax['comment'] || $p == $this->getRDFizer()->getRegistry()->getFQURI("dc:identifier")) {
$rdf .= $this->getRDFizer()->triplify($p,"rdf:type","owl:AnnotationProperty");
} else $rdf .= $this->getRDFizer()->triplify($p,"rdf:type","owl:DatatypeProperty");
}
$datatype = null;
if(isset($o['datatype']) && $o['datatype'] != '') {
$datatype = $o['datatype'];
}
$rdf .= $this->getRDFizer()->triplifyString($s_uri,$p,$literal,$datatype);
}
} // foreach o_list
} // foreach p_list
} // foreach index
return $rdf;
}
} // class