blast_ui.form_per_program.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. /**
  3. * @file
  4. * Forms in the file provide a per BLAST program interface to submitting BLASTs.
  5. *
  6. * In other words, it provides a form for blastn, one for blastp, etc. It does
  7. * this using a single form function for code reusability and depending upon
  8. * the $type passed in, it will execute additional hooks allowing for program
  9. * specific modifications & advanced options.
  10. */
  11. /**
  12. * This single form definition provides 4 different program-specific forms.
  13. */
  14. function blast_ui_per_blast_program_form($form, $form_state) {
  15. // CSS support to the form
  16. $form['#attached']['css'] = array(
  17. drupal_get_path('module', 'blast_ui') . '/theme/css/form.css',
  18. );
  19. // Determine the BLAST program.
  20. $query_type = $form_state['build_info']['args'][0];
  21. $db_type = $form_state['build_info']['args'][1];
  22. if ($query_type == 'nucleotide') {
  23. if ($db_type == 'nucleotide') {
  24. $blast_program = 'blastn';
  25. }
  26. elseif ($db_type == 'protein') {
  27. $blast_program = 'blastx';
  28. }
  29. }
  30. elseif ($query_type == 'protein') {
  31. if ($db_type == 'nucleotide') {
  32. $blast_program = 'tblastn';
  33. }
  34. elseif ($db_type == 'protein') {
  35. $blast_program = 'blastp';
  36. }
  37. }
  38. // Set the title to be more Researcher friendly.
  39. drupal_set_title(t(
  40. '@query to @db BLAST (@program)',
  41. array(
  42. '@query' => ucfirst($query_type),
  43. '@db' => ucfirst($db_type),
  44. '@program' => $blast_program
  45. )
  46. ));
  47. // Add the details about the specific BLAST choosen.
  48. $form['query_type'] = array(
  49. '#type' => 'hidden',
  50. '#value' => $query_type
  51. );
  52. $form['db_type'] = array(
  53. '#type' => 'hidden',
  54. '#value' => $db_type
  55. );
  56. $form['blast_program'] = array(
  57. '#type' => 'hidden',
  58. '#value' => $blast_program
  59. );
  60. // NUCLEOTIDE QUERY
  61. //.........................
  62. $form['query'] = array(
  63. '#type' => 'fieldset',
  64. '#title' => t('Enter %type Query Sequence',
  65. array('%type' => ucfirst($query_type))),
  66. '#description' => t('Enter one or more queries in the top text box or use '
  67. . 'the browse button to upload a file from your local disk. The file may '
  68. . 'contain a single sequence or a list of sequences. In both cases, the '
  69. . 'data must be in <a href="@formaturl" target="_blank">FASTA format</a>.',
  70. array(
  71. '@formaturl' => 'http://www.ncbi.nlm.nih.gov/BLAST/blastcgihelp.shtml'
  72. )
  73. ),
  74. '#collapsible' => TRUE,
  75. '#collapsed' => FALSE,
  76. );
  77. // Checkbox to show an example.
  78. $form['query']['example_sequence'] = array(
  79. '#type' => 'checkbox',
  80. '#title' => t('Show an Example Sequence'),
  81. '#prefix' => '<span style="float: right;">',
  82. '#suffix' => '</span>',
  83. '#ajax' => array(
  84. 'callback' => 'ajax_blast_ui_example_sequence_callback',
  85. 'wrapper' => 'fasta-textarea',
  86. 'method' => 'replace',
  87. 'effect' => 'fade',
  88. ),
  89. );
  90. // Textfield for submitting a mult-FASTA query
  91. $form['query']['FASTA'] = array(
  92. '#type' => 'textarea',
  93. '#title' => t('Enter FASTA sequence(s)'),
  94. '#description'=>t('Enter query sequence(s) in the text area.'),
  95. '#prefix' => '<div id="fasta-textarea">',
  96. '#suffix' => '</div>',
  97. );
  98. // Upload a file as an alternative to enter a query sequence
  99. $form['#attributes']['enctype'] = 'multipart/form-data';
  100. $form['query']['UPLOAD'] = array(
  101. '#title' => 'Or upload your own query FASTA: ',
  102. '#type' => 'managed_file',
  103. '#description' => t('The file should be a plain-text FASTA
  104. (.fasta, .fna, .fa) file. In other words, it cannot have formatting as is the
  105. case with MS Word (.doc, .docx) or Rich Text Format (.rtf). It cannot be greater
  106. than %max_size in size. <strong>Don\'t forget to press the Upload button before
  107. attempting to submit your BLAST.</strong>',
  108. array(
  109. '%max_size' => round(file_upload_max_size() / 1024 / 1024,1) . 'MB'
  110. )
  111. ),
  112. '#upload_validators' => array(
  113. 'file_validate_extensions' => array('fasta fna fa'),
  114. 'file_validate_size' => array(file_upload_max_size()),
  115. ),
  116. );
  117. // BLAST DATABASE
  118. //.........................
  119. $form['DB'] = array(
  120. '#type' => 'fieldset',
  121. '#title' => t('Choose Search Set'),
  122. '#description' => t('Choose from one of the %type BLAST databases listed '
  123. . 'below. You can also use the browse button to upload a file from your '
  124. . 'local disk. The file may contain a single sequence or a list of '
  125. . 'sequences. ',
  126. array('%type' => $db_type)),
  127. '#collapsible' => TRUE,
  128. '#collapsed' => FALSE,
  129. );
  130. $options = get_blast_database_options($db_type);
  131. $form['DB']['SELECT_DB'] = array(
  132. '#type' => 'select',
  133. '#title' => t('%type BLAST Databases:', array('%type' => ucfirst($db_type))),
  134. '#options' => $options,
  135. '#default_value' => 0,
  136. );
  137. // Upload a file as an alternative to selecting an existing BLAST database
  138. $form['#attributes']['enctype'] = 'multipart/form-data';
  139. $form['DB']['DBUPLOAD'] = array(
  140. '#title' => 'Or upload your own dataset: ',
  141. '#type' => 'managed_file',
  142. '#description' => t('The file should be a plain-text FASTA
  143. (.fasta, .fna, .fa) file. In other words, it cannot have formatting as is the
  144. case with MS Word (.doc, .docx) or Rich Text Format (.rtf). It cannot be greater
  145. than %max_size in size. <strong>Don\'t forget to press the Upload button before
  146. attempting to submit your BLAST.</strong>',
  147. array(
  148. '%max_size' => round(file_upload_max_size() / 1024 / 1024,1) . 'MB'
  149. )
  150. ),
  151. '#upload_validators' => array(
  152. 'file_validate_extensions' => array('fasta fna fa'),
  153. 'file_validate_size' => array(file_upload_max_size()),
  154. ),
  155. );
  156. // Advanced Options
  157. //.........................
  158. // These options will be different depending upon the program selected.
  159. // Therefore, allow for program-specific callbacks to populate these options.
  160. $form['ALG'] = array(
  161. '#type' => 'fieldset',
  162. '#title' => t('Advanced Options'),
  163. '#collapsible' => TRUE,
  164. '#collapsed' => TRUE,
  165. );
  166. $advanced_options_form = 'blast_ui_' . $blast_program . '_advanced_options_form';
  167. if (function_exists($advanced_options_form)) {
  168. call_user_func_array($advanced_options_form, array(&$form, $form_state));
  169. }
  170. // Submit
  171. //.........................
  172. $form['submit'] = array(
  173. '#type' => 'submit',
  174. '#default_value' => ' BLAST ',
  175. );
  176. return $form;
  177. }
  178. /**
  179. * Validate the user options submitted via the above form.
  180. *
  181. * @see blast_ui_per_blast_program_form().
  182. */
  183. function blast_ui_per_blast_program_form_validate($form, &$form_state) {
  184. $blast_program = $form_state['values']['blast_program'];
  185. $type = $form_state['values']['query_type'];
  186. if ($type == 'nucleotide') {
  187. $molecule_type = 'nucleotides';
  188. }
  189. else {
  190. $molecule_type = 'amino acid residues';
  191. }
  192. // Validate Query
  193. //----------------
  194. // @todo: We are currently not validating uploaded files are valid FASTA.
  195. // First check to see if we have an upload & if so then validate it.
  196. $file = file_load($form_state['values']['UPLOAD']);
  197. // If the $file is populated then this is a newly uploaded, temporary file.
  198. if (is_object($file)) {
  199. $form_state['qFlag'] = 'upQuery';
  200. $form_state['upQuery_path'] = drupal_realpath($file->uri);
  201. }
  202. // Otherwise there was no file uploaded.
  203. // Check if there was a query sequence entered in the texfield.
  204. elseif (!empty($form_state['input']['FASTA'])) {
  205. // Check to ensure that the query sequence entered is valid FASTA.
  206. if (validate_fasta_sequence($type, $form_state['input']['FASTA'])){
  207. form_set_error('query', t('You need to provide a valid FASTA sequence '
  208. . 'for the query. It should contain a FASTA header/definition line '
  209. . 'followed by %molecule-type. For more information see the '
  210. . '<a href="@url" target="_blank">NCBI FASTA Specification</a>.',
  211. array(
  212. '%molecule-type' => $molecule_type,
  213. '@url' => 'http://www.ncbi.nlm.nih.gov/BLAST/blastcgihelp.shtml'
  214. )));
  215. }
  216. else {
  217. $form_state['qFlag'] = 'seqQuery';
  218. }
  219. }
  220. // Otherwise they didn't enter a query!!
  221. else {
  222. form_set_error('query', t('No query sequence given. Only raw sequence or '
  223. . 'sequence of type FASTA can be read. Enter sequence in the box provided '
  224. . 'or upload a plain text file.'));
  225. }
  226. // Validate Database
  227. //-------------------
  228. // @todo: We are currently not validating uploaded files are valid FASTA.
  229. // First check to see if we have an upload & if so then validate it.
  230. $file = file_load($form_state['values']['DBUPLOAD']);
  231. // If the $file is populated then this is a newly uploaded, temporary file.
  232. if (is_object($file)) {
  233. $form_state['dbFlag'] = 'upDB';
  234. $form_state['upDB_path'] = drupal_realpath($file->uri);
  235. }
  236. // Otherwise there was no file uploaded
  237. // Check if there was a database choosen from the list instead
  238. elseif (!empty($form_state['values']['SELECT_DB'])) {
  239. $form_state['dbFlag'] = 'blastdb';
  240. }
  241. // Otherwise they didn't select a database!!
  242. else {
  243. form_set_error('DB', t('No database selected. Either choose a database '
  244. . 'from the list or upload one of your own.'));
  245. }
  246. // Validate Advanced Options
  247. //---------------------------
  248. $advanced_options_form_validate = 'blast_ui_' . $blast_program . '_advanced_options_form_validate';
  249. if (function_exists($advanced_options_form_validate)) {
  250. call_user_func_array(
  251. $advanced_options_form_validate,
  252. array(&$form, $form_state)
  253. );
  254. }
  255. }
  256. /**
  257. * Process the user options submitted via the blast program form.
  258. *
  259. * @see blast_ui_per_blast_program_form().
  260. */
  261. function blast_ui_per_blast_program_form_submit($form, &$form_state) {
  262. $error = FALSE;
  263. $blast_program = $form_state['values']['blast_program'];
  264. if ($form_state['values']['db_type'] == 'nucleotide') {
  265. $mdb_type = 'nucl';
  266. $file_suffix = '.nsq';
  267. }
  268. else {
  269. $mdb_type = 'prot';
  270. $file_suffix = '.psq';
  271. }
  272. // If the query was submitted via the texrfield then create a file containing it
  273. if ( isset($form_state['qFlag']) ) {
  274. if ( $form_state['qFlag'] == 'seqQuery' ) {
  275. $seq_content = $form_state['values']['FASTA'];
  276. $query = '/tmp/' . date('YMd_His') . '_query.fasta';
  277. file_put_contents ( $query , $seq_content);
  278. }
  279. elseif ( $form_state['qFlag'] == 'upQuery' ) {
  280. $query = $form_state['upQuery_path'];
  281. }
  282. }
  283. // If the BLAST database was uploaded then use it to run the BLAST
  284. if ( $form_state['dbFlag'] == 'upDB') {
  285. // Since we only support using the -db flag (not -subject) we need to create a
  286. // blast database for the FASTA uploaded.
  287. // NOTE: We can't support subject because we need to generate the ASN.1+ format
  288. // to provide multiple download type options from the same BLAST
  289. $blastdb_with_path = $form_state['upDB_path'];
  290. $result = NULL;
  291. exec("makeblastdb -in $blastdb_with_path -dbtype $mdb_type -parse_seqids 2>&1", $result);
  292. // Check that the BLAST database was made correctly.
  293. $result = implode('<br />', $result);
  294. if (preg_match('/Error/', $result)) {
  295. drupal_set_message('Unable to generate a BLAST database from your uploaded
  296. FASTA sequence. Please check that your file is a valid FASTA file and that if
  297. your sequence headers include pipes (i.e.: | ) they adhere to '
  298. . l('NCBI standards.', 'http://www.ncbi.nlm.nih.gov/books/NBK21097/table/A632/?report=objectonly', array('attributes' => array('target' => '_blank'))),
  299. 'error'
  300. );
  301. $error = TRUE;
  302. }
  303. }
  304. // Otherwise, we are using one of the website provided BLAST databases so form the
  305. // BLAST command accordingly
  306. elseif ($form_state['dbFlag'] == 'blastdb') {
  307. $selected_db = $form_state['values']['SELECT_DB'];
  308. $blastdb_node = node_load($selected_db);
  309. $blastdb_with_path = $blastdb_node->db_path;
  310. }
  311. // Now let each program process it's own advanced options.
  312. $advanced_options = array();
  313. $advanced_options_form_submit = 'blast_ui_' . $blast_program . '_advanced_options_form_submit';
  314. if (function_exists($advanced_options_form_submit)) {
  315. $advanced_options = call_user_func_array(
  316. $advanced_options_form_submit,
  317. array($form, &$form_state)
  318. );
  319. }
  320. // Actually submit the BLAST Tripal Job
  321. // NOTE: Tripal jobs needs to be executed from the command-line before it will be run!!
  322. $blastdb_with_suffix = $blastdb_with_path . $file_suffix;
  323. if (is_readable($blastdb_with_suffix)) {
  324. global $user;
  325. $output_filestub = date('YMd_His');
  326. $job_args = array(
  327. 'program' => $blast_program,
  328. 'query' => $query,
  329. 'database' => $blastdb_with_path,
  330. 'output_filename' => $output_filestub,
  331. 'options' => $advanced_options
  332. );
  333. $job_id = tripal_add_job(
  334. t('BLAST (@program): @query', array('@program' => $blast_program, '@query' => $query)),
  335. 'blast_job',
  336. 'run_BLAST_tripal_job',
  337. $job_args,
  338. $user->uid
  339. );
  340. // Redirect to the BLAST results page
  341. drupal_goto("blast/report/$job_id");
  342. }
  343. // We check if $error is set to TRUE because if so then the error has already
  344. // been reported.
  345. elseif (!$error) {
  346. $dbfile_uploaded_msg = ($form_state['dbFlag'] == 'upDB') ? 'The BLAST database was submitted via user upload.' : 'Existing BLAST Database was chosen';
  347. tripal_report_error(
  348. 'blast_ui',
  349. TRIPAL_ERROR,
  350. "BLAST database %db unaccessible. $dbfile_uploaded_msg",
  351. array('%db' => $blastdb_with_path)
  352. );
  353. drupal_set_message('BLAST database unaccessible. Please contact the site administrator.','error');
  354. }
  355. }