tripal_stock_nd_genotypes.tpl.php

  1. 2.x tripal_natural_diversity/theme/templates/tripal_stock_nd_genotypes.tpl.php
  2. 3.x legacy/tripal_natural_diversity/theme/templates/tripal_stock_nd_genotypes.tpl.php
1 theme call to tripal_stock_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_stock_nd_genotypes.tpl.php
View source
  1. <?php
  2. /*
  3. * Details about genotypes associated with stocks can be found in two ways by
  4. * traversing the the foreign key (FK) relationships in these ways:
  5. *
  6. * Simple Method: stock => stock_genotype => genotype
  7. * ND Method: stock => nd_experiment_stock => nd_experiment => nd_experiment_genotype => genotype
  8. *
  9. * The tripal_genetic module handles display of genotypes when stored using the
  10. * simple method. This template handles display of genotype when stored using
  11. * the ND Method. The ND method uses the natural diversity tables and allows for
  12. * association or more ancilliary information.
  13. *
  14. *
  15. * Within the ND tables, If a stock has genotypes then you can find the corresponding
  16. * features by traversing the FK relationships in this manner:
  17. *
  18. * stock => nd_experiment_stock => nd_experiment => nd_experiment_genotype => genotype => feature_genotype => feature
  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. * stock => nd_experiment_stock => nd_experiment => nd_experiment_project => project
  25. * => nd_experiment_pub => pub
  26. * => nd_experiment_contact => contact
  27. * => nd_experiment_dbxref => dbxref
  28. * => nd_experiment_protocol => protocol
  29. * => nd_experiment_stockprop
  30. * => nd_experiment_stock_dbxref
  31. *
  32. * In the FK relationships shown above, the nd_experiment_id value represents a single
  33. * experimental unit that may have all of the ancilliary data associated with it or none.
  34. * If the genotype record shares an nd_experiment_id with a genotype, pub, contact,
  35. * protocol, etc then all of that data is associated with the genotype and vice-versa.
  36. *
  37. * Techincally, we can skip including the 'nd_experiment' table when traversing the FK's
  38. * because we have the nd_experiment_id value when we get the nd_experiment_stock record.
  39. *
  40. * When lots of genotypes are associated with a stock (e.g. thousands) then traversing
  41. * the FK relationships as described above can be very slow. Ideally, we only need to
  42. * show a small subset with a pager. Therefore, a list of nd_experiment_genotype_id's
  43. * are provided to this template automatically within the stock object.
  44. *
  45. * NOTE: if the tripal_natural_diversity module is enabled this template will supercede
  46. * the tripal_stock_genotypes.tpl.php template (provided by the tripal_genetic module).
  47. * Therefore, this template must handle both cases for linking to features as described above
  48. */
  49. // get the current stock
  50. $stock = $variables['node']->stock;
  51. // specify the number of genotypes to show by default and the unique pager ID
  52. $num_results_per_page = 25;
  53. $stock_pager_id = 15;
  54. // the nd_experiment_genotype IDs get passed into this template, so we use
  55. // those to iterate and show a subset via a pager. This is faster than trying
  56. // to traverse all of the FK relationship, especially when thousands of
  57. // associations may be present. Because the nd_experiment_id in Chado
  58. // can be associated with other data types it becomes slow to use the
  59. // chado_expand_var functions that we would normal use.
  60. $nd_experiment_genotype_ids = $stock->nd_experiment_genotype_ids;
  61. $total_records = count($nd_experiment_genotype_ids);
  62. // initialize the Drupal pager
  63. $current_page_num = pager_default_initialize($total_records, $num_results_per_page, $stock_pager_id);
  64. $offset = $num_results_per_page * $current_page_num;
  65. $genotypes = array();
  66. if ($total_records > 0) {
  67. // iterate through the nd_experiment_genotype_ids and get the genotype record
  68. for ($i = $offset ; $i < $offset + $num_results_per_page; $i++) {
  69. $nd_experiment_genotype_id = $nd_experiment_genotype_ids[$i];
  70. // expand the nd_experiment record to include the nd_experiment_genotype table
  71. // there many be many genotypes for a stock so we want to use a pager to limit
  72. // the results returned
  73. $options = array(
  74. 'return_array' => 1,
  75. 'include_fk' => array(
  76. 'genotype_id' => array(
  77. 'type_id' => 1,
  78. )
  79. ),
  80. );
  81. $values = array('nd_experiment_genotype_id' => $nd_experiment_genotype_id);
  82. $nd_experiment_genotype = chado_generate_var('nd_experiment_genotype', $values);
  83. $genotype = $nd_experiment_genotype->genotype_id;
  84. $genotypes[$genotype->genotype_id]['genotype'] = $genotype;
  85. $genotypes[$genotype->genotype_id]['nd_experiment_id'] = $nd_experiment_genotype->nd_experiment_id->nd_experiment_id;
  86. }
  87. }
  88. // now iterate through the feature genotypes and print a paged table.
  89. if (count($genotypes) > 0) { ?>
  90. <div class="tripal_feature-data-block-desc tripal-data-block-desc">
  91. The following <?php print number_format($total_records) ?> genotype(s) have been recorded.
  92. </div> <?php
  93. // the $headers array is an array of fields to use as the colum headers.
  94. // additional documentation can be found here
  95. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  96. $headers = array('Name', 'Type', 'Genotype', 'Details', 'Markers', 'Project');
  97. // the $rows array contains an array of rows where each row is an array
  98. // of values for each column of the table in that row. Additional documentation
  99. // can be found here:
  100. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  101. $rows = array();
  102. // iterate through the genotypes and build each row of the resulting table
  103. foreach ($genotypes as $info) {
  104. $genotype = $info['genotype'];
  105. $nd_experiment_id = $info['nd_experiment_id'];
  106. // get the nd_experiment record
  107. $nd_experiment = chado_generate_var('nd_experiment', array('nd_experiment_id' => $nd_experiment_id));
  108. // set some defaults for project and feature names
  109. $project_names = 'N/A';
  110. $feature_names = 'N/A';
  111. // build the name for displaying the genotype. Use the uniquename by default
  112. // unless a name exists
  113. $name = $genotype->uniquename;
  114. if ($genotype->name){
  115. $name = $genotype->name;
  116. }
  117. // build the genotype type for display
  118. $type = 'N/A';
  119. if ($genotype->type_id) {
  120. $type = ucwords(preg_replace('/_/', ' ', $genotype->type_id->name));
  121. }
  122. // build the genotype properties
  123. $options = array('return_array' => 1);
  124. $genotype = chado_expand_var($genotype, 'table', 'genotypeprop', $options);
  125. $properties = $genotype->genotypeprop;
  126. $details = '';
  127. if(count($properties) > 0) {
  128. foreach ($properties as $property){
  129. $details .= ucwords(preg_replace('/_/', ' ', $property->type_id->name)) . ': ' . $property->value . '<br>';
  130. }
  131. $details = substr($details, 0, -4); // remove trailing <br>
  132. }
  133. // get the features as found in the feature_genotype table and if any, add them to the $features array
  134. // we will later add in the features list for display
  135. $features = array();
  136. $options = array(
  137. 'return_array' => 1,
  138. 'include_fk' => array(
  139. 'feature_id' => array(
  140. 'type_id' => 1
  141. )
  142. ),
  143. );
  144. $genotype = chado_expand_var($genotype, 'table', 'feature_genotype', $options);
  145. $feature_genotypes = $genotype->feature_genotype;
  146. if (count($feature_genotypes) > 0) {
  147. $feature_names = '';
  148. foreach ($feature_genotypes as $feature_genotype) {
  149. $feature = $feature_genotype->feature_id;
  150. $feature_name = $feature->name . ' (' . $feature->uniquename . ')';
  151. if (property_exists($feature, 'nid')) {
  152. $feature_name = l($feature_name, 'node/' . $feature->nid);
  153. }
  154. $feature_names .= $feature_name . '<br>';
  155. }
  156. $feature_names = substr($feature_names, 0, -4); // remove trailing <br>
  157. }
  158. // expand the nd_experiment object to incldue the nd_experiment_project table
  159. $values = array('nd_experiment_id' => $nd_experiment_id);
  160. $options = array('return_array' => 1);
  161. $nd_experiment = chado_expand_var($nd_experiment, 'table', 'nd_experiment_project', $options);
  162. $nd_experiment_projects = $nd_experiment->nd_experiment_project;
  163. if (count($nd_experiment_projects) > 0) {
  164. $project_names = '';
  165. foreach ($nd_experiment_projects as $nd_experiment_project) {
  166. $project = $nd_experiment_project->project_id;
  167. $project_name = $project->name;
  168. if (property_exists($project, 'nid')) {
  169. $project_name = l($project_name, "node/" . $project->nid, array('attributes' => array('target' => '_blank')));
  170. }
  171. $project_names .= $project_name . '<br>';
  172. }
  173. $project_names = substr($project_names, 0, -4); // remove trailing <br>
  174. }
  175. $rows[] = array(
  176. $name,
  177. $type,
  178. $genotype->description,
  179. $details,
  180. $feature_names,
  181. $project_names,
  182. );
  183. }
  184. // the $table array contains the headers and rows array as well as other
  185. // options for controlling the display of the table. Additional
  186. // documentation can be found here:
  187. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  188. $table = array(
  189. 'header' => $headers,
  190. 'rows' => $rows,
  191. 'attributes' => array(
  192. 'id' => 'tripal_natural_diversity-table-genotypes',
  193. 'class' => 'tripal-data-table'
  194. ),
  195. 'sticky' => FALSE,
  196. 'caption' => '',
  197. 'colgroups' => array(),
  198. 'empty' => '',
  199. );
  200. // once we have our table array structure defined, we call Drupal's theme_table()
  201. // function to generate the table.
  202. print theme_table($table);
  203. // the $pager array values that control the behavior of the pager. For
  204. // documentation on the values allows in this array see:
  205. // https://api.drupal.org/api/drupal/includes!pager.inc/function/theme_pager/7
  206. // here we add the paramter 'block' => 'features'. This is because
  207. // the pager is not on the default block that appears. When the user clicks a
  208. // page number we want the browser to re-appear with the page is loaded.
  209. // We remove the 'pane' parameter from the original query parameters because
  210. // Drupal won't reset the parameter if it already exists.
  211. $get = $_GET;
  212. unset($_GET['pane']);
  213. $pager = array(
  214. 'tags' => array(),
  215. 'element' => $stock_pager_id,
  216. 'parameters' => array(
  217. 'pane' => 'genotypes'
  218. ),
  219. 'quantity' => $num_results_per_page,
  220. );
  221. print theme_pager($pager);
  222. $_GET = $get;
  223. }