tripal_chado.setup.inc

Functions to install chado schema through Drupal

File

tripal_chado/includes/setup/tripal_chado.setup.inc
View source
  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. );
  52. tripal_add_job('Prepare Chado', 'tripal_chado',
  53. 'tripal_chado_prepare_chado', $args,
  54. $user->uid, 10, $includes);
  55. }
  56. if ($form_state['clicked_button']['#name'] == "prepare-skip") {
  57. variable_set('tripal_chado_is_prepared', TRUE);
  58. }
  59. }
  60. /**
  61. * Submit function for the tripal_chado_prepare_form().
  62. *
  63. * @param $form
  64. * @param $form_state
  65. */
  66. function tripal_chado_prepare_drush_submit() {
  67. $args = array();
  68. $includes = array(
  69. module_load_include('inc', 'tripal_chado', 'includes/setup/tripal_chado.setup'),
  70. );
  71. tripal_add_job('Prepare Chado', 'tripal_chado',
  72. 'tripal_chado_prepare_chado', $args,
  73. 1, 10, $includes);
  74. }
  75. /**
  76. *
  77. */
  78. function tripal_chado_load_ontologies() {
  79. // Insert commonly used ontologies into the tables.
  80. $ontologies = array(
  81. array(
  82. 'name' => 'Relationship Ontology (legacy)',
  83. 'path' => '{tripal_chado}/files/legacy_ro.obo',
  84. 'auto_load' => FALSE,
  85. 'cv_name' => 'ro',
  86. 'db_name' => 'RO',
  87. ),
  88. array(
  89. 'name' => 'Gene Ontology',
  90. 'path' => 'http://purl.obolibrary.org/obo/go.obo',
  91. 'auto_load' => FALSE,
  92. 'cv_name' => 'cellualar_component',
  93. 'db_name' => 'GO',
  94. ),
  95. array(
  96. 'name' => 'Taxonomic Rank',
  97. 'path' => 'http://purl.obolibrary.org/obo/taxrank.obo',
  98. 'auto_load' => TRUE,
  99. 'cv_name' => 'taxonomic_rank',
  100. 'db_name' => 'TAXRANK'
  101. ),
  102. array(
  103. 'name' => 'Tripal Contact',
  104. 'path' => '{tripal_chado}/files/tcontact.obo',
  105. 'auto_load' => TRUE,
  106. 'cv_name' => 'tripal_contact',
  107. 'db_name' => 'TCONTACT'
  108. ),
  109. array(
  110. 'name' => 'Tripal Publication',
  111. 'path' => '{tripal_chado}/files/tpub.obo',
  112. 'auto_load' => TRUE,
  113. 'cv_name' => 'tripal_pub',
  114. 'db_name' => 'TPUB',
  115. ),
  116. array(
  117. 'name' => 'Sequence Ontology',
  118. 'path' => 'http://purl.obolibrary.org/obo/so.obo',
  119. 'auto_load' => TRUE,
  120. 'cv_name' => 'sequence',
  121. 'db_name' => 'SO',
  122. ),
  123. );
  124. module_load_include('inc', 'tripal_chado', 'includes/TripalImporter/OBOImporter');
  125. for ($i = 0; $i < count($ontologies); $i++) {
  126. $obo_id = chado_insert_obo($ontologies[$i]['name'], $ontologies[$i]['path']);
  127. if ($ontologies[$i]['auto_load'] == TRUE) {
  128. // Only load ontologies that are not already in the cv table.
  129. $cv = chado_get_cv(array('name' => $ontologies[$i]['cv_name']));
  130. $db = chado_get_db(array('name' => $ontologies[$i]['db_name']));
  131. if (!$cv or !$db) {
  132. print "Loading ontology: " . $ontologies[$i]['name'] . " ($obo_id)...\n";
  133. $obo_importer = new OBOImporter();
  134. $obo_importer->create(array('obo_id' => $obo_id));
  135. $obo_importer->run();
  136. }
  137. else {
  138. print "Ontology already loaded (skipping): " . $ontologies[$i]['name'] . "...\n";
  139. }
  140. }
  141. }
  142. }
  143. /**
  144. * Prepares Chado for use by Tripal.
  145. */
  146. function tripal_chado_prepare_chado($job = NULL) {
  147. // Retrieve the job arguement in order to report progress.
  148. if (is_int($job)) {
  149. $job = new TripalJob();
  150. $job->load($job);
  151. }
  152. $report_progress = TRUE;
  153. if (!is_object($job)) {
  154. $report_progress = FALSE;
  155. }
  156. try {
  157. // We want to provide a set of commonly used entity types by default. This
  158. // way when a user first installs Tripal there are some commonly used
  159. // formats.
  160. module_load_include('inc', 'tripal', 'api/tripal.api');
  161. module_load_include('inc', 'tripal', 'includes/tripal.admin');
  162. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.semweb');
  163. // Get the effective version. Pass true as second argument
  164. // to warn the user if the current version is not compatible.
  165. $version = chado_get_version(FALSE, FALSE);
  166. // We want to force the version of Chado to be set properly.
  167. $real_version = chado_get_version(TRUE);
  168. // Create custom tables depending on the Chado version installed.
  169. drush_print("Creating Tripal Materialized Views and Custom Tables...");
  170. $chado_version = chado_get_version();
  171. if ($chado_version == '1.1') {
  172. tripal_chado_add_v1_1_custom_tables();
  173. tripal_chado_add_vx_x_custom_tables();
  174. }
  175. if ($chado_version == '1.2') {
  176. tripal_chado_add_v1_2_custom_tables();
  177. tripal_chado_add_vx_x_custom_tables();
  178. }
  179. if ($chado_version == '1.3') {
  180. tripal_chado_add_vx_x_custom_tables();
  181. tripal_chado_fix_v1_3_custom_tables();
  182. }
  183. if ($report_progress) {
  184. $job->setProgress(5);
  185. }
  186. // Import commonly used ontologies if needed.
  187. drush_print("Loading Ontologies...");
  188. tripal_chado_load_ontologies();
  189. // Populate the mviews based on controlled vocabularies.
  190. drush_print('Populating materialized view db2cv_mview...');
  191. $mview_id = chado_get_mview_id('db2cv_mview');
  192. chado_populate_mview($mview_id);
  193. drush_print('Populating materialized view cv_root_mview...');
  194. $mview_id = chado_get_mview_id('cv_root_mview');
  195. chado_populate_mview($mview_id);
  196. if ($report_progress) {
  197. $job->setProgress(50);
  198. }
  199. // Populate the semantic web associations for Chado tables/fields.
  200. drush_print("Making semantic connections for Chado tables/fields...");
  201. tripal_chado_populate_chado_semweb_table();
  202. if ($report_progress) {
  203. $job->setProgress(60);
  204. }
  205. // Initialize the population of the chado_cvterm_mapping table. This will
  206. // map existing data types already in Chado so that when users want to
  207. // add new content types it simplifies the form for them.
  208. drush_print("Map Chado Controlled vocabularies to Tripal Terms...");
  209. tripal_chado_map_cvterms();
  210. if ($report_progress) {
  211. $job->setProgress(70);
  212. }
  213. drush_print("Creating common Tripal Content Types...");
  214. // Create the 'Organism' entity type. This uses the obi:organism term.
  215. $error = '';
  216. $args = array(
  217. 'vocabulary' => 'OBI',
  218. 'accession' => '0100026',
  219. 'term_name' => 'organism',
  220. 'storage_args' => array(
  221. 'data_table' => 'organism',
  222. )
  223. );
  224. $term = tripal_load_term_entity(array('vocabulary' => 'OBI', 'accession' => '0100026'));
  225. if ($term) {
  226. $bundle = tripal_load_bundle_entity(array('term_id' => $term->id));
  227. }
  228. if (!$term or !$bundle) {
  229. if (!tripal_create_bundle($args, $error)) {
  230. $msg = (isset($error['!message'])) ? $error['!message'] : 'Error Encountered creating "Organism" Tripal Content Type.';
  231. throw new Exception($msg);
  232. }
  233. }
  234. if ($report_progress) {
  235. $job->setProgress(74);
  236. }
  237. // Create the 'Analysis' entity type. This uses the local:analysis term.
  238. $error = '';
  239. $args = array(
  240. 'vocabulary' => 'operation',
  241. 'accession' => '2945',
  242. 'term_name' => 'Analysis',
  243. 'storage_args' => array(
  244. 'data_table' => 'analysis',
  245. )
  246. );
  247. $term = tripal_load_term_entity(array('vocabulary' => 'operation', 'accession' => '2945'));
  248. if ($term) {
  249. $bundle = tripal_load_bundle_entity(array('term_id' => $term->id));
  250. }
  251. if (!$term or !$bundle) {
  252. if (!tripal_create_bundle($args, $error)) {
  253. $msg = (isset($error['!message'])) ? $error['!message'] : 'Error Encountered creating "Analysis" Tripal Content Type.';
  254. throw new Exception($msg);
  255. }
  256. }
  257. if ($report_progress) {
  258. $job->setProgress(78);
  259. }
  260. // Create the 'Project' entity type. This uses the local:project term.
  261. $error = '';
  262. $args = array(
  263. 'vocabulary' => 'local',
  264. 'accession' => 'project',
  265. 'term_name' => 'project',
  266. 'storage_args' => array(
  267. 'data_table' => 'project',
  268. )
  269. );
  270. $term = tripal_load_term_entity(array('vocabulary' => 'local', 'accession' => 'project'));
  271. if ($term) {
  272. $bundle = tripal_load_bundle_entity(array('term_id' => $term->id));
  273. }
  274. if (!$term or !$bundle) {
  275. if (!tripal_create_bundle($args, $error)) {
  276. $msg = (isset($error['!message'])) ? $error['!message'] : 'Error Encountered creating "Project" Tripal Content Type.';
  277. throw new Exception($msg);
  278. }
  279. }
  280. if ($report_progress) {
  281. $job->setProgress(82);
  282. }
  283. // Create the 'Map' entity type. This uses the local:project term.
  284. $error = '';
  285. $args = array(
  286. 'vocabulary' => 'data',
  287. 'accession' => '1274',
  288. 'term_name' => 'Map',
  289. 'storage_args' => array(
  290. 'data_table' => 'featuremap',
  291. )
  292. );
  293. $term = tripal_load_term_entity(array('vocabulary' => 'data', 'accession' => '1274'));
  294. if ($term) {
  295. $bundle = tripal_load_bundle_entity(array('term_id' => $term->id));
  296. }
  297. if (!$term or !$bundle) {
  298. if (!tripal_create_bundle($args, $error)) {
  299. $msg = (isset($error['!message'])) ? $error['!message'] : 'Error Encountered creating "Map" Tripal Content Type.';
  300. throw new Exception($msg);
  301. }
  302. }
  303. // Add cvterm mapping for the Map entity type
  304. $identifier = array(
  305. 'cv_id' => array('name' => 'EDAM'),
  306. 'name' => 'Map'
  307. );
  308. $cvterm = chado_get_cvterm($identifier);
  309. tripal_chado_add_cvterm_mapping($cvterm->cvterm_id, 'featuremap', NULL);
  310. if ($report_progress) {
  311. $job->setProgress(86);
  312. }
  313. // Import a publication so we get all of the properties before
  314. // creating the content type.
  315. chado_import_pub_by_dbxref('PMID:24163125');
  316. // Create the 'Publication' entity type.
  317. $error = '';
  318. $args = array(
  319. 'vocabulary' => 'TPUB',
  320. 'accession' => '0000002',
  321. 'term_name' => 'Publication',
  322. 'storage_args' => array(
  323. 'data_table' => 'pub',
  324. )
  325. );
  326. $term = tripal_load_term_entity(array('vocabulary' => 'TPUB', 'accession' => '0000002'));
  327. if ($term) {
  328. $bundle = tripal_load_bundle_entity(array('term_id' => $term->id));
  329. }
  330. if (!$term or !$bundle) {
  331. if (!tripal_create_bundle($args, $error)) {
  332. $msg = (isset($error['!message'])) ? $error['!message'] : 'Error Encountered creating "Publication" Tripal Content Type.';
  333. throw new Exception($msg);
  334. }
  335. }
  336. // Add cvterm mapping for the Publication entity type
  337. $identifier = array(
  338. 'cv_id' => array('name' => 'tripal_pub'),
  339. 'name' => 'Publication'
  340. );
  341. $cvterm = chado_get_cvterm($identifier);
  342. tripal_chado_add_cvterm_mapping($cvterm->cvterm_id, 'pub', NULL);
  343. // Now remove the publication that was added above.
  344. $values = array(
  345. 'dbxref_id' => array(
  346. 'accession' => '24163125',
  347. 'db_id' => array(
  348. 'name' => 'PMID',
  349. ),
  350. ),
  351. );
  352. $result = chado_select_record('pub_dbxref', array('pub_id'), $values);
  353. chado_delete_record('pub', array('pub_id' => $result[0]->pub_id));
  354. if ($report_progress) {
  355. $job->setProgress(90);
  356. }
  357. // Create the 'Gene' entity type.
  358. $error = '';
  359. $args = array(
  360. 'vocabulary' => 'SO',
  361. 'accession' => '0000704',
  362. 'term_name' => 'gene',
  363. 'storage_args' => array(
  364. 'data_table' => 'feature',
  365. 'type_column' => 'type_id',
  366. )
  367. );
  368. $term = tripal_load_term_entity(array('vocabulary' => 'SO', 'accession' => '0000704'));
  369. if ($term) {
  370. $bundle = tripal_load_bundle_entity(array('term_id' => $term->id));
  371. }
  372. if (!$term or !$bundle) {
  373. if (!tripal_create_bundle($args, $error)) {
  374. $msg = (isset($error['!message'])) ? $error['!message'] : 'Error Encountered creating "Gene" Tripal Content Type.';
  375. throw new Exception($msg);
  376. }
  377. }
  378. if ($report_progress) {
  379. $job->setProgress(94);
  380. }
  381. // Create the 'mRNA' entity type.
  382. $error = '';
  383. $args = array(
  384. 'vocabulary' => 'SO',
  385. 'accession' => '0000234',
  386. 'term_name' => 'mRNA',
  387. 'storage_args' => array(
  388. 'data_table' => 'feature',
  389. 'type_column' => 'type_id',
  390. )
  391. );
  392. $term = tripal_load_term_entity(array('vocabulary' => 'SO', 'accession' => '0000234'));
  393. if ($term) {
  394. $bundle = tripal_load_bundle_entity(array('term_id' => $term->id));
  395. }
  396. if (!$term or !$bundle) {
  397. if (!tripal_create_bundle($args, $error)) {
  398. $msg = (isset($error['!message'])) ? $error['!message'] : 'Error Encountered creating "mRNA" Tripal Content Type.';
  399. throw new Exception($msg);
  400. }
  401. }
  402. if ($report_progress) {
  403. $job->setProgress(98);
  404. }
  405. // Create the 'biological sample' entity type.
  406. $error = '';
  407. $args = array(
  408. 'vocabulary' => 'sep',
  409. 'accession' => '00195',
  410. 'term_name' => 'biological sample',
  411. 'storage_args' => array(
  412. 'data_table' => 'biomaterial',
  413. )
  414. );
  415. $term = tripal_load_term_entity(array('vocabulary' => 'sep', 'accession' => '00195'));
  416. if ($term) {
  417. $bundle = tripal_load_bundle_entity(array('term_id' => $term->id));
  418. }
  419. if (!$term or !$bundle) {
  420. if (!tripal_create_bundle($args, $error)) {
  421. $msg = (isset($error['!message'])) ? $error['!message'] : 'Error Encountered creating "Biological Sample" Tripal Content Type.';
  422. throw new Exception($msg);
  423. }
  424. }
  425. if ($report_progress) {
  426. $job->setProgress(99);
  427. }
  428. // Create the 'Phylogenetic tree' entity type.
  429. $error = '';
  430. $args = array(
  431. 'vocabulary' => 'data',
  432. 'accession' => '0872',
  433. 'term_name' => 'Phylogenetic tree',
  434. 'storage_args' => array(
  435. 'data_table' => 'phylotree',
  436. )
  437. );
  438. $term = tripal_load_term_entity(array('vocabulary' => 'data', 'accession' => '0872'));
  439. if ($term) {
  440. $bundle = tripal_load_bundle_entity(array('term_id' => $term->id));
  441. }
  442. if (!$term or !$bundle) {
  443. if (!tripal_create_bundle($args, $error)) {
  444. $msg = (isset($error['!message'])) ? $error['!message'] : 'Error Encountered creating "Phylogenetic tree" Tripal Content Type';
  445. throw new Exception($msg);
  446. }
  447. }
  448. if ($report_progress) {
  449. $job->setProgress(99);
  450. }
  451. // Add the supported loaders
  452. variable_set('tripal_pub_supported_dbs', array('PMID', 'AGL'));
  453. // Set a variable to indicate the site is prepared.
  454. variable_set('tripal_chado_is_prepared', TRUE);
  455. }
  456. catch (Exception $e) {
  457. $job->logMessage($e);
  458. throw new Exception($e);
  459. }
  460. }
  461. /**
  462. * For Chado v1.1 Tripal provides some new custom tables.
  463. *
  464. * For Chado v1.2 or greater these tables are not needed as they are part of the
  465. * schema update.
  466. */
  467. function tripal_chado_add_v1_1_custom_tables(){
  468. module_load_include('inc', 'tripal_chado', 'includes/setup/tripal_chado.chado_v1_1');
  469. tripal_chado_add_analysisfeatureprop_table();
  470. }
  471. /**
  472. * For Chado v1.2 Tripal provides some new custom tables.
  473. *
  474. * For Chado v1.3 these tables are not needed as they are part of the
  475. * schema update.
  476. */
  477. function tripal_chado_add_v1_2_custom_tables(){
  478. module_load_include('inc', 'tripal_chado', 'includes/setup/tripal_chado.chado_v1.2');
  479. tripal_chado_add_contactprop_table();
  480. tripal_chado_add_featuremap_dbxref_table();
  481. tripal_chado_add_featuremapprop_table();
  482. tripal_chado_add_featureposprop_table();
  483. tripal_chado_add_pubauthor_contact_table();
  484. }
  485. /**
  486. * Add custom tables for any version of Chado.
  487. *
  488. * These are tables that Chado uses to manage the site (i.e. temporary
  489. * loading tables) and not for primary data storage.
  490. */
  491. function tripal_chado_add_vx_x_custom_tables(){
  492. module_load_include('inc', 'tripal_chado', 'includes/setup/tripal_chado.chado_vx_x');
  493. // Add in custom tables.
  494. tripal_chado_add_tripal_gff_temp_table();
  495. tripal_chado_add_tripal_gffcds_temp_table();
  496. tripal_chado_add_tripal_gffprotein_temp_table();
  497. tripal_chado_add_tripal_obo_temp_table();
  498. // Add in materialized views.
  499. tripal_chado_add_organism_stock_count_mview();
  500. tripal_chado_add_library_feature_count_mview();
  501. tripal_chado_add_organism_feature_count_mview();
  502. tripal_chado_add_analysis_organism_mview();
  503. tripal_chado_add_cv_root_mview_mview();
  504. tripal_chado_add_db2cv_mview_mview();
  505. }
  506. /**
  507. * Many of the custom tables created for Chado v1.2 are now in Chado v1.3.
  508. *
  509. * These tables need not be tracked by Tripal anymore as custom tables and
  510. * in some cases the Chado version has different columns so we need to
  511. * adjust them.
  512. */
  513. function tripal_chado_fix_v1_3_custom_tables() {
  514. // Update the featuremap_dbxref table by adding an is_current field.
  515. if (!chado_column_exists('featuremap_dbxref', 'is_current')) {
  516. chado_query("ALTER TABLE {featuremap_dbxref} ADD COLUMN is_current boolean DEFAULT true NOT NULL,;");
  517. }
  518. // Remove the previously managed custom tables from the
  519. // tripal_custom_tables table.
  520. db_delete('tripal_custom_tables')
  521. ->condition('table_name', array('analysisfeatureprop', 'featuremap_dbxref', 'contactprop', 'featuremapprop', 'featureposprop', 'pubauthor_contact'))
  522. ->execute();
  523. }