tripal_views_handler_field_sequence.inc 10 KB

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