tripal_analysis.install 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. // move the analysis_type property into a new CV so that user's can change this property if
  30. // they want too
  31. tripal_cv_add_cv('tripal_analysis', 'Terms used for managing analyses in Tripal');
  32. $sql = "
  33. UPDATE {cvterm} SET cv_id =
  34. (SELECT cv_id FROM {cv} WHERE name = 'tripal_analysis')
  35. WHERE cv_id = (SELECT cv_id FROM {cv} WHERE name = 'tripal') AND
  36. name = 'analysis_type'
  37. ";
  38. chado_query($sql);
  39. $ret = array(
  40. '#finished' => 1,
  41. );
  42. return $ret;
  43. }
  44. /*
  45. *
  46. */
  47. function tripal_analysis_create_analysisfeatureprop(){
  48. // Create analysisfeatureprop table in chado. This is needed for Chado
  49. // version 1.11, the table exists in Chado 1.2.
  50. if (!db_table_exists('analysisfeatureprop')) {
  51. $sql = "CREATE TABLE analysisfeatureprop (".
  52. " analysisfeatureprop_id SERIAL PRIMARY KEY, ".
  53. " analysisfeature_id INTEGER NOT NULL REFERENCES analysisfeature(analysisfeature_id) " .
  54. " ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, ".
  55. " type_id INTEGER NOT NULL REFERENCES cvterm(cvterm_id) ".
  56. " ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, ".
  57. " value TEXT, ".
  58. " rank INTEGER NOT NULL, ".
  59. " CONSTRAINT analysisfeature_id_type_id_rank UNIQUE(analysisfeature_id, type_id, rank)".
  60. ")";
  61. chado_query($sql);
  62. }
  63. }
  64. /*
  65. *
  66. */
  67. function tripal_analysis_add_cvterms(){
  68. tripal_cv_add_cv('tripal_analysis', 'Terms used for managing analyses in Tripal');
  69. // add analysis_type. This goes in the 'tripal_analysis' CV so that site admins
  70. // change change this property
  71. $term = array(
  72. 'name' => 'analysis_type',
  73. 'def' => 'The type of analysis was performed. This value is automatically set by '.
  74. 'each Tripal Analysis module and should be equal to the module name '.
  75. '(e.g. tripal_analysis_blast, tripal_analysis_go).'
  76. );
  77. tripal_cv_add_cvterm($term, 'tripal_analysis', 0, 1, 'tripal');
  78. // add analysis_date. This is no longer used (as far as we can tell) but we don't
  79. // get rid of it in case it is used, so just keep it in the Tripal CV
  80. $term = array(
  81. 'name' => 'analysis_date',
  82. 'def' => 'The date that an analysis was performed.'
  83. );
  84. tripal_cv_add_cvterm($term, 'tripal', 0, 1, 'tripal');
  85. // add analysis_short_name. 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_short_name',
  89. 'def' => 'A computer legible (no spaces or special characters) abbreviation for the analysis.'
  90. );
  91. tripal_cv_add_cvterm($term, 'tripal', 0, 1 , 'tripal');
  92. }
  93. /**
  94. * Implementation of hook_uninstall().
  95. */
  96. function tripal_analysis_uninstall() {
  97. // Use schema API to delete database table.
  98. drupal_uninstall_schema('tripal_analysis');
  99. // Remove analysis nodes from drupal.
  100. $sql_ana_id = "SELECT nid, vid ".
  101. "FROM {node} ".
  102. "WHERE type like 'chado_analysi%'";
  103. $result = db_query($sql_ana_id);
  104. while ($ana = db_fetch_object($result)) {
  105. node_delete($ana->nid);
  106. }
  107. }
  108. /**
  109. * Implementation of hook_schema() creates two tables.
  110. *
  111. * - chado_analysis table
  112. * stores nodes that are also saved in the analysis table of chado database.
  113. * - tripal_analysis table
  114. * stores the sub-module names, such as tripal_analysis_blast, that are registered
  115. * with this module.
  116. */
  117. function tripal_analysis_schema() {
  118. // chado_analysis table
  119. $schema['chado_analysis'] = array(
  120. 'fields' => array(
  121. 'vid' => array(
  122. 'type' => 'int',
  123. 'unsigned' => TRUE,
  124. 'not null' => TRUE,
  125. 'default' => 0
  126. ),
  127. 'nid' => array(
  128. 'type' => 'int',
  129. 'unsigned' => TRUE,
  130. 'not null' => TRUE,
  131. 'default' => 0
  132. ),
  133. 'analysis_id' => array(
  134. 'type' => 'int',
  135. 'not null' => TRUE,
  136. 'default' => 0
  137. )
  138. ),
  139. 'indexes' => array(
  140. 'analysis_id' => array('analysis_id')
  141. ),
  142. 'unique keys' => array(
  143. 'nid_vid' => array('nid', 'vid'),
  144. 'vid' => array('vid')
  145. ),
  146. 'primary key' => array('nid'),
  147. );
  148. // tripal_analysis table
  149. $schema['tripal_analysis'] = array(
  150. 'description' => t('Table to store analysis sub-modules'),
  151. 'fields' => array(
  152. 'modulename' => array(
  153. 'type' => 'text',
  154. 'size' => 'small',
  155. 'not null' => TRUE,
  156. 'description' => t('The module name. Tripal Analysis will use the '.
  157. 'module name to call module_setting_form()')
  158. )
  159. ),
  160. 'unique keys' => array(
  161. 'modulename' => array('modulename')
  162. )
  163. );
  164. return $schema;
  165. }
  166. /**
  167. * Provide update script for adding new cvterms
  168. */
  169. function tripal_analysis_update_6001() {
  170. // we have some new cvterms to add
  171. 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');
  172. 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');
  173. $ret = array(
  174. '#finished' => 1,
  175. );
  176. return $ret;
  177. }
  178. /**
  179. * Implementation of hook_requirements().
  180. */
  181. function tripal_analysis_requirements($phase) {
  182. $requirements = array();
  183. if ($phase == 'install') {
  184. // make sure chado is installed
  185. if (!tripal_core_is_chado_installed()) {
  186. $requirements ['tripal_analysis'] = array(
  187. 'title' => "tripal_analysis",
  188. 'value' => "ERROR: Chado most be installed before this module can be enabled",
  189. 'severity' => REQUIREMENT_ERROR,
  190. );
  191. }
  192. }
  193. return $requirements;
  194. }
  195. /*
  196. *
  197. * @ingroup tripal_network
  198. */
  199. function tripal_analysis_add_mview_analysis_organism() {
  200. // this is the SQL used to identify the organism to which an analsysis
  201. // has been used. This is obtained though the analysisfeature -> feature -> organism
  202. // joins
  203. $sql = "
  204. SELECT DISTINCT A.analysis_id, O.organism_id
  205. FROM analysis A
  206. INNER JOIN analysisfeature AF ON A.analysis_id = AF.analysis_id
  207. INNER JOIN feature F ON AF.feature_id = F.feature_id
  208. INNER JOIN organism O ON O.organism_id = F.organism_id
  209. ";
  210. // the schema array for describing this view
  211. $schema = array(
  212. 'table' => 'analysis_organism',
  213. 'fields' => array(
  214. 'analysis_id' => array(
  215. 'type' => 'int',
  216. 'not null' => TRUE,
  217. ),
  218. 'organism_id' => array(
  219. 'type' => 'int',
  220. 'not null' => TRUE,
  221. ),
  222. ),
  223. 'indexes' => array(
  224. 'networkmod_qtl_indx0' => array('analysis_id'),
  225. 'networkmod_qtl_indx1' => array('organism_id'),
  226. ),
  227. 'foreign keys' => array(
  228. 'analysis' => array(
  229. 'table' => 'analysis',
  230. 'columns' => array(
  231. 'analysis_id' => 'analysis_id',
  232. ),
  233. ),
  234. 'organism' => array(
  235. 'table' => 'organism',
  236. 'columns' => array(
  237. 'organism_id' => 'organism_id',
  238. ),
  239. ),
  240. ),
  241. );
  242. // add a comment to make sure this view makes sense to the site administator
  243. $comment = t('This view is for associating an organism (via it\'s associated features) to an analysis.');
  244. // add the view
  245. tripal_add_mview('analysis_organism', 'tripal_analysis', NULL, NULL, NULL,
  246. $sql, NULL, $comment, $schema);
  247. }