tripal_chado.feature.api.inc 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. <?php
  2. /**
  3. * @file
  4. * Provides an application programming interface (API) for working with features
  5. */
  6. /**
  7. * @defgroup tripal_feature_api Feature API
  8. * @ingroup tripal_api
  9. * @{
  10. * Provides an application programming interface (API) for working with features
  11. * @}
  12. */
  13. /**
  14. * Performs a reverse compliment of a nucleotide sequence
  15. *
  16. * @param $sequence
  17. * The nucelotide sequence
  18. *
  19. * @return
  20. * an upper-case reverse complemented sequence
  21. *
  22. * @ingroup tripal_feature_api
  23. */
  24. function tripal_reverse_compliment_sequence($sequence) {
  25. $seq = strtoupper($sequence);
  26. $seq = strrev($seq);
  27. $seq = str_replace("A", "t", $seq);
  28. $seq = str_replace("T", "a", $seq);
  29. $seq = str_replace("G", "c", $seq);
  30. $seq = str_replace("C", "g", $seq);
  31. $seq = str_replace("Y", "r", $seq);
  32. $seq = str_replace("R", "y", $seq);
  33. $seq = str_replace("W", "w", $seq);
  34. $seq = str_replace("S", "s", $seq);
  35. $seq = str_replace("K", "m", $seq);
  36. $seq = str_replace("M", "k", $seq);
  37. $seq = str_replace("D", "h", $seq);
  38. $seq = str_replace("V", "b", $seq);
  39. $seq = str_replace("H", "d", $seq);
  40. $seq = str_replace("B", "v", $seq);
  41. return strtoupper($seq);
  42. }
  43. /**
  44. * Retrieves the sequences for a given feature.
  45. *
  46. * If a feature has multiple alignments or multiple relationships then
  47. * multiple sequences will be returned.
  48. *
  49. * @param $feature
  50. * An associative array describing the feature. Valid keys include:
  51. * - feature_id: The feature_id of the feature for which the sequence will
  52. * be retrieved
  53. * - name: The feature name. This will appear on the FASTA definition line
  54. * - parent_id: (optional) only retrieve a sequence if 'derive_from_parent'
  55. * is true and the parent matches this ID.
  56. * - featureloc_id: (optional) only retrieve a sequence if 'derive_from_parent' is
  57. * true and the alignment is defined with this featureloc_id
  58. * @param $options
  59. * An associative array of options. Valid keys include:
  60. * - width: Indicate the number of bases to use per line. A new line will
  61. * be added after the specified number of bases on each line.
  62. * - is_html: Set to '1' if the sequence is meant to be displayed on a web
  63. * page. This will cause a <br> tag to separate lines of the FASTA sequence.
  64. * - derive_from_parent: Set to '1' if the sequence should be obtained from
  65. * the parent to which this feature is aligned.
  66. * - aggregate: Set to '1' if the sequence should only contain sub features,
  67. * excluding intro sub feature sequence. For example, set this option to
  68. * obtain just the coding sequence of an mRNA.
  69. * - upstream: An integer specifing the number of upstream bases to include
  70. * in the output
  71. * - downstream: An integer specifying the number of downstream bases to
  72. * include in the output.
  73. * - sub_feature_types: Only include sub features (or child features) of
  74. * the types provided in the array
  75. * - relationship_type: If a relationship name is provided (e.g. sequence_of)
  76. * then any sequences that are in relationships of this type with matched
  77. * sequences are also included
  78. * - relationship_part: If a relationship is provided in the preceeding
  79. * argument then the rel_part must be either 'object' or 'subject' to
  80. * indicate which side of the relationship the matched features belong
  81. *
  82. * @return
  83. * an array of matching sequence in the following keys for each sequence:
  84. * 'types' => an array of feature types that were used to derive
  85. * the sequence (e.g. from an aggregated sequence)
  86. * 'upstream' => the number of upstream bases included in the sequence
  87. * 'downstream' => the number of downstream bases included in the
  88. * sequence
  89. * 'defline' => the definintion line used to create a FASTA sequence
  90. * 'residues' => the residues
  91. * 'featureloc_id' => the featureloc_id if the sequences is from an
  92. * alignment
  93. *
  94. * @ingroup tripal_feature_api
  95. */
  96. function tripal_get_feature_sequences($feature, $options) {
  97. // Default values for finding the feature.
  98. $feature_id = array_key_exists('feature_id', $feature) ? $feature['feature_id'] : 0;
  99. $parent_id = array_key_exists('parent_id', $feature) ? $feature['parent_id'] : 0;
  100. $featureloc_id = array_key_exists('featureloc_id', $feature) ? $feature['featureloc_id'] : 0;
  101. $feature_name = array_key_exists('name', $feature) ? $feature['name'] : '';
  102. // Default values for building the sequence.
  103. $num_bases_per_line = array_key_exists('width', $options) ? $options['width'] : 50;
  104. $derive_from_parent = array_key_exists('derive_from_parent', $options) ? $options['derive_from_parent'] : 0;
  105. $aggregate = array_key_exists('aggregate', $options) ? $options['aggregate'] : 0;
  106. $upstream = array_key_exists('upstream', $options) ? $options['upstream'] : 0;
  107. $downstream = array_key_exists('downstream', $options) ? $options['downstream'] : 0;
  108. $sub_features = array_key_exists('sub_feature_types', $options) ? $options['sub_feature_types'] : array();
  109. $relationship = array_key_exists('relationship_type', $options) ? $options['relationship_type'] : '';
  110. $rel_part = array_key_exists('relationship_part', $options) ? $options['relationship_part'] : '';
  111. $is_html = array_key_exists('is_html', $options) ? $options['is_html'] : 0;
  112. $is_txt = array_key_exists('is_txt', $options) ? $options['is_txt'] : 0;
  113. $is_raw = array_key_exists('is_raw', $options) ? $options['is_raw'] : 1;
  114. if (!$upstream) {
  115. $upstream = 0;
  116. }
  117. if (!$downstream) {
  118. $downstream = 0;
  119. }
  120. // Make sure the sub_features variable is an array.
  121. if (!is_array($sub_features)) {
  122. tripal_report_error('tripal_feature', TRIPAL_ERROR,
  123. "'sub_features' option must be an array for function tripal_get_feature_sequences().",
  124. array()
  125. );
  126. return array();
  127. }
  128. // If a relationship was specified then retreive and the sequences that
  129. // have the given relationship and the recurse to extract the appropriate
  130. // sequence.
  131. if ($rel_part == "object" or $rel_part == "subject") {
  132. if ($rel_part == "subject") {
  133. $sql = '
  134. SELECT FO.feature_id, FO.name, FO.uniquename, CVTO.name as feature_type, O.genus, O.species
  135. FROM {feature} FS
  136. INNER JOIN {feature_relationship} FR ON FR.subject_id = FS.feature_id
  137. INNER JOIN {cvterm} CVTFR ON CVTFR.cvterm_id = FR.type_id
  138. INNER JOIN {feature} FO ON FO.feature_id = FR.object_id
  139. INNER JOIN {cvterm} CVTO ON CVTO.cvterm_id = FO.type_id
  140. INNER JOIN {organism} O ON O.organism_id = FO.organism_id
  141. WHERE
  142. FS.feature_id = :feature_id AND
  143. CVTFR.name = :relationship
  144. ';
  145. $features = chado_query($sql, array(':feature_id' => $feature_id, ':relationship' => $relationship));
  146. }
  147. if ($rel_part == "object") {
  148. $sql = '
  149. SELECT FS.feature_id, FS.name, FS.uniquename, CVTO.name as feature_type, O.genus, O.species
  150. FROM {feature} FO
  151. INNER JOIN {feature_relationship} FR ON FR.object_id = FO.feature_id
  152. INNER JOIN {cvterm} CVTFR ON CVTFR.cvterm_id = FR.type_id
  153. INNER JOIN {feature} FS ON FS.feature_id = FR.subject_id
  154. INNER JOIN {cvterm} CVTO ON CVTO.cvterm_id = FS.type_id
  155. INNER JOIN {organism} O ON O.organism_id = FS.organism_id
  156. WHERE
  157. FO.feature_id = :feature_id AND
  158. CVTFR.name = :relationship
  159. ';
  160. $features = chado_query($sql, array(':feature_id' => $feature_id, ':relationship' => $relationship));
  161. }
  162. $sequences = '';
  163. while ($feature = $features->fetchObject()) {
  164. // Recurse and get the sequences for these in the relationship.
  165. if ($rel_part == "subject") {
  166. $defline = "$feature_name, $relationship, $feature->uniquename $feature->feature_type ($feature->genus $feature->species)";
  167. }
  168. if ($rel_part == "object") {
  169. $defline = "$feature->uniquename $feature->feature_type ($feature->genus $feature->species), $relationship, $feature_name";
  170. }
  171. return tripal_get_feature_sequences(
  172. array(
  173. 'feature_id' => $feature->feature_id,
  174. 'name' => $defline,
  175. 'parent_id' => $parent_id,
  176. ),
  177. array(
  178. 'width' => $num_bases_per_line,
  179. 'derive_from_parent' => $derive_from_parent,
  180. 'aggregate' => $aggregate,
  181. 'upstream' => $upstream,
  182. 'downstream' => $downstream,
  183. 'sub_features' => $sub_features,
  184. )
  185. );
  186. }
  187. }
  188. // Prepare the queries we're going to use later during the render phase
  189. // This SQL statement uses conditionals in the select clause to handle
  190. // cases cases where the alignment is in the reverse direction and when
  191. // the upstream and downstream extensions go beyond the lenght of the
  192. // parent sequence.
  193. $parent_sql ='
  194. SELECT featureloc_id, srcname, srcfeature_id, strand, srctypename, typename,
  195. fmin, fmax, upstream, downstream, adjfmin, adjfmax,
  196. substring(residues from (cast(adjfmin as int4) + 1) for cast((upstream + (fmax - fmin) + downstream) as int4)) as residues,
  197. genus, species
  198. FROM (
  199. SELECT
  200. FL.featureloc_id, OF.name srcname, FL.srcfeature_id, FL.strand,
  201. OCVT.name as srctypename, SCVT.name as typename,
  202. FL.fmin, FL.fmax, OO.genus, OO.species,
  203. CASE
  204. WHEN FL.strand >= 0 THEN
  205. CASE
  206. WHEN FL.fmin - :upstream <= 0 THEN 0
  207. ELSE FL.fmin - :upstream
  208. END
  209. WHEN FL.strand < 0 THEN
  210. CASE
  211. WHEN FL.fmin - :downstream <= 0 THEN 0
  212. ELSE FL.fmin - :downstream
  213. END
  214. END as adjfmin,
  215. CASE
  216. WHEN FL.strand >= 0 THEN
  217. CASE
  218. WHEN FL.fmax + :downstream > OF.seqlen THEN OF.seqlen
  219. ELSE FL.fmax + :downstream
  220. END
  221. WHEN FL.strand < 0 THEN
  222. CASE
  223. WHEN FL.fmax + :upstream > OF.seqlen THEN OF.seqlen
  224. ELSE FL.fmax + :upstream
  225. END
  226. END as adjfmax,
  227. CASE
  228. WHEN FL.strand >= 0 THEN
  229. CASE
  230. WHEN FL.fmin - :upstream <= 0 THEN FL.fmin
  231. ELSE :upstream
  232. END
  233. ELSE
  234. CASE
  235. WHEN FL.fmax + :upstream > OF.seqlen THEN OF.seqlen - FL.fmax
  236. ELSE :upstream
  237. END
  238. END as upstream,
  239. CASE
  240. WHEN FL.strand >= 0 THEN
  241. CASE
  242. WHEN FL.fmax + :downstream > OF.seqlen THEN OF.seqlen - FL.fmax
  243. ELSE :downstream
  244. END
  245. ELSE
  246. CASE
  247. WHEN FL.fmin - :downstream <= 0 THEN FL.fmin
  248. ELSE :downstream
  249. END
  250. END as downstream,
  251. OF.residues
  252. FROM {featureloc} FL
  253. INNER JOIN {feature} SF on FL.feature_id = SF.feature_id
  254. INNER JOIN {cvterm} SCVT on SF.type_id = SCVT.cvterm_id
  255. INNER JOIN {feature} OF on FL.srcfeature_id = OF.feature_id
  256. INNER JOIN {cvterm} OCVT on OF.type_id = OCVT.cvterm_id
  257. INNER JOIN {organism} OO on OF.organism_id = OO.organism_id
  258. WHERE SF.feature_id = :feature_id and NOT (OF.residues = \'\' or OF.residues IS NULL)) as tbl1
  259. ';
  260. // This query is meant to get all of the sub features of any given
  261. // feature (arg #1) and order them as they appear on the reference
  262. // feature (arg #2).
  263. $sfsql = '
  264. SELECT SF.feature_id, CVT.name as type_name, SF.type_id
  265. FROM {feature_relationship} FR
  266. INNER JOIN {feature} SF ON SF.feature_id = FR.subject_id
  267. INNER JOIN {cvterm} CVT ON CVT.cvterm_id = SF.type_id
  268. INNER JOIN {featureloc} FL ON FL.feature_id = FR.subject_id
  269. INNER JOIN {feature} PF ON PF.feature_id = FL.srcfeature_id
  270. WHERE FR.object_id = :feature_id and PF.feature_id = :srcfeature_id
  271. ORDER BY FL.fmin ASC
  272. ';
  273. // For counting the number of children.
  274. $fsql ='
  275. SELECT count(*) as num_children
  276. FROM {feature_relationship} FR
  277. INNER JOIN {feature} SF ON SF.feature_id = FR.subject_id
  278. INNER JOIN {cvterm} CVT ON CVT.cvterm_id = SF.type_id
  279. INNER JOIN {featureloc} FL ON FL.feature_id = FR.subject_id
  280. INNER JOIN {feature} PF ON PF.feature_id = FL.srcfeature_id
  281. WHERE FR.object_id = :feature_id and PF.feature_id = :srcfeature_id
  282. ';
  283. // The array to be returned.
  284. $sequences = array();
  285. // If we need to get the sequence from the parent then do so now.
  286. if ($derive_from_parent) {
  287. // Execute the query to get the sequence from the parent.
  288. $parents = chado_query($parent_sql, array(':upstream' => $upstream, ':downstream' => $downstream, ':feature_id' => $feature_id));
  289. while ($parent = $parents->fetchObject()) {
  290. // If the user specified a particular parent and this one doesn't
  291. // match then skip it.
  292. if ($parent_id and $parent_id != $parent->srcfeature_id) {
  293. continue;
  294. }
  295. // if the user specified a particular featureloc_id and this one
  296. // doesn't match then skip it.
  297. if ($featureloc_id and $featureloc_id != $parent->featureloc_id) {
  298. continue;
  299. }
  300. // Initialize the sequence for each parent.
  301. $seq = '';
  302. $notes = '';
  303. $types = array();
  304. // if we are to aggregate then we will ignore the feature returned
  305. // by the query above and rebuild it using the sub features
  306. if ($aggregate) {
  307. // now get the sub features that are located on the parent.
  308. $children = chado_query($sfsql, array(':feature_id' => $feature_id, ':srcfeature_id' => $parent->srcfeature_id));
  309. $num_children = chado_query($fsql, array(':feature_id' => $feature_id, ':srcfeature_id' => $parent->srcfeature_id))->fetchField();
  310. // Iterate through the sub features and concat their sequences. They
  311. // should already be in order.
  312. $i = 0;
  313. while ($child = $children->fetchObject()) {
  314. // If the callee has specified that only certain sub features should be
  315. // included then continue if this child is not one of those allowed
  316. // subfeatures.
  317. if (count($sub_features) > 0 and !in_array($child->type_name, $sub_features)) {
  318. $i++;
  319. continue;
  320. }
  321. // keep up with the types
  322. if (!in_array($child->type_name, $types)) {
  323. $types[] = $child->type_name;
  324. }
  325. // if the first sub feature we need to include the upstream bases. first check if
  326. // the feature is in the foward direction or the reverse.
  327. if ($i == 0 and $parent->strand >= 0) { // forward direction
  328. // -------------------------- ref
  329. // ....----> ---->
  330. // up 1 2
  331. $q = chado_query($parent_sql, array(':upstream' => $upstream, ':downstream' => 0, ':feature_id' => $child->feature_id));
  332. }
  333. elseif ($i == 0 and $parent->strand < 0) { // reverse direction
  334. // -------------------------- ref
  335. // ....<---- <----
  336. // down 1 2
  337. $q = chado_query($parent_sql, array(':upstream' => 0, ':downstream' => $downstream, ':feature_id' => $child->feature_id));
  338. }
  339. // Next, if the last sub feature we need to include the downstream bases. first check if
  340. // the feature is in teh forward direction or the reverse
  341. elseif ($i == $num_children - 1 and $parent->strand >= 0) { // forward direction
  342. // -------------------------- ref
  343. // ----> ---->....
  344. // 1 2 down
  345. $q = chado_query($parent_sql, array(':upstream' => 0, ':downstream' => $downstream, ':feature_id' => $child->feature_id));
  346. }
  347. elseif ($i == $num_children - 1 and $parent->strand < 0) { // reverse direction
  348. // -------------------------- ref
  349. // <---- <----....
  350. // 1 2 up
  351. $q = chado_query($parent_sql, array(':upstream' => $upstream, ':downstream' => 0, ':feature_id' => $child->feature_id));
  352. }
  353. // for internal sub features we don't want upstream or downstream bases
  354. else {
  355. $q = chado_query($parent_sql, array(':upstream' => 0, ':downstream' => 0, ':feature_id' => $child->feature_id));
  356. }
  357. while ($subseq = $q->fetchObject()) {
  358. // concatenate the sequences of all the sub features
  359. if ($subseq->srcfeature_id == $parent->srcfeature_id) {
  360. $seq .= $subseq->residues;
  361. }
  362. if ($subseq->upstream > 0 ) {
  363. $notes .= "Includes " . $subseq->upstream . " bases upstream. ";
  364. }
  365. if ($subseq->downstream > 0) {
  366. $notes .= "Includes " . $subseq->downstream . " bases downstream. ";
  367. }
  368. }
  369. $i++;
  370. }
  371. }
  372. // if this isn't an aggregate then use the parent residues
  373. else {
  374. $seq = $parent->residues;
  375. if ($parent->upstream > 0) {
  376. $notes .= "Includes " . $parent->upstream . " bases upstream. ";
  377. }
  378. if ($parent->downstream > 0) {
  379. $notes .= "Includes " . $parent->downstream . " bases downstream. ";
  380. }
  381. }
  382. // get the reverse compliment if feature is on the reverse strand
  383. $dir = 'forward';
  384. $length = strlen($seq);
  385. if ($parent->strand < 0) {
  386. $seq = tripal_reverse_compliment_sequence($seq);
  387. $dir = 'reverse';
  388. }
  389. // now format for display
  390. if ($is_html) {
  391. $seq = wordwrap($seq, $num_bases_per_line, "<br>", TRUE);
  392. }
  393. if ($is_txt) {
  394. $seq = wordwrap($seq, $num_bases_per_line, "\r\n", TRUE);
  395. }
  396. if (!$seq) {
  397. $notes .= "No sequence available.";
  398. }
  399. if (count($types) > 0) {
  400. $notes .= "Excludes all bases but those of type(s): " . implode(', ', $types) . ". " ;
  401. }
  402. // Construct the definition line for this feature. To construct the
  403. // defline we need a featureloc record, so we'll create one using
  404. // the information we have.
  405. $featureloc = new stdClass;
  406. $featureloc->feature_id = $feature;
  407. $featureloc->fmin = $parent->adjfmin;
  408. $featureloc->fmax = $parent->adjfmax;
  409. $featureloc->strand = $parent->strand;
  410. $featureloc->srcfeature_id = new stdClass;
  411. $featureloc->srcfeature_id->name = $parent->srcname;
  412. $featureloc->srcfeature_id->type_id = $parent->srctypename;
  413. $featureloc->srcfeature_id->organism_id = new stdClass;
  414. $featureloc->srcfeature_id->organism_id->genus = $parent->genus;
  415. $featureloc->srcfeature_id->organism_id->species = $parent->species;
  416. // Get a proper feature object.
  417. $f = chado_generate_var('feature', array('feature_id' => $feature_id));
  418. $defline = tripal_get_fasta_defline($f, $notes, $featureloc, '', $length);
  419. $sequences[] = array(
  420. 'types' => $types,
  421. 'upstream' => $parent->upstream,
  422. 'downstream' => $parent->downstream,
  423. 'defline' => $defline,
  424. 'residues' => $seq,
  425. 'featureloc_id' => $parent->featureloc_id,
  426. 'length' => $length,
  427. );
  428. }
  429. }
  430. // If we are not getting the sequence from the parent sequence then
  431. // use what comes through from the feature record.
  432. else {
  433. $f = chado_generate_var('feature', array('feature_id' => $feature_id));
  434. $f = chado_expand_var($f, 'field', 'feature.residues');
  435. $residues = $f->residues;
  436. $length = strlen($residues);
  437. if ($is_html) {
  438. $residues = wordwrap($residues, $num_bases_per_line, "<br>", TRUE);
  439. }
  440. else {
  441. $residues = wordwrap($residues, $num_bases_per_line, "\r\n", TRUE);
  442. }
  443. // get the definintion line for this feature
  444. $defline = tripal_get_fasta_defline($f, '', NULL, '', $length);
  445. // add to the sequence array
  446. $sequences[] = array(
  447. 'types' => $f->type_id->name,
  448. 'upstream' => 0,
  449. 'downstream' => 0,
  450. 'defline' => $defline,
  451. 'residues' => $residues,
  452. 'length' => $length,
  453. );
  454. }
  455. return $sequences;
  456. }
  457. /**
  458. *
  459. * @param $options
  460. * An associative array of options for selecting a feature. Valid keys include:
  461. * - org_commonname: The common name of the organism for which sequences
  462. * should be retrieved
  463. * - genus: The genus of the organism for which sequences should be retrieved
  464. * - species: The species of the organism for which sequences should be
  465. * retrieved
  466. * - analysis_name: The name of an analysis to which sequences belong. Only
  467. * those that are associated with the analysis will be retrieved.
  468. * - type: The type of feature (a sequence ontology term).
  469. * - feature_name: the name of the feature. Can be an array of feature names.
  470. * - feature_uname: the uniquename of the feature. Can be an array of
  471. * feature unique names.
  472. * - upstream: An integer specifing the number of upstream bases to include
  473. * in the output
  474. * - downstream: An integer specifying the number of downstream bases to
  475. * include in the output.
  476. * - derive_from_parent: Set to '1' if the sequence should be obtained from
  477. * the parent to which this feature is aligned.
  478. * - aggregate: Set to '1' if the sequence should only contain sub features,
  479. * excluding intro sub feature sequence. For example, set this option to
  480. * obtain just the coding sequence of an mRNA.
  481. * - sub_feature_types: Only include sub features (or child features) of
  482. * the types provided in the array
  483. * - relationship_type: If a relationship name is provided (e.g. sequence_of)
  484. * then any sequences that are in relationships of this type with matched
  485. * sequences are also included
  486. * - relationship_part: If a relationship is provided in the preceeding
  487. * argument then the rel_part must be either 'object' or 'subject' to
  488. * indicate which side of the relationship the matched features belong
  489. * - width: Indicate the number of bases to use per line. A new line will
  490. * be added after the specified number of bases on each line.
  491. * - is_html: Set to '1' if the sequence is meant to be displayed on a
  492. * web page. This will cause a <br> tag to separate lines of the FASTA
  493. * sequence.
  494. * @return
  495. * Returns an array of sequences. The sequences will be in an array with the
  496. * following keys for each sequence:
  497. * 'types' => an array of feature types that were used to derive
  498. * the sequence (e.g. from an aggregated sequence)
  499. * 'upstream' => the number of upstream bases in the sequence
  500. * 'downstream' => the number of downstream bases in the sequence
  501. * 'defline' => the definintion line used to create a FASTA sequence
  502. * 'residues' => the residues
  503. * 'featureloc_id' => the featureloc_id if from an alignment
  504. *
  505. * @ingroup tripal_feature_api
  506. */
  507. function tripal_get_bulk_feature_sequences($options) {
  508. // default values for building the sequence
  509. $org_commonname = array_key_exists('org_commonname', $options) ? $options['org_commonname'] : '';
  510. $genus = array_key_exists('genus', $options) ? $options['genus'] : '';
  511. $species = array_key_exists('species', $options) ? $options['species'] : '';
  512. $analysis_name = array_key_exists('analysis_name', $options) ? $options['analysis_name'] : '';
  513. $type = array_key_exists('type', $options) ? $options['type'] : '';
  514. $feature_name = array_key_exists('feature_name', $options) ? $options['feature_name'] : '';
  515. $feature_uname = array_key_exists('feature_uname', $options) ? $options['feature_uname'] : '';
  516. $derive_from_parent = array_key_exists('derive_from_parent', $options) ? $options['derive_from_parent'] : 0;
  517. $aggregate = array_key_exists('aggregate', $options) ? $options['aggregate'] : 0;
  518. $sub_features = array_key_exists('sub_feature_types', $options) ? $options['sub_feature_types'] : array();
  519. $relationship = array_key_exists('relationship_type', $options) ? $options['relationship_type'] : '';
  520. $rel_part = array_key_exists('relationship_part', $options) ? $options['relationship_part'] : '';
  521. $num_bases_per_line = array_key_exists('width', $options) ? $options['width'] : 50;
  522. $upstream = array_key_exists('upstream', $options) ? $options['upstream'] : 0;
  523. $downstream = array_key_exists('downstream', $options) ? $options['downstream'] : 0;
  524. if (!$type and !$feature_name and !$genus) {
  525. print "Please provide a type, feature name or genus\n";
  526. return;
  527. }
  528. // get the list of features
  529. $vars = array();
  530. $sql = "
  531. SELECT DISTINCT F.feature_id, F.name, F.uniquename,
  532. O.genus, O.species, CVT.name as feature_type
  533. FROM {feature} F
  534. INNER JOIN {organism} O on O.organism_id = F.organism_id
  535. INNER JOIN {cvterm} CVT on CVT.cvterm_id = F.type_id
  536. ";
  537. if ($analysis_name) {
  538. $sql .= "
  539. INNER JOIN {analysisfeature} AF on AF.feature_id = F.feature_id
  540. INNER JOIN {analysis} A on AF.analysis_id = A.analysis_id
  541. ";
  542. }
  543. $sql .= "WHERE (1=1) ";
  544. if ($org_commonname) {
  545. $sql .= "AND O.common_name = :common_name ";
  546. $vars[':common_name'] = $org_commonname;
  547. }
  548. if ($genus) {
  549. $sql .= "AND O.genus = :genus ";
  550. $vars[':genus'] = $genus;
  551. }
  552. if ($species) {
  553. $sql .= "AND O.species = :species ";
  554. $vars[':species'] = $species;
  555. }
  556. if ($type) {
  557. $sql .= "AND CVT.name = :cvtname ";
  558. $vars[':cvtname'] = $type;
  559. }
  560. if ($feature_name) {
  561. if (is_array($feature_name)) {
  562. $sql .= "AND F.name IN (";
  563. foreach ($feature_name as $i => $fname) {
  564. $sql .= ":fname$i, ";
  565. $vars[":fname$i"] = $fname;
  566. }
  567. // remove the trailing comma and close the paren
  568. $sql = substr($sql, 0, -2) . ")";
  569. }
  570. else {
  571. $sql .= "AND F.name = :fname";
  572. $vars[':fname'] = $feature_name;
  573. }
  574. }
  575. if ($feature_uname) {
  576. if (is_array($feature_uname)) {
  577. $sql .= "AND F.uniquename IN (";
  578. foreach ($feature_uname as $i => $funame) {
  579. $sql .= ":funame$i, ";
  580. $vars[":funame$i"] = $funame;
  581. }
  582. // remove the trailing comma and close the paren
  583. $sql = substr($sql, 0, -2) . ")";
  584. }
  585. else {
  586. $sql .= "AND F.uniquename = :funame";
  587. $vars[':funame'] = $feature_uname;
  588. }
  589. }
  590. if ($analysis_name) {
  591. $sql .= "AND A.name = :aname";
  592. $vars[':aname'] = $analysis_name;
  593. }
  594. $num_bases_per_line = 50;
  595. $num_seqs = 0;
  596. $q = chado_query($sql, $vars);
  597. $sequences = array();
  598. while ($feature = $q->fetchObject()) {
  599. // get the sequences
  600. $seqs = tripal_get_feature_sequences(array('feature_id' => $feature->feature_id), $options);
  601. $sequences = array_merge($sequences, $seqs);
  602. $num_seqs++;
  603. }
  604. return $sequences;
  605. }
  606. /**
  607. * Returns a definition line that can be used in a FASTA file
  608. *
  609. * @param $feature
  610. * A single feature object containing all the fields from the chado.feature table.
  611. * Best case is to provide an object generated by the chado_generate_var() function.
  612. * @param $notes
  613. * Optional: additional notes to be added to the definition line
  614. * @param $featureloc
  615. * Optional: a single featureloc object generated using chado_generate_var that
  616. * contains a record from the chado.featureloc table. Provide this if the
  617. * sequence was obtained by using the alignment rather than from the feature.residues
  618. * column
  619. * @param $type
  620. * Optional: the type of sequence. By default the feature type is used.
  621. * @param $length
  622. * Optional: the length of the sequence
  623. *
  624. * @return
  625. * A string of the format: uniquename|name|type|feature_id
  626. * or if an alignment: srcfeature_name:fmin..fmax[+-]; alignment of uniquename|name|type|feature_id
  627. */
  628. function tripal_get_fasta_defline($feature, $notes = '', $featureloc = NULL, $type = '', $length = 0) {
  629. // make sure the featureloc object has the srcfeature if not, then add it
  630. if ($featureloc) {
  631. if (!is_object($featureloc->srcfeature_id)) {
  632. $featureloc->srcfeature_id = chado_generate_var('feature', array('feature_id' => $featureloc->srcfeature_id));
  633. }
  634. if (!is_object($featureloc->srcfeature_id->organism_id)) {
  635. $featureloc->srcfeature_id->organism_id = chado_generate_var('organism', array('organism_id' => $featureloc->srcfeature_id->organism_id));
  636. }
  637. }
  638. // make sure the feature object has the organism if not, then add it
  639. if (!is_object($feature->organism_id)) {
  640. $feature->organism_id = chado_generate_var('organism', array('organism_id' => $feature->organism_id));
  641. }
  642. // if a type is not provided then use the default type
  643. if (!$type) {
  644. $type = $feature->type_id->name;
  645. }
  646. // construct the definition line
  647. $defline = $feature->uniquename . " " .
  648. 'ID=' . $feature->uniquename . "|" .
  649. 'Name=' . $feature->name . "|" .
  650. 'organism=' . $feature->organism_id->genus . " " . $feature->organism_id->species . "|" .
  651. 'type=' . $type . '|';
  652. if ($length > 0) {
  653. $defline .= "length=" . $length . "bp|";
  654. }
  655. if ($featureloc) {
  656. $defline .= "location=Sequence derived from alignment at " . tripal_get_location_string($featureloc);
  657. $defline .= " (" . $featureloc->srcfeature_id->organism_id->genus . " " . $featureloc->srcfeature_id->organism_id->species . ")|";
  658. }
  659. if ($notes) {
  660. $defline .= "Notes=$notes|";
  661. }
  662. $defline = substr($defline, 0, -1); // remove the trailing |
  663. return $defline;
  664. }
  665. /**
  666. * Returns a string representing a feature location in an alignment
  667. *
  668. * @param unknown $featureloc
  669. * A single featureloc object generated using chado_generate_var that
  670. * contains a record from the chado.featureloc table.
  671. */
  672. function tripal_get_location_string($featureloc) {
  673. $feature = $featureloc->feature_id;
  674. $strand = '';
  675. if ($featureloc->strand == 1) {
  676. $strand = '+';
  677. }
  678. elseif ($featureloc->strand == -1) {
  679. $strand = '-';
  680. }
  681. return $featureloc->srcfeature_id->name . ":" . ($featureloc->fmin + 1) . ".." . $featureloc->fmax . $strand;
  682. }
  683. /**
  684. * Quickly retrieves relationships for a feature.
  685. *
  686. * Using the chado_expand_var function to retrieve a set
  687. * of relationships can be very slow, especialy if there are many relationships
  688. * This function is intended to help speed up the retrieval of relationships
  689. * by only retrieving the base information for the relationship and returning
  690. * an array with
  691. *
  692. * @param $feature
  693. * The feature object
  694. * @return
  695. * An array with two objects
  696. *
  697. * @ingroup tripal_feature_api
  698. */
  699. function tripal_get_feature_relationships($feature) {
  700. // expand the feature object to include the feature relationships.
  701. $options = array(
  702. 'return_array' => 1,
  703. 'order_by' => array('rank' => 'ASC'),
  704. // we don't want to fully recurse we only need information about the
  705. // relationship type and the object and subject features (including feature type
  706. // and organism)
  707. 'include_fk' => array(
  708. 'type_id' => 1,
  709. 'object_id' => array(
  710. 'type_id' => 1,
  711. 'organism_id' => 1
  712. ),
  713. 'subject_id' => array(
  714. 'type_id' => 1,
  715. 'organism_id' => 1
  716. ),
  717. ),
  718. );
  719. $feature = chado_expand_var($feature, 'table', 'feature_relationship', $options);
  720. // get the subject relationships
  721. $srelationships = $feature->feature_relationship->subject_id;
  722. $orelationships = $feature->feature_relationship->object_id;
  723. // get alignment as child. The $feature->featureloc element
  724. // is already populated from the alignment preprocess function
  725. $options = array(
  726. 'return_array' => 1,
  727. 'include_fk' => array(
  728. 'srcfeature_id' => 1,
  729. 'feature_id' => 1,
  730. ),
  731. );
  732. $feature = chado_expand_var($feature, 'table', 'featureloc', $options);
  733. $cfeaturelocs = $feature->featureloc->feature_id;
  734. if (!$cfeaturelocs) {
  735. $cfeaturelocs = array();
  736. }
  737. elseif (!is_array($cfeaturelocs)) {
  738. $cfeaturelocs = array($cfeaturelocs);
  739. }
  740. // prepare the SQL statement to get the featureloc for the
  741. // feature in the relationships.
  742. $flrels_sql = "
  743. SELECT
  744. FL.featureloc_id, F.name as srcfeature_name, FL.srcfeature_id,
  745. FL.feature_id, FL.fmin, FL.fmax, FL.strand, FL.phase
  746. FROM {featureloc} FL
  747. INNER JOIN {feature} F ON F.feature_id = FL.srcfeature_id
  748. WHERE FL.feature_id = :feature_id and FL.srcfeature_id = :srcfeature_id
  749. ";
  750. // combine both object and subject relationshisp into a single array
  751. $relationships = array();
  752. $relationships['object'] = array();
  753. $relationships['subject'] = array();
  754. // iterate through the object relationships
  755. if ($orelationships) {
  756. foreach ($orelationships as $relationship) {
  757. $rel = new stdClass();
  758. // get locations where the child feature and this feature overlap with the
  759. // same landmark feature.
  760. $rel->child_featurelocs = array();
  761. foreach ($cfeaturelocs as $featureloc) {
  762. $res = chado_query($flrels_sql, array(':feature_id' => $relationship->subject_id->feature_id, ':srcfeature_id' => $featureloc->srcfeature_id->feature_id));
  763. while ($loc = $res->fetchObject()) {
  764. // add in the node id of the src feature if it exists and save this location
  765. if (property_exists($featureloc->srcfeature_id, 'nid')) {
  766. $loc->nid = $featureloc->srcfeature_id->nid;
  767. }
  768. $rel->child_featurelocs[] = $loc;
  769. }
  770. }
  771. $rel->record = $relationship;
  772. // get the relationship and child types
  773. $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
  774. $child_type = $relationship->subject_id->type_id->name;
  775. // get the node id of the subject
  776. // $sql = "SELECT nid FROM {chado_feature} WHERE feature_id = :feature_id";
  777. // $n = db_query($sql, array(':feature_id' => $relationship->subject_id->feature_id))->fetchObject();
  778. // if ($n) {
  779. // $rel->record->nid = $n->nid;
  780. // }
  781. if (!array_key_exists($rel_type, $relationships['object'])) {
  782. $relationships['object'][$rel_type] = array();
  783. }
  784. if (!array_key_exists($child_type, $relationships['object'][$rel_type])) {
  785. $relationships['object'][$rel_type][$child_type] = array();
  786. }
  787. $relationships['object'][$rel_type][$child_type][] = $rel;
  788. }
  789. }
  790. // now add in the subject relationships
  791. if ($srelationships) {
  792. foreach ($srelationships as $relationship) {
  793. $rel = new stdClass();
  794. // get locations where this feature overlaps with the parent
  795. $rel->parent_featurelocs = array();
  796. foreach ($cfeaturelocs as $featureloc) {
  797. $res = chado_query($flrels_sql, array(':feature_id' => $relationship->object_id->feature_id, ':srcfeature_id' => $featureloc->srcfeature_id->feature_id));
  798. while ($loc = $res->fetchObject()) {
  799. // add in the node id of the src feature if it exists and save this location
  800. if (property_exists($featureloc->srcfeature_id, 'nid')) {
  801. $loc->nid = $featureloc->srcfeature_id->nid;
  802. }
  803. $rel->parent_featurelocs[] = $loc;
  804. }
  805. }
  806. $rel->record = $relationship;
  807. $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
  808. $parent_type = $relationship->object_id->type_id->name;
  809. // // get the node id of the subject
  810. // $sql = "SELECT nid FROM {chado_feature} WHERE feature_id = :feature_id";
  811. // $n = db_query($sql, array(':feature_id' => $relationship->object_id->feature_id))->fetchObject();
  812. // if ($n) {
  813. // $rel->record->nid = $n->nid;
  814. // }
  815. if (!array_key_exists($rel_type, $relationships['subject'])) {
  816. $relationships['subject'][$rel_type] = array();
  817. }
  818. if (!array_key_exists($parent_type, $relationships['subject'][$rel_type])) {
  819. $relationships['subject'][$rel_type][$parent_type] = array();
  820. }
  821. $relationships['subject'][$rel_type][$parent_type][] = $rel;
  822. }
  823. }
  824. return $relationships;
  825. }