tripal_feature_sequence.tpl.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /*
  3. * There are several ways that sequences can be displayed. They can come from the
  4. * feature.residues column, they can come from an alignment with another feature,
  5. * they can come from a protein sequence that has relationship with this sequence,
  6. * or they can come from sub children (e.g. CDS coding sequences).
  7. *
  8. * This template will show all types depending on the data available.
  9. *
  10. */
  11. $feature = $variables['node']->feature;
  12. // we don't want to get the sequence for traditionally large types. They are
  13. // too big, bog down the web browser, take longer to load and it's not
  14. // reasonable to print them on a page.
  15. $residues ='';
  16. if(strcmp($feature->type_id->name,'scaffold') !=0 and
  17. strcmp($feature->type_id->name,'chromosome') !=0 and
  18. strcmp($feature->type_id->name,'supercontig') !=0 and
  19. strcmp($feature->type_id->name,'pseudomolecule') !=0) {
  20. $feature = chado_expand_var($feature,'field','feature.residues');
  21. $residues = $feature->residues;
  22. }
  23. // get the sequence derived from alignments
  24. $feature = $variables['node']->feature;
  25. $featureloc_sequences = $feature->featureloc_sequences;
  26. if ($residues or count($featureloc_sequences) > 0) {
  27. $sequences_html = ''; // a variable for holding all sequences HTML text
  28. $list_items = array(); // a list to be used for theming of content on this page
  29. // ADD IN RESIDUES FOR THIS FEATURE
  30. // add in the residues if they are present
  31. if ($residues) {
  32. $list_items[] = '<a href="#residues">' . $feature->type_id->name . ' sequence</a>';
  33. // format the sequence to break every 50 residues
  34. $sequences_html .= '<a name="residues"></a>';
  35. $sequences_html .= '<div id="residues" class="tripal_feature-sequence-item">';
  36. $sequences_html .= '<p><b>' . $feature->type_id->name . ' sequence</b></p>';
  37. $sequences_html .= '<pre class="tripal_feature-sequence">';
  38. $sequences_html .= '>' . tripal_get_fasta_defline($feature) . "\n";
  39. $sequences_html .= preg_replace("/(.{50})/","\\1<br>",$feature->residues);
  40. $sequences_html .= '</pre>';
  41. $sequences_html .= '<a href="#sequences-top">back to top</a>';
  42. $sequences_html .= '</div>';
  43. }
  44. // ADD IN RELATIONSHIP SEQUENCES (e.g. proteins)
  45. // see the explanation in the tripal_feature_relationships.tpl.php
  46. // template for how the 'all_relationships' is provided. It is this
  47. // variable that we use to get the proteins.
  48. $all_relationships = $feature->all_relationships;
  49. $object_rels = $all_relationships['object'];
  50. $has_coding_seq = 0;
  51. $coding_seq = '';
  52. foreach ($object_rels as $rel_type => $rels){
  53. foreach ($rels as $subject_type => $subjects){
  54. foreach ($subjects as $subject){
  55. // add in protein sequence if it has residues
  56. if ($rel_type == 'derives from' and $subject_type == 'polypeptide') {
  57. $protein = $subject->record->subject_id;
  58. $protein = chado_expand_var($protein, 'field', 'feature.residues');
  59. if ($protein->residues) {
  60. $list_items[] = '<a href="#residues">protein sequence</a>';
  61. $sequences_html .= '<a name="protein-' . $protein->feature_id . '"></a>';
  62. $sequences_html .= '<div id="protein-' . $protein->feature_id . '" class="tripal_feature-sequence-item">';
  63. $sequences_html .= '<p><b>protein sequence of ' . $protein->name . '</b></p>';
  64. $sequences_html .= '<pre class="tripal_feature-sequence">';
  65. $sequences_html .= '>' . tripal_get_fasta_defline($protein) . "\n";
  66. $sequences_html .= preg_replace("/(.{50})/","\\1<br>", $protein->residues);
  67. $sequences_html .= '</pre>';
  68. $sequences_html .= '<a href="#sequences-top">back to top</a>';
  69. $sequences_html .= '</div>';
  70. }
  71. }
  72. // If the CDS has sequences then concatenate those. The objects
  73. // should be returned in order of rank
  74. if ($rel_type == 'part of' and $subject_type == 'CDS') {
  75. $cds = $subject->record->subject_id;
  76. $cds = chado_expand_var($cds, 'field', 'feature.residues');
  77. if ($cds->residues) {
  78. $has_coding_seq = 1;
  79. $coding_seq .= $cds->residues;
  80. }
  81. }
  82. // add any other sequences that are related through a relationship
  83. // and that have values in the 'residues' column
  84. }
  85. }
  86. }
  87. // CODING SEQUENCES FROM RELATIONSHIPS
  88. // add in any CDS sequences.
  89. if ($has_coding_seq) {
  90. $list_items[] = '<a href="#coding_sequence">coding sequence </a>';
  91. $sequences_html .= '<a name="coding_sequence"></a>';
  92. $sequences_html .= '<div id="coding_sequence" class="tripal_feature-sequence-item">';
  93. $sequences_html .= '<p><b>coding sequence</b></p>';
  94. $sequences_html .= '<pre class="tripal_feature-sequence">';
  95. $sequences_html .= $coding_seq;
  96. $sequences_html .= '</pre>';
  97. $sequences_html .= '<a href="#sequences-top">back to top</a>';
  98. $sequences_html .= '</div>';
  99. }
  100. /* ADD IN ALIGNMENT SEQUENCES FOR THIS FEATURE
  101. * For retreiving the sequence from an alignment we would typically make a call to
  102. * chado_expand_var function. For example, to retrieve all
  103. * of the featurelocs in order to get the sequences needed for this template, the
  104. * following function call would be made:
  105. *
  106. * $feature = chado_expand_var($feature,'table','featureloc');
  107. *
  108. * Then all of the sequences would need to be retreived from the alignments and
  109. * formatted for display below. However, to simplify this template, this has already
  110. * been done by the tripal_feature module and the sequences are made available in
  111. * the variable:
  112. *
  113. * $feature->featureloc_sequences
  114. */
  115. if(count($featureloc_sequences) > 0){
  116. foreach($featureloc_sequences as $src => $attrs){
  117. // the $attrs array has the following keys
  118. // * id: a unique identifier combining the feature id with the cvterm id
  119. // * type: the type of sequence (e.g. mRNA, etc)
  120. // * location: the alignment location
  121. // * defline: the definition line
  122. // * formatted_seq: the formatted sequences
  123. // * featureloc: the feature object aligned to
  124. $list_items[] = '<a href="#' . $attrs['id'] . '">'. $feature->type_id->name . ' from alignment at ' . $attrs['location'] . "</a>";
  125. $sequences_html .= '<a name="' . $attrs['id'] . '"></a>';
  126. $sequences_html .= '<div id="' . $attrs['id'] . '" class="tripal_feature-sequence-item">';
  127. $sequences_html .= '<p><b>'. $feature->type_id->name . ' from alignment at ' . $attrs['location'] .'</b></p>';
  128. $sequences_html .= $attrs['formatted_seq'];
  129. $sequences_html .= '<a href="#sequences-top">back to top</a>';
  130. $sequences_html .= '</div>';
  131. }
  132. // check to see if this alignment has any CDS. If so, generate a CDS sequence
  133. $cds_sequence = tripal_get_sequence(
  134. array(
  135. 'feature_id' => $feature->feature_id,
  136. 'parent_id' => $attrs['featureloc']->srcfeature_id->feature_id,
  137. 'name' => $feature->name,
  138. 'featureloc_id' => $attrs['featureloc']->featureloc_id,
  139. ),
  140. array(
  141. 'width' => 50, // FASTA sequence should have 50 chars per line
  142. 'derive_from_parent' => 1, // CDS are in parent-child relationships so we want to use the sequence from the parent
  143. 'aggregate' => 1, // we want to combine all CDS for this feature into a single sequence
  144. 'output_format' => 'fasta_txt', // we just want plain text, we'll format it here.
  145. 'sub_feature_types' => array('CDS'), // we're looking for CDS features
  146. ''
  147. )
  148. );
  149. if (count($cds_sequence) > 0) {
  150. $list_items[] = '<a href="#coding_' . $attrs['id'] . '">coding sequnece from alignment at ' . $attrs['location'] . "</a>";
  151. $sequences_html .= '<a name="ccoding_' . $attrs['id'] . '"></a>';
  152. $sequences_html .= '<div id="coding_' . $attrs['id'] . '" class="tripal_feature-sequence-item">';
  153. $sequences_html .= '<p><b>Coding sequence (CDS) from alignment at ' . $attrs['location'] . '</b></p>';
  154. $sequences_html .= '<pre class="tripal_feature-sequence">';
  155. $sequences_html .= '>' . tripal_get_fasta_defline($feature, $attrs['featureloc'], 'CDS') . "\n";
  156. $sequences_html .= $cds_sequence[0]['residues'];
  157. $sequences_html .= '</pre>';
  158. $sequences_html .= '<a href="#sequences-top">back to top</a>';
  159. $sequences_html .= '</div>';
  160. }
  161. }
  162. ?>
  163. <div class="tripal_feature-data-block-desc tripal-data-block-desc">The following sequences are available for this feature:</div>
  164. <?php
  165. // first add a list at the top of the page that can be formatted as the
  166. // user desires. We use the theme_item_list function of Drupal to create
  167. // the list rather than hard-code the HTML here. Instructions for how
  168. // to create the list can be found here:
  169. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_item_list/7
  170. print '<a name="sequences-top"></a>';
  171. print theme_item_list(array(
  172. 'items' => $list_items,
  173. 'title' => '',
  174. 'type' => 'ul',
  175. 'attributes' => array(),
  176. ));
  177. $message = 'Administrators, sequences will appear on this page if:
  178. <ul>
  179. <li>This feature has residues stored in the "residues" field of the feature table of Chado.</li>
  180. <li>This feature has a protein feature associated via the "feature_relationship" table of Chado with a
  181. relationship of type "derives from" and the protein feature has residues.</li>
  182. <li>This feature has one or more CDS feature associated via the "feature_relationship" table of Chado with a
  183. relationship of type "part of". If the CDS features have residues then those will be concatenated
  184. and presented as a feature.</li>
  185. <li>This feature is aligned to another feature (e.g. scaffold, or chromosome). In this case, the
  186. sequence underlying the alignment will be shown</li>
  187. <li>This feature is aligned to another feature (e.g. scaffold, or chromosome) and this feature has
  188. one ore more CDS features associated. The CDS sequenes underlying the alignment will be
  189. shown.</li>
  190. </ul>
  191. </p>';
  192. print tripal_set_message($message, TRIPAL_INFO, array('return_html' => 1));
  193. // now print the sequences
  194. print $sequences_html;
  195. }