tripal_feature.api.inc 38 KB

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