tripal_analysis.install 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * @file
  4. * Implements hooks from the Schema API
  5. */
  6. /**
  7. * Implementation of hook_requirements().
  8. */
  9. function tripal_analysis_requirements($phase) {
  10. $requirements = array();
  11. if ($phase == 'install') {
  12. // make sure chado is installed
  13. if (!tripal_core_is_chado_installed()) {
  14. $requirements ['tripal_analysis'] = array(
  15. 'title' => "tripal_analysis",
  16. 'value' => "ERROR: Chado most be installed before this module can be enabled",
  17. 'severity' => REQUIREMENT_ERROR,
  18. );
  19. }
  20. }
  21. return $requirements;
  22. }
  23. /**
  24. * Implementation of hook_install().
  25. */
  26. function tripal_analysis_install() {
  27. // create the module's data directory
  28. tripal_create_moddir('tripal_analysis');
  29. // we may need the analysisfeatureprop table if it doesn't already exist
  30. tripal_analysis_create_analysisfeatureprop();
  31. // add cvterms
  32. tripal_analysis_add_cvterms();
  33. // add materialized views
  34. tripal_analysis_add_mview_analysis_organism();
  35. }
  36. /**
  37. * Implementation of hook_uninstall().
  38. */
  39. function tripal_analysis_uninstall() {
  40. // Remove analysis nodes from drupal.
  41. $sql_ana_id = "
  42. SELECT nid, vid
  43. FROM {node}
  44. WHERE type like 'chado_analysi%'
  45. ";
  46. $result = db_query($sql_ana_id);
  47. while ($ana = $result->fetchObject()) {
  48. node_delete($ana->nid);
  49. }
  50. }
  51. /*
  52. *
  53. */
  54. function tripal_analysis_create_analysisfeatureprop() {
  55. // Create analysisfeatureprop table in chado. This is needed for Chado
  56. // version 1.11, the table exists in Chado 1.2.
  57. if (!db_table_exists('chado.analysisfeatureprop')) {
  58. $sql = "
  59. CREATE TABLE chado.analysisfeatureprop (
  60. analysisfeatureprop_id SERIAL PRIMARY KEY,
  61. analysisfeature_id INTEGER NOT NULL REFERENCES chado.analysisfeature(analysisfeature_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
  62. type_id INTEGER NOT NULL REFERENCES chdo.cvterm(cvterm_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
  63. value TEXT,
  64. rank INTEGER NOT NULL,
  65. CONSTRAINT analysisfeature_id_type_id_rank UNIQUE(analysisfeature_id, type_id, rank)
  66. )
  67. ";
  68. chado_query($sql);
  69. }
  70. }
  71. /*
  72. *
  73. */
  74. function tripal_analysis_add_cvterms() {
  75. tripal_cv_add_cv('tripal_analysis', 'Terms used for managing analyses in Tripal');
  76. // add analysis_date. This is no longer used (as far as we can tell) but we don't
  77. // get rid of it in case it is used, so just keep it in the Tripal CV
  78. $term = array(
  79. 'name' => 'analysis_date',
  80. 'def' => 'The date that an analysis was performed.'
  81. );
  82. tripal_cv_add_cvterm($term, 'tripal', 0, 1, 'tripal');
  83. // add analysis_short_name. This is no longer used (as far as we can tell) but we don't
  84. // get rid of it in case it is used, so just keep it in the Tripal CV
  85. $term = array(
  86. 'name' => 'analysis_short_name',
  87. 'def' => 'A computer legible (no spaces or special characters) abbreviation for the analysis.'
  88. );
  89. tripal_cv_add_cvterm($term, 'tripal', 0, 1 , 'tripal');
  90. // the 'analysis_property' vocabulary is for user definable properties.
  91. tripal_cv_add_cvterm(array('name' => 'Analysis Type', 'def' => 'The type of analysis that was performed.'),
  92. 'analysis_property', 0, 1, 'tripal');
  93. }
  94. /**
  95. * Implementation of hook_schema() creates two tables.
  96. *
  97. * - chado_analysis table
  98. * stores nodes that are also saved in the analysis table of chado database.
  99. * - tripal_analysis table
  100. * stores the sub-module names, such as tripal_analysis_blast, that are registered
  101. * with this module.
  102. */
  103. function tripal_analysis_schema() {
  104. // chado_analysis table
  105. $schema['chado_analysis'] = array(
  106. 'fields' => array(
  107. 'vid' => array(
  108. 'type' => 'int',
  109. 'unsigned' => TRUE,
  110. 'not null' => TRUE,
  111. 'default' => 0
  112. ),
  113. 'nid' => array(
  114. 'type' => 'int',
  115. 'unsigned' => TRUE,
  116. 'not null' => TRUE,
  117. 'default' => 0
  118. ),
  119. 'analysis_id' => array(
  120. 'type' => 'int',
  121. 'not null' => TRUE,
  122. 'default' => 0
  123. )
  124. ),
  125. 'indexes' => array(
  126. 'analysis_id' => array('analysis_id')
  127. ),
  128. 'unique keys' => array(
  129. 'nid_vid' => array('nid', 'vid'),
  130. 'vid' => array('vid')
  131. ),
  132. 'primary key' => array('nid'),
  133. );
  134. // tripal_analysis table
  135. $schema['tripal_analysis'] = array(
  136. 'description' => 'Table to store analysis sub-modules',
  137. 'fields' => array(
  138. 'modulename' => array(
  139. 'type' => 'text',
  140. 'size' => 'small',
  141. 'not null' => TRUE,
  142. 'description' => 'The module name. Tripal Analysis will use the module name to call module_setting_form()'
  143. )
  144. ),
  145. 'unique keys' => array(
  146. 'modulename' => array('modulename')
  147. )
  148. );
  149. return $schema;
  150. }
  151. /*
  152. *
  153. * @ingroup tripal_network
  154. */
  155. function tripal_analysis_add_mview_analysis_organism() {
  156. $view_name = 'analysis_organism';
  157. $comment = t('This view is for associating an organism (via it\'s associated features) to an analysis.');
  158. // this is the SQL used to identify the organism to which an analsysis
  159. // has been used. This is obtained though the analysisfeature -> feature -> organism
  160. // joins
  161. $sql = "
  162. SELECT DISTINCT A.analysis_id, O.organism_id
  163. FROM analysis A
  164. INNER JOIN analysisfeature AF ON A.analysis_id = AF.analysis_id
  165. INNER JOIN feature F ON AF.feature_id = F.feature_id
  166. INNER JOIN organism O ON O.organism_id = F.organism_id
  167. ";
  168. // the schema array for describing this view
  169. $schema = array(
  170. 'table' => $view_name,
  171. 'description' => $comment,
  172. 'fields' => array(
  173. 'analysis_id' => array(
  174. 'type' => 'int',
  175. 'not null' => TRUE,
  176. ),
  177. 'organism_id' => array(
  178. 'type' => 'int',
  179. 'not null' => TRUE,
  180. ),
  181. ),
  182. 'indexes' => array(
  183. 'networkmod_qtl_indx0' => array('analysis_id'),
  184. 'networkmod_qtl_indx1' => array('organism_id'),
  185. ),
  186. 'foreign keys' => array(
  187. 'analysis' => array(
  188. 'table' => 'analysis',
  189. 'columns' => array(
  190. 'analysis_id' => 'analysis_id',
  191. ),
  192. ),
  193. 'organism' => array(
  194. 'table' => 'organism',
  195. 'columns' => array(
  196. 'organism_id' => 'organism_id',
  197. ),
  198. ),
  199. ),
  200. );
  201. // add the view
  202. tripal_add_mview($view_name, 'tripal_analysis', $schema, $sql, $comment);
  203. }