Skip to content

Add decoding variants example C code. #2438

Open
@jeromekelleher

Description

@jeromekelleher

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C APIIssue is about the C APIdocumentationDocumentation

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions