tripal_feature_sequence.tpl.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. <div class="tripal_feature-data-block-desc tripal-data-block-desc">The following sequences are available for this feature:</div> <?php
  30. // ADD IN RESIDUES FOR THIS FEATURE
  31. // add in the residues if they are present
  32. if ($residues) {
  33. $list_items[] = '<a href="#residues">Current ' . $feature->type_id->name . ' sequence</a>';
  34. // format the sequence to break every 50 residues
  35. $sequences_html .= '<a name="residues"></a>';
  36. $sequences_html .= '<div id="residues" class="tripal_feature-sequence-item">';
  37. $sequences_html .= '<p><b>Current ' . $feature->type_id->name . ' sequence</b></p>';
  38. $sequences_html .= '<pre class="tripal_feature-sequence">';
  39. $sequences_html .= '>' . tripal_get_fasta_defline($feature) . "\n";
  40. $sequences_html .= preg_replace("/(.{50})/","\\1<br>",$feature->residues);
  41. $sequences_html .= '</pre>';
  42. $sequences_html .= '<a href="#sequences-top">back to top</a>';
  43. $sequences_html .= '</div>';
  44. }
  45. // ADD IN RELATIONSHIP SEQUENCES (e.g. proteins)
  46. // see the explanation in the tripal_feature_relationships.tpl.php
  47. // template for how the 'all_relationships' is provided. It is this
  48. // variable that we use to get the proteins.
  49. $all_relationships = $feature->all_relationships;
  50. $object_rels = $all_relationships['object'];
  51. $has_coding_seq = 0;
  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 of ' . $protein->name . '</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. // we want to know if there are any coding sequences associated with this feature
  73. // if so we will use some code a bit later on to get those sequences
  74. if ($rel_type == 'part of' and $subject_type == 'CDS') {
  75. $has_coding_seq = 1;
  76. }
  77. // add any other sequences that are related through a relationship
  78. // and that have values in the 'residues' column
  79. }
  80. }
  81. }
  82. /* ADD IN ALIGNMENT SEQUENCES FOR THIS FEATURE
  83. * For retreiving the sequence from an alignment we would typically make a call to
  84. * chado_expand_var function. For example, to retrieve all
  85. * of the featurelocs in order to get the sequences needed for this template, the
  86. * following function call would be made:
  87. *
  88. * $feature = chado_expand_var($feature,'table','featureloc');
  89. *
  90. * Then all of the sequences would need to be retreived from the alignments and
  91. * formatted for display below. However, to simplify this template, this has already
  92. * been done by the tripal_feature module and the sequences are made available in
  93. * the variable:
  94. *
  95. * $feature->featureloc_sequences
  96. */
  97. if(count($featureloc_sequences) > 0){
  98. foreach($featureloc_sequences as $src => $attrs){
  99. // the $attrs array has the following keys
  100. // * src: a unique identifier combining the feature id with the cvterm id
  101. // * type: the type of sequence (e.g. mRNA, etc)
  102. // * location: the alignment location
  103. // * defline: the definition line
  104. // * formatted_seq: the formatted sequences
  105. $list_items[] = '<a href="#' . $attrs['src'] . '">Alignment at ' . $attrs['location'] . "</a>";
  106. $sequences_html .= '<a name="' . $attrs['src'] . '"></a>';
  107. $sequences_html .= '<div id="' . $attrs['src'] . '" class="tripal_feature-sequence-item">';
  108. $sequences_html .= '<p><b>Alignment at ' . $attrs['location'] .'</b></p>';
  109. $sequences_html .= $attrs['formatted_seq'];
  110. $sequences_html .= '<a href="#sequences-top">back to top</a>';
  111. $sequences_html .= '</div>';
  112. }
  113. }
  114. // CODING SEQUENCES
  115. // add in any CDS sequences.
  116. if ($has_coding_seq) {
  117. // use the tripal_get_sequence() API function to retreive the CDS sequences
  118. $cds_sequence = tripal_get_sequence(
  119. array(
  120. 'feature_id' => $feature->feature_id,
  121. 'name' => $feature->name,
  122. ),
  123. array(
  124. 'width' => 50, // FASTA sequence should have 50 chars per line
  125. 'derive_from_parent' => 1, // CDS are in parent-child relationships so we want to use the sequence from the parent
  126. 'aggregate' => 1, // we want to combine all CDS for this feature into a single sequence
  127. 'output_format' => 'fasta_txt', // we just want plain text, we'll format it here.
  128. 'sub_feature_types' => array('CDS'), // we're looking for CDS features
  129. ''
  130. )
  131. );
  132. $list_items[] = '<a href="#coding_sequence">Coding sequence </a>';
  133. $sequences_html .= '<a name="coding_sequence"></a>';
  134. $sequences_html .= '<div id="coding_sequence" class="tripal_feature-sequence-item">';
  135. $sequences_html .= '<p><b>Coding sequence</b></p>';
  136. $sequences_html .= '<pre class="tripal_feature-sequence">';
  137. $sequences_html .= $cds_sequence;
  138. $sequences_html .= '</pre>';
  139. $sequences_html .= '<a href="#sequences-top">back to top</a>';
  140. $sequences_html .= '</div>';
  141. }
  142. // first add a list at the top of the page that can be formatted as the
  143. // user desires. We use the theme_item_list function of Drupal to create
  144. // the list rather than hard-code the HTML here. Instructions for how
  145. // to create the list can be found here:
  146. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_item_list/7
  147. print '<a name="sequences-top"></a>';
  148. print theme_item_list(array(
  149. 'items' => $list_items,
  150. 'title' => '',
  151. 'type' => 'ul',
  152. 'attributes' => array(),
  153. ));
  154. // now print the sequences
  155. print $sequences_html;
  156. }