forked from andreiaamaral/IsomiR-Window
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rdeseq.R
186 lines (128 loc) · 5.71 KB
/
Rdeseq.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
#!/usr/bin/env Rscript
args = commandArgs(trailingOnly=TRUE)
# test if there is at least one argument: if not, return an error
if (length(args)<2) {
stop("This R script needs at least 3 arguments to work", call.=FALSE)
} else if (length(args)==2) {
# default output file
args[4] = "out.txt"
}
source("http://bioconductor.org/biocLite.R")
#Load the packages
library(DESeq2)
library("pheatmap")
library("gplots")
library(grid)
library(reshape)
# Define the variables for the arguments that will be used
ID <- args[1]
deseq_file <- args[2]
path_results <- args[4]
paired_unpaired <- args[5]
pvalue <- args[6]
cts <- as.matrix(read.csv(deseq_file,sep="\t",row.names="IsomiR_ID"))
cts[is.na(cts)] = 0
experimental_design_file <- args[3]
coldata <- read.table(experimental_design_file, row.names=1)
if (paired_unpaired == "Unpaired_Sample") {
coldata <- rename(coldata, c("V2"="fileName", "V3"="condition"))
coldata <- coldata[, c("fileName", "condition")]
dds <- DESeqDataSetFromMatrix(countData = cts, colData = coldata, design =~ condition)
} else if (paired_unpaired == "Paired_Sample"){
coldata <- rename(coldata, c("V2"="fileName", "V3"="condition", "V4"="subject"))
coldata <- coldata[, c("fileName", "condition", "subject")]
#print(coldata)
dds <- DESeqDataSetFromMatrix(countData = cts, colData = coldata, design =~ subject + condition)
}
# Perform a minimal pre-filtering to remove rows that have only 0 or 1 read
dds <- dds[ rowSums(counts(dds)) > 1, ]
dds <- DESeq(dds)
res<-results(dds)
# Order our results table by the smallest adjusted p value:
res<-res[order(res$padj),]
# Define the name of the complete table
# This table is allways produced
name_file_table_DE_complete <- paste('CompleteTableDE_',ID,'.txt', sep='')
# Define the file path
path_to_table_DE_complete <- paste(path_results, name_file_table_DE_complete, sep='/')
# Write the output DE table
dds <- estimateSizeFactors(dds)
norm<-data.frame(counts(dds, normalized=TRUE))
res_data<-as.data.frame(res)
res_data$IsomiRs<-row.names(res_data)
norm$IsomiRs<-row.names(norm)
table_merge_res<-merge(res_data, norm, by="IsomiRs")
write.table(table_merge_res, path_to_table_DE_complete, quote=F, row.names=F, col.names=T, sep="\t")
# Select a subset with the p-value less than 0.1 or 0.05
if(pvalue == "0.1") {
#differ<-subset(res, res$padj < 0.1)
differ<-subset(res, res$padj < 0.1 | res$padj != 'NA')
if (nrow(differ)>=15) {
top15<-differ[1:15,]
test<-dds[rownames(top15)]
}
else {
test<-matrix(, nrow=0, ncol=0)
}
} else if (pvalue == "0.05"){
#differ<-subset(res, res$padj < 0.05)
differ<-subset(res, res$padj < 0.05 | res$padj != 'NA')
if (nrow(differ)>=15) {
top15<-differ[1:15,]
test<-dds[rownames(top15)]
}
else {
test<-matrix(, nrow=0, ncol=0)
}
}
if (nrow(test)==0) {
# Produce a file justifying the absence of a heatmap
name_file_no_heatmap <- paste('NoHeatmap_',ID,'.txt', sep='')
# Define the file path
path_to_file_no_heatmap <- paste(path_results, name_file_no_heatmap, sep='/')
message <- paste("There are no differentially expressed miRNAs/isomiRs for the value",
" of p-value selected. Only the first 100 entries of the complete DE",
" table are shown. No heatmap was produced.",sep='')
# Write the output DE table
writeLines(message, path_to_file_no_heatmap)
} else {
# Define the name of the table
# This table is only produced when there are
# differently expressed miRNAs
name_file_table_DE_test <- paste('TableDE_',ID,'.txt', sep='')
# Define the file path
path_to_table_DE_test <- paste(path_results, name_file_table_DE_test, sep='/')
# add the normalized counts
dds <- estimateSizeFactors(dds)
norm<-data.frame(counts(dds, normalized=TRUE))
differ_data<-as.data.frame(differ)
differ_data$IsomiRs<-row.names(differ_data)
norm$IsomiRs<-row.names(norm)
table_merge<-merge(differ_data, norm, by="IsomiRs")
write.table(table_merge, path_to_table_DE_test, quote=F, row.names=F, col.names=T, sep="\t")
###### MA-plot ####
# Define the names of the plots
name_file_MAplot_png <- paste('MAplot_',ID,'.png', sep='')
name_file_MAplot_pdf <- paste('MAplot_',ID,'.pdf', sep='')
name_file_Heatmap_png <- paste('heatmap_',ID,'.png', sep='')
# Define the path of plot
path_to_MAplot_png <- paste(path_results, name_file_MAplot_png, sep='/')
path_to_MAplot_pdf <- paste(path_results, name_file_MAplot_pdf, sep='/')
path_to_Heatmap_png <- paste(path_results, name_file_Heatmap_png, sep='/')
# Create the MA plots in png and in pdf formats
png(path_to_MAplot_png, width=5*300, height=5*300, res=300, pointsize=8)
plotMA(res, main="DESeq2", ylim=c(-2,2))
dev.off()
pdf(path_to_MAplot_pdf)
plotMA(res, main="DESeq2", ylim=c(-2,2))
dev.off()
# Heatmap
vsd <- varianceStabilizingTransformation(test, blind=FALSE)
df <- as.data.frame(colData(dds)[,c("condition")])
row.names(df)<-row.names(colData(dds))
colnames(df)<-("Conditions")
# Write the heatmap to a file
png(path_to_Heatmap_png, width=1050, height=1000, res=100)
pheatmap(assay(test), cluster_rows=TRUE,show_colnames = TRUE, show_rownames=TRUE, cluster_cols=TRUE, fontsize=9, fontsize_row = 8, fontsize_col = 8, annotation_col=df)
dev.off()
}