-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMutationEnrichment_hist.R
More file actions
executable file
·148 lines (131 loc) · 5.69 KB
/
MutationEnrichment_hist.R
File metadata and controls
executable file
·148 lines (131 loc) · 5.69 KB
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
#!/usr/bin/Rscript
# Enrichment of Mutations in and around Motifs
# Tahmid Mehdi, Princess Margaret Cancer Centre - UHN, Dec 21, 2015
args <- commandArgs(trailingOnly = TRUE)
workingDir <- args[1]
mutations <- args[2] # bed file with mutation locations
motifs <- args[3] # bed file with motif locations
left <- as.numeric(args[4]) # length of left flanking sequence
right <- as.numeric(args[5]) # length of right flanking sequence
imgName <- args[6] # name of output image
imgWidth <- as.numeric(args[7]) # image width
imgHeight <- as.numeric(args[8]) # image height
tf <- args[9] # transcription factor whose motif is being plotted
setwd("/Users/lupienlab3/Documents/tahmid/PCa")
library("GenomicRanges")
library("ggplot2")
library("stringr")
# From http://davetang.org/muse/2015/02/04/bed-granges/
bed_to_granges <- function(file){
df <- read.table(file,
header=F,
stringsAsFactors=F)[,1:4]
if(length(df) > 6){
df <- df[,-c(7:length(df))]
}
if(length(df)<3){
stop("File has less than 3 columns")
}
header <- c('chr','start','end','strand','id','score')
names(df) <- header[1:length(names(df))]
if('strand' %in% colnames(df)){
df$strand <- gsub(pattern="[^+-]+", replacement = '*', x = df$strand)
}
if(length(df)==3){
gr <- with(df, GRanges(chr, IRanges(start, end)))
} else if (length(df)==4){
gr <- with(df, GRanges(chr, IRanges(start, end), strand=strand))
} else if (length(df)==5){
gr <- with(df, GRanges(chr, IRanges(start, end), id=id, score=score))
} else if (length(df)==6){
gr <- with(df, GRanges(chr, IRanges(start, end), id=id, score=score, strand=strand))
}
return(gr)
}
# like bed_to_granges but for mutation files
mut_to_granges <- function(file){
df <- read.table(file,
header=F,
stringsAsFactors=F)[,1:4]
if(length(df) > 6){
df <- df[,-c(7:length(df))]
}
if(length(df)<3){
stop("File has less than 3 columns")
}
header <- c('chr','start','end','id','strand','score')
names(df) <- header[1:length(names(df))]
if('strand' %in% colnames(df)){
df$strand <- gsub(pattern="[^+-]+", replacement = '*', x = df$strand)
}
if(length(df)==3){
gr <- with(df, GRanges(chr, IRanges(start, end)))
} else if (length(df)==4){
gr <- with(df, GRanges(chr, IRanges(start, end), id=id))
} else if (length(df)==5){
gr <- with(df, GRanges(chr, IRanges(start, end), id=id, score=score))
} else if (length(df)==6){
gr <- with(df, GRanges(chr, IRanges(start, end), id=id, score=score, strand=strand))
}
return(gr)
}
# converts substitution types to uppercase & reverse complements if neccessary
convertSubType = function(type) {
type <- toupper(type)
if (type=="A>C") {return("T>G")
} else if (type=="A>G") {return("T>C")
} else if (type=="A>T") {return("T>A")
} else if (type=="G>A") {return("C>T")
} else if (type=="G>C") {return("C>G")
} else if (type=="G>T") {return("C>A")
} else {return(type)}
}
convertSubTypes = Vectorize(convertSubType)
# create GRanges objects
mut <- mut_to_granges(mutations)
mot <- bed_to_granges(motifs)
motWidth <- width(mot)[1]
# extend motifs
flankedMot <- punion(punion(mot, flank(mot,left)), flank(mot,right,start=FALSE))
flankedMotWidth <- width(flankedMot)[1]
endMot <- flankedMotWidth - right + 0.5
mut <- mut[unique(queryHits(findOverlaps(mut, flankedMot)))]
# get nearest motif neighbours for each mutation
nn <- nearest(mut,mot,ignore.strand=TRUE)
# calculate distances of mutations from 5' ends of motif flanking regions & track substitution types
mutInMot <- findOverlaps(mut, mot)
if (length(mutInMot)==0) {
distances <- c()
types <- c()
} else {
distances <- sapply(1:length(mutInMot),
function(i) if(as.character(strand(flankedMot)[subjectHits(mutInMot)[i]])=="+") {
start(mut)[queryHits(mutInMot)[i]] - start(flankedMot)[subjectHits(mutInMot)[i]] + 1
} else { end(flankedMot)[subjectHits(mutInMot)[i]] - start(mut)[queryHits(mutInMot)[i]] + 1 })
types <- mut[queryHits(mutInMot)]$id
}
mutNotInMot <- which(!(1:length(mut) %in% queryHits(mutInMot)))
distances <- c(distances, sapply(mutNotInMot,
function(i) if(as.character(strand(flankedMot)[nn[i]])=="+") {
start(mut)[i] - start(flankedMot)[nn[i]] + 1
} else { end(flankedMot)[nn[i]] - start(mut)[i] + 1 }))
types <- c(types, mut[mutNotInMot]$id)
types <- unname(convertSubTypes(types))
# create data frame with distances and types
mutDist <- data.frame(distances, types)
# filter distances
mutDist <- mutDist[1<=mutDist$distances & mutDist$distances<=flankedMotWidth, ]
# colours for histograms & streamgraphs
z <- c("deepskyblue2", "#252525", "firebrick2", "gray", "chartreuse3", "rosybrown1")
# save image to current directory
pdf(paste(imgName,"_hist.pdf", sep=""), width=imgWidth, height=imgHeight, res=3000)
ggplot(mutDist, aes(distances, fill=types)) + geom_bar() +
scale_x_continuous(breaks=c(1,flankedMotWidth), labels=c(-left, right)) + theme_bw() +
theme(panel.grid.minor=element_blank(),panel.grid.major=element_blank(),
axis.title.x=element_text(hjust=((left+motWidth/2)/flankedMotWidth))) +
geom_vline(xintercept=left+0.5, lty=2) + geom_vline(xintercept=endMot, lty=2) + guides(fill=guide_legend(title=NULL)) +
# geom_vline(xintercept=left+11+0.5) + geom_vline(xintercept=endMot-11) +
# stat_smooth(method="loess", formula=df$freq~as.numeric(df$pos), size=1, aes(group=1), se=FALSE, span=0.25) +
labs(x=paste(tf," + flanking region (bp)", sep=""), title=paste("N=",length(mot)," motif occurences", sep=""), y="Number of Mutations") +
scale_fill_manual(values=z)
dev.off()