tripal_library.install 4.8 KB

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