Concatenate nexus

From Biopython
Revision as of 22:31, 14 May 2009 by Davidw (Talk | contribs)
Jump to: navigation, search

The Problem

It's become increasingly common to make species-level phylogenetic inferences from multiple genes. Demographic processes can cause single gene trees to diverge from species trees so support from multiple genes for the same tree topology is considered stronger evidence than single gene inferences (of course, we still need to test that each gene is telling the same story).

This is usually handled by aligning each gene separately then creating a single "supermatrix" from the individual files. i.e. You need a single alignment containing one row for each taxon where the rows are the concatenated pre-aligned sequences. In NEXUS files (used by PAUP* and Mr Bayes) multiple alignments can be explicitly represented as different' character partitions' within a data matrix that contains one long sequence for each taxon. In this way you can create a supermatrix but still apply different transition models to each gene within in it or run PAUP*'s Partition Homogeneity Test to check for significant difference in the rate/topology of each gene tree.

The Bio.Nexus module makes this relatively straight forward.

The Solution


from Bio.Nexus import Nexus
combined = Nexus.combine(nexi)

That was easy! Lets look at our combined file

begin data;
	dimensions ntax=4 nchar=32;
	format datatype=dna missing=? gap=-;

begin sets;
charset btCOII.nex = 13-24;
charset btCOI.nex = 1-12;
charset btITS.nex = 25-32;


def check_taxa(matrices):  
  everything matches
  first_taxa = matrices[0][1].taxlabels
  for name, matrix in matrices[1:]:
    if first_only:
      raise Nexus.NexusError(msg)
    elif new_only:
      raise Nexus.NexusError(msg)
def concat(file_list, same_taxa=True):
  use  same_taxa=False if you are not concerned by this
  if same_taxa:
    if not check_taxa(nexi): 
      return Nexus.combine(nexi)
    return Nexus.combine(nexi)

And now, using our new functions:

>>> concat(handles)
Traceback (most recent call last):
  File "<stdin>", line 5, in concat
  File "<stdin>", line 16, in check_taxa
Bio.Nexus.Nexus.NexusError: btITS.nex taxa bt4 not in all matrices




The details of the Nexus class are provided in the API Domcumentation

Personal tools