blast_report.tpl.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. <p><strong>Download</strong>:
  63. <a href="<?php print '../../' . $html_filename; ?>">Alignment</a>,
  64. <a href="<?php print '../../' . $tsv_filename; ?>">Tab-Delimited</a>,
  65. <a href="<?php print '../../' . $xml_filename; ?>">XML</a>
  66. </p>
  67. <!-- @deepaksomanadh: For displaying BLAST command details -->
  68. <table>
  69. <tr>
  70. <th>Input query sequence(s) </th>
  71. <th>Target Database selected </th>
  72. <th>BLAST command executed </th>
  73. <tr>
  74. <tr>
  75. <?php
  76. // get input sequences from job_data variable
  77. $query_def = $job_id_data['query_def'];
  78. echo "<td>";
  79. echo "<ol>";
  80. foreach($query_def as $row) {
  81. echo "<li>";
  82. echo $row . "</li>";
  83. }
  84. echo "</ol></td>";
  85. echo "<td>" . $job_id_data['db_name'] . "</td>";
  86. include_once("blast_align_image.php");
  87. //display the BLAST command without revealing the internal path
  88. $blast_cmd = $job_id_data['program'];
  89. foreach($job_id_data['options'] as $key => $value) {
  90. $blast_cmd .= ' -' . $key. ' ' . $value ;
  91. }
  92. print "<td>" . $blast_cmd . "</td>";
  93. ?>
  94. </table>
  95. <p>The following table summarizes the results of your BLAST.
  96. Click on a <strong>triangle </strong> on the left to see the alignment and a visualization of the hit,
  97. and click the <strong>target name </strong> to get more information about the target hit.</p>
  98. <?php
  99. include_once("blast_align_image.php");
  100. // Load the XML file
  101. $xml = simplexml_load_file($xml_filename);
  102. /**
  103. * We are using the drupal table theme functionality to create this listing
  104. * @see theme_table() for additional documentation
  105. */
  106. if ($xml) {
  107. // Specify the header of the table
  108. $header = array(
  109. 'arrow-col' => array('data' => '', 'class' => array('arrow-col')),
  110. 'number' => array('data' => '#', 'class' => array('number')),
  111. 'query' => array('data' => 'Query Name (Click for alignment & visualization)', 'class' => array('query')),
  112. 'hit' => array('data' => 'Target Name', 'class' => array('hit')),
  113. 'evalue' => array('data' => 'E-Value', 'class' => array('evalue')),
  114. );
  115. $rows = array();
  116. $count = 0;
  117. // Parse the BLAST XML to generate the rows of the table
  118. // where each hit results in two rows in the table: 1) A summary of the query/hit and
  119. // significance and 2) additional information including the alignment
  120. foreach ($xml->{'BlastOutput_iterations'}->children() as $iteration) {
  121. $children_count = $iteration->{'Iteration_hits'}->children()->count();
  122. //@deepaksomanadh: Code added for BLAST visualization
  123. // parameters that need to be passed for BLAST image generation
  124. $target_name = '';
  125. $q_name = $xml->{'BlastOutput_query-def'};
  126. $query_size = $xml->{'BlastOutput_query-len'};
  127. $target_size = $iteration->{'Iteration_stat'}->{'Statistics'}->{'Statistics_db-len'};
  128. if ($children_count != 0) {
  129. foreach ($iteration->{'Iteration_hits'}->children() as $hit) {
  130. if (is_object($hit)) {
  131. $count +=1;
  132. $zebra_class = ($count % 2 == 0) ? 'even' : 'odd';
  133. $no_hits = FALSE;
  134. // RETRIEVE INFO
  135. $hit_name = (preg_match('/BL_ORD_ID/', $hit->{'Hit_id'})) ? $hit->{'Hit_def'} : $hit->{'Hit_id'};
  136. $score = $hit->{'Hit_hsps'}->{'Hsp'}->{'Hsp_score'};
  137. $evalue = $hit->{'Hit_hsps'}->{'Hsp'}->{'Hsp_evalue'};
  138. $query_name = $iteration->{'Iteration_query-def'};
  139. // Round e-val to two decimal values
  140. $rounded_evalue = '';
  141. if (strpos($evalue,'e') != false) {
  142. $evalue_split = explode('e', $evalue);
  143. $rounded_evalue = round($evalue_split[0], 2, PHP_ROUND_HALF_EVEN);
  144. $rounded_evalue .= 'e' . $evalue_split[1];
  145. }
  146. else {
  147. $rounded_evalue = $evalue;
  148. }
  149. // ALIGNMENT ROW (collapsed by default)
  150. // Process HSPs
  151. // @deepaksomanadh: Code added for BLAST visualization
  152. // hit array and corresponding bit scores
  153. // hits=4263001_4262263_1_742;4260037_4259524_895_1411;&scores=722;473;
  154. $HSPs = array();
  155. $track_start = INF;
  156. $track_end = -1;
  157. $hsps_range = '';
  158. $hit_hsps = '';
  159. $hit_hsp_score = '';
  160. $target_size = $hit->{'Hit_len'};
  161. foreach ($hit->{'Hit_hsps'}->children() as $hsp_xml) {
  162. $HSPs[] = (array) $hsp_xml;
  163. if ($track_start > $hsp_xml->{'Hsp_hit-from'}) {
  164. $track_start = $hsp_xml->{'Hsp_hit-from'} . "";
  165. }
  166. if ($track_end < $hsp_xml->{'Hsp_hit-to'}) {
  167. $track_end = $hsp_xml->{'Hsp_hit-to'} . "";
  168. }
  169. }
  170. $range_start = (int) $track_start - 50000;
  171. $range_end = (int) $track_end + 50000;
  172. if ($range_start < 1)
  173. $range_start = 1;
  174. // For BLAST visualization
  175. $target_size = $hit->{'Hit_len'};
  176. foreach ($hit->{'Hit_hsps'}->children() as $hsp_xml) {
  177. $hit_hsps .= $hsp_xml->{'Hsp_hit-from'} . '_' .
  178. $hsp_xml->{'Hsp_hit-to'} . '_' .
  179. $hsp_xml->{'Hsp_query-from'} . '_' . $hsp_xml->{'Hsp_query-to'} .
  180. ';';
  181. $Hsp_bit_score .= $hsp_xml->{'Hsp_bit-score'} .';';
  182. }
  183. // SUMMARY ROW
  184. // If the id is of the form gnl|BL_ORD_ID|### then the parseids flag
  185. // to makeblastdb did a really poor job. In this case we want to use
  186. // the def to provide the original FASTA header.
  187. // If our BLAST DB is configured to handle link-outs then use the
  188. // regex & URL prefix provided to create one.
  189. $hit_name = $hit->{'Hit_def'};
  190. $query_name = $iteration->{'Iteration_query-def'};
  191. if ($linkout) {
  192. //echo "link out regex: $linkout_regex executed on [$hit_name]<br>";
  193. //preg_match($linkout_regex, $hit_name, $linkout_match);
  194. //echo "<br>matches:<pre>" . var_dump($linkout_match);echo "</pre>";
  195. if (preg_match($linkout_regex, $hit_name, $linkout_match)) {
  196. $linkout_id = $linkout_match[1];
  197. $hit->{'linkout_id'} = $linkout_id;
  198. $hit->{'hit_name'} = $hit_name;
  199. }
  200. $hit_url = call_user_func(
  201. $url_function,
  202. $linkout_urlprefix,
  203. $hit,
  204. array(
  205. 'query_name' => $query_name,
  206. 'score' => $score,
  207. 'e-value' => $evalue,
  208. 'HSPs' => $HSPs,
  209. 'Target' => $blastdb->title,
  210. )
  211. );
  212. // The linkout id might have been set/changed by the custom linkout code.
  213. if ($linkout_type == 'custom' && $hit->{'linkout_id'}) {
  214. $linkout_id = $hit->{'linkout_id'};
  215. }
  216. if ($hit_url) {
  217. /* eksc- l() URL-encodes the URL path too, which is often not what we want.
  218. $hit_name = l(
  219. $linkout_id,
  220. $hit_url,
  221. array('attributes' => array('target' => '_blank'))
  222. );
  223. */
  224. $hit_name = "
  225. <a href=\"$hit_url\" target=\"_blank\">
  226. $linkout_id
  227. </a>";
  228. }
  229. }//handle linkout
  230. //@deepaksomanadh: Code added for BLAST visualization
  231. // get the image and display
  232. $hit_img = generateImage($target_name, $Hsp_bit_score, $hit_hsps,
  233. $target_size, $query_size, $q_name, $hit_name);
  234. ob_start(); // Start buffering the output
  235. imagepng($hit_img, null, 0, PNG_NO_FILTER);
  236. $b64 = base64_encode(ob_get_contents()); // Get what we've just outputted and base64 it
  237. imagedestroy($hit_img);
  238. ob_end_clean();
  239. // Print the HTML tag with the image embedded
  240. $hit_img = '<h4><strong> Hit Visualization </strong></h4> <br><img src="data:image/png;base64,'.$b64.'"/>';
  241. $row = array(
  242. 'data' => array(
  243. 'arrow-col' => array('data' => '<div class="arrow"></div>', 'class' => array('arrow-col')),
  244. 'number' => array('data' => $count, 'class' => array('number')),
  245. 'query' => array('data' => $query_name, 'class' => array('query')),
  246. 'hit' => array('data' => $hit_name, 'class' => array('hit')),
  247. 'evalue' => array('data' => $rounded_evalue, 'class' => array('evalue')),
  248. 'arrow-col' => array('data' => '<div class="arrow"></div>', 'class' => array('arrow-col'))
  249. ),
  250. 'class' => array('result-summary')
  251. );
  252. $rows[] = $row;
  253. // ALIGNMENT ROW (collapsed by default)
  254. // Process HSPs
  255. $row = array(
  256. 'data' => array(
  257. 'arrow' => '',
  258. 'number' => '',
  259. 'query' => array(
  260. 'data' => theme('blast_report_alignment_row', array('HSPs' => $HSPs)),
  261. // 'colspan' => 4,
  262. ),
  263. 'hit' => array(
  264. 'data' => $hit_img,
  265. 'colspan' => 3,
  266. ),
  267. ),
  268. 'class' => array('alignment-row', $zebra_class),
  269. 'no_striping' => TRUE
  270. );
  271. $rows[] = $row;
  272. }//end of if - checks $hit
  273. }//end of foreach - iteration_hits
  274. }//end of if - check for iteration_hits
  275. else {
  276. // Currently where the "no results" is added.
  277. $query_name = $iteration->{'Iteration_query-def'};
  278. $query_with_no_hits[] = $query_name;
  279. }//no results
  280. }//end of foreach - BlastOutput_iterations
  281. if ($no_hits) {
  282. print '<p class="no-hits-message">No results found.</p>';
  283. }
  284. else {
  285. // We want to warn the user if some of their query sequences had no hits.
  286. if (!empty($query_with_no_hits)) {
  287. print '<p class="no-hits-message">Some of your query sequences did not '
  288. . 'match to the database/template. They are: '
  289. . implode(', ', $query_with_no_hits) . '.</p>';
  290. }
  291. // Actually print the table.
  292. if (!empty($rows)) {
  293. print theme('table', array(
  294. 'header' => $header,
  295. 'rows' => $rows,
  296. 'attributes' => array('id' => 'blast_report'),
  297. ));
  298. }
  299. }//handle no hits
  300. }//XML exists
  301. else {
  302. drupal_set_title('BLAST: Error Encountered');
  303. print '<p>We encountered an error and are unable to load your BLAST results.</p>';
  304. }
  305. ?>
  306. <p>
  307. <a style ="align:center" href="<?php print '../../'. $job_id_data['job_url'] . '?jid=' . base64_encode($job_id) ?>">Edit this query and re-submit</a>
  308. </p>
  309. <?php echo get_recent_jobs(); ?>