tripal_cv.install 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * @file
  4. * Contains functions executed only on install/uninstall of this module
  5. */
  6. /**
  7. * Disable default views when module is disabled
  8. */
  9. function tripal_cv_disable() {
  10. // Disable all default views provided by this module
  11. require_once("tripal_cv.views_default.inc");
  12. $views = tripal_cv_views_default_views();
  13. foreach (array_keys($views) as $view_name) {
  14. tripal_views_admin_disable_view($view_name,FALSE,array('suppress_error' => TRUE));
  15. }
  16. }
  17. /**
  18. * Implementation of hook_requirements().
  19. */
  20. function tripal_cv_requirements($phase) {
  21. $requirements = array();
  22. if ($phase == 'install') {
  23. // make sure chado is installed
  24. if (!$GLOBALS["chado_is_installed"]) {
  25. $requirements ['tripal_cv'] = array(
  26. 'title' => "tripal_cv",
  27. 'value' => "ERROR: Chado must be installed before this module can be enabled",
  28. 'severity' => REQUIREMENT_ERROR,
  29. );
  30. }
  31. }
  32. return $requirements;
  33. }
  34. /**
  35. * Implementation of hook_install().
  36. *
  37. * @ingroup tripal_cv
  38. */
  39. function tripal_cv_install() {
  40. // create the module's data directory
  41. tripal_create_moddir('tripal_cv');
  42. // add the cv_root_mview
  43. tripal_cv_add_cv_root_mview();
  44. // add defaults to the tables that correlate OBO files/references with a chado CV
  45. tripal_cv_add_obo_defaults();
  46. // create the temp table we will use for loading OBO files
  47. tripal_cv_create_tripal_obo_temp();
  48. }
  49. /**
  50. * Implementation of hook_uninstall().
  51. *
  52. * @ingroup tripal_cv
  53. */
  54. function tripal_cv_uninstall() {
  55. // remove the materialized view
  56. if ($mview = tripal_mviews_get_mview_id('cv_root_mview')) {
  57. tripal_mviews_action('delete', $mview);
  58. }
  59. // drop the tripal_obo_temp table
  60. if (db_table_exists('chado.tripal_obo_temp')) {
  61. $sql = "DROP TABLE {tripal_obo_temp}";
  62. chado_query($sql);
  63. }
  64. }
  65. /**
  66. *
  67. */
  68. function tripal_cv_create_tripal_obo_temp() {
  69. // the tripal_obo_temp table is used for temporary housing of records when loading OBO files
  70. // we create it here using plain SQL because we want it to be in the chado schema but we
  71. // do not want to use the Tripal Custom Table API because we don't want it to appear in the
  72. // list of custom tables. It needs to be available for the Tripal Chado API so we create it
  73. // here and then define it in the tripal_cv/api/tripal_cv.schema.api.inc
  74. if (!db_table_exists('chado.tripal_obo_temp')) {
  75. $sql = "
  76. CREATE TABLE {tripal_obo_temp} (
  77. id character varying(255) NOT NULL,
  78. stanza text NOT NULL,
  79. type character varying(50) NOT NULL,
  80. CONSTRAINT tripal_obo_temp_uq0 UNIQUE (id)
  81. );
  82. ";
  83. chado_query($sql);
  84. $sql = "CREATE INDEX tripal_obo_temp_idx0 ON {tripal_obo_temp} USING btree (id)";
  85. chado_query($sql);
  86. $sql = "CREATE INDEX tripal_obo_temp_idx1 ON {tripal_obo_temp} USING btree (type)";
  87. chado_query($sql);
  88. }
  89. }
  90. /**
  91. * Implementation of hook_schema().
  92. *
  93. * @ingroup tripal_cv
  94. */
  95. function tripal_cv_schema() {
  96. $schema['tripal_cv_obo'] = array(
  97. 'fields' => array(
  98. 'obo_id' => array(
  99. 'type' => 'serial',
  100. 'unsigned' => TRUE,
  101. 'not null' => TRUE
  102. ),
  103. 'name' => array(
  104. 'type' => 'varchar',
  105. 'length' => 255
  106. ),
  107. 'path' => array(
  108. 'type' => 'varchar',
  109. 'length' => 1024
  110. ),
  111. ),
  112. 'indexes' => array(
  113. 'tripal_cv_obo_idx1' => array('obo_id'),
  114. ),
  115. 'primary key' => array('obo_id'),
  116. );
  117. return $schema;
  118. }
  119. /**
  120. *
  121. * @ingroup tripal_cv
  122. */
  123. function tripal_cv_add_cv_root_mview() {
  124. $mv_name = 'cv_root_mview';
  125. $comment = 'A list of the root terms for all controlled vocabularies. This is needed for viewing CV trees';
  126. $schema = array(
  127. 'table' => $mv_name,
  128. 'description' => $comment,
  129. 'fields' => array(
  130. 'name' => array(
  131. 'type' => 'varchar',
  132. 'length' => 255,
  133. 'not null' => TRUE,
  134. ),
  135. 'cvterm_id' => array(
  136. 'type' => 'int',
  137. 'not null' => TRUE,
  138. ),
  139. 'cv_id' => array(
  140. 'type' => 'int',
  141. 'not null' => TRUE,
  142. ),
  143. 'cv_name' => array(
  144. 'type' => 'varchar',
  145. 'length' => 255,
  146. 'not null' => TRUE,
  147. ),
  148. ),
  149. 'indexes' => array(
  150. 'cv_root_mview_indx1' => array('cvterm_id'),
  151. 'cv_root_mview_indx2' => array('cv_id'),
  152. ),
  153. );
  154. $sql = "
  155. SELECT DISTINCT CVT.name,CVT.cvterm_id, CV.cv_id, CV.name
  156. FROM cvterm_relationship CVTR
  157. INNER JOIN cvterm CVT on CVTR.object_id = CVT.cvterm_id
  158. INNER JOIN cv CV on CV.cv_id = CVT.cv_id
  159. WHERE CVTR.object_id not in
  160. (SELECT subject_id FROM cvterm_relationship)
  161. ";
  162. // Create the MView
  163. tripal_add_mview($mv_name, 'tripal_cv', $schema, $sql, $comment);
  164. }
  165. /**
  166. * Add's defaults to the tripal_cv_obo table
  167. *
  168. * @ingroup tripal_cv
  169. */
  170. function tripal_cv_add_obo_defaults() {
  171. // insert commonly used ontologies into the tables
  172. $ontologies = array(
  173. array('Chado Feature Properties', drupal_get_path('module', 'tripal_cv') . '/feature_property.obo'),
  174. array('Relationship Ontology', 'http://www.obofoundry.org/ro/ro.obo'),
  175. array('Sequence Ontology', 'http://song.cvs.sourceforge.net/*checkout*/song/ontology/so.obo'),
  176. array('Gene Ontology', 'http://www.geneontology.org/ontology/gene_ontology.obo'),
  177. array('Cell Ontology', 'http://obo.cvs.sourceforge.net/obo/obo/ontology/anatomy/cell_type/cell.obo?rev=HEAD'),
  178. array('Plant Structure Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_anatomy.obo?view=co'),
  179. array('Plant Growth and Development Stages Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_temporal.obo?view=co')
  180. );
  181. foreach ($ontologies as $o) {
  182. db_query("INSERT INTO {tripal_cv_obo} (name,path) VALUES (:name, :path)", array(':name' => $o[0], ':path' => $o[1]));
  183. }
  184. }