tripal_cv.install 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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['tripal_cv_obo'] = array(
  100. 'fields' => array(
  101. 'obo_id' => array(
  102. 'type' => 'serial',
  103. 'unsigned' => TRUE,
  104. 'not null' => TRUE
  105. ),
  106. 'name' => array(
  107. 'type' => 'varchar',
  108. 'length' => 255
  109. ),
  110. 'path' => array(
  111. 'type' => 'varchar',
  112. 'length' => 1024
  113. ),
  114. ),
  115. 'indexes' => array(
  116. 'tripal_cv_obo_idx1' => array('obo_id'),
  117. ),
  118. 'primary key' => array('obo_id'),
  119. );
  120. return $schema;
  121. }
  122. /**
  123. * Add a materialized view of root terms for all chado cvs. This is needed for viewing cv trees
  124. *
  125. * @ingroup tripal_cv
  126. */
  127. function tripal_cv_add_cv_root_mview() {
  128. $mv_name = 'cv_root_mview';
  129. $comment = 'A list of the root terms for all controlled vocabularies. This is needed for viewing CV trees';
  130. $schema = array(
  131. 'table' => $mv_name,
  132. 'description' => $comment,
  133. 'fields' => array(
  134. 'name' => array(
  135. 'type' => 'varchar',
  136. 'length' => 255,
  137. 'not null' => TRUE,
  138. ),
  139. 'cvterm_id' => array(
  140. 'type' => 'int',
  141. 'not null' => TRUE,
  142. ),
  143. 'cv_id' => array(
  144. 'type' => 'int',
  145. 'not null' => TRUE,
  146. ),
  147. 'cv_name' => array(
  148. 'type' => 'varchar',
  149. 'length' => 255,
  150. 'not null' => TRUE,
  151. ),
  152. ),
  153. 'indexes' => array(
  154. 'cv_root_mview_indx1' => array('cvterm_id'),
  155. 'cv_root_mview_indx2' => array('cv_id'),
  156. ),
  157. );
  158. $sql = "
  159. SELECT DISTINCT CVT.name,CVT.cvterm_id, CV.cv_id, CV.name
  160. FROM cvterm_relationship CVTR
  161. INNER JOIN cvterm CVT on CVTR.object_id = CVT.cvterm_id
  162. INNER JOIN cv CV on CV.cv_id = CVT.cv_id
  163. WHERE CVTR.object_id not in
  164. (SELECT subject_id FROM cvterm_relationship)
  165. ";
  166. // Create the MView
  167. tripal_add_mview($mv_name, 'tripal_cv', $schema, $sql, $comment);
  168. }
  169. /**
  170. * Add's defaults to the tripal_cv_obo table
  171. *
  172. * @ingroup tripal_cv
  173. */
  174. function tripal_cv_add_obo_defaults() {
  175. // insert commonly used ontologies into the tables
  176. $ontologies = array(
  177. array('Chado Feature Properties', drupal_get_path('module', 'tripal_cv') . '/feature_property.obo'),
  178. array('Relationship Ontology', 'http://www.obofoundry.org/ro/ro.obo'),
  179. array('Sequence Ontology', 'http://song.cvs.sourceforge.net/*checkout*/song/ontology/so.obo'),
  180. array('Gene Ontology', 'http://www.geneontology.org/ontology/gene_ontology.obo'),
  181. array('Cell Ontology', 'http://obo.cvs.sourceforge.net/obo/obo/ontology/anatomy/cell_type/cell.obo?rev=HEAD'),
  182. array('Plant Structure Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_anatomy.obo?view=co'),
  183. array('Plant Growth and Development Stages Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_temporal.obo?view=co')
  184. );
  185. foreach ($ontologies as $o) {
  186. db_query("INSERT INTO {tripal_cv_obo} (name,path) VALUES (:name, :path)", array(':name' => $o[0], ':path' => $o[1]));
  187. }
  188. }