commonnexus 1.4.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.4.0'
4
+ __version__ = '1.6.0'
@@ -1109,9 +1109,10 @@ class Characters(Block):
1109
1109
  DIMENSIONS NCHAR=3;
1110
1110
  FORMAT DATATYPE=STANDARD MISSING=? GAP=- SYMBOLS="01";
1111
1111
  MATRIX
1112
- t1 100
1113
- t2 010
1114
- t3 001;
1112
+ t1 100
1113
+ t2 010
1114
+ t3 001
1115
+ ;
1115
1116
  END;
1116
1117
 
1117
1118
  :param matrix: A matrix as returned by :meth:`Characters.get_matrix()`, with unlabeled \
@@ -1149,7 +1150,7 @@ class Characters(Block):
1149
1150
  row.append('{{{}}}'.format(''.join(sorted(symbol(c) for c in entry))))
1150
1151
  else:
1151
1152
  row.append(symbol(entry))
1152
- rows.append('\n {} {}'.format(tlabels[taxon].ljust(maxlen), ''.join(row)))
1153
+ rows.append('\n{} {}'.format(tlabels[taxon].ljust(maxlen), ''.join(row)))
1153
1154
 
1154
1155
  symbols = ''.join(sorted([s for s in symbols if s not in [None, GAP]]))
1155
1156
  if missing in symbols or (gap in symbols):
@@ -1176,7 +1177,7 @@ class Characters(Block):
1176
1177
  ) for n, l in charlabels.items())))
1177
1178
  if taxlabels:
1178
1179
  cmds.append(('TAXLABELS', ' '.join(tlabels.values())))
1179
- cmds.append(('MATRIX', ''.join(rows)))
1180
+ cmds.append(('MATRIX', ''.join(rows) + '\n'))
1180
1181
  return cls.from_commands(cmds, nexus=nexus, TITLE=TITLE, ID=ID, LINK=LINK)
1181
1182
 
1182
1183
 
@@ -383,5 +383,5 @@ class Distances(Block):
383
383
  row = [tlabels[taxon].ljust(maxlen)]
384
384
  row.extend(['?' if v is None else str(v) for v in dists.values()])
385
385
  mrows.append(' '.join(row))
386
- cmds.append(('MATRIX', ''.join('\n ' + row for row in mrows)))
386
+ cmds.append(('MATRIX', ''.join('\n' + row for row in mrows) + '\n'))
387
387
  return cls.from_commands(cmds, nexus=nexus, TITLE=TITLE, LINK=LINK, ID=ID)
commonnexus/command.py CHANGED
@@ -27,7 +27,7 @@ class Command(tuple):
27
27
  tokens = []
28
28
  if comment:
29
29
  tokens.extend([Token('\n', TokenType.WHITESPACE), Token(comment, TokenType.COMMENT)])
30
- tokens.append(Token('\n\t' if in_block else '\n', TokenType.WHITESPACE))
30
+ tokens.append(Token('\n', TokenType.WHITESPACE))
31
31
  name = list(iter_tokens(iter(name)))
32
32
  assert len(name) == 1 and name[0].type == TokenType.WORD
33
33
  tokens.extend(name)
@@ -11,10 +11,15 @@ Normalisation includes
11
11
 
12
12
  In addition, after normalisation, the following assumptions hold:
13
13
 
14
- - All commands start on a new line, preceded by "\t" if the command is within a block.
14
+ - All commands start on a new line.
15
15
  - All command names (**not** block names) are in uppercase with no "in-name-comment",
16
16
  like "MA[c]TRiX"
17
+ - The ";" terminating MATRIX commands is on a separate line, allowing more simplistic parsing
18
+ of matrix rows.
17
19
  """
20
+ import typing
21
+ import collections
22
+
18
23
  from commonnexus import Nexus
19
24
  from commonnexus.blocks.characters import Data
20
25
  from commonnexus.blocks import Taxa, Distances, Characters, Trees
@@ -22,12 +27,14 @@ from commonnexus.blocks import Taxa, Distances, Characters, Trees
22
27
 
23
28
  def normalise(nexus: Nexus,
24
29
  data_to_characters: bool = False,
25
- strip_comments: bool = False) -> Nexus:
30
+ strip_comments: bool = False,
31
+ remove_taxa: typing.Optional[typing.Container[str]] = None) -> Nexus:
26
32
  """
27
33
  :param nexus: A `Nexus` object to be normalised in-place.
28
34
  :param data_to_characters: Flag signaling whether DATA blocks should be converted to CHARACTER \
29
35
  blocks.
30
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.
31
38
  :return: The modified `Nexus` object.
32
39
 
33
40
  .. code-block:: python
@@ -67,22 +74,26 @@ def normalise(nexus: Nexus,
67
74
  DIMENSIONS NCHAR=3;
68
75
  FORMAT DATATYPE=STANDARD MISSING=? GAP=- SYMBOLS="01";
69
76
  MATRIX
70
- t1 100
71
- t2 010
72
- t3 001;
77
+ t1 100
78
+ t2 010
79
+ t3 001
80
+ ;
73
81
  END;
74
82
  BEGIN DISTANCES;
75
83
  DIMENSIONS NTAX=3;
76
84
  FORMAT TRIANGLE=BOTH MISSING=?;
77
85
  MATRIX
78
- t1 0 1.0 2.0
79
- t2 1.0 0 3.0
80
- t3 2.0 3.0 0;
86
+ t1 0 1.0 2.0
87
+ t2 1.0 0 3.0
88
+ t3 2.0 3.0 0
89
+ ;
81
90
  END;
82
91
  BEGIN TREES;
83
92
  TREE 1 = (t1,t2,t3);
84
93
  END;
85
94
  """
95
+ remove_taxa = remove_taxa or []
96
+
86
97
  if strip_comments:
87
98
  nexus = Nexus([cmd.without_comments() for cmd in nexus], config=nexus.cfg)
88
99
  nexus = Nexus([cmd.with_normalised_whitespace() for cmd in nexus], config=nexus.cfg)
@@ -91,6 +102,7 @@ def normalise(nexus: Nexus,
91
102
  if nexus.characters:
92
103
  matrix = nexus.characters.get_matrix()
93
104
  taxlabels = list(matrix.keys())
105
+ matrix = collections.OrderedDict((k, v) for k, v in matrix.items() if k not in remove_taxa)
94
106
  characters = nexus.DATA or nexus.CHARACTERS
95
107
  cls = Data if characters.name == 'DATA' and not data_to_characters else Characters
96
108
  nexus.replace_block(
@@ -103,19 +115,26 @@ def normalise(nexus: Nexus,
103
115
  assert set(matrix.keys()).issubset(taxlabels)
104
116
  else:
105
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)
106
121
  nexus.replace_block(nexus.DISTANCES, Distances.from_data(matrix))
107
122
 
108
123
  if nexus.TREES:
109
124
  trees = []
110
125
  for tree in nexus.TREES.trees:
111
126
  nwk = nexus.TREES.translate(tree) if nexus.TREES.TRANSLATE else tree.newick
127
+ if remove_taxa:
128
+ nwk.prune_by_names(remove_taxa)
112
129
  trees.append((tree.name, nwk, tree.rooted))
113
130
  nexus.replace_block(nexus.TREES, Trees.from_data(*trees))
114
131
 
115
132
  if taxlabels:
133
+ taxa = Taxa.from_data([t for t in taxlabels if t not in remove_taxa])
116
134
  if nexus.TAXA:
117
135
  assert nexus.TAXA.DIMENSIONS.ntax == len(taxlabels)
118
136
  assert set(nexus.TAXA.TAXLABELS.labels.values()) == set(taxlabels)
137
+ nexus.replace_block(nexus.TAXA, taxa)
119
138
  else:
120
- nexus.prepend_block(Taxa.from_data(taxlabels))
139
+ nexus.prepend_block(taxa)
121
140
  return nexus
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: commonnexus
3
- Version: 1.4.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,16 +1,16 @@
1
- commonnexus/__init__.py,sha256=5_gCJDmeiZzd1NGnKzB-ZV44Q-JyyUt-jizbL8V1kRI,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
- commonnexus/command.py,sha256=-qPv_WWrQEq_09lC4CmQP4kJHdYoj3zMnSNu7-clMMI,4308
4
+ commonnexus/command.py,sha256=5ZbW3wQY1GO6fChK7aqOzwjKBRxkPlKdUf09JbYEy9c,4284
5
5
  commonnexus/nexus.py,sha256=VFuBOpujAWb551mMCvTmmMPd7A_ciL5Z8u9LyzQoIBE,17585
6
6
  commonnexus/tokenizer.py,sha256=8G2nP-xNN1gBd2a_7qxGDhHloCzoH4YE063p01FLTuk,11459
7
7
  commonnexus/util.py,sha256=WomyVUggYJON5T9uNuIZ_Bxm16UJHIhO737SjKcFYO0,168
8
8
  commonnexus/blocks/__init__.py,sha256=ehKEEc_E62zhlkvbp0p3asY56QkLuhqk7wYd1AqQuKI,602
9
9
  commonnexus/blocks/assumptions.py,sha256=SNURUIUNP86SgTTdfLVTnGQ0FJDnd3I6FTz3jT9A7aw,11215
10
10
  commonnexus/blocks/base.py,sha256=MzEkgBYl0dSIPGuFqZvdROGRr_gEz-HgHBOIQzBmGhI,7988
11
- commonnexus/blocks/characters.py,sha256=rP2bl_c_npvochu883-nVngEESyA8DI0AHb8qqGsGNo,59647
11
+ commonnexus/blocks/characters.py,sha256=VK6T5enMJbG2n-oncbojRE3-q4oPcUmn3A5f_S7XIgQ,59651
12
12
  commonnexus/blocks/codons.py,sha256=Gox96r1Y3y8wmJIP1KhchfAepMC8BZB4Y7O2BTOcybc,7365
13
- commonnexus/blocks/distances.py,sha256=1Oeh2Ad8u2S2FZfupGCqOnmF4hd7iXx9WNGq8DcDHKk,15853
13
+ commonnexus/blocks/distances.py,sha256=fRnV4BDFOL43x22sHIz7HjTwXdwWK9Tb4x8DLS92pLI,15856
14
14
  commonnexus/blocks/notes.py,sha256=Ukx2SNQYWaUSmFlCNE3MiiVpEQn_Bir1CPrYt92xE9M,8333
15
15
  commonnexus/blocks/sets.py,sha256=iLtOgDSAbgkh8JeVi7kdMK4lTuAZMXdZUaE7cU2Mrio,7699
16
16
  commonnexus/blocks/taxa.py,sha256=KU2GnZXBC3LGsJgzr9l48fi_Y-z740QWjr0rsOossWM,3040
@@ -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=G5Gm6a0f_GRXjU2uIwJqLPNHxfcfJfzmJTG_Wz2jGmk,4137
31
- commonnexus-1.4.0.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
32
- commonnexus-1.4.0.dist-info/METADATA,sha256=vEJisLVvA43uIm0uI6x3orjsy1R9S99GK7URo38OPVQ,5328
33
- commonnexus-1.4.0.dist-info/WHEEL,sha256=bb2Ot9scclHKMOLDEHY6B2sicWOgugjFKaJsT7vwMQo,110
34
- commonnexus-1.4.0.dist-info/entry_points.txt,sha256=0CbsOaqe6jaHnG1rpheuIISCZvFp1G1oBgDym82rpAo,58
35
- commonnexus-1.4.0.dist-info/top_level.txt,sha256=SiWuQrmGciG5Ivhrqz6ueU1Sxv-fzudflsMof81-I54,12
36
- commonnexus-1.4.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,,