blast_ui.form_per_program.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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, .fas) 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 fas'),
  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. // Recent jobs list
  191. $form['recentjobs'] = array(
  192. '#type' => 'fieldset',
  193. '#prefix' => get_recent_jobs(),
  194. );
  195. return $form;
  196. }
  197. /**
  198. * Validate the user options submitted via the above form.
  199. *
  200. * @see blast_ui_per_blast_program_form().
  201. */
  202. function blast_ui_per_blast_program_form_validate($form, &$form_state) {
  203. $blast_program = $form_state['values']['blast_program'];
  204. $type = $form_state['values']['query_type'];
  205. if ($type == 'nucleotide') {
  206. $molecule_type = 'nucleotides';
  207. }
  208. else {
  209. $molecule_type = 'amino acid residues';
  210. }
  211. // Validate Query
  212. //----------------
  213. // @todo: We are currently not validating uploaded files are valid FASTA.
  214. // First check to see if we have an upload & if so then validate it.
  215. $file = file_load($form_state['values']['UPLOAD']);
  216. // If the $file is populated then this is a newly uploaded, temporary file.
  217. if (is_object($file)) {
  218. $form_state['qFlag'] = 'upQuery';
  219. $form_state['upQuery_path'] = drupal_realpath($file->uri);
  220. }
  221. // Otherwise there was no file uploaded.
  222. // Check if there was a query sequence entered in the texfield.
  223. elseif (!empty($form_state['input']['FASTA'])) {
  224. // Check to ensure that the query sequence entered is valid FASTA.
  225. if (validate_fasta_sequence($type, $form_state['input']['FASTA'])){
  226. form_set_error('query', t('You need to provide a valid FASTA sequence '
  227. . 'for the query. It should contain a FASTA header/definition line '
  228. . 'followed by %molecule-type. For more information see the '
  229. . '<a href="@url" target="_blank">NCBI FASTA Specification</a>.',
  230. array(
  231. '%molecule-type' => $molecule_type,
  232. '@url' => 'http://www.ncbi.nlm.nih.gov/BLAST/blastcgihelp.shtml'
  233. )));
  234. }
  235. else {
  236. $form_state['qFlag'] = 'seqQuery';
  237. }
  238. }
  239. // Otherwise they didn't enter a query!!
  240. else {
  241. form_set_error('query', t('No query sequence given. Only raw sequence or '
  242. . 'sequence of type FASTA can be read. Enter sequence in the box provided '
  243. . 'or upload a plain text file.'));
  244. }
  245. // Validate Database
  246. //-------------------
  247. // @todo: We are currently not validating uploaded files are valid FASTA.
  248. // First check to see if we have an upload & if so then validate it.
  249. if (isset($form_state['values']['DBUPLOAD'])) {
  250. $file = file_load($form_state['values']['DBUPLOAD']);
  251. // If the $file is populated then this is a newly uploaded, temporary file.
  252. if (is_object($file)) {
  253. $form_state['dbFlag'] = 'upDB';
  254. $form_state['upDB_path'] = drupal_realpath($file->uri);
  255. }
  256. // Otherwise there was no file uploaded
  257. // Check if there was a database choosen from the list instead
  258. elseif (!empty($form_state['values']['SELECT_DB'])) {
  259. $form_state['dbFlag'] = 'blastdb';
  260. }
  261. // Otherwise they didn't select a database!!
  262. else {
  263. form_set_error('DB', t('No database selected. Either choose a database '
  264. . 'from the list or upload one of your own.'));
  265. }
  266. }
  267. // Otherwise there was no file uploaded
  268. // Check if there was a database choosen from the list instead
  269. elseif (!empty($form_state['values']['SELECT_DB'])) {
  270. $form_state['dbFlag'] = 'blastdb';
  271. }
  272. // Otherwise they didn't select a database!!
  273. else {
  274. form_set_error('DB', t('No database selected. Either choose a database '
  275. . 'from the list or upload one of your own.'));
  276. }
  277. // Validate Advanced Options
  278. //---------------------------
  279. $advanced_options_form_validate = 'blast_ui_' . $blast_program . '_advanced_options_form_validate';
  280. if (function_exists($advanced_options_form_validate)) {
  281. call_user_func_array(
  282. $advanced_options_form_validate,
  283. array(&$form, $form_state)
  284. );
  285. }
  286. }//blast_ui_per_blast_program_form_validate
  287. /**
  288. * Process the user options submitted via the blast program form.
  289. *
  290. * @see blast_ui_per_blast_program_form().
  291. */
  292. function blast_ui_per_blast_program_form_submit($form, &$form_state) {
  293. $error = FALSE;
  294. $blast_program = $form_state['values']['blast_program'];
  295. if ($form_state['values']['db_type'] == 'nucleotide') {
  296. $mdb_type = 'nucl';
  297. }
  298. else {
  299. $mdb_type = 'prot';
  300. }
  301. // If the query was submitted via the texrfield then create a file containing it
  302. if (isset($form_state['qFlag'])) {
  303. if ($form_state['qFlag'] == 'seqQuery') {
  304. $seq_content = $form_state['values']['FASTA'];
  305. $query = '/tmp/' . date('YMd_His') . '_query.fasta';
  306. file_put_contents ($query , $seq_content);
  307. }
  308. elseif ($form_state['qFlag'] == 'upQuery') {
  309. $query = $form_state['upQuery_path'];
  310. }
  311. }
  312. // If the BLAST database was uploaded then use it to run the BLAST
  313. if ($form_state['dbFlag'] == 'upDB') {
  314. // Since we only support using the -db flag (not -subject) we need to create a
  315. // blast database for the FASTA uploaded.
  316. // NOTE: We can't support subject because we need to generate the ASN.1+ format
  317. // to provide multiple download type options from the same BLAST
  318. $blastdb_with_path = $form_state['upDB_path'];
  319. $result = NULL;
  320. exec("makeblastdb -in $blastdb_with_path -dbtype $mdb_type -parse_seqids 2>&1", $result);
  321. // Check that the BLAST database was made correctly.
  322. $result = implode('<br />', $result);
  323. if (preg_match('/Error/', $result)) {
  324. drupal_set_message('Unable to generate a BLAST database from your uploaded
  325. FASTA sequence. Please check that your file is a valid FASTA file and that if
  326. your sequence headers include pipes (i.e.: | ) they adhere to '
  327. . l('NCBI standards.', 'http://www.ncbi.nlm.nih.gov/books/NBK21097/table/A632/?report=objectonly', array('attributes' => array('target' => '_blank'))),
  328. 'error'
  329. );
  330. $error = TRUE;
  331. }
  332. }//upload target db
  333. // Otherwise, we are using one of the website provided BLAST databases so form the
  334. // BLAST command accordingly
  335. elseif ($form_state['dbFlag'] == 'blastdb') {
  336. $selected_db = $form_state['values']['SELECT_DB'];
  337. $blastdb_node = node_load($selected_db);
  338. $blastdb_name = $blastdb_node->db_name;
  339. $blastdb_with_path = $blastdb_node->db_path;
  340. }
  341. // Now let each program process its own advanced options.
  342. $advanced_options = array();
  343. $advanced_options_form_submit = 'blast_ui_' . $blast_program . '_advanced_options_form_submit';
  344. if (function_exists($advanced_options_form_submit)) {
  345. $advanced_options = call_user_func_array(
  346. $advanced_options_form_submit,
  347. array($form, &$form_state)
  348. );
  349. }
  350. else {
  351. $advanced_options = array('none' => 0);
  352. }
  353. // Set path to a BLAST target file to check for its existence
  354. if ($mdb_type == 'nucl' && (preg_match('/\.[pn]al/', $blastdb_with_path) == 0)) {
  355. // Suffix may be .nsq or .nal
  356. if (is_readable("$blastdb_with_path.nsq")) {
  357. $blastdb_with_suffix = "$blastdb_with_path.nsq";
  358. }
  359. else if (is_readable("$blastdb_with_path.nal")) {
  360. $blastdb_with_suffix = "$blastdb_with_path.nal";
  361. }
  362. }
  363. else if ($mdb_type == 'prot' && (preg_match('/\.[pn]al/', $blastdb_with_path) == 0)) {
  364. // Suffix may be .psq or .pal
  365. if (is_readable("$blastdb_with_path.psq")) {
  366. $blastdb_with_suffix = "$blastdb_with_path.psq";
  367. }
  368. else if (is_readable("$blastdb_with_path.pal")) {
  369. $blastdb_with_suffix = "$blastdb_with_path.pal";
  370. }
  371. }
  372. else {
  373. $blastdb_with_suffix = $blastdb_with_path;
  374. }
  375. // Actually submit the BLAST Tripal Job
  376. if (is_readable($blastdb_with_suffix)) {
  377. // BLAST target exists.
  378. global $user;
  379. $output_filestub = date('YMd_His');
  380. $job_args = array(
  381. 'program' => $blast_program,
  382. 'query' => $query,
  383. 'database' => $blastdb_with_path,
  384. 'output_filename' => $output_filestub,
  385. 'options' => $advanced_options
  386. );
  387. $job_id = tripal_add_job(
  388. t('BLAST (@program): @query', array('@program' => $blast_program, '@query' => $query)),
  389. 'blast_job',
  390. 'run_BLAST_tripal_job',
  391. $job_args,
  392. $user->uid
  393. );
  394. $job_data = variable_get('job_data', '');
  395. $seq_rows = explode(PHP_EOL, $seq_content);
  396. foreach($seq_rows as $row) {
  397. if (strpos($row, ">") !== FALSE) {
  398. $query_def[] = trim($row, "> \t\n\r\0\x0B");
  399. }
  400. }
  401. $job_data[$job_id] =
  402. array(
  403. 'program' => $blast_program,
  404. 'job_url' => current_path(),
  405. 'fasta' => $seq_content,
  406. 'query_def' => $query_def,
  407. 'db_name' => $blastdb_node->db_name,
  408. 'db_option' => $selected_db,
  409. 'options' => $advanced_options,
  410. );
  411. variable_set('job_data', $job_data);
  412. //@deepaksomanadh create session and save the recent jobs in respective session
  413. if (session_status() === PHP_SESSION_NONE) {
  414. session_start();
  415. }
  416. $sid = session_id();
  417. $job_encode_id = base64_encode($job_id);
  418. $job_url = "blast/report/$job_encode_id";
  419. $all_jobs = $_SESSION['all_jobs'];
  420. $session_jobs = $all_jobs[$sid];
  421. $session_jobs[$job_id] =
  422. array(
  423. 'job_output_url'=> $job_url,
  424. 'query_defs' => $query_def,
  425. 'target' => $blastdb_name,
  426. 'program' => $blast_program,
  427. 'date' => date('Y-M-d h:i:s'),
  428. );
  429. $all_jobs[$sid] = $session_jobs;
  430. $_SESSION['all_jobs'] = $all_jobs;
  431. // Comment out this line to run BLAST by hand via the command:
  432. // drush trp-run-jobs --username=admin
  433. tripal_jobs_launch(1, $job_id);
  434. //Encode the job_id
  435. $job_encode_id = base64_encode($job_id);
  436. // Redirect to the BLAST results page
  437. drupal_goto("blast/report/$job_encode_id");
  438. }//BLAST target is readable
  439. // BLAST target is unreadable
  440. else {
  441. $dbfile_uploaded_msg = ($form_state['dbFlag'] == 'upDB')
  442. ? 'The BLAST database was submitted via user upload.'
  443. : 'Existing BLAST Database was chosen.';
  444. tripal_report_error(
  445. 'blast_ui',
  446. TRIPAL_ERROR,
  447. "BLAST database %db unaccessible. %msg",
  448. array('%db' => $blastdb_with_path, '%msg' => $dbfile_uploaded_msg)
  449. );
  450. $msg = "$dbfile_uploaded_msg BLAST database '$blastdb_with_path' is unaccessible. ";
  451. $msg .= "Please contact the site administrator.";
  452. drupal_set_message($msg, 'error');
  453. }
  454. }//blast_ui_per_blast_program_form_submit