tripal_feature.seq_extract.inc 20 KB

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