tripal_cv.install

  1. 2.x tripal_cv/tripal_cv.install
  2. 3.x legacy/tripal_cv/tripal_cv.install
  3. 1.x tripal_cv/tripal_cv.install

Contains functions executed only on install/uninstall of this module

File

tripal_cv/tripal_cv.install
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains functions executed only on install/uninstall of this module
  5. */
  6. /**
  7. * Implements hook_disable().
  8. * Disable default views when module is disabled
  9. *
  10. * @ingroup tripal_cv
  11. */
  12. function tripal_cv_disable() {
  13. // Disable all default views provided by this module
  14. require_once("tripal_cv.views_default.inc");
  15. $views = tripal_cv_views_default_views();
  16. foreach (array_keys($views) as $view_name) {
  17. tripal_disable_view($view_name,FALSE,array('suppress_error' => TRUE));
  18. }
  19. }
  20. /**
  21. * Implementation of hook_requirements().
  22. *
  23. * @ingroup tripal_cv
  24. */
  25. function tripal_cv_requirements($phase) {
  26. $requirements = array();
  27. if ($phase == 'install') {
  28. // make sure chado is installed
  29. if (!$GLOBALS["chado_is_installed"]) {
  30. $requirements ['tripal_cv'] = array(
  31. 'title' => "tripal_cv",
  32. 'value' => "ERROR: Chado must be installed before this module can be enabled",
  33. 'severity' => REQUIREMENT_ERROR,
  34. );
  35. }
  36. }
  37. return $requirements;
  38. }
  39. /**
  40. * Implementation of hook_install().
  41. *
  42. * @ingroup tripal_cv
  43. */
  44. function tripal_cv_install() {
  45. // Add the cv_root_mview.
  46. tripal_cv_add_cv_root_mview();
  47. // Add defaults to the tables that correlate OBO files/references with
  48. // a chado CV.
  49. tripal_cv_add_obo_defaults();
  50. // Add the Chado ontology CV.
  51. $obo_path = '{tripal_cv}/files/cv_property.obo';
  52. $obo_id = tripal_insert_obo('Chado CV Properties', $obo_path);
  53. tripal_submit_obo_job(array('obo_id' => $obo_id));
  54. // Create the temp table we will use for loading OBO files.
  55. tripal_cv_create_tripal_obo_temp();
  56. }
  57. /**
  58. * Implementation of hook_uninstall().
  59. *
  60. * @ingroup tripal_cv
  61. */
  62. function tripal_cv_uninstall() {
  63. // drop the tripal_obo_temp table
  64. if (chado_table_exists('tripal_obo_temp')) {
  65. $sql = "DROP TABLE {tripal_obo_temp}";
  66. chado_query($sql);
  67. }
  68. }
  69. /**
  70. * Creates a temporary table to store obo details while loading an obo file
  71. *
  72. * @ingroup tripal_cv
  73. */
  74. function tripal_cv_create_tripal_obo_temp() {
  75. // the tripal_obo_temp table is used for temporary housing of records when loading OBO files
  76. // we create it here using plain SQL because we want it to be in the chado schema but we
  77. // do not want to use the Tripal Custom Table API because we don't want it to appear in the
  78. // list of custom tables. It needs to be available for the Tripal Chado API so we create it
  79. // here and then define it in the tripal_cv/api/tripal_cv.schema.api.inc
  80. if (!chado_table_exists('tripal_obo_temp')) {
  81. $sql = "
  82. CREATE TABLE {tripal_obo_temp} (
  83. id character varying(255) NOT NULL,
  84. stanza text NOT NULL,
  85. type character varying(50) NOT NULL,
  86. CONSTRAINT tripal_obo_temp_uq0 UNIQUE (id)
  87. );
  88. ";
  89. chado_query($sql);
  90. $sql = "CREATE INDEX tripal_obo_temp_idx0 ON {tripal_obo_temp} USING btree (id)";
  91. chado_query($sql);
  92. $sql = "CREATE INDEX tripal_obo_temp_idx1 ON {tripal_obo_temp} USING btree (type)";
  93. chado_query($sql);
  94. }
  95. }
  96. /**
  97. * Implementation of hook_schema().
  98. *
  99. * @ingroup tripal_cv
  100. */
  101. function tripal_cv_schema() {
  102. $schema = array();
  103. tripal_cv_get_tripal_cv_obo_table($schema);
  104. tripal_cv_get_tripal_cv_defaults_table($schema);
  105. return $schema;
  106. }
  107. /**
  108. * Table definition for the tripal_cv_obo table
  109. * @param $schema
  110. */
  111. function tripal_cv_get_tripal_cv_obo_table(&$schema) {
  112. $schema['tripal_cv_obo'] = array(
  113. 'fields' => array(
  114. 'obo_id' => array(
  115. 'type' => 'serial',
  116. 'unsigned' => TRUE,
  117. 'not null' => TRUE
  118. ),
  119. 'name' => array(
  120. 'type' => 'varchar',
  121. 'length' => 255
  122. ),
  123. 'path' => array(
  124. 'type' => 'varchar',
  125. 'length' => 1024
  126. ),
  127. ),
  128. 'indexes' => array(
  129. 'tripal_cv_obo_idx1' => array('obo_id'),
  130. ),
  131. 'primary key' => array('obo_id'),
  132. );
  133. }
  134. /**
  135. * * Table definition for the tripal_cv_defaults table
  136. * @param unknown $schema
  137. */
  138. function tripal_cv_get_tripal_cv_defaults_table(&$schema) {
  139. $schema['tripal_cv_defaults'] = array(
  140. 'fields' => array(
  141. 'cv_default_id' => array(
  142. 'type' => 'serial',
  143. 'unsigned' => TRUE,
  144. 'not null' => TRUE
  145. ),
  146. 'table_name' => array(
  147. 'type' => 'varchar',
  148. 'length' => 128,
  149. 'not null' => TRUE,
  150. ),
  151. 'field_name' => array(
  152. 'type' => 'varchar',
  153. 'length' => 128,
  154. 'not null' => TRUE,
  155. ),
  156. 'cv_id' => array(
  157. 'type' => 'int',
  158. 'not null' => TRUE,
  159. )
  160. ),
  161. 'indexes' => array(
  162. 'tripal_cv_defaults_idx1' => array('table_name', 'field_name'),
  163. ),
  164. 'unique keys' => array(
  165. 'tripal_cv_defaults_unq1' => array('table_name', 'field_name', 'cv_id'),
  166. ),
  167. 'primary key' => array('cv_default_id')
  168. );
  169. }
  170. /**
  171. * Add a materialized view of root terms for all chado cvs. This is needed for viewing cv trees
  172. *
  173. * @ingroup tripal_cv
  174. */
  175. function tripal_cv_add_cv_root_mview() {
  176. $mv_name = 'cv_root_mview';
  177. $comment = 'A list of the root terms for all controlled vocabularies. This is needed for viewing CV trees';
  178. $schema = array(
  179. 'table' => $mv_name,
  180. 'description' => $comment,
  181. 'fields' => array(
  182. 'name' => array(
  183. 'type' => 'varchar',
  184. 'length' => 255,
  185. 'not null' => TRUE,
  186. ),
  187. 'cvterm_id' => array(
  188. 'size' => 'big',
  189. 'type' => 'int',
  190. 'not null' => TRUE,
  191. ),
  192. 'cv_id' => array(
  193. 'size' => 'big',
  194. 'type' => 'int',
  195. 'not null' => TRUE,
  196. ),
  197. 'cv_name' => array(
  198. 'type' => 'varchar',
  199. 'length' => 255,
  200. 'not null' => TRUE,
  201. ),
  202. ),
  203. 'indexes' => array(
  204. 'cv_root_mview_indx1' => array('cvterm_id'),
  205. 'cv_root_mview_indx2' => array('cv_id'),
  206. ),
  207. );
  208. $sql = "
  209. SELECT DISTINCT CVT.name,CVT.cvterm_id, CV.cv_id, CV.name
  210. FROM cvterm_relationship CVTR
  211. INNER JOIN cvterm CVT on CVTR.object_id = CVT.cvterm_id
  212. INNER JOIN cv CV on CV.cv_id = CVT.cv_id
  213. WHERE CVTR.object_id not in
  214. (SELECT subject_id FROM cvterm_relationship)
  215. ";
  216. // Create the MView
  217. tripal_add_mview($mv_name, 'tripal_cv', $schema, $sql, $comment);
  218. }
  219. /**
  220. * Add's defaults to the tripal_cv_obo table
  221. *
  222. * @ingroup tripal_cv
  223. */
  224. function tripal_cv_add_obo_defaults() {
  225. // Insert commonly used ontologies into the tables.
  226. $ontologies = array(
  227. // array('Relationship Ontology', 'http://purl.obolibrary.org/obo/ro.obo'),
  228. // array('Relationship Ontology (older deprecated version)', 'http://www.obofoundry.org/ro/ro.obo'),
  229. array('Sequence Ontology', 'http://purl.obolibrary.org/obo/so.obo'),
  230. array('Gene Ontology', 'http://purl.obolibrary.org/obo/go.obo'),
  231. // array('Cell Ontology', 'https://raw.githubusercontent.com/obophenotype/cell-ontology/master/cl.obo'),
  232. // array('Plant Structure Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_anatomy.obo?view=co'),
  233. // array('Plant Growth and Development Stages Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_temporal.obo?view=co')
  234. );
  235. foreach ($ontologies as $o) {
  236. db_query("INSERT INTO {tripal_cv_obo} (name,path) VALUES (:name, :path)", array(':name' => $o[0], ':path' => $o[1]));
  237. }
  238. }
  239. /**
  240. * This is the required update for tripal_cv when upgrading from Drupal core API 6.x.
  241. *
  242. */
  243. function tripal_cv_update_7200() {
  244. // Make sure we have the full API loaded this will help during a
  245. // site upgrade when the tripal_core module is disabled.
  246. module_load_include('module', 'tripal_core', 'tripal_core');
  247. tripal_core_import_api();
  248. // add in the new tripal_cv_defaults table
  249. try {
  250. $schema = array();
  251. tripal_cv_get_tripal_cv_defaults_table($schema);
  252. db_create_table('tripal_cv_defaults', $schema['tripal_cv_defaults']);
  253. }
  254. catch (\PDOException $e) {
  255. $error = $e->getMessage();
  256. throw new DrupalUpdateException('Failed to create tripal_cv_defaults table: '. $error);
  257. }
  258. }
  259. /**
  260. * Fixes an error with the materialized view installation
  261. *
  262. */
  263. function tripal_cv_update_7201() {
  264. // Make sure we have the full API loaded this will help during a
  265. // site upgrade when the tripal_core module is disabled.
  266. module_load_include('module', 'tripal_core', 'tripal_core');
  267. tripal_core_import_api();
  268. // there is a bug in the Tripal v2.0-alpha release that didn't add the
  269. // materialized view schema to the mviews table.
  270. // get the schema for the materialized view from the custom_tables table
  271. // as there is a copy there, but only if the schema is missing from the
  272. // materialized view table
  273. $view_name = 'cv_root_mview';
  274. $schema = db_select('tripal_mviews', 'tm')
  275. ->fields('tm', array('mv_schema'))
  276. ->condition('name', $view_name)
  277. ->execute()
  278. ->fetchField();
  279. if (!$schema or $schema == 'Array') {
  280. $schema = db_select('tripal_custom_tables', 'tct')
  281. ->fields('tct', array('schema'))
  282. ->condition('table_name', $view_name)
  283. ->execute()
  284. ->fetchField();
  285. $schema_str = var_export(unserialize($schema), TRUE);
  286. $schema_str = preg_replace('/=>\s+\n\s+array/', '=> array', $schema_str);
  287. db_update('tripal_mviews')
  288. ->fields(array(
  289. 'mv_schema' => $schema_str
  290. ))
  291. ->condition('name', $view_name)
  292. ->execute();
  293. }
  294. }
  295. /**
  296. * Changes the URLs for OBOs.
  297. */
  298. function tripal_cv_update_7202() {
  299. // Make sure we have the full API loaded this will help during a
  300. // site upgrade when the tripal_core module is disabled.
  301. module_load_include('module', 'tripal_core', 'tripal_core');
  302. tripal_core_import_api();
  303. // add in the new tripal_cv_defaults table
  304. try {
  305. // Convert the OBO's to their appropriate PURL link
  306. db_update('tripal_cv_obo')
  307. ->fields(array(
  308. 'path' => 'http://purl.obolibrary.org/obo/so.obo',
  309. ))
  310. ->condition('name', 'Sequence Ontology')
  311. ->execute();
  312. db_update('tripal_cv_obo')
  313. ->fields(array(
  314. 'path' => 'http://purl.obolibrary.org/obo/go.obo',
  315. ))
  316. ->condition('name', 'Gene Ontology')
  317. ->execute();
  318. db_update('tripal_cv_obo')
  319. ->fields(array(
  320. 'name' => 'Relationship Ontology (legacy)',
  321. 'path' => '{tripal_cv}/files/legacy_ro.obo',
  322. ))
  323. ->condition('name', 'Relationship Ontology')
  324. ->execute();
  325. }
  326. catch (\PDOException $e) {
  327. $error = $e->getMessage();
  328. throw new DrupalUpdateException('Failed to create tripal_cv_defaults table: '. $error);
  329. }
  330. }
  331. /**
  332. * Implementation of hook_update_dependencies().
  333. *
  334. * It specifies a list of other modules whose updates must be run prior to
  335. * this one. It also ensures the the Tripal API is in scope for site
  336. * upgrades when tripal_core is disabled.
  337. */
  338. function tripal_cv_update_dependencies() {
  339. $dependencies = array();
  340. return $dependencies;
  341. }