tripal_library.install

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

Installation of the library module

File

tripal_library/tripal_library.install
View source
  1. <?php
  2. /**
  3. * @file
  4. * Installation of the library module
  5. */
  6. /**
  7. * Implements hook_disable().
  8. * Disable default views when module is disabled
  9. *
  10. * @ingroup tripal_library
  11. */
  12. function tripal_library_disable() {
  13. // Disable all default views provided by this module
  14. require_once("tripal_library.views_default.inc");
  15. $views = tripal_library_views_default_views();
  16. foreach (array_keys($views) as $view_name) {
  17. tripal_disable_view($view_name,FALSE,array('suppress_error' => TRUE));
  18. }
  19. }
  20. /**
  21. * Implementation of hook_requirements().
  22. *
  23. * @ingroup tripal_library
  24. */
  25. function tripal_library_requirements($phase) {
  26. $requirements = array();
  27. if ($phase == 'install') {
  28. // make sure chado is installed
  29. if (!$GLOBALS["chado_is_installed"]) {
  30. $requirements ['tripal_library'] = array(
  31. 'title' => "tripal_library",
  32. 'value' => "ERROR: Chado must be installed before this module can be enabled",
  33. 'severity' => REQUIREMENT_ERROR,
  34. );
  35. }
  36. }
  37. return $requirements;
  38. }
  39. /**
  40. * Implementation of hook_install().
  41. *
  42. * @ingroup tripal_library
  43. */
  44. function tripal_library_install() {
  45. // add the materialized view
  46. tripal_library_add_mview_library_feature_count();
  47. // add cvterms
  48. tripal_library_add_cvs();
  49. tripal_library_add_cvterms();
  50. // set the default vocabularies
  51. tripal_set_default_cv('libraryprop', 'type_id', 'library_property');
  52. tripal_set_default_cv('library', 'type_id', 'library_type');
  53. }
  54. /**
  55. * Implementation of hook_uninstall().
  56. *
  57. * @ingroup tripal_library
  58. */
  59. function tripal_library_uninstall() {
  60. }
  61. /**
  62. * Implementation of hook_schema().
  63. *
  64. * @ingroup tripal_library
  65. */
  66. function tripal_library_schema() {
  67. $schema['chado_library'] = array(
  68. 'fields' => array(
  69. 'vid' => array(
  70. 'type' => 'int',
  71. 'unsigned' => TRUE,
  72. 'not null' => TRUE,
  73. 'default' => 0
  74. ),
  75. 'nid' => array(
  76. 'type' => 'int',
  77. 'unsigned' => TRUE,
  78. 'not null' => TRUE,
  79. 'default' => 0
  80. ),
  81. 'library_id' => array(
  82. 'type' => 'int',
  83. 'not null' => TRUE,
  84. 'default' => 0
  85. )
  86. ),
  87. 'indexes' => array(
  88. 'chado_library_idx1' => array('library_id')
  89. ),
  90. 'unique keys' => array(
  91. 'chado_library_uq1' => array('nid', 'vid'),
  92. 'chado_library_uq2' => array('vid')
  93. ),
  94. 'primary key' => array('nid'),
  95. );
  96. return $schema;
  97. }
  98. /**
  99. * Adds a materialized view keeping track of the type of features associated with each library
  100. *
  101. * @ingroup tripal_library
  102. */
  103. function tripal_library_add_mview_library_feature_count(){
  104. $view_name = 'library_feature_count';
  105. $comment = 'Provides count of feature by type that are associated with all libraries';
  106. $schema = array(
  107. 'table' => $view_name,
  108. 'description' => $comment,
  109. 'fields' => array(
  110. 'library_id' => array(
  111. 'size' => 'big',
  112. 'type' => 'int',
  113. 'not null' => TRUE,
  114. ),
  115. 'name' => array(
  116. 'type' => 'varchar',
  117. 'length' => 255,
  118. 'not null' => TRUE,
  119. ),
  120. 'num_features' => array(
  121. 'type' => 'int',
  122. 'not null' => TRUE,
  123. ),
  124. 'feature_type' => array(
  125. 'type' => 'varchar',
  126. 'length' => 255,
  127. 'not null' => TRUE,
  128. ),
  129. ),
  130. 'indexes' => array(
  131. 'library_feature_count_idx1' => array('library_id'),
  132. ),
  133. );
  134. $sql = "
  135. SELECT
  136. L.library_id, L.name,
  137. count(F.feature_id) as num_features,
  138. CVT.name as feature_type
  139. FROM library L
  140. INNER JOIN library_feature LF ON LF.library_id = L.library_id
  141. INNER JOIN feature F ON LF.feature_id = F.feature_id
  142. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  143. GROUP BY L.library_id, L.name, CVT.name
  144. ";
  145. tripal_add_mview($view_name, 'tripal_library', $schema, $sql, $comment);
  146. }
  147. /**
  148. * Adds new CV's used by this module
  149. *
  150. * @ingroup tripal_library
  151. */
  152. function tripal_library_add_cvs(){
  153. tripal_insert_cv(
  154. 'library_property',
  155. 'Contains properties for libraries.'
  156. );
  157. tripal_insert_cv(
  158. 'library_type',
  159. 'Contains terms for types of libraries (e.g. BAC, cDNA, FOSMID, etc).'
  160. );
  161. }
  162. /**
  163. * Adds cvterms needed for the library module
  164. *
  165. * @ingroup tripal_library
  166. */
  167. function tripal_library_add_cvterms() {
  168. // Insert cvterm 'library_description' into cvterm table of chado
  169. // database. This CV term is used to keep track of the library
  170. // description in the libraryprop table.
  171. tripal_insert_cvterm(
  172. array(
  173. 'name' => 'Library Description',
  174. 'definition' => 'Description of a library',
  175. 'cv_name' => 'library_property',
  176. 'is_relationship' => 0,
  177. 'db_name' => 'tripal'
  178. ),
  179. array('update_existing' => TRUE)
  180. );
  181. // add cvterms for the map unit types
  182. tripal_insert_cvterm(
  183. array(
  184. 'name' => 'cdna_library',
  185. 'definition' => 'cDNA library',
  186. 'cv_name' => 'library_type',
  187. 'is_relationship' => 0,
  188. 'db_name' => 'tripal'
  189. ),
  190. array('update_existing' => TRUE)
  191. );
  192. tripal_insert_cvterm(
  193. array(
  194. 'name' => 'bac_library',
  195. 'definition' => 'Bacterial Artifical Chromsome (BAC) library',
  196. 'cv_name' => 'library_type',
  197. 'is_relationship' => 0,
  198. 'db_name' => 'tripal'
  199. ),
  200. array('update_existing' => TRUE)
  201. );
  202. tripal_insert_cvterm(
  203. array(
  204. 'name' => 'fosmid_library',
  205. 'definition' => 'Fosmid library',
  206. 'cv_name' => 'library_type',
  207. 'is_relationship' => 0,
  208. 'db_name' => 'tripal'
  209. ),
  210. array('update_existing' => TRUE)
  211. );
  212. tripal_insert_cvterm(
  213. array(
  214. 'name' => 'cosmid_library',
  215. 'definition' => 'Cosmid library',
  216. 'cv_name' => 'library_type',
  217. 'is_relationship' => 0,
  218. 'db_name' => 'tripal'
  219. ),
  220. array('update_existing' => TRUE)
  221. );
  222. tripal_insert_cvterm(
  223. array(
  224. 'name' => 'yac_library',
  225. 'definition' => 'Yeast Artificial Chromosome (YAC) library',
  226. 'cv_name' => 'library_type',
  227. 'is_relationship' => 0,
  228. 'db_name' => 'tripal'
  229. ),
  230. array('update_existing' => TRUE)
  231. );
  232. tripal_insert_cvterm(
  233. array(
  234. 'name' => 'genomic_library',
  235. 'definition' => 'Genomic Library',
  236. 'cv_name' => 'library_type',
  237. 'is_relationship' => 0,
  238. 'db_name' => 'tripal'
  239. ),
  240. array('update_existing' => TRUE)
  241. );
  242. }
  243. /**
  244. * This is the required update for tripal_library when upgrading from Drupal core API 6.x.
  245. *
  246. */
  247. function tripal_library_update_7200() {
  248. // Make sure we have the full API loaded this will help during a
  249. // site upgrade when the tripal_core module is disabled.
  250. module_load_include('module', 'tripal_core', 'tripal_core');
  251. tripal_core_import_api();
  252. module_load_include('inc', 'tripal_cv', 'api/tripal_cv.api');
  253. // the library types were formerly in a vocabulary named 'tripal_library_types'.
  254. // rename that to just be 'library_type'.
  255. try {
  256. $check = chado_query("SELECT cv_id FROM {cv} WHERE name = 'library_type'")->fetchObject();
  257. if (!$check->cv_id) {
  258. $sql = "UPDATE {cv} SET name = 'library_type' WHERE name = 'tripal_library_types'";
  259. chado_query($sql);
  260. }
  261. }
  262. catch (\PDOException $e) {
  263. $error = $e->getMessage();
  264. throw new DrupalUpdateException('Failed to change the vocabulary from tripal_library_types to library_type: '. $error);
  265. }
  266. // add the library_property CV
  267. try {
  268. $cv = tripal_insert_cv(
  269. 'library_property',
  270. 'Contains properties for libraries.'
  271. );
  272. if ($cv) {
  273. $cv_id = $cv->cv_id;
  274. // Set as Default CV for library properties.
  275. $is_set = tripal_get_default_cv('libraryprop', 'type_id');
  276. if (!$is_set) {
  277. tripal_set_default_cv('libraryprop','type_id', 'library_property', $cv_id);
  278. }
  279. }
  280. }
  281. catch (\PDOException $e) {
  282. $error = $e->getMessage();
  283. throw new DrupalUpdateException('Failed to add library_property vocabulary: '. $error);
  284. }
  285. // add the library_type CV
  286. try {
  287. // Note: tripal_insert_cv will only insert it if doesn't already exist
  288. // so this doesn't conflict with the update above.
  289. $cv = tripal_insert_cv(
  290. 'library_type',
  291. 'Contains terms for types of libraries (e.g. BAC, cDNA, FOSMID, etc).'
  292. );
  293. if ($cv) {
  294. $cv_id = $cv->cv_id;
  295. // Set as Default CV for library types.
  296. $is_set = tripal_get_default_cv('library', 'type_id');
  297. if (!$is_set) {
  298. tripal_set_default_cv('library','type_id', 'library_type', $cv_id);
  299. }
  300. }
  301. }
  302. catch (\PDOException $e) {
  303. $error = $e->getMessage();
  304. throw new DrupalUpdateException('Failed to add library_type vocabulary: '. $error);
  305. }
  306. // For Tripal in Drupal 6 the library_description cvterm was stored in the
  307. // 'tripal' CV. It should be stored in the new library_property CV that
  308. // is added by this module for Tripal 2.0 and Drupal 7. So, we need to
  309. // reset the CV ID for that term and rename the term to 'Library Description'
  310. try {
  311. $sql = "
  312. UPDATE {cvterm}
  313. SET
  314. name = 'Library Description',
  315. cv_id = (SELECT cv_id FROM {cv} WHERE name = 'library_property')
  316. WHERE
  317. name = 'library_description' AND
  318. cv_id = (SELECT cv_id FROM {cv} WHERE name = 'tripal')
  319. ";
  320. chado_query($sql);
  321. }
  322. catch (\PDOException $e) {
  323. $error = $e->getMessage();
  324. throw new DrupalUpdateException('Failed to change library_description property type to the library_property CV and update the name: '. $error);
  325. }
  326. // During the upgrade from D6 to D7 the vocabulary terms assigned to libraries were
  327. // copied to the field_data_taxonomyextra table rather than to the correct
  328. // field_data_taxonomy_vocabulary_[vid] table. We'll move them.
  329. $vid = db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE name = 'Library'")->fetchField();
  330. if ($vid) {
  331. try {
  332. if (db_table_exists('field_data_taxonomyextra')) {
  333. // first move from the field_data_taxonomyextra table
  334. $sql = "
  335. INSERT INTO {field_data_taxonomy_vocabulary_$vid}
  336. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  337. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  338. FROM field_data_taxonomyextra
  339. WHERE bundle = 'chado_feature')
  340. ";
  341. db_query($sql);
  342. $sql = "DELETE FROM field_data_taxonomyextra WHERE bundle = 'chado_library'";
  343. db_query($sql);
  344. // next move from the field_revision_taxonomyextra table
  345. $sql = "
  346. INSERT INTO {field_revision_taxonomy_vocabulary_$vid}
  347. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  348. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  349. FROM field_revision_taxonomyextra
  350. WHERE bundle = 'chado_feature')
  351. ";
  352. db_query($sql);
  353. $sql = "DELETE FROM field_revision_taxonomyextra WHERE bundle = 'chado_library'";
  354. db_query($sql);
  355. }
  356. }
  357. catch (\PDOException $e) {
  358. $error = $e->getMessage();
  359. throw new DrupalUpdateException('Could not move library taxonomy terms: '. $error);
  360. }
  361. }
  362. }
  363. /**
  364. * Implementation of hook_update_dependencies().
  365. *
  366. * It specifies a list of other modules whose updates must be run prior to
  367. * this one. It also ensures the the Tripal API is in scope for site
  368. * upgrades when tripal_core is disabled.
  369. */
  370. function tripal_library_update_dependencies() {
  371. $dependencies = array();
  372. // the tripal_cv update 7200 must run prior to update 7200 of this module
  373. $dependencies['tripal_library'][7200] = array(
  374. 'tripal_cv' => 7200
  375. );
  376. return $dependencies;
  377. }
  378. /**
  379. * Fixes an error with the materialized view installation
  380. *
  381. */
  382. function tripal_library_update_7201() {
  383. // Make sure we have the full API loaded this will help during a
  384. // site upgrade when the tripal_core module is disabled.
  385. module_load_include('module', 'tripal_core', 'tripal_core');
  386. tripal_core_import_api();
  387. // there is a bug in the Tripal v2.0-alpha release that didn't add the
  388. // materialized view schema to the mviews table.
  389. // get the schema for the materialized view from the custom_tables table
  390. // as there is a copy there, but only if the schema is missing from the
  391. // materialized view table
  392. $view_name = 'library_feature_count';
  393. $schema = db_select('tripal_mviews', 'tm')
  394. ->fields('tm', array('mv_schema'))
  395. ->condition('name', $view_name)
  396. ->execute()
  397. ->fetchField();
  398. if (!$schema or $schema == 'Array') {
  399. $schema = db_select('tripal_custom_tables', 'tct')
  400. ->fields('tct', array('schema'))
  401. ->condition('table_name', $view_name)
  402. ->execute()
  403. ->fetchField();
  404. $schema_str = var_export(unserialize($schema), TRUE);
  405. $schema_str = preg_replace('/=>\s+\n\s+array/', '=> array', $schema_str);
  406. db_update('tripal_mviews')
  407. ->fields(array(
  408. 'mv_schema' => $schema_str
  409. ))
  410. ->condition('name', $view_name)
  411. ->execute();
  412. }
  413. }