blast_report.tpl.php 17 KB

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