Skip to content

Commit 5503661

Browse files
committed
removed excessive input checking
1 parent fb972c8 commit 5503661

15 files changed

Lines changed: 19 additions & 163 deletions

src/nexus/commands/edit.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
from nexus.cli_util import add_nexus, get_reader, add_output, write_output, list_of_ranges
77
from nexus.tools import count_site_values
88
from nexus.tools import check_zeros
9-
from nexus.tools import find_constant_sites
10-
from nexus.tools import find_unique_sites
9+
from nexus.tools import iter_constant_sites
10+
from nexus.tools import iter_unique_sites
1111
from nexus.tools import new_nexus_without_sites
1212

1313

@@ -59,11 +59,11 @@ def run(args):
5959

6060
const, unique, zeros, remove = [], [], [], []
6161
if args.constant:
62-
const = find_constant_sites(nexus)
62+
const = list(iter_constant_sites(nexus))
6363
if const:
6464
args.log.info("Constant Sites: %s" % ",".join([str(i + 1) for i in const]))
6565
if args.unique:
66-
unique = find_unique_sites(nexus)
66+
unique = list(iter_unique_sites(nexus))
6767
if unique:
6868
args.log.info("Unique Sites: %s" % ",".join([str(i + 1) for i in unique]))
6969
if args.zeros:

src/nexus/tools/__init__.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
# tools
2-
from nexus.tools.check_for_valid_NexusReader import check_for_valid_NexusReader
32
from nexus.tools.check_zeros import check_zeros, remove_zeros
43
from nexus.tools.combine_nexuses import combine_nexuses
54
from nexus.tools.shufflenexus import shufflenexus
6-
from nexus.tools.sites import find_constant_sites
7-
from nexus.tools.sites import find_unique_sites
5+
from nexus.tools.sites import iter_constant_sites
6+
from nexus.tools.sites import iter_unique_sites
87
from nexus.tools.sites import count_site_values
98
from nexus.tools.sites import new_nexus_without_sites
109
from nexus.tools.sites import tally_by_site
1110
from nexus.tools.sites import tally_by_taxon
1211
from nexus.tools.sites import count_binary_set_size
1312

1413
__all__ = [
15-
"check_for_valid_NexusReader",
1614
"binarise",
1715
"multistatise",
1816
"combine_nexuses",
1917
"shufflenexus",
20-
"find_constant_sites",
21-
"find_unique_sites",
18+
"iter_constant_sites",
19+
"iter_unique_sites",
2220
"count_site_values",
2321
"new_nexus_without_sites",
2422
"tally_by_site",

src/nexus/tools/binarise.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from nexus.writer import NexusWriter
2-
from nexus.tools.check_for_valid_NexusReader import check_for_valid_NexusReader
32

43

54
def _recode_to_binary(char, keep_zero=False):
@@ -79,7 +78,6 @@ def binarise(nexus_obj, keep_zero=False):
7978
:raises AssertionError: if nexus_obj is not a nexus
8079
:raises NexusFormatException: if nexus_obj does not have a `data` block
8180
"""
82-
check_for_valid_NexusReader(nexus_obj, required_blocks=['data'])
8381
n = NexusWriter()
8482

8583
for i in sorted(nexus_obj.data.charlabels):

src/nexus/tools/check_for_valid_NexusReader.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/nexus/tools/check_zeros.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import collections
22

3-
from nexus.tools.check_for_valid_NexusReader import check_for_valid_NexusReader
43
from nexus.tools.sites import new_nexus_without_sites
54

65

@@ -28,8 +27,6 @@ def check_zeros(nexus_obj, absences=None, missing=None):
2827
`char` dictionary is not a string (i.e.
2928
integer or None values)
3029
"""
31-
check_for_valid_NexusReader(nexus_obj, required_blocks=['data'])
32-
3330
absences = absences if absences else ['0']
3431
missing = missing if missing else ['-', '?']
3532

src/nexus/tools/combine_nexuses.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
22

33
from nexus.writer import NexusWriter
4-
from nexus.tools.check_for_valid_NexusReader import check_for_valid_NexusReader
54

65

76
def combine_nexuses(nexuslist):
@@ -21,7 +20,6 @@ def combine_nexuses(nexuslist):
2120
# check they're all nexus instances and get all block types
2221
blocks = set()
2322
for nex in nexuslist:
24-
check_for_valid_NexusReader(nex)
2523
blocks.update(list(nex.blocks))
2624

2725
for block in blocks:
@@ -36,15 +34,13 @@ def combine_nexuses(nexuslist):
3634

3735
def combine_treeblocks(out, nexuslist):
3836
for nex in nexuslist:
39-
check_for_valid_NexusReader(nex, required_blocks=['trees'])
4037
out.trees.extend(nex.trees.trees)
4138
return out
4239

4340

4441
def combine_datablocks(out, nexuslist):
4542
charpos = 0
4643
for nex_id, nex in enumerate(nexuslist, 1):
47-
check_for_valid_NexusReader(nex, required_blocks=['data'])
4844
if nex.short_filename:
4945
nexus_label = os.path.splitext(nex.short_filename)[0]
5046
else:

src/nexus/tools/multistatise.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from nexus.writer import NexusWriter
2-
from nexus.tools.check_for_valid_NexusReader import check_for_valid_NexusReader
32

43

54
def multistatise(nexus_obj, charlabel=None):
@@ -13,8 +12,6 @@ def multistatise(nexus_obj, charlabel=None):
1312
:raises AssertionError: if nexus_obj is not a nexus
1413
:raises NexusFormatException: if nexus_obj does not have a `data` block
1514
"""
16-
check_for_valid_NexusReader(nexus_obj, required_blocks=['data'])
17-
1815
if not charlabel:
1916
charlabel = getattr(nexus_obj, 'short_filename', 1)
2017

src/nexus/tools/shufflenexus.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import random
22

33
from nexus.writer import NexusWriter
4-
from nexus.tools.check_for_valid_NexusReader import check_for_valid_NexusReader
54

65

76
def shufflenexus(nexus_obj, resample=False):
@@ -21,8 +20,6 @@ def shufflenexus(nexus_obj, resample=False):
2120
:raises ValueError: if resample is not False or a positive Integer
2221
:raises NexusFormatException: if nexus_obj does not have a `data` block
2322
"""
24-
check_for_valid_NexusReader(nexus_obj, required_blocks=['data'])
25-
2623
if resample is False:
2724
resample = nexus_obj.data.nchar
2825

src/nexus/tools/sites.py

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,12 @@
33
from collections.abc import Iterable
44

55
from nexus.writer import NexusWriter
6-
from nexus.tools.check_for_valid_NexusReader import check_for_valid_NexusReader
76

87

9-
def find_constant_sites(nexus_obj):
8+
def iter_constant_sites(nexus_obj):
109
"""
11-
Returns a list of the constant sites in a nexus
12-
13-
:param nexus_obj: A `NexusReader` instance
14-
:type nexus_obj: NexusReader
15-
16-
:return: A list of constant site positions.
17-
:raises AssertionError: if nexus_obj is not a nexus
18-
:raises NexusFormatException: if nexus_obj does not have a `data` block
10+
Returns a list of zero-based indices of the constant sites in a nexus
1911
"""
20-
check_for_valid_NexusReader(nexus_obj, required_blocks=['data'])
21-
22-
const = []
2312
for i in range(0, nexus_obj.data.nchar):
2413
states = []
2514
for taxa, data in nexus_obj.data:
@@ -30,26 +19,15 @@ def find_constant_sites(nexus_obj):
3019
states.append(c)
3120

3221
if len(states) == 1:
33-
const.append(i)
34-
return const
22+
yield i
3523

3624

37-
def find_unique_sites(nexus_obj):
25+
def iter_unique_sites(nexus_obj):
3826
"""
3927
Returns a list of the unique sites in a binary nexus
4028
i.e. sites with only one taxon belonging to them.
4129
(this only really makes sense if the data is coded as presence/absence)
42-
43-
:param nexus_obj: A `NexusReader` instance
44-
:type nexus_obj: NexusReader
45-
46-
:return: A list of unique site positions.
47-
:raises AssertionError: if nexus_obj is not a nexus
48-
:raises NexusFormatException: if nexus_obj does not have a `data` block
4930
"""
50-
check_for_valid_NexusReader(nexus_obj, required_blocks=['data'])
51-
52-
unique = []
5331
for i in range(0, nexus_obj.data.nchar):
5432
members = Counter()
5533
missing = 0
@@ -66,8 +44,7 @@ def find_unique_sites(nexus_obj):
6644
if len(members) == 2:
6745
for state, count in members.items():
6846
if state != '0' and count == 1:
69-
unique.append(i)
70-
return unique
47+
yield i
7148

7249

7350
def count_site_values(nexus_obj, characters=('-', '?')):
@@ -87,8 +64,6 @@ def count_site_values(nexus_obj, characters=('-', '?')):
8764
if not isinstance(characters, Iterable):
8865
raise TypeError("characters should be iterable")
8966

90-
check_for_valid_NexusReader(nexus_obj, required_blocks=['data'])
91-
9267
tally = {}
9368
for taxon, sites in nexus_obj.data:
9469
tally[taxon] = tally.get(taxon, 0)
@@ -113,8 +88,6 @@ def new_nexus_without_sites(nexus_obj, sites_to_remove):
11388
:raises AssertionError: if nexus_obj is not a nexus
11489
:raises NexusFormatException: if nexus_obj does not have a `data` block
11590
"""
116-
check_for_valid_NexusReader(nexus_obj, required_blocks=['data'])
117-
11891
# make new nexus
11992
nexout = NexusWriter()
12093
nexout.add_comment(
@@ -150,7 +123,6 @@ def tally_by_site(nexus_obj):
150123
'site2': {'state1': ['taxon2'], 'state0': ['taxon1', 'taxon3'], }
151124
}
152125
"""
153-
check_for_valid_NexusReader(nexus_obj, required_blocks=['data'])
154126
tally = {}
155127
for site, data in nexus_obj.data.characters.items():
156128
tally[site] = tally.get(site, {})
@@ -179,7 +151,6 @@ def tally_by_taxon(nexus_obj):
179151
'taxon2': {'state1': ['site2'], 'state0': ['site1', 'site3'], }
180152
}
181153
"""
182-
check_for_valid_NexusReader(nexus_obj, required_blocks=['data'])
183154
tally = {}
184155
for taxon, characters in nexus_obj.data:
185156
tally[taxon] = {}
@@ -210,7 +181,6 @@ def count_binary_set_size(nexus_obj):
210181
2: 20,
211182
}
212183
"""
213-
check_for_valid_NexusReader(nexus_obj, required_blocks=['data'])
214184
tally = Counter()
215185
for char_id in nexus_obj.data.characters:
216186
char = nexus_obj.data.characters[char_id]

tests/test_tools/test_check_for_valid_NexusReader.py

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)