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)

Load the dbxref attribute for a feature

Parameters

$feature:

$dbxrefs:

Related topics

1 call to tripal_feature_load_gff3_dbxref()
tripal_feature_load_gff3 in tripal_feature/includes/tripal_feature.gff_loader.inc
Actually load a GFF3 file. This is the function called by tripal jobs

File

tripal_feature/includes/tripal_feature.gff_loader.inc, line 1377
Provides gff3 loading functionality. Creates features based on their specification in a GFF3 file.

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 = trim($ref[0]);
    $accession = trim($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");
    $db = chado_select_record('db', array('db_id'), $values);
    if (count($db) == 0) {
      $values = array('name' => "$dbname");
      $db = chado_select_record('db', array('db_id'), $values);
    }
    if (count($db) == 0) {
      $values = array(
        'name' => $dbname,
        'description' => 'Added automatically by the GFF loader'
      );
      $success = chado_insert_record('db', $values);
      if ($success) {
        $values = array('name' => "$dbname");
        $db = chado_select_record('db', array('db_id'), $values);
      }
      else {
        tripal_report_error("tripal_feature", TRIPAL_WARNING, "Cannot find or add the database $dbname", array());
        return 0;
      }
    }
    $db = $db[0];

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

    // 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' => ''
      );
      $ret = chado_insert_record('dbxref', $values);
      $values = array(
        'accession' => $accession,
        'db_id' => $db->db_id
      );
      $dbxref = chado_select_record('dbxref', array('dbxref_id'), $values);
    }
    $dbxref = $dbxref[0];

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

    // 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
      );
      $success = chado_insert_record('feature_dbxref', $values);
      if (!$success) {
        tripal_report_error("tripal_feature", TRIPAL_WARNING, "Failed to insert Dbxref: $dbname:$accession", array());
        return 0;
      }
    }
  }
  return 1;
}