-
Notifications
You must be signed in to change notification settings - Fork 0
/
mRNA.R
218 lines (160 loc) · 7.71 KB
/
mRNA.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
213
214
215
library(ggpubr)
library(tidyverse)
library(ggplot2)
library(lemon)
library(scales)
library(RColorBrewer)
library(ggrepel)
library(ggpmisc)
library(ggseqlogo)
library(rstatix)
library(ggdist)
source("/fs/ess/PCON0160/ben/bin/mighty.R")
# counts
deseq_analysis = function(counts_file, coldata_file, pathways_file = FALSE, comparisons) {
counts = read.table(counts_file, header = TRUE, fill = TRUE, check.names=FALSE)
counts = counts %>% filter(!grepl("snRNA|snoRNA|scRNA|tRNA|asRNA|miRNA|piRNA", biotype)) %>% filter(biotype != "ncRNA")
colnames(counts) = gsub(".tsv","",colnames(counts))
head(counts)
# pathways
if (pathways_file != FALSE){
pathways = read.delim(pathways_file, sep = "\t",
col.names = c("gene_name", "seq_id", "locus_id", "biotype", "class"))
pathways = pathways %>% select(-seq_id, -locus_id)
# merge counts to pathways
counts = counts %>%
select(-class, -biotype) %>%
left_join(pathways, by = c("gene_id" = "gene_name"))
}
# coldata
coldata = read.table(coldata_file, header = TRUE, sep = ",", col.names = c("sample", "condition"))
row.names(coldata) = coldata$sample
coldata = subset(coldata, select = c("condition"))
coldata
# select cols that are non-numeric/numeric
tmp = data.frame(counts, row.names = 1, check.names=FALSE)
names_df = select_if(tmp, Negate(is.numeric))
names_df['gene_id'] = rownames(names_df)
cts = select_if(tmp, is.numeric)
cts = cts[ , sort(colnames(cts))]
head(cts)
# remove rows that have no counts
x = rowSums(cts) >= 1
cts_filt = cts[x,]
cts_filt[] <- sapply(cts_filt, as.integer)
list(colnames(cts_filt))[[1]]
# set condition equal to coldata condition
condition = coldata$condition
# make sure columns in counts are in same order as they appear in coldata
rownames(coldata)
cts_filt = cts_filt[,rownames(coldata)]
all( rownames(coldata) == colnames(cts_filt) )
# DESeq
deobj <- DESeqDataSetFromMatrix(countData = cts_filt, colData = coldata, design = ~condition)
dds <- DESeq(deobj)
tmp = counts(dds, normalized = TRUE)
dds_counts = as.data.frame(tmp) %>% rownames_to_column(var = "gene_id")
counts_table = dds_counts %>%
left_join(names_df, by = "gene_id") %>%
select(gene_id, seq_id, locus_id, biotype, class, everything())
#write.table( counts_table, "normalized_counts.tsv", quote = FALSE, sep = "\t", row.names = FALSE, col.names = TRUE)
# PCA plot to assess variance within sample groups and between sample groups
vsd <- vst(dds, blind=FALSE)
pcaData <- plotPCA(vsd, intgroup=c("condition"), returnData=TRUE)
percentVar <- round(100 * attr(pcaData, "percentVar"))
PCA = ggplot(pcaData, aes(PC1, PC2, color=condition)) +
geom_point(size=5) +
xlab(paste0("PC1: ",percentVar[1],"% variance")) +
ylab(paste0("PC2: ",percentVar[2],"% variance")) +
theme(aspect.ratio = 1) +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
print(PCA)
plist = list()
for (i in 1:nrow(comparisons)) {
sample_x = as.character(comparisons[i,1])
sample_y = as.character(comparisons[i, 2])
curr = paste0(sample_x,"_vs_",sample_y)
print(curr)
xdf = subset(coldata, condition == paste0(sample_x))
xnames = rownames(xdf)
ydf = subset(coldata, condition == paste0(sample_y))
ynames = rownames(ydf)
res = results(dds, contrast = c("condition",paste0(sample_y),paste0(sample_x)))
c = as.data.frame(counts(dds,normalized = TRUE))
keep = c(paste0(xnames), paste0(ynames))
c_sub = subset(c, select = keep)
resdata = merge(as.data.frame(res), c_sub, by = 'row.names', sort = FALSE)
names(resdata)[1] <- "gene_id"
resdata[paste0(sample_x)] = rowMeans( resdata[ , xnames] )
resdata[paste0(sample_y)] = rowMeans( resdata[ , ynames] )
resdata = merge(resdata, names_df, by = "gene_id", all.x = TRUE)
deseq_cols = c("baseMean","log2FoldChange","lfcSE","stat","pvalue","padj")
cols = c(names_df, deseq_cols)
write.table(resdata, paste0("./deseq_tables/", curr,"_deseq_results.tsv"), sep = "\t", quote = FALSE, row.names = FALSE, col.names = TRUE)
p = xy_dge(resdata, paste0(sample_x), paste0(sample_y), 2^0, 2^15, deseq = TRUE, fold_change = 2)
# plot cuticle genes
gene_list = read.delim("/fs/ess/PAS1473/ben_past_projects/Function_eggd_smRNA_RNA/cuticle_GO.txt", sep = "\t")
#p = xy_highlight(resdata,
# paste0(sample_x),
# paste0(sample_y),
# resdata %>% filter(locus_id == 'col-12'),
# "col-12",
# 2^-5,
# 2^20,
# fold_change = 2)
plist[[i]] = p
}
return(plist)
}
#results and plot for each comparison
setwd("/fs/ess/PAS1473/ben_past_projects/Function_eggd_smRNA_RNA")
counts_file = "./mRNA_analysis/counts/analysis.tsv"
coldata_file = "./mRNA_analysis/samples/replicates.csv"
pathways_file = "/fs/ess/PCON0160/ben/genomes/c_elegans/WS279/pathways.tsv"
comparisons = data.frame(x = c("control"), y = c("eggd1"))
p = deseq_analysis(counts_file, coldata_file, pathways_file, comparisons)
ggsave(pout, filename = './figs/control_vs_eggd1.png', dpi = 300, height = 15, width = 15)
# hlh30 stuff
counts_file = "hlh30_paper/counts/analysis.tsv"
coldata_file = "hlh30_paper/samples/replicates.csv"
pathways_file = "/fs/ess/PCON0160/ben/genomes/c_elegans/WS279/pathways.tsv"
comparisons = read.delim('hlh30_dge.csv', sep = ',')
p = deseq_analysis(counts_file, coldata_file, pathways_file, comparisons)
pout = ggarrange(plotlist = p)
ggsave(pout, filename = 'figs/hlh30_analysis.png', dpi = 300, height = 15, width = 15)
# compare cuticle genes fc in eggd-1 that are hlh-30 target vs. those that are not
cuticle_genes = read.delim("/fs/ess/PAS1473/ben_past_projects/Function_eggd_smRNA_RNA/cuticle_GO.txt", sep = "\t")
dat = read.delim("deseq_tables/control_vs_eggd1_deseq_results.tsv")
hlh30_cuticle_targets = read.delim("hlh30_cuticle_targets.txt")
cut_dat = dat %>%
filter(gene_id %in% cuticle_genes$gene_name) %>%
mutate(hlh30_target = ifelse(gene_id %in% hlh30_cuticle_targets$gene_name, T, F))
p = plot_boxplot(cut_dat, counts_col = "log2FoldChange", samples_col = "hlh30_target", ylog2 = F, dots = T, pvals = T )
ggsave(p, filename = "hlh30_dep_vs_ind_eggdlfc.pdf", dpi = 300, height = 5, width = 5, device = cairo_pdf)
view(cut_dat %>% filter(hlh30_target == T))
# subset promoter bed file with cuticle genes
bed = read.delim("/fs/ess/PAS1473/ben_past_projects/Function_eggd_smRNA_RNA/hlh30_chip_modEncode/MACS2/assigned/protein_coding.bed",
col.names = c("chrom", "start", "end", "gene", "type", "strand"))
bed %>%
filter(gene %in% cuticle_genes$gene_name) %>%
select(chrom, start, end) %>%
write.table(., "cuticle_genes.bed", sep = "\t", col.names = F, row.names = F, quote = F)
up_cuticle_genes = cut_dat %>% filter(log2FoldChange >= 2 & padj < 0.05)
bed %>%
filter(gene %in% up_cuticle_genes$gene_id) %>%
select(chrom, start, end) %>%
write.table(., "cuticle_genes_up.bed", sep = "\t", col.names = F, row.names = F, quote = F)
other_cuticle_genes = cut_dat %>% filter(log2FoldChange < 2)
bed %>%
filter(gene %in% other_cuticle_genes$gene_id) %>%
select(chrom, start, end) %>%
write.table(., "cuticle_genes_other.bed", sep = "\t", col.names = F, row.names = F, quote = F)
bed %>%
filter(gene %in% cuticle_genes$gene_name) %>%
select(chrom, start, end) %>%
write.table(., "cuticle_genes.bed", sep = "\t", col.names = F, row.names = F, quote = F)
bed %>%
select(chrom, start, end) %>%
write.table(., "pc_genes.bed", sep = "\t", col.names = F, row.names = F, quote = F)