blast_report.tpl.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. //eksc- linkout vs gbrowse
  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 open a new window with a genome browser around this 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. //eksc- linkout vs gbrowse
  194. $hit_name = $hit->{'Hit_def'};
  195. $query_name = $iteration->{'Iteration_query-def'};
  196. // ***** Future modification ***** The gbrowse_url can be extracted from Tripal Database table
  197. if(preg_match('/.*(aradu).*/i', $hit_name) == 1) {
  198. $gbrowse_url = 'gbrowse_aradu1.0';
  199. }
  200. else if(preg_match('/.*(araip).*/i', $hit_name) == 1) {
  201. $gbrowse_url = 'gbrowse_araip1.0';
  202. } else if(preg_match('/.*(phytozome).*/i', $hit_name) == 1) {
  203. $gbrowse_url = 'http://legumeinfo.org/chado_phylotree/';
  204. } else {
  205. // Not existing in available GBrowse tracks
  206. $gbrowse_url = null;
  207. }
  208. // $hit_name_url = l($linkout_urlprefix . $linkout_match[1],
  209. // array('attributes' => array('target' => '_blank'))
  210. // );
  211. // Link out functionality to GBrowse
  212. if($gbrowse_url == null) {
  213. // Not a valid hit. Hence, No link outs to GBrowse and the hit name is displayed.
  214. $hit_name_url = $hit_name;
  215. }
  216. else {
  217. // Link out is possible for this hit
  218. // Check if our BLAST DB is configured to handle link-outs then use the
  219. // regex & URL prefix provided to create one.
  220. // Then, check if the db is configured to handle linkouts
  221. // For alias targets
  222. if ($linkout) {
  223. // For CDS/protein alias targets
  224. if(preg_match("/http:/", $gbrowse_url) == 1) {
  225. $hit_url = $gbrowse_url . $hit_name ;
  226. $hit_name_url = l($hit_name, $hit_url, array('attributes' => array('target' => '_blank')));
  227. }
  228. else if (preg_match($linkout_regex, $hit_name, $linkout_match) == 1) {
  229. $hit_name = $linkout_match[1];
  230. // matches found
  231. $hit_url = $GLOBALS['base_url'] . '/' . $gbrowse_url . '?' . 'query=q=';
  232. $hit_url .= $hit_name . ';h_feat=' . $iteration->{'Iteration_query-ID'};
  233. $hit_name_url = l($hit_name, $hit_url, array('attributes' => array('target' => '_blank')));
  234. }
  235. else {
  236. // No matches for regex. Hence, linkouts not possible
  237. $hit_name_url = $hit_name;
  238. }
  239. }
  240. else {
  241. // For Genome targets
  242. $hit_url = $GLOBALS['base_url'] . '/' . $gbrowse_url . '?' . 'query=' . 'start=' . $range_start . ';' . 'stop=' .
  243. $range_end . ';' . 'ref=' . $hit_name . ';' . 'add=' . $hit_name . '+' . 'BLAST+' . $iteration->{'Iteration_query-ID'} . '+' . $hsps_range . ';h_feat=' . $iteration->{'Iteration_query-ID'} ;
  244. $hit_name_url = l($hit_name, $hit_url, array('attributes' => array('target' => '_blank')));
  245. }
  246. }// end of GBrowse functionality
  247. /* eksc- linkout vs gbrowse
  248. if ($linkout) {
  249. if (preg_match($linkout_regex, $hit_name, $linkout_match)) {
  250. $linkout_id = $linkout_match[1];
  251. $hit->{'linkout_id'} = $linkout_id;
  252. $hit->{'hit_name'} = $hit_name;
  253. $hit_url = call_user_func(
  254. $url_function,
  255. $linkout_urlprefix,
  256. $hit,
  257. array(
  258. 'query_name' => $query_name,
  259. 'score' => $score,
  260. 'e-value' => $evalue,
  261. 'HSPs' => $HSPs
  262. )
  263. );
  264. if ($hit_url) {
  265. $hit_name = l(
  266. $linkout_id,
  267. $hit_url,
  268. array('attributes' => array('target' => '_blank'))
  269. );
  270. }
  271. }
  272. }
  273. */
  274. //@deepaksomanadh: Code added for BLAST visualization
  275. // get the image and display
  276. $hit_img = generateImage($target_name, $Hsp_bit_score, $hit_hsps, $target_size, $query_size, $q_name, $hit_name);
  277. ob_start(); // Start buffering the output
  278. imagepng($hit_img, null, 0, PNG_NO_FILTER);
  279. $b64 = base64_encode(ob_get_contents()); // Get what we've just outputted and base64 it
  280. imagedestroy($hit_img);
  281. ob_end_clean();
  282. // Print the HTML tag with the image embedded
  283. $hit_img = '<h4><strong> Hit Visualization </strong></h4> <br><img src="data:image/png;base64,'.$b64.'"/>';
  284. $row = array(
  285. 'data' => array(
  286. 'arrow-col' => array('data' => '<div class="arrow"></div>', 'class' => array('arrow-col')),
  287. 'number' => array('data' => $count, 'class' => array('number')),
  288. 'query' => array('data' => $query_name, 'class' => array('query')),
  289. 'hit' => array('data' => $hit_name, 'class' => array('hit')),
  290. 'evalue' => array('data' => $rounded_evalue, 'class' => array('evalue')),
  291. 'arrow-col' => array('data' => '<div class="arrow"></div>', 'class' => array('arrow-col'))
  292. ),
  293. 'class' => array('result-summary')
  294. );
  295. $rows[] = $row;
  296. // ALIGNMENT ROW (collapsed by default)
  297. // Process HSPs
  298. $row = array(
  299. 'data' => array(
  300. 'arrow' => '',
  301. 'number' => '',
  302. 'query' => array(
  303. 'data' => theme('blast_report_alignment_row', array('HSPs' => $HSPs)),
  304. // 'colspan' => 4,
  305. ),
  306. 'hit' => array(
  307. 'data' => $hit_img,
  308. 'colspan' => 3,
  309. ),
  310. ),
  311. 'class' => array('alignment-row', $zebra_class),
  312. 'no_striping' => TRUE
  313. );
  314. $rows[] = $row;
  315. }// end of if - checks $hit
  316. } //end of foreach - iteration_hits
  317. } // end of if - check for iteration_hits
  318. else {
  319. // Currently where the "no results" is added.
  320. $query_name = $iteration->{'Iteration_query-def'};
  321. $query_with_no_hits[] = $query_name;
  322. }// end of else
  323. }//end of foreach - BlastOutput_iterations
  324. if ($no_hits) {
  325. print '<p class="no-hits-message">No results found.</p>';
  326. }
  327. else {
  328. // We want to warn the user if some of their query sequences had no hits.
  329. if (!empty($query_with_no_hits)) {
  330. print '<p class="no-hits-message">Some of your query sequences did not '
  331. . 'match to the database/template. They are: '
  332. . implode(', ', $query_with_no_hits) . '.</p>';
  333. }
  334. // Actually print the table.
  335. if (!empty($rows)) {
  336. print theme('table', array(
  337. 'header' => $header,
  338. 'rows' => $rows,
  339. 'attributes' => array('id' => 'blast_report'),
  340. ));
  341. }
  342. }
  343. }
  344. else {
  345. drupal_set_title('BLAST: Error Encountered');
  346. print '<p>We encountered an error and are unable to load your BLAST results.</p>';
  347. }
  348. ?>
  349. <p> <!-- @deepaksomanadh: Building the edit and resubmit URL -->
  350. <a style ="align:center" href="<?php print '../../'. $job_id_data['job_url'] . '?jid=' . base64_encode($job_id) ?>">Edit this query and re-submit</a>
  351. </p>
  352. <strong> Recent Jobs </strong>
  353. <ol>
  354. <?php
  355. echo "<pre>";var_dump($job_id_data);echo "</pre>";
  356. $sid = session_id();
  357. $jobs = $_SESSION['all_jobs'][$sid];
  358. foreach ($jobs as $job) {
  359. echo "<pre>";var_dump($job);echo "</pre>";
  360. echo "<li>";
  361. $q_def = !isset($job['query_defs'][0]) ? "Query" : $job['query_defs'][0];
  362. echo "
  363. <a href='" . "../../" . $job['job_output_url'] ."'>
  364. Q:$q_def, T:" . $job['target'] . ' (' . $job['program'] . ') - ' . $job['date'] . "
  365. </a>";
  366. echo "</li>";
  367. }
  368. ?>
  369. </ol>