tripal_library.install 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 must 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. }
  55. /**
  56. * Implementation of hook_schema().
  57. *
  58. * @ingroup tripal_library
  59. */
  60. function tripal_library_schema() {
  61. $schema['chado_library'] = array(
  62. 'fields' => array(
  63. 'vid' => array(
  64. 'type' => 'int',
  65. 'unsigned' => TRUE,
  66. 'not null' => TRUE,
  67. 'default' => 0
  68. ),
  69. 'nid' => array(
  70. 'type' => 'int',
  71. 'unsigned' => TRUE,
  72. 'not null' => TRUE,
  73. 'default' => 0
  74. ),
  75. 'library_id' => array(
  76. 'type' => 'int',
  77. 'not null' => TRUE,
  78. 'default' => 0
  79. )
  80. ),
  81. 'indexes' => array(
  82. 'chado_library_idx1' => array('library_id')
  83. ),
  84. 'unique keys' => array(
  85. 'chado_library_uq1' => array('nid', 'vid'),
  86. 'chado_library_uq2' => array('vid')
  87. ),
  88. 'primary key' => array('nid'),
  89. );
  90. return $schema;
  91. }
  92. /**
  93. * @ingroup tripal_library
  94. */
  95. function tripal_library_add_mview_library_feature_count(){
  96. $view_name = 'library_feature_count';
  97. $comment = 'Provides count of feature by type that are associated with all libraries';
  98. $schema = array(
  99. 'table' => $view_name,
  100. 'description' => $comment,
  101. 'fields' => array(
  102. 'library_id' => array(
  103. 'type' => 'int',
  104. 'not null' => TRUE,
  105. ),
  106. 'name' => array(
  107. 'type' => 'varchar',
  108. 'length' => 255,
  109. 'not null' => TRUE,
  110. ),
  111. 'num_features' => array(
  112. 'type' => 'int',
  113. 'not null' => TRUE,
  114. ),
  115. 'feature_type' => array(
  116. 'type' => 'varchar',
  117. 'length' => 255,
  118. 'not null' => TRUE,
  119. ),
  120. ),
  121. 'indexes' => array(
  122. 'library_feature_count_idx1' => array('library_id'),
  123. ),
  124. );
  125. $sql = "
  126. SELECT
  127. L.library_id, L.name,
  128. count(F.feature_id) as num_features,
  129. CVT.name as feature_type
  130. FROM library L
  131. INNER JOIN library_feature LF ON LF.library_id = L.library_id
  132. INNER JOIN feature F ON LF.feature_id = F.feature_id
  133. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  134. GROUP BY L.library_id, L.name, CVT.name
  135. ";
  136. tripal_add_mview($view_name, 'tripal_library', $schema, $sql, $comment);
  137. }
  138. /**
  139. * Adds new CV's used by this module
  140. */
  141. function tripal_library_add_cvs(){
  142. tripal_cv_add_cv(
  143. 'library_property',
  144. 'Contains properties for libraries'
  145. );
  146. tripal_cv_add_cv(
  147. 'library_type',
  148. 'Contains terms for types of libraries (e.g. BAC, cDNA, FOSMID, etc).'
  149. );
  150. }
  151. /**
  152. * @ingroup tripal_library
  153. */
  154. function tripal_library_add_cvterms() {
  155. // Insert cvterm 'library_description' into cvterm table of chado
  156. // database. This CV term is used to keep track of the library
  157. // description in the libraryprop table.
  158. tripal_cv_add_cvterm(
  159. array(
  160. 'name' => 'Library Description',
  161. 'def' => 'Description of a library'
  162. ),
  163. 'library_property', 0, 1, 'tripal'
  164. );
  165. // add cvterms for the map unit types
  166. tripal_cv_add_cvterm(
  167. array(
  168. 'name' => 'cdna_library',
  169. 'def' => 'cDNA library'
  170. ),
  171. 'library_type', 0, 1, 'tripal'
  172. );
  173. tripal_cv_add_cvterm(
  174. array(
  175. 'name' => 'bac_library',
  176. 'def' => 'Bacterial Artifical Chromsome (BAC) library'
  177. ),
  178. 'library_type', 0, 1, 'tripal'
  179. );
  180. tripal_cv_add_cvterm(
  181. array(
  182. 'name' => 'fosmid_library',
  183. 'def' => 'Fosmid library'
  184. ),
  185. 'library_type', 0, 1, 'tripal'
  186. );
  187. tripal_cv_add_cvterm(
  188. array(
  189. 'name' => 'cosmid_library',
  190. 'def' => 'Cosmid library'
  191. ),
  192. 'library_type', 0, 1, 'tripal'
  193. );
  194. tripal_cv_add_cvterm(
  195. array(
  196. 'name' => 'yac_library',
  197. 'def' => 'Yeast Artificial Chromosome (YAC) library'
  198. ),
  199. 'library_type', 0, 1, 'tripal'
  200. );
  201. tripal_cv_add_cvterm(
  202. array(
  203. 'name' => 'genomic_library',
  204. 'def' => 'Genomic Library'),
  205. 'library_type', 0, 1, 'tripal'
  206. );
  207. }
  208. /**
  209. * This is the required update for tripal_library when upgrading from Drupal core API 6.x.
  210. */
  211. function tripal_library_update_7000() {
  212. // the library types were formerly in a vocabulary named 'tripal_library_types'.
  213. // rename that to just be 'library_type'
  214. $cv = tripal_cv_get_cv_by_name('tripal_library_types');
  215. $match = array(
  216. 'cv_id' => $cv->cv_id
  217. );
  218. $values = array(
  219. 'name' => 'library_type'
  220. );
  221. $success = tripal_core_chado_update('cv', $match, $values);
  222. if (!$success) {
  223. throw new DrupalUpdateException('Failed to rename tripal_library_types CV.');
  224. }
  225. // For Tripal in Drupal 6 the library_description cvterm was stored in the
  226. // 'tripal' CV. It should be stored in the new library_property CV that
  227. // is added by this module for Tripal 2.0 and Drupal 7. So, we need to
  228. // reset the CV ID for that term and rename the term to 'Library Description'
  229. tripal_library_add_cvs();
  230. $cv = tripal_cv_get_cv_by_name('library_property');
  231. $cvterm = tripal_cv_get_cvterm_by_name('library_description');
  232. $match = array(
  233. 'cvterm_id' => $cvterm->cvterm_id,
  234. );
  235. $values = array(
  236. 'cv_id' => $cv->cv_id,
  237. 'name' => 'Library Description',
  238. );
  239. tripal_core_chado_update('cvterm', $match, $values);
  240. if (!$success) {
  241. throw new DrupalUpdateException('Failed to move library properties to new library_property CV.');
  242. }
  243. }