tripal_chado.install 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. }
  12. /**
  13. * Implementation of hook_uninstall().
  14. *
  15. * @ingroup tripal
  16. */
  17. function tripal_chado_uninstall() {
  18. // Drop the foreign key between tripal_custom_tables and tripal_mviews
  19. // so that Drupal can then drop the tables
  20. db_query('
  21. ALTER TABLE {tripal_custom_tables}
  22. DROP CONSTRAINT tripal_custom_tables_fk1 CASCADE
  23. ');
  24. }
  25. /**
  26. * Table definition for the tripal_cv_obo table
  27. * @param $schema
  28. */
  29. function tripal_chado_tripal_cv_obo_schema() {
  30. return array(
  31. 'fields' => array(
  32. 'obo_id' => array(
  33. 'type' => 'serial',
  34. 'unsigned' => TRUE,
  35. 'not null' => TRUE
  36. ),
  37. 'name' => array(
  38. 'type' => 'varchar',
  39. 'length' => 255
  40. ),
  41. 'path' => array(
  42. 'type' => 'varchar',
  43. 'length' => 1024
  44. ),
  45. ),
  46. 'indexes' => array(
  47. 'tripal_cv_obo_idx1' => array('obo_id'),
  48. ),
  49. 'primary key' => array('obo_id'),
  50. );
  51. }
  52. /**
  53. * * Table definition for the tripal_cv_defaults table
  54. * @param unknown $schema
  55. */
  56. function tripal_chado_tripal_cv_defaults_schema() {
  57. return array(
  58. 'fields' => array(
  59. 'cv_default_id' => array(
  60. 'type' => 'serial',
  61. 'unsigned' => TRUE,
  62. 'not null' => TRUE
  63. ),
  64. 'table_name' => array(
  65. 'type' => 'varchar',
  66. 'length' => 128,
  67. 'not null' => TRUE,
  68. ),
  69. 'field_name' => array(
  70. 'type' => 'varchar',
  71. 'length' => 128,
  72. 'not null' => TRUE,
  73. ),
  74. 'cv_id' => array(
  75. 'type' => 'int',
  76. 'not null' => TRUE,
  77. )
  78. ),
  79. 'indexes' => array(
  80. 'tripal_cv_defaults_idx1' => array('table_name', 'field_name'),
  81. ),
  82. 'unique keys' => array(
  83. 'tripal_cv_defaults_unq1' => array('table_name', 'field_name', 'cv_id'),
  84. ),
  85. 'primary key' => array('cv_default_id')
  86. );
  87. }
  88. /**
  89. * Add's defaults to the tripal_cv_obo table
  90. *
  91. * @ingroup tripal_cv
  92. */
  93. function tripal_cv_add_obo_defaults() {
  94. // Insert commonly used ontologies into the tables.
  95. $ontologies = array(
  96. array('Relationship Ontology', 'http://purl.obolibrary.org/obo/ro.obo'),
  97. // array('Relationship Ontology (older deprecated version)', 'http://www.obofoundry.org/ro/ro.obo'),
  98. array('Sequence Ontology', 'https://github.com/The-Sequence-Ontology/SO-Ontologies/blob/master/so-xp-simple.obo'),
  99. array('Gene Ontology', 'http://www.geneontology.org/ontology/gene_ontology.obo'),
  100. // array('Cell Ontology', 'https://raw.githubusercontent.com/obophenotype/cell-ontology/master/cl.obo'),
  101. // array('Plant Structure Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_anatomy.obo?view=co'),
  102. // array('Plant Growth and Development Stages Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_temporal.obo?view=co')
  103. );
  104. foreach ($ontologies as $o) {
  105. db_query("INSERT INTO {tripal_cv_obo} (name,path) VALUES (:name, :path)", array(':name' => $o[0], ':path' => $o[1]));
  106. }
  107. }
  108. /**
  109. * Implements hook_schema().
  110. */
  111. function tripal_chado_schema() {
  112. // Links TripalEntity entities to the chado record.
  113. $schema['chado_entity'] = tripal_chado_chado_entity_schema();
  114. $schema['tripal_mviews'] = tripal_chado_tripal_mviews_schema();
  115. $schema['tripal_custom_tables'] = tripal_chado_tripal_custom_tables_schema();
  116. $schema['tripal_cv_obo'] = tripal_chado_tripal_cv_obo_schema();
  117. $schema['tripal_cv_defaults'] = tripal_chado_tripal_cv_defaults_schema();
  118. $schema['tripal_pub_import'] = tripal_chado_tripal_pub_import_schema();
  119. // if this module is already installed and enabled, then we want to provide
  120. // the schemas for all of the custom tables. This will allow Views to
  121. // see the schemas. We check if the module is installed because during
  122. // installation we don't want to make these custom tables available as we don't
  123. // want them created in the Drupal database. The custom tables go in the
  124. // Chado database.
  125. if (db_table_exists('tripal_custom_tables')) {
  126. $sql = 'SELECT * FROM {tripal_custom_tables}';
  127. $results = db_query($sql);
  128. foreach ($results as $custom) {
  129. $schema[$custom->table_name] = unserialize($custom->schema);
  130. }
  131. }
  132. return $schema;
  133. }
  134. /**
  135. * @section
  136. * Schema Definitions.
  137. */
  138. /**
  139. * Describes the Tripal Custom Tables (tripal_custom_tables) table
  140. * This keeps track of tables created by Tripal and stored in chado that may or may not
  141. * also be materialized views.
  142. *
  143. * @ingroup tripal
  144. */
  145. function tripal_chado_tripal_custom_tables_schema() {
  146. return array(
  147. 'fields' => array(
  148. 'table_id' => array(
  149. 'type' => 'serial',
  150. 'unsigned' => TRUE,
  151. 'not NULL' => TRUE
  152. ),
  153. 'table_name' => array(
  154. 'type' => 'varchar',
  155. 'length' => 255,
  156. 'not NULL' => TRUE
  157. ),
  158. 'schema' => array(
  159. 'type' => 'text',
  160. 'not NULL' => TRUE
  161. ),
  162. 'mview_id' => array(
  163. 'type' => 'int',
  164. 'not NULL' => FALSE
  165. )
  166. ),
  167. 'indexes' => array(
  168. 'table_id' => array('table_id'),
  169. ),
  170. 'primary key' => array('table_id'),
  171. 'foreign keys' => array(
  172. 'tripal_mviews' => array(
  173. 'table' => 'tripal_mviews',
  174. 'columns' => array(
  175. 'mview_id' => 'mview_id'
  176. ),
  177. ),
  178. ),
  179. );
  180. }
  181. /**
  182. * Describes the Tripal Materialized View (tripal_mviews) table
  183. * This table keeps track of all materialized views created by Tripal and stored in chado
  184. *
  185. * @ingroup tripal
  186. */
  187. function tripal_chado_tripal_mviews_schema() {
  188. return array(
  189. 'fields' => array(
  190. 'mview_id' => array(
  191. 'type' => 'serial',
  192. 'unsigned' => TRUE,
  193. 'not NULL' => TRUE
  194. ),
  195. 'name' => array(
  196. 'type' => 'varchar',
  197. 'length' => 255,
  198. 'not NULL' => TRUE
  199. ),
  200. 'modulename' => array(
  201. 'type' => 'varchar',
  202. 'length' => 50,
  203. 'not NULL' => TRUE,
  204. 'description' => 'The module name that provides the callback for this job'
  205. ),
  206. 'mv_table' => array(
  207. 'type' => 'varchar',
  208. 'length' => 128,
  209. 'not NULL' => FALSE
  210. ),
  211. 'mv_specs' => array(
  212. 'type' => 'text',
  213. 'size' => 'normal',
  214. 'not NULL' => FALSE
  215. ),
  216. 'mv_schema' => array(
  217. 'type' => 'text',
  218. 'size' => 'normal',
  219. 'not NULL' => FALSE
  220. ),
  221. 'indexed' => array(
  222. 'type' => 'text',
  223. 'size' => 'normal',
  224. 'not NULL' => FALSE
  225. ),
  226. 'query' => array(
  227. 'type' => 'text',
  228. 'size' => 'normal',
  229. 'not NULL' => TRUE
  230. ),
  231. 'special_index' => array(
  232. 'type' => 'text',
  233. 'size' => 'normal',
  234. 'not NULL' => FALSE
  235. ),
  236. 'last_update' => array(
  237. 'type' => 'int',
  238. 'not NULL' => FALSE,
  239. 'description' => 'UNIX integer time'
  240. ),
  241. 'status' => array(
  242. 'type' => 'text',
  243. 'size' => 'normal',
  244. 'not NULL' => FALSE
  245. ),
  246. 'comment' => array(
  247. 'type' => 'text',
  248. 'size' => 'normal',
  249. 'not NULL' => FALSE
  250. ),
  251. ),
  252. 'indexes' => array(
  253. 'mview_id' => array('mview_id')
  254. ),
  255. 'unique keys' => array(
  256. 'mv_table' => array('mv_table'),
  257. 'mv_name' => array('name'),
  258. ),
  259. 'primary key' => array('mview_id'),
  260. );
  261. }
  262. /**
  263. * Links Biological Data Entities to the chado "base" table the data is stored in.
  264. * This is where we would specify that a particular gene maps to the record in the
  265. * chado.feature table with a feature_id=2432;
  266. */
  267. function tripal_chado_chado_entity_schema() {
  268. $schema = array(
  269. 'description' => 'The linker table that associates an enitity from the public.tripal_entity table with a "base" record in Chado',
  270. 'fields' => array(
  271. 'chado_entity_id' => array(
  272. 'description' => 'The primary identifier for this table.',
  273. 'type' => 'serial',
  274. 'unsigned' => TRUE,
  275. 'not null' => TRUE,
  276. ),
  277. 'entity_id' => array(
  278. 'description' => 'The unique entity id.',
  279. 'type' => 'int',
  280. 'not null' => TRUE,
  281. ),
  282. 'record_id' => array(
  283. 'description' => 'The unique numerical identifier for the record that this entity is associated with (e.g. feature_id, stock_id, library_id, etc.).',
  284. 'type' => 'int',
  285. 'not null' => TRUE,
  286. ),
  287. 'data_table' => array(
  288. 'description' => 'Indicates the table in Chado that this term services (e.g. feature, stock, library, etc.)',
  289. 'type' => 'varchar',
  290. 'length' => 128,
  291. 'not null' => TRUE,
  292. 'default' => '',
  293. ),
  294. 'type_table' => array(
  295. '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.',
  296. 'type' => 'varchar',
  297. 'length' => 128,
  298. 'not null' => TRUE,
  299. 'default' => '',
  300. ),
  301. 'field' => array(
  302. 'description' => 'The name of the field in the typetable that contains the cvterm record.',
  303. 'type' => 'varchar',
  304. 'length' => 128,
  305. 'not null' => FALSE,
  306. 'default' => ''
  307. ),
  308. ),
  309. 'indexes' => array(
  310. 'record_id' => array('record_id'),
  311. 'entity_id' => array('entity_id'),
  312. 'data_table' => array('data_table'),
  313. ),
  314. 'unique keys' => array(
  315. 'record' => array('data_table', 'record_id'),
  316. 'entity_id' => array('entity_id'),
  317. ),
  318. 'primary key' => array('chado_entity_id'),
  319. );
  320. return $schema;
  321. }