blast_report.tpl.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * Display the results of a BLAST job execution
  4. *
  5. * Variables Available in this template:
  6. * $xml_filename: The full path & filename of XML file containing the BLAST results
  7. */
  8. // Set ourselves up to do link-out if our blast database is configured to do so.
  9. $linkout = FALSE;
  10. if ($blastdb->linkout->none === FALSE) {
  11. $linkout = TRUE;
  12. $linkout_regex = $blastdb->linkout->regex;
  13. if (isset($blastdb->linkout->db_id->urlprefix) AND !empty($blastdb->linkout->db_id->urlprefix)) {
  14. $linkout_urlprefix = $blastdb->linkout->db_id->urlprefix;
  15. // Furthermore, check that we can determine the URL.
  16. // (ie: that the function specified to do so, exists).
  17. if (function_exists($blastdb->linkout->url_function)) {
  18. $url_function = $blastdb->linkout->url_function;
  19. }
  20. else {
  21. $linkout = FALSE;
  22. }
  23. }
  24. else {
  25. $linkout = FALSE;
  26. }
  27. }
  28. // Handle no hits. This following array will hold the names of all query
  29. // sequences which didn't have any hits.
  30. $query_with_no_hits = array();
  31. // Furthermore, if no query sequences have hits we don't want to bother listing
  32. // them all but just want to give a single, all-include "No Results" message.
  33. $no_hits = TRUE;
  34. ?>
  35. <!-- JQuery controlling display of the alignment information (hidden by default) -->
  36. <script type="text/javascript">
  37. $(document).ready(function(){
  38. // Hide the alignment rows in the table
  39. // (ie: all rows not labelled with the class "result-summary" which contains the tabular
  40. // summary of the hit)
  41. $("#blast_report tr:not(.result-summary)").hide();
  42. $("#blast_report tr:first-child").show();
  43. // When a results summary row is clicked then show the next row in the table
  44. // which should be corresponding the alignment information
  45. $("#blast_report tr.result-summary").click(function(){
  46. $(this).next("tr").toggle();
  47. $(this).find(".arrow").toggleClass("up");
  48. });
  49. });
  50. </script>
  51. <style>
  52. .no-hits-message {
  53. color: red;
  54. font-style: italic;
  55. }
  56. </style>
  57. <p><strong>Download</strong>:
  58. <a href="<?php print '../../' . $html_filename; ?>">HTML</a>,
  59. <a href="<?php print '../../' . $tsv_filename; ?>">Tab-Delimited</a>,
  60. <a href="<?php print '../../' . $xml_filename; ?>">XML</a>
  61. </p>
  62. <p>The following table summarizes the results of your BLAST. To see additional information
  63. about each hit including the alignment, click on that row in the table to expand it.</p>
  64. <?php
  65. // Load the XML file
  66. $xml = simplexml_load_file($xml_filename);
  67. /**
  68. * We are using the drupal table theme functionality to create this listing
  69. * @see theme_table() for additional documentation
  70. */
  71. if ($xml) {
  72. // Specify the header of the table
  73. $header = array(
  74. 'number' => array('data' => '#', 'class' => array('number')),
  75. 'query' => array('data' => 'Query Name', 'class' => array('query')),
  76. 'hit' => array('data' => 'Hit Name', 'class' => array('hit')),
  77. 'evalue' => array('data' => 'E-Value', 'class' => array('evalue')),
  78. 'arrow-col' => array('data' => '', 'class' => array('arrow-col'))
  79. );
  80. $rows = array();
  81. $count = 0;
  82. // Parse the BLAST XML to generate the rows of the table
  83. // where each hit results in two rows in the table: 1) A summary of the query/hit and
  84. // significance and 2) additional information including the alignment
  85. foreach($xml->{'BlastOutput_iterations'}->children() as $iteration) {
  86. $children_count = $iteration->{'Iteration_hits'}->children()->count();
  87. if($children_count != 0) {
  88. foreach($iteration->{'Iteration_hits'}->children() as $hit) {
  89. if (is_object($hit)) {
  90. $count +=1;
  91. $zebra_class = ($count % 2 == 0) ? 'even' : 'odd';
  92. $no_hits = FALSE;
  93. // RETRIEVE INFO
  94. $hit_name = (preg_match('/BL_ORD_ID/', $hit->{'Hit_id'})) ? $hit->{'Hit_def'} : $hit->{'Hit_id'};
  95. $score = $hit->{'Hit_hsps'}->{'Hsp'}->{'Hsp_score'};
  96. $evalue = $hit->{'Hit_hsps'}->{'Hsp'}->{'Hsp_evalue'};
  97. $query_name = $iteration->{'Iteration_query-def'};
  98. $HSPs = array();
  99. foreach ($hit->{'Hit_hsps'}->children() as $hsp_xml) {
  100. $HSPs[] = (array) $hsp_xml;
  101. }
  102. // SUMMARY ROW
  103. // If the id is of the form gnl|BL_ORD_ID|### then the parseids flag
  104. // to makeblastdb did a really poor job. In thhis case we want to use
  105. // the def to provide the original FASTA header.
  106. // If our BLAST DB is configured to handle link-outs then use the
  107. // regex & URL prefix provided to create one.
  108. if ($linkout) {
  109. if (preg_match($linkout_regex, $hit_name, $linkout_match)) {
  110. $linkout_id = $linkout_match[1];
  111. $hit->{'linkout_id'} = $linkout_id;
  112. $hit->{'hit_name'} = $hit_name;
  113. $hit_url = call_user_func(
  114. $url_function,
  115. $linkout_urlprefix,
  116. $hit,
  117. array(
  118. 'query_name' => $query_name,
  119. 'score' => $score,
  120. 'e-value' => $evalue,
  121. 'HSPs' => $HSPs
  122. )
  123. );
  124. if ($hit_url) {
  125. $hit_name = l(
  126. $linkout_id,
  127. $hit_url,
  128. array('attributes' => array('target' => '_blank'))
  129. );
  130. }
  131. }
  132. }
  133. $row = array(
  134. 'data' => array(
  135. 'number' => array('data' => $count, 'class' => array('number')),
  136. 'query' => array('data' => $query_name, 'class' => array('query')),
  137. 'hit' => array('data' => $hit_name, 'class' => array('hit')),
  138. 'evalue' => array('data' => $evalue, 'class' => array('evalue')),
  139. 'arrow-col' => array('data' => '<div class="arrow"></div>', 'class' => array('arrow-col'))
  140. ),
  141. 'class' => array('result-summary')
  142. );
  143. $rows[] = $row;
  144. // ALIGNMENT ROW (collapsed by default)
  145. // Process HSPs
  146. $row = array(
  147. 'data' => array(
  148. 'number' => '',
  149. 'query' => array(
  150. 'data' => theme('blast_report_alignment_row', array('HSPs' => $HSPs)),
  151. 'colspan' => 4,
  152. )
  153. ),
  154. 'class' => array('alignment-row', $zebra_class),
  155. 'no_striping' => TRUE
  156. );
  157. $rows[] = $row;
  158. }// end of if - checks $hit
  159. } //end of foreach - iteration_hits
  160. } // end of if - check for iteration_hits
  161. else {
  162. // Currently where the "no results" is added.
  163. $query_name = $iteration->{'Iteration_query-def'};
  164. $query_with_no_hits[] = $query_name;
  165. } // end of else
  166. }
  167. if ($no_hits) {
  168. print '<p class="no-hits-message">No results found.</p>';
  169. }
  170. else {
  171. // We want to warn the user if some of their query sequences had no hits.
  172. if (!empty($query_with_no_hits)) {
  173. print '<p class="no-hits-message">Some of your query sequences did not '
  174. . 'match to the database/template. They are: '
  175. . implode(', ', $query_with_no_hits) . '.</p>';
  176. }
  177. // Actually print the table.
  178. if (!empty($rows)) {
  179. print theme('table', array(
  180. 'header' => $header,
  181. 'rows' => $rows,
  182. 'attributes' => array('id' => 'blast_report'),
  183. ));
  184. }
  185. }
  186. }
  187. else {
  188. drupal_set_title('BLAST: Error Encountered');
  189. print '<p>We encountered an error and are unable to load your BLAST results.</p>';
  190. }
  191. ?>