tripal_analysis.install 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. // the 'analysis_property' vocabulary is for user definable properties. Even though we already have
  93. // an analysis_type term in the 'tripal_analysis' vocabular we duplicate it here because the
  94. // tripal_analysis vocabulary is intended for use by the extension modules. user's should not be able
  95. // to directly modify properties set by extension modules for an analysis.
  96. tripal_cv_add_cvterm(array('name' => 'Analysis Type','def' => 'The type of analysis was performed.'),
  97. 'analysis_property', 0, 1, 'tripal');
  98. tripal_cv_add_cvterm(array('name' => 'Analysis Type','def' => 'The type of analysis was performed.'),
  99. 'analysis_property', 0, 1, 'tripal');
  100. }
  101. /**
  102. * Implementation of hook_uninstall().
  103. */
  104. function tripal_analysis_uninstall() {
  105. // Use schema API to delete database table.
  106. drupal_uninstall_schema('tripal_analysis');
  107. // Remove analysis nodes from drupal.
  108. $sql_ana_id = "SELECT nid, vid ".
  109. "FROM {node} ".
  110. "WHERE type like 'chado_analysi%'";
  111. $result = db_query($sql_ana_id);
  112. while ($ana = db_fetch_object($result)) {
  113. node_delete($ana->nid);
  114. }
  115. }
  116. /**
  117. * Implementation of hook_schema() creates two tables.
  118. *
  119. * - chado_analysis table
  120. * stores nodes that are also saved in the analysis table of chado database.
  121. * - tripal_analysis table
  122. * stores the sub-module names, such as tripal_analysis_blast, that are registered
  123. * with this module.
  124. */
  125. function tripal_analysis_schema() {
  126. // chado_analysis table
  127. $schema['chado_analysis'] = array(
  128. 'fields' => array(
  129. 'vid' => array(
  130. 'type' => 'int',
  131. 'unsigned' => TRUE,
  132. 'not null' => TRUE,
  133. 'default' => 0
  134. ),
  135. 'nid' => array(
  136. 'type' => 'int',
  137. 'unsigned' => TRUE,
  138. 'not null' => TRUE,
  139. 'default' => 0
  140. ),
  141. 'analysis_id' => array(
  142. 'type' => 'int',
  143. 'not null' => TRUE,
  144. 'default' => 0
  145. )
  146. ),
  147. 'indexes' => array(
  148. 'analysis_id' => array('analysis_id')
  149. ),
  150. 'unique keys' => array(
  151. 'nid_vid' => array('nid', 'vid'),
  152. 'vid' => array('vid')
  153. ),
  154. 'primary key' => array('nid'),
  155. );
  156. // tripal_analysis table
  157. $schema['tripal_analysis'] = array(
  158. 'description' => t('Table to store analysis sub-modules'),
  159. 'fields' => array(
  160. 'modulename' => array(
  161. 'type' => 'text',
  162. 'size' => 'small',
  163. 'not null' => TRUE,
  164. 'description' => t('The module name. Tripal Analysis will use the '.
  165. 'module name to call module_setting_form()')
  166. )
  167. ),
  168. 'unique keys' => array(
  169. 'modulename' => array('modulename')
  170. )
  171. );
  172. return $schema;
  173. }
  174. /**
  175. * Provide update script for adding new cvterms
  176. */
  177. function tripal_analysis_update_6001() {
  178. // we have some new cvterms to add
  179. 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');
  180. 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');
  181. $ret = array(
  182. '#finished' => 1,
  183. );
  184. return $ret;
  185. }
  186. /**
  187. * Implementation of hook_requirements().
  188. */
  189. function tripal_analysis_requirements($phase) {
  190. $requirements = array();
  191. if ($phase == 'install') {
  192. // make sure chado is installed
  193. if (!tripal_core_is_chado_installed()) {
  194. $requirements ['tripal_analysis'] = array(
  195. 'title' => "tripal_analysis",
  196. 'value' => "ERROR: Chado most be installed before this module can be enabled",
  197. 'severity' => REQUIREMENT_ERROR,
  198. );
  199. }
  200. }
  201. return $requirements;
  202. }
  203. /*
  204. *
  205. * @ingroup tripal_network
  206. */
  207. function tripal_analysis_add_mview_analysis_organism() {
  208. // this is the SQL used to identify the organism to which an analsysis
  209. // has been used. This is obtained though the analysisfeature -> feature -> organism
  210. // joins
  211. $sql = "
  212. SELECT DISTINCT A.analysis_id, O.organism_id
  213. FROM analysis A
  214. INNER JOIN analysisfeature AF ON A.analysis_id = AF.analysis_id
  215. INNER JOIN feature F ON AF.feature_id = F.feature_id
  216. INNER JOIN organism O ON O.organism_id = F.organism_id
  217. ";
  218. // the schema array for describing this view
  219. $schema = array(
  220. 'table' => 'analysis_organism',
  221. 'fields' => array(
  222. 'analysis_id' => array(
  223. 'type' => 'int',
  224. 'not null' => TRUE,
  225. ),
  226. 'organism_id' => array(
  227. 'type' => 'int',
  228. 'not null' => TRUE,
  229. ),
  230. ),
  231. 'indexes' => array(
  232. 'networkmod_qtl_indx0' => array('analysis_id'),
  233. 'networkmod_qtl_indx1' => array('organism_id'),
  234. ),
  235. 'foreign keys' => array(
  236. 'analysis' => array(
  237. 'table' => 'analysis',
  238. 'columns' => array(
  239. 'analysis_id' => 'analysis_id',
  240. ),
  241. ),
  242. 'organism' => array(
  243. 'table' => 'organism',
  244. 'columns' => array(
  245. 'organism_id' => 'organism_id',
  246. ),
  247. ),
  248. ),
  249. );
  250. // add a comment to make sure this view makes sense to the site administator
  251. $comment = t('This view is for associating an organism (via it\'s associated features) to an analysis.');
  252. // add the view
  253. tripal_add_mview('analysis_organism', 'tripal_analysis', NULL, NULL, NULL,
  254. $sql, NULL, $comment, $schema);
  255. }