blast_ui.form_per_program.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. if (variable_get('blast_ui_allow_target_upload', FALSE)) {
  138. // Upload a file as an alternative to selecting an existing BLAST database
  139. $form['#attributes']['enctype'] = 'multipart/form-data';
  140. $form['DB']['DBUPLOAD'] = array(
  141. '#title' => 'Or upload your own dataset: ',
  142. '#type' => 'managed_file',
  143. '#description' => t('The file should be a plain-text FASTA
  144. (.fasta, .fna, .fa) file. In other words, it cannot have formatting as is the
  145. case with MS Word (.doc, .docx) or Rich Text Format (.rtf). It cannot be greater
  146. than %max_size in size. <strong>Don\'t forget to press the Upload button before
  147. attempting to submit your BLAST.</strong>',
  148. array(
  149. '%max_size' => round(file_upload_max_size() / 1024 / 1024,1) . 'MB'
  150. )
  151. ),
  152. '#upload_validators' => array(
  153. 'file_validate_extensions' => array('fasta fna fa'),
  154. 'file_validate_size' => array(file_upload_max_size()),
  155. ),
  156. );
  157. }
  158. // Advanced Options
  159. //.........................
  160. // These options will be different depending upon the program selected.
  161. // Therefore, allow for program-specific callbacks to populate these options.
  162. $form['ALG'] = array(
  163. '#type' => 'fieldset',
  164. '#title' => t('Advanced Options'),
  165. '#collapsible' => TRUE,
  166. '#collapsed' => TRUE,
  167. );
  168. $advanced_options_form = 'blast_ui_' . $blast_program . '_advanced_options_form';
  169. if (function_exists($advanced_options_form)) {
  170. call_user_func_array($advanced_options_form, array(&$form, $form_state));
  171. }
  172. // Submit
  173. //.........................
  174. $form['submit'] = array(
  175. '#type' => 'submit',
  176. '#default_value' => ' BLAST ',
  177. );
  178. return $form;
  179. }
  180. /**
  181. * Validate the user options submitted via the above form.
  182. *
  183. * @see blast_ui_per_blast_program_form().
  184. */
  185. function blast_ui_per_blast_program_form_validate($form, &$form_state) {
  186. $blast_program = $form_state['values']['blast_program'];
  187. $type = $form_state['values']['query_type'];
  188. if ($type == 'nucleotide') {
  189. $molecule_type = 'nucleotides';
  190. }
  191. else {
  192. $molecule_type = 'amino acid residues';
  193. }
  194. // Validate Query
  195. //----------------
  196. // @todo: We are currently not validating uploaded files are valid FASTA.
  197. // First check to see if we have an upload & if so then validate it.
  198. $file = file_load($form_state['values']['UPLOAD']);
  199. // If the $file is populated then this is a newly uploaded, temporary file.
  200. if (is_object($file)) {
  201. $form_state['qFlag'] = 'upQuery';
  202. $form_state['upQuery_path'] = drupal_realpath($file->uri);
  203. }
  204. // Otherwise there was no file uploaded.
  205. // Check if there was a query sequence entered in the texfield.
  206. elseif (!empty($form_state['input']['FASTA'])) {
  207. // Check to ensure that the query sequence entered is valid FASTA.
  208. if (validate_fasta_sequence($type, $form_state['input']['FASTA'])){
  209. form_set_error('query', t('You need to provide a valid FASTA sequence '
  210. . 'for the query. It should contain a FASTA header/definition line '
  211. . 'followed by %molecule-type. For more information see the '
  212. . '<a href="@url" target="_blank">NCBI FASTA Specification</a>.',
  213. array(
  214. '%molecule-type' => $molecule_type,
  215. '@url' => 'http://www.ncbi.nlm.nih.gov/BLAST/blastcgihelp.shtml'
  216. )));
  217. }
  218. else {
  219. $form_state['qFlag'] = 'seqQuery';
  220. }
  221. }
  222. // Otherwise they didn't enter a query!!
  223. else {
  224. form_set_error('query', t('No query sequence given. Only raw sequence or '
  225. . 'sequence of type FASTA can be read. Enter sequence in the box provided '
  226. . 'or upload a plain text file.'));
  227. }
  228. // Validate Database
  229. //-------------------
  230. // @todo: We are currently not validating uploaded files are valid FASTA.
  231. // First check to see if we have an upload & if so then validate it.
  232. if (isset($form_state['values']['DBUPLOAD'])) {
  233. $file = file_load($form_state['values']['DBUPLOAD']);
  234. // If the $file is populated then this is a newly uploaded, temporary file.
  235. if (is_object($file)) {
  236. $form_state['dbFlag'] = 'upDB';
  237. $form_state['upDB_path'] = drupal_realpath($file->uri);
  238. }
  239. // Otherwise there was no file uploaded
  240. // Check if there was a database choosen from the list instead
  241. elseif (!empty($form_state['values']['SELECT_DB'])) {
  242. $form_state['dbFlag'] = 'blastdb';
  243. }
  244. // Otherwise they didn't select a database!!
  245. else {
  246. form_set_error('DB', t('No database selected. Either choose a database '
  247. . 'from the list or upload one of your own.'));
  248. }
  249. }
  250. // Otherwise there was no file uploaded
  251. // Check if there was a database choosen from the list instead
  252. elseif (!empty($form_state['values']['SELECT_DB'])) {
  253. $form_state['dbFlag'] = 'blastdb';
  254. }
  255. // Otherwise they didn't select a database!!
  256. else {
  257. form_set_error('DB', t('No database selected. Either choose a database '
  258. . 'from the list or upload one of your own.'));
  259. }
  260. // Validate Advanced Options
  261. //---------------------------
  262. $advanced_options_form_validate = 'blast_ui_' . $blast_program . '_advanced_options_form_validate';
  263. if (function_exists($advanced_options_form_validate)) {
  264. call_user_func_array(
  265. $advanced_options_form_validate,
  266. array(&$form, $form_state)
  267. );
  268. }
  269. }
  270. /**
  271. * Process the user options submitted via the blast program form.
  272. *
  273. * @see blast_ui_per_blast_program_form().
  274. */
  275. function blast_ui_per_blast_program_form_submit($form, &$form_state) {
  276. $error = FALSE;
  277. $blast_program = $form_state['values']['blast_program'];
  278. if ($form_state['values']['db_type'] == 'nucleotide') {
  279. $mdb_type = 'nucl';
  280. $file_suffix = '.nsq';
  281. }
  282. else {
  283. $mdb_type = 'prot';
  284. $file_suffix = '.psq';
  285. }
  286. // If the query was submitted via the texrfield then create a file containing it
  287. if ( isset($form_state['qFlag']) ) {
  288. if ( $form_state['qFlag'] == 'seqQuery' ) {
  289. $seq_content = $form_state['values']['FASTA'];
  290. $query = '/tmp/' . date('YMd_His') . '_query.fasta';
  291. file_put_contents ( $query , $seq_content);
  292. }
  293. elseif ( $form_state['qFlag'] == 'upQuery' ) {
  294. $query = $form_state['upQuery_path'];
  295. }
  296. }
  297. // If the BLAST database was uploaded then use it to run the BLAST
  298. if ( $form_state['dbFlag'] == 'upDB') {
  299. // Since we only support using the -db flag (not -subject) we need to create a
  300. // blast database for the FASTA uploaded.
  301. // NOTE: We can't support subject because we need to generate the ASN.1+ format
  302. // to provide multiple download type options from the same BLAST
  303. $blastdb_with_path = $form_state['upDB_path'];
  304. $result = NULL;
  305. exec("makeblastdb -in $blastdb_with_path -dbtype $mdb_type -parse_seqids 2>&1", $result);
  306. // Check that the BLAST database was made correctly.
  307. $result = implode('<br />', $result);
  308. if (preg_match('/Error/', $result)) {
  309. drupal_set_message('Unable to generate a BLAST database from your uploaded
  310. FASTA sequence. Please check that your file is a valid FASTA file and that if
  311. your sequence headers include pipes (i.e.: | ) they adhere to '
  312. . l('NCBI standards.', 'http://www.ncbi.nlm.nih.gov/books/NBK21097/table/A632/?report=objectonly', array('attributes' => array('target' => '_blank'))),
  313. 'error'
  314. );
  315. $error = TRUE;
  316. }
  317. }
  318. // Otherwise, we are using one of the website provided BLAST databases so form the
  319. // BLAST command accordingly
  320. elseif ($form_state['dbFlag'] == 'blastdb') {
  321. $selected_db = $form_state['values']['SELECT_DB'];
  322. $blastdb_node = node_load($selected_db);
  323. $blastdb_with_path = $blastdb_node->db_path;
  324. }
  325. // Now let each program process it's own advanced options.
  326. $advanced_options = array();
  327. $advanced_options_form_submit = 'blast_ui_' . $blast_program . '_advanced_options_form_submit';
  328. if (function_exists($advanced_options_form_submit)) {
  329. $advanced_options = call_user_func_array(
  330. $advanced_options_form_submit,
  331. array($form, &$form_state)
  332. );
  333. }
  334. // Actually submit the BLAST Tripal Job
  335. // NOTE: Tripal jobs needs to be executed from the command-line before it will be run!!
  336. $blastdb_with_suffix = $blastdb_with_path . $file_suffix;
  337. if (is_readable($blastdb_with_suffix)) {
  338. global $user;
  339. $output_filestub = date('YMd_His');
  340. $job_args = array(
  341. 'program' => $blast_program,
  342. 'query' => $query,
  343. 'database' => $blastdb_with_path,
  344. 'output_filename' => $output_filestub,
  345. 'options' => $advanced_options
  346. );
  347. $job_id = tripal_add_job(
  348. t('BLAST (@program): @query', array('@program' => $blast_program, '@query' => $query)),
  349. 'blast_job',
  350. 'run_BLAST_tripal_job',
  351. $job_args,
  352. $user->uid
  353. );
  354. // Redirect to the BLAST results page
  355. drupal_goto("blast/report/$job_id");
  356. }
  357. // We check if $error is set to TRUE because if so then the error has already
  358. // been reported.
  359. elseif (!$error) {
  360. $dbfile_uploaded_msg = ($form_state['dbFlag'] == 'upDB') ? 'The BLAST database was submitted via user upload.' : 'Existing BLAST Database was chosen';
  361. tripal_report_error(
  362. 'blast_ui',
  363. TRIPAL_ERROR,
  364. "BLAST database %db unaccessible. $dbfile_uploaded_msg",
  365. array('%db' => $blastdb_with_path)
  366. );
  367. drupal_set_message('BLAST database unaccessible. Please contact the site administrator.','error');
  368. }
  369. }