tripal_library.install 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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('library_property', 'Contains properties for libraries');
  143. tripal_cv_add_cv('library_type', 'Contains terms for types of libraries (e.g. BAC, cDNA, FOSMID, etc).');
  144. }
  145. /**
  146. * @ingroup tripal_library
  147. */
  148. function tripal_library_add_cvterms() {
  149. // Insert cvterm 'library_description' into cvterm table of chado
  150. // database. This CV term is used to keep track of the library
  151. // description in the libraryprop table.
  152. tripal_cv_add_cvterm(
  153. array(
  154. 'name' => 'Library Description',
  155. 'def' => 'Description of a library'
  156. ),
  157. 'library_property', 0, 1, 'tripal'
  158. );
  159. // add cvterms for the map unit types
  160. tripal_cv_add_cvterm(
  161. array(
  162. 'name' => 'cdna_library',
  163. 'def' => 'cDNA library'
  164. ),
  165. 'library_type', 0, 1, 'tripal'
  166. );
  167. tripal_cv_add_cvterm(
  168. array(
  169. 'name' => 'bac_library',
  170. 'def' => 'Bacterial Artifical Chromsome (BAC) library'
  171. ),
  172. 'library_type', 0, 1, 'tripal'
  173. );
  174. tripal_cv_add_cvterm(
  175. array(
  176. 'name' => 'fosmid_library',
  177. 'def' => 'Fosmid library'
  178. ),
  179. 'library_type', 0, 1, 'tripal'
  180. );
  181. tripal_cv_add_cvterm(
  182. array(
  183. 'name' => 'cosmid_library',
  184. 'def' => 'Cosmid library'
  185. ),
  186. 'library_type', 0, 1, 'tripal'
  187. );
  188. tripal_cv_add_cvterm(
  189. array(
  190. 'name' => 'yac_library',
  191. 'def' => 'Yeast Artificial Chromosome (YAC) library'
  192. ),
  193. 'library_type', 0, 1, 'tripal'
  194. );
  195. tripal_cv_add_cvterm(
  196. array(
  197. 'name' => 'genomic_library',
  198. 'def' => 'Genomic Library'),
  199. 'library_type', 0, 1, 'tripal'
  200. );
  201. }
  202. /**
  203. * This is the required update for tripal_library when upgrading from Drupal core API 6.x.
  204. */
  205. function tripal_library_update_7000() {
  206. // the library types were formerly in a vocabulary named 'tripal_library_types'.
  207. // rename that to just be 'library_type'
  208. $cv = tripal_cv_get_cv_by_name('tripal_library_types');
  209. $match = array(
  210. 'cv_id' => $cv->cv_id
  211. );
  212. $values = array(
  213. 'name' => 'library_type'
  214. );
  215. $success = tripal_core_chado_update('cv', $match, $values);
  216. if (!$success) {
  217. throw new DrupalUpdateException('Failed to rename tripal_library_types CV.');
  218. }
  219. // For Tripal in Drupal 6 the library_description cvterm was stored in the
  220. // 'tripal' CV. It should be stored in the new library_property CV that
  221. // is added by this module for Tripal 2.0 and Drupal 7. So, we need to
  222. // reset the CV ID for that term and rename the term to 'Library Description'
  223. tripal_library_add_cvs();
  224. $cv = tripal_cv_get_cv_by_name('library_property');
  225. $cvterm = tripal_cv_get_cvterm_by_name('library_description');
  226. $match = array(
  227. 'cvterm_id' => $cvterm->cvterm_id,
  228. );
  229. $values = array(
  230. 'cv_id' => $cv->cv_id,
  231. 'name' => 'Library Description',
  232. );
  233. tripal_core_chado_update('cvterm', $match, $values);
  234. if (!$success) {
  235. throw new DrupalUpdateException('Failed to move library properties to new library_property CV.');
  236. }
  237. }