Repository navigation
Expand file tree
/
Copy pathsimBigMemory_lin.R
More file actions
executable file
·349 lines (244 loc) · 11.2 KB
/
Copy pathsimBigMemory_lin.R
File metadata and controls
executable file
·349 lines (244 loc) · 11.2 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
library(phangorn)
library(stringdist)
library(doParallel)
library(gplots)
source("simulation3_lin.R")
source("additionalFunctions.R")
source("cascadeFunctions.R")
os=system("cat ../os.txt",intern = T) #Local Mac repository (laptop)
if(os=="mac"){
git.path = "../GIT/"
}else if(os =="linux"){ #local linux subsystem (maybe under Windows10)
git.path = "../lineageSoftware/"
}else if(os=="aws"){ #AMAZON cloud computing server
git.path="../lineageSoftware/"
}
#Load fucntions from the main GIT repository (lineageSoftware)
source(paste(git.path,"MLfunctions.R",sep=""))
registerDoParallel(8)
rand.dist<-c(10, 26, 58, 120, 250, 506)
nRepeats = 20
# tHIS IS THE FUNCTION CURRENTLY CALLED BY bitVStrit.R
compareDist <- function(simulationType='trit',nGen=5,mu=0.3,alpha_=1/2,barcodeLength=20,nRepeats=20,methods=c(),
recType="integrase",nIntegrases=2){
results= foreach(i=1:nRepeats) %dopar% simMemoirStrdist(nGen=nGen,mu=mu,alpha=alpha_,barcodeLength=barcodeLength,methods=methods,simulationType=simulationType,nIntegrases = nIntegrases)
#let's unlist the results from the parallel calculations:
results_=list()
tree.list = list()
for(i in 1:length(results)){
results_[[i]] =results[[i]][[1]]
tree.list[[i]] = results[[i]][[2]]
} #this takes the first element of the results list which is the array with distance calculations
#save the simulated trees to the hard drive
simulation.file = paste("recType_",recType,"_mu_",toString(mu),
"_BC_",toString(barcodeLength),
"_nG_",toString(nGen),"_Nrep_",toString(nRepeats),"_.rda",sep="")
results.matrix=do.call(rbind,results_)
#Optional when only interested in the mean
#apply(results.matrix,2,mean)
# return(results.matrix)
return(list(results.matrix,tree.list))
}
# # # # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # CASCADE
#this does not work
compareDistCascade <- function(simulationType='trit',nGen=6,mu=0.3,alpha_=2/3,barcodeLength=40,nRepeats=20,methods=c(),
recType="integrase",nIntegrases=2){
results= foreach(i=1:nRepeats) %dopar% simMemoirStrdist(nGen=nGen,mu=mu,alpha=alpha_,barcodeLength=barcodeLength,methods=methods,simulationType=simulationType,nIntegrases = nIntegrases)
#let's unlist the results from the parallel calculations:
results_=list()
tree.list = list()
for(i in 1:length(results)){
results_[[i]] =results[[i]][[1]]
tree.list[[i]] = results[[i]][[2]]
} #this takes the first element of the results list which is the array with distance calculations
#put this inside the parallel loop
#just a test now
dist.cascade =array()
for(i in 1:length(tree.list)){
this.tree = tree.list[[i]]
barcodeLeaves= this.tree$tip.label
dist.cascade[i]=RF.dist(cascadeReconstruction(barcodeLeaves,totalInts=nIntegrases,currentInts=1,nGen=nGen),this.tree)
}
#old reconstruction
results.matrix=do.call(rbind,results_)
return(list(tree.list,results.matrix,dist))
}
#functions to use the proportion of perfect trees as the measure.
#May 8
eq.zero<-function(r,x){sum(r[,x]==0)}
#nGen=5;mu=0.4;alpha=1/2;barcodeLength=20;methods=c();simulationType='trit';recType="integrase";nIntegrases=2
#April 8th
#Test stringdistance measures using the stringdist R library
#use the same format as before but testing different methods included in the stringdist function
simMemoirStrdist<-function(nGen=3,mu=0.4,alpha=1/2,barcodeLength=10,methods=c(),
simulationType='trit',recType="integrase",nIntegrases=2){
#load necessary libraries and functions
#detection of OS
#update (trying to create a single branch that works in AWS and in my laptop)
os=system("cat ../os.txt",intern = T)
if(os=="mac"){ #local Alejandro's laptop
pathName="/Users/alejandrog/MEGA/Caltech/trees/simulation/"
pathName2="/Users/alejandrog/MEGA/Caltech/trees/simulation"
}else if(os=="linux"){ #AWS server or any other linux machine (path is for AWS)
pathName = "/home/agranado/MEGA/Caltech/trees/simulations/"
pathName2= "/home/agranado/MEGA/Caltech/trees/simulations"
}else if(os=="aws"){
#linux desktop
file.dir = "/home/alejandrog/MEGA/Caltech/trees/GraceData/integrase-data/10mer/"
#setwd("/home/alejandrog/MEGA/Caltech/trees/macbookBranch/lineageSoftware")
pathName2= "/home/alejandrog/MEGA/Caltech/lineage"
pathName= "/home/alejandrog/MEGA/Caltech/lineage/"
#At this point we are supposed to be in the local branch already, because we read the os.txt...
pathName="/home/ubuntu/alejandrog/Caltech/lineage/"
pathName2="/home/ubuntu/alejandrog/Caltech/lineage"
}
#clear the variable (since it behaves as global)
if(exists("firstCell")){
rm(firstCell)
}
firstCell<-Node$new("1"); firstCell$barcode <-paste(rep("u",barcodeLength),collapse="");
#all variables of the data.tree structure are global
#any pointer to a node, points to the real tree.
# SIMULATION
# # # # # # # # #
# # # # # # # #
# # # # # # # # #
#Here we implment 3 recording systems: 4 intgrases using 40 barcodes/10 each ; 2 integrases using 40 barcodes/ 20 each; and a single array using 40 barcodes
# actIntegrase=1 # start recording using integrase 1
#nIntegrases =4 # how many integrases comprise the cascade
#1 is the first integrase (for nomal memoir , this is the only integrase)
#We need to assign a span of time (in generation units for each integrases to be active)
#The idea is to divide the total number of generations between the N integrases:
#So for 7 generation and 4 integrases we want a vector of activity = c(1,1,2,2,3,3,4)
# cascadeActivation function takes care of this
act_time=cascadeActivation(nGen, nIntegrases)
for (g in 1:nGen){
#this function simulates one generation of the tree
actIntegrase = act_time[g]
divideCellRecursive2(firstCell,mu,alpha,type=simulationType,recType=recType,actIntegrase,nIntegrases)
}
trueTree<-as.phylo.Node(firstCell)
#get the sequences from the simulated tree + names
barcodes<-firstCell$Get("barcode")
#now we have the patters for ALL cells, but we need only the leaves, since that is what
#we are going to use for reconstruction.
#The way the tree was built, only the last 2^g cells are leaves; where g is the number of generations
#take the number ID for the leaves.
leavesID<-(length(barcodes)-2^nGen+1):length(barcodes)
#grab those cells from the tree
barcodeLeaves = array()
namesLeaves=array()
for (l in 1:length(leavesID)){
barcodeLeaves[l] <-barcodes[names(barcodes)==leavesID[l]]
namesLeaves[l] <-names(barcodes)[names(barcodes)==leavesID[l]]
}
names(barcodeLeaves)<-namesLeaves
#take the barcodes from internal nodes, not useful here but for saving the complete tree simulations
#
nodesID = as.numeric(trueTree$node.label) #THIS WORKS
barcodeNodes=array()
namesNodes=array()
for(l in 1:length(nodesID)){
barcodeNodes[l]<-barcodes[names(barcodes)==nodesID[l]]
namesNodes[l]<-names(barcodes)[names(barcodes)==nodesID[l]]
}
names(barcodeNodes)<-namesNodes
### SAVE TREE with NAMES
named.tree = trueTree
named.tree$tip.label = barcodeLeaves
named.tree$node.label = barcodeNodes
#now barcodeLeaves has all the leaves of the tree, with their original ID from the data.tree structure.
#create Fasta file using only the leaves of the tree (n= 2^g)
fastaBarcodes<-convertSimToFasta(barcodeLeaves)
#convert name of variable to string
#3 varName<-deparse(substitute(firstCell))
fasID =toString(runif(1))
#fasIN <-paste(pathName,"fasta/firstCell_",fasID,".fas",sep="")
fasIN =tempfile("fasta/firstCell",tmpdir = pathName2)
fasIN = paste(fasIN,fasID,".fas",sep="")
#randomize the barcodes such that order is not a factor in the lineage reconstruction
#the real tree (because the way is constructed, has an order 1,2,3,...N), if the barcodes are not
#randomized, the order will "help" to the reconstruction which is not good!
rand.barcode.order<-sample(1:length(fastaBarcodes))
#We re-order the barcodes using a fixed (but random) order
fastaBarcodes<-fastaBarcodes[rand.barcode.order]
barcodeLeaves<-barcodeLeaves[rand.barcode.order]
write(fastaBarcodes,file=fasIN)
sed.return<-convertMemoirToDNA(fasIN)
#now we can use the phyDat
if(sed.return){
memoirfas<-read.phyDat(fasIN,format="fasta",type="DNA")
#for distance based trees
#from the phangorn tutorial pdf:
}
#Apr 8th: this is where the distance comes into place
#Apr 8th:
#we calculate string distances only for the leaves ( which is the data we actually get)
allDistances = array()
# for(m in 1:length(methods)){
# stringTree= upgma(stringdistmatrix(barcodeLeaves,method=methods[m]))
# stringTree$tip.label<-trueTree$tip.label
# allDistances[m]= RF.dist(stringTree,trueTree)
# }
#control against default dist.ml function + UPGMA, which so far is the best method
m=0
if(sed.return==1){
dm<-dist.ml(memoirfas)
dm.ham=dist.hamming(memoirfas)
if(simulationType=='binary'){
treeUPGMA<-upgma(dm.ham)
}else{
treeUPGMA<-upgma(dm)
}
treeUPGMA_noSeq<-removeSeqLabel(treeUPGMA)
allDistances[m+1]= RF.dist(treeUPGMA_noSeq,trueTree)
}else{
allDistances[m+1]= NaN
}
matdist_ = manualDistML(barcodeLeaves,mu,alpha,nGen)
manualTree_ =upgma(as.dist(t(matdist_)))
manualTree_$tip.label= treeUPGMA$tip.label
allDistances[m+2]= RF.dist(removeSeqLabel(manualTree_),trueTree)
#try new distance using the built in dendrogram of heatmap2
#h=heatmap.2(matdist_+t(matdist_),trace="none",dendrogram = 'column')
# h=heatmap.2(matdist_+t(matdist_),Colv="Rowv")
# heatmap.tree=as.phylo(as.hclust(h$colDendrogram))
# heatmap.tree$tip.label = treeUPGMA$tip.label
# allDistances[m+3]= RF.dist(removeSeqLabel(heatmap.tree),trueTree)
#
#alternative w/o plotting the actual heatmap, only hclust method
# hclust.tree=as.phylo(hclust(as.dist(t(matdist_))))
# hclust.tree$tip.label = treeUPGMA$tip.label
# allDistances[m+3]= RF.dist(removeSeqLabel(hclust.tree),trueTree)
# CASCADE RECONSTRUCTION::::::::: Sep27
if(nIntegrases>1){
r=cascadeReconstruction(barcodeLeaves,totalInts=nIntegrases,currentInts=1,nGen,mu,alpha)
allDistances[m+3] = RF.dist(r,named.tree)
}else{
allDistances[m+3] = 0
}
#system(paste("rm ",firstCellFile,sep=""))
if(sed.return){
system(paste("rm ",paste(fasIN,".bak",sep=""),sep=""))
system(paste("rm ",fasIN,sep=""))
}
#system(paste("rm ",fasIN,".bak",sep=""))
return(list(allDistances,named.tree))
}
#END of simulation function
# # # # # # # # # #
# # # # # # # # #
# # # # # # # # # #
runThisScript <- function (){
#run memoirSim.R once and plot both trees.
x11()
par(mfrow=c(1,2))
source("/Users/alejandrog/MEGA/Caltech/trees/simulation/memoirSim.R")
ratioMat = manualDist(barcodeLeaves,mu,alpha,nGen );ratioTree = upgma(as.dist(t(ratioMat))); ratioTree$tip.label= treeUPGMA$tip.label;
plot(ratioTree,main=paste("Manual tree",toString(RF.dist(trueTree,removeSeqLabel(ratioTree))) ))
plot(treeUPGMA,main=paste("UPGMA dist tree",toString(RF.dist(trueTree,removeSeqLabel(treeUPGMA))) ))
#execute multiple trees to get statistics
# results= foreach(i=1:200) %dopar% simMemoirStrdist(3,0.3,alpha,barcodeLength,methods); results.matrix=do.call(rbind,results); meanDist =apply(results.matrix,2,mean)
}