function tripal_associate_dbxref

2.x tripal_db.api.inc tripal_associate_dbxref($basetable, $record_id, $dbxref, $options = array())
3.x tripal_chado.module.DEPRECATED.api.inc tripal_associate_dbxref($basetable, $record_id, $dbxref, $options = array())

Add a record to a database reference linking table (ie: feature_dbxref)

Parameters

$basetable: The base table for which the dbxref should be associated. Thus to associate a dbxref with a feature the basetable=feature and dbxref_id is added to the feature_dbxref table

$record_id: The primary key of the basetable to associate the dbxref with. This should be in integer.

$dbxref: An associative array describing the dbxref. Valid keys include: 'accession' => the accession for the dbxref, 'db_name' => the name of the database the dbxref belongs to; 'db_id' => the primary key of the database the dbxref belongs to.

$options: An associative array of options. Valid keys include:

  • insert_dbxref: Insert the dbxref if it doesn't already exist. TRUE is the default

Related topics

5 calls to tripal_associate_dbxref()
chado_update_node_form_dbxrefs in tripal_core/api/tripal_core.chado_nodes.dbxrefs.api.inc
This function is used in hook_insert or hook_update and handles inserting of any new dbxrefs and creation of links between those dbxrefs and node content
tripal_db_add_dbxref_link in tripal_db/api/tripal_db.DEPRECATED.inc
tripal_feature_add_dbxref in tripal_feature/api/tripal_feature.DEPRECATED.inc
tripal_pub_add_publications in tripal_pub/includes/tripal_pub.pub_importers.inc
Adds publications that have been retrieved from a remote database and consolidated into an array of details.
tripal_pub_add_pub_dbxref in tripal_pub/api/tripal_pub.DEPRECATED.inc
3 string references to 'tripal_associate_dbxref'

File

tripal_db/api/tripal_db.api.inc, line 425
Provides an application programming interface (API) to manage references to external databases

Code

function tripal_associate_dbxref($basetable, $record_id, $dbxref, $options = array()) {
  $linking_table = $basetable . '_dbxref';
  $foreignkey_name = $basetable . '_id';

  // Default Values
  $options['insert_dbxref'] = (isset($options['insert_dbxref'])) ? $options['insert_dbxref'] : TRUE;

  // If the dbxref_id is set then we know it already exists
  // Otherwise, select to check
  if (!isset($dbxref['dbxref_id'])) {
    $values = array(
      'accession' => $dbxref['accession'],
    );
    if (isset($dbxref['db_id'])) {
      $values['db_id'] = $dbxref['db_id'];
    }
    elseif (isset($dbxref['db_name'])) {
      $values['db_id'] = array(
        'name' => $dbxref['db_name']
      );
    }
    else {
      tripal_report_error(
      'tripal_db_api', 
      TRIPAL_WARNING, 
      "tripal_associate_dbxref: The dbxref needs to have either the db_name or db_id
          supplied. You were trying to associate a dbxref with the %base %record_id
          and supplied the dbxref values: %dbxref.", 
      array('%base' => $basetable, '%record_id' => $record_id, '%dbxref' => print_r($dbxref, TRUE))
      );
      return FALSE;
    }
    $select = chado_select_record('dbxref', array('*'), $values);
    if ($select) {
      $dbxref['dbxref_id'] = $select[0]->dbxref_id;
    }
    elseif ($options['insert_dbxref']) {
      // Insert the dbxref
      $insert = tripal_insert_dbxref($values);
      if (isset($insert->dbxref_id)) {
        $dbxref['dbxref_id'] = $insert->dbxref_id;
      }
      else {
        tripal_report_error(
        'tripal_db_api', 
        TRIPAL_WARNING, 
        "tripal_associate_dbxref: Unable to insert the dbxref using the dbxref values: %dbxref.", 
        array('%dbxref' => print_r($dbxref, TRUE))
        );
        return FALSE;
      }
    }
    else {
      tripal_report_error(
      'tripal_api', 
      TRIPAL_WARNING, 
      "tripal_associate_dbxref: The dbxref doesn't already exist. You supplied the dbxref values: %dbxref.", 
      array('%dbxref' => print_r($dbxref, TRUE))
      );
      return FALSE;
    }
  }

  // Now add the link between the record & dbxref
  if ($dbxref['dbxref_id'] > 0) {
    $values = array(
      'dbxref_id' => $dbxref['dbxref_id'],
      $foreignkey_name => $record_id
    );

    $result = chado_select_record($linking_table, array('*'), $values);

    // if it doesn't already exist then add it
    if (!$result) {
      $success = chado_insert_record($linking_table, $values);
      if (!$success) {
        tripal_report_error(
        'tripal_api', 
        TRIPAL_WARNING, 
        "Failed to insert the %base record %accession", 
        array('%base' => $linking_table, '%accession' => $dbxref['accession'])
        );
        return FALSE;
      }
      $result = chado_select_record($linking_table, array('*'), $values);
    }

    if (isset($result[0])) {
      return $result[0];
    }
    else {
      return FALSE;
    }
  }

  return FALSE;
}