bodkan / slendr

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

Question about Sampling Process #81

Closed jaurbanChicago closed 2 years ago

jaurbanChicago commented 2 years ago

Hello Martin,

I have a question about the slendr sampling process. I am interested in sampling individuals of three different time slices from within single locations in Europe. I am attaching a map to show you what I would like to do: Europe_Sampling_Places

As you can see, I want individuals to come from a single location (simulating a small town/village across time). However, the individuals sampled for your tutorial vignettes are scattered across the map:

map_Europe_Samplings_tutorial

I guess the model would need to be changed maybe??? I used the model shown in this tutorial https://www.slendr.net/articles/vignette-06-locations.html#scheduling-sampling-events-and-simulation.

Thank you so much in advance.

Best,

Toño

bodkan commented 2 years ago

Hello again Toño,

First of all, I have to say I'm beyond excited you're already simulating real spatial data! I can't wait to see what you guys are doing and what kinds of projects in general will slendr allow people to do. I'm just about done with the manuscript dedicated to the software and this is a great motivation to push forward.

To your question: I have recently implemented a feature which should hopefully help you do exactly this.

Take a look at the manual page of the sampling() function. You will note there is an optional argument locations = which allows you to specify where should given number of individuals from given populations be sampled.

(Un)fortunately, the stream of new features over the last 3-4 months has been too significant and things have been moving too quickly to update all vignettes to showcase the usage of all of them.

That said, maybe this bit of code in slendr's unit tests will help? In particular, in the linked unit test script, you will note that the locations = argument can be of the form such as

sampling(model, times = ..., locations = list(c(10, 15), c(0, 30), list(popA, 5), list(popB, 5), ...)

etc, indicating that 5 individuals from popA and popB should be sampled at given time points as close to locations [10, 15] and [0, 30] (degrees lon/lat, for instance) as possible.

Hope this helps!

bodkan commented 2 years ago

Closing this for now. Feel free to re-open and comment if things behave weirdly!

I'm not 100% happy with the interface of sampling() and might make some improvements and changes in the near future.

One thing I should mention is that you don't have to specify the whole sampling schedule in a single sampling() call. This function returns a data frame, so you can combine many different results of sampling() calls (each for a specific population, a time point, or a spatial location) and just combine them together by something like:

samples <- dplyr::bind_rows(
  sampling(model, times = ..., locations = ..., list(popA, nA), ...),
  sampling(model, times = ..., locations = ..., list(popB, nB), ...),
  sampling(model, ...)
)

and then just provide the new samples data frame to the slim(..., sampling = samples) call.

jaurbanChicago commented 2 years ago

Thank you so much for your very detailed answers Martin!!! I will download the newest version of slendr and get right into it!!! Thanks a lot!!! Best,

Toño

jaurbanChicago commented 2 years ago

Hello again Martin,

Is there a problem with the downloading process of the slendr package? I tried to download the newest version and this error prompt came out:

> devtools::install_github("bodkan/slendr") Downloading GitHub repo bodkan/slendr@HEAD Error in utils::download.file(url, path, method = method, quiet = quiet, : download from 'https://api.github.com/repos/bodkan/slendr/tarball/HEAD' failed

jaurbanChicago commented 2 years ago
The downloaded binary packages are in
    /var/folders/v0/h1l93y5d56ndrcl7bg26gysc0000gn/T//RtmpITV464/downloaded_packages
✓  checking for file ‘/private/var/folders/v0/h1l93y5d56ndrcl7bg26gysc0000gn/T/RtmpITV464/remotesac6781648bc/bodkan-slendr-a99e00c/DESCRIPTION’ (439ms)
─  preparing ‘slendr’: (395ms)
✓  checking DESCRIPTION meta-information ...
─  checking for LF line-endings in source and make files and shell scripts
─  checking for empty or unneeded directories
─  building ‘slendr_0.9.tar.gz’

* installing *source* package ‘slendr’ ...
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
Warning in utils::compareVersion(slim_version, required_version) :
  NAs introduced by coercion
Error: package or namespace load failed for ‘slendr’:
 .onAttach failed in attachNamespace() for 'slendr', details:
  call: if (a[k] > b[k]) return(1) else if (a[k] < b[k]) return(-1L)
  error: missing value where TRUE/FALSE needed
Error: loading failed
Execution halted
ERROR: loading failed
* removing ‘/Library/Frameworks/R.framework/Versions/4.1/Resources/library/slendr’
Warning message:
In i.p(...) :
  installation of package ‘/var/folders/v0/h1l93y5d56ndrcl7bg26gysc0000gn/T//RtmpITV464/fileac6936acb8/slendr_0.9.tar.gz’ had non-zero exit status
bodkan commented 2 years ago

Hello.

The first error looks like an internet problem with a connection to Github. However, it seems that's been resolved now?

As for the second error. I'm investigating now. I will get back to you in a minute... I think I know what's wrong (this comes from a new check for the required SLiM version).

jaurbanChicago commented 2 years ago

Yea, the first one was resolved, but now the second one appeared. Thanks a lot Martin!

bodkan commented 2 years ago

Can you check devtools::install_github("bodkan/slendr") now again?

What happened: I have noticed that SLiM 3.7 had some unpleasant bug which corrupts simulation output. See here for more detail:

IMPORTANT BUG FIXES:

  • Fixed a semi-reproducible crash when treeSeqOutput() is called. In cases where the crash did not occur, it is possible that data corruption occurred and output is incorrect. Users who used tree-sequence recording in SLiM 3.7 should therefore re-run their models just to be safe. This bug did not exist in previous SLiM versions.

This was fixed in SLiM 3.7.1 which was released a couple of days ago (I recommend you download the update to avoid problems).

To make sure that the users will not have these problems, I implemented a check in slendr which makes sure they are running at least SLiM 3.7.1 and if not, the package will inform that a SLiM upgrade is recommended... but the check didn't work properly, leading to the error you've been getting.

Should be fixed now I hope.

jaurbanChicago commented 2 years ago

Ok, I´ll first download the SLiM 3.7.1 and then I will download slendr again! I´ll tell you what happens!

jaurbanChicago commented 2 years ago

I already downloaded the new SLiM 3.7.1 and now this prompt appears:

> devtools::install_github("bodkan/slendr")
Downloading GitHub repo bodkan/slendr@HEAD
Error in utils::download.file(url, path, method = method, quiet = quiet,  : 
  download from 'https://api.github.com/repos/bodkan/slendr/tarball/HEAD' failed
bodkan commented 2 years ago

I'm afraid this is an internet connection problem. Either on your part or an issue with Github connection. Not a slendr issue.

For what its worth, the command works on my machine which suggests that Github connection should be OK.

Are you behind some proxy connection at a university network or something? I'm afraid there's nothing I can do on my end. Maybe switching wifi or using another internet connection would help?

Either way, it's the same error you reported first above, so perhaps it's an intermittent connection problem which will resolve again by itself.

jaurbanChicago commented 2 years ago

Yes, I think that is the problem. My internet connection has been unstable today. I´ll wait until it gets better. Thanks again Martin!

bodkan commented 2 years ago

Good luck! 👍