blast_report.tpl.php 14 KB

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