Package Bio :: Package GFF :: Module binning
[hide private]
[frames] | no frames]

Source Code for Module Bio.GFF.binning

 1  #!/usr/bin/env python 
 2  # 
 3  # Copyright 2002 by Michael Hoffman.  All rights reserved. 
 4  # This code is part of the Biopython distribution and governed by its 
 5  # license.  Please see the LICENSE file that should have been included 
 6  # as part of this package. 
 7  # 
 8  # note: 
 9  # I used Lincoln Stein's perl Bio::DB::GFF::Util::Binning as a model for this 
10   
11  """ 
12  Binning support for Bio.GFF 
13  """ 
14   
15  __version__ = "$Revision: 1.1 $" 
16  # $Source: /home/repository/biopython/biopython/Bio/GFF/binning.py,v $ 
17   
18 -class Meta(object):
19 MIN_BIN = 1000 20 MAX_BIN = 100000000
21
22 -def query(start=0, stop=Meta.MAX_BIN, minbin=Meta.MIN_BIN, maxbin=Meta.MAX_BIN):
23 args = [] 24 bins = [] 25 tier = maxbin 26 27 if start is None: 28 start=0 29 if stop is None: 30 stop = Meta.MAX_BIN 31 32 while tier >= minbin: 33 tier_start, tier_stop = bot(tier, start), top(tier, stop) 34 if (tier_start == tier_stop): 35 bins.append('fbin=%s') 36 args.append(tier_start) 37 else: 38 bins.append('fbin BETWEEN %s AND %s') 39 args.extend([tier_start, tier_stop]) 40 tier /= 10 41 query = "\n\t OR ".join(bins) 42 43 return query % tuple(args)
44
45 -def bin(start, stop, min, wantarray=0):
46 tier = min 47 while 1: 48 bin_start = int(float(start)/tier) 49 bin_end = int(float(stop)/tier) 50 if bin_start == bin_end: 51 break 52 tier *= 10 53 if wantarray: 54 return tier, bin_start 55 else: 56 return bin_name(tier, bin_start)
57
58 -def bot(tier, pos):
59 return name(tier, int(pos/tier))
60 61 top = bot 62
63 -def name(x, y):
64 return "%d.%06d" % (x, y)
65
66 -def _test(*args, **keywds):
67 import doctest, sys 68 doctest.testmod(sys.modules[__name__], *args, **keywds)
69 70 if __name__ == "__main__": 71 if __debug__: 72 _test() 73