tripal_analysis.install 7.1 KB

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