blast_ui.api.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. * Returns a list BLAST DATABASE options
  9. *
  10. * @param $type
  11. * The type of BLAST dabases to restrict the list to (ie: n: nucleotide or p: protein)
  12. *
  13. * @return
  14. * An array where the nid is the key and the value is the human-readable name of the option
  15. */
  16. function get_blast_database_options($type) {
  17. // Get all BlastDB nodes
  18. $nodes = node_load_multiple(array(), array('type'=> 'blastdb'));
  19. $options = array();
  20. foreach ($nodes as $node) {
  21. if ( isset($node) && isset($node->db_dbtype) ) {
  22. if ( ($node->db_dbtype == $type) ) {
  23. $options[$node->nid] = $node->db_name;
  24. }
  25. }
  26. }
  27. asort($options);
  28. $options[0] = 'Select a Dataset';
  29. return $options;
  30. }
  31. /**
  32. * Run BLAST (should be called from the command-line)
  33. *
  34. * @param $program
  35. * Which BLAST program to run (ie: 'blastn', 'tblastn', tblastx', 'blastp','blastx')
  36. * @param $query
  37. * The full path and filename of the query FASTA file
  38. * @param $database
  39. * The full path and filename prefix (excluding .nhr, .nin, .nsq, etc.)
  40. * @param $output_filestub
  41. * The filename (not including path) to give the results. Should not include file type suffix
  42. * @param $options
  43. * An array of additional option where the key is the name of the option used by
  44. * BLAST (ie: 'num_alignments') and the value is relates to this particular
  45. * BLAST job (ie: 250)
  46. */
  47. function run_BLAST_tripal_job($program, $query, $database, $output_filestub, $options, $job_id = NULL) {
  48. $output_file = 'sites/default/files/' . $output_filestub . '.blast.asn';
  49. $output_file_xml = 'sites/default/files/' . $output_filestub . '.blast.xml';
  50. $output_file_tsv = 'sites/default/files/' . $output_filestub . '.blast.tsv';
  51. $output_file_html = 'sites/default/files/' . $output_filestub . '.blast.html';
  52. print "\nExecuting $program\n\n";
  53. print "Query: $query\n";
  54. print "Database: $database\n";
  55. print "Results File: $output_file\n";
  56. print "Options:\n";
  57. $blast_cmd = "$program -query $query -db $database -out $output_file -outfmt=11";
  58. if (!empty($options)) {
  59. foreach ($options as $opt => $val) {
  60. print "\t$opt: $val\n";
  61. $blast_cmd .= " -$opt $val";
  62. }
  63. }
  64. print "\nExecuting the following BLAST command:\n" . $blast_cmd . "\n";
  65. system($blast_cmd);
  66. if(!file_exists($output_file)) {
  67. tripal_report_error(
  68. 'blast_ui',
  69. TRIPAL_ERROR,
  70. "BLAST did not complete successfully as is implied by the lack of output file (%file). The command run was @command",
  71. array('%file' => $output_file, '@command' => $blast_cmd),
  72. array('print' => TRUE)
  73. );
  74. return FALSE;
  75. }
  76. print "\nGenerating additional download formats...\n";
  77. print "\tXML\n";
  78. system("blast_formatter -archive $output_file -outfmt 5 -out $output_file_xml");
  79. if(!file_exists($output_file_xml)) {
  80. tripal_report_error(
  81. 'blast_ui',
  82. TRIPAL_ERROR,
  83. "Unable to convert BLAST ASN.1 archive (%archive) to XML (%file).",
  84. array('%archive' => $output_file, '%file' => $output_file_xml),
  85. array('print' => TRUE)
  86. );
  87. }
  88. print "\tTab-delimited\n";
  89. system("blast_formatter -archive $output_file -outfmt 7 -out $output_file_tsv");
  90. if(!file_exists($output_file_tsv)) {
  91. tripal_report_error(
  92. 'blast_ui',
  93. TRIPAL_WARNING,
  94. "Unable to convert BLAST ASN.1 archive (%archive) to Tabular Output (%file).",
  95. array('%archive' => $output_file, '%file' => $output_file_tsv),
  96. array('print' => TRUE)
  97. );
  98. }
  99. print "\tHTML (includes alignments)\n";
  100. system("blast_formatter -archive $output_file -outfmt 0 -out $output_file_html -html");
  101. if(!file_exists($output_file_tsv)) {
  102. tripal_report_error(
  103. 'blast_ui',
  104. TRIPAL_WARNING,
  105. "Unable to convert BLAST ASN.1 archive (%archive) to HTML Output (%file).",
  106. array('%archive' => $output_file, '%file' => $output_file_html),
  107. array('print' => TRUE)
  108. );
  109. }
  110. print "\nDone!\n";
  111. }