tripal_analysis.install 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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' => '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. 'type' => 'int',
  231. 'not null' => TRUE,
  232. ),
  233. 'organism_id' => array(
  234. 'type' => 'int',
  235. 'not null' => TRUE,
  236. ),
  237. ),
  238. 'indexes' => array(
  239. 'networkmod_qtl_indx0' => array('analysis_id'),
  240. 'networkmod_qtl_indx1' => array('organism_id'),
  241. ),
  242. 'foreign keys' => array(
  243. 'analysis' => array(
  244. 'table' => 'analysis',
  245. 'columns' => array(
  246. 'analysis_id' => 'analysis_id',
  247. ),
  248. ),
  249. 'organism' => array(
  250. 'table' => 'organism',
  251. 'columns' => array(
  252. 'organism_id' => 'organism_id',
  253. ),
  254. ),
  255. ),
  256. );
  257. // add the view
  258. tripal_add_mview($view_name, 'tripal_analysis', $schema, $sql, $comment);
  259. }
  260. /**
  261. * This is the required update for tripal_organism when upgrading from Drupal core API 6.x.
  262. *
  263. */
  264. function tripal_analysis_update_7200() {
  265. // We cannot use the Tripal API calls in the 7200 update
  266. // because during upgrade the tripal_core should also be disabled
  267. // set the analysis_property as default
  268. try {
  269. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'analysis_property'")->fetchField();
  270. db_insert('tripal_cv_defaults')
  271. ->fields(array(
  272. 'table_name' => 'analysisprop',
  273. 'field_name' => 'type_id',
  274. 'cv_id' => $cv_id
  275. ))
  276. ->execute();
  277. }
  278. catch (\PDOException $e) {
  279. $error = $e->getMessage();
  280. throw new DrupalUpdateException('Failed to add analysis_property vocabulary: '. $error);
  281. }
  282. // During the upgrade from D6 to D7 the vocabulary terms assigned to organisms were
  283. // copied to the field_data_taxonomyextra table rather than to the correct
  284. // field_data_taxonomy_vocabulary_[vid] table. We'll move them.
  285. $vid = db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE name = 'Analysis'")->fetchField();
  286. if ($vid) {
  287. try {
  288. // first move from the field_data_taxonomyextra table
  289. $sql = "
  290. INSERT INTO {field_data_taxonomy_vocabulary_$vid}
  291. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  292. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  293. FROM field_data_taxonomyextra
  294. WHERE bundle = 'chado_feature')
  295. ";
  296. db_query($sql);
  297. $sql = "DELETE FROM field_data_taxonomyextra WHERE bundle = 'chado_analysis'";
  298. db_query($sql);
  299. // next move from the field_revision_taxonomyextra table
  300. $sql = "
  301. INSERT INTO {field_revision_taxonomy_vocabulary_$vid}
  302. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  303. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  304. FROM field_revision_taxonomyextra
  305. WHERE bundle = 'chado_feature')
  306. ";
  307. db_query($sql);
  308. $sql = "DELETE FROM field_revision_taxonomyextra WHERE bundle = 'chado_analysis'";
  309. db_query($sql);
  310. }
  311. catch (\PDOException $e) {
  312. $error = $e->getMessage();
  313. throw new DrupalUpdateException('Could not move organism taxonomy terms: '. $error);
  314. }
  315. }
  316. }
  317. /**
  318. * Implementation of hook_update_dependencies(). It specifies a list of
  319. * other modules whose updates must be run prior to this one.
  320. */
  321. function tripal_analysis_update_dependencies() {
  322. $dependencies = array();
  323. // the tripal_cv update 7200 must run prior to update 7200 of this module
  324. $dependencies['tripal_analysis'][7200] = array(
  325. 'tripal_cv' => 7200
  326. );
  327. return $dependencies;
  328. }
  329. /**
  330. * Fixes an error with the materialized view installation
  331. *
  332. */
  333. function tripal_analysis_update_7201() {
  334. // there is a bug in the Tripal v2.0-alpha release that didn't add the
  335. // materialized view schema to the mviews table.
  336. // get the schema for the materialized view from the custom_tables table
  337. // as there is a copy there, but only if the schema is missing from the
  338. // materialized view table
  339. $view_name = 'analysis_organism';
  340. $schema = db_select('tripal_mviews', 'tm')
  341. ->fields('tm', array('mv_schema'))
  342. ->condition('name', $view_name)
  343. ->execute()
  344. ->fetchField();
  345. if (!$schema or $schema == 'Array') {
  346. $schema = db_select('tripal_custom_tables', 'tct')
  347. ->fields('tct', array('schema'))
  348. ->condition('table_name', $view_name)
  349. ->execute()
  350. ->fetchField();
  351. $schema_str = var_export(unserialize($schema), TRUE);
  352. $schema_str = preg_replace('/=>\s+\n\s+array/', '=> array', $schema_str);
  353. db_update('tripal_mviews')
  354. ->fields(array(
  355. 'mv_schema' => $schema_str
  356. ))
  357. ->condition('name', $view_name)
  358. ->execute();
  359. }
  360. }