Skip to content

Commit 257e096

Browse files
committed
be precise, tolist -> tolists, fts.d/todict behave now in the same way as seqs.d/todict, both return a dict {id: ft}
1 parent 8c1bed6 commit 257e096

File tree

4 files changed

+19
-22
lines changed

4 files changed

+19
-22
lines changed

sugar/core/fts.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ def tofmtstr(self, fmt, **kw):
567567
"""
568568
return self.write(None, fmt)
569569

570-
def tolist(self, vals='type start stop strand'):
570+
def tolists(self, vals='type start stop strand'):
571571
"""
572572
Return a generator yielding a list for each feature
573573
@@ -579,7 +579,7 @@ def tolist(self, vals='type start stop strand'):
579579
580580
>>> from sugar import read_fts
581581
>>> fts = read_fts().select('cDNA_match')
582-
>>> for record in fts.tolist('type start strand len'):
582+
>>> for record in fts.tolists('type start strand len'):
583583
... print(*record)
584584
cDNA_match 101888622 - 4245
585585
cDNA_match 103140200 - 30745
@@ -623,7 +623,7 @@ def topandas(self, vals='type start stop strand', **kw):
623623
if isinstance(vals, str):
624624
vals = vals.split()
625625
kw.setdefault('columns', vals)
626-
return pandas.DataFrame(self.tolist(vals=vals), **kw)
626+
return pandas.DataFrame(self.tolists(vals=vals), **kw)
627627

628628

629629
def get(self, type):
@@ -660,17 +660,15 @@ def select(self, type):
660660

661661
def todict(self):
662662
"""
663-
Return a dictionary with sequence ids as keys and FeatureLists as values
663+
Return a dictionary with feature ids as keys and features as values
664664
665-
Similar as ``FeatureList.groupby('seqid')``.
666-
The key for features without seqids is set to the empty string ``''``,
667-
in the groupyby() method this key is set to ``None``.
665+
.. note::
666+
This method is different from the `FeatureList.groupby()` method.
667+
Each value of the dict returned by ``todict()`` is a feature,
668+
whereas each value of the dict returned by ``groupby()`` is a
669+
FeatureList.
668670
"""
669-
d = {}
670-
for ft in self:
671-
seqid = ft.meta.get('seqid', '')
672-
d.setdefault(seqid, self.__class__()).append(ft)
673-
return d
671+
return {ft.id: ft for ft in self}
674672

675673
def groupby(self, keys=('seqid',)):
676674
"""
@@ -693,9 +691,9 @@ def groupby(self, keys=('seqid',)):
693691
@property
694692
def d(self):
695693
"""
696-
Alias for ``FeatureList.groupby('seqid')``
694+
Alias for ``FeatureList.todict()``
697695
"""
698-
return self.groupby('seqid')
696+
return self.todict()
699697

700698
@property
701699
def loc_range(self):

sugar/core/seq.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ def fts(self):
735735

736736
@fts.setter
737737
def fts(self, value):
738-
fts = FeatureList(value).todict()
738+
fts = FeatureList(value).groupby('seqid')
739739
for seq in self:
740740
if seq.id in fts:
741741
seq.fts = fts.pop(seq.id)
@@ -752,7 +752,7 @@ def add_fts(self, fts):
752752
753753
:param fts: features to add
754754
"""
755-
fts = FeatureList(fts).todict()
755+
fts = FeatureList(fts).groupby('seqid')
756756
for seq in self:
757757
if seq.id in fts:
758758
seq.fts = seq.fts + fts.pop(seq.id)

sugar/tests/test_core_cane.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def test_match():
6464

6565

6666
def test_orf():
67-
seqs=read()
67+
seqs = read()
6868
orfs = seqs[0].find_orfs()
6969
assert len(orfs) > 0
7070
longest_orf = orfs.sort(len)[-1]
@@ -75,7 +75,7 @@ def test_orf():
7575

7676
orfs = seqs.find_orfs()
7777
for id_ in seqs.ids:
78-
assert seqs.d[id_][orfs.d[id_].sort(len)[-1]] == seqs.d[id_]['cds']
78+
assert seqs.d[id_][orfs.groupby('seqid')[id_].sort(len)[-1]] == seqs.d[id_]['cds']
7979

8080

8181
def test_filter_fts():
@@ -99,7 +99,6 @@ def test_filter_seqs():
9999
assert len(seqs2) < len(seqs)
100100

101101

102-
103102
def test_groupby_fts_nested():
104103
fts = read_fts()
105104
d = fts.groupby(('type', len))

sugar/tests/test_core_fts.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_fts_str():
3838

3939
def test_fts_d():
4040
fts = read_fts()
41-
assert len(fts.d[fts[0].meta.seqid]) > 0
41+
assert fts.d[fts[-1].id] == fts[-1]
4242

4343

4444
def test_fts_rc():
@@ -124,9 +124,9 @@ def test_loc_hash():
124124
assert locs in {locs}
125125

126126

127-
def test_fts_tolist():
127+
def test_fts_tolists():
128128
fts = read_fts().select('CDS')
129-
for type_, start, stop, strand in fts.tolist():
129+
for type_, start, stop, strand in fts.tolists():
130130
assert type_ == 'CDS'
131131
assert start == 61943
132132
assert strand == '+'

0 commit comments

Comments
 (0)