tripal_feature_nd_genotypes.tpl.php

  1. 2.x tripal_natural_diversity/theme/templates/tripal_feature_nd_genotypes.tpl.php
  2. 3.x legacy/tripal_natural_diversity/theme/templates/tripal_feature_nd_genotypes.tpl.php
1 theme call to tripal_feature_nd_genotypes.tpl.php
tripal_natural_diversity_node_view in tripal_natural_diversity/tripal_natural_diversity.module
Implements hook_node_view(). Acts on all content types.

File

tripal_natural_diversity/theme/templates/tripal_feature_nd_genotypes.tpl.php
View source
  1. <?php
  2. /*
  3. * Deatils about genotypes associated features can be found by traversing the foreign key (FK)
  4. * relationships in this way
  5. *
  6. * feature => feature_genotype => genotype
  7. *
  8. * There are two ways that features with genotypes can be associated with stocks. The first,
  9. * more simple method, is by traversion the FK relationships in this manner:
  10. *
  11. * Simple Method: feature => feature_genotype => genotype => stock_genotype => stock
  12. *
  13. * The second method involves use of the natural diversity (ND) tables which allows for association
  14. * or more ancilliary information. Within the ND tables, If a feature has genotypes then
  15. * you can find the corresponding stock by traversing the FK relationships
  16. * in this manner:
  17. *
  18. * ND MEthod: feature => feature_genotype => nd_experiment_genotype => nd_experiment => nd_experiment_stock => stock
  19. *
  20. * You can find ancilliary information about data associated with a genotype in the ND tables such as
  21. * a contact, pub, protocol, project, genotype, dbxref by using the
  22. * nd_experiment.nd_experiment_id value and traversing the other FK relationships
  23. *
  24. * feature => feature_genotype => nd_experiment_genotype => nd_experiment => nd_experiment_stock => stock
  25. * => nd_experiment_project => project
  26. * => nd_experiment_pub => pub
  27. * => nd_experiment_contact => contact
  28. * => nd_experiment_dbxref => dbxref
  29. * => nd_experiment_protocol => protocol
  30. * => nd_experiment_stockprop
  31. * => nd_experiment_stock_dbxref
  32. *
  33. * In the FK relationships shown above, the nd_experiment_id value represents a single
  34. * experimental value that may have all of the ancilliary data associated with it.
  35. * If the genotype record shares an nd_experiment_id with a genotype, pub, contact,
  36. * protocol, etc then all of that data is associated with the genotype and vice-versa.
  37. *
  38. * Techincally, we can skip including the 'nd_experiment' table when traversing the FK's
  39. * because we have the nd_experiment_id value when we get the nd_experiment_genotype record.
  40. *
  41. * NOTE: if the tripal_natural_diversity module is enabled this template will supercede
  42. * the tripal_feature_genotypes.tpl.php template (provided by the tripal_genetic module).
  43. * Therefore, this template must handle both cases for linking to stocks as described above
  44. */
  45. // get the current feature
  46. $feature = $variables['node']->feature;
  47. // we can have any number of genotypes for a given feature, so we want a pager.
  48. // specify the number of genotypes to show by default and the unique pager ID
  49. $num_results_per_page = 25;
  50. $feature_pager_id = 15;
  51. // get the genotypes from the feature_genotype table
  52. $options = array(
  53. 'return_array' => 1,
  54. 'pager' => array(
  55. 'limit' => $num_results_per_page,
  56. 'element' => $feature_pager_id
  57. ),
  58. );
  59. $feature = chado_expand_var($feature, 'table', 'feature_genotype', $options);
  60. $feature_genotypes = $feature->feature_genotype->feature_id;
  61. // get the total number of records
  62. $total_records = chado_pager_get_count($feature_pager_id);
  63. // now iterate through the feature genotypes and print a paged table.
  64. if (count($feature_genotypes) > 0) { ?>
  65. <div class="tripal_feature-data-block-desc tripal-data-block-desc">This following <?php print number_format($total_records) ?> genotype(s) have been recorded for this feature.</div> <?php
  66. // the $headers array is an array of fields to use as the colum headers.
  67. // additional documentation can be found here
  68. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  69. $headers = array('Name', 'Type', 'Genotype', 'Details', 'Germplasm', 'Project');
  70. // the $rows array contains an array of rows where each row is an array
  71. // of values for each column of the table in that row. Additional documentation
  72. // can be found here:
  73. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  74. $rows = array();
  75. foreach ($feature_genotypes as $feature_genotype) {
  76. $project_names = 'N/A';
  77. $stock_names = 'N/A';
  78. // get the genotype from the feature_genotype record
  79. $genotype = $feature_genotype->genotype_id;
  80. // build the name for displaying the genotype. Use the uniquename by default
  81. // unless a name exists
  82. $name = $genotype->uniquename;
  83. if ($genotype->name){
  84. $name = $genotype->name;
  85. }
  86. // build the genotype type for display
  87. $type = 'N/A';
  88. if ($genotype->type_id) {
  89. $type = ucwords(preg_replace('/_/', ' ', $genotype->type_id->name));
  90. }
  91. // build the genotype properties
  92. $options = array('return_array' => 1);
  93. $genotype = chado_expand_var($genotype, 'table', 'genotypeprop', $options);
  94. $properties = $genotype->genotypeprop;
  95. $details = '';
  96. if(count($properties) > 0) {
  97. foreach ($properties as $property){
  98. $details .= ucwords(preg_replace('/_/', ' ', $property->type_id->name)) . ': ' . $property->value . '<br>';
  99. }
  100. $details = substr($details, 0, -4); // remove trailing <br>
  101. }
  102. // get the nd_experiment_genotype records and if any
  103. $values = array('genotype_id' => $genotype->genotype_id);
  104. $nd_experiment_genotype = chado_generate_var('nd_experiment_genotype', $values);
  105. if ($nd_experiment_genotype) {
  106. $nd_experiment = $nd_experiment_genotype->nd_experiment_id;
  107. $nd_experiment_id = $nd_experiment_genotype->nd_experiment_id->nd_experiment_id;
  108. // expand the nd_experiment object to incldue the nd_experiment_stock table
  109. $values = array('nd_experiment_id' => $nd_experiment_id);
  110. $options = array(
  111. 'return_array' => 1,
  112. 'include_fk' => array(
  113. 'stock_id' => array(
  114. 'type_id' => 1
  115. )
  116. ),
  117. );
  118. $nd_experiment = chado_expand_var($nd_experiment, 'table', 'nd_experiment_stock', $options);
  119. $nd_experiment_stocks = $nd_experiment->nd_experiment_stock;
  120. if (count($nd_experiment_stocks) > 0) {
  121. $stock_names = '';
  122. foreach ($nd_experiment_stocks as $nd_experiment_stock) {
  123. $stock = $nd_experiment_stock->stock_id;
  124. $stock_name = $stock->name . ' (' . $stock->uniquename . ')';
  125. if (property_exists($stock, 'nid')) {
  126. $stock_name = l($stock_name, 'node/' . $stock->nid);
  127. }
  128. $stock_names .= $stock_name . '<br>';
  129. }
  130. $stock_names = substr($stock_names, 0, -4); // remove trailing <br>
  131. }
  132. // expand the nd_experiment object to incldue the nd_experiment_project table
  133. $values = array('nd_experiment_id' => $nd_experiment_id);
  134. $options = array('return_array' => 1);
  135. $nd_experiment = chado_expand_var($nd_experiment, 'table', 'nd_experiment_project', $options);
  136. $nd_experiment_projects = $nd_experiment->nd_experiment_project;
  137. if (count($nd_experiment_projects) > 0) {
  138. $project_names = '';
  139. foreach ($nd_experiment_projects as $nd_experiment_project) {
  140. $project = $nd_experiment_project->project_id;
  141. $project_name = $project->name;
  142. if (property_exists($project, 'nid')) {
  143. $project_name = l($project_name, "node/" . $project->nid, array('attributes' => array('target' => '_blank')));
  144. }
  145. $project_names .= $project_name . '<br>';
  146. }
  147. $project_names = substr($project_names, 0, -4); // remove trailing <br>
  148. }
  149. }
  150. $rows[] = array(
  151. $name,
  152. $type,
  153. $genotype->description,
  154. $details,
  155. $stock_names,
  156. $project_names,
  157. );
  158. }
  159. // the $table array contains the headers and rows array as well as other
  160. // options for controlling the display of the table. Additional
  161. // documentation can be found here:
  162. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  163. $table = array(
  164. 'header' => $headers,
  165. 'rows' => $rows,
  166. 'attributes' => array(
  167. 'id' => 'tripal_natural_diversity-table-genotypes',
  168. 'class' => 'tripal-data-table'
  169. ),
  170. 'sticky' => FALSE,
  171. 'caption' => '',
  172. 'colgroups' => array(),
  173. 'empty' => '',
  174. );
  175. // once we have our table array structure defined, we call Drupal's theme_table()
  176. // function to generate the table.
  177. print theme_table($table);
  178. // the $pager array values that control the behavior of the pager. For
  179. // documentation on the values allows in this array see:
  180. // https://api.drupal.org/api/drupal/includes!pager.inc/function/theme_pager/7
  181. // here we add the paramter 'block' => 'features'. This is because
  182. // the pager is not on the default block that appears. When the user clicks a
  183. // page number we want the browser to re-appear with the page is loaded.
  184. // We remove the 'pane' parameter from the original query parameters because
  185. // Drupal won't reset the parameter if it already exists.
  186. $get = $_GET;
  187. unset($_GET['pane']);
  188. $pager = array(
  189. 'tags' => array(),
  190. 'element' => $feature_pager_id,
  191. 'parameters' => array(
  192. 'pane' => 'genotypes'
  193. ),
  194. 'quantity' => $num_results_per_page,
  195. );
  196. print theme_pager($pager);
  197. $_GET = $get;
  198. }