tripal_chado.install 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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 Tripal v2 is already installed, the installation of this module
  163. // will try and recreate some of the tables created with tripal_core and the
  164. // installation will fail. Therefore, we need to temporarily move those
  165. // tables out of the way, let the module install and then move them back.
  166. $migrated = variable_get ('tripal_v2_upgrade_v3_check_chado', FALSE);
  167. if (!$migrated) {
  168. if (db_table_exists('tripal_mviews')) {
  169. // Move the tripal_mviews table out of the way.
  170. $sql = "ALTER TABLE tripal_mviews RENAME TO tripal_mviews2";
  171. db_query($sql);
  172. $sql = "ALTER INDEX tripal_mviews_mv_name_key RENAME TO tripal_mviews_mv_name_key2";
  173. db_query($sql);
  174. $sql = "ALTER INDEX tripal_mviews_mv_table_key RENAME TO tripal_mviews_mv_table_key2";
  175. db_query($sql);
  176. $sql = "ALTER INDEX tripal_mviews_mview_id_idx RENAME TO tripal_mviews_mview_id_idx2";
  177. db_query($sql);
  178. $sql = "ALTER INDEX tripal_mviews_pkey RENAME TO tripal_mviews_pkey2";
  179. db_query($sql);
  180. }
  181. if (db_table_exists('tripal_custom_tables')) {
  182. // Move the tripal_custom_tables table out of the way.
  183. $sql = "ALTER TABLE tripal_custom_tables RENAME TO tripal_custom_tables2";
  184. db_query($sql);
  185. $sql = "ALTER INDEX tripal_custom_tables_pkey RENAME TO tripal_custom_tables_pkey2";
  186. db_query($sql);
  187. $sql = "ALTER INDEX tripal_custom_tables_table_id_idx RENAME TO tripal_custom_tables_table_id_idx2";
  188. db_query($sql);
  189. }
  190. if (db_table_exists('tripal_cv_obo')) {
  191. // Move the tripal_cv_obo table out of the way.
  192. $sql = "ALTER TABLE tripal_cv_obo RENAME TO tripal_cv_obo2";
  193. db_query($sql);
  194. $sql = "ALTER INDEX tripal_cv_obo_obo_id_idx RENAME TO tripal_cv_obo_obo_id_idx2";
  195. db_query($sql);
  196. $sql = "ALTER INDEX tripal_cv_obo_pkey RENAME TO tripal_cv_obo_pkey2";
  197. db_query($sql);
  198. }
  199. if (db_table_exists('tripal_cv_defaults')) {
  200. // Move the tripal_cv_defaults table out of the way.
  201. $sql = "ALTER TABLE tripal_cv_defaults RENAME TO tripal_cv_defaults2";
  202. db_query($sql);
  203. $sql = "ALTER INDEX tripal_cv_defaults_pkey RENAME TO tripal_cv_defaults_pkey2";
  204. db_query($sql);
  205. $sql = "ALTER INDEX tripal_cv_defaults_tripal_cv_defaults_idx1_idx RENAME TO tripal_cv_defaults_tripal_cv_defaults_idx1_idx2";
  206. db_query($sql);
  207. $sql = "ALTER INDEX tripal_cv_defaults_tripal_cv_defaults_unq1_key RENAME TO tripal_cv_defaults_tripal_cv_defaults_unq1_key2";
  208. db_query($sql);
  209. }
  210. if (db_table_exists('tripal_pub_import')) {
  211. // Move the tripal_pub_import table out of the way.
  212. $sql = "ALTER TABLE tripal_pub_import RENAME TO tripal_pub_import2";
  213. db_query($sql);
  214. $sql = "ALTER INDEX tripal_pub_import_name_idx RENAME TO tripal_pub_import_name_idx2";
  215. db_query($sql);
  216. $sql = "ALTER INDEX tripal_pub_import_pkey RENAME TO tripal_pub_import_pkey2";
  217. db_query($sql);
  218. }
  219. variable_set ('tripal_v2_upgrade_v3_check_chado', TRUE);
  220. }
  221. // Links TripalEntity entities to the chado record.
  222. $schema['chado_entity'] = tripal_chado_chado_entity_schema();
  223. $schema['tripal_mviews'] = tripal_chado_tripal_mviews_schema();
  224. $schema['tripal_custom_tables'] = tripal_chado_tripal_custom_tables_schema();
  225. $schema['tripal_cv_obo'] = tripal_chado_tripal_cv_obo_schema();
  226. $schema['tripal_cv_defaults'] = tripal_chado_tripal_cv_defaults_schema();
  227. $schema['tripal_pub_import'] = tripal_chado_tripal_pub_import_schema();
  228. // if this module is already installed and enabled, then we want to provide
  229. // the schemas for all of the custom tables. This will allow Views to
  230. // see the schemas. We check if the module is installed because during
  231. // installation we don't want to make these custom tables available as we don't
  232. // want them created in the Drupal database. The custom tables go in the
  233. // Chado database.
  234. if (db_table_exists('tripal_custom_tables')) {
  235. $sql = 'SELECT * FROM {tripal_custom_tables}';
  236. $results = db_query($sql);
  237. foreach ($results as $custom) {
  238. $schema[$custom->table_name] = unserialize($custom->schema);
  239. }
  240. }
  241. return $schema;
  242. }
  243. /**
  244. * @section
  245. * Schema Definitions.
  246. */
  247. /**
  248. * Implementation of hook_schema().
  249. *
  250. * @ingroup tripal_pub
  251. */
  252. function tripal_chado_tripal_pub_import_schema() {
  253. return array(
  254. 'fields' => array(
  255. 'pub_import_id' => array(
  256. 'type' => 'serial',
  257. 'not null' => TRUE
  258. ),
  259. 'name' => array(
  260. 'type' => 'varchar',
  261. 'length' => 255,
  262. 'not null' => TRUE
  263. ),
  264. 'criteria' => array(
  265. 'type' => 'text',
  266. 'size' => 'normal',
  267. 'not null' => TRUE,
  268. 'description' => 'Contains a serialized PHP array containing the search criteria'
  269. ),
  270. 'disabled' => array(
  271. 'type' => 'int',
  272. 'unsigned' => TRUE,
  273. 'not NULL' => TRUE,
  274. 'default' => 0
  275. ),
  276. 'do_contact' => array(
  277. 'type' => 'int',
  278. 'unsigned' => TRUE,
  279. 'not NULL' => TRUE,
  280. 'default' => 0
  281. ),
  282. ),
  283. 'primary key' => array('pub_import_id'),
  284. 'indexes' => array(
  285. 'name' => array('name')
  286. ),
  287. );
  288. }
  289. /**
  290. * Describes the Tripal Custom Tables (tripal_custom_tables) table
  291. * This keeps track of tables created by Tripal and stored in chado that may or may not
  292. * also be materialized views.
  293. *
  294. * @ingroup tripal
  295. */
  296. function tripal_chado_tripal_custom_tables_schema() {
  297. return array(
  298. 'fields' => array(
  299. 'table_id' => array(
  300. 'type' => 'serial',
  301. 'unsigned' => TRUE,
  302. 'not NULL' => TRUE
  303. ),
  304. 'table_name' => array(
  305. 'type' => 'varchar',
  306. 'length' => 255,
  307. 'not NULL' => TRUE
  308. ),
  309. 'schema' => array(
  310. 'type' => 'text',
  311. 'not NULL' => TRUE
  312. ),
  313. 'mview_id' => array(
  314. 'type' => 'int',
  315. 'not NULL' => FALSE
  316. )
  317. ),
  318. 'indexes' => array(
  319. 'table_id' => array('table_id'),
  320. ),
  321. 'primary key' => array('table_id'),
  322. 'foreign keys' => array(
  323. 'tripal_mviews' => array(
  324. 'table' => 'tripal_mviews',
  325. 'columns' => array(
  326. 'mview_id' => 'mview_id'
  327. ),
  328. ),
  329. ),
  330. );
  331. }
  332. /**
  333. * Describes the Tripal Materialized View (tripal_mviews) table
  334. * This table keeps track of all materialized views created by Tripal and stored in chado
  335. *
  336. * @ingroup tripal
  337. */
  338. function tripal_chado_tripal_mviews_schema() {
  339. return array(
  340. 'fields' => array(
  341. 'mview_id' => array(
  342. 'type' => 'serial',
  343. 'unsigned' => TRUE,
  344. 'not NULL' => TRUE
  345. ),
  346. 'name' => array(
  347. 'type' => 'varchar',
  348. 'length' => 255,
  349. 'not NULL' => TRUE
  350. ),
  351. 'modulename' => array(
  352. 'type' => 'varchar',
  353. 'length' => 50,
  354. 'not NULL' => TRUE,
  355. 'description' => 'The module name that provides the callback for this job'
  356. ),
  357. 'mv_table' => array(
  358. 'type' => 'varchar',
  359. 'length' => 128,
  360. 'not NULL' => FALSE
  361. ),
  362. 'mv_specs' => array(
  363. 'type' => 'text',
  364. 'size' => 'normal',
  365. 'not NULL' => FALSE
  366. ),
  367. 'mv_schema' => array(
  368. 'type' => 'text',
  369. 'size' => 'normal',
  370. 'not NULL' => FALSE
  371. ),
  372. 'indexed' => array(
  373. 'type' => 'text',
  374. 'size' => 'normal',
  375. 'not NULL' => FALSE
  376. ),
  377. 'query' => array(
  378. 'type' => 'text',
  379. 'size' => 'normal',
  380. 'not NULL' => TRUE
  381. ),
  382. 'special_index' => array(
  383. 'type' => 'text',
  384. 'size' => 'normal',
  385. 'not NULL' => FALSE
  386. ),
  387. 'last_update' => array(
  388. 'type' => 'int',
  389. 'not NULL' => FALSE,
  390. 'description' => 'UNIX integer time'
  391. ),
  392. 'status' => array(
  393. 'type' => 'text',
  394. 'size' => 'normal',
  395. 'not NULL' => FALSE
  396. ),
  397. 'comment' => array(
  398. 'type' => 'text',
  399. 'size' => 'normal',
  400. 'not NULL' => FALSE
  401. ),
  402. ),
  403. 'indexes' => array(
  404. 'mview_id' => array('mview_id')
  405. ),
  406. 'unique keys' => array(
  407. 'mv_table' => array('mv_table'),
  408. 'mv_name' => array('name'),
  409. ),
  410. 'primary key' => array('mview_id'),
  411. );
  412. }
  413. /**
  414. * Links Biological Data Entities to the chado "base" table the data is stored in.
  415. * This is where we would specify that a particular gene maps to the record in the
  416. * chado.feature table with a feature_id=2432;
  417. */
  418. function tripal_chado_chado_entity_schema() {
  419. $schema = array(
  420. 'description' => 'The linker table that associates an enitity from the public.tripal_entity table with a "base" record in Chado',
  421. 'fields' => array(
  422. 'chado_entity_id' => array(
  423. 'description' => 'The primary identifier for this table.',
  424. 'type' => 'serial',
  425. 'unsigned' => TRUE,
  426. 'not null' => TRUE,
  427. ),
  428. 'entity_id' => array(
  429. 'description' => 'The unique entity id.',
  430. 'type' => 'int',
  431. 'not null' => TRUE,
  432. ),
  433. 'record_id' => array(
  434. 'description' => 'The unique numerical identifier for the record that this entity is associated with (e.g. feature_id, stock_id, library_id, etc.).',
  435. 'type' => 'int',
  436. 'not null' => TRUE,
  437. ),
  438. 'data_table' => array(
  439. 'description' => 'Indicates the table in Chado that this term services (e.g. feature, stock, library, etc.)',
  440. 'type' => 'varchar',
  441. 'length' => 128,
  442. 'not null' => TRUE,
  443. 'default' => '',
  444. ),
  445. 'type_table' => array(
  446. '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.',
  447. 'type' => 'varchar',
  448. 'length' => 128,
  449. 'not null' => TRUE,
  450. 'default' => '',
  451. ),
  452. 'field' => array(
  453. 'description' => 'The name of the field in the typetable that contains the cvterm record.',
  454. 'type' => 'varchar',
  455. 'length' => 128,
  456. 'not null' => FALSE,
  457. 'default' => ''
  458. ),
  459. 'nid' => array(
  460. 'description' => 'Optional. For linking nid to the entity when migrating Tripal v2 content',
  461. 'type' => 'int',
  462. )
  463. ),
  464. 'indexes' => array(
  465. 'record_id' => array('record_id'),
  466. 'entity_id' => array('entity_id'),
  467. 'data_table' => array('data_table'),
  468. 'nid' => array('nid'),
  469. ),
  470. 'unique keys' => array(
  471. 'record' => array('data_table', 'record_id'),
  472. 'entity_id' => array('entity_id'),
  473. ),
  474. 'primary key' => array('chado_entity_id'),
  475. );
  476. return $schema;
  477. }
  478. /**
  479. * Moves vocabulary terms from the 'tripal' vocabulary to the 'local' vocabulary.
  480. */
  481. function tripal_chado_update_7300() {
  482. try {
  483. tripal_insert_db(array(
  484. 'name' => 'local',
  485. 'description' => variable_get('site_name', 'This site.'),
  486. ));
  487. // Move the library properties out of the tripal database and into the
  488. // local database.
  489. $sql = "
  490. UPDATE {dbxref}
  491. SET db_id = (SELECT db_id FROM {db} WHERE name = 'local')
  492. WHERE dbxref_id IN (
  493. SELECT DISTINCT CVT.dbxref_id
  494. FROM {cvterm} CVT
  495. INNER JOIN {cv} CV ON CV.cv_id = CVT.cv_id
  496. WHERE CV.name IN (
  497. 'library_property',
  498. 'library_type',
  499. 'project_property',
  500. 'nd_experiment_types',
  501. 'nd_geolocation_property',
  502. 'tripal_analysis'
  503. )
  504. )
  505. ";
  506. chado_query($sql);
  507. }
  508. catch (\PDOException $e) {
  509. $error = $e->getMessage();
  510. throw new DrupalUpdateException('Failed to complete update' . $error);
  511. }}