blast_align_image.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. // extract hit information from hit param
  23. function generateImage($acc = '', $scores, $hits, $tsize, $qsize, $name, $hit_name) {
  24. $tok = strtok($hits, ";");
  25. $b_hits = Array();
  26. while ($tok !== false) {
  27. $b_hits[] = $tok;
  28. $tok = strtok(";");
  29. }
  30. // extract score information from score param
  31. $tokscr = strtok($scores, ";");
  32. $b_scores = Array();
  33. while ($tokscr !== false) {
  34. $b_scores[] = $tokscr;
  35. $tokscr = strtok(";");
  36. }
  37. // image measurements
  38. $height = 200 + (count($b_hits) * 16);
  39. $width = 520;
  40. $img = imagecreatetruecolor($width, $height);
  41. $white = imagecolorallocate($img, 255, 255, 255);
  42. $black = imagecolorallocate($img, 0, 0, 0);
  43. $darkgray = imagecolorallocate($img, 100, 100, 100);
  44. $strong = imagecolorallocatealpha($img, 202, 0, 0, 15);
  45. $moderate = imagecolorallocatealpha($img, 204, 102, 0, 20);
  46. $present = imagecolorallocatealpha($img, 204, 204, 0, 35);
  47. $weak = imagecolorallocatealpha($img, 102, 204, 0, 50);
  48. $gray = imagecolorallocate($img, 190, 190, 190);
  49. $lightgray = imagecolorallocate($img, 230, 230, 230);
  50. imagefill($img, 0, 0, $lightgray);
  51. // Target coordinates
  52. $maxlength = 300;
  53. $t_length = ($tsize > $qsize)
  54. ? $maxlength : $maxlength - 50;
  55. $q_length = ($qsize > $tsize)
  56. ? $maxlength : $maxlength - 50;
  57. $tnormal = $t_length / $tsize;
  58. $qnormal = $q_length / $qsize;
  59. $t_ystart = 30;
  60. $t_yend = $t_ystart + 20;
  61. $t_xstart = 50;
  62. $t_xend = $t_xstart + $t_length;
  63. $t_center = $t_xstart + ($t_length / 2);
  64. // Target labels
  65. $warn = '"'. $hit_name . '"';
  66. imagestring($img, 5, $t_xstart, $t_ystart-20, $acc.$warn, $black);
  67. imagestring($img, 3, 5, $t_ystart+2, "Target", $black);
  68. // Draw bar representing target
  69. imagefilledrectangle($img, $t_xstart, $t_ystart, $t_xend, $t_yend, $gray);
  70. imagerectangle($img, $t_xstart, $t_ystart, $t_xend, $t_yend, $darkgray);
  71. // query coordinates
  72. $q_maxheight = 250;
  73. $q_ystart = $t_yend + 100;
  74. $q_yend = $q_ystart + 20;
  75. $q_xstart = $t_center - $q_length / 2;
  76. $q_xend = $q_xstart + $q_length;
  77. $q_center = ($q_xend + $q_xstart) / 2;
  78. $q_xwidth = $q_xend - $q_xstart;
  79. // Query labels
  80. imagestring($img, 5, $q_xstart, $q_yend+2, $name, $black);
  81. imagestring($img, 3, $q_xstart, $q_ystart+2, 'Query', $black);
  82. // Draw bar representing query
  83. imagefilledrectangle($img, $q_xstart, $q_ystart, $q_xend, $q_yend, $gray);
  84. imagerectangle($img ,$q_xstart, $q_ystart, $q_xend, $q_yend, $darkgray);
  85. // HSP bars will start here
  86. $hsp_bary = $q_yend + 20;
  87. // Draw solids for HSP alignments
  88. for ($ii=count($b_hits)-1; $ii>=0; $ii--) {
  89. // alignment
  90. $cur_hit = $b_hits[$ii];
  91. $cur_score = intval($b_scores[$ii]);
  92. // set color according to score
  93. $cur_color = $darkgray;
  94. if ($cur_score > 200) {
  95. $cur_color = $strong;
  96. }
  97. else if ($cur_score > 80 && $cur_score <= 200) {
  98. $cur_color = $moderate;
  99. }
  100. else if ($cur_score > 50 && $cur_score <= 80) {
  101. $cur_color = $present;
  102. }
  103. else if ($cur_score > 40 && $cur_score <= 50) {
  104. $cur_color = $weak;
  105. }
  106. $t_start = $tnormal * intval(strtok($cur_hit, "_")) + $t_xstart;
  107. $t_end = $tnormal * intval(strtok("_")) + $t_xstart;
  108. $q_start = $qnormal * intval(strtok("_")) + $q_xstart;
  109. $q_end = $qnormal * intval(strtok("_")) + $q_xstart;
  110. $hit1_array = array($t_start, $t_yend, $t_end, $t_yend, $q_end,
  111. $q_ystart, $q_start, $q_ystart);
  112. // HSP coords
  113. imagefilledpolygon($img, $hit1_array, 4, $cur_color);
  114. }//each hit
  115. // Draw lines over fills for HSP alignments
  116. for ($ii=0; $ii<count($b_hits); $ii++) {
  117. // alignment
  118. $cur_hit = $b_hits[$ii];
  119. $t_start = $tnormal * intval(strtok($cur_hit, "_")) + $t_xstart;
  120. $t_end = $tnormal * intval(strtok("_")) + $t_xstart;
  121. $q_start = $qnormal * intval(strtok("_")) + $q_xstart;
  122. $q_end = $qnormal * intval(strtok("_")) + $q_xstart;
  123. $hit1_array = array($t_start, $t_yend, $t_end, $t_yend, $q_end, $q_ystart,
  124. $q_start, $q_ystart,);
  125. imagerectangle($img, $t_start, $t_ystart, $t_end, $t_yend, $black);
  126. imagerectangle($img, $q_start, $q_ystart, $q_end, $q_yend, $black);
  127. imagepolygon ($img, $hit1_array, 4, $black);
  128. // show HSP
  129. imagestring($img, 3, 2, $hsp_bary, ($acc ."HSP" . ($ii + 1)), $black);
  130. $cur_score = intval($b_scores[$ii]);
  131. // set color according to score
  132. $cur_color = $darkgray;
  133. if ($cur_score > 200) {
  134. $cur_color = $strong;
  135. }
  136. else if ($cur_score > 80 && $cur_score <= 200) {
  137. $cur_color = $moderate;
  138. }
  139. else if ($cur_score > 50 && $cur_score <= 80) {
  140. $cur_color = $present;
  141. }
  142. else if ($cur_score > 40 && $cur_score <= 50) {
  143. $cur_color = $weak;
  144. }
  145. imagefilledrectangle($img, $q_start, $hsp_bary, $q_end, $hsp_bary+10, $cur_color);
  146. $hsp_bary += 15;
  147. }//each hit
  148. // Draw the key
  149. $xchart = 390;
  150. $ychart = 10;
  151. $fontsize = 4;
  152. $yinc = 20;
  153. $ywidth = 7;
  154. $xinc = 10;
  155. imagestring($img, 5, $xchart, $ychart - 5, "Bit Scores", $black);
  156. imagestring($img, $fontsize, $xchart + $yinc + $xinc,$ychart + ($yinc * 1) + $ywidth, ">= 200" , $black);
  157. imagestring($img, $fontsize, $xchart + $yinc + $xinc,$ychart + ($yinc * 2) + $ywidth, "80 - 200" , $black);
  158. imagestring($img, $fontsize, $xchart + $yinc + $xinc,$ychart + ($yinc * 3) + $ywidth, "50 - 80" , $black);
  159. imagestring($img, $fontsize, $xchart + $yinc + $xinc,$ychart + ($yinc * 4) + $ywidth, "40 - 50" , $black);
  160. imagestring($img, $fontsize, $xchart + $yinc + $xinc,$ychart + ($yinc * 5) + $ywidth, "< 40" , $black);
  161. imagefilledRectangle($img, $xchart, $ychart + ($yinc * 1) + $xinc, $xchart + $yinc, $ychart + ($yinc * 2), $strong);
  162. imagefilledRectangle($img, $xchart, $ychart + ($yinc * 2) + $xinc, $xchart + $yinc, $ychart + ($yinc * 3), $moderate);
  163. imagefilledRectangle($img, $xchart, $ychart + ($yinc * 3) + $xinc, $xchart + $yinc, $ychart + ($yinc * 4), $present);
  164. imagefilledRectangle($img, $xchart, $ychart + ($yinc * 4) + $xinc, $xchart + $yinc, $ychart + ($yinc * 5), $weak);
  165. imagefilledRectangle($img, $xchart, $ychart + ($yinc * 5) + $xinc, $xchart + $yinc, $ychart + ($yinc * 6), $darkgray);
  166. return $img;
  167. }
  168. ?>