function chado_associate_semweb_term

3.x tripal_chado.semweb.api.inc chado_associate_semweb_term($chado_table, $chado_column, $term, $update = FALSE)

Associates a controlled vocabulary term with a field in a Chado table.

For sharing of data via the semantic web we need to associate a term from a controlled vocabulary with every column of every table in Chado.

Temporary tables (e.g. Tripal tables that begin with 'tripal_' and end with '_temp', are not supported.

Parameters

$chado_table: The name of the table in Chado. This argument is optional. If left empty or set to NULL then all fields in all Chado tables with that have the $column_name will be associated with the provided $term.

$chado_column: The column name in the Chado table to which the term should be associated.

$term: A cvterm object as returned by chado_generate_var().

$update: Set to TRUE if the association should be updated to use the new term if a term is already associated with the table and column. Default is FALSE. If not TRUE and a term is already associated, then no change occurs.

Return value

boolean Returns TRUE if the association was made successfully and FALSE otherwise.

Related topics

23 calls to chado_associate_semweb_term()
tripal_associate_chado_semweb_term in tripal_chado/api/tripal_chado.DEPRECATED.api.inc
Associates a controlled vocabulary term with a field in a Chado table.
tripal_chado_populate_vocab_EDAM in tripal_chado/includes/tripal_chado.semweb.inc
Adds the EDAM database and terms.
tripal_chado_populate_vocab_IAO in tripal_chado/includes/tripal_chado.semweb.inc
Adds the Information Artifact Ontology database and terms.
tripal_chado_populate_vocab_LOCAL in tripal_chado/includes/tripal_chado.semweb.inc
Adds terms to the 'local' database.
tripal_chado_populate_vocab_NCBITAXON in tripal_chado/includes/tripal_chado.semweb.inc
Adds the NCBI Taxon vocabulary database and terms.

... See full list

File

tripal_chado/api/tripal_chado.semweb.api.inc, line 95
Provides an application programming interface (API) for semantic web support.

Code

function chado_associate_semweb_term($chado_table, $chado_column, $term, 
$update = FALSE) {

  // Check for required arguments.
  if (!$chado_column) {
    tripal_set_message('Please provide the $chado_column argument.', TRIPAL_ERROR);
    return FALSE;
  }
  if (!$term) {
    tripal_set_message('Please provide the $term argument.', TRIPAL_ERROR);
    return FALSE;
  }

  // Make sure the field is a real field for the table.
  if ($chado_table) {
    $schema = chado_get_schema($chado_table);
    if (!$schema) {
      tripal_set_message('The $chado_table is not a known table in Chado.', TRIPAL_ERROR);
      return FALSE;
    }
    if (!array_key_exists($chado_column, $schema['fields'])) {
      tripal_set_message('The $chado_column is not a known column in the $chado_table.', TRIPAL_ERROR);
      return FALSE;
    }
  }

  // First check to see if a valid record exists that matches the table and
  // column indicated. If it doesn't then insert the record.
  $query = db_select('chado_semweb', 'CS')
    ->fields('CS', array('chado_semweb_id'))
    ->condition('chado_column', $chado_column);
  if ($chado_table) {
    $query->condition('chado_table', $chado_table);
  }
  $query->range(0, 1);
  $id = $query->execute()->fetchField();
  if (!$id) {

    // If no $chado_table record is provided then return FALSE as we can't
    // insert a record without a table.
    if (!$chado_table) {
      tripal_set_message('The provided $chado_column has no match for any
          table currently known. This could be because the table has not yet
          been added to the semantic web management. Please provide the
          $chado_table.', TRIPAL_ERROR);
      return FALSE;
    }

    // Insert the record.
    $id = db_insert('chado_semweb')
      ->fields(array(
        'chado_table' => $chado_table,
        'chado_column' => $chado_column,
        'cvterm_id' => $term->cvterm_id,
      ));
    if ($id) {
      return TRUE;
    }
    else {
      tripal_set_message('Failure associating term.', TRIPAL_ERROR);
      return FALSE;
    }
  }

  // If the $chado_table argument is empty or NULL then the term applies to
  // all fields of the specified name.
  $update = db_update('chado_semweb')
    ->fields(array(
      'cvterm_id' => $term->cvterm_id
    ))
    ->condition('chado_column', $chado_column);
  if ($chado_table) {
    $update->condition('chado_table', $chado_table);
  }
  if (!$update) {
    $update->condition('cvterm_id', NULL);
  }
  $num_updated = $update->execute();
  if (!$num_updated) {
    tripal_set_message('Failure associating term.', TRIPAL_ERROR);
    return FALSE;
  }

  return TRUE;
}