tripal_chado.install 16 KB

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