tripal_cv.install 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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_views_admin_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. }
  103. /**
  104. * Table definition for the tripal_cv_obo table
  105. * @param $schema
  106. */
  107. function tripal_cv_get_tripal_cv_obo_table(&$schema) {
  108. $schema['tripal_cv_obo'] = array(
  109. 'fields' => array(
  110. 'obo_id' => array(
  111. 'type' => 'serial',
  112. 'unsigned' => TRUE,
  113. 'not null' => TRUE
  114. ),
  115. 'name' => array(
  116. 'type' => 'varchar',
  117. 'length' => 255
  118. ),
  119. 'path' => array(
  120. 'type' => 'varchar',
  121. 'length' => 1024
  122. ),
  123. ),
  124. 'indexes' => array(
  125. 'tripal_cv_obo_idx1' => array('obo_id'),
  126. ),
  127. 'primary key' => array('obo_id'),
  128. );
  129. }
  130. /**
  131. * * Table definition for the tripal_cv_defaults table
  132. * @param unknown $schema
  133. */
  134. function tripal_cv_get_tripal_cv_defaults_table(&$schema) {
  135. $schema['tripal_cv_defaults'] = array(
  136. 'fields' => array(
  137. 'cv_default_id' => array(
  138. 'type' => 'serial',
  139. 'unsigned' => TRUE,
  140. 'not null' => TRUE
  141. ),
  142. 'table_name' => array(
  143. 'type' => 'varchar',
  144. 'length' => 128,
  145. 'not null' => TRUE,
  146. ),
  147. 'field_name' => array(
  148. 'type' => 'varchar',
  149. 'length' => 128,
  150. 'not null' => TRUE,
  151. ),
  152. 'cv_id' => array(
  153. 'type' => 'int',
  154. 'not null' => TRUE,
  155. )
  156. ),
  157. 'indexes' => array(
  158. 'tripal_cv_defaults_idx1' => array('table_name', 'field_name'),
  159. ),
  160. 'primary key' => array('cv_default_id')
  161. );
  162. }
  163. /**
  164. * Add a materialized view of root terms for all chado cvs. This is needed for viewing cv trees
  165. *
  166. * @ingroup tripal_cv
  167. */
  168. function tripal_cv_add_cv_root_mview() {
  169. $mv_name = 'cv_root_mview';
  170. $comment = 'A list of the root terms for all controlled vocabularies. This is needed for viewing CV trees';
  171. $schema = array(
  172. 'table' => $mv_name,
  173. 'description' => $comment,
  174. 'fields' => array(
  175. 'name' => array(
  176. 'type' => 'varchar',
  177. 'length' => 255,
  178. 'not null' => TRUE,
  179. ),
  180. 'cvterm_id' => array(
  181. 'type' => 'int',
  182. 'not null' => TRUE,
  183. ),
  184. 'cv_id' => array(
  185. 'type' => 'int',
  186. 'not null' => TRUE,
  187. ),
  188. 'cv_name' => array(
  189. 'type' => 'varchar',
  190. 'length' => 255,
  191. 'not null' => TRUE,
  192. ),
  193. ),
  194. 'indexes' => array(
  195. 'cv_root_mview_indx1' => array('cvterm_id'),
  196. 'cv_root_mview_indx2' => array('cv_id'),
  197. ),
  198. );
  199. $sql = "
  200. SELECT DISTINCT CVT.name,CVT.cvterm_id, CV.cv_id, CV.name
  201. FROM cvterm_relationship CVTR
  202. INNER JOIN cvterm CVT on CVTR.object_id = CVT.cvterm_id
  203. INNER JOIN cv CV on CV.cv_id = CVT.cv_id
  204. WHERE CVTR.object_id not in
  205. (SELECT subject_id FROM cvterm_relationship)
  206. ";
  207. // Create the MView
  208. tripal_add_mview($mv_name, 'tripal_cv', $schema, $sql, $comment);
  209. }
  210. /**
  211. * Add's defaults to the tripal_cv_obo table
  212. *
  213. * @ingroup tripal_cv
  214. */
  215. function tripal_cv_add_obo_defaults() {
  216. // insert commonly used ontologies into the tables
  217. $ontologies = array(
  218. array('Chado Feature Properties', drupal_get_path('module', 'tripal_cv') . '/feature_property.obo'),
  219. array('Relationship Ontology', 'http://www.obofoundry.org/ro/ro.obo'),
  220. array('Sequence Ontology', 'http://song.cvs.sourceforge.net/*checkout*/song/ontology/so.obo'),
  221. array('Gene Ontology', 'http://www.geneontology.org/ontology/gene_ontology.obo'),
  222. array('Cell Ontology', 'http://obo.cvs.sourceforge.net/obo/obo/ontology/anatomy/cell_type/cell.obo?rev=HEAD'),
  223. array('Plant Structure Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_anatomy.obo?view=co'),
  224. array('Plant Growth and Development Stages Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_temporal.obo?view=co')
  225. );
  226. foreach ($ontologies as $o) {
  227. db_query("INSERT INTO {tripal_cv_obo} (name,path) VALUES (:name, :path)", array(':name' => $o[0], ':path' => $o[1]));
  228. }
  229. }
  230. /**
  231. * This is the required update for tripal_cv when upgrading from Drupal core API 6.x.
  232. *
  233. */
  234. function tripal_cv_update_7200() {
  235. // add in the new tripal_cv_defaults table
  236. try {
  237. $schema = array();
  238. tripal_cv_get_tripal_cv_defaults_table($schema);
  239. db_create_table('tripal_cv_defaults', $schema['tripal_cv_defaults']);
  240. }
  241. catch (\PDOException $e) {
  242. $error = $e->getMessage();
  243. throw new DrupalUpdateException('Failed to create tripal_cv_defaults table: '. $error);
  244. }
  245. }