... | ... |
@@ -282,7 +282,7 @@ rfitness <- function(g, c= 0.5, |
282 | 282 |
min = 1e-10, |
283 | 283 |
max = 1e-9) |
284 | 284 |
} |
285 |
- m <- cbind(m, Fitness = fi) |
|
285 |
+ m <- cbind(m, Birth = fi) |
|
286 | 286 |
if(!is_null_na(min_accessible_genotypes)) { |
287 | 287 |
## num_accessible_genotypes <- count_accessible_g(m, accessible_th) |
288 | 288 |
## Recall accessibleGenotypes includes the wt, if accessible. |
... | ... |
@@ -232,14 +232,28 @@ rfitness <- function(g, c= 0.5, |
232 | 232 |
} |
233 | 233 |
} else { ## a length-3 scale |
234 | 234 |
if(scale[2] > scale[3]) warning("In scale, minimum fitness > wildtype") |
235 |
+ if(scale[1] < scale[3]) warning("In scale, maximum fitness < wildtype") |
|
235 | 236 |
fiwt <- fi[1] |
236 |
- prod_above <- (scale[1] - scale[3]) / (max(fi) - fiwt) |
|
237 |
- prod_below <- (scale[3] - scale[2]) / (fiwt - min(fi)) |
|
238 |
- fi_above <- which(fi >= fiwt) |
|
239 |
- fi_below <- which(fi < fiwt) |
|
240 |
- fi[fi_above] <- ((fi[fi_above] - fiwt) * prod_above) + scale[3] |
|
241 |
- fi[fi_below] <- ((fi[fi_below] - fiwt) * prod_below) + scale[3] |
|
242 |
- fi[1] <- scale[3] |
|
237 |
+ new_fi <- rep(NA, length(fi)) |
|
238 |
+ mode(new_fi) <- "numeric" |
|
239 |
+ ## If WT is min or max, there are no below or above |
|
240 |
+ if(max(fi) == fiwt) |
|
241 |
+ warning("WT has maximum fitness. Range will be from scale[2] to scale[3]") |
|
242 |
+ if(min(fi) == fiwt) |
|
243 |
+ warning("WT has minimum fitness. Range will be from scale[3] to scale[1]") |
|
244 |
+ if(max(fi) > fiwt) { |
|
245 |
+ prod_above <- (scale[1] - scale[3]) / (max(fi) - fiwt) |
|
246 |
+ fi_above <- which(fi >= fiwt) |
|
247 |
+ new_fi[fi_above] <- ((fi[fi_above] - fiwt) * prod_above) + scale[3] |
|
248 |
+ } |
|
249 |
+ if(min(fi) < fiwt) { |
|
250 |
+ prod_below <- (scale[3] - scale[2]) / (fiwt - min(fi)) |
|
251 |
+ fi_below <- which(fi < fiwt) |
|
252 |
+ new_fi[fi_below] <- ((fi[fi_below] - fiwt) * prod_below) + scale[3] |
|
253 |
+ } |
|
254 |
+ new_fi[1] <- scale[3] |
|
255 |
+ fi <- new_fi |
|
256 |
+ rm(new_fi) |
|
243 | 257 |
} |
244 | 258 |
|
245 | 259 |
if(log) { |
... | ... |
@@ -202,48 +202,66 @@ rfitness <- function(g, c= 0.5, |
202 | 202 |
## rm(m1) |
203 | 203 |
## rm(fl1) |
204 | 204 |
} |
205 |
- |
|
206 |
- if(!is.null(scale)) { |
|
207 |
- fi <- (fi - min(fi))/(max(fi) - min(fi)) |
|
208 |
- fi <- scale[1] + fi * (scale[2] - scale[1]) |
|
209 |
- } |
|
210 |
- if(wt_is_1 == "divide") { |
|
211 |
- ## We need to shift to avoid ratios involving negative numbers and |
|
212 |
- ## we need to avoid having any fitness at 0, specially the wt. If |
|
213 |
- ## any negative value, add the min, and shift by the magnitude of |
|
214 |
- ## the min to avoid any at 0. |
|
215 |
- |
|
216 |
- ## If you use scale and wt_is_1, this will move the scale. It is |
|
217 |
- ## not possible to obtain a linear transformation that will keep |
|
218 |
- ## the min and max of the scale, and wt at 1. |
|
219 |
- min_fi <- min(fi) |
|
220 |
- if(min_fi < 0) |
|
221 |
- fi <- fi + 2 * abs(min(fi)) |
|
222 |
- fi <- fi/fi[1] |
|
223 |
- } else if (wt_is_1 == "subtract") { |
|
224 |
- fi <- fi - fi[1] + 1.0 |
|
225 |
- } else if(wt_is_1 == "force") { |
|
226 |
- fi[1] <- 1.0 |
|
205 |
+ if(!(length(scale) == 3)) { |
|
227 | 206 |
if(!is.null(scale)) { |
228 |
- if( (1 > scale[2]) || (1 < scale[1])) |
|
229 |
- warning("Using wt_is_1 = force and scale, but scale does ", |
|
230 |
- "not include 1") |
|
207 |
+ fi <- (fi - min(fi))/(max(fi) - min(fi)) |
|
208 |
+ fi <- scale[1] + fi * (scale[2] - scale[1]) |
|
209 |
+ } |
|
210 |
+ if(wt_is_1 == "divide") { |
|
211 |
+ ## We need to shift to avoid ratios involving negative numbers and |
|
212 |
+ ## we need to avoid having any fitness at 0, specially the wt. If |
|
213 |
+ ## any negative value, add the min, and shift by the magnitude of |
|
214 |
+ ## the min to avoid any at 0. |
|
215 |
+ |
|
216 |
+ ## If you use scale and wt_is_1, this will move the scale. It is |
|
217 |
+ ## not possible to obtain a linear transformation that will keep |
|
218 |
+ ## the min and max of the scale, and wt at 1. |
|
219 |
+ min_fi <- min(fi) |
|
220 |
+ if(min_fi < 0) |
|
221 |
+ fi <- fi + 2 * abs(min(fi)) |
|
222 |
+ fi <- fi/fi[1] |
|
223 |
+ } else if (wt_is_1 == "subtract") { |
|
224 |
+ fi <- fi - fi[1] + 1.0 |
|
225 |
+ } else if(wt_is_1 == "force") { |
|
226 |
+ fi[1] <- 1.0 |
|
227 |
+ if(!is.null(scale)) { |
|
228 |
+ if( (1 > scale[2]) || (1 < scale[1])) |
|
229 |
+ warning("Using wt_is_1 = force and scale, but scale does ", |
|
230 |
+ "not include 1") |
|
231 |
+ } |
|
231 | 232 |
} |
233 |
+ } else { ## a length-3 scale |
|
234 |
+ if(scale[2] > scale[3]) warning("In scale, minimum fitness > wildtype") |
|
235 |
+ fiwt <- fi[1] |
|
236 |
+ prod_above <- (scale[1] - scale[3]) / (max(fi) - fiwt) |
|
237 |
+ prod_below <- (scale[3] - scale[2]) / (fiwt - min(fi)) |
|
238 |
+ fi_above <- which(fi >= fiwt) |
|
239 |
+ fi_below <- which(fi < fiwt) |
|
240 |
+ fi[fi_above] <- ((fi[fi_above] - fiwt) * prod_above) + scale[3] |
|
241 |
+ fi[fi_below] <- ((fi[fi_below] - fiwt) * prod_below) + scale[3] |
|
242 |
+ fi[1] <- scale[3] |
|
232 | 243 |
} |
244 |
+ |
|
233 | 245 |
if(log) { |
246 |
+ if(length(scale == 3)) { |
|
247 |
+ wt_is_1 <- "no" |
|
248 |
+ message("You passed a three-element scale argument. ", |
|
249 |
+ "Setting wt_is_1 to no ", |
|
250 |
+ "to avoid modifying your requested value for WT.") |
|
251 |
+ } |
|
234 | 252 |
## If you want logs, you certainly do not want |
235 | 253 |
## the log of a negative number |
236 | 254 |
fi[fi < 0] <- 0 |
237 | 255 |
if(wt_is_1 == "no") { |
238 | 256 |
fi <- log(fi) |
239 |
- } else { |
|
240 |
- ## by decree, fitness of wt is 1. So shift everything |
|
241 |
- fi <- log(fi) + 1 |
|
242 |
- } |
|
243 |
- ## former expression, but it was more confusing |
|
257 |
+ } else { |
|
258 |
+ ## by decree, fitness of wt is 1. So shift everything |
|
259 |
+ fi <- log(fi) + 1 |
|
260 |
+ } |
|
261 |
+ ## former expression, but it was more confusing |
|
244 | 262 |
## fi <- log(fi/fi[1]) + 1 |
245 |
- |
|
246 | 263 |
} |
264 |
+ |
|
247 | 265 |
if(truncate_at_0) { |
248 | 266 |
## yes, truncate but add noise to prevent identical |
249 | 267 |
fi[fi <= 0] <- runif(sum(fi <= 0), |
... | ... |
@@ -279,110 +279,3 @@ rfitness <- function(g, c= 0.5, |
279 | 279 |
|
280 | 280 |
|
281 | 281 |
|
282 |
- |
|
283 |
- |
|
284 |
- |
|
285 |
- |
|
286 |
-## rfitness <- function(g, c= 0.5, |
|
287 |
-## sd = 1, |
|
288 |
-## mu = 1, |
|
289 |
-## reference = "random", ## "random", "max", or the vector, |
|
290 |
-## ## e.g., rep(1, g). If random, a |
|
291 |
-## ## random genotype is chosen as |
|
292 |
-## ## reference. If "max" this is rep(1, g) |
|
293 |
-## scale = NULL, ## a two-element vector: min and max |
|
294 |
-## wt_is_1 = c("subtract", "divide", "force", "no"), |
|
295 |
-## ## wt_is_1 = TRUE, ## wt has fitness 1 |
|
296 |
-## log = FALSE, ## log only makes sense if all values > |
|
297 |
-## ## 0. scale with min > 0, and/or set |
|
298 |
-## ## wt_is_1 = divide |
|
299 |
-## min_accessible_genotypes = NULL, |
|
300 |
-## accessible_th = 0, |
|
301 |
-## truncate_at_0 = TRUE) { |
|
302 |
-## ## Like Franke et al., 2011 and others of Krug. Very similar to Greene |
|
303 |
-## ## and Crona, 2014. And this allows moving from HoC to purely additive |
|
304 |
-## ## changing c and sd. |
|
305 |
- |
|
306 |
-## ## FIXME future: do this with order too? |
|
307 |
-## ## - do not generate a matrix of genotypes but a matrix of ordered genot. |
|
308 |
-## wt_is_1 = match.arg(wt_is_1) |
|
309 |
-## if(is_null_na(g)) stop("Number of genes argument (g) is null or NA") |
|
310 |
-## m <- generate_matrix_genotypes(g) |
|
311 |
-## done <- FALSE |
|
312 |
-## ## attempts <- 0 ## for debugging/tracking purposes |
|
313 |
-## while(!done) { |
|
314 |
-## ## attempts <- attempts + 1 |
|
315 |
-## f_r <- rnorm(nrow(m), mean = mu, sd = sd) |
|
316 |
-## if(inherits(reference, "character") && length(reference) == 1) { |
|
317 |
-## if(reference == "random") { |
|
318 |
-## referenceI <- m[sample(nrow(m), 1), ] |
|
319 |
-## } else if(reference == "max") { |
|
320 |
-## referenceI <- rep(1, g) |
|
321 |
-## } else if(reference == "random2") { |
|
322 |
-## referenceI <- create_eq_ref(g) |
|
323 |
-## } |
|
324 |
-## } else { |
|
325 |
-## referenceI <- reference |
|
326 |
-## } |
|
327 |
-## d_reference <- apply(m, 1, function(x) sum(abs(x - referenceI))) |
|
328 |
-## f_det <- -c * d_reference |
|
329 |
-## ## f_det <- rowSums(m) * slope/nrow(m) ## this is Greene and Krona |
|
330 |
-## fi <- f_r + f_det |
|
331 |
- |
|
332 |
-## if(!is.null(scale)) { |
|
333 |
-## fi <- (fi - min(fi))/(max(fi) - min(fi)) |
|
334 |
-## fi <- scale[1] + fi * (scale[2] - scale[1]) |
|
335 |
-## } |
|
336 |
-## if(wt_is_1 == "divide") { |
|
337 |
-## ## We need to shift to avoid ratios involving negative numbers and |
|
338 |
-## ## we need to avoid having any fitness at 0, specially the wt. If |
|
339 |
-## ## any negative value, add the min, and shift by the magnitude of |
|
340 |
-## ## the min to avoid any at 0. |
|
341 |
- |
|
342 |
-## ## If you use scale and wt_is_1, this will move the scale. It is |
|
343 |
-## ## not possible to obtain a linear transformation that will keep |
|
344 |
-## ## the min and max of the scale, and wt at 1. |
|
345 |
-## min_fi <- min(fi) |
|
346 |
-## if(min_fi < 0) |
|
347 |
-## fi <- fi + 2 * abs(min(fi)) |
|
348 |
-## fi <- fi/fi[1] |
|
349 |
-## } else if (wt_is_1 == "subtract") { |
|
350 |
-## fi <- fi - fi[1] + 1.0 |
|
351 |
-## } else if(wt_is_1 == "force") { |
|
352 |
-## fi[1] <- 1.0 |
|
353 |
-## if(!is.null(scale)) { |
|
354 |
-## if( (1 > scale[2]) || (1 < scale[1])) |
|
355 |
-## warning("Using wt_is_1 = force and scale, but scale does ", |
|
356 |
-## "not include 1") |
|
357 |
-## } |
|
358 |
-## } |
|
359 |
-## if(truncate_at_0) { |
|
360 |
-## fi[fi <= 0] <- 1e-9 |
|
361 |
-## } |
|
362 |
-## if(log) { |
|
363 |
-## fi <- log(fi/fi[1]) + 1 |
|
364 |
-## } |
|
365 |
-## m <- cbind(m, Fitness = fi) |
|
366 |
-## if(!is_null_na(min_accessible_genotypes)) { |
|
367 |
-## ## num_accessible_genotypes <- count_accessible_g(m, accessible_th) |
|
368 |
-## ## Recall accessibleGenotypes includes the wt, if accessible. |
|
369 |
-## num_accessible_genotypes <- length(wrap_accessibleGenotypes(m, accessible_th)) - 1 |
|
370 |
-## ## message("\n num_accessible_genotypes = ", num_accessible_genotypes, "\n") |
|
371 |
-## if(num_accessible_genotypes >= min_accessible_genotypes) { |
|
372 |
-## done <- TRUE |
|
373 |
-## attributes(m) <- c(attributes(m), |
|
374 |
-## accessible_genotypes = num_accessible_genotypes, |
|
375 |
-## accessible_th = accessible_th) |
|
376 |
-## } else { |
|
377 |
-## ## Cannot start again with a fitness column |
|
378 |
-## m <- m[, -ncol(m), drop = FALSE] |
|
379 |
-## } |
|
380 |
-## } else { |
|
381 |
-## done <- TRUE |
|
382 |
-## } |
|
383 |
-## } |
|
384 |
-## ## message("\n number of attempts = ", attempts, "\n") |
|
385 |
-## class(m) <- c(class(m), "genotype_fitness_matrix") |
|
386 |
-## return(m) |
|
387 |
-## } |
|
388 |
- |
... | ... |
@@ -75,7 +75,22 @@ rfitness <- function(g, c= 0.5, |
75 | 75 |
truncate_at_0 = TRUE, |
76 | 76 |
K = 1, |
77 | 77 |
r = TRUE, |
78 |
- model = c("RMF", "NK")) { |
|
78 |
+ i = 0, # Ising, cost incompatibility |
|
79 |
+ I = -1, # Ising, sd for "i" |
|
80 |
+ circular = FALSE, # Ising, circular arrangement |
|
81 |
+ e = 0, # Eggbox, +/- e |
|
82 |
+ E = -1, # Eggbox, noise on "e" |
|
83 |
+ H = -1, # HoC stdev |
|
84 |
+ s = 0.1, # mean multiplivative |
|
85 |
+ S = -1, # SD multiplicative |
|
86 |
+ d = 0, # disminishing/increasing for multiplicative |
|
87 |
+ o = 0, # mean optimum |
|
88 |
+ O = -1, # sd optimum |
|
89 |
+ p = 0, # mean production for non 0 allele (optimum) |
|
90 |
+ P = -1, # sd for p |
|
91 |
+ |
|
92 |
+ model = c("RMF", "Additive", |
|
93 |
+ "NK", "Ising", "Eggbox", "Full")) { |
|
79 | 94 |
## Like Franke et al., 2011 and others of Krug. Very similar to Greene |
80 | 95 |
## and Crona, 2014. And this allows moving from HoC to purely additive |
81 | 96 |
## changing c and sd. |
... | ... |
@@ -107,12 +122,46 @@ rfitness <- function(g, c= 0.5, |
107 | 122 |
f_det <- -c * d_reference |
108 | 123 |
## f_det <- rowSums(m) * slope/nrow(m) ## this is Greene and Krona |
109 | 124 |
fi <- f_r + f_det |
125 |
+ } else if (model == "Additive") { |
|
126 |
+ ## get fitness effect for mutations in each gene |
|
127 |
+ mutants <-rep(1,g) |
|
128 |
+ ## FIXME: Why not just? |
|
129 |
+ ## f_single_mut <- rnorm(g, mean = mu, sd = sd) |
|
130 |
+ f_single_mut <- sapply(mutants, FUN = function(x) |
|
131 |
+ rnorm(x, mean = mu, sd = sd)) |
|
132 |
+ ## find which gene is mutated |
|
133 |
+ m2 <- m == 1 |
|
134 |
+ ## Sum the fitness effect of that mutation to generate a vector fi with |
|
135 |
+ ## the fitness for each mutant condition |
|
136 |
+ fi <- apply(m2, MARGIN = 1, FUN = function (x) sum(x*f_single_mut)) |
|
137 |
+ ## remove unnecessary variables |
|
138 |
+ rm (f_single_mut, m2) |
|
110 | 139 |
} else if(model == "NK") { |
111 | 140 |
if(K >= g) stop("It makes no sense to have K >= g") |
112 | 141 |
argsnk <- paste0("-K ", K, |
113 | 142 |
ifelse(r, " -r ", " "), |
114 | 143 |
g, " 2") |
115 | 144 |
fl1 <- system2(fl_generate_binary(), args = argsnk, stdout = TRUE)[-1] |
145 |
+ } else if (model == "Ising") { |
|
146 |
+ argsIsing <- paste0("-i ", i, " -I ", I , |
|
147 |
+ ifelse(circular, " -c ", " "), |
|
148 |
+ g, " 2") |
|
149 |
+ fl1 <- system2(fl_generate_binary(), args = argsIsing, stdout = TRUE)[-1] |
|
150 |
+ } else if (model == "Eggbox") { |
|
151 |
+ argsEgg <- paste0("-e ", e, " -E ", E," ", g, " 2") |
|
152 |
+ fl1 <- system2(fl_generate_binary(), args = argsEgg, stdout = TRUE)[-1] |
|
153 |
+ } else if (model == "Full") { |
|
154 |
+ if(K >= g) stop("It makes no sense to have K >= g") |
|
155 |
+ argsFull <- paste0("-K ", K, ifelse(r, " -r ", " "), |
|
156 |
+ "-i ", i, " -I ", I , ifelse(circular, " -c ", " "), |
|
157 |
+ "-e ", e, " -E ", E, " ", |
|
158 |
+ "-H ", H, " ", |
|
159 |
+ "-s ", s, " -S ", S, " -d ", d, " ", |
|
160 |
+ "-o ", o, " -O ", O, " -p ", p, " -P ", P, " ", |
|
161 |
+ g, " 2") |
|
162 |
+ fl1 <- system2(fl_generate_binary(), args = argsFull, stdout = TRUE)[-1] |
|
163 |
+ } |
|
164 |
+ if (model == "Eggbox" || model == "Ising" || model == "Full" || model == "NK") { |
|
116 | 165 |
fl1 <- matrix( |
117 | 166 |
as.numeric(unlist(strsplit(paste(fl1, collapse = " "), " "))), |
118 | 167 |
ncol = g + 1, byrow = TRUE) |
... | ... |
@@ -181,11 +181,25 @@ rfitness <- function(g, c= 0.5, |
181 | 181 |
"not include 1") |
182 | 182 |
} |
183 | 183 |
} |
184 |
- if(truncate_at_0) { |
|
185 |
- fi[fi <= 0] <- 1e-9 |
|
186 |
- } |
|
187 | 184 |
if(log) { |
188 |
- fi <- log(fi/fi[1]) + 1 |
|
185 |
+ ## If you want logs, you certainly do not want |
|
186 |
+ ## the log of a negative number |
|
187 |
+ fi[fi < 0] <- 0 |
|
188 |
+ if(wt_is_1 == "no") { |
|
189 |
+ fi <- log(fi) |
|
190 |
+ } else { |
|
191 |
+ ## by decree, fitness of wt is 1. So shift everything |
|
192 |
+ fi <- log(fi) + 1 |
|
193 |
+ } |
|
194 |
+ ## former expression, but it was more confusing |
|
195 |
+ ## fi <- log(fi/fi[1]) + 1 |
|
196 |
+ |
|
197 |
+ } |
|
198 |
+ if(truncate_at_0) { |
|
199 |
+ ## yes, truncate but add noise to prevent identical |
|
200 |
+ fi[fi <= 0] <- runif(sum(fi <= 0), |
|
201 |
+ min = 1e-10, |
|
202 |
+ max = 1e-9) |
|
189 | 203 |
} |
190 | 204 |
m <- cbind(m, Fitness = fi) |
191 | 205 |
if(!is_null_na(min_accessible_genotypes)) { |
... | ... |
@@ -14,6 +14,48 @@ |
14 | 14 |
## along with this program. If not, see <http://www.gnu.org/licenses/>. |
15 | 15 |
|
16 | 16 |
|
17 |
+create_eq_ref <- function(g) { |
|
18 |
+ ## "random" gives more prob. to genotypes with |
|
19 |
+ ## number of mutated genes close to g/2. |
|
20 |
+ ## This gives equal prob to having the reference |
|
21 |
+ ## be of any of the possible number of mutated genes. |
|
22 |
+ nm <- sample(g, 1) |
|
23 |
+ ref <- c(rep(1, nm), rep(0, g - nm)) |
|
24 |
+ sample(ref) |
|
25 |
+} |
|
26 |
+ |
|
27 |
+ |
|
28 |
+ |
|
29 |
+get_magellan_binaries <- |
|
30 |
+ function(names_binaries = c("fl_statistics", "fl_generate", "fl_genchains")) |
|
31 |
+{ |
|
32 |
+ rarch <- Sys.getenv('R_ARCH') |
|
33 |
+ nn_names_binaries <- names_binaries |
|
34 |
+ if(.Platform$OS.type == "windows") |
|
35 |
+ names_binaries <- paste0(names_binaries, ".exe") |
|
36 |
+ if(nzchar(rarch)) { |
|
37 |
+ rarch <- sub("^/", "", rarch) |
|
38 |
+ magellan_binaries <- system.file(package = "OncoSimulR", "exec", |
|
39 |
+ rarch, names_binaries) |
|
40 |
+ } else { |
|
41 |
+ magellan_binaries <- system.file(package = "OncoSimulR", "exec", |
|
42 |
+ names_binaries) |
|
43 |
+ } |
|
44 |
+ names(magellan_binaries) <- nn_names_binaries |
|
45 |
+ return(magellan_binaries) |
|
46 |
+} |
|
47 |
+ |
|
48 |
+## pkg.env <- new.env() |
|
49 |
+## ## The next will not install with error |
|
50 |
+## ## ERROR: hard-coded installation path: |
|
51 |
+## please report to the package maintainer and use ‘--no-staged-install’ |
|
52 |
+## pkg.env <- c(pkg.env, get_magellan_binaries()) |
|
53 |
+ |
|
54 |
+ |
|
55 |
+ |
|
56 |
+fl_statistics_binary <- function() get_magellan_binaries("fl_statistics") |
|
57 |
+fl_generate_binary <- function() get_magellan_binaries("fl_generate") |
|
58 |
+ |
|
17 | 59 |
|
18 | 60 |
rfitness <- function(g, c= 0.5, |
19 | 61 |
sd = 1, |
... | ... |
@@ -21,7 +63,7 @@ rfitness <- function(g, c= 0.5, |
21 | 63 |
reference = "random", ## "random", "max", or the vector, |
22 | 64 |
## e.g., rep(1, g). If random, a |
23 | 65 |
## random genotype is chosen as |
24 |
- ## reference. If "max" this is rep(1, g) |
|
66 |
+ ## reference. If "max" this is rep(1, g) |
|
25 | 67 |
scale = NULL, ## a two-element vector: min and max |
26 | 68 |
wt_is_1 = c("subtract", "divide", "force", "no"), |
27 | 69 |
## wt_is_1 = TRUE, ## wt has fitness 1 |
... | ... |
@@ -30,7 +72,10 @@ rfitness <- function(g, c= 0.5, |
30 | 72 |
## wt_is_1 = divide |
31 | 73 |
min_accessible_genotypes = NULL, |
32 | 74 |
accessible_th = 0, |
33 |
- truncate_at_0 = TRUE) { |
|
75 |
+ truncate_at_0 = TRUE, |
|
76 |
+ K = 1, |
|
77 |
+ r = TRUE, |
|
78 |
+ model = c("RMF", "NK")) { |
|
34 | 79 |
## Like Franke et al., 2011 and others of Krug. Very similar to Greene |
35 | 80 |
## and Crona, 2014. And this allows moving from HoC to purely additive |
36 | 81 |
## changing c and sd. |
... | ... |
@@ -38,29 +83,77 @@ rfitness <- function(g, c= 0.5, |
38 | 83 |
## FIXME future: do this with order too? |
39 | 84 |
## - do not generate a matrix of genotypes but a matrix of ordered genot. |
40 | 85 |
wt_is_1 = match.arg(wt_is_1) |
86 |
+ model = match.arg(model) |
|
41 | 87 |
if(is_null_na(g)) stop("Number of genes argument (g) is null or NA") |
42 | 88 |
m <- generate_matrix_genotypes(g) |
43 | 89 |
done <- FALSE |
44 | 90 |
## attempts <- 0 ## for debugging/tracking purposes |
45 | 91 |
while(!done) { |
46 |
- ## attempts <- attempts + 1 |
|
47 |
- f_r <- rnorm(nrow(m), mean = mu, sd = sd) |
|
48 |
- if(inherits(reference, "character") && length(reference) == 1) { |
|
49 |
- if(reference == "random") { |
|
50 |
- referenceI <- m[sample(nrow(m), 1), ] |
|
51 |
- } else if(reference == "max") { |
|
52 |
- referenceI <- rep(1, g) |
|
53 |
- } else if(reference == "random2") { |
|
54 |
- referenceI <- create_eq_ref(g) |
|
55 |
- } |
|
56 |
- } else { |
|
57 |
- referenceI <- reference |
|
92 |
+ if(model == "RMF") { |
|
93 |
+ ## attempts <- attempts + 1 |
|
94 |
+ f_r <- rnorm(nrow(m), mean = mu, sd = sd) |
|
95 |
+ if(inherits(reference, "character") && length(reference) == 1) { |
|
96 |
+ if(reference == "random") { |
|
97 |
+ referenceI <- m[sample(nrow(m), 1), ] |
|
98 |
+ } else if(reference == "max") { |
|
99 |
+ referenceI <- rep(1, g) |
|
100 |
+ } else if(reference == "random2") { |
|
101 |
+ referenceI <- create_eq_ref(g) |
|
102 |
+ } |
|
103 |
+ } else { |
|
104 |
+ referenceI <- reference |
|
58 | 105 |
} |
59 |
- d_reference <- apply(m, 1, function(x) sum(abs(x - referenceI))) |
|
60 |
- f_det <- -c * d_reference |
|
61 |
- ## f_det <- rowSums(m) * slope/nrow(m) ## this is Greene and Krona |
|
62 |
- fi <- f_r + f_det |
|
63 |
- |
|
106 |
+ d_reference <- apply(m, 1, function(x) sum(abs(x - referenceI))) |
|
107 |
+ f_det <- -c * d_reference |
|
108 |
+ ## f_det <- rowSums(m) * slope/nrow(m) ## this is Greene and Krona |
|
109 |
+ fi <- f_r + f_det |
|
110 |
+ } else if(model == "NK") { |
|
111 |
+ if(K >= g) stop("It makes no sense to have K >= g") |
|
112 |
+ argsnk <- paste0("-K ", K, |
|
113 |
+ ifelse(r, " -r ", " "), |
|
114 |
+ g, " 2") |
|
115 |
+ fl1 <- system2(fl_generate_binary(), args = argsnk, stdout = TRUE)[-1] |
|
116 |
+ fl1 <- matrix( |
|
117 |
+ as.numeric(unlist(strsplit(paste(fl1, collapse = " "), " "))), |
|
118 |
+ ncol = g + 1, byrow = TRUE) |
|
119 |
+ m1 <- fl1[, 1:g] |
|
120 |
+ fi <- fl1[, g + 1] |
|
121 |
+ |
|
122 |
+ ## For scaling, etc, all that matters, if anything, is the wildtype |
|
123 |
+ |
|
124 |
+ ## We could order by doing this |
|
125 |
+ ## But I am not 100% sure this will always be the same as |
|
126 |
+ ## generate_matrix_genotypes |
|
127 |
+ ## oo <- do.call(order, |
|
128 |
+ ## c(list(muts), |
|
129 |
+ ## as.list(data.frame(m1[, rev(1:ncol(m1))])))) |
|
130 |
+ ## m2 <- m1[oo, ] |
|
131 |
+ ## Or create an id and order by it? |
|
132 |
+ |
|
133 |
+ ## When we get to 20 genes, this is slow, about 18 seconds each |
|
134 |
+ ## apply. Matching is fast (< 0.5 seconds) |
|
135 |
+ gtstring <- apply(m, 1, function(x) paste0(x, collapse = "")) |
|
136 |
+ gtstring2 <- apply(m1, 1, function(x) paste0(x, collapse = "")) |
|
137 |
+ oo <- match(gtstring, gtstring2) |
|
138 |
+ fi <- fi[oo] |
|
139 |
+ ## make sure no left overs |
|
140 |
+ rm(gtstring, gtstring2, oo, fl1, m1) |
|
141 |
+ |
|
142 |
+ ## Had we not ordered, do this!!! |
|
143 |
+ ## Which one is WT? |
|
144 |
+ ## muts <- rowSums(m1) |
|
145 |
+ ## w_wt <- which(muts == 0) |
|
146 |
+ ## if(w_wt != 1) { |
|
147 |
+ ## f_a <- fi[1] |
|
148 |
+ ## fi[1] <- fi[w_wt] |
|
149 |
+ ## fi[w_wt] <- f_a |
|
150 |
+ ## rm(f_a) |
|
151 |
+ ## } |
|
152 |
+ ## m[] <- m1 |
|
153 |
+ ## rm(m1) |
|
154 |
+ ## rm(fl1) |
|
155 |
+ } |
|
156 |
+ |
|
64 | 157 |
if(!is.null(scale)) { |
65 | 158 |
fi <- (fi - min(fi))/(max(fi) - min(fi)) |
66 | 159 |
fi <- scale[1] + fi * (scale[2] - scale[1]) |
... | ... |
@@ -119,16 +212,114 @@ rfitness <- function(g, c= 0.5, |
119 | 212 |
} |
120 | 213 |
|
121 | 214 |
|
122 |
-create_eq_ref <- function(g) { |
|
123 |
- ## "random" gives more prob. to genotypes with |
|
124 |
- ## number of mutated genes close to g/2. |
|
125 |
- ## This gives equal prob to having the reference |
|
126 |
- ## be of any of the possible number of mutated genes. |
|
127 |
- nm <- sample(g, 1) |
|
128 |
- ref <- c(rep(1, nm), rep(0, g - nm)) |
|
129 |
- sample(ref) |
|
130 |
-} |
|
131 | 215 |
|
132 | 216 |
|
133 | 217 |
|
134 | 218 |
|
219 |
+ |
|
220 |
+ |
|
221 |
+ |
|
222 |
+ |
|
223 |
+## rfitness <- function(g, c= 0.5, |
|
224 |
+## sd = 1, |
|
225 |
+## mu = 1, |
|
226 |
+## reference = "random", ## "random", "max", or the vector, |
|
227 |
+## ## e.g., rep(1, g). If random, a |
|
228 |
+## ## random genotype is chosen as |
|
229 |
+## ## reference. If "max" this is rep(1, g) |
|
230 |
+## scale = NULL, ## a two-element vector: min and max |
|
231 |
+## wt_is_1 = c("subtract", "divide", "force", "no"), |
|
232 |
+## ## wt_is_1 = TRUE, ## wt has fitness 1 |
|
233 |
+## log = FALSE, ## log only makes sense if all values > |
|
234 |
+## ## 0. scale with min > 0, and/or set |
|
235 |
+## ## wt_is_1 = divide |
|
236 |
+## min_accessible_genotypes = NULL, |
|
237 |
+## accessible_th = 0, |
|
238 |
+## truncate_at_0 = TRUE) { |
|
239 |
+## ## Like Franke et al., 2011 and others of Krug. Very similar to Greene |
|
240 |
+## ## and Crona, 2014. And this allows moving from HoC to purely additive |
|
241 |
+## ## changing c and sd. |
|
242 |
+ |
|
243 |
+## ## FIXME future: do this with order too? |
|
244 |
+## ## - do not generate a matrix of genotypes but a matrix of ordered genot. |
|
245 |
+## wt_is_1 = match.arg(wt_is_1) |
|
246 |
+## if(is_null_na(g)) stop("Number of genes argument (g) is null or NA") |
|
247 |
+## m <- generate_matrix_genotypes(g) |
|
248 |
+## done <- FALSE |
|
249 |
+## ## attempts <- 0 ## for debugging/tracking purposes |
|
250 |
+## while(!done) { |
|
251 |
+## ## attempts <- attempts + 1 |
|
252 |
+## f_r <- rnorm(nrow(m), mean = mu, sd = sd) |
|
253 |
+## if(inherits(reference, "character") && length(reference) == 1) { |
|
254 |
+## if(reference == "random") { |
|
255 |
+## referenceI <- m[sample(nrow(m), 1), ] |
|
256 |
+## } else if(reference == "max") { |
|
257 |
+## referenceI <- rep(1, g) |
|
258 |
+## } else if(reference == "random2") { |
|
259 |
+## referenceI <- create_eq_ref(g) |
|
260 |
+## } |
|
261 |
+## } else { |
|
262 |
+## referenceI <- reference |
|
263 |
+## } |
|
264 |
+## d_reference <- apply(m, 1, function(x) sum(abs(x - referenceI))) |
|
265 |
+## f_det <- -c * d_reference |
|
266 |
+## ## f_det <- rowSums(m) * slope/nrow(m) ## this is Greene and Krona |
|
267 |
+## fi <- f_r + f_det |
|
268 |
+ |
|
269 |
+## if(!is.null(scale)) { |
|
270 |
+## fi <- (fi - min(fi))/(max(fi) - min(fi)) |
|
271 |
+## fi <- scale[1] + fi * (scale[2] - scale[1]) |
|
272 |
+## } |
|
273 |
+## if(wt_is_1 == "divide") { |
|
274 |
+## ## We need to shift to avoid ratios involving negative numbers and |
|
275 |
+## ## we need to avoid having any fitness at 0, specially the wt. If |
|
276 |
+## ## any negative value, add the min, and shift by the magnitude of |
|
277 |
+## ## the min to avoid any at 0. |
|
278 |
+ |
|
279 |
+## ## If you use scale and wt_is_1, this will move the scale. It is |
|
280 |
+## ## not possible to obtain a linear transformation that will keep |
|
281 |
+## ## the min and max of the scale, and wt at 1. |
|
282 |
+## min_fi <- min(fi) |
|
283 |
+## if(min_fi < 0) |
|
284 |
+## fi <- fi + 2 * abs(min(fi)) |
|
285 |
+## fi <- fi/fi[1] |
|
286 |
+## } else if (wt_is_1 == "subtract") { |
|
287 |
+## fi <- fi - fi[1] + 1.0 |
|
288 |
+## } else if(wt_is_1 == "force") { |
|
289 |
+## fi[1] <- 1.0 |
|
290 |
+## if(!is.null(scale)) { |
|
291 |
+## if( (1 > scale[2]) || (1 < scale[1])) |
|
292 |
+## warning("Using wt_is_1 = force and scale, but scale does ", |
|
293 |
+## "not include 1") |
|
294 |
+## } |
|
295 |
+## } |
|
296 |
+## if(truncate_at_0) { |
|
297 |
+## fi[fi <= 0] <- 1e-9 |
|
298 |
+## } |
|
299 |
+## if(log) { |
|
300 |
+## fi <- log(fi/fi[1]) + 1 |
|
301 |
+## } |
|
302 |
+## m <- cbind(m, Fitness = fi) |
|
303 |
+## if(!is_null_na(min_accessible_genotypes)) { |
|
304 |
+## ## num_accessible_genotypes <- count_accessible_g(m, accessible_th) |
|
305 |
+## ## Recall accessibleGenotypes includes the wt, if accessible. |
|
306 |
+## num_accessible_genotypes <- length(wrap_accessibleGenotypes(m, accessible_th)) - 1 |
|
307 |
+## ## message("\n num_accessible_genotypes = ", num_accessible_genotypes, "\n") |
|
308 |
+## if(num_accessible_genotypes >= min_accessible_genotypes) { |
|
309 |
+## done <- TRUE |
|
310 |
+## attributes(m) <- c(attributes(m), |
|
311 |
+## accessible_genotypes = num_accessible_genotypes, |
|
312 |
+## accessible_th = accessible_th) |
|
313 |
+## } else { |
|
314 |
+## ## Cannot start again with a fitness column |
|
315 |
+## m <- m[, -ncol(m), drop = FALSE] |
|
316 |
+## } |
|
317 |
+## } else { |
|
318 |
+## done <- TRUE |
|
319 |
+## } |
|
320 |
+## } |
|
321 |
+## ## message("\n number of attempts = ", attempts, "\n") |
|
322 |
+## class(m) <- c(class(m), "genotype_fitness_matrix") |
|
323 |
+## return(m) |
|
324 |
+## } |
|
325 |
+ |
... | ... |
@@ -38,7 +38,7 @@ rfitness <- function(g, c= 0.5, |
38 | 38 |
## FIXME future: do this with order too? |
39 | 39 |
## - do not generate a matrix of genotypes but a matrix of ordered genot. |
40 | 40 |
wt_is_1 = match.arg(wt_is_1) |
41 |
- |
|
41 |
+ if(is_null_na(g)) stop("Number of genes argument (g) is null or NA") |
|
42 | 42 |
m <- generate_matrix_genotypes(g) |
43 | 43 |
done <- FALSE |
44 | 44 |
## attempts <- 0 ## for debugging/tracking purposes |
- Several improvements to rfitness.
- simOGraph using transitive reduction properly.
- Miscell documentation improvements.
- Updated citation to Bioinformatics paper.
git-svn-id: file:///home/git/hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/OncoSimulR@126818 bc3139a8-67e5-0310-9ffc-ced21a209358
... | ... |
@@ -1,26 +1,50 @@ |
1 |
+## Copyright 2013, 2014, 2015, 2016, 2017 Ramon Diaz-Uriarte |
|
2 |
+ |
|
3 |
+## This program is free software: you can redistribute it and/or modify |
|
4 |
+## it under the terms of the GNU General Public License as published by |
|
5 |
+## the Free Software Foundation, either version 3 of the License, or |
|
6 |
+## (at your option) any later version. |
|
7 |
+ |
|
8 |
+## This program is distributed in the hope that it will be useful, |
|
9 |
+## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 |
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 |
+## GNU General Public License for more details. |
|
12 |
+ |
|
13 |
+## You should have received a copy of the GNU General Public License |
|
14 |
+## along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
15 |
+ |
|
16 |
+ |
|
17 |
+ |
|
1 | 18 |
rfitness <- function(g, c= 0.5, |
2 | 19 |
sd = 1, |
20 |
+ mu = 1, |
|
3 | 21 |
reference = "random", ## "random", "max", or the vector, |
4 | 22 |
## e.g., rep(1, g). If random, a |
5 | 23 |
## random genotype is chosen as |
6 | 24 |
## reference. If "max" this is rep(1, g) |
7 | 25 |
scale = NULL, ## a two-element vector: min and max |
8 |
- wt_is_1 = TRUE, ## wt has fitness 1 |
|
26 |
+ wt_is_1 = c("subtract", "divide", "force", "no"), |
|
27 |
+ ## wt_is_1 = TRUE, ## wt has fitness 1 |
|
9 | 28 |
log = FALSE, ## log only makes sense if all values > |
10 | 29 |
## 0. scale with min > 0, and/or set |
11 |
- ## wt_is_1 = TRUE |
|
12 |
- min_accessible_genotypes = 0, |
|
13 |
- accessible_th = 0) { |
|
30 |
+ ## wt_is_1 = divide |
|
31 |
+ min_accessible_genotypes = NULL, |
|
32 |
+ accessible_th = 0, |
|
33 |
+ truncate_at_0 = TRUE) { |
|
14 | 34 |
## Like Franke et al., 2011 and others of Krug. Very similar to Greene |
15 | 35 |
## and Crona, 2014. And this allows moving from HoC to purely additive |
16 | 36 |
## changing c and sd. |
17 | 37 |
|
18 | 38 |
## FIXME future: do this with order too? |
19 | 39 |
## - do not generate a matrix of genotypes but a matrix of ordered genot. |
40 |
+ wt_is_1 = match.arg(wt_is_1) |
|
41 |
+ |
|
20 | 42 |
m <- generate_matrix_genotypes(g) |
21 | 43 |
done <- FALSE |
44 |
+ ## attempts <- 0 ## for debugging/tracking purposes |
|
22 | 45 |
while(!done) { |
23 |
- f_r <- rnorm(nrow(m), mean = 0, sd = sd) |
|
46 |
+ ## attempts <- attempts + 1 |
|
47 |
+ f_r <- rnorm(nrow(m), mean = mu, sd = sd) |
|
24 | 48 |
if(inherits(reference, "character") && length(reference) == 1) { |
25 | 49 |
if(reference == "random") { |
26 | 50 |
referenceI <- m[sample(nrow(m), 1), ] |
... | ... |
@@ -41,7 +65,7 @@ rfitness <- function(g, c= 0.5, |
41 | 65 |
fi <- (fi - min(fi))/(max(fi) - min(fi)) |
42 | 66 |
fi <- scale[1] + fi * (scale[2] - scale[1]) |
43 | 67 |
} |
44 |
- if(wt_is_1) { |
|
68 |
+ if(wt_is_1 == "divide") { |
|
45 | 69 |
## We need to shift to avoid ratios involving negative numbers and |
46 | 70 |
## we need to avoid having any fitness at 0, specially the wt. If |
47 | 71 |
## any negative value, add the min, and shift by the magnitude of |
... | ... |
@@ -54,16 +78,28 @@ rfitness <- function(g, c= 0.5, |
54 | 78 |
if(min_fi < 0) |
55 | 79 |
fi <- fi + 2 * abs(min(fi)) |
56 | 80 |
fi <- fi/fi[1] |
81 |
+ } else if (wt_is_1 == "subtract") { |
|
82 |
+ fi <- fi - fi[1] + 1.0 |
|
83 |
+ } else if(wt_is_1 == "force") { |
|
84 |
+ fi[1] <- 1.0 |
|
85 |
+ if(!is.null(scale)) { |
|
86 |
+ if( (1 > scale[2]) || (1 < scale[1])) |
|
87 |
+ warning("Using wt_is_1 = force and scale, but scale does ", |
|
88 |
+ "not include 1") |
|
89 |
+ } |
|
90 |
+ } |
|
91 |
+ if(truncate_at_0) { |
|
92 |
+ fi[fi <= 0] <- 1e-9 |
|
57 | 93 |
} |
58 |
- |
|
59 | 94 |
if(log) { |
60 | 95 |
fi <- log(fi/fi[1]) + 1 |
61 | 96 |
} |
62 | 97 |
m <- cbind(m, Fitness = fi) |
63 |
- if(min_accessible_genotypes > 0) { |
|
98 |
+ if(!is_null_na(min_accessible_genotypes)) { |
|
64 | 99 |
## num_accessible_genotypes <- count_accessible_g(m, accessible_th) |
65 | 100 |
## Recall accessibleGenotypes includes the wt, if accessible. |
66 | 101 |
num_accessible_genotypes <- length(wrap_accessibleGenotypes(m, accessible_th)) - 1 |
102 |
+ ## message("\n num_accessible_genotypes = ", num_accessible_genotypes, "\n") |
|
67 | 103 |
if(num_accessible_genotypes >= min_accessible_genotypes) { |
68 | 104 |
done <- TRUE |
69 | 105 |
attributes(m) <- c(attributes(m), |
... | ... |
@@ -77,6 +113,7 @@ rfitness <- function(g, c= 0.5, |
77 | 113 |
done <- TRUE |
78 | 114 |
} |
79 | 115 |
} |
116 |
+ ## message("\n number of attempts = ", attempts, "\n") |
|
80 | 117 |
class(m) <- c(class(m), "genotype_fitness_matrix") |
81 | 118 |
return(m) |
82 | 119 |
} |
git-svn-id: file:///home/git/hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/OncoSimulR@123922 bc3139a8-67e5-0310-9ffc-ced21a209358
... | ... |
@@ -61,7 +61,9 @@ rfitness <- function(g, c= 0.5, |
61 | 61 |
} |
62 | 62 |
m <- cbind(m, Fitness = fi) |
63 | 63 |
if(min_accessible_genotypes > 0) { |
64 |
- num_accessible_genotypes <- count_accessible_g(m, accessible_th) |
|
64 |
+ ## num_accessible_genotypes <- count_accessible_g(m, accessible_th) |
|
65 |
+ ## Recall accessibleGenotypes includes the wt, if accessible. |
|
66 |
+ num_accessible_genotypes <- length(wrap_accessibleGenotypes(m, accessible_th)) - 1 |
|
65 | 67 |
if(num_accessible_genotypes >= min_accessible_genotypes) { |
66 | 68 |
done <- TRUE |
67 | 69 |
attributes(m) <- c(attributes(m), |
... | ... |
@@ -89,3 +91,7 @@ create_eq_ref <- function(g) { |
89 | 91 |
ref <- c(rep(1, nm), rep(0, g - nm)) |
90 | 92 |
sample(ref) |
91 | 93 |
} |
94 |
+ |
|
95 |
+ |
|
96 |
+ |
|
97 |
+ |
git-svn-id: file:///home/git/hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/OncoSimulR@121246 bc3139a8-67e5-0310-9ffc-ced21a209358
... | ... |
@@ -26,7 +26,9 @@ rfitness <- function(g, c= 0.5, |
26 | 26 |
referenceI <- m[sample(nrow(m), 1), ] |
27 | 27 |
} else if(reference == "max") { |
28 | 28 |
referenceI <- rep(1, g) |
29 |
- } |
|
29 |
+ } else if(reference == "random2") { |
|
30 |
+ referenceI <- create_eq_ref(g) |
|
31 |
+ } |
|
30 | 32 |
} else { |
31 | 33 |
referenceI <- reference |
32 | 34 |
} |
... | ... |
@@ -76,3 +78,14 @@ rfitness <- function(g, c= 0.5, |
76 | 78 |
class(m) <- c(class(m), "genotype_fitness_matrix") |
77 | 79 |
return(m) |
78 | 80 |
} |
81 |
+ |
|
82 |
+ |
|
83 |
+create_eq_ref <- function(g) { |
|
84 |
+ ## "random" gives more prob. to genotypes with |
|
85 |
+ ## number of mutated genes close to g/2. |
|
86 |
+ ## This gives equal prob to having the reference |
|
87 |
+ ## be of any of the possible number of mutated genes. |
|
88 |
+ nm <- sample(g, 1) |
|
89 |
+ ref <- c(rep(1, nm), rep(0, g - nm)) |
|
90 |
+ sample(ref) |
|
91 |
+} |
git-svn-id: file:///home/git/hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/OncoSimulR@120020 bc3139a8-67e5-0310-9ffc-ced21a209358
... | ... |
@@ -59,8 +59,12 @@ rfitness <- function(g, c= 0.5, |
59 | 59 |
} |
60 | 60 |
m <- cbind(m, Fitness = fi) |
61 | 61 |
if(min_accessible_genotypes > 0) { |
62 |
- if(count_accessible_g(m, accessible_th) >= min_accessible_genotypes) { |
|
62 |
+ num_accessible_genotypes <- count_accessible_g(m, accessible_th) |
|
63 |
+ if(num_accessible_genotypes >= min_accessible_genotypes) { |
|
63 | 64 |
done <- TRUE |
65 |
+ attributes(m) <- c(attributes(m), |
|
66 |
+ accessible_genotypes = num_accessible_genotypes, |
|
67 |
+ accessible_th = accessible_th) |
|
64 | 68 |
} else { |
65 | 69 |
## Cannot start again with a fitness column |
66 | 70 |
m <- m[, -ncol(m), drop = FALSE] |
git-svn-id: file:///home/git/hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/OncoSimulR@119231 bc3139a8-67e5-0310-9ffc-ced21a209358
... | ... |
@@ -6,10 +6,11 @@ rfitness <- function(g, c= 0.5, |
6 | 6 |
## reference. If "max" this is rep(1, g) |
7 | 7 |
scale = NULL, ## a two-element vector: min and max |
8 | 8 |
wt_is_1 = TRUE, ## wt has fitness 1 |
9 |
- log = FALSE ## log only makes sense if all values > |
|
9 |
+ log = FALSE, ## log only makes sense if all values > |
|
10 | 10 |
## 0. scale with min > 0, and/or set |
11 | 11 |
## wt_is_1 = TRUE |
12 |
- ) { |
|
12 |
+ min_accessible_genotypes = 0, |
|
13 |
+ accessible_th = 0) { |
|
13 | 14 |
## Like Franke et al., 2011 and others of Krug. Very similar to Greene |
14 | 15 |
## and Crona, 2014. And this allows moving from HoC to purely additive |
15 | 16 |
## changing c and sd. |
... | ... |
@@ -17,42 +18,57 @@ rfitness <- function(g, c= 0.5, |
17 | 18 |
## FIXME future: do this with order too? |
18 | 19 |
## - do not generate a matrix of genotypes but a matrix of ordered genot. |
19 | 20 |
m <- generate_matrix_genotypes(g) |
20 |
- f_r <- rnorm(nrow(m), mean = 0, sd = sd) |
|
21 |
- if(inherits(reference, "character") && length(reference) == 1) { |
|
22 |
- if(reference == "random") { |
|
23 |
- reference <- m[sample(nrow(m), 1), ] |
|
24 |
- } else if(reference == "max") { |
|
25 |
- reference <- rep(1, g) |
|
21 |
+ done <- FALSE |
|
22 |
+ while(!done) { |
|
23 |
+ f_r <- rnorm(nrow(m), mean = 0, sd = sd) |
|
24 |
+ if(inherits(reference, "character") && length(reference) == 1) { |
|
25 |
+ if(reference == "random") { |
|
26 |
+ referenceI <- m[sample(nrow(m), 1), ] |
|
27 |
+ } else if(reference == "max") { |
|
28 |
+ referenceI <- rep(1, g) |
|
29 |
+ } |
|
30 |
+ } else { |
|
31 |
+ referenceI <- reference |
|
32 |
+ } |
|
33 |
+ d_reference <- apply(m, 1, function(x) sum(abs(x - referenceI))) |
|
34 |
+ f_det <- -c * d_reference |
|
35 |
+ ## f_det <- rowSums(m) * slope/nrow(m) ## this is Greene and Krona |
|
36 |
+ fi <- f_r + f_det |
|
37 |
+ |
|
38 |
+ if(!is.null(scale)) { |
|
39 |
+ fi <- (fi - min(fi))/(max(fi) - min(fi)) |
|
40 |
+ fi <- scale[1] + fi * (scale[2] - scale[1]) |
|
26 | 41 |
} |
27 |
- } |
|
28 |
- d_reference <- apply(m, 1, function(x) sum(abs(x - reference))) |
|
29 |
- f_det <- -c * d_reference |
|
30 |
- ## f_det <- rowSums(m) * slope/nrow(m) ## this is Greene and Krona |
|
31 |
- fi <- f_r + f_det |
|
32 |
- |
|
33 |
- if(!is.null(scale)) { |
|
34 |
- fi <- (fi - min(fi))/(max(fi) - min(fi)) |
|
35 |
- fi <- scale[1] + fi * (scale[2] - scale[1]) |
|
36 |
- } |
|
37 |
- if(wt_is_1) { |
|
38 |
- ## We need to shift to avoid ratios involving negative numbers and |
|
39 |
- ## we need to avoid having any fitness at 0, specially the wt. If |
|
40 |
- ## any negative value, add the min, and shift by the magnitude of |
|
41 |
- ## the min to avoid any at 0. |
|