-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzNorm.r
54 lines (40 loc) · 1.86 KB
/
zNorm.r
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
#!/usr/bin/env Rscript
# Author: Spencer Nystrom ([email protected])
# Z-normalizes input bigwig file by chromosome arm
# Usage: module load r
Rscript --vanilla zNorm.r <input.bw> <output.bw>
library(rtracklayer)
argv <- commandArgs(trailingOnly = TRUE)
if (length(argv) != 2){
stop("usage: Rscript --vanilla zNorm.r <input.bw> <output.bw>")
}
bw <- import.bw(argv[1])
bwList <- split(bw, bw@seqnames) # split by chromosome arm
# For filtering out specific regions you don't want. testing if using a blacklist file will overcome need for this.
#chrKeep <- c("chr2L", "chr2R", "chr3R", "chr3L", "chr4", "chrX")
#bwList <- bwList[chrKeep] # only keep chr2,3,4,X , no heterochromatin
write(paste("chr", "Mean", "Sd", sep = " "), stdout())
zScoreChrList <- lapply(bwList, function(x){
# Calculate z-Score per chromosome arm:
# z = (x-u)/sd
# Mean and SD must be calculated as if from a frequency table, as deeptools will merge regions with identical scores
chrScore <- x@elementMetadata$score
nfreq <- sum(x@ranges@width)
zMean <- (sum(x@ranges@width * x@elementMetadata$score)/nfreq)
zSD <- sqrt((sum(x@ranges@width * (x@elementMetadata$score^2)) - nfreq * (zMean)^2)/(nfreq - 1))
x@elementMetadata$score <- (chrScore - zMean)/zSD
# Print ChrStats for report:
chrName <- unique(x@seqnames)
write(paste(chrName, zMean,zSD, sep = " "), stdout())
#return(x)
if (all(is.na(mcols(x)$score))) {
# Chromosomes with no reads will have NaN score for whole chromosome
return(NULL)
} else {
return(x)
}
})
zScoreChrList_noNull <- Filter(Negate(is.null), zScoreChrList)
zScoreChrList_noNull <- GRangesList(zScoreChrList_noNull)
zScoreBw <- unlist(zScoreChrList_noNull) # recombine into Granges object
export.bw(con = argv[2], object = zScoreBw) # write bigwig