seq_extract.inc 16 KB

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