Sfoglia il codice sorgente

Made transactions optional in GFF loader. Changed all appropriate db_queryt to chado_query in feature module. Added a drush command to sync a single feature (useful for debugging/testing).

spficklin 12 anni fa
parent
commit
2a743acf56

+ 2 - 6
tripal_core/api/tripal_core.api.inc

@@ -339,9 +339,7 @@ function tripal_core_chado_insert($table, $values, $options = array()) {
   }
   // if it's not a prepared statement then insert normally
   else {
-    $previous_db = tripal_db_set_active('chado');  // use chado database
-    $result = db_query($sql, $ivalues);
-    tripal_db_set_active($previous_db);  // now use drupal database
+    $result = chado_query($sql, $ivalues);
   }
 
   // if we have a result then add primary keys to return array
@@ -693,9 +691,7 @@ function tripal_core_chado_update($table, $match, $values, $options = NULL) {
   }
   // if it's not a prepared statement then insert normally
   else {
-    $previous_db = tripal_db_set_active('chado');  // use chado database
-    $resource = db_query($sql, $uargs);
-    tripal_db_set_active($previous_db);  // now use drupal database
+    $resource = chado_query($sql, $uargs);
   }
 
   if ($resource) {

+ 9 - 2
tripal_feature/includes/gff_loader.inc

@@ -58,7 +58,7 @@ function tripal_feature_gff3_load_form() {
       the entire datset loaded prior to the failure will be rolled back and will not be available
       in the database.  If this option is unchecked and failure occurs all records up to the point
       of failure will be present in the database.'),
-    '#weight' => 2
+    '#weight' => 1
   );
   $form['import_options']['add_only']= array(
     '#type' => 'checkbox',
@@ -187,7 +187,8 @@ function tripal_feature_gff3_load_form_submit($form, &$form_state) {
   $analysis_id = $form_state['values']['analysis_id'];
   $use_transaction   = $form_state['values']['use_transaction'];
 
-  $args = array($gff_file, $organism_id, $analysis_id, $add_only, $update, $refresh, $remove, $use_transaction);
+  $args = array($gff_file, $organism_id, $analysis_id, $add_only, 
+    $update, $refresh, $remove, $use_transaction);
   $type = '';
   if ($add_only) {
     $type = 'import only new features';
@@ -245,6 +246,12 @@ function tripal_feature_load_gff3($gff_file, $organism_id, $analysis_id,
 	  	       "insertions/updates is rolled back and will not be found in the database\n\n";
 	  }
   }
+  else {
+  	$connection = tripal_db_persistent_chado();
+    if (!$connection) {
+       print "A persistant connection was not obtained. Loading will be slow\n";
+    }
+  }
 
   // check to see if the file is located local to Drupal
   $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $gff_file;

+ 11 - 18
tripal_feature/includes/syncFeatures.inc

@@ -226,10 +226,7 @@ function tripal_feature_sync_features($max_sync = 0, $organism_id = NULL,
         "ORDER BY feature_id";
 
   // get the list of features
-  $previous_db = tripal_db_set_active('chado');  // use chado database
-  $results = db_query($sql);
-
-  tripal_db_set_active($previous_db);  // now use drupal database
+  $results = chado_query($sql);
 
   // load into ids array
   $count = 0;
@@ -312,18 +309,14 @@ function tripal_feature_sync_feature($feature_id) {
          "  INNER JOIN Cvterm CVT ON F.type_id = CVT.cvterm_id ".
          "  INNER JOIN Organism O ON F.organism_id = O.organism_ID ".
          "WHERE F.feature_id = %d";
-  $previous_db = tripal_db_set_active('chado');  // use chado database
-  $feature = db_fetch_object(db_query($fsql, $feature_id));
-  tripal_db_set_active($previous_db);  // now use drupal database
+  $feature = db_fetch_object(chado_query($fsql, $feature_id));
 
   // get the synonyms for this feature
   $synsql = "SELECT S.name ".
-           "FROM {feature_synonym} FS ".
-           "  INNER JOIN {synonym} S on FS.synonym_id = S.synonym_id ".
-           "WHERE FS.feature_id = %d";
-  $previous_db = tripal_db_set_active('chado');  // use chado database
-  $synonyms = db_query($synsql, $feature_id);
-  tripal_db_set_active($previous_db);  // now use drupal database
+            "FROM {feature_synonym} FS ".
+            "  INNER JOIN {synonym} S on FS.synonym_id = S.synonym_id ".
+            "WHERE FS.feature_id = %d";
+  $synonyms = chado_query($synsql, $feature_id);
 
   // now add these synonyms to the feature object as a single string
   $synstring = '';
@@ -337,9 +330,9 @@ function tripal_feature_sync_feature($feature_id) {
   // clean up that node.  (If a node is found we don't know if it belongs to our feature or
   // not since features can have the same name/title.)
   $tsql =  "SELECT * FROM {node} N ".
-          "WHERE title = '%s'";
+           "WHERE title = '%s'";
   $cnsql = "SELECT * FROM {chado_feature} ".
-          "WHERE nid = %d";
+           "WHERE nid = %d";
   $nodes = db_query($tsql, $feature->name);
   // cycle through all nodes that may have this title
   while ($node = db_fetch_object($nodes)) {
@@ -353,10 +346,10 @@ function tripal_feature_sync_feature($feature_id) {
   // check if this feature already exists in the chado_feature table.
   // if we have a chado feature, we want to check to see if we have a node
   $cfsql = "SELECT * FROM {chado_feature} ".
-          "WHERE feature_id = %d";
+           "WHERE feature_id = %d";
   // @coder-ignore: don't need to use db_rewrite_sql() since need all nodes regardless of access control
   $nsql =  "SELECT * FROM {node} ".
-          "WHERE nid = %d";
+           "WHERE nid = %d";
   $chado_feature = db_fetch_object(db_query($cfsql, $feature->feature_id));
   if ($chado_feature) {
     drupal_set_message(t("%feature_id: A chado_feature entry exists", array('%feature_id' => $feature_id)));
@@ -386,7 +379,7 @@ function tripal_feature_sync_feature($feature_id) {
   if ($create_node) {
     // get the organism for this feature
     $sql = "SELECT * FROM {organism} WHERE organism_id = %d";
-    $organism = db_fetch_object(db_query($sql, $feature->organism_id));
+    $organism = db_fetch_object(chado_query($sql, $feature->organism_id));
 
     drupal_set_message(t("%feature_id: Creating node $feature->name", array('%feature_id' => $feature_id)));
     $new_node = new stdClass();

+ 17 - 0
tripal_feature/tripal_feature.drush.inc

@@ -45,6 +45,16 @@ function tripal_feature_drush_command() {
     ),
     'aliases' => array('trp-get-seq'),
   );
+  $items['tripal-feature-sync'] = array(
+    'description' => dt('Syncs an individual feature.'),
+    'options'   => array(
+      'id'    => dt('The feature ID of the feature to sync'),
+    ),
+    'examples' => array(
+      'Standard example' => 'drush tripal-feature-sync --id=48273',
+    ),
+    'aliases' => array('trp-fsync'),
+  );
   return $items;
 }
 
@@ -116,3 +126,10 @@ function drush_tripal_feature_tripal_get_sequence() {
     print $sequence;
   }
 }
+/*
+ * 
+ */
+function drush_tripal_feature_sync() {
+  $feature_id = drush_get_option('id');  
+	tripal_feature_sync_feature($feature_id);
+}

+ 27 - 64
tripal_feature/tripal_feature.module

@@ -572,9 +572,7 @@ function chado_feature_delete($node) {
   // Remove data from feature tables of chado database.  This will
   // cause a cascade delete and remove all data in referencing tables
   // for this feature
-  $previous_db = tripal_db_set_active('chado');
-  db_query("DELETE FROM {feature} WHERE feature_id = %d", $feature_id);
-  tripal_db_set_active($previous_db);
+  chado_query("DELETE FROM {feature} WHERE feature_id = %d", $feature_id);
 
   drupal_set_message(t("The feature and all associated data were removed from") .
   "chado");
@@ -593,18 +591,15 @@ function chado_feature_add_synonyms($synonyms, $feature_id) {
   $syn_array = array();
   $syn_array = explode(" ", $synonyms);
 
-  // use the chado database
-  $previous_db = tripal_db_set_active('chado');
 
   // remove any old synonyms
   $feature_syn_dsql = "DELETE FROM {feature_synonym} WHERE feature_id = %d";
-  if (!db_query($feature_syn_dsql, $feature_id)) {
+  if (!chado_query($feature_syn_dsql, $feature_id)) {
     $error .= "Could not remove synonyms from feature. ";
   }
 
   // return if we don't have any synonmys to add
   if (!$synonyms) {
-    tripal_db_set_active($previous_db);
     return;
   }
   // iterate through each synonym and add it to the database
@@ -618,7 +613,7 @@ function chado_feature_add_synonyms($synonyms, $feature_id) {
     // It is possible for the accession number to be present and not the feature
     $synonym_sql = "SELECT synonym_id FROM {synonym} ".
                    "WHERE name = '%s'";
-    $synonym = db_fetch_object(db_query($synonym_sql, $syn));
+    $synonym = db_fetch_object(chado_query($synonym_sql, $syn));
     if (!$synonym) {
       $synonym_isql = "INSERT INTO {synonym} (name,synonym_sgml,type_id) ".
                       "VALUES ('%s','%s', ".
@@ -626,25 +621,24 @@ function chado_feature_add_synonyms($synonyms, $feature_id) {
                       "    FROM {CVTerm} CVT ".
                       "    INNER JOIN CV ON CVT.cv_id = CV.cv_id ".
                       "    WHERE CV.name = 'feature_property' and CVT.name = 'synonym'))";
-      if (!db_query($synonym_isql, $syn, $syn)) {
+      if (!chado_query($synonym_isql, $syn, $syn)) {
         $error .= "Could not add synonym. ";
       }
       // now get the synonym we just added
       $synonym_sql = "SELECT synonym_id FROM {synonym} ".
                      "WHERE name = '%s'";
-      $synonym = db_fetch_object(db_query($synonym_sql, $syn));
+      $synonym = db_fetch_object(chado_query($synonym_sql, $syn));
     }
 
     // now add in our new sysnonym
     $feature_syn_isql = "INSERT INTO {feature_synonym} (synonym_id,feature_id,pub_id) ".
                         "VALUES (%d,%d,1)";
-    if (!db_query($feature_syn_isql, $synonym->synonym_id, $feature_id)) {
+    if (!chado_query($feature_syn_isql, $synonym->synonym_id, $feature_id)) {
       $error .= "Could not add synonyms to feature. ";
     }
   }
 
   // return to the drupal database
-  tripal_db_set_active($previous_db);
   return $error;
 
 }
@@ -656,7 +650,6 @@ function chado_feature_add_synonyms($synonyms, $feature_id) {
 function chado_feature_add_gbaccession($accession, $feature_id) {
 
   // use chado database
-  $previous_db = tripal_db_set_active('chado');
 
   // remove any old accession from genbank dbEST
   $fdbxref_dsql = "DELETE FROM {feature_dbxref} ".
@@ -665,48 +658,46 @@ function chado_feature_add_gbaccession($accession, $feature_id) {
                  "    INNER JOIN DB  ON DB.db_id = DBX.db_id ".
                  "    INNER JOIN feature_dbxref FDBX ON DBX.dbxref_id = FDBX.dbxref_id ".
                  "    WHERE DB.name = 'DB:Genbank' and FDBX.feature_id = %d)";
-  if (!db_query($fdbxref_dsql, $feature_id, $feature_id)) {
+  if (!chado_query($fdbxref_dsql, $feature_id, $feature_id)) {
     $error .= "Could not remove accession from feature. ";
   }
 
   // if we don't have an accession number to add then just return
   if (!$accession) {
-    tripal_db_set_active($previous_db);
     return;
   }
   // get the db_id
   $db_sql = "SELECT db_id FROM {DB} ".
             "WHERE name = 'DB:Genbank_est'";
-  $db = db_fetch_object(db_query($db_sql));
+  $db = db_fetch_object(chado_query($db_sql));
 
   // check to see if we have this accession number already in the database
   // if so then don't add it again. it messes up drupal if the insert fails.
   // It is possible for the accession number to be present and not the feature
   $dbxref_sql = "SELECT dbxref_id FROM {dbxref} ".
                 "WHERE db_id = %d and accession = '%s'";
-  $dbxref = db_fetch_object(db_query($dbxref_sql, $db->db_id, $accession));
+  $dbxref = db_fetch_object(chado_query($dbxref_sql, $db->db_id, $accession));
   if (!$dbxref) {
     // add the accession number
     $dbxref_isql = "INSERT INTO {dbxref} (db_id,accession) ".
                    "  VALUES (%d, '%s') ";
-    if (!db_query($dbxref_isql, $db->db_id, $accession)) {
+    if (!chado_query($dbxref_isql, $db->db_id, $accession)) {
       $error .= 'Could not add accession as a database reference ';
     }
     // get the dbxref_id for the just added accession number
     $dbxref_sql = "SELECT dbxref_id FROM {dbxref} ".
                   "WHERE db_id = %d and accession = '%s'";
-    $dbxref = db_fetch_object(db_query($dbxref_sql, $db->db_id, $accession));
+    $dbxref = db_fetch_object(chado_query($dbxref_sql, $db->db_id, $accession));
   }
 
 
   // associate the accession number with the feature
   $feature_dbxref_isql = "INSERT INTO {feature_dbxref} (feature_id,dbxref_id) ".
                          "  VALUES (%d, %d) ";
-  if (!db_query($feature_dbxref_isql, $feature_id, $dbxref->dbxref_id)) {
+  if (!chado_query($feature_dbxref_isql, $feature_id, $dbxref->dbxref_id)) {
     $error .= 'Could not add feature database reference. ';
   }
 
-  tripal_db_set_active($previous_db);
   return $error;
 }
 
@@ -821,9 +812,7 @@ function chado_feature_form($node, $param) {
   );
   // get the list of organisms
   $sql = "SELECT * FROM {Organism} ORDER BY genus, species";
-  $previous_db = tripal_db_set_active('chado');  // use chado database
-  $org_rset = db_query($sql);
-  tripal_db_set_active($previous_db);  // now use drupal database
+  $org_rset = chado_query($sql);
 
   //
   $organisms = array();
@@ -902,9 +891,7 @@ function chado_feature_validate($node) {
               INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
             WHERE uniquename = '%s'
              AND organism_id = %d AND CVT.name = '%s' AND NOT feature_id = %d";
-    $previous_db = tripal_db_set_active('chado');
-    $result = db_fetch_object(db_query($sql, $node->uniquename, $node->organism_id, $node->feature_type, $node->feature_id));
-    tripal_db_set_active($previous_db);
+    $result = db_fetch_object(chado_query($sql, $node->uniquename, $node->organism_id, $node->feature_type, $node->feature_id));
     if ($result) {
       form_set_error('uniquename', t("Feature update cannot proceed. The feature name '$node->uniquename' is not unique for this organism. Please provide a unique name for this feature."));
     }
@@ -918,9 +905,7 @@ function chado_feature_validate($node) {
               INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
             WHERE uniquename = '%s'
              AND organism_id = %d AND CVT.name = '%s'";
-    $previous_db = tripal_db_set_active('chado');
-    $result = db_fetch_object(db_query($sql, $node->uniquename, $node->organism_id, $node->feature_type));
-    tripal_db_set_active($previous_db);
+    $result = db_fetch_object(chado_query($sql, $node->uniquename, $node->organism_id, $node->feature_type));
     if ($result) {
       form_set_error('uniquename', t("Feature insert cannot proceed. The feature name '$node->uniquename' already exists for this organism. Please provide a unique name for this feature."));
     }
@@ -972,9 +957,7 @@ function chado_feature_load($node) {
 function tripal_feature_load_organism($organism_id) {
   // add organism details
   $sql = "SELECT * FROM {organism} WHERE organism_id = %d";
-  $previous_db = tripal_db_set_active('chado');  // use chado database
-  $organism = db_fetch_object(db_query($sql, $organism_id));
-  tripal_db_set_active($previous_db);  // now use drupal database
+  $organism = db_fetch_object(chado_query($sql, $organism_id));
   return $organism;
 }
 /**
@@ -990,9 +973,7 @@ function tripal_feature_load_synonyms($feature_id) {
         "    ON FS.synonym_id = S.Synonym_id ".
         "WHERE FS.feature_id = %d ".
         "ORDER BY S.name ";
-  $previous_db = tripal_db_set_active('chado');  // use chado database
-  $results = db_query($sql, $feature_id);
-  tripal_db_set_active($previous_db);  // now use drupal database
+  $results = chado_query($sql, $feature_id);
   $synonyms = array();
   $i=0;
   while ($synonym = db_fetch_object($results)) {
@@ -1017,9 +998,7 @@ function tripal_feature_load_properties($feature_id) {
            INNER JOIN {db} DB      ON DB.db_id = DBX.db_id
          WHERE FS.feature_id = %d
          ORDER BY FS.rank ASC";
-  $previous_db = tripal_db_set_active('chado');  // use chado database
-  $results = db_query($sql, $feature_id);
-  tripal_db_set_active($previous_db);  // now use drupal database
+  $results = chado_query($sql, $feature_id);
   $i=0;
   $properties = array();
   while ($property = db_fetch_object($results)) {
@@ -1042,9 +1021,7 @@ function tripal_feature_load_references($feature_id) {
         "  INNER JOIN {db} on DB.db_id = DBX.db_id ".
         "WHERE F.feature_id = %d ".
         "ORDER BY DB.name ";
-  $previous_db = tripal_db_set_active('chado');  // use chado database
-  $results = db_query($sql, $feature_id);
-  tripal_db_set_active($previous_db);  // now use drupal database
+  $results = chado_query($sql, $feature_id);
   $references = array();
   $i=0;
   while ($accession = db_fetch_object($results)) {
@@ -1081,9 +1058,7 @@ function tripal_feature_load_featurelocs($feature_id, $side = 'as_parent', $aggr
     $sql .= "WHERE FL.feature_id = %d ";
   }
 
-  $previous_db = tripal_db_set_active('chado');  // use chado database
-  $flresults = db_query($sql, $feature_id);
-  tripal_db_set_active($previous_db);  // now use drupal database
+  $flresults = chado_query($sql, $feature_id);
 
   // copy the results into an array
   $i=0;
@@ -1164,9 +1139,7 @@ function tripal_feature_load_relationships($feature_id, $side = 'as_subject') {
   $sql .= " ORDER BY FR.rank";
 
   // get the relationships
-  $previous_db = tripal_db_set_active('chado');  // use chado database
-  $results = db_query($sql, $feature_id);
-  tripal_db_set_active($previous_db);  // now use drupal database
+  $results = chado_query($sql, $feature_id);
 
 
   // iterate through the relationships, put these in an array and add
@@ -1195,9 +1168,7 @@ function tripal_feature_load_relationships($feature_id, $side = 'as_subject') {
 function tripal_feature_get_aggregate_types($feature_id) {
   // get the feature details
   $sql = 'SELECT type_id FROM {feature} WHERE feature_id = %d';
-  $previous_db = tripal_db_set_active('chado');  // use chado database
-  $feature = db_fetch_object(db_query($sql, $feature_id));
-  tripal_db_set_active($previous_db);  // now use drupal database
+  $feature = db_fetch_object(chado_query($sql, $feature_id));
 
   // check to see if this feature is of a type with an aggregate
   $sql = "SELECT * FROM {tripal_feature_relagg} WHERE type_id = %d";
@@ -1445,7 +1416,7 @@ function tripal_feature_get_matched_alignments($feature) {
           "  AND (CVT2.name = 'match' or CVT2.name like '%_match') ".
           "ORDER BY FL1.fmin";
 
-   $results = db_query($sql, $feature->feature_id, $feature->feature_id);
+   $results = chado_query($sql, $feature->feature_id, $feature->feature_id);
    
    // iterate through the results and add them to our featurelocs array
    $featurelocs = array();
@@ -1514,9 +1485,7 @@ function tripal_feature_load_organism_feature_counts($organism) {
     ORDER BY num_features desc
   ";
   $args[] = $organism->organism_id;
-  $previous_db = tripal_db_set_active('chado');  // use chado database
-  $org_features = db_query($sql, $args);
-  tripal_db_set_active($previous_db);  // now use drupal database
+  $org_features = chado_query($sql, $args);
 
   // iterate through the types
   $types = array();
@@ -2617,9 +2586,7 @@ function tripal_feature_set_taxonomy($node, $feature_id) {
         "  INNER JOIN Feature F on F.type_id = CVT.cvterm_id ".
         "  INNER JOIN Organism O ON F.organism_id = O.organism_id ".
         "WHERE F.feature_id = $feature_id";
-  $previous_db = tripal_db_set_active('chado');  // use chado database
-  $feature = db_fetch_object(db_query($sql));
-  tripal_db_set_active($previous_db);  // now use drupal database
+  $feature = db_fetch_object(chado_query($sql));
 
   // Set the feature type for this feature
   if ($do_ft && $ft_vid) {
@@ -2636,9 +2603,7 @@ function tripal_feature_set_taxonomy($node, $feature_id) {
            "FROM {Library} L ".
            "  INNER JOIN Library_feature LF ON LF.library_id = L.library_id ".
            "WHERE LF.feature_id = %d ";
-    $previous_db = tripal_db_set_active('chado');  // use chado database
-    $library = db_fetch_object(db_query($sql, $feature_id));
-    tripal_db_set_active($previous_db);  // now use drupal database
+    $library = db_fetch_object(chado_query($sql, $feature_id));
     $tags["$lb_vid"] = "$library->name";
   }
 
@@ -2654,15 +2619,13 @@ function tripal_feature_set_taxonomy($node, $feature_id) {
            "FROM {Analysis} A ".
            "  INNER JOIN Analysisfeature AF ON AF.analysis_id = A.analysis_id ".
            "WHERE AF.feature_id = $feature_id ";
-    $results = db_query($sql);
-    $previous_db = tripal_db_set_active('chado');  // use chado database
+    $results = chado_query($sql);
     $analysis_terms = array();
     while ($analysis=db_fetch_object($results)) {
       $tags2["$an_vid"] = "$analysis->name";
       $terms['tags'] = $tags2;
       taxonomy_node_save($node, $terms);
     }
-    tripal_db_set_active($previous_db);  // now use drupal database
   }
 
 }