-
Notifications
You must be signed in to change notification settings - Fork 0
/
seq_utils.py
203 lines (152 loc) · 4.53 KB
/
seq_utils.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
203
import re
###
#find sequence
def find_seq_pattern(seqs, pattern=r'n.[stc]', ret_start=True):
"""
find a pattern position in a sequence
input
------
seqs: string or list of strings to process
pattern: regular expression to search
ret_start: logical (default True)
if true, return only the start of the pattern match position in pos
if false, return (start,end) tuple of pattern match position in pos
output
------
pat: list of patterns matched in sequence
pos: list of positions of patterns found
if seqs is a list of sequences, then return is list of lists:
pat[i][j]={pattern j found for sequence i}
"""
#compile pattern
p = re.compile(pattern)
if type(seqs) is list:
SL = seqs
else:
SL = [seqs]
#containers for all matches
patL = []
posL = []
#loop over all sequences
for x in SL:
pat = []
pos = []
#find all pattern matches
for m in p.finditer(x):
if m:
pat.append(m.group())
pos.append(m.span())
if ret_start:
pos = [x[0] for x in pos]
patL.append(pat)
posL.append(pos)
if len(SL) == 1:
patL = patL[0]
posL = posL[0]
return patL, posL
def pretty_seq(seq,
pos,
fmt=' %s ',
colorcode=5,
upper=True,
lower=False,
offset=3):
"""
create a "pretty" sequence
seq: string to consider
pos: position to pretty up
if pos[i] is tuple:
pretty up pos[i][0]:pos[i][1]
else:
pretty up pos[i]:pos[i]+offset
color: color code [0=black, 1=red, 2=green, 3=yellow, 4=blue, 5=magenta
6=cyan]
offset: integer (default=3)
"""
f = "\x1b[3%sm%s\x1b[0m" % (colorcode, fmt)
a = ''
x = seq
start = 0
for p in pos:
if type(p) is tuple:
ps, pe = p
else:
ps, pe = (p, p + offset)
#not highlighted part
a += x[start:ps]
#highlighted part
#a+="\x1b[35m %s \x1b[0m"%x[p:p+3]
s = x[ps:pe]
if upper:
a += f % (s.upper())
elif lower:
a += f % (s.lower())
else:
a += f % s
start = pe
a += x[start:]
return a
#http://ozzmaker.com/add-colour-to-text-in-python/
import colorama
import itertools
def seq_to_pretty_list(seq,
pos_list,
background_list,
fmt='%s',
text='BLACK',
upper=True,
lower=False,
offset=3):
"""
create a "pretty" sequence
seq: string to consider
pos: position to pretty up
if pos[i] is tuple:
pretty up pos[i][0]:pos[i][1]
else:
pretty up pos[i]:pos[i]+offset
text: string (default black)
color in colorama (Note, converted to upper)
backgroud : string (default YELLOW)
offset: integer (default=3)
"""
#f="\x1b[3%sm%s\x1b[0m"%(colorcode,fmt)
#f="\033[1:40;43m%s\033[0m"%(fmt)
#f = "\x1b[1;4%s;4%sm%s\x1b[0m" % (text, background, fmt)
out = list(seq)
for pos,color in itertools.zip_longest(pos_list,background_list):
for p in pos:
if type(p) is tuple:
ps, pe = p
else:
ps, pe = (p, p+offset)
#highlighted part
for i in range(ps,pe):
s=out[i]
fmt = getattr(colorama.Fore, text.upper()) + getattr(colorama.Back, color.upper()) + '{}' + \
colorama.Style.RESET_ALL + colorama.Fore.BLACK
x = fmt.format(s)
out[i] = x
return out
def extract_info_before_after(seq, pos, offset, offset_pos=3):
"""
extract sequence info before and after pattern match
input
------
seq: sequence
pos: position of matches (if list of ints, starts, if list if tuples, start/end)
offset_pos: if pos is start, then offset of pattern match
offset: offset for before/after strings
output
------
P: list of tuples of form (before,match,after)
"""
P = []
for p in pos:
if type(p) is tuple:
ps, pe = p
else:
ps, pe = (p, p + offset_pos)
t = (seq[ps - offset:ps], seq[ps:pe], seq[pe:pe + offset])
P.append(t)
return P