tripal_chado.chado_vx_x.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <?php
  2. function tripal_chado_add_tripal_gff_temp_table() {
  3. $schema = array(
  4. 'table' => 'tripal_gff_temp',
  5. 'fields' => array(
  6. 'feature_id' => array(
  7. 'type' => 'int',
  8. 'not null' => TRUE,
  9. ),
  10. 'organism_id' => array(
  11. 'type' => 'int',
  12. 'not null' => TRUE,
  13. ),
  14. 'uniquename' => array(
  15. 'type' => 'text',
  16. 'not null' => TRUE,
  17. ),
  18. 'type_name' => array(
  19. 'type' => 'varchar',
  20. 'length' => '1024',
  21. 'not null' => TRUE,
  22. ),
  23. ),
  24. 'indexes' => array(
  25. 'tripal_gff_temp_idx0' => array('feature_id'),
  26. 'tripal_gff_temp_idx0' => array('organism_id'),
  27. 'tripal_gff_temp_idx1' => array('uniquename'),
  28. ),
  29. 'unique keys' => array(
  30. 'tripal_gff_temp_uq0' => array('feature_id'),
  31. 'tripal_gff_temp_uq1' => array('uniquename', 'organism_id', 'type_name'),
  32. ),
  33. );
  34. chado_create_custom_table('tripal_gff_temp', $schema, TRUE);
  35. }
  36. /**
  37. *
  38. */
  39. function tripal_chado_add_tripal_gffcds_temp_table() {
  40. $schema = array(
  41. 'table' => 'tripal_gffcds_temp',
  42. 'fields' => array(
  43. 'feature_id' => array(
  44. 'type' => 'int',
  45. 'not null' => TRUE,
  46. ),
  47. 'parent_id' => array(
  48. 'type' => 'int',
  49. 'not null' => TRUE,
  50. ),
  51. 'phase' => array(
  52. 'type' => 'int',
  53. 'not null' => FALSE,
  54. ),
  55. 'strand' => array(
  56. 'type' => 'int',
  57. 'not null' => TRUE,
  58. ),
  59. 'fmin' => array(
  60. 'type' => 'int',
  61. 'not null' => TRUE,
  62. ),
  63. 'fmax' => array(
  64. 'type' => 'int',
  65. 'not null' => TRUE,
  66. ),
  67. ),
  68. 'indexes' => array(
  69. 'tripal_gff_temp_idx0' => array('feature_id'),
  70. 'tripal_gff_temp_idx0' => array('parent_id'),
  71. ),
  72. );
  73. chado_create_custom_table('tripal_gffcds_temp', $schema);
  74. }
  75. /**
  76. *
  77. */
  78. function tripal_chado_add_tripal_gffprotein_temp_table() {
  79. $schema = array(
  80. 'table' => 'tripal_gffprotein_temp',
  81. 'fields' => array(
  82. 'feature_id' => array(
  83. 'type' => 'int',
  84. 'not null' => TRUE,
  85. ),
  86. 'parent_id' => array(
  87. 'type' => 'int',
  88. 'not null' => TRUE,
  89. ),
  90. 'fmin' => array(
  91. 'type' => 'int',
  92. 'not null' => TRUE,
  93. ),
  94. 'fmax' => array(
  95. 'type' => 'int',
  96. 'not null' => TRUE,
  97. ),
  98. ),
  99. 'indexes' => array(
  100. 'tripal_gff_temp_idx0' => array('feature_id'),
  101. 'tripal_gff_temp_idx0' => array('parent_id'),
  102. ),
  103. 'unique keys' => array(
  104. 'tripal_gff_temp_uq0' => array('feature_id'),
  105. ),
  106. );
  107. chado_create_custom_table('tripal_gffprotein_temp', $schema, TRUE);
  108. }
  109. /**
  110. * Creates a temporary table to store obo details while loading an obo file
  111. *
  112. * @ingroup tripal_cv
  113. */
  114. function tripal_chado_add_tripal_obo_temp_table() {
  115. // the tripal_obo_temp table is used for temporary housing of records when loading OBO files
  116. // we create it here using plain SQL because we want it to be in the chado schema but we
  117. // do not want to use the Tripal Custom Table API because we don't want it to appear in the
  118. // list of custom tables. It needs to be available for the Tripal Chado API so we create it
  119. // here and then define it in the tripal_cv/api/tripal_cv.schema.api.inc
  120. if (!chado_table_exists('tripal_obo_temp')) {
  121. $sql = "
  122. CREATE TABLE {tripal_obo_temp} (
  123. id character varying(255) NOT NULL,
  124. stanza text NOT NULL,
  125. type character varying(50) NOT NULL,
  126. CONSTRAINT tripal_obo_temp_uq0 UNIQUE (id)
  127. );
  128. ";
  129. chado_query($sql);
  130. $sql = "CREATE INDEX tripal_obo_temp_idx0 ON {tripal_obo_temp} USING btree (id)";
  131. chado_query($sql);
  132. $sql = "CREATE INDEX tripal_obo_temp_idx1 ON {tripal_obo_temp} USING btree (type)";
  133. chado_query($sql);
  134. }
  135. }
  136. /**
  137. * Creates a materialized view that stores the type & number of stocks per organism
  138. *
  139. * @ingroup tripal_stock
  140. */
  141. function tripal_chado_add_organism_stock_count_mview() {
  142. $view_name = 'organism_stock_count';
  143. $comment = 'Stores the type and number of stocks per organism';
  144. $schema = array(
  145. 'description' => $comment,
  146. 'table' => $view_name,
  147. 'fields' => array(
  148. 'organism_id' => array(
  149. 'size' => 'big',
  150. 'type' => 'int',
  151. 'not null' => TRUE,
  152. ),
  153. 'genus' => array(
  154. 'type' => 'varchar',
  155. 'length' => '255',
  156. 'not null' => TRUE,
  157. ),
  158. 'species' => array(
  159. 'type' => 'varchar',
  160. 'length' => '255',
  161. 'not null' => TRUE,
  162. ),
  163. 'common_name' => array(
  164. 'type' => 'varchar',
  165. 'length' => '255',
  166. 'not null' => FALSE,
  167. ),
  168. 'num_stocks' => array(
  169. 'type' => 'int',
  170. 'not null' => TRUE,
  171. ),
  172. 'cvterm_id' => array(
  173. 'size' => 'big',
  174. 'type' => 'int',
  175. 'not null' => TRUE,
  176. ),
  177. 'stock_type' => array(
  178. 'type' => 'varchar',
  179. 'length' => '255',
  180. 'not null' => TRUE,
  181. ),
  182. ),
  183. 'indexes' => array(
  184. 'organism_stock_count_idx1' => array('organism_id'),
  185. 'organism_stock_count_idx2' => array('cvterm_id'),
  186. 'organism_stock_count_idx3' => array('stock_type'),
  187. ),
  188. );
  189. $sql = "
  190. SELECT
  191. O.organism_id, O.genus, O.species, O.common_name,
  192. count(S.stock_id) as num_stocks,
  193. CVT.cvterm_id, CVT.name as stock_type
  194. FROM organism O
  195. INNER JOIN stock S ON O.Organism_id = S.organism_id
  196. INNER JOIN cvterm CVT ON S.type_id = CVT.cvterm_id
  197. GROUP BY
  198. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  199. ";
  200. tripal_add_mview($view_name, 'tripal_stock', $schema, $sql, $comment);
  201. }
  202. /**
  203. * Adds a materialized view keeping track of the type of features associated with each library
  204. *
  205. * @ingroup tripal_library
  206. */
  207. function tripal_chado_add_library_feature_count_mview(){
  208. $view_name = 'library_feature_count';
  209. $comment = 'Provides count of feature by type that are associated with all libraries';
  210. $schema = array(
  211. 'table' => $view_name,
  212. 'description' => $comment,
  213. 'fields' => array(
  214. 'library_id' => array(
  215. 'size' => 'big',
  216. 'type' => 'int',
  217. 'not null' => TRUE,
  218. ),
  219. 'name' => array(
  220. 'type' => 'varchar',
  221. 'length' => 255,
  222. 'not null' => TRUE,
  223. ),
  224. 'num_features' => array(
  225. 'type' => 'int',
  226. 'not null' => TRUE,
  227. ),
  228. 'feature_type' => array(
  229. 'type' => 'varchar',
  230. 'length' => 255,
  231. 'not null' => TRUE,
  232. ),
  233. ),
  234. 'indexes' => array(
  235. 'library_feature_count_idx1' => array('library_id'),
  236. ),
  237. );
  238. $sql = "
  239. SELECT
  240. L.library_id, L.name,
  241. count(F.feature_id) as num_features,
  242. CVT.name as feature_type
  243. FROM library L
  244. INNER JOIN library_feature LF ON LF.library_id = L.library_id
  245. INNER JOIN feature F ON LF.feature_id = F.feature_id
  246. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  247. GROUP BY L.library_id, L.name, CVT.name
  248. ";
  249. tripal_add_mview($view_name, 'tripal_library', $schema, $sql, $comment);
  250. }
  251. /**
  252. *
  253. */
  254. /**
  255. * Creates a materialized view that stores the type & number of features per organism
  256. *
  257. * @ingroup tripal_feature
  258. */
  259. function tripal_chado_add_organism_feature_count_mview() {
  260. $view_name = 'organism_feature_count';
  261. $comment = 'Stores the type and number of features per organism';
  262. $schema = array(
  263. 'description' => $comment,
  264. 'table' => $view_name,
  265. 'fields' => array(
  266. 'organism_id' => array(
  267. 'size' => 'big',
  268. 'type' => 'int',
  269. 'not null' => TRUE,
  270. ),
  271. 'genus' => array(
  272. 'type' => 'varchar',
  273. 'length' => '255',
  274. 'not null' => TRUE,
  275. ),
  276. 'species' => array(
  277. 'type' => 'varchar',
  278. 'length' => '255',
  279. 'not null' => TRUE,
  280. ),
  281. 'common_name' => array(
  282. 'type' => 'varchar',
  283. 'length' => '255',
  284. 'not null' => FALSE,
  285. ),
  286. 'num_features' => array(
  287. 'type' => 'int',
  288. 'not null' => TRUE,
  289. ),
  290. 'cvterm_id' => array(
  291. 'size' => 'big',
  292. 'type' => 'int',
  293. 'not null' => TRUE,
  294. ),
  295. 'feature_type' => array(
  296. 'type' => 'varchar',
  297. 'length' => '255',
  298. 'not null' => TRUE,
  299. ),
  300. ),
  301. 'indexes' => array(
  302. 'organism_feature_count_idx1' => array('organism_id'),
  303. 'organism_feature_count_idx2' => array('cvterm_id'),
  304. 'organism_feature_count_idx3' => array('feature_type'),
  305. ),
  306. );
  307. $sql = "
  308. SELECT
  309. O.organism_id, O.genus, O.species, O.common_name,
  310. count(F.feature_id) as num_features,
  311. CVT.cvterm_id, CVT.name as feature_type
  312. FROM organism O
  313. INNER JOIN feature F ON O.Organism_id = F.organism_id
  314. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  315. GROUP BY
  316. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  317. ";
  318. tripal_add_mview($view_name, 'tripal_feature', $schema, $sql, $comment);
  319. }
  320. /**
  321. * Creates a view showing the link between an organism & it's analysis through associated features.
  322. *
  323. * @ingroup tripal_analysis
  324. */
  325. function tripal_chado_add_analysis_organism_mview() {
  326. $view_name = 'analysis_organism';
  327. $comment = t('This view is for associating an organism (via it\'s associated features) to an analysis.');
  328. // this is the SQL used to identify the organism to which an analsysis
  329. // has been used. This is obtained though the analysisfeature -> feature -> organism
  330. // joins
  331. $sql = "
  332. SELECT DISTINCT A.analysis_id, O.organism_id
  333. FROM analysis A
  334. INNER JOIN analysisfeature AF ON A.analysis_id = AF.analysis_id
  335. INNER JOIN feature F ON AF.feature_id = F.feature_id
  336. INNER JOIN organism O ON O.organism_id = F.organism_id
  337. ";
  338. // the schema array for describing this view
  339. $schema = array(
  340. 'table' => $view_name,
  341. 'description' => $comment,
  342. 'fields' => array(
  343. 'analysis_id' => array(
  344. 'size' => 'big',
  345. 'type' => 'int',
  346. 'not null' => TRUE,
  347. ),
  348. 'organism_id' => array(
  349. 'size' => 'big',
  350. 'type' => 'int',
  351. 'not null' => TRUE,
  352. ),
  353. ),
  354. 'indexes' => array(
  355. 'networkmod_qtl_indx0' => array('analysis_id'),
  356. 'networkmod_qtl_indx1' => array('organism_id'),
  357. ),
  358. 'foreign keys' => array(
  359. 'analysis' => array(
  360. 'table' => 'analysis',
  361. 'columns' => array(
  362. 'analysis_id' => 'analysis_id',
  363. ),
  364. ),
  365. 'organism' => array(
  366. 'table' => 'organism',
  367. 'columns' => array(
  368. 'organism_id' => 'organism_id',
  369. ),
  370. ),
  371. ),
  372. );
  373. // add the view
  374. tripal_add_mview($view_name, 'tripal_analysis', $schema, $sql, $comment);
  375. }
  376. /**
  377. * Add a materialized view of root terms for all chado cvs. This is needed for viewing cv trees
  378. *
  379. * @ingroup tripal_cv
  380. */
  381. function tripal_chado_add_cv_root_mview_mview() {
  382. $mv_name = 'cv_root_mview';
  383. $comment = 'A list of the root terms for all controlled vocabularies. This is needed for viewing CV trees';
  384. $schema = array(
  385. 'table' => $mv_name,
  386. 'description' => $comment,
  387. 'fields' => array(
  388. 'name' => array(
  389. 'type' => 'varchar',
  390. 'length' => 255,
  391. 'not null' => TRUE,
  392. ),
  393. 'cvterm_id' => array(
  394. 'size' => 'big',
  395. 'type' => 'int',
  396. 'not null' => TRUE,
  397. ),
  398. 'cv_id' => array(
  399. 'size' => 'big',
  400. 'type' => 'int',
  401. 'not null' => TRUE,
  402. ),
  403. 'cv_name' => array(
  404. 'type' => 'varchar',
  405. 'length' => 255,
  406. 'not null' => TRUE,
  407. ),
  408. ),
  409. 'indexes' => array(
  410. 'cv_root_mview_indx1' => array('cvterm_id'),
  411. 'cv_root_mview_indx2' => array('cv_id'),
  412. ),
  413. );
  414. $sql = "
  415. SELECT DISTINCT CVT.name,CVT.cvterm_id, CV.cv_id, CV.name
  416. FROM cvterm_relationship CVTR
  417. INNER JOIN cvterm CVT on CVTR.object_id = CVT.cvterm_id
  418. INNER JOIN cv CV on CV.cv_id = CVT.cv_id
  419. WHERE CVTR.object_id not in
  420. (SELECT subject_id FROM cvterm_relationship)
  421. ";
  422. // Create the MView
  423. tripal_add_mview($mv_name, 'tripal_cv', $schema, $sql, $comment);
  424. }