tripal_cv.install 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. // @todo: Remove hardcoding of chado schema name.
  65. if (db_table_exists('chado.tripal_obo_temp')) {
  66. $sql = "DROP TABLE {tripal_obo_temp}";
  67. chado_query($sql);
  68. }
  69. }
  70. /**
  71. * Creates a temporary table to store obo details while loading an obo file
  72. *
  73. * @ingroup tripal_cv
  74. */
  75. function tripal_cv_create_tripal_obo_temp() {
  76. // the tripal_obo_temp table is used for temporary housing of records when loading OBO files
  77. // we create it here using plain SQL because we want it to be in the chado schema but we
  78. // do not want to use the Tripal Custom Table API because we don't want it to appear in the
  79. // list of custom tables. It needs to be available for the Tripal Chado API so we create it
  80. // here and then define it in the tripal_cv/api/tripal_cv.schema.api.inc
  81. // @todo: Remove hardcoding of chado schema name.
  82. if (!db_table_exists('chado.tripal_obo_temp')) {
  83. $sql = "
  84. CREATE TABLE {tripal_obo_temp} (
  85. id character varying(255) NOT NULL,
  86. stanza text NOT NULL,
  87. type character varying(50) NOT NULL,
  88. CONSTRAINT tripal_obo_temp_uq0 UNIQUE (id)
  89. );
  90. ";
  91. chado_query($sql);
  92. $sql = "CREATE INDEX tripal_obo_temp_idx0 ON {tripal_obo_temp} USING btree (id)";
  93. chado_query($sql);
  94. $sql = "CREATE INDEX tripal_obo_temp_idx1 ON {tripal_obo_temp} USING btree (type)";
  95. chado_query($sql);
  96. }
  97. }
  98. /**
  99. * Implementation of hook_schema().
  100. *
  101. * @ingroup tripal_cv
  102. */
  103. function tripal_cv_schema() {
  104. $schema = array();
  105. tripal_cv_get_tripal_cv_obo_table($schema);
  106. tripal_cv_get_tripal_cv_defaults_table($schema);
  107. return $schema;
  108. }
  109. /**
  110. * Table definition for the tripal_cv_obo table
  111. * @param $schema
  112. */
  113. function tripal_cv_get_tripal_cv_obo_table(&$schema) {
  114. $schema['tripal_cv_obo'] = array(
  115. 'fields' => array(
  116. 'obo_id' => array(
  117. 'type' => 'serial',
  118. 'unsigned' => TRUE,
  119. 'not null' => TRUE
  120. ),
  121. 'name' => array(
  122. 'type' => 'varchar',
  123. 'length' => 255
  124. ),
  125. 'path' => array(
  126. 'type' => 'varchar',
  127. 'length' => 1024
  128. ),
  129. ),
  130. 'indexes' => array(
  131. 'tripal_cv_obo_idx1' => array('obo_id'),
  132. ),
  133. 'primary key' => array('obo_id'),
  134. );
  135. }
  136. /**
  137. * * Table definition for the tripal_cv_defaults table
  138. * @param unknown $schema
  139. */
  140. function tripal_cv_get_tripal_cv_defaults_table(&$schema) {
  141. $schema['tripal_cv_defaults'] = array(
  142. 'fields' => array(
  143. 'cv_default_id' => array(
  144. 'type' => 'serial',
  145. 'unsigned' => TRUE,
  146. 'not null' => TRUE
  147. ),
  148. 'table_name' => array(
  149. 'type' => 'varchar',
  150. 'length' => 128,
  151. 'not null' => TRUE,
  152. ),
  153. 'field_name' => array(
  154. 'type' => 'varchar',
  155. 'length' => 128,
  156. 'not null' => TRUE,
  157. ),
  158. 'cv_id' => array(
  159. 'type' => 'int',
  160. 'not null' => TRUE,
  161. )
  162. ),
  163. 'indexes' => array(
  164. 'tripal_cv_defaults_idx1' => array('table_name', 'field_name'),
  165. ),
  166. 'unique keys' => array(
  167. 'tripal_cv_defaults_unq1' => array('table_name', 'field_name', 'cv_id'),
  168. ),
  169. 'primary key' => array('cv_default_id')
  170. );
  171. }
  172. /**
  173. * Add a materialized view of root terms for all chado cvs. This is needed for viewing cv trees
  174. *
  175. * @ingroup tripal_cv
  176. */
  177. function tripal_cv_add_cv_root_mview() {
  178. $mv_name = 'cv_root_mview';
  179. $comment = 'A list of the root terms for all controlled vocabularies. This is needed for viewing CV trees';
  180. $schema = array(
  181. 'table' => $mv_name,
  182. 'description' => $comment,
  183. 'fields' => array(
  184. 'name' => array(
  185. 'type' => 'varchar',
  186. 'length' => 255,
  187. 'not null' => TRUE,
  188. ),
  189. 'cvterm_id' => array(
  190. 'size' => 'big',
  191. 'type' => 'int',
  192. 'not null' => TRUE,
  193. ),
  194. 'cv_id' => array(
  195. 'size' => 'big',
  196. 'type' => 'int',
  197. 'not null' => TRUE,
  198. ),
  199. 'cv_name' => array(
  200. 'type' => 'varchar',
  201. 'length' => 255,
  202. 'not null' => TRUE,
  203. ),
  204. ),
  205. 'indexes' => array(
  206. 'cv_root_mview_indx1' => array('cvterm_id'),
  207. 'cv_root_mview_indx2' => array('cv_id'),
  208. ),
  209. );
  210. $sql = "
  211. SELECT DISTINCT CVT.name,CVT.cvterm_id, CV.cv_id, CV.name
  212. FROM cvterm_relationship CVTR
  213. INNER JOIN cvterm CVT on CVTR.object_id = CVT.cvterm_id
  214. INNER JOIN cv CV on CV.cv_id = CVT.cv_id
  215. WHERE CVTR.object_id not in
  216. (SELECT subject_id FROM cvterm_relationship)
  217. ";
  218. // Create the MView
  219. tripal_add_mview($mv_name, 'tripal_cv', $schema, $sql, $comment);
  220. }
  221. /**
  222. * Add's defaults to the tripal_cv_obo table
  223. *
  224. * @ingroup tripal_cv
  225. */
  226. function tripal_cv_add_obo_defaults() {
  227. // Insert commonly used ontologies into the tables.
  228. $ontologies = array(
  229. array('Relationship Ontology', 'http://purl.obolibrary.org/obo/ro.obo'),
  230. // array('Relationship Ontology (older deprecated version)', 'http://www.obofoundry.org/ro/ro.obo'),
  231. array('Sequence Ontology', 'https://raw.githubusercontent.com/The-Sequence-Ontology/SO-Ontologies/master/so-xp-simple.obo'),
  232. array('Gene Ontology', 'http://www.geneontology.org/ontology/gene_ontology.obo'),
  233. // array('Cell Ontology', 'https://raw.githubusercontent.com/obophenotype/cell-ontology/master/cl.obo'),
  234. // array('Plant Structure Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_anatomy.obo?view=co'),
  235. // array('Plant Growth and Development Stages Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_temporal.obo?view=co')
  236. );
  237. foreach ($ontologies as $o) {
  238. db_query("INSERT INTO {tripal_cv_obo} (name,path) VALUES (:name, :path)", array(':name' => $o[0], ':path' => $o[1]));
  239. }
  240. }
  241. /**
  242. * This is the required update for tripal_cv when upgrading from Drupal core API 6.x.
  243. *
  244. */
  245. function tripal_cv_update_7200() {
  246. // Make sure we have the full API loaded this will help during a
  247. // site upgrade when the tripal_core module is disabled.
  248. module_load_include('module', 'tripal_core', 'tripal_core');
  249. tripal_core_import_api();
  250. // add in the new tripal_cv_defaults table
  251. try {
  252. $schema = array();
  253. tripal_cv_get_tripal_cv_defaults_table($schema);
  254. db_create_table('tripal_cv_defaults', $schema['tripal_cv_defaults']);
  255. }
  256. catch (\PDOException $e) {
  257. $error = $e->getMessage();
  258. throw new DrupalUpdateException('Failed to create tripal_cv_defaults table: '. $error);
  259. }
  260. }
  261. /**
  262. * Fixes an error with the materialized view installation
  263. *
  264. */
  265. function tripal_cv_update_7201() {
  266. // Make sure we have the full API loaded this will help during a
  267. // site upgrade when the tripal_core module is disabled.
  268. module_load_include('module', 'tripal_core', 'tripal_core');
  269. tripal_core_import_api();
  270. // there is a bug in the Tripal v2.0-alpha release that didn't add the
  271. // materialized view schema to the mviews table.
  272. // get the schema for the materialized view from the custom_tables table
  273. // as there is a copy there, but only if the schema is missing from the
  274. // materialized view table
  275. $view_name = 'cv_root_mview';
  276. $schema = db_select('tripal_mviews', 'tm')
  277. ->fields('tm', array('mv_schema'))
  278. ->condition('name', $view_name)
  279. ->execute()
  280. ->fetchField();
  281. if (!$schema or $schema == 'Array') {
  282. $schema = db_select('tripal_custom_tables', 'tct')
  283. ->fields('tct', array('schema'))
  284. ->condition('table_name', $view_name)
  285. ->execute()
  286. ->fetchField();
  287. $schema_str = var_export(unserialize($schema), TRUE);
  288. $schema_str = preg_replace('/=>\s+\n\s+array/', '=> array', $schema_str);
  289. db_update('tripal_mviews')
  290. ->fields(array(
  291. 'mv_schema' => $schema_str
  292. ))
  293. ->condition('name', $view_name)
  294. ->execute();
  295. }
  296. }
  297. /**
  298. * Implementation of hook_update_dependencies().
  299. *
  300. * It specifies a list of other modules whose updates must be run prior to
  301. * this one. It also ensures the the Tripal API is in scope for site
  302. * upgrades when tripal_core is disabled.
  303. */
  304. function tripal_cv_update_dependencies() {
  305. $dependencies = array();
  306. return $dependencies;
  307. }