Commit 288e605c authored by Laros's avatar Laros
Browse files

Got rid of the class.

parent 35e0fc3d
...@@ -7,7 +7,7 @@ Copyright (c) 2013-2016 Jeroen F.J. Laros <J.F.J.Laros@lumc.nl> ...@@ -7,7 +7,7 @@ Copyright (c) 2013-2016 Jeroen F.J. Laros <J.F.J.Laros@lumc.nl>
Licensed under the MIT license, see the LICENSE file. Licensed under the MIT license, see the LICENSE file.
""" """
from .barcode import BarCode from .barcode import filter_distance, all_barcodes, filter_stretches
__version_info__ = ('0', '6', '0') __version_info__ = ('0', '6', '0')
......
import Levenshtein import Levenshtein
class BarCode(object): _nucleotides = ['A', 'C', 'G', 'T']
"""
Design and test NGS barcodes.
"""
_nucleotides = ['A', 'C', 'G', 'T']
def __init__(self, distance=Levenshtein.distance):
"""
Initialise the class.
:arg function distance: Distance function. def _all_words(bucket, word, length, result):
"""
self.distance = distance
def _all_words(self, bucket, word, length, result):
""" """
Generate all possible words of a certain length over a specified Generate all possible words of a certain length over a specified
alphabet. alphabet.
...@@ -27,11 +16,12 @@ class BarCode(object): ...@@ -27,11 +16,12 @@ class BarCode(object):
""" """
if length: if length:
for i in bucket: for i in bucket:
self._all_words(bucket, word + i, length - 1, result) _all_words(bucket, word + i, length - 1, result)
else: else:
result.append(word) result.append(word)
def _filter_stretch(self, barcode, stretches):
def _filter_stretch(barcode, stretches):
""" """
Test whether {barcode} contains none of the stretches in {stretches}. Test whether {barcode} contains none of the stretches in {stretches}.
...@@ -46,7 +36,8 @@ class BarCode(object): ...@@ -46,7 +36,8 @@ class BarCode(object):
return True return True
def _filter_distance(self, barcodes, candidate, min_dist):
def _filter_distance(barcodes, candidate, min_dist, distance):
""" """
Test whether {candidate} can be added to {barcodes} based on the Test whether {candidate} can be added to {barcodes} based on the
minimum distance between {candidate} and all barcodes in {barcodes}. minimum distance between {candidate} and all barcodes in {barcodes}.
...@@ -54,16 +45,18 @@ class BarCode(object): ...@@ -54,16 +45,18 @@ class BarCode(object):
:arg list barcodes: List of barcodes. :arg list barcodes: List of barcodes.
:arg str candidate: Candidate barcode. :arg str candidate: Candidate barcode.
:arg int min_dist: Minimum distance between the barcodes. :arg int min_dist: Minimum distance between the barcodes.
:arg function distance: Distance function.
:returns bool: True if the barcode is clean, False otherwise. :returns bool: True if the barcode is clean, False otherwise.
""" """
for i in barcodes: for i in barcodes:
if self.distance(i, candidate) < min_dist: if distance(i, candidate) < min_dist:
return False return False
return True return True
def all_barcodes(self, length):
def all_barcodes(length):
""" """
Generate all possible barcodes of a certain length. Generate all possible barcodes of a certain length.
...@@ -73,11 +66,12 @@ class BarCode(object): ...@@ -73,11 +66,12 @@ class BarCode(object):
""" """
result = [] result = []
self._all_words(self._nucleotides, '', length, result) _all_words(_nucleotides, '', length, result)
return result return result
def filter_stretches(self, barcodes, max_stretch):
def filter_stretches(barcodes, max_stretch):
""" """
Filter a list of barcodes for mononucleotide stretches. Filter a list of barcodes for mononucleotide stretches.
...@@ -86,21 +80,23 @@ class BarCode(object): ...@@ -86,21 +80,23 @@ class BarCode(object):
:returns list: List of barcodes filtered for mononucleotide stretches. :returns list: List of barcodes filtered for mononucleotide stretches.
""" """
stretches = map(lambda x: (max_stretch + 1) * x, self._nucleotides) stretches = map(lambda x: (max_stretch + 1) * x, _nucleotides)
result = [] result = []
for i in barcodes: for i in barcodes:
if self._filter_stretch(i, stretches): if _filter_stretch(i, stretches):
result.append(i) result.append(i)
return result return result
def filter_distance(self, barcodes, min_dist):
def filter_distance(barcodes, min_dist, distance=Levenshtein.distance):
""" """
Filter a list of barcodes for distance to other barcodes. Filter a list of barcodes for distance to other barcodes.
:arg list barcodes: List of barcodes. :arg list barcodes: List of barcodes.
:arg int min_dist: Minimum distance between the barcodes. :arg int min_dist: Minimum distance between the barcodes.
:arg function distance: Distance function.
:returns list: List of barcodes filtered for distance to other :returns list: List of barcodes filtered for distance to other
barcodes. barcodes.
...@@ -108,7 +104,7 @@ class BarCode(object): ...@@ -108,7 +104,7 @@ class BarCode(object):
result = [] result = []
for i in barcodes: for i in barcodes:
if self._filter_distance(result, i, min_dist): if _filter_distance(result, i, min_dist, distance):
result.append(i) result.append(i)
return result return result
...@@ -5,8 +5,8 @@ import sys ...@@ -5,8 +5,8 @@ import sys
import Levenshtein import Levenshtein
from . import doc_split, version, usage from . import doc_split, usage, version
from .barcode import BarCode from .barcode import all_barcodes, filter_distance, filter_stretches
def make_barcodes(length, max_stretch, min_dist, distance): def make_barcodes(length, max_stretch, min_dist, distance):
...@@ -19,10 +19,8 @@ def make_barcodes(length, max_stretch, min_dist, distance): ...@@ -19,10 +19,8 @@ def make_barcodes(length, max_stretch, min_dist, distance):
:arg int min_dist: Minimum distance between the barcodes. :arg int min_dist: Minimum distance between the barcodes.
:arg function distance: Distance function. :arg function distance: Distance function.
""" """
bc = BarCode(distance) return filter_distance(
filter_stretches(all_barcodes(length), max_stretch), min_dist)
return bc.filter_distance(
bc.filter_stretches(bc.all_barcodes(length), max_stretch), min_dist)
def test_barcodes(barcodes, min_dist, distance, handle): def test_barcodes(barcodes, min_dist, distance, handle):
...@@ -32,12 +30,11 @@ def test_barcodes(barcodes, min_dist, distance, handle): ...@@ -32,12 +30,11 @@ def test_barcodes(barcodes, min_dist, distance, handle):
:arg list barcodes: List of barcodes. :arg list barcodes: List of barcodes.
:arg int min_dist: Minimum distance between the barcodes. :arg int min_dist: Minimum distance between the barcodes.
:arg function distance: Distance function. :arg function distance: Distance function.
:arg steam handle: Open readable handle to a file.
:returns int: The number of barcodes that violate the distance constraint. :returns int: The number of barcodes that violate the distance constraint.
""" """
bc = BarCode(distance) good_subset = filter_distance(barcodes, min_dist)
good_subset = bc.filter_distance(barcodes, min_dist)
if handle: if handle:
handle.write('\n'.join(good_subset)) handle.write('\n'.join(good_subset))
......
Supports Markdown
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