tripal_chado.seq_extract.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. <?php
  2. /**
  3. * @file
  4. * Interface for downloading feature sequences
  5. */
  6. /**
  7. * The page allowing users to download feature sequences
  8. *
  9. * @ingroup tripal_chado
  10. */
  11. function tripal_chado_seq_extract_page() {
  12. if ($_SESSION['tripal_chado_seq_extract']['download']) {
  13. $genus = $_SESSION['tripal_chado_seq_extract']['genus'];
  14. $species = $_SESSION['tripal_chado_seq_extract']['species'];
  15. $analysis = $_SESSION['tripal_chado_seq_extract']['analysis'];
  16. $ftype = $_SESSION['tripal_chado_seq_extract']['ftype'];
  17. $fnames = $_SESSION['tripal_chado_seq_extract']['fnames'];
  18. $upstream = $_SESSION['tripal_chado_seq_extract']['upstream'];
  19. $downstream = $_SESSION['tripal_chado_seq_extract']['downstream'];
  20. $format = $_SESSION['tripal_chado_seq_extract']['format'];
  21. $use_parent = $_SESSION['tripal_chado_seq_extract']['use_parent'];
  22. $aggregate = $_SESSION['tripal_chado_seq_extract']['aggregate'];
  23. $agg_types = $_SESSION['tripal_chado_seq_extract']['agg_types'];
  24. unset($_SESSION['tripal_chado_seq_extract']['download']);
  25. if ($format == 'fasta_html') {
  26. drupal_add_http_header('Content-Type: text/html');
  27. }
  28. else {
  29. drupal_add_http_header('Content-Type: text');
  30. drupal_add_http_header('Content-Disposition: attachment; filename="sequences.fasta.txt"');
  31. }
  32. chado_get_bulk_feature_sequences(array(
  33. 'genus' => $genus,
  34. 'species' => $species,
  35. 'analysis_name' => $analysis,
  36. 'type' => $ftype,
  37. 'feature_name' => $fnames['items_array'],
  38. 'upstream' => $upstream,
  39. 'downstream' => $downstream,
  40. 'print' => 1,
  41. 'output_format' => $format,
  42. 'derive_from_parent' => $use_parent,
  43. 'aggregate' => $aggregate,
  44. 'sub_feature_types' => $agg_types
  45. ));
  46. return;
  47. }
  48. // generate the search form
  49. $output .= '';
  50. if (user_access('administer tripal')) {
  51. $output .= tripal_set_message("Administrators, the " .
  52. l('organism_feature_count', 'admin/tripal/storage/chado/mviews') . " and " .
  53. l('analysis_organism', 'admin/tripal/storage/chado/mviews') . " materialized
  54. views must be populated before using this form. Those views should be re-populated
  55. when new data is added.");
  56. }
  57. $output .= "<div id=\"tripal-feature-seq-extract-form-block\">";
  58. $output .= drupal_get_form('tripal_chado_seq_extract_form');
  59. $output .= "</div>";
  60. return $output;
  61. }
  62. /**
  63. * Theme the Form to choose which features to extract sequence for
  64. *
  65. * @ingroup tripal_chado
  66. */
  67. function theme_tripal_chado_seq_extract_form($form, &$form_state) {
  68. $rows = array(
  69. 0 => array(
  70. array('data' => drupal_render($form['description']), 'colspan' => 3),
  71. ),
  72. 1 => array(
  73. drupal_render($form['genus']),
  74. drupal_render($form['species']) ,
  75. drupal_render($form['ftype']),
  76. ),
  77. 2 => array(
  78. array('data' => drupal_render($form['analysis']), 'colspan' => 3),
  79. //drupal_render($form['format']),
  80. ),
  81. 3 => array(
  82. array('data' => drupal_render($form['fnames']), 'colspan' => 2),
  83. drupal_render($form['upstream']) . drupal_render($form['downstream']) ,
  84. ),
  85. 4 => array(
  86. array(
  87. 'data' => drupal_render($form['advanced']),
  88. 'colspan' => 3,
  89. ),
  90. ),
  91. 5 => array(
  92. array(
  93. 'data' => drupal_render($form['retrieve_btn']) . drupal_render($form['reset_btn']),
  94. 'colspan' => 3,
  95. ),
  96. ),
  97. );
  98. $headers = array();
  99. $table = theme('table', $headers, $rows, array('id' => 'tripal-feature-seq-extract-form-table', 'border' => '0'));
  100. $markup .= $table;
  101. $form['criteria'] = array(
  102. '#markup' => $markup,
  103. '#weight' => -10,
  104. );
  105. return drupal_render($form);
  106. }
  107. /**
  108. * Form to choose which features to extract sequence for
  109. *
  110. * @ingroup tripal_chado
  111. */
  112. function tripal_chado_seq_extract_form(&$form_state = NULL) {
  113. tripal_core_ahah_init_form();
  114. // we want to allow the query string to provide values for the form
  115. if ($_GET['fnames']) {
  116. $form_state['values']['fnames']['items'] = $_GET['fnames'];
  117. }
  118. if ($_GET['genus']) {
  119. $form_state['values']['genus'] = $_GET['genus'];
  120. }
  121. if ($_GET['species']) {
  122. $form_state['values']['species'] = $_GET['species'];
  123. }
  124. if ($_GET['ftype']) {
  125. $form_state['values']['ftype'] = $_GET['ftype'];
  126. }
  127. if ($_GET['analysis']) {
  128. $form_state['values']['analysis'] = $_GET['analysis'];
  129. }
  130. if ($_GET['upstream']) {
  131. $form_state['values']['upstream'] = $_GET['upstream'];
  132. }
  133. if ($_GET['downstream']) {
  134. $form_state['values']['downstream'] = $_GET['downstream'];
  135. }
  136. if ($_GET['use_parent']) {
  137. $form_state['values']['use_parent'] = $_GET['use_parent'];
  138. }
  139. if ($_GET['aggregate']) {
  140. $form_state['values']['aggregate'] = $_GET['aggregate'];
  141. }
  142. if ($_GET['agg_types']) {
  143. $form_state['values']['agg_types'] = $_GET['agg_types'];
  144. }
  145. // get defaults
  146. $dgenus = isset($form_state['values']['genus']) ? $form_state['values']['genus'] : $_SESSION['tripal_chado_seq_extract']['genus'];
  147. $dspecies = isset($form_state['values']['species']) ? $form_state['values']['species'] : $_SESSION['tripal_chado_seq_extract']['species'];
  148. $danalysis = isset($form_state['values']['analysis']) ? $form_state['values']['analysis'] : $_SESSION['tripal_chado_seq_extract']['analysis'];
  149. $dftype = isset($form_state['values']['ftype']) ? $form_state['values']['ftype'] : $_SESSION['tripal_chado_seq_extract']['ftype'];
  150. $dfnames = isset($form_state['values']['fnames']) ? $form_state['values']['fnames'] : $_SESSION['tripal_chado_seq_extract']['fnames'];
  151. $dupstream = isset($form_state['values']['upstream']) ? $form_state['values']['upstream'] : $_SESSION['tripal_chado_seq_extract']['upstream'];
  152. $ddownstream = isset($form_state['values']['downstream']) ? $form_state['values']['downstream'] : $_SESSION['tripal_chado_seq_extract']['downstream'];
  153. $dformat = isset($form_state['values']['format']) ? $form_state['values']['format'] : 'fasta_txt';
  154. $duse_parent = isset($form_state['values']['use_parent']) ? $form_state['values']['use_parent'] : $_SESSION['tripal_chado_seq_extract']['use_parent'];
  155. $daggregate = isset($form_state['values']['aggregate']) ? $form_state['values']['aggregate'] : $_SESSION['tripal_chado_seq_extract']['aggregate'];
  156. $dagg_types = isset($form_state['values']['agg_types']) ? $form_state['values']['agg_types'] : $_SESSION['tripal_chado_seq_extract']['agg_types'];
  157. $form = array();
  158. // because we're using Tripal's file_upload_combo form element we
  159. // need to allow the form to upload files
  160. $form['#attributes']['enctype'] = 'multipart/form-data';
  161. $form['#method'] = 'POST';
  162. $form['description'] = array(
  163. '#markup' => t('Use this form to retrieve sequences in FASTA format.')
  164. );
  165. $sql = "
  166. SELECT DISTINCT genus
  167. FROM {organism}
  168. ORDER BY genus
  169. ";
  170. $results = chado_query($sql);
  171. $genus = array();
  172. $genus[] = '';
  173. while ($organism = $results->fetchObject()) {
  174. $genus[$organism->genus] = $organism->genus;
  175. }
  176. $form['genus'] = array(
  177. '#title' => t('Genus'),
  178. '#type' => 'select',
  179. '#options' => $genus,
  180. '#default_value' => $dgenus,
  181. '#multiple' => FALSE,
  182. '#description' => t('The organism\'s genus. If specified, features for all organism with this genus will be retrieved.'),
  183. '#ahah' => array(
  184. 'path' => 'find/sequences/ajax',
  185. 'wrapper' => 'tripal-feature-seq-extract-form-block',
  186. 'event' => 'change',
  187. 'method' => 'replace',
  188. ),
  189. );
  190. $species = array();
  191. $species[] = '';
  192. if ($dgenus) {
  193. $sql = "
  194. SELECT DISTINCT species
  195. FROM {organism}
  196. WHERE genus = :genus
  197. ORDER BY species
  198. ";
  199. $results = chado_query($sql, array(':genus' => $dgenus));
  200. while ($organism = $results->fetchObject()) {
  201. $species[$organism->species] = $organism->species;
  202. }
  203. }
  204. $form['species'] = array(
  205. '#title' => t('Species'),
  206. '#type' => 'select',
  207. '#options' => $species,
  208. '#default_value' => $dspecies,
  209. '#multiple' => FALSE,
  210. '#description' => t('The organism\'s species name. If specified, features for all organisms with this species will be retrieved. Please first select a genus'),
  211. '#ahah' => array(
  212. 'path' => 'find/sequences/ajax',
  213. 'wrapper' => 'tripal-feature-seq-extract-form-block',
  214. 'event' => 'change',
  215. 'method' => 'replace',
  216. ),
  217. );
  218. $analyses = array();
  219. $analyses[] = '';
  220. if ($dgenus) {
  221. $sql = "
  222. SELECT DISTINCT A.analysis_id, A.name
  223. FROM {analysis_organism} AO
  224. INNER JOIN {analysis} A ON A.analysis_id = AO.analysis_id
  225. INNER JOIN {organism} O ON O.organism_id = AO.organism_id
  226. WHERE O.genus = :genus
  227. ";
  228. $args = array();
  229. $args[':genus'] = $dgenus;
  230. if ($dspecies) {
  231. $sql .= " AND O.species = :species ";
  232. $args[':species'] = $dspecies;
  233. }
  234. $sql .=" ORDER BY A.name ";
  235. $results = chado_query($sql, $args);
  236. while ($analysis = $results->fetchObject()) {
  237. $analyses[$analysis->name] = $analysis->name;
  238. }
  239. }
  240. $form['analysis'] = array(
  241. '#title' => t('Analyses'),
  242. '#type' => 'select',
  243. '#options' => $analyses,
  244. '#default_value' => $danalysis,
  245. '#multiple' => FALSE,
  246. '#description' => t('You can limit sequences by the analyses to which it was derived or was used. If specified, only features associated with the specific analysis will be retrieved.'),
  247. );
  248. $ftype = array();
  249. $ftype[] = '';
  250. if ($dgenus) {
  251. $sql = "
  252. SELECT DISTINCT OFC.cvterm_id, OFC.feature_type
  253. FROM {organism_feature_count} OFC
  254. WHERE OFC.genus = :genus
  255. ";
  256. $args = array();
  257. $args['genus'] = $dgenus;
  258. if ($dspecies) {
  259. $sql .= " AND OFC.species = :species";
  260. $args['species'] = $dspecies;
  261. }
  262. $sql .= " ORDER BY OFC.feature_type ";
  263. $results = chado_query($sql, $args);
  264. while ($type = $results->fetchObject()) {
  265. $ftype[$type->feature_type] = $type->feature_type;
  266. }
  267. }
  268. $form['ftype'] = array(
  269. '#title' => t('Feature Type'),
  270. '#type' => 'select',
  271. '#options' => $ftype,
  272. '#multiple' => FALSE,
  273. '#default_value' => $dftype,
  274. '#description' => t('The type of feature to retrieve (e.g. mRNA). All features that match this type will be retrieved.'),
  275. );
  276. $form['fnames'] = array(
  277. '#title' => t('Feature Name'),
  278. '#type' => 'file_upload_combo',
  279. '#default_value' => $dfnames,
  280. '#description' => t('The names of the features to retrieve. Separate each with a new line or comma. Leave blank to retrieve all features matching other criteria.'),
  281. '#rows' => 8
  282. );
  283. $form['upstream'] = array(
  284. '#title' => t('Upstream Bases'),
  285. '#type' => 'textfield',
  286. '#description' => t('A numeric value specifying the number of upstream bases to include. Only works if the feature is aligned to a larger sequence.'),
  287. '#default_value' => $dupstream,
  288. '#size' => 5,
  289. );
  290. $form['downstream'] = array(
  291. '#title' => t('Downstream Bases'),
  292. '#type' => 'textfield',
  293. '#description' => t('A numeric value specifying the number of downstream bases to incldue. Only works if the feature is aligned to a larger sequence.'),
  294. '#default_value' => $ddownstream,
  295. '#size' => 5,
  296. );
  297. $form['format'] = array(
  298. '#title' => t('Output Format'),
  299. '#type' => 'hidden',
  300. '#default_value' => $dformat,
  301. '#options' => array(
  302. 'fasta_html' => 'FASTA (in browser)',
  303. 'fasta_txt' => 'FASTA (download)',
  304. ),
  305. );
  306. $form['advanced'] = array(
  307. '#type' => 'fieldset',
  308. '#title' => 'Advanced',
  309. '#collapsible' => TRUE,
  310. '#collapsed' => TRUE
  311. );
  312. $form['advanced']['use_parent'] = array(
  313. '#title' => t('Use Parent'),
  314. '#type' => 'checkbox',
  315. '#default_value' => $duse_parent,
  316. '#description' => t('Check this box to retrieve the sequence from the parent in an alignment rather than the feature itself. This is useful if the same feature is aligned to multiple parents and you would like to retrieve the underlying sequence from each parent.'),
  317. );
  318. $form['advanced']['aggregate'] = array(
  319. '#title' => t('Aggregate'),
  320. '#type' => 'checkbox',
  321. '#default_value' => $daggregate,
  322. '#description' => t('Check this box to aggregate sub features into a single sequence. This is useful, for example, for obtaining CDS sequence from an mRNA. Rather than retrieve the mRNA sequence, the sub features of the mRNA will be aggregated and that will be returned.')
  323. );
  324. $form['advanced']['agg_types'] = array(
  325. '#title' => t('Types to aggregate'),
  326. '#type' => 'textarea',
  327. '#default_value' => $dagg_types,
  328. '#description' => t('Set this argument to the type of children to aggregate. This is useful in the case where a gene has exons, CDSs and UTRs. In this case, you may only want to aggregate CDSs and exclude exons. If you want to aggregate both CDSs and UTRs you could specify both.')
  329. );
  330. $form['retrieve_btn'] = array(
  331. '#type' => 'submit',
  332. '#value' => 'Retrieve',
  333. );
  334. $form['reset_btn'] = array(
  335. '#type' => 'submit',
  336. '#value' => 'Reset',
  337. );
  338. return $form;
  339. }
  340. /**
  341. * AJAX callback
  342. *
  343. * @ingroup tripal_chado
  344. */
  345. function tripal_chado_seq_extract_form_ahah_update() {
  346. $status = TRUE;
  347. // prepare and render the form. If no form is returned that means
  348. // we got here by an AHAH call after the form has been submitted. This
  349. // is possible because results are downloaded and the page is not refreshed
  350. // but the form_id goes away. So, we need to rebuild the form in this case.
  351. // otherwise, if the form already exists we just theme it.
  352. $form = tripal_core_ahah_prepare_form();
  353. if ($form) {
  354. $data = theme('tripal_chado_seq_extract_form', $form);
  355. }
  356. else {
  357. $data = drupal_get_form('tripal_chado_seq_extract_form');
  358. }
  359. // bind javascript events to the new objects that will be returned
  360. // so that AHAH enabled elements will work.
  361. $settings = tripal_core_ahah_bind_events();
  362. // return the updated JSON
  363. drupal_json(
  364. array(
  365. 'status' => $status,
  366. 'data' => $data,
  367. 'settings' => $settings,
  368. )
  369. );
  370. }
  371. /**
  372. * Validate the extract sequence form
  373. *
  374. * @ingroup tripal_chado
  375. */
  376. function tripal_chado_seq_extract_form_validate($form, &$form_state) {
  377. $genus = $form_state['values']['genus'];
  378. $species = $form_state['values']['species'];
  379. $analysis = $form_state['values']['analysis'];
  380. $ftype = $form_state['values']['ftype'];
  381. $fnames = $form_state['values']['fnames'];
  382. $upstream = $form_state['values']['upstream'];
  383. $downstream = $form_state['values']['downstream'];
  384. $format = $form_state['values']['format'];
  385. $use_parent = $form_state['values']['use_parent'];
  386. $aggregate = $form_state['values']['aggregate'];
  387. $agg_types = $form_state['values']['agg_types'];
  388. $op = $form_state['values']['op'];
  389. if ($op == 'Retrieve') {
  390. if ($upstream and !preg_match('/^\d+$/', $upstream)) {
  391. form_set_error('upstream', 'Please enter a positive numeric value for the upstream bases');
  392. }
  393. if ($downstream and !preg_match('/^\d+$/', $downstream)) {
  394. form_set_error('downstream', 'Please enter a positive numeric value for the downstream bases');
  395. }
  396. if (!$genus and !$species and !$ftype and !$fnames) {
  397. form_set_error('', 'Please provide a feature name, a feature type or a genus.');
  398. }
  399. }
  400. }
  401. /**
  402. * Submit the extract sequence form
  403. *
  404. * @ingroup tripal_chado
  405. */
  406. function tripal_chado_seq_extract_form_submit($form, &$form_state) {
  407. $genus = $form_state['values']['genus'];
  408. $species = $form_state['values']['species'];
  409. $analysis = $form_state['values']['analysis'];
  410. $ftype = $form_state['values']['ftype'];
  411. $fnames = $form_state['values']['fnames'];
  412. $upstream = $form_state['values']['upstream'];
  413. $downstream = $form_state['values']['downstream'];
  414. $format = $form_state['values']['format'];
  415. $use_parent = $form_state['values']['use_parent'];
  416. $aggregate = $form_state['values']['aggregate'];
  417. $agg_types = $form_state['values']['agg_types'];
  418. $op = $form_state['values']['op'];
  419. // we must use the parent sequence if the user has selected
  420. // the upstream, downstream or to aggregate
  421. if ($upstream or $downstream or $aggregate) {
  422. $use_parent = 1;
  423. }
  424. if ($op == 'Retrieve') {
  425. $_SESSION['tripal_chado_seq_extract']['genus'] = $genus;
  426. $_SESSION['tripal_chado_seq_extract']['species'] = $species;
  427. $_SESSION['tripal_chado_seq_extract']['analysis'] = $analysis;
  428. $_SESSION['tripal_chado_seq_extract']['ftype'] = $ftype;
  429. $_SESSION['tripal_chado_seq_extract']['fnames'] = $fnames;
  430. $_SESSION['tripal_chado_seq_extract']['upstream'] = $upstream;
  431. $_SESSION['tripal_chado_seq_extract']['downstream'] = $downstream;
  432. $_SESSION['tripal_chado_seq_extract']['format'] = $format;
  433. $_SESSION['tripal_chado_seq_extract']['use_parent'] = $use_parent;
  434. $_SESSION['tripal_chado_seq_extract']['aggregate'] = $aggregate;
  435. $_SESSION['tripal_chado_seq_extract']['agg_types'] = $agg_types;
  436. $_SESSION['tripal_chado_seq_extract']['download'] = 1;
  437. }
  438. if ($op == 'Reset') {
  439. unset($_SESSION['tripal_chado_seq_extract']);
  440. }
  441. }