class NexusSets

A container for Nexus CharSet, CharPartition, and TaxSet objects.

When the first Nexus sets block is read, a NexusSets object is made and saved as var.nexusSets. CharSet, TaxSet, and CharPartition objects are placed in it, as they are read/created. TaxPartition commands are not implemented. Here is a simple nexus sets block that only has charsets:


begin sets;
  charset pos1 = 1-.\3;
  charset pos2 = 2-.\3;
  charset pos3 = 3-.\3;

To get the third positions only, you could say:

a = var.alignments[0]
read('mySets.nex')       # the sets block above
b = a.subsetUsingCharSet('pos3')

What happens above when the mySets.nex file is read is that a NexusSets object is created as var.nexusSets and populated with the three charsets as CharSet objects. Then when you asked for a subset, a copy of that NexusSets object was made and applied to the alignment.

Notice that the length of the alignment is not part of the information in the sets block, and so things remain undefined in var.nexusSets until the nexus sets are applied to a particular alignment. One consequence of this somewhat awkward system is that the same charsets could then be applied to another alignment of a different size:

aA = var.alignments[0]
aB = var.alignments[1]
read('mySets.nex')       # the sets block above
bA = aA.subsetUsingCharSet('pos3')
bB = aB.subsetUsingCharSet('pos3')

In the above example, bA.nexusSets and bB.nexusSets are both derived from var.nexusSets but are independent of it, and different from each other.

So when an Alignment (or Tree object) wants to use var.nexusSets, it makes a copy of it, and attaches the copy as theAlignment.nexusSets or theTree.nexusSets

Here is another example, including a charPartition definition:

begin sets;
  charset gene1 = 1-213;
  charset gene2 = 214-497;
  charPartition cpName = gene1:gene1, gene2:gene2;

For an alignment, you can then set a character partition by


Do this before you make a Data object, to partition the alignment.

You can also use charsets to extract subsets, eg via:

b = a.subsetUsingCharSet(csName)

Setting a charPartition or asking for a subset will trigger applying var.nexusSets to the alignment, but you can also do it explicitly, by:


NexusSets knows about predefined ‘constant’, ‘gapped’, and ‘remainder’ charsets. It does not know about ‘missambig’ or ‘uninf’ charsets.

NexusSets can either be in the default standard format or in vector format – you can change them to vector format with the


method, and you can change them to standard format with the


method. For taxSets, you can use actual tax names (rather than numbers or ranges) by invoking the method:


which sets the attribute ‘useTaxNames’ to True, and puts the taxNames for the taxSet in the


list, which might be handy.

You can see the current state of a NexusSets object using


It can also be written out as a nexus sets block. If an Alignment object has a nexusSets attribute then if you ask the alignment to write itself to a nexus file then the Alignment.nexusSets is also written. If you would rather it not be written, delete it first. If you would rather it be written to a separate file, do that first and then delete it.

One nice thing about taxsets is that and Tree.Tree.btv() know about them and can display them.

dupeCharSet(existingCharSetName, newName)
newCharSet(name, mask=None)

Write self in Nexus format to stdout.


Write self in Nexus format to stdout or a file.


This only writes non-trivial stuff.

Ie if self has only constant and gapped charsets, then it does not write anything.