tripal_analysis.install 9.4 KB

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