tripal_cv.install 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. 'unique keys' => array(
  161. 'tripal_cv_defaults_unq1' => array('table_name', 'field_name', 'cv_id'),
  162. ),
  163. 'primary key' => array('cv_default_id')
  164. );
  165. }
  166. /**
  167. * Add a materialized view of root terms for all chado cvs. This is needed for viewing cv trees
  168. *
  169. * @ingroup tripal_cv
  170. */
  171. function tripal_cv_add_cv_root_mview() {
  172. $mv_name = 'cv_root_mview';
  173. $comment = 'A list of the root terms for all controlled vocabularies. This is needed for viewing CV trees';
  174. $schema = array(
  175. 'table' => $mv_name,
  176. 'description' => $comment,
  177. 'fields' => array(
  178. 'name' => array(
  179. 'type' => 'varchar',
  180. 'length' => 255,
  181. 'not null' => TRUE,
  182. ),
  183. 'cvterm_id' => array(
  184. 'type' => 'int',
  185. 'not null' => TRUE,
  186. ),
  187. 'cv_id' => array(
  188. 'type' => 'int',
  189. 'not null' => TRUE,
  190. ),
  191. 'cv_name' => array(
  192. 'type' => 'varchar',
  193. 'length' => 255,
  194. 'not null' => TRUE,
  195. ),
  196. ),
  197. 'indexes' => array(
  198. 'cv_root_mview_indx1' => array('cvterm_id'),
  199. 'cv_root_mview_indx2' => array('cv_id'),
  200. ),
  201. );
  202. $sql = "
  203. SELECT DISTINCT CVT.name,CVT.cvterm_id, CV.cv_id, CV.name
  204. FROM cvterm_relationship CVTR
  205. INNER JOIN cvterm CVT on CVTR.object_id = CVT.cvterm_id
  206. INNER JOIN cv CV on CV.cv_id = CVT.cv_id
  207. WHERE CVTR.object_id not in
  208. (SELECT subject_id FROM cvterm_relationship)
  209. ";
  210. // Create the MView
  211. tripal_add_mview($mv_name, 'tripal_cv', $schema, $sql, $comment);
  212. }
  213. /**
  214. * Add's defaults to the tripal_cv_obo table
  215. *
  216. * @ingroup tripal_cv
  217. */
  218. function tripal_cv_add_obo_defaults() {
  219. // insert commonly used ontologies into the tables
  220. $ontologies = array(
  221. array('Chado Feature Properties', drupal_get_path('module', 'tripal_cv') . '/feature_property.obo'),
  222. array('Relationship Ontology', 'http://www.obofoundry.org/ro/ro.obo'),
  223. array('Sequence Ontology', 'http://song.cvs.sourceforge.net/*checkout*/song/ontology/so.obo'),
  224. array('Gene Ontology', 'http://www.geneontology.org/ontology/gene_ontology.obo'),
  225. array('Cell Ontology', 'http://obo.cvs.sourceforge.net/obo/obo/ontology/anatomy/cell_type/cell.obo?rev=HEAD'),
  226. array('Plant Structure Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_anatomy.obo?view=co'),
  227. array('Plant Growth and Development Stages Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_temporal.obo?view=co')
  228. );
  229. foreach ($ontologies as $o) {
  230. db_query("INSERT INTO {tripal_cv_obo} (name,path) VALUES (:name, :path)", array(':name' => $o[0], ':path' => $o[1]));
  231. }
  232. }
  233. /**
  234. * This is the required update for tripal_cv when upgrading from Drupal core API 6.x.
  235. *
  236. */
  237. function tripal_cv_update_7200() {
  238. // add in the new tripal_cv_defaults table
  239. try {
  240. $schema = array();
  241. tripal_cv_get_tripal_cv_defaults_table($schema);
  242. db_create_table('tripal_cv_defaults', $schema['tripal_cv_defaults']);
  243. }
  244. catch (\PDOException $e) {
  245. $error = $e->getMessage();
  246. throw new DrupalUpdateException('Failed to create tripal_cv_defaults table: '. $error);
  247. }
  248. }