function chado_get_record_with_property

2.x tripal_core.chado_general.api.inc chado_get_record_with_property($record, $property, $options = array())
3.x tripal_chado.property.api.inc chado_get_record_with_property($record, $property, $options = array())

Get all records in the base table assigned one or more properties.

The property or properties of interest are specified using the $property argument.

Parameters

$record: An associative array used to identify the table and subset of records to to be searched: -table: The base table for which the property should be updated. Thus to update a property for a feature the base_table=feature. -base_records: An array in the format accepted by the chado_select_record for specifying a subset of records in the base table.

$property: An associative array used to specify the property to be selected for. It can contain the following keys. The keys must be specified to uniquely identify the term to be searched. If the options identify more than one CV term then an error will occur. -type_name: The cvterm name to be selected. -type_id: The cvterm_id of the term to be selected. -cv_id: The cv_id of the CV that contains the term. -cv_name: The name of the CV that contains the term. -value: The specific value for the property. -rank: The specific rank for the property.

$options: An array of options supported by chado_generate_var(). These keys are used for generating the cvterm objects returned by this function.

Return value

An array of chado variables with the given property

Related topics

7 calls to chado_get_record_with_property()
tripal_get_analysis in tripal_analysis/api/tripal_analysis.api.inc
Retrieves an chado analysis variable
tripal_get_cvterm in tripal_cv/api/tripal_cv.api.inc
Retrieves a chado controlled vocabulary term variable
tripal_get_dbxref in tripal_db/api/tripal_db.api.inc
Retrieves a chado database reference variable
tripal_get_multiple_stocks in tripal_stock/api/tripal_stock.api.inc
Retrieves a chado stock variable
tripal_get_organism in tripal_organism/api/tripal_organism.api.inc
Retrieves a chado organism variable

... See full list

File

tripal_core/api/tripal_core.chado_general.api.inc, line 699
Provides an application programming interface (API) to manage data withing the Chado database.

Code

function chado_get_record_with_property($record, $property, $options = array()) {

  $base_table = array_key_exists('table', $record) ? $record['table'] : '';
  $base_records = array_key_exists('base_records', $record) ? $record['base_records'] : array();

  $type_name = array_key_exists('type_name', $property) ? $property['type_name'] : '';
  $type_id = array_key_exists('type_id', $property) ? $property['type_id'] : '';
  $cv_name = array_key_exists('cv_name', $property) ? $property['cv_name'] : '';
  $cv_id = array_key_exists('cv_id', $property) ? $property['cv_id'] : '';
  $value = array_key_exists('value', $property) ? $property['value'] : '';
  $rank = array_key_exists('rank', $property) ? $property['rank'] : '';

  $property_table = $base_table . 'prop';
  $foreignkey_name = $base_table . '_id';

  // Build the values array for checking if the CVterm exists and for
  // inserting the term as a property.
  $type = array();
  if ($cv_id) {
    $type['cv_id'] = $cv_id;
  }
  if ($cv_name) {
    $type['cv_id'] = array(
      'name' => $cv_name,
    );
  }
  if ($type_name) {
    $type['name'] = $type_name;
  }
  if ($type_id) {
    $type['cvterm_id'] = $type_id;
  }

  // Make sure the CV term exists;
  $term = chado_select_record('cvterm', array('cvterm_id'), $type);
  if (!$term or count($term) == 0) {
    tripal_report_error('tripal_core', TRIPAL_ERROR, "chado_update_property: " .
      "Cannot find the term described by: %property.", 
    array('%property' => print_r($property, TRUE)));
    return FALSE;
  }
  if (count($term) > 1) {
    tripal_report_error('tripal_core', TRIPAL_ERROR, "chado_update_property: " .
      "Multiple terms found. Cannot add the property. Property was described " .
      "by: %property.", 
    array('%property' => print_r($property, TRUE)));
    return FALSE;
  }

  // Build the array for identifying the property.
  $values = array();
  $values['type_id'] = $type;
  if ($rank) {
    $values['rank'] = $rank;
  }
  if ($value) {
    $values['value'] = $value;
  }

  // Add the base records details to the values array.
  if (!empty($base_records)) {
    $values[$foreignkey_name] = $base_records;
  }

  // Now select the ids of the base table that have the properties we want that match.
  $select = chado_select_record($property_table, array($foreignkey_name), $values);

  // For each of these ids, pull out the full base records
  $records = array();
  foreach ($select as $s) {
    $id = $s->{$foreignkey_name};
    $values = array($foreignkey_name => $id);
    $records[$id] = chado_generate_var($base_table, $values, $options);
  }

  return $records;
}