tripal_library.install 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. /**
  7. * Disable default views when module is disabled
  8. */
  9. function tripal_library_disable() {
  10. // Disable all default views provided by this module
  11. require_once("tripal_library.views_default.inc");
  12. $views = tripal_library_views_default_views();
  13. foreach (array_keys($views) as $view_name) {
  14. tripal_views_admin_disable_view($view_name,FALSE,array('suppress_error' => TRUE));
  15. }
  16. }
  17. /**
  18. * Implementation of hook_requirements().
  19. */
  20. function tripal_library_requirements($phase) {
  21. $requirements = array();
  22. if ($phase == 'install') {
  23. // make sure chado is installed
  24. if (!$GLOBALS["chado_is_installed"]) {
  25. $requirements ['tripal_library'] = array(
  26. 'title' => "tripal_library",
  27. 'value' => "ERROR: Chado most be installed before this module can be enabled",
  28. 'severity' => REQUIREMENT_ERROR,
  29. );
  30. }
  31. }
  32. return $requirements;
  33. }
  34. /**
  35. * Implementation of hook_install().
  36. *
  37. * @ingroup tripal_library
  38. */
  39. function tripal_library_install() {
  40. // create the module's data directory
  41. tripal_create_moddir('tripal_library');
  42. // add the materialized view
  43. tripal_library_add_mview_library_feature_count();
  44. // add cvterms
  45. tripal_library_add_cvs();
  46. tripal_library_add_cvterms();
  47. }
  48. /**
  49. * Implementation of hook_uninstall().
  50. *
  51. * @ingroup tripal_library
  52. */
  53. function tripal_library_uninstall() {
  54. // Drop the MView table if it exists
  55. if ($mview_id = tripal_mviews_get_mview_id('library_feature_count')) {
  56. tripal_mviews_action("delete", $mview_id);
  57. }
  58. }
  59. /**
  60. * Implementation of hook_schema().
  61. *
  62. * @ingroup tripal_library
  63. */
  64. function tripal_library_schema() {
  65. $schema['chado_library'] = array(
  66. 'fields' => array(
  67. 'vid' => array(
  68. 'type' => 'int',
  69. 'unsigned' => TRUE,
  70. 'not null' => TRUE,
  71. 'default' => 0
  72. ),
  73. 'nid' => array(
  74. 'type' => 'int',
  75. 'unsigned' => TRUE,
  76. 'not null' => TRUE,
  77. 'default' => 0
  78. ),
  79. 'library_id' => array(
  80. 'type' => 'int',
  81. 'not null' => TRUE,
  82. 'default' => 0
  83. )
  84. ),
  85. 'indexes' => array(
  86. 'chado_library_idx1' => array('library_id')
  87. ),
  88. 'unique keys' => array(
  89. 'chado_library_uq1' => array('nid', 'vid'),
  90. 'chado_library_uq2' => array('vid')
  91. ),
  92. 'primary key' => array('nid'),
  93. );
  94. return $schema;
  95. }
  96. /**
  97. * @ingroup tripal_library
  98. */
  99. function tripal_library_add_mview_library_feature_count(){
  100. $view_name = 'library_feature_count';
  101. $comment = 'Provides count of feature by type that are associated with all libraries';
  102. $schema = array(
  103. 'table' => $view_name,
  104. 'description' => $comment,
  105. 'fields' => array(
  106. 'library_id' => array(
  107. 'type' => 'int',
  108. 'not null' => TRUE,
  109. ),
  110. 'name' => array(
  111. 'type' => 'varchar',
  112. 'length' => 255,
  113. 'not null' => TRUE,
  114. ),
  115. 'num_features' => array(
  116. 'type' => 'int',
  117. 'not null' => TRUE,
  118. ),
  119. 'feature_type' => array(
  120. 'type' => 'varchar',
  121. 'length' => 255,
  122. 'not null' => TRUE,
  123. ),
  124. ),
  125. 'indexes' => array(
  126. 'library_feature_count_idx1' => array('library_id'),
  127. ),
  128. );
  129. $sql = "
  130. SELECT
  131. L.library_id, L.name,
  132. count(F.feature_id) as num_features,
  133. CVT.name as feature_type
  134. FROM library L
  135. INNER JOIN library_feature LF ON LF.library_id = L.library_id
  136. INNER JOIN feature F ON LF.feature_id = F.feature_id
  137. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  138. GROUP BY L.library_id, L.name, CVT.name
  139. ";
  140. tripal_add_mview($view_name, 'tripal_library', $schema, $sql, $comment);
  141. }
  142. /**
  143. * Adds new CV's used by this module
  144. */
  145. function tripal_library_add_cvs(){
  146. tripal_cv_add_cv('library_property', 'Contains properties for libraries');
  147. tripal_cv_add_cv('library_type', 'Contains terms for types of libraries (e.g. BAC, cDNA, FOSMID, etc).');
  148. }
  149. /**
  150. * @ingroup tripal_library
  151. */
  152. function tripal_library_add_cvterms() {
  153. // Insert cvterm 'library_description' into cvterm table of chado
  154. // database. This CV term is used to keep track of the library
  155. // description in the libraryprop table.
  156. tripal_cv_add_cvterm(
  157. array(
  158. 'name' => 'Library Description',
  159. 'def' => 'Description of a library'
  160. ),
  161. 'library_property', 0, 1, 'tripal'
  162. );
  163. // add cvterms for the map unit types
  164. tripal_cv_add_cvterm(
  165. array(
  166. 'name' => 'cdna_library',
  167. 'def' => 'cDNA library'
  168. ),
  169. 'library_type', 0, 1, 'tripal'
  170. );
  171. tripal_cv_add_cvterm(
  172. array(
  173. 'name' => 'bac_library',
  174. 'def' => 'Bacterial Artifical Chromsome (BAC) library'
  175. ),
  176. 'library_type', 0, 1, 'tripal'
  177. );
  178. tripal_cv_add_cvterm(
  179. array(
  180. 'name' => 'fosmid_library',
  181. 'def' => 'Fosmid library'
  182. ),
  183. 'library_type', 0, 1, 'tripal'
  184. );
  185. tripal_cv_add_cvterm(
  186. array(
  187. 'name' => 'cosmid_library',
  188. 'def' => 'Cosmid library'
  189. ),
  190. 'library_type', 0, 1, 'tripal'
  191. );
  192. tripal_cv_add_cvterm(
  193. array(
  194. 'name' => 'yac_library',
  195. 'def' => 'Yeast Artificial Chromosome (YAC) library'
  196. ),
  197. 'library_type', 0, 1, 'tripal'
  198. );
  199. tripal_cv_add_cvterm(
  200. array(
  201. 'name' => 'genomic_library',
  202. 'def' => 'Genomic Library'),
  203. 'library_type', 0, 1, 'tripal'
  204. );
  205. }
  206. /**
  207. * This is the required update for tripal_library when upgrading from Drupal core API 6.x.
  208. */
  209. function tripal_library_update_7000() {
  210. // the library types were formerly in a vocabulary named 'tripal_library_types'.
  211. // rename that to just be 'library_type'
  212. $cv = tripal_cv_get_cv_by_name('tripal_library_types');
  213. $match = array(
  214. 'cv_id' => $cv->cv_id
  215. );
  216. $values = array(
  217. 'name' => 'library_type'
  218. );
  219. $success = tripal_core_chado_update('cv', $match, $values);
  220. if (!$success) {
  221. throw new DrupalUpdateException('Failed to rename tripal_library_types CV.');
  222. }
  223. // For Tripal in Drupal 6 the library_description cvterm was stored in the
  224. // 'tripal' CV. It should be stored in the new library_property CV that
  225. // is added by this module for Tripal 2.0 and Drupal 7. So, we need to
  226. // reset the CV ID for that term and rename the term to 'Library Description'
  227. tripal_library_add_cvs();
  228. $cv = tripal_cv_get_cv_by_name('library_property');
  229. $cvterm = tripal_cv_get_cvterm_by_name('library_description');
  230. $match = array(
  231. 'cvterm_id' => $cvterm->cvterm_id,
  232. );
  233. $values = array(
  234. 'cv_id' => $cv->cv_id,
  235. 'name' => 'Library Description',
  236. );
  237. tripal_core_chado_update('cvterm', $match, $values);
  238. if (!$success) {
  239. throw new DrupalUpdateException('Failed to move library properties to new library_property CV.');
  240. }
  241. }