tripal_cv.install 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. // drop the tripal_obo_temp table
  56. if (db_table_exists('chado.tripal_obo_temp')) {
  57. $sql = "DROP TABLE chado.tripal_obo_temp";
  58. db_query($sql);
  59. }
  60. }
  61. /**
  62. *
  63. */
  64. function tripal_cv_create_tripal_obo_temp() {
  65. // the tripal_obo_temp table is used for temporary housing of records when loading OBO files
  66. // we create it here using plain SQL because we want it to be in the chado schema but we
  67. // do not want to use the Tripal Custom Table API because we don't want it to appear in the
  68. // list of custom tables. It needs to be available for the Tripal Chado API so we create it
  69. // here and then define it in the tripal_cv/api/tripal_cv.schema.api.inc
  70. if (!db_table_exists('chado.tripal_obo_temp')) {
  71. $sql = "
  72. CREATE TABLE {tripal_obo_temp} (
  73. id character varying(255) NOT NULL,
  74. stanza text NOT NULL,
  75. type character varying(50) NOT NULL,
  76. CONSTRAINT tripal_obo_temp_uq0 UNIQUE (id)
  77. );
  78. ";
  79. chado_query($sql);
  80. $sql = "CREATE INDEX tripal_obo_temp_idx0 ON {tripal_obo_temp} USING btree (id)";
  81. chado_query($sql);
  82. $sql = "CREATE INDEX tripal_obo_temp_idx1 ON {tripal_obo_temp} USING btree (type)";
  83. chado_query($sql);
  84. }
  85. }
  86. /**
  87. * Implementation of hook_schema().
  88. *
  89. * @ingroup tripal_cv
  90. */
  91. function tripal_cv_schema() {
  92. $schema['tripal_cv_obo'] = array(
  93. 'fields' => array(
  94. 'obo_id' => array(
  95. 'type' => 'serial',
  96. 'unsigned' => TRUE,
  97. 'not null' => TRUE
  98. ),
  99. 'name' => array(
  100. 'type' => 'varchar',
  101. 'length' => 255
  102. ),
  103. 'path' => array(
  104. 'type' => 'varchar',
  105. 'length' => 1024
  106. ),
  107. ),
  108. 'indexes' => array(
  109. 'tripal_cv_obo_idx1' => array('obo_id'),
  110. ),
  111. 'primary key' => array('obo_id'),
  112. );
  113. return $schema;
  114. }
  115. /**
  116. *
  117. * @ingroup tripal_cv
  118. */
  119. function tripal_cv_add_cv_root_mview() {
  120. $mv_name = 'cv_root_mview';
  121. $comment = 'A list of the root terms for all controlled vocabularies. This is needed for viewing CV trees';
  122. $schema = array(
  123. 'table' => $mv_name,
  124. 'description' => $comment,
  125. 'fields' => array(
  126. 'name' => array(
  127. 'type' => 'varchar',
  128. 'length' => 255,
  129. 'not null' => TRUE,
  130. ),
  131. 'cvterm_id' => array(
  132. 'type' => 'int',
  133. 'not null' => TRUE,
  134. ),
  135. 'cv_id' => array(
  136. 'type' => 'int',
  137. 'not null' => TRUE,
  138. ),
  139. 'cv_name' => array(
  140. 'type' => 'varchar',
  141. 'length' => 255,
  142. 'not null' => TRUE,
  143. ),
  144. ),
  145. 'indexes' => array(
  146. 'cv_root_mview_indx1' => array('cvterm_id'),
  147. 'cv_root_mview_indx2' => array('cv_id'),
  148. ),
  149. );
  150. $sql = "
  151. SELECT DISTINCT CVT.name,CVT.cvterm_id, CV.cv_id, CV.name
  152. FROM cvterm_relationship CVTR
  153. INNER JOIN cvterm CVT on CVTR.object_id = CVT.cvterm_id
  154. INNER JOIN cv CV on CV.cv_id = CVT.cv_id
  155. WHERE CVTR.object_id not in
  156. (SELECT subject_id FROM cvterm_relationship)
  157. ";
  158. // Create the MView
  159. tripal_add_mview($mv_name, 'tripal_cv', $schema, $sql, $comment);
  160. }
  161. /**
  162. * Add's defaults to the tripal_cv_obo table
  163. *
  164. * @ingroup tripal_cv
  165. */
  166. function tripal_cv_add_obo_defaults() {
  167. // insert commonly used ontologies into the tables
  168. $ontologies = array(
  169. array('Chado Feature Properties', drupal_get_path('module', 'tripal_cv') . '/feature_property.obo'),
  170. array('Relationship Ontology', 'http://www.obofoundry.org/ro/ro.obo'),
  171. array('Sequence Ontology', 'http://song.cvs.sourceforge.net/*checkout*/song/ontology/so.obo'),
  172. array('Gene Ontology', 'http://www.geneontology.org/ontology/gene_ontology.obo'),
  173. array('Cell Ontology', 'http://obo.cvs.sourceforge.net/obo/obo/ontology/anatomy/cell_type/cell.obo?rev=HEAD'),
  174. array('Plant Structure Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_anatomy.obo?view=co'),
  175. array('Plant Growth and Development Stages Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_temporal.obo?view=co')
  176. );
  177. foreach ($ontologies as $o) {
  178. db_query("INSERT INTO {tripal_cv_obo} (name,path) VALUES (:name, :path)", array(':name' => $o[0], ':path' => $o[1]));
  179. }
  180. }