tripal_feature.seq_extract.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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_feature
  10. */
  11. function tripal_feature_seq_extract_page() {
  12. if ($_SESSION['tripal_feature_seq_extract']['download']) {
  13. $genus = $_SESSION['tripal_feature_seq_extract']['genus'];
  14. $species = $_SESSION['tripal_feature_seq_extract']['species'];
  15. $analysis = $_SESSION['tripal_feature_seq_extract']['analysis'];
  16. $ftype = $_SESSION['tripal_feature_seq_extract']['ftype'];
  17. $fnames = $_SESSION['tripal_feature_seq_extract']['fnames'];
  18. $upstream = $_SESSION['tripal_feature_seq_extract']['upstream'];
  19. $downstream = $_SESSION['tripal_feature_seq_extract']['downstream'];
  20. $format = $_SESSION['tripal_feature_seq_extract']['format'];
  21. $use_parent = $_SESSION['tripal_feature_seq_extract']['use_parent'];
  22. $aggregate = $_SESSION['tripal_feature_seq_extract']['aggregate'];
  23. $agg_types = $_SESSION['tripal_feature_seq_extract']['agg_types'];
  24. unset($_SESSION['tripal_feature_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. tripal_feature_seq_extract_get_features(NULL, $genus, $species, $analysis,
  33. $ftype, $fnames['items_array'], $upstream, $downstream, $format, $use_parent, $aggregate,
  34. $agg_types);
  35. return;
  36. }
  37. // generate the search form
  38. $output .= '';
  39. if (user_access('access administration pages')) {
  40. $output .= tripal_set_message("Administrators, the " .
  41. l('organism_feature_count', 'admin/tripal/schema/mviews') . " and " .
  42. l('analysis_organism', 'admin/tripal/schema/mviews') . " materialized
  43. views must be populated before using this form. Those views should be re-populated
  44. when new data is added.");
  45. }
  46. $output .= "<div id=\"tripal-feature-seq-extract-form-block\">";
  47. $output .= drupal_get_form('tripal_feature_seq_extract_form');
  48. $output .= "</div>";
  49. return $output;
  50. }
  51. /**
  52. * Theme the Form to choose which features to extract sequence for
  53. *
  54. * @ingroup tripal_feature
  55. */
  56. function theme_tripal_feature_seq_extract_form($form) {
  57. $rows = array(
  58. 0 => array(
  59. array('data' => drupal_render($form['description']), 'colspan' => 3),
  60. ),
  61. 1 => array(
  62. drupal_render($form['genus']),
  63. drupal_render($form['species']) ,
  64. drupal_render($form['ftype']),
  65. ),
  66. 2 => array(
  67. array('data' => drupal_render($form['analysis']), 'colspan' => 3),
  68. //drupal_render($form['format']),
  69. ),
  70. 3 => array(
  71. array('data' => drupal_render($form['fnames']), 'colspan' => 2),
  72. drupal_render($form['upstream']) . drupal_render($form['downstream']) ,
  73. ),
  74. 4 => array(
  75. array(
  76. 'data' => drupal_render($form['advanced']),
  77. 'colspan' => 3,
  78. ),
  79. ),
  80. 5 => array(
  81. array(
  82. 'data' => drupal_render($form['retrieve_btn']) . drupal_render($form['reset_btn']),
  83. 'colspan' => 3,
  84. ),
  85. ),
  86. );
  87. $headers = array();
  88. $table = theme('table', $headers, $rows, array('id' => 'tripal-feature-seq-extract-form-table', 'border' => '0'));
  89. $markup .= $table;
  90. $form['criteria'] = array(
  91. '#type' => 'markup',
  92. '#value' => $markup,
  93. '#weight' => -10,
  94. );
  95. return drupal_render($form);
  96. }
  97. /**
  98. * Form to choose which features to extract sequence for
  99. *
  100. * @ingroup tripal_feature
  101. */
  102. function tripal_feature_seq_extract_form(&$form_state = NULL) {
  103. tripal_core_ahah_init_form();
  104. // we want to allow the query string to provide values for the form
  105. if ($_GET['fnames']) {
  106. $form_state['values']['fnames']['items'] = $_GET['fnames'];
  107. }
  108. if ($_GET['genus']) {
  109. $form_state['values']['genus'] = $_GET['genus'];
  110. }
  111. if ($_GET['species']) {
  112. $form_state['values']['species'] = $_GET['species'];
  113. }
  114. if ($_GET['ftype']) {
  115. $form_state['values']['ftype'] = $_GET['ftype'];
  116. }
  117. if ($_GET['analysis']) {
  118. $form_state['values']['analysis'] = $_GET['analysis'];
  119. }
  120. if ($_GET['upstream']) {
  121. $form_state['values']['upstream'] = $_GET['upstream'];
  122. }
  123. if ($_GET['downstream']) {
  124. $form_state['values']['downstream'] = $_GET['downstream'];
  125. }
  126. if ($_GET['use_parent']) {
  127. $form_state['values']['use_parent'] = $_GET['use_parent'];
  128. }
  129. if ($_GET['aggregate']) {
  130. $form_state['values']['aggregate'] = $_GET['aggregate'];
  131. }
  132. if ($_GET['agg_types']) {
  133. $form_state['values']['agg_types'] = $_GET['agg_types'];
  134. }
  135. // get defaults
  136. $dgenus = isset($form_state['values']['genus']) ? $form_state['values']['genus'] : $_SESSION['tripal_feature_seq_extract']['genus'];
  137. $dspecies = isset($form_state['values']['species']) ? $form_state['values']['species'] : $_SESSION['tripal_feature_seq_extract']['species'];
  138. $danalysis = isset($form_state['values']['analysis']) ? $form_state['values']['analysis'] : $_SESSION['tripal_feature_seq_extract']['analysis'];
  139. $dftype = isset($form_state['values']['ftype']) ? $form_state['values']['ftype'] : $_SESSION['tripal_feature_seq_extract']['ftype'];
  140. $dfnames = isset($form_state['values']['fnames']) ? $form_state['values']['fnames'] : $_SESSION['tripal_feature_seq_extract']['fnames'];
  141. $dupstream = isset($form_state['values']['upstream']) ? $form_state['values']['upstream'] : $_SESSION['tripal_feature_seq_extract']['upstream'];
  142. $ddownstream = isset($form_state['values']['downstream']) ? $form_state['values']['downstream'] : $_SESSION['tripal_feature_seq_extract']['downstream'];
  143. $dformat = isset($form_state['values']['format']) ? $form_state['values']['format'] : 'fasta_txt';
  144. $duse_parent = isset($form_state['values']['use_parent']) ? $form_state['values']['use_parent'] : $_SESSION['tripal_feature_seq_extract']['use_parent'];
  145. $daggregate = isset($form_state['values']['aggregate']) ? $form_state['values']['aggregate'] : $_SESSION['tripal_feature_seq_extract']['aggregate'];
  146. $dagg_types = isset($form_state['values']['agg_types']) ? $form_state['values']['agg_types'] : $_SESSION['tripal_feature_seq_extract']['agg_types'];
  147. $form = array();
  148. // because we're using Tripal's file_upload_combo form element we
  149. // need to allow the form to upload files
  150. $form['#attributes']['enctype'] = 'multipart/form-data';
  151. $form['#method'] = 'POST';
  152. $form['description'] = array(
  153. '#type' => 'markup',
  154. '#value' => t('Use this form to retrieve sequences in FASTA format.')
  155. );
  156. $sql = "
  157. SELECT DISTINCT genus
  158. FROM {organism}
  159. ORDER BY genus
  160. ";
  161. $results = chado_query($sql);
  162. $genus = array();
  163. $genus[] = '';
  164. while ($organism = $results->fetchObject()) {
  165. $genus[$organism->genus] = $organism->genus;
  166. }
  167. $form['genus'] = array(
  168. '#title' => t('Genus'),
  169. '#type' => 'select',
  170. '#options' => $genus,
  171. '#default_value' => $dgenus,
  172. '#multiple' => FALSE,
  173. '#description' => t('The organism\'s genus. If specified, features for all organism with this genus will be retrieved.'),
  174. '#ahah' => array(
  175. 'path' => 'find/sequences/ajax',
  176. 'wrapper' => 'tripal-feature-seq-extract-form-block',
  177. 'event' => 'change',
  178. 'method' => 'replace',
  179. ),
  180. );
  181. $species = array();
  182. $species[] = '';
  183. if ($dgenus) {
  184. $sql = "
  185. SELECT DISTINCT species
  186. FROM {organism}
  187. WHERE genus = :genus
  188. ORDER BY species
  189. ";
  190. $results = chado_query($sql, array(':genus' => $dgenus));
  191. while ($organism = $results->fetchObject()) {
  192. $species[$organism->species] = $organism->species;
  193. }
  194. }
  195. $form['species'] = array(
  196. '#title' => t('Species'),
  197. '#type' => 'select',
  198. '#options' => $species,
  199. '#default_value' => $dspecies,
  200. '#multiple' => FALSE,
  201. '#description' => t('The organism\'s species name. If specified, features for all organisms with this species will be retrieved. Please first select a genus'),
  202. '#ahah' => array(
  203. 'path' => 'find/sequences/ajax',
  204. 'wrapper' => 'tripal-feature-seq-extract-form-block',
  205. 'event' => 'change',
  206. 'method' => 'replace',
  207. ),
  208. );
  209. $analyses = array();
  210. $analyses[] = '';
  211. if ($dgenus) {
  212. $sql = "
  213. SELECT DISTINCT A.analysis_id, A.name
  214. FROM {analysis_organism} AO
  215. INNER JOIN {analysis} A ON A.analysis_id = AO.analysis_id
  216. INNER JOIN {organism} O ON O.organism_id = AO.organism_id
  217. WHERE O.genus = :genus
  218. ";
  219. $args = array();
  220. $args[':genus'] = $dgenus;
  221. if ($dspecies) {
  222. $sql .= " AND O.species = :species ";
  223. $args[':species'] = $dspecies;
  224. }
  225. $sql .=" ORDER BY A.name ";
  226. $results = chado_query($sql, $args);
  227. while ($analysis = $results->fetchObject()) {
  228. $analyses[$analysis->name] = $analysis->name;
  229. }
  230. }
  231. $form['analysis'] = array(
  232. '#title' => t('Analyses'),
  233. '#type' => 'select',
  234. '#options' => $analyses,
  235. '#default_value' => $danalysis,
  236. '#multiple' => FALSE,
  237. '#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.'),
  238. );
  239. $ftype = array();
  240. $ftype[] = '';
  241. if ($dgenus) {
  242. $sql = "
  243. SELECT DISTINCT OFC.cvterm_id, OFC.feature_type
  244. FROM {organism_feature_count} OFC
  245. WHERE OFC.genus = :genus
  246. ";
  247. $args = array();
  248. $args['genus'] = $dgenus;
  249. if ($dspecies) {
  250. $sql .= " AND OFC.species = :species";
  251. $args['species'] = $dspecies;
  252. }
  253. $sql .= " ORDER BY OFC.feature_type ";
  254. $results = chado_query($sql, $args);
  255. while ($type = $results->fetchObject()) {
  256. $ftype[$type->feature_type] = $type->feature_type;
  257. }
  258. }
  259. $form['ftype'] = array(
  260. '#title' => t('Feature Type'),
  261. '#type' => 'select',
  262. '#options' => $ftype,
  263. '#multiple' => FALSE,
  264. '#default_value' => $dftype,
  265. '#description' => t('The type of feature to retrieve (e.g. mRNA). All features that match this type will be retrieved.'),
  266. );
  267. $form['fnames'] = array(
  268. '#title' => t('Feature Name'),
  269. '#type' => 'file_upload_combo',
  270. '#default_value' => $dfnames,
  271. '#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.'),
  272. '#rows' => 8
  273. );
  274. $form['upstream'] = array(
  275. '#title' => t('Upstream Bases'),
  276. '#type' => 'textfield',
  277. '#description' => t('A numeric value specifying the number of upstream bases to include. Only works if the feature is aligned to a larger sequence.'),
  278. '#default_value' => $dupstream,
  279. '#size' => 5,
  280. );
  281. $form['downstream'] = array(
  282. '#title' => t('Downstream Bases'),
  283. '#type' => 'textfield',
  284. '#description' => t('A numeric value specifying the number of downstream bases to incldue. Only works if the feature is aligned to a larger sequence.'),
  285. '#default_value' => $ddownstream,
  286. '#size' => 5,
  287. );
  288. $form['format'] = array(
  289. '#title' => t('Output Format'),
  290. '#type' => 'hidden',
  291. '#default_value' => $dformat,
  292. '#options' => array(
  293. 'fasta_html' => 'FASTA (in browser)',
  294. 'fasta_txt' => 'FASTA (download)',
  295. ),
  296. );
  297. $form['advanced'] = array(
  298. '#type' => 'fieldset',
  299. '#title' => 'Advanced',
  300. '#collapsible' => TRUE,
  301. '#collapsed' => TRUE
  302. );
  303. $form['advanced']['use_parent'] = array(
  304. '#title' => t('Use Parent'),
  305. '#type' => 'checkbox',
  306. '#default_value' => $duse_parent,
  307. '#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.'),
  308. );
  309. $form['advanced']['aggregate'] = array(
  310. '#title' => t('Aggregate'),
  311. '#type' => 'checkbox',
  312. '#default_value' => $daggregate,
  313. '#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.')
  314. );
  315. $form['advanced']['agg_types'] = array(
  316. '#title' => t('Types to aggregate'),
  317. '#type' => 'textarea',
  318. '#default_value' => $dagg_types,
  319. '#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.')
  320. );
  321. $form['retrieve_btn'] = array(
  322. '#type' => 'submit',
  323. '#value' => 'Retrieve',
  324. );
  325. $form['reset_btn'] = array(
  326. '#type' => 'submit',
  327. '#value' => 'Reset',
  328. );
  329. return $form;
  330. }
  331. /**
  332. * AJAX callback
  333. *
  334. * @ingroup tripal_feature
  335. */
  336. function tripal_feature_seq_extract_form_ahah_update() {
  337. $status = TRUE;
  338. // prepare and render the form. If no form is returned that means
  339. // we got here by an AHAH call after the form has been submitted. This
  340. // is possible because results are downloaded and the page is not refreshed
  341. // but the form_id goes away. So, we need to rebuild the form in this case.
  342. // otherwise, if the form already exists we just theme it.
  343. $form = tripal_core_ahah_prepare_form();
  344. if ($form) {
  345. $data = theme('tripal_feature_seq_extract_form', $form);
  346. }
  347. else {
  348. $data = drupal_get_form('tripal_feature_seq_extract_form');
  349. }
  350. // bind javascript events to the new objects that will be returned
  351. // so that AHAH enabled elements will work.
  352. $settings = tripal_core_ahah_bind_events();
  353. // return the updated JSON
  354. drupal_json(
  355. array(
  356. 'status' => $status,
  357. 'data' => $data,
  358. 'settings' => $settings,
  359. )
  360. );
  361. }
  362. /**
  363. * Validate the extract sequence form
  364. *
  365. * @ingroup tripal_feature
  366. */
  367. function tripal_feature_seq_extract_form_validate($form, &$form_state) {
  368. $genus = $form_state['values']['genus'];
  369. $species = $form_state['values']['species'];
  370. $analysis = $form_state['values']['analysis'];
  371. $ftype = $form_state['values']['ftype'];
  372. $fnames = $form_state['values']['fnames'];
  373. $upstream = $form_state['values']['upstream'];
  374. $downstream = $form_state['values']['downstream'];
  375. $format = $form_state['values']['format'];
  376. $use_parent = $form_state['values']['use_parent'];
  377. $aggregate = $form_state['values']['aggregate'];
  378. $agg_types = $form_state['values']['agg_types'];
  379. $op = $form_state['values']['op'];
  380. if ($op == 'Retrieve') {
  381. if ($upstream and !preg_match('/^\d+$/', $upstream)) {
  382. form_set_error('upstream', 'Please enter a positive numeric value for the upstream bases');
  383. }
  384. if ($downstream and !preg_match('/^\d+$/', $downstream)) {
  385. form_set_error('downstream', 'Please enter a positive numeric value for the downstream bases');
  386. }
  387. if (!$genus and !$species and !$ftype and !$fnames) {
  388. form_set_error('', 'Please provide a feature name, a feature type or a genus.');
  389. }
  390. }
  391. }
  392. /**
  393. * Submit the extract sequence form
  394. *
  395. * @ingroup tripal_feature
  396. */
  397. function tripal_feature_seq_extract_form_submit($form, &$form_state) {
  398. $genus = $form_state['values']['genus'];
  399. $species = $form_state['values']['species'];
  400. $analysis = $form_state['values']['analysis'];
  401. $ftype = $form_state['values']['ftype'];
  402. $fnames = $form_state['values']['fnames'];
  403. $upstream = $form_state['values']['upstream'];
  404. $downstream = $form_state['values']['downstream'];
  405. $format = $form_state['values']['format'];
  406. $use_parent = $form_state['values']['use_parent'];
  407. $aggregate = $form_state['values']['aggregate'];
  408. $agg_types = $form_state['values']['agg_types'];
  409. $op = $form_state['values']['op'];
  410. // we must use the parent sequence if the user has selected
  411. // the upstream, downstream or to aggregate
  412. if ($upstream or $downstream or $aggregate) {
  413. $use_parent = 1;
  414. }
  415. if ($op == 'Retrieve') {
  416. $_SESSION['tripal_feature_seq_extract']['genus'] = $genus;
  417. $_SESSION['tripal_feature_seq_extract']['species'] = $species;
  418. $_SESSION['tripal_feature_seq_extract']['analysis'] = $analysis;
  419. $_SESSION['tripal_feature_seq_extract']['ftype'] = $ftype;
  420. $_SESSION['tripal_feature_seq_extract']['fnames'] = $fnames;
  421. $_SESSION['tripal_feature_seq_extract']['upstream'] = $upstream;
  422. $_SESSION['tripal_feature_seq_extract']['downstream'] = $downstream;
  423. $_SESSION['tripal_feature_seq_extract']['format'] = $format;
  424. $_SESSION['tripal_feature_seq_extract']['use_parent'] = $use_parent;
  425. $_SESSION['tripal_feature_seq_extract']['aggregate'] = $aggregate;
  426. $_SESSION['tripal_feature_seq_extract']['agg_types'] = $agg_types;
  427. $_SESSION['tripal_feature_seq_extract']['download'] = 1;
  428. }
  429. if ($op == 'Reset') {
  430. unset($_SESSION['tripal_feature_seq_extract']);
  431. }
  432. }
  433. /**
  434. * Actually extract the sequences
  435. *
  436. * @ingroup tripal_feature
  437. */
  438. function tripal_feature_seq_extract_get_features($org_commonname, $genus, $species, $analysis_name,
  439. $type, $feature_name, $upstream, $downstream, $output_format, $derive_from_parent, $aggregate,
  440. $child, $relationship, $rel_part) {
  441. $sub_features = explode(',', $child);
  442. if (!$output_format) {
  443. $output_format = 'fasta_txt';
  444. }
  445. if (!$type and !$feature_name and !$genus) {
  446. print "Please provide a type, feature name or genus\n";
  447. return;
  448. }
  449. // get the list of features
  450. $vars = array();
  451. $sql = "SELECT DISTINCT F.feature_id, F.name, F.uniquename, O.genus, O.species, CVT.name as feature_type " .
  452. "FROM {feature} F " .
  453. " INNER JOIN {organism} O on O.organism_id = F.organism_id " .
  454. " INNER JOIN {cvterm} CVT on CVT.cvterm_id = F.type_id ";
  455. if ($analysis_name) {
  456. $sql .= " INNER JOIN {analysisfeature} AF on AF.feature_id = F.feature_id " .
  457. " INNER JOIN {analysis} A on AF.analysis_id = A.analysis_id ";
  458. }
  459. $sql .= "WHERE (1=1) ";
  460. if ($org_commonname) {
  461. $sql .= "AND O.common_name = :common_name ";
  462. $vars[':common_name'] = $org_commonname;
  463. }
  464. if ($genus) {
  465. $sql .= "AND O.genus = :genus ";
  466. $vars[':genus'] = $genus;
  467. }
  468. if ($species) {
  469. $sql .= "AND O.species = :species ";
  470. $vars[':species'] = $species;
  471. }
  472. if ($type) {
  473. $sql .= "AND CVT.name = ':cvtname ";
  474. $vars[':cvtname'] = $type;
  475. }
  476. if ($feature_name) {
  477. if (is_array($feature_name)) {
  478. $sql .= "AND F.name IN (";
  479. foreach ($feature_name as $i => $fname) {
  480. $sql .= ":fname$i, ";
  481. $vars[":fname$i"] = $fname;
  482. }
  483. // remove the trailing comma and close the paren
  484. $sql = substr($sql, 0, -2) . ")";
  485. }
  486. else {
  487. $sql .= "AND F.name = :fname";
  488. $vars[':fname'] = $feature_name;
  489. }
  490. }
  491. if ($analysis_name) {
  492. $sql .= "AND A.name = :aname";
  493. $vars[':aname'] = $analysis_name;
  494. }
  495. $num_bases_per_line = 50;
  496. $num_seqs = 0;
  497. $q = chado_query($sql, $vars);
  498. while ($feature = $q->fetchObject()) {
  499. $feature_id = $feature->feature_id;
  500. // build the header for each FASTA entry
  501. if ($feature->uniquename == $feature->name) {
  502. $feature_name = "$feature->uniquename $feature->feature_type ($feature->genus $feature->species)";
  503. }
  504. else {
  505. $feature_name = "$feature->uniquename $feature->name $feature->feature_type ($feature->genus $feature->species)";
  506. }
  507. // generate the sequence
  508. $sequence = tripal_feature_get_formatted_sequence($feature_id, $feature_name,
  509. $num_bases_per_line, $derive_from_parent, $aggregate, $output_format,
  510. $upstream, $downstream, $sub_features, $relationship, $rel_part);
  511. // print the sequence
  512. print $sequence;
  513. $num_seqs++;
  514. }
  515. if ($num_seqs == 0) {
  516. print "No Sequences Found";
  517. }
  518. }