Skip to content
Snippets Groups Projects
Commit 9f5e8774 authored by nfontrod's avatar nfontrod
Browse files

src/db_utils/frequency_scripts/frequency_function.py: modification of...

src/db_utils/frequency_scripts/frequency_function.py: modification of frequencies for better performances
parent 49f7a4af
No related branches found
No related tags found
No related merge requests found
......@@ -12,6 +12,7 @@ from typing import Iterable, Dict, List
import logging
from Bio.Seq import Seq
import numpy as np
import regex as re
def frequencies(sequence: str, nt_list: Iterable[str]) -> Dict[str, float]:
......@@ -24,23 +25,16 @@ def frequencies(sequence: str, nt_list: Iterable[str]) -> Dict[str, float]:
"""
if not full_defined(sequence) or len(sequence) < 3:
return {nt: np.nan for nt in nt_list}
dic = {nt: 0. for nt in nt_list}
seql = len(sequence)
for i, n in enumerate(sequence):
for nt in nt_list:
ntl = len(nt)
if ntl == 1:
if nt in ["S", "W", "K", "M", "R", "Y"]:
if n in Config_freq.iupac_dic[nt]:
dic[nt] += 1 / seql * 100
else:
if n == nt:
dic[nt] += 1 / seql * 100
else:
if sequence[i:i + ntl] == nt:
dic[nt] += 1 / (seql - ntl + 1) * 100
for nt in nt_list:
dic[nt] = round(dic[nt], 5)
dic = {}
for n in nt_list:
if n in Config_freq.iupac_dic.keys():
pat = re.compile(Config_freq.iupac_dic[n])
else:
pat = re.compile(n)
seqlen = seql - len(n) + 1
dic[n] = round((len(list(re.findall(pat, sequence, overlapped=True)))
/ seqlen) * 100, 5)
return dic
......@@ -57,7 +51,7 @@ def compute_dic(dic_seq: Dict[str, Seq], coord: List[str],
sequence = dic_seq[coord[0]][int(coord[1]): int(coord[2])]
if coord[3] == "-":
sequence = sequence.reverse_complement()
return frequencies(sequence, nt_list)
return frequencies(str(sequence), nt_list)
def get_ft_type(ft: str) -> str:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment