tripal_feature_relationships.tpl.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /* Typically in a Tripal template, the data needed is retrieved using a call to
  3. * chado_expand_var function. For example, to retrieve all
  4. * of the feature relationships for this node, the following function call would be made:
  5. *
  6. * $feature = chado_expand_var($feature,'table','feature_relationship');
  7. *
  8. * However, this function call can be extremely slow when there are numerous relationships.
  9. * This is because the chado_expand_var function is recursive and expands
  10. * all data following the foreign key relationships tree. Therefore, to speed retrieval
  11. * of data, a special variable is provided to this template:
  12. *
  13. * $feature->all_relationships;
  14. *
  15. * This variable is an array with two sub arrays with the keys 'object' and 'subject'. The array with
  16. * key 'object' contains relationships where the feature is the object, and the array with
  17. * the key 'subject' contains relationships where the feature is the subject
  18. */
  19. $feature = $variables['node']->feature;
  20. $all_relationships = $feature->all_relationships;
  21. $object_rels = $all_relationships['object'];
  22. $subject_rels = $all_relationships['subject'];
  23. if (count($object_rels) > 0 or count($subject_rels) > 0) { ?>
  24. <div class="tripal_feature-data-block-desc tripal-data-block-desc"></div> <?php
  25. // first add in the subject relationships.
  26. foreach ($subject_rels as $rel_type => $rels) {
  27. foreach ($rels as $obj_type => $objects) {
  28. // Make the verb in the sentence make sense in English.
  29. switch ($rel_type) {
  30. case 'integral part of':
  31. case 'instance of':
  32. $verb = 'is an';
  33. break;
  34. case 'proper part of':
  35. case 'transformation of':
  36. case 'genome of':
  37. case 'part of':
  38. case 'position of':
  39. case 'sequence of':
  40. case 'variant of':
  41. $verb = 'is a';
  42. break;
  43. case 'derives from':
  44. case 'connects on':
  45. case 'contains':
  46. case 'finishes':
  47. case 'guides':
  48. case 'has origin':
  49. case 'has part':
  50. case 'has quality':
  51. case 'is consecutive sequence of':
  52. case 'maximally overlaps':
  53. case 'overlaps':
  54. case 'starts':
  55. $verb = '';
  56. break;
  57. default:
  58. $verb = 'is';
  59. } ?>
  60. <p>
  61. This <?php print $feature->type_id->name; ?> <?php print $verb ?> <?php print $rel_type ?>
  62. the following <b><?php print $obj_type ?></b> feature(s): <?php
  63. // the $headers array is an array of fields to use as the colum headers.
  64. // additional documentation can be found here
  65. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  66. $headers = ['Feature Name', 'Unique Name', 'Species', 'Type'];
  67. // the $rows array contains an array of rows where each row is an array
  68. // of values for each column of the table in that row. Additional documentation
  69. // can be found here:
  70. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  71. $rows = [];
  72. foreach ($objects as $object) {
  73. // link the feature to it's node
  74. $feature_name = $object->record->object_id->name;
  75. if (property_exists($object->record, 'nid')) {
  76. $feature_name = l($feature_name, "node/" . $object->record->nid, ['attributes' => ['target' => "_blank"]]);
  77. }
  78. // link the organism to it's node
  79. $organism = $object->record->object_id->organism_id;
  80. $organism_name = $organism->genus . " " . $organism->species;
  81. if (property_exists($organism, 'nid')) {
  82. $organism_name = l("<i>" . $organism->genus . " " . $organism->species . "</i>", "node/" . $organism->nid, ['html' => TRUE]);
  83. }
  84. $rows[] = [
  85. ['data' => $feature_name, 'width' => '30%'],
  86. [
  87. 'data' => $object->record->object_id->uniquename,
  88. 'width' => '30%',
  89. ],
  90. ['data' => $organism_name, 'width' => '30%'],
  91. [
  92. 'data' => $object->record->object_id->type_id->name,
  93. 'width' => '10%',
  94. ],
  95. ];
  96. }
  97. // the $table array contains the headers and rows array as well as other
  98. // options for controlling the display of the table. Additional
  99. // documentation can be found here:
  100. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  101. $table = [
  102. 'header' => $headers,
  103. 'rows' => $rows,
  104. 'attributes' => [
  105. 'id' => 'tripal_feature-table-relationship-object',
  106. 'class' => 'tripal-data-table',
  107. ],
  108. 'sticky' => FALSE,
  109. 'caption' => '',
  110. 'colgroups' => [],
  111. 'empty' => '',
  112. ];
  113. // once we have our table array structure defined, we call Drupal's theme_table()
  114. // function to generate the table.
  115. print theme_table($table); ?>
  116. </p>
  117. <br><?php
  118. }
  119. }
  120. // second add in the object relationships.
  121. foreach ($object_rels as $rel_type => $rels) {
  122. foreach ($rels as $subject_type => $subjects) {
  123. // Make the verb in the sentence make sense in English.
  124. switch ($rel_type) {
  125. case 'integral part of':
  126. case 'instance of':
  127. $verb = 'are an';
  128. break;
  129. case 'proper part of':
  130. case 'transformation of':
  131. case 'genome of':
  132. case 'part of':
  133. case 'position of':
  134. case 'sequence of':
  135. case 'variant of':
  136. $verb = 'are a';
  137. break;
  138. case 'derives from':
  139. case 'connects on':
  140. case 'contains':
  141. case 'finishes':
  142. case 'guides':
  143. case 'has origin':
  144. case 'has part':
  145. case 'has quality':
  146. case 'is consecutive sequence of':
  147. case 'maximally overlaps':
  148. case 'overlaps':
  149. case 'starts':
  150. $verb = '';
  151. break;
  152. default:
  153. $verb = 'are';
  154. } ?>
  155. <p>The following
  156. <b><?php print $subjects[0]->record->subject_id->type_id->name ?></b>
  157. feature(s) <?php print $verb ?> <?php print $rel_type ?>
  158. this <?php print $feature->type_id->name; ?>: <?php
  159. // the $headers array is an array of fields to use as the colum headers.
  160. // additional documentation can be found here
  161. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  162. $headers = ['Feature Name', 'Unique Name', 'Species', 'Type'];
  163. // the $rows array contains an array of rows where each row is an array
  164. // of values for each column of the table in that row. Additional documentation
  165. // can be found here:
  166. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  167. $rows = [];
  168. foreach ($subjects as $subject) {
  169. // link the feature to it's node
  170. $feature_name = $subject->record->subject_id->name;
  171. if (property_exists($subject->record, 'nid')) {
  172. $feature_name = l($feature_name, "node/" . $subject->record->nid, ['attributes' => ['target' => "_blank"]]);
  173. }
  174. // link the organism to it's node
  175. $organism = $subject->record->subject_id->organism_id;
  176. $organism_name = $organism->genus . " " . $organism->species;
  177. if (property_exists($organism, 'nid')) {
  178. $organism_name = l("<i>" . $organism->genus . " " . $organism->species . "</i>", "node/" . $organism->nid, ['html' => TRUE]);
  179. }
  180. $rows[] = [
  181. ['data' => $feature_name, 'width' => '30%'],
  182. [
  183. 'data' => $subject->record->subject_id->uniquename,
  184. 'width' => '30%',
  185. ],
  186. ['data' => $organism_name, 'width' => '30%'],
  187. [
  188. 'data' => $subject->record->subject_id->type_id->name,
  189. 'width' => '10%',
  190. ],
  191. ];
  192. }
  193. // the $table array contains the headers and rows array as well as other
  194. // options for controlling the display of the table. Additional
  195. // documentation can be found here:
  196. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  197. $table = [
  198. 'header' => $headers,
  199. 'rows' => $rows,
  200. 'attributes' => [
  201. 'id' => 'tripal_feature-table-relationship-subject',
  202. 'class' => 'tripal-data-table',
  203. ],
  204. 'sticky' => FALSE,
  205. 'caption' => '',
  206. 'colgroups' => [],
  207. 'empty' => '',
  208. ];
  209. // once we have our table array structure defined, we call Drupal's theme_table()
  210. // function to generate the table.
  211. print theme_table($table); ?>
  212. </p>
  213. <br><?php
  214. }
  215. }
  216. }