tripal_analysis.install

  1. 2.x tripal_analysis/tripal_analysis.install
  2. 3.x legacy/tripal_analysis/tripal_analysis.install
  3. 1.x tripal_analysis/tripal_analysis.install

Implements hooks from the Schema API.

File

tripal_analysis/tripal_analysis.install
View source
  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 (!chado_table_exists('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' => 'tripal'
  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' => 'tripal'
  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. }
  262. /**
  263. * This is the required update for tripal_organism when upgrading from Drupal core API 6.x.
  264. *
  265. */
  266. function tripal_analysis_update_7200() {
  267. // Make sure we have the full API loaded this will help during a
  268. // site upgrade when the tripal_core module is disabled.
  269. module_load_include('module', 'tripal_core', 'tripal_core');
  270. tripal_core_import_api();
  271. module_load_include('inc', 'tripal_cv', 'api/tripal_cv.api');
  272. // Set the analysis_property as default.
  273. try {
  274. $is_set = tripal_get_default_cv('analysisprop', 'type_id');
  275. if (!$is_set) {
  276. tripal_set_default_cv('analysisprop','type_id', 'analysis_property');
  277. }
  278. }
  279. catch (\PDOException $e) {
  280. $error = $e->getMessage();
  281. throw new DrupalUpdateException('Failed to add analysis_property vocabulary: '. $error);
  282. }
  283. // During the upgrade from D6 to D7 the vocabulary terms assigned to organisms were
  284. // copied to the field_data_taxonomyextra table rather than to the correct
  285. // field_data_taxonomy_vocabulary_[vid] table. We'll move them.
  286. $vid = db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE name = 'Analysis'")->fetchField();
  287. if ($vid) {
  288. try {
  289. if (db_table_exists('field_data_taxonomyextra')) {
  290. // first move from the field_data_taxonomyextra table
  291. $sql = "
  292. INSERT INTO {field_data_taxonomy_vocabulary_$vid}
  293. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  294. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  295. FROM field_data_taxonomyextra
  296. WHERE bundle = 'chado_feature')
  297. ";
  298. db_query($sql);
  299. $sql = "DELETE FROM field_data_taxonomyextra WHERE bundle = 'chado_analysis'";
  300. db_query($sql);
  301. // next move from the field_revision_taxonomyextra table
  302. $sql = "
  303. INSERT INTO {field_revision_taxonomy_vocabulary_$vid}
  304. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  305. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  306. FROM field_revision_taxonomyextra
  307. WHERE bundle = 'chado_feature')
  308. ";
  309. db_query($sql);
  310. $sql = "DELETE FROM field_revision_taxonomyextra WHERE bundle = 'chado_analysis'";
  311. db_query($sql);
  312. }
  313. }
  314. catch (\PDOException $e) {
  315. $error = $e->getMessage();
  316. throw new DrupalUpdateException('Could not move organism taxonomy terms: '. $error);
  317. }
  318. }
  319. }
  320. /**
  321. * Implementation of hook_update_dependencies().
  322. *
  323. * It specifies a list of other modules whose updates must be run prior to
  324. * this one. It also ensures the the Tripal API is in scope for site
  325. * upgrades when tripal_core is disabled.
  326. */
  327. function tripal_analysis_update_dependencies() {
  328. $dependencies = array();
  329. $dependencies['tripal_analysis'][7200] = array(
  330. 'tripal_cv' => 7200
  331. );
  332. return $dependencies;
  333. }
  334. /**
  335. * Fixes an error with the materialized view installation
  336. *
  337. */
  338. function tripal_analysis_update_7201() {
  339. // Make sure we have the full API loaded this will help during a
  340. // site upgrade when the tripal_core module is disabled.
  341. module_load_include('module', 'tripal_core', 'tripal_core');
  342. tripal_core_import_api();
  343. // there is a bug in the Tripal v2.0-alpha release that didn't add the
  344. // materialized view schema to the mviews table.
  345. // get the schema for the materialized view from the custom_tables table
  346. // as there is a copy there, but only if the schema is missing from the
  347. // materialized view table
  348. $view_name = 'analysis_organism';
  349. $schema = db_select('tripal_mviews', 'tm')
  350. ->fields('tm', array('mv_schema'))
  351. ->condition('name', $view_name)
  352. ->execute()
  353. ->fetchField();
  354. if (!$schema or $schema == 'Array') {
  355. $schema = db_select('tripal_custom_tables', 'tct')
  356. ->fields('tct', array('schema'))
  357. ->condition('table_name', $view_name)
  358. ->execute()
  359. ->fetchField();
  360. $schema_str = var_export(unserialize($schema), TRUE);
  361. $schema_str = preg_replace('/=>\s+\n\s+array/', '=> array', $schema_str);
  362. db_update('tripal_mviews')
  363. ->fields(array(
  364. 'mv_schema' => $schema_str
  365. ))
  366. ->condition('name', $view_name)
  367. ->execute();
  368. }
  369. }

Related topics