Recursively flatten a MultiPop-class object into a shallower
MultiPop containing only Pop-class objects
down to a requested level (see Details).
Arguments
- x
A
Pop-classorMultiPop-classobject.- level
Integer scalar >= 1. Number of
MultiPoplevels to preserve.- preserveNames
Character scalar controlling how to handle names when flattening. One of:
-"auto"(default): Keep names only if all are present and unique after flattening; otherwise drop them.
-"concatenate": Prefix child names with parent names using underscore separator. Keep names only if resulting names are unique.
-"force": Always keep concatenated names, making them unique viamake.uniqueif duplicates are found (with a warning).
-"none": Drop all names.
Value
If x is a Pop-class, the same x
object is returned. Otherwise a MultiPop-class is returned
whose x@pops slot contains Pop-class (and possibly
MultiPop-class) objects flattened according to level.
Details
The level argument controls how many levels of nesting are preserved.
- level = 1: flatten structure of MultiPop-class so that
all items are Pop-class objects (that is, flatten everything
below the top level).
- level > 1: preserve the top level levels of nesting; any
deeper MultiPop-class objects are flattened.
If level is greater than or equal to the nesting depth, the original
object is returned unchanged.
The preserveNames argument allows control over name preservation during
flattening. This is useful when you want to track the source of flattened
populations through their hierarchical names. The default "auto" mode
is conservative: names are kept only when they are meaningful (all present
and unique); missing names are not replaced with positional names. Use
"concatenate" to prefix names with their parent names, labeling
an element without a name by its position and replacing duplicated
names within a child by positions, "force" to guarantee names
with uniqueness enforcement, or
"none" to explicitly discard all names.
Examples
# Create founder haplotypes
founderPop = quickHaplo(nInd=12, nChr=1, segSites=10)
# Set simulation parameters
SP = SimParam$new(founderPop)
# Create population
pop = newPop(founderPop, simParam=SP)
# Create a multi-population with down to level 3 nesting
mp_nested = newMultiPop(pop1 = pop[1:2],
mp1 = newMultiPop(pop2 = pop[3:4],
mp2 = newMultiPop(pop3 = pop[5:7], pop4 = pop[8:12])))
mp_nested
#> An object of class "MultiPop" with 2 item(s)
#> Level 1:
#> |-- [[1]] "pop1" - An object of class "Pop" with 2 individual(s): 1, 2
#> `-- [[2]] "mp1" - An object of class "MultiPop" with 2 item(s)
#> | Level 2:
#> |-- [[1]] "pop2" - An object of class "Pop" with 2 individual(s): 3, 4
#> `-- [[2]] "mp2" - An object of class "MultiPop" with 2 item(s)
#> | Level 3:
#> |-- [[1]] "pop3" - An object of class "Pop" with 3 individual(s): 5, 6, 7
#> `-- [[2]] "pop4" - An object of class "Pop" with 5 individual(s): 8, 9, ..., 12
# Completely flatten to a single top-level MultiPop
# With default "auto" mode: names are kept if unique
flattenMultiPop(mp_nested)
#> An object of class "MultiPop" with 4 item(s)
#> Level 1:
#> |-- [[1]] "pop1" - An object of class "Pop" with 2 individual(s): 1, 2
#> |-- [[2]] "pop2" - An object of class "Pop" with 2 individual(s): 3, 4
#> |-- [[3]] "pop3" - An object of class "Pop" with 3 individual(s): 5, 6, 7
#> `-- [[4]] "pop4" - An object of class "Pop" with 5 individual(s): 8, 9, ..., 12
# With "concatenate": build hierarchical names
flattenMultiPop(mp_nested, preserveNames = "concatenate")
#> An object of class "MultiPop" with 4 item(s)
#> Level 1:
#> |-- [[1]] "pop1" - An object of class "Pop" with 2 individual(s): 1, 2
#> |-- [[2]] "mp1_pop2" - An object of class "Pop" with 2 individual(s): 3, 4
#> |-- [[3]] "mp1_mp2_pop3" - An object of class "Pop" with 3 individual(s): 5, 6, 7
#> `-- [[4]] "mp1_mp2_pop4" - An object of class "Pop" with 5 individual(s): 8, 9, ..., 12
# With "force": guarantee unique names with suffixes
# (not needed here but useful if duplicates were present)
flattenMultiPop(mp_nested, preserveNames = "force")
#> An object of class "MultiPop" with 4 item(s)
#> Level 1:
#> |-- [[1]] "pop1" - An object of class "Pop" with 2 individual(s): 1, 2
#> |-- [[2]] "mp1_pop2" - An object of class "Pop" with 2 individual(s): 3, 4
#> |-- [[3]] "mp1_mp2_pop3" - An object of class "Pop" with 3 individual(s): 5, 6, 7
#> `-- [[4]] "mp1_mp2_pop4" - An object of class "Pop" with 5 individual(s): 8, 9, ..., 12
# With "none": drop all names
flattenMultiPop(mp_nested, preserveNames = "none")
#> 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 2 individual(s): 3, 4
#> |-- [[3]] An object of class "Pop" with 3 individual(s): 5, 6, 7
#> `-- [[4]] An object of class "Pop" with 5 individual(s): 8, 9, ..., 12
# Preserve two levels of nesting (level 2 names are kept)
flattenMultiPop(mp_nested, level = 2)
#> An object of class "MultiPop" with 2 item(s)
#> Level 1:
#> |-- [[1]] "pop1" - An object of class "Pop" with 2 individual(s): 1, 2
#> `-- [[2]] "mp1" - An object of class "MultiPop" with 3 item(s)
#> | Level 2:
#> |-- [[1]] "pop2" - An object of class "Pop" with 2 individual(s): 3, 4
#> |-- [[2]] "pop3" - An object of class "Pop" with 3 individual(s): 5, 6, 7
#> `-- [[3]] "pop4" - An object of class "Pop" with 5 individual(s): 8, 9, ..., 12