1 # Copyright 2000 by Andrew Dalke.
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.
7 """Miscellaneous functions for dealing with sequences (obsolete?)."""
12 from PropertyManager import default_manager
14 def translate(seq, id = None):
15 """Translate a sequence (DEPRECATED)."""
17 warnings.warn("Bio.utils.translate() has been deprecated, and we" \
18 +" intend to remove it in a future release of Biopython."\
19 +" Please use the translate method or function in Bio.Seq"\
20 +" instead, as described in the Tutorial.",
25 s = "translator.id.%d" % id
26 translator = default_manager.resolve(seq.alphabet, s)
27 return translator.translate(seq)
29 def translate_to_stop(seq, id = None):
30 """Translate a sequence up to the first in frame stop codon (DEPRECATED)."""
32 warnings.warn("Bio.utils.translate_to_stop() has been deprecated, and we" \
33 +" intend to remove it in a future release of Biopython."\
34 +" Please use the translate method or function in Bio.Seq"\
35 +" instead, as described in the Tutorial.",
40 s = "translator.id.%d" % id
41 translator = default_manager.resolve(seq.alphabet, s)
42 return translator.translate_to_stop(seq)
44 def back_translate(seq, id = None):
45 """Back-translate a sequence (DEPRECATED)."""
47 warnings.warn("Bio.utils.back_translate() has been deprecated, and we" \
48 +" intend to remove it in a future release of Biopython."\
49 +" If you use it, please tell us on the mailing list.",
54 s = "translator.id.%d" % id
55 translator = default_manager.resolve(seq.alphabet, s)
56 return translator.back_translate(seq)
60 """Transcribe a sequence (DEPRECATED)."""
62 warnings.warn("Bio.utils.transcribe() has been deprecated, and we" \
63 +" intend to remove it in a future release of Biopython."\
64 +" Please use the transcribe method or function in"\
65 +" Bio.Seq instead, as described in the Tutorial.",
67 transcriber = default_manager.resolve(seq.alphabet, "transcriber")
68 return transcriber.transcribe(seq)
70 def back_transcribe(seq):
71 """Back-transcribe a sequence (DEPRECATED)."""
73 warnings.warn("Bio.utils.back_transcribe() has been deprecated, and we" \
74 +" intend to remove it in a future release of Biopython."\
75 +" Please use the back_transcribe method or function in"\
76 +" Bio.Seq instead, as described in the Tutorial.",
78 transcriber = default_manager.resolve(seq.alphabet, "transcriber")
79 return transcriber.back_transcribe(seq)
82 """given a sequence with gap encoding, return the ungapped sequence"""
83 #TODO - Fix this? It currently assumes the outmost AlphabetEncoder
84 #is for the gap. Consider HasStopCodon(Gapped(Protein())) as a test case.
90 return Seq.Seq("".join(letters), seq.alphabet.alphabet)
92 def verify_alphabet(seq):
94 for c in seq.alphabet.letters:
103 def count_monomers(seq):
105 # bugfix: string.count(s,c) raises an AttributeError. Iddo Friedberg 16 Mar. 04
106 # s = buffer(seq.data) # works for strings and array.arrays
107 for c in seq.alphabet.letters:
108 dict[c] = seq.data.count(c)
111 def percent_monomers(seq):
114 dict = count_monomers(seq)
116 dict2[m] = dict[m] * 100. / seq_len
119 def sum(seq, table, zero = 0.0):
121 for c in getattr(seq, "data", seq):
122 total = total + table[c]
125 # For ranged addition
126 def sum_2ple(seq, table, zero = (0.0, 0.0)):
128 data = getattr(seq, "data", seq)
135 def total_weight(seq, weight_table = None):
136 if weight_table is None:
137 weight_table = default_manager.resolve(seq.alphabet, "weight_table")
138 return sum(seq, weight_table)
140 def total_weight_range(seq, weight_table = None):
141 if weight_table is None:
142 weight_table = default_manager.resolve(seq.alphabet, "weight_range_table")
143 return sum_2ple(seq, weight_table)
145 def reduce_sequence(seq, reduction_table,new_alphabet=None):
146 """ given an amino-acid sequence, return it in reduced alphabet form based
147 on the letter-translation table passed. Some "standard" tables are in
149 seq: a Seq.Seq type sequence
150 reduction_table: a dictionary whose keys are the "from" alphabet, and values
151 are the "to" alphabet"""
152 if new_alphabet is None:
153 new_alphabet = Alphabet.single_letter_alphabet
154 new_alphabet.letters = ''
155 for letter in reduction_table:
156 new_alphabet.letters += letter
157 new_alphabet.size = len(new_alphabet.letters)
158 new_seq = Seq.Seq('',new_alphabet)
160 new_seq += reduction_table[letter]