Disembl binaries and its dependancies e.g. minimized BioPython distribution and sovgo...
[jabaws.git] / binaries / src / disembl / biopython-1.50 / Bio / utils.py
1 # Copyright 2000 by Andrew Dalke.
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
7 """Miscellaneous functions for dealing with sequences (obsolete?)."""
8
9 import Seq
10 import Alphabet
11
12 from PropertyManager import default_manager
13
14 def translate(seq, id = None):
15     """Translate a sequence (DEPRECATED)."""
16     import warnings
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.",
21                   DeprecationWarning)
22     if id is None:
23         s = "translator"
24     else:
25         s = "translator.id.%d" % id
26     translator = default_manager.resolve(seq.alphabet, s)
27     return translator.translate(seq)
28
29 def translate_to_stop(seq, id = None):
30     """Translate a sequence up to the first in frame stop codon (DEPRECATED)."""
31     import warnings
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.",
36                   DeprecationWarning)
37     if id is None:
38         s = "translator"
39     else:
40         s = "translator.id.%d" % id
41     translator = default_manager.resolve(seq.alphabet, s)
42     return translator.translate_to_stop(seq)
43
44 def back_translate(seq, id = None):
45     """Back-translate a sequence (DEPRECATED)."""
46     import warnings
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.",
50                   DeprecationWarning)
51     if id is None:
52         s = "translator"
53     else:
54         s = "translator.id.%d" % id
55     translator = default_manager.resolve(seq.alphabet, s)
56     return translator.back_translate(seq)
57
58
59 def transcribe(seq):
60     """Transcribe a sequence (DEPRECATED)."""
61     import warnings
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.",
66                   DeprecationWarning)
67     transcriber = default_manager.resolve(seq.alphabet, "transcriber")
68     return transcriber.transcribe(seq)
69
70 def back_transcribe(seq):
71     """Back-transcribe a sequence (DEPRECATED)."""
72     import warnings
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.",
77                   DeprecationWarning)
78     transcriber = default_manager.resolve(seq.alphabet, "transcriber")
79     return transcriber.back_transcribe(seq)
80
81 def ungap(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.
85     gap = seq.gap_char
86     letters = []
87     for c in seq.data:
88         if c != gap:
89             letters.append(c)
90     return Seq.Seq("".join(letters), seq.alphabet.alphabet)
91
92 def verify_alphabet(seq):
93     letters = {}
94     for c in seq.alphabet.letters:
95         letters[c] = 1
96     try:
97         for c in seq.data:
98             letters[c]
99     except KeyError:
100         return 0
101     return 1
102
103 def count_monomers(seq):
104     dict = {}
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)
109     return dict
110
111 def percent_monomers(seq):
112     dict2 = {}
113     seq_len = len(seq)
114     dict = count_monomers(seq)
115     for m in dict:
116         dict2[m] = dict[m] * 100. / seq_len
117     return dict2
118
119 def sum(seq, table, zero = 0.0):
120     total = zero
121     for c in getattr(seq, "data", seq):
122         total = total + table[c]
123     return total
124
125 # For ranged addition
126 def sum_2ple(seq, table, zero = (0.0, 0.0)):
127     x, y = zero
128     data = getattr(seq, "data", seq)
129     for c in data:
130         x2, y2 = table[c]
131         x = x + x2
132         y = y + y2
133     return (x, y)
134
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)
139
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)
144
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
148        Alphabet.Reduced.
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)
159    for letter in seq:
160       new_seq += reduction_table[letter]
161    return new_seq
162
163