Browse Source

Fixed issues with ND genotypes/phenotypes templates for stocks

Stephen Ficklin 11 years ago
parent
commit
6bd10c5a57

+ 1 - 1
tripal_featuremap/theme/tripal_feature/tripal_feature_featurepos.tpl.php

@@ -34,7 +34,7 @@ $feature = tripal_core_expand_chado_vars($feature, 'table', 'featurepos', $optio
 // we only want to show the map that this feature belongs to
 $map_positions = $feature->featurepos->map_feature_id;
 
-// the total number of records for the paged query is stored in a session variable
+// get the total number of records
 $total_records = chado_pager_get_count($featurepos_pager_id);
 
 

+ 1 - 1
tripal_featuremap/theme/tripal_featuremap/tripal_featuremap_featurepos.tpl.php

@@ -33,7 +33,7 @@ $featuremap = tripal_core_expand_chado_vars($featuremap, 'table', 'featurepos',
 $feature_positions = $featuremap->featurepos;
 
 
-// the total number of records for the paged query is stored in a session variable
+// get the total number of records
 $total_features = chado_pager_get_count($featurepos_pager_id);
 
 

+ 1 - 1
tripal_genetic/theme/tripal_feature/tripal_feature_genotypes.tpl.php

@@ -39,7 +39,7 @@ $options = array(
 $feature = tripal_core_expand_chado_vars($feature, 'table', 'feature_genotype', $options); 
 $feature_genotypes = $feature->feature_genotype->feature_id;
 
-// the total number of records for the paged query is stored in a session variable
+// get the total number of records
 $total_records = chado_pager_get_count($feature_pager_id);
 
 // now iterate through the feature genotypes and print a paged table.

+ 1 - 1
tripal_genetic/theme/tripal_stock/tripal_stock_genotypes.tpl.php

@@ -32,7 +32,7 @@ $options = array(
 $stock = tripal_core_expand_chado_vars($stock, 'table', 'stock_genotype', $options); 
 $stock_genotypes = $stock->stock_genotype;
 
-// the total number of records for the paged query is stored in a session variable
+// get the total number of records
 $total_records = chado_pager_get_count($stock_pager_id);
 
 // now iterate through the stock genotypes and print a paged table.

+ 1 - 1
tripal_natural_diversity/theme/tripal_feature/tripal_feature_nd_genotypes.tpl.php

@@ -62,7 +62,7 @@ $options = array(
 $feature = tripal_core_expand_chado_vars($feature, 'table', 'feature_genotype', $options);
 $feature_genotypes = $feature->feature_genotype->feature_id;
 
-// the total number of records for the paged query is stored in a session variable
+// get the total number of records
 $total_records = chado_pager_get_count($feature_pager_id);
 
 // now iterate through the feature genotypes and print a paged table.

+ 64 - 0
tripal_natural_diversity/theme/tripal_natural_diversity.theme.inc

@@ -1 +1,65 @@
 <?php
+
+/**
+ * Because the nd_experiment_id can be associated with projects, phenotypes,
+ * stocks, genotypes, etc. it becomes slow to allow the template to use the
+ * chado_expand_var function to traverse the FK relationships.  Ideally
+ * we would use the chado_expand_var function to only return a subset of 
+ * paged results.  This function queries for all of the nd_experiment_genotypes
+ * that share an nd_experiment_id with a stock
+ * 
+ * @param $variables
+ *   The list of variables that will be passed into the template
+ */
+function tripal_natural_diversity_preprocess_tripal_stock_nd_genotypes(&$variables) {
+  $stock = $variables['node']->stock;
+
+  // because the nd_experiment_id value can be used to associate projects, genotypes,
+  // phenotypes, etc, we must do an inner join to only pull out those that
+  // associate a stock to a genotype.
+  
+  $sql = "
+    SELECT NDEG.nd_experiment_genotype_id
+    FROM {nd_experiment_stock} NDES
+      INNER JOIN {nd_experiment_genotype} NDEG ON NDEG.nd_experiment_id = NDES.nd_experiment_id
+    WHERE NDES.stock_id = :stock_id
+  ";
+  $results = chado_query($sql, array(':stock_id' => $stock->stock_id));
+  $nd_exp_ids = array();
+  foreach ($results as $result) {
+    $nd_exp_ids[] = $result->nd_experiment_genotype_id;
+  }
+  $stock->nd_experiment_genotype_ids = $nd_exp_ids;
+}
+
+/**
+ * Because the nd_experiment_id can be associated with projects, phenotypes,
+ * stocks, genotypes, etc. it becomes slow to allow the template to use the
+ * chado_expand_var function to traverse the FK relationships.  Ideally
+ * we would use the chado_expand_var function to only return a subset of
+ * paged results.  This function queries for all of the nd_experiment_phenotypes
+ * that share an nd_experiment_id with a stock
+ *
+ * @param $variables
+ *   The list of variables that will be passed into the template
+ */
+function tripal_natural_diversity_preprocess_tripal_stock_nd_phenotypes(&$variables) {
+  $stock = $variables['node']->stock;
+
+  // because the nd_experiment_id value can be used to associate projects, phenotypes,
+  // phenotypes, etc, we must do an inner join to only pull out those that
+  // associate a stock to a phenotype.
+
+  $sql = "
+    SELECT NDEP.nd_experiment_phenotype_id
+    FROM {nd_experiment_stock} NDES
+      INNER JOIN {nd_experiment_phenotype} NDEP ON NDEP.nd_experiment_id = NDES.nd_experiment_id
+    WHERE NDES.stock_id = :stock_id
+  ";
+  $results = chado_query($sql, array(':stock_id' => $stock->stock_id));
+  $nd_exp_ids = array();
+  foreach ($results as $result) {
+    $nd_exp_ids[] = $result->nd_experiment_phenotype_id;
+  }
+  $stock->nd_experiment_phenotype_ids = $nd_exp_ids;
+}

+ 33 - 34
tripal_natural_diversity/theme/tripal_stock/tripal_stock_nd_genotypes.tpl.php

@@ -37,6 +37,10 @@
  * Techincally, we can skip including the 'nd_experiment' table when traversing the FK's 
  * because we have the nd_experiment_id value when we get the nd_experiment_stock record.
  * 
+ * When lots of genotypes are associated with a stock (e.g. thousands) then traversing
+ * the FK relationships as described above can be very slow. Ideally, we only need to
+ * show a small subset with a pager. Therefore, a list of nd_experiment_genotype_id's 
+ * are provided to this template automatically within the stock object.
  * 
  * NOTE: if the tripal_natural_diversity module is enabled this template will supercede 
  * the tripal_stock_genotypes.tpl.php template (provided by the tripal_genetic module).
@@ -46,27 +50,30 @@
 // get the current stock
 $stock = $variables['node']->stock;
 
+
 // specify the number of genotypes to show by default and the unique pager ID
 $num_results_per_page = 25;
 $stock_pager_id = 15;
 
-// get all of the nd_experiment_stock records for this stock.
+// the nd_experiment_genotype IDs get passed into this template, so we use
+// those to iterate and show a subset via a pager.  This is faster than trying
+// to traverse all of the FK relationship, especially when thousands of 
+// associations may be present.  Because the nd_experiment_id in Chado
+// can be associated with other data types it becomes slow to use the
+// chado_expand_var functions that we would normal use.
+$nd_experiment_genotype_ids = $stock->nd_experiment_genotype_ids;
+$total_records = count($nd_experiment_genotype_ids);
+
+// initialize the Drupal pager
+$current_page_num = pager_default_initialize($total_records, $num_results_per_page, $stock_pager_id);
+$offset = $num_results_per_page * $current_page_num;
+
 $genotypes = array();
-$options = array(
-  'return_array' => 1,
-  'include_fk' => array(
-    'nd_experiment_id' => 1
-  ),
-);
-$stock = tripal_core_expand_chado_vars($stock, 'table', 'nd_experiment_stock', $options);
-$nd_experiment_stocks = $stock->nd_experiment_stock;
-if (count($nd_experiment_stocks) > 0) {
+if ($total_records > 0) {
   
-  // iterate through the nd_experiment_stock records and look to see if there is
-  // an nd_experiment_genotype record. If so, then add it out $genotypes array
-  foreach ($nd_experiment_stocks as $nd_experiment_stock) {
-    $nd_experiment_id = $nd_experiment_stock->nd_experiment_id->nd_experiment_id;
-    $nd_experiment    = $nd_experiment_stock->nd_experiment_id;
+  // iterate through the nd_experiment_genotype_ids and get the genotype record
+  for ($i = $offset ; $i < $offset + $num_results_per_page; $i++) {
+    $nd_experiment_genotype_id = $nd_experiment_genotype_ids[$i];
       
     // expand the nd_experiment record to include the nd_experiment_genotype table
     // there many be many genotypes for a stock so we want to use a pager to limit 
@@ -78,31 +85,20 @@ if (count($nd_experiment_stocks) > 0) {
           'type_id' => 1,
         )
       ),
-      'pager' => array(
-        'limit' => $num_results_per_page,
-        'element' => $stock_pager_id
-      ),
     );
-    $nd_experiment = tripal_core_expand_chado_vars($nd_experiment, 'table', 'nd_experiment_genotype', $options);
-    $nd_experiment_genotypes = $nd_experiment->nd_experiment_genotype;
-    if ($nd_experiment_genotypes) {
-      // for each of the genotypes, add them to our $genotypes array so we can 
-      // display each one
-      foreach ($nd_experiment_genotypes as $nd_experiment_genotype) {
-        $genotype = $nd_experiment_genotype->genotype_id;
-        $genotypes[$genotype->genotype_id]['genotype'] = $genotype;
-        $genotypes[$genotype->genotype_id]['nd_experiment_id'] = $nd_experiment_id;
-      }
-    }
+    $values = array('nd_experiment_genotype_id' => $nd_experiment_genotype_id);
+    $nd_experiment_genotype = chado_generate_var('nd_experiment_genotype', $values);
+    $genotype = $nd_experiment_genotype->genotype_id;
+    $genotypes[$genotype->genotype_id]['genotype'] = $genotype;
+    $genotypes[$genotype->genotype_id]['nd_experiment_id'] = $nd_experiment_genotype->nd_experiment_id->nd_experiment_id;
   }
 }
 
-// the total number of records for the paged query is stored in a session variable
-$total_records = chado_pager_get_count($stock_pager_id);
-
 // now iterate through the feature genotypes and print a paged table.
 if (count($genotypes) > 0) { ?>
-  <div class="tripal_feature-data-block-desc tripal-data-block-desc">The following <?php print number_format($total_records) ?> genotype(s) have been recorded for this feature.</div> <?php 
+  <div class="tripal_feature-data-block-desc tripal-data-block-desc">
+    The following <?php print number_format($total_records) ?> genotype(s) have been recorded.
+  </div> <?php 
 
   // the $headers array is an array of fields to use as the colum headers.
   // additional documentation can be found here
@@ -120,6 +116,9 @@ if (count($genotypes) > 0) { ?>
     $genotype         = $info['genotype'];
     $nd_experiment_id = $info['nd_experiment_id'];
     
+    // get the nd_experiment record
+    $nd_experiment = chado_generate_var('nd_experiment', array('nd_experiment_id' => $nd_experiment_id));
+    
     // set some defaults for project and feature names
     $project_names = 'N/A';
     $feature_names = 'N/A';

+ 71 - 32
tripal_natural_diversity/theme/tripal_stock/tripal_stock_nd_phenotypes.tpl.php

@@ -7,11 +7,11 @@
  * stock => nd_experiment_stock => nd_experiment => nd_experiment_phenotype => phenotype.
  * 
  * You can find ancillary information about data associated with a phenotype such as
- * a contact, pub, protocol, project, genotype, dbxref by using the 
+ * a contact, pub, protocol, project, phenotype, dbxref by using the 
  * nd_experiment.nd_experiment_id value and traversing the other FK relationships
  * 
  * stock => nd_experiment_stock => nd_experiment => nd_experiment_phenotype => phenotype
- *                                               => nd_experiment_genotype => genotype
+ *                                               => nd_experiment_phenotype => phenotype
  *                                               => nd_experiment_project => project
  *                                               => nd_experiment_pub => pub
  *                                               => nd_experiment_contact => contact
@@ -22,51 +22,69 @@
  * 
  * In the FK relationships shown above, the nd_experiment_id value represents a single 
  * experimental value that may have all of the ancilliary data associated with it.  
- * If the phenotype record shares an nd_experiment_id with a genotype, pub, contact,
+ * If the phenotype record shares an nd_experiment_id with a phenotype, pub, contact,
  * protocol, etc then all of that data is associated with the phenotype and vice-versa.
  * 
  * Techincally, we can skip including the 'nd_experiment' table when traversing the FK's 
  * because we have the nd_experiment_id value when we get the nd_experiment_stock record.
  * 
+ * When lots of phenotypes are associated with a stock (e.g. thousands) then traversing
+ * the FK relationships as described above can be very slow. Ideally, we only need to
+ * show a small subset with a pager. Therefore, a list of nd_experiment_phenotype_id's 
+ * are provided to this template automatically within the stock object.
+ * 
  */
 
 // get the current stock
 $stock = $variables['node']->stock;
 
-// expand the stock object to include the nd_experiment_stock table
-$options = array('return_array' => 1);
-$stock = tripal_core_expand_chado_vars($stock, 'table', 'nd_experiment_stock', $options);
-$nd_experiment_stocks = $stock->nd_experiment_stock;
+// specify the number of phenotypes to show by default and the unique pager ID
+$num_results_per_page = 25;
+$stock_pager_id = 10;
+
+
+// the nd_experiment_phenotype IDs get passed into this template, so we use
+// those to iterate and show a subset via a pager.  This is faster than trying
+// to traverse all of the FK relationship, especially when thousands of 
+// associations may be present.  Because the nd_experiment_id in Chado
+// can be associated with other data types it becomes slow to use the
+// chado_expand_var functions that we would normal use.
+$nd_experiment_phenotype_ids = $stock->nd_experiment_phenotype_ids;
+$total_records = count($nd_experiment_phenotype_ids);
+
+// initialize the Drupal pager
+$current_page_num = pager_default_initialize($total_records, $num_results_per_page, $stock_pager_id);
+$offset = $num_results_per_page * $current_page_num;
+
 
-// Get the experiments to which this stock belongs that have a phenotype
-// associated.  Store those in the $phenotypes array indexed by the nd_experiment_id
 $phenotypes = array();
-if (count($nd_experiment_stocks) > 0) {
+if ($total_records > 0) {
   
-  // iterate through the nd_experiment_stock records.  there could be multiple experimetnal
-  // units (e.g. nd_experiment_id's) for this stock and we want to use only those that have
-  // phenotypes associated with them.
-  foreach ($nd_experiment_stocks as $nd_experiment_stock){
-    
-    // get the nd_experiment_id
-    $nd_experiment_id = $nd_experiment_stock->nd_experiment_id->nd_experiment_id;
-    
-    // get the nd_experiment_phenotype records for this nd_experiment_id
-    $values = array('nd_experiment_id' => $nd_experiment_id);
-    $nd_experiment_phenotypes = tripal_core_generate_chado_var('nd_experiment_phenotype', $values, $options);
-    
-    // iterate through any nd_experiment_phenotype records and add them to our array
-    if ($nd_experiment_phenotypes) {
-      foreach ($nd_experiment_phenotypes as $nd_experiment_phenotype){
-        $phenotype = $nd_experiment_phenotype->phenotype_id;
-        $phenotypes[$nd_experiment_id]['phenotype'] = $phenotype;
-      }
-    }
+  // iterate through the nd_experiment_phenotype_ids and get the phenotype record
+  for ($i = $offset ; $i < $offset + $num_results_per_page; $i++) {
+    // expand the nd_experiment record to include the nd_experiment_phenotype table
+    // there many be many phenotypes for a stock so we want to use a pager to limit 
+    // the results returned
+    $options = array(
+      'return_array' => 1,
+      'include_fk' => array(
+        'phenotype_id' => array(
+          'type_id' => 1,
+        )
+      ),
+    );
+    $values = array('nd_experiment_phenotype_id' => $nd_experiment_phenotype_id);
+    $nd_experiment_phenotype = chado_generate_var('nd_experiment_phenotype', $values);
+    $phenotype = $nd_experiment_phenotype->phenotype_id;
+    $phenotypes[$phenotype->phenotype_id]['phenotype'] = $phenotype;
+    $phenotypes[$phenotype->phenotype_id]['nd_experiment_id'] = $nd_experiment_phenotype->nd_experiment_id->nd_experiment_id;
   }
 }
 
 if (count($phenotypes) > 0) {?>
-  <div class="tripal_stock-data-block-desc tripal-data-block-desc">This following phenotypes have been recorded for this stock.</div><?php 
+  <div class="tripal_stock-data-block-desc tripal-data-block-desc">
+    The following <?php print number_format($total_records) ?> phenotypes(s) have been recorded.
+  </div><?php 
 
   // the $headers array is an array of fields to use as the colum headers.
   // additional documentation can be found here
@@ -81,8 +99,13 @@ if (count($phenotypes) > 0) {?>
   
   // iterate through the nd_experiment_stock records and get 
   // each experiment and the associated phenotypes
-  foreach ($phenotypes as $nd_experiment_id => $phenotype){
-
+  foreach ($phenotypes as $info){
+    $phenotype         = $info['phenotype'];
+    $nd_experiment_id  = $info['nd_experiment_id'];
+    
+    // get the nd_experiment record
+    $nd_experiment = chado_generate_var('nd_experiment', array('nd_experiment_id' => $nd_experiment_id));
+    
     $details = '';
 
     if ($phenotype->name) { 
@@ -154,4 +177,20 @@ if (count($phenotypes) > 0) {?>
   // once we have our table array structure defined, we call Drupal's theme_table()
   // function to generate the table.
   print theme_table($table); 
+  
+  // the $pager array values that control the behavior of the pager.  For
+  // documentation on the values allows in this array see:
+  // https://api.drupal.org/api/drupal/includes!pager.inc/function/theme_pager/7
+  // here we add the paramter 'block' => 'features'. This is because
+  // the pager is not on the default block that appears. When the user clicks a
+  // page number we want the browser to re-appear with the page is loaded.
+  $pager = array(
+    'tags' => array(),
+    'element' => $stock_pager_id,
+    'parameters' => array(
+      'block' => 'genotypes'
+    ),
+    'quantity' => $num_results_per_page,
+  );
+  print theme_pager($pager);
 }

+ 1 - 1
tripal_pub/theme/tripal_pub/tripal_pub_featuremaps.tpl.php

@@ -28,7 +28,7 @@ if (count($featuremap_pubs) > 0 ) {
   }
 }
 
-// the total number of records for the paged query is stored in a session variable
+// get the total number of records
 $total_records = chado_pager_get_count($element);
 
 if(count($featuremaps) > 0){ ?>

+ 1 - 1
tripal_pub/theme/tripal_pub/tripal_pub_features.tpl.php

@@ -28,7 +28,7 @@ if (count($feature_pubs) > 0 ) {
   }
 }
 
-// the total number of records for the paged query is stored in a session variable
+// get the total number of records
 $total_records = chado_pager_get_count($element);
 
 if(count($features) > 0){ ?>

+ 1 - 1
tripal_pub/theme/tripal_pub/tripal_pub_libraries.tpl.php

@@ -28,7 +28,7 @@ if (count($library_pubs) > 0 ) {
   }
 }
 
-// the total number of records for the paged query is stored in a session variable
+// get the total number of records
 $total_records = chado_pager_get_count($element);
 
 if(count($libraries) > 0){ ?>

+ 1 - 1
tripal_pub/theme/tripal_pub/tripal_pub_projects.tpl.php

@@ -28,7 +28,7 @@ if (count($project_pubs) > 0 ) {
   }
 }
 
-// the total number of records for the paged query is stored in a session variable
+// get the total number of records
 $total_records = chado_pager_get_count($element);
 
 if(count($projects) > 0){ ?>

+ 1 - 1
tripal_pub/theme/tripal_pub/tripal_pub_stocks.tpl.php

@@ -28,7 +28,7 @@ if (count($stock_pubs) > 0 ) {
   }
 }
 
-// the total number of records for the paged query is stored in a session variable
+// get the total number of records
 $total_records = chado_pager_get_count($element);
 
 if(count($stocks) > 0){ ?>

+ 7 - 1
tripal_stock/includes/tripal_stock.chado_node.inc

@@ -460,6 +460,9 @@ function chado_stock_validate(&$node, $form, &$form_state) {
  * @ingroup tripal_stock
  */
 function chado_stock_insert($node) {
+  
+  $node->uniquename   = trim($node->uniquename);
+  $node->sname        = trim($node->sname);
 
   // if there is an stock_id in the $node object then this must be a sync so
   // we can skip adding the stock to chado as it is already there, although
@@ -597,6 +600,9 @@ function chado_stock_insert($node) {
  * @ingroup tripal_stock
  */
 function chado_stock_update($node) {
+  
+  $node->uniquename   = trim($node->uniquename);
+  $node->sname        = trim($node->sname);
 
   if ($node->revision) {
     // there is no way to handle revisions in Chado but leave
@@ -799,7 +805,7 @@ function tripal_stock_node_presave($node) {
       $values = array('organism_id' => $organism_id);
       $organism = chado_select_record('organism', array('genus','species'), $values);
       $node->title = "$sname, $uniquename ($type) " . $organism[0]->genus . ' ' . $organism[0]->species;
-      if ($name == $uniquename) {
+      if ($sname == $uniquename) {
         $node->title = "$sname ($type) " . $organism[0]->genus . ' ' . $organism[0]->species;
       }
       break;

+ 1 - 2
tripal_stock/theme/tripal_organism/tripal_organism_stocks.tpl.php

@@ -22,8 +22,7 @@ $options = array(
 $organism = tripal_core_expand_chado_vars($organism, 'table', 'stock', $options);
 $stocks = $organism->stock;
 
-// create the pager.  
-// the total number of records for the paged query is stored in a session variable
+// get the total number of records
 $total_records = chado_pager_get_count($pager_id);