blast_report.tpl.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 = TRUE;
  13. $linkout_regex = $blastdb->linkout->regex;
  14. if (isset($blastdb->linkout->db_id->urlprefix) AND !empty($blastdb->linkout->db_id->urlprefix)) {
  15. $linkout_urlprefix = $blastdb->linkout->db_id->urlprefix;
  16. // Furthermore, check that we can determine the URL.
  17. // (ie: that the function specified to do so, exists).
  18. if (function_exists($blastdb->linkout->url_function)) {
  19. $url_function = $blastdb->linkout->url_function;
  20. }
  21. else {
  22. $linkout = FALSE;
  23. }
  24. }
  25. else {
  26. $linkout = FALSE;
  27. }
  28. }
  29. // Handle no hits. This following array will hold the names of all query
  30. // sequences which didn't have any hits.
  31. $query_with_no_hits = array();
  32. // Furthermore, if no query sequences have hits we don't want to bother listing
  33. // them all but just want to give a single, all-include "No Results" message.
  34. $no_hits = TRUE;
  35. ?>
  36. <!-- JQuery controlling display of the alignment information (hidden by default) -->
  37. <script type="text/javascript">
  38. $(document).ready(function(){
  39. // Hide the alignment rows in the table
  40. // (ie: all rows not labelled with the class "result-summary" which contains the tabular
  41. // summary of the hit)
  42. $("#blast_report tr:not(.result-summary)").hide();
  43. $("#blast_report tr:first-child").show();
  44. // When a results summary row is clicked then show the next row in the table
  45. // which should be corresponding the alignment information
  46. $("#blast_report tr.result-summary").click(function(){
  47. $(this).next("tr").toggle();
  48. $(this).find(".arrow").toggleClass("up");
  49. });
  50. });
  51. </script>
  52. <style>
  53. .no-hits-message {
  54. color: red;
  55. font-style: italic;
  56. }
  57. </style>
  58. <p><strong>Download</strong>:
  59. <a href="<?php print '../../' . $html_filename; ?>">HTML</a>,
  60. <a href="<?php print '../../' . $tsv_filename; ?>">Tab-Delimited</a>,
  61. <a href="<?php print '../../' . $xml_filename; ?>">XML</a>
  62. </p>
  63. <!-- @deepaksomanadh: For displaying BLAST command details -->
  64. <table>
  65. <tr>
  66. <th>Input query sequence(s) </th>
  67. <th>Target Database selected </th>
  68. <th>BLAST command executed </th>
  69. <tr>
  70. <tr>
  71. <?php
  72. // get input sequences from job_data variable
  73. $query_def = $job_id_data['query_def'];
  74. echo "<td>";
  75. echo "<ol>";
  76. foreach($query_def as $row) {
  77. echo "<li>";
  78. echo $row . "</li>";
  79. }
  80. echo "</ol></td>";
  81. echo "<td>" . $job_id_data['db_name'] . "</td>"
  82. ?>
  83. <?php
  84. include_once("blast_align_image.php");
  85. //display the BLAST command without revealing the internal path
  86. $blast_cmd = $job_id_data['program'];
  87. foreach($job_id_data['options'] as $key => $value) {
  88. $blast_cmd .= ' -' . $key. ' ' . $value ;
  89. }
  90. print "<td>" . $blast_cmd . "</td>";
  91. ?>
  92. </table>
  93. <p>The following table summarizes the results of your BLAST.
  94. Click on a <strong>triangle </strong> on the left to see the alignment and a visualization of the hit,
  95. and click the <strong>target name </strong> to open a new window with a genome browser around this hit.</p>
  96. <?php
  97. include_once("blast_align_image.php");
  98. // Load the XML file
  99. $xml = simplexml_load_file($xml_filename);
  100. /**
  101. * We are using the drupal table theme functionality to create this listing
  102. * @see theme_table() for additional documentation
  103. */
  104. if ($xml) {
  105. // Specify the header of the table
  106. $header = array(
  107. 'arrow-col' => array('data' => '', 'class' => array('arrow-col')),
  108. 'number' => array('data' => '#', 'class' => array('number')),
  109. 'query' => array('data' => 'Query Name (Click for alignment & visualization)', 'class' => array('query')),
  110. 'hit' => array('data' => 'Target Name', 'class' => array('hit')),
  111. 'evalue' => array('data' => 'E-Value', 'class' => array('evalue')),
  112. );
  113. $rows = array();
  114. $count = 0;
  115. // Parse the BLAST XML to generate the rows of the table
  116. // where each hit results in two rows in the table: 1) A summary of the query/hit and
  117. // significance and 2) additional information including the alignment
  118. foreach($xml->{'BlastOutput_iterations'}->children() as $iteration) {
  119. $children_count = $iteration->{'Iteration_hits'}->children()->count();
  120. //@deepaksomanadh: Code added for BLAST visualization
  121. // parameters that need to be passed for BLAST image generation
  122. $target_name = '';
  123. $q_name = $xml->{'BlastOutput_query-def'};
  124. $query_size = $xml->{'BlastOutput_query-len'};
  125. $target_size = $iteration->{'Iteration_stat'}->{'Statistics'}->{'Statistics_db-len'};
  126. if($children_count != 0) {
  127. foreach($iteration->{'Iteration_hits'}->children() as $hit) {
  128. if (is_object($hit)) {
  129. $count +=1;
  130. $zebra_class = ($count % 2 == 0) ? 'even' : 'odd';
  131. $no_hits = FALSE;
  132. // RETRIEVE INFO
  133. $hit_name = (preg_match('/BL_ORD_ID/', $hit->{'Hit_id'})) ? $hit->{'Hit_def'} : $hit->{'Hit_id'};
  134. $score = $hit->{'Hit_hsps'}->{'Hsp'}->{'Hsp_score'};
  135. $evalue = $hit->{'Hit_hsps'}->{'Hsp'}->{'Hsp_evalue'};
  136. $query_name = $iteration->{'Iteration_query-def'};
  137. // Round e-val to two decimal values
  138. $rounded_evalue = '';
  139. if (strpos($evalue,'e') != false) {
  140. $evalue_split = explode('e', $evalue);
  141. $rounded_evalue = round($evalue_split[0], 2, PHP_ROUND_HALF_EVEN);
  142. $rounded_evalue .= 'e' . $evalue_split[1];
  143. }
  144. else {
  145. $rounded_evalue = $evalue;
  146. }
  147. // ALIGNMENT ROW (collapsed by default)
  148. // Process HSPs
  149. // @deepaksomanadh: Code added for BLAST visualization
  150. // hit array and corresponding bit scores
  151. // hits=4263001_4262263_1_742;4260037_4259524_895_1411;&scores=722;473;
  152. $HSPs = array();
  153. $hit_hsps = '';
  154. $hit_hsp_score = '';
  155. $target_size = $hit->{'Hit_len'};
  156. foreach ($hit->{'Hit_hsps'}->children() as $hsp_xml) {
  157. $HSPs[] = (array) $hsp_xml;
  158. $hit_hsps .= $hsp_xml->{'Hsp_hit-from'} . '_' . $hsp_xml->{'Hsp_hit-to'}
  159. . '_' . $hsp_xml->{'Hsp_query-from'} . '_'
  160. . $hsp_xml->{'Hsp_query-to'} . ';';
  161. $Hsp_bit_score .= $hsp_xml->{'Hsp_bit-score'} .';';
  162. }
  163. // SUMMARY ROW
  164. // If the id is of the form gnl|BL_ORD_ID|### then the parseids flag
  165. // to makeblastdb did a really poor job. In this case we want to use
  166. // the def to provide the original FASTA header.
  167. // If our BLAST DB is configured to handle link-outs then use the
  168. // regex & URL prefix provided to create one.
  169. if ($linkout) {
  170. if (preg_match($linkout_regex, $hit_name, $linkout_match)) {
  171. $linkout_id = $linkout_match[1];
  172. $hit->{'linkout_id'} = $linkout_id;
  173. $hit->{'hit_name'} = $hit_name;
  174. $hit_url = call_user_func(
  175. $url_function,
  176. $linkout_urlprefix,
  177. $hit,
  178. array(
  179. 'query_name' => $query_name,
  180. 'score' => $score,
  181. 'e-value' => $evalue,
  182. 'HSPs' => $HSPs
  183. )
  184. );
  185. if ($hit_url) {
  186. $hit_name = l(
  187. $linkout_id,
  188. $hit_url,
  189. array('attributes' => array('target' => '_blank'))
  190. );
  191. }
  192. }
  193. }
  194. //@deepaksomanadh: Code added for BLAST visualization
  195. // get the image and display
  196. $hit_img = generateImage($target_name, $Hsp_bit_score, $hit_hsps, $target_size, $query_size, $q_name, $hit_name);
  197. ob_start(); // Start buffering the output
  198. imagepng($hit_img, null, 0, PNG_NO_FILTER);
  199. $b64 = base64_encode(ob_get_contents()); // Get what we've just outputted and base64 it
  200. imagedestroy($hit_img);
  201. ob_end_clean();
  202. // Print the HTML tag with the image embedded
  203. $hit_img = '<h4><strong> Hit Visualization </strong></h4> <br><img src="data:image/png;base64,'.$b64.'"/>';
  204. $row = array(
  205. 'data' => array(
  206. 'arrow-col' => array('data' => '<div class="arrow"></div>', 'class' => array('arrow-col')),
  207. 'number' => array('data' => $count, 'class' => array('number')),
  208. 'query' => array('data' => $query_name, 'class' => array('query')),
  209. 'hit' => array('data' => $hit_name, 'class' => array('hit')),
  210. 'evalue' => array('data' => $rounded_evalue, 'class' => array('evalue')),
  211. 'arrow-col' => array('data' => '<div class="arrow"></div>', 'class' => array('arrow-col'))
  212. ),
  213. 'class' => array('result-summary')
  214. );
  215. $rows[] = $row;
  216. // ALIGNMENT ROW (collapsed by default)
  217. // Process HSPs
  218. $row = array(
  219. 'data' => array(
  220. 'arrow' => '',
  221. 'number' => '',
  222. 'query' => array(
  223. 'data' => theme('blast_report_alignment_row', array('HSPs' => $HSPs)),
  224. // 'colspan' => 4,
  225. ),
  226. 'hit' => array(
  227. 'data' => $hit_img,
  228. 'colspan' => 3,
  229. ),
  230. ),
  231. 'class' => array('alignment-row', $zebra_class),
  232. 'no_striping' => TRUE
  233. );
  234. $rows[] = $row;
  235. }// end of if - checks $hit
  236. } //end of foreach - iteration_hits
  237. } // end of if - check for iteration_hits
  238. else {
  239. // Currently where the "no results" is added.
  240. $query_name = $iteration->{'Iteration_query-def'};
  241. $query_with_no_hits[] = $query_name;
  242. } // end of else
  243. } //end of foreach - BlastOutput_iterations
  244. if ($no_hits) {
  245. print '<p class="no-hits-message">No results found.</p>';
  246. }
  247. else {
  248. // We want to warn the user if some of their query sequences had no hits.
  249. if (!empty($query_with_no_hits)) {
  250. print '<p class="no-hits-message">Some of your query sequences did not '
  251. . 'match to the database/template. They are: '
  252. . implode(', ', $query_with_no_hits) . '.</p>';
  253. }
  254. // Actually print the table.
  255. if (!empty($rows)) {
  256. print theme('table', array(
  257. 'header' => $header,
  258. 'rows' => $rows,
  259. 'attributes' => array('id' => 'blast_report'),
  260. ));
  261. }
  262. }
  263. }
  264. else {
  265. drupal_set_title('BLAST: Error Encountered');
  266. print '<p>We encountered an error and are unable to load your BLAST results.</p>';
  267. }
  268. ?>
  269. <p> <!-- @deepaksomanadh: Building the edit and resubmit URL -->
  270. <a style ="align:center" href="<?php print '../../'. $job_id_data['job_url'] . '?jid=' . base64_encode($job_id) ?>">Edit this query and re-submit</a>
  271. </p>
  272. <strong> Recent Jobs </strong>
  273. <ol>
  274. <?php
  275. $sid = session_id();
  276. $jobs = $_SESSION['all_jobs'][$sid];
  277. foreach ( $jobs as $job) {
  278. echo "<li>";
  279. $q_def = !isset($job['query_defs'][0]) ? "Query" : $job['query_defs'][0];
  280. echo "<a href='" . "../../" . $job['job_output_url'] ."' >"
  281. . $q_def ."->". $job['program'] . "</a>";
  282. echo "</li>";
  283. }
  284. ?>
  285. </ol>