tripal_analysis.install 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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_type. This goes in the 'tripal_analysis' CV so that site admins
  77. // change change this property
  78. $term = array(
  79. 'name' => 'analysis_type',
  80. 'def' => 'The type of analysis was performed. This value is automatically set by ' .
  81. 'each Tripal Analysis module and should be equal to the module name ' .
  82. '(e.g. tripal_analysis_blast, tripal_analysis_go).'
  83. );
  84. tripal_cv_add_cvterm($term, 'tripal_analysis', 0, 1, 'tripal');
  85. // add analysis_date. This is no longer used (as far as we can tell) but we don't
  86. // get rid of it in case it is used, so just keep it in the Tripal CV
  87. $term = array(
  88. 'name' => 'analysis_date',
  89. 'def' => 'The date that an analysis was performed.'
  90. );
  91. tripal_cv_add_cvterm($term, 'tripal', 0, 1, 'tripal');
  92. // add analysis_short_name. This is no longer used (as far as we can tell) but we don't
  93. // get rid of it in case it is used, so just keep it in the Tripal CV
  94. $term = array(
  95. 'name' => 'analysis_short_name',
  96. 'def' => 'A computer legible (no spaces or special characters) abbreviation for the analysis.'
  97. );
  98. tripal_cv_add_cvterm($term, 'tripal', 0, 1 , 'tripal');
  99. // the 'analysis_property' vocabulary is for user definable properties. Even though we already have
  100. // an analysis_type term in the 'tripal_analysis' vocabular we duplicate it here because the
  101. // tripal_analysis vocabulary is intended for use by the extension modules. user's should not be able
  102. // to directly modify properties set by extension modules for an analysis.
  103. tripal_cv_add_cvterm(array('name' => 'Analysis Type', 'def' => 'The type of analysis was performed.'),
  104. 'analysis_property', 0, 1, 'tripal');
  105. }
  106. /**
  107. * Implementation of hook_schema() creates two tables.
  108. *
  109. * - chado_analysis table
  110. * stores nodes that are also saved in the analysis table of chado database.
  111. * - tripal_analysis table
  112. * stores the sub-module names, such as tripal_analysis_blast, that are registered
  113. * with this module.
  114. */
  115. function tripal_analysis_schema() {
  116. // chado_analysis table
  117. $schema['chado_analysis'] = array(
  118. 'fields' => array(
  119. 'vid' => array(
  120. 'type' => 'int',
  121. 'unsigned' => TRUE,
  122. 'not null' => TRUE,
  123. 'default' => 0
  124. ),
  125. 'nid' => array(
  126. 'type' => 'int',
  127. 'unsigned' => TRUE,
  128. 'not null' => TRUE,
  129. 'default' => 0
  130. ),
  131. 'analysis_id' => array(
  132. 'type' => 'int',
  133. 'not null' => TRUE,
  134. 'default' => 0
  135. )
  136. ),
  137. 'indexes' => array(
  138. 'analysis_id' => array('analysis_id')
  139. ),
  140. 'unique keys' => array(
  141. 'nid_vid' => array('nid', 'vid'),
  142. 'vid' => array('vid')
  143. ),
  144. 'primary key' => array('nid'),
  145. );
  146. // tripal_analysis table
  147. $schema['tripal_analysis'] = array(
  148. 'description' => 'Table to store analysis sub-modules',
  149. 'fields' => array(
  150. 'modulename' => array(
  151. 'type' => 'text',
  152. 'size' => 'small',
  153. 'not null' => TRUE,
  154. 'description' => 'The module name. Tripal Analysis will use the module name to call module_setting_form()'
  155. )
  156. ),
  157. 'unique keys' => array(
  158. 'modulename' => array('modulename')
  159. )
  160. );
  161. return $schema;
  162. }
  163. /*
  164. *
  165. * @ingroup tripal_network
  166. */
  167. function tripal_analysis_add_mview_analysis_organism() {
  168. $view_name = 'analysis_organism';
  169. $comment = t('This view is for associating an organism (via it\'s associated features) to an analysis.');
  170. // this is the SQL used to identify the organism to which an analsysis
  171. // has been used. This is obtained though the analysisfeature -> feature -> organism
  172. // joins
  173. $sql = "
  174. SELECT DISTINCT A.analysis_id, O.organism_id
  175. FROM analysis A
  176. INNER JOIN analysisfeature AF ON A.analysis_id = AF.analysis_id
  177. INNER JOIN feature F ON AF.feature_id = F.feature_id
  178. INNER JOIN organism O ON O.organism_id = F.organism_id
  179. ";
  180. // the schema array for describing this view
  181. $schema = array(
  182. 'table' => $view_name,
  183. 'description' => $comment,
  184. 'fields' => array(
  185. 'analysis_id' => array(
  186. 'type' => 'int',
  187. 'not null' => TRUE,
  188. ),
  189. 'organism_id' => array(
  190. 'type' => 'int',
  191. 'not null' => TRUE,
  192. ),
  193. ),
  194. 'indexes' => array(
  195. 'networkmod_qtl_indx0' => array('analysis_id'),
  196. 'networkmod_qtl_indx1' => array('organism_id'),
  197. ),
  198. 'foreign keys' => array(
  199. 'analysis' => array(
  200. 'table' => 'analysis',
  201. 'columns' => array(
  202. 'analysis_id' => 'analysis_id',
  203. ),
  204. ),
  205. 'organism' => array(
  206. 'table' => 'organism',
  207. 'columns' => array(
  208. 'organism_id' => 'organism_id',
  209. ),
  210. ),
  211. ),
  212. );
  213. // add the view
  214. tripal_add_mview($view_name, 'tripal_analysis', $schema, $sql, $comment);
  215. }