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 (DEPRECATED). 
  8   
  9  This module is deprecated, and is expected to be removed in the next release. 
 10  If you use this module, please contact the Biopython developers via the 
 11  mailing lists. 
 12  """ 
 13   
 14  import warnings 
 15  import Bio 
 16  warnings.warn("Bio.utils has been deprecated, and we intend to remove it in " 
 17                "the next release of Biopython.", Bio.BiopythonDeprecationWarning) 
 18   
 19  import Seq 
 20  import Alphabet 
 21   
 22  from Bio.Alphabet import _verify_alphabet as verify_alphabet 
 23   
 24  from PropertyManager import default_manager 
 25   
26 -def ungap(seq):
27 """given a sequence with gap encoding, return the ungapped sequence""" 28 #TODO - Fix this? It currently assumes the outmost AlphabetEncoder 29 #is for the gap. Consider HasStopCodon(Gapped(Protein())) as a test case. 30 warnings.warn("Bio.utils has been deprecated, and we intend to remove it " 31 "in the next release of Biopython. Instead of function " 32 "Bio.utils.ungap please use the ungap method of the Seq " 33 "object (added in Biopython 1.53).", 34 Bio.BiopythonDeprecationWarning) 35 gap = seq.gap_char 36 letters = [] 37 for c in seq: 38 if c != gap: 39 letters.append(c) 40 return Seq.Seq("".join(letters), seq.alphabet.alphabet)
41
42 -def count_monomers(seq):
43 dict = {} 44 for c in seq.alphabet.letters: 45 dict[c] = seq.count(c) 46 return dict
47
48 -def percent_monomers(seq):
49 dict2 = {} 50 seq_len = len(seq) 51 dict = count_monomers(seq) 52 for m in dict: 53 dict2[m] = dict[m] * 100. / seq_len 54 return dict2
55
56 -def sum(seq, table, zero = 0.0):
57 total = zero 58 for c in getattr(seq, "data", seq): 59 total = total + table[c] 60 return total
61 62 # For ranged addition
63 -def sum_2ple(seq, table, zero = (0.0, 0.0)):
64 x, y = zero 65 data = getattr(seq, "data", seq) 66 for c in data: 67 x2, y2 = table[c] 68 x = x + x2 69 y = y + y2 70 return (x, y)
71
72 -def total_weight(seq, weight_table = None):
73 if weight_table is None: 74 weight_table = default_manager.resolve(seq.alphabet, "weight_table") 75 return sum(seq, weight_table)
76
77 -def total_weight_range(seq, weight_table = None):
78 if weight_table is None: 79 weight_table = default_manager.resolve(seq.alphabet, "weight_range_table") 80 return sum_2ple(seq, weight_table)
81
82 -def reduce_sequence(seq, reduction_table,new_alphabet=None):
83 """ given an amino-acid sequence, return it in reduced alphabet form based 84 on the letter-translation table passed. Some "standard" tables are in 85 Alphabet.Reduced. 86 seq: a Seq.Seq type sequence 87 reduction_table: a dictionary whose keys are the "from" alphabet, and values 88 are the "to" alphabet""" 89 if new_alphabet is None: 90 new_alphabet = Alphabet.single_letter_alphabet 91 new_alphabet.letters = '' 92 for letter in reduction_table: 93 new_alphabet.letters += letter 94 new_alphabet.size = len(new_alphabet.letters) 95 new_seq = Seq.Seq('',new_alphabet) 96 for letter in seq: 97 new_seq += reduction_table[letter] 98 return new_seq
99