tripal_feature.api.inc 35 KB

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