Package obitools :: Package sequenceencoder
[hide private]
[frames] | no frames]

Source Code for Package obitools.sequenceencoder

 1  from obitools import location 
2 3 -class SequenceEncoder(object):
4 pass
5
6 -class DNAComplementEncoder(SequenceEncoder):
7 _comp={'a': 't', 'c': 'g', 'g': 'c', 't': 'a', 8 'r': 'y', 'y': 'r', 'k': 'm', 'm': 'k', 9 's': 's', 'w': 'w', 'b': 'v', 'd': 'h', 10 'h': 'd', 'v': 'b', 'n': 'n', 'u': 'a', 11 '-': '-'} 12 13 _info={'complemented':True} 14 15 @staticmethod
16 - def _encode(seq,position=slice(None, None, -1)):
17 cseq = [DNAComplementEncoder._comp.get(x.lower(),'n') for x in seq[position]] 18 return ''.join(cseq)
19 20 @staticmethod
21 - def _check(seq):
22 assert seq.isNucleotide()
23 24 @staticmethod
25 - def _convertpos(position):
26 if isinstance(position, int): 27 return -(position+1) 28 elif isinstance(position, slice): 29 return slice(-(position.stop+1), 30 -(position.start+1), 31 -position.step) 32 elif isinstance(position, location.Location): 33 return location.ComplementLocation(position).simplify() 34 35 raise TypeError,"position must be an int, slice or Location instance"
36 37 @staticmethod
38 - def complement(seq):
39 return seq
40
41 -class SeqFragmentEncoder(SequenceEncoder):
42 - def __init__(self,begin,end):
43 assert begin < end and begin >=0 44 self._limits = slice(begin,end) 45 self._info = {'cut' : [begin,end,1]} 46 self._len = end - begin + 1
47
48 - def _check(self,seq):
49 lseq = len(seq) 50 assert self._limits.stop <= lseq
51
52 - def _encode(self,seq,position=None):
53 return str(seq)[self._limits]
54
55 - def _convertpos(self,position):
56 if isinstance(position, int): 57 if position < -self._len or position >= self._len: 58 raise IndexError,position 59 if position >=0: 60 return self._limits.start + position 61 else: 62 return self._limits.stop + position + 1 63 elif isinstance(position, slice): 64 return slice(-(position.stop+1), 65 -(position.start+1), 66 -position.step) 67 elif isinstance(position, location.Location): 68 return location.ComplementLocation(position).simplify() 69 70 raise TypeError,"position must be an int, slice or Location instance"
71