blast_align_image.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?PHP
  2. /* file: blast_align_image.php
  3. *
  4. * purpose: generate an image of HSPs for a given target
  5. *
  6. * URL paramters:
  7. * acc - target name
  8. * name - query name, false if none
  9. * tsize - target size
  10. * qsize - query size
  11. * hits - each hit represented in URL as:
  12. * targetstart_targetend_hspstart_hspend;
  13. * score - score for each hit
  14. *
  15. * Example: <url>blast_align_image.php?acc=chr2&name=query1&tsize=237068873&qsize=1411&hits=4263001_4262263_1_742;4260037_4259524_895_1411;4260405_4260248_740_897;192158716_192158843_612_742;&scores=722;473;155;51;
  16. *
  17. * history:
  18. * 09/23/10 Carson created
  19. * 04/16/12 eksc adapted into POPcorn code
  20. * 03/12/15 deepak Adapted code into Tripal BLAST
  21. */
  22. /* include_once('../../inc/lib.php');
  23. session_start();
  24. $pc_system = getSystemInfoPC('sequence_search');
  25. $acc = getCGIParamPC('acc', 'GP', '');
  26. $scores = getCGIParamPC('scores', 'GP', '');
  27. $links = getCGIParamPC('links', 'GP', '');
  28. $hits = getCGIParamPC('hits', 'GP', '');
  29. $tsize = intval(getCGIParamPC('tsize', 'GP', ''));
  30. $qsize = intval(getCGIParamPC('qsize', 'GP', ''));
  31. $name = getCGIParamPC('name', 'GP', '');
  32. */
  33. // extract hit information from hit param
  34. function generateImage($acc = '', $scores, $hits, $tsize, $qsize, $name) {
  35. $tok = strtok($hits, ";");
  36. $b_hits = Array();
  37. while ($tok !== false) {
  38. $b_hits[] = $tok;
  39. $tok = strtok(";");
  40. }
  41. // extract score information from score param
  42. $tokscr = strtok($scores, ";");
  43. $b_scores = Array();
  44. while ($tokscr !== false) {
  45. $b_scores[] = $tokscr;
  46. $tokscr = strtok(";");
  47. }
  48. // image measurements
  49. $height = 200 + (count($b_hits) * 16);
  50. $width = 520;
  51. $img = imagecreatetruecolor($width, $height);
  52. $white = imagecolorallocate($img, 255, 255, 255);
  53. $black = imagecolorallocate($img, 0, 0, 0);
  54. $darkgray = imagecolorallocate($img, 100, 100, 100);
  55. $strong = imagecolorallocatealpha($img, 202, 0, 0, 15);
  56. $moderate = imagecolorallocatealpha($img, 204, 102, 0, 20);
  57. $present = imagecolorallocatealpha($img, 204, 204, 0, 35);
  58. $weak = imagecolorallocatealpha($img, 102, 204, 0, 50);
  59. $gray = imagecolorallocate($img, 190, 190, 190);
  60. $lightgray = imagecolorallocate($img, 230, 230, 230);
  61. imagefill($img, 0, 0, $lightgray);
  62. // Target coordinates
  63. $maxlength = 300;
  64. $t_length = ($tsize > $qsize)
  65. ? $maxlength : $maxlength - 50;
  66. $q_length = ($qsize > $tsize)
  67. ? $maxlength : $maxlength - 50;
  68. $tnormal = $t_length / $tsize;
  69. $qnormal = $q_length / $qsize;
  70. $t_ystart = 30;
  71. $t_yend = $t_ystart + 20;
  72. $t_xstart = 50;
  73. $t_xend = $t_xstart + $t_length;
  74. $t_center = $t_xstart + ($t_length / 2);
  75. // Target labels
  76. $warn = " (not to scale)";
  77. imagestring($img, 5, $t_xstart, $t_ystart-20, $acc.$warn, $black);
  78. imagestring($img, 3, 5, $t_ystart+2, "Target", $black);
  79. // Draw bar representing target
  80. imagefilledrectangle($img, $t_xstart, $t_ystart, $t_xend, $t_yend, $gray);
  81. imagerectangle($img, $t_xstart, $t_ystart, $t_xend, $t_yend, $darkgray);
  82. // query coordinates
  83. $q_maxheight = 250;
  84. $q_ystart = $t_yend + 100;
  85. $q_yend = $q_ystart + 20;
  86. $q_xstart = $t_center - $q_length / 2;
  87. $q_xend = $q_xstart + $q_length;
  88. $q_center = ($q_xend + $q_xstart) / 2;
  89. $q_xwidth = $q_xend - $q_xstart;
  90. // Query labels
  91. imagestring($img, 5, $q_xstart, $q_yend+2, $name.$warn, $black);
  92. imagestring($img, 3, 5, $q_ystart+2, 'Query', $black);
  93. // Draw bar representing query
  94. imagefilledrectangle($img, $q_xstart, $q_ystart, $q_xend, $q_yend, $gray);
  95. imagerectangle($img ,$q_xstart, $q_ystart, $q_xend, $q_yend, $darkgray);
  96. // HSP bars will start here
  97. $hsp_bary = $q_yend + 20;
  98. // Draw solids for HSP alignments
  99. for ($ii=count($b_hits)-1; $ii>=0; $ii--) {
  100. // alignment
  101. $cur_hit = $b_hits[$ii];
  102. $cur_score = intval($b_scores[$ii]);
  103. // set color according to score
  104. $cur_color = $darkgray;
  105. if ($cur_score > 200) {
  106. $cur_color = $strong;
  107. }
  108. else if ($cur_score > 80 && $cur_score <= 200) {
  109. $cur_color = $moderate;
  110. }
  111. else if ($cur_score > 50 && $cur_score <= 80) {
  112. $cur_color = $present;
  113. }
  114. else if ($cur_score > 40 && $cur_score <= 50) {
  115. $cur_color = $weak;
  116. }
  117. $t_start = $tnormal * intval(strtok($cur_hit, "_")) + $t_xstart;
  118. $t_end = $tnormal * intval(strtok("_")) + $t_xstart;
  119. $q_start = $qnormal * intval(strtok("_")) + $q_xstart;
  120. $q_end = $qnormal * intval(strtok("_")) + $q_xstart;
  121. $hit1_array = array($t_start, $t_yend, $t_end, $t_yend, $q_end,
  122. $q_ystart, $q_start, $q_ystart);
  123. // HSP coords
  124. imagefilledpolygon($img, $hit1_array, 4, $cur_color);
  125. }//each hit
  126. // Draw lines over fills for HSP alignments
  127. for ($ii=0; $ii<count($b_hits); $ii++) {
  128. // alignment
  129. $cur_hit = $b_hits[$ii];
  130. $t_start = $tnormal * intval(strtok($cur_hit, "_")) + $t_xstart;
  131. $t_end = $tnormal * intval(strtok("_")) + $t_xstart;
  132. $q_start = $qnormal * intval(strtok("_")) + $q_xstart;
  133. $q_end = $qnormal * intval(strtok("_")) + $q_xstart;
  134. $hit1_array = array($t_start, $t_yend, $t_end, $t_yend, $q_end, $q_ystart,
  135. $q_start, $q_ystart,);
  136. imagerectangle($img, $t_start, $t_ystart, $t_end, $t_yend, $black);
  137. imagerectangle($img, $q_start, $q_ystart, $q_end, $q_yend, $black);
  138. imagepolygon ($img, $hit1_array, 4, $black);
  139. // show HSP
  140. imagestring($img, 3, 2, $hsp_bary, ($acc ."Hit" . $ii), $black);
  141. $cur_score = intval($b_scores[$ii]);
  142. // set color according to score
  143. $cur_color = $darkgray;
  144. if ($cur_score > 200) {
  145. $cur_color = $strong;
  146. }
  147. else if ($cur_score > 80 && $cur_score <= 200) {
  148. $cur_color = $moderate;
  149. }
  150. else if ($cur_score > 50 && $cur_score <= 80) {
  151. $cur_color = $present;
  152. }
  153. else if ($cur_score > 40 && $cur_score <= 50) {
  154. $cur_color = $weak;
  155. }
  156. imagefilledrectangle($img, $q_start, $hsp_bary, $q_end, $hsp_bary+10, $cur_color);
  157. $hsp_bary += 15;
  158. }//each hit
  159. // Draw the key
  160. $xchart = 390;
  161. $ychart = 10;
  162. $fontsize = 4;
  163. $yinc = 20;
  164. $ywidth = 7;
  165. $xinc = 10;
  166. imagestring($img, 5, $xchart, $ychart - 5, "Scores", $black);
  167. imagestring($img, $fontsize, $xchart + $yinc + $xinc,$ychart + ($yinc * 1) + $ywidth, ">= 200" , $black);
  168. imagestring($img, $fontsize, $xchart + $yinc + $xinc,$ychart + ($yinc * 2) + $ywidth, "80 - 200" , $black);
  169. imagestring($img, $fontsize, $xchart + $yinc + $xinc,$ychart + ($yinc * 3) + $ywidth, "50 - 80" , $black);
  170. imagestring($img, $fontsize, $xchart + $yinc + $xinc,$ychart + ($yinc * 4) + $ywidth, "40 - 50" , $black);
  171. imagestring($img, $fontsize, $xchart + $yinc + $xinc,$ychart + ($yinc * 5) + $ywidth, "< 40" , $black);
  172. imagefilledRectangle($img, $xchart, $ychart + ($yinc * 1) + $xinc, $xchart + $yinc, $ychart + ($yinc * 2), $strong);
  173. imagefilledRectangle($img, $xchart, $ychart + ($yinc * 2) + $xinc, $xchart + $yinc, $ychart + ($yinc * 3), $moderate);
  174. imagefilledRectangle($img, $xchart, $ychart + ($yinc * 3) + $xinc, $xchart + $yinc, $ychart + ($yinc * 4), $present);
  175. imagefilledRectangle($img, $xchart, $ychart + ($yinc * 4) + $xinc, $xchart + $yinc, $ychart + ($yinc * 5), $weak);
  176. imagefilledRectangle($img, $xchart, $ychart + ($yinc * 5) + $xinc, $xchart + $yinc, $ychart + ($yinc * 6), $darkgray);
  177. return $img;
  178. // write out images
  179. // header("Content-type: image/png");
  180. // imagepng($img);
  181. }
  182. ?>