Skip to content

Commit 189f1d6

Browse files
committed
shaved off a couple of lines of code
1 parent 5503661 commit 189f1d6

9 files changed

Lines changed: 28 additions & 66 deletions

File tree

src/nexus/checker.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1+
import string
12
import statistics
3+
import collections
24

3-
from collections import Counter
4-
from string import ascii_lowercase, ascii_uppercase, digits
5-
6-
SAFE_CHARACTERS = ascii_uppercase + ascii_lowercase + digits + '-_'
5+
SAFE_CHARACTERS = string.ascii_letters + string.digits + '-_'
76

87

98
class Checker(object):
109

1110
EMPTY_STATES = ('?', '-', '0')
1211

13-
def __init__(self, nex, verbose=False):
14-
self.verbose = verbose
12+
def __init__(self, nex):
1513
self.errors, self.messages = [], []
1614
self.check(nex)
1715

@@ -38,7 +36,7 @@ class DuplicateLabelChecker(Checker):
3836
Checks for Duplicate Character Labels
3937
"""
4038
def check(self, nex):
41-
labels = Counter(nex.data.charlabels.values())
39+
labels = collections.Counter(nex.data.charlabels.values())
4240
for label in labels:
4341
if labels[label] > 1:
4442
self.errors.append("Duplicate Label (n=%d): %s" % (labels[label], label))
@@ -82,7 +80,7 @@ class UnusualStateChecker(Checker):
8280
THRESHOLD = 0.001 # anything less than this is flagged
8381

8482
def check(self, nex):
85-
states = Counter()
83+
states = collections.Counter()
8684
for s in nex.data.matrix.values():
8785
states.update(s)
8886
total = sum(states.values())
@@ -100,7 +98,7 @@ class EmptyCharacterChecker(Checker):
10098
MIN_COUNT = 0
10199

102100
def check(self, nex):
103-
tally = Counter()
101+
tally = collections.Counter()
104102
for taxon in nex.data.matrix:
105103
tally.update([
106104
i for i, c in enumerate(nex.data.matrix[taxon], 0)
@@ -111,8 +109,7 @@ def check(self, nex):
111109
n = tally.get(i, 0)
112110
if n == self.MIN_COUNT:
113111
if 'ascert' in nex.data.charlabels.get(i, '').lower(): # ignore
114-
if self.verbose:
115-
self.log("Character %d is an ascertainment character" % i)
112+
self.log("Character %d is an ascertainment character" % i)
116113
else:
117114
self.errors.append("Character %d has count %d" % (i, self.MIN_COUNT))
118115
return not self.has_errors
@@ -159,7 +156,7 @@ def check(self, nex):
159156
# are they empty?
160157
for a in ascert:
161158
states = [nex.data.matrix[t][a] for t in nex.data.matrix]
162-
states = Counter([s for s in states if s not in self.EMPTY_STATES])
159+
states = collections.Counter([s for s in states if s not in self.EMPTY_STATES])
163160
if len(states):
164161
self.errors.append(
165162
"Character %d - %s should be an ascertainment character but has data (%r)" % (

src/nexus/commands/check.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ def register(parser):
1919
help="add ascertainment checks",
2020
action='store_true'
2121
)
22-
parser.add_argument(
23-
'-v', "--verbose",
24-
help="more output",
25-
default=True,
26-
action='store_true'
27-
)
2822

2923

3024
def run(args):
@@ -44,4 +38,4 @@ def run(args):
4438
print("\t%s" % w)
4539

4640
for checker in checkers:
47-
checker(nex, verbose=args.verbose).status()
41+
checker(nex).status()

src/nexus/handlers/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,9 @@ def remove_comments(line):
5757
"""
5858
Removes comments from lines
5959
60-
>>> g = GenericHandler()
61-
>>> g.remove_comments("Hello [world]")
60+
>>> GenericHandler.remove_comments("Hello [world]")
6261
'Hello '
63-
>>> g.remove_comments("He[bite]ll[me]o")
62+
>>> GenericHandler.remove_comments("He[bite]ll[me]o")
6463
'Hello'
6564
6665
:param line: string

src/nexus/tools/combine_nexuses.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,8 @@ def combine_nexuses(nexuslist):
88
Combines a list of NexusReader instances into a single nexus
99
1010
:param nexuslist: A list of NexusReader instances
11-
:type nexuslist: List
1211
1312
:return: A NexusWriter instance
14-
15-
:raises TypeError: if nexuslist is not a list of NexusReader instances
16-
:raises IOError: if unable to read an file in nexuslist
17-
:raises NexusFormatException: if a nexus file does not have a `data` block
1813
"""
1914
out = NexusWriter()
2015
# check they're all nexus instances and get all block types

src/nexus/tools/multistatise.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ def multistatise(nexus_obj, charlabel=None):
1212
:raises AssertionError: if nexus_obj is not a nexus
1313
:raises NexusFormatException: if nexus_obj does not have a `data` block
1414
"""
15-
if not charlabel:
16-
charlabel = getattr(nexus_obj, 'short_filename', 1)
15+
charlabel = charlabel or getattr(nexus_obj, 'short_filename', 1)
1716

1817
states = {}
1918
for taxon in nexus_obj.data.matrix:

src/nexus/tools/shufflenexus.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ def shufflenexus(nexus_obj, resample=False):
1616
:type resample: Integer
1717
1818
:return: A shuffled NexusReader instance
19-
:raises AssertionError: if nexus_obj is not a nexus
2019
:raises ValueError: if resample is not False or a positive Integer
21-
:raises NexusFormatException: if nexus_obj does not have a `data` block
2220
"""
2321
if resample is False:
2422
resample = nexus_obj.data.nchar

src/nexus/tools/sites.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,7 @@ def iter_constant_sites(nexus_obj):
1010
Returns a list of zero-based indices of the constant sites in a nexus
1111
"""
1212
for i in range(0, nexus_obj.data.nchar):
13-
states = []
14-
for taxa, data in nexus_obj.data:
15-
c = data[i]
16-
if c in ('?', '-'):
17-
continue # pragma: no cover
18-
elif c not in states:
19-
states.append(c)
20-
21-
if len(states) == 1:
13+
if len({data[i] for _, data in nexus_obj.data if data[i] not in {'?', '-'}}) == 1:
2214
yield i
2315

2416

@@ -61,12 +53,8 @@ def count_site_values(nexus_obj, characters=('-', '?')):
6153
:raises AssertionError: if nexus_obj is not a nexus
6254
:raises NexusFormatException: if nexus_obj does not have a `data` block
6355
"""
64-
if not isinstance(characters, Iterable):
65-
raise TypeError("characters should be iterable")
66-
67-
tally = {}
56+
tally = {taxon: 0 for taxon, _ in nexus_obj.data}
6857
for taxon, sites in nexus_obj.data:
69-
tally[taxon] = tally.get(taxon, 0)
7058
for site in sites:
7159
if site in characters:
7260
tally[taxon] += 1
@@ -172,8 +160,6 @@ def count_binary_set_size(nexus_obj):
172160
:type nexus_obj: NexusReader
173161
174162
:return: A Dictionary
175-
:raises AssertionError: if nexus_obj is not a nexus
176-
:raises NexusFormatException: if nexus_obj does not have a `data` block
177163
178164
e.g. {
179165
0: 0,

src/nexus/writer.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def clean(self, s):
5757

5858
@property
5959
def characters(self):
60-
return self.data.keys()
60+
return list(self.data.keys())
6161

6262
@property
6363
def ntrees(self):
@@ -77,26 +77,23 @@ def symbols(self):
7777
symbols = [s for s in symbols if s not in ('-', '?')]
7878
return symbols
7979

80-
def _make_charlabel_block(self):
80+
def _iter_charlabels(self):
8181
"""Generates a character label block"""
82-
out = ["CHARSTATELABELS"]
82+
yield "CHARSTATELABELS"
8383
for i, char in enumerate(sorted(self.characters), 1):
84-
out.append("\t\t%d %s," % (i, self.clean(str(char))))
85-
out[-1] = out[-1].strip(',') # remove trailing comma
86-
out.append(";")
87-
return "\n".join(out)
84+
yield "\t\t%d %s%s" % (
85+
i, self.clean(str(char)), '' if i == len(self.characters) else ',')
86+
yield ";"
8887

89-
def _make_matrix_block(self, interleave):
88+
def _iter_matrix(self, interleave):
9089
"""Generates a matrix block"""
9190
max_taxon_size = max([len(t) for t in self.taxa]) + 3
9291

93-
out = []
9492
if interleave:
9593
for c in sorted(self.characters):
9694
for t in self.taxa:
97-
out.append("%s %s" % (
98-
t.ljust(max_taxon_size), self.data[c].get(t, self.MISSING)))
99-
out.append("")
95+
yield "%s %s" % (t.ljust(max_taxon_size), self.data[c].get(t, self.MISSING))
96+
yield ""
10097
else:
10198
for t in sorted(self.taxa):
10299
s = []
@@ -105,8 +102,7 @@ def _make_matrix_block(self, interleave):
105102
if len(value) > 1: # wrap equivocal states in ()'s
106103
value = "(%s)" % value
107104
s.append(value)
108-
out.append("%s %s" % (t.ljust(max_taxon_size), ''.join(s)))
109-
return "\n".join(out)
105+
yield "%s %s" % (t.ljust(max_taxon_size), ''.join(s))
110106

111107
def make_treeblock(self):
112108
return "\n".join([" %s" % t.lstrip().strip() for t in self.trees])
@@ -148,9 +144,7 @@ def write(self, interleave=False, charblock=False, **kw):
148144
(basically a wrapper around make_nexus)
149145
150146
:param interleave: Generate interleaved matrix or not
151-
:type interleave: Boolean
152147
:param charblock: Include a characters block or not
153-
:type charblock: Boolean
154148
155149
:return: String
156150
"""
@@ -182,8 +176,8 @@ def make_nexus(self, interleave=False, charblock=False):
182176
datablock = DATA_TEMPLATE % {
183177
'ntax': len(self.taxa),
184178
'nchar': len(self.characters),
185-
'charblock': self._make_charlabel_block() if charblock else '',
186-
'matrix': self._make_matrix_block(interleave=interleave),
179+
'charblock': '\n'.join(self._iter_charlabels()) if charblock else '',
180+
'matrix': '\n'.join(self._iter_matrix(interleave=interleave)),
187181
'interleave': 'INTERLEAVE' if interleave else '',
188182
'comments': self._make_comments(),
189183
'symbols': ''.join(sorted(self.symbols)),

tests/test_checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def test_EmptyCharacterChecker_ignore_ascert():
128128
C 001
129129
;
130130
""")
131-
c = EmptyCharacterChecker(nex, verbose=True)
131+
c = EmptyCharacterChecker(nex)
132132
assert len(c.errors) == 1 # and not two!
133133
assert len(c.messages) == 1
134134

0 commit comments

Comments
 (0)