Last updated: 2021-06-22
Checks: 7 0
Knit directory: muse/
This reproducible R Markdown analysis was created with workflowr (version 1.6.2). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.
Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.
Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.
The command set.seed(20200712)
was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.
Great job! Recording the operating system, R version, and package versions is critical for reproducibility.
Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.
Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.
Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.
The results in this page were generated with repository version 8ae8e21. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.
Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish
or wflow_git_commit
). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:
Ignored files:
Ignored: .Rhistory
Ignored: .Rproj.user/
Unstaged changes:
Modified: muse.Rproj
Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.
These are the previous versions of the repository in which changes were made to the R Markdown (analysis/split_column.Rmd
) and HTML (docs/split_column.html
) files. If you’ve configured a remote Git repository (see ?wflow_git_remote
), click on the hyperlinks in the table below to view the files as they were in that past version.
File | Version | Author | Date | Message |
---|---|---|---|---|
Rmd | 8ae8e21 | Dave Tang | 2021-06-22 | Post on splitting a single column of annotations |
Use importbio to load a VCF file (or load any file that has a single column containing multiple annotations that are consistently defined).
library(importbio)
my_vcf <- importvcf("https://raw.githubusercontent.com/davetang/learning_vcf_file/master/eg/Pfeiffer.vcf")
head(my_vcf)
# A tibble: 6 x 10
vid chrom pos id ref alt qual filter info type
<chr> <fct> <int> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 1_86651~ 1 866511 rs607~ C CCCCT 258.~ PASS AC=2;AF=1.00;AN=2~ ins
2 1_87931~ 1 879317 rs752~ C T 150.~ PASS AC=1;AF=0.50;AN=2~ snv
3 1_87948~ 1 879482 . G C 484.~ PASS AC=1;AF=0.50;AN=2~ snv
4 1_88039~ 1 880390 rs374~ C A 288.~ PASS AC=1;AF=0.50;AN=2~ snv
5 1_88162~ 1 881627 rs227~ G A 486.~ PASS AC=1;AF=0.50;AN=2~ snv
6 1_88409~ 1 884091 rs752~ C G 65.46 PASS AC=1;AF=0.50;AN=2~ snv
We will be functions within the tidyverse
.
library(tidyverse)
-- Attaching packages --------------------------------------- tidyverse 1.3.1 --
v ggplot2 3.3.3 v purrr 0.3.4
v tibble 3.1.1 v dplyr 1.0.6
v tidyr 1.1.3 v stringr 1.4.0
v readr 1.4.0 v forcats 0.5.1
-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag() masks stats::lag()
Use separate_rows
to create a new row for each annotation. The mutate
call is to remove a trailing semicolon, if it exists.
library(tidyverse)
my_vcf %>%
mutate(info = sub(pattern = ";$", replacement = "", x = .data$info)) %>%
separate_rows(info, sep = ";\\s*") %>%
head()
# A tibble: 6 x 10
vid chrom pos id ref alt qual filter info type
<chr> <fct> <int> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 1_866511_C_CCC~ 1 866511 rs60722469 C CCCCT 258.~ PASS AC=2 ins
2 1_866511_C_CCC~ 1 866511 rs60722469 C CCCCT 258.~ PASS AF=1.00 ins
3 1_866511_C_CCC~ 1 866511 rs60722469 C CCCCT 258.~ PASS AN=2 ins
4 1_866511_C_CCC~ 1 866511 rs60722469 C CCCCT 258.~ PASS DB ins
5 1_866511_C_CCC~ 1 866511 rs60722469 C CCCCT 258.~ PASS DP=11 ins
6 1_866511_C_CCC~ 1 866511 rs60722469 C CCCCT 258.~ PASS FS=0.0~ ins
Use separate
to split key-value annotation pair (separated by an equal sign) into two columns. Missing values will have a NA value.
my_vcf %>%
mutate(info = sub(pattern = ";$", replacement = "", x = .data$info)) %>%
separate_rows(info, sep = ";\\s*") %>%
separate(info, c('key', 'value'), sep = "=") %>%
head(10)
# A tibble: 10 x 11
vid chrom pos id ref alt qual filter key value type
<chr> <fct> <int> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 1_866511~ 1 866511 rs6072~ C CCCCT 258.62 PASS AC 2 ins
2 1_866511~ 1 866511 rs6072~ C CCCCT 258.62 PASS AF 1.00 ins
3 1_866511~ 1 866511 rs6072~ C CCCCT 258.62 PASS AN 2 ins
4 1_866511~ 1 866511 rs6072~ C CCCCT 258.62 PASS DB <NA> ins
5 1_866511~ 1 866511 rs6072~ C CCCCT 258.62 PASS DP 11 ins
6 1_866511~ 1 866511 rs6072~ C CCCCT 258.62 PASS FS 0.000 ins
7 1_866511~ 1 866511 rs6072~ C CCCCT 258.62 PASS HRun 0 ins
8 1_866511~ 1 866511 rs6072~ C CCCCT 258.62 PASS Haploty~ 41.3~ ins
9 1_866511~ 1 866511 rs6072~ C CCCCT 258.62 PASS MQ0 0 ins
10 1_866511~ 1 866511 rs6072~ C CCCCT 258.62 PASS MQ 61.94 ins
Finally, use pivot_wider
to convert the data back into wide format. In the example below, I show three new columns created from splitting the info
column.
my_vcf %>%
mutate(info = sub(pattern = ";$", replacement = "", x = .data$info)) %>%
separate_rows(info, sep = ";\\s*") %>%
separate(info, c('key', 'value'), sep = "=") %>%
distinct() %>% # remove duplication annotations, if any
filter(key == "AC" | key == "DP" | key == "MQ") %>%
pivot_wider(id_cols = vid, names_from = key, values_from = value)
Warning: Expected 2 pieces. Missing pieces filled with `NA` in 35503 rows [4,
17, 48, 64, 80, 96, 112, 128, 144, 159, 172, 188, 204, 220, 236, 251, 266, 282,
298, 314, ...].
# A tibble: 37,708 x 4
vid AC DP MQ
<chr> <chr> <chr> <chr>
1 1_866511_C_CCCCT 2 11 61.94
2 1_879317_C_T 1 21 60.00
3 1_879482_G_C 1 48 59.13
4 1_880390_C_A 1 29 56.93
5 1_881627_G_A 1 33 60.00
6 1_884091_C_G 1 12 53.22
7 1_884101_A_C 1 12 53.22
8 1_892460_G_C 1 152 58.85
9 1_897730_C_T 1 21 58.89
10 1_909238_G_C 1 19 60.00
# ... with 37,698 more rows
separate_rows
to split a single column with multiple annotations into rowsseparate
to split a key-value annotation pair into two columnspivot_wider
to convert the table in long format back to wide format
sessionInfo()
R version 4.0.5 (2021-03-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)
Matrix products: default
locale:
[1] LC_COLLATE=English_Australia.1252 LC_CTYPE=English_Australia.1252
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C
[5] LC_TIME=English_Australia.1252
system code page: 932
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] forcats_0.5.1 stringr_1.4.0 dplyr_1.0.6
[4] purrr_0.3.4 readr_1.4.0 tidyr_1.1.3
[7] tibble_3.1.1 ggplot2_3.3.3 tidyverse_1.3.1
[10] importbio_0.0.0.9000 workflowr_1.6.2
loaded via a namespace (and not attached):
[1] tidyselect_1.1.1 xfun_0.22 haven_2.4.1 colorspace_2.0-1
[5] vctrs_0.3.8 generics_0.1.0 htmltools_0.5.1.1 yaml_2.2.1
[9] utf8_1.2.1 rlang_0.4.11 later_1.2.0 pillar_1.6.1
[13] withr_2.4.2 glue_1.4.2 DBI_1.1.1 dbplyr_2.1.1
[17] readxl_1.3.1 modelr_0.1.8 lifecycle_1.0.0 cellranger_1.1.0
[21] munsell_0.5.0 gtable_0.3.0 rvest_1.0.0 evaluate_0.14
[25] knitr_1.33 httpuv_1.6.1 ps_1.6.0 curl_4.3.1
[29] fansi_0.4.2 broom_0.7.6 Rcpp_1.0.6 promises_1.2.0.1
[33] backports_1.2.1 scales_1.1.1 jsonlite_1.7.2 fs_1.5.0
[37] hms_1.1.0 digest_0.6.27 stringi_1.5.3 rprojroot_2.0.2
[41] grid_4.0.5 cli_2.5.0 tools_4.0.5 magrittr_2.0.1
[45] crayon_1.4.1 whisker_0.4 pkgconfig_2.0.3 ellipsis_0.3.2
[49] xml2_1.3.2 reprex_2.0.0 lubridate_1.7.10 httr_1.4.2
[53] assertthat_0.2.1 rmarkdown_2.8 rstudioapi_0.13 R6_2.5.0
[57] git2r_0.28.0 compiler_4.0.5