-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathestimateSizeFactors.R
174 lines (143 loc) · 7.15 KB
/
estimateSizeFactors.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
library('DESeq2')
library('ggplot2')
library( "gplots" )
library( "RColorBrewer" )
library('genefilter')
library('pheatmap')
library('FactoMinheR')
library(ggvenn)
library(venneuler)
library(ggrepel)
library(tibble)
###############################################################################
### Estimate sice factors for merged files
directory <- '/Users/andreas/Bacillus/Bioinformatics/RNAseq_jag_khpa_kre/X204SC22071444-Z01-F001/Analysis/Coverage_bed'
sampleFiles <- list.files(path=directory, pattern='ALL_merged_cut.txt')
sampleFiles
sampleNames <- c('jag', 'khpA', 'kre', 'WT')
sampleTable<-data.frame(sampleName=sampleNames, fileName=sampleFiles)
sampleTable$strain <- as.factor(sampleTable$sampleName)
sampleTable
dds<-DESeqDataSetFromHTSeqCount(sampleTable=sampleTable, directory=directory, design=~ strain)
### View orginal counts table
#View(counts(dds))
### estimate size factors for normalising bedgraph files
dds <- estimateSizeFactors(dds)
sizeFactors(dds)
### beware that KhpA is much smaller because it has been merged from only 2 and not 3 samples
#normalized_counts <- counts(dds, normalized=TRUE)
#View(normalized_counts)
#colData(dds)$strain<-factor(colData(dds)$strain, levels=c('WT','kre'))
###############################################################################
### Estimate size factors for all strains/replicates in the same comparison
directory <- '/Users/andreas/Bacillus/Bioinformatics/RNAseq_jag_khpa_kre/X204SC22071444-Z01-F001/Analysis/Coverage_bed'
sampleFiles <- list.files(path=directory, pattern='txt')
# Filter out files that match the exclude pattern
sampleFiles <- sampleFiles[!grepl('ALL_merged', sampleFiles)]
sampleFiles <- sampleFiles[!grepl('khpA_1', sampleFiles)]
sampleFiles
sampleNames <- c('jag_1', 'jag_2', 'jag_3', 'khpA_2', 'khpA_3', 'kre_1', 'kre_2', 'kre_3', 'WT_1', 'WT_2', 'WT_3')
OD = c(2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 1)
Names <- c('jag_1', 'jag_2', 'jag_3', 'khpA_2', 'khpA_3', 'kre_1', 'kre_2', 'kre_3', 'WT_1', 'WT_2', 'WT_3')
Strain <- c('jag', 'jag', 'jag', 'khpA', 'khpA', 'kre', 'kre', 'kre', 'WT', 'WT', 'WT')
replicate <- c(1, 2, 3, 2, 3, 1, 2, 3, 1, 2, 3)
sampleTable<-data.frame(sampleName=sampleNames, fileName=sampleFiles, names = Names, strain=Strain, OD = OD, replicate = replicate)
sampleTable$OD <- as.factor(sampleTable$OD)
sampleTable$strain <- as.factor(sampleTable$strain)
sampleTable
dds<-DESeqDataSetFromHTSeqCount(sampleTable=sampleTable, directory=directory, design=~ OD + strain)
dds <- estimateSizeFactors(dds)
sizeFactors(dds)
###############################################################################
### Make PCA plot with all strains in the same plot #niiice
dds<-DESeqDataSetFromHTSeqCount(sampleTable=sampleTable, directory=directory, design=~ OD + strain)
dds <- DESeq(dds)
### PCA plot
rld <- rlogTransformation(dds, blind=TRUE)
pca_plot <- plotPCA(rld, intgroup = c("strain"), ntop=5000)
pca_data <- pca_plot$data
pca_plot <- ggplot(data = pca_data, aes(x = PC1, y = PC2, color = strain, shape = factor(replicate))) +
geom_point(size = 3) +
labs(title = "kre") +
xlab(pca_plot$labels$x) +
ylab(pca_plot$labels$y) +
labs(colour = 'strain', shape = 'replicate') +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5),
panel.border = element_rect(color = "black", fill = NA, size = 0.5)
) +
guides(colour = guide_legend(order = 1))
pca_plot
# Specify the output directory and file name
output_dir <- "/Users/andreas/Bacillus/Bioinformatics/RNAseq_jag_khpA_kre/X204SC22071444-Z01-F001/Analysis/figures/"
output_file <- file.path(output_dir, "PCA_plot_ALL.pdf")
# Save the plot as a PDF file
ggsave(output_file, plot = pca_plot, width = 8, height = 6)
###############################################################################
### Estimate size factors for WT/jag_khpA merged files
directory <- '/Users/andreas/Bacillus/Bioinformatics/RNAseq_jag_khpa_kre/X204SC22071444-Z01-F001/Analysis/Coverage_bed'
sampleFiles <- list.files(path=directory, pattern='ALL_merged_cut.txt')
sampleFiles <- sampleFiles[!grepl('kre', sampleFiles)]
sampleFiles
sampleNames <- c('jag', 'khpA', 'WT')
sampleTable<-data.frame(sampleName=sampleNames, fileName=sampleFiles)
sampleTable$strain <- as.factor(sampleTable$sampleName)
sampleTable
dds<-DESeqDataSetFromHTSeqCount(sampleTable=sampleTable, directory=directory, design=~ strain)
### View orginal counts table
#View(counts(dds))
### estimate size factors for normalising bedgraph files
dds <- estimateSizeFactors(dds)
sizeFactors(dds)
###############################################################################
### Estimate size factors for WT/jag_khpA NOT merged files
directory <- '/Users/andreas/Bacillus/Bioinformatics/RNAseq_jag_khpa_kre/X204SC22071444-Z01-F001/Analysis/Coverage_bed'
sampleFiles <- list.files(path=directory, pattern='txt')
exclude_patterns <- 'kre|khpA_1|ALL_merged'
sampleFiles <- sampleFiles[!grepl(exclude_patterns, sampleFiles)]
sampleNames <- c('jag_1', 'jag_2', 'jag_3', 'khpA_2', 'khpA_3', 'WT_1', 'WT_2', 'WT_3')
Strain <- c('jag', 'jag', 'jag', 'khpA', 'khpA', 'WT', 'WT', 'WT')
sampleTable<-data.frame(sampleName=sampleNames, fileName=sampleFiles, strain=Strain)
sampleTable$strain <- as.factor(sampleTable$strain)
sampleTable
dds<-DESeqDataSetFromHTSeqCount(sampleTable=sampleTable, directory=directory, design=~ strain)
### View orginal counts table
#View(counts(dds))
### estimate size factors for normalising bedgraph files
dds <- estimateSizeFactors(dds)
sizeFactors(dds)
###############################################################################
### Estimate size factors for WT/kre merged files
directory <- '/Users/andreas/Bacillus/Bioinformatics/RNAseq_jag_khpa_kre/X204SC22071444-Z01-F001/Analysis/Coverage_bed'
sampleFiles <- list.files(path=directory, pattern='ALL_merged_cut.txt')
exclude_patterns <- 'jag|khpA'
sampleFiles <- sampleFiles[!grepl(exclude_patterns, sampleFiles)]
sampleFiles
sampleNames <- c('kre', 'WT')
sampleTable<-data.frame(sampleName=sampleNames, fileName=sampleFiles)
sampleTable$strain <- as.factor(sampleTable$sampleName)
sampleTable
dds<-DESeqDataSetFromHTSeqCount(sampleTable=sampleTable, directory=directory, design=~ strain)
### View orginal counts table
#View(counts(dds))
### estimate size factors for normalising bedgraph files
dds <- estimateSizeFactors(dds)
sizeFactors(dds)
###############################################################################
### Estimate size factors for WT/jag_khpA NOT merged files
directory <- '/Users/andreas/Bacillus/Bioinformatics/RNAseq_jag_khpa_kre/X204SC22071444-Z01-F001/Analysis/Coverage_bed'
sampleFiles <- list.files(path=directory, pattern='txt')
exclude_patterns <- 'jag|khpA|ALL_merged'
sampleFiles <- sampleFiles[!grepl(exclude_patterns, sampleFiles)]
sampleNames <- c('kre_1', 'kre_2', 'kre_3', 'WT_1', 'WT_2', 'WT_3')
Strain <- c('kre', 'kre', 'kre', 'WT', 'WT', 'WT')
sampleTable<-data.frame(sampleName=sampleNames, fileName=sampleFiles, strain=Strain)
sampleTable$strain <- as.factor(sampleTable$strain)
sampleTable
dds<-DESeqDataSetFromHTSeqCount(sampleTable=sampleTable, directory=directory, design=~ strain)
### View orginal counts table
#View(counts(dds))
### estimate size factors for normalising bedgraph files
dds <- estimateSizeFactors(dds)
sizeFactors(dds)