tripal_chado.setup.inc 17 KB

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