tripal_analysis.install 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 vocabularies
  52. tripal_analysis_add_cvs();
  53. // add cvterms
  54. tripal_analysis_add_cvterms();
  55. // add materialized views
  56. tripal_analysis_add_mview_analysis_organism();
  57. // set the default vocabularies
  58. tripal_set_default_cv('analysisprop', 'type_id', 'analysis_property');
  59. }
  60. /**
  61. * Implementation of hook_uninstall().
  62. *
  63. * @ingroup tripal_analysis
  64. */
  65. function tripal_analysis_uninstall() {
  66. }
  67. /**
  68. * Create a legacy custom chado table (analysisfeatureprop) to store properties of
  69. * analysisfeature links.
  70. *
  71. * @ingroup tripal_analysis
  72. */
  73. function tripal_analysis_create_analysisfeatureprop() {
  74. // Create analysisfeatureprop table in chado. This is needed for Chado
  75. // version 1.11, the table exists in Chado 1.2.
  76. if (!db_table_exists('chado.analysisfeatureprop')) {
  77. $sql = "
  78. CREATE TABLE {analysisfeatureprop} (
  79. analysisfeatureprop_id SERIAL PRIMARY KEY,
  80. analysisfeature_id INTEGER NOT NULL,
  81. type_id INTEGER NOT NULL,
  82. value TEXT,
  83. rank INTEGER NOT NULL,
  84. CONSTRAINT analysisfeature_id_type_id_rank UNIQUE (analysisfeature_id, type_id, rank),
  85. CONSTRAINT analysisfeatureprop_analysisfeature_id_fkey FOREIGN KEY (analysisfeature_id) REFERENCES {analysisfeature}(analysisfeature_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
  86. CONSTRAINT analysisfeatureprop_type_id_fkey FOREIGN KEY (type_id) REFERENCES {cvterm}(cvterm_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  87. )
  88. ";
  89. chado_query($sql);
  90. }
  91. }
  92. /**
  93. * Add cvs related to analyses
  94. *
  95. * @ingroup tripal_analysis
  96. */
  97. function tripal_analysis_add_cvs() {
  98. // typically here we would add the analysis_property vocabulary
  99. // but it already comes with Chado.
  100. }
  101. /**
  102. * Adds controlled vocabulary terms needed by this module.
  103. *
  104. * @ingroup tripal_analysis
  105. */
  106. function tripal_analysis_add_cvterms() {
  107. tripal_cv_add_cv('tripal_analysis', 'Terms used for managing analyses in Tripal');
  108. // add analysis_date. This is no longer used (as far as we can tell) but we don't
  109. // get rid of it in case it is used, so just keep it in the Tripal CV
  110. $term = array(
  111. 'name' => 'analysis_date',
  112. 'def' => 'The date that an analysis was performed.'
  113. );
  114. tripal_cv_add_cvterm($term, 'tripal', 0, 1, 'tripal');
  115. // add analysis_short_name. This is no longer used (as far as we can tell) but we don't
  116. // get rid of it in case it is used, so just keep it in the Tripal CV
  117. $term = array(
  118. 'name' => 'analysis_short_name',
  119. 'def' => 'A computer legible (no spaces or special characters) abbreviation for the analysis.'
  120. );
  121. tripal_cv_add_cvterm($term, 'tripal', 0, 1 , 'tripal');
  122. // the 'analysis_property' vocabulary is for user definable properties wo we
  123. // will add an 'Analysis Type' to this vocubulary
  124. $term = array(
  125. 'name' => 'Analysis Type',
  126. 'def' => 'The type of analysis that was performed.'
  127. );
  128. tripal_cv_add_cvterm($term, 'analysis_property', 0, 1, 'tripal');
  129. }
  130. /**
  131. * Implementation of hook_schema().
  132. *
  133. * - chado_analysis table
  134. * stores nodes that are also saved in the analysis table of chado database.
  135. * - tripal_analysis table
  136. * stores the sub-module names, such as tripal_analysis_blast, that are registered
  137. * with this module.
  138. *
  139. * @ingroup tripal_analysis
  140. */
  141. function tripal_analysis_schema() {
  142. // chado_analysis table
  143. $schema['chado_analysis'] = array(
  144. 'fields' => array(
  145. 'vid' => array(
  146. 'type' => 'int',
  147. 'unsigned' => TRUE,
  148. 'not null' => TRUE,
  149. 'default' => 0
  150. ),
  151. 'nid' => array(
  152. 'type' => 'int',
  153. 'unsigned' => TRUE,
  154. 'not null' => TRUE,
  155. 'default' => 0
  156. ),
  157. 'analysis_id' => array(
  158. 'type' => 'int',
  159. 'not null' => TRUE,
  160. 'default' => 0
  161. )
  162. ),
  163. 'indexes' => array(
  164. 'analysis_id' => array('analysis_id')
  165. ),
  166. 'unique keys' => array(
  167. 'nid_vid' => array('nid', 'vid'),
  168. 'vid' => array('vid')
  169. ),
  170. 'primary key' => array('nid'),
  171. );
  172. // tripal_analysis table
  173. $schema['tripal_analysis'] = array(
  174. 'description' => 'Table to store analysis sub-modules',
  175. 'fields' => array(
  176. 'modulename' => array(
  177. 'type' => 'text',
  178. 'size' => 'small',
  179. 'not null' => TRUE,
  180. 'description' => 'The module name. Tripal Analysis will use the module name to call module_setting_form()'
  181. )
  182. ),
  183. 'unique keys' => array(
  184. 'modulename' => array('modulename')
  185. )
  186. );
  187. return $schema;
  188. }
  189. /**
  190. * Creates a view showing hte link between an organism & it's analysis through associated features.
  191. *
  192. * @ingroup tripal_analysis
  193. */
  194. function tripal_analysis_add_mview_analysis_organism() {
  195. $view_name = 'analysis_organism';
  196. $comment = t('This view is for associating an organism (via it\'s associated features) to an analysis.');
  197. // this is the SQL used to identify the organism to which an analsysis
  198. // has been used. This is obtained though the analysisfeature -> feature -> organism
  199. // joins
  200. $sql = "
  201. SELECT DISTINCT A.analysis_id, O.organism_id
  202. FROM analysis A
  203. INNER JOIN analysisfeature AF ON A.analysis_id = AF.analysis_id
  204. INNER JOIN feature F ON AF.feature_id = F.feature_id
  205. INNER JOIN organism O ON O.organism_id = F.organism_id
  206. ";
  207. // the schema array for describing this view
  208. $schema = array(
  209. 'table' => $view_name,
  210. 'description' => $comment,
  211. 'fields' => array(
  212. 'analysis_id' => array(
  213. 'type' => 'int',
  214. 'not null' => TRUE,
  215. ),
  216. 'organism_id' => array(
  217. 'type' => 'int',
  218. 'not null' => TRUE,
  219. ),
  220. ),
  221. 'indexes' => array(
  222. 'networkmod_qtl_indx0' => array('analysis_id'),
  223. 'networkmod_qtl_indx1' => array('organism_id'),
  224. ),
  225. 'foreign keys' => array(
  226. 'analysis' => array(
  227. 'table' => 'analysis',
  228. 'columns' => array(
  229. 'analysis_id' => 'analysis_id',
  230. ),
  231. ),
  232. 'organism' => array(
  233. 'table' => 'organism',
  234. 'columns' => array(
  235. 'organism_id' => 'organism_id',
  236. ),
  237. ),
  238. ),
  239. );
  240. // add the view
  241. tripal_add_mview($view_name, 'tripal_analysis', $schema, $sql, $comment);
  242. }
  243. /**
  244. * This is the required update for tripal_organism when upgrading from Drupal core API 6.x.
  245. *
  246. */
  247. function tripal_analysis_update_7200() {
  248. // We cannot use the Tripal API calls in the 7200 update
  249. // because during upgrade the tripal_core should also be disabled
  250. // set the analysis_property as default
  251. try {
  252. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'analysis_property'")->fetchField();
  253. db_insert('tripal_cv_defaults')
  254. ->fields(array(
  255. 'table_name' => 'analysisprop',
  256. 'field_name' => 'type_id',
  257. 'cv_id' => $cv_id
  258. ))
  259. ->execute();
  260. }
  261. catch (\PDOException $e) {
  262. $error = $e->getMessage();
  263. throw new DrupalUpdateException('Failed to add analysis_property vocabulary: '. $error);
  264. }
  265. // During the upgrade from D6 to D7 the vocabulary terms assigned to organisms were
  266. // copied to the field_data_taxonomyextra table rather than to the correct
  267. // field_data_taxonomy_vocabulary_[vid] table. We'll move them.
  268. $vid = db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE name = 'Analysis'")->fetchField();
  269. if ($vid) {
  270. try {
  271. // first move from the field_data_taxonomyextra table
  272. $sql = "
  273. INSERT INTO {field_data_taxonomy_vocabulary_$vid}
  274. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  275. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  276. FROM field_data_taxonomyextra
  277. WHERE bundle = 'chado_feature')
  278. ";
  279. db_query($sql);
  280. $sql = "DELETE FROM field_data_taxonomyextra WHERE bundle = 'chado_analysis'";
  281. db_query($sql);
  282. // next move from the field_revision_taxonomyextra table
  283. $sql = "
  284. INSERT INTO {field_revision_taxonomy_vocabulary_$vid}
  285. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  286. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  287. FROM field_revision_taxonomyextra
  288. WHERE bundle = 'chado_feature')
  289. ";
  290. db_query($sql);
  291. $sql = "DELETE FROM field_revision_taxonomyextra WHERE bundle = 'chado_analysis'";
  292. db_query($sql);
  293. }
  294. catch (\PDOException $e) {
  295. $error = $e->getMessage();
  296. throw new DrupalUpdateException('Could not move organism taxonomy terms: '. $error);
  297. }
  298. }
  299. }
  300. /**
  301. * Implementation of hook_update_dependencies(). It specifies a list of
  302. * other modules whose updates must be run prior to this one.
  303. */
  304. function tripal_analysis_update_dependencies() {
  305. $dependencies = array();
  306. // the tripal_cv update 7200 must run prior to update 7200 of this module
  307. $dependencies['tripal_analysis'][7200] = array(
  308. 'tripal_cv' => 7200
  309. );
  310. return $dependencies;
  311. }