tripal_cv.install 8.6 KB

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