tripal_analysis.install 7.2 KB

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