2020-01-30 17:19:10 +01:00
#' @title Simulate All Frequencies
#' @description Lisää jokaista alleelia joka populaation joka lokukseen j1/noalle(j) verran. Näin saatuja counts:eja vastaavista Dirichlet-jakaumista simuloidaan arvot populaatioiden alleelifrekvensseille.
#' Add each allele to each locus in each population by j 1 / noalle(j). The Dirichlet distributions corresponding to the counts thus obtained simulate values for the allele frequencies of the populations.
#' @param noalle noalle
2020-11-19 14:29:37 +01:00
simulateAllFreqs <- function ( noalle ) {
2024-04-10 16:04:07 +02:00
if ( isGlobalEmpty ( globals $ COUNTS ) ) {
2021-11-10 14:02:35 +01:00
max_noalle <- 0
nloci <- 0
npops <- 1
} else {
2024-04-10 16:04:07 +02:00
max_noalle <- size ( globals $ COUNTS , 1 )
nloci <- size ( globals $ COUNTS , 2 )
npops <- size ( globals $ COUNTS , 3 )
2021-11-10 14:02:35 +01:00
}
2020-01-30 17:19:10 +01:00
2021-11-10 14:02:35 +01:00
prioriAlleelit <- zeros ( max_noalle , nloci )
if ( nloci > 0 ) {
for ( j in 1 : nloci ) {
prioriAlleelit [1 : noalle [j ] , j ] <- 1 / noalle [j ]
2020-01-30 17:19:10 +01:00
}
2021-11-10 14:02:35 +01:00
}
prioriAlleelit <- repmat ( prioriAlleelit , matrix ( c ( 1 , 1 , npops ) , 1 ) )
counts <- ifelse (
2024-04-10 16:04:07 +02:00
test = isGlobalEmpty ( globals $ COUNTS ) ,
2021-11-10 14:02:35 +01:00
yes = prioriAlleelit ,
2024-04-10 16:04:07 +02:00
no = globals $ COUNTS + prioriAlleelit
2021-11-10 14:02:35 +01:00
)
allfreqs <- zeros ( size ( counts ) )
2020-01-30 17:19:10 +01:00
2021-11-10 14:02:35 +01:00
for ( i in 1 : npops ) {
if ( nloci > 0 ) {
for ( j in 1 : nloci ) {
simuloidut <- randdir ( counts [1 : noalle [j ] , j , i ] , noalle [j ] )
allfreqs [1 : noalle [j ] , j , i ] <- simuloidut
}
2020-01-30 17:19:10 +01:00
}
2021-11-10 14:02:35 +01:00
}
return ( allfreqs )
}