blast_ui.form_per_program.inc 20 KB

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