tripal_feature.install 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. /**
  7. * Implementation of hook_install().
  8. *
  9. * @ingroup tripal_feature
  10. */
  11. function tripal_feature_install() {
  12. // create the module's data directory
  13. tripal_create_moddir('tripal_feature');
  14. // add the materialized view
  15. tripal_feature_add_organism_count_mview();
  16. }
  17. /**
  18. *
  19. * @ingroup tripal_feature
  20. */
  21. function tripal_feature_add_organism_count_mview() {
  22. $view_name = 'organism_feature_count';
  23. // Drop the MView table if it exists
  24. $mview_id = tripal_mviews_get_mview_id($view_name);
  25. if ($mview_id) {
  26. tripal_mviews_action("delete", $mview_id);
  27. }
  28. // Create the MView
  29. tripal_add_mview(
  30. // view name
  31. $view_name,
  32. // tripal module name
  33. 'tripal_feature',
  34. // table name
  35. $view_name,
  36. // table schema definition
  37. 'organism_id integer, genus character varying(255), ' .
  38. ' species character varying(255), ' .
  39. ' common_name character varying(255), ' .
  40. ' num_features integer, cvterm_id integer, ' .
  41. ' feature_type character varying(255)',
  42. // columns for indexing
  43. 'organism_id,cvterm_id,feature_type',
  44. // SQL statement to populate the view
  45. 'SELECT O.organism_id, O.genus, O.species, O.common_name,
  46. count(F.feature_id) as num_features,
  47. CVT.cvterm_id, CVT.name as feature_type
  48. FROM Organism O
  49. INNER JOIN Feature F ON O.Organism_id = F.organism_id
  50. INNER JOIN Cvterm CVT ON F.type_id = CVT.cvterm_id
  51. GROUP BY O.Organism_id, O.genus, O.species, O.common_name,
  52. CVT.cvterm_id, CVT.name',
  53. // special index
  54. ''
  55. );
  56. // add a job to the job queue so this view gets updated automatically next
  57. // time the job facility is run
  58. $mview_id = tripal_mviews_get_mview_id($view_name);
  59. if ($mview_id) {
  60. tripal_mviews_action('update', $mview_id);
  61. }
  62. }
  63. /**
  64. * Implementation of hook_schema().
  65. *
  66. * @ingroup tripal_feature
  67. */
  68. function tripal_feature_schema() {
  69. $schema = tripal_feature_get_schemas();
  70. return $schema;
  71. }
  72. /**
  73. * Implementation of hook_uninstall().
  74. *
  75. * @ingroup tripal_feature
  76. */
  77. function tripal_feature_uninstall() {
  78. // Drop the MView table if it exists
  79. $mview_id = tripal_mviews_get_mview_id('organism_feature_count');
  80. if ($mview_id) {
  81. tripal_mviews_action("delete", $mview_id);
  82. }
  83. // Get the list of nodes to remove
  84. $sql_feature_id = "SELECT nid, vid " .
  85. "FROM {node} " .
  86. "WHERE type='chado_feature'";
  87. $result = db_query($sql_feature_id);
  88. while ($node = db_fetch_object($result)) {
  89. node_delete($node->nid);
  90. }
  91. }
  92. /**
  93. * This function simply defines all tables needed for the module to work
  94. * correctly. By putting the table definitions in a separate function we
  95. * can easily provide the entire list for hook_install or individual
  96. * tables for an update.
  97. *
  98. * @ingroup tripal_feature
  99. */
  100. function tripal_feature_get_schemas($table = NULL) {
  101. $schema = array();
  102. if (!$table or strcmp($table, 'chado_feature')==0) {
  103. $schema['chado_feature'] = array(
  104. 'fields' => array(
  105. 'vid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
  106. 'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
  107. 'feature_id' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
  108. 'sync_date' => array('type' => 'int', 'not null' => FALSE, 'description' => 'UNIX integer sync date/time'),
  109. ),
  110. 'indexes' => array(
  111. 'feature_id' => array('feature_id')
  112. ),
  113. 'unique keys' => array(
  114. 'nid_vid' => array('nid', 'vid'),
  115. 'vid' => array('vid')
  116. ),
  117. 'primary key' => array('nid'),
  118. );
  119. }
  120. return $schema;
  121. };
  122. /**
  123. * Implementation of hook_requirements().
  124. */
  125. function tripal_feature_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_feature'] = array(
  131. 'title' => "tripal_feature",
  132. 'value' => "ERROR: Chado most be installed before this module can be enabled",
  133. 'severity' => REQUIREMENT_ERROR,
  134. );
  135. }
  136. }
  137. return $requirements;
  138. }