33from collections .abc import Iterable
44
55from 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
7350def 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 ]
0 commit comments