function tripal_feature_load_gff3_dbxref

2.x tripal_feature.gff_loader.inc tripal_feature_load_gff3_dbxref($feature, $dbxrefs)
1.x gff_loader.inc tripal_feature_load_gff3_dbxref($feature, $dbxrefs)
1 call to tripal_feature_load_gff3_dbxref()
tripal_feature_load_gff3 in tripal_feature/includes/gff_loader.inc

File

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

Code

function tripal_feature_load_gff3_dbxref($feature, $dbxrefs) {

  // iterate through each of the dbxrefs
  foreach ($dbxrefs as $dbxref) {

    // get the database name from the reference.  If it doesn't exist then create one.
    $ref = explode(":", $dbxref);
    $dbname = $ref[0];
    $accession = $ref[1];

    // first look for the database name if it doesn't exist then create one.
    // first check for the fully qualified URI (e.g. DB:<dbname>. If that
    // can't be found then look for the name as is.  If it still can't be found
    // the create the database
    $values = array('name' => "DB:$dbname");
    $options = array('statement_name' => 'sel_db_name');
    $db = tripal_core_chado_select('db', array('db_id'), $values, $options);
    if (count($db) == 0) {
      $values = array('name' => "$dbname");
      $db = tripal_core_chado_select('db', array('db_id'), $values, $options);
    }
    if (count($db) == 0) {
      $values = array(
        'name' => $dbname,
        'description' => 'Added automatically by the GFF loader'
      );
      $options = array('statement_name' => 'ins_db_name');
      $success = tripal_core_chado_insert('db', $values, $options);
      if ($success) {
        $values = array('name' => "$dbname");
        $options = array('statement_name' => 'sel_db_name');
        $db = tripal_core_chado_select('db', array('db_id'), $values, $options);
      }
      else {
        watchdog("T_gff3_loader", "Cannot find or add the database $dbname", array(), WATCHDOG_WARNING);
        return 0;
      }
    }
    $db = $db[0];

    // now check to see if the accession exists
    $values = array(
      'accession' => $accession,
      'db_id' => $db->db_id
    );
    $options = array('statement_name' => 'sel_dbxref_accession_dbid');
    $dbxref = tripal_core_chado_select('dbxref', array('dbxref_id'), $values, $options);

    // if the accession doesn't exist then we want to add it
    if (sizeof($dbxref) == 0) {
      $values = array(
        'db_id' => $db->db_id,
        'accession' => $accession,
        'version' => ''
      );
      $options = array('statement_name' => 'ins_dbxref_dbid_accession_version');
      $ret = tripal_core_chado_insert('dbxref', $values, $options);
      $values = array(
        'accession' => $accession,
        'db_id' => $db->db_id
      );
      $options = array('statement_name' => 'sel_dbxref_accession_dbid');
      $dbxref = tripal_core_chado_select('dbxref', array('dbxref_id'), $values, $options);
    }
    $dbxref = $dbxref[0];

    // check to see if this feature dbxref already exists
    $values = array(
      'dbxref_id' => $dbxref->dbxref_id,
      'feature_id' => $feature->feature_id
    );
    $options = array('statement_name' => 'sel_featuredbxref_dbxrefid_featureid');
    $fdbx = tripal_core_chado_select('feature_dbxref', array('feature_dbxref_id'), $values, $options);

    // now associate this feature with the database reference if it doesn't
    // already exist
    if (sizeof($fdbx) == 0) {
      $values = array(
        'dbxref_id' => $dbxref->dbxref_id,
        'feature_id' => $feature->feature_id
      );
      $options = array('statement_name' => 'ins_featuredbxref_dbxrefid_featureid');
      $success = tripal_core_chado_insert('feature_dbxref', $values, $options);
      if (!$success) {
        watchdog("T_gff3_loader", "Failed to insert Dbxref: $dbname:$accession", array(), WATCHDOG_WARNING);
        return 0;
      }
    }
  }
  return 1;
}