Skip to content

Commit

Permalink
Stripped version: very fast
Browse files Browse the repository at this point in the history
  • Loading branch information
horta committed Feb 5, 2024
1 parent 168515c commit c2172cd
Show file tree
Hide file tree
Showing 30 changed files with 1,187 additions and 2,208 deletions.
103 changes: 103 additions & 0 deletions c-core/setup_viterbi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include "protein.h"
#include "protein_node.h"
#include "vit.h"
#include <math.h>

struct viterbi_xtrans
{
float const SB;
float const SN;
float const NN;
float const NB;

float const ET;
float const EC;
float const CC;
float const CT;

float const EB;
float const EJ;
float const JJ;
float const JB;

float const ME;
float const DE;
};

struct viterbi_xtrans viterbi_xtrans_init(struct xtrans x)
{
return (struct viterbi_xtrans){
.SB = x.NB,
.SN = x.NN,
.NN = x.NN,
.NB = x.NB,

.ET = x.EC + x.CT,
.EC = x.EC + x.CC,
.CC = x.CC,
.CT = x.CT,

.EB = x.EJ + x.JB,
.EJ = x.EJ + x.JJ,
.JJ = x.JJ,
.JB = x.JB,

.ME = 0.0f,
.DE = 0.0f,
};
}

int setup_viterbi(struct vit *x, struct protein const *protein)
{
int K = protein->core_size;
int rc = vit_setup(x, K);
if (rc) return rc;

struct viterbi_xtrans const xt = viterbi_xtrans_init(protein->xtrans);
vit_set_extr_trans(x, EXTR_TRANS_RR, -protein->null.RR);
vit_set_extr_trans(x, EXTR_TRANS_SN, -xt.SN);
vit_set_extr_trans(x, EXTR_TRANS_NN, -xt.NN);
vit_set_extr_trans(x, EXTR_TRANS_SB, -xt.SB);
vit_set_extr_trans(x, EXTR_TRANS_NB, -xt.NB);
vit_set_extr_trans(x, EXTR_TRANS_EB, -xt.EB);
vit_set_extr_trans(x, EXTR_TRANS_JB, -xt.JB);
vit_set_extr_trans(x, EXTR_TRANS_EJ, -xt.EJ);
vit_set_extr_trans(x, EXTR_TRANS_JJ, -xt.JJ);
vit_set_extr_trans(x, EXTR_TRANS_EC, -xt.EC);
vit_set_extr_trans(x, EXTR_TRANS_CC, -xt.CC);
vit_set_extr_trans(x, EXTR_TRANS_ET, -xt.ET);
vit_set_extr_trans(x, EXTR_TRANS_CT, -xt.CT);

for (int k = 0; k < K; ++k)
{
vit_set_core_trans(x, CORE_TRANS_BM, -protein->BMk[k], k);
}

vit_set_core_trans(x, CORE_TRANS_MM, INFINITY, 0);
vit_set_core_trans(x, CORE_TRANS_MD, INFINITY, 0);
vit_set_core_trans(x, CORE_TRANS_IM, INFINITY, 0);
vit_set_core_trans(x, CORE_TRANS_DM, INFINITY, 0);
vit_set_core_trans(x, CORE_TRANS_DD, INFINITY, 0);
for (int k = 0; k < K - 1; ++k)
{
vit_set_core_trans(x, CORE_TRANS_MM, -protein->nodes[k].trans.MM, k + 1);
vit_set_core_trans(x, CORE_TRANS_MI, -protein->nodes[k].trans.MI, k + 0);
vit_set_core_trans(x, CORE_TRANS_MD, -protein->nodes[k].trans.MD, k + 1);
vit_set_core_trans(x, CORE_TRANS_IM, -protein->nodes[k].trans.IM, k + 1);
vit_set_core_trans(x, CORE_TRANS_II, -protein->nodes[k].trans.II, k + 0);
vit_set_core_trans(x, CORE_TRANS_DM, -protein->nodes[k].trans.DM, k + 1);
vit_set_core_trans(x, CORE_TRANS_DD, -protein->nodes[k].trans.DD, k + 1);
}
vit_set_core_trans(x, CORE_TRANS_MI, INFINITY, K - 1);
vit_set_core_trans(x, CORE_TRANS_II, INFINITY, K - 1);

for (size_t i = 0; i < VITERBI_TABLE_SIZE; ++i)
{
vit_set_null(x, -protein->null.emission[i], i);
vit_set_background(x, -protein->bg.emission[i], i);

for (int k = 0; k < K; ++k)
vit_set_match(x, -protein->nodes[k].emission[i], k, i);
}
return 0;
}
9 changes: 9 additions & 0 deletions c-core/setup_viterbi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef SETUP_VITERBI_H
#define SETUP_VITERBI_H

struct vit;
struct protein;

int setup_viterbi(struct vit *, struct protein const *);

#endif
46 changes: 28 additions & 18 deletions c-core/test_protein.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
#include "imm/nuclt_code.h"
#include "imm/path.h"
#include "protein.h"
#include "setup_viterbi.h"
#include "state.h"
#include "trellis.h"
#include "vendor/minctest.h"
#include "viterbi.h"
#include "viterbi_struct.h"
#include "vit.h"

static void test_protein_uniform(void);
static void test_protein_occupancy(void);
Expand All @@ -19,6 +21,12 @@ int main(void)
return lfails;
}

static int code_fn(int pos, int len, void *arg)
{
struct imm_eseq const *seq = arg;
return imm_eseq_get(seq, pos, len, 1);
}

static void test_protein_uniform(void)
{
struct imm_path path = imm_path();
Expand Down Expand Up @@ -55,13 +63,14 @@ static void test_protein_uniform(void)

eq(imm_eseq_setup(&eseq, &seq), 0);

struct viterbi task = {};
viterbi_init(&task);
viterbi_setup(&task, &protein, &eseq);
close(viterbi_null_loglik(&task), -48.9272687711);
float score = 0;
close(viterbi_alt_path(&task, &path, &score), 0);
close(score, -55.59428153448);
struct vit *task = vit_new();
eq(vit_setup(task, protein.core_size), 0);
eq(setup_viterbi(task, &protein), 0);
close(vit_null(task, imm_eseq_size(&eseq), code_fn, (void *)&eseq), 48.9272687711);
close(vit_cost(task, imm_eseq_size(&eseq), code_fn, (void *)&eseq), 55.59428153448);
eq(vit_path(task, imm_eseq_size(&eseq), code_fn, (void *)&eseq), 0);
imm_path_reset(&path);
eq(trellis_unzip(vit_trellis(task), imm_eseq_size(&eseq), &path), 0);

eq(imm_path_nsteps(&path), 14);

Expand Down Expand Up @@ -100,7 +109,7 @@ static void test_protein_uniform(void)
eq(i, 10);

imm_eseq_cleanup(&eseq);
viterbi_cleanup(&task);
vit_del(task);
protein_cleanup(&protein);
imm_path_cleanup(&path);
}
Expand Down Expand Up @@ -141,13 +150,14 @@ static void test_protein_occupancy(void)

eq(imm_eseq_setup(&eseq, &seq), 0);

struct viterbi task = {};
viterbi_init(&task);
viterbi_setup(&task, &protein, &eseq);
close(viterbi_null_loglik(&task), -48.9272687711);
float score = 0;
close(viterbi_alt_path(&task, &path, &score), 0);
close(score, -54.35543421312);
struct vit *task = vit_new();
eq(vit_setup(task, protein.core_size), 0);
eq(setup_viterbi(task, &protein), 0);
close(vit_null(task, imm_eseq_size(&eseq), code_fn, (void *)&eseq), 48.9272687711);
close(vit_cost(task, imm_eseq_size(&eseq), code_fn, (void *)&eseq), 54.35543421312);
eq(vit_path(task, imm_eseq_size(&eseq), code_fn, (void *)&eseq), 0);
imm_path_reset(&path);
eq(trellis_unzip(vit_trellis(task), imm_eseq_size(&eseq), &path), 0);

eq(imm_path_nsteps(&path), 14);

Expand Down Expand Up @@ -186,7 +196,7 @@ static void test_protein_occupancy(void)
eq(i, 10);

imm_eseq_cleanup(&eseq);
viterbi_cleanup(&task);
vit_del(task);
protein_cleanup(&protein);
imm_path_cleanup(&path);
}
114 changes: 0 additions & 114 deletions c-core/test_viterbi.c

This file was deleted.

2 changes: 1 addition & 1 deletion c-core/test_window.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ int main(void)
eq(scan_add(scan, sequences[0].id, sequences[0].name, seq), 0);
eq(scan_run(scan, PRODDIR), 0);
eq(scan_progress(scan), 100);
eq(chksum(PRODDIR "/products.tsv"), 5268);
eq(chksum(PRODDIR "/products.tsv"), 5999);
eq(scan_close(scan), 0);

scan_del(scan);
Expand Down
Loading

0 comments on commit c2172cd

Please sign in to comment.