Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Laros
barcode
Commits
288e605c
Commit
288e605c
authored
Nov 05, 2016
by
Laros
Browse files
Got rid of the class.
parent
35e0fc3d
Changes
3
Show whitespace changes
Inline
Side-by-side
barcode/__init__.py
View file @
288e605c
...
...
@@ -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.
"""
from
.barcode
import
B
ar
C
ode
from
.barcode
import
filter_distance
,
all_b
ar
c
ode
s
,
filter_stretches
__version_info__
=
(
'0'
,
'6'
,
'0'
)
...
...
barcode/barcode.py
View file @
288e605c
import
Levenshtein
class
BarCode
(
object
):
"""
Design and test NGS barcodes.
"""
_nucleotides
=
[
'A'
,
'C'
,
'G'
,
'T'
]
_nucleotides
=
[
'A'
,
'C'
,
'G'
,
'T'
]
def
__init__
(
self
,
distance
=
Levenshtein
.
distance
):
"""
Initialise the class.
:arg function distance: Distance function.
"""
self
.
distance
=
distance
def
_all_words
(
self
,
bucket
,
word
,
length
,
result
):
def
_all_words
(
bucket
,
word
,
length
,
result
):
"""
Generate all possible words of a certain length over a specified
alphabet.
...
...
@@ -27,11 +16,12 @@ class BarCode(object):
"""
if
length
:
for
i
in
bucket
:
self
.
_all_words
(
bucket
,
word
+
i
,
length
-
1
,
result
)
_all_words
(
bucket
,
word
+
i
,
length
-
1
,
result
)
else
:
result
.
append
(
word
)
def
_filter_stretch
(
self
,
barcode
,
stretches
):
def
_filter_stretch
(
barcode
,
stretches
):
"""
Test whether {barcode} contains none of the stretches in {stretches}.
...
...
@@ -46,7 +36,8 @@ class BarCode(object):
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
minimum distance between {candidate} and all barcodes in {barcodes}.
...
...
@@ -54,16 +45,18 @@ class BarCode(object):
:arg list barcodes: List of barcodes.
:arg str candidate: Candidate barcode.
:arg int min_dist: Minimum distance between the barcodes.
:arg function distance: Distance function.
:returns bool: True if the barcode is clean, False otherwise.
"""
for
i
in
barcodes
:
if
self
.
distance
(
i
,
candidate
)
<
min_dist
:
if
distance
(
i
,
candidate
)
<
min_dist
:
return
False
return
True
def
all_barcodes
(
self
,
length
):
def
all_barcodes
(
length
):
"""
Generate all possible barcodes of a certain length.
...
...
@@ -73,11 +66,12 @@ class BarCode(object):
"""
result
=
[]
self
.
_all_words
(
self
.
_nucleotides
,
''
,
length
,
result
)
_all_words
(
_nucleotides
,
''
,
length
,
result
)
return
result
def
filter_stretches
(
self
,
barcodes
,
max_stretch
):
def
filter_stretches
(
barcodes
,
max_stretch
):
"""
Filter a list of barcodes for mononucleotide stretches.
...
...
@@ -86,21 +80,23 @@ class BarCode(object):
: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
=
[]
for
i
in
barcodes
:
if
self
.
_filter_stretch
(
i
,
stretches
):
if
_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.
:arg list barcodes: List of 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
barcodes.
...
...
@@ -108,7 +104,7 @@ class BarCode(object):
result
=
[]
for
i
in
barcodes
:
if
self
.
_filter_distance
(
result
,
i
,
min_dist
):
if
_filter_distance
(
result
,
i
,
min_dist
,
distance
):
result
.
append
(
i
)
return
result
barcode/cli.py
View file @
288e605c
...
...
@@ -5,8 +5,8 @@ import sys
import
Levenshtein
from
.
import
doc_split
,
version
,
usage
from
.barcode
import
B
ar
C
ode
from
.
import
doc_split
,
usage
,
version
from
.barcode
import
all_b
ar
c
ode
s
,
filter_distance
,
filter_stretches
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 function distance: Distance function.
"""
bc
=
BarCode
(
distance
)
return
bc
.
filter_distance
(
bc
.
filter_stretches
(
bc
.
all_barcodes
(
length
),
max_stretch
),
min_dist
)
return
filter_distance
(
filter_stretches
(
all_barcodes
(
length
),
max_stretch
),
min_dist
)
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 int min_dist: Minimum distance between the barcodes.
: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.
"""
bc
=
BarCode
(
distance
)
good_subset
=
bc
.
filter_distance
(
barcodes
,
min_dist
)
good_subset
=
filter_distance
(
barcodes
,
min_dist
)
if
handle
:
handle
.
write
(
'
\n
'
.
join
(
good_subset
))
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment