tripal_analysis.install 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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_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. // we may need the analysisfeatureprop table if it doesn't already exist
  48. tripal_analysis_create_analysisfeatureprop();
  49. // add vocabularies
  50. tripal_analysis_add_cvs();
  51. // add cvterms
  52. tripal_analysis_add_cvterms();
  53. // add materialized views
  54. tripal_analysis_add_mview_analysis_organism();
  55. // set the default vocabularies
  56. tripal_set_default_cv('analysisprop', 'type_id', 'analysis_property');
  57. }
  58. /**
  59. * Implementation of hook_uninstall().
  60. *
  61. * @ingroup tripal_analysis
  62. */
  63. function tripal_analysis_uninstall() {
  64. }
  65. /**
  66. * Create a legacy custom chado table (analysisfeatureprop) to store properties of
  67. * analysisfeature links.
  68. *
  69. * @ingroup tripal_analysis
  70. */
  71. function tripal_analysis_create_analysisfeatureprop() {
  72. // Create analysisfeatureprop table in chado. This is needed for Chado
  73. // version 1.11, the table exists in Chado 1.2.
  74. if (!db_table_exists('chado.analysisfeatureprop')) {
  75. $sql = "
  76. CREATE TABLE {analysisfeatureprop} (
  77. analysisfeatureprop_id SERIAL PRIMARY KEY,
  78. analysisfeature_id INTEGER NOT NULL,
  79. type_id INTEGER NOT NULL,
  80. value TEXT,
  81. rank INTEGER NOT NULL,
  82. CONSTRAINT analysisfeature_id_type_id_rank UNIQUE (analysisfeature_id, type_id, rank),
  83. CONSTRAINT analysisfeatureprop_analysisfeature_id_fkey FOREIGN KEY (analysisfeature_id) REFERENCES {analysisfeature}(analysisfeature_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
  84. CONSTRAINT analysisfeatureprop_type_id_fkey FOREIGN KEY (type_id) REFERENCES {cvterm}(cvterm_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  85. )
  86. ";
  87. chado_query($sql);
  88. }
  89. }
  90. /**
  91. * Add cvs related to analyses
  92. *
  93. * @ingroup tripal_analysis
  94. */
  95. function tripal_analysis_add_cvs() {
  96. // typically here we would add the analysis_property vocabulary
  97. // but it already comes with Chado.
  98. }
  99. /**
  100. * Adds controlled vocabulary terms needed by this module.
  101. *
  102. * @ingroup tripal_analysis
  103. */
  104. function tripal_analysis_add_cvterms() {
  105. tripal_insert_cv(
  106. 'tripal_analysis',
  107. 'Terms used for managing analyses in Tripal'
  108. );
  109. // add analysis_date. This is no longer used (as far as we can tell) but we don't
  110. // get rid of it in case it is used, so just keep it in the Tripal CV
  111. tripal_insert_cvterm(
  112. array(
  113. 'name' => 'analysis_date',
  114. 'definition' => 'The date that an analysis was performed.',
  115. 'cv_name' => 'tripal',
  116. 'is_relationship' => 0,
  117. 'db_name' => 'tripal'
  118. ),
  119. array('update_existing' => TRUE)
  120. );
  121. // add analysis_short_name. This is no longer used (as far as we can tell) but we don't
  122. // get rid of it in case it is used, so just keep it in the Tripal CV
  123. tripal_insert_cvterm(
  124. array(
  125. 'name' => 'analysis_short_name',
  126. 'definition' => 'A computer legible (no spaces or special characters) '
  127. . 'abbreviation for the analysis.',
  128. 'cv_name' => 'tripal',
  129. 'is_relationship' => 0,
  130. 'db_name' => 'local'
  131. ),
  132. array('update_existing' => TRUE)
  133. );
  134. // the 'analysis_property' vocabulary is for user definable properties wo we
  135. // will add an 'Analysis Type' to this vocubulary
  136. tripal_insert_cvterm(
  137. array(
  138. 'name' => 'Analysis Type',
  139. 'definition' => 'The type of analysis that was performed.',
  140. 'cv_name' => 'analysis_property',
  141. 'is_relationship' => 0,
  142. 'db_name' => 'local'
  143. ),
  144. array('update_existing' => TRUE)
  145. );
  146. }
  147. /**
  148. * Implementation of hook_schema().
  149. *
  150. * - chado_analysis table
  151. * stores nodes that are also saved in the analysis table of chado database.
  152. * - tripal_analysis table
  153. * stores the sub-module names, such as tripal_analysis_blast, that are registered
  154. * with this module.
  155. *
  156. * @ingroup tripal_analysis
  157. */
  158. function tripal_analysis_schema() {
  159. // chado_analysis table
  160. $schema['chado_analysis'] = array(
  161. 'fields' => array(
  162. 'vid' => array(
  163. 'type' => 'int',
  164. 'unsigned' => TRUE,
  165. 'not null' => TRUE,
  166. 'default' => 0
  167. ),
  168. 'nid' => array(
  169. 'type' => 'int',
  170. 'unsigned' => TRUE,
  171. 'not null' => TRUE,
  172. 'default' => 0
  173. ),
  174. 'analysis_id' => array(
  175. 'type' => 'int',
  176. 'not null' => TRUE,
  177. 'default' => 0
  178. )
  179. ),
  180. 'indexes' => array(
  181. 'analysis_id' => array('analysis_id')
  182. ),
  183. 'unique keys' => array(
  184. 'nid_vid' => array('nid', 'vid'),
  185. 'vid' => array('vid')
  186. ),
  187. 'primary key' => array('nid'),
  188. );
  189. // tripal_analysis table
  190. $schema['tripal_analysis'] = array(
  191. 'description' => 'Table to store analysis sub-modules',
  192. 'fields' => array(
  193. 'modulename' => array(
  194. 'type' => 'text',
  195. 'size' => 'small',
  196. 'not null' => TRUE,
  197. 'description' => 'The module name. Tripal Analysis will use the module name to call module_setting_form()'
  198. )
  199. ),
  200. 'unique keys' => array(
  201. 'modulename' => array('modulename')
  202. )
  203. );
  204. return $schema;
  205. }
  206. /**
  207. * Creates a view showing the link between an organism & it's analysis through associated features.
  208. *
  209. * @ingroup tripal_analysis
  210. */
  211. function tripal_analysis_add_mview_analysis_organism() {
  212. $view_name = 'analysis_organism';
  213. $comment = t('This view is for associating an organism (via it\'s associated features) to an analysis.');
  214. // this is the SQL used to identify the organism to which an analsysis
  215. // has been used. This is obtained though the analysisfeature -> feature -> organism
  216. // joins
  217. $sql = "
  218. SELECT DISTINCT A.analysis_id, O.organism_id
  219. FROM analysis A
  220. INNER JOIN analysisfeature AF ON A.analysis_id = AF.analysis_id
  221. INNER JOIN feature F ON AF.feature_id = F.feature_id
  222. INNER JOIN organism O ON O.organism_id = F.organism_id
  223. ";
  224. // the schema array for describing this view
  225. $schema = array(
  226. 'table' => $view_name,
  227. 'description' => $comment,
  228. 'fields' => array(
  229. 'analysis_id' => array(
  230. 'size' => 'big',
  231. 'type' => 'int',
  232. 'not null' => TRUE,
  233. ),
  234. 'organism_id' => array(
  235. 'size' => 'big',
  236. 'type' => 'int',
  237. 'not null' => TRUE,
  238. ),
  239. ),
  240. 'indexes' => array(
  241. 'networkmod_qtl_indx0' => array('analysis_id'),
  242. 'networkmod_qtl_indx1' => array('organism_id'),
  243. ),
  244. 'foreign keys' => array(
  245. 'analysis' => array(
  246. 'table' => 'analysis',
  247. 'columns' => array(
  248. 'analysis_id' => 'analysis_id',
  249. ),
  250. ),
  251. 'organism' => array(
  252. 'table' => 'organism',
  253. 'columns' => array(
  254. 'organism_id' => 'organism_id',
  255. ),
  256. ),
  257. ),
  258. );
  259. // add the view
  260. tripal_add_mview($view_name, 'tripal_analysis', $schema, $sql, $comment);
  261. }