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