tripal_chado.setup.inc 18 KB

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