tripal_analysis.install 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 (!tripal_core_is_chado_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 analysis nodes from drupal.
  52. $sql_ana_id = "
  53. SELECT nid, vid
  54. FROM {node}
  55. WHERE type like 'chado_analysi%'
  56. ";
  57. $result = db_query($sql_ana_id);
  58. while ($ana = $result->fetchObject()) {
  59. node_delete($ana->nid);
  60. }
  61. }
  62. /*
  63. *
  64. */
  65. function tripal_analysis_create_analysisfeatureprop() {
  66. // Create analysisfeatureprop table in chado. This is needed for Chado
  67. // version 1.11, the table exists in Chado 1.2.
  68. if (!db_table_exists('chado.analysisfeatureprop')) {
  69. $sql = "
  70. CREATE TABLE chado.analysisfeatureprop (
  71. analysisfeatureprop_id SERIAL PRIMARY KEY,
  72. analysisfeature_id INTEGER NOT NULL REFERENCES chado.analysisfeature(analysisfeature_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
  73. type_id INTEGER NOT NULL REFERENCES chdo.cvterm(cvterm_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
  74. value TEXT,
  75. rank INTEGER NOT NULL,
  76. CONSTRAINT analysisfeature_id_type_id_rank UNIQUE(analysisfeature_id, type_id, rank)
  77. )
  78. ";
  79. chado_query($sql);
  80. }
  81. }
  82. /*
  83. *
  84. */
  85. function tripal_analysis_add_cvterms() {
  86. tripal_cv_add_cv('tripal_analysis', 'Terms used for managing analyses in Tripal');
  87. // add analysis_date. This is no longer used (as far as we can tell) but we don't
  88. // get rid of it in case it is used, so just keep it in the Tripal CV
  89. $term = array(
  90. 'name' => 'analysis_date',
  91. 'def' => 'The date that an analysis was performed.'
  92. );
  93. tripal_cv_add_cvterm($term, 'tripal', 0, 1, 'tripal');
  94. // add analysis_short_name. This is no longer used (as far as we can tell) but we don't
  95. // get rid of it in case it is used, so just keep it in the Tripal CV
  96. $term = array(
  97. 'name' => 'analysis_short_name',
  98. 'def' => 'A computer legible (no spaces or special characters) abbreviation for the analysis.'
  99. );
  100. tripal_cv_add_cvterm($term, 'tripal', 0, 1 , 'tripal');
  101. // the 'analysis_property' vocabulary is for user definable properties.
  102. tripal_cv_add_cvterm(array('name' => 'Analysis Type', 'def' => 'The type of analysis that was performed.'),
  103. 'analysis_property', 0, 1, 'tripal');
  104. }
  105. /**
  106. * Implementation of hook_schema() creates two tables.
  107. *
  108. * - chado_analysis table
  109. * stores nodes that are also saved in the analysis table of chado database.
  110. * - tripal_analysis table
  111. * stores the sub-module names, such as tripal_analysis_blast, that are registered
  112. * with this module.
  113. */
  114. function tripal_analysis_schema() {
  115. // chado_analysis table
  116. $schema['chado_analysis'] = array(
  117. 'fields' => array(
  118. 'vid' => array(
  119. 'type' => 'int',
  120. 'unsigned' => TRUE,
  121. 'not null' => TRUE,
  122. 'default' => 0
  123. ),
  124. 'nid' => array(
  125. 'type' => 'int',
  126. 'unsigned' => TRUE,
  127. 'not null' => TRUE,
  128. 'default' => 0
  129. ),
  130. 'analysis_id' => array(
  131. 'type' => 'int',
  132. 'not null' => TRUE,
  133. 'default' => 0
  134. )
  135. ),
  136. 'indexes' => array(
  137. 'analysis_id' => array('analysis_id')
  138. ),
  139. 'unique keys' => array(
  140. 'nid_vid' => array('nid', 'vid'),
  141. 'vid' => array('vid')
  142. ),
  143. 'primary key' => array('nid'),
  144. );
  145. // tripal_analysis table
  146. $schema['tripal_analysis'] = array(
  147. 'description' => 'Table to store analysis sub-modules',
  148. 'fields' => array(
  149. 'modulename' => array(
  150. 'type' => 'text',
  151. 'size' => 'small',
  152. 'not null' => TRUE,
  153. 'description' => 'The module name. Tripal Analysis will use the module name to call module_setting_form()'
  154. )
  155. ),
  156. 'unique keys' => array(
  157. 'modulename' => array('modulename')
  158. )
  159. );
  160. return $schema;
  161. }
  162. /*
  163. *
  164. * @ingroup tripal_network
  165. */
  166. function tripal_analysis_add_mview_analysis_organism() {
  167. $view_name = 'analysis_organism';
  168. $comment = t('This view is for associating an organism (via it\'s associated features) to an analysis.');
  169. // this is the SQL used to identify the organism to which an analsysis
  170. // has been used. This is obtained though the analysisfeature -> feature -> organism
  171. // joins
  172. $sql = "
  173. SELECT DISTINCT A.analysis_id, O.organism_id
  174. FROM analysis A
  175. INNER JOIN analysisfeature AF ON A.analysis_id = AF.analysis_id
  176. INNER JOIN feature F ON AF.feature_id = F.feature_id
  177. INNER JOIN organism O ON O.organism_id = F.organism_id
  178. ";
  179. // the schema array for describing this view
  180. $schema = array(
  181. 'table' => $view_name,
  182. 'description' => $comment,
  183. 'fields' => array(
  184. 'analysis_id' => array(
  185. 'type' => 'int',
  186. 'not null' => TRUE,
  187. ),
  188. 'organism_id' => array(
  189. 'type' => 'int',
  190. 'not null' => TRUE,
  191. ),
  192. ),
  193. 'indexes' => array(
  194. 'networkmod_qtl_indx0' => array('analysis_id'),
  195. 'networkmod_qtl_indx1' => array('organism_id'),
  196. ),
  197. 'foreign keys' => array(
  198. 'analysis' => array(
  199. 'table' => 'analysis',
  200. 'columns' => array(
  201. 'analysis_id' => 'analysis_id',
  202. ),
  203. ),
  204. 'organism' => array(
  205. 'table' => 'organism',
  206. 'columns' => array(
  207. 'organism_id' => 'organism_id',
  208. ),
  209. ),
  210. ),
  211. );
  212. // add the view
  213. tripal_add_mview($view_name, 'tripal_analysis', $schema, $sql, $comment);
  214. }