blast_ui.api.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. <?php
  2. /**
  3. * @file
  4. * Contains more generally applicable functions as well as some meant to help developers
  5. * Plug-in to the BLAST UI functionality
  6. */
  7. /**
  8. * Get a specific BlastDB.
  9. *
  10. * @param $identifiers
  11. * An array of identifiers used to determine which BLAST DB to retrieve.
  12. *
  13. * @return
  14. * A fully-loaded BLAST DB Node
  15. */
  16. function get_blast_database($identifiers) {
  17. $node = FALSE;
  18. if (isset($identifiers['nid'])) {
  19. $node = node_load($identifiers['nid']);
  20. }
  21. elseif (isset($identifiers['name'])) {
  22. $nid = db_query('SELECT nid FROM {blastdb} WHERE name=:name', array(':name' => $identifiers['name']))->fetchField();
  23. $node = node_load($nid);
  24. } elseif (isset($identifiers['path'])) {
  25. $nid = db_query('SELECT nid FROM {blastdb} WHERE path LIKE :path', array(':path' => db_like($identifiers['path']) . '%'))->fetchField();
  26. $node = node_load($nid);
  27. }
  28. return $node;
  29. }
  30. /**
  31. * Returns a list BLAST DATABASE options
  32. *
  33. * @param $type
  34. * The type of BLAST dabases to restrict the list to (ie: n: nucleotide or p: protein)
  35. *
  36. * @return
  37. * An array where the nid is the key and the value is the human-readable name of the option
  38. */
  39. function get_blast_database_options($type) {
  40. global $user;
  41. // Use the Entity API to get a list of BLAST Nodes to load
  42. // We use this function in order respect node access control so that
  43. // administrators can use this module in combination with a node access module
  44. // of their choice to limit access to specific BLAST databases.
  45. $query = new EntityFieldQuery();
  46. $query->entityCondition('entity_type', 'node')
  47. // Restrict to BLASTDB nodes.
  48. ->entityCondition('bundle', 'blastdb')
  49. // Restrict to Published nodes.
  50. ->propertyCondition('status', 1)
  51. // Restrict to nodes the current user has permission to view.
  52. ->addTag('node_access');
  53. $entities = $query->execute();
  54. // Get all BlastDB nodes
  55. $nodes = node_load_multiple(array_keys($entities['node']));
  56. // Support obsolete database type n/p
  57. $obs_type = '';
  58. if ($type == 'protein') {
  59. $obs_type = 'p';
  60. }
  61. else {
  62. $obs_type = 'n';
  63. }
  64. $options = array();
  65. foreach ($nodes as $node) {
  66. if ( isset($node) && isset($node->db_dbtype) ) {
  67. if ( ($node->db_dbtype == $type) OR ($node->db_dbtype == $obs_type) ) {
  68. $options[$node->nid] = $node->db_name;
  69. }
  70. }
  71. }
  72. asort($options);
  73. $options[0] = 'Select a Dataset';
  74. return $options;
  75. }
  76. /**
  77. * Retrieve all the information for a blast job in a standardized node-like format.
  78. *
  79. * @param $job_id
  80. * The non-encoded tripal job_id.
  81. * @retun
  82. * An object describing the blast job.
  83. */
  84. function get_BLAST_job($job_id) {
  85. $blastjob = db_query('SELECT * FROM blastjob WHERE job_id=:id', array(':id' => $job_id))->fetchObject();
  86. $tripal_job = tripal_get_job($job_id);
  87. $job = new stdClass();
  88. $job->job_id = $job_id;
  89. $job->program = $blastjob->blast_program;
  90. $job->options = unserialize($blastjob->options);
  91. $job->date_submitted = $tripal_job->submit_date;
  92. $job->date_started = $tripal_job->start_time;
  93. $job->date_completed = $tripal_job->end_time;
  94. // TARGET BLAST DATABASE.
  95. // If a provided blast database was used then load details.
  96. if ($blastjob->target_blastdb ) {
  97. $job->blastdb = get_blast_database(array('nid' => $blastjob->target_blastdb));
  98. }
  99. // Otherwise the user uploaded their own database so provide what information we can.
  100. else {
  101. $job->blastdb = new stdClass();
  102. $job->blastdb->db_name = 'User Uploaded';
  103. $job->blastdb->db_path = $blastjob->target_file;
  104. $job->blastdb->linkout = new stdClass();
  105. $job->blastdb->linkout->none = TRUE;
  106. if ($job->program == 'blastp' OR $job->program == 'tblastn') {
  107. $job->blastdb->db_dbtype = 'protein';
  108. }
  109. else {
  110. $job->blastdb->db_dbtype = 'nucleotide';
  111. }
  112. }
  113. // FILES.
  114. $job->files = new stdClass();
  115. $job->files->query = $blastjob->query_file;
  116. $job->files->target = $blastjob->target_file;
  117. $job->files->result = new stdClass();
  118. $job->files->result->archive = $blastjob->result_filestub . '.asn';
  119. $job->files->result->xml = $blastjob->result_filestub . '.xml';
  120. $job->files->result->tsv = $blastjob->result_filestub . '.tsv';
  121. $job->files->result->html = $blastjob->result_filestub . '.html';
  122. return $job;
  123. }
  124. /**
  125. * Run BLAST (should be called from the command-line)
  126. *
  127. * @param $program
  128. * Which BLAST program to run (ie: 'blastn', 'tblastn', tblastx', 'blastp','blastx')
  129. * @param $query
  130. * The full path and filename of the query FASTA file
  131. * @param $database
  132. * The full path and filename prefix (excluding .nhr, .nin, .nsq, etc.)
  133. * @param $output_filestub
  134. * The filename (not including path) to give the results. Should not include file type suffix
  135. * @param $options
  136. * An array of additional option where the key is the name of the option used by
  137. * BLAST (ie: 'num_alignments') and the value is relates to this particular
  138. * BLAST job (ie: 250)
  139. */
  140. function run_BLAST_tripal_job($program, $query, $database, $output_filestub, $options, $job_id = NULL) {
  141. $output_file = $output_filestub . '.asn';
  142. $output_file_xml = $output_filestub . '.xml';
  143. $output_file_tsv = $output_filestub . '.tsv';
  144. $output_file_html = $output_filestub . '.html';
  145. print "\nExecuting $program\n\n";
  146. print "Query: $query\n";
  147. print "Database: $database\n";
  148. print "Results File: $output_file\n";
  149. print "Options:\n";
  150. // Allow administrators to use an absolute path for these commands.
  151. // Defaults to using $PATH.
  152. $blast_path = variable_get('blast_path', '');
  153. $blast_threads = variable_get('blast_threads', 1);
  154. // Strip the extension off the BLAST target
  155. $database = preg_replace("/(.*)\.[pn]\w\w/", '$1', $database);
  156. // The executables:
  157. $program = $blast_path . $program;
  158. $blast_formatter_command = $blast_path . 'blast_formatter';
  159. $blast_cmd = "$program -query '$query' -db '$database' -out '$output_file' -outfmt=11";
  160. if (!empty($options)) {
  161. foreach ($options as $opt => $val) {
  162. if ($val) {
  163. print "\t$opt: $val\n";
  164. $blast_cmd .= " -$opt $val";
  165. }
  166. }
  167. }
  168. // Setting the value of threads by admin page
  169. $blast_cmd .= " -num_threads ".$blast_threads;
  170. print "\nExecuting the following BLAST command:\n" . $blast_cmd . "\n";
  171. system($blast_cmd);
  172. if (!file_exists($output_file)) {
  173. tripal_report_error(
  174. 'blast_ui',
  175. TRIPAL_ERROR,
  176. "BLAST did not complete successfully as is implied by the lack of output file (%file). The command run was @command",
  177. array('%file' => $output_file, '@command' => $blast_cmd),
  178. array('print' => TRUE)
  179. );
  180. return FALSE;
  181. }
  182. print "\nGenerating additional download formats...\n";
  183. print "\tXML\n";
  184. $format_cmd = "$blast_formatter_command -archive $output_file -outfmt 5 -out $output_file_xml";
  185. print "\nExecuting $format_cmd\n\n";
  186. system($format_cmd);
  187. if (!file_exists($output_file_xml)) {
  188. tripal_report_error(
  189. 'blast_ui',
  190. TRIPAL_ERROR,
  191. "Unable to convert BLAST ASN.1 archive to XML (%archive => %file).",
  192. array('%archive' => $output_file, '%file' => $output_file_xml),
  193. array('print' => TRUE)
  194. );
  195. }
  196. print "\tTab-delimited\n";
  197. system("$blast_formatter_command -archive $output_file -outfmt 7 -out $output_file_tsv");
  198. if (!file_exists($output_file_tsv)) {
  199. tripal_report_error(
  200. 'blast_ui',
  201. TRIPAL_WARNING,
  202. "Unable to convert BLAST ASN.1 archive to Tabular Output (%archive => %file).",
  203. array('%archive' => $output_file, '%file' => $output_file_tsv),
  204. array('print' => TRUE)
  205. );
  206. }
  207. print "\tHTML (includes alignments)\n";
  208. system("$blast_formatter_command -archive $output_file -outfmt 0 -out $output_file_html -html");
  209. if (!file_exists($output_file_tsv)) {
  210. tripal_report_error(
  211. 'blast_ui',
  212. TRIPAL_WARNING,
  213. "Unable to convert BLAST ASN.1 archive to HTML Output (%archive => %file).",
  214. array('%archive' => $output_file, '%file' => $output_file_html),
  215. array('print' => TRUE)
  216. );
  217. }
  218. print "\nDone!\n";
  219. }
  220. /**
  221. * FASTA validating parser
  222. *
  223. * A sequence in FASTA format begins with a single-line description, followed
  224. * by lines of sequence data.The description line is distinguished from the
  225. * sequence data by a greater-than (">") symbol in the first column. The word
  226. * following the ">" symbol is the identifier of the sequence, and the rest of
  227. * the line is the description (both are optional). There should be no space
  228. * between the ">" and the first letter of the identifier. The sequence ends
  229. * if another line starting with a ">" appears which indicates the start of
  230. * another sequence.
  231. *
  232. * @param $type
  233. * The type of sequence to be validated (ie: either nucleotide or protein).
  234. * @param $sequence
  235. * A string of characters to be validated.
  236. *
  237. * @return
  238. * Return a boolean. 1 if the sequence does not pass the format valifation stage and 0 otherwise.
  239. *
  240. */
  241. function validate_fasta_sequence($type, $sequence) {
  242. if ($type == 'nucleotide') {
  243. $fastaIdRegEx = '/^>.*(\\n|\\r)/';
  244. $fastaSeqRegEx = '/[^acgntuACGNTU\n\r]/';
  245. if ( preg_match($fastaSeqRegEx,$sequence) && !(preg_match($fastaIdRegEx,$sequence)) ) {
  246. return TRUE;
  247. } else {
  248. return FALSE;
  249. }
  250. } elseif ($type == 'protein') {
  251. $fastaIdRegEx = '/^>.*(\\n|\\r)/';
  252. $fastaSeqRegEx = '/[^acgturykmswbdhvnxACGTURYKMSWBDHVNX\*\-\n\r]/';
  253. if ( preg_match($fastaSeqRegEx,$sequence) && !(preg_match($fastaIdRegEx,$sequence)) ) {
  254. return TRUE;
  255. } else {
  256. return FALSE;
  257. }
  258. }
  259. return FALSE;
  260. }
  261. /**
  262. * Retrieve the regex to capture the Link-out Accession from the Hit Def.
  263. *
  264. * @param $nid
  265. * The node ID of the BLAST database the hit is from.
  266. * @param $options
  267. * An array of options that can be passed to this function. Supported
  268. * options include:
  269. * -
  270. *
  271. * @return
  272. * A PHP regex for use with preg_match to cature the Link-out Accession.
  273. */
  274. function get_blastdb_linkout_regex($node, $options = array()) {
  275. if (empty($node->linkout->regex)) {
  276. switch ($node->linkout->regex_type) {
  277. case 'default':
  278. $regex = '/^(\S+).*/';
  279. break;
  280. case 'genbank':
  281. $regex = '/^gb\|([^\|])*\|.*/';
  282. break;
  283. case 'embl':
  284. $regex = '/^embl\|([^\|])*\|.*/';
  285. break;
  286. case 'swissprot':
  287. $regex = '/^sp\|([^\|])*\|.*/';
  288. break;
  289. }
  290. }
  291. else {
  292. $regex = $node->linkout->regex;
  293. }
  294. return $regex;
  295. }
  296. /**
  297. * Return a list of recent blast jobs to be displayed to the user.
  298. *
  299. * @param $programs
  300. * An array of blast programs you want jobs to be displayed for (ie: blastn, blastx, tblastn, blastp)
  301. *
  302. * @return
  303. * An array of recent jobs.
  304. */
  305. function get_recent_blast_jobs($programs = array()) {
  306. $filter_jobs = !empty($programs);
  307. // Retrieve any recent jobs from the session variable.
  308. if (isset($_SESSION['blast_jobs'])) {
  309. $jobs = array();
  310. foreach ($_SESSION['blast_jobs'] as $job_secret) {
  311. $add = TRUE;
  312. $job_id = blast_ui_reveal_secret($job_secret);
  313. $job = get_BLAST_job($job_id);
  314. // @TODO: Check that the results are still available.
  315. // This is meant to replace the arbitrary only show jobs executed less than 48 hrs ago.
  316. // Remove jobs from the list that are not of the correct program.
  317. if ($filter_jobs AND !in_array($job->program, $programs)) {
  318. $add = FALSE;
  319. }
  320. if ($add) {
  321. $job->query_summary = format_query_headers($job->files->query);
  322. $jobs[] = $job;
  323. }
  324. }
  325. return $jobs;
  326. }
  327. else {
  328. return array();
  329. }
  330. }
  331. /**
  332. * Retrieve the number of recent jobs.
  333. */
  334. function get_number_of_recent_jobs() {
  335. if (isset($_SESSION['blast_jobs'])) {
  336. return sizeof($_SESSION['blast_jobs']);
  337. }
  338. return 0;
  339. }
  340. /**
  341. * Summarize a fasta file based on it's headers.
  342. *
  343. * @param $file
  344. * The full path to the FASTA file.
  345. *
  346. * @return
  347. * A string describing the number of sequences and often including the first query header.
  348. */
  349. function format_query_headers($file) {
  350. $headers = array();
  351. exec('grep ">" '.$file, $headers);
  352. // Easiest case: if there is only one query header then show it.
  353. if (sizeof($headers) == 1 AND isset($headers[0])) {
  354. return ltrim($headers[0], '>');
  355. }
  356. // If we have at least one header then show that along with the count of queries.
  357. elseif (isset($headers[0])) {
  358. return sizeof($headers) . ' queries including "' . ltrim($headers[0], '>') . '"';
  359. }
  360. // If they provided a sequence with no header.
  361. elseif (empty($headers)) {
  362. return 'Unnamed Query';
  363. }
  364. // At the very least show the count of queries.
  365. else {
  366. return sizeof($headers) . ' queries';
  367. }
  368. }
  369. /**
  370. * Sort recent blast jobs by the date they were submitted.
  371. * Ascending is in order of submission.
  372. *
  373. * THIS FUNCTION SHOULD BY USED BY USORT().
  374. */
  375. function sort_blast_jobs_by_date_submitted_asc($a, $b) {
  376. return ($a->date_submitted - $b->date_submitted);
  377. }
  378. /**
  379. * Sort recent blast jobs by the date they were submitted.
  380. * Descending is most recent first.
  381. *
  382. * THIS FUNCTION SHOULD BY USED BY USORT().
  383. */
  384. function sort_blast_jobs_by_date_submitted_desc($a, $b) {
  385. return ($b->date_submitted - $a->date_submitted);
  386. }
  387. /**
  388. * Generate an image of HSPs for a given hit.
  389. *
  390. * history:
  391. * 09/23/10 Carson created
  392. * 04/16/12 eksc adapted into POPcorn code
  393. * 03/12/15 deepak Adapted code into Tripal BLAST
  394. * 10/23/15 lacey Fixed deepak's code to be suitable for Drupal.
  395. *
  396. * @param $acc
  397. * target name
  398. * @param $name
  399. * query name, false if none
  400. * @param $tsize
  401. * target size
  402. * @param $qsize
  403. * query size
  404. * @param $hits
  405. * each hit represented in URL as: targetstart_targetend_hspstart_hspend;
  406. * @param $score
  407. * score for each hit
  408. *
  409. * @returm
  410. * A base64 encoded image representing the hit information.
  411. */
  412. function generate_blast_hit_image($acc = '', $scores, $hits, $tsize, $qsize, $name, $hit_name) {
  413. $tok = strtok($hits, ";");
  414. $b_hits = Array();
  415. while ($tok !== false) {
  416. $b_hits[] = $tok;
  417. $tok = strtok(";");
  418. }
  419. // extract score information from score param
  420. $tokscr = strtok($scores, ";");
  421. $b_scores = Array();
  422. while ($tokscr !== false) {
  423. $b_scores[] = $tokscr;
  424. $tokscr = strtok(";");
  425. }
  426. // image measurements
  427. $height = 200 + (count($b_hits) * 16);
  428. $width = 520;
  429. $img = imagecreatetruecolor($width, $height);
  430. $white = imagecolorallocate($img, 255, 255, 255);
  431. $black = imagecolorallocate($img, 0, 0, 0);
  432. $darkgray = imagecolorallocate($img, 100, 100, 100);
  433. $strong = imagecolorallocatealpha($img, 202, 0, 0, 15);
  434. $moderate = imagecolorallocatealpha($img, 204, 102, 0, 20);
  435. $present = imagecolorallocatealpha($img, 204, 204, 0, 35);
  436. $weak = imagecolorallocatealpha($img, 102, 204, 0, 50);
  437. $gray = imagecolorallocate($img, 190, 190, 190);
  438. $lightgray = $white; //imagecolorallocate($img, 230, 230, 230);
  439. imagefill($img, 0, 0, $lightgray);
  440. // Target coordinates
  441. $maxlength = 300;
  442. $t_length = ($tsize > $qsize)
  443. ? $maxlength : $maxlength - 50;
  444. $q_length = ($qsize > $tsize)
  445. ? $maxlength : $maxlength - 50;
  446. $tnormal = $t_length / $tsize;
  447. $qnormal = $q_length / $qsize;
  448. $t_ystart = 30;
  449. $t_yend = $t_ystart + 20;
  450. $t_xstart = 50;
  451. $t_xend = $t_xstart + $t_length;
  452. $t_center = $t_xstart + ($t_length / 2);
  453. // Target labels
  454. $warn = '"'. $hit_name . '"';
  455. imagestring($img, 5, $t_xstart, $t_ystart-20, $acc.$warn, $black);
  456. imagestring($img, 3, 5, $t_ystart+2, "Target", $black);
  457. // Draw bar representing target
  458. imagefilledrectangle($img, $t_xstart, $t_ystart, $t_xend, $t_yend, $gray);
  459. imagerectangle($img, $t_xstart, $t_ystart, $t_xend, $t_yend, $darkgray);
  460. // query coordinates
  461. $q_maxheight = 250;
  462. $q_ystart = $t_yend + 100;
  463. $q_yend = $q_ystart + 20;
  464. $q_xstart = $t_center - $q_length / 2;
  465. $q_xend = $q_xstart + $q_length;
  466. $q_center = ($q_xend + $q_xstart) / 2;
  467. $q_xwidth = $q_xend - $q_xstart;
  468. // Query labels
  469. imagestring($img, 5, $q_xstart, $q_yend+2, $name, $black);
  470. imagestring($img, 3, $q_xstart, $q_ystart+2, 'Query', $black);
  471. // Draw bar representing query
  472. imagefilledrectangle($img, $q_xstart, $q_ystart, $q_xend, $q_yend, $gray);
  473. imagerectangle($img ,$q_xstart, $q_ystart, $q_xend, $q_yend, $darkgray);
  474. // HSP bars will start here
  475. $hsp_bary = $q_yend + 20;
  476. // Draw solids for HSP alignments
  477. for ($ii=count($b_hits)-1; $ii>=0; $ii--) {
  478. // alignment
  479. $cur_hit = $b_hits[$ii];
  480. $cur_score = intval($b_scores[$ii]);
  481. // set color according to score
  482. $cur_color = $darkgray;
  483. if ($cur_score > 200) {
  484. $cur_color = $strong;
  485. }
  486. else if ($cur_score > 80 && $cur_score <= 200) {
  487. $cur_color = $moderate;
  488. }
  489. else if ($cur_score > 50 && $cur_score <= 80) {
  490. $cur_color = $present;
  491. }
  492. else if ($cur_score > 40 && $cur_score <= 50) {
  493. $cur_color = $weak;
  494. }
  495. $t_start = $tnormal * intval(strtok($cur_hit, "_")) + $t_xstart;
  496. $t_end = $tnormal * intval(strtok("_")) + $t_xstart;
  497. $q_start = $qnormal * intval(strtok("_")) + $q_xstart;
  498. $q_end = $qnormal * intval(strtok("_")) + $q_xstart;
  499. $hit1_array = array($t_start, $t_yend, $t_end, $t_yend, $q_end,
  500. $q_ystart, $q_start, $q_ystart);
  501. // HSP coords
  502. imagefilledpolygon($img, $hit1_array, 4, $cur_color);
  503. }//each hit
  504. // Draw lines over fills for HSP alignments
  505. for ($ii=0; $ii<count($b_hits); $ii++) {
  506. // alignment
  507. $cur_hit = $b_hits[$ii];
  508. $t_start = $tnormal * intval(strtok($cur_hit, "_")) + $t_xstart;
  509. $t_end = $tnormal * intval(strtok("_")) + $t_xstart;
  510. $q_start = $qnormal * intval(strtok("_")) + $q_xstart;
  511. $q_end = $qnormal * intval(strtok("_")) + $q_xstart;
  512. $hit1_array = array($t_start, $t_yend, $t_end, $t_yend, $q_end, $q_ystart,
  513. $q_start, $q_ystart,);
  514. imagerectangle($img, $t_start, $t_ystart, $t_end, $t_yend, $black);
  515. imagerectangle($img, $q_start, $q_ystart, $q_end, $q_yend, $black);
  516. imagepolygon ($img, $hit1_array, 4, $black);
  517. // show HSP
  518. imagestring($img, 3, 2, $hsp_bary, ($acc ."HSP" . ($ii + 1)), $black);
  519. $cur_score = intval($b_scores[$ii]);
  520. // set color according to score
  521. $cur_color = $darkgray;
  522. if ($cur_score > 200) {
  523. $cur_color = $strong;
  524. }
  525. else if ($cur_score > 80 && $cur_score <= 200) {
  526. $cur_color = $moderate;
  527. }
  528. else if ($cur_score > 50 && $cur_score <= 80) {
  529. $cur_color = $present;
  530. }
  531. else if ($cur_score > 40 && $cur_score <= 50) {
  532. $cur_color = $weak;
  533. }
  534. imagefilledrectangle($img, $q_start, $hsp_bary, $q_end, $hsp_bary+10, $cur_color);
  535. $hsp_bary += 15;
  536. }//each hit
  537. // Draw the key
  538. $xchart = 390;
  539. $ychart = 10;
  540. $fontsize = 4;
  541. $yinc = 20;
  542. $ywidth = 7;
  543. $xinc = 10;
  544. imagestring($img, 5, $xchart, $ychart - 5, "Bit Scores", $black);
  545. imagestring($img, $fontsize, $xchart + $yinc + $xinc,$ychart + ($yinc * 1) + $ywidth, ">= 200" , $black);
  546. imagestring($img, $fontsize, $xchart + $yinc + $xinc,$ychart + ($yinc * 2) + $ywidth, "80 - 200" , $black);
  547. imagestring($img, $fontsize, $xchart + $yinc + $xinc,$ychart + ($yinc * 3) + $ywidth, "50 - 80" , $black);
  548. imagestring($img, $fontsize, $xchart + $yinc + $xinc,$ychart + ($yinc * 4) + $ywidth, "40 - 50" , $black);
  549. imagestring($img, $fontsize, $xchart + $yinc + $xinc,$ychart + ($yinc * 5) + $ywidth, "< 40" , $black);
  550. imagefilledRectangle($img, $xchart, $ychart + ($yinc * 1) + $xinc, $xchart + $yinc, $ychart + ($yinc * 2), $strong);
  551. imagefilledRectangle($img, $xchart, $ychart + ($yinc * 2) + $xinc, $xchart + $yinc, $ychart + ($yinc * 3), $moderate);
  552. imagefilledRectangle($img, $xchart, $ychart + ($yinc * 3) + $xinc, $xchart + $yinc, $ychart + ($yinc * 4), $present);
  553. imagefilledRectangle($img, $xchart, $ychart + ($yinc * 4) + $xinc, $xchart + $yinc, $ychart + ($yinc * 5), $weak);
  554. imagefilledRectangle($img, $xchart, $ychart + ($yinc * 5) + $xinc, $xchart + $yinc, $ychart + ($yinc * 6), $darkgray);
  555. // Now, we have a completed image resource and need to change it to an actual image
  556. // that can be displayed. This is done using imagepng() but unfortuatly that function
  557. // either saves the image to a file or outputs it directly to the screen. Thus, we use
  558. // the following code to capture it and base64 encode it.
  559. ob_start(); // Start buffering the output
  560. imagepng($img, null, 0, PNG_NO_FILTER);
  561. $b64_img = base64_encode(ob_get_contents()); // Get what we've just outputted and base64 it
  562. imagedestroy($img);
  563. ob_end_clean();
  564. return $b64_img;
  565. }