tripal_library.install 4.7 KB

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