## File S4. Function to compute number of recombinations with penalty ## Takes as input an mpprob object with founder probabilities calculated nrec <- function(mpp, penalty=3) { if (!inherits(mpp, "mpprob")) stop("Must run mpprob first\n") output = list() nfou=nrow(mpp$founders) mosaic = mpp$estfnd recevents = list() for (i in 1:length(mpp$map)) { for (j in 1:nrow(mpp$finals)) { mat <- matrix(mpp$prob[[i]][j,], ncol=nfou, byrow=T) mosaic[[i]][j,] = optpath(mat, penalty)$ids } recevents[[i]] <- apply(mosaic[[i]], 1, function(x) return(sum(diff(x[!is.na(x)])!=0))) } ## Recombination events occurring in the genome output$mosaic <- mosaic ## Number of recombination events per chromosome output$nrec <- recevents ## Total number of recombinations per line output$totrec <- rowSums(do.call("cbind", output$nrec)) output } #### C function for computing recombination events library(inline) library(RcppArmadillo) optpath <- cxxfunction(signature(probs="numeric", penalty="numeric"), plugin="RcppArmadillo", body=' arma::mat pr = Rcpp::as(probs); int nr = pr.n_rows, nf=pr.n_cols; double mms = Rcpp::as(penalty); arma::mat onevec = arma::ones(nf, 1); arma::vec strains = arma::zeros(nf); arma::mat pathscore = arma::zeros(1, nf); arma::mat D = arma::ones(nf, nf); arma::mat bestpath = arma::randu(nr, nf); arma::mat y = arma::zeros(1, nf); arma::mat X = arma::zeros(nf, nf); arma::vec w = arma::zeros(nf); arma::vec ids = arma::zeros(nr); arma::vec steps = arma::zeros(nr); arma::mat tmp = arma::zeros(1, nf); int i, j; arma::uword m, k, step; for (i=0; i