tripal_cv.install 8.5 KB

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