bodkan / slendr

Population genetic simulations in R 🌍
https://bodkan.net/slendr
Other
54 stars 5 forks source link

Incorrect error messages in `gene_flow()` caused by time errors #132

Closed IsabelMarleen closed 1 year ago

IsabelMarleen commented 1 year ago

There is an issue with the error messages in the gene_flow() function for time inconsistencies. I found a minimal example where an error caused by an inconsistency with the assumed direction of time causes an error about the populations not being present in the time window and a second scenario where the opposite happens. This seems like an issue that could be caused by a condition being the wrong way around.

# The model
anc <- population("ancestor", time = 9e6, remove=8e6, N=100)
popA <- population("A", time = 8e6, parent=anc, N=100)
popB <- population("B", time = 7e6, parent=anc, N=100)

gf <- gene_flow(from = popA, to = popB, start = 4e6, end = (4e6+5), rate = 0.3) This throws the error:

Both A and B must be present within the gene-flow window 4e+06-4000005

but it should complain that the end time is before the start time.

gf <- gene_flow(from = popA, to = popB, start = 8e6, end = (8e6-5), rate = 0.3) This throws the error:

Specified times are not consistent with the assumed direction of time (gene flow A -> B in the time window 8e+06-7999995)

but it should complain that population B does not exist yet in this time window.

bodkan commented 1 year ago

Thanks for the report.

Hmm, I am sort of aware of this issue. I have poked around this a bit and discovered some weird edge cases in which the gene_flow() function does not have a complete information about the intended flow of time... which doesn't appear to be the case here in your minimal example (thanks for providing this, this is really great).

I think it had something to do with one population existing within the gene-flow time window, but the other existing outside (either before it or after it)? I'll have to check my notes.

The easiest thing we could do for now is simply write a generic error about a gene flow being somehow broken. Less satisfying and less informative, but at least not misleading.

bodkan commented 1 year ago

Fun fact: I just removed some code and gene_flow() behaves like you (correctly) identified @IsabelMarleen.

image

... too good to be true on Friday afternoon.

bodkan commented 1 year ago

Fun fact: I just removed some code and gene_flow() behaves like you (correctly) identified @IsabelMarleen […] ... too good to be true on Friday afternoon.

Huh, it seems it was that after all. An old time consistency check which originated when slendr could only do spatial SLiM models was triggered in situations were another error message was more appropriate.

All tests pass as they should, so fingers crossed.