tripal_library.install 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. /**
  7. * Implementation of hook_install().
  8. *
  9. * @ingroup tripal_library
  10. */
  11. function tripal_library_install() {
  12. // create the module's data directory
  13. tripal_create_moddir('tripal_library');
  14. // create the tables that correlate drupal nodes with chado
  15. // features, librarys, etc....
  16. drupal_install_schema('tripal_library');
  17. // Insert cvterm 'library_description' into cvterm table of chado
  18. // database. This CV term is used to keep track of the library
  19. // description in the libraryprop table.
  20. tripal_cv_add_cvterm(array('name' => 'library_description', 'def' => 'Description of a library'), 'tripal', 0, 1, 'tripal');
  21. // Add CVTerms for the library types
  22. tripal_cv_add_cvterm(array('name' => 'cdna_library', 'def' => 'cDNA Library'), 'tripal', 0, 1, 'tripal');
  23. tripal_cv_add_cvterm(array('name' => 'bac_library', 'def' => 'BAC Library'), 'tripal', 0, 1, 'tripal');
  24. tripal_cv_add_cvterm(array('name' => 'fosmid_library', 'def' => 'FOSMID Library'), 'tripal', 0, 1, 'tripal');
  25. tripal_cv_add_cvterm(array('name' => 'cosmid_library', 'def' => 'COSMID Library'), 'tripal', 0, 1, 'tripal');
  26. tripal_cv_add_cvterm(array('name' => 'yac_library', 'def' => 'YAC Library'), 'tripal', 0, 1, 'tripal');
  27. // Add the materialized view needed to count the features for the library
  28. // Create the MView
  29. tripal_add_mview('library_feature_count', 'tripal_library',
  30. 'library_feature_count',
  31. 'library_id integer, name character varying(255), '.
  32. ' num_features integer, feature_type character varying(255)',
  33. 'library_id',
  34. 'SELECT '.
  35. ' L.library_id, '.
  36. ' L.name, '.
  37. ' count(F.feature_id) as num_features, '.
  38. ' CVT.name as feature_type '.
  39. 'FROM {Library} L '.
  40. ' INNER JOIN Library_Feature LF ON LF.library_id = L.library_id '.
  41. ' INNER JOIN Feature F ON LF.feature_id = F.feature_id '.
  42. ' INNER JOIN Cvterm CVT ON F.type_id = CVT.cvterm_id '.
  43. 'GROUP BY L.library_id, L.name, CVT.name',
  44. ''
  45. );
  46. }
  47. /**
  48. * Implementation of hook_schema().
  49. *
  50. * @ingroup tripal_library
  51. */
  52. function tripal_library_schema() {
  53. $schema = tripal_library_get_schemas();
  54. return $schema;
  55. }
  56. /**
  57. * Implementation of hook_uninstall().
  58. *
  59. * @ingroup tripal_library
  60. */
  61. function tripal_library_uninstall() {
  62. drupal_uninstall_schema('tripal_library');
  63. // remove the materialized view
  64. $sql = "SELECT * FROM {tripal_mviews} ".
  65. "WHERE name = 'library_feature_count'";
  66. if (db_table_exists('tripal_mviews')) {
  67. $mview = db_fetch_object(db_query($sql));
  68. if ($mview) {
  69. tripal_mviews_action('delete', $mview->mview_id);
  70. }
  71. }
  72. // Get the list of nodes to remove
  73. $sql_lib_id = "SELECT nid, vid ".
  74. "FROM {node} ".
  75. "WHERE type='chado_library'";
  76. $result = db_query($sql_lib_id);
  77. while ($node = db_fetch_object($result)) {
  78. node_delete($node->nid);
  79. }
  80. }
  81. /**
  82. * This function simply defines all tables needed for the module to work
  83. * correctly. By putting the table definitions in a separate function we
  84. * can easily provide the entire list for hook_install or individual
  85. * tables for an update.
  86. *
  87. * @ingroup tripal_library
  88. */
  89. function tripal_library_get_schemas() {
  90. $schema = array();
  91. $schema['chado_library'] = array(
  92. 'fields' => array(
  93. 'vid' => array(
  94. 'type' => 'int',
  95. 'unsigned' => TRUE,
  96. 'not null' => TRUE,
  97. 'default' => 0
  98. ),
  99. 'nid' => array(
  100. 'type' => 'int',
  101. 'unsigned' => TRUE,
  102. 'not null' => TRUE,
  103. 'default' => 0
  104. ),
  105. 'library_id' => array(
  106. 'type' => 'int',
  107. 'not null' => TRUE,
  108. 'default' => 0
  109. )
  110. ),
  111. 'indexes' => array(
  112. 'library_id' => array('library_id')
  113. ),
  114. 'unique keys' => array(
  115. 'nid_vid' => array('nid', 'vid'),
  116. 'vid' => array('vid')
  117. ),
  118. 'primary key' => array('nid'),
  119. );
  120. return $schema;
  121. }
  122. /**
  123. * Implementation of hook_requirements().
  124. */
  125. function tripal_library_requirements($phase) {
  126. $requirements = array();
  127. if ($phase == 'install') {
  128. // make sure chado is installed
  129. if (!tripal_core_is_chado_installed()) {
  130. $requirements ['tripal_library'] = array(
  131. 'title' => "tripal_library",
  132. 'value' => "ERROR: Chado most be installed before this module can be enabled",
  133. 'severity' => REQUIREMENT_ERROR,
  134. );
  135. }
  136. }
  137. return $requirements;
  138. }