tripal_library.install 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. // Drop the MView table if it exists
  29. $previous_db = tripal_db_set_active('chado');
  30. if (db_table_exists('library_feature_count')) {
  31. $sql = "DROP TABLE library_feature_count";
  32. db_query($sql);
  33. }
  34. tripal_db_set_active($previous_db);
  35. // Create the MView
  36. tripal_add_mview('library_feature_count', 'tripal_library',
  37. 'library_feature_count',
  38. 'library_id integer, name character varying(255), '.
  39. ' num_features integer, feature_type character varying(255)',
  40. 'library_id',
  41. 'SELECT '.
  42. ' L.library_id, '.
  43. ' L.name, '.
  44. ' count(F.feature_id) as num_features, '.
  45. ' CVT.name as feature_type '.
  46. 'FROM {Library} L '.
  47. ' INNER JOIN Library_Feature LF ON LF.library_id = L.library_id '.
  48. ' INNER JOIN Feature F ON LF.feature_id = F.feature_id '.
  49. ' INNER JOIN Cvterm CVT ON F.type_id = CVT.cvterm_id '.
  50. 'GROUP BY L.library_id, L.name, CVT.name',
  51. ''
  52. );
  53. }
  54. /**
  55. * Implementation of hook_schema().
  56. *
  57. * @ingroup tripal_library
  58. */
  59. function tripal_library_schema() {
  60. $schema = tripal_library_get_schemas();
  61. return $schema;
  62. }
  63. /**
  64. * Implementation of hook_uninstall().
  65. *
  66. * @ingroup tripal_library
  67. */
  68. function tripal_library_uninstall() {
  69. drupal_uninstall_schema('tripal_library');
  70. // remove the materialized view
  71. $sql = "SELECT * FROM {tripal_mviews} ".
  72. "WHERE name = 'library_feature_count'";
  73. if (db_table_exists('tripal_mviews')) {
  74. $mview = db_fetch_object(db_query($sql));
  75. if ($mview) {
  76. tripal_mviews_action('delete', $mview->mview_id);
  77. }
  78. }
  79. // Get the list of nodes to remove
  80. $sql_lib_id = "SELECT nid, vid ".
  81. "FROM {node} ".
  82. "WHERE type='chado_library'";
  83. $result = db_query($sql_lib_id);
  84. while ($node = db_fetch_object($result)) {
  85. node_delete($node->nid);
  86. }
  87. }
  88. /**
  89. * This function simply defines all tables needed for the module to work
  90. * correctly. By putting the table definitions in a separate function we
  91. * can easily provide the entire list for hook_install or individual
  92. * tables for an update.
  93. *
  94. * @ingroup tripal_library
  95. */
  96. function tripal_library_get_schemas() {
  97. $schema = array();
  98. $schema['chado_library'] = array(
  99. 'fields' => array(
  100. 'vid' => array(
  101. 'type' => 'int',
  102. 'unsigned' => TRUE,
  103. 'not null' => TRUE,
  104. 'default' => 0
  105. ),
  106. 'nid' => array(
  107. 'type' => 'int',
  108. 'unsigned' => TRUE,
  109. 'not null' => TRUE,
  110. 'default' => 0
  111. ),
  112. 'library_id' => array(
  113. 'type' => 'int',
  114. 'not null' => TRUE,
  115. 'default' => 0
  116. )
  117. ),
  118. 'indexes' => array(
  119. 'library_id' => array('library_id')
  120. ),
  121. 'unique keys' => array(
  122. 'nid_vid' => array('nid', 'vid'),
  123. 'vid' => array('vid')
  124. ),
  125. 'primary key' => array('nid'),
  126. );
  127. return $schema;
  128. }
  129. /**
  130. * Implementation of hook_requirements(). Make sure 'Tripal Core' is enabled
  131. * before installation
  132. *
  133. * @ingroup tripal_library
  134. */
  135. function tripal_library_requirements($phase) {
  136. $requirements = array();
  137. if ($phase == 'install') {
  138. if (!function_exists('tripal_create_moddir')) {
  139. $requirements ['tripal_library'] = array(
  140. 'title' => "tripal_library",
  141. 'value' => "error. Some required modules are just being installed. Please try again.",
  142. 'severity' => REQUIREMENT_ERROR,
  143. );
  144. }
  145. }
  146. return $requirements;
  147. }