Bladeren bron

Merge branch '6.x-1.x' of git.drupal.org:sandbox/spficklin/1337878 into 6.x-1.x

Lacey Sanderson 12 jaren geleden
bovenliggende
commit
538fa53d05

+ 76 - 33
tripal_core/includes/mviews.php

@@ -166,43 +166,52 @@ function tripal_edit_mview($mview_id, $name, $modulename, $mv_table, $mv_specs,
     $mv_table = $schema_arr['table'];
   }
 
-  // Create a new record
   $record = new stdClass();
   $record->mview_id = $mview_id;
   $record->name = $name;
   $record->modulename = $modulename;
-  $record->mv_schema = $mv_schema;
-  $record->mv_table = $mv_table;
-  $record->mv_specs = $mv_specs;
-  $record->indexed = $indexed;
   $record->query = $query;
-  $record->special_index = $special_index;
   $record->last_update = 0;
   $record->status = '';
   $record->comment = $comment;
 
-  // drop the table from chado if it exists
+  // get the view before we update and check to see if the table structure has
+  // changed. IF so, then we want to drop and recreate the table. If not, then
+  // just save the updated SQL.
+  $create_table = 1;  
   $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = %d";
-  $mview = db_fetch_object(db_query($sql, $mview_id));
-  $previous_db = tripal_db_set_active('chado');  // use chado database
-  if (db_table_exists($mview->mv_table)) {
-    $sql = "DROP TABLE %s";
-    db_query($sql, $mview->mv_table);
+  $mview = db_fetch_object(db_query($sql, $mview_id));  
+  if($mview->mv_schema == $mv_schema and $mview->mv_table == $mv_table and 
+     $mview->mv_specs == $mv_specs and $mview->indexed == $indexed and 
+     $mview->special_index == $special_index) {
+    // nothing has changed so simpy update the SQL and other fields 
+    $create_table = 0;
   }
-  tripal_db_set_active($previous_db);  // now use drupal database
-
-  // update the record to the tripal_mviews table and if successful
-  // create the new materialized view in the chado schema
-  if (drupal_write_record('tripal_mviews', $record, 'mview_id')) {
-    // drop the table from chado if it exists
+  else {
+    // add in the table structure fields
+    $record->mv_schema = $mv_schema;
+    $record->mv_table = $mv_table;
+    $record->mv_specs = $mv_specs;
+    $record->indexed = $indexed;
+    $record->query = $query;
+    $record->special_index = $special_index;
+  }
+  
+  // if we are going to create the table then we must first drop it if it exists
+  if ($create_table) {
     $previous_db = tripal_db_set_active('chado');  // use chado database
-    if (db_table_exists($mv_table)) {
+    if (db_table_exists($mview->mv_table)) {
       $sql = "DROP TABLE %s";
-      db_query($sql, $mv_table);
+      db_query($sql, $mview->mv_table);
+      drupal_set_message(t("View '%name' dropped", array('%name' => $name)));
     }
     tripal_db_set_active($previous_db);  // now use drupal database
+  }
 
-    // now construct the indexes
+  // update the record to the tripal_mviews table and if successful
+  // create the new materialized view in the chado schema
+  if (drupal_write_record('tripal_mviews', $record, 'mview_id')) {
+    // construct the indexes SQL if needed
     $index = '';
     if ($indexed) {
       // add to the array of values
@@ -216,7 +225,7 @@ function tripal_edit_mview($mview_id, $name, $modulename, $mv_table, $mv_specs,
 
     // re-create the table differently depending on if it the traditional method
     // or the Drupal Schema API method
-    if ($mv_schema) {    	
+    if ($create_table and $mv_schema) {    	
       if (!tripal_core_create_custom_table($ret, $mv_table, $schema_arr, 0)) {
         drupal_set_message(t("Could not create the materialized view. Check Drupal error report logs."));
       }
@@ -224,18 +233,29 @@ function tripal_edit_mview($mview_id, $name, $modulename, $mv_table, $mv_specs,
         drupal_set_message(t("View '%name' created", array('%name' => $name)));
       }
     }
-    else {
+    if ($create_table and !$mv_schema) {
       $sql = "CREATE TABLE {$mv_table} ($mv_specs); $index";
-      $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);
       if ($results) {
-        drupal_set_message(t("View '%name' edited and saved.  All results cleared. Please re-populate the view.", array('%name' => $name)));
+        drupal_set_message(t("View '%name' created.  All records cleared. Please re-populate the view.", 
+          array('%name' => $name)));
       }
       else {
-        drupal_set_message(t("Failed to create the materialized view table: '%mv_table'", array('%mv_table' => $mv_table)), 'error');
+        drupal_set_message(t("Failed to create the materialized view table: '%mv_table'", 
+          array('%mv_table' => $mv_table)), 'error');
       }
     }
+    if (!$create_table) {
+      $message = "View '%name' updated.  All records remain. ";
+      if ($query != $mview->query) {
+        $message .= "Please repopulate the view to use updated query.";
+      }
+      drupal_set_message(t($message, array('%name' => $name)));  
+    }
+  }
+  else {
+    drupal_set_message(t("Failed to update the materialized view: '%mv_table'", 
+      array('%mv_table' => $mv_table)), 'error');  
   }
 }
 
@@ -465,8 +485,20 @@ function tripal_mviews_report() {
         'colspan' => 6),
     )
   );
-
-  $page = theme('table', $header, $rows);
+  $page = '</p>' . t("Materialized Views (MViews) are custom tables populated with a defined SQL statement.  
+    Because Chado is highly normalized and highly constrained it serves as a wonderful 
+    data storage platform, but unfortunately some queries may be slow.  MViews alleviate slowness by aggregating data
+    into tables that are more easy to query.  Use MViews to create tables for custom search pages or custom Tripal
+    module development.") . '</p>';
+  $page .= '<p><b>' . t("MViews behaves in the following way:") . '</b><ul>'.
+           '<li>' . t("The SQL statement defined for an MVIEW will be used to populate the table") . '</li>' .
+           '<li>' . t("Altering the table structure of an MView will cause the MView table to be dropped and recreated.  All records in the MView will be lost.") . '</li>' .
+           '<li>' . t("Altering the query of an existing view will not change the MView table. No records will be lost. ") . '</li>' .
+           '<li>' . t("Repopulating an MView that is already populated will result in replacement of all records.") . '</li>' .
+           '<li>' . t("A database transaction will be used when populating MViews. Therefore replacement of records does not occur until the query completes.  Any search forms or pages dependent on the MView will continue to function.") . '</li>' .
+           '</ul></p>';
+  $page .= '<b>' . t("Existing MViews") . '</b>';
+  $page .= theme('table', $header, $rows);
   return $page;
 }
 
@@ -511,7 +543,8 @@ function tripal_mviews_form(&$form_state = NULL, $mview_id = NULL) {
     $default_indexed = $form_state['values']['indexed'];
     $default_mvquery = $form_state['values']['mvquery'];
     $default_special_index = $form_state['values']['special_index'];
-    $default_comment = $form_state['values']['cpmment'];
+    $default_comment = $form_state['values']['comment'];
+    $default_modulename = $form_state['values']['modulename'];
 
     if (!$default_name) {
       $default_name = $mview->name;
@@ -537,6 +570,10 @@ function tripal_mviews_form(&$form_state = NULL, $mview_id = NULL) {
     if (!$default_schema) {
       $default_schema = $mview->mv_schema;
     }
+    if (!$default_modulename) {      
+      $default_modulename = $mview->modulename ? $mview->modulename : 'tripal_core';
+    }
+    
 
     // the mv_table column of the tripal_mviews table always has the table
     // name even if it is a custom table. However, for the sake of the form,
@@ -564,6 +601,11 @@ function tripal_mviews_form(&$form_state = NULL, $mview_id = NULL) {
     '#type' => 'value',
     '#value' => $mview_id
   );
+  
+  $form['modulename'] = array(
+    '#type' => 'value',
+    '#value' => $default_modulename,
+  );
 
   $form['name']= array(
     '#type'          => 'textfield',
@@ -743,13 +785,14 @@ function tripal_mviews_form_submit($form, &$form_state) {
   $special_index = $form_state['values']['special_index'];
   $comment = $form_state['values']['comment'];
   $schema = $form_state['values']['schema'];
+  $modulename = $form_state['values']['modulename'];
 
   if (strcmp($action, 'Edit') == 0) {
-    tripal_edit_mview($mview_id, $name, 'tripal_core', $mv_table, $mv_specs,
+    tripal_edit_mview($mview_id, $name, $modulename, $mv_table, $mv_specs,
       $indexed, $query, $special_index, $comment, $schema);
   }
   elseif (strcmp($action, 'Add') == 0) {
-    tripal_add_mview($name, 'tripal_core', $mv_table, $mv_specs,
+    tripal_add_mview($name, $modulename, $mv_table, $mv_specs,
       $indexed, $query, $special_index, $comment, $schema);
   }
   else {

+ 2 - 2
tripal_core/tripal_core.install

@@ -28,7 +28,7 @@ function tripal_core_install() {
 }
 
 /**
- *  Update for Drupal 6.x, Tripal 0.4
+ *  Update for Drupal 6.x, Tripal 1.0
  *  This update
  *   - adjusts the materialized view by adding status, comment and mv_schema columns
  *   - changes the specs of mv_table, mv_specs and indexed
@@ -37,7 +37,7 @@ function tripal_core_install() {
  * @ingroup tripal_feature
  */
 function tripal_core_update_6000() {
-  // add additional columsn to the tripal_mviews table
+  // add additional columns to the tripal_mviews table
   db_add_field($ret, 'tripal_mviews', 'status', array('type' => 'text', 'size' => 'normal', 'not NULL' => FALSE));
   db_add_field($ret, 'tripal_mviews', 'comment', array('type' => 'text', 'size' => 'normal', 'not NULL' => FALSE));
   db_add_field($ret, 'tripal_mviews', 'mv_schema', array('type' => 'text', 'size' => 'normal', 'not NULL' => FALSE));

+ 91 - 33
tripal_feature/includes/fasta_loader.inc

@@ -50,7 +50,7 @@ function tripal_feature_fasta_load_form( ) {
     '#type' => 'textfield',
     '#title' => t('Sequence Type'),
     '#required' => TRUE,
-    '#description' => t('Please enter the Sequence Ontology term that describes the sequences in the FASTA file.'),
+    '#description' => t('Please enter the Sequence Ontology (SO) term name that describes the sequences in the FASTA file (e.g. gene, mRNA, protein, etc...)'),
   );
 
 
@@ -99,18 +99,17 @@ function tripal_feature_fasta_load_form( ) {
       t('Name'),
       t('Unique name'),
     ),
-    '#description' => t('Feature data is stored in Chado with both a human-readable
-      name and a unique name. If the features in your FASTA file are identified using
+    '#description' => t('Used for "updates only" or "insert and update" methods. Not required if method type is "insert".  
+      Feature data is stored in Chado with both a human-readable
+      name and a unique name. If the features in your FASTA file are uniquely identified using
       a human-readable name then select the "Name" button. If your features are
-      identified using the unique name then select the "Unique name" button.  If you
+      uniquely identified using the unique name then select the "Unique name" button.  If you
       loaded your features first using the GFF loader then the unique name of each
       features were indicated by the "ID=" attribute and the name by the "Name=" attribute.
       By default, the FASTA loader will use the first word (character string
       before the first space) as  the name for your feature. If
       this does not uniquely identify your feature consider specifying a regular expression in the advanced section below.
-      Additionally, you may import both a name and a unique name for each sequence using the advanced options.
-      When updating a sequence, the value selected here will be used to identify the sequence in the
-      database in combination with any regular expression provided below.'),
+      Additionally, you may import both a name and a unique name for each sequence using the advanced options.'),
     '#default_value' => 1,
   );
 
@@ -484,6 +483,11 @@ function tripal_feature_load_fasta($dfile, $organism_id, $type,
     $interval = 1;
   }
   $inv_read = 0;
+  
+  // we need to get the table schema to make sure we don't overrun the 
+  // size of fields with what our regular expressions retrieve
+  $feature_tbl = tripal_core_get_chado_table_schema('feature');
+  $dbxref_tbl = tripal_core_get_chado_table_schema('dbxref');
 
   //foreach ($lines as $line_num => $line) {  
   while ($line = fgets($fh)) {
@@ -496,7 +500,7 @@ function tripal_feature_load_fasta($dfile, $organism_id, $type,
     if (preg_match('/^>/', $line)) {
       // if we have a feature name then we are starting a new sequence
       // so lets handle the previous one before moving on
-      if ($name or $uname) {
+      if ($name or $uname) {       
         tripal_feature_fasta_loader_handle_feature($name, $uname, $db_id,
           $accession, $subject, $rel_type, $parent_type, $analysis_id, $organism_id, $cvterm,
           $source, $residues, $method, $re_name, $match_type, $parentcvterm, $relcvterm);
@@ -505,26 +509,42 @@ function tripal_feature_load_fasta($dfile, $organism_id, $type,
         $uname = '';
       }
 
-      $line = preg_replace("/^>/", '', $line);
+      $line = preg_replace("/^>/", '', $line); // remove the > symbol from the defline
+     
       // get the feature name
       if ($re_name) {
         if (!preg_match("/$re_name/", $line, $matches)) {
-          print "WARNING: Regular expression for the feature name finds nothing\n";
+          watchdog('trp-fasta', "ERROR: Regular expression for the feature name finds nothing. Line %line.", array('%line' => $i), 'error');
+        }
+        elseif (strlen($matches[1]) > $feature_tbl['fields']['name']['length']) {
+          watchdog('trp-fasta', "WARNING: Regular expression retrieves a value too long for the feature name. Line %line.", array('%line' => $i), 'error');  
         }
-        $name = trim($matches[1]);
+        else {
+          $name = trim($matches[1]);
+        }        
       }
       else {
         // if the match_type is name and no regular expression was provided
         // then use the first word as the name, otherwise we don't set the name
         if (strcmp($match_type, 'Name')==0) {
-          preg_match("/^\s*(.*?)[\s\|].*$/", $line, $matches);
-          $name = trim($matches[1]);
+          if(preg_match("/^\s*(.*?)[\s\|].*$/", $line, $matches)){
+            if (strlen($matches[1]) > $feature_tbl['fields']['name']['length']) {
+              watchdog('trp-fasta', "WARNING: Regular expression retrieves a feature name too long for the feature name. Line %line.", array('%line' => $i), 'error');  
+            }
+            else {
+              $name = trim($matches[1]);
+            }
+          }
+          else {
+            watchdog('trp-fasta', "ERROR: Cannot find a feature name. Line %line.", array('%line' => $i), 'error');  
+          }
         }
       }
+      
       // get the feature unique name
       if ($re_uname) {
         if (!preg_match("/$re_uname/", $line, $matches)) {
-          print "WARNING: Regular expression for the feature unique name finds nothing\n";
+          watchdog('trp-fasta', "ERROR: Regular expression for the feature unique name finds nothing. Line %line.", array('%line' => $i), 'error');
         }
         $uname = trim($matches[1]);
       }
@@ -532,13 +552,22 @@ function tripal_feature_load_fasta($dfile, $organism_id, $type,
         // if the match_type is name and no regular expression was provided
         // then use the first word as the name, otherwise, we don't set the unqiuename
         if (strcmp($match_type, 'Unique name')==0) {
-          preg_match("/^\s*(.*?)[\s\|].*$/", $line, $matches);
-          $uname = trim($matches[1]);
+          if(preg_match("/^\s*(.*?)[\s\|].*$/", $line, $matches)){
+            $uname = trim($matches[1]);
+          }
+          else {
+            watchdog('trp-fasta', "ERROR: Cannot find a feature unique name. Line %line.", array('%line' => $i), 'error');  
+          }
         }
       }
       // get the accession
       preg_match("/$re_accession/", $line, $matches);
-      $accession = trim($matches[1]);
+      if (strlen($matches[1]) > $dbxref_tbl['fields']['accession']['length']) {
+        watchdog('trp-fasta', "WARNING: Regular expression retrieves an accession too long for the feature name. Cannot add cross reference. Line %line.", array('%line' => $i), 'warning');  
+      }
+      else {
+        $accession = trim($matches[1]);
+      }
 
       // get the relationship subject
       preg_match("/$re_subject/", $line, $matches);
@@ -552,16 +581,17 @@ function tripal_feature_load_fasta($dfile, $organism_id, $type,
         $intv_read = 0;
         $percent = sprintf("%.2f", ($num_read / $filesize) * 100);
         if ($name) {
-          print "Parsing Line $i (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes. Parsing: $name\r";
+          print "Parsing Line $i (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes. Current feature: $name\r";
         }
         else {
-          print "Parsing Line $i (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes. Parsing: $uname\r";  
+          print "Parsing Line $i (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes. Current feature: $uname\r";  
         }
         tripal_job_set_progress($job, intval(($num_read / $filesize) * 100));
       }
     }
   }
-   // now load the last sequence in the file
+  
+  // now load the last sequence in the file
   tripal_feature_fasta_loader_handle_feature($name, $uname, $db_id,
     $accession, $subject, $rel_type, $parent_type, $analysis_id, $organism_id, $cvterm,
     $source, $residues, $method, $re_name, $match_type, $parentcvterm, $relcvterm);
@@ -596,7 +626,7 @@ function tripal_feature_fasta_loader_handle_feature($name, $uname, $db_id, $acce
     } 
     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) {
@@ -615,14 +645,20 @@ function tripal_feature_fasta_loader_handle_feature($name, $uname, $db_id, $acce
     } 
     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 we have a unique name but not a name then set them to be the same and vice versa
     if (!$uname) {
       $uname = $name;
     }
@@ -666,23 +702,40 @@ function tripal_feature_fasta_loader_handle_feature($name, $uname, $db_id, $acce
       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 uesr wants to do an update then fail
+  // 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' ('%uiname') while matching on " . 
-      drupal_strtolower($match_type), array('%name' => $name, '%uiname' => $uname), WATCHDOG_ERROR);
+    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 new unique name then we don't want to update the uniquename.  
+      // 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,
@@ -698,7 +751,7 @@ function tripal_feature_fasta_loader_handle_feature($name, $uname, $db_id, $acce
         );
         $options = array('statement_name' => 'upd_feature_resemdisis_naorty_un');        
       }
-      // if we have a unique name then update it after matching by the name
+      // if we do not have a new unique name then don't change the existing uniquename field
       else {
         $values = array(                 
           'residues' => $residues,
@@ -714,6 +767,8 @@ function tripal_feature_fasta_loader_handle_feature($name, $uname, $db_id, $acce
         );
         $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')", 
@@ -812,6 +867,9 @@ function tripal_feature_fasta_loader_handle_feature($name, $uname, $db_id, $acce
           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
@@ -851,13 +909,13 @@ function tripal_feature_fasta_loader_handle_feature($name, $uname, $db_id, $acce
    // check to see if the relationship already exists if not then add it
     $values = array(
       'subject_id' => $feature->feature_id,
-      'ojbect_id' => $parent_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_relationships_id'), $values, $sel_options);
+    $results = tripal_core_chado_select('feature_relationship', array('feature_relationship_id'), $values, $sel_options);
     if (count($results) == 0) {    
-      $ins_options = array('statement_name' => 'sel_featurerelationship_suojty');
+      $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", 

+ 25 - 18
tripal_feature/includes/gff_loader.inc

@@ -224,7 +224,7 @@ function tripal_feature_load_gff3($gff_file, $organism_id, $analysis_id,
     $schema = tripal_feature_get_custom_tables('tripal_gff_temp');  
     $success = tripal_core_create_custom_table($ret, 'tripal_gff_temp', $schema['tripal_gff_temp']);
     if (!$success) {
-      watchdog('T_gff3_loader', "Cannot creat temporary loading table", array(), WATCHDOG_ERROR); 
+      watchdog('T_gff3_loader', "Cannot create temporary loading table", array(), WATCHDOG_ERROR); 
       return;
     } 
   }
@@ -554,11 +554,17 @@ function tripal_feature_load_gff3($gff_file, $organism_id, $analysis_id,
           'type_name' => $type,
           'uniquename' => $feature->uniquename
         );
-        $options = array('statement_name' => 'ins_tripalgfftemp');
-        $result = tripal_core_chado_insert('tripal_gff_temp', $values, $options);
-        if (!$result) {
-          watchdog('T_gff3_loader', "Cound not save record in temporary table, Cannot continue.", array(), WATCHDOG_ERROR);
-          return;
+        // make sure this record doesn't already exist in oru temp table
+        $options = array('statement_name' => 'sel_tripalgfftemp_all');
+        $results = tripal_core_chado_select('tripal_gff_temp', array('*'), $values, $options);
+
+        if (count($results) == 0) {
+          $options = array('statement_name' => 'ins_tripalgfftemp');
+          $result = tripal_core_chado_insert('tripal_gff_temp', $values, $options);
+          if (!$result) {
+            watchdog('T_gff3_loader', "Cound not save record in temporary table, Cannot continue.", array(), WATCHDOG_ERROR);
+            return;
+          }
         }
 
         // add/update the featureloc if the landmark and the ID are not the same
@@ -589,7 +595,7 @@ function tripal_feature_load_gff3($gff_file, $organism_id, $analysis_id,
         // add target relationships
         if (array_key_exists('Target', $tags)) {
           // format is: "target_id start end [strand]", where strand is optional and may be "+" or "-"
-          $matched = preg_match('/^(.*?)\s+(\d+)\s+(\d+)(\s+[\+|\-])*$/', $tags['Target'][0], $matches);
+          $matched = preg_match('/^(.*?)\s+(\d+)\s+(\d+)(\s+[\+|\-])*$/', trim($tags['Target'][0]), $matches);
           
           // if we have matches and the Target is in the correct format then load the alignment 
           if ($matched) {
@@ -667,18 +673,19 @@ function tripal_feature_load_gff3($gff_file, $organism_id, $analysis_id,
 
   print "\nSetting ranks of children...\n";
   
-  // get features in a parent relationship
-  $sql = "SELECT DISTINCT F.feature_id, F.organism_id, F.type_id, F.uniquename, FL.strand 
+  // get features in a relationship that are also children of an alignment
+  $sql = "SELECT DISTINCT F.feature_id, F.organism_id, F.type_id, 
+            F.uniquename, FL.strand 
           FROM tripal_gff_temp TGT 
-            INNER JOIN feature F on TGT.feature_id = F.feature_id
+            INNER JOIN feature F                ON TGT.feature_id = F.feature_id
             INNER JOIN feature_relationship FR  ON FR.object_id = TGT.feature_id
-            INNER JOIN cvterm CVT on CVT.cvterm_id = FR.type_id  
-            INNER JOIN featureloc FL on FL.feature_id = F.feature_id    
+            INNER JOIN cvterm CVT               ON CVT.cvterm_id = FR.type_id  
+            INNER JOIN featureloc FL            ON FL.feature_id = F.feature_id    
           WHERE CVT.name = 'part_of'";
   $parents = chado_query($sql);
   
   // build and prepare the SQL for selecting the children relationship
-  $sql = "SELECT FR.feature_relationship_id, FL.fmin, FR.rank
+  $sql = "SELECT DISTINCT FR.feature_relationship_id, FL.fmin, FR.rank
           FROM feature_relationship FR              
             INNER JOIN featureloc FL on FL.feature_id = FR.subject_id";
   if (!$connection) {
@@ -717,7 +724,7 @@ function tripal_feature_load_gff3($gff_file, $organism_id, $analysis_id,
        $children[] = $child;  
     }
     
-    // sort the children come in order of their fmin position
+    // the children list comes sorted in ascending fmin
     // but if the parent is on the reverse strand we need to 
     // reverse the order of the children.
     if ($parent->strand == -1) {
@@ -734,7 +741,7 @@ function tripal_feature_load_gff3($gff_file, $organism_id, $analysis_id,
       tripal_core_chado_update('feature_relationship', $match, $values, $options);
       $rank--;
     }
-    // now set the rank correctly
+    // now set the rank correctly. The rank should start at 0.
     $rank = 0;
     foreach ($children as $child) {
       $match = array('feature_relationship_id' => $child->feature_relationship_id);
@@ -1029,7 +1036,7 @@ function tripal_feature_load_gff3_ontology($feature, $dbxrefs) {
       // now look for the name without the 'DB:' prefix.
       $db = tripal_core_chado_select('db', array('db_id'), array('name' => "$dbname"), $options);
       if (sizeof($db) == 0) {
-        watchdog("T_gff3_loader", "Database, $dbname is missing for reference: $dbname:$accession", array(), WATCHDOG_WARNING);
+        watchdog("T_gff3_loader", "Database, $dbname, is not present. Cannot associate term: $dbname:$accession", array(), WATCHDOG_WARNING);
         return 0;
       }
     }
@@ -1389,7 +1396,7 @@ function tripal_feature_load_gff3_featureloc($feature, $organism, $landmark, $fm
   $r = tripal_core_chado_select('feature', array('feature_id'), $select, $options);
   
   if (count($r)==0) {
-    // so we couldn't find it using the uniquename. Let's try the 'name'.
+    // so we couldn't find the landmark using the uniquename. Let's try the 'name'.
     // if we return only a singe result then we can proceed. Otherwise give an
     // error message
     $select = array(
@@ -1399,7 +1406,7 @@ function tripal_feature_load_gff3_featureloc($feature, $organism, $landmark, $fm
     $options = array('statement_name' => 'sel_feature_organism_id_name');
     $r = tripal_core_chado_select('feature', array('feature_id'), $select, $options);
     if (count($r) == 0) {
-       watchdog("T_gff3_loader", "Cannot find landmark feature: '$landmark'.  Cannot add the feature location record", array(), WATCHDOG_WARNING);
+       watchdog("T_gff3_loader", "Cannot find landmark feature: '$landmark'.", array(), WATCHDOG_WARNING);
        return 0;
     } 
     elseif (count($r) > 1) {

+ 6 - 10
tripal_feature/includes/syncFeatures.inc

@@ -71,7 +71,7 @@ function tripal_feature_sync_form() {
   $form['organism_id'] = array(
     '#title'       => t('Organism'),
     '#type'        => t('select'),
-    '#description' => t("Choose the organism for which features set above will be synced."),
+    '#description' => t("Choose the organism for which features set above will be synced. Only organisms which also have been synced will appear in this list."),
     '#options'     => $organisms,
   );
 
@@ -284,11 +284,7 @@ function tripal_feature_sync_features($max_sync = 0, $organism_id = NULL,
  * @ingroup tripal_feature
  */
 function tripal_feature_sync_feature($feature_id) {
-  //   print "\tSyncing feature $feature_id\n";
-
-  $mem = memory_get_usage(TRUE);
-  $mb = $mem/1048576;
-  //   print "$mb mb\n";
+  //print "\tSyncing feature $feature_id\n";
 
   global $user;
   $create_node = 1;   // set to 0 if the node exists and we just sync and not create
@@ -397,16 +393,16 @@ function tripal_feature_sync_feature($feature_id) {
     // validate the node and if okay then submit
     node_validate($new_node);
     if ($errors = form_get_errors()) {
-      foreach ($errors as $key => $msg) {
-        drupal_set_message(t("%msg", array('%msg' => $msg)));
+      print "Error encountered validating new node. Cannot sync\n";
+      foreach ($errors as $key => $msg) {        
+        watchdog('trp-fsync', "%msg", array('%msg' => $msg), 'error');
       }
-      return $errors;
+      exit;
     }
     else {
       $node = node_submit($new_node);
       node_save($node);
     }
-
   }
   else {
     $node = $chado_feature;

+ 42 - 52
tripal_feature/tripal_feature.module

@@ -507,54 +507,54 @@ function chado_feature_insert($node) {
  */
 function chado_feature_update($node) {
   if ($node->revision) {
-    // TODO -- decide what to do about revisions
+    // there is no way to handle revisions in Chado but leave
+    // this here just to make not we've addressed it.
   }
-  else {
-    $residues = preg_replace("/[\n\r\s]/", "", $node->residues);
-    $obsolete = 'FALSE';
-    if ($node->is_obsolete) {
-      $obsolete = 'TRUE';
-    }
 
-    // get the feature type id
-    $values = array(
-      'cv_id' => array(
-        'name' => 'sequence'
-        ),
-      'name' => $node->feature_type
-    );
-    $type = tripal_core_chado_select('cvterm', array('cvterm_id'), $values);
+  $residues = preg_replace("/[\n\r\s]/", "", $node->residues);
+  $obsolete = 'FALSE';
+  if ($node->is_obsolete) {
+    $obsolete = 'TRUE';
+  }
+
+  // get the feature type id
+  $values = array(
+    'cv_id' => array(
+      'name' => 'sequence'
+      ),
+    'name' => $node->feature_type
+  );
+  $type = tripal_core_chado_select('cvterm', array('cvterm_id'), $values);
 
-    $feature_id = chado_get_id_for_node('feature', $node) ;
+  $feature_id = chado_get_id_for_node('feature', $node) ;
 
-    if (sizeof($type) > 0) {
-      $match = array(
-        'feature_id' => $feature_id,
-      );
-      $values = array(
-        'organism_id' => $node->organism_id,
-        'name' => $node->fname,
-        'uniquename' => $node->uniquename,
-        'residues' => $residues,
-        'seqlen' => drupal_strlen($residues),
-        'is_obsolete' => $obsolete,
-        'type_id' => $type[0]->cvterm_id,
-        'md5checksum' => md5($residues)
-      );
-        $status = tripal_core_chado_update('feature', $match, $values);
+  if (sizeof($type) > 0) {
+    $match = array(
+      'feature_id' => $feature_id,
+    );
+    $values = array(
+      'organism_id' => $node->organism_id,
+      'name' => $node->fname,
+      'uniquename' => $node->uniquename,
+      'residues' => $residues,
+      'seqlen' => drupal_strlen($residues),
+      'is_obsolete' => $obsolete,
+      'type_id' => $type[0]->cvterm_id,
+      'md5checksum' => md5($residues)
+    );
+      $status = tripal_core_chado_update('feature', $match, $values);
 
-        // add the genbank synonyms
-        chado_feature_add_synonyms($node->synonyms, $feature_id);
-    }
-    else {
-      drupal_set_message(t('Unable to update feature.'), 'warning');
-      watchdog('tripal_feature',
-      'Update feature: Unable to update feature where values: %values',
-      array('%values' => print_r($values, TRUE)),
-      WATCHDOG_WARNING
-      );
-    }
+      // add the genbank synonyms
+      chado_feature_add_synonyms($node->synonyms, $feature_id);
   }
+  else {
+    drupal_set_message(t('Unable to update feature.'), 'warning');
+    watchdog('tripal_feature',
+    'Update feature: Unable to update feature where values: %values',
+    array('%values' => print_r($values, TRUE)),
+    WATCHDOG_WARNING
+    );
+  }  
 }
 /**
  *
@@ -928,16 +928,6 @@ function chado_feature_validate($node) {
     }
   }
 
-  // we want to remove all characters except IUPAC nucleotide characters from the
-  // the residues. however, residues are not required so if blank then we'll skip
-  // this step
-  if ($node->residues) {
-    $residues = preg_replace("/[^\w]/", '', $node->residues);
-    if (!preg_match("/^[ACTGURYMKSWBDHVN]+$/i", $residues)) {
-      form_set_error('residues', t("The residues in feature $node->name contains more than the nucleotide IUPAC characters. Only the following characters are allowed: A,C,T,G,U,R,Y,M,K,S,W,B,D,H,V,N: '" . $residues . "'"));
-    }
-  }
-
   // we don't allow a genbank accession number for a contig
   if ($node->feature_type == 'contig' and $node->gbaccession) {
     form_set_error('gbaccession', t("Contigs cannot have a genbank accession number.  Please change the feature type or remove the accession number"));

+ 11 - 12
tripal_featuremap/tripal_featuremap.module

@@ -745,20 +745,19 @@ function chado_featuremap_insert($node) {
  */
 function chado_featuremap_update($node) {
   if ($node->revision) {
-    // TODO -- decide what to do about revisions
+    // there is no way to handle revisions in Chado but leave
+    // this here just to make not we've addressed it.
   }
-  else {
-    $featuremap_id = chado_get_id_for_node('featuremap', $node) ;
+  $featuremap_id = chado_get_id_for_node('featuremap', $node) ;
 
-    // update the map record
-    $match = array(
-       'featuremap_id' => $featuremap_id,
-    );
-    $values = array(
-       'name' => $node->title,
-    );
-    $status = tripal_core_chado_update('featuremap', $match, $values);
-  }
+  // update the map record
+  $match = array(
+     'featuremap_id' => $featuremap_id,
+  );
+  $values = array(
+     'name' => $node->title,
+  );
+  $status = tripal_core_chado_update('featuremap', $match, $values);  
 }
 
 

+ 25 - 25
tripal_library/tripal_library.module

@@ -281,7 +281,6 @@ function tripal_library_admin() {
   // add the field set for syncing libraries
   if (!$active_jobs) {
     get_tripal_library_admin_form_sync_set($form);
-    get_tripal_library_admin_form_cleanup_set($form);
     get_tripal_library_admin_form_reindex_set($form);
     get_tripal_library_admin_form_taxonomy_set($form);
     get_tripal_library_admin_form_cleanup_set($form);
@@ -908,34 +907,35 @@ function chado_library_insert($node) {
  */
 function chado_library_update($node) {
   if ($node->revision) {
-    // TODO -- decide what to do about revisions
+    // there is no way to handle revisions in Chado but leave
+    // this here just to make not we've addressed it.
   }
-  else {
-    $library_id = chado_get_id_for_node('library', $node) ;
+  
+  $library_id = chado_get_id_for_node('library', $node) ;
 
-    // get the library type id
-    $values = array(
-       'cv_id' => array(
-          'name' => 'tripal'
-        ),
-       'name' => $node->library_type
-    );
-    $type = tripal_core_chado_select('cvterm', array('cvterm_id'), $values);
+  // get the library type id
+  $values = array(
+     'cv_id' => array(
+        'name' => 'tripal'
+      ),
+     'name' => $node->library_type
+  );
+  $type = tripal_core_chado_select('cvterm', array('cvterm_id'), $values);
 
-    // update the library record
-    $match = array(
-       'library_id' => $library_id,
-    );
-    $values = array(
-       'name' => $node->title,
-       'uniquename' => $node->uniquename,
-       'organism_id' => $node->organism_id,
-       'type_id' => $type[0]->cvterm_id
-    );
-    $status = tripal_core_chado_update('library', $match, $values);
+  // update the library record
+  $match = array(
+     'library_id' => $library_id,
+  );
+  $values = array(
+     'name' => $node->title,
+     'uniquename' => $node->uniquename,
+     'organism_id' => $node->organism_id,
+     'type_id' => $type[0]->cvterm_id
+  );
+  $status = tripal_core_chado_update('library', $match, $values);
+
+  tripal_library_update_property($library_id, 'library_description', $node->library_description);
 
-    tripal_library_update_property($library_id, 'library_description', $node->library_description);
-  }
 }
 
 

+ 2 - 1
tripal_organism/tripal_organism.module

@@ -757,7 +757,8 @@ function chado_organism_insert($node) {
  */
 function chado_organism_update($node) {
   if ($node->revision) {
-    // TODO -- decide what to do about revisions
+    // there is no way to handle revisions in Chado but leave
+    // this here just to make not we've addressed it.
   }
   $match = array(
     'organism_id' => chado_get_id_for_node('organism', $node),

+ 4 - 1
tripal_project/tripal_project.module

@@ -249,7 +249,10 @@ function chado_project_delete($node) {
  *
  */
 function chado_project_update($node) {
-
+  if ($node->revision) {
+    // there is no way to handle revisions in Chado but leave
+    // this here just to make not we've addressed it.
+  }
   // Find the project to update
   $match= array(
     'project_id' => $node->project_id,

+ 67 - 22
tripal_stock/includes/tripal_stock-administration.inc

@@ -156,20 +156,17 @@ function tripal_stock_admin() {
     $active_jobs = TRUE;
   }
   if ($active_jobs) {
-
-  $form['notice'] = array(
+    $form['notice'] = array(
        '#type' => 'fieldset',
        '#title' => t('Stock Management Temporarily Unavailable')
     );
-
-  $form['notice']['message'] = array(
+    $form['notice']['message'] = array(
        '#value' => t("Currently, stock management jobs are waiting or ".
           "are running. Managemment features have been hidden until these ".
           "jobs complete.  Please check back later once these jobs have ".
           "finished.  You can view the status of pending jobs in the Tripal ".
           "jobs page."),
     );
-
   }
   else {
 
@@ -245,6 +242,7 @@ function tripal_stock_admin() {
       '#type' => 'submit',
       '#value' => t('Sync Stocks')
     );
+    get_tripal_stock_admin_form_cleanup_set($form);
   }
 
   return system_settings_form($form);
@@ -287,6 +285,12 @@ function tripal_stock_admin_validate($form, &$form_state) {
     variable_set('chado_stock_prop_types_cv', $form_state['values']['stock_prop_types_cv']);
     variable_set('chado_stock_relationship_cv', $form_state['values']['stock_relationship_cv']);
   }
+  
+  // Submit the Cleanup Job if selected
+  if ($form_state['values']['op'] == t('Clean up orphaned stocks')) {
+    tripal_add_job('Cleanup orphaned stocks', 'tripal_stock',
+       'tripal_stock_cleanup', $job_args, $user->uid);
+  }
 }
 
 /**
@@ -365,31 +369,72 @@ function tripal_stock_sync_stock_set($organism_id, $job_id) {
              $node->vid
            );
            */
+          }
+        }
+        else {
+          print "Not completed due to errors:\nCreate Stock Form Errors: ";
+          print_r(form_get_errors());
         }
+        print "Nid=" . $node->nid . "\n";
       }
       else {
-        print "Not completed due to errors:\nCreate Stock Form Errors: ";
-        print_r(form_get_errors());
-      }
-        print "Nid=" . $node->nid . "\n";
-    }
-    else {
-      print "Skipped $r->uniquename because it's already in drupal.\n";
-    } //end of if not already in drupal
-  } //end of while still stocks to be sync'd
+        print "Skipped $r->uniquename because it's already in drupal.\n";
+      } //end of if not already in drupal
+    } //end of while still stocks to be sync'd
   } //end of if organism_id not supplied
 
   if ($stocks_attempted == 0) {
-  print "No stocks retrieved for organism (" . $organism_id . ")\n";
-  return 1;
-  }
-  else {
-  if ($stocks_created_count > 0) {
-    print "$stocks_created_count Stocks Successfully Created\n";
+    print "No stocks retrieved for organism (" . $organism_id . ")\n";
     return 1;
   }
   else {
-    return 0;
-  }
+    if ($stocks_created_count > 0) {
+      print "$stocks_created_count Stocks Successfully Created\n";
+      return 1;
+    }
+    else {
+      return 0;
+    }
   }
 }
+
+/**
+ *
+ *
+ * @ingroup tripal_stock
+ */
+function get_tripal_stock_admin_form_cleanup_set(&$form) {
+  $form['cleanup'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Clean Up')
+  );
+  $form['cleanup']['description'] = array(
+     '#type' => 'item',
+     '#value' => t("With Drupal and Chado residing in different databases ".
+        "it is possible that nodes in Drupal and stocks in Chado become ".
+        "\"orphaned\".  This can occur if an stock node in Drupal is ".
+        "deleted but the corresponding chado stock is not and/or vice ".
+        "versa. Click the button below to resolve these discrepancies."),
+     '#weight' => 1,
+  );
+  $form['cleanup']['button'] = array(
+    '#type' => 'submit',
+    '#value' => t('Clean up orphaned stocks'),
+    '#weight' => 2,
+  );
+}
+/**
+ * Remove orphaned drupal nodes
+ *
+ * @param $dummy
+ *   Not Used -kept for backwards compatibility
+ * @param $job_id
+ *   The id of the tripal job executing this function
+ *
+ * @ingroup tripal_stock
+ */
+function tripal_stock_cleanup($dummy = NULL, $job_id = NULL) {
+
+  return tripal_core_clean_orphaned_nodes('stock', $job_id);
+  
+}

+ 79 - 77
tripal_stock/tripal_stock.module

@@ -705,103 +705,105 @@ function chado_stock_insert($node) {
 function chado_stock_update($node) {
 
   if ($node->revision) {
-    chado_stock_insert($node);
+    // there is no way to handle revisions in Chado but leave
+    // this here just to make not we've addressed it.
   }
-  else {
+//  if ($node->revision) {
+//    chado_stock_insert($node);
+//  }
+//  else {
+
+  //update dbxref
+  if ($node->database) {
+    if ($node->accession) {
+      $dbxref_mode = '';
+      $stock = tripal_core_chado_select(
+        'stock',
+        array('dbxref_id', 'type_id'),
+        array('stock_id' => $node->stock_id)
+      );
 
-    //update dbxref
-    if ($node->database) {
-      if ($node->accession) {
-        $dbxref_mode = '';
-        $stock = tripal_core_chado_select(
-          'stock',
-          array('dbxref_id', 'type_id'),
-          array('stock_id' => $node->stock_id)
+      if ($stock[0]->dbxref_id) {
+        $values = array(
+          'db_id' => $node->database,
+          'accession' => $node->accession,
+          'description' => $node->db_description
         );
-
-        if ($stock[0]->dbxref_id) {
+        $dbxref_status = tripal_core_chado_update(
+          'dbxref',
+          array('dbxref_id' => $stock[0]->dbxref_id),
+          $values
+        );
+        $dbxref_mode = 'Update';
+      }
+      else {
+        if ($stock[0]->type_id) {
+          //create the dbxref
+          //used the type_id as a control to check we have a stock but not a dbxref
           $values = array(
             'db_id' => $node->database,
             'accession' => $node->accession,
-            'description' => $node->db_description
+            'description' => $node->db_description,
+            'version' => '1',
           );
-          $dbxref_status = tripal_core_chado_update(
+          $dbxref_status = tripal_core_chado_insert(
             'dbxref',
-            array('dbxref_id' => $stock[0]->dbxref_id),
             $values
           );
-          $dbxref_mode = 'Update';
+          $dbxref_mode = 'Create';
         }
         else {
-          if ($stock[0]->type_id) {
-            //create the dbxref
-            //used the type_id as a control to check we have a stock but not a dbxref
-            $values = array(
-              'db_id' => $node->database,
-              'accession' => $node->accession,
-              'description' => $node->db_description,
-              'version' => '1',
-            );
-            $dbxref_status = tripal_core_chado_insert(
-              'dbxref',
-              $values
-            );
-            $dbxref_mode = 'Create';
-          }
-          else {
-            drupal_set_message(t('Unable to find stock to Update'), 'error');
-            watchdog(
-              'tripal_stock',
-              'Stock Update: Unable to find stock to update using values: %values',
-              array('%values', print_r($values, TRUE)),
-              WATCHDOG_ERROR
-            );
-            return FALSE;
-          }
+          drupal_set_message(t('Unable to find stock to Update'), 'error');
+          watchdog(
+            'tripal_stock',
+            'Stock Update: Unable to find stock to update using values: %values',
+            array('%values', print_r($values, TRUE)),
+            WATCHDOG_ERROR
+          );
+          return FALSE;
         }
       }
     }
+  }
 
-    if (!$dbxref_status) {
-      watchdog(
-        'tripal_stock',
-        'Stock Update: Unable to %mode main stock dbxref with values: %values',
-        array('%values' => print_r($values, TRUE), '%mode' => $dbxref_mode),
-        WATCHDOG_WARNING
-      );
-    }
-
-    //can't change stock id which is all thats stored in drupal thus only update chado
-    $update_values = array(
-      'organism_id' => $node->organism_id,
-      'name' => $node->title,
-      'uniquename' => $node->uniquename,
-      'description' => $node->stock_description,
-      'type_id' => $node->type_id,
-    );
-    if ($dbxref_status) {
-      $update_values['dbxref_id'] = array(
-        'db_id' => $node->database,
-        'accession' => $node->accession
-      );
-    }
-    $status = tripal_core_chado_update(
-      'stock',
-      array('stock_id' => $node->stock_id),
-      $update_values
+  if (!$dbxref_status) {
+    watchdog(
+      'tripal_stock',
+      'Stock Update: Unable to %mode main stock dbxref with values: %values',
+      array('%values' => print_r($values, TRUE), '%mode' => $dbxref_mode),
+      WATCHDOG_WARNING
     );
+  }
 
-    if (!$status) {
-      drupal_set_message(t('Unable to update stock'), 'error');
-      watchdog(
-        'tripal_stock',
-        'Stock Update: Unable to update stock using match values: %mvalues and update values: %uvalues',
-        array('%mvalues' => print_r(array('stock_id' => $node->stock_id), TRUE), '%uvalues' => print_r($update_values, TRUE)),
-        WATCHDOG_ERROR
-      );
-    }
+  //can't change stock id which is all thats stored in drupal thus only update chado
+  $update_values = array(
+    'organism_id' => $node->organism_id,
+    'name' => $node->title,
+    'uniquename' => $node->uniquename,
+    'description' => $node->stock_description,
+    'type_id' => $node->type_id,
+  );
+  if ($dbxref_status) {
+    $update_values['dbxref_id'] = array(
+      'db_id' => $node->database,
+      'accession' => $node->accession
+    );
   }
+  $status = tripal_core_chado_update(
+    'stock',
+    array('stock_id' => $node->stock_id),
+    $update_values
+  );
 
+  if (!$status) {
+    drupal_set_message(t('Unable to update stock'), 'error');
+    watchdog(
+      'tripal_stock',
+      'Stock Update: Unable to update stock using match values: %mvalues and update values: %uvalues',
+      array('%mvalues' => print_r(array('stock_id' => $node->stock_id), TRUE), '%uvalues' => print_r($update_values, TRUE)),
+      WATCHDOG_ERROR
+    );
+  }
 }
 
 /**

+ 1 - 1
tripal_views/tripal_views.install

@@ -79,7 +79,7 @@ function tripal_views_update_6040() {
 
   // Add tripal_views_field to keep track of fields for views integration
   $schema = tripal_views_views_schema();
-  db_create_table(&$ret, 'tripal_views_field', $schema['tripal_views_field']);
+  db_create_table($ret, 'tripal_views_field', $schema['tripal_views_field']);
 
   // Add base_table TRUE/FALSE to tripal_views
   db_add_field($ret, 'tripal_views', 'base_table', array('type' => 'int', 'not null ' => TRUE,