Skip to contents

Merge one or more Pop-class and MultiPop-class objects. Because a MultiPop can have a nested structure merging is controlled by the level argument (see Details).

Usage

mergeMultiPops(..., level = 0)

Arguments

...

Pop-class or MultiPop-class objects; NULL values are ignored.

level

Integer scalar >= 0 to merge at a specific level of nesting; see Details.

Value

If level == 0, or when merging yields a single Pop, a Pop-class object is returned. Otherwise a MultiPop-class object is returned with the requested level of nesting preserved.

Details

The function accepts multiple inputs and merges them according to the level argument.
- level = 0: merge all Pop or MultiPop objects in the inputs into a single Pop object (the inputs are first completely flattened then merged).
- level = 1: merge inputs into a MultiPop so that each item is a Pop (level 1). Each input is first flattened to level 1 and its Pop objects merged into one Pop, and then all these Pop objects are merged into a single MultiPop.
- level > 1: merge inputs into a MultiPop while preserving top level structure. Each input is first flattened to the requested level and its items merged into a single MultiPop, and then inputs are merged.

Important behavioral notes: If a single Pop is provided, it is returned unchanged. If a single MultiPop is provided, it is processed according to level. If multiple objects are provided, they are combined into a MultiPop (preserving the order of inputs) and then processed according to level. If neither of the multiple inputs is a MultiPop, an error is raised. An error is also raised if inputs are not a Pop or a MultiPop. NULL inputs are ignored.

Examples

# Create founder haplotypes
founderPop = quickHaplo(nInd=11, nChr=1, segSites=10)

# Set simulation parameters
SP = SimParam$new(founderPop)

# Create population
pop = newPop(founderPop, simParam=SP)

# Create two multi-population with different levels of nesting
mp1 = newMultiPop(pop[1:2], pop[3:5])
mp1
#> An object of class "MultiPop" with 2 item(s)
#>     Level 1:
#> |-- [[1]] An object of class "Pop" with 2 individual(s): 1, 2
#> `-- [[2]] An object of class "Pop" with 3 individual(s): 3, 4, 5
mp2 = newMultiPop(pop[6:7],
                  newMultiPop(pop[8:10], pop[11]))
mp2
#> An object of class "MultiPop" with 2 item(s)
#>     Level 1:
#> |-- [[1]] An object of class "Pop" with 2 individual(s): 6, 7
#> `-- [[2]] An object of class "MultiPop" with 2 item(s)
#>       |   Level 2:
#>       |-- [[1]] An object of class "Pop" with 3 individual(s): 8, 9, 10
#>       `-- [[2]] An object of class "Pop" with 1 individual(s): 11

# Fully merge all Pops in inputs into one Pop
mergeMultiPops(mp1[[1]]) # nothing happens with a single Pop
#> An object of class "Pop" 
#> Ploidy: 2 
#> Individuals: 2 
#> Chromosomes: 1 
#> Loci: 10 
#> Traits: 0 
mergeMultiPops(mp1)
#> An object of class "Pop" 
#> Ploidy: 2 
#> Individuals: 5 
#> Chromosomes: 1 
#> Loci: 10 
#> Traits: 0 
mergeMultiPops(mp1, mp2)
#> An object of class "Pop" 
#> Ploidy: 2 
#> Individuals: 11 
#> Chromosomes: 1 
#> Loci: 10 
#> Traits: 0 

# Merge into a MultiPop where each level 1 item is a Pop
mergeMultiPops(mp1[[1]], level=1) # nothing happens with a single Pop
#> An object of class "Pop" 
#> Ploidy: 2 
#> Individuals: 2 
#> Chromosomes: 1 
#> Loci: 10 
#> Traits: 0 
mergeMultiPops(mp1, level=1)
#> An object of class "MultiPop" with 2 item(s)
#>     Level 1:
#> |-- [[1]] An object of class "Pop" with 2 individual(s): 1, 2
#> `-- [[2]] An object of class "Pop" with 3 individual(s): 3, 4, 5
mergeMultiPops(mp1, mp2[[1]], level=1)
#> An object of class "MultiPop" with 3 item(s)
#>     Level 1:
#> |-- [[1]] An object of class "Pop" with 2 individual(s): 1, 2
#> |-- [[2]] An object of class "Pop" with 3 individual(s): 3, 4, 5
#> `-- [[3]] An object of class "Pop" with 2 individual(s): 6, 7
mergeMultiPops(mp2, level=1)
#> An object of class "MultiPop" with 2 item(s)
#>     Level 1:
#> |-- [[1]] An object of class "Pop" with 2 individual(s): 6, 7
#> `-- [[2]] An object of class "Pop" with 4 individual(s): 8, 9, ..., 11
mergeMultiPops(mp1, mp2, level=1)
#> An object of class "MultiPop" with 4 item(s)
#>     Level 1:
#> |-- [[1]] An object of class "Pop" with 2 individual(s): 1, 2
#> |-- [[2]] An object of class "Pop" with 3 individual(s): 3, 4, 5
#> |-- [[3]] An object of class "Pop" with 2 individual(s): 6, 7
#> `-- [[4]] An object of class "Pop" with 4 individual(s): 8, 9, ..., 11

# Merge into a MultiPop and preserve level 1 and 2 structure
mergeMultiPops(mp2, level=2)
#> An object of class "MultiPop" with 2 item(s)
#>     Level 1:
#> |-- [[1]] An object of class "Pop" with 2 individual(s): 6, 7
#> `-- [[2]] An object of class "MultiPop" with 2 item(s)
#>       |   Level 2:
#>       |-- [[1]] An object of class "Pop" with 3 individual(s): 8, 9, 10
#>       `-- [[2]] An object of class "Pop" with 1 individual(s): 11
mergeMultiPops(mp2, mp1, level=2)
#> An object of class "MultiPop" with 4 item(s)
#>     Level 1:
#> |-- [[1]] An object of class "Pop" with 2 individual(s): 6, 7
#> |-- [[2]] An object of class "MultiPop" with 2 item(s)
#> |     |   Level 2:
#> |     |-- [[1]] An object of class "Pop" with 3 individual(s): 8, 9, 10
#> |     `-- [[2]] An object of class "Pop" with 1 individual(s): 11
#> |-- [[3]] An object of class "Pop" with 2 individual(s): 1, 2
#> `-- [[4]] An object of class "Pop" with 3 individual(s): 3, 4, 5
mergeMultiPops(mp2, mp1[[1]], level=2)
#> An object of class "MultiPop" with 3 item(s)
#>     Level 1:
#> |-- [[1]] An object of class "Pop" with 2 individual(s): 6, 7
#> |-- [[2]] An object of class "MultiPop" with 2 item(s)
#> |     |   Level 2:
#> |     |-- [[1]] An object of class "Pop" with 3 individual(s): 8, 9, 10
#> |     `-- [[2]] An object of class "Pop" with 1 individual(s): 11
#> `-- [[3]] An object of class "Pop" with 2 individual(s): 1, 2