mutationFrequencies(): return 0.0 rather than float(0)?

24 views
Skip to first unread message

Max Shpak

unread,
Jul 6, 2022, 6:22:49 PM7/6/22
to slim-discuss
I'm conditioning my simulations of a selective sweep on an allele not being lost, so in each generation I check if its frequency > 0 and otherwise restart with the following statement:
 
 m2mut = sim.mutationsOfType(m2);
 freqs = sim.mutationFrequencies(p3, m2mut);

 if(freqs==0){  <RESTART> }

If the allele is present, mtuationFrequencies returns a float-valued decimal and the conditional statement evaluates as false. However, if the allele is absent, then mutationFrequencies() returns float(0), which is empty, and thus cannot be assigned to the variable freqs nor evaluated in a conditional statement.

Is there some way to return 0.0 using mutationFrequencies() or some similar function rather than float(0) in this case?

Ben Haller

unread,
Jul 6, 2022, 6:38:20 PM7/6/22
to Max Shpak, slim-discuss
Hi Max.  The float(0) value *can* be assigned to freqs; there should be no difficulty with that.  As for testing for it, just use size(freqs) to test for a zero-length vector.  You could do:

if (size(freqs) == 0)
    freqs = 0.0;

to get the value into the form you want it in, for example, if you wish.  You could also just test for m2mut being zero-length, in the same way, if you prefer.

A more modern approach (but the above works fine too) would be to remember the sweep mutation itself, with defineGlobal(), and test its isSegregating and/or isFixed properties to find out what has happened to it; section 9.9 has an example of this technique, I think, which was made possible by changes in SLiM 3.5.  Happy modeling!

Cheers,
-B.

Benjamin C. Haller
Messer Lab
Cornell University


Max Shpak wrote on 7/6/22 3:22 PM:
--
SLiM forward genetic simulation: http://messerlab.org/slim/
---
You received this message because you are subscribed to the Google Groups "slim-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to slim-discuss...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/slim-discuss/5bc680b9-f151-49c9-9f45-d21b3e95481cn%40googlegroups.com.

Max Shpak

unread,
Jul 6, 2022, 6:40:04 PM7/6/22
to slim-discuss
Thanks. Is there some reason why mutationFrequencies() wasn't written to return 0.0 when the allele is lost?

Ben Haller

unread,
Jul 6, 2022, 6:47:52 PM7/6/22
to Max Shpak, slim-discuss
Hi Max.  When the allele has been lost, the mutation is no longer part of the simulation; it is removed upon loss (and upon fixation, if convertToSubstitution is T).  So the call to sim.mutationsOfType(m2) returns a vector of length 0; there are no mutations of type m2 in the registry, at that point.  When you call mutationFrequencies() with that vector of length 0, the correct return value is therefore a vector of length zero.  Returning 0.0 means "the mutation you asked about has a frequency of 0.0", whereas the return of float(0) says "you asked about the frequency of no mutations, so I have no frequencies to report".  Note that you can pass a vector containing more than one mutation to mutationFrequencies(), and then it returns a vector with the frequency of each mutation you asked about; so the length of the vector of frequencies it returns is always the same as the length of the vector of mutations you passed to it.  So when you pass it a zero-length vector, you get a zero-length vector back.  This is very standard in Eidos, and indeed in other vectorized languages.  A simple example in R:

> (1:5)+1
[1] 2 3 4 5 6

> (integer(0))+1
numeric(0)

The + operator (which is really a function I guess in R) returns a result equal in length to the vector you gave it to work with.  Many other examples could be adduced, of course.  :->


Cheers,
-B.

Benjamin C. Haller
Messer Lab
Cornell University


Max Shpak wrote on 7/6/22 3:40 PM:
Reply all
Reply to author
Forward
0 new messages