Package Bio :: Module Translate
[hide private]
[frames] | no frames]

Source Code for Module Bio.Translate

  1  """Code to translate DNA or RNA into proteins (OBSOLETE). 
  2   
  3  Instead of Bio.Translate, for translation you are now encouraged to use the 
  4  Seq object's translate method, or the translate function in the Bio.Seq 
  5  module.  Translate-to-stop functionality is via an optional argument. 
  6   
  7  Bio.Seq does not offer any back-translation function like the one here. It 
  8  was concluded that a since a simple back-translation giving a Seq or python 
  9  string could only capture some of the possible back translations, there were 
 10  no practical uses for such a method/function. 
 11   
 12  This module is now considered to be obsolete, and is likely to be deprecated 
 13  in a future release of Biopython, and later removed. 
 14  """ 
 15  from Bio import Alphabet, Seq 
 16  from Bio.Data import CodonTable 
 17   
18 -class Translator:
19 - def __init__(self, table):
20 self.table = table 21 self._encoded = {}
22
23 - def __str__(self) :
24 return "Translator object\n" + str(self.table)
25
26 - def translate(self, seq, stop_symbol = "*"):
27 #Allow different instances of the same class to be used: 28 assert seq.alphabet.__class__ == \ 29 self.table.nucleotide_alphabet.__class__, \ 30 "cannot translate from given alphabet (have %s, need %s)" %\ 31 (seq.alphabet, self.table.nucleotide_alphabet) 32 s = seq.data 33 letters = [] 34 append = letters.append 35 table = self.table 36 get = table.forward_table.get 37 n = len(seq) 38 for i in range(0, n-n%3, 3): 39 append(get(s[i:i+3], stop_symbol)) 40 41 # return with the correct alphabet encoding (cache the encoding) 42 try: 43 alphabet = self._encoded[stop_symbol] 44 except KeyError: 45 alphabet = Alphabet.HasStopCodon(table.protein_alphabet) 46 self._encoded[stop_symbol] = alphabet 47 48 return Seq.Seq("".join(letters), alphabet)
49
50 - def translate_to_stop(self, seq):
51 # This doesn't have a stop encoding 52 53 #Allow different instances of the same class to be used: 54 assert seq.alphabet.__class__ == \ 55 self.table.nucleotide_alphabet.__class__, \ 56 "cannot translate from given alphabet (have %s, need %s)" %\ 57 (seq.alphabet, self.table.nucleotide_alphabet) 58 s = seq.data 59 letters = [] 60 append = letters.append 61 table = self.table.forward_table 62 n = len(seq) 63 try: 64 for i in range(0, n-n%3, 3): 65 append(table[s[i:i+3]]) 66 except KeyError: 67 # Stop at the first codon failure 68 pass 69 return Seq.Seq("".join(letters), self.table.protein_alphabet)
70
71 - def back_translate(self, seq):
72 # includes the stop codon 73 if not isinstance(seq.alphabet, Alphabet.HasStopCodon): 74 return self._back_translate_no_stop(seq) 75 assert seq.alphabet.alphabet == self.table.protein_alphabet, \ 76 "cannot back translate from the given alphabet (%s)" % \ 77 seq.alphabet.alphabet 78 s = seq.data 79 letter = seq.alphabet.stop_symbol 80 letters = [] 81 append = letters.append 82 table = self.table.back_table 83 for c in seq.data: 84 if c == letter: 85 append(table[None]) 86 else: 87 append(table[c]) 88 return Seq.Seq("".join(letters), 89 self.table.nucleotide_alphabet)
90
91 - def _back_translate_no_stop(self, seq):
92 # does not allow a stop codon 93 assert seq.alphabet == self.table.protein_alphabet, \ 94 "cannot back translate from the given alphabet (%s)" % \ 95 seq.alphabet 96 s = seq.data 97 letters = [] 98 append = letters.append 99 table = self.table.back_table 100 for c in seq.data: 101 append(table[c]) 102 return Seq.Seq("".join(letters), 103 self.table.nucleotide_alphabet)
104 105 unambiguous_dna_by_name = {} 106 for key, value in CodonTable.unambiguous_dna_by_name.items(): 107 unambiguous_dna_by_name[key] = Translator(value) 108 unambiguous_dna_by_id = {} 109 for key, value in CodonTable.unambiguous_dna_by_id.items(): 110 unambiguous_dna_by_id[key] = Translator(value) 111 112 unambiguous_rna_by_name = {} 113 for key, value in CodonTable.unambiguous_rna_by_name.items(): 114 unambiguous_rna_by_name[key] = Translator(value) 115 unambiguous_rna_by_id = {} 116 for key, value in CodonTable.unambiguous_rna_by_id.items(): 117 unambiguous_rna_by_id[key] = Translator(value) 118 119 # XXX Ambiguous - can be done the same except for stop codons! 120 ambiguous_dna_by_name = {} 121 for key, value in CodonTable.ambiguous_dna_by_name.items(): 122 ambiguous_dna_by_name[key] = Translator(value) 123 ambiguous_dna_by_id = {} 124 for key, value in CodonTable.ambiguous_dna_by_id.items(): 125 ambiguous_dna_by_id[key] = Translator(value) 126 127 ambiguous_rna_by_name = {} 128 for key, value in CodonTable.ambiguous_rna_by_name.items(): 129 ambiguous_rna_by_name[key] = Translator(value) 130 ambiguous_rna_by_id = {} 131 for key, value in CodonTable.ambiguous_rna_by_id.items(): 132 ambiguous_rna_by_id[key] = Translator(value) 133