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

Source Code for Module Bio.Translate

  1  """Code to translate DNA or RNA into proteins (DEPRECATED). 
  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 deprecated, and will be removed in a future release of 
 13  Biopython. 
 14  """ 
 15  import warnings 
 16  import Bio 
 17  warnings.warn("Bio.Translate and Bio.Transcribe are deprecated, and will be "\ 
 18                "removed in a future release of Biopython. Please use the "\ 
 19                "functions or object methods defined in Bio.Seq instead "\ 
 20                "(described in the tutorial). If you want to continue to use "\ 
 21                "this code, please get in contact with the Biopython developers "\ 
 22                "via the mailing lists to avoid its permanent removal from " 
 23                +"Biopython.", \ 
 24                Bio.BiopythonDeprecationWarning) 
 25   
 26  from Bio import Alphabet, Seq 
 27  from Bio.Data import CodonTable 
 28   
29 -class Translator:
30 - def __init__(self, table):
31 self.table = table 32 self._encoded = {}
33
34 - def __str__(self):
35 return "Translator object\n" + str(self.table)
36
37 - def translate(self, seq, stop_symbol = "*"):
38 #Allow different instances of the same class to be used: 39 assert seq.alphabet.__class__ == \ 40 self.table.nucleotide_alphabet.__class__, \ 41 "cannot translate from given alphabet (have %s, need %s)" %\ 42 (seq.alphabet, self.table.nucleotide_alphabet) 43 s = seq.data 44 letters = [] 45 append = letters.append 46 table = self.table 47 get = table.forward_table.get 48 n = len(seq) 49 for i in range(0, n-n%3, 3): 50 append(get(s[i:i+3], stop_symbol)) 51 52 # return with the correct alphabet encoding (cache the encoding) 53 try: 54 alphabet = self._encoded[stop_symbol] 55 except KeyError: 56 alphabet = Alphabet.HasStopCodon(table.protein_alphabet, 57 stop_symbol) 58 self._encoded[stop_symbol] = alphabet 59 60 return Seq.Seq("".join(letters), alphabet)
61
62 - def translate_to_stop(self, seq):
63 # This doesn't have a stop encoding 64 65 #Allow different instances of the same class to be used: 66 assert seq.alphabet.__class__ == \ 67 self.table.nucleotide_alphabet.__class__, \ 68 "cannot translate from given alphabet (have %s, need %s)" %\ 69 (seq.alphabet, self.table.nucleotide_alphabet) 70 s = seq.data 71 letters = [] 72 append = letters.append 73 table = self.table.forward_table 74 n = len(seq) 75 try: 76 for i in range(0, n-n%3, 3): 77 append(table[s[i:i+3]]) 78 except KeyError: 79 # Stop at the first codon failure 80 pass 81 return Seq.Seq("".join(letters), self.table.protein_alphabet)
82
83 - def back_translate(self, seq):
84 # includes the stop codon 85 if not isinstance(seq.alphabet, Alphabet.HasStopCodon): 86 return self._back_translate_no_stop(seq) 87 assert seq.alphabet.alphabet == self.table.protein_alphabet, \ 88 "cannot back translate from the given alphabet (%s)" % \ 89 seq.alphabet.alphabet 90 s = seq.data 91 letter = seq.alphabet.stop_symbol 92 letters = [] 93 append = letters.append 94 table = self.table.back_table 95 for c in seq.data: 96 if c == letter: 97 append(table[None]) 98 else: 99 append(table[c]) 100 return Seq.Seq("".join(letters), 101 self.table.nucleotide_alphabet)
102
103 - def _back_translate_no_stop(self, seq):
104 # does not allow a stop codon 105 assert seq.alphabet == self.table.protein_alphabet, \ 106 "cannot back translate from the given alphabet (%s)" % \ 107 seq.alphabet 108 s = seq.data 109 letters = [] 110 append = letters.append 111 table = self.table.back_table 112 for c in seq.data: 113 append(table[c]) 114 return Seq.Seq("".join(letters), 115 self.table.nucleotide_alphabet)
116 117 unambiguous_dna_by_name = {} 118 for key, value in CodonTable.unambiguous_dna_by_name.items(): 119 unambiguous_dna_by_name[key] = Translator(value) 120 unambiguous_dna_by_id = {} 121 for key, value in CodonTable.unambiguous_dna_by_id.items(): 122 unambiguous_dna_by_id[key] = Translator(value) 123 124 unambiguous_rna_by_name = {} 125 for key, value in CodonTable.unambiguous_rna_by_name.items(): 126 unambiguous_rna_by_name[key] = Translator(value) 127 unambiguous_rna_by_id = {} 128 for key, value in CodonTable.unambiguous_rna_by_id.items(): 129 unambiguous_rna_by_id[key] = Translator(value) 130 131 # XXX Ambiguous - can be done the same except for stop codons! 132 ambiguous_dna_by_name = {} 133 for key, value in CodonTable.ambiguous_dna_by_name.items(): 134 ambiguous_dna_by_name[key] = Translator(value) 135 ambiguous_dna_by_id = {} 136 for key, value in CodonTable.ambiguous_dna_by_id.items(): 137 ambiguous_dna_by_id[key] = Translator(value) 138 139 ambiguous_rna_by_name = {} 140 for key, value in CodonTable.ambiguous_rna_by_name.items(): 141 ambiguous_rna_by_name[key] = Translator(value) 142 ambiguous_rna_by_id = {} 143 for key, value in CodonTable.ambiguous_rna_by_id.items(): 144 ambiguous_rna_by_id[key] = Translator(value) 145