tripal_chado.install 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. function tripal_chado_install() {
  3. // The foreign key specification doesn't really add one to the
  4. // Drupal schema, it is just used internally, but we want one.
  5. db_query('
  6. ALTER TABLE {tripal_custom_tables}
  7. ADD CONSTRAINT tripal_custom_tables_fk1
  8. FOREIGN KEY (mview_id) REFERENCES {tripal_mviews} (mview_id)
  9. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  10. ');
  11. // Unfortunately, some Chado base tables do not have a type_id, so we must
  12. // take special action for those tables. These include: organism and
  13. // analysis. Until we can find an appropriate controlled vocabulary
  14. // that is well supported by the community with types for these tables we
  15. // will have to use in-house terms.
  16. // Add a term to be used for an inherent 'type_id' for the organism table.
  17. tripal_insert_cvterm(array(
  18. 'id' => 'local:organism',
  19. 'name' => 'organism',
  20. 'definition' => 'An individual form of life, such as a bacterium, protist, ' .
  21. 'fungus, plant, or animal, composed of a single cell or a complex of cells ' .
  22. 'in which organelles or organs work together to carry out the various ' .
  23. 'processes of life. (American Heritage® Dictionary of the English ' .
  24. 'Language, Fifth Edition. Copyright © 2011 by Houghton Mifflin ' .
  25. 'Harcourt Publishing Company).',
  26. 'cv_name' => 'local',
  27. ));
  28. // Add a term to be used for an inherent 'type_id' for the organism table.
  29. tripal_insert_cvterm(array(
  30. 'id' => 'local:analysis',
  31. 'name' => 'analysis',
  32. 'definition' => 'A process as a method of studying the nature of something ' .
  33. 'or of determining its essential features and their relations. ' .
  34. '(Random House Kernerman Webster\'s College Dictionary, © 2010 K ' .
  35. 'Dictionaries Ltd).',
  36. 'cv_name' => 'local',
  37. ));
  38. tripal_insert_cvterm(array(
  39. 'id' => 'local:project',
  40. 'name' => 'project',
  41. 'definition' => 'A plan or proposal for accomplishing something. ' .
  42. '(American Heritage® Dictionary of the English Language, Fifth Edition. ' .
  43. 'Copyright © 2011 by Houghton Mifflin Harcourt Publishing Company).',
  44. 'cv_name' => 'local',
  45. ));
  46. // For the TripalBundle entities we will want to associate the cvterm_id,
  47. // and the chado table and field that it maps to. We will use a few
  48. // variables to do this:
  49. tripal_insert_variable('chado_cvterm_id', 'The cvterm_id that a TripalBundle maps to.');
  50. tripal_insert_variable('chado_table', 'The name of the table to which a TripalBundle maps.');
  51. tripal_insert_variable('chado_column', 'The name of the column within the table that a TripalBundle maps to.');
  52. // We want to provide a set of commonly used entity types by default. This
  53. // way when a user first installs Tripal there are some commonly used
  54. // formats.
  55. module_load_include('inc', 'tripal_entities', 'api/tripal_entities.api');
  56. module_load_include('inc', 'tripal_entities', 'includes/tripal_entities.admin');
  57. // Create the 'Organism' entity type. This uses the local:organism term.
  58. $error = '';
  59. $term = array('name' => 'organism', 'cv_id' => array('name' => 'local'));
  60. $cvterm = chado_generate_var('cvterm', $term);
  61. if (!tripal_create_bundle('local', 'organism', 'organism', $error)) {
  62. throw new Exception($error);
  63. }
  64. // Create the 'Analysis' entity type. This uses the local:analysis term.
  65. $error = '';
  66. $term = array('name' => 'analysis', 'cv_id' => array('name' => 'local'));
  67. $cvterm = chado_generate_var('cvterm', $term);
  68. if (!tripal_create_bundle('local', 'analysis', 'analysis', $error)) {
  69. throw new Exception($error);
  70. }
  71. // Create the 'Project' entity type. This uses the local:project term.
  72. $error = '';
  73. $term = array('name' => 'project', 'cv_id' => array('name' => 'local'));
  74. $cvterm = chado_generate_var('cvterm', $term);
  75. if (!tripal_create_bundle('local', 'project', 'project', $error)) {
  76. throw new Exception($error);
  77. }
  78. }
  79. /**
  80. * Implementation of hook_uninstall().
  81. *
  82. * @ingroup tripal
  83. */
  84. function tripal_chado_uninstall() {
  85. // Drop the foreign key between tripal_custom_tables and tripal_mviews
  86. // so that Drupal can then drop the tables
  87. db_query('
  88. ALTER TABLE {tripal_custom_tables}
  89. DROP CONSTRAINT tripal_custom_tables_fk1 CASCADE
  90. ');
  91. }
  92. /**
  93. * Implements hook_schema().
  94. */
  95. function tripal_chado_schema() {
  96. // Links TripalEntity entities to the chado record.
  97. $schema['chado_entity'] = tripal_chado_chado_entity_schema();
  98. $schema['tripal_mviews'] = tripal_chado_tripal_mviews_schema();
  99. $schema['tripal_custom_tables'] = tripal_chado_tripal_custom_tables_schema();
  100. // if this module is already installed and enabled, then we want to provide
  101. // the schemas for all of the custom tables. This will allow Views to
  102. // see the schemas. We check if the module is installed because during
  103. // installation we don't want to make these custom tables available as we don't
  104. // want them created in the Drupal database. The custom tables go in the
  105. // Chado database.
  106. if (db_table_exists('tripal_custom_tables')) {
  107. $sql = 'SELECT * FROM {tripal_custom_tables}';
  108. $results = db_query($sql);
  109. foreach ($results as $custom) {
  110. $schema[$custom->table_name] = unserialize($custom->schema);
  111. }
  112. }
  113. return $schema;
  114. }
  115. /**
  116. * @section
  117. * Schema Definitions.
  118. */
  119. /**
  120. * Describes the Tripal Custom Tables (tripal_custom_tables) table
  121. * This keeps track of tables created by Tripal and stored in chado that may or may not
  122. * also be materialized views.
  123. *
  124. * @ingroup tripal
  125. */
  126. function tripal_chado_tripal_custom_tables_schema() {
  127. return array(
  128. 'fields' => array(
  129. 'table_id' => array(
  130. 'type' => 'serial',
  131. 'unsigned' => TRUE,
  132. 'not NULL' => TRUE
  133. ),
  134. 'table_name' => array(
  135. 'type' => 'varchar',
  136. 'length' => 255,
  137. 'not NULL' => TRUE
  138. ),
  139. 'schema' => array(
  140. 'type' => 'text',
  141. 'not NULL' => TRUE
  142. ),
  143. 'mview_id' => array(
  144. 'type' => 'int',
  145. 'not NULL' => FALSE
  146. )
  147. ),
  148. 'indexes' => array(
  149. 'table_id' => array('table_id'),
  150. ),
  151. 'primary key' => array('table_id'),
  152. 'foreign keys' => array(
  153. 'tripal_mviews' => array(
  154. 'table' => 'tripal_mviews',
  155. 'columns' => array(
  156. 'mview_id' => 'mview_id'
  157. ),
  158. ),
  159. ),
  160. );
  161. }
  162. /**
  163. * Describes the Tripal Materialized View (tripal_mviews) table
  164. * This table keeps track of all materialized views created by Tripal and stored in chado
  165. *
  166. * @ingroup tripal
  167. */
  168. function tripal_chado_tripal_mviews_schema() {
  169. return array(
  170. 'fields' => array(
  171. 'mview_id' => array(
  172. 'type' => 'serial',
  173. 'unsigned' => TRUE,
  174. 'not NULL' => TRUE
  175. ),
  176. 'name' => array(
  177. 'type' => 'varchar',
  178. 'length' => 255,
  179. 'not NULL' => TRUE
  180. ),
  181. 'modulename' => array(
  182. 'type' => 'varchar',
  183. 'length' => 50,
  184. 'not NULL' => TRUE,
  185. 'description' => 'The module name that provides the callback for this job'
  186. ),
  187. 'mv_table' => array(
  188. 'type' => 'varchar',
  189. 'length' => 128,
  190. 'not NULL' => FALSE
  191. ),
  192. 'mv_specs' => array(
  193. 'type' => 'text',
  194. 'size' => 'normal',
  195. 'not NULL' => FALSE
  196. ),
  197. 'mv_schema' => array(
  198. 'type' => 'text',
  199. 'size' => 'normal',
  200. 'not NULL' => FALSE
  201. ),
  202. 'indexed' => array(
  203. 'type' => 'text',
  204. 'size' => 'normal',
  205. 'not NULL' => FALSE
  206. ),
  207. 'query' => array(
  208. 'type' => 'text',
  209. 'size' => 'normal',
  210. 'not NULL' => TRUE
  211. ),
  212. 'special_index' => array(
  213. 'type' => 'text',
  214. 'size' => 'normal',
  215. 'not NULL' => FALSE
  216. ),
  217. 'last_update' => array(
  218. 'type' => 'int',
  219. 'not NULL' => FALSE,
  220. 'description' => 'UNIX integer time'
  221. ),
  222. 'status' => array(
  223. 'type' => 'text',
  224. 'size' => 'normal',
  225. 'not NULL' => FALSE
  226. ),
  227. 'comment' => array(
  228. 'type' => 'text',
  229. 'size' => 'normal',
  230. 'not NULL' => FALSE
  231. ),
  232. ),
  233. 'indexes' => array(
  234. 'mview_id' => array('mview_id')
  235. ),
  236. 'unique keys' => array(
  237. 'mv_table' => array('mv_table'),
  238. 'mv_name' => array('name'),
  239. ),
  240. 'primary key' => array('mview_id'),
  241. );
  242. }
  243. /**
  244. * Links Biological Data Entities to the chado "base" table the data is stored in.
  245. * This is where we would specify that a particular gene maps to the record in the
  246. * chado.feature table with a feature_id=2432;
  247. */
  248. function tripal_chado_chado_entity_schema() {
  249. $schema = array(
  250. 'description' => 'The linker table that associates an enitity from the public.tripal_entity table with a "base" record in Chado',
  251. 'fields' => array(
  252. 'chado_entity_id' => array(
  253. 'description' => 'The primary identifier for this table.',
  254. 'type' => 'serial',
  255. 'unsigned' => TRUE,
  256. 'not null' => TRUE,
  257. ),
  258. 'entity_id' => array(
  259. 'description' => 'The unique entity id.',
  260. 'type' => 'int',
  261. 'not null' => TRUE,
  262. ),
  263. 'record_id' => array(
  264. 'description' => 'The unique numerical identifier for the record that this entity is associated with (e.g. feature_id, stock_id, library_id, etc.).',
  265. 'type' => 'int',
  266. 'not null' => TRUE,
  267. ),
  268. 'data_table' => array(
  269. 'description' => 'Indicates the table in Chado that this term services (e.g. feature, stock, library, etc.)',
  270. 'type' => 'varchar',
  271. 'length' => 128,
  272. 'not null' => TRUE,
  273. 'default' => '',
  274. ),
  275. 'type_table' => array(
  276. 'description' => 'Sometimes the record in the data table doesn’t have a field that specifies the record type. For example, an analysis type is stored in the analysisprop table. If the data_table does have a type field then this value will be the same as the data_table.',
  277. 'type' => 'varchar',
  278. 'length' => 128,
  279. 'not null' => TRUE,
  280. 'default' => '',
  281. ),
  282. 'field' => array(
  283. 'description' => 'The name of the field in the typetable that contains the cvterm record.',
  284. 'type' => 'varchar',
  285. 'length' => 128,
  286. 'not null' => FALSE,
  287. 'default' => ''
  288. ),
  289. ),
  290. 'indexes' => array(
  291. 'record_id' => array('record_id'),
  292. 'entity_id' => array('entity_id'),
  293. 'data_table' => array('data_table'),
  294. ),
  295. 'unique keys' => array(
  296. 'record' => array('data_table', 'record_id'),
  297. 'entity_id' => array('entity_id'),
  298. ),
  299. 'primary key' => array('chado_entity_id'),
  300. );
  301. return $schema;
  302. }