tripal_stock.install 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Install the tripal stock module including it's content type
  4. * @file
  5. */
  6. /**
  7. * Implements hook_disable().
  8. * Disable default views when module is disabled
  9. *
  10. * @ingroup tripal_legacy_stock
  11. */
  12. function tripal_stock_disable() {
  13. // Disable all default views provided by this module
  14. require_once("tripal_stock.views_default.inc");
  15. $views = tripal_stock_views_default_views();
  16. foreach (array_keys($views) as $view_name) {
  17. tripal_disable_view($view_name,FALSE,array('suppress_error' => TRUE));
  18. }
  19. }
  20. /**
  21. * Implementation of hook_requirements().
  22. *
  23. * @ingroup tripal_legacy_stock
  24. */
  25. function tripal_stock_requirements($phase) {
  26. $requirements = array();
  27. if ($phase == 'install') {
  28. // make sure chado is installed
  29. if (!$GLOBALS["chado_is_installed"]) {
  30. $requirements ['tripal_stock'] = array(
  31. 'title' => "tripal_stock",
  32. 'value' => "ERROR: Chado must be installed before this module can be enabled",
  33. 'severity' => REQUIREMENT_ERROR,
  34. );
  35. }
  36. }
  37. return $requirements;
  38. }
  39. /**
  40. * Implementation of hook_install().
  41. *
  42. * @ingroup tripal_legacy_stock
  43. */
  44. function tripal_stock_install() {
  45. // set the default vocabularies
  46. tripal_set_default_cv('stock', 'type_id', 'stock_type');
  47. tripal_set_default_cv('stockprop', 'type_id', 'stock_property');
  48. tripal_set_default_cv('stock_relationship', 'type_id', 'stock_relationship');
  49. // add the materialized view
  50. tripal_stock_add_organism_count_mview();
  51. }
  52. /**
  53. * Implementation of hook_uninstall().
  54. *
  55. * @ingroup tripal_legacy_stock
  56. */
  57. function tripal_stock_uninstall() {
  58. }
  59. /**
  60. * Implementation of hook_schema().
  61. *
  62. * @ingroup tripal_legacy_stock
  63. */
  64. function tripal_stock_schema() {
  65. $schema['chado_stock'] = array(
  66. 'fields' => array(
  67. 'vid' => array(
  68. 'type' => 'int',
  69. 'unsigned' => TRUE,
  70. 'not null' => TRUE,
  71. ),
  72. 'nid' => array(
  73. 'type' => 'int',
  74. 'unsigned' => TRUE,
  75. 'not null' => TRUE,
  76. ),
  77. 'stock_id' => array(
  78. 'type' => 'int',
  79. 'unsigned' => TRUE,
  80. 'not null' => TRUE,
  81. ),
  82. ),
  83. 'indexes' => array(
  84. 'stock_id' => array('stock_id'),
  85. 'nid' => array('nid'),
  86. ),
  87. 'unique' => array(
  88. 'stock_id' => array('stock_id'),
  89. ),
  90. 'primary key' => array('vid'),
  91. );
  92. return $schema;
  93. }
  94. /**
  95. * Creates a materialized view that stores the type & number of stocks per organism
  96. *
  97. * @ingroup tripal_stock
  98. */
  99. function tripal_stock_add_organism_count_mview() {
  100. $view_name = 'organism_stock_count';
  101. $comment = 'Stores the type and number of stocks per organism';
  102. $schema = array(
  103. 'description' => $comment,
  104. 'table' => $view_name,
  105. 'fields' => array(
  106. 'organism_id' => array(
  107. 'size' => 'big',
  108. 'type' => 'int',
  109. 'not null' => TRUE,
  110. ),
  111. 'genus' => array(
  112. 'type' => 'varchar',
  113. 'length' => '255',
  114. 'not null' => TRUE,
  115. ),
  116. 'species' => array(
  117. 'type' => 'varchar',
  118. 'length' => '255',
  119. 'not null' => TRUE,
  120. ),
  121. 'common_name' => array(
  122. 'type' => 'varchar',
  123. 'length' => '255',
  124. 'not null' => FALSE,
  125. ),
  126. 'num_stocks' => array(
  127. 'type' => 'int',
  128. 'not null' => TRUE,
  129. ),
  130. 'cvterm_id' => array(
  131. 'size' => 'big',
  132. 'type' => 'int',
  133. 'not null' => TRUE,
  134. ),
  135. 'stock_type' => array(
  136. 'type' => 'varchar',
  137. 'length' => '255',
  138. 'not null' => TRUE,
  139. ),
  140. ),
  141. 'indexes' => array(
  142. 'organism_stock_count_idx1' => array('organism_id'),
  143. 'organism_stock_count_idx2' => array('cvterm_id'),
  144. 'organism_stock_count_idx3' => array('stock_type'),
  145. ),
  146. );
  147. $sql = "
  148. SELECT
  149. O.organism_id, O.genus, O.species, O.common_name,
  150. count(S.stock_id) as num_stocks,
  151. CVT.cvterm_id, CVT.name as stock_type
  152. FROM organism O
  153. INNER JOIN stock S ON O.Organism_id = S.organism_id
  154. INNER JOIN cvterm CVT ON S.type_id = CVT.cvterm_id
  155. GROUP BY
  156. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  157. ";
  158. tripal_add_mview($view_name, 'tripal_stock', $schema, $sql, $comment);
  159. }