tripal_analysis.install 9.8 KB

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