function tripal_feature_load_fasta_feature

2.x tripal_feature.fasta_loader.inc tripal_feature_load_fasta_feature($fh, $name, $uname, $db_id, $accession, $parent, $rel_type, $parent_type, $analysis_id, $organism_id, $cvterm, $source, $method, $re_name, $match_type, $parentcvterm, $relcvterm, $seq_start, $seq_end)

A helper function for tripal_feature_load_fasta() to load a single feature

Related topics

1 call to tripal_feature_load_fasta_feature()
tripal_feature_load_fasta in tripal_feature/includes/tripal_feature.fasta_loader.inc
Actually load a fasta file. This is the function called by tripal jobs

File

tripal_feature/includes/tripal_feature.fasta_loader.inc, line 634
Provides fasta loading functionality. Creates features based on their specification in a fasta file.

Code

function tripal_feature_load_fasta_feature($fh, $name, $uname, $db_id, $accession, $parent, 
$rel_type, $parent_type, $analysis_id, $organism_id, $cvterm, $source, $method, $re_name, 
$match_type, $parentcvterm, $relcvterm, $seq_start, $seq_end) {

  // Check to see if this feature already exists if the match_type is 'Name'.
  if (strcmp($match_type, 'Name') == 0) {
    $values = array('organism_id' => $organism_id, 'name' => $name, 'type_id' => $cvterm->cvterm_id
    );
    $results = chado_select_record('feature', array('feature_id'
    ), $values);
    if (count($results) > 1) {
      tripal_report_error('T_fasta_loader', TRIPAL_ERROR, "Multiple features exist with the name '%name' of type
               '%type' for the organism.  skipping", array('%name' => $name, '%type' => $type));
      return 0;
    }
    if (count($results) == 1) {
      $feature = $results[0];
    }
  }

  // Check if this feature already exists if the match_type is 'Unique Name'.
  if (strcmp($match_type, 'Unique name') == 0) {
    $values = array(
      'organism_id' => $organism_id,
      'uniquename' => $uname,
      'type_id' => $cvterm->cvterm_id
    );

    $results = chado_select_record('feature', array('feature_id'), $values);
    if (count($results) > 1) {
      tripal_report_error('T_fasta_loader', TRIPAL_WARNING, "Multiple features exist with the name '%name' of type '%type' for the organism.  skipping", array(
        '%name' => $name, '%type' => $type));
      return 0;
    }
    if (count($results) == 1) {
      $feature = $results[0];
    }

    // If the feature exists but this is an "insert only" then skip.
    if (isset($feature) and (strcmp($method, 'Insert only') == 0)) {
      tripal_report_error('T_fasta_loader', TRIPAL_WARNING, "Feature already exists '%name' ('%uname') while matching on %type. Skipping insert.", array(
        '%name' => $name, '%uname' => $uname, '%type' => drupal_strtolower($match_type)
      ));
      return 0;
    }
  }

  // If we don't have a feature and we're doing an insert then do the insert.
  $inserted = 0;
  if (!isset($feature) and (strcmp($method, 'Insert only') == 0 or strcmp($method, 'Insert and update') == 0)) {
    // If we have a unique name but not a name then set them to be the same
    if (!$uname) {
      $uname = $name;
    }
    elseif (!$name) {
      $name = $uname;
    }

    // Insert the feature record.
    $values = array(
      'organism_id' => $organism_id,
      'name' => $name,
      'uniquename' => $uname,
      'type_id' => $cvterm->cvterm_id
    );
    $success = chado_insert_record('feature', $values);
    if (!$success) {
      tripal_report_error('T_fasta_loader', TRIPAL_ERROR, "Failed to insert feature '%name (%uname)'", array(
        '%name' => $name, '%uname' => $numane));
      return 0;
    }

    // now get the feature we just inserted
    $values = array(
      'organism_id' => $organism_id,
      'uniquename' => $uname,
      'type_id' => $cvterm->cvterm_id
    );
    $results = chado_select_record('feature', array('feature_id'), $values);
    if (count($results) == 1) {
      $inserted = 1;
      $feature = $results[0];
    }
    else {
      tripal_report_error('T_fasta_loader', TRIPAL_ERROR, "Failed to retreive newly inserted feature '%name (%uname)'", array(
        '%name' => $name, '%uname' => $numane));
      return 0;
    }

    // Add the residues for this feature
    tripal_feature_load_fasta_residues($fh, $feature->feature_id, $seq_start, $seq_end);
  }

  // if we don't have a feature and the user wants to do an update then fail
  if (!isset($feature) and (strcmp($method, 'Update only') == 0 or
      drupal_strcmp($method, 'Insert and update') == 0)) {
    tripal_report_error('T_fasta_loader', TRIPAL_ERROR, "Failed to find feature '%name' ('%uname') while matching on " .
      drupal_strtolower($match_type), array('%name' => $name, '%uname' => $uname));
    return 0;
  }

  // if we do have a feature and this is an update then proceed with the update
  if (isset($feature) and !$inserted and (strcmp($method, 'Update only') == 0 or
      strcmp($method, 'Insert and update') == 0)) {

    // if the user wants to match on the Name field
    if (strcmp($match_type, 'Name') == 0) {

      // if we're matching on the name but do not have a unique name then we
      // don't want to update the uniquename.
      $values = array();
      if ($uname) {

        // First check to make sure that by changing the unique name of this
        // feature that we won't conflict with another existing feature of
        // the same name
        $values = array(
          'organism_id' => $organism_id,
          'uniquename' => $uname,
          'type_id' => $cvterm->cvterm_id
        );
        $results = chado_select_record('feature', array('feature_id'
        ), $values);
        if (count($results) > 0) {
          tripal_report_error('T_fasta_loader', TRIPAL_ERROR, "Cannot update the feature '%name' with a uniquename of '%uname' and type of '%type' as it
            conflicts with an existing feature with the same uniquename and type.", array(
            '%name' => $name, '%uname' => $uname, '%type' => $type
          ));
          return 0;
        }

        // the changes to the uniquename don't conflict so proceed with the update
        $values = array('uniquename' => $uname);
        $match = array(
          'name' => $name,
          'organism_id' => $organism_id,
          'type_id' => $cvterm->cvterm_id
        );

        // perform the update
        $success = chado_update_record('feature', $match, $values);
        if (!$success) {
          tripal_report_error('T_fasta_loader', TRIPAL_ERROR, "Failed to update feature '%name' ('%name')", array(
            '%name' => $name, '%uiname' => $uname
          ));
          return 0;
        }
      }
    }

    // If the user wants to match on the unique name field.
    if (strcmp($match_type, 'Unique name') == 0) {
      // If we're matching on the uniquename and have a new name then
      // we want to update the name.
      $values = array();
      if ($name) {
        $values = array('name' => $name);
        $match = array(
          'uniquename' => $uname,
          'organism_id' => $organism_id,
          'type_id' => $cvterm->cvterm_id
        );
        $success = chado_update_record('feature', $match, $values);
        if (!$success) {
          tripal_report_error('T_fasta_loader', TRIPAL_ERROR, "Failed to update feature '%name' ('%name')", array(
            '%name' => $name, '%uiname' => $uname
          ));
          return 0;
        }
      }
    }
  }

  // Update the residues for this feature
  tripal_feature_load_fasta_residues($fh, $feature->feature_id, $seq_start, $seq_end);

  // add in the analysis link
  if ($analysis_id) {
    // if the association doens't alredy exist then add one
    $values = array(
      'analysis_id' => $analysis_id,
      'feature_id' => $feature->feature_id
    );
    $results = chado_select_record('analysisfeature', array('analysisfeature_id'), $values);
    if (count($results) == 0) {
      $success = chado_insert_record('analysisfeature', $values);
      if (!$success) {
        tripal_report_error('T_fasta_loader', TRIPAL_ERROR, "Failed to associate analysis and feature '%name' ('%name')", array(
          '%name' => $name, '%uname' => $uname
        ));
        return 0;
      }
    }
  }

  // now add the database cross reference
  if ($db_id) {
    // check to see if this accession reference exists, if not add it
    $values = array(
      'db_id' => $db_id,
      'accession' => $accession
    );
    $results = chado_select_record('dbxref', array('dbxref_id'), $values);
    // if the accession doesn't exist then add it
    if (count($results) == 0) {
      $results = chado_insert_record('dbxref', $values);
      if (!$results) {
        tripal_report_error('T_fasta_loader', TRIPAL_ERROR, "Failed to add database accession '%accession'", array(
          '%accession' => $accession));
        return 0;
      }
      $results = chado_select_record('dbxref', array('dbxref_id'), $values);
      if (count($results) == 1) {
        $dbxref = $results[0];
      }
      else {
        tripal_report_error('T_fasta_loader', TRIPAL_ERROR, "Failed to retreive newly inserted dbxref '%name (%uname)'", array(
          '%name' => $name, '%uname' => $numane));
        return 0;
      }
    }
    else {
      $dbxref = $results[0];
    }

    // check to see if the feature dbxref record exists if not, then add it
    $values = array(
      'feature_id' => $feature->feature_id,
      'dbxref_id' => $dbxref->dbxref_id
    );
    $results = chado_select_record('feature_dbxref', array('feature_dbxref_id'), $values);
    if (count($results) == 0) {
      $success = chado_insert_record('feature_dbxref', $values);
      if (!$success) {
        tripal_report_error('T_fasta_loader', TRIPAL_ERROR, "Failed to add associate database accession '%accession' with feature", array(
          '%accession' => $accession
        ));
        return 0;
      }
    }
  }

  // now add in the relationship if one exists. If not, then add it
  if ($rel_type) {
    $values = array('organism_id' => $organism_id, 'uniquename' => $parent,
      'type_id' => $parentcvterm->cvterm_id
    );
    $results = chado_select_record('feature', array('feature_id'
    ), $values);
    if (count($results) != 1) {
      tripal_report_error('T_fasta_loader', TRIPAL_ERROR, "Cannot find a unique feature for the parent '%parent' of type
               '%type' for the feature.", array(
        '%parent' => $parent, '%type' => $parent_type
      ));
      return 0;
    }
    $parent_feature = $results[0];

    // check to see if the relationship already exists if not then add it
    $values = array(
      'subject_id' => $feature->feature_id,
      'object_id' => $parent_feature->feature_id,
      'type_id' => $relcvterm->cvterm_id
    );
    $results = chado_select_record('feature_relationship', array('feature_relationship_id'), $values);
    if (count($results) == 0) {
      $success = chado_insert_record('feature_relationship', $values);
      if (!$success) {
        tripal_report_error('T_fasta_loader', TRIPAL_ERROR, "Failed to add associate database accession '%accession' with feature", array(
          '%accession' => $accession
        ));
        return 0;
      }
    }
  }
}