tripal_chado.seq_extract.inc 17 KB

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