tripal_library.install 12 KB

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