function tripal_feature_load_gff3_alias

2.x tripal_feature.gff_loader.inc tripal_feature_load_gff3_alias($feature, $aliases)
1.x gff_loader.inc tripal_feature_load_gff3_alias($feature, $aliases)

Related topics

1 call to tripal_feature_load_gff3_alias()
tripal_feature_load_gff3 in tripal_feature/includes/gff_loader.inc

File

tripal_feature/includes/gff_loader.inc, line 1265
@todo Add file header description

Code

function tripal_feature_load_gff3_alias($feature, $aliases) {

  // make sure we have a 'synonym_type' vocabulary
  $select = array('name' => 'synonym_type');
  $options = array('statement_name' => 'sel_cv_name');
  $results = tripal_core_chado_select('cv', array('*'), $select, $options);

  if (count($results) == 0) {
    // insert the 'synonym_type' vocabulary
    $values = array(
      'name' => 'synonym_type',
      'definition' => 'vocabulary for synonym types',
    );
    $options = array('statement_name' => 'ins_cv_name_definition');
    $success = tripal_core_chado_insert('cv', $values, $options);
    if (!$success) {
      watchdog("T_gff3_loader", "Failed to add the synonyms type vocabulary", array(), WATCHDOG_WARNING);
      return 0;
    }
    // now that we've added the cv we need to get the record
    $options = array('statement_name' => 'sel_cv_name');
    $results = tripal_core_chado_select('cv', array('*'), $select, $options);
    if (count($results) > 0) {
      $syncv = $results[0];
    }
  }
  else {
    $syncv = $results[0];
  }

  // get the 'exact' cvterm, which is the type of synonym we're adding
  $select = array(
    'name' => 'exact',
    'cv_id' => array(
      'name' => 'synonym_type'
    ),
  );
  $options = array('statement_name' => 'sel_cvterm_name_cvid');
  $result = tripal_core_chado_select('cvterm', array('*'), $select, $options);
  if (count($result) == 0) {
    $term = array(
      'name' => 'exact',
      'id' => "internal:exact",
      'definition' => '',
      'is_obsolete' => 0,
    );
    // TODO: fix the function so it uses prepared statements    
    $syntype = tripal_cv_add_cvterm($term, $syncv->name, 0, 1);
    if (!$syntype) {
      watchdog("T_gff3_loader", "Cannot add synonym type: internal:$type", array(), WATCHDOG_WARNING);
      return 0;
    }
  }
  else {
    $syntype = $result[0];
  }

  // iterate through all of the aliases and add each one
  foreach ($aliases as $alias) {

    // check to see if the alias already exists in the synonym table
    // if not, then add it
    $select = array(
      'name' => $alias,
      'type_id' => $syntype->cvterm_id,
    );
    $options = array('statement_name' => 'sel_synonym_name_typeid');
    $result = tripal_core_chado_select('synonym', array('*'), $select, $options);
    if (count($result) == 0) {
      $values = array(
        'name' => $alias,
        'type_id' => $syntype->cvterm_id,
        'synonym_sgml' => '',
      );
      $options = array('statement_name' => 'ins_synonym_name_typeid_synonymsgml');
      $success = tripal_core_chado_insert('synonym', $values, $options);
      if (!$success) {
        watchdog("T_gff3_loader", "Cannot add alias $alias to synonym table", array(), WATCHDOG_WARNING);
        return 0;
      }
      $options = array('statement_name' => 'sel_synonym_name_typeid');
      $result = tripal_core_chado_select('synonym', array('*'), $select, $options);
      $synonym = $result[0];
    }
    else {
      $synonym = $result[0];
    }

    // check to see if we have a NULL publication in the pub table.  If not,
    // then add one.
    // @coder-ignore: non-drupal schema thus table prefixing does not apply
    $select = array('uniquename' => 'null');
    $options = array('statement_name' => 'sel_pub_uniquename');
    $result = tripal_core_chado_select('pub', array('*'), $select, $options);
    if (count($result) == 0) {
      // prepare the statement
      if (!tripal_core_is_sql_prepared('ins_pub_uniquename_typeid')) {
        $psql = "PREPARE ins_pub_uniquename_typeid (text, text) AS
                 INSERT INTO {pub} (uniquename,type_id) VALUES ('%s',
                 (SELECT cvterm_id
                  FROM {cvterm} CVT
                    INNER JOIN {dbxref} DBX on DBX.dbxref_id = CVT.dbxref_id
                    INNER JOIN {db} DB on DB.db_id = DBX.db_id
                  WHERE CVT.name = $1 and DB.name = $2)";
        $status = tripal_core_chado_prepare('ins_pub_uniquename_typeid', $psql, args('text', 'text'));
        if (!$status) {
          watchdog("T_gff3_loader", "Cannot prepare statement 'ins_pub_uniquename_typeid", array(), WATCHDOG_WARNING);
          return 0;
        }
      }
      // insert the null pub 
      $result = db_fetch_object(chado_query("EXECUTE ins_pub_uniquename_typeid ('%s', '%s')", 'null', 'null'));
      if (!$result) {
        watchdog("T_gff3_loader", "Cannot add null publication needed for setup of alias", array(), WATCHDOG_WARNING);
        return 0;
      }
      $options = array('statement_name' => 'sel_pub_uniquename');
      $result = tripal_core_chado_select('pub', array('*'), $select, $options);
      $pub = $result[0];
    }
    else {
      $pub = $result[0];
    }

    // check to see if the synonym exists in the feature_synonym table
    // if not, then add it.
    $values = array(
      'synonym_id' => $synonym->synonym_id,
      'feature_id' => $feature->feature_id,
      'pub_id' => $pub->pub_id,
    );
    $columns = array('feature_synonym_id');
    $options = array('statement_name' => 'sel_featuresynonym_syfepu');
    $result = tripal_core_chado_select('feature_synonym', $columns, $values, $options);
    if (count($result) == 0) {
      $values = array(
        'synonym_id' => $synonym->synonym_id,
        'feature_id' => $feature->feature_id,
        'pub_id' => $pub->pub_id,
      );
      $ins_options = array('statement_name' => 'ins_featuresynonym_syfepu');
      $success = tripal_core_chado_insert('feature_synonym', $values, $ins_options);

      if (!$success) {
        watchdog("T_gff3_loader", "Cannot add alias $alias to feature synonym table", array(), WATCHDOG_WARNING);
        return 0;
      }
    }
  }
  return 1;
}