blast_ui.form_per_program.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. /*TODO: FIX THIS! Shouldn't come up if not selected in configuration
  110. // Upload a file as an alternative to enter a query sequence
  111. $form['#attributes']['enctype'] = 'multipart/form-data';
  112. $form['query']['UPLOAD'] = array(
  113. '#title' => 'Or upload your own query FASTA: ',
  114. '#type' => 'managed_file',
  115. '#description' => t('The file should be a plain-text FASTA
  116. (.fasta, .fna, .fa, .fas) file. In other words, it cannot have formatting as is the
  117. case with MS Word (.doc, .docx) or Rich Text Format (.rtf). It cannot be greater
  118. than %max_size in size. <strong>Don\'t forget to press the Upload button before
  119. attempting to submit your BLAST.</strong>',
  120. array(
  121. '%max_size' => round(file_upload_max_size() / 1024 / 1024,1) . 'MB'
  122. )
  123. ),
  124. '#upload_validators' => array(
  125. 'file_validate_extensions' => array('fasta fna fa fas'),
  126. 'file_validate_size' => array(file_upload_max_size()),
  127. ),
  128. );
  129. */
  130. // BLAST DATABASE
  131. //.........................
  132. $form['DB'] = array(
  133. '#type' => 'fieldset',
  134. '#title' => t('Choose Search Target'),
  135. '#description' => t('Choose from one of the %type BLAST databases listed '
  136. . 'below. You can also use the browse button to upload a file from your '
  137. . 'local disk. The file may contain a single sequence or a list of '
  138. . 'sequences. ',
  139. array('%type' => $db_type)),
  140. '#collapsible' => TRUE,
  141. '#collapsed' => FALSE,
  142. );
  143. $options = get_blast_database_options($db_type);
  144. $form['DB']['SELECT_DB'] = array(
  145. '#type' => 'select',
  146. '#title' => t('%type BLAST Databases:', array('%type' => ucfirst($db_type))),
  147. '#options' => $options,
  148. '#default_value' => isset($job_data[$jid]['db_option']) ? $job_data[$jid]['db_option'] : 0,
  149. );
  150. if (variable_get('blast_ui_allow_target_upload', FALSE)) {
  151. // Upload a file as an alternative to selecting an existing BLAST database
  152. $form['#attributes']['enctype'] = 'multipart/form-data';
  153. $form['DB']['DBUPLOAD'] = array(
  154. '#title' => 'Or upload your own dataset: ',
  155. '#type' => 'managed_file',
  156. '#description' => t('The file should be a plain-text FASTA
  157. (.fasta, .fna, .fa) file. In other words, it cannot have formatting as is the
  158. case with MS Word (.doc, .docx) or Rich Text Format (.rtf). It cannot be greater
  159. than %max_size in size. <strong>Don\'t forget to press the Upload button before
  160. attempting to submit your BLAST.</strong>',
  161. array(
  162. '%max_size' => round(file_upload_max_size() / 1024 / 1024,1) . 'MB'
  163. )
  164. ),
  165. '#default_value' => variable_get($db_file_id, ''),
  166. '#upload_validators' => array(
  167. 'file_validate_extensions' => array('fasta fna fa'),
  168. 'file_validate_size' => array(file_upload_max_size()),
  169. ),
  170. );
  171. }
  172. // Advanced Options
  173. //.........................
  174. // These options will be different depending upon the program selected.
  175. // Therefore, allow for program-specific callbacks to populate these options.
  176. $form['ALG'] = array(
  177. '#type' => 'fieldset',
  178. '#title' => t('Advanced Options'),
  179. '#collapsible' => TRUE,
  180. '#collapsed' => TRUE,
  181. );
  182. $advanced_options_form = 'blast_ui_' . $blast_program . '_advanced_options_form';
  183. if (function_exists($advanced_options_form)) {
  184. call_user_func_array($advanced_options_form, array(&$form, $form_state));
  185. }
  186. // Submit
  187. //.........................
  188. $form['submit'] = array(
  189. '#type' => 'submit',
  190. '#default_value' => ' BLAST ',
  191. );
  192. // Recent jobs list
  193. $form['recentjobs'] = array(
  194. '#type' => 'fieldset',
  195. '#prefix' => get_recent_jobs(),
  196. );
  197. return $form;
  198. }
  199. /**
  200. * Validate the user options submitted via the above form.
  201. *
  202. * @see blast_ui_per_blast_program_form().
  203. */
  204. function blast_ui_per_blast_program_form_validate($form, &$form_state) {
  205. $blast_program = $form_state['values']['blast_program'];
  206. $type = $form_state['values']['query_type'];
  207. if ($type == 'nucleotide') {
  208. $molecule_type = 'nucleotides';
  209. }
  210. else {
  211. $molecule_type = 'amino acid residues';
  212. }
  213. // Validate Query
  214. //----------------
  215. // @todo: We are currently not validating uploaded files are valid FASTA.
  216. // First check to see if we have an upload & if so then validate it.
  217. $file = file_load($form_state['values']['UPLOAD']);
  218. // If the $file is populated then this is a newly uploaded, temporary file.
  219. if (is_object($file)) {
  220. $form_state['qFlag'] = 'upQuery';
  221. $form_state['upQuery_path'] = drupal_realpath($file->uri);
  222. }
  223. // Otherwise there was no file uploaded.
  224. // Check if there was a query sequence entered in the texfield.
  225. elseif (!empty($form_state['input']['FASTA'])) {
  226. // Check to ensure that the query sequence entered is valid FASTA.
  227. if (validate_fasta_sequence($type, $form_state['input']['FASTA'])){
  228. form_set_error('query', t('You need to provide a valid FASTA sequence '
  229. . 'for the query. It should contain a FASTA header/definition line '
  230. . 'followed by %molecule-type. For more information see the '
  231. . '<a href="@url" target="_blank">NCBI FASTA Specification</a>.',
  232. array(
  233. '%molecule-type' => $molecule_type,
  234. '@url' => 'http://www.ncbi.nlm.nih.gov/BLAST/blastcgihelp.shtml'
  235. )));
  236. }
  237. else {
  238. $form_state['qFlag'] = 'seqQuery';
  239. }
  240. }
  241. // Otherwise they didn't enter a query!!
  242. else {
  243. form_set_error('query', t('No query sequence given. Only raw sequence or '
  244. . 'sequence of type FASTA can be read. Enter sequence in the box provided '
  245. . 'or upload a plain text file.'));
  246. }
  247. // Validate Database
  248. //-------------------
  249. // @todo: We are currently not validating uploaded files are valid FASTA.
  250. // First check to see if we have an upload & if so then validate it.
  251. if (isset($form_state['values']['DBUPLOAD'])) {
  252. $file = file_load($form_state['values']['DBUPLOAD']);
  253. // If the $file is populated then this is a newly uploaded, temporary file.
  254. if (is_object($file)) {
  255. $form_state['dbFlag'] = 'upDB';
  256. $form_state['upDB_path'] = drupal_realpath($file->uri);
  257. }
  258. // Otherwise there was no file uploaded
  259. // Check if there was a database choosen from the list instead
  260. elseif (!empty($form_state['values']['SELECT_DB'])) {
  261. $form_state['dbFlag'] = 'blastdb';
  262. }
  263. // Otherwise they didn't select a database!!
  264. else {
  265. form_set_error('DB', t('No database selected. Either choose a database '
  266. . 'from the list or upload one of your own.'));
  267. }
  268. }
  269. // Otherwise there was no file uploaded
  270. // Check if there was a database choosen from the list instead
  271. elseif (!empty($form_state['values']['SELECT_DB'])) {
  272. $form_state['dbFlag'] = 'blastdb';
  273. }
  274. // Otherwise they didn't select a database!!
  275. else {
  276. form_set_error('DB', t('No database selected. Either choose a database '
  277. . 'from the list or upload one of your own.'));
  278. }
  279. // Validate Advanced Options
  280. //---------------------------
  281. $advanced_options_form_validate = 'blast_ui_' . $blast_program . '_advanced_options_form_validate';
  282. if (function_exists($advanced_options_form_validate)) {
  283. call_user_func_array(
  284. $advanced_options_form_validate,
  285. array(&$form, $form_state)
  286. );
  287. }
  288. }//blast_ui_per_blast_program_form_validate
  289. /**
  290. * Process the user options submitted via the blast program form.
  291. *
  292. * @see blast_ui_per_blast_program_form().
  293. */
  294. function blast_ui_per_blast_program_form_submit($form, &$form_state) {
  295. $error = FALSE;
  296. $blast_program = $form_state['values']['blast_program'];
  297. if ($form_state['values']['db_type'] == 'nucleotide') {
  298. $mdb_type = 'nucl';
  299. }
  300. else {
  301. $mdb_type = 'prot';
  302. }
  303. // If the query was submitted via the texrfield then create a file containing it
  304. if (isset($form_state['qFlag'])) {
  305. if ($form_state['qFlag'] == 'seqQuery') {
  306. $seq_content = $form_state['values']['FASTA'];
  307. $query = '/tmp/' . date('YMd_His') . '_query.fasta';
  308. file_put_contents ($query , $seq_content);
  309. }
  310. elseif ($form_state['qFlag'] == 'upQuery') {
  311. $query = $form_state['upQuery_path'];
  312. }
  313. }
  314. // If the BLAST database was uploaded then use it to run the BLAST
  315. if ($form_state['dbFlag'] == 'upDB') {
  316. // Since we only support using the -db flag (not -subject) we need to create a
  317. // blast database for the FASTA uploaded.
  318. // NOTE: We can't support subject because we need to generate the ASN.1+ format
  319. // to provide multiple download type options from the same BLAST
  320. $blastdb_with_path = $form_state['upDB_path'];
  321. $result = NULL;
  322. exec("makeblastdb -in $blastdb_with_path -dbtype $mdb_type -parse_seqids 2>&1", $result);
  323. // Check that the BLAST database was made correctly.
  324. $result = implode('<br />', $result);
  325. if (preg_match('/Error/', $result)) {
  326. drupal_set_message('Unable to generate a BLAST database from your uploaded
  327. FASTA sequence. Please check that your file is a valid FASTA file and that if
  328. your sequence headers include pipes (i.e.: | ) they adhere to '
  329. . l('NCBI standards.', 'http://www.ncbi.nlm.nih.gov/books/NBK21097/table/A632/?report=objectonly', array('attributes' => array('target' => '_blank'))),
  330. 'error'
  331. );
  332. $error = TRUE;
  333. }
  334. }//upload target db
  335. // Otherwise, we are using one of the website provided BLAST databases so form the
  336. // BLAST command accordingly
  337. elseif ($form_state['dbFlag'] == 'blastdb') {
  338. $selected_db = $form_state['values']['SELECT_DB'];
  339. $blastdb_node = node_load($selected_db);
  340. $blastdb_name = $blastdb_node->db_name;
  341. $blastdb_with_path = $blastdb_node->db_path;
  342. }
  343. // Now let each program process its own advanced options.
  344. $advanced_options = array();
  345. $advanced_options_form_submit = 'blast_ui_' . $blast_program . '_advanced_options_form_submit';
  346. if (function_exists($advanced_options_form_submit)) {
  347. $advanced_options = call_user_func_array(
  348. $advanced_options_form_submit,
  349. array($form, &$form_state)
  350. );
  351. }
  352. else {
  353. $advanced_options = array('none' => 0);
  354. }
  355. // Set path to a BLAST target file to check for its existence
  356. if ($mdb_type == 'nucl' && (preg_match('/\.[pn]al/', $blastdb_with_path) == 0)) {
  357. // Suffix may be .nsq or .nal
  358. if (is_readable("$blastdb_with_path.nsq")) {
  359. $blastdb_with_suffix = "$blastdb_with_path.nsq";
  360. }
  361. else if (is_readable("$blastdb_with_path.nal")) {
  362. $blastdb_with_suffix = "$blastdb_with_path.nal";
  363. }
  364. }
  365. else if ($mdb_type == 'prot' && (preg_match('/\.[pn]al/', $blastdb_with_path) == 0)) {
  366. // Suffix may be .psq or .pal
  367. if (is_readable("$blastdb_with_path.psq")) {
  368. $blastdb_with_suffix = "$blastdb_with_path.psq";
  369. }
  370. else if (is_readable("$blastdb_with_path.pal")) {
  371. $blastdb_with_suffix = "$blastdb_with_path.pal";
  372. }
  373. }
  374. else {
  375. $blastdb_with_suffix = $blastdb_with_path;
  376. }
  377. // Actually submit the BLAST Tripal Job
  378. if (is_readable($blastdb_with_suffix)) {
  379. // BLAST target exists.
  380. global $user;
  381. $output_filestub = date('YMd_His');
  382. $job_args = array(
  383. 'program' => $blast_program,
  384. 'query' => $query,
  385. 'database' => $blastdb_with_path,
  386. 'output_filename' => $output_filestub,
  387. 'options' => $advanced_options
  388. );
  389. $job_id = tripal_add_job(
  390. t('BLAST (@program): @query', array('@program' => $blast_program, '@query' => $query)),
  391. 'blast_job',
  392. 'run_BLAST_tripal_job',
  393. $job_args,
  394. $user->uid
  395. );
  396. $job_data = variable_get('job_data', '');
  397. $seq_rows = explode(PHP_EOL, $seq_content);
  398. foreach($seq_rows as $row) {
  399. if (strpos($row, ">") !== FALSE) {
  400. $query_def[] = trim($row, "> \t\n\r\0\x0B");
  401. }
  402. }
  403. $job_data[$job_id] =
  404. array(
  405. 'program' => $blast_program,
  406. 'job_url' => current_path(),
  407. 'fasta' => $seq_content,
  408. 'query_def' => $query_def,
  409. 'db_name' => $blastdb_node->db_name,
  410. 'db_option' => $selected_db,
  411. 'options' => $advanced_options,
  412. );
  413. variable_set('job_data', $job_data);
  414. //@deepaksomanadh create session and save the recent jobs in respective session
  415. if (session_status() === PHP_SESSION_NONE) {
  416. session_start();
  417. }
  418. $sid = session_id();
  419. $job_encode_id = base64_encode($job_id);
  420. $job_url = "blast/report/$job_encode_id";
  421. $all_jobs = $_SESSION['all_jobs'];
  422. $session_jobs = $all_jobs[$sid];
  423. $session_jobs[$job_id] =
  424. array(
  425. 'job_output_url'=> $job_url,
  426. 'query_defs' => $query_def,
  427. 'target' => $blastdb_name,
  428. 'program' => $blast_program,
  429. 'date' => date('Y-M-d h:i:s'),
  430. );
  431. $all_jobs[$sid] = $session_jobs;
  432. $_SESSION['all_jobs'] = $all_jobs;
  433. // Comment out this line to run BLAST by hand via the command:
  434. // drush trp-run-jobs --username=admin
  435. tripal_jobs_launch(1, $job_id);
  436. //Encode the job_id
  437. $job_encode_id = base64_encode($job_id);
  438. // Redirect to the BLAST results page
  439. drupal_goto("blast/report/$job_encode_id");
  440. }//BLAST target is readable
  441. // BLAST target is unreadable
  442. else {
  443. $dbfile_uploaded_msg = ($form_state['dbFlag'] == 'upDB')
  444. ? 'The BLAST database was submitted via user upload.'
  445. : 'Existing BLAST Database was chosen.';
  446. tripal_report_error(
  447. 'blast_ui',
  448. TRIPAL_ERROR,
  449. "BLAST database %db unaccessible. %msg",
  450. array('%db' => $blastdb_with_path, '%msg' => $dbfile_uploaded_msg)
  451. );
  452. $msg = "$dbfile_uploaded_msg BLAST database '$blastdb_with_path' is unaccessible. ";
  453. $msg .= "Please contact the site administrator.";
  454. drupal_set_message($msg, 'error');
  455. }
  456. }//blast_ui_per_blast_program_form_submit