tripal_analysis.install 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * @file
  4. * Implements hooks from the Schema API
  5. */
  6. /**
  7. * Disable default views when module is disabled
  8. */
  9. function tripal_analysis_disable() {
  10. // Disable all default views provided by this module
  11. require_once("tripal_analysis.views_default.inc");
  12. $views = tripal_analysis_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_analysis_requirements($phase) {
  21. $requirements = array();
  22. if ($phase == 'install') {
  23. // make sure chado is installed
  24. if (!$GLOBALS["chado_is_installed"]) {
  25. $requirements ['tripal_analysis'] = array(
  26. 'title' => "tripal_analysis",
  27. 'value' => "ERROR: Chado most 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. function tripal_analysis_install() {
  38. // create the module's data directory
  39. tripal_create_moddir('tripal_analysis');
  40. // we may need the analysisfeatureprop table if it doesn't already exist
  41. tripal_analysis_create_analysisfeatureprop();
  42. // add cvterms
  43. tripal_analysis_add_cvterms();
  44. // add materialized views
  45. tripal_analysis_add_mview_analysis_organism();
  46. }
  47. /**
  48. * Implementation of hook_uninstall().
  49. */
  50. function tripal_analysis_uninstall() {
  51. // remove the materialized view
  52. if ($mview = tripal_mviews_get_mview_id('analysis_organism')) {
  53. tripal_mviews_action('delete', $mview);
  54. }
  55. }
  56. /**
  57. *
  58. */
  59. function tripal_analysis_create_analysisfeatureprop() {
  60. // Create analysisfeatureprop table in chado. This is needed for Chado
  61. // version 1.11, the table exists in Chado 1.2.
  62. if (!db_table_exists('chado.analysisfeatureprop')) {
  63. $sql = "
  64. CREATE TABLE {analysisfeatureprop} (
  65. analysisfeatureprop_id SERIAL PRIMARY KEY,
  66. analysisfeature_id INTEGER NOT NULL,
  67. type_id INTEGER NOT NULL,
  68. value TEXT,
  69. rank INTEGER NOT NULL,
  70. CONSTRAINT analysisfeature_id_type_id_rank UNIQUE (analysisfeature_id, type_id, rank),
  71. CONSTRAINT analysisfeatureprop_analysisfeature_id_fkey FOREIGN KEY (analysisfeature_id) REFERENCES {analysisfeature}(analysisfeature_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
  72. CONSTRAINT analysisfeatureprop_type_id_fkey FOREIGN KEY (type_id) REFERENCES {cvterm}(cvterm_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  73. )
  74. ";
  75. chado_query($sql);
  76. }
  77. }
  78. /*
  79. *
  80. */
  81. function tripal_analysis_add_cvterms() {
  82. tripal_cv_add_cv('tripal_analysis', 'Terms used for managing analyses in Tripal');
  83. // add analysis_date. This is no longer used (as far as we can tell) but we don't
  84. // get rid of it in case it is used, so just keep it in the Tripal CV
  85. $term = array(
  86. 'name' => 'analysis_date',
  87. 'def' => 'The date that an analysis was performed.'
  88. );
  89. tripal_cv_add_cvterm($term, 'tripal', 0, 1, 'tripal');
  90. // add analysis_short_name. This is no longer used (as far as we can tell) but we don't
  91. // get rid of it in case it is used, so just keep it in the Tripal CV
  92. $term = array(
  93. 'name' => 'analysis_short_name',
  94. 'def' => 'A computer legible (no spaces or special characters) abbreviation for the analysis.'
  95. );
  96. tripal_cv_add_cvterm($term, 'tripal', 0, 1 , 'tripal');
  97. // the 'analysis_property' vocabulary is for user definable properties.
  98. tripal_cv_add_cvterm(array('name' => 'Analysis Type', 'def' => 'The type of analysis that was performed.'),
  99. 'analysis_property', 0, 1, 'tripal');
  100. }
  101. /**
  102. * Implementation of hook_schema() creates two tables.
  103. *
  104. * - chado_analysis table
  105. * stores nodes that are also saved in the analysis table of chado database.
  106. * - tripal_analysis table
  107. * stores the sub-module names, such as tripal_analysis_blast, that are registered
  108. * with this module.
  109. */
  110. function tripal_analysis_schema() {
  111. // chado_analysis table
  112. $schema['chado_analysis'] = array(
  113. 'fields' => array(
  114. 'vid' => array(
  115. 'type' => 'int',
  116. 'unsigned' => TRUE,
  117. 'not null' => TRUE,
  118. 'default' => 0
  119. ),
  120. 'nid' => array(
  121. 'type' => 'int',
  122. 'unsigned' => TRUE,
  123. 'not null' => TRUE,
  124. 'default' => 0
  125. ),
  126. 'analysis_id' => array(
  127. 'type' => 'int',
  128. 'not null' => TRUE,
  129. 'default' => 0
  130. )
  131. ),
  132. 'indexes' => array(
  133. 'analysis_id' => array('analysis_id')
  134. ),
  135. 'unique keys' => array(
  136. 'nid_vid' => array('nid', 'vid'),
  137. 'vid' => array('vid')
  138. ),
  139. 'primary key' => array('nid'),
  140. );
  141. // tripal_analysis table
  142. $schema['tripal_analysis'] = array(
  143. 'description' => 'Table to store analysis sub-modules',
  144. 'fields' => array(
  145. 'modulename' => array(
  146. 'type' => 'text',
  147. 'size' => 'small',
  148. 'not null' => TRUE,
  149. 'description' => 'The module name. Tripal Analysis will use the module name to call module_setting_form()'
  150. )
  151. ),
  152. 'unique keys' => array(
  153. 'modulename' => array('modulename')
  154. )
  155. );
  156. return $schema;
  157. }
  158. /*
  159. *
  160. * @ingroup tripal_network
  161. */
  162. function tripal_analysis_add_mview_analysis_organism() {
  163. $view_name = 'analysis_organism';
  164. $comment = t('This view is for associating an organism (via it\'s associated features) to an analysis.');
  165. // this is the SQL used to identify the organism to which an analsysis
  166. // has been used. This is obtained though the analysisfeature -> feature -> organism
  167. // joins
  168. $sql = "
  169. SELECT DISTINCT A.analysis_id, O.organism_id
  170. FROM analysis A
  171. INNER JOIN analysisfeature AF ON A.analysis_id = AF.analysis_id
  172. INNER JOIN feature F ON AF.feature_id = F.feature_id
  173. INNER JOIN organism O ON O.organism_id = F.organism_id
  174. ";
  175. // the schema array for describing this view
  176. $schema = array(
  177. 'table' => $view_name,
  178. 'description' => $comment,
  179. 'fields' => array(
  180. 'analysis_id' => array(
  181. 'type' => 'int',
  182. 'not null' => TRUE,
  183. ),
  184. 'organism_id' => array(
  185. 'type' => 'int',
  186. 'not null' => TRUE,
  187. ),
  188. ),
  189. 'indexes' => array(
  190. 'networkmod_qtl_indx0' => array('analysis_id'),
  191. 'networkmod_qtl_indx1' => array('organism_id'),
  192. ),
  193. 'foreign keys' => array(
  194. 'analysis' => array(
  195. 'table' => 'analysis',
  196. 'columns' => array(
  197. 'analysis_id' => 'analysis_id',
  198. ),
  199. ),
  200. 'organism' => array(
  201. 'table' => 'organism',
  202. 'columns' => array(
  203. 'organism_id' => 'organism_id',
  204. ),
  205. ),
  206. ),
  207. );
  208. // add the view
  209. tripal_add_mview($view_name, 'tripal_analysis', $schema, $sql, $comment);
  210. }