blast_report.tpl.php 13 KB

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