Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add decoding variants example C code. #2438

Open
jeromekelleher opened this issue Jul 25, 2022 · 0 comments
Open

Add decoding variants example C code. #2438

jeromekelleher opened this issue Jul 25, 2022 · 0 comments
Labels
C API Issue is about the C API documentation Documentation
Milestone

Comments

@jeromekelleher
Copy link
Member

We currently don't have any simple C example code for decoding variants. This is useful both for C API users and ourselves for timing/bug tracking purposes. This is roughly what we want:

#include <stdio.h>
#include <stdlib.h>
#include <err.h>

#include <tskit.h>

#define check_tsk_error(val)                                                            \
    if (val < 0) {                                                                      \
        errx(EXIT_FAILURE, "line %d: %s", __LINE__, tsk_strerror(val));                 \
    }

int
main(int argc, char **argv)
{
    int ret;
    tsk_treeseq_t ts;
    tsk_variant_t var;
    tsk_size_t s, j;
    tsk_id_t k;
    const tsk_id_t *samples;

    if (argc != 2) {
        errx(EXIT_FAILURE, "usage: <tree sequence file>");
    }
    ret = tsk_treeseq_load(&ts, argv[1], 0);
    check_tsk_error(ret);

    samples = tsk_treeseq_get_samples(&ts);

    ret = tsk_variant_init(&var, &ts, NULL, 0, NULL, 0);
    check_tsk_error(ret);

    for (s = 0; s < tsk_treeseq_get_num_sites(&ts); s++) {
        ret = tsk_variant_decode(&var, (tsk_id_t) s, 0);
        check_tsk_error(ret);     
        for (j = 0; j < tsk_treeseq_get_num_samples(&ts); j++) {
            k = var.genotypes[j];
            printf("\t%d\t%d: '%.*s'\n", samples[j], k, (int) var.allele_lengths[k],
                    var.alleles[k]);
        }
        tsk_variant_free(&copy);
    }

    tsk_variant_free(&var);
    tsk_treeseq_free(&ts);
    return 0;
}

#2429 (comment)

@jeromekelleher jeromekelleher added C API Issue is about the C API documentation Documentation labels Jul 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C API Issue is about the C API documentation Documentation
Projects
None yet
Development

No branches or pull requests

1 participant