tripal_feature.install 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. /**
  3. * @file
  4. * Installation of the feature module
  5. */
  6. /**
  7. * Implements hook_disable().
  8. *
  9. * Disable default views when module is disabled
  10. *
  11. * @ingroup tripal_feature
  12. */
  13. function tripal_feature_disable() {
  14. // Disable all default views provided by this module
  15. require_once("tripal_feature.views_default.inc");
  16. $views = tripal_feature_views_default_views();
  17. foreach (array_keys($views) as $view_name) {
  18. tripal_disable_view($view_name,FALSE,array('suppress_error' => TRUE));
  19. }
  20. }
  21. /**
  22. * Implements hook_requirements().
  23. *
  24. * @ingroup tripal_feature
  25. */
  26. function tripal_feature_requirements($phase) {
  27. $requirements = array();
  28. if ($phase == 'install') {
  29. // make sure chado is installed
  30. if (!$GLOBALS["chado_is_installed"]) {
  31. $requirements ['tripal_feature'] = array(
  32. 'title' => "t ripal_feature",
  33. 'value' => "ERROR: Chado must be installed before this module can be enabled",
  34. 'severity' => REQUIREMENT_ERROR,
  35. );
  36. }
  37. }
  38. return $requirements;
  39. }
  40. /**
  41. * Implements hook_install().
  42. *
  43. * @ingroup tripal_feature
  44. */
  45. function tripal_feature_install() {
  46. // Note: the feature_property OBO that came with Chado v1.2 should not
  47. // be automatically installed. Some of the terms are duplicates of
  48. // others in better maintained vocabularies. New Tripal sites should
  49. // use those.
  50. // $obo_path = '{tripal_feature}/files/feature_property.obo';
  51. // $obo_id = tripal_insert_obo('Chado Feature Properties', $obo_path);
  52. // tripal_submit_obo_job(array('obo_id' => $obo_id));
  53. // Add the materialized view.
  54. tripal_feature_add_organism_count_mview();
  55. // Add the custom tables.
  56. tripal_feature_add_tripal_gff_temp_table();
  57. tripal_feature_add_tripal_gffcds_temp_table();
  58. tripal_feature_add_tripal_gffprotein_temp_table();
  59. // Add the vocabularies used by the feature module.
  60. tripal_feature_add_cvs();
  61. // Set the default vocabularies.
  62. tripal_set_default_cv('feature', 'type_id', 'sequence');
  63. tripal_set_default_cv('featureprop', 'type_id', 'feature_property');
  64. tripal_set_default_cv('feature_relationship', 'type_id', 'feature_relationship');
  65. }
  66. /**
  67. * Implements hook_uninstall().
  68. *
  69. * @ingroup tripal_feature
  70. */
  71. function tripal_feature_uninstall() {
  72. }
  73. function tripal_feature_add_tripal_gff_temp_table() {
  74. $schema = array(
  75. 'table' => 'tripal_gff_temp',
  76. 'fields' => array(
  77. 'feature_id' => array(
  78. 'type' => 'int',
  79. 'not null' => TRUE,
  80. ),
  81. 'organism_id' => array(
  82. 'type' => 'int',
  83. 'not null' => TRUE,
  84. ),
  85. 'uniquename' => array(
  86. 'type' => 'text',
  87. 'not null' => TRUE,
  88. ),
  89. 'type_name' => array(
  90. 'type' => 'varchar',
  91. 'length' => '1024',
  92. 'not null' => TRUE,
  93. ),
  94. ),
  95. 'indexes' => array(
  96. 'tripal_gff_temp_idx0' => array('feature_id'),
  97. 'tripal_gff_temp_idx0' => array('organism_id'),
  98. 'tripal_gff_temp_idx1' => array('uniquename'),
  99. ),
  100. 'unique keys' => array(
  101. 'tripal_gff_temp_uq0' => array('feature_id'),
  102. 'tripal_gff_temp_uq1' => array('uniquename', 'organism_id', 'type_name'),
  103. ),
  104. );
  105. chado_create_custom_table('tripal_gff_temp', $schema, TRUE);
  106. }
  107. /**
  108. *
  109. */
  110. function tripal_feature_add_tripal_gffcds_temp_table($skip_recreate = TRUE) {
  111. $schema = array(
  112. 'table' => 'tripal_gffcds_temp',
  113. 'fields' => array(
  114. 'feature_id' => array(
  115. 'type' => 'int',
  116. 'not null' => TRUE,
  117. ),
  118. 'parent_id' => array(
  119. 'type' => 'int',
  120. 'not null' => TRUE,
  121. ),
  122. 'phase' => array(
  123. 'type' => 'int',
  124. 'not null' => TRUE,
  125. ),
  126. 'strand' => array(
  127. 'type' => 'int',
  128. 'not null' => TRUE,
  129. ),
  130. 'fmin' => array(
  131. 'type' => 'int',
  132. 'not null' => TRUE,
  133. ),
  134. 'fmax' => array(
  135. 'type' => 'int',
  136. 'not null' => TRUE,
  137. ),
  138. ),
  139. 'indexes' => array(
  140. 'tripal_gff_temp_idx0' => array('feature_id'),
  141. 'tripal_gff_temp_idx0' => array('parent_id'),
  142. ),
  143. );
  144. chado_create_custom_table('tripal_gffcds_temp', $schema, $skip_recreate);
  145. }
  146. /**
  147. *
  148. */
  149. function tripal_feature_add_tripal_gffprotein_temp_table() {
  150. $schema = array(
  151. 'table' => 'tripal_gffprotein_temp',
  152. 'fields' => array(
  153. 'feature_id' => array(
  154. 'type' => 'int',
  155. 'not null' => TRUE,
  156. ),
  157. 'parent_id' => array(
  158. 'type' => 'int',
  159. 'not null' => TRUE,
  160. ),
  161. 'fmin' => array(
  162. 'type' => 'int',
  163. 'not null' => TRUE,
  164. ),
  165. 'fmax' => array(
  166. 'type' => 'int',
  167. 'not null' => TRUE,
  168. ),
  169. ),
  170. 'indexes' => array(
  171. 'tripal_gff_temp_idx0' => array('feature_id'),
  172. 'tripal_gff_temp_idx0' => array('parent_id'),
  173. ),
  174. 'unique keys' => array(
  175. 'tripal_gff_temp_uq0' => array('feature_id'),
  176. ),
  177. );
  178. chado_create_custom_table('tripal_gffprotein_temp', $schema, TRUE);
  179. }
  180. /**
  181. * Implementation of hook_schema().
  182. *
  183. * @ingroup tripal_feature
  184. */
  185. function tripal_feature_schema() {
  186. $schema['chado_feature'] = array(
  187. 'fields' => array(
  188. 'vid' => array(
  189. 'type' => 'int',
  190. 'unsigned' => TRUE,
  191. 'not null' => TRUE,
  192. 'default' => 0
  193. ),
  194. 'nid' => array(
  195. 'type' => 'int',
  196. 'unsigned' => TRUE,
  197. 'not null' => TRUE,
  198. 'default' => 0
  199. ),
  200. 'feature_id' => array(
  201. 'type' => 'int',
  202. 'not null' => TRUE,
  203. 'default' => 0
  204. ),
  205. 'sync_date' => array(
  206. 'type' => 'int',
  207. 'not null' => FALSE,
  208. 'description' => 'UNIX integer sync date/time'
  209. ),
  210. ),
  211. 'indexes' => array(
  212. 'chado_feature_idx1' => array('feature_id')
  213. ),
  214. 'unique keys' => array(
  215. 'chado_feature_uq1' => array('nid', 'vid'),
  216. 'chado_feature_uq2' => array('vid')
  217. ),
  218. 'primary key' => array('nid'),
  219. );
  220. return $schema;
  221. };
  222. /**
  223. * Creates a materialized view that stores the type & number of features per organism
  224. *
  225. * @ingroup tripal_feature
  226. */
  227. function tripal_feature_add_organism_count_mview() {
  228. $view_name = 'organism_feature_count';
  229. $comment = 'Stores the type and number of features per organism';
  230. $schema = array(
  231. 'description' => $comment,
  232. 'table' => $view_name,
  233. 'fields' => array(
  234. 'organism_id' => array(
  235. 'type' => 'int',
  236. 'not null' => TRUE,
  237. ),
  238. 'genus' => array(
  239. 'type' => 'varchar',
  240. 'length' => '255',
  241. 'not null' => TRUE,
  242. ),
  243. 'species' => array(
  244. 'type' => 'varchar',
  245. 'length' => '255',
  246. 'not null' => TRUE,
  247. ),
  248. 'common_name' => array(
  249. 'type' => 'varchar',
  250. 'length' => '255',
  251. 'not null' => FALSE,
  252. ),
  253. 'num_features' => array(
  254. 'type' => 'int',
  255. 'not null' => TRUE,
  256. ),
  257. 'cvterm_id' => array(
  258. 'type' => 'int',
  259. 'not null' => TRUE,
  260. ),
  261. 'feature_type' => array(
  262. 'type' => 'varchar',
  263. 'length' => '255',
  264. 'not null' => TRUE,
  265. ),
  266. ),
  267. 'indexes' => array(
  268. 'organism_feature_count_idx1' => array('organism_id'),
  269. 'organism_feature_count_idx2' => array('cvterm_id'),
  270. 'organism_feature_count_idx3' => array('feature_type'),
  271. ),
  272. );
  273. $sql = "
  274. SELECT
  275. O.organism_id, O.genus, O.species, O.common_name,
  276. count(F.feature_id) as num_features,
  277. CVT.cvterm_id, CVT.name as feature_type
  278. FROM organism O
  279. INNER JOIN feature F ON O.Organism_id = F.organism_id
  280. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  281. GROUP BY
  282. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  283. ";
  284. tripal_add_mview($view_name, 'tripal_feature', $schema, $sql, $comment);
  285. }
  286. /**
  287. * Add cvs related to publications
  288. *
  289. * @ingroup tripal_pub
  290. */
  291. function tripal_feature_add_cvs() {
  292. // Add cv for relationship types
  293. tripal_insert_cv(
  294. 'feature_relationship',
  295. 'Contains types of relationships between features.'
  296. );
  297. // The feature_property CV may already exists. It comes with Chado, but
  298. // we need to add it just in case it doesn't get added before the feature
  299. // module is installed. But as of Tripal v3.0 the Chado version of this
  300. // vocabulary is no longer loaded by default.
  301. tripal_insert_cv(
  302. 'feature_property',
  303. 'Stores properties about features'
  304. );
  305. // the feature type vocabulary should be the sequence ontology, and even though
  306. // this ontology should get loaded we will create it here just so that we can
  307. // set the default vocabulary for the feature.type_id field
  308. tripal_insert_cv(
  309. 'sequence',
  310. 'The Sequence Ontology'
  311. );
  312. }