Skip to content

can't find the cause to CrosscheckTreeSeqIntegrity #71

Closed

Description

@mufernando

Hi,
I have been doing some simulations in which I stop and restart them from a tree sequence, and save a new one. This can go on for ~6 iterations.

In some cases, I have been getting a weird error, e.g.:
ERROR (SLiMSim::CrosscheckTreeSeqIntegrity): (internal error) genome/allele size mismatch at position 365: the treeseq has 1 mutation of mutid 12, SLiM has 0 segregating and 0 fixed mutation(s).

@petrelharp has looked into the tree sequences and said they seem fine. You can try and load those tree sequences to SLiM and it will throw that error.

I put them here: http://deep.uoregon.edu/~murillor/weird_trees/

Do you have any idea why that would be happening? I am happy to provide more info on the type of simulation,etc. @petrelharp tried to come up with a minimal example but we weren't successful.

Activity

  1. bhaller commented on Jan 30, 2020

    @bhaller
    Contributor

    Hi Murillo. Interesting. Can you provide the script that produces these tree sequences, too? It may be that something non-obvious is wrong with the tree sequence files themselves, or it may be that something is wrong with the way that SLiM loads them in and creates its internal state from them. It's a little bit surprising, though, since this basic idea (write out a tree sequence and then read it back in) has been massively stress-tested, so I think there must be something a bit unusual about your model that triggers the problem. Thanks.

  2. bhaller commented on Jan 30, 2020

    @bhaller
    Contributor

    And I need the script that you're using to read the .trees files back in; the context into which a .trees file is read matters, so I can't just try to load your .trees files into the default SLiMgui model. You can email them to me if you don't want to post them publicly here. Thanks.

  3. petrelharp commented on Jan 30, 2020

    @petrelharp
    Collaborator

    Here is a reload script that produces the error:

    initialize() {
        initializeTreeSeq();
        initializeMutationType("m1", 0.5, "f", 0.0);
        m1.convertToSubstitution = F;
        initializeGenomicElementType("g1", m1, 1.0);
        initializeGenomicElement(g1, 0, 1e4-1);
        initializeMutationRate(1e-7);
        initializeRecombinationRate(1e-8);
    }
    1 early() {
        sim.readFromPopulationFile("eastern-central_MKF28XM4UPZ1NE1_rep0.trees");
        catn("Number of individuals: " + p1.individualCount + " in generation " + sim.generation);
    }
    

    ... but I've just discovered that, on this computer, it produces the error only on the command line and not in the GUI. I'm currently tracking down what difference there might be between the executables.

  4. petrelharp commented on Jan 30, 2020

    @petrelharp
    Collaborator

    Both are version 3.3.1, at least - but perhaps different points in the git history.

  5. petrelharp commented on Jan 30, 2020

    @petrelharp
    Collaborator

    Confirmed that it happens on current HEAD on the command line...

  6. mufernando commented on Jan 30, 2020

    @mufernando
    Author

    @petrelharp built SLiM GUI from the current HEAD (3.3.1 build 2158) and it threw an error, whereas it didn't in the previous build which was a version 3.3.1 build 2125

  7. petrelharp commented on Jan 31, 2020

    @petrelharp
    Collaborator

    Sounds like time for git bisect...

  8. petrelharp commented on Jan 31, 2020

    @petrelharp
    Collaborator

    I'll have a go at it.

  9. petrelharp commented on Jan 31, 2020

    @petrelharp
    Collaborator

    I can't obtain a build that fails to throw the error on the command line, even building the git commit that looks to have been the one used to build the non-erroring GUI. Once in the office I'll see if rebuilding the GUI with various git commits fails to throw the error, but this lack-of-error-in-the-GUI-once is feeling like a red herring...

  10. petrelharp commented on Jan 31, 2020

    @petrelharp
    Collaborator

    printfs have got me this far through _InstantiateSLiMObjectsFromTables, and everything seems on track to be loading in the mutation which the error says in not in fact there in SLiM. I've no idea what could be leading to the mutation not ending up in the genome eventually.

  11. bhaller commented on Jan 31, 2020

    @bhaller
    Contributor

    I just ran the test model in SLiMgui and got the raise on my first try. I think the lack of raise in the GUI is a red herring, yes. I have noticed that catching and reporting raises in SLiMgui does not seem to be working reliably lately, for reasons I don't understand and have not yet been able to reproduce. But I think this bug occurs in the GUI as well as at the command line; it just maybe doesn't always get reported correctly by the GUI. So anyway, I see it happening on my machine, and will track it down. My guess is that it is a very long-standing bug; I don't think we've done anything any time recently that should have destabilized the loading of tree sequences. There's just something very special about these .trees files. @mufernando, please do post or email the model that produced them, if you can; seeing what it does might somehow help to diagnose why these .trees files are so special.

  12. bhaller commented on Jan 31, 2020

    @bhaller
    Contributor

    The data in the tree sequence appears to be corrupted in a way that the read code is not presently set up to catch, so it doesn't get caught until crosscheck. (Hooray for crosscheck!) Specifically, the mutation table appears to contain mutations (i.e., derived states) at multiple positions that reference the same mutation ID. This is not legal; a given mutation ID must refer uniquely to one mutation, which exists at one position. I added a log in __TabulateMutationsFromTables() that logs out each mutation that gets tabulated from the mutation table:

    std::cout << "__TabulateMutationsFromTables: tabulating derived state at position " << position << " with stack_count " << stack_count << ", id " << mut_id << std::endl;

    Among other output, it writes:

    __TabulateMutationsFromTables: tabulating derived state at position 365 with stack_count 1, id 12
    __TabulateMutationsFromTables: tabulating derived state at position 2678 with stack_count 1, id 12
    

    Since mutations go into a tabulation map that is indexed by mutation ID, the mutation at position 2678 overwrites the mutation at position 365 in the tabulation. Later, when mutations are getting added to genomes, this means that when the position-365 mutation ought to be added to a genome, the position-2678 mutation is added instead; looking up mutation ID 12 in the tabulation produces the position-2678 mutation due to the overwritten entry. The crosscheck then catches that discrepancy.

    So the .trees file itself is corrupted, and SLiM catches that and raises, so in that sense all is well. The question now is: how did it get that way? Now we really need to know exactly how this .trees file was produced. Either SLiM wrote out a corrupted .trees file (presumably after somehow, erroneously, re-using a mutation ID that was already in use), or the .trees file got corrupted downstream somehow by tskit/msprime/pyslim. Please supply the full SLiM model used to generate this .trees file, all necessary auxiliary files, a random number seed that reproducibly leads to a corrupted .trees file, and any post-run Python scripts that modify the .trees file. Thanks!

  13. petrelharp commented on Jan 31, 2020

    @petrelharp
    Collaborator

    the mutation table appears to contain mutations (i.e., derived states) at multiple positions that reference the same mutation ID.

    Oh, darn it, I vaguely noticed this and it seemed vaguely suspicious, but there were so many with the same ID that I didn't follow up on it...

    I can't think of what is special about these simulations, other than the fact that we're simulating, saving, reloading, changing size, and simulating more several times. But, it works most of the time... it seems that you really do need to track down the seed, Murillo? Or, maybe just having the tree sequences that happened earlier in the chain would be sufficient - maybe the error occurs when loading the previous tree sequence.

  14. petrelharp commented on Jan 31, 2020

    @petrelharp
    Collaborator

    Ah, right - there is no python modification (right, Murillo?) - it's all in SLiM.

  15. 3 remaining items

  16. petrelharp commented on Jan 31, 2020

    @petrelharp
    Collaborator

    So, there's actually a lot of duplicated mutation IDs at different positions - it's not just a single one, I think.

  17. bhaller commented on Jan 31, 2020

    @bhaller
    Contributor

    Yep, looks that way. Which is very odd, since this has not come up for any other test model at all.

  18. petrelharp commented on Jan 31, 2020

    @petrelharp
    Collaborator

    Yeah, we did a test where we saved and reloaded several times and this did not happen.

  19. bhaller commented on Jan 31, 2020

    @bhaller
    Contributor

    Yes, and I run those tests regularly, with different seeds, etc.

  20. mufernando commented on Feb 4, 2020

    @mufernando
    Author

    Ok, so I trimmed down my recipe and found a combination of seeds that can produce the problem - i.e., accumulation of mutations with the same ID at different positions.

    I noted that when I remove the treeSeqRememberIndividuals calls, I get no duplicated mutation IDs. This made me wonder that I'm blowing up the unique mutation IDs?

    I attached the slim recipe and a bash file with all the slim runs.
    crosscheck_error.zip

  21. petrelharp commented on Feb 4, 2020

    @petrelharp
    Collaborator

    Looks like the sim_test.slim in that bash script should be recipe_crosscheck.slim.

  22. petrelharp commented on Feb 4, 2020

    @petrelharp
    Collaborator

    So - sim6.trees is the first one that has duplicated mutation ID: mutation ID 362 appears twice, at positions 52 and 74. We are not running out of mutation IDs - the tree sequence has only 26 mutations. A possible clue is that the first instance of this mutation is recorded in the tree sequence as having appeared in the final generation (slim_generation 6001). This happened at least one of the other examples I've seen. Here's their information:

    {'id': 1, 'site': 1, 'node': 4798455, 'derived_state': '362', 'parent': -1, 'metadata': [MutationMetadata(mutation_type=1, selection_coeff=-1.1239014384045731e-05, population=1, slim_time=6001, nucleotide=-1)], 'position': 52.0, 'index': 1}
    {'id': 2, 'site': 2, 'node': 7414680, 'derived_state': '362', 'parent': -1, 'metadata': [MutationMetadata(mutation_type=1, selection_coeff=-5.203275961138121e-13, population=1, slim_time=2484, nucleotide=-1)], 'position': 74.0, 'index': 2}
    
  23. petrelharp commented on Feb 4, 2020

    @petrelharp
    Collaborator

    So - after sim_chain.sh has been run (with the fix above); to debug one can just run the second-to-last command (that produces sim6.trees); it is during this simulation that the bad thing occurs (duplicate mutation id recorded).

    And, in case it's helpful, here's the info about the genomes those mutations occurred in:

    >>> print(ts.node(4798455))
    {'id': 4798455, 'time': 0.0, 'population': 1, 'individual': 22536, 'metadata': NodeMetadata(slim_id=1501113073, is_null=0, genome_type=0), 'flags': 1}
    >>> print(ts.node(7414680))
    {'id': 7414680, 'time': 3506.0, 'population': 1, 'individual': -1, 'metadata': NodeMetadata(slim_id=624176082, is_null=0, genome_type=0), 'flags': 0}
    >>> print(ts.individual(22536))
    {'id': 22536, 'flags': 65536, 'location': array([0., 0., 0.]), 'metadata': IndividualMetadata(pedigree_id=750556536, age=-1, population=1, sex=-1, flags=0), 'nodes': array([4798454, 4798455], dtype=int32), 'population': 1, 'time': 0.0}
    
  24. bhaller commented on Feb 4, 2020

    @bhaller
    Contributor

    OK, I'll see whether I can get this reproducing on my end and track it down today.

  25. bhaller commented on Feb 4, 2020

    @bhaller
    Contributor

    Reproduced the bug on my end with this script, thanks. Will look into the next-to-last run.

  26. bhaller commented on Feb 4, 2020

    @bhaller
    Contributor

    Ha, figured it out. The bug is at line 7716 in slim_sim.cpp, @petrelharp. This is in SLiMSim::__CreateMutationsFromTabulation(); we're reading in mutations from the mutation table and creating Mutation objects in SLiM for them. If an existing mutation in the mutations table is not referenced by any genome (refcount == 0), we don't make a mutation object for it, which makes sense – as far as SLiM is concerned, it doesn't exist. BUT since it continues to exist in the mutation table, we need to be sure that we don't reuse its mutation ID, and at present this doesn't happen. So I think this bug requires specifically that a model (a) reads in a .trees file, (b) that .trees has a mutation that is not ancestral to any extant genome, and yet was retained (presumably by a rememberIndividuals() call), (c) that mutation has an ID that is higher than the highest mutation ID present in the .trees file for any mutation that does get instantiated, and (d) the model finishes by writing out a new .trees file. If all four conditions are true, the final .trees file can potentially have two mutations with the same ID (if the SLiM simulation ran long enough to re-use the previously used ID, and if that new mutation survives to the final generation or gets remembered).

    Yikes! Now that the bug is found it's a trivial fix. I'll post again when it's done and I've tested.

  27. bhaller commented on Feb 4, 2020

    @bhaller
    Contributor

    Hi @petrelharp and @mufernando. I've just committed the fix. The test script now runs the whole chain of simulations without any error. A pretty subtle bug; I doubt it will bite anyone else any time soon. :-> If you want the fix, just run with the current GitHub head on the master branch, it's there now. Thanks for the very helpful report; this would have been hard to figure out without a good reproducible case!

  28. mufernando commented on Feb 4, 2020

    @mufernando
    Author

    Thank you, @bhaller, for the speedy follow up!

  29. petrelharp commented on Feb 5, 2020

    @petrelharp
    Collaborator

    Damn. Good catch. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions