tripal_analysis.install 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. // Use schema API to create database table.
  13. drupal_install_schema('tripal_analysis');
  14. // we may need the analysisfeatureprop table if it doesn't already exist
  15. tripal_analysis_create_analysisfeatureprop();
  16. // add cvterms
  17. tripal_analysis_add_cvterms();
  18. // add materialized views
  19. tripal_analysis_add_mview_analysis_organism();
  20. }
  21. /**
  22. * Update for Drupal 6.x, Tripal 1.1, Analysis Module 1.1
  23. * This update adds a new analysis_organism materialized view
  24. *
  25. */
  26. function tripal_analysis_update_6100() {
  27. // add the new materialized view
  28. tripal_analysis_add_mview_analysis_organism();
  29. $ret = array(
  30. '#finished' => 1,
  31. );
  32. return $ret;
  33. }
  34. /*
  35. *
  36. */
  37. function tripal_analysis_create_analysisfeatureprop(){
  38. // Create analysisfeatureprop table in chado. This is needed for Chado
  39. // version 1.11, the table exists in Chado 1.2.
  40. if (!db_table_exists('analysisfeatureprop')) {
  41. $sql = "CREATE TABLE analysisfeatureprop (".
  42. " analysisfeatureprop_id SERIAL PRIMARY KEY, ".
  43. " analysisfeature_id INTEGER NOT NULL REFERENCES analysisfeature(analysisfeature_id) " .
  44. " ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, ".
  45. " type_id INTEGER NOT NULL REFERENCES cvterm(cvterm_id) ".
  46. " ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, ".
  47. " value TEXT, ".
  48. " rank INTEGER NOT NULL, ".
  49. " CONSTRAINT analysisfeature_id_type_id_rank UNIQUE(analysisfeature_id, type_id, rank)".
  50. ")";
  51. chado_query($sql);
  52. }
  53. }
  54. /*
  55. *
  56. */
  57. function tripal_analysis_add_cvterms(){
  58. // add analysis_type
  59. $term = array(
  60. 'name' => 'analysis_type',
  61. 'def' => 'The type of analysis was performed. This value is automatically set by '.
  62. 'each Tripal Analysis module and should be equal to the module name '.
  63. '(e.g. tripal_analysis_blast, tripal_analysis_go).'
  64. );
  65. tripal_cv_add_cvterm($term, 'tripal', 0, 1, 'tripal');
  66. // add analysis_date
  67. $term = array(
  68. 'name' => 'analysis_date',
  69. 'def' => 'The date that an analysis was performed.'
  70. );
  71. tripal_cv_add_cvterm($term, 'tripal', 0, 1, 'tripal');
  72. // add analysis_short_name
  73. $term = array(
  74. 'name' => 'analysis_short_name',
  75. 'def' => 'A computer legible (no spaces or special characters) abbreviation for the analysis.'
  76. );
  77. tripal_cv_add_cvterm($term, 'tripal', 0, 1 , 'tripal');
  78. }
  79. /**
  80. * Implementation of hook_uninstall().
  81. */
  82. function tripal_analysis_uninstall() {
  83. // Use schema API to delete database table.
  84. drupal_uninstall_schema('tripal_analysis');
  85. // Remove analysis nodes from drupal.
  86. $sql_ana_id = "SELECT nid, vid ".
  87. "FROM {node} ".
  88. "WHERE type like 'chado_analysi%'";
  89. $result = db_query($sql_ana_id);
  90. while ($ana = db_fetch_object($result)) {
  91. node_delete($ana->nid);
  92. }
  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' => t('Table to store analysis sub-modules'),
  137. 'fields' => array(
  138. 'modulename' => array(
  139. 'type' => 'text',
  140. 'size' => 'small',
  141. 'not null' => TRUE,
  142. 'description' => t('The module name. Tripal Analysis will use the '.
  143. 'module name to call module_setting_form()')
  144. )
  145. ),
  146. 'unique keys' => array(
  147. 'modulename' => array('modulename')
  148. )
  149. );
  150. return $schema;
  151. }
  152. /**
  153. * Provide update script for adding new cvterms
  154. */
  155. function tripal_analysis_update_6001() {
  156. // we have some new cvterms to add
  157. tripal_cv_add_cvterm(array('name' => 'based_on_analysis', 'def' => 'The analysis that this analysis was based on. For example, blast/kegg/interpro analyses are based on a unigene analysis. The unigene analysis_id should be stored in analysisprop as the rank using this cvterm. The name of said unigene analysis can be inserted as the value in analysisprop.'), 'tripal', 0, 1, 'tripal');
  158. tripal_cv_add_cvterm(array('name' => 'additional_files', 'def' => 'Additional files for this analysis. Each file should be separated by a semi-colon and have this format: <file description>, <file path>;'), 'tripal', 0, 1, 'tripal');
  159. $ret = array(
  160. '#finished' => 1,
  161. );
  162. return $ret;
  163. }
  164. /**
  165. * Implementation of hook_requirements().
  166. */
  167. function tripal_analysis_requirements($phase) {
  168. $requirements = array();
  169. if ($phase == 'install') {
  170. // make sure chado is installed
  171. if (!tripal_core_is_chado_installed()) {
  172. $requirements ['tripal_analysis'] = array(
  173. 'title' => "tripal_analysis",
  174. 'value' => "ERROR: Chado most be installed before this module can be enabled",
  175. 'severity' => REQUIREMENT_ERROR,
  176. );
  177. }
  178. }
  179. return $requirements;
  180. }
  181. /*
  182. *
  183. * @ingroup tripal_network
  184. */
  185. function tripal_analysis_add_mview_analysis_organism() {
  186. // this is the SQL used to identify the organism to which an analsysis
  187. // has been used. This is obtained though the analysisfeature -> feature -> organism
  188. // joins
  189. $sql = "
  190. SELECT DISTINCT A.analysis_id, O.organism_id
  191. FROM analysis A
  192. INNER JOIN analysisfeature AF ON A.analysis_id = AF.analysis_id
  193. INNER JOIN feature F ON AF.feature_id = F.feature_id
  194. INNER JOIN organism O ON O.organism_id = F.organism_id
  195. ";
  196. // the schema array for describing this view
  197. $schema = array(
  198. 'table' => 'analysis_organism',
  199. 'fields' => array(
  200. 'analysis_id' => array(
  201. 'type' => 'int',
  202. 'not null' => TRUE,
  203. ),
  204. 'organism_id' => array(
  205. 'type' => 'int',
  206. 'not null' => TRUE,
  207. ),
  208. ),
  209. 'indexes' => array(
  210. 'networkmod_qtl_indx0' => array('analysis_id'),
  211. 'networkmod_qtl_indx1' => array('organism_id'),
  212. ),
  213. 'foreign keys' => array(
  214. 'analysis' => array(
  215. 'table' => 'analysis',
  216. 'columns' => array(
  217. 'analysis_id' => 'analysis_id',
  218. ),
  219. ),
  220. 'organism' => array(
  221. 'table' => 'organism',
  222. 'columns' => array(
  223. 'organism_id' => 'organism_id',
  224. ),
  225. ),
  226. ),
  227. );
  228. // add a comment to make sure this view makes sense to the site administator
  229. $comment = t('This view is for associating an organism (via it\'s associated features) to an analysis.');
  230. // add the view
  231. tripal_add_mview('analysis_organism', 'tripal_analysis', NULL, NULL, NULL,
  232. $sql, NULL, $comment, $schema);
  233. }