-
Notifications
You must be signed in to change notification settings - Fork 1
/
WGBSpipe.interval_stats.limma.R
executable file
·154 lines (117 loc) · 6.73 KB
/
WGBSpipe.interval_stats.limma.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
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
#run in R-3.3.1
Rlib<-commandArgs(trailingOnly=TRUE)[6]
#set working directory
wdir<-commandArgs(trailingOnly=TRUE)[1]
#system(paste0('mkdir -p ',wdir)) #for debugging
setwd(wdir)
message(sprintf("working directory is %s",getwd()))
options(stringsAsFactors=FALSE,na.rm=TRUE)
bedF<-commandArgs(trailingOnly=TRUE)[2]
message(sprintf("processing %s",bedF))
bedshort<-gsub(".bed","",basename(bedF))
bedtab<-read.table(bedF,header=FALSE,sep="\t",as.is=TRUE,quote="")
cnpool<-c("CHROM","START","END")#message(sprintf("processing %s",bedF))
bedshort<-gsub(".bed","",basename(bedF))
bedtab<-read.table(bedF,header=FALSE,sep="\t",as.is=TRUE,quote="")
colnames(bedtab)[1:3]<-cnpool#[1:ncol(bedtab)]
if(!unique(grepl("STRAND",colnames(bedtab)))){bedtab$STRAND<-"*"}
if(!unique(grepl("Name",colnames(bedtab)))){bedtab$Name<-paste(bedtab$CHROM,bedtab$START,bedtab$END,sep="_")}
bedtab<-unique(bedtab)
auxF<-commandArgs(trailingOnly=TRUE)[3]
message(sprintf("processing %s",auxF))
auxbed<-read.table(auxF,header=FALSE,sep="\t",as.is=TRUE,quote="")
colnames(auxbed)[1:3]<-cnpool#[1:ncol(bedtab)]
if(!unique(grepl("STRAND",colnames(auxbed)))){auxbed$STRAND<-"*"}
if(!unique(grepl("Name",colnames(auxbed)))){auxbed$Name<-paste(auxbed$CHROM,auxbed$START,sep="_")}#auxbed$END
#load single CpG data, count NAs per row/CpG
sCpGF<-commandArgs(trailingOnly=TRUE)[4]
message(sprintf("loading %s",sCpGF))
require(data.table,lib.loc=Rlib)
load(sCpGF)
noNA<-apply(limdat.LG,1,function(X)sum(is.na(X)))
NAf<-ifelse(noNA>1,1,0)
limdat.LG$NAf<-NAf
colnames(limdat.LG)[colnames(limdat.LG) %in% "ms"]<-"Name"
require(GenomicRanges,lib.loc=Rlib)
bedGR<-GRanges(seqnames=bedtab$CHROM,ranges=IRanges(start=bedtab$START,end=bedtab$END,names=bedtab$Name),strand=bedtab$STRAND)
auxdat<-as.data.table(merge(x=auxbed,y=limdat.LG,by="Name",all.x=TRUE,sort=FALSE))
auxdat<-auxdat[,!colnames(auxdat) %in% c("CHROM","START","END","V4","V5","STRAND"),with=FALSE]
auxGR<-GRanges(seqnames=auxbed$CHROM,ranges=IRanges(start=auxbed$START,end=auxbed$END,names=auxbed$Name),strand=auxbed$STRAND)
auxdat<-auxdat[match(names(auxGR),auxdat$Name),] ##there is some reordering during GRanges construction
############################# UPDATED CODE
auxmtch<-as.data.frame(findOverlaps(query=auxGR, subject=bedGR))
limdat.LG.inCGI<-auxdat[auxmtch$queryHits,] #for compatibility with previous method; not really necessary as auxdat comes from a pre-intersected bed file
limdat.LG.inCGI$IntID<-names(ranges(bedGR))[auxmtch$subjectHits] ##needed for grouping per interval
##
noNA2<-apply(limdat.LG.inCGI[,!colnames(limdat.LG.inCGI) %in% c("Name","NAf","IntID"),with=FALSE],1,function(X)sum(is.na(X)))
NAf2<-ifelse(noNA2>1,1,0)
limdat.LG.inCGI$NAf<-NAf2
########################### END UPDATED CODE
NA.inCGI<-with(limdat.LG.inCGI,ave(NAf,factor(IntID),FUN=sum))
limdat.LG.inCGI$NA.inCGI<-NA.inCGI
to.m<-limdat.LG.inCGI[,c("IntID", "NA.inCGI"),with=FALSE]
CGI.map<-unique(to.m)
bedtab$N.CG.NA<-CGI.map$NA.inCGI[match(bedtab$Name,CGI.map$IntID)]
N.CG.tot<-with(limdat.LG.inCGI,ave(NAf,IntID,FUN=length))
bedtab$N.CG.tot<-N.CG.tot[match(bedtab$Name,limdat.LG.inCGI$IntID)]
bedtab$CGI.NAf<-ifelse(bedtab$N.CG.NA>(0.8*bedtab$N.CG.tot),NA,1)
bedtab.CC<-bedtab[complete.cases(bedtab),]
if(nrow(bedtab.CC)==0) {message("None of the genomic intervals passed the filtering.")}else{
CGI.limdat<-as.data.frame(apply(limdat.LG.inCGI[,2:(ncol(limdat.LG.inCGI)-3)],2,function(X){ave(X,factor(limdat.LG.inCGI$IntID),FUN=function(X)mean(X,na.rm=TRUE))}),stringsAsFactors=FALSE)
CGI.limdat$IntID<-limdat.LG.inCGI$IntID
CGI.limdat<-unique(CGI.limdat)
rownames(CGI.limdat)<-CGI.limdat$IntID
CGI.limdat<-CGI.limdat[,-grep("IntID",colnames(CGI.limdat))]
CGI.limdat.CC<-CGI.limdat[bedtab.CC$Name,]
####for differential interval methylation
### limma + ebayes + BH p value adjustment
require("limma",lib.loc=Rlib)
require("car",lib.loc=Rlib)
require("FactoMineR",lib.loc=Rlib)
require("reshape2",lib.loc=Rlib)
require("ggplot2",lib.loc=Rlib)
require("dplyr",lib.loc=Rlib)
CGI.limdat.CC.logit<-logit(CGI.limdat.CC,percents=FALSE,adjust=0.025)
x1<-PCA(CGI.limdat.CC,graph=FALSE)
pdf(paste0(bedshort,".CGI.limdat.CC.PCA.pdf"),paper="a4",bg="white")
plot.PCA(x1,choix="var")
dev.off()
#calculate row means
spath<-commandArgs(trailingOnly=TRUE)[5]
sampleInfo<-read.table(spath,header=TRUE,sep="\t",as.is=TRUE)
#calculate and save row means
CGI.limdat.CC$IntID<-rownames(CGI.limdat.CC)
CGI.limdat.CC.L<-melt(CGI.limdat.CC,id.vars="IntID",value.name="Beta",variable.name="SampleID")
CGI.limdat.CC.L$Group<-sampleInfo$Group[match(CGI.limdat.CC.L$SampleID,sampleInfo$PlottingID)]
CGI.limdat.CC.Means<-data.table(summarize(group_by(CGI.limdat.CC.L,IntID,Group),Beta.Mean=mean(Beta)))
##density plots
ggplot(data=CGI.limdat.CC.Means,aes(x=Beta.Mean))+geom_density(aes(group=Group,colour=Group,fill=Group),alpha=0.3)+ggtitle("Genomic intervals")+
theme(text = element_text(size=16),axis.text = element_text(size=12),axis.title = element_text(size=14))+xlab("Mean methylation ratio")
ggsave(paste0(bedshort,".Beta.MeanXgroup.int.dens.png"))
##violin plots
ggplot(data=CGI.limdat.CC.Means)+geom_violin(aes(x=Group,y=Beta.Mean,fill=Group))+geom_boxplot(aes(x=Group,y=Beta.Mean),width=0.1)+ggtitle("Genomic intervals")+
theme(text = element_text(size=16),axis.text = element_text(size=12),axis.title = element_text(size=14))+xlab("Mean methylation ratio")
ggsave(paste0(bedshort,".Beta.MeanXgroup.int.violin.png"))
#differential methylation
design<-as.data.frame(matrix(ncol=2,nrow=(ncol(CGI.limdat.CC.logit))),stringsAsFactors=FALSE)
colnames(design)<-c("Intercept","Group")
rownames(design)<-colnames(CGI.limdat.CC.logit)
design$Group<-as.numeric(factor(sampleInfo$Group[match(colnames(CGI.limdat.CC.logit),sampleInfo$PlottingID)]))
design$Intercept<-1
design<-as.matrix(design)
fit<-lmFit(CGI.limdat.CC.logit,design)
fit.eB<-eBayes(fit)
tT.FDR5<-topTable(fit.eB,2,p.value=0.05,number=Inf)
if(nrow(tT.FDR5)==0) {message("No genomic intervals were significantly differentially methylated.")}else{
tT.FDR5<-tT.FDR5[,c("logFC","t","adj.P.Val","B")]
write.table(tT.FDR5,file=paste0(bedshort,".CGI.limdat.CC.tT.FDR5.txt"),sep="\t",quote=FALSE)
nrow(tT.FDR5)
nrow(CGI.limdat.CC.logit)
nrow(tT.FDR5)/nrow(CGI.limdat.CC.logit)
CGI.limdat.CC.Diff<-summarize(group_by(CGI.limdat.CC.Means,IntID),Diff=(Beta.Mean[1]-Beta.Mean[2]))
tT.FDR5.Diff0.2<-tT.FDR5[rownames(tT.FDR5) %in% CGI.limdat.CC.Diff$IntID[abs(CGI.limdat.CC.Diff$Diff)>=0.2],]
nrow(tT.FDR5.Diff0.2)
nrow(tT.FDR5.Diff0.2)/nrow(CGI.limdat.CC.logit)
save(bedtab,limdat.LG.inCGI,CGI.limdat.CC,CGI.limdat.CC.Means,tT.FDR5,file=paste0(bedshort,".aggCpG.RData"))
}
}