Commit 1750cf57 authored by Sander Bollen's avatar Sander Bollen

Rewrite atleast functions in cython.

90% speedup.
parent 331eee9c
......@@ -10,6 +10,7 @@ setup.py
from os.path import abspath, dirname, join
from setuptools import setup
from Cython.Build import cythonize
from vtools import __version__
......@@ -41,5 +42,6 @@ setup(
},
classifiers=[
"Topic :: Scientific/Engineering :: Bio-Informatics"
]
],
ext_modules=cythonize("vtools/*.pyx")
)
\ No newline at end of file
......@@ -15,6 +15,8 @@ from itertools import chain
from typing import List, Optional, Tuple
from .optimized import amount_atleast
Region = namedtuple("Region", ["chr", "start", "end"])
......@@ -80,16 +82,16 @@ class CovStats(object):
return np.mean(self.gq_qualities)
def percent_atleast_dp(self, atleast) -> Optional[float]:
passing = [x for x in self.coverages if x >= atleast]
if len(self.coverages) > 0:
return (len(passing)/len(self.coverages))*100
return None
if len(self.coverages) == 0:
return None
k = amount_atleast(self.coverages, atleast)
return (k/len(self.coverages))*100
def percent_atleast_gq(self, atleast) -> Optional[float]:
passing = [x for x in self.gq_qualities if x >= atleast]
if len(self.gq_qualities) > 0:
return (len(passing)/len(self.gq_qualities))*100
return None
if len(self.gq_qualities) == 0:
return None
k = amount_atleast(self.gq_qualities, atleast)
return (k/len(self.gq_qualities))*100
@property
def stats(self) -> dict:
......
"""
vtools.optimized
~~~~~~~~~~~~~~~~
:copyright: (c) 2018 Sander Bollen
:copyright: (c) 2018 Leiden University Medical Center
:license: MIT
"""
cpdef int amount_atleast(values, int atleast):
"""
Return amount of values at least `atleast`
:param values: Iterable of int
:param atleast: int
:return: int
"""
cdef int passed = 0
cdef int val
for val in values:
if val >= atleast:
passed += 1
return passed
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment