-
Notifications
You must be signed in to change notification settings - Fork 1
/
mutationRegression.Rmd
217 lines (192 loc) · 8.09 KB
/
mutationRegression.Rmd
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
---
title: "Mutations"
author: "Sushil Sudhakar"
date: "10/25/2022"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
#install.packages("ape")
#install.packages("nlme")
#install.packages("rms")
#install.packages("phytools")
#install.packages("geiger")
#install.packages("caper")
#install.packages("tidyverse")
#install.packages("cowplot")
#install.packages("ggrepel")
#install.packages("ggsci")
#install.packages("patchwork")
library(ape)
library(nlme)
library(rms)
library(phytools)
library(geiger)
library(caper)
library(tidyverse)
library(cowplot)
library(ggrepel)
library(ggsci)
library(patchwork)
library(rr2)
```
```{r}
modPgls.SEy = function (model, data, corClass = corBrownian, tree, se = NULL,
method = c("REML", "ML"), interval = c(0, 1000), corClassValue=1, sig2e=NULL, ...)
{
Call <- match.call()
corfunc <- corClass
spp <- rownames(data)
data <- cbind(data, spp)
if (is.null(se))
se <- setNames(rep(0, Ntip(tree)), tree$tip.label)[spp]
else se <- se[spp]
lk <- function(sig2e, data, tree, model, ve, corfunc, spp) {
tree$edge.length <- tree$edge.length * sig2e
ii <- sapply(1:Ntip(tree), function(x, e) which(e ==
x), e = tree$edge[, 2])
tree$edge.length[ii] <- tree$edge.length[ii] + ve[tree$tip.label]
vf <- diag(vcv(tree))[spp]
w <- varFixed(~vf)
COR <- corfunc(corClassValue, tree, form = ~spp, ...)
fit <- gls(model, data = cbind(data, vf), correlation = COR,
method = method, weights = w)
-logLik(fit)
}
if (is.null(sig2e)) {
fit <- optimize(lk, interval = interval, data = data, tree = tree,
model = model, ve = se^2, corfunc = corfunc, spp = spp)
sig2e=fit$minimum
}
tree$edge.length <- tree$edge.length * sig2e
ii <- sapply(1:Ntip(tree), function(x, e) which(e == x),
e = tree$edge[, 2])
tree$edge.length[ii] <- tree$edge.length[ii] + se[tree$tip.label]^2
vf <- diag(vcv(tree))[spp]
w <- varFixed(~vf)
obj <- gls(model, data = cbind(data, vf), correlation = corfunc(corClassValue,
tree, form = ~spp, ...), weights = w, method = method)
obj$call <- Call
obj$sig2e <- sig2e
obj
}
#Internal function
pglsSEyPagelToOptimizeLambda=function(lambda,model,data,tree,...) {
-logLik(modPgls.SEy(model=model,data=data,tree=tree,corClassValue=lambda,corClass=corPagel,fixed=T,...)) #Returns -logLikelihood of the pgls.SEy model with lambda fixed to the value of the lambda argument. sig2e will be optimized within modPgls.SEy unless given as an argument here
}
#Function intended for users
pglsSEyPagel=function(model, data, tree, lambdaInterval=c(0,1),...){
optimizedModel=optimize(pglsSEyPagelToOptimizeLambda,lambdaInterval,model=model,data=data,tree=tree,...) #Optimizes lambda in the lambdaInterval using the pglsSEyPagelToOptimizeLambda function
return(modPgls.SEy(model=model,data=data,tree=tree,corClass=corPagel,fixed=T,corClassValue=optimizedModel$minimum,...)) #Returns the final model fit
}
```
```{r}
Data <- read.csv("Mutation_Data.csv")
tree <- read.tree("min20Fixed516.nwk")
View(Data)
```
this code takes in the .nwk file i sent and prunes the phylogeny so that it matches your dataset.
```{r}
Data$Species <- gsub(" ", "_", Data$Species)
Data$common_name<-gsub("_", "", Data$common_name)
includedSpecies<-Data$Species
pruned.tree<-drop.tip(
tree, setdiff(
tree$tip.label, includedSpecies))
pruned.tree <- keep.tip(pruned.tree,pruned.tree$tip.label)
Data$Keep <- Data$Species %in% pruned.tree$tip.label
Data <- Data[!(Data$Keep==FALSE),]
rownames(Data)<-Data$Species
SE<-setNames(Data$SE_simple,Data$Species)[rownames(Data)]
```
Construct the Regression Model
```{r}
mutation.neo<-pglsSEyPagel(NeoplasiaPrevalence~log10(Mean.mutation.rate..SBS.genome.year.),data=Data,tree=pruned.tree,se=SE,method = "ML")
```
```{r}
summary(mutation.neo)
###the code below extracts the stats from the above model to be used to construct the regression graph
r.v.mutation.neo <- R2(phy = pruned.tree,mutation.neo)
r.v.mutation.neo <- format(r.v.mutation.neo[3])
r.v.mutation.neo <-signif(as.numeric(r.v.mutation.neo), digits= 2)
ld.v.mutation.neo<- summary(mutation.neo)$modelStruct$corStruct
ld.v.mutation.neo <- signif(ld.v.mutation.neo[1], digits = 2)
p.v.mutation.neo<-summary(mutation.neo)$tTable
p.v.mutation.neo<-signif(p.v.mutation.neo[2,4], digits = 2)
```
```{r}
mutation.neo.plot<-ggplot(Data, aes(y=NeoplasiaPrevalence*100, x=log10(Mean.mutation.rate..SBS.genome.year.)))+
## I *think* that we only have mutation for Mammalia so you may need to change the line below
scale_color_manual(values = c("Mammalia" = "#631879FF"))+
scale_y_continuous(
limits = c(0,75),
breaks = c(0, 25,50,75),
labels = c(0, 25,50,75))+
geom_abline(intercept = coef(mutation.neo)[1]*100, slope = coef(mutation.neo)[2]*100,
color = 'grey',size = 1.2) +
theme_cowplot(12)+
theme(axis.title = element_text(size = 18))+
ylab("Neoplasia Prevalence (%)") +
xlab("(log10) Mutation Rate") +
geom_point(aes(colour= Clade, size = RecordsWithDenominators)) +
scale_size(name = "Total Necropsies",
breaks = c(20,100,200,300,477),
labels = c(20,100,200,300,477))+
geom_text_repel(aes(label=ifelse( NeoplasiaPrevalence > 0,as.character(common_name),'')),max.overlaps = Inf,size=5, direction = "y")+
guides(colour = guide_legend(override.aes = list(size=3))) +
labs(title = "A")+
theme(
plot.title = element_text(size = 20, face = "bold")) +
theme(legend.position = "bottom")+
labs(colour="Clade", size="Total Necropsies")
mutation.neo.plot
##this line will save the graph to the folder you are using as your working directory
#ggsave(filename='mutation.neo.pdf', width=9.5, height=7, limitsize=FALSE,bg="white")
```
Construct the Regression Model Malignancy
```{r}
mutation.mal<-pglsSEyPagel(MalignancyPrevalence~log10(Mean.mutation.rate..SBS.genome.year.),data=Data,tree=pruned.tree,se=SE,method = "ML")
```
```{r}
summary(mutation.mal)
###the code below extracts the stats from the above model to be used to construct the regression graph
r.v.mutation.mal <- R2(phy = pruned.tree,mutation.mal)
r.v.mutation.mal <- format(r.v.mutation.mal[3])
r.v.mutation.mal <-signif(as.numeric(r.v.mutation.mal), digits= 2)
ld.v.mutation.mal<- summary(mutation.mal)$modelStruct$corStruct
ld.v.mutation.mal <- signif(ld.v.mutation.mal[1], digits = 3)
p.v.mutation.mal<-summary(mutation.mal)$tTable
p.v.mutation.mal<-signif(p.v.mutation.mal[2,4], digits = 2)
```
```{r}
mutation.mal.plot<-ggplot(Data, aes(y=MalignancyPrevalence*100, x=log10(Mean.mutation.rate..SBS.genome.year.)))+
## I *think* that we only have mutation for Mammalia so you may need to change the line below
scale_color_manual(values = c("Mammalia" = "#631879FF"))+
scale_y_continuous(
limits = c(0,75),
breaks = c(0, 25,50,75),
labels = c(0, 25,50,75))+
geom_abline(intercept = coef(mutation.mal)[1]*100, slope = coef(mutation.mal)[2]*100,
color = 'grey',size = 1.2) +
theme_cowplot(12)+
theme(axis.title = element_text(size = 18))+
ylab("Malignancy Prevalence (%)") +
xlab("(log10) Mutation Rate") +
geom_point(aes(colour= Clade, size = RecordsWithDenominators)) +
scale_size(name = "Total Necropsies",
breaks = c(20,100,200,300,477),
labels = c(20,100,200,300,477))+
geom_text_repel(aes(label=ifelse( MalignancyPrevalence > 0,as.character(common_name),'')),max.overlaps = Inf,size=5, direction = "y")+
guides(colour = guide_legend(override.aes = list(size=3))) +
labs(title = "Malignancy Prevalence vs. Mutation Rate",
subtitle =bquote(p-value:.(p.v.mutation.mal)~R^2:.(r.v.mutation.mal)~Lambda:.(ld.v.mutation.mal))) +
theme(
plot.title = element_text(size = 20, face = "bold")) +
theme(legend.position = "bottom")+
labs(colour="Clade", size="Total Necropsies")
mutation.mal.plot
##this line will save the graph to the folder you are using as your working directory
ggsave(filename='S49mutation.mal.png', width=9.5, height=7, limitsize=FALSE,bg="white")
```