Package Bio :: Package CodonAlign :: Module CodonAlphabet'
[hide private]
[frames] | no frames]

Source Code for Module Bio.CodonAlign.CodonAlphabet'

 1  # Copyright 2013 by Zheng Ruan (zruan1991@gmail.com). 
 2  # All rights reserved. 
 3  # This code is part of the Biopython distribution and governed by its 
 4  # license.  Please see the LICENSE file that should have been included 
 5  # as part of this package. 
 6  """Code for Codon Alphabet. 
 7   
 8  CodonAlphabet class is interited from Alphabet class. It is an 
 9  alphabet for CodonSeq class. 
10   
11  """ 
12  __docformat__ = "epytext en"  # Don't just use plain text in epydoc API pages! 
13   
14   
15  import copy 
16  try: 
17      from itertools import izip 
18  except ImportError: 
19      izip = zip 
20   
21  from Bio.Seq import Seq 
22  from Bio.SeqRecord import SeqRecord 
23  from Bio.Alphabet import IUPAC, Gapped, HasStopCodon, Alphabet, generic_dna 
24  from Bio.Data.CodonTable import generic_by_id 
25   
26   
27  default_codon_table = copy.deepcopy(generic_by_id[1]) 
28   
29 -def get_codon_alphabet(alphabet, gap="-", stop="*"):
30 """function to get alignment alphabet for codon alignment. Only 31 nucleotide alphabet is accepted. Raise an error when the type of 32 alphabet is incompatible. 33 """ 34 from Bio.Alphabet import NucleotideAlphabet 35 if isinstance(alphabet, NucleotideAlphabet): 36 alpha = alphabet 37 if gap: 38 alpha = Gapped(alpha, gap_char=gap) 39 if stop: 40 alpha = HasStopCodon(alpha, stop_symbol=stop) 41 else: 42 raise TypeError("Only Nuclteotide Alphabet is accepted!") 43 return alpha
44 45 default_alphabet = get_codon_alphabet(IUPAC.unambiguous_dna) 46
47 -class CodonAlphabet(Alphabet):
48 """Generic Codon Alphabet with a size of three""" 49 size = 3 50 letters = None 51 name = ''
52 - def __repr__(self):
53 return "%s(%s)" % (self.__class__.__name__, self.names[0])
54 55
56 -def get_codon_alphabet(codon_table, gap_char="-"):
57 letters = list(codon_table.forward_table.keys()) 58 letters.extend(codon_table.stop_codons) 59 letters.extend(codon_table.start_codons) 60 if gap_char: 61 letters.append(gap_char*3) 62 generic_codon_alphabet = CodonAlphabet() 63 generic_codon_alphabet.letters = letters 64 generic_codon_alphabet.gap_char = '-' 65 generic_codon_alphabet.names = codon_table.names 66 return generic_codon_alphabet
67 68 default_codon_alphabet = get_codon_alphabet(default_codon_table) 69 70 71 if __name__ == "__main__": 72 from Bio._utils import run_doctest 73 run_doctest() 74