Package obitools :: Package word :: Module binary
[hide private]
[frames] | no frames]

Source Code for Module obitools.word.binary

  1  ''' 
  2  Created on 2 juil. 2009 
  3   
  4  @author: coissac 
  5  ''' 
  6   
  7   
  8   
9 -def bitCount(x):
10 i=0 11 while(x): 12 i+=1 13 x&=x-1 14 return i
15
16 -def allWordIterator(size):
17 ''' 18 Iterate thought the list of all DNA word of 19 size `size`. 20 21 @param size: size of the DNA word 22 @type size: int 23 24 @return: an iterator on DNA word (int) 25 @rtype: iterator 26 ''' 27 28 maxi=4**size 29 return xrange(maxi)
30
31 -def wordDist(w1,w2):
32 ''' 33 estimate Hamming distance between two words of the same size. 34 35 @param w1: the first word 36 @type w1: str 37 @param w2: the second word 38 @type w2: str 39 40 @return: the count of difference between the two words 41 @rtype: int 42 ''' 43 44 diff = (~(w1 & w2) & (w1 | w2)) 45 diff = (diff | (diff >> 1)) & 0x55555555 46 dist = bitCount(diff) 47 return dist
48
49 -def homoMax(word,size):
50 mask = (1 << (size << 1))-1 51 good = 0x55555555 52 maxi=0 53 shift = word 54 while good: 55 maxi+=1 56 shift>>=2 57 mask>>=2 58 id = (word & shift) | (~word & ~shift) 59 good&= id & (id>>1) & mask 60 return maxi
61
62 -def countA(word,size):
63 mask = (1 << (size << 1))-1 64 id = ~word 65 good= id & (id>>1) & 0x55555555 & mask 66 return bitCount(good)
67
68 -def countT(word,size):
69 good= word & (word>>1) & 0x55555555 70 return bitCount(good)
71
72 -def countAT(word,size):
73 mask = (1 << (size << 1))-1 74 shift = word >> 1 75 good = ((word & shift) | (~word & ~shift)) & 0x55555555 & mask 76 return bitCount(good)
77
78 -def countC(word,size):
79 mask = (1 << (size << 1))-1 80 good = ((word & 0x55555555) | (~word & 0xAAAAAAAA)) 81 good &= (good >> 1) & 0x55555555 & mask 82 return bitCount(good)
83
84 -def countG(word,size):
85 mask = (1 << (size << 1))-1 86 good = ((word & 0xAAAAAAAA) | (~word & 0x55555555)) 87 good &= (good >> 1) & 0x55555555 & mask 88 return bitCount(good)
89
90 -def countCG(word,size):
91 mask = (1 << (size << 1))-1 92 shift = word >> 1 93 good = ((word & ~shift) | (~word & shift)) & 0x55555555 & mask 94 return bitCount(good)
95 96
97 -def decodeWord(word,size):
98 return ''.join(['acgt'[(word >> i) & 3] for i in xrange(size*2-2,-1,-2)])
99