blast_report.tpl.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. /**
  3. * Display the results of a BLAST job execution
  4. */
  5. // Set ourselves up to do link-out if our blast database is configured to do so.
  6. $linkout = FALSE;
  7. if ($blast_job->blastdb->linkout->none === FALSE) {
  8. $linkout_type = $blast_job->blastdb->linkout->type;
  9. $linkout_regex = $blast_job->blastdb->linkout->regex;
  10. // Note that URL prefix is not required if linkout type is 'custom'
  11. if (isset($blast_job->blastdb->linkout->db_id->urlprefix) && !empty($blast_job->blastdb->linkout->db_id->urlprefix)) {
  12. $linkout_urlprefix = $blast_job->blastdb->linkout->db_id->urlprefix;
  13. }
  14. // Check that we can determine the linkout URL.
  15. // (ie: that the function specified to do so, exists).
  16. if (function_exists($blast_job->blastdb->linkout->url_function)) {
  17. $url_function = $blast_job->blastdb->linkout->url_function;
  18. $linkout = TRUE;
  19. }
  20. }
  21. // Handle no hits. This following array will hold the names of all query
  22. // sequences which didn't have any hits.
  23. $query_with_no_hits = array();
  24. // Furthermore, if no query sequences have hits we don't want to bother listing
  25. // them all but just want to give a single, all-include "No Results" message.
  26. $no_hits = TRUE;
  27. ?>
  28. <script type="text/javascript">
  29. // JQuery controlling display of the alignment information (hidden by default)
  30. $(document).ready(function(){
  31. // Hide the alignment rows in the table
  32. // (ie: all rows not labelled with the class "result-summary" which contains the tabular
  33. // summary of the hit)
  34. $("#blast_report tr:not(.result-summary)").hide();
  35. $("#blast_report tr:first-child").show();
  36. // When a results summary row is clicked then show the next row in the table
  37. // which should be corresponding the alignment information
  38. $("#blast_report tr.result-summary").click(function(){
  39. $(this).next("tr").toggle();
  40. $(this).find(".arrow").toggleClass("up");
  41. });
  42. });
  43. </script>
  44. <style>
  45. .no-hits-message {
  46. color: red;
  47. font-style: italic;
  48. }
  49. </style>
  50. <div class="blast-report">
  51. <!-- Provide Information to the user about their blast job -->
  52. <div class="blast-job-info">
  53. <?php if($xml):
  54. $output_files = array();
  55. if (file_exists($blast_job->files->result->html))
  56. $output_files['html'] = array('path' => $blast_job->files->result->html, 'title' => 'Alignment');
  57. if (file_exists($blast_job->files->result->tsv))
  58. $output_files['tsv'] = array('path' => $blast_job->files->result->tsv, 'title' => 'Tab-Delimited');
  59. if (file_exists($blast_job->files->result->gff))
  60. $output_files['gff'] = array('path' => $blast_job->files->result->gff, 'title' => 'GFF3');
  61. if (file_exists($blast_job->files->result->xml))
  62. $output_files['xml'] = array('path' => $blast_job->files->result->xml, 'title' => 'XML');
  63. ?>
  64. <div class="blast-download-info"><strong>Download</strong>:
  65. <?php
  66. $i = 0;
  67. foreach($output_files as $file) {
  68. $i++;
  69. print l($file['title'], $file['path']);
  70. if (sizeof($output_files) != $i) print ', ';
  71. } ?>
  72. </div>
  73. <?php endif; ?>
  74. <br />
  75. <div class="blast-query-info"><strong>Query Information</strong>:
  76. <?php print $blast_job->files->query;?></div>
  77. <div class="blast-target-info"><strong>Search Target</strong>:
  78. <?php print $blast_job->blastdb->db_name;?></div>
  79. <div class="blast-date-info"><strong>Submission Date</strong>:
  80. <?php print format_date($blast_job->date_submitted, 'medium');?></div>
  81. <div class="blast-cmd-info"><strong>BLAST Command executed</strong>:
  82. <?php print $blast_job->blast_cmd;?></div>
  83. </div>
  84. <br />
  85. <?php
  86. /**
  87. * We are using the drupal table theme functionality to create this listing
  88. * @see theme_table() for additional documentation
  89. */
  90. if ($xml) {
  91. ?>
  92. <p>The following table summarizes the results of your BLAST.
  93. Click on a <em>triangle </em> on the left to see the alignment and a visualization of the hit,
  94. and click the <em>target name </em> to get more information about the target hit.</p>
  95. <?php
  96. // Specify the header of the table
  97. $header = array(
  98. 'arrow-col' => array('data' => '', 'class' => array('arrow-col')),
  99. 'number' => array('data' => '#', 'class' => array('number')),
  100. 'query' => array('data' => 'Query Name (Click for alignment & visualization)', 'class' => array('query')),
  101. 'hit' => array('data' => 'Target Name', 'class' => array('hit')),
  102. 'evalue' => array('data' => 'E-Value', 'class' => array('evalue')),
  103. );
  104. $rows = array();
  105. $count = 0;
  106. // Parse the BLAST XML to generate the rows of the table
  107. // where each hit results in two rows in the table: 1) A summary of the query/hit and
  108. // significance and 2) additional information including the alignment
  109. foreach ($xml->{'BlastOutput_iterations'}->children() as $iteration) {
  110. $children_count = $iteration->{'Iteration_hits'}->children()->count();
  111. // Save some information needed for the hit visualization.
  112. $target_name = '';
  113. $q_name = $xml->{'BlastOutput_query-def'};
  114. $query_size = $xml->{'BlastOutput_query-len'};
  115. $target_size = $iteration->{'Iteration_stat'}->{'Statistics'}->{'Statistics_db-len'};
  116. if ($children_count != 0) {
  117. foreach ($iteration->{'Iteration_hits'}->children() as $hit) {
  118. if (is_object($hit)) {
  119. $count +=1;
  120. $zebra_class = ($count % 2 == 0) ? 'even' : 'odd';
  121. $no_hits = FALSE;
  122. // SUMMARY ROW
  123. // -- Save additional information needed for the summary.
  124. $score = (float) $hit->{'Hit_hsps'}->{'Hsp'}->{'Hsp_score'};
  125. $evalue = (float) $hit->{'Hit_hsps'}->{'Hsp'}->{'Hsp_evalue'};
  126. $query_name = (string) $iteration->{'Iteration_query-def'};
  127. // If the id is of the form gnl|BL_ORD_ID|### then the parseids flag
  128. // to makeblastdb did a really poor job. In thhis case we want to use
  129. // the def to provide the original FASTA header.
  130. // @todo Deepak changed this to use just the hit_def; inquire as to why.
  131. $hit_name = (preg_match('/BL_ORD_ID/', $hit->{'Hit_id'})) ? $hit->{'Hit_def'} : $hit->{'Hit_id'};
  132. // Used for the hit visualization to ensure the name isn't truncated.
  133. $hit_name_short = (preg_match('/^([^\s]+)/', $hit_name, $matches)) ? $matches[1] : $hit_name;
  134. // Round e-value to two decimal values.
  135. $rounded_evalue = '';
  136. if (strpos($evalue,'e') != false) {
  137. $evalue_split = explode('e', $evalue);
  138. $rounded_evalue = round($evalue_split[0], 2, PHP_ROUND_HALF_EVEN);
  139. $rounded_evalue .= 'e' . $evalue_split[1];
  140. }
  141. else {
  142. $rounded_evalue = $evalue;
  143. }
  144. // State what should be in the summary row for theme_table() later.
  145. $summary_row = array(
  146. 'data' => array(
  147. 'arrow-col' => array('data' => '<div class="arrow"></div>', 'class' => array('arrow-col')),
  148. 'number' => array('data' => $count, 'class' => array('number')),
  149. 'query' => array('data' => $query_name, 'class' => array('query')),
  150. 'hit' => array('data' => $hit_name, 'class' => array('hit')),
  151. 'evalue' => array('data' => $rounded_evalue, 'class' => array('evalue')),
  152. ),
  153. 'class' => array('result-summary')
  154. );
  155. // ALIGNMENT ROW (collapsed by default)
  156. // Process HSPs
  157. $HSPs = array();
  158. // We need to save some additional summary information in order to draw the
  159. // hit visualization. First, initialize some variables...
  160. $track_start = INF;
  161. $track_end = -1;
  162. $hsps_range = '';
  163. $hit_hsps = '';
  164. $hit_hsp_score = '';
  165. $target_size = $hit->{'Hit_len'};
  166. $Hsp_bit_score = '';
  167. // Then for each hit hsp, keep track of the start of first hsp and the end of
  168. // the last hsp. Keep in mind that hsps might not be recorded in order.
  169. foreach ($hit->{'Hit_hsps'}->children() as $hsp_xml) {
  170. $HSPs[] = (array) $hsp_xml;
  171. if ($track_start > $hsp_xml->{'Hsp_hit-from'}) {
  172. $track_start = $hsp_xml->{'Hsp_hit-from'} . "";
  173. }
  174. if ($track_end < $hsp_xml->{'Hsp_hit-to'}) {
  175. $track_end = $hsp_xml->{'Hsp_hit-to'} . "";
  176. }
  177. // The BLAST visualization code requires the hsps to be formatted in a
  178. // very specific manner. Here we build up the strings to be submitted.
  179. // hits=4263001_4262263_1_742;4260037_4259524_895_1411;&scores=722;473;
  180. $hit_hsps .= $hsp_xml->{'Hsp_hit-from'} . '_' .
  181. $hsp_xml->{'Hsp_hit-to'} . '_' .
  182. $hsp_xml->{'Hsp_query-from'} . '_' . $hsp_xml->{'Hsp_query-to'} .
  183. ';';
  184. $Hsp_bit_score .= $hsp_xml->{'Hsp_bit-score'} .';';
  185. }
  186. // Finally record the range.
  187. // @todo figure out why we arbitrarily subtract 50,000 here...
  188. // @more removing the 50,000 and using track start/end appears to cause no change...
  189. $range_start = (int) $track_start;// - 50000;
  190. $range_end = (int) $track_end;// + 50000;
  191. if ($range_start < 1) $range_start = 1;
  192. // Call the function to generate the hit image.
  193. $hit_img = generate_blast_hit_image($target_name, $Hsp_bit_score, $hit_hsps,
  194. $target_size, $query_size, $q_name, $hit_name_short);
  195. // State what should be in the alignment row for theme_table() later.
  196. $alignment_row = array(
  197. 'data' => array(
  198. 'arrow' => array(
  199. 'data' => theme('blast_report_alignment_row', array('HSPs' => $HSPs, 'hit_visualization' => $hit_img)),
  200. 'colspan' => 5,
  201. ),
  202. ),
  203. 'class' => array('alignment-row', $zebra_class),
  204. 'no_striping' => TRUE
  205. );
  206. // LINK-OUTS.
  207. // It was determined above whether link-outs were supported for the
  208. // tripal blast database used as a search target. Thus we only want to
  209. // determine a link-out if it's actually supported... ;-)
  210. if ($linkout) {
  211. // First extract the linkout text using the regex provided through
  212. // the Tripal blast database node.
  213. if (preg_match($linkout_regex, $hit_name, $linkout_match)) {
  214. $hit->{'linkout_id'} = $linkout_match[1];
  215. $hit->{'hit_name'} = $hit_name;
  216. // Allow custom functions to determine the URL to support more complicated
  217. // link-outs rather than just using the tripal database prefix.
  218. $hit_name = call_user_func(
  219. $url_function,
  220. $linkout_urlprefix,
  221. $hit,
  222. array(
  223. 'query_name' => $query_name,
  224. 'score' => $score,
  225. 'e-value' => $evalue,
  226. 'HSPs' => $HSPs,
  227. 'Target' => $blast_job->blastdb->db_name,
  228. )
  229. );
  230. }
  231. // Replace the target name with the link.
  232. $summary_row['data']['hit']['data'] = $hit_name;
  233. }
  234. // ADD TO TABLE ROWS
  235. $rows[] = $summary_row;
  236. $rows[] = $alignment_row;
  237. }//end of if - checks $hit
  238. }//end of foreach - iteration_hits
  239. }//end of if - check for iteration_hits
  240. else {
  241. // Currently where the "no results" is added.
  242. $query_name = $iteration->{'Iteration_query-def'};
  243. $query_with_no_hits[] = $query_name;
  244. }//no results
  245. }//end of foreach - BlastOutput_iterations
  246. if ($no_hits) {
  247. print '<p class="no-hits-message">No results found.</p>';
  248. }
  249. else {
  250. // We want to warn the user if some of their query sequences had no hits.
  251. if (!empty($query_with_no_hits)) {
  252. print '<p class="no-hits-message">Some of your query sequences did not '
  253. . 'match to the database/template. They are: '
  254. . implode(', ', $query_with_no_hits) . '.</p>';
  255. }
  256. // Actually print the table.
  257. if (!empty($rows)) {
  258. print theme('table', array(
  259. 'header' => $header,
  260. 'rows' => $rows,
  261. 'attributes' => array('id' => 'blast_report'),
  262. 'sticky' => FALSE
  263. ));
  264. }
  265. }//handle no hits
  266. }//XML exists
  267. else {
  268. drupal_set_title('BLAST: Error Encountered');
  269. print '<p class="blast-no-results">We encountered an error and are unable to load your BLAST results.</p>';
  270. }
  271. ?>
  272. <p><?php print l(
  273. 'Edit this query and re-submit',
  274. $blast_form_url,
  275. array('query' => array('resubmit' => blast_ui_make_secret($job_id))));
  276. ?></p>
  277. </div>
  278. <?php print theme('blast_recent_jobs', array()); ?>