array($property), 'id' => array("internal:$property"), 'definition' => array(''), 'is_obsolete' => array(0), ); $cvproptype = tripal_cv_obo_add_cv_term($term,$cv,0,0); if(!$cvproptype){ return 0; } } // remove any properties that currently exist for this term. We'll reset them if($rank == 0){ $sql = "DELETE FROM {cvtermprop} WHERE cvterm_id = %d"; db_query($sql,$cvterm->cvterm_id); } // now add the property $sql = "INSERT INTO {cvtermprop} (cvterm_id,type_id,value,rank) ". "VALUES (%d, %d, '%s',%d)"; if(!db_query($sql,$cvterm->cvterm_id,$cvproptype->cvterm_id,$value,$rank)){ print "Could not add property $property for term\n"; return 0; } return 1; } /************************************************************************* * */ function tripal_cv_obo_add_relationship($cvterm,$cv,$obo,$rel,$objname){ // make sure the relationship cvterm exists $sql = " SELECT * FROM {cvterm} CVT INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id WHERE CVT.name = '%s' and CV.name = '%s' "; $cvisa = db_fetch_object(db_query($sql,$rel,$cv->name)); if(!$cvisa){ $term = array( 'name' => array($rel), 'id' => array($rel), 'definition' => array(''), 'is_obsolete' => array(0), ); if(!tripal_cv_obo_add_cv_term($term,$cv,1,1)){ print "Cannot find or insert the relationship term: $rel.\n"; return 0; } $cvisa = db_fetch_object(db_query($sql,$rel,$cv->name)); } // get the object term $objterm = tripal_cv_obo_get_term($obo,$objname); if(!$objterm) { print "Could not find object term $objname\n"; return 0; } $objcvterm = tripal_cv_obo_add_cv_term($objterm,$cv,1,1); if(!$objcvterm){ return 0; } // check to see if the cvterm_relationship already exists, if not add it $cvrsql = "SELECT * FROM {cvterm_relationship} WHERE type_id = %d and subject_id = %d and object_id = %d"; if(!db_fetch_object(db_query($cvrsql,$cvisa->cvterm_id,$cvterm->cvterm_id,$objcvterm->cvterm_id))){ $sql = "INSERT INTO {cvterm_relationship} ". "(type_id,subject_id,object_id) VALUES (%d,%d,%d)"; if(!db_query($sql,$cvisa->cvterm_id,$cvterm->cvterm_id,$objcvterm->cvterm_id)){ print "Cannot add $rel relationship"; return 0; } // print " $rel $objname\n"; } return 1; } /************************************************************************* * */ function tripal_cv_obo_get_term($obo,$id){ foreach ($obo as $type){ foreach ($type as $term){ $accession = $term['id'][0]; if(strcmp($accession,$id)==0){ return $term; } } } return; } /************************************************************************* * */ function tripal_cv_obo_add_synonyms($term,$cvterm){ // make sure we have a 'synonym_type' vocabulary $sql = "SELECT * FROM {cv} WHERE name='synonym_type'"; $syncv = db_fetch_object(db_query($sql)); if(!$syncv){ $sql = "INSERT INTO {cv} (name,definition) VALUES ('synonym_type','')"; if(!db_query($sql)){ print "Failed to add the synonyms type vocabulary"; return 0; } } // now add the synonyms if(isset($term['synonym'])){ foreach($term['synonym'] as $synonym){ // separate out the synonym definition and the synonym type $def = preg_replace('/^\s*"(.*)"\s*.*$/','\1',$synonym); $type = strtolower(preg_replace('/^.*"\s+(.*?)\s+.*$/','\1',$synonym)); // make sure the synonym type exists in the 'synonym_type' vocabulary $cvtsql = " SELECT * FROM {cvterm} CVT INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id WHERE CVT.name = '%s' and CV.name = '%s' "; $syntype = db_fetch_object(db_query($cvtsql,$type,'synonym_type')); if(!$syntype){ // build a 'term' object so we can add the missing term $term = array( 'name' => array($type), 'id' => array("internal:$type"), 'definition' => array(''), 'is_obsolete' => array(0), ); if(!tripal_cv_obo_add_cv_term($term,$syncv,0,1)){ return 0; } $syntype = db_fetch_object(db_query($cvtsql,$type,'synonym_type')); } // make sure the synonym doesn't already exists $sql = " SELECT * FROM {cvtermsynonym} WHERE cvterm_id = %d and synonym = '%s' and type_id = %d "; $syn = db_fetch_object(db_query($sql,$cvterm->cvterm_id,$def,$syntype->cvterm_id)); if(!$syn){ $sql = "INSERT INTO {cvtermsynonym} (cvterm_id,synonym,type_id) VALUES(%d,'%s',%d)"; if(!db_query($sql,$cvterm->cvterm_id,$def,$syntype->cvterm_id)){ print "Failed to insert the synonym for term: $name ($def)\n"; return 0; } } } } return 1; } /************************************************************************* * */ function tripal_cv_obo_add_cv_term($term,$cv,$is_relationship = 0,$update = 1){ // get the term properties $id = $term['id'][0]; $name = $term['name'][0]; $definition = preg_replace('/^\"(.*)\"/','\1',$term['def'][0]); $is_obsolete = 0; if(isset($term['is_obsolete'][0]) and strcmp($term['is_obsolete'][0],'true')==0){ $is_obsolete = 1; } // get the accession and the database from the cvterm if(preg_match('/^.+?:.*$/',$id)){ $accession = preg_replace('/^.+?:(.*)$/','\1',$id); $dbname = preg_replace('/^(.+?):.*$/','\1',$id); } else if($is_relationship) { $accession = $id; $dbname = 'OBO_REL'; } // check to see if the database exists $db = tripal_cv_obo_add_db($dbname); if(!$db){ print "Cannot find database '$dbname' in Chado.\n"; return 0; } // check to see if the cvterm already exists $cvtermsql = "SELECT * from {cvterm} WHERE name = '%s' and cv_id = %d"; $cvterm = db_fetch_object(db_query($cvtermsql,$name,$cv->cv_id)); // if the cvterm doesn't exist then add it otherwise just update it if(!$cvterm){ // check to see if the dbxref exists if not, add it $dbxref = tripal_cv_obo_add_dbxref($db->db_id,$accession); if(!$dbxref){ print "Failed to find or insert the dbxref record for cvterm, $name (id: $accession), for database $dbname\n"; return 0; } // now add the cvterm $sql = " INSERT INTO {cvterm} (cv_id, name, definition, dbxref_id, is_obsolete, is_relationshiptype) VALUES (%d,'%s','%s',%d,%d,%d) "; if(!db_query($sql,$cv->cv_id,$name,$definition, $dbxref->dbxref_id,$is_obsolete,$is_relationship)){ print "Failed to insert the term: $id\n"; return 0; } print "Added CV term: $id\n"; $cvterm = db_fetch_object(db_query($cvtermsql,$name,$cv->cv_id)); } elseif($update) { // update the cvterm $sql = " UPDATE {cvterm} SET name='%s', definition='%s', is_obsolete = %d, is_relationshiptype = %d WHERE cvterm_id = %d "; if(!db_query($sql,$term['name'][0],$definition, $is_obsolete,$is_relationship,$cvterm->cvterm_id)){ print "Failed to update the term: $name\n"; return 0; } print "Updated CV term: $id\n"; $cvterm = db_fetch_object(db_query($cvtermsql,$name,$cv->cv_id)); } // return the cvterm return $cvterm; } /************************************************************************* * */ function tripal_cv_obo_add_cvterm_dbxref($cvterm,$xref){ $accession = preg_replace('/^.+?:(.*)$/','\1',$xref); $dbname = preg_replace('/^(.+?):.*$/','\1',$xref); // if the xref is a database link, handle that specially if(strcmp($dbname,'http')==0){ $accession = $xref; $dbname = 'URL'; } // check to see if the database exists $db = tripal_cv_obo_add_db($dbname); if(!$db){ print "Cannot find database '$dbname' in Chado."; return 0; } // now add the dbxref $dbxref = tripal_cv_obo_add_dbxref($db->db_id,$accession); if(!$dbxref){ return 0;} // finally add the cvterm_dbxref but first check to make sure it exists $sql = "SELECT * from {cvterm_dbxref} WHERE cvterm_id = %d and dbxref_id = %d"; if(!db_fetch_object(db_query($sql,$cvterm->cvterm_id,$dbxref->dbxref_id))){ $sql = "INSERT INTO {cvterm_dbxref} (cvterm_id,dbxref_id)". "VALUES (%d,%d)"; if(!db_query($sql,$cvterm->cvterm_id,$dbxref->dbxref_id)){ print "Cannot add cvterm_dbxref: $accession\n"; return 0; } } return 1; } /************************************************************************* * */ function tripal_cv_obo_add_dbxref($db_id,$accession,$version='',$description=''){ // check to see if the dbxref exists if not, add it $dbxsql = "SELECT dbxref_id FROM {dbxref} WHERE db_id = %d and accession = '%s'"; $dbxref = db_fetch_object(db_query($dbxsql,$db_id,$accession)); if(!$dbxref){ $sql = " INSERT INTO {dbxref} (db_id, accession, version, description) VALUES (%d,'%s','%s','%s') "; if(!db_query($sql,$db_id,$accession,$version,$description)){ print "Failed to insert the dbxref record $accession\n"; return 0; } $dbxref = db_fetch_object(db_query($dbxsql,$db_id,$accession)); } return $dbxref; } /************************************************************************* * */ function tripal_cv_obo_parse($obo_file,&$obo,&$header){ $i = 0; $in_header = 1; $stanza = array(); // iterate through the lines in the OBO file and parse the stanzas $fh = fopen($obo_file,'r'); while($line = fgets($fh)) { $i++; // remove newlines $line = rtrim($line); // skip empty lines if(strcmp($line,'')==0) { continue; } //remove comments from end of lines $line = preg_replace('/^(.*?)\!.*$/','\1',$line); // TODO: if the explamation is escaped if(preg_match('/^\s*\[/',$line)){ // at the first stanza we're out of header $in_header = 0; // load the stanza we just finished reading if(sizeof($stanza) > 0){ if(!isset($obo[$type])){ $obo[$type] = array(); } if(!isset($obo[$type][$stanza['id'][0]])){ $obo[$type][$stanza['id'][0]] = $stanza; } else { array_merge($obo[$type][$stanza['id'][0]],$stanza); } } // get the stanza type: Term, Typedef or Instance $type = preg_replace('/^\s*\[\s*(.+?)\s*\]\s*$/','\1',$line); // start fresh with a new array $stanza = array(); continue; } // break apart the line into the tag and value but ignore any escaped colons preg_replace("/\\:/","|-|-|",$line); // temporarily replace escaped colons $pair = explode(":",$line,2); $tag = $pair[0]; $value = ltrim(rtrim($pair[1]));// remove surrounding spaces $tag = preg_replace("/\|-\|-\|/","\:",$tag); // return the escaped colon $value = preg_replace("/\|-\|-\|/","\:",$value); if($in_header){ if(!isset($header[$tag])){ $header[$tag] = array(); } $header[$tag][] = $value; } else { if(!isset($stanza[$tag])){ $stanza[$tag] = array(); } $stanza[$tag][] = $value; } } // now add the last term in the file if(sizeof($stanza) > 0){ if(!isset($obo[$type])){ $obo[$type] = array(); } if(!isset($obo[$type][$stanza['id'][0]])){ $obo[$type][$stanza['id'][0]] = $stanza; } else { array_merge($obo[$type][$stanza['id'][0]],$stanza); } } } /************************************************************************* * */ function tripal_cv_obo_loader_done (){ // return the search path to normal db_query("set search_path to public"); return ''; }