tripal_chado.install 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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. if (chado_is_installed()) {
  12. // For an upgraded site we need to move some vocabulary terms over
  13. // to the new 'local' vocabulary:
  14. tripal_insert_db(array(
  15. 'name' => 'local',
  16. 'description' => variable_get('site_name', 'This site.'),
  17. ));
  18. // Move the library properties out of the tripal database and into the
  19. // local database.
  20. $sql = "
  21. UPDATE {dbxref}
  22. SET db_id = (SELECT db_id FROM {db} WHERE name = 'local')
  23. WHERE dbxref_id IN (
  24. SELECT DISTINCT CVT.dbxref_id
  25. FROM {cvterm} CVT
  26. INNER JOIN {cv} CV ON CV.cv_id = CVT.cv_id
  27. WHERE CV.name IN (
  28. 'library_property',
  29. 'library_type',
  30. 'project_property',
  31. 'nd_experiment_types',
  32. 'nd_geolocation_property',
  33. 'tripal_analysis'
  34. )
  35. )
  36. ";
  37. chado_query($sql);
  38. }
  39. }
  40. /**
  41. * Implementation of hook_uninstall().
  42. *
  43. * @ingroup tripal
  44. */
  45. function tripal_chado_uninstall() {
  46. // // Drop the foreign key between tripal_custom_tables and tripal_mviews
  47. // // so that Drupal can then drop the tables
  48. // db_query('
  49. // ALTER TABLE {tripal_custom_tables}
  50. // DROP CONSTRAINT tripal_custom_tables_fk1 CASCADE
  51. // ');
  52. variable_set('tripal_chado_is_prepared', FALSE);
  53. }
  54. function tripal_chado_chado_semweb_schema(){
  55. return array(
  56. 'fields' => array(
  57. 'chado_semweb_id' => array(
  58. 'type' => 'serial',
  59. 'not null' => TRUE
  60. ),
  61. 'chado_table' => array(
  62. 'type' => 'varchar',
  63. 'length ' => 128,
  64. 'not null' => TRUE
  65. ),
  66. 'chado_column' => array(
  67. 'type' => 'text',
  68. 'length ' => 128,
  69. 'not null' => TRUE
  70. ),
  71. 'cvterm_id' => array(
  72. 'type' => 'int',
  73. ),
  74. ),
  75. 'primary key' => array(
  76. 0 => 'chado_semweb_id',
  77. ),
  78. 'indexes' => array(
  79. 'chado_semweb_id_idx1' => array('cvterm_id'),
  80. 'chado_semweb_id_idx2' => array('chado_column'),
  81. 'chado_semweb_id_idx3' => array('chado_table'),
  82. ),
  83. 'unique keys' => array(
  84. 'chado_semweb_uq1' => array('chado_table', 'chado_column'),
  85. ),
  86. );
  87. }
  88. /**
  89. * Table definition for the tripal_cv_obo table
  90. * @param $schema
  91. */
  92. function tripal_chado_tripal_cv_obo_schema() {
  93. return array(
  94. 'fields' => array(
  95. 'obo_id' => array(
  96. 'type' => 'serial',
  97. 'unsigned' => TRUE,
  98. 'not null' => TRUE
  99. ),
  100. 'name' => array(
  101. 'type' => 'varchar',
  102. 'length' => 255
  103. ),
  104. 'path' => array(
  105. 'type' => 'varchar',
  106. 'length' => 1024
  107. ),
  108. ),
  109. 'indexes' => array(
  110. 'tripal_cv_obo_idx1' => array('obo_id'),
  111. ),
  112. 'primary key' => array('obo_id'),
  113. );
  114. }
  115. /**
  116. *
  117. */
  118. function tripal_chado_enable() {
  119. // If Tripal v2 is already installed, then when the module is first enabled
  120. // after an upgade, the installation of this module will try and recreate
  121. // some of the tables created with tripal_core and the installation will fail.
  122. // Therefore, the tables were temporarily moved out of the way to preserve
  123. // the data. Now we'll move them back.
  124. tripal_chado_upgrade_v2_v3_enable();
  125. }
  126. /**
  127. * Implements hook_schema().
  128. */
  129. function tripal_chado_schema() {
  130. // If Tripal v2 is already installed, then when the module is first enabled
  131. // after an upgade, the installation of this module will try and recreate
  132. // some of the tables created with tripal_core and the installation will fail.
  133. // Therefore, we need to temporarily move those tables out of the way, let
  134. // the module install and then move them back.
  135. $migrated = variable_get('tripal_v2_upgrade_v3_check_chado', FALSE);
  136. if (!$migrated) {
  137. try {
  138. tripal_chado_upgrade_v2_v3_pre_enable();
  139. variable_set('tripal_v2_upgrade_v3_check_chado', TRUE);
  140. }
  141. catch(Exception $e) {
  142. watchdog_exception('tripal_chado', $e);
  143. }
  144. }
  145. // Links TripalEntity entities to the chado record.
  146. $schema['chado_bundle'] = tripal_chado_chado_bundle_schema();
  147. $schema['chado_semweb'] = tripal_chado_chado_semweb_schema();
  148. $schema['tripal_mviews'] = tripal_chado_tripal_mviews_schema();
  149. $schema['tripal_custom_tables'] = tripal_chado_tripal_custom_tables_schema();
  150. $schema['tripal_cv_obo'] = tripal_chado_tripal_cv_obo_schema();
  151. $schema['tripal_pub_import'] = tripal_chado_tripal_pub_import_schema();
  152. // if this module is already installed and enabled, then we want to provide
  153. // the schemas for all of the custom tables. This will allow Views to
  154. // see the schemas. We check if the module is installed because during
  155. // installation we don't want to make these custom tables available as we don't
  156. // want them created in the Drupal database. The custom tables go in the
  157. // Chado database.
  158. if (db_table_exists('tripal_custom_tables')) {
  159. $sql = 'SELECT * FROM {tripal_custom_tables}';
  160. $results = db_query($sql);
  161. foreach ($results as $custom) {
  162. $schema[$custom->table_name] = unserialize($custom->schema);
  163. }
  164. }
  165. // Map cvterm usage to chado tables
  166. $schema['chado_cvterm_mapping'] = tripal_chado_chado_cvterm_mapping_schema();
  167. // When a chado Tripal content type is created, a linking table is also created to
  168. // link the entity to it's record in chado (@see tripal_chado_bundle_create() ).
  169. // This table is created via db_create_table() but in order to expose it to
  170. // the Drupal Schema API, we also need to define each one here.
  171. if (db_table_exists('chado_bundle')) {
  172. $resource = db_query('SELECT tb.name FROM chado_bundle cb LEFT JOIN tripal_bundle tb ON tb.id=cb.bundle_id');
  173. foreach ($resource as $r) {
  174. $bundle_name = $r->name;
  175. // This makes an assumption about the name of the linking table.
  176. // @todo: Switch to chado_get_bundle_entity_table($bundle).
  177. $chado_entity_table = 'chado_' . $bundle_name;
  178. $schema[$chado_entity_table] = array(
  179. 'description' => 'The linker table that associates TripalEntities with Chado records for entities of type ' . $bundle_name . '.',
  180. 'fields' => array(
  181. 'mapping_id' => array(
  182. 'type' => 'serial',
  183. 'not null' => TRUE
  184. ),
  185. 'entity_id' => array(
  186. 'description' => 'The unique entity id.',
  187. 'type' => 'int',
  188. 'not null' => TRUE,
  189. ),
  190. 'record_id' => array(
  191. 'description' => 'The unique numerical identifier for the record that this entity is associated with (e.g. feature_id, stock_id, library_id, etc.).',
  192. 'type' => 'int',
  193. 'not null' => TRUE,
  194. ),
  195. 'nid' => array(
  196. 'description' => 'Optional. For linking nid to the entity when migrating Tripal v2 content',
  197. 'type' => 'int',
  198. )
  199. ),
  200. 'primary key' => array(
  201. 'mapping_id',
  202. ),
  203. 'indexes' => array(
  204. 'record_id' => array('record_id'),
  205. 'entity_id' => array('entity_id'),
  206. 'nid' => array('nid'),
  207. ),
  208. 'unique keys' => array(
  209. 'table_record' => array('record_id'),
  210. 'entity_id' => array('entity_id'),
  211. ),
  212. );
  213. }
  214. }
  215. return $schema;
  216. }
  217. /**
  218. * This function should be executed only one time during upgrade of v2 to v3.
  219. */
  220. function tripal_chado_upgrade_v2_v3_pre_enable() {
  221. // If Tripal v2 is already installed, then when the module is first enabled
  222. // after an upgade, the installation of this module will try and recreate
  223. // some of the tables created with tripal_core and the installation will fail.
  224. // Therefore, we need to temporarily move those tables out of the way, let
  225. // the module install and then move them back.
  226. if (db_table_exists('tripal_mviews')) {
  227. // Move the tripal_mviews table out of the way.
  228. $sql = "ALTER TABLE tripal_mviews RENAME TO tripal_mviews2";
  229. db_query($sql);
  230. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_mviews_mv_name_key'")->fetchField()) {
  231. $sql = "ALTER INDEX tripal_mviews_mv_name_key RENAME TO tripal_mviews_mv_name_key2";
  232. }
  233. else {
  234. $sql = "CREATE UNIQUE INDEX tripal_mviews_mv_name_key2 ON tripal_mviews2 USING btree (name)";
  235. }
  236. db_query($sql);
  237. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_mviews_mv_table_key'")->fetchField()) {
  238. $sql = "ALTER INDEX tripal_mviews_mv_table_key RENAME TO tripal_mviews_mv_table_key2";
  239. }
  240. else {
  241. $sql = "CREATE UNIQUE INDEX tripal_mviews_mv_table_key2 ON tripal_mviews2 USING btree (mv_table)";
  242. }
  243. db_query($sql);
  244. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_mviews_mview_id_idx'")->fetchField()) {
  245. $sql = "ALTER INDEX tripal_mviews_mview_id_idx RENAME TO tripal_mviews_mview_id_idx2";
  246. }
  247. else {
  248. $sql = "CREATE INDEX tripal_mviews_mview_id_idx2 ON tripal_mviews2 USING btree (mview_id)";
  249. }
  250. db_query($sql);
  251. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_mviews_pkey'")->fetchField()) {
  252. $sql = "ALTER INDEX tripal_mviews_pkey RENAME TO tripal_mviews_pkey2";
  253. }
  254. else {
  255. $sql = "CREATE UNIQUE INDEX tripal_mviews_pkey2 ON tripal_mviews2 USING btree (mview_id)";
  256. }
  257. db_query($sql);
  258. }
  259. if (db_table_exists('tripal_custom_tables')) {
  260. // Move the tripal_custom_tables table out of the way.
  261. $sql = "ALTER TABLE tripal_custom_tables RENAME TO tripal_custom_tables2";
  262. db_query($sql);
  263. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_custom_tables_pkey'")->fetchField()) {
  264. $sql = "ALTER INDEX tripal_custom_tables_pkey RENAME TO tripal_custom_tables_pkey2";
  265. }
  266. else {
  267. $sql = "CREATE UNIQUE INDEX tripal_custom_tables_pkey2 ON tripal_custom_tables2 USING btree (table_id)";
  268. }
  269. db_query($sql);
  270. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_custom_tables_table_id_idx'")->fetchField()) {
  271. $sql = "ALTER INDEX tripal_custom_tables_table_id_idx RENAME TO tripal_custom_tables_table_id_idx2";
  272. }
  273. else {
  274. $sql = "CREATE INDEX tripal_custom_tables_table_id_idx2 ON tripal_custom_tables2 USING btree (table_id)";
  275. }
  276. db_query($sql);
  277. }
  278. if (db_table_exists('tripal_cv_obo')) {
  279. // Move the tripal_cv_obo table out of the way.
  280. $sql = "ALTER TABLE tripal_cv_obo RENAME TO tripal_cv_obo2";
  281. db_query($sql);
  282. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_cv_obo_obo_id_idx'")->fetchField()) {
  283. $sql = "ALTER INDEX tripal_cv_obo_obo_id_idx RENAME TO tripal_cv_obo_obo_id_idx2";
  284. }
  285. else {
  286. $sql = "CREATE INDEX tripal_cv_obo_obo_id_idx2 ON tripal_cv_obo2 USING btree (obo_id)";
  287. }
  288. db_query($sql);
  289. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_cv_obo_pkey'")->fetchField()) {
  290. $sql = "ALTER INDEX tripal_cv_obo_pkey RENAME TO tripal_cv_obo_pkey2";
  291. }
  292. else if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_cv_obo_tripal_cv_obo_idx1_idx'")->fetchField()) {
  293. $sql = "ALTER INDEX tripal_cv_obo_tripal_cv_obo_idx1_idx RENAME TO tripal_cv_obo_tripal_cv_obo_idx1_idx2";
  294. }
  295. else {
  296. $sql = "CREATE UNIQUE INDEX tripal_cv_obo_pkey2 ON tripal_cv_obo2 USING btree (obo_id)";
  297. }
  298. db_query($sql);
  299. }
  300. if (db_table_exists('tripal_pub_import')) {
  301. // Move the tripal_pub_import table out of the way.
  302. $sql = "ALTER TABLE tripal_pub_import RENAME TO tripal_pub_import2";
  303. db_query($sql);
  304. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_pub_import_name_idx'")->fetchField()) {
  305. $sql = "ALTER INDEX tripal_pub_import_name_idx RENAME TO tripal_pub_import_name_idx2";
  306. }
  307. else {
  308. $sql = "CREATE INDEX tripal_pub_import_name_idx2 ON tripal_pub_import2 USING btree (name)";
  309. }
  310. db_query($sql);
  311. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_pub_import_pkey'")->fetchField()) {
  312. $sql = "ALTER INDEX tripal_pub_import_pkey RENAME TO tripal_pub_import_pkey2";
  313. }
  314. else {
  315. $sql = "CREATE UNIQUE INDEX tripal_pub_import_pkey2 ON tripal_pub_import2 USING btree (pub_import_id)";
  316. }
  317. db_query($sql);
  318. }
  319. }
  320. /**
  321. * This function should be executed only one time during upgrade of v2 to v3.
  322. */
  323. function tripal_chado_upgrade_v2_v3_enable() {
  324. // If Tripal v2 is already installed, the installation of this module
  325. // will try and recreate some of the tables created with tripal_core and the
  326. // installation will fail. Therefore, in the install we renamed it. Now
  327. // we want to move it back.
  328. if (db_table_exists('tripal_mviews2')) {
  329. // tripal_mviews
  330. $sql = "DROP TABLE tripal_mviews";
  331. db_query($sql);
  332. $sql = "ALTER TABLE tripal_mviews2 RENAME to tripal_mviews";
  333. db_query($sql);
  334. $sql = "ALTER INDEX tripal_mviews_mv_name_key2 RENAME TO tripal_mviews_mv_name_key";
  335. db_query($sql);
  336. $sql = "ALTER INDEX tripal_mviews_mv_table_key2 RENAME TO tripal_mviews_mv_table_key";
  337. db_query($sql);
  338. $sql = "ALTER INDEX tripal_mviews_mview_id_idx2 RENAME TO tripal_mviews_mview_id_idx";
  339. db_query($sql);
  340. $sql = "ALTER INDEX tripal_mviews_pkey2 RENAME TO tripal_mviews_pkey";
  341. db_query($sql);
  342. }
  343. // tripal_custom_tables
  344. if (db_table_exists('tripal_custom_tables2')) {
  345. $sql = "DROP TABLE tripal_custom_tables";
  346. db_query($sql);
  347. $sql = "ALTER TABLE tripal_custom_tables2 RENAME to tripal_custom_tables";
  348. db_query($sql);
  349. $sql = "ALTER INDEX tripal_custom_tables_pkey2 RENAME TO tripal_custom_tables_pkey";
  350. db_query($sql);
  351. $sql = "ALTER INDEX tripal_custom_tables_table_id_idx2 RENAME TO tripal_custom_tables_table_id_idx";
  352. db_query($sql);
  353. }
  354. // tripal_cv_obo
  355. if (db_table_exists('tripal_cv_obo2')) {
  356. $sql = "DROP TABLE tripal_cv_obo";
  357. db_query($sql);
  358. $sql = "ALTER TABLE tripal_cv_obo2 RENAME to tripal_cv_obo";
  359. db_query($sql);
  360. $sql = "ALTER INDEX tripal_cv_obo_obo_id_idx2 RENAME TO tripal_cv_obo_obo_id_idx";
  361. db_query($sql);
  362. if (db_query("SELECT 1 FROM pg_indexes WHERE indexname = 'tripal_cv_obo_pkey2'")->fetchField()) {
  363. $sql = "ALTER INDEX tripal_cv_obo_pkey2 RENAME TO tripal_cv_obo_pkey";
  364. }
  365. else {
  366. $sql = "ALTER INDEX tripal_cv_obo_tripal_cv_obo_idx1_idx2 RENAME TO tripal_cv_obo_tripal_cv_obo_idx1_idx";
  367. }
  368. db_query($sql);
  369. }
  370. // tripal_pub_import
  371. if (db_table_exists('tripal_pub_import2')) {
  372. $sql = "DROP TABLE tripal_pub_import";
  373. db_query($sql);
  374. $sql = "ALTER TABLE tripal_pub_import2 RENAME to tripal_pub_import";
  375. db_query($sql);
  376. $sql = "ALTER INDEX tripal_pub_import_name_idx2 RENAME TO tripal_pub_import_name_idx";
  377. db_query($sql);
  378. $sql = "ALTER INDEX tripal_pub_import_pkey2 RENAME TO tripal_pub_import_pkey";
  379. db_query($sql);
  380. }
  381. }
  382. /**
  383. * @section
  384. * Schema Definitions.
  385. */
  386. /**
  387. * Implementation of hook_schema().
  388. *
  389. * @ingroup tripal_pub
  390. */
  391. function tripal_chado_tripal_pub_import_schema() {
  392. return array(
  393. 'fields' => array(
  394. 'pub_import_id' => array(
  395. 'type' => 'serial',
  396. 'not null' => TRUE
  397. ),
  398. 'name' => array(
  399. 'type' => 'varchar',
  400. 'length' => 255,
  401. 'not null' => TRUE
  402. ),
  403. 'criteria' => array(
  404. 'type' => 'text',
  405. 'size' => 'normal',
  406. 'not null' => TRUE,
  407. 'description' => 'Contains a serialized PHP array containing the search criteria'
  408. ),
  409. 'disabled' => array(
  410. 'type' => 'int',
  411. 'unsigned' => TRUE,
  412. 'not NULL' => TRUE,
  413. 'default' => 0
  414. ),
  415. 'do_contact' => array(
  416. 'type' => 'int',
  417. 'unsigned' => TRUE,
  418. 'not NULL' => TRUE,
  419. 'default' => 0
  420. ),
  421. ),
  422. 'primary key' => array('pub_import_id'),
  423. 'indexes' => array(
  424. 'name' => array('name')
  425. ),
  426. );
  427. }
  428. /**
  429. * Describes the Tripal Custom Tables (tripal_custom_tables) table
  430. * This keeps track of tables created by Tripal and stored in chado that may or may not
  431. * also be materialized views.
  432. *
  433. * @ingroup tripal
  434. */
  435. function tripal_chado_tripal_custom_tables_schema() {
  436. return array(
  437. 'fields' => array(
  438. 'table_id' => array(
  439. 'type' => 'serial',
  440. 'unsigned' => TRUE,
  441. 'not NULL' => TRUE
  442. ),
  443. 'table_name' => array(
  444. 'type' => 'varchar',
  445. 'length' => 255,
  446. 'not NULL' => TRUE
  447. ),
  448. 'schema' => array(
  449. 'type' => 'text',
  450. 'not NULL' => TRUE
  451. ),
  452. 'mview_id' => array(
  453. 'type' => 'int',
  454. 'not NULL' => FALSE
  455. )
  456. ),
  457. 'indexes' => array(
  458. 'table_id' => array('table_id'),
  459. ),
  460. 'primary key' => array('table_id'),
  461. 'foreign keys' => array(
  462. 'tripal_mviews' => array(
  463. 'table' => 'tripal_mviews',
  464. 'columns' => array(
  465. 'mview_id' => 'mview_id'
  466. ),
  467. ),
  468. ),
  469. );
  470. }
  471. /**
  472. * Describes the Tripal Materialized View (tripal_mviews) table
  473. * This table keeps track of all materialized views created by Tripal and stored in chado
  474. *
  475. * @ingroup tripal
  476. */
  477. function tripal_chado_tripal_mviews_schema() {
  478. return array(
  479. 'fields' => array(
  480. 'mview_id' => array(
  481. 'type' => 'serial',
  482. 'unsigned' => TRUE,
  483. 'not NULL' => TRUE
  484. ),
  485. 'name' => array(
  486. 'type' => 'varchar',
  487. 'length' => 255,
  488. 'not NULL' => TRUE
  489. ),
  490. 'modulename' => array(
  491. 'type' => 'varchar',
  492. 'length' => 50,
  493. 'not NULL' => TRUE,
  494. 'description' => 'The module name that provides the callback for this job'
  495. ),
  496. 'mv_table' => array(
  497. 'type' => 'varchar',
  498. 'length' => 128,
  499. 'not NULL' => FALSE
  500. ),
  501. 'mv_specs' => array(
  502. 'type' => 'text',
  503. 'size' => 'normal',
  504. 'not NULL' => FALSE
  505. ),
  506. 'mv_schema' => array(
  507. 'type' => 'text',
  508. 'size' => 'normal',
  509. 'not NULL' => FALSE
  510. ),
  511. 'indexed' => array(
  512. 'type' => 'text',
  513. 'size' => 'normal',
  514. 'not NULL' => FALSE
  515. ),
  516. 'query' => array(
  517. 'type' => 'text',
  518. 'size' => 'normal',
  519. 'not NULL' => TRUE
  520. ),
  521. 'special_index' => array(
  522. 'type' => 'text',
  523. 'size' => 'normal',
  524. 'not NULL' => FALSE
  525. ),
  526. 'last_update' => array(
  527. 'type' => 'int',
  528. 'not NULL' => FALSE,
  529. 'description' => 'UNIX integer time'
  530. ),
  531. 'status' => array(
  532. 'type' => 'text',
  533. 'size' => 'normal',
  534. 'not NULL' => FALSE
  535. ),
  536. 'comment' => array(
  537. 'type' => 'text',
  538. 'size' => 'normal',
  539. 'not NULL' => FALSE
  540. ),
  541. ),
  542. 'indexes' => array(
  543. 'mview_id' => array('mview_id')
  544. ),
  545. 'unique keys' => array(
  546. 'mv_table' => array('mv_table'),
  547. 'mv_name' => array('name'),
  548. ),
  549. 'primary key' => array('mview_id'),
  550. );
  551. }
  552. /**
  553. * Links Biological Data Entities to the chado "base" table the data is stored in.
  554. * This is where we would specify that a particular gene maps to the record in the
  555. * chado.feature table with a feature_id=2432;
  556. */
  557. function tripal_chado_chado_bundle_schema() {
  558. $schema = array(
  559. 'description' => 'Describes how a bundle maps data to Chado',
  560. 'fields' => array(
  561. 'chado_bundle_id' => array(
  562. 'description' => 'The primary identifier for this table.',
  563. 'type' => 'serial',
  564. 'unsigned' => TRUE,
  565. 'not null' => TRUE,
  566. ),
  567. 'bundle_id' => array(
  568. 'description' => 'The unique entity id.',
  569. 'type' => 'int',
  570. 'not null' => TRUE,
  571. ),
  572. 'data_table' => array(
  573. 'description' => 'The table in Chado that this term services (e.g. feature, stock, library, etc.)',
  574. 'type' => 'varchar',
  575. 'length' => 128,
  576. 'not null' => TRUE,
  577. 'default' => '',
  578. ),
  579. 'type_linker_table' => array(
  580. 'description' => 'If a linker table (e.g. cvterm/prop) is needed to uniquely identify a content type then that table name is provided here.',
  581. 'type' => 'varchar',
  582. 'length' => 128,
  583. 'not null' => FALSE,
  584. 'default' => '',
  585. ),
  586. 'type_column' => array(
  587. 'description' => 'The column in the data table or linker table that distinguishes the data type. This must be in a foreign key relationship to the cvterm table.',
  588. 'type' => 'varchar',
  589. 'length' => 128,
  590. 'not null' => FALSE,
  591. 'default' => '',
  592. ),
  593. 'type_id' => array(
  594. 'description' => 'If a type_column is set then this is the cvterm_id of the data type that this bundle maps to.',
  595. 'type' => 'varchar',
  596. 'length' => 128,
  597. 'not null' => TRUE,
  598. 'default' => '',
  599. ),
  600. 'type_value' => array(
  601. 'description' => 'If a property table is used for a linker, then the value that should be matched to identify this content type is stored here.',
  602. 'type' => 'text',
  603. 'not null' => FALSE,
  604. 'default' => '',
  605. )
  606. ),
  607. 'indexes' => array(
  608. 'bundle_id' => array('bundle_id'),
  609. 'data_table' => array('data_table'),
  610. ),
  611. 'unique keys' => array(
  612. 'record' => array('bundle_id'),
  613. ),
  614. 'primary key' => array('chado_bundle_id'),
  615. );
  616. return $schema;
  617. }
  618. /**
  619. * Tripal cvterm mapping schema
  620. * Map cvterms to chado tables that use them
  621. */
  622. function tripal_chado_chado_cvterm_mapping_schema() {
  623. $schema = array (
  624. 'table' => 'chado_cvterm_mapping',
  625. 'fields' => array (
  626. 'mapping_id' => array(
  627. 'type' => 'serial',
  628. 'not null' => TRUE
  629. ),
  630. 'cvterm_id' => array (
  631. 'type' => 'int',
  632. 'not null' => TRUE
  633. ),
  634. 'chado_table' => array (
  635. 'type' => 'varchar',
  636. 'length' => 128,
  637. 'not null' => TRUE
  638. ),
  639. 'chado_field' => array (
  640. 'type' => 'varchar',
  641. 'length' => 128,
  642. 'not null' => FALSE
  643. ),
  644. ),
  645. 'primary key' => array (
  646. 0 => 'mapping_id'
  647. ),
  648. 'unique key' => array(
  649. 'cvterm_id',
  650. ),
  651. 'indexes' => array(
  652. 'tripal_cvterm2table_idx1' => array('cvterm_id'),
  653. 'tripal_cvterm2table_idx2' => array('chado_table'),
  654. 'tripal_cvterm2table_idx3' => array('chado_table', 'chado_field'),
  655. ),
  656. );
  657. return $schema;
  658. }
  659. /**
  660. * Fixes the phase on the tripal_gffcds_temp table used for importing GFF files, and fixes the db.name term mapping.
  661. *
  662. */
  663. function tripal_chado_update_7300() {
  664. try {
  665. if (chado_table_exists('tripal_gffcds_temp')) {
  666. chado_query("ALTER TABLE {tripal_gffcds_temp} ALTER COLUMN phase DROP NOT NULL;");
  667. }
  668. $term = tripal_insert_cvterm(array(
  669. 'id' => 'data:1048',
  670. 'name' => 'Database ID',
  671. 'cv_name' => 'EDAM',
  672. 'definition' => 'An identifier of a biological or bioinformatics database.',
  673. ));
  674. tripal_associate_chado_semweb_term('db', 'name', $term);
  675. }
  676. catch (\PDOException $e) {
  677. $error = $e->getMessage();
  678. throw new DrupalUpdateException('Could not fix phase on tripal_gffcds_temp table: '. $error);
  679. }
  680. }
  681. /**
  682. * Divides chado_entity table for better integration with views.
  683. */
  684. function tripal_chado_update_7301() {
  685. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.bundle');
  686. try {
  687. $transaction = db_transaction();
  688. $query = db_select('chado_bundle', 'CB');
  689. $query->join('tripal_bundle', 'TB', 'TB.id = CB.bundle_id');
  690. $query->fields('CB', array('data_table'));
  691. $query->fields('TB', array('name'));
  692. $cbundles = $query->execute();
  693. // If the table for the bundle doesn't exist then create one, and then
  694. // move all of the records from the chado_entity table to it.
  695. while($cbundle = $cbundles->fetchObject()) {
  696. $cbundle_table = chado_get_bundle_entity_table($cbundle);
  697. if (!db_table_exists($cbundle_table)) {
  698. // Create the bundle table.
  699. tripal_chado_create_bundle_table($cbundle);
  700. // Now move the records over.
  701. $sql = "
  702. INSERT INTO {$cbundle_table} (entity_id, record_id, nid)
  703. SELECT CE.entity_id, CE.record_id, CE.nid
  704. FROM {chado_entity} CE
  705. INNER JOIN {tripal_entity} TE ON CE.entity_id = TE.id
  706. WHERE TE.bundle = :bundle
  707. ";
  708. db_query($sql, array(':bundle' => $cbundle->name));
  709. }
  710. }
  711. // Now remove the chado_entity table.
  712. db_drop_table('chado_entity');
  713. }
  714. catch (\PDOException $e) {
  715. $transaction->rollback();
  716. $error = $e->getMessage();
  717. throw new DrupalUpdateException('Could not perform update: '. $error);
  718. }
  719. }
  720. /**
  721. * Corrections to the EDAM database.
  722. */
  723. function tripal_chado_update_7302(){
  724. try {
  725. // Add the term for the field.
  726. tripal_insert_db(array(
  727. 'name' => 'format',
  728. 'description' => 'A defined way or layout of representing and structuring data in a computer file, blob, string, message, or elsewhere. The main focus in EDAM lies on formats as means of structuring data exchanged between different tools or resources. ',
  729. 'url' => 'http://edamontology.org/page',
  730. 'urlprefix' => 'http://edamontology.org/{db}_{accession}',
  731. ));
  732. tripal_insert_db(array(
  733. 'name' => 'operation',
  734. 'description' => 'A function that processes a set of inputs and results in a set of outputs, or associates arguments (inputs) with values (outputs). Special cases are: a) An operation that consumes no input (has no input arguments).',
  735. 'url' => 'http://edamontology.org/page',
  736. 'urlprefix' => 'http://edamontology.org/{db}_{accession}',
  737. ));
  738. tripal_insert_db(array(
  739. 'name' => 'topic',
  740. 'description' => 'A category denoting a rather broad domain or field of interest, of study, application, work, data, or technology. Topics have no clearly defined borders between each other.',
  741. 'url' => 'http://edamontology.org/page',
  742. 'urlprefix' => 'http://edamontology.org/{db}_{accession}',
  743. ));
  744. tripal_insert_cv(
  745. 'EDAM',
  746. 'EDAM is an ontology of well established, familiar concepts that are prevalent within bioinformatics, including types of data and data identifiers, data formats, operations and topics. EDAM is a simple ontology - essentially a set of terms with synonyms and definitions - organised into an intuitive hierarchy for convenient use by curators, software developers and end-users. EDAM is suitable for large-scale semantic annotations and categorization of diverse bioinformatics resources. EDAM is also suitable for diverse application including for example within workbenches and workflow-management systems, software distributions, and resource registries.'
  747. );
  748. }
  749. catch (\PDOException $e) {
  750. $transaction->rollback();
  751. $error = $e->getMessage();
  752. throw new DrupalUpdateException('Could not perform update: '. $error);
  753. }
  754. }
  755. /**
  756. * Fixes inconsistency with Chado v1.3 update if was applied.
  757. */
  758. function tripal_chado_update_7303() {
  759. try {
  760. $chado_version = chado_get_version();
  761. if ($chado_version == '1.3') {
  762. module_load_include('inc', 'tripal_chado', 'includes/setup/tripal_chado.setup');
  763. tripal_chado_fix_v1_3_custom_tables();
  764. }
  765. }
  766. catch (\PDOException $e) {
  767. $transaction->rollback();
  768. $error = $e->getMessage();
  769. throw new DrupalUpdateException('Could not perform update: '. $error);
  770. }
  771. }
  772. /**
  773. * Add some new controlled vocabulary terms.
  774. */
  775. function tripal_chado_update_7304() {
  776. try {
  777. $term = tripal_insert_cvterm(array(
  778. 'id' => 'SIO:001080',
  779. 'name' => 'vocabulary',
  780. 'cv_name' => 'SIO',
  781. 'definition' => 'A vocabulary is a collection of terms.',
  782. ));
  783. tripal_associate_chado_semweb_term('cvterm', 'cv_id', $term);
  784. $term = tripal_insert_cvterm(array(
  785. 'id' => 'data:2976',
  786. 'name' => 'Protein sequence',
  787. 'cv_name' => 'EDAM',
  788. 'definition' => 'One or more protein sequences, possibly with associated annotation.',
  789. ));
  790. $term = tripal_insert_cvterm(array(
  791. 'id' => 'local:fmin',
  792. 'name' => 'minimal boundary',
  793. 'definition' => 'The leftmost, minimal boundary in the linear range ' .
  794. 'represented by the feature location. Sometimes this is called ' .
  795. 'start although this is confusing because it does not necessarily ' .
  796. 'represent the 5-prime coordinate.',
  797. 'cv_name' => 'local',
  798. ));
  799. tripal_associate_chado_semweb_term('featureloc', 'fmin', $term);
  800. $term = tripal_insert_cvterm(array(
  801. 'id' => 'local:fmax',
  802. 'name' => 'maximal boundary',
  803. 'definition' => 'The rightmost, maximal boundary in the linear range ' .
  804. 'represented by the featureloc. Sometimes this is called end although ' .
  805. 'this is confusing because it does not necessarily represent the ' .
  806. '3-prime coordinate',
  807. 'cv_name' => 'local',
  808. ));
  809. tripal_associate_chado_semweb_term('featureloc', 'fmax', $term);
  810. $term = tripal_insert_cvterm(array(
  811. 'id' => 'data:2336',
  812. 'name' => 'Translation phase specification',
  813. 'cv_name' => 'EDAM',
  814. 'definition' => 'Phase for translation of DNA (0, 1 or 2) relative to a fragment of the coding sequence.',
  815. ));
  816. tripal_associate_chado_semweb_term('featureloc', 'phase', $term);
  817. $term = tripal_insert_cvterm(array(
  818. 'id' => 'data:3002',
  819. 'name' => 'Annotation track',
  820. 'cv_name' => 'EDAM',
  821. 'definition' => 'Annotation of one particular positional feature on a ' .
  822. 'biomolecular (typically genome) sequence, suitable for import and ' .
  823. 'display in a genome browser. Synonym: Sequence annotation track.',
  824. ));
  825. tripal_associate_chado_semweb_term('featureloc', 'srcfeature_id', $term);
  826. }
  827. catch (\PDOException $e) {
  828. $transaction->rollback();
  829. $error = $e->getMessage();
  830. throw new DrupalUpdateException('Could not perform update: '. $error);
  831. }
  832. }
  833. /**
  834. * Adding missing cv/db details and cvterms.
  835. */
  836. function tripal_chado_update_7305() {
  837. try {
  838. tripal_insert_db(array(
  839. 'name' => 'rdfs',
  840. 'description' => 'Resource Description Framework Schema',
  841. 'url' => 'https://www.w3.org/TR/rdf-schema/',
  842. 'urlprefix' => 'https://www.w3.org/TR/rdf-schema/#ch_{accession}',
  843. ));
  844. tripal_insert_cv('rdfs', 'Resource Description Framework Schema');
  845. tripal_insert_db(array(
  846. 'name' => 'SO',
  847. 'description' => 'The sequence ontology.',
  848. 'url' => 'http://www.sequenceontology.org/',
  849. 'urlprefix' => 'http://www.sequenceontology.org/browser/current_svn/term/{db}:{accession}',
  850. ));
  851. tripal_insert_cv('sequence', 'The sequence ontology.');
  852. tripal_insert_db(array(
  853. 'name' => 'TAXRANK',
  854. 'description' => 'A vocabulary of taxonomic ranks (species, family, phylum, etc)',
  855. 'url' => 'http://www.obofoundry.org/ontology/taxrank.html',
  856. 'urlprefix' => 'http://purl.obolibrary.org/obo/{db}_{accession}',
  857. ));
  858. tripal_insert_cv('taxonomic_rank', 'A vocabulary of taxonomic ranks (species, family, phylum, etc)');
  859. tripal_insert_db(array(
  860. 'name' => 'hydra',
  861. 'description' => 'A Vocabulary for Hypermedia-Driven Web APIs',
  862. 'url' => 'https://www.hydra-cg.com/spec/latest/core/',
  863. 'urlprefix' => 'https://www.hydra-cg.com/spec/latest/core/#{db}:{accession}',
  864. ));
  865. tripal_insert_cv(
  866. 'hydra',
  867. 'A Vocabulary for Hypermedia-Driven Web APIs.'
  868. );
  869. tripal_insert_db(array(
  870. 'name' => 'dc',
  871. 'description' => 'DCMI Metadata Terms.',
  872. 'url' => 'http://purl.org/dc/dcmitype/',
  873. 'urlprefix' => 'http://purl.org/dc/terms/{accession}',
  874. ));
  875. tripal_insert_cv(
  876. 'dc',
  877. 'DCMI Metadata Terms.'
  878. );
  879. $term = tripal_insert_cvterm(array(
  880. 'id' => 'dc:Service',
  881. 'name' => 'Service',
  882. 'cv_name' => 'dc',
  883. 'definition' => 'A system that provides one or more functions.',
  884. ));
  885. $name = tripal_insert_cvterm(array(
  886. 'id' => 'hydra:Collection',
  887. 'name' => 'Collection',
  888. 'cv_name' => 'hydra',
  889. 'definition' => 'A collection holding references to a number of related resources.',
  890. ));
  891. $name = tripal_insert_cvterm(array(
  892. 'id' => 'hydra:member',
  893. 'name' => 'member',
  894. 'cv_name' => 'hydra',
  895. 'definition' => 'A member of the collection',
  896. ));
  897. $name = tripal_insert_cvterm(array(
  898. 'id' => 'hydra:description',
  899. 'name' => 'description',
  900. 'cv_name' => 'hydra',
  901. 'definition' => 'A description.',
  902. ));
  903. $name = tripal_insert_cvterm(array(
  904. 'id' => 'hydra:totalItems',
  905. 'name' => 'totalItems',
  906. 'cv_name' => 'hydra',
  907. 'definition' => 'The total number of items referenced by a collection.',
  908. ));
  909. $name = tripal_insert_cvterm(array(
  910. 'id' => 'hydra:title',
  911. 'name' => 'title',
  912. 'cv_name' => 'hydra',
  913. 'definition' => 'A title, often used along with a description.',
  914. ));
  915. $name = tripal_insert_cvterm(array(
  916. 'id' => 'hydra:PartialCollectionView',
  917. 'name' => 'PartialCollectionView',
  918. 'cv_name' => 'hydra',
  919. 'definition' => 'A PartialCollectionView describes a partial view of a Collection. Multiple PartialCollectionViews can be connected with the the next/previous properties to allow a client to retrieve all members of the collection.',
  920. ));
  921. $term = tripal_insert_cvterm(array(
  922. 'id' => 'schema:ItemPage',
  923. 'name' => 'ItemPage',
  924. 'cv_name' => 'schema',
  925. 'definition' => 'A page devoted to a single item, such as a particular product or hotel.',
  926. ));
  927. global $base_path;
  928. tripal_insert_db(array(
  929. 'name' => 'null',
  930. 'description' => 'No online database.',
  931. 'url' => $base_path . 'cv/lookup/null',
  932. 'urlprefix' => $base_path. 'cv/lookup/{db}/{accession}',
  933. ));
  934. tripal_insert_db(array(
  935. 'name' => 'local',
  936. 'description' => 'Terms created for this site.',
  937. 'url' => $base_path . 'cv/lookup/local',
  938. 'urlprefix' => $base_path . 'cv/lookup/{db}/{accession}',
  939. ));
  940. $term = tripal_insert_cvterm(array(
  941. 'id' => 'local:rank',
  942. 'name' => 'rank',
  943. 'definition' => 'A taxonmic rank',
  944. 'cv_name' => 'local',
  945. ));
  946. }
  947. catch (\PDOException $e) {
  948. $transaction->rollback();
  949. $error = $e->getMessage();
  950. throw new DrupalUpdateException('Could not perform update: '. $error);
  951. }
  952. }