Scenario: Parallel “universe” lattices with some migration between “universes” in a zone of “universe” overlap.

A demographic model is a list of population models on the same grid, with a bit more information (time to switch for each). The way to build a demographic model is to:

  1. Start with a grid,
  2. then moving backwards in time, copy the last state,
  3. then modify it.

The function add_to_demography does this copy-and-modify step; and demographic models can be modified directly by subsetting.

Building the demographic model

Initialization

First, let’s choose the parameters for the grid:

nrow <- 100               # width of grid
ncol <- 40                # height of grid
overlap.size <- 20        # with of the overlap (in the middle)
admix.rate <- 0.05
mig.rate <- 1

Now, set up the grid.

ga <- grid_array(nlayers=2,nrow=nrow,ncol=ncol,N=1,mig.rate=mig.rate,admix.rate=admix.rate) 
plot(ga)

Create the overlap

Then, we modify that first state, by zeroing out populations in each layer where we don’t want them. We do this by creating ‘masks’ for each and using modify_grid_layer, which multiplies N by the mask we pass in with dN:

mask.1 <- mask.2 <- matrix(1,nrow=nrow(ga),ncol=ncol(ga))
mask.1[row(mask.1) > nrow(ga)/2+overlap.size/2] <- 0
mask.2[row(mask.1) < nrow(ga)/2-overlap.size/2] <- 0
ga <- modify_grid_layer( ga, layer=1, dN=mask.1 )
ga <- modify_grid_layer( ga, layer=2, dN=mask.2 )
plot(ga)

Simulating with ms

Trees, in R

Since we’ve only got one, unchanging, demographic scenario, we don’t have to make a special demography object. We’ll use a convenience function to pick some random sampling locations, two each from 30 random locations.

sample.config <- sample_locations( ga, n=30, each=2 )
sample.cols <- rainbow(nrow(sample.config))
sample.config
##       row col layer n
##  [1,]  11   8     1 2
##  [2,]  54   8     1 2
##  [3,]  25  12     1 2
##  [4,]  78  15     1 2
##  [5,]  99  19     1 2
##  [6,]  17  24     1 2
##  [7,]  29  26     1 2
##  [8,]  50  32     1 2
##  [9,]  80  35     1 2
## [10,]  45  36     1 2
## [11,]  53  39     1 2
## [12,]  96   2     2 2
## [13,]  33   3     2 2
## [14,]  26  14     2 2
## [15,]  92  14     2 2
## [16,]  15  19     2 2
## [17,]  27  20     2 2
## [18,]  31  24     2 2
## [19,]  13  25     2 2
## [20,]  48  27     2 2
## [21,]  66  27     2 2
## [22,]  44  31     2 2
## [23,]  52  32     2 2
## [24,]  97  33     2 2
## [25,]  77  34     2 2
## [26,]  62  36     2 2
## [27,]  11  37     2 2
## [28,]  55  37     2 2
## [29,]  37  38     2 2
## [30,]  84  40     2 2

With this in hand, we can run ms. We’ll ask it to output trees, not sequence. For some reason, it gives an error if we keep it all in R: maybe the string of parameters is too long? (Using tofile=TRUE puts the parameters in a file, passed to ms via -f.)

ms.output <- run_ms( ga, nsamp=sample.config, trees=TRUE, nreps=3 )
## Outputting to  msarg_752142  .
tree.output <- trees_from_ms( file.path(ms.output,"msoutput.txt") )
layout(t(1:2))
for (tree in tree.output) {
    plot_sample_config( ga, sample.config )
    abline(v=(nrow(ga)+c(1,-1)*overlap.size)/2, lty=2, lwd=2, col='grey')
    ape::plot.phylo( tree, tip.color=sample.cols[findInterval(as.numeric(tree$tip.label),1+c(0,cumsum(sample.config[,4])))] )
    ape::axisPhylo(1)
}

Sequence, to a file

To get sequence, we could do as follows, this time outputting to a directory:

ms.output <- run_ms( ga, nsamp=sample.config, theta=0.01 )
## Outputting to  msarg_976869  .
cat(paste(scan(file.path(ms.output,"msoutput.txt"),what='char',sep='\n'),collapse='\n'))
## ms 60 1 -t 0.01 -f msarg_976869/msarg.txt 
## 11265 54137 46168
## //
## segsites: 301
## positions: 0.0013 0.0048 0.0081 0.0098 0.0161 0.0205 0.0215 0.0221 0.0256 0.0264 0.0318 0.0319 0.0333 0.0410 0.0413 0.0415 0.0428 0.0436 0.0457 0.0488 0.0495 0.0512 0.0532 0.0534 0.0564 0.0577 0.0678 0.0682 0.0805 0.0844 0.0890 0.0903 0.0939 0.0962 0.0968 0.0972 0.0990 0.1006 0.1072 0.1113 0.1188 0.1198 0.1219 0.1243 0.1292 0.1310 0.1333 0.1410 0.1426 0.1449 0.1483 0.1484 0.1518 0.1527 0.1552 0.1628 0.1674 0.1711 0.1742 0.1766 0.1793 0.1843 0.1852 0.1865 0.1870 0.1874 0.1956 0.2006 0.2009 0.2021 0.2062 0.2191 0.2318 0.2321 0.2343 0.2353 0.2377 0.2480 0.2492 0.2502 0.2540 0.2590 0.2670 0.2726 0.2743 0.2800 0.2820 0.2843 0.2844 0.2905 0.2905 0.2946 0.2966 0.3023 0.3026 0.3028 0.3032 0.3128 0.3256 0.3269 0.3283 0.3335 0.3355 0.3365 0.3371 0.3386 0.3440 0.3441 0.3446 0.3457 0.3465 0.3472 0.3472 0.3579 0.3582 0.3585 0.3636 0.3706 0.3755 0.3900 0.3923 0.3952 0.3962 0.3973 0.4002 0.4003 0.4108 0.4149 0.4217 0.4264 0.4295 0.4296 0.4301 0.4328 0.4488 0.4499 0.4507 0.4525 0.4527 0.4601 0.4643 0.4648 0.4660 0.4770 0.4840 0.4851 0.4852 0.4858 0.4876 0.4899 0.4905 0.4911 0.4923 0.4937 0.5052 0.5131 0.5133 0.5181 0.5203 0.5220 0.5260 0.5269 0.5316 0.5402 0.5439 0.5442 0.5510 0.5694 0.5700 0.5761 0.5767 0.5768 0.5783 0.5822 0.5864 0.5883 0.5923 0.5983 0.5990 0.5998 0.6003 0.6056 0.6098 0.6101 0.6150 0.6154 0.6181 0.6252 0.6287 0.6325 0.6402 0.6432 0.6437 0.6445 0.6481 0.6485 0.6500 0.6549 0.6641 0.6692 0.6746 0.6949 0.6953 0.6998 0.7054 0.7065 0.7103 0.7166 0.7190 0.7190 0.7200 0.7237 0.7272 0.7324 0.7386 0.7420 0.7423 0.7512 0.7567 0.7671 0.7680 0.7695 0.7710 0.7728 0.7740 0.7759 0.7775 0.7866 0.7885 0.7928 0.7945 0.7977 0.7982 0.8067 0.8088 0.8099 0.8187 0.8189 0.8218 0.8270 0.8287 0.8337 0.8365 0.8470 0.8490 0.8498 0.8504 0.8550 0.8584 0.8637 0.8667 0.8671 0.8680 0.8687 0.8748 0.8790 0.8871 0.8873 0.8944 0.8978 0.9009 0.9021 0.9027 0.9033 0.9055 0.9055 0.9073 0.9162 0.9188 0.9195 0.9233 0.9311 0.9344 0.9346 0.9378 0.9385 0.9398 0.9399 0.9407 0.9449 0.9475 0.9501 0.9504 0.9532 0.9543 0.9561 0.9615 0.9620 0.9625 0.9666 0.9751 0.9759 0.9764 0.9771 0.9780 0.9806 0.9853 0.9885 0.9934 0.9986 0.9993 
## 0000100010100000100000001000010000001001101100000000100000000001000010100000000011000001100000000000000001010010000101000011000000010100000001100000000000000000010000000101010100000000010100001000100000000000000001001000000000000001000010011000000111001000000000100000000000000001000100000000000001001
## 0000100010100000100000001000010000001001101100000000100000000001000010100000000011000001100000000000000001010010000101000011000000010100000001100000000000000000010000000101010100000000010100001000100000000000000001001000000000000001000010011000000111001000000000100000000000000001000100000000000001001
## 1010000000000000000000000000000000110000000001001010000101000100100000010000000000100000000100001001000000000001100000000000000000001000100000001010010100000010000000000000000001000010100000000010000000100000000100000000000001100100000000000000000000000011000001010000000000001100000011001000001010000
## 0100000001000000000111010000001001000110000000100000000000100000011000000100100000010000010010000110000011000000000100000000110100100010010000000000101000000101101000000000101000000000000010010000000010000100000010000000010010000000100000000000000100100000000010001000001101010000110000000001110100000
## 0100000000000010010011010001000001000110000000000001010000110000011001000100100000010100011000000110000011000000000100000000110100100010010000010000100000000100100000000000101000000000000010010000001011000000000000100010010010001000000000000000010100100000000010001000001101010000010000000001010000010
## 0100000000000010010011010001000001000110000000000001010000110000011001000100100000010100011000000110000011000000000100000000110100100010010000010000100000000100100000000000101000000000000010010000001011000000000000100010010010001000000000000000010100100000000010001000001101010000010000000001010000010
## 1011000000000000000000000100000000110000000000010010000101000100100000010000000000100000000100101001100000000001100000000000000000001000100000001010000100000010000000000000000001000010100000000010000000100000000100000000000001100100000000000000000000000011000001010000000000001100000011001000001000000
## 1011000000000000000000000100000000110000000000010010000101000100100000010000000000100000000100101001100000000001100000000000000000001000100000001010000100000010000000000000000001000010100000000010000000100000000100000000000001100100000000000000000000000011000001010000000000001100000011001000001000000
## 0100000000000010010011010010000001000110000000000001000000110000011001000100100000010100011000000110000011000000000100000000110100100010011000000000100000000100100000000000101000000000000010010001000011000000000000010110010010001000000000000001010100100000000010001000001101010000010000000001010000010
## 0100000000000010010011010010000001000110000000000001000000110000011001000100100000010100011000000110000011000000000100000000110100100010011000000000100000000100100000000000101000000000000010010001000011000000000000010110010010001000000000000001010100100000000010001000001101010000010000000001010000010
## 0100000000000010010011010000000001000110000000000001000010110010011001000100100000010100011001000110010011000000000100001000110100100010010000000000100000000100100000000000101000000000000010110001000011000000000000000010010010001000000000000001010100100000000010001001001101010000010000000001010000010
## 0100000000000010010011010000000001000110000000000001000010110010011001000100100000010100011001000110000011000000000100001000110100100010010000000000100000000100100000000000101000000000000010010001000011000010000000000010010010001000000000000001010100100000000010001001001101010000010000000001010000010
## 0100000000000110011011010000000001000110000010000001000000111000011001000100100000010100011000000110000011000000000100000000110100100010010000000000100000000100100000000000101000000000000010010001000011000000000000000110010010001000000000100001010100100000000010001000001101010000010000000001010000010
## 0100000100000000000111010000000101000110000000000000000000100000011000000100100000010000010010000110000011000000000110000000110110100010010000000001100000000101100000000000101000000000000010010100000010000100000010000001010110000000000000000000000100100000000010001000001101010000010000000001010000000
## 0100000000000000000011010000000001000110000000000000001000100000011000000100110000010000010010010110000111000000000100010000111101100010010000000000100001000101100000010010101000000000000010010000000010010100000010000000110010000000000101000000000100100000110010001000001101010000011000000111010000000
## 0100001000000000010011010000000001000110000000000000000000100000011100000101101100010000010000000110001011100000001100000000110100100010010000000100100000000100100001000000101010110100000011010000000010000000100000000000010010000000010000000000100100100000000010001000001101010000010000000001010000100
## 0100000000000000000011010000100001000110000000000000001000100000011000000100100000010000010010000110000011000000000100000000111101100010010000000000100001000101100000010010101000000000001010010000000010000100000010000000110010000000000001000000001100100000110110001100001111010000011000100011010000000
## 0100000000000000000011010000100001000110000000000000001000100000011000000100100000010000010010000110000011000000000100000000111101100010010000000000100001000101100000010010101000000000001010010000000010000100000010000000110010000000000001000000001100100000110110001100001111010000011000100011010000000
## 0100000000000000000011010000000001000110000000000000001000100000011000000100110000010000010010010110000111000000000100010000111101100010010000000000100001000101100000010010101000000000000010010000000010010100000010000000110010000000000101000000000100100000110010001000001101010000011000000111010000000
## 1010000000000000000000000000000000110000000001000010000101000100100000010000000000100000000100001001000000000001100000000000000000001000100000001010010100000010000000000000000001000011100000000010010000100000000100000000000001100100000000000000000000000011000001010000000000001100000011001000001010000
## 0100000000000000000011010000000001000110000000000000001000100000011000000100110000010000010010010110000111000000000100010000111101100010010000000000100001000101100000010010101000000000000010010000000010010100000010000000110010000000000101000000000100100000110010001000001101010000011000000111010000000
## 1011000000000000000000000000000000110000000000010010000101000100100000010000000000100000000100001001100000000001100000000000000000001000100000001010000100000010000100000000000001000010100000000010000000100000000100000000000001100100000000000000000000000011000001010000000000001100000011001000001000000
## 0100000000000000000111010000001011000110000000000000000000100000011000000100100000010000010010000110000011000000000100000000110100100010010000000000100000100101100000100000101000000000000010010000000010000100000010000000010010000000100000000010000100110000000010001000011101010000010000000001010000000
## 0100000000000000000111010000001011000110000000000000000000100000011000000100100000010000010010000110000011000000000100000000110100100010010000000000100000100101100000101000101000000000000010010000000010000100000010000000010010000000100000000010000100110000000010001000011101010000010000000001010000000
## 1010000000000000000000000000000000110000000001001010000101000100100000010000000000100000000100001001000000000001100000000000000000001000100000001010010100000010000000000000000001000010100000000010000000100000000100000000000001100110000000000000000000000011000001010000000000001100000011001000001010000
## 1010000000000000000000000000000000110000000001001010000101000100100000010000000000100000000100001001000000000001100000000000000000001000100000001010010100000010000000000000000001000010100000000010000000100000000100000000000001100110000000000000000000000011000001010000000000001100000011001000001010000
## 0100001000000000010011010000000001000110000000000000000000100000011000000101101100010000010000000110001011100000001100100000110100100010010000000100100010000100100001000000101010110100000011010000000010000000100000000000010010000000010000000000100100100000000010001000001101010000010000000001010000100
## 0100001000000000010011010000000001000110000000000000000000100000011000000101101100010000010000000110001011100000001100100000110100100010010000000100100010000100100001000000101010110100000011010000000010000000100000000000010010000000010000000000100100100000000010001000001101010000010000000001010000100
## 0100000000000000000011010000000001000110000000000000001000100000011000000100100000010000010010000110000011000000000100000000111101100010010000000000100001000101100000010010101000000000000010010000000010000100000010000000110010000000000001000000001100100000110110001000001111010000011000100011010000000
## 0100000000000000000111010000001011000110000000000000000000100000011000000100100000010000010010000110000011000000000100000000110100100010010000000000100000000101100000100000101000000000000010010000000010000100000010000000010010000000100000000110000100110000000010001000011101010000010000000001010000000
## 0100000000000010010011010000000001000110000000000001000010110010011001000110100000010100011001000110010011000000000100001000110100100010010000000000100000001100100000000000101000000000000010110001000011000000000000000010010010001000000000000001010100100000000010001001001101010000010000000001010000010
## 0100000000000010010011010000000001000110000000000001000010110010011001000110100000010100011001000110010011000000000100001000110100100010010000000000100000001100100000000000101000000000000010110001000011000000000000000010010010001000000000000001010100100000000010001001001101010000010000000001010000010
## 0100000000000110011011010000000001000110000010000001000000111000011001000100100000010100011000000110000011000000000100000000110100100010010000000000100000000100100000000000101000000000000010010001000011000000000000000110010010001000000000100001010100100000000010001000001101010000010000000001010000010
## 0100000000000110011011010000000001000110000010000001000000111000011001000100100000010100011000000110000011000000000100000000110100100010010000000000100000000100100000000000101000000000000010010001000011000000000000000110010010001000000000100001010100100000000010001000001101010000010000000001010000010
## 1010000000000000000000100000000000110000000000000010000101000100100000010000000000100000000100001001000000000001100000000000000000001000100000001010010100000010000000000000000001000010100000000010000000100000000100000000000001100100000000000000000000000011000001010000000000001100000011001000001000000
## 1010000000000000000000100000000000110000000000000010000101000100100000010000000000100000000100001001000000000001100000000000000000001000100000001010010100000010000000000000000001000010100000000010000000100000000100000000000001100100000000000000000000000011000001010000000000001100000011001000001000000
## 0100000000000010010011010000000001000110000000000001000010110010011001000100100000010100011001000110000011000000000100001000110100100010010000000000100000000100100000000000101000000000000010010001000011000000000000000010010010001000000000000001010100100000000010001001001101010000010000000001010000010
## 0100000000000010010011010000000001000110000000000001000010110010011001000100100000010100011001000110000011000000000100001000110100100010010000000000100000000100100000000000101000000000000010010001000011000000000000000010010010001000000000000001010100100000000010001001001101010000010000000001010000010
## 0100000000010010010011010000000001000110000000000001000010110000011001000100100000010100011001000110000011000000000100001000110100100010010000000000100000000100100000000000101000000000000010010001000011000000000000000010011010001000000000000001010100100000000010001001001101010000010000000001010000010
## 0100000100000000000111010000000101000110000000000000000000100000011000000100100000010000010010000110000011000000000110000100110110100010010000000000100000010101100000000000101000000000000010010100000010000101000010000001010110000000001000000000000100100000000010001000001101010000010000000001010000000
## 0100010000001001000111010000001001000110000000000000000000100000011000000100100000011000010010000110000011000000000100000000110100100010010000000000100000000101100000000000101000000000000010010000000010000100001010000000010010000000100000000000000100100000000010001000001101010010110000000001010000000
## 1010000000000000000000000000000000110000010000000010000001000100100000010000000000100000000100001001000000000101110000000000000000001000100000000000000100000010000000000000000001000010100000000010000100100000000100000000000001100100000000000000000000000011001001010000000000101100000011011000001000000
## 0100000000000010011011010000000001000110000010000001000000111000011001000100100000010100011000000110000011001000000100000000110100100010010000000000100000000100100000000000101000000000000010010001000011000000000000000110010010001000000000100001010100100000000010001000001101010000010000000001010000010
## 1010000000000000000000000000000000110000000000000010000101000100100000010000000000100000000100001001000000000001100000000000000000001000100000001010010100000010000000000000000001000010100000000010000000100000000100000000000001100100000000000000000000000011000001010000000000001100000011001000001000000
## 0100000000000000000011010000000001000110000000000000001000100000011000000100110000010000010010010110000111000000000100010000111101100010010000000000100001000101100000010010101000000000000010010000000010010100000010000000110010000000000101000000000100100000110010001000001101010000011000000111010000000
## 0100001000000000010011010000000001000110000000000000000000100000011100000101101100010000010000000110001011100000001100000000110100100010010000000100100000000100100001000000101010110100000011010000000010000000100000000000010010000000010000000000100100100000000010001000001101010000010000000001010000100
## 0100000100000000000111010000000101000110000000000000000000100000011000000100100000010000010010000110000011000000000110000000110110100010010010000000100000000101100010000000101000001000000010010100000010000100000010000001010110000000000000000000000100100000000010001000001101010000010000000001010000000
## 0100000100000000000111010000000101000110000000000000000000100000011000000100100000010000010010000110000011000000000110000000110110100010010010000000100000000101100010000000101000001000000010010100000010000100000010000001010110000000000000000000000100100000000010001000001101010000010000000001010000000
## 0100000000000000000011010000100001000110000000000000001000100000011000000100100000010000010010000110000011000000000100000000111101100010010000000000100001000101100000010010101000000000001010010000000010000100000010000000110010000000000001000000001100100000110110001100001111010000011000100011010000000
## 0100000000000000000111010000001001000110000000000100000000100000011000001100100000010010010010000110000011000000000100000000110100100011010000000000100000000101100000000000101000000000000010010000000010000100000010000000010010000000100000000010000100100100000010001010101101010000010000000001010000000
## 0100010000001001000111010000001001000110000000000000000000100000011000000100100000011000010010000110000011000000000100000000110100100010010000000000100000000101100000000000101000000000000010010000000010000100001010000000010010010000100000000000000100100000000010001000001101010010110000000001010000000
## 0100000000000000000011010000000001000110000000000000001000100000011000000100110000010000010010010110000111000000000100010000111101100010010000000000100001000101100000010010101000000000000010010000000010010100000010000000110010000000000101000000000100100000110010001000001101010000011000000111010000000
## 0100000000000010010011010000000001000110000000000001000010110010011001000100100000010100011001000110010011000000000100001000110100100010010100000000100000000100100000000000101000000000000010110001000011000000000000000010010010001000000000000001010100100000000010001001001101010000010000000001010000010
## 0100000000000010010011010000000001000110000000000001000010110010011001000100100000010100011001000110010011000000000100001000110100100010010100000000100000000100100000000000101000000000000010110001000011000000000000000010010010001000000000000001010100100000000010001001001101010000010000000001010000010
## 0100000100000000000111010000000101000110000000000000000000100000011000000100100000010000010010000110000011000000000110000100110110100010010000000000100000000101100000000000101000000000000010010100000010000101000010000001010110000000001000000000000100100000000010001000001101010000010000000001010000000
## 0100000000000010010011010000000001000110000000000001000010110010011001000100100000010100011001000110000011000000000100001000110100100010010000000000100000000100100000000000101000000000000010010001000011000000010000000010010010001000000000000001010100100000000010001001001101010000010000000001010000010
## 1010000000000000000000100000000000110000000000000010000101000100100000010000000000100000000100001001000000000001100000000000000000001000100000001010010100000010000000000000000001000010100000000010000000101000000100000000000001100100000000000000000000000011000001010000000000001100000011001000001000000
## 1010000000000000000000100000000000110000000000000010000101000100100000010000000000100000000100001001000000000001100000000000000000001000100000001010010100000010000000000000000001000010100000000010000000101000000100000000000001100100000000000000000000000011000001010000000000001100000011001000001000000
## 0100000000000000000111010000001001000110000000000100000000100000011000001100100000010010010010000110000011000000000100000000110100100011010000000000100000000101100000000000101000000000000010010000000010000100000010000000010010000000100000000010000100100100000010001010101101010000010000000001010000000
## 0100000100000000000111010000000101000110000000000000000000100000011000000100100000010000010010000110000011000000000110000000110110100010010010000000100000000101100000000000101000001000000010010100000010000100000010000001010110000000000000000000000100100000000010001000001101010000010000000001010000000