-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Construct subst matrix based on snv frequencies
- Loading branch information
Showing
5 changed files
with
175 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
(ns cljam.io.cram.encode.subst-matrix) | ||
|
||
(defprotocol ICounter | ||
(inc! [this])) | ||
|
||
(defprotocol IMutableInt | ||
(set-val! [this v])) | ||
|
||
(deftype MutableInt [^:unsynchronized-mutable ^long n] | ||
ICounter | ||
(inc! [_] | ||
(set! n (inc n))) | ||
IMutableInt | ||
(set-val! [_ v] | ||
(set! n (long v))) | ||
clojure.lang.IDeref | ||
(deref [_] n)) | ||
|
||
(defprotocol ISubstMatrixBuilder | ||
(assign-code! [this ref alt]) | ||
(build-subst-matrix [this])) | ||
|
||
(definline ^:private base->index [b] | ||
`(case (long ~b) | ||
~(int \A) 0 | ||
~(int \C) 1 | ||
~(int \G) 2 | ||
~(int \T) 3 | ||
~(int \N) 4)) | ||
|
||
(deftype SubstMatrixBuilder [^objects freqs] | ||
ISubstMatrixBuilder | ||
(assign-code! [_ r a] | ||
(let [mut (aget ^objects (aget freqs (base->index r)) (base->index a))] | ||
(inc! mut) | ||
mut)) | ||
(build-subst-matrix [_] | ||
(let [all-bases [\A \C \G \T \N]] | ||
(reduce | ||
(fn [m r] | ||
(let [^objects freqs (aget freqs (base->index (int r)))] | ||
(->> all-bases | ||
(keep (fn [a] | ||
(when (not= r a) | ||
[a (aget freqs (base->index (int a)))]))) | ||
(sort-by (comp - deref second)) | ||
(map-indexed vector) | ||
(reduce (fn [m [i [a mut]]] | ||
(set-val! mut i) | ||
(assoc-in m [r a] i)) | ||
m)))) | ||
{} all-bases)))) | ||
|
||
(defn make-subst-matrix-builder | ||
"Creates a new substitution matrix builder." | ||
[] | ||
(->> (fn [] (into-array (repeatedly 5 #(->MutableInt 0)))) | ||
(repeatedly 5) | ||
into-array | ||
->SubstMatrixBuilder)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
(ns cljam.io.cram.encode.subst-matrix-test | ||
(:require [cljam.io.cram.encode.subst-matrix :as subst-mat] | ||
[clojure.test :refer [deftest is]])) | ||
|
||
(deftest make-subst-matrix-builder-test | ||
(let [builder (subst-mat/make-subst-matrix-builder) | ||
a->t (subst-mat/assign-code! builder (int \A) (int \T)) | ||
a->c (subst-mat/assign-code! builder (int \A) (int \C)) | ||
a->g (subst-mat/assign-code! builder (int \A) (int \G)) | ||
a->t' (subst-mat/assign-code! builder (int \A) (int \T)) | ||
g->c (subst-mat/assign-code! builder (int \G) (int \C)) | ||
g->t (subst-mat/assign-code! builder (int \G) (int \T)) | ||
t->n (subst-mat/assign-code! builder (int \T) (int \N)) | ||
n->c (subst-mat/assign-code! builder (int \N) (int \C))] | ||
(is (= {\A {\C 1, \G 2, \T 0, \N 3} | ||
\C {\A 0, \G 1, \T 2, \N 3} | ||
\G {\A 2, \C 0, \T 1, \N 3} | ||
\T {\A 1, \C 2, \G 3, \N 0} | ||
\N {\A 1, \C 0, \G 2, \T 3}} | ||
(subst-mat/build-subst-matrix builder))) | ||
(is (= 0 @a->t @a->t')) | ||
(is (= 1 @a->c)) | ||
(is (= 2 @a->g)) | ||
(is (= 0 @g->c)) | ||
(is (= 1 @g->t)) | ||
(is (= 0 @t->n)) | ||
(is (= 0 @n->c)))) |