-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path01_Data_pre-processing.Rmd
2450 lines (2193 loc) · 61.8 KB
/
01_Data_pre-processing.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: >
Using single-cell chromatin accessibility sequencing to characterize CD4+ T cells from murine tissues
author: >
Kathrin Luise Braband, Annekathrin Silvia Nedwed, Sara Salome Helbich, Malte Simon, Niklas Beumer, Benedikt Brors, Federico Marini and Michael Delacher
email: >
delacher@uni-mainz.de, marinif@uni-mainz.de
date: "`r Sys.Date()`"
output:
pdf_document:
toc: yes
number_sections: yes
html_document:
toc: yes
toc_float: yes
code_folding: show
number_sections: yes
theme: lumen
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r libraries, include=FALSE}
library(ArchR)
library(tidyverse)
library(dplyr)
library(pheatmap)
library(cowplot)
library(magick)
library(pdftools)
library(ggplot2)
library(viridis)
library(ggpointdensity)
```
```{r setInitialParameters}
# Set seeds and initial parameters for ArchR
RNGkind("L'Ecuyer-CMRG")
set.seed(1)
# Number of cores to use (adapt to your machine, but has to be > 1 otherwise plotTSSenrichment does not work properly)
addArchRThreads(threads = 8)
# Reference genome (adapt to your data, use the same reference that was used for the alignment)
addArchRGenome("mm10")
```
# Samples
We are analyzing CD4+ T cells from healthy murine tissue (spleen, colon, fat and skin). For spleen we have an additional sample of CD25+ regulatory T cells. Overall we have 5 samples in this analysis, which were previously aligned using Cell Ranger ATAC (v2.0.0) count. <br>
<br>
scATAC_1 spleen CD4+ T cells <br>
scATAC_8 spleen CD25+ T cells <br>
scATAC_4 colon CD4+ T cells <br>
scATAC_5 fat CD4+ T cells <br>
scATAC_9 skin CD4+ T cells <br>
# Create ArrowFiles
From the fragments.tsv files output by Cell Ranger ATAC count, generate ArrowFiles for subsequent analysis with ArchR.
```{r create arrow files, warning=FALSE, message=FALSE, eval=FALSE}
# Read in fragments files
samples = paste0("MD_scATAC_", c(1,4,5,8,9))
inputFiles = file.path("data", samples, "fragments.tsv.gz")
names(inputFiles) = paste0("scATAC_", c(1,4,5,8,9))
# Create arrow files
# Evaluate different thresholds depending on your data:
# - minTSS: Start with 0 to see all cells, afterwards evaluate which threshold
# works for all of the samples
# - minFrags: Recommended to set >= 1000, otherwise the analysis might not be
# robust enough
# Check different parameters to set with ?createArrowFiles
ArrowFiles = createArrowFiles(
inputFiles = inputFiles,
sampleNames = names(inputFiles),
minTSS = 0,
minFrags = 1000,
addTileMat = TRUE,
addGeneScoreMat = TRUE,
force = TRUE
)
```
After you created the ArrowFiles, check the folder "QualityControl" for each of the samples to evaluate which minTSS threshold to use for the analysis. In this analysis, we decided on minTSS = 10. We will filter the data accordingly later on.
# Create ArchRProject
Now combine the individual ArrowFiles into an ArchRProject.
```{r create ArchRProject, warning=FALSE, message=FALSE, eval=FALSE}
# Define arrow files
ArrowFiles = paste0("scATAC_", c(1,4,5,8,9), ".arrow")
# Create ArchRProject
# It is recommended to set copyArrows = TRUE to maintain an unaltered copy for
# later usage.
proj = ArchRProject(
ArrowFiles = ArrowFiles,
copyArrows = TRUE
)
saveArchRProject(proj,
outputDirectory="ArchRProject",
load=F
)
```
```{r load ArchRProject proj, echo=T, results='hide', message=FALSE, include=FALSE}
# Load ArchRProject
proj = loadArchRProject(
path = "ArchRProject",
force = TRUE,
showLogo = FALSE
)
```
# QC and filtering
## TSS enrichment vs nFrags (unfiltered)
```{r filter for cells passing the TSS enrichment cut-off, message=FALSE, warning=FALSE, eval=FALSE}
# After checking the plots in "QualityControl", we decided to use minTSS = 10.
# Now, we filter for all cells with minTSS >= 10.
proj = proj[proj@cellColData$TSSEnrichment >= 10, ]
```
```{r QC plot (TSS Enrichment vs nFrags), message=FALSE, warning=FALSE}
# As part of the quality control, we plot the TSS enrichment against the number
# of fragments. As we only have one project now, we get one plot for all of the
# data.
# We use geom_pointdensity() for plotting, which colours each datapoint
# according to the number of neighbours and thus allows for evaluation of the
# individual datapoints as well as their distribution
qc_metrics = getCellColData(proj, select = c("log10(nFrags)", "TSSEnrichment"))
p_qc_metrics = ggPoint(
x = qc_metrics[,1],
y = qc_metrics[,2],
colorDensity = FALSE,
xlabel = "Log10 Unique Fragments",
ylabel = "TSS Enrichment",
xlim = c(log10(500), quantile(qc_metrics[,1], probs = 0.99)),
ylim = c(0, quantile(qc_metrics[,2], probs = 0.99))) +
geom_hline(yintercept = 10, lty = "dashed") +
geom_vline(xintercept = 3, lty = "dashed") +
geom_pointdensity(size = 0.5) +
scale_color_viridis()
p_qc_metrics
```
```{r save TSS Enrichment vs nFrags plot, warning=FALSE, message=FALSE, echo=FALSE}
# Save plot as PDF, will be saved in "ArchRProject/Plots"
plotPDF(p_qc_metrics,
name = "TSS_Enrichment_vs_nFrags.pdf",
ArchRProj = proj,
addDOC = FALSE,
width = 4,
height = 4
)
```
## Plot sample statistics for the ArchRProject
```{r sample statistics plots, message=FALSE, warning=FALSE}
# 1. Show TSS enrichment score for each sample in a ridge plot.
# The TSS enrichment score is a proxy for the quality of the sample and should
# be similar in all samples in the dataset.
p1 = plotGroups(
ArchRProj = proj,
groupBy = "Sample",
colorBy = "cellColData",
name = "TSSEnrichment",
plotAs = "ridges"
)
p1
# 2. You can also display TSS enrichment scores in a violin plot.
# Lower and upper hinges correspond to the 25th and 75th percentiles, the middle
# corresponds to the median. Lower and upper whiskers extend from the hinge to
# the lowest or highest value or 1.5 times the IQR.
p2 = plotGroups(
ArchRProj = proj,
groupBy = "Sample",
colorBy = "cellColData",
name = "TSSEnrichment",
plotAs = "violin",
alpha = 0.4,
addBoxPlot = TRUE
)
p2
# 3. Show log10(unique nuclear fragments) for each sample in a ridge plot.
# It would be ideal if all of the samples have a similar number of fragments
# per cell. If this is not the case, you can downsample the files by randomly
# removing fragments from the larger sample(s) in the fragments.tsv.gz file.
p3 = plotGroups(
ArchRProj = proj,
groupBy = "Sample",
colorBy = "cellColData",
name = "log10(nFrags)",
plotAs = "ridges"
)
p3
# 4. You can also display log10(unique nuclear fragments) in a violin plot.
p4 = plotGroups(
ArchRProj = proj,
groupBy = "Sample",
colorBy = "cellColData",
name = "log10(nFrags)",
plotAs = "violin",
alpha = 0.4,
addBoxPlot = TRUE
)
p4
```
```{r save sample statistics plots, warning=FALSE, message=FALSE, echo=FALSE}
# Save plots as PDF
plotPDF(p1,p2,p3,p4,
name = "QC-Sample-Statistics.pdf",
ArchRProj = proj,
addDOC = FALSE,
width = 4,
height = 4
)
```
```{r sample statistics plots2, message=FALSE}
# 5. plot sample fragment size distributions:
# Ideally, you should see a nucleosome profile with enrichment in
# periodicity of 150 bp (length of DNA wrapped around one nucleosome)
p5 = plotFragmentSizes(ArchRProj = proj)
p5
# 6. plot TSS enrichment profiles:
p6 = plotTSSEnrichment(ArchRProj = proj)
p6
```
```{r save sample statistics plots2, warning=FALSE, message=FALSE, echo=FALSE}
# Save plots as PDF
plotPDF(p5,p6,
name = "QC-Sample-FragSizes-TSSProfile.pdf",
ArchRProj = proj,
addDOC = FALSE,
width = 5,
height = 5
)
```
# Dimensionality reduction
## Compute LSI dimensionality reduction
```{r dimensionality reduction, message=FALSE, warning=FALSE, eval=FALSE}
# LSI dimensionality reduction
# Create a dimensionality reduction of the data as basis for visualisation as UMAP
proj = addIterativeLSI(
ArchRProj = proj,
useMatrix = "TileMatrix",
name = "IterativeLSI",
iterations = 2,
clusterParams = list(
resolution = c(0.2),
sampleCells = 10000,
n.start = 10
),
varFeatures = 25000,
dimsToUse = 1:30,
force = TRUE
)
```
```{r clustering, message=FALSE, warning=FALSE, eval=FALSE}
# Clustering using the Louvain algorithm
# The Leiden algorithm can be using instead by passing “algorithm = 4”, which is
# an argument of Seurat’s FindClusters() function, to the addClusters() function
# (requires the leidenalg Python package)
proj = addClusters(
input = proj,
reducedDims = "IterativeLSI",
method = "Seurat",
name = "Clusters",
resolution = 0.8,
force = TRUE
)
```
## Visualization in UMAP embedding
```{r add UMAP embedding, message=FALSE, eval=FALSE}
# Visualization of clustering in UMAP embedding.
# This step is not deterministic, plots will look different to the figures in
# the manuscript
proj = addUMAP(
ArchRProj = proj,
reducedDims = "IterativeLSI",
name = "UMAP",
nNeighbors = 30,
minDist = 0.5,
metric = "cosine",
force = TRUE
)
```
```{r save ArchRProject after adding UMAP, warning=FALSE, message=FALSE, echo=FALSE, include=FALSE, eval=FALSE}
# Save ArchRProject
saveArchRProject(proj,
outputDirectory= "ArchRProject",
overwrite = TRUE,
load = FALSE
)
```
```{r visualization in UMAP, warning=FALSE, message=FALSE}
# Color by clusters
p_clusters = plotEmbedding(
ArchRProj = proj,
colorBy = "cellColData",
name = "Clusters",
embedding = "UMAP",
size = 0.5
)
# Color by samples
p_samples = plotEmbedding(
ArchRProj = proj,
colorBy = "cellColData",
name = "Sample",
embedding = "UMAP",
size = 0.5
)
# Plot
ggAlignPlots(p_clusters, p_samples, type = "h")
```
```{r save UMAP plots, warning=FALSE, message=FALSE, echo=FALSE}
# Save as PDF
plotPDF(p_clusters, p_samples,
name = "UMAP_clusters_samples.pdf",
ArchRProj = proj,
addDOC = FALSE,
width = 5,
height = 5
)
```
## Identifying marker genes
```{r get marker features, message=FALSE, warning=FALSE}
# Get marker features
# Determine marker genes of each cluster based on the gene score matrix.
# For this, each cluster is compared to all other clusters using wilcoxon
# rank-sum test.
markersGS = getMarkerFeatures(
ArchRProj = proj,
useMatrix = "GeneScoreMatrix",
groupBy = "Clusters",
bias = c("TSSEnrichment", "log10(nFrags)"),
testMethod = "wilcoxon"
)
```
```{r identifying marker genes grouped by clusters, message = TRUE, warning=FALSE}
# Define which genes to highlight
markerGenes = c("Cd4", "Il2ra", "Foxp3", "Batf", "Ccr8", "Ikzf2", "Rorc", "Tbx21", "Ifng", "Gata3", "Klrg1", "Il2")
# Get markers
markerList = getMarkers(markersGS, cutOff = "FDR <= 0.01 & Log2FC >= 1.25")
```
```{r overlay per-cell gene scores on UMAP embedding, message=FALSE, warning=FALSE}
# Overlay per-cell gene scores on our UMAP embedding
genes = plotEmbedding(
ArchRProj = proj,
colorBy = "GeneScoreMatrix",
name = markerGenes,
embedding = "UMAP",
plotAs = "points",
quantCut = c(0.01, 0.95),
imputeWeights = NULL,
size = 0.5
)
# To plot all genes we can use cowplot to arrange the various marker genes into
# a single plot
genes_cow = lapply(genes, function(x){
x + guides(color = FALSE, fill = FALSE) +
theme_ArchR(baseSize = 6.5) +
theme(plot.margin = unit(c(0, 0, 0, 0), "cm")) +
theme(
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank()
)
})
do.call(cowplot::plot_grid, c(list(ncol = 4), genes_cow))
```
```{r save genescores overlayed on UMAP, warning=FALSE, message=FALSE, echo=FALSE}
# Save an editable vectorized version of this plot
plotPDF(plotList = genes,
name = "Plot-UMAP-Marker-Genes_proj.pdf",
ArchRProj = proj,
addDOC = FALSE, width = 5, height = 5)
```
## Impute marker genes with MAGIC
```{r impute marker genes with MAGIC, message=FALSE, warning=FALSE}
# Some of the gene score overlays appear quite variable. This is due to the
# sparsity of scATAC-seq data. MAGIC can be used to impute gene scores by
# smoothing signal across nearby cells. This greatly improves the visual
# interpretation of gene scores.
#Wwe first add impute weights to our ArchRProject
proj = addImputeWeights(proj)
# These impute weights can then be passed to plotEmbedding() when plotting gene
# scores overlayed on the UMAP embedding
magic_genes = plotEmbedding(
ArchRProj = proj,
colorBy = "GeneScoreMatrix",
name = markerGenes,
embedding = "UMAP",
plotAs = "points",
imputeWeights = getImputeWeights(proj),
size = 0.5
)
#Plot all genes in cowplot
magic_genes_cow = lapply(magic_genes, function(x){
x + guides(color = FALSE, fill = FALSE) +
theme_ArchR(baseSize = 6.5) +
theme(plot.margin = unit(c(0, 0, 0, 0), "cm")) +
theme(
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank()
)
})
do.call(cowplot::plot_grid, c(list(ncol = 4), magic_genes_cow))
```
```{r save magic marker genes, warning=FALSE, message=FALSE, echo=FALSE}
#Save
plotPDF(plotList = magic_genes,
name = "Plot-UMAP-MAGIC-Marker-Genes_proj.pdf",
ArchRProj = proj,
addDOC = FALSE, width = 5, height = 5)
```
## Tweak different parameters of LSI dimensionality reduction
Now we tweak different parameters to show their effect on the results.
For this dataset we previously selected the defaults parameters. All of the
following is not saved and only for demonstration purposes. <br>
<br>
This should give you an idea of which parameters to tweak in your own data
analysis. <br>
### More iterations
```{r dimensionality reduction 4iterations, message=FALSE, warning=FALSE}
# LSI dimensionality reduction
proj_4iterations = addIterativeLSI(
ArchRProj = proj,
useMatrix = "TileMatrix",
name = "IterativeLSI",
iterations = 4,
clusterParams = list( #See Seurat::FindClusters
resolution = c(0.2),
sampleCells = 10000,
n.start = 10
),
varFeatures = 25000,
dimsToUse = 1:30,
force = TRUE
)
```
```{r clustering 4iterations, message=FALSE, warning=FALSE}
# Clustering
proj_4iterations = addClusters(
input = proj_4iterations,
reducedDims = "IterativeLSI",
method = "Seurat",
name = "Clusters",
resolution = 0.8,
force = TRUE
)
```
```{r add UMAP embedding 4iterations, message=FALSE}
# Visualization of clustering as UMAP
proj_4iterations = addUMAP(
ArchRProj = proj_4iterations,
reducedDims = "IterativeLSI",
name = "UMAP",
nNeighbors = 30,
minDist = 0.5,
metric = "cosine",
force = TRUE
)
```
```{r visualization in UMAP 4iterations, warning=FALSE, message=FALSE}
# Color by clusters
p_clusters_4iterations = plotEmbedding(
ArchRProj = proj_4iterations,
colorBy = "cellColData",
name = "Clusters",
embedding = "UMAP",
size = 0.5
)
# Color by samples
p_samples_4iterations = plotEmbedding(
ArchRProj = proj_4iterations,
colorBy = "cellColData",
name = "Sample",
embedding = "UMAP",
size = 0.5
)
# Plot
ggAlignPlots(p_clusters_4iterations, p_samples_4iterations, type = "h")
```
```{r save UMAP plots 4iterations, warning=FALSE, message=FALSE, echo=FALSE}
# Save as PDF
plotPDF(p_clusters_4iterations, p_samples_4iterations,
name = "UMAP_clusters_samples_4iterations.pdf",
ArchRProj = proj_4iterations,
addDOC = FALSE,
width = 5,
height = 5
)
```
```{r get marker features 4iterations, message=FALSE, warning=FALSE}
# Get marker features
markersGS_4iterations = getMarkerFeatures(
ArchRProj = proj_4iterations,
useMatrix = "GeneScoreMatrix",
groupBy = "Clusters",
bias = c("TSSEnrichment", "log10(nFrags)"),
testMethod = "wilcoxon"
)
```
```{r impute marker genes with MAGIC 4iterations, message=FALSE, warning=FALSE}
# Add impute weights
proj_4iterations = addImputeWeights(proj_4iterations)
# These impute weights can then be passed to plotEmbedding() when plotting gene
# scores overlayed on the UMAP embedding
magic_genes_4iterations = plotEmbedding(
ArchRProj = proj_4iterations,
colorBy = "GeneScoreMatrix",
name = markerGenes,
embedding = "UMAP",
plotAs = "points",
imputeWeights = getImputeWeights(proj_4iterations),
size = 0.5
)
# Plot all genes in cowplot
magic_genes_cow_4iterations = lapply(magic_genes_4iterations, function(x){
x + guides(color = FALSE, fill = FALSE) +
theme_ArchR(baseSize = 6.5) +
theme(plot.margin = unit(c(0, 0, 0, 0), "cm")) +
theme(
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank()
)
})
do.call(cowplot::plot_grid, c(list(ncol = 4), magic_genes_cow_4iterations))
```
```{r save magic marker genes 4iterations, warning=FALSE, message=FALSE, echo=FALSE}
# Save
plotPDF(plotList = magic_genes_4iterations,
name = "Plot-UMAP-MAGIC-Marker-Genes_proj_4iterations.pdf",
ArchRProj = proj_4iterations,
addDOC = FALSE, width = 5, height = 5)
```
### Less variable features
```{r dimensionality reduction varFeatures, message=FALSE, warning=FALSE}
# LSI dimensionality reduction
proj_varFeatures = addIterativeLSI(
ArchRProj = proj,
useMatrix = "TileMatrix",
name = "IterativeLSI",
iterations = 2,
clusterParams = list( #See Seurat::FindClusters
resolution = c(0.2),
sampleCells = 10000,
n.start = 10
),
varFeatures = 10000,
dimsToUse = 1:30,
force = TRUE
)
```
```{r clustering varFeatures, message=FALSE, warning=FALSE}
# Clustering
proj_varFeatures = addClusters(
input = proj_varFeatures,
reducedDims = "IterativeLSI",
method = "Seurat",
name = "Clusters",
resolution = 0.8,
force = TRUE
)
```
```{r add UMAP embedding varFeatures, message=FALSE}
# Visualization of clustering as UMAP
proj_varFeatures = addUMAP(
ArchRProj = proj_varFeatures,
reducedDims = "IterativeLSI",
name = "UMAP",
nNeighbors = 30,
minDist = 0.5,
metric = "cosine",
force = TRUE
)
```
```{r visualization in UMAP varFeatures, warning=FALSE, message=FALSE}
# Color by clusters
p_clusters_varFeatures = plotEmbedding(
ArchRProj = proj_varFeatures,
colorBy = "cellColData",
name = "Clusters",
embedding = "UMAP",
size = 0.5
)
# Color by samples
p_samples_varFeatures = plotEmbedding(
ArchRProj = proj_varFeatures,
colorBy = "cellColData",
name = "Sample",
embedding = "UMAP",
size = 0.5
)
# Plot
ggAlignPlots(p_clusters_varFeatures, p_samples_varFeatures, type = "h")
```
```{r save UMAP plots varFeatures, warning=FALSE, message=FALSE, echo=FALSE}
# Save as PDF
plotPDF(p_clusters_varFeatures, p_samples_varFeatures,
name = "UMAP_clusters_sample_varFeaturess.pdf",
ArchRProj = proj_varFeatures,
addDOC = FALSE,
width = 5,
height = 5
)
```
```{r get marker features varFeatures, message=FALSE, warning=FALSE}
# Get marker features
markersGS_varFeatures = getMarkerFeatures(
ArchRProj = proj_varFeatures,
useMatrix = "GeneScoreMatrix",
groupBy = "Clusters",
bias = c("TSSEnrichment", "log10(nFrags)"),
testMethod = "wilcoxon"
)
```
```{r impute marker genes with MAGIC varFeatures, message=FALSE, warning=FALSE}
# Add impute weights
proj_varFeatures = addImputeWeights(proj_varFeatures)
# These impute weights can then be passed to plotEmbedding() when plotting gene
# scores overlayed on the UMAP embedding
magic_genes_varFeatures = plotEmbedding(
ArchRProj = proj_varFeatures,
colorBy = "GeneScoreMatrix",
name = markerGenes,
embedding = "UMAP",
plotAs = "points",
imputeWeights = getImputeWeights(proj_varFeatures),
size = 0.5
)
# Plot all genes in cowplot
magic_genes_cow_varFeatures = lapply(magic_genes_varFeatures, function(x){
x + guides(color = FALSE, fill = FALSE) +
theme_ArchR(baseSize = 6.5) +
theme(plot.margin = unit(c(0, 0, 0, 0), "cm")) +
theme(
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank()
)
})
do.call(cowplot::plot_grid, c(list(ncol = 4), magic_genes_cow_varFeatures))
```
```{r save magic marker genes varFeatures, warning=FALSE, message=FALSE, echo=FALSE}
# Save
plotPDF(plotList = magic_genes_varFeatures,
name = "Plot-UMAP-MAGIC-Marker-Genes_proj_varFeatures.pdf",
ArchRProj = proj_varFeatures,
addDOC = FALSE, width = 5, height = 5)
```
### dimsToUse 2:30
```{r dimensionality reduction dimsToUse, message=FALSE, warning=FALSE}
# LSI dimensionality reduction
proj_dimsToUse = addIterativeLSI(
ArchRProj = proj,
useMatrix = "TileMatrix",
name = "IterativeLSI",
iterations = 2,
clusterParams = list( #See Seurat::FindClusters
resolution = c(0.2),
sampleCells = 10000,
n.start = 10
),
varFeatures = 25000,
dimsToUse = 2:30,
force = TRUE
)
```
```{r clustering dimsToUse, message=FALSE, warning=FALSE}
# Clustering
proj_dimsToUse = addClusters(
input = proj_dimsToUse,
reducedDims = "IterativeLSI",
method = "Seurat",
name = "Clusters",
resolution = 0.8,
force = TRUE
)
```
```{r add UMAP embedding dimsToUse, message=FALSE}
# Visualization of clustering as UMAP
proj_dimsToUse = addUMAP(
ArchRProj = proj_dimsToUse,
reducedDims = "IterativeLSI",
name = "UMAP",
nNeighbors = 30,
minDist = 0.5,
metric = "cosine",
force = TRUE
)
```
```{r visualization in UMAP dimsToUse, warning=FALSE, message=FALSE}
# Color by clusters
p_clusters_dimsToUse = plotEmbedding(
ArchRProj = proj_dimsToUse,
colorBy = "cellColData",
name = "Clusters",
embedding = "UMAP",
size = 0.5
)
# Color by samples
p_samples_dimsToUse = plotEmbedding(
ArchRProj = proj_dimsToUse,
colorBy = "cellColData",
name = "Sample",
embedding = "UMAP",
size = 0.5
)
# Plot
ggAlignPlots(p_clusters_dimsToUse, p_samples_dimsToUse, type = "h")
```
```{r save UMAP plots dimsToUse, warning=FALSE, message=FALSE, echo=FALSE}
# Save as PDF
plotPDF(p_clusters_dimsToUse, p_samples_dimsToUse,
name = "UMAP_clusters_samples_dimsToUse.pdf",
ArchRProj = proj_dimsToUse,
addDOC = FALSE,
width = 5,
height = 5
)
```
```{r get marker features dimsToUse, message=FALSE, warning=FALSE}
# Get marker features
markersGS_dimsToUse = getMarkerFeatures(
ArchRProj = proj_dimsToUse,
useMatrix = "GeneScoreMatrix",
groupBy = "Clusters",
bias = c("TSSEnrichment", "log10(nFrags)"),
testMethod = "wilcoxon"
)
```
```{r impute marker genes with MAGIC dimsToUse, message=FALSE, warning=FALSE}
# Add impute weights
proj_dimsToUse = addImputeWeights(proj_dimsToUse)
# These impute weights can then be passed to plotEmbedding() when plotting gene
# scores overlayed on the UMAP embedding
magic_genes_dimsToUse = plotEmbedding(
ArchRProj = proj_dimsToUse,
colorBy = "GeneScoreMatrix",
name = markerGenes,
embedding = "UMAP",
plotAs = "points",
imputeWeights = getImputeWeights(proj_dimsToUse),
size = 0.5
)
# Plot all genes in cowplot
magic_genes_cow_dimsToUse = lapply(magic_genes_dimsToUse, function(x){
x + guides(color = FALSE, fill = FALSE) +
theme_ArchR(baseSize = 6.5) +
theme(plot.margin = unit(c(0, 0, 0, 0), "cm")) +
theme(
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank()
)
})
do.call(cowplot::plot_grid, c(list(ncol = 4), magic_genes_cow_dimsToUse))
```
```{r save magic marker genes dimsToUse, warning=FALSE, message=FALSE, echo=FALSE}
# Save
plotPDF(plotList = magic_genes_dimsToUse,
name = "Plot-UMAP-MAGIC-Marker-Genes_proj_dimsToUse.pdf",
ArchRProj = proj_dimsToUse,
addDOC = FALSE, width = 5, height = 5)
```
```{r add UMAP proj, message=FALSE}
# Visualisation of clustering as UMAP
proj = addUMAP(
ArchRProj = proj,
reducedDims = "IterativeLSI",
name = "UMAP",
nNeighbors = 30,
minDist = 0.5,
metric = "cosine",
force = TRUE
)
```
```{r save ArchRProject, message=FALSE, warning=FALSE, echo=FALSE, include=FALSE}
# Save ArchRProject
saveArchRProject(proj,
outputDirectory= "ArchRProject",
load = FALSE
)
```
```{r visualization in UMAP embedding proj, message=FALSE}
# Color by clusters
p_clusters = plotEmbedding(
ArchRProj = proj,
colorBy = "cellColData",
name = "Clusters",
embedding = "UMAP",
size = 0.5
)
# Color by samples
p_samples = plotEmbedding(
ArchRProj = proj,
colorBy = "cellColData",
name = "Sample",
embedding = "UMAP",
size = 0.5
)
# Plot
ggAlignPlots(p_clusters, p_samples, type = "h")
```
```{r save UMAP plots after removing contamination, warning=FALSE, message=FALSE, echo=FALSE}
# Save as PDF
plotPDF(p_clusters, p_samples,
name = "UMAP_clusters_samples.pdf",
ArchRProj = proj,
addDOC = FALSE,
width = 5,
height = 5
)
```
## Filtering out barcodes marked as non-cell by CellRanger
```{r evaluate cell barcodes, message=FALSE, warning=FALSE}
# We use additional Cell Ranger information to filter out low-quality cells.
# For this we use the "is__cell_barcode" column of the "singlecell.csv" file,
# which maps high-quality cells to 1.
singlecell = list()
for (x in c("1","4","5","8","9")){
# Define filenames and read files
filename = paste("Data/MD_scATAC_",x,"/singlecell.csv",sep = "")
data = read.csv(filename)
# To match quality information to cells, we need the barcodes to match the
# ones in our ArchRProject. For this we:
# 1) create a vector of "scATAC_x#"
# 2) add vector as a column to the data
# 3) create column containing ArchRProj-style barcodes
bc = c(rep(paste("scATAC_",x,"#",sep = ""),nrow(data)))
data_barcode = cbind(bc,data)
data_fullbc = data_barcode %>% unite("full_barcode", bc:barcode, remove = FALSE, sep = "")
singlecell[[x]] = data_fullbc
}
# Combine the dataframes
singlecell_fullbc = rbindlist(singlecell, use.names = FALSE, fill = FALSE)
# Extract rownames that are also in the ArchRProject
rownames_archr = rownames(proj@cellColData)
subset_singlecell_fullbc = singlecell_fullbc[singlecell_fullbc$full_barcode %in% rownames_archr, ]
# Extract is__cell_barcode column from singlecell.csv and give it barcodes as rownames
df_is_cell_barcode = as.data.frame(subset_singlecell_fullbc$is__cell_barcode)
rownames(df_is_cell_barcode) = subset_singlecell_fullbc$full_barcode
# Order is_cell_barcode the way the ArchRProject is ordered and create filter
is_cell_barcode = df_is_cell_barcode[order(match(rownames(df_is_cell_barcode), rownames_archr)), ]
filter_archr = is_cell_barcode==1
```
```{r highlight cells to be filtered out on UMAP, warning=FALSE, message=FALSE}
# Create column in the ArchRProject containing is_cell_barcode information
proj@cellColData$is_cell_barcode = as.character(is_cell_barcode)
# Plot
UMAP_cellranger_filter = plotEmbedding(
ArchRProj = proj,
colorBy = "cellColData",
name = "is_cell_barcode",
embedding = "UMAP",
size = 0.5,
plotAs = "points",
pal = c("1" = "lightgrey", "0" = "#D51F26")
)
UMAP_cellranger_filter
```
```{r filter for cell barcodes, message=FALSE, warning=FALSE, eval=FALSE}
# Filter ArchRProject for is_cell_barcode==1
proj = proj[filter_archr, ]
```
```{r save ArchRProject after filtering for cell barcodes, warning=FALSE, message=FALSE, echo=FALSE, include=FALSE}
saveArchRProject(proj,
outputDirectory= "ArchRProject_filtered",
overwrite = TRUE,
load = FALSE
)
```
```{r load ArchRProject 5, echo=T, results='hide', message=FALSE, include=FALSE}
# Load ArchRProject
proj = loadArchRProject(
path = "ArchRProject_filtered",
force = TRUE,
showLogo = FALSE
)
```
# Filter doublets
## Calculate doublet scores
```{r calculate doublet scores, message=FALSE, warning=FALSE, results='hide', eval=FALSE}
# Calculate doublet scores on the ArchRProject
proj = addDoubletScores(
input = proj,
k = 10, #Refers to how many cells near a "pseudo-doublet" to count
knnMethod = "UMAP", #Refers to the embedding to use for nearest neighbor search
LSIMethod = 1,
force = TRUE
)
```
```{r save ArchRProject after adding doublet scores, message=FALSE, warning=FALSE, echo=FALSE, include=FALSE}
# Save ArchRProject
saveArchRProject(proj,
outputDirectory= "ArchRProject_filtered",
overwrite = TRUE
)
```
## Test different doublet filter ratios
In the next step, we evaluate different filter ratios. The filter ratio
determines how many cells will be filtered out as doublets. Doublets scores were
calculated above.