123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510 |
- <?php
- require_once ('OWLStanza.inc');
- function tripal_cv_parse_owl($filename) {
-
-
-
-
-
- $owl = new XMLReader();
-
- if (!$owl->open($filename)) {
- print "ERROR opening OWL file: '$filename'\n";
- exit();
- }
-
-
- $rdf = new OWLStanza($owl, FALSE);
-
-
- $ontology = new OWLStanza($owl);
-
-
- $namespace = $ontology->getChild('oboInOwl:default-namespace');
- if ($namespace) {
- $db_name = $namespace->getValue();
- }
- else {
-
- $about = $ontology->getAttribute('rdf:about');
-
- if (preg_match('/^.*\/(.*?)\..*$/', $about, $matches)) {
- $db_name = strtoupper($matches[1]);
- }
- }
-
-
-
-
-
- $stanza = new OWLStanza($owl);
-
- $deps = array (
- 'db' => array (),
- 'dbxref' => array ()
- );
-
- while (!$stanza->isFinished()) {
-
- switch ($stanza->getTagName()) {
- case 'owl:Class':
- tripal_owl_check_class_depedencies($stanza, $vocab_db_name, $deps);
- break;
- }
-
- $stanza = new OWLStanza($owl);
- }
- if (count(array_keys($deps['db'])) > 0 or count(array_keys($deps['dbxref'])) > 0) {
-
-
- if (count($deps['db']) > 0) {
- drupal_set_message('Cannot import ontology, "' . $db_name . '", as the following ' . 'dependent vocabularies must first be imported: ' . print_r(array_keys($deps['db']), TRUE) . '\n', 'error');
- }
- if (count($deps['dbxref']) > 0) {
- drupal_set_message('Cannot import ontology, "' . $db_name . '", as the following ' . 'dependent terms must first be imported: ' . print_r(array_keys($deps['dbxref']), TRUE) . '\n', 'error');
- }
- return;
- }
-
-
-
-
-
-
- $vocabs = array (
- 'db' => array (),
- 'cv' => array (),
- 'this' => array ()
- );
-
-
- $owl = new XMLReader();
- if (!$owl->open($filename)) {
- print "ERROR opening OWL file: '$filename'\n";
- exit();
- }
- $rdf = new OWLStanza($owl, FALSE);
- $ontology = new OWLStanza($owl);
-
-
- $url = '';
- $homepage = $ontology->getChild('foaf:homepage');
- if ($homepage) {
- $url = $homepage->getValue();
- }
- $db = array (
- 'url' => $url,
- 'name' => $db_name
- );
-
- $db = chado_insert_db($db);
-
-
-
- $cv_description = '';
- $description = $ontology->getChild('dc:description');
- if ($description) {
- $cv_description = $description->getValue();
- }
-
-
- $cv_name == $namespace = $ontology->getChild('oboInOwl:default-namespace');
- if ($namespace) {
- $cv_name = $namespace->getValue();
- }
- $title = $ontology->getChild('dc:title');
- if ($title) {
- $cv_name = preg_replace("/[^\w]/", "_", strtolower($title->getValue()));
- }
-
- $cv = chado_insert_cv($cv_name, $cv_description);
-
- $vocabs[$db_name]['cv'] = $namespace_cv;
- $vocabs[$db_name]['db'] = $db;
- $vocabs['this'] = $db_name;
-
-
- $stanza = new OWLStanza($owl);
- while (!$stanza->isFinished()) {
-
- switch ($stanza->getTagName()) {
- case 'owl:AnnotationProperty':
-
- break;
- case 'rdf:Description':
-
- break;
- case 'owl:ObjectProperty':
-
- break;
- case 'owl:Class':
- tripal_owl_handle_class($stanza, $vocabs);
- break;
- case 'owl:Axiom':
- break;
- case 'owl:Restriction':
- break;
- default:
- throw new Exception("Unhandled stanza: " . $stanza->getTagName());
- exit();
- break;
- }
-
- $stanza = new OWLStanza($owl);
- }
-
- $owl->close();
- }
- function tripal_owl_check_class_depedencies(OWLStanza $stanza, $vocab_db_name, &$deps) {
-
- $db_name = '';
- $accession = '';
- $db = null;
-
- $about = $stanza->getAttribute('rdf:about');
- if (!$about) {
-
-
- return;
- }
-
-
- if (preg_match('/.*\/(.+)_(.+)/', $about, $matches)) {
- $db_name = strtoupper($matches[1]);
- $accession = $matches[2];
- }
- else {
- throw new Exception("owl:Class stanza 'rdf:about' attribute is not formated as expected: '$about'. " . "This is necessary to determine the term's accession: \n\n" . $stanza->getXML());
- }
-
-
- if ($db_name !== $vocab_db_name) {
- return;
- }
-
-
-
- $db = chado_select_record('db', array (
- 'db_id'
- ), array (
- 'name' => $db_name
- ));
- if ($db === FALSE) {
- throw new Exception("Failed to execute query to find vocabulary in chado.db table\n\n" . $stanza->getXML());
- }
- else if (count($db) == 0) {
- $deps['db'][$db_name] = TRUE;
-
-
- $imported_from = $stanza->getChild('obo:IAO_0000412');
- if ($imported_from == NULL) {
- return;
- }
- $url = $imported_from->getAttribute('rdf:resource');
- if ($url) {
- $deps['db'][$db_name] = $url;
- }
- return;
- }
-
-
-
- $values = array (
- 'db_id' => $db[0]->db_id,
- 'accession' => $accession
- );
- $dbxref = chado_select_record('dbxref', array (
- 'dbxref_id',
- 'db_id'
- ), $values);
- if ($dbxref === FALSE) {
- throw new Exception("Failed to execute query to find vocabulary term in chado.dbxref table\n\n" . $stanza->getXML());
- }
- elseif (count($accession) == 0) {
- $deps['dbxref'][$db_name . ':' . $accesson] = TRUE;
- }
- return;
- }
- function tripal_owl_handle_object_property($stanza, $vocabs) {
- }
- function tripal_owl_handle_annotation_property($stanza, $vocabs) {
- }
- function tripal_owl_handle_description($stanza, $vocabs) {
- }
- function tripal_owl_handle_class(OWLStanza $stanza, $vocabs) {
-
- $db_name = $vocabs['this'];
- $accession = '';
- $is_a = '';
- $namespace_cv = $vocabs[$db_name]['cv'];
- $db = $vocabs[$db_name]['db'];
-
-
- $obo_id = $stanza->getChild('oboInOwl:id');
- if ($obo_id) {
- if (preg_match('/.*>(.+):(.+)<.*/', $about, $matches)) {
- $db_name = strtoupper($matches[1]);
- $accession = $matches[2];
- }
- else {
- $about = $stanza->getAttribute('rdf:about');
-
-
- if (preg_match('/.*\/(.+)_(.+)/', $about, $matches)) {
- $db_name = strtoupper($matches[1]);
- $accession = $matches[2];
- }
- else {
- throw new Exception("owl:Class stanza 'rdf:about' attribute is not formated as expected: '$about'. " . "This is necessary to determine the term's accession: \n\n" . $stanza->getXML());
- }
- }
- }
-
-
- if ($db_name == $vocabs['this']) {
- return;
- }
-
- $values = array (
- 'db_id' => $db->db_id,
- 'accession' => $accession
- );
- $dbxref = chado_insert_dbxref($values);
- $cvterm_name = $stanza->getChild('rdfs:label');
- if ($cvterm_name) {
- $cvterm_name = $stanza->getValue();
- }
- $definition = $stanza->getChild('obo:IAO_0000115');
- if ($definition) {
- $definition = $stanza->getValue();
- }
- $term = array (
- 'id' => $db->name . ':' . $dbxref->accession,
- 'name' => $db->name,
- 'cv_name' => $stanza->getValue(),
- 'definition' => $stanza->getValue(),
- );
- $options = array ();
- if ($vocabs['this'] != $db->name) {
- $options['update_existing'] = FALSE;
- }
- $cvterm = chado_insert_cvterm($term, $options);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
|