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

Source Code for Module Bio.utils

  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 string 
 10  import Seq 
 11  import Alphabet 
 12   
 13  from PropertyManager import default_manager 
 14   
15 -def translate(seq, id = None):
16 """Translate a sequence (DEPRECATED).""" 17 import warnings 18 warnings.warn("Bio.utils.translate() has been deprecated, and we" \ 19 +" intend to remove it in a future release of Biopython."\ 20 +" Please use the translate method or function in Bio.Seq"\ 21 +" instead, as described in the Tutorial.", 22 DeprecationWarning) 23 if id is None: 24 s = "translator" 25 else: 26 s = "translator.id.%d" % id 27 translator = default_manager.resolve(seq.alphabet, s) 28 return translator.translate(seq)
29
30 -def translate_to_stop(seq, id = None):
31 """Translate a sequence up to the first in frame stop codon (DEPRECATED).""" 32 import warnings 33 warnings.warn("Bio.utils.translate_to_stop() has been deprecated, and we" \ 34 +" intend to remove it in a future release of Biopython."\ 35 +" Please use the translate method or function in Bio.Seq"\ 36 +" instead, as described in the Tutorial.", 37 DeprecationWarning) 38 if id is None: 39 s = "translator" 40 else: 41 s = "translator.id.%d" % id 42 translator = default_manager.resolve(seq.alphabet, s) 43 return translator.translate_to_stop(seq)
44
45 -def back_translate(seq, id = None):
46 """Back-translate a sequence (DEPRECATED).""" 47 import warnings 48 warnings.warn("Bio.utils.back_translate() has been deprecated, and we" \ 49 +" intend to remove it in a future release of Biopython."\ 50 +" If you use it, please tell us on the mailing list.", 51 DeprecationWarning) 52 if id is None: 53 s = "translator" 54 else: 55 s = "translator.id.%d" % id 56 translator = default_manager.resolve(seq.alphabet, s) 57 return translator.back_translate(seq)
58 59
60 -def transcribe(seq):
61 """Transcribe a sequence (DEPRECATED).""" 62 import warnings 63 warnings.warn("Bio.utils.transcribe() has been deprecated, and we" \ 64 +" intend to remove it in a future release of Biopython."\ 65 +" Please use the transcribe method or function in"\ 66 +" Bio.Seq instead, as described in the Tutorial.", 67 DeprecationWarning) 68 transcriber = default_manager.resolve(seq.alphabet, "transcriber") 69 return transcriber.transcribe(seq)
70
71 -def back_transcribe(seq):
72 """Back-transcribe a sequence (DEPRECATED).""" 73 import warnings 74 warnings.warn("Bio.utils.back_transcribe() has been deprecated, and we" \ 75 +" intend to remove it in a future release of Biopython."\ 76 +" Please use the back_transcribe method or function in"\ 77 +" Bio.Seq instead, as described in the Tutorial.", 78 DeprecationWarning) 79 transcriber = default_manager.resolve(seq.alphabet, "transcriber") 80 return transcriber.back_transcribe(seq)
81
82 -def ungap(seq):
83 """given a sequence with gap encoding, return the ungapped sequence""" 84 #TODO - Fix this? It currently assumes the outmost AlphabetEncoder 85 #is for the gap. Consider HasStopCodon(Gapped(Protein())) as a test case. 86 gap = seq.gap_char 87 letters = [] 88 for c in seq.data: 89 if c != gap: 90 letters.append(c) 91 return Seq.Seq(string.join(letters, ""), seq.alphabet.alphabet)
92
93 -def verify_alphabet(seq):
94 letters = {} 95 for c in seq.alphabet.letters: 96 letters[c] = 1 97 try: 98 for c in seq.data: 99 letters[c] 100 except KeyError: 101 return 0 102 return 1
103
104 -def count_monomers(seq):
105 dict = {} 106 # bugfix: string.count(s,c) raises an AttributeError. Iddo Friedberg 16 Mar. 04 107 # s = buffer(seq.data) # works for strings and array.arrays 108 for c in seq.alphabet.letters: 109 dict[c] = string.count(seq.data, c) 110 return dict
111
112 -def percent_monomers(seq):
113 dict2 = {} 114 seq_len = len(seq) 115 dict = count_monomers(seq) 116 for m in dict: 117 dict2[m] = dict[m] * 100. / seq_len 118 return dict2
119
120 -def sum(seq, table, zero = 0.0):
121 total = zero 122 for c in getattr(seq, "data", seq): 123 total = total + table[c] 124 return total
125 126 # For ranged addition
127 -def sum_2ple(seq, table, zero = (0.0, 0.0)):
128 x, y = zero 129 data = getattr(seq, "data", seq) 130 for c in data: 131 x2, y2 = table[c] 132 x = x + x2 133 y = y + y2 134 return (x, y)
135
136 -def total_weight(seq, weight_table = None):
137 if weight_table is None: 138 weight_table = default_manager.resolve(seq.alphabet, "weight_table") 139 return sum(seq, weight_table)
140
141 -def total_weight_range(seq, weight_table = None):
142 if weight_table is None: 143 weight_table = default_manager.resolve(seq.alphabet, "weight_range_table") 144 return sum_2ple(seq, weight_table)
145
146 -def reduce_sequence(seq, reduction_table,new_alphabet=None):
147 """ given an amino-acid sequence, return it in reduced alphabet form based 148 on the letter-translation table passed. Some "standard" tables are in 149 Alphabet.Reduced. 150 seq: a Seq.Seq type sequence 151 reduction_table: a dictionary whose keys are the "from" alphabet, and values 152 are the "to" alphabet""" 153 if new_alphabet is None: 154 new_alphabet = Alphabet.single_letter_alphabet 155 new_alphabet.letters = '' 156 for letter in reduction_table: 157 new_alphabet.letters += letter 158 new_alphabet.size = len(new_alphabet.letters) 159 new_seq = Seq.Seq('',new_alphabet) 160 for letter in seq: 161 new_seq += reduction_table[letter] 162 return new_seq
163