... | ... |
@@ -50,7 +50,7 @@ oncoSimulSample <- function(Nindiv, |
50 | 50 |
minDetectDrvCloneSz = "auto", |
51 | 51 |
extraTime = 0, |
52 | 52 |
finalTime = 0.25 * 25 * 365, |
53 |
- onlyCancer = TRUE, |
|
53 |
+ onlyCancer = FALSE, |
|
54 | 54 |
keepPhylog = FALSE, |
55 | 55 |
mutationPropGrowth = ifelse(model == "Bozic", |
56 | 56 |
FALSE, TRUE), |
... | ... |
@@ -375,7 +375,7 @@ oncoSimulPop <- function(Nindiv, |
375 | 375 |
## ifelse(model \%in\% c("Bozic", "Exp"), -9, |
376 | 376 |
## 5 * sampleEvery), |
377 | 377 |
finalTime = 0.25 * 25 * 365, |
378 |
- onlyCancer = TRUE, |
|
378 |
+ onlyCancer = FALSE, |
|
379 | 379 |
keepPhylog = FALSE, |
380 | 380 |
mutationPropGrowth = ifelse(model == "Bozic", |
381 | 381 |
FALSE, TRUE), |
... | ... |
@@ -468,7 +468,7 @@ oncoSimulIndiv <- function(fp, |
468 | 468 |
minDetectDrvCloneSz = "auto", |
469 | 469 |
extraTime = 0, |
470 | 470 |
finalTime = 0.25 * 25 * 365, |
471 |
- onlyCancer = TRUE, |
|
471 |
+ onlyCancer = FALSE, |
|
472 | 472 |
keepPhylog = FALSE, |
473 | 473 |
mutationPropGrowth = ifelse(model == "Bozic", |
474 | 474 |
FALSE, TRUE), |
... | ... |
@@ -710,9 +710,12 @@ summary.oncosimul <- function(object, ...) { |
710 | 710 |
## if those are not regarded as errors |
711 | 711 |
pbp <- ("pops.by.time" %in% names(object) ) |
712 | 712 |
|
713 |
- if(object$other$UnrecoverExcept) { ## yes, when bailing out from |
|
714 |
- ## except. can have just minimal |
|
715 |
- ## content |
|
713 |
+ if(object$other$UnrecoverExcept) { |
|
714 |
+ ## yes, when bailing out from |
|
715 |
+ ## except. can have just minimal |
|
716 |
+ ## content |
|
717 |
+ message("An unrecoverable exception happened in ", |
|
718 |
+ "the simulation of the input object.") |
|
716 | 719 |
return(NA) |
717 | 720 |
} else if( !pbp & (object$HittedWallTime || object$HittedMaxTries) ) { |
718 | 721 |
return(NA) |
... | ... |
@@ -928,7 +931,20 @@ plot.oncosimul <- function(x, |
928 | 931 |
... |
929 | 932 |
) { |
930 | 933 |
|
931 |
- |
|
934 |
+ if (x$other$UnrecoverExcept) { |
|
935 |
+ stop("An unrecoverable exception happened in ", |
|
936 |
+ "the simulation of the input object.", |
|
937 |
+ "Empty object with nothing to plot.") |
|
938 |
+ } else { |
|
939 |
+ pbp <- ("pops.by.time" %in% names(x) ) |
|
940 |
+ if ( !pbp & (x$HittedWallTime || x$HittedMaxTries) ) { |
|
941 |
+ stop("The simulation hit max wall time or max tries. ", |
|
942 |
+ "Empty object with nothing to plot.") |
|
943 |
+ } else if ( !pbp & !(x$HittedWallTime || x$HittedMaxTries) ) { |
|
944 |
+ stop("Eh? No pops.by.time but did not hit wall time or max tries? BUG!") |
|
945 |
+ } |
|
946 |
+ } |
|
947 |
+ |
|
932 | 948 |
if(!(type %in% c("stacked", "stream", "line"))) |
933 | 949 |
stop("Type of plot unknown: it must be one of", |
934 | 950 |
"stacked, stream or line") |
... | ... |
@@ -64,7 +64,10 @@ oncoSimulSample <- function(Nindiv, |
64 | 64 |
fixation = NULL, |
65 | 65 |
verbosity = 1, |
66 | 66 |
showProgress = FALSE, |
67 |
- seed = "auto"){ |
|
67 |
+ seed = "auto", |
|
68 |
+ interventions = NULL, |
|
69 |
+ userVars = NULL, |
|
70 |
+ rules = NULL){ |
|
68 | 71 |
## No longer using mclapply, because of the way we use the limit on |
69 | 72 |
## the number of tries. |
70 | 73 |
|
... | ... |
@@ -192,7 +195,10 @@ oncoSimulSample <- function(Nindiv, |
192 | 195 |
mutationPropGrowth = mutationPropGrowth, |
193 | 196 |
detectionProb = detectionProb, |
194 | 197 |
AND_DrvProbExit = AND_DrvProbExit, |
195 |
- fixation = fixation) |
|
198 |
+ fixation = fixation, |
|
199 |
+ interventions = interventions, |
|
200 |
+ userVars = userVars, |
|
201 |
+ rules = rules) |
|
196 | 202 |
if(tmp$other$UnrecoverExcept) { |
197 | 203 |
return(f.out.unrecover.except(tmp)) |
198 | 204 |
} |
... | ... |
@@ -383,7 +389,10 @@ oncoSimulPop <- function(Nindiv, |
383 | 389 |
fixation = NULL, |
384 | 390 |
verbosity = 0, |
385 | 391 |
mc.cores = detectCores(), |
386 |
- seed = "auto") { |
|
392 |
+ seed = "auto", |
|
393 |
+ interventions = NULL, |
|
394 |
+ userVars = NULL, |
|
395 |
+ rules = NULL) { |
|
387 | 396 |
|
388 | 397 |
if(Nindiv < 1) |
389 | 398 |
stop("Nindiv must be >= 1") |
... | ... |
@@ -424,7 +433,10 @@ oncoSimulPop <- function(Nindiv, |
424 | 433 |
mutationPropGrowth = mutationPropGrowth, |
425 | 434 |
detectionProb = detectionProb, |
426 | 435 |
AND_DrvProbExit = AND_DrvProbExit, |
427 |
- fixation = fixation), |
|
436 |
+ fixation = fixation, |
|
437 |
+ interventions = interventions, |
|
438 |
+ userVars = userVars, |
|
439 |
+ rules = rules), |
|
428 | 440 |
mc.cores = mc.cores) |
429 | 441 |
## mc.allow.recursive = FALSE ## FIXME: remove? |
430 | 442 |
## done for covr issue |
... | ... |
@@ -469,8 +481,10 @@ oncoSimulIndiv <- function(fp, |
469 | 481 |
initMutant = NULL, |
470 | 482 |
AND_DrvProbExit = FALSE, |
471 | 483 |
fixation = NULL, |
472 |
- seed = NULL |
|
473 |
- ) { |
|
484 |
+ seed = NULL, |
|
485 |
+ interventions = NULL, |
|
486 |
+ userVars = NULL, |
|
487 |
+ rules = NULL) { |
|
474 | 488 |
call <- match.call() |
475 | 489 |
if(all(c(is_null_na(detectionProb), |
476 | 490 |
is_null_na(detectionSize), |
... | ... |
@@ -495,6 +509,9 @@ oncoSimulIndiv <- function(fp, |
495 | 509 |
} |
496 | 510 |
if(!inherits(fp, "fitnessEffects")) |
497 | 511 |
stop("v.1 functionality has been removed. Please use v.2") |
512 |
+ |
|
513 |
+ if(!inherits(fp, "fitnessEffects_v3")) |
|
514 |
+ fp <- convertFitnessEffects(fp) |
|
498 | 515 |
|
499 | 516 |
## legacies from poor name choices |
500 | 517 |
typeFitness <- switch(model, |
... | ... |
@@ -504,6 +521,8 @@ oncoSimulIndiv <- function(fp, |
504 | 521 |
"McFL" = "mcfarlandlog", |
505 | 522 |
"McFarlandLogD" = "mcfarlandlogd", |
506 | 523 |
"McFLD" = "mcfarlandlogd", |
524 |
+ "Arb" = "arbitrary", |
|
525 |
+ "Const" = "constant", |
|
507 | 526 |
stop("No valid value for model") |
508 | 527 |
) |
509 | 528 |
if(max(initSize) < 1) |
... | ... |
@@ -515,7 +534,21 @@ oncoSimulIndiv <- function(fp, |
515 | 534 |
} ## if ( !(model %in% c("McFL", "McFarlandLog") )) { |
516 | 535 |
## K <- 1 ## K is ONLY used for McFarland; set it to 1, to avoid |
517 | 536 |
## ## C++ blowing. |
518 |
- |
|
537 |
+ |
|
538 |
+ if("deathSpec" %in% names(fp)) { |
|
539 |
+ if (fp$deathSpec) { |
|
540 |
+ if (typeFitness != "arbitrary" && typeFitness != "constant") { |
|
541 |
+ stop("If death is specified in the fitness effects, use Arb or Const model.") |
|
542 |
+ } |
|
543 |
+ } |
|
544 |
+ |
|
545 |
+ else { |
|
546 |
+ if (typeFitness == "arbitrary") { |
|
547 |
+ stop("To use Arb model specify both birth and death in fitness effects.") |
|
548 |
+ } |
|
549 |
+ } |
|
550 |
+ } |
|
551 |
+ |
|
519 | 552 |
if(typeFitness == "exp") { |
520 | 553 |
death <- 1 |
521 | 554 |
## mutationPropGrowth <- 1 |
... | ... |
@@ -531,7 +564,7 @@ oncoSimulIndiv <- function(fp, |
531 | 564 |
} |
532 | 565 |
|
533 | 566 |
if(minDetectDrvCloneSz == "auto") { |
534 |
- if(model %in% c("Bozic", "Exp") ) |
|
567 |
+ if(model %in% c("Bozic", "Exp", "Arb", "Const") ) |
|
535 | 568 |
minDetectDrvCloneSz <- 0 |
536 | 569 |
else if (model %in% c("McFL", "McFarlandLog", |
537 | 570 |
"McFLD", "McFarlandLogD")) { |
... | ... |
@@ -610,6 +643,12 @@ oncoSimulIndiv <- function(fp, |
610 | 643 |
if(AND_DrvProbExit) |
611 | 644 |
stop("It makes no sense to pass AND_DrvProbExit and a fixation list.") |
612 | 645 |
} |
646 |
+ |
|
647 |
+ #if interventions is null, we create an empty list, cos' it will be easier to handle by C++ |
|
648 |
+ if(is_null_na(interventions)){ |
|
649 |
+ interventions = list() |
|
650 |
+ } |
|
651 |
+ |
|
613 | 652 |
op <- try(nr_oncoSimul.internal(rFE = fp, |
614 | 653 |
birth = birth, |
615 | 654 |
death = death, |
... | ... |
@@ -643,7 +682,10 @@ oncoSimulIndiv <- function(fp, |
643 | 682 |
MMUEF = muEF, |
644 | 683 |
detectionProb = detectionProb, |
645 | 684 |
AND_DrvProbExit = AND_DrvProbExit, |
646 |
- fixation = fixation), |
|
685 |
+ fixation = fixation, |
|
686 |
+ interventions = interventions, |
|
687 |
+ userVars = userVars, |
|
688 |
+ rules = rules), |
|
647 | 689 |
silent = !verbosity) |
648 | 690 |
objClass <- c("oncosimul", "oncosimul2") |
649 | 691 |
## } |
... | ... |
@@ -1494,7 +1536,11 @@ get.the.time.for.sample <- function(tmp, timeSample, popSizeSample) { |
1494 | 1536 |
} else if (length(candidate.time) == 1) { |
1495 | 1537 |
message("Only one sampled time period with mutants.") |
1496 | 1538 |
the.time <- candidate.time |
1497 |
- } else { |
|
1539 |
+ } else { |
|
1540 |
+ ## If we have drivers at t = 2, t= 4, t= 5, ... but not at t=3 |
|
1541 |
+ ## because at t=3 there were no clones with drivers, |
|
1542 |
+ ## t = 3 is not among the candidate times. |
|
1543 |
+ |
|
1498 | 1544 |
the.time <- sample(candidate.time, 1) |
1499 | 1545 |
} |
1500 | 1546 |
} else { |
... | ... |
@@ -57,9 +57,6 @@ oncoSimulSample <- function(Nindiv, |
57 | 57 |
max.memory = 2000, |
58 | 58 |
max.wall.time.total = 600, |
59 | 59 |
max.num.tries.total = 500 * Nindiv, |
60 |
- ## well, obviously they are errors |
|
61 |
- ## errorHitWallTime = TRUE, |
|
62 |
- ## errorHitMaxTries = TRUE, |
|
63 | 60 |
typeSample = "whole", |
64 | 61 |
thresholdWhole = 0.5, |
65 | 62 |
initMutant = NULL, |
... | ... |
@@ -458,9 +455,6 @@ oncoSimulIndiv <- function(fp, |
458 | 455 |
keepEvery = sampleEvery, |
459 | 456 |
minDetectDrvCloneSz = "auto", |
460 | 457 |
extraTime = 0, |
461 |
- ## used to be this |
|
462 |
- ## ifelse(model \%in\% c("Bozic", "Exp"), -9, |
|
463 |
- ## 5 * sampleEvery), |
|
464 | 458 |
finalTime = 0.25 * 25 * 365, |
465 | 459 |
onlyCancer = TRUE, |
466 | 460 |
keepPhylog = FALSE, |
... | ... |
@@ -585,84 +579,6 @@ oncoSimulIndiv <- function(fp, |
585 | 579 |
if(is_null_na(finalTime)) finalTime <- Inf |
586 | 580 |
|
587 | 581 |
if(is_null_na(sampleEvery)) stop("sampleEvery cannot be NULL or NA") |
588 |
- |
|
589 |
- ## if(!inherits(fp, "fitnessEffects")) { |
|
590 |
- ## if(any(unlist(lapply(list(fp, |
|
591 |
- ## numPassengers, |
|
592 |
- ## s, sh), is.null)))) { |
|
593 |
- ## m <- paste("You are using the old poset format.", |
|
594 |
- ## "You must specify all of poset, numPassengers", |
|
595 |
- ## "s, and sh.") |
|
596 |
- ## stop(m) |
|
597 |
- |
|
598 |
- ## } |
|
599 |
- ## if(AND_DrvProbExit) { |
|
600 |
- ## stop("The AND_DrvProbExit = TRUE setting is invalid", |
|
601 |
- ## " with the old poset format.") |
|
602 |
- ## } |
|
603 |
- ## if(!is.null(muEF)) |
|
604 |
- ## stop("Mutator effects cannot be specified with the old poset format.") |
|
605 |
- ## if( length(initMutant) > 0) |
|
606 |
- ## warning("With the old poset format you can no longer use initMutant.", |
|
607 |
- ## " The initMutant you passed will be ignored.") |
|
608 |
- ## ## stop("With the old poset, initMutant can only take a single value.") |
|
609 |
- ## if(!is_null_na(fixation)) |
|
610 |
- ## stop("'fixation' cannot be specified with the old poset format.") |
|
611 |
- ## ## Seeding C++ is now much better in new version |
|
612 |
- ## if(is.null(seed) || (seed == "auto")) {## passing a null creates a random seed |
|
613 |
- ## ## name is a legacy. This is really the seed for the C++ generator. |
|
614 |
- ## ## Nope, we cannot use 2^32, because as.integer will fail. |
|
615 |
- ## seed <- as.integer(round(runif(1, min = 0, max = 2^16))) |
|
616 |
- ## } |
|
617 |
- ## if(verbosity >= 2) |
|
618 |
- ## cat(paste("\n Using ", seed, " as seed for C++ generator\n")) |
|
619 |
- |
|
620 |
- ## if(!is_null_na(detectionProb)) stop("detectionProb cannot be used in v.1 objects") |
|
621 |
- ## ## if(message.v1) |
|
622 |
- ## ## message("You are using the old poset format. Consider using the new one.") |
|
623 |
- |
|
624 |
- |
|
625 |
- ## ## A simulation stops if cancer or finalTime appear, the first |
|
626 |
- ## ## one. But if we set onlyCnacer = FALSE, we also accept simuls |
|
627 |
- ## ## without cancer (or without anything) |
|
628 |
- |
|
629 |
- ## op <- try(oncoSimul.internal(poset = fp, ## restrict.table = rt, |
|
630 |
- ## ## numGenes = numGenes, |
|
631 |
- ## numPassengers = numPassengers, |
|
632 |
- ## typeCBN = "CBN", |
|
633 |
- ## birth = birth, |
|
634 |
- ## s = s, |
|
635 |
- ## death = death, |
|
636 |
- ## mu = mu, |
|
637 |
- ## initSize = initSize, |
|
638 |
- ## sampleEvery = sampleEvery, |
|
639 |
- ## detectionSize = detectionSize, |
|
640 |
- ## finalTime = finalTime, |
|
641 |
- ## initSize_species = 2000, |
|
642 |
- ## initSize_iter = 500, |
|
643 |
- ## seed = seed, |
|
644 |
- ## verbosity = verbosity, |
|
645 |
- ## speciesFS = 10000, |
|
646 |
- ## ratioForce = 2, |
|
647 |
- ## typeFitness = typeFitness, |
|
648 |
- ## max.memory = max.memory, |
|
649 |
- ## mutationPropGrowth = mutationPropGrowth, |
|
650 |
- ## initMutant = -1, |
|
651 |
- ## max.wall.time = max.wall.time, |
|
652 |
- ## max.num.tries = max.num.tries, |
|
653 |
- ## keepEvery = keepEvery, |
|
654 |
- ## ## alpha = 0.0015, |
|
655 |
- ## sh = sh, |
|
656 |
- ## K = K, |
|
657 |
- ## minDetectDrvCloneSz = minDetectDrvCloneSz, |
|
658 |
- ## extraTime = extraTime, |
|
659 |
- ## detectionDrivers = detectionDrivers, |
|
660 |
- ## onlyCancer = onlyCancer, |
|
661 |
- ## errorHitWallTime = errorHitWallTime, |
|
662 |
- ## errorHitMaxTries = errorHitMaxTries), |
|
663 |
- ## silent = !verbosity) |
|
664 |
- ## objClass <- "oncosimul" |
|
665 |
- ## } else { |
|
666 | 582 |
s <- sh <- NULL ## force it. |
667 | 583 |
if(numPassengers != 0) |
668 | 584 |
warning(paste("Specifying numPassengers has no effect", |
... | ... |
@@ -934,88 +850,6 @@ plot.oncosimulpop <- function(x, ask = TRUE, |
934 | 850 |
} |
935 | 851 |
|
936 | 852 |
|
937 |
-## plot.oncosimul <- function(x, col = c(8, "orange", 6:1), |
|
938 |
-## log = "y", |
|
939 |
-## ltyClone = 2:6, |
|
940 |
-## lwdClone = 0.9, |
|
941 |
-## ltyDrivers = 1, |
|
942 |
-## lwdDrivers = 3, |
|
943 |
-## xlab = "Time units", |
|
944 |
-## ylab = "Number of cells", |
|
945 |
-## plotClones = TRUE, |
|
946 |
-## plotDrivers = TRUE, |
|
947 |
-## addtot = FALSE, |
|
948 |
-## addtotlwd = 0.5, |
|
949 |
-## yl = NULL, |
|
950 |
-## thinData = FALSE, |
|
951 |
-## thinData.keep = 0.1, |
|
952 |
-## thinData.min = 2, |
|
953 |
-## plotDiversity = FALSE, |
|
954 |
-## ... |
|
955 |
-## ) { |
|
956 |
- |
|
957 |
-## if(thinData) |
|
958 |
-## x <- thin.pop.data(x, keep = thinData.keep, min.keep = thinData.min) |
|
959 |
- |
|
960 |
-## ## uvx |
|
961 |
-## if(!inherits(x, "oncosimul2")) |
|
962 |
-## ndr <- colSums(x$Genotypes[1:x$NumDrivers, , drop = FALSE]) |
|
963 |
-## else { |
|
964 |
-## ndr <- colSums(x$Genotypes[x$Drivers, , drop = FALSE]) |
|
965 |
-## } |
|
966 |
- |
|
967 |
-## if(is.null(yl)) { |
|
968 |
-## if(log %in% c("y", "xy", "yx") ) |
|
969 |
-## yl <- c(1, max(apply(x$pops.by.time[, -1, drop = FALSE], 1, sum))) |
|
970 |
-## else |
|
971 |
-## yl <- c(0, max(apply(x$pops.by.time[, -1, drop = FALSE], 1, sum))) |
|
972 |
-## } |
|
973 |
-## if(plotDiversity) { |
|
974 |
-## par(fig = c(0, 1, 0.8, 1)) |
|
975 |
-## m1 <- par()$mar |
|
976 |
-## m <- m1 |
|
977 |
-## m[c(1, 3)] <- c(0, 0.7) |
|
978 |
-## op <- par(mar = m ) |
|
979 |
-## plotShannon(x) |
|
980 |
-## par(op) |
|
981 |
-## m1[c(3)] <- 0.2 |
|
982 |
-## op <- par(mar = m1) |
|
983 |
-## par(fig = c(0, 1, 0, 0.8), new = TRUE) |
|
984 |
-## } |
|
985 |
-## if(plotClones) { |
|
986 |
-## plotClones(x, |
|
987 |
-## ndr = ndr, |
|
988 |
-## xlab = xlab, |
|
989 |
-## ylab = ylab, |
|
990 |
-## lty = ltyClone, |
|
991 |
-## col = col, |
|
992 |
-## ylim = yl, |
|
993 |
-## lwd = lwdClone, |
|
994 |
-## axes = FALSE, |
|
995 |
-## log = log, |
|
996 |
-## ...) |
|
997 |
-## } |
|
998 |
- |
|
999 |
-## if(plotClones && plotDrivers) |
|
1000 |
-## par(new = TRUE) |
|
1001 |
- |
|
1002 |
-## if(plotDrivers){ |
|
1003 |
-## plotDrivers0(x, |
|
1004 |
-## ndr, |
|
1005 |
-## timescale = 1, |
|
1006 |
-## trim.no.drivers = FALSE, |
|
1007 |
-## xlab = "", ylab = "", |
|
1008 |
-## lwd = lwdDrivers, |
|
1009 |
-## lty = ltyDrivers, |
|
1010 |
-## col = col, |
|
1011 |
-## addtot = addtot, |
|
1012 |
-## addtotlwd = addtotlwd, |
|
1013 |
-## log = log, ylim = yl, |
|
1014 |
-## ...) |
|
1015 |
-## } |
|
1016 |
- |
|
1017 |
-## } |
|
1018 |
- |
|
1019 | 853 |
|
1020 | 854 |
plot.oncosimul <- function(x, |
1021 | 855 |
show = "drivers", |
... | ... |
@@ -1511,10 +1345,6 @@ plotPoset <- function(x, names = NULL, addroot = FALSE, |
1511 | 1345 |
box() |
1512 | 1346 |
} |
1513 | 1347 |
|
1514 |
-## this function seems to never be used |
|
1515 |
-## plotAdjMat <- function(adjmat) { |
|
1516 |
-## plot(as(adjmat, "graphNEL")) |
|
1517 |
-## } |
|
1518 | 1348 |
|
1519 | 1349 |
|
1520 | 1350 |
|
... | ... |
@@ -1591,12 +1421,6 @@ plotClonePhylog <- function(x, N = 1, t = "last", |
1591 | 1421 |
"very fast, before any clones beyond the initial were ", |
1592 | 1422 |
"generated.") |
1593 | 1423 |
pc <- phylogClone(x, N, t, keepEvents) |
1594 |
- ## if(is.na(pc)) { |
|
1595 |
- ## ## This should not be reachable, as caught before |
|
1596 |
- ## ## where we check for nrow of PhylogDF |
|
1597 |
- ## warning("No clone phylogeny available. Exiting without plotting.") |
|
1598 |
- ## return(NULL) |
|
1599 |
- ## } |
|
1600 | 1424 |
|
1601 | 1425 |
l0 <- igraph::layout.reingold.tilford(pc$g) |
1602 | 1426 |
if(!timeEvents) { |
... | ... |
@@ -1724,198 +1548,6 @@ get.mut.vector <- function(x, timeSample, typeSample, |
1724 | 1548 |
} |
1725 | 1549 |
|
1726 | 1550 |
|
1727 |
-## get.mut.vector <- function(x, timeSample, typeSample, |
|
1728 |
-## thresholdWhole, popSizeSample) { |
|
1729 |
-## if(is.null(x$TotalPopSize)) { |
|
1730 |
-## warning(paste("It looks like this simulation never completed.", |
|
1731 |
-## " Maybe it reached maximum allowed limits.", |
|
1732 |
-## " You will get NAs")) |
|
1733 |
-## return(rep(NA, length(x$geneNames))) |
|
1734 |
-## } |
|
1735 |
-## the.time <- get.the.time.for.sample(x, timeSample, popSizeSample) |
|
1736 |
-## if(the.time < 0) { |
|
1737 |
-## return(rep(NA, nrow(x$Genotypes))) |
|
1738 |
-## } |
|
1739 |
-## pop <- x$pops.by.time[the.time, -1] |
|
1740 |
- |
|
1741 |
-## if(all(pop == 0)) { |
|
1742 |
-## stop("You found a bug: this should never happen") |
|
1743 |
-## } |
|
1744 |
- |
|
1745 |
-## if(typeSample %in% c("wholeTumor", "whole")) { |
|
1746 |
-## popSize <- x$PerSampleStats[the.time, 1] |
|
1747 |
-## return( as.numeric((tcrossprod(pop, |
|
1748 |
-## x$Genotypes)/popSize) >= thresholdWhole) ) |
|
1749 |
-## } else if (typeSample %in% c("singleCell", "single")) { |
|
1750 |
- |
|
1751 |
-## return(x$Genotypes[, sample(seq_along(pop), 1, prob = pop)]) |
|
1752 |
-## } else { |
|
1753 |
-## stop("Unknown typeSample option") |
|
1754 |
-## } |
|
1755 |
-## } |
|
1756 |
- |
|
1757 |
- |
|
1758 |
- |
|
1759 |
- |
|
1760 |
- |
|
1761 |
- |
|
1762 |
- |
|
1763 |
- |
|
1764 |
- |
|
1765 |
- |
|
1766 |
- |
|
1767 |
- |
|
1768 |
-## oncoSimul.internal <- function(poset, ## restrict.table, |
|
1769 |
-## numPassengers, |
|
1770 |
-## ## numGenes, |
|
1771 |
-## typeCBN, |
|
1772 |
-## birth, |
|
1773 |
-## s, |
|
1774 |
-## death, |
|
1775 |
-## mu, |
|
1776 |
-## initSize, |
|
1777 |
-## sampleEvery, |
|
1778 |
-## detectionSize, |
|
1779 |
-## finalTime, |
|
1780 |
-## initSize_species, |
|
1781 |
-## initSize_iter, |
|
1782 |
-## seed, |
|
1783 |
-## verbosity, |
|
1784 |
-## speciesFS, |
|
1785 |
-## ratioForce, |
|
1786 |
-## typeFitness, |
|
1787 |
-## max.memory, |
|
1788 |
-## mutationPropGrowth, ## make it explicit |
|
1789 |
-## initMutant, |
|
1790 |
-## max.wall.time, |
|
1791 |
-## keepEvery, |
|
1792 |
-## alpha, |
|
1793 |
-## sh, |
|
1794 |
-## K, |
|
1795 |
-## ## endTimeEvery, |
|
1796 |
-## detectionDrivers, |
|
1797 |
-## onlyCancer, |
|
1798 |
-## errorHitWallTime, |
|
1799 |
-## max.num.tries, |
|
1800 |
-## errorHitMaxTries, |
|
1801 |
-## minDetectDrvCloneSz, |
|
1802 |
-## extraTime) { |
|
1803 |
- |
|
1804 |
-## ## the value of 20000, in megabytes, for max.memory sets a limit of ~ 20 GB |
|
1805 |
- |
|
1806 |
- |
|
1807 |
-## ## if(keepEvery < sampleEvery) |
|
1808 |
-## ## warning("setting keepEvery to sampleEvery") |
|
1809 |
- |
|
1810 |
-## ## a backdoor to allow passing restrictionTables directly |
|
1811 |
-## if(inherits(poset, "restrictionTable")) |
|
1812 |
-## restrict.table <- poset |
|
1813 |
-## else |
|
1814 |
-## restrict.table <- poset.to.restrictTable(poset) |
|
1815 |
-## numDrivers <- nrow(restrict.table) |
|
1816 |
-## numGenes <- (numDrivers + numPassengers) |
|
1817 |
-## if(numGenes < 2) |
|
1818 |
-## stop("There must be at least two genes (loci) in the fitness effects.", |
|
1819 |
-## "If you only care about a degenerate case with just one,", |
|
1820 |
-## "you can enter a second gene", |
|
1821 |
-## "with fitness effect of zero.") |
|
1822 |
-## if(numGenes > 64) |
|
1823 |
-## stop("Largest possible number of genes (loci) is 64 for version 1.", |
|
1824 |
-## "You are strongly encouraged to use the new specification", |
|
1825 |
-## "as in version 2.") |
|
1826 |
- |
|
1827 |
-## ## These can never be set by the user |
|
1828 |
-## ## if(initSize_species < 10) { |
|
1829 |
-## ## warning("initSize_species too small?") |
|
1830 |
-## ## } |
|
1831 |
-## ## if(initSize_iter < 100) { |
|
1832 |
-## ## warning("initSize_iter too small?") |
|
1833 |
-## ## } |
|
1834 |
- |
|
1835 |
-## ## numDrivers <- nrow(restrict.table) |
|
1836 |
-## if(length(unique(restrict.table[, 1])) != numDrivers) |
|
1837 |
-## stop("BAIL OUT NOW: length(unique(restrict.table[, 1])) != numDrivers)") |
|
1838 |
-## ddr <- restrict.table[, 1] |
|
1839 |
-## if(any(diff(ddr) != 1)) |
|
1840 |
-## stop("BAIL OUT NOW: any(diff(ddr) != 1") |
|
1841 |
-## ## sanity checks |
|
1842 |
-## if(max(restrict.table[, 1]) != numDrivers) |
|
1843 |
-## stop("BAIL OUT NOW: max(restrict.table[, 1]) != numDrivers") |
|
1844 |
-## if(numDrivers > numGenes) |
|
1845 |
-## stop("BAIL OUT NOW: numDrivers > numGenes") |
|
1846 |
- |
|
1847 |
-## non.dep.drivers <- restrict.table[which(restrict.table[, 2] == 0), 1] |
|
1848 |
- |
|
1849 |
- |
|
1850 |
- |
|
1851 |
- |
|
1852 |
-## ## if( (is.null(endTimeEvery) || (endTimeEvery > 0)) && |
|
1853 |
-## ## (typeFitness %in% c("bozic1", "exp") )) { |
|
1854 |
-## ## warning(paste("endTimeEvery will take a positive value. ", |
|
1855 |
-## ## "This will make simulations not stop until the next ", |
|
1856 |
-## ## "endTimeEvery has been reached. Thus, in simulations ", |
|
1857 |
-## ## "with very fast growth, simulations can take a long ", |
|
1858 |
-## ## "time to finish, or can hit the wall time limit. ")) |
|
1859 |
-## ## } |
|
1860 |
-## ## if(is.null(endTimeEvery)) |
|
1861 |
-## ## endTimeEvery <- keepEvery |
|
1862 |
-## ## if( (endTimeEvery > 0) && (endTimeEvery %% keepEvery) ) |
|
1863 |
-## ## warning("!(endTimeEvery %% keepEvery)") |
|
1864 |
-## ## a sanity check in restricTable, so no neg. indices for the positive deps |
|
1865 |
-## neg.deps <- function(x) { |
|
1866 |
-## ## checks a row of restrict.table |
|
1867 |
-## numdeps <- x[2] |
|
1868 |
-## if(numdeps) { |
|
1869 |
-## deps <- x[3:(3+numdeps - 1)] |
|
1870 |
-## return(any(deps < 0)) |
|
1871 |
-## } else FALSE |
|
1872 |
-## } |
|
1873 |
-## if(any(apply(restrict.table, 1, neg.deps))) |
|
1874 |
-## stop("BAIL OUT NOW: Negative dependencies in restriction table") |
|
1875 |
- |
|
1876 |
-## ## transpose the table |
|
1877 |
-## rtC <- convertRestrictTable(restrict.table) |
|
1878 |
- |
|
1879 |
- |
|
1880 |
-## return(c( |
|
1881 |
-## BNB_Algo5(restrictTable = rtC, |
|
1882 |
-## numDrivers = numDrivers, |
|
1883 |
-## numGenes = numGenes, |
|
1884 |
-## typeCBN_= typeCBN, |
|
1885 |
-## s = s, |
|
1886 |
-## death = death, |
|
1887 |
-## mu = mu, |
|
1888 |
-## initSize = initSize, |
|
1889 |
-## sampleEvery = sampleEvery, |
|
1890 |
-## detectionSize = detectionSize, |
|
1891 |
-## finalTime = finalTime, |
|
1892 |
-## initSp = initSize_species, |
|
1893 |
-## initIt = initSize_iter, |
|
1894 |
-## seed = seed, |
|
1895 |
-## verbosity = verbosity, |
|
1896 |
-## speciesFS = speciesFS, |
|
1897 |
-## ratioForce = ratioForce, |
|
1898 |
-## typeFitness_ = typeFitness, |
|
1899 |
-## maxram = max.memory, |
|
1900 |
-## mutationPropGrowth = as.integer(mutationPropGrowth), |
|
1901 |
-## initMutant = initMutant, |
|
1902 |
-## maxWallTime = max.wall.time, |
|
1903 |
-## keepEvery = keepEvery, |
|
1904 |
-## sh = sh, |
|
1905 |
-## K = K, |
|
1906 |
-## detectionDrivers = detectionDrivers, |
|
1907 |
-## onlyCancer = onlyCancer, |
|
1908 |
-## errorHitWallTime = errorHitWallTime, |
|
1909 |
-## maxNumTries = max.num.tries, |
|
1910 |
-## errorHitMaxTries = errorHitMaxTries, |
|
1911 |
-## minDetectDrvCloneSz = minDetectDrvCloneSz, |
|
1912 |
-## extraTime = extraTime |
|
1913 |
-## ), |
|
1914 |
-## NumDrivers = numDrivers |
|
1915 |
-## )) |
|
1916 |
- |
|
1917 |
-## } |
|
1918 |
- |
|
1919 | 1551 |
OncoSimulWide2Long <- function(x) { |
1920 | 1552 |
## Put data in long format, for ggplot et al |
1921 | 1553 |
|
... | ... |
@@ -1947,23 +1579,6 @@ OncoSimulWide2Long <- function(x) { |
1947 | 1579 |
|
1948 | 1580 |
|
1949 | 1581 |
|
1950 |
-## We are not using this anymore |
|
1951 |
-## create.muts.by.time <- function(tmp) { ## tmp is the output from Algorithm5 |
|
1952 |
-## if(tmp$NumClones > 1) { |
|
1953 |
-## NumMutations <- apply(tmp$Genotypes, 2, sum) |
|
1954 |
-## muts.by.time <- cbind(tmp$pops.by.time[, c(1), drop = FALSE], |
|
1955 |
-## t(apply(tmp$pops.by.time[, -c(1), |
|
1956 |
-## drop = FALSE], 1, |
|
1957 |
-## function(x) tapply(x, |
|
1958 |
-## NumMutations, sum)))) |
|
1959 |
-## colnames(muts.by.time)[c(1)] <- "Time" |
|
1960 |
-## } else { |
|
1961 |
-## muts.by.time <- tmp$pops.by.time |
|
1962 |
-## } |
|
1963 |
-## return(muts.by.time) |
|
1964 |
-## } |
|
1965 |
- |
|
1966 |
- |
|
1967 | 1582 |
create.drivers.by.time <- function(tmp, ndr) { |
1968 | 1583 |
## CountNumDrivers <- apply(tmp$Genotypes[1:numDrivers, ,drop = FALSE], 2, sum) |
1969 | 1584 |
CountNumDrivers <- ndr |
... | ... |
@@ -2084,248 +1699,9 @@ is_null_na <- function(x) { |
2084 | 1699 |
|
2085 | 1700 |
|
2086 | 1701 |
|
2087 |
- |
|
2088 |
- |
|
2089 |
- |
|
2090 |
- |
|
2091 |
- |
|
2092 |
- |
|
2093 |
- |
|
2094 |
- |
|
2095 |
- |
|
2096 | 1702 |
## simpsonI <- function(x) { |
2097 | 1703 |
## sx <- sum(x) |
2098 | 1704 |
## p <- x/sx |
2099 | 1705 |
## p <- p[p > 0] |
2100 | 1706 |
## return(sum(p^2))) |
2101 | 1707 |
## } |
2102 |
- |
|
2103 |
-## plotSimpson <- function(z) { |
|
2104 |
- |
|
2105 |
-## h <- apply(z$pops.by.time[, 2:ncol(z$pops.by.time), drop = FALSE], |
|
2106 |
-## 1, shannonI) |
|
2107 |
-## plot(x = z$pops.by.time[, 1], |
|
2108 |
-## y = h, lty = "l", xlab = "", ylab = "H") |
|
2109 |
-## } |
|
2110 |
- |
|
2111 |
- |
|
2112 |
-## plotClones <- function(z, ndr = NULL, na.subs = TRUE, |
|
2113 |
-## log = "y", type = "l", |
|
2114 |
-## lty = 1:8, col = 1:9, ...) { |
|
2115 |
- |
|
2116 |
-## ## if given ndr, we order columns based on ndr, so clones with more |
|
2117 |
-## ## drivers are plotted last |
|
2118 |
- |
|
2119 |
-## y <- z$pops.by.time[, 2:ncol(z$pops.by.time), drop = FALSE] |
|
2120 |
- |
|
2121 |
-## if(na.subs){ |
|
2122 |
-## y[y == 0] <- NA |
|
2123 |
-## } |
|
2124 |
-## if(!is.null(ndr)) { |
|
2125 |
-## ## could be done above, to avoid creating |
|
2126 |
-## ## more copies |
|
2127 |
-## oo <- order(ndr) |
|
2128 |
-## y <- y[, oo, drop = FALSE] |
|
2129 |
-## ndr <- ndr[oo] |
|
2130 |
-## col <- col[ndr + 1] |
|
2131 |
-## } |
|
2132 |
-## matplot(x = z$pops.by.time[, 1], |
|
2133 |
-## y = y, |
|
2134 |
-## log = log, type = type, |
|
2135 |
-## col = col, lty = lty, |
|
2136 |
-## ...) |
|
2137 |
-## box() |
|
2138 |
-## } |
|
2139 |
- |
|
2140 |
- |
|
2141 |
- |
|
2142 |
- |
|
2143 |
- |
|
2144 |
-## No longer used |
|
2145 |
-## rtNoDep <- function(numdrivers) { |
|
2146 |
-## ## create a restriction table with no dependencies |
|
2147 |
-## x <- matrix(nrow = numdrivers, ncol = 3) |
|
2148 |
-## x[, 1] <- 1:numdrivers |
|
2149 |
-## x[, 2] <- 0 |
|
2150 |
-## x[, 3] <- -9 |
|
2151 |
-## return(x) |
|
2152 |
-## } |
|
2153 |
- |
|
2154 |
- |
|
2155 |
-## Simulate from generative model. This is a quick function, and is most |
|
2156 |
-## likely wrong! Never used for anything. |
|
2157 |
- |
|
2158 |
-## simposet <- function(poset, p) { |
|
2159 |
-## ## if (length(parent.nodes) != length (child.nodes)){ |
|
2160 |
-## ## print("An Error Occurred") |
|
2161 |
-## ## } |
|
2162 |
-## ## else { |
|
2163 |
-## num.genes <- max(poset) - 1 ## as root is not a gene |
|
2164 |
-## genotype <-t(c(1, rep(NA, num.genes))) |
|
2165 |
-## colnames(genotype) <- as.character(0:num.genes) |
|
2166 |
- |
|
2167 |
- |
|
2168 |
-## poset$runif <- runif(nrow(poset)) |
|
2169 |
-## ## this.relation.prob.OK could be done outside, but having it inside |
|
2170 |
-## ## the loop would allow to use different thresholds for different |
|
2171 |
-## ## relationships |
|
2172 |
-## for (i in (1:nrow(poset))) { |
|
2173 |
-## child <- poset[i, 2] |
|
2174 |
-## this.relation.prob.OK <- as.numeric(poset[i, "runif"] > p) |
|
2175 |
-## the.parent <- genotype[ poset[i, 1] ] ## it's the value of parent in genotype. |
|
2176 |
-## if (is.na(genotype[child])){ |
|
2177 |
-## genotype[child] <- this.relation.prob.OK * the.parent |
|
2178 |
-## } |
|
2179 |
-## else |
|
2180 |
-## genotype[child] <- genotype[child]*(this.relation.prob.OK * the.parent) |
|
2181 |
-## } |
|
2182 |
-## ## } |
|
2183 |
- |
|
2184 |
-## return(genotype) |
|
2185 |
-## } |
|
2186 |
- |
|
2187 |
- |
|
2188 |
-## to plot and adjacency matrix in this context can do |
|
2189 |
-## plotPoset(intAdjMatToPoset(adjMat)) |
|
2190 |
-## where intAdjMatToPoset is from best oncotree code: generate-random-trees. |
|
2191 |
-## No! the above is simpler |
|
2192 |
- |
|
2193 |
- |
|
2194 |
- |
|
2195 |
- |
|
2196 |
-## get.mut.vector.whole <- function(tmp, timeSample = "last", threshold = 0.5) { |
|
2197 |
-## ## Obtain, from results from a simulation run, the vector |
|
2198 |
-## ## of 0/1 corresponding to each gene. |
|
2199 |
- |
|
2200 |
-## ## threshold is the min. proportion for a mutation to be detected |
|
2201 |
-## ## We are doing whole tumor sampling here, as in Sprouffske |
|
2202 |
- |
|
2203 |
-## ## timeSample: do we sample at end, or at a time point, chosen |
|
2204 |
-## ## randomly, from all those with at least one driver? |
|
2205 |
- |
|
2206 |
-## if(timeSample == "last") { |
|
2207 |
-## if(tmp$TotalPopSize == 0) |
|
2208 |
-## warning(paste("Final population size is 0.", |
|
2209 |
-## "Thus, there is nothing to sample with ", |
|
2210 |
-## "sampling last. You will get NAs")) |
|
2211 |
-## return(as.numeric( |
|
2212 |
-## (tcrossprod(tmp$pops.by.time[nrow(tmp$pops.by.time), -1], |
|
2213 |
-## tmp$Genotypes)/tmp$TotalPopSize) > threshold)) |
|
2214 |
-## } else if (timeSample %in% c("uniform", "unif")) { |
|
2215 |
-## candidate.time <- which(tmp$PerSampleStats[, 4] > 0) |
|
2216 |
- |
|
2217 |
-## if (length(candidate.time) == 0) { |
|
2218 |
-## warning(paste("There is not a single sampled time", |
|
2219 |
-## "at which there are any mutants.", |
|
2220 |
-## "Thus, no uniform sampling possible.", |
|
2221 |
-## "You will get NAs")) |
|
2222 |
-## return(rep(NA, nrow(tmp$Genotypes))) |
|
2223 |
-## } else if (length(candidate.time) == 1) { |
|
2224 |
-## the.time <- candidate.time |
|
2225 |
-## } else { |
|
2226 |
-## the.time <- sample(candidate.time, 1) |
|
2227 |
-## } |
|
2228 |
-## pop <- tmp$pops.by.time[the.time, -1] |
|
2229 |
-## popSize <- tmp$PerSampleStats[the.time, 1] |
|
2230 |
-## ## if(popSize == 0) |
|
2231 |
-## ## warning(paste("Population size at this time is 0.", |
|
2232 |
-## ## "Thus, there is nothing to sample at this time point.", |
|
2233 |
-## ## "You will get NAs")) |
|
2234 |
-## return( as.numeric((tcrossprod(pop, |
|
2235 |
-## tmp$Genotypes)/popSize) > threshold) ) |
|
2236 |
-## } |
|
2237 |
-## } |
|
2238 |
- |
|
2239 |
- |
|
2240 |
- |
|
2241 |
-## the.time <- sample(which(tmp$PerSampleStats[, 4] > 0), 1) |
|
2242 |
-## if(length(the.time) == 0) { |
|
2243 |
-## warning(paste("There are no clones with drivers at any time point.", |
|
2244 |
-## "No uniform sampling possible.", |
|
2245 |
-## "You will get a vector of NAs.")) |
|
2246 |
-## return(rep(NA, nrow(tmp$Genotypes))) |
|
2247 |
-## } |
|
2248 |
-## get.mut.vector.singlecell <- function(tmp, timeSample = "last") { |
|
2249 |
-## ## No threshold, as single cell. |
|
2250 |
- |
|
2251 |
-## ## timeSample: do we sample at end, or at a time point, chosen |
|
2252 |
-## ## randomly, from all those with at least one driver? |
|
2253 |
- |
|
2254 |
-## if(timeSample == "last") { |
|
2255 |
-## the.time <- nrow(tmp$pops.by.time) |
|
2256 |
-## } else if (timeSample %in% c("uniform", "unif")) { |
|
2257 |
-## candidate.time <- which(tmp$PerSampleStats[, 4] > 0) |
|
2258 |
- |
|
2259 |
-## if (length(candidate.time) == 0) { |
|
2260 |
-## warning(paste("There is not a single sampled time", |
|
2261 |
-## "at which there are any mutants.", |
|
2262 |
-## "Thus, no uniform sampling possible.", |
|
2263 |
-## "You will get NAs")) |
|
2264 |
-## return(rep(NA, nrow(tmp$Genotypes))) |
|
2265 |
-## } else if (length(candidate.time) == 1) { |
|
2266 |
-## the.time <- candidate.time |
|
2267 |
-## } else { |
|
2268 |
-## the.time <- sample(candidate.time, 1) |
|
2269 |
-## } |
|
2270 |
- |
|
2271 |
-## } |
|
2272 |
-## pop <- tmp$pops.by.time[the.time, -1] |
|
2273 |
-## ## popSize <- tmp$PerSampleStats[the.time, 1] |
|
2274 |
-## ## genot <- sample(seq_along(pop), 1, prob = pop) |
|
2275 |
-## if(all(pop == 0)) { |
|
2276 |
-## warning(paste("All clones have a population size of 0", |
|
2277 |
-## "at the chosen time. Nothing to sample.", |
|
2278 |
-## "You will get NAs")) |
|
2279 |
-## return(rep(NA, nrow(tmp$Genotypes))) |
|
2280 |
-## } else { |
|
2281 |
-## return(tmp$Genotypes[, sample(seq_along(pop), 1, prob = pop)]) |
|
2282 |
-## } |
|
2283 |
-## } |
|
2284 |
- |
|
2285 |
- |
|
2286 |
-## get.mut.vector <- function(x, timeSample = "whole", typeSample = "last", |
|
2287 |
-## thresholdWhole = 0.5) { |
|
2288 |
-## if(typeSample %in% c("wholeTumor", "whole")) { |
|
2289 |
-## get.mut.vector.whole(x, timeSample = timeSample, |
|
2290 |
-## threshold = thresholdWhole) |
|
2291 |
-## } else if(typeSample %in% c("singleCell", "single")) { |
|
2292 |
-## get.mut.vector.singlecell(x, timeSample = timeSample) |
|
2293 |
-## } |
|
2294 |
-## } |
|
2295 |
- |
|
2296 |
- |
|
2297 |
- |
|
2298 |
- |
|
2299 |
- |
|
2300 |
-## plotClonePhylog <- function(x, timeEvent = FALSE, |
|
2301 |
-## showEvents = TRUE, |
|
2302 |
-## fixOverlap = TRUE) { |
|
2303 |
-## if(!inherits(x, "oncosimul2")) |
|
2304 |
-## stop("Phylogenetic information is only stored with v >=2") |
|
2305 |
-## if(nrow(x$other$PhylogDF) == 0) |
|
2306 |
-## stop("It seems you run the simulation with keepPhylog= FALSE") |
|
2307 |
-## ## requireNamespace("igraph") |
|
2308 |
-## df <- x$other$PhylogDF |
|
2309 |
-## if(!showEvents) { |
|
2310 |
-## df <- df[!duplicated(df[, c(1, 2)]), ] |
|
2311 |
-## } |
|
2312 |
-## g <- igraph::graph.data.frame(df) |
|
2313 |
-## l0 <- igraph::layout.reingold.tilford(g) |
|
2314 |
-## if(!timeEvent) { |
|
2315 |
-## plot(g, layout = l0) |
|
2316 |
-## } else { |
|
2317 |
-## l1 <- l0 |
|
2318 |
-## indexAppear <- match(V(g)$name, as.character(df[, 2])) |
|
2319 |
-## firstAppear <- df$time[indexAppear] |
|
2320 |
-## firstAppear[1] <- 0 |
|
2321 |
-## l1[, 2] <- (max(firstAppear) - firstAppear) |
|
2322 |
-## if(fixOverlap) { |
|
2323 |
-## dx <- which(duplicated(l1[, 1])) |
|
2324 |
-## if(length(dx)) { |
|
2325 |
-## ra <- range(l1[, 1]) |
|