Commit 288e605c by Laros

### Got rid of the class.

parent 35e0fc3d
 ... @@ -7,7 +7,7 @@ Copyright (c) 2013-2016 Jeroen F.J. Laros ... @@ -7,7 +7,7 @@ Copyright (c) 2013-2016 Jeroen F.J. Laros 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'] def _all_words(bucket, word, length, result): """ """ Design and test NGS barcodes. Generate all possible words of a certain length over a specified alphabet. :arg list bucket: An alphabet. :arg str word: A word over the alphabet {bucket}. :arg int length: Lenth of the barcodes. :arg list result: Constructed words. """ """ _nucleotides = ['A', 'C', 'G', 'T'] if length: for i in bucket: _all_words(bucket, word + i, length - 1, result) else: result.append(word) def __init__(self, distance=Levenshtein.distance): """ Initialise the class. :arg function distance: Distance function. def _filter_stretch(barcode, stretches): """ """ self.distance = distance Test whether {barcode} contains none of the stretches in {stretches}. def _all_words(self, bucket, word, length, result): :arg str barcode: A barcode. """ :arg list stretches: Generate all possible words of a certain length over a specified alphabet. :arg list bucket: An alphabet. :returns bool: True if the barcode is clean, False otherwise. :arg str word: A word over the alphabet {bucket}. """ :arg int length: Lenth of the barcodes. for i in stretches: :arg list result: Constructed words. if i in barcode: """ return False if length: for i in bucket: return True self._all_words(bucket, word + i, length - 1, result) else: result.append(word) def _filter_stretch(self, barcode, stretches): """ Test whether {barcode} contains none of the stretches in {stretches}. :arg str barcode: A barcode. def _filter_distance(barcodes, candidate, min_dist, distance): :arg list stretches: """ Test whether {candidate} can be added to {barcodes} based on the minimum distance between {candidate} and all barcodes in {barcodes}. :returns bool: True if the barcode is clean, False otherwise. :arg list barcodes: List of barcodes. """ :arg str candidate: Candidate barcode. for i in stretches: :arg int min_dist: Minimum distance between the barcodes. if i in barcode: :arg function distance: Distance function. return False return True :returns bool: True if the barcode is clean, False otherwise. """ for i in barcodes: if distance(i, candidate) < min_dist: return False def _filter_distance(self, barcodes, candidate, min_dist): return True """ Test whether {candidate} can be added to {barcodes} based on the minimum distance between {candidate} and all barcodes in {barcodes}. :arg list barcodes: List of barcodes. :arg str candidate: Candidate barcode. :arg int min_dist: Minimum distance between the barcodes. :returns bool: True if the barcode is clean, False otherwise. def all_barcodes(length): """ """ for i in barcodes: Generate all possible barcodes of a certain length. if self.distance(i, candidate) < min_dist: return False return True :arg int length: Lenth of the barcodes. def all_barcodes(self, length): :returns list: List of barcodes. """ """ Generate all possible barcodes of a certain length. result = [] :arg int length: Lenth of the barcodes. _all_words(_nucleotides, '', length, result) :returns list: List of barcodes. return result """ result = [] self._all_words(self._nucleotides, '', length, result) return result def filter_stretches(barcodes, max_stretch): """ Filter a list of barcodes for mononucleotide stretches. def filter_stretches(self, barcodes, max_stretch): :arg list barcodes: List of barcodes. """ :arg int max_stretch: Maximum mononucleotide stretch length. Filter a list of barcodes for mononucleotide stretches. :arg list barcodes: List of barcodes. :returns list: List of barcodes filtered for mononucleotide stretches. :arg int max_stretch: Maximum mononucleotide stretch length. """ stretches = map(lambda x: (max_stretch + 1) * x, _nucleotides) result = [] :returns list: List of barcodes filtered for mononucleotide stretches. for i in barcodes: """ if _filter_stretch(i, stretches): stretches = map(lambda x: (max_stretch + 1) * x, self._nucleotides) result.append(i) result = [] for i in barcodes: return result if self._filter_stretch(i, stretches): result.append(i) 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. """ """ 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!