Back to index

python-biopython  1.60
Public Member Functions | Private Member Functions
Bio.AlignIO.NexusIO.NexusWriter Class Reference
Inheritance diagram for Bio.AlignIO.NexusIO.NexusWriter:
Inheritance graph
[legend]
Collaboration diagram for Bio.AlignIO.NexusIO.NexusWriter:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def write_file
def write_alignment

Private Member Functions

def _classify_alphabet_for_nexus

Detailed Description

Nexus alignment writer.

Note that Nexus files are only expected to hold ONE alignment
matrix.

You are expected to call this class via the Bio.AlignIO.write() or
Bio.SeqIO.write() functions.

Definition at line 60 of file NexusIO.py.


Member Function Documentation

def Bio.AlignIO.NexusIO.NexusWriter._classify_alphabet_for_nexus (   self,
  alphabet 
) [private]
Returns 'protein', 'dna', 'rna' based on the alphabet (PRIVATE).

Raises an exception if this is not possible.

Definition at line 112 of file NexusIO.py.

00112 
00113     def _classify_alphabet_for_nexus(self, alphabet):
00114         """Returns 'protein', 'dna', 'rna' based on the alphabet (PRIVATE).
00115 
00116         Raises an exception if this is not possible."""
00117         #Get the base alphabet (underneath any Gapped or StopCodon encoding)
00118         a = Alphabet._get_base_alphabet(alphabet)
00119 
00120         if not isinstance(a, Alphabet.Alphabet):
00121             raise TypeError("Invalid alphabet")
00122         elif isinstance(a, Alphabet.ProteinAlphabet):
00123             return "protein"
00124         elif isinstance(a, Alphabet.DNAAlphabet):
00125             return "dna"
00126         elif isinstance(a, Alphabet.RNAAlphabet):
00127             return "rna"
00128         else:
00129             #Must be something like NucleotideAlphabet or
00130             #just the generic Alphabet (default for fasta files)
00131             raise ValueError("Need a DNA, RNA or Protein alphabet")

Here is the caller graph for this function:

def Bio.AlignIO.NexusIO.NexusWriter.write_alignment (   self,
  alignment 
)

Definition at line 96 of file NexusIO.py.

00096 
00097     def write_alignment(self, alignment):
00098         #Creates an empty Nexus object, adds the sequences,
00099         #and then gets Nexus to prepare the output.
00100         if len(alignment) == 0:
00101             raise ValueError("Must have at least one sequence")
00102         if alignment.get_alignment_length() == 0:
00103             raise ValueError("Non-empty sequences are required")
00104         minimal_record = "#NEXUS\nbegin data; dimensions ntax=0 nchar=0; " \
00105                          + "format datatype=%s; end;"  \
00106                          % self._classify_alphabet_for_nexus(alignment._alphabet)
00107         n = Nexus.Nexus(minimal_record)
00108         n.alphabet = alignment._alphabet
00109         for record in alignment:
00110             n.add_sequence(record.id, record.seq.tostring())
00111         n.write_nexus_data(self.handle)
    

Here is the call graph for this function:

Here is the caller graph for this function:

def Bio.AlignIO.NexusIO.NexusWriter.write_file (   self,
  alignments 
)
Use this to write an entire file containing the given alignments.

alignments - A list or iterator returning MultipleSeqAlignment objects.
     This should hold ONE and only one alignment.

Definition at line 69 of file NexusIO.py.

00069 
00070     def write_file(self, alignments):
00071         """Use this to write an entire file containing the given alignments.
00072 
00073         alignments - A list or iterator returning MultipleSeqAlignment objects.
00074                      This should hold ONE and only one alignment.
00075         """
00076         align_iter = iter(alignments) #Could have been a list
00077         try:
00078             first_alignment = align_iter.next()
00079         except StopIteration:
00080             first_alignment = None
00081         if first_alignment is None:
00082             #Nothing to write!
00083             return 0
00084         
00085         #Check there is only one alignment...
00086         try:
00087             second_alignment = align_iter.next()
00088         except StopIteration:
00089             second_alignment = None
00090         if second_alignment is not None:
00091             raise ValueError("We can only write one Alignment to a Nexus file.")
00092 
00093         #Good.  Actually write the single alignment,
00094         self.write_alignment(first_alignment)
00095         return 1 #we only support writing one alignment!

Here is the call graph for this function:


The documentation for this class was generated from the following file: