-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgene.plot.all.R
95 lines (91 loc) · 3.41 KB
/
gene.plot.all.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
#' Dummy plot for when a gene symbol or Entrez ID is not found in the database
#'
#' @param identifier Identifier (symbol or Entrez ID) of the gene
gene.not.found.plot <- function(identifier){
df <- data.frame(x=c(1,1),y=c(1.5,1),text=c("Gene not found:",identifier))
ggplot(df,aes(x,y,label=text)) + geom_text(size=20) + scale_x_continuous(limits=c(0,2)) + scale_y_continuous(limits=c(0,2))
}
#' Plot counts for a given gene
#'
#' @param entrez Entrez ID of gene
#' @param symbol Gene symbol
#' @param normalized Boolean: whether or not to give the normalized counts rather than the raw counts
#' @param labelled Boolean: whether to label the samples
#' @param sample.data Data frame of sample data
#' @param x Column name from sample.data to be used for the x axis
#' @param color Column name from sample.data to be used for the color
#' @param color.scheme Named list giving the color mapping
#' @export
gene.plot.all <- function(entrez=NULL,
symbol=NULL,
normalized=TRUE,
labelled=FALSE,
sample.data=coldata,
x='treatment',
color='treatment',
color.scheme=NULL
){
suppressPackageStartupMessages(OK <- require(ggplot2))
if (!OK) stop("Error: ggplot2 package not found")
suppressPackageStartupMessages(OK <- require(org.Hs.eg.db))
if (!OK) stop("Error: org.Hs.eg.db package not found")
suppressPackageStartupMessages(OK <- require(DESeq2))
if (!OK) stop("Error: DESeq2 package not found")
if (!is.null(entrez)){
if (!(symbol %in% AnnotationDbi::keys(org.Mm.eg.db,keytype = 'ENTREZID'))){
return(gene.not.found.plot(symbol))
}
suppressMessages(
symbol <- AnnotationDbi::select(org.Mm.eg.db,entrez,keytype="ENTREZID",columns='SYMBOL')$SYMBOL
)
}
else{
if (!is.null(symbol)){
if (!(symbol %in% AnnotationDbi::keys(org.Mm.eg.db,keytype = 'SYMBOL'))){
return(gene.not.found.plot(symbol))
}
suppressMessages(
entrez <- AnnotationDbi::select(org.Mm.eg.db,symbol,keytype="SYMBOL",columns='ENTREZID')$ENTREZID
)
}
else{
stop("Need either an Entrez ID or a gene symbol as input")
}
}
if(all(as.numeric(countdata[entrez,]) == 0)){
df <- data.frame(
sample.data,
counts=0,
label=rownames(sample.data)
)
g <- ggplot(df,aes_string(x=x,y='counts',label='label',color=color)) + scale_y_continuous(limits=c(0,100))
if (labelled){
g <- g + geom_text()
}
g <- g + ggtitle(symbol) + labs("Raw counts")
return(g)
}
df <- data.frame(
sample.data,
counts=DESeq2::counts(dds,normalized=normalized)[entrez,rownames(sample.data)],
label=rownames(sample.data)
)
# gene.info <- info.from.entrez(entrez)
normString <- ifelse(normalized,"Normalized","Raw")
m <- max(df$counts)
g <- ggplot(df,aes_string(x=x,y='counts',label='label',color=color))
if (labelled){
g <- g + geom_text()
}
else{
g <- g + geom_jitter(height=0,width=0.1,size=3) #+
#scale_fill_manual(values=treatmentColors)
}
if (!is.null(color.scheme)){
color.values <- color.scheme[sample.data[,x]]
#g <- g + scale_fill_manual(values=color.values)
g <- g + scale_color_manual(values=color.values)
}
g <- g + ggtitle(symbol) + labs(y=paste(normString,'Counts')) + scale_y_continuous(limits=c(0,1.1*m))
g
}