-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge_ATAC_samples_auto.v.3.0_mouse_liver.R
More file actions
164 lines (130 loc) · 4.54 KB
/
Copy pathMerge_ATAC_samples_auto.v.3.0_mouse_liver.R
File metadata and controls
164 lines (130 loc) · 4.54 KB
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
#####author: Nadezhda V. Terekhanova
### modified by Alla
#####2020-09-26: v.3.0 - this version of the script was adapted for the latest Signac v.1.0.0
###do prior to running the script `export TMPDIR=/diskmnt/Projects/Users/$USER/mytmp` - otherwise it crushes
library(Signac)
library(Seurat)
library(GenomeInfoDb)
library(ggplot2)
library(RColorBrewer)
library(optparse)
require(magrittr)
require(readr)
require(Matrix)
require(tidyr)
require(dplyr)
set.seed(1234)
library(plyr)
library(dplyr)
library(tibble)
library(reshape)
library(plyr)
library(EnsDb.Mmusculus.v79)
library(GenomicRanges)
library(future)
option_list = list(
make_option(c("-i", "--input.folder"),
type="character",
default=NULL,
help="path to folder with rds objects",
metavar="character"),
# make_option(c("-m", "--metadata"),
# type="character",
# default=NULL,
# help="output folder path",
# metavar="character"),
make_option(c("-o", "--output"),
type="character",
default="./",
help="output folder path",
metavar="character"),
make_option(c("-e", "--extra"),
type="character",
default="./",
help="add unique string identifier for your data",
metavar="character")
);
opt_parser = OptionParser(option_list=option_list);
opt = parse_args(opt_parser);
# read in initial arguments
input.path <- opt$input.folder
out_path <- opt$output
add_filename <- opt$extra
setwd(out_path)
###some parallelization-solution from the tutorial:
plan("multiprocess", workers = 20)
options(future.globals.maxSize = 100000 * 1024^2) # for 50 Gb RAM
###### CHANGE THIS!!!!!! ########
samples=c('SM002H1-Md', 'SM001H1-Md')
#########
# make the list of atac objects
atac=vector(mode = "list", length = length(samples))
for (i in 1:length(samples)){
atac[[i]]=readRDS(list.files(path = input.path, full.names = T, pattern = paste0(samples[i],'.*rds'), all.files = T, recursive = T))
DefaultAssay(atac[[i]]) <- 'peaks'
}
#####To obtain the best results - use ALL peaks!
combined.peaks <- UnifyPeaks(object.list = atac, mode = "reduce")
peakwidths <- width(combined.peaks)
combined.peaks <- combined.peaks[peakwidths < 10000 & peakwidths > 20]
combined.peaks
peaks.use=combined.peaks
#For testing purposes only:
#peaks.use=sample(combined.peaks, size = 5000, replace = FALSE)
#We don't filter cells like in the tutorial, because we use already filtered matrices. And all cells are pass those filters in the tutorial.
matrix.counts=vector(mode = "list", length = length(samples))
for (i in 1:length(samples)){
matrix.counts[[i]] <- FeatureMatrix(
fragments = Fragments(atac[[i]]@assays$peaks),
features = peaks.use,
sep = c("-","-"),
cells = colnames(atac[[i]])
)
}
for (i in 1:length(samples)){
atac[[i]][['peaksinters']] <- CreateChromatinAssay(counts = matrix.counts[[i]],fragments=Fragments(atac[[i]]@assays$peaks))
atac[[i]]$dataset=samples[i]
DefaultAssay(atac[[i]])<-'peaksinters'
}
####Merging:
combined <- merge(x = atac[[1]], y = atac[2:length(samples)], add.cell.ids = samples)
DefaultAssay(combined) <- "peaksinters"
combined <- RunTFIDF(combined)
combined <- FindTopFeatures(combined, min.cutoff = 20)
combined <- RunSVD(
combined,
reduction.key = 'LSI_',
reduction.name = 'lsi',
irlba.work = 400
)
combined <- RunUMAP(combined, dims = 2:50, reduction = 'lsi')
combined <- FindNeighbors(
object = combined,
reduction = 'lsi',
dims = 2:50
)
combined <- FindClusters(
object = combined,
algorithm = 3,
resolution = 1.2,
verbose = FALSE
)
cat('saving the object...\n')
saveRDS(combined, paste0(length(samples),"_snATAC_Merged_",add_filename,".rds"))
cat('plotting...\n')
p1 <- DimPlot(combined, group.by = 'dataset', pt.size = 0.1) +
ggplot2::ggtitle("Combined snATAC samples")+
scale_color_manual(values=c(brewer.pal(n = 12, name = "Paired"),"grey"))
p2 <- DimPlot(combined, pt.size = 0.1,label=T)
pdf(paste0(length(samples),"_snATAC_Merged_", add_filename, ".pdf"),height=6,width=16, useDingbats = F)
p1+p2
dev.off()
write.table(samples,paste0("Samples_snATAC_Merged_",add_filename,".txt"),sep="\t",quote=FALSE)
###Run ChromVar using RunChromVar.R
p1 <- DimPlot(atac, group.by = 'dataset', pt.size = 0.1) +
ggplot2::ggtitle("Combined snATAC samples")+
scale_color_manual(values=c(brewer.pal(n = 12, name = "Paired"),"grey"))
p2 <- DimPlot(atac, pt.size = 0.1,label=T)
pdf(paste0(name.file, ".pdf"),height=6,width=16, useDingbats = F)
p1+p2
dev.off()