1 | #Dan Blankenberg |
---|
2 | #Contains methods to tranform sequence strings |
---|
3 | import string |
---|
4 | |
---|
5 | #Translation table for reverse Complement, with ambiguity codes |
---|
6 | DNA_COMPLEMENT = string.maketrans( "ACGTRYKMBDHVacgtrykmbdhv", "TGCAYRMKVHDBtgcayrmkvhdb" ) |
---|
7 | RNA_COMPLEMENT = string.maketrans( "ACGURYKMBDHVacgurykmbdhv", "UGCAYRMKVHDBugcayrmkvhdb" ) |
---|
8 | #Translation table for DNA <--> RNA |
---|
9 | DNA_TO_RNA = string.maketrans( "Tt", "Uu" ) |
---|
10 | RNA_TO_DNA = string.maketrans( "Uu", "Tt" ) |
---|
11 | |
---|
12 | #reverse sequence string |
---|
13 | def reverse( sequence ): |
---|
14 | return sequence[::-1] |
---|
15 | #complement DNA sequence string |
---|
16 | def DNA_complement( sequence ): |
---|
17 | return sequence.translate( DNA_COMPLEMENT ) |
---|
18 | #complement RNA sequence string |
---|
19 | def RNA_complement( sequence ): |
---|
20 | return sequence.translate( RNA_COMPLEMENT ) |
---|
21 | #returns the reverse complement of the sequence |
---|
22 | def DNA_reverse_complement( sequence ): |
---|
23 | sequence = reverse( sequence ) |
---|
24 | return DNA_complement( sequence ) |
---|
25 | def RNA_reverse_complement( sequence ): |
---|
26 | sequence = reverse( sequence ) |
---|
27 | return RNA_complement( sequence ) |
---|
28 | def to_DNA( sequence ): |
---|
29 | return sequence.translate( DNA_TO_RNA ) |
---|
30 | def to_RNA( sequence ): |
---|
31 | return sequence.translate( RNA_TO_DNA ) |
---|
32 | |
---|
33 | class ColorSpaceConverter( object ): |
---|
34 | unknown_base = 'N' |
---|
35 | unknown_color = '.' |
---|
36 | color_to_base_dict = {} |
---|
37 | color_to_base_dict[ 'A' ] = { '0':'A', '1':'C', '2':'G', '3':'T', '4':'N', '5':'N', '6':'N', '.':'N' } |
---|
38 | color_to_base_dict[ 'C' ] = { '0':'C', '1':'A', '2':'T', '3':'G', '4':'N', '5':'N', '6':'N', '.':'N' } |
---|
39 | color_to_base_dict[ 'G' ] = { '0':'G', '1':'T', '2':'A', '3':'C', '4':'N', '5':'N', '6':'N', '.':'N' } |
---|
40 | color_to_base_dict[ 'T' ] = { '0':'T', '1':'G', '2':'C', '3':'A', '4':'N', '5':'N', '6':'N', '.':'N' } |
---|
41 | color_to_base_dict[ 'N' ] = { '0':'N', '1':'N', '2':'N', '3':'N', '4':'N', '5':'N', '6':'N', '.':'N' } |
---|
42 | base_to_color_dict = {} |
---|
43 | for base, color_dict in color_to_base_dict.iteritems(): |
---|
44 | base_to_color_dict[ base ] = {} |
---|
45 | for key, value in color_dict.iteritems(): |
---|
46 | base_to_color_dict[ base ][ value ] = key |
---|
47 | base_to_color_dict[ base ][ 'N' ] = '4' #force ACGT followed by N to be '4', because this is now 'processed' data; we could force to '.' (non-processed data) also |
---|
48 | base_to_color_dict[ 'N' ].update( { 'A':'5', 'C':'5', 'G':'5', 'T':'5', 'N':'6' } ) |
---|
49 | def __init__( self, fake_adapter_base = 'G' ): |
---|
50 | assert fake_adapter_base in self.base_to_color_dict, 'A bad fake adapter base was provided: %s.' % fake_adapter_base |
---|
51 | self.fake_adapter_base = fake_adapter_base |
---|
52 | def to_color_space( self, sequence, adapter_base = None ): |
---|
53 | if adapter_base is None: |
---|
54 | adapter_base = self.fake_adapter_base |
---|
55 | last_base = adapter_base #we add a fake adapter base so that the sequence can be decoded properly again |
---|
56 | rval = last_base |
---|
57 | for base in sequence: |
---|
58 | rval += self.base_to_color_dict.get( last_base, self.base_to_color_dict[ self.unknown_base ] ).get( base, self.unknown_color ) |
---|
59 | last_base = base |
---|
60 | return rval |
---|
61 | def to_base_space( self, sequence ): |
---|
62 | if not isinstance( sequence, list ): |
---|
63 | sequence = list( sequence ) |
---|
64 | if sequence: |
---|
65 | last_base = sequence.pop( 0 ) |
---|
66 | else: |
---|
67 | last_base = None |
---|
68 | assert last_base in self.color_to_base_dict, 'A valid adapter base must be included when converting to base space from color space. Found: %s' % last_base |
---|
69 | rval = '' |
---|
70 | for color_val in sequence: |
---|
71 | last_base = self.color_to_base_dict[ last_base ].get( color_val, self.unknown_base ) |
---|
72 | rval += last_base |
---|
73 | return rval |
---|