tripal_library.install 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. * @ingroup tripal_library
  223. */
  224. function tripal_library_update_7000() {
  225. // the library types were formerly in a vocabulary named 'tripal_library_types'.
  226. // rename that to just be 'library_type'. We cannot use the Tripal API calls
  227. // because during upgrade the tripal_core should also be disabled
  228. try {
  229. $check = db_query("SELECT cv_id FROM chado.cv WHERE name = 'library_type'")->fetchObject();
  230. if (!$check->cv_id) {
  231. $sql = "UPDATE chado.cv SET name = 'library_type' WHERE name = 'tripal_library_types'";
  232. db_query($sql);
  233. }
  234. }
  235. catch (\PDOException $e) {
  236. $error = $e->getMessage();
  237. throw new DrupalUpdateException('Failed to change the vocabulary from tripal_library_types to library_type: '. $error);
  238. }
  239. // add the library_property CV
  240. try {
  241. $check = db_query("SELECT cv_id FROM chado.cv WHERE name = 'library_property'")->fetchObject();
  242. if (!$check->cv_id) {
  243. $sql = "INSERT INTO chado.cv (name, definition) VALUES (
  244. 'library_property',
  245. 'Contains properties for libraries.')
  246. ";
  247. db_query($sql);
  248. }
  249. }
  250. catch (\PDOException $e) {
  251. $error = $e->getMessage();
  252. throw new DrupalUpdateException('Failed to add library_property vocabulary: '. $error);
  253. }
  254. // add the library_type CV
  255. try {
  256. $check = db_query("SELECT cv_id FROM chado.cv WHERE name = 'library_type'")->fetchObject();
  257. if (!$check->cv_id) {
  258. $sql = "INSERT INTO chado.cv (name, definition) VALUES (
  259. 'library_type',
  260. 'Contains terms for types of libraries (e.g. BAC, cDNA, FOSMID, etc).')
  261. ";
  262. db_query($sql);
  263. }
  264. }
  265. catch (\PDOException $e) {
  266. $error = $e->getMessage();
  267. throw new DrupalUpdateException('Failed to add library_type vocabulary: '. $error);
  268. }
  269. // For Tripal in Drupal 6 the library_description cvterm was stored in the
  270. // 'tripal' CV. It should be stored in the new library_property CV that
  271. // is added by this module for Tripal 2.0 and Drupal 7. So, we need to
  272. // reset the CV ID for that term and rename the term to 'Library Description'
  273. // We cannot use the Tripal API calls in the 7000 update
  274. // because during upgrade the tripal_core should also be disabled
  275. $sql = "
  276. UPDATE chado.cvterm
  277. SET
  278. name = 'Library Description',
  279. cv_id = (SELECT cv_id FROM chado.cv WHERE name = 'library_property')
  280. WHERE
  281. name = 'library_description' AND
  282. cv_id = (SELECT cv_id FROM chado.cv WHERE name = 'tripal')
  283. ";
  284. try {
  285. db_query($sql);
  286. }
  287. catch (\PDOException $e) {
  288. $error = $e->getMessage();
  289. throw new DrupalUpdateException('Failed to change library_description property type to the library_property CV and update the name: '. $error);
  290. }
  291. }