-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPeaks_ArchR_windows.R
More file actions
631 lines (527 loc) · 24 KB
/
Peaks_ArchR_windows.R
File metadata and controls
631 lines (527 loc) · 24 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
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
addReproduciblePeakSet_win <- function(
ArchRProj = NULL,
groupBy = "Clusters",
peakMethod = "Macs2",
reproducibility = "2",
peaksPerCell = 500,
maxPeaks = 150000,
minCells = 25,
excludeChr = c("chrM","chrY"),
pathToMacs2 = if(tolower(peakMethod)=="macs2") findMacs2() else NULL,
genomeSize = NULL,
shift = -75,
extsize = 150,
method = if(tolower(peakMethod)=="macs2") "q" else "p", #P-Method for Tiles Results Better Agree w/ Macs2
cutOff = 0.1,
additionalParams = "--nomodel --nolambda",
extendSummits = 250,
promoterRegion = c(2000, 100),
genomeAnnotation = getGenomeAnnotation(ArchRProj),
geneAnnotation = getGeneAnnotation(ArchRProj),
plot = TRUE,
threads = getArchRThreads(),
parallelParam = NULL,
force = FALSE,
verbose = TRUE,
logFile = createLogFile("addReproduciblePeakSet"),
...
){
ArchR:::.validInput(input = ArchRProj, name = "ArchRProj", valid = c("ArchRProj"))
ArchR:::.validInput(input = groupBy, name = "groupBy", valid = c("character"))
ArchR:::.validInput(input = reproducibility, name = "reproducibility", valid = c("character"))
ArchR:::.validInput(input = peaksPerCell, name = "peaksPerCell", valid = c("integer"))
ArchR:::.validInput(input = maxPeaks, name = "maxPeaks", valid = c("integer"))
ArchR:::.validInput(input = minCells, name = "minCells", valid = c("integer"))
ArchR:::.validInput(input = excludeChr, name = "excludeChr", valid = c("character", "null"))
if(tolower(peakMethod) == "macs2"){
#ArchR:::.validInput(input = pathToMacs2, name = "pathToMacs2", valid = c("character"))
ArchR:::.validInput(input = genomeSize, name = "genomeSize", valid = c("character", "numeric", "null"))
ArchR:::.validInput(input = shift, name = "shift", valid = c("integer"))
ArchR:::.validInput(input = extsize, name = "extsize", valid = c("integer"))
ArchR:::.validInput(input = method, name = "method", valid = c("character"))
ArchR:::.validInput(input = additionalParams, name = "additionalParams", valid = c("character"))
ArchR:::.validInput(input = extendSummits, name = "extendSummits", valid = c("integer"))
#.checkMacs2Options(pathToMacs2) #Check Macs2 Version
}else if(tolower(peakMethod) == "tiles"){
}else{
stop("peakMethod not recognized! Supported peakMethods are Macs2 or Tiles!")
}
ArchR:::.validInput(input = cutOff, name = "cutOff", valid = c("numeric"))
ArchR:::.validInput(input = promoterRegion, name = "promoterRegion", valid = c("integer"))
geneAnnotation <- ArchR:::.validGeneAnnotation(geneAnnotation)
genomeAnnotation <- ArchR:::.validGenomeAnnotation(genomeAnnotation)
geneAnnotation <- ArchR:::.validGeneAnnoByGenomeAnno(geneAnnotation = geneAnnotation, genomeAnnotation = genomeAnnotation)
ArchR:::.validInput(input = plot, name = "plot", valid = c("boolean"))
ArchR:::.validInput(input = threads, name = "threads", valid = c("integer"))
ArchR:::.validInput(input = parallelParam, name = "parallelParam", valid = c("parallelparam", "null"))
ArchR:::.validInput(input = force, name = "force", valid = c("boolean"))
ArchR:::.validInput(input = verbose, name = "verbose", valid = c("boolean"))
ArchR:::.validInput(input = logFile, name = "logFile", valid = c("character"))
tstart <- Sys.time()
ArchR:::.startLogging(logFile = logFile)
ArchR:::.logThis(mget(names(formals()),sys.frame(sys.nframe())), "ReproduciblePeakSet Args", logFile=logFile)
if(tolower(peakMethod) == "macs2"){
ArchR:::.logMessage("Calling Peaks with Macs2", logFile = logFile)
#utility <- ArchR:::.checkPath(pathToMacs2)
coverageMetadata <- ArchR:::.getCoverageMetadata(ArchRProj = ArchRProj, groupBy = groupBy, minCells = minCells)
coverageParams <- ArchR:::.getCoverageParams(ArchRProj = ArchRProj, groupBy = groupBy)
#####################################################
# Peak Calling Summary
#####################################################
tableGroups <- table(getCellColData(ArchRProj, groupBy, drop = TRUE))
groupSummary <- lapply(seq_along(coverageParams$cellGroups), function(y){
x <- coverageParams$cellGroups[[y]]
uniq <- unique(unlist(x))
n <- lapply(x, length) %>% unlist %>% sum
nmin <- lapply(x, length) %>% unlist %>% min
nmax <- lapply(x, length) %>% unlist %>% max
data.frame(
Group=names(coverageParams$cellGroups)[y],
nCells=tableGroups[names(coverageParams$cellGroups)[y]],
nCellsUsed=length(uniq),
nReplicates=length(x),
nMin=nmin,
nMax=nmax,
maxPeaks = min(maxPeaks, length(uniq) * peaksPerCell)
)
}) %>% Reduce("rbind",.)
ArchR:::.logDiffTime("Peak Calling Parameters!", tstart, verbose = verbose, logFile = logFile)
ArchR:::.logThis(groupSummary, "PeakCallSummary", logFile = logFile)
if(verbose) print(groupSummary)
#####################################################
# Create Output Directory
#####################################################
outDir <- file.path(getOutputDirectory(ArchRProj), "PeakCalls")
outSubDir <- file.path(getOutputDirectory(ArchRProj), "PeakCalls", "ReplicateCalls")
outBedDir <- file.path(getOutputDirectory(ArchRProj), "PeakCalls", "InsertionBeds")
dir.create(outDir, showWarnings = FALSE)
dir.create(outSubDir, showWarnings = FALSE)
dir.create(outBedDir, showWarnings = FALSE)
#####################################################
# Genome Size Presets
#####################################################
if(is.null(genomeSize)){
if(grepl("hg19|hg38", getGenome(ArchRProj), ignore.case = TRUE)){
genomeSize <- 2.7e9
}else if(grepl("mm9|mm10", getGenome(ArchRProj), ignore.case = TRUE)){
genomeSize <- 1.87e9
}
}
#####################################################
# Arguments for Peak Calling
#####################################################
coverageFiles <- coverageMetadata$File
names(coverageFiles) <- coverageMetadata$Name
args <- list()
args$X <- seq_len(nrow(coverageMetadata))
args$FUN <- callSummitsOnCoverages_win
args$coverageFiles <- coverageFiles
args$outFiles <- file.path(outSubDir, paste0(make.names(coverageMetadata$Name),"-summits.rds"))
args$bedDir <- outBedDir
args$excludeChr <- excludeChr
args$peakParams <- list(
pathToMacs2 = pathToMacs2,
genomeSize = genomeSize,
shift = shift,
extsize = extsize,
cutOff = cutOff,
method = method,
additionalParams = additionalParams
)
args$parallelParam <- parallelParam
args$threads <- threads
args$logFile <- logFile
args$registryDir <- file.path(outDir, "batchRegistry")
#####################################################
# Batch Call Peaks
#####################################################
ArchR:::.logDiffTime("Batching Peak Calls!", tstart, verbose = verbose, logFile = logFile)
ArchR:::.logThis(args, "PeakArgs", logFile = logFile)
#back lapply
outSummmits <- unlist(ArchR:::.batchlapply(args))
#Summarize Output
outSummitList <- split(outSummmits, coverageMetadata$Group)
summitNamesList <- split(coverageMetadata$Name, coverageMetadata$Group)
#####################################################
# BSgenome for Add Nucleotide Frequencies!
#####################################################
ArchR:::.requirePackage(genomeAnnotation$genome)
ArchR:::.requirePackage("Biostrings",source="bioc")
BSgenome <- eval(parse(text = genomeAnnotation$genome))
BSgenome <- validBSgenome(BSgenome)
#####################################################
# Identify Reproducible Peaks!
#####################################################
ArchR:::.logDiffTime("Identifying Reproducible Peaks!", tstart, verbose = verbose, logFile = logFile)
groupPeaks <- ArchR:::.safelapply(seq_along(outSummitList), function(i){
prefix <- sprintf("Rep. Peaks Group (%s of %s) :", i, length(outSummitList))
ArchR:::.logDiffTime(sprintf("%s Creating Reproducible Peaks", prefix), tstart, verbose = FALSE, logFile = logFile)
peaks <- suppressMessages(ArchR:::.identifyReproduciblePeaks(
summitFiles = outSummitList[[i]],
summitNames = summitNamesList[[i]],
reproducibility = reproducibility,
extendSummits = extendSummits,
blacklist = genomeAnnotation$blacklist,
prefix = prefix,
logFile = logFile
))
ArchR:::.logDiffTime(sprintf("%s Annotating and Filtering Peaks", prefix), tstart, verbose = FALSE, logFile = logFile)
peaks <- sort(sortSeqlevels(peaks))
peaks <- subsetByOverlaps(peaks, genomeAnnotation$chromSizes, type = "within")
peaks <- ArchR:::.fastAnnoPeaks(peaks, BSgenome = BSgenome, geneAnnotation = geneAnnotation, promoterRegion = promoterRegion, logFile = logFile)
peaks <- peaks[which(mcols(peaks)$N < 0.001)] #Remove N Containing Peaks
peaks <- peaks[order(peaks$groupScoreQuantile, decreasing = TRUE)]
peaks <- head(peaks, groupSummary[names(outSummitList)[i],"maxPeaks"])
mcols(peaks)$N <- NULL #Remove N Column
print(file.path(outDir, paste0(make.names(names(outSummitList)[i]), "-reproduciblePeaks.gr.rds")))
saveRDS(peaks, file.path(outDir, paste0(make.names(names(outSummitList)[i]), "-reproduciblePeaks.gr.rds")))
return(peaks)
}, threads = threads) %>% GRangesList()
names(groupPeaks) <- names(outSummitList)
#Construct Union Peak Set
ArchR:::.logDiffTime("Creating Union Peak Set!", tstart, verbose = verbose, logFile = logFile)
unionPeaks <- unlist(groupPeaks)
unionPeaks <- nonOverlappingGR(unionPeaks, by = "groupScoreQuantile", decreasing = TRUE)
#Summarize Output
peakDF <- lapply(seq_along(groupPeaks), function(x){
data.frame(Group = names(groupPeaks)[x], table(groupPeaks[[x]]$peakType))
}) %>% Reduce("rbind", .)
peakDF$Group <- paste0(peakDF$Group, "(n = ", tableGroups[peakDF$Group],")")
peakDF <- rbind(data.frame(Group = "UnionPeaks", table(unionPeaks$peakType)), peakDF)
peakDF$Freq <- peakDF$Freq / 1000
metadata(unionPeaks)$PeakCallSummary <- peakDF
}else if(tolower(peakMethod) == "tiles"){
ArchR:::.logMessage("Calling Peaks with TileMatrix. We recommend using the Macs2 Version.\nThis method is still under development.", logFile = logFile)
useMatrix <- "TileMatrix"
cellGroups <- addGroupCoverages(
ArchRProj = ArchRProj,
groupBy = groupBy,
returnGroups = TRUE,
minCells = minCells,
logFile = logFile,
...
)[[1]]$cellGroups
if(verbose) print(cellGroups)
#####################################################
# Peak Calling Summary
#####################################################
tableGroups <- table(getCellColData(ArchRProj, groupBy, drop = TRUE))
groupSummary <- lapply(seq_along(cellGroups), function(y){
x <- cellGroups[[y]]
uniq <- unique(unlist(x))
n <- lapply(x, length) %>% unlist %>% sum
nmin <- lapply(x, length) %>% unlist %>% min
nmax <- lapply(x, length) %>% unlist %>% max
data.frame(
Group=names(cellGroups)[y],
nCells=tableGroups[names(cellGroups)[y]],
nCellsUsed=length(uniq),
nReplicates=length(x),
nMin=nmin,
nMax=nmax,
maxPeaks = min(maxPeaks, length(uniq) * peaksPerCell)
)
}) %>% Reduce("rbind",.)
ArchR:::.logDiffTime("Peak Calling Parameters!", tstart, verbose = verbose, logFile = logFile)
ArchR:::.logThis(groupSummary, "PeakCallSummary", logFile = logFile)
#printSummary <- groupSummary
#rownames(printSummary) <- NULL
if(verbose) print(groupSummary)
#####################################################
# Peak Calling from TileMatrix
#####################################################
#MatrixFiles
ArrowFiles <- getSampleColData(ArchRProj)[,"ArrowFiles"]
chrToRun <- ArchR:::.availableSeqnames(ArrowFiles, subGroup = useMatrix)
featureDF <- ArchR:::.getFeatureDF(ArrowFiles, useMatrix)
#Determine Resolution
d1 <- featureDF$start[2] - featureDF$start[1]
d2 <- featureDF$start[3] - featureDF$start[2]
if(d1 != d2){
ArchR:::.logMessage("Something is wrong with TileMatrix, Could not determine a resolution!", logFile = logFile)
stop("Something is wrong with TileMatrix, Could not determine a resolution!")
}else{
res <- d1
}
#Compute Row Sums Across All Samples
ArchR:::.logDiffTime("Computing Total Accessibility Across All Features", tstart, addHeader = FALSE, verbose = verbose)
totalAcc <- ArchR:::.getRowSums(ArrowFiles = ArrowFiles, useMatrix = useMatrix, seqnames = chrToRun)
ArchR:::.logThis(totalAcc, "PeakCallTiles-totalAcc", logFile=logFile)
nTiles <- nrow(totalAcc)
gc()
#Pre-Filter 0s
topFeatures <- totalAcc[which(totalAcc$rowSums != 0), ]
ArchR:::.logThis(topFeatures, "PeakCallTiles-topFeatures", logFile=logFile)
#Group Matrix
#Consider reading in group-wise if this is getting too large?
ArchR:::.logDiffTime("Computing Pseudo-Grouped Tile Matrix", tstart, addHeader = FALSE, verbose = verbose)
groupMat <- ArchR:::.getGroupMatrix(
ArrowFiles = ArrowFiles,
featureDF = topFeatures,
useMatrix = useMatrix,
threads = threads,
groupList = unlist(cellGroups),
useIndex = FALSE,
asSparse = TRUE,
verbose = FALSE
)
ArchR:::.logThis(groupMat, "PeakCallTiles-groupMat", logFile=logFile)
ArchR:::.logDiffTime(sprintf("Created Pseudo-Grouped Tile Matrix (%s GB)", round(object.size(groupMat) / 10^9, 3)), tstart, addHeader = FALSE, verbose = verbose)
expectation <- Matrix::colSums(groupMat) / nTiles
ArchR:::.logMessage(paste0("colSums = ", Matrix::colSums(groupMat)), logFile = logFile)
ArchR:::.logMessage(paste0("nTiles = ", nTiles), logFile = logFile)
ArchR:::.logMessage(paste0("Expectation = ", expectation), logFile = logFile)
#####################################################
# Peak Calling Per Group
#####################################################
groupPeaks <- ArchR:::.safelapply(seq_along(cellGroups), function(i){
ArchR:::.logDiffTime(sprintf("Computing Peaks from Tile Matrix Group (%s of %s)", i, length(cellGroups)), tstart, addHeader = FALSE, verbose = verbose)
gx <- grep(paste0(names(cellGroups)[i],"."), colnames(groupMat))
pMat <- lapply(seq_along(gx), function(x){
pval <- ppois(q = groupMat[,gx[x]]-1, lambda = expectation[gx[x]], lower.tail = FALSE, log=FALSE)
if(tolower(method) == "q"){
pval <- p.adjust(pval, method = "fdr", n = nTiles)
}else if(tolower(method)=="p"){
}else{
ArchR:::.logMessage("method should be either p for p-value or q for adjusted p-value!", logFile = logFile)
stop("method should be either p for p-value or q for adjusted p-value!")
}
as(as.matrix(-log10(pmax(pval, 10^-250))), "dgCMatrix")
}) %>% Reduce("cbind", .)
n <- ncol(pMat)
passPeaks <- Matrix::rowSums(pMat >= -log10(cutOff)) >= eval(parse(text=reproducibility))
mlogp <- Matrix::rowSums(Matrix::t(Matrix::t(pMat) / Matrix::colSums(pMat)) * 10^6) / ncol(pMat)
rm(pMat)
gc()
passPeaks <- passPeaks[order(mlogp, decreasing=TRUE)]
mlogp <- mlogp[order(mlogp, decreasing=TRUE)]
nMax <- groupSummary[names(cellGroups)[i], "maxPeaks"]
passPeaks <- head(passPeaks, nMax)
mlogp <- head(mlogp, nMax)
mlogp <- mlogp[which(passPeaks)]
passPeaks <- passPeaks[which(passPeaks)]
if(length(passPeaks) > 0){
DataFrame(
Group = Rle(names(cellGroups)[i]),
peaks = names(passPeaks),
mlog10p = mlogp,
normmlogp = 10^6 * mlogp / sum(mlogp)
)
}else{
NULL
}
}, threads = threads) %>% Reduce("rbind", .)
ArchR:::.logThis(groupPeaks, "PeakCallTiles-groupPeaks", logFile=logFile)
groupPeaks <- groupPeaks[order(groupPeaks$normmlogp, decreasing=TRUE), ]
#####################################################
# BSgenome for Add Nucleotide Frequencies!
#####################################################
ArchR:::.requirePackage(genomeAnnotation$genome)
ArchR:::.requirePackage("Biostrings",source="bioc")
BSgenome <- eval(parse(text = genomeAnnotation$genome))
BSgenome <- validBSgenome(BSgenome)
outDir <- file.path(getOutputDirectory(ArchRProj), "PeakCalls")
#####################################################
# Create Group Peaks
#####################################################
ArchR:::.logDiffTime("Creating Group Peak Sets with Annotations!", tstart, verbose = verbose, logFile = logFile)
peakDF <- ArchR:::.safelapply(seq_along(cellGroups), function(i){
groupPeaksi <- groupPeaks[groupPeaks$Group == names(cellGroups)[i], ]
if(nrow(groupPeaksi) == 0){
return(NULL)
}
groupPeaksi <- cbind(
topFeatures[as.integer(gsub("f", "", groupPeaksi$peaks)),],
groupPeaksi
)
groupPeaksi <- groupPeaksi[order(groupPeaksi$seqnames, groupPeaksi$idx), ]
groupPeaksi$peaks <- NULL
groupPeaksi$normmlogp <- NULL
groupPeaksi$mlog10p <- round(groupPeaksi$mlog10p, 3)
groupPeaksGRi <- GRanges(groupPeaksi$seqnames,
IRanges(
start = pmax((groupPeaksi$idx - 1) * res, 1),
end = (groupPeaksi$idx) * res - 1
)
)
mcols(groupPeaksGRi) <- groupPeaksi[,c("mlog10p", "Group")]
groupPeaksGRi <- subsetByOverlaps(groupPeaksGRi, genomeAnnotation$chromSizes, type = "within")
groupPeaksGRi <- subsetByOverlaps(groupPeaksGRi, genomeAnnotation$blacklist, invert = TRUE)
groupPeaksGRi <- .fastAnnoPeaks(
groupPeaksGRi,
BSgenome = BSgenome,
geneAnnotation = geneAnnotation,
promoterRegion = promoterRegion
)
groupPeaksGRi <- groupPeaksGRi[which(mcols(groupPeaksGRi)$N < 0.001)] #Remove N Containing Peaks
mcols(groupPeaksGRi)$N <- NULL #Remove N Column
#Table Peak Types
tabPT <- data.frame(Group = names(cellGroups)[i], table(groupPeaksGRi$peakType))
#Save
saveRDS(groupPeaksGRi, file.path(outDir, paste0(make.names(names(cellGroups)[i]), "-reproduciblePeaks.gr.rds")))
#Remove
rm(groupPeaksGRi)
gc()
tabPT
}, threads = threads) %>% Reduce("rbind", .)
#####################################################
# Call Union Peaks
#####################################################
ArchR:::.logDiffTime("Creating Union Peak Set with Annotations!", tstart, verbose = verbose, logFile = logFile)
unionPeaks <- groupPeaks[!duplicated(groupPeaks$peaks), ]
rm(groupPeaks)
gc()
unionPeaks <- cbind(
topFeatures[as.integer(gsub("f", "", unionPeaks$peaks)),],
unionPeaks
)
unionPeaks <- unionPeaks[order(unionPeaks$seqnames, unionPeaks$idx), ]
unionPeaks$peaks <- NULL
unionPeaks$normmlogp <- NULL
unionPeaks$mlog10p <- round(unionPeaks$mlog10p, 3)
unionPeaksGR <- GRanges(unionPeaks$seqnames,
IRanges(
start = pmax((unionPeaks$idx - 1) * res, 1),
end = (unionPeaks$idx) * res - 1
)
)
mcols(unionPeaksGR) <- unionPeaks[,c("mlog10p", "Group")]
unionPeaksGR <- subsetByOverlaps(unionPeaksGR, genomeAnnotation$chromSizes, type = "within")
unionPeaksGR <- subsetByOverlaps(unionPeaksGR, genomeAnnotation$blacklist, invert = TRUE)
unionPeaksGR <- ArchR:::.fastAnnoPeaks(
unionPeaksGR,
BSgenome = BSgenome,
geneAnnotation = geneAnnotation,
promoterRegion = promoterRegion
)
unionPeaksGR <- unionPeaksGR[which(mcols(unionPeaksGR)$N < 0.001)] #Remove N Containing Peaks
mcols(unionPeaksGR)$N <- NULL #Remove N Column
#Set To unionPeaks
unionPeaks <- unionPeaksGR
rm(unionPeaksGR)
gc()
#Summarize Output
peakDF$Group <- paste0(peakDF$Group, "(n = ", tableGroups[peakDF$Group],")")
peakDF <- rbind(data.frame(Group = "UnionPeaks", table(unionPeaks$peakType)), peakDF)
peakDF$Freq <- peakDF$Freq / 1000
metadata(unionPeaks)$PeakCallSummary <- peakDF
}else{
ArchR:::.logMessage("method not recognized! Supported methods are Macs2 or Tiles!", logFile = logFile)
stop("method not recognized! Supported methods are Macs2 or Tiles!")
}
#Add Peak Set
ArchRProj <- addPeakSet(ArchRProj, unionPeaks, force = TRUE)
if(plot){
plotPDF(ArchR:::.plotPeakCallSummary(ArchRProj), name = "Peak-Call-Summary", width = 8, height = 5, ArchRProj = ArchRProj, addDOC = FALSE)
}
ArchR:::.logDiffTime(sprintf("Finished Creating Union Peak Set (%s)!", length(unionPeaks)), tstart, verbose = verbose, logFile = logFile)
return(ArchRProj)
}
callSummitsOnCoverages_win <- function(
i = NULL,
coverageFiles = NULL,
outFiles = NULL,
peakParams = NULL,
bedDir = NULL,
excludeChr = NULL,
subThreads = 1,
tstart = NULL,
logFile = NULL
){
ArchR:::.logDiffTime(sprintf("Group %s of %s, Calling Peaks with MACS2!", i, length(coverageFiles)), tstart, verbose = TRUE, logFile = logFile)
################
# Create Bed File from Coverage File
################
bedFile <- file.path(bedDir, paste0(make.names(basename(names(coverageFiles)[i])),"-",i,".insertions.bed"))
o <- ArchR:::.writeCoverageToBed(coverageFiles[i], bedFile, excludeChr = excludeChr, logFile = logFile)
peakParams$bedFile <- bedFile
################
# MACS2 Peak-Calling Leave Room For Other Options?
################
peakParams$logFile <- logFile
summits <- do.call(callSummitsMACS2_win, peakParams)
rmf <- file.remove(bedFile)
################
# Save output
################
saveRDS(summits, outFiles[i])
#.logDiffTime(sprintf("Group %s of %s, Finished Calling Peaks with MACS2!", i, length(coverageFiles)), tstart, verbose = verbose, logFile = logFile)
outFiles[i]
}
callSummitsMACS2_win <- function(
i = NULL,
coverageFiles = NULL,
outFiles = NULL,
peakParams = NULL,
bedDir = NULL,
excludeChr = NULL,
subThreads = 1,
tstart = NULL,
logFile = NULL
){
ArchR:::.logDiffTime(sprintf("Group %s of %s, Calling Peaks with MACS2!", i, length(coverageFiles)), tstart, verbose = TRUE, logFile = logFile)
################
# Create Bed File from Coverage File
################
bedFile <- file.path(bedDir, paste0(make.names(basename(names(coverageFiles)[i])),"-",i,".insertions.bed"))
o <- ArchR:::.writeCoverageToBed(coverageFiles[i], bedFile, excludeChr = excludeChr, logFile = logFile)
peakParams$bedFile <- bedFile
################
# MACS2 Peak-Calling Leave Room For Other Options?
################
peakParams$logFile <- logFile
summits <- do.call(callSummitsMACS2_win, peakParams)
rmf <- file.remove(bedFile)
################
# Save output
################
saveRDS(summits, outFiles[i])
#.logDiffTime(sprintf("Group %s of %s, Finished Calling Peaks with MACS2!", i, length(coverageFiles)), tstart, verbose = verbose, logFile = logFile)
outFiles[i]
}
callSummitsMACS2_win <- function(
bedFile = NULL,
pathToMacs2 = "macs2",
genomeSize = 2.7e9,
shift = -75,
extsize = 150,
cutOff = 0.05,
method = "q",
additionalParams = "--nomodel --nolambda",
logFile = NULL
){
stopifnot(tolower(method) %in% c("p","q"))
stopifnot(!is.null(genomeSize))
#utility <- .checkPath(pathToMacs2)
#Output Files
bedName <- gsub("\\.insertions.bed", "", bedFile)
summitsFile <- paste0(bedName, "_summits.bed")
narrowPeaksFile <- paste0(bedName, "_peaks.narrowPeak")
xlsFile <- paste0(bedName, "_peaks.xls")
#Create MACS2 Command
#cmd <- sprintf("callpeak -g %s --name %s --treatment %s --outdir %s --format BED --call-summits --keep-dup all %s",
# genomeSize, basename(bedName), bedFile, dirname(bedName), additionalParams)
#if(!is.null(shift) & !is.null(extsize)){
# cmd <- sprintf("%s --shift %s --extsize %s", cmd , shift, extsize)
#}
#if(tolower(method) == "p"){
# cmd <- sprintf("%s -p %s", cmd , cutOff)
#}else{
# cmd <- sprintf("%s -q %s", cmd , cutOff)
#}
#ArchR:::.logMessage(paste0("Running Macs2 with Params : macs2 ", cmd), logFile = logFile)
#run <- system2(pathToMacs2, cmd, wait=TRUE, stdout=NULL, stderr=NULL)
bedFile2<-gsub("\\\\","/",bedFile)
mount_drive<-substring(bedFile2, 1, 1)
bedFile2<-gsub(sprintf("*%s:",toupper(mount_drive)),sprintf("/mnt/%s",tolower(mount_drive)),bedFile2)
bedName2<-gsub("\\\\","/",bedName)
bedName2<-gsub(sprintf("*%s:",toupper(mount_drive)),sprintf("/mnt/%s",tolower(mount_drive)),bedName2)
pathToMacs2_temp<-system("bash -c 'find /home -name macs2'", intern = TRUE)
run <- system(sprintf('bash -c "%s callpeak -t %s -g %s --format BED --nomodel --nolambda --extsize 150 --shift -75 --call-summits --keep-dup all -q 0.05 --name %s --outdir %s" ',pathToMacs2_temp, bedFile2, genomeSize, basename(bedName2), dirname(bedName2)))
#Read Summits!
out <- data.table::fread(summitsFile, select = c(1,2,3,5))
out <- GRanges(out$V1, IRanges(out$V2 + 1, out$V3), score = out$V5)
#Remove Files
r2 <- suppressWarnings(file.remove(summitsFile, narrowPeaksFile, xlsFile))
return(out)
}