-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added entropy calculation and printing to the explore command
- Loading branch information
Showing
2 changed files
with
67 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import itertools | ||
from collections import deque | ||
|
||
import numpy as np | ||
|
||
|
||
# Rolling window implementation | ||
def _window(seq, n=3): | ||
win = deque(maxlen=n) | ||
for char in seq: | ||
win.append(char) | ||
if len(win) >= n: | ||
yield tuple(win) | ||
|
||
|
||
def _entropy(pk, qk): | ||
"""Taken from https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.entropy.html | ||
Felt unecessary to add scipy as dependency just for this function. | ||
""" | ||
# TODO: Catch potential exceptions here for weird input values. | ||
return -np.sum(pk * np.log(qk)) | ||
|
||
|
||
# Each sequence gives matrix of position and k-mer occurence. | ||
def _seq_to_count_matrix(dna_seq, kmer_positions, max=50, k=2): | ||
"""max is the length of the prefix that will be considered""" | ||
if len(dna_seq) < max: | ||
max = len(dna_seq) | ||
|
||
matrix = np.zeros((4**k, max - k + 1)) | ||
for i, word in enumerate(_window(dna_seq[:max], n=k)): | ||
matrix[kmer_positions[word], i] += 1 | ||
return matrix | ||
|
||
|
||
def _count_for_seqs(seqs, kmer_positions, insert_length, k=2): | ||
seqs = [seq for seq in seqs if len(seq) == insert_length] | ||
matrices = [_seq_to_count_matrix(seq, kmer_positions, max=50, k=k) for seq in seqs] | ||
stacked = np.stack(matrices) | ||
sum_a = np.sum(stacked, axis=0) | ||
even_dist = np.ones(4**k) / (4**k) | ||
entropies = [_entropy(a, even_dist) for a in sum_a.T] | ||
|
||
return entropies | ||
|
||
|
||
def _extract_inserts_from_df(df): | ||
mim_re_cs = r"^cs:Z::[1-9][0-9]*\+([a,c,t,g]*):[1-9][0-9]*$" | ||
return df.cs.str.extract(mim_re_cs, expand=False).str.upper().tolist() | ||
|
||
|
||
def calculate_relative_entropy(df_good_hits, kmer_length, insert_length): | ||
all_kmers = itertools.product("ACTG", repeat=kmer_length) | ||
kmer_positions = dict( | ||
(kmer, position) for kmer, position in zip(all_kmers, range(4**kmer_length)) | ||
) | ||
seqs = _extract_inserts_from_df(df_good_hits) | ||
entropies = _count_for_seqs(seqs, kmer_positions, insert_length, k=kmer_length) | ||
return entropies |
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