tripal_cv.install 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 (db_table_exists('chado.tripal_obo_temp')) {
  65. $sql = "DROP TABLE chado.tripal_obo_temp";
  66. db_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 (!db_table_exists('chado.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. 'type' => 'int',
  189. 'not null' => TRUE,
  190. ),
  191. 'cv_id' => array(
  192. 'type' => 'int',
  193. 'not null' => TRUE,
  194. ),
  195. 'cv_name' => array(
  196. 'type' => 'varchar',
  197. 'length' => 255,
  198. 'not null' => TRUE,
  199. ),
  200. ),
  201. 'indexes' => array(
  202. 'cv_root_mview_indx1' => array('cvterm_id'),
  203. 'cv_root_mview_indx2' => array('cv_id'),
  204. ),
  205. );
  206. $sql = "
  207. SELECT DISTINCT CVT.name,CVT.cvterm_id, CV.cv_id, CV.name
  208. FROM cvterm_relationship CVTR
  209. INNER JOIN cvterm CVT on CVTR.object_id = CVT.cvterm_id
  210. INNER JOIN cv CV on CV.cv_id = CVT.cv_id
  211. WHERE CVTR.object_id not in
  212. (SELECT subject_id FROM cvterm_relationship)
  213. ";
  214. // Create the MView
  215. tripal_add_mview($mv_name, 'tripal_cv', $schema, $sql, $comment);
  216. }
  217. /**
  218. * Add's defaults to the tripal_cv_obo table
  219. *
  220. * @ingroup tripal_cv
  221. */
  222. function tripal_cv_add_obo_defaults() {
  223. // Insert commonly used ontologies into the tables.
  224. $ontologies = array(
  225. array('Relationship Ontology', 'http://purl.obolibrary.org/obo/ro.obo'),
  226. // array('Relationship Ontology (older deprecated version)', 'http://www.obofoundry.org/ro/ro.obo'),
  227. array('Sequence Ontology', 'https://github.com/The-Sequence-Ontology/SO-Ontologies/blob/master/so-xp-simple.obo'),
  228. array('Gene Ontology', 'http://www.geneontology.org/ontology/gene_ontology.obo'),
  229. // array('Cell Ontology', 'https://raw.githubusercontent.com/obophenotype/cell-ontology/master/cl.obo'),
  230. // array('Plant Structure Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_anatomy.obo?view=co'),
  231. // array('Plant Growth and Development Stages Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_temporal.obo?view=co')
  232. );
  233. foreach ($ontologies as $o) {
  234. db_query("INSERT INTO {tripal_cv_obo} (name,path) VALUES (:name, :path)", array(':name' => $o[0], ':path' => $o[1]));
  235. }
  236. }
  237. /**
  238. * This is the required update for tripal_cv when upgrading from Drupal core API 6.x.
  239. *
  240. */
  241. function tripal_cv_update_7200() {
  242. // add in the new tripal_cv_defaults table
  243. try {
  244. $schema = array();
  245. tripal_cv_get_tripal_cv_defaults_table($schema);
  246. db_create_table('tripal_cv_defaults', $schema['tripal_cv_defaults']);
  247. }
  248. catch (\PDOException $e) {
  249. $error = $e->getMessage();
  250. throw new DrupalUpdateException('Failed to create tripal_cv_defaults table: '. $error);
  251. }
  252. }
  253. /**
  254. * Fixes an error with the materialized view installation
  255. *
  256. */
  257. function tripal_cv_update_7201() {
  258. // there is a bug in the Tripal v2.0-alpha release that didn't add the
  259. // materialized view schema to the mviews table.
  260. // get the schema for the materialized view from the custom_tables table
  261. // as there is a copy there, but only if the schema is missing from the
  262. // materialized view table
  263. $view_name = 'cv_root_mview';
  264. $schema = db_select('tripal_mviews', 'tm')
  265. ->fields('tm', array('mv_schema'))
  266. ->condition('name', $view_name)
  267. ->execute()
  268. ->fetchField();
  269. if (!$schema or $schema == 'Array') {
  270. $schema = db_select('tripal_custom_tables', 'tct')
  271. ->fields('tct', array('schema'))
  272. ->condition('table_name', $view_name)
  273. ->execute()
  274. ->fetchField();
  275. $schema_str = var_export(unserialize($schema), TRUE);
  276. $schema_str = preg_replace('/=>\s+\n\s+array/', '=> array', $schema_str);
  277. db_update('tripal_mviews')
  278. ->fields(array(
  279. 'mv_schema' => $schema_str
  280. ))
  281. ->condition('name', $view_name)
  282. ->execute();
  283. }
  284. }