tripal_feature.api.inc 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. /**
  7. * Retrieve properties from the analysisfeatureprop table for a feature.
  8. *
  9. * @param $analysis_id
  10. * The analysis ID for the analysis feature. This argument is optional but
  11. * if specified it must also be accompanied with a feature ID.
  12. * @param $feature_id
  13. * The feature ID for the analysis feature. This argument is optional but
  14. * if specified it must also be accompanied with an analysis ID.
  15. * @param $analysisfeature_id
  16. * The analysis feature ID for the analysis feature. This argument is
  17. * optional and can be used rather than specifying the $analysis_id and
  18. * $feature_id arguments. If all three arguments are specified (e.g.
  19. * an $analysis_id, $feature_id and $analysisfeature_id, then the
  20. * $analysisfeature_id is used and the other two arguments are ignored.
  21. * @param $property
  22. * The cvterm name of the properties to retrieve
  23. * @param $cv_name
  24. * Optional. The name of the cv to which the property belongs. By
  25. * default this is the 'tripal' cv.
  26. *
  27. * @return
  28. * An analysisfeature chado variable with the specified properties expanded
  29. *
  30. * @ingroup tripal_feature_api
  31. */
  32. function tripal_feature_analysis_get_property($analysis_id = NULL, $feature_id = NUll,
  33. $analysisfeature_id = NULL, $property, $cv_name = 'tripal') {
  34. // check that the incoming arguments are correct
  35. if (($analysis_id and !$feature_id) or
  36. (!$analysis_id and $feature_id)) {
  37. watchdog('tripal_feature',
  38. 'tripal_feature_analysis_get_property: Both an analysis ID and feature ID should be specified',
  39. array(), WATCHDOG_WARNING);
  40. }
  41. // get the analysisfeature_id if one is not provided
  42. if (!$analysisfeature_id) {
  43. $columns = array('analysisfeature_id');
  44. $values = array('analysis_id' => $analysis_id, 'feature_id' => $feature_id);
  45. $result = tripal_core_chado_select('analysisfeature', $columns, $values);
  46. $analysisfeature_id = $result[0]->analysisfeature_id;
  47. }
  48. // get the property.
  49. return tripal_core_get_property('analysisfeature', $analysisfeature_id, $property, $cv_name);
  50. }
  51. /**
  52. * Insert a property for an analysis feature
  53. *
  54. * @param $analysis_id
  55. * The analysis ID for the analysis feature. This argument is optional but
  56. * if specified it must also be accompanied with a feature ID.
  57. * @param $feature_id
  58. * The feature ID for the analysis feature. This argument is optional but
  59. * if specified it must also be accompanied with an analysis ID.
  60. * @param $analysisfeature_id
  61. * The analysis feature ID for the analysis feature. This argument is
  62. * optional and can be used rather than specifying the $analysis_id and
  63. * $feature_id arguments. If all three arguments are specified (e.g.
  64. * an $analysis_id, $feature_id and $analysisfeature_id, then the
  65. * $analysisfeature_id is used and the other two arguments are ignored.
  66. * @param $property
  67. * The cvterm name of the property to insert
  68. * @param $value
  69. * The value of the property to insert
  70. * @param $update_if_present
  71. * A boolean indicated whether to update the record if it's already present
  72. * @param $cv_name
  73. * Optional. The name of the cv to which the property belongs. By
  74. * default this is the 'tripal' cv.
  75. *
  76. * @return
  77. * True of success, False otherwise
  78. *
  79. * @ingroup tripal_feature_api
  80. */
  81. function tripal_feature_analysis_insert_property($analysis_id = NULL, $feature_id = NUll,
  82. $analysisfeature_id = NULL, $property, $value, $update_if_present = 0,
  83. $cv_name = 'tripal') {
  84. // check that the incoming arguments are correct
  85. if (($analysis_id and !$feature_id) or
  86. (!$analysis_id and $feature_id)) {
  87. watchdog('tripal_feature',
  88. 'tripal_feature_analysis_insert_property: Both an analysis ID and feature ID should be specified',
  89. array(), WATCHDOG_WARNING);
  90. }
  91. // get the analysisfeature_id if one is not provided
  92. if (!$analysisfeature_id) {
  93. $columns = array('analysisfeature_id');
  94. $values = array('analysis_id' => $analysis_id, 'feature_id' => $feature_id);
  95. $result = tripal_core_chado_select('analysisfeature', $columns, $values);
  96. $analysisfeature_id = $result[0]->analysisfeature_id;
  97. }
  98. // insert the property.
  99. return tripal_core_insert_property('analysisfeature', $analysisfeature_id,
  100. $property, $cv_name, $value, $update_if_present);
  101. }
  102. /**
  103. * Update an analysis feature property using the property name. Use this
  104. * when a property only exists once for a given analysis feature. When more
  105. * than one value can exist for the same property use the
  106. * tripal_feature_analysis_update_property_by_id() function.
  107. *
  108. * @param $analysis_id
  109. * The analysis ID for the analysis feature. This argument is optional but
  110. * if specified it must also be accompanied with a feature ID.
  111. * @param $feature_id
  112. * The feature ID for the analysis feature. This argument is optional but
  113. * if specified it must also be accompanied with an analysis ID.
  114. * @param $analysisfeature_id
  115. * The analysis feature ID for the analysis feature. This argument is
  116. * optional and can be used rather than specifying the $analysis_id and
  117. * $feature_id arguments. If all three arguments are specified (e.g.
  118. * an $analysis_id, $feature_id and $analysisfeature_id, then the
  119. * $analysisfeature_id is used and the other two arguments are ignored.
  120. * @param $property
  121. * The cvterm name of the property to update
  122. * @param $value
  123. * The value of the property to update
  124. * @param $insert_if_missing
  125. * A boolean indicated whether to insert the record if it's absent
  126. * @param $cv_name
  127. * Optional. The name of the cv to which the property belongs. By
  128. * default this is the 'tripal' cv.
  129. *
  130. * Note: The property will be identified using the unique combination of the $analysis_id and $property
  131. * and then it will be updated with the supplied value
  132. *
  133. * @return
  134. * True of success, False otherwise
  135. *
  136. * @ingroup tripal_feature_api
  137. */
  138. function tripal_feature_analysis_update_property($analysis_id = NULL, $feature_id = NUll,
  139. $analysisfeature_id = NULL, $property, $value, $insert_if_missing = 0,
  140. $cv_name = 'tripal') {
  141. // check that the incoming arguments are correct
  142. if (($analysis_id and !$feature_id) or
  143. (!$analysis_id and $feature_id)) {
  144. watchdog('tripal_feature',
  145. 'tripal_feature_analysis_update_property: Both an analysis ID and feature ID should be specified',
  146. array(), WATCHDOG_WARNING);
  147. }
  148. // get the analysisfeature_id if one is not provided
  149. if (!$analysisfeature_id) {
  150. $columns = array('analysisfeature_id');
  151. $values = array('analysis_id' => $analysis_id, 'feature_id' => $feature_id);
  152. $result = tripal_core_chado_select('analysisfeature', $columns, $values);
  153. $analysisfeature_id = $result[0]->analysisfeature_id;
  154. }
  155. // update the property.
  156. return tripal_core_update_property('analysisfeature', $analysisfeature_id, $property, $cv_name, $value, $insert_if_missing);
  157. }
  158. /**
  159. * Update a property for an analysis feature using the analysisfeatureprop_id.
  160. *
  161. * @param $analysisfeatureprop_id
  162. * The analysis feature property ID for the analysis feature.
  163. * @param $property
  164. * The cvterm name of the property
  165. * @param $value
  166. * The value of the property
  167. * @param $cv_name
  168. * Optional. The name of the cv to which the property belongs. By
  169. * default this is the 'tripal' cv.
  170. * *
  171. * @return
  172. * True of success, False otherwise
  173. *
  174. * @ingroup tripal_feature_api
  175. */
  176. function tripal_feature_analysis_update_property_by_id($analysisfeatureprop_id,
  177. $property, $value, $cv_name = 'tripal') {
  178. // update the property.
  179. return tripal_core_update_property_by_id('analysisfeature',
  180. $analysisfeatureprop_id, $property, $cv_name, $value);
  181. }
  182. /**
  183. * Delete an analysis feature property using the property name. Use this
  184. * when a property only exists once for a given analysis feature. When more
  185. * than one value can exist for the same property use the
  186. * tripal_feature_analysis_delete_property_by_id() function.
  187. *
  188. * @param $analysis_id
  189. * The analysis ID for the analysis feature. This argument is optional but
  190. * if specified it must also be accompanied with a feature ID.
  191. * @param $feature_id
  192. * The feature ID for the analysis feature. This argument is optional but
  193. * if specified it must also be accompanied with an analysis ID.
  194. * @param $analysisfeature_id
  195. * The analysis feature ID for the analysis feature. This argument is
  196. * optional and can be used rather than specifying the $analysis_id and
  197. * $feature_id arguments. If all three arguments are specified (e.g.
  198. * an $analysis_id, $feature_id and $analysisfeature_id, then the
  199. * $analysisfeature_id is used and the other two arguments are ignored.
  200. * @param $property
  201. * The cvterm name of the property to delete
  202. * @param $cv_name
  203. * Optional. The name of the cv to which the property belongs. By
  204. * default this is the 'tripal' cv.
  205. *
  206. * Note: The property will be identified using the unique combination of the $analysis_id and $property
  207. * and then it will be deleted
  208. *
  209. * @return
  210. * True of success, False otherwise
  211. *
  212. * @ingroup tripal_feature_api
  213. */
  214. function tripal_feature_analysis_delete_property($analysis_id = NULL, $feature_id = NUll,
  215. $analysisfeature_id = NULL, $property, $cv_name = 'tripal') {
  216. // check that the incoming arguments are correct
  217. if (($analysis_id and !$feature_id) or
  218. (!$analysis_id and $feature_id)) {
  219. watchdog('tripal_feature',
  220. 'tripal_feature_analysis_delete_property: Both an analysis ID and feature ID should be specified',
  221. array(), WATCHDOG_WARNING);
  222. }
  223. // get the analysisfeature_id if one is not provided
  224. if (!$analysisfeature_id) {
  225. $columns = array('analysisfeature_id');
  226. $values = array('analysis_id' => $analysis_id, 'feature_id' => $feature_id);
  227. $result = tripal_core_chado_select('analysisfeature', $columns, $values);
  228. $analysisfeature_id = $result[0]->analysisfeature_id;
  229. }
  230. // get the property.
  231. return tripal_core_delete_property('analysisfeature', $analysisfeature_id, $property, $cv_name);
  232. }
  233. /**
  234. * Delete a property using the analysisfeatureprop_id
  235. *
  236. * @param $analysisfeatureprop_id
  237. * The analysis feature property ID for the analysis feature.
  238. *
  239. * @return
  240. * True of success, False otherwise
  241. *
  242. * @ingroup tripal_feature_api
  243. */
  244. function tripal_feature_analysis_delete_property_by_id($analysisfeatureprop_id) {
  245. // get the property.
  246. return tripal_core_delete_property_by_id('analysisfeature', $analysisfeatureprop_id);
  247. }
  248. /**
  249. * Retrieve properties of a given type for a given feature
  250. *
  251. * @param $feature_id
  252. * The feature_id of the properties you would like to retrieve
  253. * @param $property
  254. * The cvterm name of the properties to retrieve
  255. * @param $cv_name
  256. * Optional. The name of the cv to which the property belongs. By
  257. * default this is the 'tripal' cv.
  258. *
  259. * @return
  260. * A feature chado variable with the specified properties expanded
  261. *
  262. * @ingroup tripal_feature_api
  263. */
  264. function tripal_feature_get_property($feature_id, $property, $cv_name='tripal') {
  265. return tripal_core_get_property('feature', $feature_id, $property, $cv_name);
  266. }
  267. /**
  268. * Insert a given property
  269. *
  270. * @param $feature_id
  271. * The feature_id of the property to insert
  272. * @param $property
  273. * The cvterm name of the property to insert
  274. * @param $value
  275. * The value of the property to insert
  276. * @param $update_if_present
  277. * A boolean indicated whether to update the record if it's already present
  278. * @param $cv_name
  279. * Optional. The name of the cv to which the property belongs. By
  280. * default this is the 'tripal' cv.
  281. *
  282. * @return
  283. * True of success, False otherwise
  284. *
  285. * @ingroup tripal_feature_api
  286. */
  287. function tripal_feature_insert_property($feature_id, $property, $value,
  288. $update_if_present = 0, $cv_name = 'tripal') {
  289. return tripal_core_insert_property('feature', $feature_id, $property,
  290. $cv_name, $value, $update_if_present);
  291. }
  292. /**
  293. * Update a feature property using the property name. Only use this
  294. * if the property is unique and only exist once for the feature.
  295. *
  296. * @param $feature_id
  297. * The feature_id of the property to update
  298. * @param $property
  299. * The cvterm name of the property to update
  300. * @param $value
  301. * The value of the property to update
  302. * @param $insert_if_missing
  303. * A boolean indicated whether to insert the record if it's absent
  304. * @param $cv_name
  305. * Optional. The name of the cv to which the property belongs. By
  306. * default this is the 'tripal' cv.
  307. *
  308. * Note: The property will be identified using the unique combination of the $feature_id and $property
  309. * and then it will be updated with the supplied value
  310. *
  311. * @return
  312. * True of success, False otherwise
  313. *
  314. * @ingroup tripal_feature_api
  315. */
  316. function tripal_feature_update_property($feature_id, $property,
  317. $value, $insert_if_missing = 0, $cv_name = 'tripal') {
  318. return tripal_core_update_property('feature', $feature_id, $property, $cv_name, $value, $insert_if_missing);
  319. }
  320. /**
  321. * Update a given feature property using the featureprop_id
  322. *
  323. * @param $featureprop_id
  324. * The featureprop_id of the property to update
  325. * @param $property
  326. * The cvterm name of the property
  327. * @param $value
  328. * The value of the property
  329. * @param $cv_name
  330. * Optional. The name of the cv to which the property belongs. By
  331. * default this is the 'tripal' cv.
  332. *
  333. * @return
  334. * True of success, False otherwise
  335. *
  336. * @ingroup tripal_feature_api
  337. */
  338. function tripal_feature_update_property_by_id($featureprop_id, $property,
  339. $value, $cv_name = 'tripal') {
  340. return tripal_core_update_property_by_id('feature', $featureprop_id, $property, $cv_name, $value);
  341. }
  342. /**
  343. * Delete a given feature property using the property name. Only use this
  344. * if the property is unique and only exists once for the feature.
  345. *
  346. * @param $feature_id
  347. * The feature_id of the property to delete
  348. * @param $property
  349. * The cvterm name of the property to delete
  350. * @param $cv_name
  351. * Optional. The name of the cv to which the property belongs. By
  352. * default this is the 'tripal' cv.
  353. *
  354. * Note: The property will be identified using the unique combination of the $feature_id and $property
  355. * and then it will be deleted
  356. *
  357. * @return
  358. * True of success, False otherwise
  359. *
  360. * @ingroup tripal_feature_api
  361. */
  362. function tripal_feature_delete_property($feature_id, $property, $cv_name='tripal') {
  363. return tripal_core_delete_property('feature', $feature_id, $property, $cv_name);
  364. }
  365. /**
  366. * Delete a given feature property using the featureprop_id
  367. *
  368. * @param $featureprop_id
  369. * The feature_id of the property to delete
  370. *
  371. * @return
  372. * True of success, False otherwise
  373. *
  374. * @ingroup tripal_feature_api
  375. */
  376. function tripal_feature_delete_property_by_id($featureprop_id) {
  377. return tripal_core_delete_property_by_id('feature', $featureprop_id);
  378. }
  379. /**
  380. * Performs a reverse compliment of a nucleotide sequence
  381. *
  382. * @param $sequence
  383. * The nucelotide sequence
  384. *
  385. * @return
  386. * an upper-case reverse complemented sequence
  387. *
  388. * @ingroup tripal_feature_api
  389. */
  390. function tripal_feature_reverse_complement($sequence) {
  391. $seq = strtoupper($sequence);
  392. $seq = strrev($seq);
  393. $seq = str_replace("A", "t", $seq);
  394. $seq = str_replace("T", "a", $seq);
  395. $seq = str_replace("G", "c", $seq);
  396. $seq = str_replace("C", "g", $seq);
  397. $seq = str_replace("Y", "r", $seq);
  398. $seq = str_replace("R", "y", $seq);
  399. $seq = str_replace("W", "w", $seq);
  400. $seq = str_replace("S", "s", $seq);
  401. $seq = str_replace("K", "m", $seq);
  402. $seq = str_replace("M", "k", $seq);
  403. $seq = str_replace("D", "h", $seq);
  404. $seq = str_replace("V", "b", $seq);
  405. $seq = str_replace("H", "d", $seq);
  406. $seq = str_replace("B", "v", $seq);
  407. return strtoupper($seq);
  408. }
  409. /**
  410. * Retrieves the sequence for a feature.
  411. *
  412. * @param $feature_id
  413. * The feature_id of the feature for which the sequence will be retrieved
  414. * @param $feature_name
  415. * The feature name. This will appear on the FASTA definition line
  416. * @param $num_bases_per_line
  417. * Indicate the number of bases to use per line. A new line will be added
  418. * after the specified number of bases on each line.
  419. * @param $derive_from_parent
  420. * Set to '1' if the sequence should be obtained from the parent to which
  421. * this feature is aligned.
  422. * @param $aggregate
  423. * Set to '1' if the sequence should only contain sub features, excluding
  424. * intra sub feature sequence. For example, set this option to obtain just
  425. * the coding sequence of an mRNA.
  426. * @param $output_format
  427. * The type of format. Valid formats include 'fasta_html', 'fasta_txt' and
  428. * 'raw'. The format 'fasta_txt' outputs line
  429. * breaks as <br> tags and the entire return value is in a <span> tag
  430. * with a fixed-width font definition. 'fasta_txt' outputs line breaks with
  431. * windows format carriage returns (e.g. \r\n) with no other formatting. The
  432. * raw format is simply the sequence with now FASTA formatting and no
  433. * line breaks.
  434. * @param $upstream
  435. * An integer specifing the number of upstream bases to include in the output
  436. * @param $downstream
  437. * An integer specifying the number of downstream bases to include in the
  438. * output.
  439. *
  440. * @return
  441. * The DNA/protein sequence formated as requested.
  442. *
  443. * @ingroup tripal_feature_api
  444. */
  445. function tripal_feature_get_formatted_sequence($feature_id, $feature_name,
  446. $num_bases_per_line, $derive_from_parent, $aggregate, $output_format,
  447. $upstream, $downstream, $sub_features = array()) {
  448. // to speed things up we need to make sure we have a persistent connection
  449. $connection = tripal_db_persistent_chado();
  450. if (!$upstream) {
  451. $upstream = 0;
  452. }
  453. if (!$downstream) {
  454. $downstream = 0;
  455. }
  456. // prepare statements we'll need to use later
  457. if (!tripal_core_is_sql_prepared('sequence_by_parent')) {
  458. // prepare the queries we're going to use later during the render phase
  459. // This SQL statement uses conditionals in the select clause to handle
  460. // cases cases where the alignment is in the reverse direction and when
  461. // the upstream and downstream extensions go beyond the lenght of the
  462. // parent sequence.
  463. $psql ='PREPARE sequence_by_parent (int, int, int) AS
  464. SELECT srcname, srcfeature_id, strand, srctypename, typename,
  465. fmin, fmax, upstream, downstream, adjfmin, adjfmax,
  466. substring(residues from (adjfmin + 1) for (upstream + (fmax - fmin) + downstream)) as residues
  467. FROM (
  468. SELECT
  469. OF.name srcname, FL.srcfeature_id, FL.strand,
  470. OCVT.name as srctypename, SCVT.name as typename,
  471. FL.fmin, FL.fmax,
  472. CASE
  473. WHEN FL.strand >= 0 THEN
  474. CASE
  475. WHEN FL.fmin - $1 <= 0 THEN 0
  476. ELSE FL.fmin - $1
  477. END
  478. WHEN FL.strand < 0 THEN
  479. CASE
  480. WHEN FL.fmin - $2 <= 0 THEN 0
  481. ELSE FL.fmin - $2
  482. END
  483. END as adjfmin,
  484. CASE
  485. WHEN FL.strand >= 0 THEN
  486. CASE
  487. WHEN FL.fmax + $2 > OF.seqlen THEN OF.seqlen
  488. ELSE FL.fmax + $2
  489. END
  490. WHEN FL.strand < 0 THEN
  491. CASE
  492. WHEN FL.fmax + $1 > OF.seqlen THEN OF.seqlen
  493. ELSE FL.fmax + $1
  494. END
  495. END as adjfmax,
  496. CASE
  497. WHEN FL.strand >= 0 THEN
  498. CASE
  499. WHEN FL.fmin - $1 <= 0 THEN FL.fmin
  500. ELSE $1
  501. END
  502. ELSE
  503. CASE
  504. WHEN FL.fmax + $1 > OF.seqlen THEN OF.seqlen - FL.fmax
  505. ELSE $1
  506. END
  507. END as upstream,
  508. CASE
  509. WHEN FL.strand >= 0 THEN
  510. CASE
  511. WHEN FL.fmax + $2 > OF.seqlen THEN OF.seqlen - FL.fmax
  512. ELSE $2
  513. END
  514. ELSE
  515. CASE
  516. WHEN FL.fmin - $2 <= 0 THEN FL.fmin
  517. ELSE $2
  518. END
  519. END as downstream,
  520. OF.residues
  521. FROM featureloc FL
  522. INNER JOIN feature SF on FL.feature_id = SF.feature_id
  523. INNER JOIN cvterm SCVT on SF.type_id = SCVT.cvterm_id
  524. INNER JOIN feature OF on FL.srcfeature_id = OF.feature_id
  525. INNER JOIN cvterm OCVT on OF.type_id = OCVT.cvterm_id
  526. WHERE SF.feature_id = $3) as tbl1
  527. ';
  528. $status = tripal_core_chado_prepare('sequence_by_parent', $psql, array('int', 'int', 'int'));
  529. if (!$status) {
  530. watchdog('tripal_views_handler_field_sequence',
  531. "init: not able to prepare SQL statement '%name'",
  532. array('%name' => 'sequence_by_parent'), 'WATCHDOG ERROR');
  533. }
  534. // this query is meant to get all of the sub features of any given
  535. // feature (arg #1) and order them as they appear on the reference
  536. // feature (arg #2).
  537. $psql ='PREPARE sub_features (int, int) AS
  538. SELECT SF.feature_id, CVT.name as type_name, SF.type_id
  539. FROM feature_relationship FR
  540. INNER JOIN feature SF on SF.feature_id = FR.subject_id
  541. INNER JOIN cvterm CVT on CVT.cvterm_id = SF.type_id
  542. INNER JOIN featureloc FL on FL.feature_id = FR.subject_id
  543. INNER JOIN feature PF on PF.feature_id = FL.srcfeature_id
  544. WHERE FR.object_id = $1 and PF.feature_id = $2
  545. ORDER BY FL.fmin ASC';
  546. $status = tripal_core_chado_prepare('sub_features', $psql, array('int', 'int'));
  547. if (!$status) {
  548. watchdog('tripal_views_handler_field_sequence',
  549. "init: not able to prepare SQL statement '%name'",
  550. array('%name' => 'ssub_features'), 'WATCHDOG ERROR');
  551. }
  552. $psql ='PREPARE count_sub_features (int, int) AS
  553. SELECT count(*) as num_children
  554. FROM feature_relationship FR
  555. INNER JOIN feature SF on SF.feature_id = FR.subject_id
  556. INNER JOIN cvterm CVT on CVT.cvterm_id = SF.type_id
  557. INNER JOIN featureloc FL on FL.feature_id = FR.subject_id
  558. INNER JOIN feature PF on PF.feature_id = FL.srcfeature_id
  559. WHERE FR.object_id = $1 and PF.feature_id = $2';
  560. $status = tripal_core_chado_prepare('count_sub_features', $psql, array('int', 'int'));
  561. if (!$status) {
  562. watchdog('tripal_views_handler_field_sequence',
  563. "init: not able to prepare SQL statement '%name'",
  564. array('%name' => 'count_sub_features'), 'WATCHDOG ERROR');
  565. }
  566. }
  567. // if we need to get the sequence from the parent then do so now.
  568. if ($derive_from_parent) {
  569. // execute the query to get the sequence from the parent
  570. $sql = "EXECUTE sequence_by_parent (%d, %d, %d)";
  571. $parents = chado_query($sql, $upstream, $downstream, $feature_id);
  572. while ($parent = db_fetch_object($parents)) {
  573. $seq = ''; // initialize the sequence for each parent
  574. // if we are to aggregate then we will ignore the feature returned
  575. // by the query above and rebuild it using the sub features
  576. if ($aggregate) {
  577. // now get the sub features that are located on the parent.
  578. $sql = "EXECUTE sub_features (%d, %d)";
  579. $children = chado_query($sql, $feature_id, $parent->srcfeature_id);
  580. $sql = "EXECUTE count_sub_features (%d, %d)";
  581. $num_children = db_fetch_object(chado_query($sql, $feature_id, $parent->srcfeature_id));
  582. // iterate through the sub features and concat their sequences. They
  583. // should already be in order.
  584. $types = array();
  585. $i = 0;
  586. while ($child = db_fetch_object($children)) {
  587. // if the callee has specified that only certain sub features should be
  588. // included then continue of this child is not one of those allowed
  589. // subfeatures
  590. if (count($sub_features) > 0 and !in_array($child->type_name, $sub_features)) {
  591. continue;
  592. }
  593. // keep up with the types
  594. if (!in_array($child->type_name, $types)) {
  595. $types[] = $child->type_name;
  596. }
  597. $sql = "EXECUTE sequence_by_parent (%d, %d, %d)";
  598. // if the first sub feature we need to include the upstream bases
  599. if ($i == 0 and $parent->strand >= 0) { // forward direction
  600. // -------------------------- ref
  601. // ....----> ---->
  602. // up 1 2
  603. $q = chado_query($sql, $upstream, 0, $child->feature_id);
  604. }
  605. elseif ($i == 0 and $parent->strand < 0) { // reverse direction
  606. // -------------------------- ref
  607. // ....<---- <----
  608. // down 1 2
  609. $q = chado_query($sql, 0, $downstream, $child->feature_id);
  610. }
  611. // if the last sub feature we need to include the downstream bases
  612. elseif ($i == $num_children->num_children - 1 and $parent->strand >= 0) { // forward direction
  613. // -------------------------- ref
  614. // ----> ---->....
  615. // 1 2 down
  616. $q = chado_query($sql, 0, $downstream, $child->feature_id);
  617. }
  618. elseif ($i == $num_children->num_children - 1 and $parent->strand < 0) { // reverse direction
  619. // -------------------------- ref
  620. // <---- <----....
  621. // 1 2 up
  622. $q = chado_query($sql, $upstream, 0, $child->feature_id);
  623. }
  624. // for internal sub features we don't want upstream or downstream bases
  625. else {
  626. $sql = "EXECUTE sequence_by_parent (%d, %d, %d)";
  627. $q = chado_query($sql, 0, 0, $child->feature_id);
  628. }
  629. while ($subseq = db_fetch_object($q)) {
  630. // concatenate the sequences of all the sub features
  631. if ($subseq->srcfeature_id == $parent->srcfeature_id) {
  632. $seq .= $subseq->residues;
  633. }
  634. }
  635. $i++;
  636. }
  637. }
  638. // if this isn't an aggregate then use the parent residues
  639. else {
  640. $seq = $parent->residues;
  641. }
  642. // get the reverse compliment if feature is on the reverse strand
  643. $dir = 'forward';
  644. if ($parent->strand < 0) {
  645. $seq = tripal_feature_reverse_complement($seq);
  646. $dir = 'reverse';
  647. }
  648. // now format for display
  649. if ($output_format == 'fasta_html') {
  650. $seq = wordwrap($seq, $num_bases_per_line, "<br>", TRUE);
  651. }
  652. elseif ($output_format == 'fasta_txt') {
  653. $seq = wordwrap($seq, $num_bases_per_line, "\n", TRUE);
  654. }
  655. $residues .= ">$feature_name ($parent->typename) $parent->srcname:" . ($parent->adjfmin + 1) . ".." . $parent->adjfmax ." ($dir). ";
  656. if (count($types) > 0) {
  657. $residues .= "Excludes all bases but those of type(s): " . implode(', ', $types) . ". " ;
  658. }
  659. if ($parent->upstream > 0) {
  660. $residues .= "Includes " . $parent->upstream . " bases upstream. ";
  661. }
  662. if ($parent->downstream > 0) {
  663. $residues .= "Includes " . $parent->downstream . " bases downstream. ";
  664. }
  665. if (!$seq) {
  666. if ($output_format == 'fasta_html') {
  667. $residues .= "No sequence available.</br>";
  668. }
  669. else {
  670. $residues .= "No sequence available.\n";
  671. }
  672. }
  673. else {
  674. if ($output_format == 'fasta_html') {
  675. $residues .= "<br>";
  676. }
  677. $residues .= "\n" . $seq . "\n";
  678. if ($output_format == 'fasta_html') {
  679. $residues .= "<br>";
  680. }
  681. }
  682. }
  683. }
  684. // if we are not getting the sequence from the parent sequence then
  685. // use what comes through from the feature record
  686. else {
  687. $sql = "SELECT * FROM feature F WHERE feature_id = %d";
  688. $values = db_fetch_object(chado_query($sql, $feature_id));
  689. $residues = $values->residues;
  690. if ($output_format == 'fasta_html') {
  691. $residues = wordwrap($residues, $num_bases_per_line, "<br>", TRUE);
  692. }
  693. elseif ($output_format == 'fasta_txt') {
  694. $residues = wordwrap($residues, $num_bases_per_line, "\n", TRUE);
  695. }
  696. $residues = ">$feature_name\n$residues\n";
  697. }
  698. // format the residues for display
  699. if ($residues and $num_bases_per_line) {
  700. if ($output_format == 'fasta_html') {
  701. $residues = '<span style="font-family: monospace;">' . $residues . '</span>';
  702. }
  703. }
  704. return $residues;
  705. }
  706. /**
  707. * This function defines the custom tables that will be created
  708. * in the chado schema.
  709. *
  710. * @ingroup tripal_feature
  711. */
  712. function tripal_feature_get_custom_tables($table = NULL) {
  713. if (!$table or strcmp($table, 'tripal_gff_temp')==0) {
  714. $schema['tripal_gff_temp'] = array(
  715. 'table' => 'tripal_gff_temp',
  716. 'fields' => array(
  717. 'feature_id' => array(
  718. 'type' => 'int',
  719. 'not null' => TRUE,
  720. ),
  721. 'organism_id' => array(
  722. 'type' => 'int',
  723. 'not null' => TRUE,
  724. ),
  725. 'uniquename' => array(
  726. 'type' => 'text',
  727. 'not null' => TRUE,
  728. ),
  729. 'type_name' => array(
  730. 'type' => 'varchar',
  731. 'length' => '1024',
  732. 'not null' => TRUE,
  733. ),
  734. ),
  735. 'indexes' => array(
  736. 'tripal_gff_temp_idx0' => array('feature_id'),
  737. 'tripal_gff_temp_idx0' => array('organism_id'),
  738. 'tripal_gff_temp_idx1' => array('uniquename'),
  739. ),
  740. 'unique keys' => array(
  741. 'tripal_gff_temp_uq0' => array('feature_id'),
  742. 'tripal_gff_temp_uq1' => array('uniquename', 'organism_id', 'type_name'),
  743. ),
  744. );
  745. }
  746. return $schema;
  747. }
  748. /**
  749. * Using the tripal_core_expand_chado_vars function to retrieve a set
  750. * of relationships can be very slow, especialy if there are many relationships
  751. * This function is intended to help speed up the retrieval of relationships
  752. * by only retrieving the base information for the relationship and returning
  753. * an array with
  754. *
  755. * @param $feature
  756. * The feature object
  757. * @return
  758. * An array with two objects
  759. *
  760. * @ingroup tripal_feature_api
  761. */
  762. function tripal_feature_get_feature_relationships($feature) {
  763. // expand the feature object to include the feature relationships.
  764. $options = array(
  765. 'return_array' => 1,
  766. 'order_by' => array('rank' => 'ASC'),
  767. );
  768. $feature = tripal_core_expand_chado_vars($feature, 'table',
  769. 'feature_relationship', $options);
  770. // get the subject relationships
  771. $srelationships = $feature->feature_relationship->subject_id;
  772. $orelationships = $feature->feature_relationship->object_id;
  773. // get alignment as child. The $feature->featureloc element
  774. // is already populated from the alignment preprocess function
  775. $feature = tripal_core_expand_chado_vars($feature, 'table', 'featureloc');
  776. $cfeaturelocs = $feature->featureloc->feature_id;
  777. if (!$cfeaturelocs) {
  778. $cfeaturelocs = array();
  779. }
  780. elseif (!is_array($cfeaturelocs)) {
  781. $cfeaturelocs = array($cfeaturelocs);
  782. }
  783. // prepare the SQL statement to get the featureloc for the
  784. // feature in the relationships.
  785. $connection = tripal_db_persistent_chado();
  786. $psql = "
  787. PREPARE sel_featureloc_preprocess_relationships (int, int) AS
  788. SELECT
  789. FL.featureloc_id, F.name as srcfeature_name, FL.srcfeature_id,
  790. FL.feature_id, FL.fmin, FL.fmax, FL.strand, FL.phase
  791. FROM featureloc FL
  792. INNER JOIN feature F ON F.feature_id = FL.srcfeature_id
  793. WHERE FL.feature_id = $1 and FL.srcfeature_id = $2
  794. ";
  795. tripal_core_chado_prepare('sel_featureloc_preprocess_relationships', $psql, array('int', 'int'));
  796. // combine both object and subject relationshisp into a single array
  797. $relationships = array();
  798. $relationships['object'] = array();
  799. $relationships['subject'] = array();
  800. // iterate through the object relationships
  801. if ($orelationships) {
  802. foreach ($orelationships as $relationship) {
  803. $rel = new stdClass();
  804. // get locations where the child feature and this feature overlap with the
  805. // same landmark feature.
  806. $rel->child_featurelocs = array();
  807. foreach ($cfeaturelocs as $featureloc) {
  808. $res = chado_query("EXECUTE sel_featureloc_preprocess_relationships (%d, %d)",
  809. $relationship->subject_id->feature_id,
  810. $featureloc->srcfeature_id->feature_id);
  811. while ($loc = db_fetch_object($res)) {
  812. // add in the node id of the src feature if it exists and save this location
  813. $loc->nid = $featureloc->srcfeature_id->nid;
  814. $rel->child_featurelocs[] = $loc;
  815. }
  816. }
  817. $rel->record = $relationship;
  818. // get the relationship and child types
  819. $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
  820. $child_type = $relationship->subject_id->type_id->name;
  821. // get the node id of the subject
  822. $sql = "SELECT nid FROM {chado_feature} WHERE feature_id = %d";
  823. $n = db_fetch_object(db_query($sql, $relationship->subject_id->feature_id));
  824. if ($n) {
  825. $rel->record->nid = $n->nid;
  826. }
  827. if (!array_key_exists($rel_type, $relationships['object'])) {
  828. $relationships['object'][$rel_type] = array();
  829. }
  830. if (!array_key_exists($child_type, $relationships['object'][$rel_type])) {
  831. $relationships['object'][$rel_type][$child_type] = array();
  832. }
  833. $relationships['object'][$rel_type][$child_type][] = $rel;
  834. }
  835. }
  836. // now add in the subject relationships
  837. if ($srelationships) {
  838. foreach ($srelationships as $relationship) {
  839. $rel = new stdClass();
  840. // get locations where this feature overlaps with the parent
  841. $rel->parent_featurelocs = array();
  842. foreach ($cfeaturelocs as $featureloc) {
  843. $res = chado_query("EXECUTE sel_featureloc_preprocess_relationships (%d, %d)",
  844. $relationship->object_id->feature_id,
  845. $featureloc->srcfeature_id->feature_id);
  846. while ($loc = db_fetch_object($res)) {
  847. // add in the node id of the src feature if it exists and save this location
  848. $loc->nid = $featureloc->srcfeature_id->nid;
  849. $rel->parent_featurelocs[] = $loc;
  850. }
  851. }
  852. $rel->record = $relationship;
  853. $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
  854. $parent_type = $relationship->object_id->type_id->name;
  855. // get the node id of the subject
  856. $sql = "SELECT nid FROM {chado_feature} WHERE feature_id = %d";
  857. $n = db_fetch_object(db_query($sql, $relationship->object_id->feature_id));
  858. if ($n) {
  859. $rel->record->nid = $n->nid;
  860. }
  861. if (!array_key_exists($rel_type, $relationships['subject'])) {
  862. $relationships['subject'][$rel_type] = array();
  863. }
  864. if (!array_key_exists($child_type, $relationships['subject'][$rel_type])) {
  865. $relationships['subject'][$rel_type][$parent_type] = array();
  866. }
  867. $relationships['subject'][$rel_type][$parent_type][] = $rel;
  868. }
  869. }
  870. return $relationships;
  871. }