-
Notifications
You must be signed in to change notification settings - Fork 0
/
RERconvergeScript.R
151 lines (124 loc) · 4.24 KB
/
RERconvergeScript.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
#rerun RERconverge
trait=read.table("Boreoeutheria_VLtrait_edited_converted.txt", sep="\t")
# make a named vector instead of a table
traitVals=trait$V2
names(traitVals)=trait$V1
trees=readRDS("zoonomiatrees.rds")
specstouse=trait$V1[!is.na(trait$V2)]
specstouse=specstouse[specstouse%in%trees$masterTree$tip.label]
#remove human
#specstouse=specstouse[!specstouse%in%c("REFERENCE")]
#saveRDS(specstouse, "specstouse.rds")
specstouse=readRDS("specstouse.rds")
#re-calculate RERs (without human)
#RERs=getAllResiduals(trees, useSpecies=specstouse)
#saveRDS(RERs, "VocalLearningRERs.rds")
RERs = readRDS("VocalLearningRERs.rds")
# VLpath=foreground2Paths(trait$V1[trait$V2==1], trees, clade="all", useSpecies = specstouse)
fgdspecs = names(traitVals[specstouse][traitVals[specstouse]==1])
VLpath=foreground2Paths(fgdspecs, trees, clade="all", useSpecies = specstouse)
#cors=correlateWithBinaryPhenotype(RERs, VLpath)
#saveRDS(cors, "VocalLearningCors.rds")
cors=readRDS("VocalLearningCors.rds")
#permulations
#my permulations
cors=cors
#bf=readRDS("VLBayesFactors.rds")
RERs=readRDS("VocalLearningRERs.rds")
#trees=readRDS("../NewComputerSpaceSaver/Zoonomia/zoonomiatrees.rds")
#trait=read.table("Boreoeutheria_VLtrait_edited_converted.txt", sep="\t")
# VLtrait=trait$V2
# names(VLtrait)=trait$V1
# VLtrait=VLtrait[!is.na(VLtrait)]
#t=foreground2Tree(names(VLtrait)[VLtrait==1], trees, plotTree=T, clade="all", useSpecies = names(VLtrait))
t=foreground2Tree(fgdspecs, trees, plotTree=T, clade="all", useSpecies = specstouse)
sum(t$edge.length) #45 total
sum(traitVals[specstouse]) #24 tip
sum(t$edge.length)-sum(traitVals[specstouse]) #21 internal
# trees=trees
#root="REFERENCE"
root = "vs_gorGor6" # closest species to human that has trait data
phenvec=traitVals[specstouse]
fgnum = 45
internal=21
drop = trees$masterTree$tip.label[!(trees$masterTree$tip.label %in% specstouse)]
statdf=NULL
pvaldf=NULL
count=1
while(count<=1000){
#get phenotype:
blsum=0
if(is.null(fgnum)){
fgnum=sum(phenvec)
}
tips=fgnum-internal
t=drop.tip(trees$masterTree, drop)
t=root.phylo(t, root, resolve.root = T)
# while(blsum!=fgnum){
while(blsum>(fgnum+5) | blsum<(fgnum-5)){
rm=ratematrix(t, phenvec)
sims=sim.char(t, rm, nsim = 1)
nam=rownames(sims)
s=as.data.frame(sims)
simulatedvec=s[,1]
names(simulatedvec)=nam
top=names(sort(simulatedvec, decreasing = TRUE))[1:tips]
tf=foreground2Tree(top, trees, clade="all", plotTree = F)
blsum=sum(tf$edge.length)
}
#get path:
p=tree2Paths(tf, trees, useSpecies = specstouse)
#run correlation:
c=correlateWithBinaryPhenotype(RERs, p)
if(count==1){
statdf=data.frame(c$Rho)
rownames(statdf)=rownames(c)
pvaldf=data.frame(c$P)
rownames(pvaldf)=rownames(c)
}else{
temp=data.frame(c$Rho)
rownames(temp)=rownames(c)
statdf=merge(statdf, temp, by="row.names", all = T)
rownames(statdf)=statdf$Row.names
statdf=statdf[,-1]
temp=data.frame(c$P)
rownames(temp)=rownames(c)
pvaldf=merge(pvaldf, temp, by="row.names", all = T)
rownames(pvaldf)=pvaldf$Row.names
pvaldf=pvaldf[,-1]
}
print(paste0("finished perm: ", count))
count=count+1
}
saveRDS(statdf, "VLstatdf.rds")
saveRDS(pvaldf, "VLpvaldf.rds")
#get perm p-val
statdf=readRDS("VLstatdf.rds")
temp=data.frame(cors$Rho)
rownames(temp)=rownames(cors)
realandperm=merge(temp, statdf, by="row.names")
rownames(realandperm)=realandperm$Row.names
realandperm=realandperm[,-1]
permpval=data.frame(matrix(nrow=nrow(realandperm), ncol=1))
colnames(permpval)="permP"
rownames(permpval)=rownames(realandperm)
count=1
while(count<=nrow(realandperm)){
real=realandperm[count,1]
perms=as.numeric(realandperm[count,2:ncol(realandperm)])
perms=perms[!is.na(perms)]
p=sum(abs(perms)>abs(real))/length(perms)
permpval[count, 1]=p
print(count)
count=count+1
}
VLcorswithpermp=cors
VLcorswithpermp=merge(VLcorswithpermp, permpval, by="row.names")
rownames(VLcorswithpermp)=VLcorswithpermp$Row.names
VLcorswithpermp=VLcorswithpermp[,-1]
VLcorswithpermp$permP.adj=p.adjust(VLcorswithpermp$permP, method="BH")
saveRDS(VLcorswithpermp, "VLcorswithpermp.rds")
#add Bayes factors
# allVLres=merge(VLcorswithpermp, bf, by="row.names", all=T)
# saveRDS(allVLres, "allVLres.rds")
# write.csv(allVLres, "allVLres.csv")