tripal_chado.setup.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <?php
  2. /**
  3. * @file
  4. * Functions to install chado schema through Drupal
  5. */
  6. /**
  7. * Prepares Chado for Tripal use
  8. */
  9. function tripal_chado_prepare_form($form, $form_state) {
  10. $form = array();
  11. if (variable_get('tripal_chado_is_prepared') == TRUE) {
  12. drupal_set_message('Your site is prepared.');
  13. }
  14. $form['instructions'] = array(
  15. '#type' => 'item',
  16. '#title' => 'Prepare Drupal for Chado.',
  17. '#description' => t("Before a Drupal site can use Chado (via Tripal), both
  18. Chado and Drupal must be prepared a bit more. Tripal will add some new
  19. materialized views, custom tables and controlled vocabularies to Chado.
  20. It will also add some management tables to Drupal. You only are
  21. required to prepare your Drupal site if this is a brand-new Drupal
  22. installation or if Chado was installed outside of Tripal. If you
  23. installed Chado using Tripal then you do not need to run this step.
  24. If you are upgrading from a previous version of Tripal, you do not
  25. need to prepare your site, and you can click the 'Skip' button."),
  26. );
  27. $form['prepare-button'] = array(
  28. '#type' => 'submit',
  29. '#value' => t('Prepare this site'),
  30. '#name' => 'prepare-chado',
  31. );
  32. $form['skip-button'] = array(
  33. '#type' => 'submit',
  34. '#value' => t('Skip'),
  35. '#name' => 'prepare-skip',
  36. );
  37. return $form;
  38. }
  39. /**
  40. * Submit function for the tripal_chado_prepare_form().
  41. *
  42. * @param $form
  43. * @param $form_state
  44. */
  45. function tripal_chado_prepare_form_submit($form, $form_state) {
  46. if ($form_state['clicked_button']['#name'] == "prepare-chado") {
  47. global $user;
  48. $args = array();
  49. $includes = array(
  50. module_load_include('inc', 'tripal_chado', 'includes/setup/tripal_chado.setup'),
  51. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader'),
  52. );
  53. tripal_add_job('Prepare Chado', 'tripal_chado',
  54. 'tripal_chado_prepare_chado', $args,
  55. $user->uid, 10, $includes);
  56. }
  57. if ($form_state['clicked_button']['#name'] == "prepare-skip") {
  58. variable_set('tripal_chado_is_prepared', TRUE);
  59. }
  60. }
  61. /**
  62. *
  63. */
  64. function tripal_chado_load_ontologies() {
  65. // Insert commonly used ontologies into the tables.
  66. $ontologies = array(
  67. array(
  68. 'name' => 'Relationship Ontology (legacy)',
  69. 'path' => '{tripal_chado}/files/legacy_ro.obo',
  70. 'auto_load' => FALSE,
  71. 'cv_name' => 'ro'
  72. ),
  73. array(
  74. 'name' => 'Gene Ontology',
  75. 'path' => 'http://purl.obolibrary.org/obo/go.obo',
  76. 'auto_load' => FALSE,
  77. 'cv_name' => 'cellualar_component'
  78. ),
  79. array(
  80. 'name' => 'Taxonomic Rank',
  81. 'path' => 'http://purl.obolibrary.org/obo/taxrank.obo',
  82. 'auto_load' => TRUE,
  83. 'cv_name' => 'taxonomic_rank'
  84. ),
  85. array(
  86. 'name' => 'Tripal Contact',
  87. 'path' => '{tripal_chado}/files/tcontact.obo',
  88. 'auto_load' => TRUE,
  89. 'cv_name' => 'tripal_contact'
  90. ),
  91. array(
  92. 'name' => 'Tripal Publication',
  93. 'path' => '{tripal_chado}/files/tpub.obo',
  94. 'auto_load' => TRUE,
  95. 'cv_name' => 'tripal_pub'
  96. ),
  97. array(
  98. 'name' => 'Sequence Ontology',
  99. 'path' => 'http://purl.obolibrary.org/obo/so.obo',
  100. 'auto_load' => TRUE,
  101. 'cv_name' => 'sequence'
  102. ),
  103. );
  104. for ($i = 0; $i < count($ontologies); $i++) {
  105. $obo_id = tripal_insert_obo($ontologies[$i]['name'], $ontologies[$i]['path']);
  106. if ($ontologies[$i]['auto_load'] == TRUE) {
  107. // Only load ontologies that are not already in the cv table.
  108. $cv = tripal_get_cv(array('name' => $ontologies[$i]['cv_name']));
  109. if (!$cv) {
  110. tripal_chado_load_obo_v1_2_id($obo_id);
  111. }
  112. }
  113. }
  114. }
  115. /**
  116. * Prepares Chado for use by Tripal.
  117. */
  118. function tripal_chado_prepare_chado() {
  119. try {
  120. // We want to provide a set of commonly used entity types by default. This
  121. // way when a user first installs Tripal there are some commonly used
  122. // formats.
  123. module_load_include('inc', 'tripal', 'api/tripal.api');
  124. module_load_include('inc', 'tripal', 'includes/tripal.admin');
  125. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.semweb');
  126. // Get the effective version. Pass true as second argument
  127. // to warn the user if the current version is not compatible.
  128. $version = chado_get_version(FALSE, FALSE);
  129. // We want to force the version of Chado to be set properly.
  130. $real_version = chado_get_version(TRUE);
  131. // Create the temp table we will use for loading OBO files.
  132. tripal_chado_add_tripal_obo_temp_table();
  133. // Import commonly used ontologies if needed.
  134. tripal_chado_load_ontologies();
  135. // Populate the semantic web associations for Chado tables/fields.
  136. tripal_chado_populate_chado_semweb_table();
  137. // Initialize the population of the chado_cvterm_mapping table. This will
  138. // map existing data types already in Chado so that when users want to
  139. // add new content types it simplifies the form for them.
  140. tripal_chado_map_cvterms();
  141. // Create the 'Organism' entity type. This uses the obi:organism term.
  142. $error = '';
  143. $args = array(
  144. 'vocabulary' => 'OBI',
  145. 'accession' => '0100026',
  146. 'term_name' => 'organism',
  147. 'storage_args' => array(
  148. 'data_table' => 'organism',
  149. )
  150. );
  151. $term = tripal_load_term_entity(array('vocabulary' => 'OBI', 'accession' => '0100026'));
  152. $bundle = tripal_load_bundle_entity(array('term_id' => $term->id));
  153. if (!$term or !$bundle) {
  154. if (!tripal_create_bundle($args, $error)) {
  155. throw new Exception($error['!message']);
  156. }
  157. }
  158. // Create the 'Analysis' entity type. This uses the local:analysis term.
  159. $error = '';
  160. $args = array(
  161. 'vocabulary' => 'local',
  162. 'accession' => 'analysis',
  163. 'term_name' => 'analysis',
  164. 'storage_args' => array(
  165. 'data_table' => 'analysis',
  166. )
  167. );
  168. $term = tripal_load_term_entity(array('vocabulary' => 'local', 'accession' => 'analysis'));
  169. $bundle = tripal_load_bundle_entity(array('term_id' => $term->id));
  170. if (!$term or !$bundle) {
  171. if (!tripal_create_bundle($args, $error)) {
  172. throw new Exception($error['!message']);
  173. }
  174. }
  175. // Create the 'Project' entity type. This uses the local:project term.
  176. $error = '';
  177. $args = array(
  178. 'vocabulary' => 'local',
  179. 'accession' => 'project',
  180. 'term_name' => 'project',
  181. 'storage_args' => array(
  182. 'data_table' => 'project',
  183. )
  184. );
  185. $term = tripal_load_term_entity(array('vocabulary' => 'local', 'accession' => 'project'));
  186. $bundle = tripal_load_bundle_entity(array('term_id' => $term->id));
  187. if (!$term or !$bundle) {
  188. if (!tripal_create_bundle($args, $error)) {
  189. throw new Exception($error['!message']);
  190. }
  191. }
  192. // Create the 'Map' entity type. This uses the local:project term.
  193. $error = '';
  194. $args = array(
  195. 'vocabulary' => 'data',
  196. 'accession' => '1274',
  197. 'term_name' => 'Map',
  198. 'storage_args' => array(
  199. 'data_table' => 'featuremap',
  200. )
  201. );
  202. $term = tripal_load_term_entity(array('vocabulary' => 'data', 'accession' => '1274'));
  203. $bundle = tripal_load_bundle_entity(array('term_id' => $term->id));
  204. if (!$term or !$bundle) {
  205. if (!tripal_create_bundle($args, $error)) {
  206. throw new Exception($error['!message']);
  207. }
  208. }
  209. // Import a publication so we get all of the properties before
  210. // creating the content type.
  211. tripal_import_pub_by_dbxref('PMID:24163125');
  212. // Create the 'Publication' entity type.
  213. $error = '';
  214. $args = array(
  215. 'vocabulary' => 'TPUB',
  216. 'accession' => '0000002',
  217. 'term_name' => 'Publication',
  218. 'storage_args' => array(
  219. 'data_table' => 'pub',
  220. )
  221. );
  222. $term = tripal_load_term_entity(array('vocabulary' => 'TPUB', 'accession' => '0000002'));
  223. $bundle = tripal_load_bundle_entity(array('term_id' => $term->id));
  224. if (!$term or !$bundle) {
  225. if (!tripal_create_bundle($args, $error)) {
  226. throw new Exception($error['!message']);
  227. }
  228. }
  229. // Now remove the publication that was added above.
  230. $values = array(
  231. 'dbxref_id' => array(
  232. 'accession' => '24163125',
  233. 'db_id' => array(
  234. 'name' => 'PMID',
  235. ),
  236. ),
  237. );
  238. $result = chado_select_record('pub_dbxref', array('pub_id'), $values);
  239. chado_delete_record('pub', array('pub_id' => $result[0]->pub_id));
  240. // Create the 'Gene' entity type.
  241. $error = '';
  242. $args = array(
  243. 'vocabulary' => 'SO',
  244. 'accession' => '0000704',
  245. 'term_name' => 'gene',
  246. 'storage_args' => array(
  247. 'data_table' => 'feature',
  248. 'type_column' => 'type_id',
  249. )
  250. );
  251. $term = tripal_load_term_entity(array('vocabulary' => 'SO', 'accession' => '0000704'));
  252. $bundle = tripal_load_bundle_entity(array('term_id' => $term->id));
  253. if (!$term or !$bundle) {
  254. if (!tripal_create_bundle($args, $error)) {
  255. throw new Exception($error['!message']);
  256. }
  257. }
  258. // Create the 'mRNA' entity type.
  259. $error = '';
  260. $args = array(
  261. 'vocabulary' => 'SO',
  262. 'accession' => '0000234',
  263. 'term_name' => 'mRNA',
  264. 'storage_args' => array(
  265. 'data_table' => 'feature',
  266. 'type_column' => 'type_id',
  267. )
  268. );
  269. $term = tripal_load_term_entity(array('vocabulary' => 'SO', 'accession' => '0000234'));
  270. $bundle = tripal_load_bundle_entity(array('term_id' => $term->id));
  271. if (!$term or !$bundle) {
  272. if (!tripal_create_bundle($args, $error)) {
  273. throw new Exception($error['!message']);
  274. }
  275. }
  276. // Create custom tables depending on the Chado version installed.
  277. $chado_version = chado_get_version();
  278. if ($chado_version == '1.1') {
  279. tripal_chado_add_v1_1_custom_tables();
  280. tripal_chado_add_vx_x_custom_tables();
  281. }
  282. if ($chado_version == '1.2') {
  283. tripal_chado_add_v1_2_custom_tables();
  284. tripal_chado_add_vx_x_custom_tables();
  285. }
  286. if ($chado_version == '1.3') {
  287. tripal_chado_add_vx_x_custom_tables();
  288. tripal_chado_fix_v1_3_custom_tables();
  289. }
  290. // Add the supported loaders
  291. variable_set('tripal_pub_supported_dbs', array('PMID', 'AGL'));
  292. // Set a variable to indicate the site is prepared.
  293. variable_set('tripal_chado_is_prepared', TRUE);
  294. }
  295. catch (Exception $e) {
  296. throw new Exception($e);
  297. }
  298. }
  299. /**
  300. * For Chado v1.1 Tripal provides some new custom tables.
  301. *
  302. * For Chado v1.2 or greater these tables are not needed as they are part of the
  303. * schema update.
  304. */
  305. function tripal_chado_add_v1_1_custom_tables(){
  306. module_load_include('inc', 'tripal_chado', 'includes/setup/tripal_chado.chado_v1_1');
  307. tripal_chado_add_analysisfeatureprop_table();
  308. }
  309. /**
  310. * For Chado v1.2 Tripal provides some new custom tables.
  311. *
  312. * For Chado v1.3 these tables are not needed as they are part of the
  313. * schema update.
  314. */
  315. function tripal_chado_add_v1_2_custom_tables(){
  316. module_load_include('inc', 'tripal_chado', 'includes/setup/tripal_chado.chado_v1_2');
  317. tripal_chado_add_contactprop_table();
  318. tripal_chado_add_featuremap_dbxref_table();
  319. tripal_chado_add_featuremapprop_table();
  320. tripal_chado_add_featureposprop_table();
  321. tripal_chado_add_pubauthor_contact_table();
  322. }
  323. /**
  324. * Add custom tables for any version of Chado.
  325. *
  326. * These are tables that Chado uses to manage the site (i.e. temporary
  327. * loading tables) and not for primary data storage.
  328. */
  329. function tripal_chado_add_vx_x_custom_tables(){
  330. module_load_include('inc', 'tripal_chado', 'includes/setup/tripal_chado.chado_vx_x');
  331. // Add in custom tables.
  332. tripal_chado_add_tripal_gff_temp_table();
  333. tripal_chado_add_tripal_gffcds_temp_table();
  334. tripal_chado_add_tripal_gffprotein_temp_table();
  335. tripal_chado_add_tripal_obo_temp_table();
  336. // Add in materialized views.
  337. tripal_chado_add_organism_stock_count_mview();
  338. tripal_chado_add_library_feature_count_mview();
  339. tripal_chado_add_organism_feature_count_mview();
  340. tripal_chado_add_analysis_organism_mview();
  341. tripal_chado_add_cv_root_mview_mview();
  342. }
  343. /**
  344. * Many of the custom tables created for Chado v1.2 are now in Chado v1.3.
  345. *
  346. * These tables need not be tracked by Tripal anymore as custom tables and
  347. * in some cases the Chado version has different columns so we need to
  348. * adjust them.
  349. */
  350. function tripal_chado_fix_v1_3_custom_tables() {
  351. // Update the featuremap_dbxref table by adding an is_current field.
  352. if (!chado_column_exists('featuremap_dbxref', 'is_current')) {
  353. chado_query("ALTER TABLE {featuremap_dbxref} ADD COLUMN is_current boolean DEFAULT true NOT NULL,;");
  354. }
  355. // Remove the previously managed custom tables from the
  356. // tripal_custom_tables table.
  357. db_delete('tripal_custom_tables')
  358. ->condition('table_name', array('analysisfeatureprop', 'featuremap_dbxref', 'contactprop', 'featuremapprop', 'featureposprop', 'pubauthor_contact'))
  359. ->execute();
  360. }