tripal_views_handler_field_sequence.inc

  1. 2.x tripal_views/views/handlers/tripal_views_handler_field_sequence.inc
  2. 3.x tripal_chado_views/views/handlers/tripal_views_handler_field_sequence.inc
  3. 1.x tripal_views/views/handlers/tripal_views_handler_field_sequence.inc

A chado wrapper for the views_handler_field.

Handles display of sequence data. If will aggregate sequences that need to be aggregated (e.g. coding sequences) and provide

File

tripal_views/views/handlers/tripal_views_handler_field_sequence.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * A chado wrapper for the views_handler_field.
  5. *
  6. * Handles display of sequence data. If will aggregate sequences that need
  7. * to be aggregated (e.g. coding sequences) and provide
  8. */
  9. class tripal_views_handler_field_sequence extends chado_views_handler_field {
  10. function init(&$view, $options) {
  11. parent::init($view, $options);
  12. }
  13. /**
  14. * Defines the options form (form available to admin when they add a field to a view)
  15. */
  16. function options_form(&$form, &$form_state) {
  17. parent::options_form($form, $form_state);
  18. $form['display'] = array(
  19. '#type' => 'fieldset',
  20. '#title' => 'Format Output',
  21. '#description' => t('Alter the way a sequence is displayed')
  22. );
  23. $default_num_bases_per_line = '50';
  24. if ($this->options['display']['num_bases_per_line']) {
  25. $default_num_bases_per_line = $this->options['display']['num_bases_per_line'];
  26. }
  27. $default_output_format = 'raw';
  28. if ($this->options['display']['output_format']) {
  29. $default_ouput_format = $this->options['display']['output_format'];
  30. }
  31. $form['display']['num_bases_per_line'] = array(
  32. '#type' => 'textfield',
  33. '#title' => t('Number of bases per line'),
  34. '#description' => t('Specify the number of bases per line. An HTML <br> tag ' .
  35. 'will be inserted after the number of bases indicated. If no value is ' .
  36. 'provided. The sequence will be one long string (default)'),
  37. '#default_value' => $default_num_bases_per_line,
  38. );
  39. $form['display']['derive_from_parent'] = array(
  40. '#type' => 'checkbox',
  41. '#title' => t('Derive sequence from parent'),
  42. '#description' => t('Rather than use the sequence from the \'residues\' of this feature, you may ' .
  43. 'derive the sequence from the parent features to which it is aligned. This is useful in the case that the feature ' .
  44. 'does not have sequence associated with it and we need to get it through it\'s alignment. ' .
  45. 'Note: this will slow queries with large numbers of results on the page.'),
  46. '#default_value' => $this->options['display']['derive_from_parent'],
  47. );
  48. $form['display']['aggregate'] = array(
  49. '#type' => 'checkbox',
  50. '#title' => t('Aggregate sub features'),
  51. '#description' => t('If the feature has sub features (e.g. CDS of an mRNA) then check this '.
  52. 'box to filter the sequence to only include the sub features. Gaps between sub features will be '.
  53. 'excluded from the sequence. This is useful for obtaining a complete CDS from an mRNA '.
  54. 'without intronic sequence'),
  55. '#default_value' => $this->options['display']['aggregate'],
  56. );
  57. $form['display']['output_format'] = array(
  58. '#type' => 'radios',
  59. '#title' => t('Output format'),
  60. '#options' => array(
  61. 'raw' => 'Raw sequence data (no formatting)',
  62. 'fasta_html' => 'FASTA in HTML format',
  63. 'fasta_txt' => 'FASTA in text format',
  64. ),
  65. '#description' => t('Choose an output format. Raw output cannot be used when the sequence is derived from the parent.'),
  66. '#default_value' => $default_ouput_format,
  67. );
  68. }
  69. /**
  70. * We need to add a few fields to our query
  71. */
  72. function query() {
  73. parent::query();
  74. // if we are going to get the sequence from the parent then
  75. // we will need to do more queries in the render function
  76. // and we must have the feature_id to do those
  77. if ($this->options['display']['derive_from_parent']) {
  78. $this->ensure_my_table();
  79. $this->query->add_field($this->table, 'feature_id');
  80. $this->query->add_field($this->table, 'name');
  81. }
  82. }
  83. /**
  84. * Prior to display of results we want to format the sequence
  85. */
  86. function render($values) {
  87. $residues = '';
  88. // get the number of bases to show per line
  89. $num_bases_per_line = $this->options['display']['num_bases_per_line'];
  90. $output_format = $this->options['display']['output_format'];
  91. // get the residues from the feature.residues column
  92. $field = $this->field_alias;
  93. // get the feature id
  94. $feature_id = $values->feature_feature_id;
  95. $feature_name = $values->feature_name;
  96. // the upstream and downstream values get set by the
  97. // tripal_views_handlers_filter_sequence.inc
  98. $upstream = $_SESSION['upstream'];
  99. $downstream = $_SESSION['downstream'];
  100. if (!$upstream) {
  101. $upstream = 0;
  102. }
  103. if (!$downstream) {
  104. $downstream = 0;
  105. }
  106. $derive_from_parent = $this->options['display']['derive_from_parent'];
  107. $aggregate = $this->options['display']['aggregate'];
  108. $residues = tripal_feature_get_formatted_sequence($feature_id, $feature_name,
  109. $num_bases_per_line, $derive_from_parent, $aggregate, $output_format,
  110. $upstream, $downstream);
  111. /*
  112. // if we need to get the sequence from the parent but there is no aggregation
  113. // then do so now.
  114. if ($this->options['display']['derive_from_parent']) {
  115. // execute our prepared statement
  116. if (tripal_core_is_sql_prepared('sequence_by_parent')) {
  117. $sql = "EXECUTE sequence_by_parent (%d, %d, %d)";
  118. $parents = chado_query($sql, $upstream, $downstream, $feature_id);
  119. }
  120. while ($parent = db_fetch_object($parents)) {
  121. $seq = ''; // initialize the sequence for each parent
  122. // if we are to aggregate then we will ignore the feature returned
  123. // by the query above and rebuild it using the sub features
  124. if ($this->options['display']['aggregate']){
  125. // now get the sub features that are located on the parent.
  126. $sql = "EXECUTE sub_features (%d, %d)";
  127. $children = chado_query($sql, $feature_id, $parent->srcfeature_id);
  128. $sql = "EXECUTE count_sub_features (%d, %d)";
  129. $num_children = db_fetch_object(chado_query($sql, $feature_id, $parent->srcfeature_id));
  130. // iterate through the sub features and concat their sequences. They
  131. // should already be in order.
  132. $types = array();
  133. $i = 0;
  134. while($child = db_fetch_object($children)) {
  135. // keep up with the types
  136. if (!in_array($child->type_name,$types)) {
  137. $types[] = $child->type_name;
  138. }
  139. $sql = "EXECUTE sequence_by_parent (%d, %d, %d)";
  140. // if the first sub feature we need to include the upstream bases
  141. if ($i == 0 and $parent->strand >= 0) {
  142. // -------------------------- ref
  143. // ....----> ---->
  144. // up 1 2
  145. $q = chado_query($sql, $upstream, 0, $child->feature_id);
  146. }
  147. elseif ($i == 0 and $parent->strand < 0) {
  148. // -------------------------- ref
  149. // ....<---- <----
  150. // down 1 2
  151. $q = chado_query($sql, 0, $downstream, $child->feature_id);
  152. }
  153. // if the last sub feature we need to include the downstream bases
  154. elseif ($i == $num_children->num_children - 1 and $parent->strand >= 0) {
  155. // -------------------------- ref
  156. // ----> ---->....
  157. // 1 2 down
  158. $q = chado_query($sql, 0, $downstream, $child->feature_id);
  159. }
  160. elseif ($i == $num_children->num_children - 1 and $parent->strand < 0) {
  161. // -------------------------- ref
  162. // <---- <----....
  163. // 1 2 up
  164. $q = chado_query($sql, $upstream, 0, $child->feature_id);
  165. }
  166. // for internal sub features we don't want upstream or downstream bases
  167. else {
  168. $sql = "EXECUTE sequence_by_parent (%d, %d, %d)";
  169. $q = chado_query($sql, 0, 0, $child->feature_id);
  170. }
  171. while($subseq = db_fetch_object($q)){
  172. // concatenate the sequences of all the sub features
  173. if($subseq->srcfeature_id == $parent->srcfeature_id){
  174. $seq .= $subseq->residues;
  175. }
  176. }
  177. $i++;
  178. }
  179. }
  180. // if this isn't an aggregate then use the parent residues
  181. else {
  182. $seq = $parent->residues;
  183. }
  184. // get the reverse compliment if feature is on the reverse strand
  185. $dir = 'forward';
  186. if ($parent->strand < 0) {
  187. $seq = trpial_feature_reverse_complement($seq);
  188. $dir = 'reverse';
  189. }
  190. // now format for display
  191. if ($output_format == 'fasta_html') {
  192. $seq = wordwrap($seq, $num_bases_per_line, "<br>", TRUE);
  193. }
  194. elseif ($output_format == 'fasta_txt') {
  195. $seq = wordwrap($seq, $num_bases_per_line, "\n", TRUE);
  196. }
  197. $residues .= ">$feature_name ($parent->typename) $parent->srcname:" . ($parent->adjfmin + 1) . ".." . $parent->adjfmax ." ($dir). ";
  198. if (count($types) > 0) {
  199. $residues .= "Excludes all bases but those of type(s): " . implode(', ',$types) . ". " ;
  200. }
  201. if ($parent->upstream > 0) {
  202. $residues .= "Includes " . $parent->upstream . " bases upstream. ";
  203. }
  204. if ($parent->downstream > 0) {
  205. $residues .= "Includes " . $parent->downstream . " bases downstream. ";
  206. }
  207. if (!$seq) {
  208. $residues .= "No sequence available\n<br>";
  209. }
  210. else {
  211. if ($output_format == 'fasta_html') {
  212. $residues .= "<br>";
  213. }
  214. $residues .= "\n" . $seq . "\n";
  215. if ($output_format == 'fasta_html') {
  216. $residues .= "<br>";
  217. }
  218. }
  219. }
  220. }
  221. // if we are not getting the sequence from the parent sequence then
  222. // use what comes through from the feature record
  223. else {
  224. $residues = $values->$field;
  225. if ($output_format == 'fasta_html') {
  226. $residues = wordwrap($residues, $num_bases_per_line, "<br>", TRUE);
  227. }
  228. elseif ($output_format == 'fasta_txt') {
  229. $residues = wordwrap($residues, $num_bases_per_line, "\n", TRUE);
  230. }
  231. }
  232. // format the residues for display
  233. if($residues and $num_bases_per_line){
  234. if ($output_format == 'fasta_html') {
  235. $residues = '<span style="font-family: monospace;">' . $residues . '</span>';
  236. }
  237. } */
  238. return $residues;
  239. }
  240. }