seq_extract.inc 20 KB

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