-
Notifications
You must be signed in to change notification settings - Fork 2
/
PCRcoal.R
217 lines (179 loc) · 5.79 KB
/
PCRcoal.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
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
#
# 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/>.
setClass(
'PCRcoal',
representation(
initial.size ="numeric",
nr.cycles ="numeric",
efficiencies ="numeric",
sample.size ="numeric",
max.tries ="numeric"
),
prototype(
),
sealed=TRUE
);
setGeneric("PCRcoal", function(object, ... ) standardGeneric("PCRcoal"));
setMethod(
"PCRcoal",
c("missing"),
function(
initial.size,
nr.cycles,
efficiencies,
sample.size,
max.tries
) {
this<-new("PCRcoal");
if(!missing(initial.size)){
[email protected] <-initial.size;
}
if(!missing(nr.cycles)){
[email protected] <-nr.cycles;
}
if(!missing(sample.size)){
[email protected] <-sample.size;
}
if(!missing(efficiencies)){
this@efficiencies <-efficiencies;
} else if (!missing(nr.cycles)){
# The default is an efficiency of 1.0 in all cycles:
this@efficiencies<-rep(1.0, nr.cycles)
}
if(!missing(max.tries)){
[email protected]<-max.tries
} else {
[email protected] <- 100
}
return(this);
}
);
setGeneric("sample.tree", function(object, ... ) standardGeneric("sample.tree"));
setMethod(
"sample.tree",
c("PCRcoal"),
function(
object
) {
# Sample size trajectory:
size.trajectory <- .sampleTrajectory(object);
# Sample subsample sizes:
subsamples <- .sampleMolecules(object, size.trajectory);
# Sample tree:
tree <- .sampleTree(object, size.trajectory, subsamples);
# Bless & reorder tree:
class(tree) <- "phylo"
tree <- reorder(tree)
return(tree);
}
);
setGeneric("sample.tnt", function(object, ... ) standardGeneric("sample.tnt"));
setMethod(
"sample.tnt",
c("PCRcoal"),
function(
object
) {
# Sample size trajectory:
size.trajectory <- .sampleTrajectory(object);
# Sample subsample sizes:
subsamples <- .sampleMolecules(object, size.trajectory);
# Sample tree:
tree <- .sampleTree(object, size.trajectory, subsamples);
# Bless & reorder tree:
class(tree) <- "phylo"
tree <- reorder(tree)
return(
list(
"phylo" = tree,
"trajectories" = size.trajectory,
"subsamples" = subsamples
)
);
}
);
setGeneric("sample.trs", function(object, ... ) standardGeneric("sample.trs"));
setMethod(
"sample.trs",
c("PCRcoal"),
function(
object
) {
# Sample size trajectory:
size.trajectory <- .sampleTrajectory(object);
# Sample subsample sizes:
subsamples <- .sampleMolecules(object, size.trajectory);
return(
list(
"trajectories" = size.trajectory,
"subsamples" = subsamples
)
);
}
);
.checkInput<-function(object){
# Missing params:
if(length([email protected]) == 0){
stop("Cannot sample trajectory: initial size is missing!")
}
else {
.check.integer([email protected],"initial size")
}
if(length([email protected]) == 0){
stop("Cannot sample trajectory: the number of cycles is missing!")
}
else {
.check.integer([email protected],"the number of cycles!")
}
if(length([email protected]) == 0){
stop("Cannot sample trajectory: the sample size is missing!")
}
else {
.check.integer([email protected],"the sample size")
}
if(length(object@efficiencies) == 0){
stop("Cannot sample trajectory: the efficiencies are missing!")
}
else if (!is.numeric(object@efficiencies)){
stop("Cannot sample trajectory: the efficiencies must be numeric!")
}
else if (any(object@efficiencies > 1.0)){
stop("Cannot sample trajectory: the efficiencies must be <= 1!")
}
.check.integer([email protected],"max.tries")
# Dangerous situation by experience, causes integer overflow:
if(all(object@efficiencies == 1.0) && ([email protected] > 31)){
stop("Simulation failed: all efficiencies are 1.0 and the number of cycles is larger than 31!");
}
}
.is.wholenumber <- function(x, tol = .Machine$double.eps^0.5){
abs(x -round(x)) < tol
}
.check.integer <- function (obj, name){
if(!.is.wholenumber(obj)){
stop(paste("\nCannot sample trajectory: the value of ",name," slot (",obj,") is not an integer!\n\n", sep=""))
}
}