forked from ddhb/Workflow_of_ChIPSeq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChIP-Seq_broad.peak_code.R
212 lines (183 loc) · 9.95 KB
/
ChIP-Seq_broad.peak_code.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
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
####################################################################
## Please install the latest release of 'Microsoft R Open'.
## https://mran.microsoft.com/open/
## Then, entering the commands.
####################################################################
####################################################################
## Step 0 | Install the packages
####################################################################
## Step 0-1. Install bioconductor base program.
source("https://bioconductor.org/biocLite.R")
biocLite()
## Step 0-2. Install each package.
biocLite("BiocParallel")
biocLite("parallel")
biocLite("ShortRead")
biocLite("RSQLite")
biocLite("QuasR")
biocLite("BSgenome")
biocLite("Rsamtools")
biocLite("rtracklayer")
biocLite("GenomicFeatures")
biocLite("Hmisc")
biocLite("Gviz")
biocLite("XML")
biocLite("mosaics")
biocLite("ChIPseeker")
biocLite("TxDb.Hsapiens.UCSC.hg19.knownGene")
biocLite("clusterProfiler")
biocLite("ReactomePA")
biocLite("dada2")
####################################################################
## Step 1 | Set the working directory and loading packages
####################################################################
## Step 1-1. Set working directory if the path is 'K:/LAB4',
## unless please correct the working directory path.
working_dir="K:/LAB4"
setwd(working_dir)
## Step 1-2. Library loading.
library("BiocParallel")
library("parallel")
library("ShortRead")
library("RSQLite")
library("QuasR")
library("BSgenome")
library("Rsamtools")
library("rtracklayer")
library("GenomicFeatures")
library("Hmisc")
library("Gviz")
library("XML")
library("mosaics")
library("ChIPseeker")
library("TxDb.Hsapiens.UCSC.hg19.knownGene")
library("clusterProfiler")
library("ReactomePA")
library("dada2")
####################################################################
## (Optional) Step 2 | Downloading sample data from EBI (2.8GB and 1.2GB respectively) ## 30 minutes
####################################################################
## Step 2-1. Assigning variables for downloading zipped fastq files.
Control_filename = "Sample_data_control.fastq.gz"
Case_filename = "Sample_data_case.fastq.gz"
sample_control = "ftp://ftp.sra.ebi.ac.uk/vol1/fastq/SRR227/SRR227391/SRR227391.fastq.gz"
sample_chip = "ftp://ftp.sra.ebi.ac.uk/vol1/fastq/SRR227/SRR227473/SRR227473.fastq.gz"
## Step 2-2. Start download file with internal method from EBI.
download.file(sample_control, destfile=Control_filename, method="internal")
download.file(sample_chip, destfile=Case_filename, method="internal")
####################################################################
## Step 3 | Trimming ## 25 minutes
####################################################################
## Step 3-1. Assigning variblaes for pre-processing
trimmed_control = paste(strsplit(Control_filename, ".fastq.gz")[[1]],"_trimmed.fastq.gz",sep="")
trimmed_case = paste(strsplit(Case_filename, ".fastq.gz")[[1]],"_trimmed.fastq.gz",sep="")
## Step 3-2. Fastq filtering
fastqFilter(fn=Control_filename, fout=trimmed_control,
truncQ = 2, truncLen = 0, trimLeft = 0, maxN=0, minQ=0, maxEE=Inf,
rm.phix=FALSE, n=1e+06, compress = TRUE, verbose=FALSE)
fastqFilter(fn=Case_filename, fout=trimmed_case,
truncQ = 2, truncLen = 0, trimLeft = 0, maxN=0, minQ=0, maxEE=Inf,
rm.phix=FALSE, n=1e+06, compress = TRUE, verbose=FALSE)
####################################################################
## Step 4 | Align to reference genome # 1.2 hours using 7 cores
## | Recommend to start at this point
####################################################################
## Step 4-1. Selecting reference genome, hg19
controlFile <- "sample_control.txt"
caseFile <- "sample_Case.txt"
genomeFile <- "BSgenome.Hsapiens.UCSC.hg19"
## Step 4-2. Making a matrix file sample and control each other to use qAlign function.
Matrix_file_control <- matrix(c("FileName",trimmed_control,"SampleName","Sample1"),nrow=2,ncol=2)
write.table(Matrix_file_control, file="sample_control.txt", sep="\t",row.names=FALSE, col.names=FALSE)
Matrix_file_case <- matrix(c("FileName",trimmed_case,"SampleName","Sample1"),nrow=2,ncol=2)
write.table(Matrix_file_case, file="sample_Case.txt", sep="\t",row.names=FALSE, col.names=FALSE)
## Step 4-3. Assigning core and aligning to hg19 reference genome
no_cores <- detectCores()-1
cl <- makeCluster(no_cores)
Aligned_control<-qAlign(sampleFile=controlFile, genome=genomeFile,
clObj=cl, alignmentsDir = working_dir)
Aligned_case<-qAlign(sampleFile=caseFile, genome=genomeFile,
clObj=cl, alignmentsDir = working_dir)
## Step 4-4. Post-processing (releasing core and removing temporal matrix files)
stopCluster(cl)
file.remove(controlFile)
file.remove(caseFile)
####################################################################
## Step 5 | Quality check ## 1.2 hours using 7 cores
####################################################################
## Step 5-1. The QC plotting of case and control files
filename_Control = strsplit(trimmed_control, ".fastq.gz")[[1]]
filename_Case = strsplit(trimmed_case, ".fastq.gz")[[1]]
qQCReport(Aligned_control, pdfFilename = paste(filename_Control,"_QCReport.pdf",sep=""))
qQCReport(Aligned_case, pdfFilename = paste(filename_Case,"_QCReport.pdf",sep=""))
## Step 5-2. Renaming aligned output files
file.rename(list.files(pattern=paste("^",filename_Control,"(.*).bam$",sep="")), to=paste(filename_Control,".bam", sep=""))
file.rename(list.files(pattern=paste("^",filename_Control,"(.*).bam.bai$",sep="")), to=paste(filename_Control,".bam.bai", sep=""))
file.rename(list.files(pattern=paste("^",filename_Control,"(.*).bam.txt$",sep="")), to=paste(filename_Control,".bam.txt", sep=""))
file.rename(list.files(pattern=paste("^",filename_Case,"(.*).bam$",sep="")), to=paste(filename_Case,".bam", sep=""))
file.rename(list.files(pattern=paste("^",filename_Case,"(.*).bam.bai$",sep="")), to=paste(filename_Case,".bam.bai", sep=""))
file.rename(list.files(pattern=paste("^",filename_Case,"(.*).bam.txt$",sep="")), to=paste(filename_Case,".bam.txt", sep=""))
####################################################################
## Step 6 | Peak calling ## 1.2 hours using 7 cores
####################################################################
## Step 6-1. Preprocessing before calling peaks
filename_Control_bam = paste(filename_Control,".bam",sep="")
filename_Case_bam = paste(filename_Case,".bam",sep="")
## Step 6-2. Setting the fragment length and binsize and contructing bin-level files from bam files
fragment_length = 200
binSize = 200
constructBins(infile=filename_Control_bam, fileFormat="bam", outfileLoc="./",byChr=FALSE, useChrfile=FALSE,
chrfile=NULL, excludeChr=NULL, PET=FALSE, fragLen=fragment_length,binSize=binSize,capping=0)
constructBins(infile=filename_Case_bam, fileFormat="bam", outfileLoc="./",byChr=FALSE, useChrfile=FALSE,
chrfile=NULL, excludeChr=NULL, PET=FALSE, fragLen=fragment_length,binSize=binSize,capping=0)
fragment_fControlb = paste(filename_Control_bam,"_fragL",fragment_length,"_bin",binSize,".txt",sep="")
fragment_fCaseb = paste(filename_Case_bam,"_fragL",fragment_length,"_bin",binSize,".txt",sep="")
## Step 6-3. Reading bin-level data
fileName <- c(fragment_fControlb,fragment_fCaseb)
binHM <- readBins( type=c("input","chip"), fileName=fileName)
## Step 6-4. Fitting the MOSAiCS model
fitHM <- mosaicsFit( binHM, analysisType="IO", bgEst="rMOM" )
hmmHM <- mosaicsFitHMM( fitHM, signalModel = "2S",
init="mosaics", init.FDR = 0.05, parallel=FALSE, nCore=no_cores )
## Step 6-5. Identifying peaks based on the fitted model
peakHM <- mosaicsPeakHMM( hmmHM, FDR = 0.05, decoding="posterior",
thres=10, parallel=FALSE, nCore=no_cores )
peakHM <- extractReads( peakHM,
chipFile=filename_Case_bam,
chipFileFormat="bam", chipPET=FALSE, chipFragLen=200,
controlFile=filename_Control_bam,
controlFileFormat="bam", controlPET=FALSE, controlFragLen=200, parallel=FALSE, nCore=8 )
peakHM <- findSummit( peakHM, parallel=FALSE, nCore=no_cores )
peakHM <- adjustBoundary( peakHM, parallel=FALSE, nCore=no_cores )
peakHM <- filterPeak( peakHM, parallel=FALSE, nCore=no_cores )
## Step 6-6. Exporting result to 'bed' format with the process of elimination of header line
result_peakfile = "broad_peakHM.bed"
export (peakHM, type="bed", filename=result_peakfile)
peakfile <- read.table(result_peakfile, header=FALSE)[-1,]
header.eliminated.file = "h.e_peakHM.bed"
write.table(peakfile, file=header.eliminated.file, sep="\t", col.names=FALSE, row.names=FALSE)
####################################################################
## Step 7 | Annotation and Visualization Peak calling ## 2 minutes
####################################################################
## Step 7-1. Preprocessing
txdb <- TxDb.Hsapiens.UCSC.hg19.knownGene
interesing_chrs = c("chr1","chr2","chr3","chr4","chr5","chr6","chr7","chr8","chr9","chr10"
,"chr11","chr12","chr13","chr14","chr15","chr16","chr17","chr18","chr19","chr20"
,"chr21","chr22","chrM","chrX","chrY")
## Step 7-2. Reading peak file and coverage plot
peak <- readPeakFile(header.eliminated.file)
covplot(peak, weightCol="V5", chrs=interesing_chrs)
## Step 7-3. Get promoter and heatmap of ChIP binding to TSS regions
promoter <- getPromoters(TxDb=txdb, upstream=3000, downstream=3000)
tagMatrix <- getTagMatrix(peak, windows=promoter)
tagHeatmap(tagMatrix, xlim=c(-3000, 3000), color="red")
plotAvgProf(tagMatrix, xlim=c(-3000, 3000),
xlab="Genomic Region (5'->3')", ylab = "Read Count Frequency")
## Step 7-4. Annotate peak and illustrate pie-plot & upset-plot
peakAnno <- annotatePeak (header.eliminated.file, tssRegion=c(-3000, 3000), TxDb=txdb, annoDb="org.Hs.eg.db")
plotAnnoPie(peakAnno)
upsetplot(peakAnno, vennpie=TRUE)
## Step 7-5. Functional enrichment analysis and plotting
gene <- seq2gene(peak, tssRegion=c(-1000,1000), flankDistance=3000, TxDb=txdb)
pathway <- enrichPathway(gene)
dotplot(pathway)