-
Notifications
You must be signed in to change notification settings - Fork 2
/
SampleTrajMol.R
103 lines (80 loc) · 3 KB
/
SampleTrajMol.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
#
# Copyright (C) 2013 EMBL - European Bioinformatics Institute
#
# This program is free software: you can redistribute it
# and/or modify it under the terms of the GNU General
# Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# Neither the institution name nor the name pcrcoal
# can be used to endorse or promote products derived from
# this software without prior written permission. For
# written permission, please contact <[email protected]>.
# Products derived from this software may not be called
# pcrcoal nor may pcrcoal appear in their
# names without prior written permission of the developers.
# You should have received a copy of the GNU General Public
# License along with this program. If not, see
# <http://www.gnu.org/licenses/>.
# Sample a size trajectories for each sub-population
# (descendants of a single starting molecule)
.sampleTrajectory<-function(this){
# Check if everything is sane about the object:
.checkInput(this)
# Recycle efficiency vector:
eff <- rep(this@efficiencies, [email protected])
# Construct trajectory matrix:
traj.mat<-matrix(
[email protected], # a row for each starting molecule
[email protected] + 1 # a column for each "generation"
);
tries <- 1
# Try to sample a sane subsample size vector. Give up after max.tries times.
while (TRUE) {
if(tries >= [email protected]){
stop("\n\nTried ",tries," times to sample trajectories, but still could not obtain a large enough final pool size.\nAborting simulation. You should try again with higher efficiencies/cycle numbers/max.tries.\n\n")
}
# Initialize molecules:
traj.mat[,1]<-1;
for(i in 1:nrow(traj.mat)){
for(j in 2:ncol(traj.mat)){
prev.size <- traj.mat[i, j-1]
traj.mat[i,j]<- prev.size + rbinom(n=1, size=prev.size,prob=eff[j-1])
}
}
# Sanity checks:
if(any(is.nan(traj.mat))){
stop("NaN elements in the trajectory matrix! Maybe the cycle number is too high?")
}
# Check if final size is large enough:
final.size<-sum(traj.mat[,ncol(traj.mat)])
if([email protected] > final.size){
# Try again
tries <- tries + 1
} else {
return(traj.mat)
}
}
}
.sampleMolecules<-function(this, size.traj){
final.sizes<-as.numeric(size.traj[,ncol(size.traj)])
probs<-final.sizes/sum(final.sizes)
while(TRUE) {
subsamples<-as.numeric(rmultinom(
n=1,
prob=probs
) )
# Multinomial sampling can result in too large subsamples:
if(all(subsamples <= final.sizes)){
return(subsamples)
}
}
}