tripal_chado.chado_vx_x.inc 13 KB

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