tripal_chado.install 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. <?php
  2. function tripal_chado_install() {
  3. // // The foreign key specification doesn't really add one to the
  4. // // Drupal schema, it is just used internally, but we want one.
  5. // db_query('
  6. // ALTER TABLE {tripal_custom_tables}
  7. // ADD CONSTRAINT tripal_custom_tables_fk1
  8. // FOREIGN KEY (mview_id) REFERENCES {tripal_mviews} (mview_id)
  9. // ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  10. // ');
  11. }
  12. /**
  13. * Implementation of hook_uninstall().
  14. *
  15. * @ingroup tripal
  16. */
  17. function tripal_chado_uninstall() {
  18. // // Drop the foreign key between tripal_custom_tables and tripal_mviews
  19. // // so that Drupal can then drop the tables
  20. // db_query('
  21. // ALTER TABLE {tripal_custom_tables}
  22. // DROP CONSTRAINT tripal_custom_tables_fk1 CASCADE
  23. // ');
  24. }
  25. /**
  26. * Table definition for the tripal_cv_obo table
  27. * @param $schema
  28. */
  29. function tripal_chado_tripal_cv_obo_schema() {
  30. return array(
  31. 'fields' => array(
  32. 'obo_id' => array(
  33. 'type' => 'serial',
  34. 'unsigned' => TRUE,
  35. 'not null' => TRUE
  36. ),
  37. 'name' => array(
  38. 'type' => 'varchar',
  39. 'length' => 255
  40. ),
  41. 'path' => array(
  42. 'type' => 'varchar',
  43. 'length' => 1024
  44. ),
  45. ),
  46. 'indexes' => array(
  47. 'tripal_cv_obo_idx1' => array('obo_id'),
  48. ),
  49. 'primary key' => array('obo_id'),
  50. );
  51. }
  52. /**
  53. * * Table definition for the tripal_cv_defaults table
  54. * @param unknown $schema
  55. */
  56. function tripal_chado_tripal_cv_defaults_schema() {
  57. return array(
  58. 'fields' => array(
  59. 'cv_default_id' => array(
  60. 'type' => 'serial',
  61. 'unsigned' => TRUE,
  62. 'not null' => TRUE
  63. ),
  64. 'table_name' => array(
  65. 'type' => 'varchar',
  66. 'length' => 128,
  67. 'not null' => TRUE,
  68. ),
  69. 'field_name' => array(
  70. 'type' => 'varchar',
  71. 'length' => 128,
  72. 'not null' => TRUE,
  73. ),
  74. 'cv_id' => array(
  75. 'type' => 'int',
  76. 'not null' => TRUE,
  77. )
  78. ),
  79. 'indexes' => array(
  80. 'tripal_cv_defaults_idx1' => array('table_name', 'field_name'),
  81. ),
  82. 'unique keys' => array(
  83. 'tripal_cv_defaults_unq1' => array('table_name', 'field_name', 'cv_id'),
  84. ),
  85. 'primary key' => array('cv_default_id')
  86. );
  87. }
  88. /**
  89. *
  90. */
  91. function tripal_chado_enable() {
  92. // If Tripal v2 is already installed, the installation of this module
  93. // will try and recreate some of the tables created with tripal_core and the
  94. // installation will fail. Therefore, in the install we renamed it. Now
  95. // we want to move it back.
  96. if (db_table_exists('tripal_mviews2')) {
  97. // tripal_mviews
  98. $sql = "DROP TABLE tripal_mviews";
  99. db_query($sql);
  100. $sql = "ALTER TABLE tripal_mviews2 RENAME to tripal_mviews";
  101. db_query($sql);
  102. $sql = "ALTER INDEX tripal_mviews_mv_name_key2 RENAME TO tripal_mviews_mv_name_key";
  103. db_query($sql);
  104. $sql = "ALTER INDEX tripal_mviews_mv_table_key2 RENAME TO tripal_mviews_mv_table_key";
  105. db_query($sql);
  106. $sql = "ALTER INDEX tripal_mviews_mview_id_idx2 RENAME TO tripal_mviews_mview_id_idx";
  107. db_query($sql);
  108. $sql = "ALTER INDEX tripal_mviews_pkey2 RENAME TO tripal_mviews_pkey";
  109. db_query($sql);
  110. }
  111. // tripal_custom_tables
  112. if (db_table_exists('tripal_custom_tables2')) {
  113. $sql = "DROP TABLE tripal_custom_tables";
  114. db_query($sql);
  115. $sql = "ALTER TABLE tripal_custom_tables2 RENAME to tripal_custom_tables";
  116. db_query($sql);
  117. $sql = "ALTER INDEX tripal_custom_tables_pkey2 RENAME TO tripal_custom_tables_pkey";
  118. db_query($sql);
  119. $sql = "ALTER INDEX tripal_custom_tables_table_id_idx2 RENAME TO tripal_custom_tables_table_id_idx";
  120. db_query($sql);
  121. }
  122. // tripal_cv_obo
  123. if (db_table_exists('tripal_cv_obo2')) {
  124. $sql = "DROP TABLE tripal_cv_obo";
  125. db_query($sql);
  126. $sql = "ALTER TABLE tripal_cv_obo2 RENAME to tripal_cv_obo";
  127. db_query($sql);
  128. $sql = "ALTER INDEX tripal_cv_obo_obo_id_idx2 RENAME TO tripal_cv_obo_obo_id_idx";
  129. db_query($sql);
  130. $sql = "ALTER INDEX tripal_cv_obo_pkey2 RENAME TO tripal_cv_obo_pkey";
  131. db_query($sql);
  132. }
  133. // tripal_cv_defaults
  134. if (db_table_exists('tripal_cv_defaults2')) {
  135. $sql = "DROP TABLE tripal_cv_defaults";
  136. db_query($sql);
  137. $sql = "ALTER TABLE tripal_cv_defaults2 RENAME to tripal_cv_defaults";
  138. db_query($sql);
  139. $sql = "ALTER INDEX tripal_cv_defaults_pkey2 RENAME TO tripal_cv_defaults_pkey";
  140. db_query($sql);
  141. $sql = "ALTER INDEX tripal_cv_defaults_tripal_cv_defaults_idx1_idx2 RENAME TO tripal_cv_defaults_tripal_cv_defaults_idx1_idx";
  142. db_query($sql);
  143. $sql = "ALTER INDEX tripal_cv_defaults_tripal_cv_defaults_unq1_key2 RENAME TO tripal_cv_defaults_tripal_cv_defaults_unq1_key";
  144. db_query($sql);
  145. }
  146. // tripal_pub_import
  147. if (db_table_exists('tripal_pub_import2')) {
  148. $sql = "DROP TABLE tripal_pub_import";
  149. db_query($sql);
  150. $sql = "ALTER TABLE tripal_pub_import2 RENAME to tripal_pub_import";
  151. db_query($sql);
  152. $sql = "ALTER INDEX tripal_pub_import_name_idx2 RENAME TO tripal_pub_import_name_idx";
  153. db_query($sql);
  154. $sql = "ALTER INDEX tripal_pub_import_pkey2 RENAME TO tripal_pub_import_pkey";
  155. db_query($sql);
  156. }
  157. }
  158. /**
  159. * Implements hook_schema().
  160. */
  161. function tripal_chado_schema() {
  162. if (db_table_exists('tripal_mviews')) {
  163. // Move the tripal_mviews table out of the way.
  164. $sql = "ALTER TABLE tripal_mviews RENAME TO tripal_mviews2";
  165. db_query($sql);
  166. $sql = "ALTER INDEX tripal_mviews_mv_name_key RENAME TO tripal_mviews_mv_name_key2";
  167. db_query($sql);
  168. $sql = "ALTER INDEX tripal_mviews_mv_table_key RENAME TO tripal_mviews_mv_table_key2";
  169. db_query($sql);
  170. $sql = "ALTER INDEX tripal_mviews_mview_id_idx RENAME TO tripal_mviews_mview_id_idx2";
  171. db_query($sql);
  172. $sql = "ALTER INDEX tripal_mviews_pkey RENAME TO tripal_mviews_pkey2";
  173. db_query($sql);
  174. }
  175. if (db_table_exists('tripal_custom_tables')) {
  176. // Move the tripal_custom_tables table out of the way.
  177. $sql = "ALTER TABLE tripal_custom_tables RENAME TO tripal_custom_tables2";
  178. db_query($sql);
  179. $sql = "ALTER INDEX tripal_custom_tables_pkey RENAME TO tripal_custom_tables_pkey2";
  180. db_query($sql);
  181. $sql = "ALTER INDEX tripal_custom_tables_table_id_idx RENAME TO tripal_custom_tables_table_id_idx2";
  182. db_query($sql);
  183. }
  184. if (db_table_exists('tripal_cv_obo')) {
  185. // Move the tripal_cv_obo table out of the way.
  186. $sql = "ALTER TABLE tripal_cv_obo RENAME TO tripal_cv_obo2";
  187. db_query($sql);
  188. $sql = "ALTER INDEX tripal_cv_obo_obo_id_idx RENAME TO tripal_cv_obo_obo_id_idx2";
  189. db_query($sql);
  190. $sql = "ALTER INDEX tripal_cv_obo_pkey RENAME TO tripal_cv_obo_pkey2";
  191. db_query($sql);
  192. }
  193. if (db_table_exists('tripal_cv_defaults')) {
  194. // Move the tripal_cv_defaults table out of the way.
  195. $sql = "ALTER TABLE tripal_cv_defaults RENAME TO tripal_cv_defaults2";
  196. db_query($sql);
  197. $sql = "ALTER INDEX tripal_cv_defaults_pkey RENAME TO tripal_cv_defaults_pkey2";
  198. db_query($sql);
  199. $sql = "ALTER INDEX tripal_cv_defaults_tripal_cv_defaults_idx1_idx RENAME TO tripal_cv_defaults_tripal_cv_defaults_idx1_idx2";
  200. db_query($sql);
  201. $sql = "ALTER INDEX tripal_cv_defaults_tripal_cv_defaults_unq1_key RENAME TO tripal_cv_defaults_tripal_cv_defaults_unq1_key2";
  202. db_query($sql);
  203. }
  204. if (db_table_exists('tripal_pub_import')) {
  205. // Move the tripal_pub_import table out of the way.
  206. $sql = "ALTER TABLE tripal_pub_import RENAME TO tripal_pub_import2";
  207. db_query($sql);
  208. $sql = "ALTER INDEX tripal_pub_import_name_idx RENAME TO tripal_pub_import_name_idx2";
  209. db_query($sql);
  210. $sql = "ALTER INDEX tripal_pub_import_pkey RENAME TO tripal_pub_import_pkey2";
  211. db_query($sql);
  212. }
  213. // Links TripalEntity entities to the chado record.
  214. $schema['chado_entity'] = tripal_chado_chado_entity_schema();
  215. $schema['tripal_mviews'] = tripal_chado_tripal_mviews_schema();
  216. $schema['tripal_custom_tables'] = tripal_chado_tripal_custom_tables_schema();
  217. $schema['tripal_cv_obo'] = tripal_chado_tripal_cv_obo_schema();
  218. $schema['tripal_cv_defaults'] = tripal_chado_tripal_cv_defaults_schema();
  219. $schema['tripal_pub_import'] = tripal_chado_tripal_pub_import_schema();
  220. // if this module is already installed and enabled, then we want to provide
  221. // the schemas for all of the custom tables. This will allow Views to
  222. // see the schemas. We check if the module is installed because during
  223. // installation we don't want to make these custom tables available as we don't
  224. // want them created in the Drupal database. The custom tables go in the
  225. // Chado database.
  226. if (db_table_exists('tripal_custom_tables')) {
  227. $sql = 'SELECT * FROM {tripal_custom_tables}';
  228. $results = db_query($sql);
  229. foreach ($results as $custom) {
  230. $schema[$custom->table_name] = unserialize($custom->schema);
  231. }
  232. }
  233. return $schema;
  234. }
  235. /**
  236. * @section
  237. * Schema Definitions.
  238. */
  239. /**
  240. * Implementation of hook_schema().
  241. *
  242. * @ingroup tripal_pub
  243. */
  244. function tripal_chado_tripal_pub_import_schema() {
  245. return array(
  246. 'fields' => array(
  247. 'pub_import_id' => array(
  248. 'type' => 'serial',
  249. 'not null' => TRUE
  250. ),
  251. 'name' => array(
  252. 'type' => 'varchar',
  253. 'length' => 255,
  254. 'not null' => TRUE
  255. ),
  256. 'criteria' => array(
  257. 'type' => 'text',
  258. 'size' => 'normal',
  259. 'not null' => TRUE,
  260. 'description' => 'Contains a serialized PHP array containing the search criteria'
  261. ),
  262. 'disabled' => array(
  263. 'type' => 'int',
  264. 'unsigned' => TRUE,
  265. 'not NULL' => TRUE,
  266. 'default' => 0
  267. ),
  268. 'do_contact' => array(
  269. 'type' => 'int',
  270. 'unsigned' => TRUE,
  271. 'not NULL' => TRUE,
  272. 'default' => 0
  273. ),
  274. ),
  275. 'primary key' => array('pub_import_id'),
  276. 'indexes' => array(
  277. 'name' => array('name')
  278. ),
  279. );
  280. }
  281. /**
  282. * Describes the Tripal Custom Tables (tripal_custom_tables) table
  283. * This keeps track of tables created by Tripal and stored in chado that may or may not
  284. * also be materialized views.
  285. *
  286. * @ingroup tripal
  287. */
  288. function tripal_chado_tripal_custom_tables_schema() {
  289. return array(
  290. 'fields' => array(
  291. 'table_id' => array(
  292. 'type' => 'serial',
  293. 'unsigned' => TRUE,
  294. 'not NULL' => TRUE
  295. ),
  296. 'table_name' => array(
  297. 'type' => 'varchar',
  298. 'length' => 255,
  299. 'not NULL' => TRUE
  300. ),
  301. 'schema' => array(
  302. 'type' => 'text',
  303. 'not NULL' => TRUE
  304. ),
  305. 'mview_id' => array(
  306. 'type' => 'int',
  307. 'not NULL' => FALSE
  308. )
  309. ),
  310. 'indexes' => array(
  311. 'table_id' => array('table_id'),
  312. ),
  313. 'primary key' => array('table_id'),
  314. 'foreign keys' => array(
  315. 'tripal_mviews' => array(
  316. 'table' => 'tripal_mviews',
  317. 'columns' => array(
  318. 'mview_id' => 'mview_id'
  319. ),
  320. ),
  321. ),
  322. );
  323. }
  324. /**
  325. * Describes the Tripal Materialized View (tripal_mviews) table
  326. * This table keeps track of all materialized views created by Tripal and stored in chado
  327. *
  328. * @ingroup tripal
  329. */
  330. function tripal_chado_tripal_mviews_schema() {
  331. return array(
  332. 'fields' => array(
  333. 'mview_id' => array(
  334. 'type' => 'serial',
  335. 'unsigned' => TRUE,
  336. 'not NULL' => TRUE
  337. ),
  338. 'name' => array(
  339. 'type' => 'varchar',
  340. 'length' => 255,
  341. 'not NULL' => TRUE
  342. ),
  343. 'modulename' => array(
  344. 'type' => 'varchar',
  345. 'length' => 50,
  346. 'not NULL' => TRUE,
  347. 'description' => 'The module name that provides the callback for this job'
  348. ),
  349. 'mv_table' => array(
  350. 'type' => 'varchar',
  351. 'length' => 128,
  352. 'not NULL' => FALSE
  353. ),
  354. 'mv_specs' => array(
  355. 'type' => 'text',
  356. 'size' => 'normal',
  357. 'not NULL' => FALSE
  358. ),
  359. 'mv_schema' => array(
  360. 'type' => 'text',
  361. 'size' => 'normal',
  362. 'not NULL' => FALSE
  363. ),
  364. 'indexed' => array(
  365. 'type' => 'text',
  366. 'size' => 'normal',
  367. 'not NULL' => FALSE
  368. ),
  369. 'query' => array(
  370. 'type' => 'text',
  371. 'size' => 'normal',
  372. 'not NULL' => TRUE
  373. ),
  374. 'special_index' => array(
  375. 'type' => 'text',
  376. 'size' => 'normal',
  377. 'not NULL' => FALSE
  378. ),
  379. 'last_update' => array(
  380. 'type' => 'int',
  381. 'not NULL' => FALSE,
  382. 'description' => 'UNIX integer time'
  383. ),
  384. 'status' => array(
  385. 'type' => 'text',
  386. 'size' => 'normal',
  387. 'not NULL' => FALSE
  388. ),
  389. 'comment' => array(
  390. 'type' => 'text',
  391. 'size' => 'normal',
  392. 'not NULL' => FALSE
  393. ),
  394. ),
  395. 'indexes' => array(
  396. 'mview_id' => array('mview_id')
  397. ),
  398. 'unique keys' => array(
  399. 'mv_table' => array('mv_table'),
  400. 'mv_name' => array('name'),
  401. ),
  402. 'primary key' => array('mview_id'),
  403. );
  404. }
  405. /**
  406. * Links Biological Data Entities to the chado "base" table the data is stored in.
  407. * This is where we would specify that a particular gene maps to the record in the
  408. * chado.feature table with a feature_id=2432;
  409. */
  410. function tripal_chado_chado_entity_schema() {
  411. $schema = array(
  412. 'description' => 'The linker table that associates an enitity from the public.tripal_entity table with a "base" record in Chado',
  413. 'fields' => array(
  414. 'chado_entity_id' => array(
  415. 'description' => 'The primary identifier for this table.',
  416. 'type' => 'serial',
  417. 'unsigned' => TRUE,
  418. 'not null' => TRUE,
  419. ),
  420. 'entity_id' => array(
  421. 'description' => 'The unique entity id.',
  422. 'type' => 'int',
  423. 'not null' => TRUE,
  424. ),
  425. 'record_id' => array(
  426. 'description' => 'The unique numerical identifier for the record that this entity is associated with (e.g. feature_id, stock_id, library_id, etc.).',
  427. 'type' => 'int',
  428. 'not null' => TRUE,
  429. ),
  430. 'data_table' => array(
  431. 'description' => 'Indicates the table in Chado that this term services (e.g. feature, stock, library, etc.)',
  432. 'type' => 'varchar',
  433. 'length' => 128,
  434. 'not null' => TRUE,
  435. 'default' => '',
  436. ),
  437. 'type_table' => array(
  438. 'description' => 'Sometimes the record in the data table doesn’t have a field that specifies the record type. For example, an analysis type is stored in the analysisprop table. If the data_table does have a type field then this value will be the same as the data_table.',
  439. 'type' => 'varchar',
  440. 'length' => 128,
  441. 'not null' => TRUE,
  442. 'default' => '',
  443. ),
  444. 'field' => array(
  445. 'description' => 'The name of the field in the typetable that contains the cvterm record.',
  446. 'type' => 'varchar',
  447. 'length' => 128,
  448. 'not null' => FALSE,
  449. 'default' => ''
  450. ),
  451. ),
  452. 'indexes' => array(
  453. 'record_id' => array('record_id'),
  454. 'entity_id' => array('entity_id'),
  455. 'data_table' => array('data_table'),
  456. ),
  457. 'unique keys' => array(
  458. 'record' => array('data_table', 'record_id'),
  459. 'entity_id' => array('entity_id'),
  460. ),
  461. 'primary key' => array('chado_entity_id'),
  462. );
  463. return $schema;
  464. }