blast_ui.form_per_program.inc 17 KB

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