print_r($identifiers, TRUE) ) ); } elseif (empty($identifiers)) { tripal_report_error( 'tripal_chado_api', TRIPAL_ERROR, "tripal_get_cv: You did not pass in anything to identify the cv you want. The identifier is expected to be an array with the key matching a column name in the cv table (ie: cv_id or name). You passed in %identifier.", array( '%identifier'=> print_r($identifiers, TRUE) ) ); } // Try to get the cv $cv = chado_generate_var( 'cv', $identifiers, $options ); // Ensure the cv is singular. If it's an array then it is not singular if (is_array($cv)) { tripal_report_error( 'tripal_chado_api', TRIPAL_ERROR, "tripal_get_cv: The identifiers you passed in were not unique. You passed in %identifier.", array( '%identifier'=> print_r($identifiers, TRUE) ) ); } // Report an error if $cv is FALSE since then chado_generate_var has failed elseif ($cv === FALSE) { tripal_report_error( 'tripal_chado_api', TRIPAL_ERROR, "tripal_get_cv: chado_generate_var() failed to return a cv based on the identifiers you passed in. You should check that your identifiers are correct, as well as, look for a chado_generate_var error for additional clues. You passed in %identifier.", array( '%identifier'=> print_r($identifiers, TRUE) ) ); } // Else, as far we know, everything is fine so give them their cv :) else { return $cv; } } /** * Create an options array to be used in a form element which provides a * list of all chado cvs * * @return * An array(cv_id => name) for each cv in the chado cv table * * @ingroup tripal_chado_api */ function tripal_get_cv_select_options() { $results = chado_select_record('cv', array('cv_id', 'name'), array(), array('order_by' => array('name' => 'ASC'))); $options = array(); $options[] = 'Select a Vocabulary'; foreach ($results as $r) { $options[$r->cv_id] = $r->name; } return $options; } /** * Retrieves a chado controlled vocabulary term variable * * @param $identifier * An array apropriate for use with the chado_generate_var for uniquely * identifying a cvterm record. Alternativley, there are also some specially * handled keys. They are: * - id: an ID for the term of the for [dbname]:[accession], where [dbname] * is the short name of the vocabulary and accession is the unique ID. * - cv_id: an integer indicating the cv_id or an array with 'name' => the * name of the cv. * - synonym: an array with 'name' => the name of the synonym of the cvterm * you want returned; 'cv_id' => the cv_id of the synonym; 'cv_name' => * the name of the cv of the synonym * - property: An array/object describing the property to select records * for. It should at least have either a type_name (if unique across cvs) * or type_id. Other supported keys include: cv_id/cv_name (of the type), * value and rank * @param $options * An array of options. Supported keys include: * - Any keys supported by chado_generate_var(). See that function * definition for additional details. * * NOTE: the $identifier parameter can really be any array similar to $values * passed into chado_select_record(). It should fully specify the cvterm * record to be returned. * * @return * If unique values were passed in as an identifier then an object describing * the cvterm will be returned (will be a chado variable from * chado_generate_var()). Otherwise, FALSE will be returned. * * @ingroup tripal_cv_api */ function tripal_get_cvterm($identifiers, $options = array()) { // Set Defaults if (!isset($options['include_fk'])) { // Tells chado_generate_var to only get the cv $options['include_fk'] = array('cv_id' => TRUE); } // Error Checking of parameters if (!is_array($identifiers)) { tripal_report_error('tripal_cv_api', TRIPAL_ERROR, "tripal_get_cvterm: The identifier passed in is expected to be an array with the key matching a column name in the cvterm table (ie: cvterm_id or name). You passed in %identifier.", array('%identifier'=> print_r($identifiers, TRUE)) ); } elseif (empty($identifiers)) { tripal_report_error('tripal_cv_api', TRIPAL_ERROR, "tripal_get_cvterm: You did not pass in anything to identify the cvterm you want. The identifier is expected to be an array with the key matching a column name in the cvterm table (ie: cvterm_id or name). You passed in %identifier.", array('%identifier'=> print_r($identifiers, TRUE)) ); } // If synonym was passed in, then process this first before calling chado_generate_var() if (isset($identifiers['synonym'])) { $synonym = $identifiers['synonym']['name']; $values = array('synonym' => $synonym); if (isset($identifiers['synonym']['cv_id'])) { $values['cvterm_id'] = array('cv_id' => $identifiers['synonym']['cv_id']); } if (isset($identifiers['synonym']['cv_name'])) { $values['cvterm_id'] = array('cv_id' => array('name' => $identifiers['synonym']['cv_name'])); } $options = array( 'case_insensitive_columns' => array('name') ); $result = chado_select_record('cvtermsynonym', array('cvterm_id'), $values, $options); // if the synonym doens't exist or more than one record is returned then return false if (count($result) == 0) { return FALSE; } if (count($result) > 1) { return FALSE; } $identifiers = array('cvterm_id' => $result[0]->cvterm_id); } // If one of the identifiers is property then use chado_get_record_with_property() if (isset($identifiers['property'])) { $property = $identifiers['property']; unset($identifiers['property']); $cvterm = chado_get_record_with_property( array('table' => 'cvterm', 'base_records' => $identifiers), array('type_name' => $property), $options ); } if (isset($identifiers['id'])) { list($db_name, $accession) = preg_split('/:/', $identifiers['id']); $cvterm = chado_generate_var('cvterm',array( 'dbxref_id' => array( 'db_id' => array( 'name' => $db_name, ), 'accession' => $accession, ) )); } // Else we have a simple case and we can just use chado_generate_var to get the cvterm else { // Try to get the cvterm $cvterm = chado_generate_var('cvterm', $identifiers, $options); } // Ensure the cvterm is singular. If it's an array then it is not singular if (is_array($cvterm)) { tripal_report_error( 'tripal_cv_api', TRIPAL_ERROR, "tripal_get_cvterm: The identifiers you passed in were not unique. You passed in %identifier.", array( '%identifier'=> print_r($identifiers, TRUE) ) ); } // Report an error if $cvterm is FALSE since then chado_generate_var has failed elseif ($cvterm === FALSE) { tripal_report_error( 'tripal_cv_api', TRIPAL_ERROR, "tripal_get_cvterm: chado_generate_var() failed to return a cvterm based on the identifiers you passed in. You should check that your identifiers are correct, as well as, look for a chado_generate_var error for additional clues. You passed in %identifier.", array( '%identifier'=> print_r($identifiers, TRUE) ) ); } // Else, as far we know, everything is fine so give them their cvterm :) else { return $cvterm; } } /** * Create an options array to be used in a form element * which provides a list of all chado cvterms * * @param $cv_id * The chado cv_id; only cvterms with the supplied cv_id will be returned * @param $rel_type * Set to TRUE if the terms returned should only be relationship types in * the vocabulary. This is useful for creating drop-downs of terms * used for relationship linker tables. * * @return * An associative array with the cvterm_id's as keys. The first * element in the array has a key of '0' and a value of 'Select a Type' * * @ingroup tripal_chado_api */ function tripal_get_cvterm_select_options($cv_id, $rel_type = FALSE) { $columns = array('cvterm_id', 'name'); $values = array('cv_id' => $cv_id); if ($rel_type) { $values['is_relationshiptype'] = 1; } $s_options = array('order_by' => array('name' => 'ASC')); $cvterms = chado_select_record('cvterm', $columns, $values, $s_options); $options = array(); $options[0] = 'Select a Type'; foreach ($cvterms as $cvterm) { $options[$cvterm->cvterm_id] = $cvterm->name; } return $options; } /** * Updates the cvtermpath table of Chado for the specified CV. * * @param $cv_id * The chado cv_id; * @param $job_id * This function is intended to be used with the Tripal Jobs API. * When this function is called as a job the $job_id is automatically * passed to this function. * @return * TRUE on success FALSE on failure * * @ingroup tripal_chado_api */ function tripal_update_cvtermpath($cv_id, $job_id = NULL) { // TODO: need better error checking in this function // first get the controlled vocabulary name: $sql = "SELECT * FROM {cv} WHERE cv_id = :cv_id"; $cv = chado_query($sql, array(':cv_id' => $cv_id))->fetchObject(); print "\nUpdating cvtermpath for $cv->name...\n"; // We need to set the chado schema as active because some of the // functions call other functions which would not be in scope. $previous = chado_set_active('chado'); try { $sql = "SELECT * FROM fill_cvtermpath(:name)"; db_query($sql, array(':name' => $cv->name)); chado_set_active($previous); } catch (Exception $e) { chado_set_active($previous); $error = $e->getMessage(); tripal_report_error('tripal_chado', TRIPAL_ERROR, "Could not fill cvtermpath table: @error", array('@error' => $error)); return FALSE; } return TRUE; } /** * * @param unknown $cv_id * @param string $job_id */ function tripal_update_cvtermpath_bak($cv_id, $job_id = NULL){ chado_set_active('chado'); $result = db_query(' SELECT DISTINCT t.* FROM cvterm t LEFT JOIN cvterm_relationship r ON (t.cvterm_id = r.subject_id) INNER JOIN cvterm_relationship r2 ON (t.cvterm_id = r2.object_id) WHERE t.cv_id = :cvid AND r.subject_id is null', array(':cvid' => $cv_id) ); $record = $result->fetchObject(); tripal_update_cvtermpath_root_loop($record->cvterm_id, $record->cv_id); } /** * * @param unknown $rootid * @param unknown $cvid */ function tripal_update_cvtermpath_root_loop($rootid, $cvid){ chado_set_active('chado'); $ttype = db_select('cvterm', 'cv') ->fields('cv', array('cvterm_id')); $db_or = db_or(); $db_or->condition('cv.name', "isa", '=' ); $db_or->condition('cv.name', "is_a", '=' ); $ttype->condition($db_or); $result = $ttype->execute()->fetchObject(); tripal_update_cvtermpath_loop($rootid, $rootid, $cvid, $result->cvterm_id, 0, 0, array()); $cterm = db_query( 'SELECT * FROM cvterm_relationship WHERE object_id = :rootid ', array(':rootid' => $rootid) ); while($cterm_result = $cterm->fetchAssoc()) { tripal_update_cvtermpath_root_loop($cterm_result['subject_id'], $cvid); }; } /** * * @param $origin * @param $child_id * @param $cv_id * @param $type_id * @param $depth * @return multitype: */ function tripal_update_cvtermpath_loop($origin, $child_id, $cv_id, $type_id, $depth, $increment_of_depth, $array_of_children){ // Check to see if a row with these values already exists. chado_set_active('chado'); $count = db_query( 'SELECT * FROM cvtermpath WHERE cv_id = :cvid AND object_id = :origin AND subject_id = :child_id AND pathdistance = :depth ', array(':cvid' => $cv_id, ':origin' => $origin, ':child_id' => $child_id, ':depth' => $depth) ); $count_total = $count->rowCount(); try{ if($count_total == 0) { // If row with values does not already exist write to table. chado_set_active('chado'); $query = db_insert('cvtermpath') ->fields(array( 'object_id' => $origin, 'subject_id' => $child_id, 'cv_id' => $cv_id, 'type_id' => $type_id, 'pathdistance' => $depth, )); $rows = $query->execute(); // Build the ID. $children_id = $origin.'|'.$child_id.'|'.$cv_id.'|'.$type_id; // Now check if the most recent entry already exists in the array. if($increment_of_depth != 0){ // Search the $array_of_children for the new $child_id in the build_id column. foreach ($array_of_children as $key => $val) { if ($val['build_id'] == $children_id) { $possible_loop_start = $val; //watchdog('debug', '
tripal_update_cvtermpath_loop_checker $val: '. print_r($val, TRUE) .''); // If the search returns something check for a possible loop. //if(!empty($possible_loop_starts)){ //foreach($possible_loop_starts as $possible_loop_start){ // Call the loop checker function. $result_of_loop_checker = tripal_update_cvtermpath_loop_checker($origin, $child_id, $cv_id, $type_id, $depth, $increment_of_depth, 0, $possible_loop_start, array(), $depth); //watchdog('debug', '
$result_of_loop_checker $val: '. print_r($result_of_loop_checker, TRUE) .''); //die(); if($result_of_loop_checker == TRUE){ watchdog('debug', 'Loop found exit the loop function'); exit; // } //} } } } } // Then add that new entry to the $array_of_children. $array_of_children[$increment_of_depth] = array('build_id'=>$children_id, 'depth' => $depth); $query = db_select('cvterm_relationship', 'cvtr') ->fields('cvtr') ->condition('cvtr.object_id', $child_id, '=') ->execute(); $cterm = $query->fetchAll(); foreach ($cterm as $item) { $increment_of_depth++; tripal_update_cvtermpath_loop($origin, $item->subject_id, $cv_id, $item->type_id, $depth + 1, $increment_of_depth, $array_of_children); } } } catch(Exception $e){ watchdog_exception('tripal_ds', $e); return FALSE; } } /** * @param $origin * @param $child_id * @param $cv_id * @param $type_id * @param $depth * @param $increment_of_depth * @param $distance_between_parent_child * @param $possible_start_of_loop * @param $array_of_possible_loop * @param $depth_at_start_of_loop * * @return bool */ function tripal_update_cvtermpath_loop_checker($origin, $child_id, $cv_id, $type_id, $depth, $increment_of_depth, $distance_between_parent_child, $possible_start_of_loop, $array_of_possible_loop, $depth_at_start_of_loop){ //watchdog('debug', '
$array_of_possible_loop: '. print_r($array_of_possible_loop, TRUE) .''); //watchdog('debug', '
$array_of_possible_loop: '. print_r($array_of_possible_loop, TRUE) .''); if ($distance_between_parent_child > 0){ // Search the $array_of_children for the new $child_id in the build_id column. //$possible_loop_starts = array_keys(array_column($array_of_possible_loop, // 'build_id'), $possible_start_of_loop); foreach ($array_of_possible_loop as $key => $val) { // watchdog('debug', '
$start_of_loop: '. print_r($possible_start_of_loop['depth'], TRUE) .''); // watchdog('debug', '
$depth_at_start_of_loop: '. print_r($depth_at_start_of_loop, TRUE) .''); if ($val['build_id'] === $possible_start_of_loop['build_id']) { $possible_loop_starts = $val; // watchdog('debug', '
$loop_starts: '. print_r($possible_loop_starts['depth'], TRUE) .''); // If the search returns something check for a possible loop. if (!empty($possible_loop_starts)) { // $array_of_possible_loop now contains the suspected loop. Call // this function again and check the next entry against the array // if the 1st entry is the same, check the next entry, if all items // in the array match, it's a loop, kill it. $result = tripal_update_cvtermpath_loop_checker_traverse($origin, $child_id, $cv_id, $type_id, $depth, $increment_of_depth, $possible_start_of_loop, $array_of_possible_loop, array(), 0); if($result == TRUE){ return TRUE; } } } } } chado_set_active('chado'); $query = db_select('cvterm_relationship', 'cvtr') ->fields('cvtr') ->condition('cvtr.object_id', $child_id, '=') ->execute(); $cterm = $query->fetchAll(); foreach ($cterm as $item){ //watchdog('debug', '
tripal_ds_preprocess_TripalEntity $item ' . print_r($item, TRUE) . ''); $increment_of_depth++; $distance_between_parent_child++; $children_id = $origin .'|' .$item->subject_id .'|' .$cv_id.'|' .$item->type_id; $array_of_possible_loop[$distance_between_parent_child] = array('build_id' => $children_id, 'depth' => $depth); tripal_update_cvtermpath_loop_checker($origin, $item->subject_id, $cv_id, $item->type_id, $depth + 1, $increment_of_depth, $distance_between_parent_child, $possible_start_of_loop, $array_of_possible_loop, $depth_at_start_of_loop); } return FALSE; } function tripal_update_cvtermpath_loop_checker_traverse($origin, $child_id, $cv_id, $type_id, $depth, $increment_of_depth, $possible_start_of_loop, $array_of_possible_loop, $traverse_of_loop, $increment){ if ($increment > 0){ watchdog('debug', '
$traverse_of_loop: '. print_r($traverse_of_loop, TRUE) .''); watchdog('debug', '
$array_of_possible_loop: '. print_r($array_of_possible_loop, TRUE) .''); // If the search returns something compare values. if($array_of_possible_loop === $traverse_of_loop){ watchdog('debug', 'LOOP'); //Report the loop. return TRUE; } } chado_set_active('chado'); $query = db_select('cvterm_relationship', 'cvtr') ->fields('cvtr') ->condition('cvtr.object_id', $child_id, '=') ->execute(); $cterm = $query->fetchAll(); foreach ($cterm as $item){ //watchdog('debug', '
tripal_ds_preprocess_TripalEntity $item ' . print_r($item, TRUE) . ''); $increment_of_depth++; $increment++; $children_id = $origin .'|' .$item->subject_id .'|' .$cv_id.'|' .$item->type_id; $traverse_of_loop[$increment] = array('build_id' => $children_id, 'depth' => $depth - 2); tripal_update_cvtermpath_loop_checker_traverse($origin, $item->subject_id, $cv_id, $item->type_id, $depth + 1, $increment_of_depth, $possible_start_of_loop, $array_of_possible_loop, $traverse_of_loop, $increment); } return FALSE; } /* * * @param $origin * @param $subject_id * @param $cv_id * @param $type_id * @param $depth * @return multitype: function tripal_update_cvtermpath_loop($origin, $child_id, $cv_id, $type_id, $depth){ // Variables and arrays needed for loop checking. $array_of_children; $array_of_possible_loop; $possible_start_of_loop; $distance_between_parent_child; $increment_of_depth; chado_set_active('chado'); $count = db_query( 'SELECT * FROM cvtermpath WHERE cv_id = :cvid AND object_id = :origin AND subject_id = :child_id AND pathdistance = :depth ', array(':cvid' => $cv_id, ':origin' => $origin, ':child_id' => $child_id, ':depth' => $depth) ); $count_total = $count->rowCount(); //Loop check chado_set_active('chado'); $loop = db_query( 'SELECT * FROM cvtermpath WHERE cv_id = :cvid AND object_id = :origin AND subject_id = :child_id AND type_id = :type_id ', array(':cvid' => $cv_id, ':origin' => $origin, ':child_id' => $child_id, ':type_id' => $type_id,) ); $loop_check = $loop->rowCount(); //watchdog('debug', '
tripal_ds_preprocess_TripalEntity $rows ' . print_r($rows, TRUE) . ''); /*if(!empty($rows)){ foreach($rows as $row){ tripal_update_cvtermpath_loop_check($origin, $child_id, $cv_id, $type_id, $depth, $row->cvtermpath_id, 0); } } else {*/ //If no loop proceed. /*try{ if($count_total == 0) { chado_set_active('chado'); $query = db_insert('cvtermpath') ->fields(array( 'object_id' => $origin, 'subject_id' => $child_id, 'cv_id' => $cv_id, 'type_id' => $type_id, 'pathdistance' => $depth, )); $rows = $query->execute(); } if ($loop_check == 0) { chado_set_active('chado'); $query = db_select('cvterm_relationship', 'cvtr') ->fields('cvtr') ->condition('cvtr.object_id', $child_id, '=') ->execute(); $cterm = $query->fetchAll(); foreach ($cterm as $item) { //watchdog('debug', '
tripal_ds_preprocess_TripalEntity $item ' . print_r($item, TRUE) . ''); tripal_update_cvtermpath_loop($origin, $item->subject_id, $cv_id, $item->type_id, $depth + 1); }; //} } } catch(Exception $e){ watchdog_exception('tripal_ds', $e); return FALSE; } return 1; } */ /** * * @param $origin * @param $subject_id * @param $cv_id * @param $type_id * @param $depth * @return multitype: function tripal_update_cvtermpath_loop_check($origin, $child_id, $cv_id, $type_id, $depth, $cvtermpath_id, $loop_count, $loop_check, $object_id){ //Store the //Check if the passed parameters match any of the items in the loop_check array. if(!empty($loop_check)){ foreach($loop_check as $item){ if ($item['type_id'] = $type_id){ if($item['subject_id'] = $child_id){ if($item['object_id'] = $object_id){ //Loop found, roll back all rows until $cvtermpath_id-1 (last correct entry) // and step into the next loop } } } } } $loop_count + 1; chado_set_active('chado'); $count = db_query( 'SELECT * FROM cvtermpath WHERE cv_id = :cvid AND object_id = :origin AND subject_id = :child_id AND pathdistance = :depth ', array(':cvid' => $cv_id, ':origin' => $origin, ':child_id' => $child_id, ':depth' => $depth) ); $count_total = $count->rowCount(); if ($count_total == 0) { chado_set_active('chado'); $query = db_insert('cvtermpath') ->fields(array( 'object_id' => $origin, 'subject_id' => $child_id, 'cv_id' => $cv_id, 'type_id' => $type_id, 'pathdistance' => $depth, )); $rows = $query->execute(); $cterm = array(); $query = db_select('cvterm_relationship', 'cvtr') ->fields('cvtr') ->condition('cvtr.object_id', $child_id, '=' ) ->execute(); $cterm = $query->fetchAll(); foreach ($cterm as $item) { $loop_check[$loop_count]= $item; tripal_update_cvtermpath_loop_check($origin, $item->subject_id, $cv_id, $item->type_id, $depth + 1, $loop_count, $loop_check, $item->object_id); }; } return 1; } */ /** * Adds a controlled vocabular to the CV table of Chado. * * @param $name * The name of the controlled vocabulary. These are typically all lower case * with no special characters other than an undrescore (for spaces). * @param $comment * A description or definition of the vocabulary. * * @return * An object populated with fields from the newly added database. * * @ingroup tripal_chado_api */ function tripal_insert_cv($name, $definition) { // insert/update values $ins_values = array( 'name' => $name, 'definition' => $definition ); // see if the CV default exists already in the database $sel_values = array('name' => $name); $results = chado_select_record('cv', array('*'), $sel_values); // if it does not exists then add it if (count($results) == 0) { $success = chado_insert_record('cv', $ins_values); if (!$success) { tripal_report_error('tripal_chado', TRIPAL_WARNING, "Failed to create the CV record", NULL); return FALSE; } $results = chado_select_record('cv', array('*'), $sel_values); } // if it already exists then do an update else { $success = chado_update_record('cv', $sel_values, $ins_values); if (!$success) { tripal_report_error('tripal_chado', TRIPAL_WARNING, "Failed to update the CV record", NULL); return FALSE; } $results = chado_select_record('cv', array('*'), $sel_values); } // return the cv object return $results[0]; } /** * Add's a controlled vocabulary term to Chado. * * This function will add a cvterm record (and a dbxref record if appropriate * values are provided). If the parent vocabulary does not exist then * that also is added to the cv table. If the cvterm is a relationship term * then the 'is_relationship' value should be set. All * terms must also have a corresponding database. This is specified in the * term's ID just before the colon (e.g. GO:003824). If the database does not * exist in the DB table then it will be added automatically. The accession * (the value just after the colon in the term's ID) will be added to the * dbxref table. If the CVterm already exists and $update is set (default) * then the cvterm is updated. If the CVTerm already exists and $update is * not set, then no changes are made and the CVTerm object is returned. * * @param $term * An associative array with the following keys: * - id: the term accession. must be of the form