tripal_feature_relationships.tpl.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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>This <?php print $feature->type_id->name;?> <?php print $verb ?> <?php print $rel_type ?> the following <b><?php print $obj_type ?></b> feature(s): <?php
  61. // the $headers array is an array of fields to use as the colum headers.
  62. // additional documentation can be found here
  63. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  64. $headers = array('Feature Name' ,'Unique Name', 'Species', 'Type');
  65. // the $rows array contains an array of rows where each row is an array
  66. // of values for each column of the table in that row. Additional documentation
  67. // can be found here:
  68. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  69. $rows = array();
  70. foreach ($objects as $object){
  71. // link the feature to it's node
  72. $feature_name = $object->record->object_id->name;
  73. if (property_exists($object->record, 'nid')) {
  74. $feature_name = l($feature_name, "node/" . $object->record->nid, array('attributes' => array('target' => "_blank")));
  75. }
  76. // link the organism to it's node
  77. $organism = $object->record->object_id->organism_id;
  78. $organism_name = $organism->genus ." " . $organism->species;
  79. if (property_exists($organism, 'nid')) {
  80. $organism_name = l("<i>" . $organism->genus . " " . $organism->species . "</i>", "node/" . $organism->nid, array('html' => TRUE));
  81. }
  82. $rows[] = array(
  83. array('data' => $feature_name, 'width' => '30%'),
  84. array('data' => $object->record->object_id->uniquename, 'width' => '30%'),
  85. array('data' => $organism_name, 'width' => '30%'),
  86. array('data' => $object->record->object_id->type_id->name, 'width' => '10%'),
  87. );
  88. }
  89. // the $table array contains the headers and rows array as well as other
  90. // options for controlling the display of the table. Additional
  91. // documentation can be found here:
  92. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  93. $table = array(
  94. 'header' => $headers,
  95. 'rows' => $rows,
  96. 'attributes' => array(
  97. 'id' => 'tripal_feature-table-relationship-object',
  98. 'class' => 'tripal-data-table'
  99. ),
  100. 'sticky' => FALSE,
  101. 'caption' => '',
  102. 'colgroups' => array(),
  103. 'empty' => '',
  104. );
  105. // once we have our table array structure defined, we call Drupal's theme_table()
  106. // function to generate the table.
  107. print theme_table($table); ?>
  108. </p>
  109. <br><?php
  110. }
  111. }
  112. // second add in the object relationships.
  113. foreach ($object_rels as $rel_type => $rels){
  114. foreach ($rels as $subject_type => $subjects){
  115. // Make the verb in the sentence make sense in English.
  116. switch ($rel_type) {
  117. case 'integral part of':
  118. case 'instance of':
  119. $verb = 'are an';
  120. break;
  121. case 'proper part of':
  122. case 'transformation of':
  123. case 'genome of':
  124. case 'part of':
  125. case 'position of':
  126. case 'sequence of':
  127. case 'variant of':
  128. $verb = 'are a';
  129. break;
  130. case 'derives from':
  131. case 'connects on':
  132. case 'contains':
  133. case 'finishes':
  134. case 'guides':
  135. case 'has origin':
  136. case 'has part':
  137. case 'has quality':
  138. case 'is consecutive sequence of':
  139. case 'maximally overlaps':
  140. case 'overlaps':
  141. case 'starts':
  142. $verb = '';
  143. break;
  144. default:
  145. $verb = 'are';
  146. } ?>
  147. <p>The following <b><?php print $subjects[0]->record->subject_id->type_id->name ?></b> feature(s) <?php print $verb ?> <?php print $rel_type ?> this <?php print $feature->type_id->name;?>: <?php
  148. // the $headers array is an array of fields to use as the colum headers.
  149. // additional documentation can be found here
  150. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  151. $headers = array('Feature Name' ,'Unique Name', 'Species', 'Type');
  152. // the $rows array contains an array of rows where each row is an array
  153. // of values for each column of the table in that row. Additional documentation
  154. // can be found here:
  155. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  156. $rows = array();
  157. foreach ($subjects as $subject){
  158. // link the feature to it's node
  159. $feature_name = $subject->record->subject_id->name;
  160. if (property_exists($subject->record, 'nid')) {
  161. $feature_name = l($feature_name, "node/" . $subject->record->nid, array('attributes' => array('target' => "_blank")));
  162. }
  163. // link the organism to it's node
  164. $organism = $subject->record->subject_id->organism_id;
  165. $organism_name = $organism->genus ." " . $organism->species;
  166. if (property_exists($organism, 'nid')) {
  167. $organism_name = l("<i>" . $organism->genus . " " . $organism->species . "</i>", "node/" . $organism->nid, array('html' => TRUE));
  168. }
  169. $rows[] = array(
  170. array('data' => $feature_name, 'width' => '30%'),
  171. array('data' =>$subject->record->subject_id->uniquename, 'width' => '30%'),
  172. array('data' =>$organism_name, 'width' => '30%'),
  173. array('data' =>$subject->record->subject_id->type_id->name, 'width' => '10%'),
  174. );
  175. }
  176. // the $table array contains the headers and rows array as well as other
  177. // options for controlling the display of the table. Additional
  178. // documentation can be found here:
  179. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  180. $table = array(
  181. 'header' => $headers,
  182. 'rows' => $rows,
  183. 'attributes' => array(
  184. 'id' => 'tripal_feature-table-relationship-subject',
  185. 'class' => 'tripal-data-table'
  186. ),
  187. 'sticky' => FALSE,
  188. 'caption' => '',
  189. 'colgroups' => array(),
  190. 'empty' => '',
  191. );
  192. // once we have our table array structure defined, we call Drupal's theme_table()
  193. // function to generate the table.
  194. print theme_table($table); ?>
  195. </p>
  196. <br><?php
  197. }
  198. }
  199. }