blast_report.tpl.php 13 KB

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