tripal_library.install 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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_views_admin_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. }
  53. /**
  54. * Implementation of hook_uninstall().
  55. *
  56. * @ingroup tripal_library
  57. */
  58. function tripal_library_uninstall() {
  59. }
  60. /**
  61. * Implementation of hook_schema().
  62. *
  63. * @ingroup tripal_library
  64. */
  65. function tripal_library_schema() {
  66. $schema['chado_library'] = array(
  67. 'fields' => array(
  68. 'vid' => array(
  69. 'type' => 'int',
  70. 'unsigned' => TRUE,
  71. 'not null' => TRUE,
  72. 'default' => 0
  73. ),
  74. 'nid' => array(
  75. 'type' => 'int',
  76. 'unsigned' => TRUE,
  77. 'not null' => TRUE,
  78. 'default' => 0
  79. ),
  80. 'library_id' => array(
  81. 'type' => 'int',
  82. 'not null' => TRUE,
  83. 'default' => 0
  84. )
  85. ),
  86. 'indexes' => array(
  87. 'chado_library_idx1' => array('library_id')
  88. ),
  89. 'unique keys' => array(
  90. 'chado_library_uq1' => array('nid', 'vid'),
  91. 'chado_library_uq2' => array('vid')
  92. ),
  93. 'primary key' => array('nid'),
  94. );
  95. return $schema;
  96. }
  97. /**
  98. * Adds a materialized view keeping track of the type of features associated with each library
  99. *
  100. * @ingroup tripal_library
  101. */
  102. function tripal_library_add_mview_library_feature_count(){
  103. $view_name = 'library_feature_count';
  104. $comment = 'Provides count of feature by type that are associated with all libraries';
  105. $schema = array(
  106. 'table' => $view_name,
  107. 'description' => $comment,
  108. 'fields' => array(
  109. 'library_id' => array(
  110. 'type' => 'int',
  111. 'not null' => TRUE,
  112. ),
  113. 'name' => array(
  114. 'type' => 'varchar',
  115. 'length' => 255,
  116. 'not null' => TRUE,
  117. ),
  118. 'num_features' => array(
  119. 'type' => 'int',
  120. 'not null' => TRUE,
  121. ),
  122. 'feature_type' => array(
  123. 'type' => 'varchar',
  124. 'length' => 255,
  125. 'not null' => TRUE,
  126. ),
  127. ),
  128. 'indexes' => array(
  129. 'library_feature_count_idx1' => array('library_id'),
  130. ),
  131. );
  132. $sql = "
  133. SELECT
  134. L.library_id, L.name,
  135. count(F.feature_id) as num_features,
  136. CVT.name as feature_type
  137. FROM library L
  138. INNER JOIN library_feature LF ON LF.library_id = L.library_id
  139. INNER JOIN feature F ON LF.feature_id = F.feature_id
  140. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  141. GROUP BY L.library_id, L.name, CVT.name
  142. ";
  143. tripal_add_mview($view_name, 'tripal_library', $schema, $sql, $comment);
  144. }
  145. /**
  146. * Adds new CV's used by this module
  147. *
  148. * @ingroup tripal_library
  149. */
  150. function tripal_library_add_cvs(){
  151. tripal_cv_add_cv(
  152. 'library_property',
  153. 'Contains properties for libraries.'
  154. );
  155. tripal_cv_add_cv(
  156. 'library_type',
  157. 'Contains terms for types of libraries (e.g. BAC, cDNA, FOSMID, etc).'
  158. );
  159. }
  160. /**
  161. * Adds cvterms needed for the library module
  162. *
  163. * @ingroup tripal_library
  164. */
  165. function tripal_library_add_cvterms() {
  166. // Insert cvterm 'library_description' into cvterm table of chado
  167. // database. This CV term is used to keep track of the library
  168. // description in the libraryprop table.
  169. tripal_cv_add_cvterm(
  170. array(
  171. 'name' => 'Library Description',
  172. 'def' => 'Description of a library'
  173. ),
  174. 'library_property', 0, 1, 'tripal'
  175. );
  176. // add cvterms for the map unit types
  177. tripal_cv_add_cvterm(
  178. array(
  179. 'name' => 'cdna_library',
  180. 'def' => 'cDNA library'
  181. ),
  182. 'library_type', 0, 1, 'tripal'
  183. );
  184. tripal_cv_add_cvterm(
  185. array(
  186. 'name' => 'bac_library',
  187. 'def' => 'Bacterial Artifical Chromsome (BAC) library'
  188. ),
  189. 'library_type', 0, 1, 'tripal'
  190. );
  191. tripal_cv_add_cvterm(
  192. array(
  193. 'name' => 'fosmid_library',
  194. 'def' => 'Fosmid library'
  195. ),
  196. 'library_type', 0, 1, 'tripal'
  197. );
  198. tripal_cv_add_cvterm(
  199. array(
  200. 'name' => 'cosmid_library',
  201. 'def' => 'Cosmid library'
  202. ),
  203. 'library_type', 0, 1, 'tripal'
  204. );
  205. tripal_cv_add_cvterm(
  206. array(
  207. 'name' => 'yac_library',
  208. 'def' => 'Yeast Artificial Chromosome (YAC) library'
  209. ),
  210. 'library_type', 0, 1, 'tripal'
  211. );
  212. tripal_cv_add_cvterm(
  213. array(
  214. 'name' => 'genomic_library',
  215. 'def' => 'Genomic Library'),
  216. 'library_type', 0, 1, 'tripal'
  217. );
  218. }
  219. /**
  220. * This is the required update for tripal_library when upgrading from Drupal core API 6.x.
  221. *
  222. */
  223. function tripal_library_update_7200() {
  224. // the library types were formerly in a vocabulary named 'tripal_library_types'.
  225. // rename that to just be 'library_type'. We cannot use the Tripal API calls
  226. // because during upgrade the tripal_core should also be disabled
  227. try {
  228. $check = db_query("SELECT cv_id FROM chado.cv WHERE name = 'library_type'")->fetchObject();
  229. if (!$check->cv_id) {
  230. $sql = "UPDATE chado.cv SET name = 'library_type' WHERE name = 'tripal_library_types'";
  231. db_query($sql);
  232. }
  233. }
  234. catch (\PDOException $e) {
  235. $error = $e->getMessage();
  236. throw new DrupalUpdateException('Failed to change the vocabulary from tripal_library_types to library_type: '. $error);
  237. }
  238. // add the library_property CV
  239. try {
  240. $check = db_query("SELECT cv_id FROM chado.cv WHERE name = 'library_property'")->fetchObject();
  241. if (!$check->cv_id) {
  242. $sql = "INSERT INTO chado.cv (name, definition) VALUES (
  243. 'library_property',
  244. 'Contains properties for libraries.')
  245. ";
  246. db_query($sql);
  247. }
  248. }
  249. catch (\PDOException $e) {
  250. $error = $e->getMessage();
  251. throw new DrupalUpdateException('Failed to add library_property vocabulary: '. $error);
  252. }
  253. // add the library_type CV
  254. try {
  255. $check = db_query("SELECT cv_id FROM chado.cv WHERE name = 'library_type'")->fetchObject();
  256. if (!$check->cv_id) {
  257. $sql = "INSERT INTO chado.cv (name, definition) VALUES (
  258. 'library_type',
  259. 'Contains terms for types of libraries (e.g. BAC, cDNA, FOSMID, etc).')
  260. ";
  261. db_query($sql);
  262. }
  263. }
  264. catch (\PDOException $e) {
  265. $error = $e->getMessage();
  266. throw new DrupalUpdateException('Failed to add library_type vocabulary: '. $error);
  267. }
  268. // For Tripal in Drupal 6 the library_description cvterm was stored in the
  269. // 'tripal' CV. It should be stored in the new library_property CV that
  270. // is added by this module for Tripal 2.0 and Drupal 7. So, we need to
  271. // reset the CV ID for that term and rename the term to 'Library Description'
  272. // We cannot use the Tripal API calls in the 7000 update
  273. // because during upgrade the tripal_core should also be disabled
  274. $sql = "
  275. UPDATE chado.cvterm
  276. SET
  277. name = 'Library Description',
  278. cv_id = (SELECT cv_id FROM chado.cv WHERE name = 'library_property')
  279. WHERE
  280. name = 'library_description' AND
  281. cv_id = (SELECT cv_id FROM chado.cv WHERE name = 'tripal')
  282. ";
  283. try {
  284. db_query($sql);
  285. }
  286. catch (\PDOException $e) {
  287. $error = $e->getMessage();
  288. throw new DrupalUpdateException('Failed to change library_description property type to the library_property CV and update the name: '. $error);
  289. }
  290. // During the upgrade from D6 to D7 the vocabulary terms assigned to libraries were
  291. // copied to the field_data_taxonomyextra table rather than to the correct
  292. // field_data_taxonomy_vocabulary_[vid] table. We'll move them.
  293. $vid = db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE name = 'Library'")->fetchField();
  294. if ($vid) {
  295. try {
  296. // first move from the field_data_taxonomyextra table
  297. $sql = "
  298. INSERT INTO {field_data_taxonomy_vocabulary_$vid}
  299. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  300. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  301. FROM field_data_taxonomyextra
  302. WHERE bundle = 'chado_feature')
  303. ";
  304. db_query($sql);
  305. $sql = "DELETE FROM field_data_taxonomyextra WHERE bundle = 'chado_library'";
  306. db_query($sql);
  307. // next move from the field_revision_taxonomyextra table
  308. $sql = "
  309. INSERT INTO {field_revision_taxonomy_vocabulary_$vid}
  310. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  311. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  312. FROM field_revision_taxonomyextra
  313. WHERE bundle = 'chado_feature')
  314. ";
  315. db_query($sql);
  316. $sql = "DELETE FROM field_revision_taxonomyextra WHERE bundle = 'chado_library'";
  317. db_query($sql);
  318. }
  319. catch (\PDOException $e) {
  320. $error = $e->getMessage();
  321. throw new DrupalUpdateException('Could not move library taxonomy terms: '. $error);
  322. }
  323. }
  324. }