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. // 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_insert_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. tripal_insert_cvterm(
  111. array(
  112. 'name' => 'analysis_date',
  113. 'definition' => 'The date that an analysis was performed.',
  114. 'cv_name' => 'tripal',
  115. 'is_relationship' => 0,
  116. 'db_name' => 'tripal'
  117. ),
  118. array('update_existing' => TRUE)
  119. );
  120. // add analysis_short_name. This is no longer used (as far as we can tell) but we don't
  121. // get rid of it in case it is used, so just keep it in the Tripal CV
  122. tripal_insert_cvterm(
  123. array(
  124. 'name' => 'analysis_short_name',
  125. 'definition' => 'A computer legible (no spaces or special characters) '
  126. . 'abbreviation for the analysis.',
  127. 'cv_name' => 'tripal',
  128. 'is_relationship' => 0,
  129. 'db_name' => 'tripal'
  130. ),
  131. array('update_existing' => TRUE)
  132. );
  133. // the 'analysis_property' vocabulary is for user definable properties wo we
  134. // will add an 'Analysis Type' to this vocubulary
  135. tripal_insert_cvterm(
  136. array(
  137. 'name' => 'Analysis Type',
  138. 'definition' => 'The type of analysis that was performed.',
  139. 'cv_name' => 'analysis_property',
  140. 'is_relationship' => 0,
  141. 'db_name' => 'tripal'
  142. ),
  143. array('update_existing' => TRUE)
  144. );
  145. }
  146. /**
  147. * Implementation of hook_schema().
  148. *
  149. * - chado_analysis table
  150. * stores nodes that are also saved in the analysis table of chado database.
  151. * - tripal_analysis table
  152. * stores the sub-module names, such as tripal_analysis_blast, that are registered
  153. * with this module.
  154. *
  155. * @ingroup tripal_analysis
  156. */
  157. function tripal_analysis_schema() {
  158. // chado_analysis table
  159. $schema['chado_analysis'] = array(
  160. 'fields' => array(
  161. 'vid' => array(
  162. 'type' => 'int',
  163. 'unsigned' => TRUE,
  164. 'not null' => TRUE,
  165. 'default' => 0
  166. ),
  167. 'nid' => array(
  168. 'type' => 'int',
  169. 'unsigned' => TRUE,
  170. 'not null' => TRUE,
  171. 'default' => 0
  172. ),
  173. 'analysis_id' => array(
  174. 'type' => 'int',
  175. 'not null' => TRUE,
  176. 'default' => 0
  177. )
  178. ),
  179. 'indexes' => array(
  180. 'analysis_id' => array('analysis_id')
  181. ),
  182. 'unique keys' => array(
  183. 'nid_vid' => array('nid', 'vid'),
  184. 'vid' => array('vid')
  185. ),
  186. 'primary key' => array('nid'),
  187. );
  188. // tripal_analysis table
  189. $schema['tripal_analysis'] = array(
  190. 'description' => 'Table to store analysis sub-modules',
  191. 'fields' => array(
  192. 'modulename' => array(
  193. 'type' => 'text',
  194. 'size' => 'small',
  195. 'not null' => TRUE,
  196. 'description' => 'The module name. Tripal Analysis will use the module name to call module_setting_form()'
  197. )
  198. ),
  199. 'unique keys' => array(
  200. 'modulename' => array('modulename')
  201. )
  202. );
  203. return $schema;
  204. }
  205. /**
  206. * Creates a view showing the link between an organism & it's analysis through associated features.
  207. *
  208. * @ingroup tripal_analysis
  209. */
  210. function tripal_analysis_add_mview_analysis_organism() {
  211. $view_name = 'analysis_organism';
  212. $comment = t('This view is for associating an organism (via it\'s associated features) to an analysis.');
  213. // this is the SQL used to identify the organism to which an analsysis
  214. // has been used. This is obtained though the analysisfeature -> feature -> organism
  215. // joins
  216. $sql = "
  217. SELECT DISTINCT A.analysis_id, O.organism_id
  218. FROM analysis A
  219. INNER JOIN analysisfeature AF ON A.analysis_id = AF.analysis_id
  220. INNER JOIN feature F ON AF.feature_id = F.feature_id
  221. INNER JOIN organism O ON O.organism_id = F.organism_id
  222. ";
  223. // the schema array for describing this view
  224. $schema = array(
  225. 'table' => $view_name,
  226. 'description' => $comment,
  227. 'fields' => array(
  228. 'analysis_id' => array(
  229. 'type' => 'int',
  230. 'not null' => TRUE,
  231. ),
  232. 'organism_id' => array(
  233. 'type' => 'int',
  234. 'not null' => TRUE,
  235. ),
  236. ),
  237. 'indexes' => array(
  238. 'networkmod_qtl_indx0' => array('analysis_id'),
  239. 'networkmod_qtl_indx1' => array('organism_id'),
  240. ),
  241. 'foreign keys' => array(
  242. 'analysis' => array(
  243. 'table' => 'analysis',
  244. 'columns' => array(
  245. 'analysis_id' => 'analysis_id',
  246. ),
  247. ),
  248. 'organism' => array(
  249. 'table' => 'organism',
  250. 'columns' => array(
  251. 'organism_id' => 'organism_id',
  252. ),
  253. ),
  254. ),
  255. );
  256. // add the view
  257. tripal_add_mview($view_name, 'tripal_analysis', $schema, $sql, $comment);
  258. }
  259. /**
  260. * This is the required update for tripal_organism when upgrading from Drupal core API 6.x.
  261. *
  262. */
  263. function tripal_analysis_update_7200() {
  264. // We cannot use the Tripal API calls in the 7200 update
  265. // because during upgrade the tripal_core should also be disabled
  266. // set the analysis_property as default
  267. try {
  268. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'analysis_property'")->fetchField();
  269. db_insert('tripal_cv_defaults')
  270. ->fields(array(
  271. 'table_name' => 'analysisprop',
  272. 'field_name' => 'type_id',
  273. 'cv_id' => $cv_id
  274. ))
  275. ->execute();
  276. }
  277. catch (\PDOException $e) {
  278. $error = $e->getMessage();
  279. throw new DrupalUpdateException('Failed to add analysis_property vocabulary: '. $error);
  280. }
  281. // During the upgrade from D6 to D7 the vocabulary terms assigned to organisms were
  282. // copied to the field_data_taxonomyextra table rather than to the correct
  283. // field_data_taxonomy_vocabulary_[vid] table. We'll move them.
  284. $vid = db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE name = 'Analysis'")->fetchField();
  285. if ($vid) {
  286. try {
  287. // first move from the field_data_taxonomyextra table
  288. $sql = "
  289. INSERT INTO {field_data_taxonomy_vocabulary_$vid}
  290. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  291. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  292. FROM field_data_taxonomyextra
  293. WHERE bundle = 'chado_feature')
  294. ";
  295. db_query($sql);
  296. $sql = "DELETE FROM field_data_taxonomyextra WHERE bundle = 'chado_analysis'";
  297. db_query($sql);
  298. // next move from the field_revision_taxonomyextra table
  299. $sql = "
  300. INSERT INTO {field_revision_taxonomy_vocabulary_$vid}
  301. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  302. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  303. FROM field_revision_taxonomyextra
  304. WHERE bundle = 'chado_feature')
  305. ";
  306. db_query($sql);
  307. $sql = "DELETE FROM field_revision_taxonomyextra WHERE bundle = 'chado_analysis'";
  308. db_query($sql);
  309. }
  310. catch (\PDOException $e) {
  311. $error = $e->getMessage();
  312. throw new DrupalUpdateException('Could not move organism taxonomy terms: '. $error);
  313. }
  314. }
  315. }
  316. /**
  317. * Implementation of hook_update_dependencies(). It specifies a list of
  318. * other modules whose updates must be run prior to this one.
  319. */
  320. function tripal_analysis_update_dependencies() {
  321. $dependencies = array();
  322. // the tripal_cv update 7200 must run prior to update 7200 of this module
  323. $dependencies['tripal_analysis'][7200] = array(
  324. 'tripal_cv' => 7200
  325. );
  326. return $dependencies;
  327. }
  328. /**
  329. * Fixes an error with the materialized view installation
  330. *
  331. */
  332. function tripal_analysis_update_7201() {
  333. // there is a bug in the Tripal v2.0-alpha release that didn't add the
  334. // materialized view schema to the mviews table.
  335. // get the schema for the materialized view from the custom_tables table
  336. // as there is a copy there, but only if the schema is missing from the
  337. // materialized view table
  338. $view_name = 'analysis_organism';
  339. $schema = db_select('tripal_mviews', 'tm')
  340. ->fields('tm', array('mv_schema'))
  341. ->condition('name', $view_name)
  342. ->execute()
  343. ->fetchField();
  344. if (!$schema or $schema == 'Array') {
  345. $schema = db_select('tripal_custom_tables', 'tct')
  346. ->fields('tct', array('schema'))
  347. ->condition('table_name', $view_name)
  348. ->execute()
  349. ->fetchField();
  350. $schema_str = var_export(unserialize($schema), TRUE);
  351. $schema_str = preg_replace('/=>\s+\n\s+array/', '=> array', $schema_str);
  352. db_update('tripal_mviews')
  353. ->fields(array(
  354. 'mv_schema' => $schema_str
  355. ))
  356. ->condition('name', $view_name)
  357. ->execute();
  358. }
  359. }