Last updated: 2024-04-14

Checks: 7 0

Knit directory: muse/

This reproducible R Markdown analysis was created with workflowr (version 1.7.1). 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 95fa6fd. 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:    .Rproj.user/
    Ignored:    r_packages_4.3.0/
    Ignored:    r_packages_4.3.2/
    Ignored:    r_packages_4.3.3/

Untracked files:
    Untracked:  analysis/breast_cancer.Rmd
    Untracked:  code/multiz100way/
    Untracked:  data/lung_bcell.rds
    Untracked:  data/pbmc3k.csv
    Untracked:  data/pbmc3k.csv.gz
    Untracked:  data/pbmc3k/

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/harmony.Rmd) and HTML (docs/harmony.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 95fa6fd Dave Tang 2024-04-14 Create some plots
html 9abb7b6 Dave Tang 2024-04-14 Build site.
Rmd 72ffea9 Dave Tang 2024-04-14 Getting started with harmony

Quickstart

Follow the quickstart tutorial

install.packages("harmony")

Load {harmony}.

library("harmony")
Loading required package: Rcpp
packageVersion("harmony")
[1] '1.2.0'

Data

We library normalized the cells, log transformed the counts, and scaled the genes. Then we performed PCA and kept the top 20 PCs. The PCA embeddings and meta data are available as part of this package.

data(cell_lines)
V <- cell_lines$scaled_pcs
meta_data <- cell_lines$meta_data

str(cell_lines)
List of 2
 $ meta_data : tibble [2,370 × 5] (S3: tbl_df/tbl/data.frame)
  ..$ cell_id     : chr [1:2370] "half_GTACGAACCACCAA" "t293_AGGTCATGCACTTT" "half_ATAGTTGACTTCTA" "half_GAGCGGCTTGCTTT" ...
  ..$ dataset     : chr [1:2370] "half" "t293" "half" "half" ...
  ..$ nGene       : int [1:2370] 1508 4009 3545 2450 2388 3762 3792 4089 3374 3023 ...
  ..$ percent_mito: num [1:2370] 0.0148 0.0232 0.0153 0.017 0.0601 ...
  ..$ cell_type   : chr [1:2370] "jurkat" "t293" "jurkat" "jurkat" ...
  ..- attr(*, ".internal.selfref")=<externalptr> 
 $ scaled_pcs:Classes 'data.table' and 'data.frame':    2370 obs. of  20 variables:
  ..$ X1 : num [1:2370] 0.00281 -0.01167 0.00933 0.00634 0.00855 ...
  ..$ X2 : num [1:2370] -0.00145 0.000877 -0.006972 -0.002518 0.007087 ...
  ..$ X3 : num [1:2370] -0.00639 0.000897 -0.002599 -0.00439 -0.002254 ...
  ..$ X4 : num [1:2370] 0.000282 0.001324 0.001882 0.000274 0.001679 ...
  ..$ X5 : num [1:2370] 0.00144 -0.00329 -0.0038 -0.0025 0.00455 ...
  ..$ X6 : num [1:2370] 0.000752 0.001303 -0.000347 0.000435 0.0003 ...
  ..$ X7 : num [1:2370] -0.00283 -0.00198 -0.00157 0.00136 -0.0016 ...
  ..$ X8 : num [1:2370] -0.000653 0.001625 -0.003272 -0.00263 -0.000263 ...
  ..$ X9 : num [1:2370] 0.001411 -0.000913 -0.001031 -0.001876 0.001389 ...
  ..$ X10: num [1:2370] -0.000417 -0.000175 -0.001623 -0.000425 0.000391 ...
  ..$ X11: num [1:2370] 0.001652 -0.000034 0.001241 -0.000458 -0.001444 ...
  ..$ X12: num [1:2370] 6.71e-05 3.76e-04 -7.61e-04 -6.52e-04 -2.44e-03 ...
  ..$ X13: num [1:2370] 0.000542 0.000219 -0.001502 -0.002067 -0.000907 ...
  ..$ X14: num [1:2370] 0.001223 0.001688 -0.000279 -0.000927 -0.000135 ...
  ..$ X15: num [1:2370] 0.002081 0.000386 -0.001141 0.001114 0.001015 ...
  ..$ X16: num [1:2370] 1.87e-03 -1.50e-03 5.99e-04 -1.98e-05 -1.25e-03 ...
  ..$ X17: num [1:2370] 0.000429 0.000259 0.001224 -0.001069 -0.001165 ...
  ..$ X18: num [1:2370] 0.00115 -0.00106 0.00145 0.00028 0.00111 ...
  ..$ X19: num [1:2370] -1.09e-03 4.11e-04 7.41e-05 9.33e-04 -1.76e-04 ...
  ..$ X20: num [1:2370] 0.000265 -0.00171 -0.000662 0.000365 0.000477 ...
  ..- attr(*, ".internal.selfref")=<externalptr> 

Cell types.

table(cell_lines$meta_data$cell_type)

jurkat   t293 
  1266   1104 

Dataset.

table(cell_lines$meta_data$dataset)

  half jurkat   t293 
   846    824    700 

Analysis

Initially, the cells cluster by both dataset (left) and cell type (right). The quickstart guide uses the do_scatter() function, which is missing. We can simply plot the first two PCs using {ggplot2}.

Plot PC1 versus PC2.

my_df <- data.frame(PC1 = V$X1, PC2 = V$X2, dataset = meta_data$dataset, cell_type = meta_data$cell_type)

ggplot(my_df, aes(PC1, PC2, colour = dataset)) +
  geom_point() +
  theme_minimal() +
  ggtitle("Before harmony") -> p1

ggplot(my_df, aes(PC1, PC2, colour = cell_type)) +
  geom_point() +
  theme_minimal() -> p2

p1 + p2

Let’s run Harmony to remove the influence of dataset-of-origin from the cell embeddings.

harmony_embeddings <- harmony::RunHarmony(
    V, meta_data, 'dataset', verbose=FALSE
)

my_df2 <- data.frame(PC1 = harmony_embeddings[, 1], PC2 = harmony_embeddings[, 2], dataset = meta_data$dataset, cell_type = meta_data$cell_type)

ggplot(my_df2, aes(PC1, PC2, colour = dataset)) +
  geom_point() +
  theme_minimal() +
  ggtitle("After harmony") -> p1

ggplot(my_df2, aes(PC1, PC2, colour = cell_type)) +
  geom_point() +
  theme_minimal() -> p2

p1 + p2


sessionInfo()
R version 4.3.3 (2024-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.4 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so;  LAPACK version 3.10.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: Etc/UTC
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] harmony_1.2.0   Rcpp_1.0.12     patchwork_1.2.0 lubridate_1.9.3
 [5] forcats_1.0.0   stringr_1.5.1   dplyr_1.1.4     purrr_1.0.2    
 [9] readr_2.1.5     tidyr_1.3.1     tibble_3.2.1    ggplot2_3.5.0  
[13] tidyverse_2.0.0 workflowr_1.7.1

loaded via a namespace (and not attached):
 [1] gtable_0.3.4        xfun_0.43           bslib_0.7.0        
 [4] processx_3.8.4      lattice_0.22-5      callr_3.7.6        
 [7] tzdb_0.4.0          vctrs_0.6.5         tools_4.3.3        
[10] ps_1.7.6            generics_0.1.3      fansi_1.0.6        
[13] highr_0.10          pkgconfig_2.0.3     Matrix_1.6-5       
[16] lifecycle_1.0.4     compiler_4.3.3      farver_2.1.1       
[19] git2r_0.33.0        munsell_0.5.1       RhpcBLASctl_0.23-42
[22] getPass_0.2-4       codetools_0.2-19    httpuv_1.6.15      
[25] htmltools_0.5.8.1   sass_0.4.9          yaml_2.3.8         
[28] later_1.3.2         pillar_1.9.0        jquerylib_0.1.4    
[31] whisker_0.4.1       cachem_1.0.8        tidyselect_1.2.1   
[34] digest_0.6.35       stringi_1.8.3       labeling_0.4.3     
[37] cowplot_1.1.3       rprojroot_2.0.4     fastmap_1.1.1      
[40] grid_4.3.3          colorspace_2.1-0    cli_3.6.2          
[43] magrittr_2.0.3      utf8_1.2.4          withr_3.0.0        
[46] scales_1.3.0        promises_1.3.0      timechange_0.3.0   
[49] rmarkdown_2.26      httr_1.4.7          hms_1.1.3          
[52] evaluate_0.23       knitr_1.46          rlang_1.1.3        
[55] glue_1.7.0          rstudioapi_0.16.0   jsonlite_1.8.8     
[58] R6_2.5.1            fs_1.6.3