tripal_stock_nd_phenotypes.tpl.php

  1. 2.x tripal_natural_diversity/theme/templates/tripal_stock_nd_phenotypes.tpl.php
  2. 3.x legacy/tripal_natural_diversity/theme/templates/tripal_stock_nd_phenotypes.tpl.php
1 theme call to tripal_stock_nd_phenotypes.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_phenotypes.tpl.php
View source
  1. <?php
  2. /*
  3. * Phenotype relationships with stocks are stored in the natural diversity tables.
  4. * If a stock has phenotypes associated then you can find the data by traversing
  5. * the foreign key (FK) relationships in this manner:
  6. *
  7. * stock => nd_experiment_stock => nd_experiment => nd_experiment_phenotype => phenotype.
  8. *
  9. * You can find ancillary information about data associated with a phenotype such as
  10. * a contact, pub, protocol, project, phenotype, dbxref by using the
  11. * nd_experiment.nd_experiment_id value and traversing the other FK relationships
  12. *
  13. * stock => nd_experiment_stock => nd_experiment => nd_experiment_phenotype => phenotype
  14. * => nd_experiment_phenotype => phenotype
  15. * => nd_experiment_project => project
  16. * => nd_experiment_pub => pub
  17. * => nd_experiment_contact => contact
  18. * => nd_experiment_dbxref => dbxref
  19. * => nd_experiment_protocol => protocol
  20. * => nd_experiment_stockprop
  21. * => nd_experiment_stock_dbxref
  22. *
  23. * In the FK relationships shown above, the nd_experiment_id value represents a single
  24. * experimental value that may have all of the ancilliary data associated with it.
  25. * If the phenotype record shares an nd_experiment_id with a phenotype, pub, contact,
  26. * protocol, etc then all of that data is associated with the phenotype and vice-versa.
  27. *
  28. * Techincally, we can skip including the 'nd_experiment' table when traversing the FK's
  29. * because we have the nd_experiment_id value when we get the nd_experiment_stock record.
  30. *
  31. * When lots of phenotypes are associated with a stock (e.g. thousands) then traversing
  32. * the FK relationships as described above can be very slow. Ideally, we only need to
  33. * show a small subset with a pager. Therefore, a list of nd_experiment_phenotype_id's
  34. * are provided to this template automatically within the stock object.
  35. *
  36. */
  37. // get the current stock
  38. $stock = $variables['node']->stock;
  39. // specify the number of phenotypes to show by default and the unique pager ID
  40. $num_results_per_page = 25;
  41. $stock_pager_id = 10;
  42. // the nd_experiment_phenotype IDs get passed into this template, so we use
  43. // those to iterate and show a subset via a pager. This is faster than trying
  44. // to traverse all of the FK relationship, especially when thousands of
  45. // associations may be present. Because the nd_experiment_id in Chado
  46. // can be associated with other data types it becomes slow to use the
  47. // chado_expand_var functions that we would normal use.
  48. $nd_experiment_phenotype_ids = $stock->nd_experiment_phenotype_ids;
  49. $total_records = count($nd_experiment_phenotype_ids);
  50. // initialize the Drupal pager
  51. $current_page_num = pager_default_initialize($total_records, $num_results_per_page, $stock_pager_id);
  52. $offset = $num_results_per_page * $current_page_num;
  53. $phenotypes = array();
  54. if ($total_records > 0) {
  55. // iterate through the nd_experiment_phenotype_ids and get the phenotype record
  56. for ($i = $offset ; $i < $offset + $num_results_per_page; $i++) {
  57. // expand the nd_experiment record to include the nd_experiment_phenotype table
  58. // there many be many phenotypes for a stock so we want to use a pager to limit
  59. // the results returned
  60. $options = array(
  61. 'return_array' => 1,
  62. 'include_fk' => array(
  63. 'phenotype_id' => array(
  64. 'type_id' => 1,
  65. )
  66. ),
  67. );
  68. $values = array('nd_experiment_phenotype_id' => $nd_experiment_phenotype_id);
  69. $nd_experiment_phenotype = chado_generate_var('nd_experiment_phenotype', $values);
  70. $phenotype = $nd_experiment_phenotype->phenotype_id;
  71. $phenotypes[$phenotype->phenotype_id]['phenotype'] = $phenotype;
  72. $phenotypes[$phenotype->phenotype_id]['nd_experiment_id'] = $nd_experiment_phenotype->nd_experiment_id->nd_experiment_id;
  73. }
  74. }
  75. if (count($phenotypes) > 0) {?>
  76. <div class="tripal_stock-data-block-desc tripal-data-block-desc">
  77. The following <?php print number_format($total_records) ?> phenotypes(s) have been recorded.
  78. </div><?php
  79. // the $headers array is an array of fields to use as the colum headers.
  80. // additional documentation can be found here
  81. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  82. $headers = array('Phenotypes', 'Project');
  83. // the $rows array contains an array of rows where each row is an array
  84. // of values for each column of the table in that row. Additional documentation
  85. // can be found here:
  86. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  87. $rows = array();
  88. // iterate through the nd_experiment_stock records and get
  89. // each experiment and the associated phenotypes
  90. foreach ($phenotypes as $info){
  91. $phenotype = $info['phenotype'];
  92. $nd_experiment_id = $info['nd_experiment_id'];
  93. // get the nd_experiment record
  94. $nd_experiment = chado_generate_var('nd_experiment', array('nd_experiment_id' => $nd_experiment_id));
  95. $details = '';
  96. if ($phenotype->name) {
  97. $details .= "Name: $phenotype->name<br>";
  98. }
  99. // add in the attribute type pheonotypes values are stored qualitatively or quantitatively.
  100. // If qualitatively the cvalue_id will link to a type. If quantitative we
  101. // use the value column
  102. $details .= ucwords(preg_replace('/_/', ' ', $phenotype->attr_id->name)) . ': ';
  103. if ($phenotype->cvalue_id) {
  104. $details .= ucwords(preg_replace('/_/', ' ', $phenotype->cvalue_id->name)) . '<br>';
  105. }
  106. else {
  107. $details .= $phenotype->value . '<br>';
  108. }
  109. // get the observable unit and add it to the details
  110. if ($phenotype->observable_id) {
  111. $details .= "Observable Unit: " . ucwords(preg_replace('/_/', ' ', $phenotype->observable_id->name)) . '<br>';
  112. }
  113. // get the evidence unit and add it to the details
  114. if ($phenotype->assay_id) {
  115. $details .= "Evidence: " . ucwords(preg_replace('/_/', ' ', $phenotype->assay_id->name)) . '<br>';
  116. }
  117. // Get the project for this experiment. For each nd_experiment_id there should only be one project
  118. // but the database does not constrain that there only be one project so just in case we get them all
  119. $projects = array();
  120. $values = array('nd_experiment_id' => $nd_experiment_stock->nd_experiment_id->nd_experiment_id);
  121. $nd_experiment_project = chado_generate_var('nd_experiment_project', $values, $options);
  122. $nd_experiment_projects = $nd_experiment_project;
  123. foreach ($nd_experiment_projects as $nd_experiment_project) {
  124. // we do have a project record, so add it to our $phenotypes array for display below
  125. $projects = $nd_experiment_project->project_id;
  126. }
  127. $pnames = 'N/A';
  128. foreach ($projects as $project) {
  129. $project = $project->project_id;
  130. $name = $project->name;
  131. if (property_exists($project, 'nid')) {
  132. $name = l($name, "node/" . $project->nid, array('attributes' => array('target' => '_blank')));
  133. }
  134. $pnames .= $name . '<br>';
  135. }
  136. $pnames = substr($pnames, 0, -4); // remove trailing <br>
  137. $rows[] = array(
  138. $details,
  139. $pnames,
  140. );
  141. }
  142. // the $table array contains the headers and rows array as well as other
  143. // options for controlling the display of the table. Additional
  144. // documentation can be found here:
  145. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  146. $table = array(
  147. 'header' => $headers,
  148. 'rows' => $rows,
  149. 'attributes' => array(
  150. 'id' => 'tripal_natural_diversity-table-phenotypes',
  151. 'class' => 'tripal-data-table'
  152. ),
  153. 'sticky' => FALSE,
  154. 'caption' => '',
  155. 'colgroups' => array(),
  156. 'empty' => '',
  157. );
  158. // once we have our table array structure defined, we call Drupal's theme_table()
  159. // function to generate the table.
  160. print theme_table($table);
  161. // the $pager array values that control the behavior of the pager. For
  162. // documentation on the values allows in this array see:
  163. // https://api.drupal.org/api/drupal/includes!pager.inc/function/theme_pager/7
  164. // here we add the paramter 'block' => 'features'. This is because
  165. // the pager is not on the default block that appears. When the user clicks a
  166. // page number we want the browser to re-appear with the page is loaded.
  167. // We remove the 'pane' parameter from the original query parameters because
  168. // Drupal won't reset the parameter if it already exists.
  169. $get = $_GET;
  170. unset($_GET['pane']);
  171. $pager = array(
  172. 'tags' => array(),
  173. 'element' => $stock_pager_id,
  174. 'parameters' => array(
  175. 'pane' => 'genotypes'
  176. ),
  177. 'quantity' => $num_results_per_page,
  178. );
  179. print theme_pager($pager);
  180. $_GET = $get;
  181. }