-
Notifications
You must be signed in to change notification settings - Fork 49
/
other_screens.py
202 lines (136 loc) · 4.6 KB
/
other_screens.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
from Bio.Seq import Seq
import struct
import numpy as np
class Screen:
#this class contains method to conduct the biological screens.
def __init__(self,
exDNA = False,
max_homopolymer = 3,
gc = 0.05,
orf = None):
self.exDNA = exDNA
self.max_homopolymer = max_homopolymer
self.gc = gc
self.orf = orf
self.prepare()
self.gc_high = 1
self.gc_low = 0
def prepare(self):
#making screening faster by memoization of simple patterns and parameters
if self.exDNA: #we a 6 nt alphabet
max_alphabet = 6
else: #we have a regular 4nt alphabet
max_alphabet = 4
patterns = [None] * max_alphabet
for t in xrange(max_alphabet):
patterns[t] = str(t) * (self.max_homopolymer + 1)
#e.g. pattern is ["0000", "1111", "2222", "3333"]
#for max_alphabet = 4, and max_homopolymer = 3.
self.patterns = patterns
self.max_alphabet = max_alphabet
if not self.exDNA:
#gc content if not defined for exDNA.
self.gc_low = self.gc - 0.5
self.gc_high = self.gc + 0.5
return 1
def screen(self, seq, l):
#main method to screen a sequence
if not self.screen_homopolymers(seq):
return 0
if not self.screen_gc(seq, l):
return 0
#if not self.screen_orf(seq):
# return 0
return 1
def screen_homopolymers(self, data):
#detects homopoltmer patterns in data.
#data needs to be an str.
#return 1 for succcess or 0 for a failed attempt.
for pattern in self.patterns:
if pattern in data:
return 0
return 1
def screen_gc(self, data, l):
gc = (data.count('1') + data.count('2') + 0.0)/ l
if (gc < self.gc_low) or (gc > self.gc_high):
return 0
return 1
def screen_orf(self, seq):
#screen for orfs above a certain thershold
#seq needs to be an str
#retrun 1 for succuess or 0 foar a failed attempt
if self.orf is None:
return 1
max_orf = self.orf
seq = Seq(seq)
for strand, nuc in [(+1, seq), (-1, seq.reverse_complement())]:
for frame in range(3):
res = (l - frame) % 3
for pro in nuc[frame:l-res].translate(1).split("*"):
#translate(1) means to translate according to the regular genetic code
if len(pro) >= max_orf and len(pro) > 0: #avoid end of line empty string
return 0
return 1
def expandable_alphabet(array_of_bytes, l, n_symbols, n_bytes = 8, alphabet_size = 6):
""" takes chunks of n_bytes from array_of_bytes (of lenght l) and convert them to
concatanted chunks (strings) of n_symbols using alphabet_size letters. alphabet_size must be <=10"""
res = ''
for i in xrange(0,l, n_bytes):
slice_array = array_of_bytes[i:i+n_bytes]
number_int = long(0)
for pos,val in enumerate(slice_array):
number_int += long(val) * 256**(n_bytes-(pos+1)) #little endian
res += _toDigits(n = number_int, b = alphabet_size, width = n_symbols)
#print slice_array, number_int, toDigits(n = number_int, b = alphabet_size, width = n_symbols)
#res += '\n'
return res
def screen_repeats_x(dna, homo_length, alphabet_size):
for t in xrange(alphabet_size):
homo = str(t) * (homo_length + 1)
if homo in dna:
return 0
return 1
def _toDigits(n, b, width):
"""Convert a positive number n to its digit representation in base b.
width is the number of overall digits.
base MUST BE SMALLER THAN 10.
"""
digits = ''
while n > 0:
digits += str(n % b)
n = n // b
digits = digits[::-1] #revsersing to little endian
return digits.rjust(width, "0")
def _toDigits_array(n, b, width):
"""Convert a positive number n to its digit representation in base b.
width is the number of overall digits.
returns an array of int.
"""
digits = list()
while n > 0:
digits.insert(0,int(n % b)) #little endian.
n = n // b
digits = [0] * (width-len(digits)) + digits
return digits
def dexpandable_alphabet(dna, l, n_symbols, n_bytes, alphabet_size = 6):
res = list()
for i in xrange(0,l, n_symbols):
slice_array = dna[i:i+n_symbols]
number_int = long(0)
for pos,val in enumerate(slice_array):
number_int += long(val) * 6**(n_symbols-(pos+1)) #little endian
res += _toDigits_array(n = number_int, b = 256, width = n_bytes)
#print slice_array, number_int, toDigits(n = number_int, b = alphabet_size, width = n_symbols)
#res += '\n'
return res
def test(size, n_symbols, n_bytes):
array_of_bytes = list(np.random.randint(0, 255, size = size))
print array_of_bytes
ex = expandable_alphabet(array_of_bytes, size, n_symbols, n_bytes, alphabet_size = 6)
o = dexpandable_alphabet(ex, len(ex), n_symbols, n_bytes)
print o
if len(set(o)-set(array_of_bytes)):
print "Error"
else:
print "All good"
#test(21,65,21)