tripal_feature.api.inc 40 KB

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