Skip to content

Commit

Permalink
move Defect and Strand class definitions out of Location class, small…
Browse files Browse the repository at this point in the history
… fix in stockholm.fts2row()
  • Loading branch information
trichter committed Nov 8, 2024
1 parent 567ded1 commit c921cef
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 25 deletions.
13 changes: 7 additions & 6 deletions sugar/_io/genbank.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
`Genbank`_ reader
"""

from sugar.core.seq import Attr, BioSeq, Meta, Feature, FeatureList, Location
from sugar.core.fts import Defect, Feature, FeatureList, Location
from sugar.core.seq import Attr, BioSeq, Meta
from sugar._io.util import _add_fmt_doc


Expand Down Expand Up @@ -34,21 +35,21 @@ def _parse_locs(loc: str):


def _parse_single_loc(loc: str):
defect = Location.Defect.NONE
defect = Defect.NONE
if loc[0] == '<':
defect |= Location.Defect.BEYOND_LEFT
defect |= Defect.BEYOND_LEFT
loc = loc[1:]
if '>' in loc:
defect |= Location.Defect.BEYOND_RIGHT
defect |= Defect.BEYOND_RIGHT
loc = loc.replace('>', '')
if ".." in loc:
splitter = ".."
elif "." in loc:
splitter = "."
defect |= Location.Defect.UNKNOWN_SINGLE_BETWEEN
defect |= Defect.UNKNOWN_SINGLE_BETWEEN
elif "^" in loc:
splitter = "^"
defect |= Location.Defect.BETWEEN_CONSECUTIVE
defect |= Defect.BETWEEN_CONSECUTIVE
else:
# single base
return Location(int(loc)-1, int(loc), defect=defect)
Expand Down
16 changes: 9 additions & 7 deletions sugar/_io/stockholm.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def is_stockholm(f, **kw):

def row2fts(row, type_=None, seqid=None):
import re
from sugar.core.fts import Feature, FeatureList, Location
from sugar.core.fts import Defect, Feature, FeatureList, Location
i = 0
fts = []
while i < len(row):
Expand All @@ -36,14 +36,14 @@ def row2fts(row, type_=None, seqid=None):
name = names.pop()
if i == 0 and row[0] != '|' and j == len(row):
assert row[-1] != '|'
defect = Location.Defect.MISS_LEFT | Location.Defect.MISS_RIGHT
defect = Defect.MISS_LEFT | Defect.MISS_RIGHT
if i == 0 and row[0] != '|':
defect = Location.Defect.MISS_LEFT
defect = Defect.MISS_LEFT
elif j == len(row):
assert row[-1] != '|'
defect = Location.Defect.MISS_RIGHT
defect = Defect.MISS_RIGHT
else:
defect = Location.Defect.NONE
defect = Defect.NONE
loc = Location(i, stop, defect=defect)
ft = Feature(type_, [loc])
ft.meta.name = name
Expand Down Expand Up @@ -79,9 +79,11 @@ def fts2row(fts):
if l < len(name)-2:
warn('Feature name too long, shorten it')
rowp = '.' + name[:l-2] + '.'
if ft.loc.Defect.MISS_LEFT not in ft.loc.defect:
if (ft.loc.defect.MISS_LEFT not in ft.loc.defect and
ft.loc.defect.BEYOND_LEFT not in ft.loc.defect):
rowp = '|' + rowp[1:]
if ft.loc.Defect.MISS_RIGHT not in ft.locs[-1].defect:
if (ft.loc.defect.MISS_RIGHT not in ft.locs[-1].defect and
ft.loc.defect.BEYOND_RIGHT not in ft.locs[-1].defect):
rowp = rowp[:-1] + '|'
assert len(rowp) == l
row.append(rowp)
Expand Down
8 changes: 3 additions & 5 deletions sugar/core/fts.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# (C) 2024, Tom Eulenfeld, MIT license

"""
Feature related classes `.Feature`, `.FeatureList`, `.Location`, `.Location.Strand`, `.Location.Defect`
Feature related classes `.Feature`, `.FeatureList`, `.Location`, `.Strand`, `.Defect`
"""

# Originally, this file was based on the annotation module from the biotite package.
Expand Down Expand Up @@ -74,8 +74,6 @@ class Strand(StrEnum):


class Location():
Strand = Strand
Defect = Defect

def __init__(self, start, stop, strand='+', defect=0, meta=None):
"""
Expand Down Expand Up @@ -657,9 +655,9 @@ def slice(self, start, stop, rel=0):
# given location range
defect = loc.defect
if loc.start < start:
defect |= Location.Defect.MISS_LEFT
defect |= Defect.MISS_LEFT
if loc.stop > stop:
defect |= Location.Defect.MISS_RIGHT
defect |= Defect.MISS_RIGHT
lstart = max(start, loc.start) - rel
lstop = min(stop, loc.stop) - rel
locs_in_scope.append(Location(
Expand Down
4 changes: 2 additions & 2 deletions sugar/tests/test_core_fts.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ def test_fts_slice():
assert fts.slice(None, None) == fts
fts2 = fts.slice(1, fts.loc_range[1]-1)
assert len(fts2) == len(fts)
assert fts[0].loc.defect == fts2[0].loc.Defect.NONE
assert fts2[0].loc.defect == fts2[0].loc.Defect.MISS_LEFT | fts2[0].loc.Defect.MISS_RIGHT
assert fts[0].loc.defect == fts2[0].loc.defect.NONE
assert fts2[0].loc.defect == fts2[0].loc.defect.MISS_LEFT | fts2[0].loc.defect.MISS_RIGHT


def test_ft_overlaps():
Expand Down
2 changes: 1 addition & 1 deletion sugar/tests/test_core_seq.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def test_seqs_getitem_special():
assert seq2 == seq3
assert len(seq2.fts) == 2
assert all(len(ft) == 99 for ft in seq2.fts)
assert all(ft.loc.defect == ft.loc.Defect.MISS_LEFT | ft.loc.Defect.MISS_RIGHT for ft in seq2.fts)
assert all(ft.loc.defect == ft.loc.defect.MISS_LEFT | ft.loc.defect.MISS_RIGHT for ft in seq2.fts)
seq2 = seq.sl(update_fts=True)['cds']
assert seq2['cds'].data == seq['cds'].data
assert len(seq2) == len(seq2['cds'])
Expand Down
8 changes: 4 additions & 4 deletions sugar/tests/test_io_stockholm.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ def test_stockholm_row2fts2row():
assert len(fts3) == 1
assert fts1[0].name == '1'
assert fts2[0].name == '1'
Defect = fts1[0].loc.Defect
assert fts1[0].loc.defect == Defect.MISS_LEFT
assert fts1[1].loc.defect == Defect.NONE
assert fts2[-1].loc.defect == Defect.MISS_RIGHT
defect = fts1[0].loc.defect
assert fts1[0].loc.defect == defect.MISS_LEFT
assert fts1[1].loc.defect == defect.NONE
assert fts2[-1].loc.defect == defect.MISS_RIGHT
assert len(fts3[0]) == 1003
row1b = fts2row(fts1)
row2b = fts2row(fts2)
Expand Down

0 comments on commit c921cef

Please sign in to comment.