tripal_chado.install 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. // Add the cv_root_mview.
  12. tripal_cv_add_cv_root_mview();
  13. // Add defaults to the tables that correlate OBO files/references with
  14. // a chado CV.
  15. tripal_cv_add_obo_defaults();
  16. // Add the Chado ontology CV.
  17. $obo_path = '{tripal_chado}/files/cv_property.obo';
  18. $obo_id = tripal_insert_obo('Chado CV Properties', $obo_path);
  19. tripal_submit_obo_job(array('obo_id' => $obo_id));
  20. // Create the temp table we will use for loading OBO files.
  21. tripal_cv_create_tripal_obo_temp();
  22. // Unfortunately, some Chado base tables do not have a type_id, so we must
  23. // take special action for those tables. These include: organism and
  24. // analysis. Until we can find an appropriate controlled vocabulary
  25. // that is well supported by the community with types for these tables we
  26. // will have to use in-house terms.
  27. // Add a term to be used for an inherent 'type_id' for the organism table.
  28. tripal_insert_cvterm(array(
  29. 'id' => 'local:organism',
  30. 'name' => 'organism',
  31. 'definition' => 'An individual form of life, such as a bacterium, protist, ' .
  32. 'fungus, plant, or animal, composed of a single cell or a complex of cells ' .
  33. 'in which organelles or organs work together to carry out the various ' .
  34. 'processes of life. (American Heritage® Dictionary of the English ' .
  35. 'Language, Fifth Edition. Copyright © 2011 by Houghton Mifflin ' .
  36. 'Harcourt Publishing Company).',
  37. 'cv_name' => 'local',
  38. ));
  39. // Add a term to be used for an inherent 'type_id' for the organism table.
  40. tripal_insert_cvterm(array(
  41. 'id' => 'local:analysis',
  42. 'name' => 'analysis',
  43. 'definition' => 'A process as a method of studying the nature of something ' .
  44. 'or of determining its essential features and their relations. ' .
  45. '(Random House Kernerman Webster\'s College Dictionary, © 2010 K ' .
  46. 'Dictionaries Ltd).',
  47. 'cv_name' => 'local',
  48. ));
  49. tripal_insert_cvterm(array(
  50. 'id' => 'local:project',
  51. 'name' => 'project',
  52. 'definition' => 'A plan or proposal for accomplishing something. ' .
  53. '(American Heritage® Dictionary of the English Language, Fifth Edition. ' .
  54. 'Copyright © 2011 by Houghton Mifflin Harcourt Publishing Company).',
  55. 'cv_name' => 'local',
  56. ));
  57. // For the TripalBundle entities we will want to associate the cvterm_id,
  58. // and the chado table and field that it maps to. We will use a few
  59. // variables to do this:
  60. tripal_insert_variable('chado_cvterm_id', 'The cvterm_id that a TripalBundle maps to.');
  61. tripal_insert_variable('chado_table', 'The name of the table to which a TripalBundle maps.');
  62. tripal_insert_variable('chado_column', 'The name of the column within the table that a TripalBundle maps to.');
  63. // We want to provide a set of commonly used entity types by default. This
  64. // way when a user first installs Tripal there are some commonly used
  65. // formats.
  66. module_load_include('inc', 'tripal', 'api/tripal.api');
  67. module_load_include('inc', 'tripal', 'includes/tripal.admin');
  68. // Create the 'Organism' entity type. This uses the local:organism term.
  69. $error = '';
  70. $term = array('name' => 'organism', 'cv_id' => array('name' => 'local'));
  71. $cvterm = chado_generate_var('cvterm', $term);
  72. if (!tripal_create_bundle('local', 'organism', 'organism', $error)) {
  73. throw new Exception($error);
  74. }
  75. // Create the 'Analysis' entity type. This uses the local:analysis term.
  76. $error = '';
  77. $term = array('name' => 'analysis', 'cv_id' => array('name' => 'local'));
  78. $cvterm = chado_generate_var('cvterm', $term);
  79. if (!tripal_create_bundle('local', 'analysis', 'analysis', $error)) {
  80. throw new Exception($error);
  81. }
  82. // Create the 'Project' entity type. This uses the local:project term.
  83. $error = '';
  84. $term = array('name' => 'project', 'cv_id' => array('name' => 'local'));
  85. $cvterm = chado_generate_var('cvterm', $term);
  86. if (!tripal_create_bundle('local', 'project', 'project', $error)) {
  87. throw new Exception($error);
  88. }
  89. }
  90. /**
  91. * Implementation of hook_uninstall().
  92. *
  93. * @ingroup tripal
  94. */
  95. function tripal_chado_uninstall() {
  96. // Drop the foreign key between tripal_custom_tables and tripal_mviews
  97. // so that Drupal can then drop the tables
  98. db_query('
  99. ALTER TABLE {tripal_custom_tables}
  100. DROP CONSTRAINT tripal_custom_tables_fk1 CASCADE
  101. ');
  102. }
  103. /**
  104. * Table definition for the tripal_cv_obo table
  105. * @param $schema
  106. */
  107. function tripal_chado_tripal_cv_obo_schema(&$schema) {
  108. return array(
  109. 'fields' => array(
  110. 'obo_id' => array(
  111. 'type' => 'serial',
  112. 'unsigned' => TRUE,
  113. 'not null' => TRUE
  114. ),
  115. 'name' => array(
  116. 'type' => 'varchar',
  117. 'length' => 255
  118. ),
  119. 'path' => array(
  120. 'type' => 'varchar',
  121. 'length' => 1024
  122. ),
  123. ),
  124. 'indexes' => array(
  125. 'tripal_cv_obo_idx1' => array('obo_id'),
  126. ),
  127. 'primary key' => array('obo_id'),
  128. );
  129. }
  130. /**
  131. * * Table definition for the tripal_cv_defaults table
  132. * @param unknown $schema
  133. */
  134. function tripal_chado_tripal_cv_defaults_schema(&$schema) {
  135. return array(
  136. 'fields' => array(
  137. 'cv_default_id' => array(
  138. 'type' => 'serial',
  139. 'unsigned' => TRUE,
  140. 'not null' => TRUE
  141. ),
  142. 'table_name' => array(
  143. 'type' => 'varchar',
  144. 'length' => 128,
  145. 'not null' => TRUE,
  146. ),
  147. 'field_name' => array(
  148. 'type' => 'varchar',
  149. 'length' => 128,
  150. 'not null' => TRUE,
  151. ),
  152. 'cv_id' => array(
  153. 'type' => 'int',
  154. 'not null' => TRUE,
  155. )
  156. ),
  157. 'indexes' => array(
  158. 'tripal_cv_defaults_idx1' => array('table_name', 'field_name'),
  159. ),
  160. 'unique keys' => array(
  161. 'tripal_cv_defaults_unq1' => array('table_name', 'field_name', 'cv_id'),
  162. ),
  163. 'primary key' => array('cv_default_id')
  164. );
  165. }
  166. /**
  167. * Add a materialized view of root terms for all chado cvs. This is needed for viewing cv trees
  168. *
  169. * @ingroup tripal_cv
  170. */
  171. function tripal_cv_add_cv_root_mview() {
  172. $mv_name = 'cv_root_mview';
  173. $comment = 'A list of the root terms for all controlled vocabularies. This is needed for viewing CV trees';
  174. $schema = array(
  175. 'table' => $mv_name,
  176. 'description' => $comment,
  177. 'fields' => array(
  178. 'name' => array(
  179. 'type' => 'varchar',
  180. 'length' => 255,
  181. 'not null' => TRUE,
  182. ),
  183. 'cvterm_id' => array(
  184. 'type' => 'int',
  185. 'not null' => TRUE,
  186. ),
  187. 'cv_id' => array(
  188. 'type' => 'int',
  189. 'not null' => TRUE,
  190. ),
  191. 'cv_name' => array(
  192. 'type' => 'varchar',
  193. 'length' => 255,
  194. 'not null' => TRUE,
  195. ),
  196. ),
  197. 'indexes' => array(
  198. 'cv_root_mview_indx1' => array('cvterm_id'),
  199. 'cv_root_mview_indx2' => array('cv_id'),
  200. ),
  201. );
  202. $sql = "
  203. SELECT DISTINCT CVT.name,CVT.cvterm_id, CV.cv_id, CV.name
  204. FROM cvterm_relationship CVTR
  205. INNER JOIN cvterm CVT on CVTR.object_id = CVT.cvterm_id
  206. INNER JOIN cv CV on CV.cv_id = CVT.cv_id
  207. WHERE CVTR.object_id not in
  208. (SELECT subject_id FROM cvterm_relationship)
  209. ";
  210. // Create the MView
  211. tripal_add_mview($mv_name, 'tripal_cv', $schema, $sql, $comment);
  212. }
  213. /**
  214. * Add's defaults to the tripal_cv_obo table
  215. *
  216. * @ingroup tripal_cv
  217. */
  218. function tripal_cv_add_obo_defaults() {
  219. // Insert commonly used ontologies into the tables.
  220. $ontologies = array(
  221. array('Relationship Ontology', 'http://purl.obolibrary.org/obo/ro.obo'),
  222. // array('Relationship Ontology (older deprecated version)', 'http://www.obofoundry.org/ro/ro.obo'),
  223. array('Sequence Ontology', 'https://github.com/The-Sequence-Ontology/SO-Ontologies/blob/master/so-xp-simple.obo'),
  224. array('Gene Ontology', 'http://www.geneontology.org/ontology/gene_ontology.obo'),
  225. // array('Cell Ontology', 'https://raw.githubusercontent.com/obophenotype/cell-ontology/master/cl.obo'),
  226. // array('Plant Structure Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_anatomy.obo?view=co'),
  227. // array('Plant Growth and Development Stages Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_temporal.obo?view=co')
  228. );
  229. foreach ($ontologies as $o) {
  230. db_query("INSERT INTO {tripal_cv_obo} (name,path) VALUES (:name, :path)", array(':name' => $o[0], ':path' => $o[1]));
  231. }
  232. }
  233. /**
  234. * Implements hook_schema().
  235. */
  236. function tripal_chado_schema() {
  237. // Links TripalEntity entities to the chado record.
  238. $schema['chado_entity'] = tripal_chado_chado_entity_schema();
  239. $schema['tripal_mviews'] = tripal_chado_tripal_mviews_schema();
  240. $schema['tripal_custom_tables'] = tripal_chado_tripal_custom_tables_schema();
  241. $schema['tripal_cv_obo'] = tripal_chado_tripal_cv_obo_schema($schema);
  242. $schema['tripal_cv_defaults'] = tripal_chado_tripal_cv_defaults_schema($schema);
  243. // if this module is already installed and enabled, then we want to provide
  244. // the schemas for all of the custom tables. This will allow Views to
  245. // see the schemas. We check if the module is installed because during
  246. // installation we don't want to make these custom tables available as we don't
  247. // want them created in the Drupal database. The custom tables go in the
  248. // Chado database.
  249. if (db_table_exists('tripal_custom_tables')) {
  250. $sql = 'SELECT * FROM {tripal_custom_tables}';
  251. $results = db_query($sql);
  252. foreach ($results as $custom) {
  253. $schema[$custom->table_name] = unserialize($custom->schema);
  254. }
  255. }
  256. return $schema;
  257. }
  258. /**
  259. * @section
  260. * Schema Definitions.
  261. */
  262. /**
  263. * Describes the Tripal Custom Tables (tripal_custom_tables) table
  264. * This keeps track of tables created by Tripal and stored in chado that may or may not
  265. * also be materialized views.
  266. *
  267. * @ingroup tripal
  268. */
  269. function tripal_chado_tripal_custom_tables_schema() {
  270. return array(
  271. 'fields' => array(
  272. 'table_id' => array(
  273. 'type' => 'serial',
  274. 'unsigned' => TRUE,
  275. 'not NULL' => TRUE
  276. ),
  277. 'table_name' => array(
  278. 'type' => 'varchar',
  279. 'length' => 255,
  280. 'not NULL' => TRUE
  281. ),
  282. 'schema' => array(
  283. 'type' => 'text',
  284. 'not NULL' => TRUE
  285. ),
  286. 'mview_id' => array(
  287. 'type' => 'int',
  288. 'not NULL' => FALSE
  289. )
  290. ),
  291. 'indexes' => array(
  292. 'table_id' => array('table_id'),
  293. ),
  294. 'primary key' => array('table_id'),
  295. 'foreign keys' => array(
  296. 'tripal_mviews' => array(
  297. 'table' => 'tripal_mviews',
  298. 'columns' => array(
  299. 'mview_id' => 'mview_id'
  300. ),
  301. ),
  302. ),
  303. );
  304. }
  305. /**
  306. * Describes the Tripal Materialized View (tripal_mviews) table
  307. * This table keeps track of all materialized views created by Tripal and stored in chado
  308. *
  309. * @ingroup tripal
  310. */
  311. function tripal_chado_tripal_mviews_schema() {
  312. return array(
  313. 'fields' => array(
  314. 'mview_id' => array(
  315. 'type' => 'serial',
  316. 'unsigned' => TRUE,
  317. 'not NULL' => TRUE
  318. ),
  319. 'name' => array(
  320. 'type' => 'varchar',
  321. 'length' => 255,
  322. 'not NULL' => TRUE
  323. ),
  324. 'modulename' => array(
  325. 'type' => 'varchar',
  326. 'length' => 50,
  327. 'not NULL' => TRUE,
  328. 'description' => 'The module name that provides the callback for this job'
  329. ),
  330. 'mv_table' => array(
  331. 'type' => 'varchar',
  332. 'length' => 128,
  333. 'not NULL' => FALSE
  334. ),
  335. 'mv_specs' => array(
  336. 'type' => 'text',
  337. 'size' => 'normal',
  338. 'not NULL' => FALSE
  339. ),
  340. 'mv_schema' => array(
  341. 'type' => 'text',
  342. 'size' => 'normal',
  343. 'not NULL' => FALSE
  344. ),
  345. 'indexed' => array(
  346. 'type' => 'text',
  347. 'size' => 'normal',
  348. 'not NULL' => FALSE
  349. ),
  350. 'query' => array(
  351. 'type' => 'text',
  352. 'size' => 'normal',
  353. 'not NULL' => TRUE
  354. ),
  355. 'special_index' => array(
  356. 'type' => 'text',
  357. 'size' => 'normal',
  358. 'not NULL' => FALSE
  359. ),
  360. 'last_update' => array(
  361. 'type' => 'int',
  362. 'not NULL' => FALSE,
  363. 'description' => 'UNIX integer time'
  364. ),
  365. 'status' => array(
  366. 'type' => 'text',
  367. 'size' => 'normal',
  368. 'not NULL' => FALSE
  369. ),
  370. 'comment' => array(
  371. 'type' => 'text',
  372. 'size' => 'normal',
  373. 'not NULL' => FALSE
  374. ),
  375. ),
  376. 'indexes' => array(
  377. 'mview_id' => array('mview_id')
  378. ),
  379. 'unique keys' => array(
  380. 'mv_table' => array('mv_table'),
  381. 'mv_name' => array('name'),
  382. ),
  383. 'primary key' => array('mview_id'),
  384. );
  385. }
  386. /**
  387. * Links Biological Data Entities to the chado "base" table the data is stored in.
  388. * This is where we would specify that a particular gene maps to the record in the
  389. * chado.feature table with a feature_id=2432;
  390. */
  391. function tripal_chado_chado_entity_schema() {
  392. $schema = array(
  393. 'description' => 'The linker table that associates an enitity from the public.tripal_entity table with a "base" record in Chado',
  394. 'fields' => array(
  395. 'chado_entity_id' => array(
  396. 'description' => 'The primary identifier for this table.',
  397. 'type' => 'serial',
  398. 'unsigned' => TRUE,
  399. 'not null' => TRUE,
  400. ),
  401. 'entity_id' => array(
  402. 'description' => 'The unique entity id.',
  403. 'type' => 'int',
  404. 'not null' => TRUE,
  405. ),
  406. 'record_id' => array(
  407. 'description' => 'The unique numerical identifier for the record that this entity is associated with (e.g. feature_id, stock_id, library_id, etc.).',
  408. 'type' => 'int',
  409. 'not null' => TRUE,
  410. ),
  411. 'data_table' => array(
  412. 'description' => 'Indicates the table in Chado that this term services (e.g. feature, stock, library, etc.)',
  413. 'type' => 'varchar',
  414. 'length' => 128,
  415. 'not null' => TRUE,
  416. 'default' => '',
  417. ),
  418. 'type_table' => array(
  419. '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.',
  420. 'type' => 'varchar',
  421. 'length' => 128,
  422. 'not null' => TRUE,
  423. 'default' => '',
  424. ),
  425. 'field' => array(
  426. 'description' => 'The name of the field in the typetable that contains the cvterm record.',
  427. 'type' => 'varchar',
  428. 'length' => 128,
  429. 'not null' => FALSE,
  430. 'default' => ''
  431. ),
  432. ),
  433. 'indexes' => array(
  434. 'record_id' => array('record_id'),
  435. 'entity_id' => array('entity_id'),
  436. 'data_table' => array('data_table'),
  437. ),
  438. 'unique keys' => array(
  439. 'record' => array('data_table', 'record_id'),
  440. 'entity_id' => array('entity_id'),
  441. ),
  442. 'primary key' => array('chado_entity_id'),
  443. );
  444. return $schema;
  445. }