function tripal_feature_fasta_loader_handle_feature

1.x fasta_loader.inc tripal_feature_fasta_loader_handle_feature($name, $uname, $db_id, $accession, $parent, $rel_type, $parent_type, $analysis_id, $organism_id, $cvterm, $source, $residues, $method, $re_name, $match_type, $parentcvterm, $relcvterm)

Related topics

1 call to tripal_feature_fasta_loader_handle_feature()
tripal_feature_load_fasta in tripal_feature/includes/fasta_loader.inc

File

tripal_feature/includes/fasta_loader.inc, line 610
@todo Add file header description

Code

function tripal_feature_fasta_loader_handle_feature($name, $uname, $db_id, $accession, 
$parent, $rel_type, $parent_type, $analysis_id, $organism_id, $cvterm, 
$source, $residues, $method, $re_name, $match_type, $parentcvterm, $relcvterm) {

  // 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,
    );
    $options = array('statement_name' => 'sel_feature_ornaty');
    $results = tripal_core_chado_select('feature', array('feature_id'), $values, $options);
    if (count($results) > 1) {
      watchdog('T_fasta_loader', "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 to see 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,
    );

    $options = array('statement_name' => 'sel_feature_oruqty');
    $results = tripal_core_chado_select('feature', array('feature_id'), $values, $options);
    if (count($results) > 1) {
      watchdog('T_fasta_loader', "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" method then skip this feature 
    if ($feature and (strcmp($method, 'Insert only') == 0)) {
      watchdog('T_fasta_loader', "Feature already exists '%name' ('%uname') while matching on %type. Skipping insert.", 
      array('%name' => $name, '%uname' => $uname, '%type' => drupal_strtolower($match_type)), WATCHDOG_WARNING);
      return 0;
    }
  }

  // if we don't have a feature and we're doing an insert then do the insert
  $inserted = 0;
  if (!$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 and vice versa
    if (!$uname) {
      $uname = $name;
    }
    elseif (!$name) {
      $name = $uname;
    }

    // insert the feature
    $values = array(
      'organism_id' => $organism_id,
      'name' => $name,
      'uniquename' => $uname,
      'residues' => $residues,
      'seqlen' => drupal_strlen($residues),
      'md5checksum' => md5($residues),
      'type_id' => $cvterm->cvterm_id,
      'is_analysis' => 'FALSE',
      'is_obsolete' => 'FALSE',
    );
    $options = array('statement_name' => 'ins_feature_all');
    $success = tripal_core_chado_insert('feature', $values, $options);
    if (!$success) {
      watchdog('T_fasta_loader', "Failed to insert feature '%name (%uname)'", 
      array('%name' => $name, '%uname' => $numane), WATCHDOG_ERROR);
      return 0;
    }

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

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

  // if we do have a feature and this is an update then proceed with the update
  if ($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,
        );
        $options = array('statement_name' => 'sel_feature_oruqty');
        $results = tripal_core_chado_select('feature', array('feature_id'), $values, $options);
        if (count($results) > 0) {
          watchdog('T_fasta_loader', "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,
          'residues' => $residues,
          'seqlen' => drupal_strlen($residues),
          'md5checksum' => md5($residues),
          'is_analysis' => 'false',
          'is_obsolete' => 'false',
        );
        $match = array(
          'name' => $name,
          'organism_id' => $organism_id,
          'type_id' => $cvterm->cvterm_id,
        );
        $options = array('statement_name' => 'upd_feature_resemdisis_naorty_un');
      }
      // if we do not have a new unique name then don't change the existing uniquename field
      else {
        $values = array(
          'residues' => $residues,
          'seqlen' => drupal_strlen($residues),
          'md5checksum' => md5($residues),
          'is_analysis' => 'false',
          'is_obsolete' => 'false',
        );
        $match = array(
          'name' => $name,
          'organism_id' => $organism_id,
          'type_id' => $cvterm->cvterm_id,
        );
        $options = array('statement_name' => 'upd_feature_unresemdisis_naorty');
      }

      // perform the update
      $success = tripal_core_chado_update('feature', $match, $values, $options);
      if (!$success) {
        watchdog('T_fasta_loader', "Failed to update feature '%name' ('%name')", 
        array('%name' => $name, '%uiname' => $uname), WATCHDOG_ERROR);
        return 0;
      }
    }
    if (strcmp($match_type, 'Unique name') == 0) {
      // if we're matching on the uniquename but do not have a new name then we don't want to update the name.  
      $values = array();
      if ($name) {
        $values = array(
          'name' => $name,
          'residues' => $residues,
          'seqlen' => drupal_strlen($residues),
          'md5checksum' => md5($residues),
          'is_analysis' => 'false',
          'is_obsolete' => 'false',
        );
        $match = array(
          'uniquename' => $uname,
          'organism_id' => $organism_id,
          'type_id' => $cvterm->cvterm_id,
        );
        $options = array('statement_name' => 'upd_feature_resemdisis_unorty_na');
      }
      // if we have a unique name then update it after matching by the name
      else {
        $values = array(
          'residues' => $residues,
          'seqlen' => drupal_strlen($residues),
          'md5checksum' => md5($residues),
          'is_analysis' => 'false',
          'is_obsolete' => 'false',
        );
        $match = array(
          'uniquename' => $uname,
          'organism_id' => $organism_id,
          'type_id' => $cvterm->cvterm_id,
        );
        $options = array('statement_name' => 'upd_feature_naresemdisis_unorty');
      }
      $success = tripal_core_chado_update('feature', $match, $values, $options);
      if (!$success) {
        watchdog('T_fasta_loader', "Failed to update feature '%name' ('%name')", 
        array('%name' => $name, '%uiname' => $uname), WATCHDOG_ERROR);
        return 0;
      }
    }
  }

  // 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,
    );
    $sel_options = array('statement_name' => 'sel_analysisfeature_anfe');
    $results = tripal_core_chado_select('analysisfeature', array('analysisfeature_id'), $values, $sel_options);
    if (count($results) == 0) {
      $ins_options = array('statement_name' => 'ins_analysisfeature_anfe');
      $success = tripal_core_chado_insert('analysisfeature', $values, $ins_options);
      if (!$success) {
        watchdog('T_fasta_loader', "Failed to associate analysis and feature '%name' ('%name')", 
        array('%name' => $name, '%uname' => $uname), WATCHDOG_ERROR);
        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
    );
    $sel_options = array('statement_name' => 'sel_dbxref_dbac');
    $results = tripal_core_chado_select('dbxref', array('dbxref_id'), $values, $sel_options);
    // if the accession doesn't exist then add it
    if (count($results) == 0) {
      $ins_options = array('statement_name' => 'ins_dbxref_dbac');
      $results = tripal_core_chado_insert('dbxref', $values, $ins_options);
      if (!$results) {
        watchdog('T_fasta_loader', "Failed to add database accession '%accession'", 
        array('%accession' => $accession), WATCHDOG_ERROR);
        return 0;
      }
      $results = tripal_core_chado_select('dbxref', array('dbxref_id'), $values, $sel_options);
      if (count($results) == 1) {
        $dbxref = $results[0];
      }
      else {
        watchdog('T_fasta_loader', "Failed to retreive newly inserted dbxref '%name (%uname)'", 
        array('%name' => $name, '%uname' => $numane), WATCHDOG_ERROR);
        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
    );
    $sel_options = array('statement_name' => 'sel_featuredbxref_fedb');
    $results = tripal_core_chado_select('feature_dbxref', array('feature_dbxref_id'), $values, $sel_options);
    if (count($results) == 0) {
      $ins_options = array('statement_name' => 'ins_featuredbxref_fedb');
      $success = tripal_core_chado_insert('feature_dbxref', $values, $ins_options);
      if (!$success) {
        watchdog('T_fasta_loader', "Failed to add associate database accession '%accession' with feature", 
        array('%accession' => $accession), WATCHDOG_ERROR);
        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,
    );
    $options = array('statement_name' => 'sel_feature_oruqty');
    $results = tripal_core_chado_select('feature', array('feature_id'), $values, $options);
    if (count($results) != 1) {
      watchdog('T_fasta_loader', "Cannot find a unique fature 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,
    );
    $sel_options = array('statement_name' => 'sel_featurerelationship_suojty');
    $results = tripal_core_chado_select('feature_relationship', array('feature_relationship_id'), $values, $sel_options);
    if (count($results) == 0) {
      $ins_options = array('statement_name' => 'ins_featurerelationship_suojty');
      $success = tripal_core_chado_insert('feature_relationship', $values, $ins_options);
      if (!$success) {
        watchdog('T_fasta_loader', "Failed to add associate database accession '%accession' with feature", 
        array('%accession' => $accession), WATCHDOG_ERROR);
        return 0;
      }
    }
  }
}