From c921cef5332e38c5ed86bb0a74458facc64d010e Mon Sep 17 00:00:00 2001 From: Tom Eulenfeld Date: Fri, 8 Nov 2024 17:01:54 +0100 Subject: [PATCH] move Defect and Strand class definitions out of Location class, small fix in stockholm.fts2row() --- sugar/_io/genbank.py | 13 +++++++------ sugar/_io/stockholm.py | 16 +++++++++------- sugar/core/fts.py | 8 +++----- sugar/tests/test_core_fts.py | 4 ++-- sugar/tests/test_core_seq.py | 2 +- sugar/tests/test_io_stockholm.py | 8 ++++---- 6 files changed, 26 insertions(+), 25 deletions(-) diff --git a/sugar/_io/genbank.py b/sugar/_io/genbank.py index 57bd419..16f0b38 100644 --- a/sugar/_io/genbank.py +++ b/sugar/_io/genbank.py @@ -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 @@ -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) diff --git a/sugar/_io/stockholm.py b/sugar/_io/stockholm.py index 5579069..0bfcdd6 100644 --- a/sugar/_io/stockholm.py +++ b/sugar/_io/stockholm.py @@ -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): @@ -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 @@ -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) diff --git a/sugar/core/fts.py b/sugar/core/fts.py index 63db528..e080ead 100644 --- a/sugar/core/fts.py +++ b/sugar/core/fts.py @@ -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. @@ -74,8 +74,6 @@ class Strand(StrEnum): class Location(): - Strand = Strand - Defect = Defect def __init__(self, start, stop, strand='+', defect=0, meta=None): """ @@ -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( diff --git a/sugar/tests/test_core_fts.py b/sugar/tests/test_core_fts.py index bf77ec9..a642ba4 100644 --- a/sugar/tests/test_core_fts.py +++ b/sugar/tests/test_core_fts.py @@ -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(): diff --git a/sugar/tests/test_core_seq.py b/sugar/tests/test_core_seq.py index bf57dd9..8b11a54 100644 --- a/sugar/tests/test_core_seq.py +++ b/sugar/tests/test_core_seq.py @@ -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']) diff --git a/sugar/tests/test_io_stockholm.py b/sugar/tests/test_io_stockholm.py index 0d001f6..6ac2edd 100644 --- a/sugar/tests/test_io_stockholm.py +++ b/sugar/tests/test_io_stockholm.py @@ -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)