Tripal v1.0 (6.x-1.0)
tripal_cv.api.inc
Go to the documentation of this file.
00001 <?php
00002 
00053 function tripal_cv_get_cv($select_values) {
00054 
00055   $columns = array(
00056     'cv_id',
00057     'name',
00058     'definition',
00059   );
00060   $results = tripal_core_chado_select('cv', $columns, $select_values);
00061   if (sizeof($results) == 1) {
00062     return $results[0];
00063   }
00064   elseif (empty($results)) {
00065     watchdog('tripal_cv',
00066       'tripal_cv_get_cv: No cv matches criteria values:%values',
00067       array('%values' => print_r($select_values, TRUE)),
00068       WATCHDOG_WARNING
00069     );
00070     return FALSE;
00071   }
00072   else {
00073     watchdog('tripal_cv',
00074       'tripal_cv_get_cv: 2+ cvs match criteria values:%values',
00075       array('%values' => print_r($select_values, TRUE)),
00076       WATCHDOG_WARNING
00077     );
00078   }
00079 
00080 }
00081 
00082 // Purpose: To retrieve a chado cv object
00083 // @param $where_options
00084 //   @code
00085 //        array(
00086 //                          <column_name> => array(
00087 //                            'type' => <type of column: INT/STRING>,
00088 //                            'value' => <the vlaue you want to filter on>,
00089 //                            'exact' => <if TRUE use =; if FALSE use ~>,
00090 //                          )
00091 //        )
00092 // @endcode
00093 //
00094 // @return
00095 //   Chado cv object with all fields from the chado cv table
00096 //
00097 // @ingroup tripal_cv_api
00098 //
00099 //function tripal_cv_get_cv ($where_options)
00100 
00111 function tripal_cv_get_cv_by_name($name) {
00112 
00113   $r = tripal_core_chado_select('cv', array('*'), array('name' => $name));
00114 
00115   return $r[0];
00116 }
00117 
00132 function tripal_cv_get_cv_by_id($cv_id) {
00133 
00134   $r = tripal_core_chado_select('cv', array('*'), array('cv_id' => $cv_id));
00135 
00136   return $r;
00137 }
00152 function tripal_cv_get_cv_id($cv_name) {
00153 
00154   $sql = "
00155     SELECT cv_id FROM {cv} WHERE name = '%s'
00156   ";
00157   $cv = db_fetch_object(chado_query($sql, $cv_name));
00158 
00159   return $cv->cv_id;
00160 }
00161 
00173 function tripal_cv_get_cv_options() {
00174 
00175   $results = tripal_core_chado_select('cv', array('cv_id', 'name'), array());
00176 
00177   $options = array();
00178   foreach ($results as $r) {
00179     $options[$r->cv_id] = $r->name;
00180   }
00181 
00182   return $options;
00183 
00184 }
00185 
00201 function tripal_cv_get_cvterm_by_name($name, $cv_id = 0, $cv_name = 'tripal') {
00202 
00203   if ($cv_id) {
00204     $values = array(
00205        'name' => $name,
00206        'cv_id' => $cv_id,
00207     );
00208     $r = tripal_core_chado_select('cvterm', array('*'), $values);
00209   }
00210   elseif ($cv_name) {
00211     $values = array(
00212       'name' => $name,
00213       'cv_id' => array(
00214         'name' => $cv_name,
00215       ),
00216     );
00217     $r = tripal_core_chado_select('cvterm', array('*'), $values);
00218   }
00219   else {
00220     $values = array(
00221       'name' => $name,
00222     );
00223     $r = tripal_core_chado_select('cvterm', array('*'), $values);
00224   }
00225 
00226   if (!$r) {
00227     return FALSE;
00228   }
00229   if (count($r) > 0) {
00230     return FALSE;
00231   }
00232   return $r[0];
00233 }
00234 
00248 function tripal_cv_get_cvterm_options($cv_id = 0) {
00249 
00250   if ($cv_id > 0) {
00251     $results = tripal_core_chado_select('cvterm', array('cvterm_id', 'name'), array('cv_id' => $cv_id));
00252   }
00253   else {
00254     $results = tripal_core_chado_select('cvterm', array('cvterm_id', 'name'), array());
00255   }
00256 
00257   $options = array();
00258   foreach ($results as $r) {
00259     $options[$r->cvterm_id] = $r->name;
00260   }
00261 
00262   return $options;
00263 
00264 }
00265 
00280 function tripal_cv_update_cvtermpath($cvid, $job_id = NULL) {
00281   // TODO: need better error checking in this function
00282 
00283   // first get the controlled vocabulary name:
00284   $cv = db_fetch_object(chado_query("SELECT * FROM {cv} WHERE cv_id = %d", $cvid));
00285   print "\nUpdating cvtermpath for $cv->name...\n";
00286 
00287   // now fill the cvtermpath table
00288   // @coder-ignore: using a function rather then tablename therefore table prefixing doesn't apply
00289   $sql = "SELECT * FROM fill_cvtermpath('%s')";
00290   $success = chado_query($sql, $cv->name); 
00291 
00292   return TRUE;
00293 }
00294 
00309 function tripal_cv_add_cv($name, $definition) {
00310   
00311   // insert/update values
00312   $ins_values = array(
00313     'name'       => $name,
00314     'definition' => $definition
00315   );
00316   
00317   // see if the CV (default-namespace) exists already in the database
00318   $sel_values = array('name' => $name);
00319   $sel_options = array('statement_name' => 'sel_cv_na');
00320   $results = tripal_core_chado_select('cv', array('*'), $sel_values, $sel_options);
00321 
00322   // if it does not exists then add it
00323   if (count($results) == 0) {    
00324     $ins_options = array('statement_name' => 'ins_cv_nade');
00325     $success = tripal_core_chado_insert('cv', $ins_values, $ins_options);
00326     if (!$success) {
00327       watchdog('tripal_cv', "Failed to create the CV record", NULL, WATCHDOG_WARNING);
00328       return FALSE;
00329     }
00330     $results = tripal_core_chado_select('cv', array('*'), $sel_values, $sel_options);
00331   }
00332   // if it already exists then do an update
00333   else {
00334     $upd_options = array('statement_name' => 'upd_cv_nade');
00335     $success = tripal_core_chado_update('cv', $sel_values, $ins_values, $upd_options);
00336     if (!$success) {
00337       watchdog('tripal_cv', "Failed to update the CV record", NULL, WATCHDOG_WARNING);
00338       return FALSE;
00339     }
00340     $results = tripal_core_chado_select('cv', array('*'), $sel_values, $sel_options);
00341   }
00342 
00343   // return the cv object
00344   return $results[0];
00345 }
00346 
00389 function tripal_cv_add_cvterm($term, $defaultcv = '_global', $is_relationship = 0, 
00390   $update = 1, $dbname = 'internal') {
00391       
00392   // get the term properties
00393   $id = $term['id'];
00394   $name = '';
00395   $cvname = '';
00396   $definition = '';
00397   $is_obsolete = 0;
00398   $accession = '';
00399   
00400   if (array_key_exists('name', $term)) {
00401     $name = $term['name'];  
00402   }
00403   else {
00404     $name = $id;  
00405   }
00406 
00407   if (array_key_exists('namespace', $term)) {
00408     $cvname = $term['namespace'];
00409   } 
00410   else {
00411     $cvname = $defaultcv;
00412   }
00413   if (array_key_exists('def', $term)) {
00414     $definition = preg_replace('/^\"(.*)\"/', '\1', $term['def']);
00415   }
00416   else {
00417     $definition = '';
00418   }
00419   if (array_key_exists('is_obsolete', $term)) {
00420     $is_obsolete = $term['is_obsolete'];
00421     if (strcmp($is_obsolete, 'true') == 0) {
00422       $is_obsolete = 1;
00423     }
00424   }  
00425   if (!$name and !$id) {
00426     watchdog('tripal_cv', "Cannot find cvterm without 'id' or 'name'", NULL, WATCHDOG_WARNING);
00427     return 0;
00428   }
00429   if (!$id) {
00430     $id = $name;
00431   }
00432   
00433   // get the accession and the database from the cvterm id
00434   if ($dbname) {
00435     $accession = $id;
00436   }
00437   
00438   if (preg_match('/^.+?:.*$/', $id)) {
00439     $accession = preg_replace('/^.+?:(.*)$/', '\1', $id);
00440     $dbname = preg_replace('/^(.+?):.*$/', '\1', $id);
00441   }
00442   
00443   // check that we have a database name, give a different message if it's a relationship
00444   if ($is_relationship and !$dbname) {
00445     watchdog('tripal_cv', "A database name is not provided for this relationship term: $id", NULL, WATCHDOG_WARNING);
00446     return 0; 
00447   }
00448   if (!$is_relationship and !$dbname) {
00449     watchdog('tripal_cv', "A database identifier is missing from the term: $id", NULL, WATCHDOG_WARNING);
00450     return 0;
00451   }
00452   
00453   // make sure the CV name exists
00454   $cv = tripal_cv_add_cv($cvname, '');
00455   if (!$cv) {
00456     watchdog('tripal_cv', "Cannot find namespace '$cvname' when adding/updating $id", NULL, WATCHDOG_WARNING);
00457     return 0;
00458   }
00459 
00460   // this SQL statement will be used a lot to find a cvterm so just set it
00461   // here for easy reference below.  Because CV terms can change their names
00462   // but accessions don't change, the following SQL finds cvterms based on
00463   // their accession rather than the name
00464   if (!tripal_core_is_sql_prepared('sel_cvterm_by_accession')) {
00465     $pcvtermsql = "
00466       PREPARE sel_cvterm_by_accession(text, text) AS
00467       SELECT CVT.name, CVT.cvterm_id, CV.cv_id, CV.name as cvname, 
00468         DB.name as dbname, DB.db_id, DBX.accession 
00469       FROM cvterm CVT
00470         INNER JOIN dbxref DBX on CVT.dbxref_id = DBX.dbxref_id
00471         INNER JOIN db DB on DBX.db_id = DB.db_id
00472         INNER JOIN cv CV on CV.cv_id = CVT.cv_id
00473       WHERE DBX.accession = $1 and DB.name = $2";
00474     if (!tripal_core_chado_prepare('sel_cvterm_by_accession', $pcvtermsql, array('text', 'text'))) {
00475       watchdog('tripal_cv', "Cannot prepare statement 'sel_cvterm_by_accession'", NULL, WATCHDOG_WARNING);
00476       return 0; 
00477     }
00478   } 
00479   $cvtermsql = "EXECUTE sel_cvterm_by_accession('%s','%s')";  
00480   
00481   // add the database. The function will just return the DB object if the
00482   // database already exists.
00483   $db = tripal_db_add_db($dbname);
00484   if (!$db) {
00485     watchdog('tripal_cv', "Cannot find database '$dbname' in Chado.", NULL, WATCHDOG_WARNING);
00486     return 0;
00487   }
00488 
00489   // the cvterm table has two unique dependencies. We need to check both.
00490   // first check the (name, cv_id, is_obsolete) constraint
00491   $values = array(
00492     'name' => $name,
00493     'is_obsolete' => $is_obsolete,
00494     'cv_id' => array(
00495       'name' => $cvname,
00496     ),
00497   );
00498   $options = array('statement_name' => 'sel_cvterm_c1');
00499   $result = tripal_core_chado_select('cvterm', array('*'), $values, $options);
00500   
00501   // if the constraint is met then let's check it to see if
00502   // the database name matches the one we have been provided
00503   if (count($result) == 1) {
00504     $cvterm = $result[0];
00505     
00506     // get the dbxref record
00507     $values = array('dbxref_id' => $cvterm->dbxref_id);
00508     $options = array('statement_name' => 'sel_dbxref_id');
00509     $result = tripal_core_chado_select('dbxref', array('*'), $values, $options);
00510     $dbxref = $result[0];
00511     
00512     // get the db
00513     $values = array('db_id' => $dbxref->db_id);
00514     $options = array('statement_name' => 'sel_db_id');
00515     $result = tripal_core_chado_select('db', array('*'), $values, $options);
00516     $db_check = $result[0];
00517     
00518     // the database name for this existing term does not match that of the 
00519     // one provided to this function.  The CV name matches otherwise we
00520     // wouldn't have made it this far. So, let's swap the database for
00521     // this term
00522     if ($db_check->name != $db->name) {
00523 
00524       // look to see if the correct dbxref record already exists for this database
00525       $values = array(
00526         'db_id' => $db->db_id,
00527         'accession' => $accession,       
00528       );
00529       $options = array('statement_name' => 'sel_dbxref_idac');
00530       $result = tripal_core_chado_select('dbxref', array('*'), $values, $options);
00531 
00532       // if we already have a good dbxref then we want to update our cvterm 
00533       // to use this dbxref
00534       if (count($result) > 0) {
00535         $dbxref = $result[0];
00536         $match = array('cvterm_id' => $cvterm->cvterm_id);
00537         $values = array('dbxref_id' => $dbxref->dbxref_id);
00538         $options = array('statement_name' => 'upd_cvterm_db');
00539         $success = tripal_core_chado_update('cvterm', $match, $values, $options); 
00540         if (!$success) {
00541           watchdog('tripal_cv', "Failed to correct the dbxref id for the cvterm " .
00542             "'$name' (id: $accession), for database $dbname", NULL, WATCHDOG_WARNING);
00543           return 0;
00544         }
00545       }
00546       // if we don't have the record then we want to delete our cvterm and let the code
00547       // below recreate it with the correct info 
00548       else {          
00549         $match = array('cvterm_id' => $cvterm->cvterm_id);
00550         $options = array('statement_name' => 'del_cvterm_cv');
00551         tripal_core_chado_delete('cvterm', $match, $options);
00552       }      
00553     }
00554     
00555     // check that the accession matches.  Sometimes an OBO can define the same term 
00556     // multiple times but with different accessions.  If this is the case we
00557     // can't do an insert or it will violate the constraint in the cvterm table.
00558     // so we'll need to add the record to the cvterm_dbxref table instead
00559     if ($dbxref->accession != $accession) {
00560       
00561       // get/add the dbxref fort his term
00562       $dbxref_new =  tripal_db_add_dbxref($db->db_id, $accession);
00563       if (!$dbxref_new) {
00564         watchdog('tripal_cv', "Failed to find or insert the dbxref record for cvterm, " .
00565           "$name (id: $accession), for database $dbname", NULL, WATCHDOG_WARNING);
00566         return 0;
00567       }
00568       
00569       // check to see if the cvterm_dbxref record already exists
00570       $values = array(
00571         'cvterm_id' => $cvterm->cvterm_id,
00572         'dbxref_id' => $dbxref_new->dbxref_id,
00573         'is_for_definition' => 1,
00574       );
00575       $options = array('statement_name' => 'sel_cvtermdbxref_cvdbis');      
00576       $result = tripal_core_chado_select('cvterm_dbxref', array('*'), $values, $options);
00577       
00578       // if the cvterm_dbxref record does not exists then add it 
00579       if (count($result)==0) {
00580         $options = array(
00581           'statement_name' => 'ins_cvtermdbxref_cvdbis',
00582           'return_record' => FALSE,
00583         );
00584         $success = tripal_core_chado_insert('cvterm_dbxref', $values, $options);
00585         if (!$success) {
00586           watchdog('tripal_cv', "Failed to find or insert the cvterm_dbxref record for a " . 
00587             "duplicated cvterm:  $name (id: $accession), for database $dbname", NULL, WATCHDOG_WARNING);
00588           return 0;
00589         }
00590       }        
00591       // get the original cvterm with the same name and return that.
00592       $cvterm = db_fetch_object(chado_query($cvtermsql, $dbxref->accession, $dbname));
00593       return $cvterm;
00594     }
00595     
00596     // continue on, we've fixed the record if the db_id did not match, 
00597     // we can now perform and updated if we need to.
00598   }
00599   
00600   // get the CVterm record
00601   $cvterm = db_fetch_object(chado_query($cvtermsql, $accession, $dbname)); 
00602   //print "$pcvtermsql\n$cvtermsql\n$accession, $dbname\n";
00603   //print "CVTERM:\n";
00604   //print_r($cvterm);
00605   if (!$cvterm) {
00606 
00607     // check to see if the dbxref exists if not, add it
00608     $dbxref =  tripal_db_add_dbxref($db->db_id, $accession);
00609     if (!$dbxref) {
00610       watchdog('tripal_cv', "Failed to find or insert the dbxref record for cvterm, " .
00611         "$name (id: $accession), for database $dbname", NULL, WATCHDOG_WARNING);
00612       return 0;
00613     }
00614     
00615     // check to see if the dbxref already has an entry in the cvterm table
00616     // this is the second constraint in the cvterm table
00617     $values = array('dbxref_id' => $dbxref->dbxref_id);
00618     $options = array('statement_name' => 'sel_cvterm_db');
00619     $check = tripal_core_chado_select('cvterm', array('cvterm_id'), $values, $options);
00620     if (count($check) == 0) {      
00621       // now add the cvterm
00622       $ins_values = array(
00623         'cv_id'                => $cv->cv_id,
00624         'name'                 => $name,
00625         'definition'           => $definition,
00626         'dbxref_id'            => $dbxref->dbxref_id,
00627         'is_obsolete'          => $is_obsolete,
00628         'is_relationshiptype'  => $is_relationship, 
00629       );
00630       $ins_options = array('statement_name' => 'ins_cvterm_all');
00631       $success = tripal_core_chado_insert('cvterm', $ins_values, $ins_options);
00632       if (!$success) {
00633         if (!$is_relationship) {
00634           watchdog('tripal_cv', "Failed to insert the term: $name ($dbname)", NULL, WATCHDOG_WARNING);
00635           return 0;
00636         }
00637         else {
00638           watchdog('tripal_cv', "Failed to insert the relationship term: $name (cv: " . $cvname . " db: $dbname)", NULL, WATCHDOG_WARNING);
00639           return 0;
00640         }
00641       }
00642     }
00643     // this dbxref already exists in the cvterm table 
00644     else {
00645       watchdog('tripal_cv', "The dbxref already exists for another cvterm record: $name (cv: " . $cvname . " db: $dbname)", NULL, WATCHDOG_WARNING);
00646       return 0;
00647     }    
00648     $cvterm = db_fetch_object(chado_query($cvtermsql, $accession, $dbname));
00649   }
00650   // upate the cvterm
00651   elseif ($update) { 
00652     $match = array('cvterm_id' => $cvterm->cvterm_id);
00653     $upd_values = array(
00654       'name'                => $name,
00655       'definition'          => $definition,
00656       'is_obsolete'         => $is_obsolete,
00657       'is_relationshiptype' => $is_relationship, 
00658     );
00659     $upd_options = array('statement_name' => 'upd_cvterm_nadeisis');
00660     $success = tripal_core_chado_update('cvterm', $match, $upd_values, $upd_options);    
00661     if (!$success) {
00662       watchdog('tripal_cv', "Failed to update the term: $name", NULL, WATCHDOG_WARNING);
00663       return 0;
00664     }
00665     $cvterm = db_fetch_object(chado_query($cvtermsql, $accession, $dbname));
00666   } 
00667   else {
00668      // do nothing, we have the cvterm but we don't want to update
00669   }
00670   // return the cvterm
00671   return $cvterm;
00672 }
00673 
00674 
00681 function tripal_cv_get_custom_tables($table = NULL) {
00682 
00683  if (!$table or strcmp($table, 'tripal_obo_temp')==0) {
00684     $schema['tripal_obo_temp'] = array(
00685       'table' => 'tripal_obo_temp',
00686       'fields' => array(
00687         'id' => array(
00688           'type' => 'varchar',
00689           'length' => '255',
00690           'not null' => TRUE,
00691         ),
00692         'stanza' => array(
00693           'type' => 'text',
00694           'not null' => TRUE,
00695         ),
00696         'type' => array(
00697           'type' => 'varchar',
00698           'length' => '50',
00699           'not null' => TRUE,
00700         ),
00701       ),
00702       'indexes' => array(
00703         'tripal_obo_temp_idx0' => array('id'),
00704         'tripal_obo_temp_idx0' => array('type'),
00705       ),
00706       'unique keys' => array(
00707         'tripal_obo_temp_uq0' => array('id'),
00708       ),
00709     );
00710   }
00711   return $schema;
00712 }
 All Classes Files Functions Variables