tripal_feature.api.inc 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. <?php
  2. /**
  3. * @defgroup tripal_feature_api Feature API
  4. * @ingroup tripal_api
  5. * @{
  6. * Provides an application programming interface (API) for working with features
  7. * @}
  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. tripal_report_error('tripal_feature', TRIPAL_WARNING,
  41. 'tripal_feature_analysis_get_property: Both an analysis ID and feature ID should be specified',
  42. array());
  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 = chado_select_record('analysisfeature', $columns, $values);
  49. $analysisfeature_id = $result[0]->analysisfeature_id;
  50. }
  51. // get the property.
  52. return chado_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. tripal_report_error('tripal_feature', TRIPAL_WARNING,
  90. 'tripal_feature_analysis_insert_property: Both an analysis ID and feature ID should be specified',
  91. array());
  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 = chado_select_record('analysisfeature', $columns, $values);
  98. $analysisfeature_id = $result[0]->analysisfeature_id;
  99. }
  100. // insert the property.
  101. $success = chado_insert_property('analysisfeature', $analysisfeature_id,
  102. $property, $cv_name, $value, $update_if_present);
  103. if (!$success) {
  104. tripal_report_error('tripal_feature', TRIPAL_WARNING,
  105. 'tripal_feature_analysis_insert_property: Failed to insert analysis feature property',
  106. array());
  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. tripal_report_error('tripal_feature', TRIPAL_WARNING,
  154. 'tripal_feature_analysis_update_property: Both an analysis ID and feature ID should be specified',
  155. array());
  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 = chado_select_record('analysisfeature', $columns, $values);
  162. $analysisfeature_id = $result[0]->analysisfeature_id;
  163. }
  164. // update the property.
  165. return chado_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 chado_update_property('analysisfeature',
  189. NULL, $property, $cv_name, $value, FALSE, $analysisfeatureprop_id);
  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. tripal_report_error('tripal_feature', TRIPAL_WARNING,
  229. 'tripal_feature_analysis_delete_property: Both an analysis ID and feature ID should be specified',
  230. array());
  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 = chado_select_record('analysisfeature', $columns, $values);
  237. $analysisfeature_id = $result[0]->analysisfeature_id;
  238. }
  239. // get the property.
  240. return chado_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. // construct the array that will match the exact record to update
  255. $match = array(
  256. 'analysisfeatureprop_id' => $analysisfeatureprop_id,
  257. );
  258. return chado_delete_record('analysisfeatureprop', $match);
  259. }
  260. /**
  261. * Retrieve properties of a given type for a given feature
  262. *
  263. * @param $feature_id
  264. * The feature_id of the properties you would like to retrieve
  265. * @param $property
  266. * The cvterm name of the properties to retrieve
  267. * @param $cv_name
  268. * Optional. The name of the cv to which the property belongs. By
  269. * default this is the 'tripal' cv.
  270. *
  271. * @return
  272. * A feature chado variable with the specified properties expanded
  273. *
  274. * @ingroup tripal_feature_api
  275. */
  276. function tripal_feature_get_property($feature_id, $property, $cv_name='tripal') {
  277. return chado_get_property('feature', $feature_id, $property, $cv_name);
  278. }
  279. /**
  280. * Insert a given property
  281. *
  282. * @param $feature_id
  283. * The feature_id of the property to insert
  284. * @param $property
  285. * The cvterm name of the property to insert
  286. * @param $value
  287. * The value of the property to insert
  288. * @param $update_if_present
  289. * A boolean indicated whether to update the record if it's already present
  290. * @param $cv_name
  291. * Optional. The name of the cv to which the property belongs. By
  292. * default this is the 'tripal' cv.
  293. *
  294. * @return
  295. * True of success, False otherwise
  296. *
  297. * @ingroup tripal_feature_api
  298. */
  299. function tripal_feature_insert_property($feature_id, $property, $value,
  300. $update_if_present = 0, $cv_name = 'tripal') {
  301. return chado_insert_property('feature', $feature_id, $property,
  302. $cv_name, $value, $update_if_present);
  303. }
  304. /**
  305. * Update a feature property using the property name. Only use this
  306. * if the property is unique and only exist once for the feature.
  307. *
  308. * @param $feature_id
  309. * The feature_id of the property to update
  310. * @param $property
  311. * The cvterm name of the property to update
  312. * @param $value
  313. * The value of the property to update
  314. * @param $insert_if_missing
  315. * A boolean indicated whether to insert the record if it's absent
  316. * @param $cv_name
  317. * Optional. The name of the cv to which the property belongs. By
  318. * default this is the 'tripal' cv.
  319. *
  320. * Note: The property will be identified using the unique combination of the $feature_id and $property
  321. * and then it will be updated with the supplied value
  322. *
  323. * @return
  324. * True of success, False otherwise
  325. *
  326. * @ingroup tripal_feature_api
  327. */
  328. function tripal_feature_update_property($feature_id, $property,
  329. $value, $insert_if_missing = 0, $cv_name = 'tripal') {
  330. return chado_update_property('feature', $feature_id, $property, $cv_name, $value, $insert_if_missing);
  331. }
  332. /**
  333. * Update a given feature property using the featureprop_id
  334. *
  335. * @param $featureprop_id
  336. * The featureprop_id of the property to update
  337. * @param $property
  338. * The cvterm name of the property
  339. * @param $value
  340. * The value of the property
  341. * @param $cv_name
  342. * Optional. The name of the cv to which the property belongs. By
  343. * default this is the 'tripal' cv.
  344. *
  345. * @return
  346. * True of success, False otherwise
  347. *
  348. * @ingroup tripal_feature_api
  349. */
  350. function tripal_feature_update_property_by_id($featureprop_id, $property,
  351. $value, $cv_name = 'tripal') {
  352. return chado_update_property('feature', NULL, $property, $cv_name, $value, FALSE, $featureprop_id);
  353. }
  354. /**
  355. * Delete a given feature property using the property name. Only use this
  356. * if the property is unique and only exists once for the feature.
  357. *
  358. * @param $feature_id
  359. * The feature_id of the property to delete
  360. * @param $property
  361. * The cvterm name of the property to delete
  362. * @param $cv_name
  363. * Optional. The name of the cv to which the property belongs. By
  364. * default this is the 'tripal' cv.
  365. *
  366. * Note: The property will be identified using the unique combination of the $feature_id and $property
  367. * and then it will be deleted
  368. *
  369. * @return
  370. * True of success, False otherwise
  371. *
  372. * @ingroup tripal_feature_api
  373. */
  374. function tripal_feature_delete_property($feature_id, $property, $cv_name='tripal') {
  375. return chado_delete_property('feature', $feature_id, $property, $cv_name);
  376. }
  377. /**
  378. * Delete a given feature property using the featureprop_id
  379. *
  380. * @param $featureprop_id
  381. * The feature_id of the property to delete
  382. *
  383. * @return
  384. * True of success, False otherwise
  385. *
  386. * @ingroup tripal_feature_api
  387. */
  388. function tripal_feature_delete_property_by_id($featureprop_id) {
  389. // construct the array that will match the exact record to update
  390. $match = array(
  391. 'featureprop_id' => $featureprop_id,
  392. );
  393. return chado_delete_record('featureprop', $match);
  394. }
  395. /**
  396. * Performs a reverse compliment of a nucleotide sequence
  397. *
  398. * @param $sequence
  399. * The nucelotide sequence
  400. *
  401. * @return
  402. * an upper-case reverse complemented sequence
  403. *
  404. * @ingroup tripal_feature_api
  405. */
  406. function tripal_feature_reverse_complement($sequence) {
  407. $seq = strtoupper($sequence);
  408. $seq = strrev($seq);
  409. $seq = str_replace("A", "t", $seq);
  410. $seq = str_replace("T", "a", $seq);
  411. $seq = str_replace("G", "c", $seq);
  412. $seq = str_replace("C", "g", $seq);
  413. $seq = str_replace("Y", "r", $seq);
  414. $seq = str_replace("R", "y", $seq);
  415. $seq = str_replace("W", "w", $seq);
  416. $seq = str_replace("S", "s", $seq);
  417. $seq = str_replace("K", "m", $seq);
  418. $seq = str_replace("M", "k", $seq);
  419. $seq = str_replace("D", "h", $seq);
  420. $seq = str_replace("V", "b", $seq);
  421. $seq = str_replace("H", "d", $seq);
  422. $seq = str_replace("B", "v", $seq);
  423. return strtoupper($seq);
  424. }
  425. /**
  426. * Retrieves the sequence for a feature.
  427. *
  428. * @param $feature_id
  429. * The feature_id of the feature for which the sequence will be retrieved
  430. * @param $feature_name
  431. * The feature name. This will appear on the FASTA definition line
  432. * @param $num_bases_per_line
  433. * Indicate the number of bases to use per line. A new line will be added
  434. * after the specified number of bases on each line.
  435. * @param $derive_from_parent
  436. * Set to '1' if the sequence should be obtained from the parent to which
  437. * this feature is aligned.
  438. * @param $aggregate
  439. * Set to '1' if the sequence should only contain sub features, excluding
  440. * intra sub feature sequence. For example, set this option to obtain just
  441. * the coding sequence of an mRNA.
  442. * @param $output_format
  443. * The type of format. Valid formats include 'fasta_html', 'fasta_txt' and
  444. * 'raw'. The format 'fasta_txt' outputs line
  445. * breaks as <br> tags and the entire return value is in a <span> tag
  446. * with a fixed-width font definition. 'fasta_txt' outputs line breaks with
  447. * windows format carriage returns (e.g. \r\n) with no other formatting. The
  448. * raw format is simply the sequence with now FASTA formatting and no
  449. * line breaks.
  450. * @param $upstream
  451. * An integer specifing the number of upstream bases to include in the output
  452. * @param $downstream
  453. * An integer specifying the number of downstream bases to include in the
  454. * output.
  455. * @param $sub_features
  456. * Only include sub features (or child features) of the types provided in the array
  457. * @param $relationship
  458. * If a relationship name is provided (e.g. sequence_of) then any sequences that
  459. * are in relationships of this type with matched sequences are also included
  460. * @param $rel_part
  461. * If a relationship is provided in the preceeding argument then the rel_part
  462. * must be either 'object' or 'subject' to indicate which side of the
  463. * relationship the matched features belong
  464. *
  465. * @return
  466. * The DNA/protein sequence formated as requested.
  467. *
  468. * @ingroup tripal_feature_api
  469. */
  470. function tripal_feature_get_formatted_sequence($feature_id, $feature_name,
  471. $num_bases_per_line, $derive_from_parent, $aggregate, $output_format,
  472. $upstream, $downstream, $sub_features = array(), $relationship = '', $rel_part = '') {
  473. // to speed things up we need to make sure we have a persistent connection
  474. $connection = tripal_db_persistent_chado();
  475. if (!$upstream) {
  476. $upstream = 0;
  477. }
  478. if (!$downstream) {
  479. $downstream = 0;
  480. }
  481. if ($rel_part == "object" or $rel_part == "subject") {
  482. if ($rel_part == "subject") {
  483. $psql = '
  484. PREPARE feature_rel_get_object (int, text) AS
  485. SELECT FO.feature_id, FO.name, FO.uniquename, CVTO.name as feature_type, O.genus, O.species
  486. FROM feature FS
  487. INNER JOIN feature_relationship FR ON FR.subject_id = FS.feature_id
  488. INNER JOIN cvterm CVTFR ON CVTFR.cvterm_id = FR.type_id
  489. INNER JOIN feature FO ON FO.feature_id = FR.object_id
  490. INNER JOIN cvterm CVTO ON CVTO.cvterm_id = FO.type_id
  491. INNER JOIN organism O ON O.organism_id = FO.organism_id
  492. WHERE
  493. FS.feature_id = $1 AND
  494. CVTFR.name = $2
  495. ';
  496. $status = tripal_core_chado_prepare('feature_rel_get_object', $psql, array('int', 'text'));
  497. if (!$status) {
  498. tripal_report_error('tripal_feature', TRIPAL_ERROR, "init: not able to prepare SQL statement '%name'",
  499. array('%name' => 'feature_by_subject'));
  500. }
  501. $sql = "EXECUTE feature_rel_get_object(:feature_id, :relationship)";
  502. $features = chado_query($sql, array(':feature_id' => $feature_id, ':relationship' => $relationship));
  503. }
  504. if ($rel_part == "object") {
  505. $psql = '
  506. PREPARE feature_rel_get_subject (int, text) AS
  507. SELECT FS.feature_id, FS.name, FS.uniquename, CVTO.name as feature_type, O.genus, O.species
  508. FROM feature FO
  509. INNER JOIN feature_relationship FR ON FR.object_id = FO.feature_id
  510. INNER JOIN cvterm CVTFR ON CVTFR.cvterm_id = FR.type_id
  511. INNER JOIN feature FS ON FS.feature_id = FR.subject_id
  512. INNER JOIN cvterm CVTO ON CVTO.cvterm_id = FS.type_id
  513. INNER JOIN organism O ON O.organism_id = FS.organism_id
  514. WHERE
  515. FO.feature_id = $1 AND
  516. CVTFR.name = $2
  517. ';
  518. $status = tripal_core_chado_prepare('feature_rel_get_subject', $psql, array('int', 'text'));
  519. if (!$status) {
  520. tripal_report_error('tripal_feature', TRIPAL_ERROR, "init: not able to prepare SQL statement '%name'",
  521. array('%name' => 'feature_by_object'));
  522. }
  523. $sql = "EXECUTE feature_rel_get_subject(:feature_id, :relationship)";
  524. $features = chado_query($sql, array(':feature_id' => $feature_id, ':relationship' => $relationship));
  525. }
  526. $sequences = '';
  527. while ($feature = $features->fetchObject()) {
  528. // recurse and get the sequences for these in the relationship
  529. if ($rel_part == "subject") {
  530. $defline = "$feature_name, $relationship, $feature->uniquename $feature->feature_type ($feature->genus $feature->species)";
  531. }
  532. if ($rel_part == "object") {
  533. $defline = "$feature->uniquename $feature->feature_type ($feature->genus $feature->species), $relationship, $feature_name";
  534. }
  535. $sequences .= tripal_feature_get_formatted_sequence($feature->feature_id, $defline,
  536. $num_bases_per_line, $derive_from_parent, $aggregate, $output_format,
  537. $upstream, $downstream, $sub_features, '', '');
  538. }
  539. return $sequences;
  540. }
  541. // prepare statements we'll need to use later
  542. if (!tripal_core_is_sql_prepared('sequence_by_parent')) {
  543. // prepare the queries we're going to use later during the render phase
  544. // This SQL statement uses conditionals in the select clause to handle
  545. // cases cases where the alignment is in the reverse direction and when
  546. // the upstream and downstream extensions go beyond the lenght of the
  547. // parent sequence.
  548. $psql ='
  549. PREPARE sequence_by_parent (int, int, int) AS
  550. SELECT srcname, srcfeature_id, strand, srctypename, typename,
  551. fmin, fmax, upstream, downstream, adjfmin, adjfmax,
  552. substring(residues from (adjfmin + 1) for (upstream + (fmax - fmin) + downstream)) as residues,
  553. genus, species
  554. FROM (
  555. SELECT
  556. OF.name srcname, FL.srcfeature_id, FL.strand,
  557. OCVT.name as srctypename, SCVT.name as typename,
  558. FL.fmin, FL.fmax, OO.genus, OO.species,
  559. CASE
  560. WHEN FL.strand >= 0 THEN
  561. CASE
  562. WHEN FL.fmin - $1 <= 0 THEN 0
  563. ELSE FL.fmin - $1
  564. END
  565. WHEN FL.strand < 0 THEN
  566. CASE
  567. WHEN FL.fmin - $2 <= 0 THEN 0
  568. ELSE FL.fmin - $2
  569. END
  570. END as adjfmin,
  571. CASE
  572. WHEN FL.strand >= 0 THEN
  573. CASE
  574. WHEN FL.fmax + $2 > OF.seqlen THEN OF.seqlen
  575. ELSE FL.fmax + $2
  576. END
  577. WHEN FL.strand < 0 THEN
  578. CASE
  579. WHEN FL.fmax + $1 > OF.seqlen THEN OF.seqlen
  580. ELSE FL.fmax + $1
  581. END
  582. END as adjfmax,
  583. CASE
  584. WHEN FL.strand >= 0 THEN
  585. CASE
  586. WHEN FL.fmin - $1 <= 0 THEN FL.fmin
  587. ELSE $1
  588. END
  589. ELSE
  590. CASE
  591. WHEN FL.fmax + $1 > OF.seqlen THEN OF.seqlen - FL.fmax
  592. ELSE $1
  593. END
  594. END as upstream,
  595. CASE
  596. WHEN FL.strand >= 0 THEN
  597. CASE
  598. WHEN FL.fmax + $2 > OF.seqlen THEN OF.seqlen - FL.fmax
  599. ELSE $2
  600. END
  601. ELSE
  602. CASE
  603. WHEN FL.fmin - $2 <= 0 THEN FL.fmin
  604. ELSE $2
  605. END
  606. END as downstream,
  607. OF.residues
  608. FROM {featureloc} FL
  609. INNER JOIN {feature} SF on FL.feature_id = SF.feature_id
  610. INNER JOIN {cvterm} SCVT on SF.type_id = SCVT.cvterm_id
  611. INNER JOIN {feature} OF on FL.srcfeature_id = OF.feature_id
  612. INNER JOIN {cvterm} OCVT on OF.type_id = OCVT.cvterm_id
  613. INNER JOIN {organism} OO on OF.organism_id = OO.organism_id
  614. WHERE SF.feature_id = $3 and NOT (OF.residues = \'\' or OF.residues IS NULL)) as tbl1
  615. ';
  616. $status = tripal_core_chado_prepare('sequence_by_parent', $psql, array('int', 'int', 'int'));
  617. if (!$status) {
  618. tripal_report_error('tripal_feature', TRIPAL_ERROR,
  619. "init: not able to prepare SQL statement '%name'",
  620. array('%name' => 'sequence_by_parent'));
  621. }
  622. // this query is meant to get all of the sub features of any given
  623. // feature (arg #1) and order them as they appear on the reference
  624. // feature (arg #2).
  625. $psql ='PREPARE sub_features (int, int) AS
  626. SELECT SF.feature_id, CVT.name as type_name, SF.type_id
  627. FROM {feature_relationship} FR
  628. INNER JOIN {feature} SF on SF.feature_id = FR.subject_id
  629. INNER JOIN {cvterm} CVT on CVT.cvterm_id = SF.type_id
  630. INNER JOIN {featureloc} FL on FL.feature_id = FR.subject_id
  631. INNER JOIN {feature} PF on PF.feature_id = FL.srcfeature_id
  632. WHERE FR.object_id = $1 and PF.feature_id = $2
  633. ORDER BY FL.fmin ASC';
  634. $status = tripal_core_chado_prepare('sub_features', $psql, array('int', 'int'));
  635. if (!$status) {
  636. tripal_report_error('tripal_feature', TRIPAL_ERROR,
  637. "init: not able to prepare SQL statement '%name'",
  638. array('%name' => 'ssub_features'));
  639. }
  640. $psql ='PREPARE count_sub_features (int, int) AS
  641. SELECT count(*) as num_children
  642. FROM {feature_relationship} FR
  643. INNER JOIN {feature} SF on SF.feature_id = FR.subject_id
  644. INNER JOIN {cvterm} CVT on CVT.cvterm_id = SF.type_id
  645. INNER JOIN {featureloc} FL on FL.feature_id = FR.subject_id
  646. INNER JOIN {feature} PF on PF.feature_id = FL.srcfeature_id
  647. WHERE FR.object_id = $1 and PF.feature_id = $2';
  648. $status = tripal_core_chado_prepare('count_sub_features', $psql, array('int', 'int'));
  649. if (!$status) {
  650. tripal_report_error('tripal_feature', TRIPAL_ERROR,
  651. "init: not able to prepare SQL statement '%name'",
  652. array('%name' => 'count_sub_features'));
  653. }
  654. }
  655. // if we need to get the sequence from the parent then do so now.
  656. if ($derive_from_parent) {
  657. // execute the query to get the sequence from the parent
  658. $sql = "EXECUTE sequence_by_parent (:upstream, :downstream, :feature_id)";
  659. $parents = chado_query($sql, array(':uptream' => $upstream, ':downstream' => $downstream, ':feature_id' => $feature_id));
  660. while ($parent = $parents->fetchObject()) {
  661. $seq = ''; // initialize the sequence for each parent
  662. // if we are to aggregate then we will ignore the feature returned
  663. // by the query above and rebuild it using the sub features
  664. if ($aggregate) {
  665. // now get the sub features that are located on the parent.
  666. $sql = "EXECUTE sub_features (:feature_id, :srcfeature_id)";
  667. $children = chado_query($sql, array(':feature_id' => $feature_id, ':srcfeature_id' => $parent->srcfeature_id));
  668. $sql = "EXECUTE count_sub_features (:feature_id, :srcfeature_id)";
  669. $sub_features = chado_query($sql, array(':feature_id' => $feature_id, ':srcfeature_id' => $parent->srcfeature_id));
  670. $num_children = $sub_features->fetchObject();
  671. // iterate through the sub features and concat their sequences. They
  672. // should already be in order.
  673. $types = array();
  674. $i = 0;
  675. while ($child = $children->fetchObject()) {
  676. // if the callee has specified that only certain sub features should be
  677. // included then continue if this child is not one of those allowed
  678. // subfeatures
  679. if (count($sub_features) > 0 and !in_array($child->type_name, $sub_features)) {
  680. continue;
  681. }
  682. // keep up with the types
  683. if (!in_array($child->type_name, $types)) {
  684. $types[] = $child->type_name;
  685. }
  686. $sql = "EXECUTE sequence_by_parent (:upstream, %d, :feature_id)";
  687. // if the first sub feature we need to include the upstream bases. first check if
  688. // the feature is in the foward direction or the reverse.
  689. if ($i == 0 and $parent->strand >= 0) { // forward direction
  690. // -------------------------- ref
  691. // ....----> ---->
  692. // up 1 2
  693. $q = chado_query($sql, array(':upstream' => $upstream, ':downstream' => 0, ':feature_id' => $child->feature_id));
  694. }
  695. elseif ($i == 0 and $parent->strand < 0) { // reverse direction
  696. // -------------------------- ref
  697. // ....<---- <----
  698. // down 1 2
  699. $q = chado_query($sql, array(':upstream' => 0, ':downstream' => $downstream, ':feature_id' => $child->feature_id));
  700. }
  701. // Next, if the last sub feature we need to include the downstream bases. first check if
  702. // the feature is in teh forward direction or the reverse
  703. if ($i == $num_children->num_children - 1 and $parent->strand >= 0) { // forward direction
  704. // -------------------------- ref
  705. // ----> ---->....
  706. // 1 2 down
  707. $q = chado_query($sql, array(':upstream' => 0, ':downstream' => $downstream, ':feature_id' => $child->feature_id));
  708. }
  709. elseif ($i == $num_children->num_children - 1 and $parent->strand < 0) { // reverse direction
  710. // -------------------------- ref
  711. // <---- <----....
  712. // 1 2 up
  713. $q = chado_query($sql, array(':upstream' => $upstream, ':downstream' => 0, ':feature_id' => $child->feature_id));
  714. }
  715. // for internal sub features we don't want upstream or downstream bases
  716. else {
  717. $sql = "EXECUTE sequence_by_parent (%d, %d, %d)";
  718. $q = chado_query($sql, array(':upstream' => 0, ':downstream' => 0, ':feature_id' => $child->feature_id));
  719. }
  720. while ($subseq = $q->fetchObject()) {
  721. // concatenate the sequences of all the sub features
  722. if ($subseq->srcfeature_id == $parent->srcfeature_id) {
  723. $seq .= $subseq->residues;
  724. }
  725. }
  726. $i++;
  727. }
  728. }
  729. // if this isn't an aggregate then use the parent residues
  730. else {
  731. $seq = $parent->residues;
  732. }
  733. // get the reverse compliment if feature is on the reverse strand
  734. $dir = 'forward';
  735. if ($parent->strand < 0) {
  736. $seq = tripal_feature_reverse_complement($seq);
  737. $dir = 'reverse';
  738. }
  739. // now format for display
  740. if ($output_format == 'fasta_html') {
  741. $seq = wordwrap($seq, $num_bases_per_line, "<br>", TRUE);
  742. }
  743. elseif ($output_format == 'fasta_txt') {
  744. $seq = wordwrap($seq, $num_bases_per_line, "\r\n", TRUE);
  745. }
  746. $residues .= ">$feature_name. Sequence derived from feature of type, '$parent->srctypename', of $parent->genus $parent->species: $parent->srcname:" . ($parent->adjfmin + 1) . ".." . $parent->adjfmax . " ($dir). ";
  747. if (count($types) > 0) {
  748. $residues .= "Excludes all bases but those of type(s): " . implode(', ', $types) . ". " ;
  749. }
  750. if ($parent->upstream > 0) {
  751. $residues .= "Includes " . $parent->upstream . " bases upstream. ";
  752. }
  753. if ($parent->downstream > 0) {
  754. $residues .= "Includes " . $parent->downstream . " bases downstream. ";
  755. }
  756. if (!$seq) {
  757. if ($output_format == 'fasta_html') {
  758. $residues .= "No sequence available.</br>";
  759. }
  760. else {
  761. $residues .= "No sequence available.\r\n";
  762. }
  763. }
  764. else {
  765. if ($output_format == 'fasta_html') {
  766. $residues .= "<br>";
  767. }
  768. $residues .= "\r\n" . $seq . "\r\n";
  769. if ($output_format == 'fasta_html') {
  770. $residues .= "<br>";
  771. }
  772. }
  773. }
  774. }
  775. // if we are not getting the sequence from the parent sequence then
  776. // use what comes through from the feature record
  777. else {
  778. $sql = "SELECT * FROM {feature} F WHERE feature_id = :feature_id";
  779. $values = chado_query($sql, array(':feature_id' => $feature_id))->fetchObject();
  780. $residues = $values->residues;
  781. if ($output_format == 'fasta_html') {
  782. $residues = wordwrap($residues, $num_bases_per_line, "<br>", TRUE);
  783. }
  784. elseif ($output_format == 'fasta_txt') {
  785. $residues = wordwrap($residues, $num_bases_per_line, "\r\n", TRUE);
  786. }
  787. $residues = ">$feature_name\r\n$residues\r\n";
  788. }
  789. // format the residues for display
  790. if ($residues and $num_bases_per_line) {
  791. if ($output_format == 'fasta_html') {
  792. $residues = '<span style="font-family: monospace;">' . $residues . '</span>';
  793. }
  794. }
  795. return $residues;
  796. }
  797. /*
  798. * This function adds an entry to the feature_dbxref table.
  799. *
  800. * @param $feature_id
  801. * The numeric feature_if of the feature
  802. * @param $dbname
  803. * The name of the database to which the term belongs
  804. * @param accession
  805. * The accession of the term
  806. *
  807. * @return
  808. * TRUE on success. FALSE on failure.
  809. *
  810. * @ingroup tripal_feature_api
  811. */
  812. function tripal_feature_add_dbxref($feature_id, $dbname, $accession) {
  813. // make sure the db exists. If it doesn't, then add it
  814. $values = array('name' => $dbname);
  815. $options = array('statement_name' => 'sel_db_na');
  816. $db = chado_select_record('db', array('db_id'), $values, $options);
  817. if (!$db or count($db) == 0) {
  818. $options = array('statement_name' => 'ins_db_na');
  819. $success = chado_insert_record('db', $values, $options);
  820. if (!$success) {
  821. tripal_report_error('tripal_feature', TRIPAL_WARNING, 'tripal_feature_add_dbxref: The feature dbxref entry for feature, %feature_id, " .
  822. "could not be added because the database, %dbname, does not exist and cannot be added.',
  823. array('%feature_id' => $feature_id, '%dbname' => $dbname));
  824. return FALSE;
  825. }
  826. }
  827. // first make sure that the record doesn't already exist
  828. $values = array(
  829. 'dbxref_id' => array(
  830. 'accession' => $accession,
  831. 'db_id' => array(
  832. 'name' => $dbname
  833. ),
  834. ),
  835. 'feature_id' => $feature_id,
  836. );
  837. $options = array('statement_name' => 'sel_featuredbxref_dbfe');
  838. $xref = chado_select_record('feature_dbxref', array('feature_dbxref_id'), $values, $options);
  839. if (count($xref) == 0) {
  840. // if the record doesn't exist then add it.
  841. $options = array('statement_name' => 'ins_featuredbxref_dbfe');
  842. $success = chado_insert_record('feature_dbxref', $values, $options);
  843. if (!$success) {
  844. tripal_report_error('tripal_feature', TRIPAL_WARNING, 'tripal_feature_add_dbxref: The feature dbxref entry for feature, %feature_id, ' .
  845. 'could not be added: %db:%accession.', array('%feature_id' => $feature_id, '%db' => $dbname,
  846. '%accession' => $accession));
  847. return FALSE;
  848. }
  849. }
  850. return TRUE;
  851. }
  852. /*
  853. * This function adds an entry to the feature_cvterm table.
  854. *
  855. * @param $feature_id
  856. * The numeric feature_if of the feature
  857. * @param $cvname
  858. * The name of the controlled vocabulary to which the term belongs
  859. * @param cvterm
  860. * The name of the cvterm
  861. *
  862. * @return
  863. * TRUE on success. FALSE on failure.
  864. *
  865. * @ingroup tripal_feature_api
  866. */
  867. function tripal_feature_add_cvterm($feature_id, $cvname, $cvterm) {
  868. // make sure the cv exists. If it doesn't, then add it
  869. $values = array('name' => $cvname);
  870. $options = array('statement_name' => 'sel_cv_na');
  871. $cv = chado_select_record('cv', array('cv_id'), $values, $options);
  872. if (!$cv or count($cv) == 0) {
  873. $options = array('statement_name' => 'ins_cv_na');
  874. $success = chado_insert_record('cv', $values, $options);
  875. if (!$success) {
  876. tripal_report_error('tripal_feature', TRIPAL_WARNING, 'tripal_feature_add_cvterm: The feature cvterm entry for feature, %feature_id, " .
  877. "could not be added because the CV, %cvname, does not exist and cannot be added.',
  878. array('%feature_id' => $feature_id, '%cvname' => $cvname));
  879. return FALSE;
  880. }
  881. }
  882. // first make sure that the record doesn't already exist
  883. $values = array(
  884. 'cvterm_id' => array(
  885. 'name' => $cvterm,
  886. 'cv_id' => array(
  887. 'name' => $cvname
  888. ),
  889. ),
  890. 'feature_id' => $feature_id,
  891. 'pub_id' => 1,
  892. );
  893. $options = array('statement_name' => 'sel_featuredcvterm_cvfepu');
  894. $xref = chado_select_record('feature_cvterm', array('feature_cvterm_id'), $values, $options);
  895. if (count($xref) == 0) {
  896. // if the record doesn't exist then add it.
  897. $options = array('statement_name' => 'ins_featurecvterm_cvfepu');
  898. $success = chado_insert_record('feature_cvterm', $values, $options);
  899. if (!$success) {
  900. tripal_report_error('tripal_feature', TRIPAL_WARNING, 'tripal_feature_add_cvterm: The feature cvterm entry for feature, %feature_id, ' .
  901. 'could not be added: %cvterm.', array('%feature_id' => $feature_id, '%cvterm' => $cvterm));
  902. return FALSE;
  903. }
  904. }
  905. return TRUE;
  906. }
  907. /**
  908. *
  909. *
  910. * @ingroup tripal_feature
  911. */
  912. function tripal_feature_return_fasta($feature, $desc) {
  913. $fasta = ">" . variable_get('chado_feature_accession_prefix', 'FID') . "$feature->feature_id|$feature->name";
  914. $fasta .= " $desc\n";
  915. $fasta .= wordwrap($feature->residues, 50, "\n", TRUE);
  916. $fasta .= "\n\n";
  917. return $fasta;
  918. }