Skip to content

Issues with ska distance for large data sets #97

Closed

Description

@vesa00

Hello everyone,
thank you very much for the very helpful tool ska2.

However, I have the following problem:
When I apply ska distance to 5 closely related assemblies I get distances from 1-7, which seems reasonable in this context. However, if I apply the exact same command to a large data set of 800 assemblies in .fasta format, I get excessive distances of >30000, even for the isolate pairs that were previously compared in the small approach. This means that the distances do not even match.

Has anyone observed something similar and has a solution?

Many thanks in advance.

Activity

  1. rderelle commented on Jun 1, 2025

    @rderelle
    Collaborator

    Hi,

    Which SKA2 version and what k-mer size are you using?

    It shouldn't happen with v0.3.11, or v4.0, and a k-mer size of 21 or larger.
    See this previous raised issue #92

    Romain

  2. vedomics commented on Jul 11, 2025

    @vedomics

    Hello,

    Huge SKA / SKA2 fan!

    Recently, I've had this exact same issue, running SKA2 v 0.40 with kmer sizes upwards of 21 (mostly 31). The output SNP distance appears to scale with the number of genomes included in the skf build when ska distance is run at low min_freq values. Setting min_freq to 0.9 has resulted in distances that are much more similar to SKA1 and whole-genome SNP distances from reference-based alignment (Snippy), though it would be great to get some clarification on why this is happening so I can feel confident in my min_freq threshold selection.

    Many thanks!

    Veda

  3. rderelle commented on Jul 14, 2025

    @rderelle
    Collaborator

    Hi Veda,

    the min_freq threshold defines the proportion of samples in which a split k-mer has to be present to call a SNP. It's not surprising that the number of detected SNPs scales more or less with the number of genomes using low min_freq values, for two reasons:

    • you also detect SNPs in the accessory genome (ie, true SNPs)
    • you also infer false positive SNPs due to repeat split k-mers. As an example, let's consider a split kmers present in several copies in each genomes with middle-bases A and G. In some cases, due to coverage variations, only the split k-mer with middle-base 'A' will be present in the skf file for genome 1 and only the split k-mer with middle-base 'G' will be present for genome 2, thereby creating a SNP between genomes 1 and 2. If you are using the command --filter-ambig-as-missing when calling SNPs, these false positive SNPs belong to split k-mer with low frequencies and are discarded thanks to the high min_freq threshold (this filtering is similar to the removal of SNP columns containing a lot of 'N' in reference-based alignment).

    For these reasons, it is recommended to use the default min_freq = 0.9, or slightly lower (eg, 0.8) to focus on true SNPs in the core-genome. Hope this helps.

    Romain

  4. vedomics commented on Jul 14, 2025

    @vedomics

    Hi Romain,

    Thanks for the response and for your super helpful explanation! I'd like to clarify a few more things, to ensure I'm doing my analysis as expected:

    1. The number of SNPs detected using ska distance and default parameters (ie no specified value for min_freq) is currently higher than SNPs detected with values of min_freq > 0.1 for k=31 -- am I misunderstanding the default setting?

    2. What parameters would you recommend to capture a maximal number of 'true' SNPs across the whole genome between two samples while building a very large skf file from thousands of assemblies? An issue I keep running into is that when two genomes are run through the ska build -> ska distance commands (a "pairwise analysis"), the calculated distance is as expected (fewer than 10 SNPs), but that number increases to 1000s with the inclusion of more genomes (an "all-by-all" analysis). I'd like to retain the ability to scan for SNPs across the entire genome (so, not just kmers from core genome regions present in >90% of samples) without incurring too many false positives, which must derive from a mixture of the two scenarios you describe. Perhaps I didn't understand your explanation fully (my apologies!) but I am not entirely sure how the reported SNP distances between a pairwise vs all-by-all analysis can differ so dramatically with nearly identical genomes (there must be a large influx of false positives that do not represent true SNPs in the accessory genome). Does the number of ambiguous calls increase with increasing sample size?

    3. The version of SKA2 I'm running (v0.40) does not seem to have a "--filter-ambig-as-missing" option for ska distance or ska build -- am I working with the wrong version?

    Thank you so much for your help and patience!

    Veda

  5. rderelle commented on Jul 17, 2025

    @rderelle
    Collaborator
    1. My mistake, sorry. I was referring to ska align not ska distance, which I never use. Indeed there is no --filter-ambig-as-missing option in ska distance and its default min_freq value is 0.

    I'll do some test and come back to you later.

  6. vedomics commented on Jul 17, 2025

    @vedomics

    Thanks so much! I just want to point out that the SKA2 documentation currently states that the default min_freq value is 0.9 ("Consider ambiguous bases by adding --allow-ambiguous flag, and change --min-freq to ignore more/less k-mers only found in some samples (default = 0.9).") which may be adding to the confusion.

    V

  7. rderelle commented on Jul 22, 2025

    @rderelle
    Collaborator

    I did some tests using Mtb isolates and k=31 (ska v0.4.0).

    When only 2 isolates A and B separated by 4 SNPs (confirmed by ska align without ambiguous bases) are analysed, the distance is correctly inferred as 4 at m=0 and m=0.9.

    However, when these 2 isolates are analysed together with 54 other isolates of the same lineage (distances up to 300 SNPs), the distance between A and B is 128 at m=0 and 6 at m=0.9.

    @johnlees I suggest changing the default value of the minimum frequency from 0 to 0.9 (line 288 of cli.rs):
    #[arg(short, long, value_parser = zero_to_one, default_value_t = 0.0)]
    into
    #[arg(short, long, value_parser = zero_to_one, default_value_t = DEFAULT_MINFREQ)]

    I will have a closer look at the difference between 4 and 6 SNPs observed at m=0.9 as this shouldn't happen (?).
    nb: also observed in #92 with 11 vs 13 SNPs.

    @vedomics 2) yes, that is essentially correct. I suggest using m=0.9 in your analyses.

  8. rderelle commented on Jul 22, 2025

    @rderelle
    Collaborator

    I might have found a solution: to restrict the distance calculation to ATGC middle-bases only.

    Here I defined a HashSet 'atgc' to filter var1 and var2 before detecting differences (file merge_ska_array.rs):

        // Distance between two samples (columns of the array)
        fn variant_dist(
            sample1: &ArrayView<u8, Dim<[usize; 1]>>,
            sample2: &ArrayView<u8, Dim<[usize; 1]>>,
            constant: f64,
        ) -> (f64, f64) {
            //  ACGT vs different ACGT -> +1
            //  Ambig bases are converted to prob vectors and multiplied
            //  '-' vs anything counts as a mismatch
            let atgc: HashSet<u8> = [b'A', b'C', b'G', b'T'].iter().cloned().collect();
            let mut distance = 0.0;
            let mut mismatches = 0.0;
            let mut matches = constant;
            for (var1, var2) in sample1.iter().zip(sample2) {
                if *var1 == b'-' || *var2 == b'-' {
                    if !(*var1 == b'-' && *var2 == b'-') {
                        mismatches += 1.0;
                    }
                } else {
                    matches += 1.0;
                    if atgc.contains(var1) && atgc.contains(var2) {
                        let var1_p = base_to_prob(*var1);
                        let var2_p = base_to_prob(*var2);
                        let overlap: f64 = var1_p.iter().zip(var2_p).map(|(p1, p2)| *p1 * p2).sum();
                        distance += 1.0 - overlap;
                    }
                }
            }
            mismatches = if (matches + mismatches) == 0.0 {
                0.0
            } else {
                mismatches / (matches + mismatches)
            };
            (distance, mismatches)
        }
    

    Using this modified version of ska 0.4.0, I now obtain a stable distance of 4 SNPs between A and B (see earlier post) in all cases (m=0, m=0.9, 2 isolates and 56 isolates). @johnlees This seems to indicate that the rule "ACGT vs different ACGT -> +1" was not properly enforced, no?

    I could share the skf files and modified ska code if needed.

  9. rderelle commented on Jul 22, 2025

    @rderelle
    Collaborator

    Not important, but ska distance uses 2 CPUs by default or with --threads 1, and does not seem to make use of additional CPUs. For instance, using the 56 Mtb isolates skf file (k=31) on a Mac M4:

    ska distance out_k31_4.3.3__56_bis.skf 11.35s user 0.14s system 204% cpu 5.613 total
    ska distance --threads 1 out_k31_4.3.3__56_bis.skf 11.31s user 0.13s system 203% cpu 5.615 total
    ska distance --threads 2 out_k31_4.3.3__56_bis.skf 9.94s user 0.13s system 133% cpu 7.547 total
    ska distance --threads 3 out_k31_4.3.3__56_bis.skf 10.48s user 0.13s system 153% cpu 6.915 total
    ska distance --threads 4 out_k31_4.3.3__56_bis.skf 10.38s user 0.11s system 163% cpu 6.417 total
    ska distance --threads 6 out_k31_4.3.3__56_bis.skf 10.48s user 0.12s system 176% cpu 6.002 total

    Edit:
    ska align is a bit different: 1 CPU used in all cases (by default and regardless --threads argument).

  10. johnlees commented on Jul 22, 2025

    @johnlees
    Member

    Summarising the above, we should:

    • Change the min-freq default (and also check this is correct in the docs)
    • Check that distances ignoring ambiguous bases are handled correctly. I would implement this a bit differently than above I think but seems like the right overall idea. And add a unit test for this.
    • Review the options that take a threads argument. I think distance and align are likely using multiple threads but just doing so very inefficiently. (I can't see how align would be parallelised; distance I recall adding but never benchmarked it. I think ~50 samples would be too small to benefit, it would probably need 1k-10k which maybe is just not realistic for ska)

    Does that look right to you @rderelle? I can make a PR later this week

  11. rderelle commented on Jul 22, 2025

    @rderelle
    Collaborator

    @johnlees

    • priority: "Check that distances ignoring ambiguous bases are handled correctly. I would implement this a bit differently than above I think but seems like the right overall idea. And add a unit test for this."
    • if first modification successful, no need to modify the default min-freq value as I obtained correct distances at both m=0 and m=0.9 with my quick fix (but in such case we need to modify the doc that mentions m=0.9)
    • review mullti-threading handling for ska distance; perhaps simply remove the --thread argument for ska align if too difficult (it is already ultra-fast anyway)
  12. rderelle commented on Jul 22, 2025

    @rderelle
    Collaborator

    Happy to test the PR with my skf file.
    Perhaps @vedomics could test it as well with her own dataset.

  13. self-assigned this
    on Jul 23, 2025
  14. johnlees commented on Jul 23, 2025

    @johnlees
    Member

    @rderelle working on this now. Could you share your skf file please? With the current tests I am not seeing the same issue, so must be something in the filtering I am missing

  15. johnlees commented on Jul 23, 2025

    @johnlees
    Member

    Sorry scratch that, this issue is in the tests!

  16. rderelle commented on Jul 24, 2025

    @rderelle
    Collaborator

    @vedomics please use version 0.4.1 that was released this morning. Using this version, you won't need to modify the min-freq threshold and can keep the default value m=0, which will allow you to detect SNPs in the accessory genome as well.

    Any feedback would be highly appreciated. 👍

  17. vedomics commented on Jul 24, 2025

    @vedomics

    Thank you so much for this latest update! I can confirm that the latest version works as expected on default settings on a small subset of genomes with in silico mutations -- will update with a full comparison on a larger set of genomes + comparisons to SKA1 and snippy. Thanks again!

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions