commonnexus 1.5.0__py2.py3-none-any.whl → 1.6.0__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
commonnexus/__init__.py CHANGED
@@ -1,4 +1,4 @@
1
1
  from .nexus import Nexus, Config # noqa: F401
2
2
  from commonnexus.blocks import Block # noqa: F401
3
3
 
4
- __version__ = '1.5.0'
4
+ __version__ = '1.6.0'
@@ -17,6 +17,9 @@ In addition, after normalisation, the following assumptions hold:
17
17
  - The ";" terminating MATRIX commands is on a separate line, allowing more simplistic parsing
18
18
  of matrix rows.
19
19
  """
20
+ import typing
21
+ import collections
22
+
20
23
  from commonnexus import Nexus
21
24
  from commonnexus.blocks.characters import Data
22
25
  from commonnexus.blocks import Taxa, Distances, Characters, Trees
@@ -24,12 +27,14 @@ from commonnexus.blocks import Taxa, Distances, Characters, Trees
24
27
 
25
28
  def normalise(nexus: Nexus,
26
29
  data_to_characters: bool = False,
27
- strip_comments: bool = False) -> Nexus:
30
+ strip_comments: bool = False,
31
+ remove_taxa: typing.Optional[typing.Container[str]] = None) -> Nexus:
28
32
  """
29
33
  :param nexus: A `Nexus` object to be normalised in-place.
30
34
  :param data_to_characters: Flag signaling whether DATA blocks should be converted to CHARACTER \
31
35
  blocks.
32
36
  :param strip_comments: Flag signaling whether to remove all non-command comments.
37
+ :param remove_taxa: Container of taxon labels specifying taxa to remove from relevant blocks.
33
38
  :return: The modified `Nexus` object.
34
39
 
35
40
  .. code-block:: python
@@ -87,6 +92,8 @@ def normalise(nexus: Nexus,
87
92
  TREE 1 = (t1,t2,t3);
88
93
  END;
89
94
  """
95
+ remove_taxa = remove_taxa or []
96
+
90
97
  if strip_comments:
91
98
  nexus = Nexus([cmd.without_comments() for cmd in nexus], config=nexus.cfg)
92
99
  nexus = Nexus([cmd.with_normalised_whitespace() for cmd in nexus], config=nexus.cfg)
@@ -95,6 +102,7 @@ def normalise(nexus: Nexus,
95
102
  if nexus.characters:
96
103
  matrix = nexus.characters.get_matrix()
97
104
  taxlabels = list(matrix.keys())
105
+ matrix = collections.OrderedDict((k, v) for k, v in matrix.items() if k not in remove_taxa)
98
106
  characters = nexus.DATA or nexus.CHARACTERS
99
107
  cls = Data if characters.name == 'DATA' and not data_to_characters else Characters
100
108
  nexus.replace_block(
@@ -107,19 +115,26 @@ def normalise(nexus: Nexus,
107
115
  assert set(matrix.keys()).issubset(taxlabels)
108
116
  else:
109
117
  taxlabels = list(matrix.keys())
118
+ matrix = collections.OrderedDict(
119
+ (k, collections.OrderedDict((kk, vv) for kk, vv in v.items() if kk not in remove_taxa))
120
+ for k, v in matrix.items() if k not in remove_taxa)
110
121
  nexus.replace_block(nexus.DISTANCES, Distances.from_data(matrix))
111
122
 
112
123
  if nexus.TREES:
113
124
  trees = []
114
125
  for tree in nexus.TREES.trees:
115
126
  nwk = nexus.TREES.translate(tree) if nexus.TREES.TRANSLATE else tree.newick
127
+ if remove_taxa:
128
+ nwk.prune_by_names(remove_taxa)
116
129
  trees.append((tree.name, nwk, tree.rooted))
117
130
  nexus.replace_block(nexus.TREES, Trees.from_data(*trees))
118
131
 
119
132
  if taxlabels:
133
+ taxa = Taxa.from_data([t for t in taxlabels if t not in remove_taxa])
120
134
  if nexus.TAXA:
121
135
  assert nexus.TAXA.DIMENSIONS.ntax == len(taxlabels)
122
136
  assert set(nexus.TAXA.TAXLABELS.labels.values()) == set(taxlabels)
137
+ nexus.replace_block(nexus.TAXA, taxa)
123
138
  else:
124
- nexus.prepend_block(Taxa.from_data(taxlabels))
139
+ nexus.prepend_block(taxa)
125
140
  return nexus
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: commonnexus
3
- Version: 1.5.0
3
+ Version: 1.6.0
4
4
  Summary: A nexus (phylogenetics) file reader and writer (.nex, .trees)
5
5
  Home-page: https://github.com/dlce-eva/commonnexus
6
6
  Author: Robert Forkel
@@ -1,4 +1,4 @@
1
- commonnexus/__init__.py,sha256=gGMpj_J8cxerva1vQVbfuUW6mbo-MNm99ACRYygHg9w,121
1
+ commonnexus/__init__.py,sha256=Tr84gK3MelAO7U__8dpEsLEXy_JmiX9NmCvX1wg8gzw,121
2
2
  commonnexus/__main__.py,sha256=k07DsoCHCRw2Wi0HzXCmvNCSt8cddDEGCBlnHZorm6k,4361
3
3
  commonnexus/cli_util.py,sha256=pmqd3Fs2Wef7hRf0Z6koZ1Bg6-16FZUMOYZ8cMTY5Ig,3710
4
4
  commonnexus/command.py,sha256=5ZbW3wQY1GO6fChK7aqOzwjKBRxkPlKdUf09JbYEy9c,4284
@@ -27,10 +27,10 @@ commonnexus/commands/trees.py,sha256=aMN9WPvzOXfNgPcLvwmkJHtyvjq3LWWo2rpbGFCyczs
27
27
  commonnexus/tools/__init__.py,sha256=X8dv4VrjAo5C8VJdUXMrKviDLUrwXbawE5SJiQ--2uQ,213
28
28
  commonnexus/tools/combine.py,sha256=CZ0JsJa8azPv6XWuUAHLGOMI0N8EYPY11ipJfTCiRUs,3061
29
29
  commonnexus/tools/matrix.py,sha256=3e2n_beug3MZyzXDgawjxxh1QfiM7KVp757OepCjQR0,11492
30
- commonnexus/tools/normalise.py,sha256=EOUi-w25X_ZZI6iuAU6Zat6UoA4mNlRJli0ESnmDBSQ,4192
31
- commonnexus-1.5.0.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
32
- commonnexus-1.5.0.dist-info/METADATA,sha256=nQ1FBxFrIZQ10T_bOFxEci_zK8nWWeHnrJLp3hCUHaI,5328
33
- commonnexus-1.5.0.dist-info/WHEEL,sha256=bb2Ot9scclHKMOLDEHY6B2sicWOgugjFKaJsT7vwMQo,110
34
- commonnexus-1.5.0.dist-info/entry_points.txt,sha256=0CbsOaqe6jaHnG1rpheuIISCZvFp1G1oBgDym82rpAo,58
35
- commonnexus-1.5.0.dist-info/top_level.txt,sha256=SiWuQrmGciG5Ivhrqz6ueU1Sxv-fzudflsMof81-I54,12
36
- commonnexus-1.5.0.dist-info/RECORD,,
30
+ commonnexus/tools/normalise.py,sha256=l7MNBkFQk0hyl2IA33AvRFS0mjAZwDYumpfw8pzzMa0,4924
31
+ commonnexus-1.6.0.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
32
+ commonnexus-1.6.0.dist-info/METADATA,sha256=dRy1hWD10vl597dRpdzmI4OiSs0FkqE61KDC9ClZF5E,5328
33
+ commonnexus-1.6.0.dist-info/WHEEL,sha256=bb2Ot9scclHKMOLDEHY6B2sicWOgugjFKaJsT7vwMQo,110
34
+ commonnexus-1.6.0.dist-info/entry_points.txt,sha256=0CbsOaqe6jaHnG1rpheuIISCZvFp1G1oBgDym82rpAo,58
35
+ commonnexus-1.6.0.dist-info/top_level.txt,sha256=SiWuQrmGciG5Ivhrqz6ueU1Sxv-fzudflsMof81-I54,12
36
+ commonnexus-1.6.0.dist-info/RECORD,,