function tripal_feature_delete_features

2.x tripal_feature.delete.inc tripal_feature_delete_features($organism_id, $analysis_id, $seq_type, $is_unique, $feature_names, $job = NULL)
3.x tripal_feature.delete.inc tripal_feature_delete_features($organism_id, $analysis_id, $seq_type, $is_unique, $feature_names, $job = NULL)
1.x tripal_feature-delete.inc tripal_feature_delete_features($organism_id, $analysis_id, $seq_type, $is_unique, $feature_names, $job = NULL)

Function to actually delete the features indicated

Parameters

$organism_id: (Optional) The organism_id of the features to delete

$analysis_id: (Optional) The analysis_id of the features to delete

$seq_type: (Optional) The cvterm.name of the feature types to delete

$is_unique: (Optional) A Boolean stating whether the names are unique (ie: feature.uniquename) or not (ie: feature.name)

$feature_names: (Optional) A space separated list of the names of features to delete

$job: The tripal_job id

2 string references to 'tripal_feature_delete_features'
tripal_feature_delete_form_submit in legacy/tripal_feature/includes/tripal_feature.delete.inc
Submit for the delete features form
tripal_feature_job_describe_args in legacy/tripal_feature/tripal_feature.module
Implements hook_job_describe_args() in order to describe the various feature jobs to the tripal jobs interface.

File

legacy/tripal_feature/includes/tripal_feature.delete.inc, line 116
Administration Interface for deleting multiple features

Code

function tripal_feature_delete_features($organism_id, $analysis_id, $seq_type, 
$is_unique, $feature_names, $job = NULL) {

  global $user;
  $match = array();

  // Begin the transaction.
  $transaction = db_transaction();
  print "\nNOTE: Deleting features is performed using a database transaction. \n" .
    "If the load fails or is terminated prematurely then the entire set of \n" .
    "deletions is rolled back and will not be found in the database\n\n";
  try {

    // If feature names have been provided then handle those
    if ($feature_names) {
      $names = preg_split('/\s+/', $feature_names);
      if (sizeof($names) == 1) {
        $names = $names[0];
      }
      if ($is_unique) {
        $match['uniquename'] = $names;
      }
      else {
        $match['name'] = $names;
      }
      $num_deletes = chado_select_record('feature', array('count(*) as cnt'), $match);
      print "Deleting " . $num_deletes[0]->cnt . " features\n";

      chado_delete_record('feature', $match);
      return;
    }

    // Now handle the combintation of all other inputs.
    $args = array();
    $sql = "";
    $join = '';
    $where = '';
    if ($analysis_id) {
      $join .= 'INNER JOIN {analysisfeature} AF on F.feature_id = AF.feature_id ';
      $join .= 'INNER JOIN {analysis} A on A.analysis_id = AF.analysis_id ';
      $where .= 'AND A.analysis_id = :analysis_id ';
      $args[':analysis_id'] = $analysis_id;
    }
    if ($organism_id) {
      $where .= 'AND F.organism_id = :organism_id ';
      $args[':organism_id'] = $organism_id;
    }
    if ($seq_type) {
      $join .= 'INNER JOIN {cvterm} CVT ON CVT.cvterm_id = F.type_id';
      $where .= 'AND CVT.name = :type_name';
      $args[':type_name'] = $seq_type;
    }

    // Do not perform a delete if we have no additions to the where clause
    // otherwise all features will be deleted and this is probably not what
    // is wanted.
    if (!$where) {
      throw new Exception('Cannot delete features as no filters are available');
    }
    // First, count the number of records to be deleted
    $sql = "
      SELECT count(F.feature_id)
      FROM {feature} F
        $join
      WHERE 1=1 $where
    ";
    $num_deleted = chado_query($sql, $args)->fetchField();
    // Second, delete the records.
    $sql = "
      DELETE FROM {feature} WHERE feature_id IN (
        SELECT F.feature_id
        FROM {feature} F
          $join
        WHERE 1=1 $where
      )
    ";
    chado_query($sql, $args);
    print "Deletiong completed successfully. Deleted $num_deleted feature(s).\n";

    print "Now removing orphaned feature pages\n";
    chado_cleanup_orphaned_nodes('feature');
  }
  catch (Exception $e) {
    print "\n"; // make sure we start errors on new line
    $transaction->rollback();
    print "FAILED: Rolling back database changes...\n";
    watchdog_exception('tripal_feature', $e);
    return 0;
  }
  print "\nDone\n";
}