function chado_feature_add_synonyms

2.x tripal_feature.chado_node.inc chado_feature_add_synonyms($synonyms, $feature_id)
3.x tripal_feature.chado_node.inc chado_feature_add_synonyms($synonyms, $feature_id)
1.x tripal_feature.module chado_feature_add_synonyms($synonyms, $feature_id)

Add synonyms to a feature

Parameters

$synonyms: A string containing synonyms separated by a return character

$feature_id: The feature to attach the synonyms to

2 calls to chado_feature_add_synonyms()
chado_feature_insert in legacy/tripal_feature/includes/tripal_feature.chado_node.inc
Implements hook_insert().
chado_feature_update in legacy/tripal_feature/includes/tripal_feature.chado_node.inc
Implements hook_update().

File

legacy/tripal_feature/includes/tripal_feature.chado_node.inc, line 649
Implementation of hooks to create a feature content type

Code

function chado_feature_add_synonyms($synonyms, $feature_id) {

  // separate synomys by carriage returns
  $synonyms = preg_replace("/[\n\r]+/", " ", $synonyms);
  // split the synonyms into an array based on a space as the delimieter
  $syn_array = array();
  $syn_array = explode(" ", $synonyms);


  // remove any old synonyms
  $feature_syn_dsql = "DELETE FROM {feature_synonym} WHERE feature_id = :feature_id";
  if (!chado_query($feature_syn_dsql, array(':feature_id' => $feature_id))) {
    tripal_report_error('tripal_feature', TRIPAL_ERROR, "Could not remove synonyms from feature. ", array());
    return;
  }

  // return if we don't have any synonmys to add
  if (!$synonyms) {
    return;
  }
  // iterate through each synonym and add it to the database
  foreach ($syn_array as $syn) {
    // skip this item if it's empty
    if (!$syn) {
      break;
    }

    // check to see if we have this accession number already in the database
    // if so then don't add it again. it messes up drupal if the insert fails.
    // It is possible for the accession number to be present and not the feature
    $synonym_sql = "SELECT synonym_id FROM {synonym} WHERE name = :name";
    $synonym = chado_query($synonym_sql, array(':name' => $syn))->fetchObject();
    if (!$synonym) {
      $synonym_isql = "
        INSERT INTO {synonym} (name, synonym_sgml, type_id)
        VALUES (:name, :synonym_sgml,
          (SELECT cvterm_id
           FROM {cvterm} CVT
             INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
           WHERE CV.name = 'feature_property' and CVT.name = 'synonym')
          )
      ";
      if (!chado_query($synonym_isql, array(':name' => $syn, ':synonym_sgml' => $syn))) {
        tripal_report_error('tripal_feature', "Could not add synonym. ", array(), TRIPAL_WARNING);
        return;
      }
      // now get the synonym we just added
      $synonym_sql = "SELECT synonym_id FROM {synonym} WHERE name = :name";
      $synonym = chado_query($synonym_sql, array(':name' => $syn))->fetchObject();
    }

    // now add in our new sysnonym
    $feature_syn_isql = "
      INSERT INTO {feature_synonym} (synonym_id,feature_id,pub_id)
      VALUES (:synonym_id, :feature_id, :pub_id)";
    $args = array(':synonym_id' => $synonym->synonym_id, ':feature_id' => $feature_id, ':pub_id' => 1);
    if (!chado_query($feature_syn_isql, $args)) {
      tripal_report_error('tripal_feature', "Could not associate synonym with feature. ", array(), TRIPAL_WARNING);
      return;
    }
  }
}