tripal_feature.seq_extract.inc 20 KB

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