blast_ui.blastp.inc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. <?php
  2. /**
  3. * @file
  4. * Contains all functions for the Protein BLAST
  5. */
  6. /**
  7. * Protein BLAST Submission Form
  8. *
  9. * @see blast_protein_form_validate()
  10. * @see blast_protein_form_submit()
  11. */
  12. function blast_protein_form($form, &$form_state) {
  13. // CSS support to the form
  14. $form['#attached']['css'] = array(
  15. drupal_get_path('module', 'blast_ui') . '/css/form.css',
  16. );
  17. // PROTEIN QUERY
  18. //.........................
  19. $form['query'] = array(
  20. '#type' => 'fieldset',
  21. '#title' => t('Enter Query Sequence'),
  22. '#description' => t('Enter one or more queries in the top text box or use the browse button to upload a file from your local disk. The file may contain a single sequence or a list of sequences. In both cases, the data must be in FASTA format. <a href="http://www.ncbi.nlm.nih.gov/BLAST/blastcgihelp.shtml" target="_blank">More information..</a> '),
  23. '#collapsible' => TRUE,
  24. '#collapsed' => FALSE,
  25. '#prefix' => '<div class="two-col">',
  26. '#suffix' => '</div>',
  27. );
  28. $form['query']['example_sequence'] = array(
  29. '#type' => 'button',
  30. '#button_type'=> 'button',
  31. '#limit_validation_errors' => array(),
  32. '#value' => t('Example Sequence'),
  33. '#prefix' => '<div class="center">',
  34. '#suffix' => '</div>',
  35. '#validate' => array(),
  36. '#ajax' => array(
  37. 'callback' => 'ajax_nucleotide_text_area_callback',
  38. 'wrapper' => 'fasta_seq',
  39. 'method' => 'replace',
  40. 'effect' => 'fade',
  41. ),
  42. '#attributes' => array('onclick' => 'return false;'),
  43. );
  44. $form['query']['FASTA'] = array(
  45. '#type' => 'textarea',
  46. '#title' => t('Enter FASTA sequence(s)'),
  47. '#description'=>t('Enter query sequence(s) in the text area.'),
  48. '#prefix' => '<div id="fasta_seq">',
  49. '#suffix' => '</div>',
  50. );
  51. // Upload a file as an alternative to enter a query sequence
  52. $form['#attributes']['enctype'] = 'multipart/form-data';
  53. $form['query']['UPLOAD'] = array(
  54. '#title' => 'Or upload your own query FASTA: ',
  55. '#type' => 'managed_file',
  56. '#description' => t('The file should be a plain-text FASTA
  57. (.fasta, .fna, .fa) file. In other words, it cannot have formatting as is the
  58. case with MS Word (.doc, .docx) or Rich Text Format (.rtf). It cannot be greater
  59. than %max_size in size. <strong>Don\'t forget to press the Upload button before
  60. attempting to submit your BLAST.</strong>',
  61. array(
  62. '%max_size' => round(file_upload_max_size() / 1024 / 1024,1) . 'MB'
  63. )
  64. ),
  65. '#upload_validators' => array(
  66. 'file_validate_extensions' => array('fasta fna fa'),
  67. 'file_validate_size' => array(file_upload_max_size()),
  68. ),
  69. );
  70. $form['query']['example_sequence'] = array(
  71. '#type' => 'button',
  72. '#button_type'=> 'button',
  73. '#limit_validation_errors' => array(),
  74. '#value' => t('Example Sequence'),
  75. '#prefix' => '<div class="center">',
  76. '#suffix' => '</div>',
  77. '#validate' => array(),
  78. '#ajax' => array(
  79. 'callback' => 'ajax_protein_text_area_callback',
  80. 'wrapper' => 'fasta_seq',
  81. 'method' => 'replace',
  82. 'effect' => 'fade',
  83. ),
  84. '#attributes' => array('onclick' => 'return false;'),
  85. );
  86. // BLAST DATABASE
  87. //.........................
  88. $form['DB'] = array(
  89. '#type' => 'fieldset',
  90. '#title' => t('Choose Search Set'),
  91. '#description' => t('Choose from one of the protein BLAST databases listed below. You can also use the browse button to upload a file from your local disk. The file may contain a single sequence or a list of sequences. '),
  92. '#collapsible' => TRUE,
  93. '#collapsed' => FALSE,
  94. );
  95. $options = get_blast_database_options('p');
  96. $form['DB']['SELECT_DB'] = array(
  97. '#type' => 'select',
  98. '#title' => t('Protein BLAST Databases:'),
  99. '#options' => $options,
  100. '#default_value' => t('Select a database'),
  101. );
  102. // Upload a file as an alternative to enter a query sequence
  103. $form['#attributes']['enctype'] = 'multipart/form-data';
  104. $form['DB']['DBUPLOAD'] = array(
  105. '#title' => 'Or upload your own dataset: ',
  106. '#type' => 'managed_file',
  107. '#description' => t('The file should be a plain-text FASTA
  108. (.fasta, .fna, .fa) file. In other words, it cannot have formatting as is the
  109. case with MS Word (.doc, .docx) or Rich Text Format (.rtf). It cannot be greater
  110. than %max_size in size. <strong>Don\'t forget to press the Upload button before
  111. attempting to submit your BLAST.</strong>',
  112. array(
  113. '%max_size' => round(file_upload_max_size() / 1024 / 1024,1) . 'MB'
  114. )
  115. ),
  116. '#upload_validators' => array(
  117. 'file_validate_extensions' => array('fasta fna fa'),
  118. 'file_validate_size' => array(file_upload_max_size()),
  119. ),
  120. );
  121. // ALGORITHM PARAMETERS
  122. //.........................
  123. $form['ALG'] = array(
  124. '#type' => 'fieldset',
  125. '#title' => t('Algorithm parameters'),
  126. '#collapsible' => TRUE,
  127. '#collapsed' => TRUE,
  128. );
  129. //General parameters
  130. $form['ALG']['GParam'] = array(
  131. '#type' => 'fieldset',
  132. '#title' => t('General parameters'),
  133. '#collapsible' => FALSE,
  134. );
  135. $form['ALG']['GParam']['maxTarget'] = array(
  136. '#type' => 'select',
  137. '#title' => t('Max target sequences:'),
  138. '#options' => array(
  139. 0 => t('10'),
  140. 1 => t('50'),
  141. 2 => t('100'),
  142. 3 => t('250'),
  143. 4 => t('500'),
  144. 5 => t('1000'),
  145. 6 => t('5000'),
  146. 7 => t('10000'),
  147. 8 => t('20000'),
  148. ),
  149. '#default_value' => 2,
  150. '#description' => t('Select the maximum number of aligned sequences to display'),
  151. );
  152. $form['ALG']['GParam']['shortQueries'] = array(
  153. '#type' => 'checkbox',
  154. '#title' => t('Automatically adjust parameters for short input sequences'),
  155. '#default_value' => TRUE,
  156. );
  157. $form['ALG']['GParam']['eVal'] = array(
  158. '#type' => 'textfield',
  159. '#title' => t('e-value(Expect threshold)'),
  160. '#default_value' => 10,
  161. '#size' => 12,
  162. '#maxlength' => 20,
  163. '#description' => t('Expected number of chance matches in a random model.'),
  164. );
  165. $form['ALG']['GParam']['wordSize'] = array(
  166. '#type' => 'select',
  167. '#title' => t('Word size:'),
  168. '#options' => array(
  169. 0 => t('2'),
  170. 1 => t('3'),
  171. ),
  172. '#default_value' => 1,
  173. '#description' => t('The length of the seed that initiates an alignment'),
  174. );
  175. $form['ALG']['GParam']['qRange'] = array(
  176. '#type' => 'textfield',
  177. '#title' => t('Max matches in a query range'),
  178. '#default_value' => 0,
  179. '#size' => 12,
  180. '#maxlength' => 20,
  181. '#description' => t('Limit the number of matches to a query range. This option is useful if many strong matches to one part of a query may prevent BLAST from presenting weaker matches to another part of the query.'),
  182. );
  183. // Scoring parameters
  184. $form['ALG']['SParam'] = array(
  185. '#type' => 'fieldset',
  186. '#title' => t('Scoring parameters'),
  187. '#collapsible' => FALSE,
  188. );
  189. $options_first = _ajax_example_get_first_dropdown_options();
  190. $selected = isset($form_state['values']['MATRIX'] ) ? $form_state['values']['MATRIX'] : key($options_first);
  191. $form['ALG']['SParam']['MATRIX'] = array(
  192. '#type' => 'select',
  193. '#title' => 'Matrix',
  194. '#options' => $options_first,
  195. '#default_value' => $selected,
  196. '#description' => t('Assigns a score for aligning pairs of residues, and determines overall alignment score..'),
  197. '#ajax' => array(
  198. 'callback' => 'ajax_example_dependent_dropdown_callback',
  199. 'wrapper' => 'dropdown-second-replace',
  200. ),
  201. );
  202. $form['ALG']['SParam']['gapCost'] = array(
  203. '#type' => 'select',
  204. '#title' => t('Gap Costs:'),
  205. '#prefix' => '<div id="dropdown-second-replace">',
  206. '#suffix' => '</div>',
  207. '#options' => _ajax_example_get_second_dropdown_options($selected),
  208. '#default_value' => 2,
  209. '#description' => t('Cost to create and extend a gap in an alignment.'),
  210. );
  211. $form['ALG']['SParam']['M&MScores'] = array(
  212. '#type' => 'select',
  213. '#title' => t('Match/Mismatch Scores:'),
  214. '#options' => array(
  215. 0 => t('No adjustment'),
  216. 1 => t('Composition-based statistics'),
  217. 2 => t('Conditional compositional score matrix adjustment'),
  218. 3 => t('Universal composition score matrix adjustment '),
  219. ),
  220. '#default_value' => 2,
  221. '#description' => t('Matrix adjustment method to compensate for amino acid composition of sequences'),
  222. );
  223. //Submit
  224. $form['submit'] = array(
  225. '#type' => 'submit',
  226. '#default_value' => ' BLAST ',
  227. );
  228. return $form;
  229. }
  230. /**
  231. * Form validation handler for blast_protein_form().
  232. *
  233. * @see blast_protein_form_validate()
  234. */
  235. function blast_protein_form_validate($form, &$form_state) {
  236. // Validate Query
  237. //----------------
  238. // @todo: We are currently not validating uploaded files are valid FASTA.
  239. // First check to see if we have an upload & if so then validate it.
  240. $file = file_load($form_state['values']['UPLOAD']);
  241. // If the $file is populated then this is a newly uploaded, temporary file.
  242. if (is_object($file)) {
  243. $form_state['qFlag'] = 'upQuery';
  244. $form_state['upQuery_path'] = drupal_realpath($file->uri);
  245. }
  246. // Otherwise there was no file uploaded.
  247. // Check if there was a query sequence entered in the texfield.
  248. elseif (!empty($form_state['input']['FASTA'])) {
  249. // Check to ensure that the query sequence entered is valid FASTA.
  250. if (validateFasta($form_state['input']['FASTA'])){
  251. form_set_error('pBLAST', t('You need to provide a valid FASTA sequence
  252. for the query.'));
  253. }
  254. else {
  255. $form_state['qFlag'] = 'seqQuery';
  256. }
  257. }
  258. // Otherwise they didn't enter a query!!
  259. else {
  260. form_set_error('pBLAST', t('You must either enter a FASTA sequence in the
  261. text field or upload one of your own.'));
  262. }
  263. // Validate Database
  264. //-------------------
  265. // @todo: We are currently not validating uploaded files are valid FASTA.
  266. // First check to see if we have an upload & if so then validate it.
  267. $file = file_load($form_state['values']['DBUPLOAD']);
  268. // If the $file is populated then this is a newly uploaded, temporary file.
  269. if (is_object($file)) {
  270. $form_state['dbFlag'] = 'upDB';
  271. $form_state['upDB_path'] = drupal_realpath($file->uri);
  272. }
  273. // Otherwise there was no file uploaded
  274. // Check if there was a database choosen from the list instead
  275. elseif (!empty($form_state['values']['SELECT_DB'])) {
  276. $form_state['dbFlag'] = 'blastdb';
  277. }
  278. // Otherwise they didn't select a database!!
  279. else {
  280. form_set_error('DB', t('You must either choose a database from the list or upload one of your own.'));
  281. }
  282. }
  283. /**
  284. * Form submition handler for blast_protein_form().
  285. *
  286. * @see blast_protein_form_submit()
  287. */
  288. function blast_protein_form_submit($form, &$form_state) {
  289. $error = FALSE;
  290. $eVal = $form_state['values']['eVal'];
  291. $trgtKey = $form_state['values']['maxTarget'];
  292. $numAlign = $form['ALG']['GParam']['maxTarget']['#options'][$trgtKey];
  293. $wsKey = $form_state['values']['wordSize'];
  294. $wordSize = $form['ALG']['GParam']['wordSize']['#options'][$wsKey];
  295. // Expand Gap Cost key into open and extend penalties
  296. $gapKey = $form_state['values']['MATRIX'];
  297. switch ($gapKey) {
  298. case 0:
  299. $matrix ="PAM30";
  300. $gapKey = $form_state['values']['gapCost'];
  301. switch ($gapKey) {
  302. case 0:
  303. $gapOpen = 7;
  304. $gapExtend = 2;
  305. break;
  306. case 1:
  307. $gapOpen = 6;
  308. $gapExtend = 2;
  309. break;
  310. case 2:
  311. $gapOpen = 5;
  312. $gapExtend = 2;
  313. break;
  314. case 3:
  315. $gapOpen = 10;
  316. $gapExtend = 1;
  317. break;
  318. case 4:
  319. $gapOpen = 9;
  320. $gapExtend = 1;
  321. break;
  322. case 5:
  323. $gapOpen = 8;
  324. $gapExtend = 1;
  325. break;
  326. }
  327. break;
  328. case 1:
  329. $matrix ="PAM70";
  330. $gapKey = $form_state['values']['gapCost'];
  331. switch ($gapKey) {
  332. case 0:
  333. $gapOpen = 8;
  334. $gapExtend = 2;
  335. break;
  336. case 1:
  337. $gapOpen = 7;
  338. $gapExtend = 2;
  339. break;
  340. case 2:
  341. $gapOpen = 6;
  342. $gapExtend = 2;
  343. break;
  344. case 3:
  345. $gapOpen = 11;
  346. $gapExtend = 1;
  347. break;
  348. case 4:
  349. $gapOpen = 10;
  350. $gapExtend = 1;
  351. break;
  352. case 5:
  353. $gapOpen = 9;
  354. $gapExtend = 1;
  355. break;
  356. }
  357. break;
  358. case 2:
  359. $matrix ="PAM250";
  360. $gapKey = $form_state['values']['gapCost'];
  361. switch ($gapKey) {
  362. case 0:
  363. $gapOpen = 15;
  364. $gapExtend = 3;
  365. break;
  366. case 1:
  367. $gapOpen = 14;
  368. $gapExtend = 3;
  369. break;
  370. case 2:
  371. $gapOpen = 13;
  372. $gapExtend = 3;
  373. break;
  374. case 3:
  375. $gapOpen = 12;
  376. $gapExtend = 3;
  377. break;
  378. case 4:
  379. $gapOpen = 11;
  380. $gapExtend = 3;
  381. break;
  382. case 5:
  383. $gapOpen = 17;
  384. $gapExtend = 2;
  385. break;
  386. case 6:
  387. $gapOpen = 16;
  388. $gapExtend = 2;
  389. break;
  390. case 7:
  391. $gapOpen = 15;
  392. $gapExtend = 2;
  393. break;
  394. case 8:
  395. $gapOpen = 14;
  396. $gapExtend = 2;
  397. break;
  398. case 9:
  399. $gapOpen = 13;
  400. $gapExtend = 2;
  401. break;
  402. case 10:
  403. $gapOpen = 21;
  404. $gapExtend = 1;
  405. break;
  406. case 11:
  407. $gapOpen = 20;
  408. $gapExtend = 1;
  409. break;
  410. case 12:
  411. $gapOpen = 19;
  412. $gapExtend = 1;
  413. break;
  414. case 13:
  415. $gapOpen = 18;
  416. $gapExtend = 1;
  417. break;
  418. case 14:
  419. $gapOpen = 17;
  420. $gapExtend = 1;
  421. break;
  422. }
  423. break;
  424. case 3:
  425. $matrix ="BLOSUM80";
  426. $gapKey = $form_state['values']['gapCost'];
  427. switch ($gapKey) {
  428. case 0:
  429. $gapOpen = 8;
  430. $gapExtend = 2;
  431. break;
  432. case 1:
  433. $gapOpen = 7;
  434. $gapExtend = 2;
  435. break;
  436. case 2:
  437. $gapOpen = 6;
  438. $gapExtend = 2;
  439. break;
  440. case 3:
  441. $gapOpen = 11;
  442. $gapExtend = 1;
  443. break;
  444. case 4:
  445. $gapOpen = 10;
  446. $gapExtend = 1;
  447. break;
  448. case 5:
  449. $gapOpen = 9;
  450. $gapExtend = 1;
  451. break;
  452. }
  453. break;
  454. case 4:
  455. $matrix ="BLOSUM62";
  456. $gapKey = $form_state['values']['gapCost'];
  457. switch ($gapKey) {
  458. case 0:
  459. $gapOpen = 11;
  460. $gapExtend = 2;
  461. break;
  462. case 1:
  463. $gapOpen = 10;
  464. $gapExtend = 2;
  465. break;
  466. case 2:
  467. $gapOpen = 9;
  468. $gapExtend = 2;
  469. break;
  470. case 3:
  471. $gapOpen = 8;
  472. $gapExtend = 2;
  473. break;
  474. case 4:
  475. $gapOpen = 7;
  476. $gapExtend = 2;
  477. break;
  478. case 5:
  479. $gapOpen = 6;
  480. $gapExtend = 2;
  481. break;
  482. case 6:
  483. $gapOpen = 13;
  484. $gapExtend = 1;
  485. break;
  486. case 7:
  487. $gapOpen = 12;
  488. $gapExtend = 1;
  489. break;
  490. case 8:
  491. $gapOpen = 11;
  492. $gapExtend = 1;
  493. break;
  494. case 9:
  495. $gapOpen = 10;
  496. $gapExtend = 1;
  497. break;
  498. case 10:
  499. $gapOpen = 9;
  500. $gapExtend = 1;
  501. break;
  502. }
  503. break;
  504. case 5:
  505. $matrix ="BLOSUM45";
  506. $gapKey = $form_state['values']['gapCost'];
  507. switch ($gapKey) {
  508. case 0:
  509. $gapOpen = 13;
  510. $gapExtend = 3;
  511. break;
  512. case 1:
  513. $gapOpen = 12;
  514. $gapExtend = 3;
  515. break;
  516. case 2:
  517. $gapOpen = 11;
  518. $gapExtend = 3;
  519. break;
  520. case 3:
  521. $gapOpen = 10;
  522. $gapExtend = 3;
  523. break;
  524. case 4:
  525. $gapOpen = 15;
  526. $gapExtend = 2;
  527. break;
  528. case 5:
  529. $gapOpen = 14;
  530. $gapExtend = 2;
  531. break;
  532. case 6:
  533. $gapOpen = 13;
  534. $gapExtend = 2;
  535. break;
  536. case 7:
  537. $gapOpen = 12;
  538. $gapExtend = 2;
  539. break;
  540. case 8:
  541. $gapOpen = 19;
  542. $gapExtend = 1;
  543. break;
  544. case 9:
  545. $gapOpen = 18;
  546. $gapExtend = 1;
  547. break;
  548. case 10:
  549. $gapOpen = 17;
  550. $gapExtend = 1;
  551. break;
  552. case 11:
  553. $gapOpen = 16;
  554. $gapExtend = 1;
  555. break;
  556. }
  557. break;
  558. case 6:
  559. $matrix ="BLOSUM50";
  560. $gapKey = $form_state['values']['gapCost'];
  561. switch ($gapKey) {
  562. case 0:
  563. $gapOpen = 13;
  564. $gapExtend = 3;
  565. break;
  566. case 1:
  567. $gapOpen = 12;
  568. $gapExtend = 3;
  569. break;
  570. case 2:
  571. $gapOpen = 11;
  572. $gapExtend = 3;
  573. break;
  574. case 3:
  575. $gapOpen = 10;
  576. $gapExtend = 3;
  577. break;
  578. case 4:
  579. $gapOpen = 9;
  580. $gapExtend = 3;
  581. break;
  582. case 5:
  583. $gapOpen = 16;
  584. $gapExtend = 2;
  585. break;
  586. case 6:
  587. $gapOpen = 15;
  588. $gapExtend = 2;
  589. break;
  590. case 7:
  591. $gapOpen = 14;
  592. $gapExtend = 2;
  593. break;
  594. case 8:
  595. $gapOpen = 13;
  596. $gapExtend = 2;
  597. break;
  598. case 9:
  599. $gapOpen = 12;
  600. $gapExtend = 2;
  601. break;
  602. case 10:
  603. $gapOpen = 19;
  604. $gapExtend = 1;
  605. break;
  606. case 11:
  607. $gapOpen = 18;
  608. $gapExtend = 1;
  609. break;
  610. case 12:
  611. $gapOpen = 17;
  612. $gapExtend = 1;
  613. break;
  614. case 13:
  615. $gapOpen = 16;
  616. $gapExtend = 1;
  617. break;
  618. case 14:
  619. $gapOpen = 15;
  620. $gapExtend = 1;
  621. break;
  622. }
  623. break;
  624. case 7:
  625. $matrix ="BLOSUM90";
  626. $gapKey = $form_state['values']['gapCost'];
  627. switch ($gapKey) {
  628. case 0:
  629. $gapOpen = 9;
  630. $gapExtend = 2;
  631. break;
  632. case 1:
  633. $gapOpen = 8;
  634. $gapExtend = 2;
  635. break;
  636. case 2:
  637. $gapOpen = 7;
  638. $gapExtend = 2;
  639. break;
  640. case 3:
  641. $gapOpen = 6;
  642. $gapExtend = 2;
  643. break;
  644. case 4:
  645. $gapOpen = 11;
  646. $gapExtend = 1;
  647. break;
  648. case 5:
  649. $gapOpen = 10;
  650. $gapExtend = 1;
  651. break;
  652. case 6:
  653. $gapOpen = 9;
  654. $gapExtend = 1;
  655. break;
  656. }
  657. break;
  658. }
  659. // If the query was submitted via the texrfield then create a file containing it
  660. if ( isset($form_state['qFlag']) ) {
  661. if ( $form_state['qFlag'] == 'seqQuery' ) {
  662. $seq_content = $form_state['values']['FASTA'];
  663. $query = '/tmp/' . date('YMd_His') . '_query.fasta';
  664. file_put_contents ( $query , $seq_content);
  665. }
  666. elseif ( $form_state['qFlag'] == 'upQuery' ) {
  667. $query = $form_state['upQuery_path'];
  668. }
  669. }
  670. // If the BLAST database was uploaded then use it to run the BLAST
  671. if ($form_state['dbFlag'] == 'upDB') {
  672. // Since we only support using the -db flag (not -subject) we need to create a
  673. // blast database for the FASTA uploaded.
  674. // NOTE: We can't support subject because we need to generate the ASN.1+ format
  675. // to provide multiple download type options from the same BLAST
  676. $blastdb_with_path = $form_state['upDB_path'];
  677. $result = NULL;
  678. exec("makeblastdb -in $blastdb_with_path -dbtype prot -parse_seqids 2>&1", $result);
  679. // Check that the BLAST database was made correctly.
  680. $result = implode('<br />', $result);
  681. if (preg_match('/Error/', $result)) {
  682. drupal_set_message('Unable to generate a BLAST database from your uploaded
  683. FASTA sequence. Please check that your file is a valid FASTA file and that if
  684. your sequence headers include pipes (i.e.: | ) they adhere to '
  685. . l('NCBI standards.', 'http://www.ncbi.nlm.nih.gov/books/NBK21097/table/A632/?report=objectonly', array('attributes' => array('target' => '_blank'))),
  686. 'error'
  687. );
  688. $error = TRUE;
  689. }
  690. }
  691. // Otherwise, we are using one of the website provided BLAST databases so form the
  692. // BLAST command accordingly
  693. elseif ($form_state['dbFlag'] == 'blastdb') {
  694. $selected_db = $form_state['values']['SELECT_DB'];
  695. $blastdb_node = node_load($selected_db);
  696. $blastdb_with_path = $blastdb_node->db_path;
  697. }
  698. // Actually submit the BLAST Tripal Job
  699. // NOTE: Tripal jobs needs to be executed from the command-line before it will be run!!
  700. $blastdb_with_suffix = $blastdb_with_path . '.psq';
  701. if (is_readable($blastdb_with_suffix)) {
  702. global $user;
  703. $output_filestub = date('YMd_His');
  704. $job_args = array(
  705. 'program' => 'blastp',
  706. 'query' => $query,
  707. 'database' => $blastdb_with_path,
  708. 'output_filename' => $output_filestub,
  709. 'options' => array(
  710. 'evalue' => $eVal,
  711. 'word_size' => $wordSize,
  712. 'gapopen' => $gapOpen,
  713. 'gapextend' => $gapExtend,
  714. 'matrix' => $matrix
  715. )
  716. );
  717. $job_id = tripal_add_job("BLAST (blastp): $query",'blast_job','run_BLAST_tripal_job', $job_args, $user->uid);
  718. // Redirect to the BLAST results page
  719. drupal_goto("blast/report/$job_id");
  720. }
  721. // We check if $error is set to TRUE because if so then the error has already
  722. // been reported.
  723. elseif (!$error) {
  724. $dbfile_uploaded_msg = ($form_state['dbFlag'] == 'upDB') ? 'The BLAST database was submitted via user upload.' : 'Existing BLAST Database was chosen';
  725. tripal_report_error(
  726. 'blast_ui',
  727. TRIPAL_ERROR,
  728. "BLAST database %db unaccessible. $dbfile_uploaded_msg",
  729. array('%db' => $blastdb_with_path)
  730. );
  731. drupal_set_message('BLAST database unaccessible. Please contact the site administrator.','error');
  732. }
  733. }
  734. /**
  735. * FASTA validating parser
  736. *
  737. * @param $sequence
  738. * A string of characters to be validated. A sequence in FASTA format begins with a single-line description, followed by lines of sequence data.
  739. * The description line is distinguished from the sequence data by a greater-than (">") symbol in the first column.
  740. * The word following the ">" symbol is the identifier of the sequence, and the rest of the line is the description (both are optional).
  741. * There should be no space between the ">" and the first letter of the identifier. The sequence ends if another line starting with a ">" appears;
  742. * this indicates the start of another sequence.
  743. *
  744. * @return
  745. * Return a boolean. 1 if the sequence does not pass the format valifation stage and 0 otherwise.
  746. *
  747. */
  748. function validateFasta($sequence) {
  749. $fastaIdRegEx = '/^>.*(\\n|\\r)/';
  750. $fastaSeqRegEx = '/[^acgturykmswbdhvnxACGTURYKMSWBDHVNX\*\-\n\r]/';
  751. if ( preg_match($fastaSeqRegEx,$sequence) && !(preg_match($fastaIdRegEx,$sequence)) ) {
  752. $flag = 1;
  753. } else {
  754. $flag = 0;
  755. }
  756. return $flag;
  757. }
  758. /**
  759. * Fill the first dropdown list with appropriate options
  760. *
  761. * @return
  762. * An array consisting of matrices name for the first dropdown list
  763. */
  764. function _ajax_example_get_first_dropdown_options() {
  765. return drupal_map_assoc(array(
  766. t('PAM30'),
  767. t('PAM70'),
  768. t('PAM250'),
  769. t('BLOSUM80'),
  770. t('BLOSUM62'),
  771. t('BLOSUM45'),
  772. t('BLOSUM50'),
  773. t('BLOSUM90'),
  774. ));
  775. }
  776. /**
  777. * Fill the second dropdown list with appropriate options
  778. *
  779. * @return
  780. * An array containing open and extension gap values for the chosen matrix (to fill the second dropdown list)
  781. */
  782. function _ajax_example_get_second_dropdown_options($key = '') {
  783. $options = array(
  784. t('PAM30') => drupal_map_assoc(array(
  785. t('Existence: 7 Extension: 2'),
  786. t('Existence: 6 Extension: 2'),
  787. t('Existence: 5 Extension: 2'),
  788. t('Existence: 10 Extension: 1'),
  789. t('Existence: 9 Extension: 1'),
  790. t('Existence: 8 Extension: 1'),
  791. )),
  792. t('PAM70') => drupal_map_assoc(array(
  793. t('Existence: 8 Extension: 2'),
  794. t('Existence: 7 Extension: 2'),
  795. t('Existence: 6 Extension: 2'),
  796. t('Existence: 11 Extension: 1'),
  797. t('Existence: 10 Extension: 1'),
  798. t('Existence: 9 Extension: 1'),
  799. )),
  800. t('PAM250') => drupal_map_assoc(array(
  801. t('Existence: 15 Extension: 3'),
  802. t('Existence: 14 Extension: 3'),
  803. t('Existence: 13 Extension: 3'),
  804. t('Existence: 12 Extension: 3'),
  805. t('Existence: 11 Extension: 3'),
  806. t('Existence: 17 Extension: 2'),
  807. t('Existence: 16 Extension: 2'),
  808. t('Existence: 15 Extension: 2'),
  809. t('Existence: 14 Extension: 2'),
  810. t('Existence: 13 Extension: 2'),
  811. t('Existence: 21 Extension: 1'),
  812. t('Existence: 20 Extension: 1'),
  813. t('Existence: 19 Extension: 1'),
  814. t('Existence: 18 Extension: 1'),
  815. t('Existence: 17 Extension: 1'),
  816. )),
  817. t('BLOSUM80') => drupal_map_assoc(array(
  818. t('Existence: 8 Extension: 2'),
  819. t('Existence: 7 Extension: 2'),
  820. t('Existence: 6 Extension: 2'),
  821. t('Existence: 11 Extension: 1'),
  822. t('Existence: 10 Extension: 1'),
  823. t('Existence: 9 Extension: 1'),
  824. )),
  825. t('BLOSUM62') => drupal_map_assoc(array(
  826. t('Existence: 11 Extension: 2'),
  827. t('Existence: 10 Extension: 2'),
  828. t('Existence: 9 Extension: 2'),
  829. t('Existence: 8 Extension: 2'),
  830. t('Existence: 7 Extension: 2'),
  831. t('Existence: 6 Extension: 2'),
  832. t('Existence: 13 Extension: 1'),
  833. t('Existence: 12 Extension: 1'),
  834. t('Existence: 11 Extension: 1'),
  835. t('Existence: 10 Extension: 1'),
  836. t('Existence: 9 Extension: 1'),
  837. )),
  838. t('BLOSUM45') => drupal_map_assoc(array(
  839. t('Existence: 13 Extension: 3'),
  840. t('Existence: 12 Extension: 3'),
  841. t('Existence: 11 Extension: 3'),
  842. t('Existence: 10 Extension: 3'),
  843. t('Existence: 15 Extension: 2'),
  844. t('Existence: 14 Extension: 2'),
  845. t('Existence: 13 Extension: 2'),
  846. t('Existence: 12 Extension: 2'),
  847. t('Existence: 19 Extension: 1'),
  848. t('Existence: 18 Extension: 1'),
  849. t('Existence: 17 Extension: 1'),
  850. t('Existence: 16 Extension: 1'),
  851. )),
  852. t('BLOSUM50') => drupal_map_assoc(array(
  853. t('Existence: 13 Extension: 3'),
  854. t('Existence: 12 Extension: 3'),
  855. t('Existence: 11 Extension: 3'),
  856. t('Existence: 10 Extension: 3'),
  857. t('Existence: 9 Extension: 3'),
  858. t('Existence: 16 Extension: 2'),
  859. t('Existence: 15 Extension: 2'),
  860. t('Existence: 14 Extension: 2'),
  861. t('Existence: 13 Extension: 2'),
  862. t('Existence: 12 Extension: 2'),
  863. t('Existence: 19 Extension: 1'),
  864. t('Existence: 18 Extension: 1'),
  865. t('Existence: 17 Extension: 1'),
  866. t('Existence: 16 Extension: 1'),
  867. t('Existence: 15 Extension: 1'),
  868. )),
  869. t('BLOSUM90') => drupal_map_assoc(array(
  870. t('Existence: 9 Extension: 2'),
  871. t('Existence: 8 Extension: 2'),
  872. t('Existence: 7 Extension: 2'),
  873. t('Existence: 6 Extension: 2'),
  874. t('Existence: 11 Extension: 1'),
  875. t('Existence: 10 Extension: 1'),
  876. t('Existence: 9 Extension: 1'),
  877. )),
  878. );
  879. if (isset($options[$key])) {
  880. return $options[$key];
  881. } else {
  882. return array();
  883. }
  884. }
  885. /**
  886. * Respond to Ajax dropdown call
  887. */
  888. function ajax_example_dependent_dropdown_callback($form, $form_state) {
  889. return $form['ALG']['SParam']['gapCost'];
  890. }
  891. // call back function for example sequence
  892. function ajax_protein_text_area_callback($form, $form_state) {
  893. $element = $form['query']['FASTA']; // Get example Protein sequence
  894. $element['#value'] =
  895. '>gi|166477|gb|AAA96434.1| resveratrol synthase [Arachis hypogaea]
  896. MVSVSGIRKVQRAEGPATVLAIGTANPPNCIDQSTYADYYFRVTNSEHMTDLKKKFQRICERTQIKNRHM
  897. YLTEEILKENPNMCAYKAPSLDAREDMMIREVPRVGKEAATKAIKEWGQPMSKITHLIFCTTSGVALPGV
  898. DYELIVLLGLDPCVKRYMMYHQGCFAGGTVLRLAKDLAENNKDARVLIVCSENTAVTFRGPSETDMDSLV
  899. GQALFADGAAAIIIGSDPVPEVEKPIFELVSTDQKLVPGSHGAIGGLLREVGLTFYLNKSVPDIISQNIN
  900. DALNKAFDPLGISDYNSIFWIAHPGGRAILDQVEQKVNLKPEKMKATRDVLSNYGNMSSACVFFIMDLMR
  901. KRSLEEGLKTTGEGLDWGVLFGFGPGLTIETVVLRSVAI';
  902. return $element;
  903. }