-
Notifications
You must be signed in to change notification settings - Fork 183
/
generate_site_positions.py
280 lines (201 loc) · 6.55 KB
/
generate_site_positions.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env python
# Generate site positions in genome from given restriction enzyme
# Juicer 1.5
from __future__ import print_function
import sys
import re
def usage():
print('Usage: {} <restriction enzyme> <genome> [location]'.format(sys.argv[0]), file=sys.stderr)
sys.exit(1)
# ------------------------------------------------------------------------------
def process_args(args):
# Genome to filename mappings
#
# You may hardcode filenames belonging to frequently used genomes by inserting
# elements into this dictionary.
filenames = {
'hg19': '/seq/references/Homo_sapiens_assembly19.fasta',
'mm9' : '/seq/references/Mus_musculus_assembly9.fasta',
'mm10': '/seq/references/Mus_musculus_assembly10.fasta',
'hg18': '/seq/references/Homo_sapiens_assembly18.fasta',
}
# Enzyme to search pattern mappings
#
# You may provide your own patterns by inserting elements into this dictionary.
# Multiple patterns are supported in the form of lists. If you enumerate more
# than one patterns for an enzyme, then a match will be reported when at least
# one of them can be found at a given position.
#
# Wildcards:
# N: A, C, G or T
# M: A or C
# R: A or G
# W: A or T
# Y: C or T
# S: C or G
# K: G or T
# H: A, C or T
# B: C, G or T
# V: A, C or G
# D: A, G or T
patterns = {
'HindIII' : 'AAGCTT',
'DpnII' : 'GATC',
'MboI' : 'GATC',
'Sau3AI' : 'GATC',
'Arima' : [ 'GATC', 'GANTC' ],
}
if len(args) != 3 and len(args) != 4:
usage()
enzyme = args[1]
genome = args[2]
inputfile = ''
outputfile = ''
pattern = ''
if len(args) == 4:
inputfile = args[3]
elif genome in filenames:
inputfile = filenames[genome]
else:
print('<genome> not found and [location] not defined', file=sys.stderr)
usage()
if enzyme in patterns:
pattern = patterns[enzyme]
# Convert a simple string to a list.
if not isinstance(pattern, list):
pattern = [ pattern ]
# Make patterns uppercase.
pattern = [ p.upper() for p in pattern ]
else:
print('<restriction enzyme> must be one of {}'.format(list(patterns.keys())), file=sys.stderr)
usage()
outputfile = genome + '_' + enzyme + '.txt'
return {
'enzyme' : enzyme,
'genome' : genome,
'pattern' : pattern,
'inputfile' : inputfile,
'outputfile' : outputfile,
}
# ------------------------------------------------------------------------------
def has_wildcard(pattern):
# Input pattern can be a list or a string.
wildcards = re.compile(r'[NMRWYSKHBVD]')
if (isinstance(pattern, list)):
for p in pattern:
if re.search(wildcards, p):
return True
else:
if re.search(wildcards, pattern):
return True
return False
# ------------------------------------------------------------------------------
def pattern2regexp(pattern):
# Input pattern can be a list or a string.
wildcards = {
'N': '[ACGT]',
'M': '[AC]',
'R': '[AG]',
'W': '[AT]',
'Y': '[CT]',
'S': '[CG]',
'K': '[GT]',
'H': '[ACT]',
'B': '[CGT]',
'V': '[ACG]',
'D': '[AGT]',
}
if isinstance(pattern, list):
return [ pattern2regexp(p) for p in pattern ]
pattern = pattern.upper()
for p, r in wildcards.items():
pattern = re.sub(p, r, pattern)
return re.compile(pattern.upper())
# ------------------------------------------------------------------------------
def get_match_func(pattern):
# Input pattern can be a list or a string.
if not isinstance(pattern, list):
pattern = [ pattern ]
if has_wildcard(pattern):
pattern = pattern2regexp(pattern)
if len(pattern) == 1: # There is only a single pattern.
pattern = pattern[0] # Use the only element from the list as a single regexp.
def match_single_regexp(segment):
if re.match(pattern, segment):
return True
return False
return match_single_regexp
else: # There are multiple patterns.
def match_multi_regexp(segment):
for p in pattern:
if re.match(p, segment):
return True
return False
return match_multi_regexp
else: # No wildcard in any of the patterns.
if len(pattern) == 1: # There is only a single pattern.
pattern = pattern[0] # Use the only element from the list as a single string.
def match_single_string(segment):
if segment.startswith(pattern):
return True
return False
return match_single_string
else: # There are multiple patterns.
def match_multi_string(segment):
for p in pattern:
if segment.startswith(p):
return True
return False
return match_multi_string
# ------------------------------------------------------------------------------
def process_input(params):
f = open(params['inputfile' ], 'r')
g = open(params['outputfile'], 'w')
minsize = min([ len(p) for p in params['pattern'] ])
maxsize = max([ len(p) for p in params['pattern'] ])
matches = get_match_func(params['pattern'])
segment = ''
counter = 0
endl = ''
for line in f:
line = line.strip()
if line.startswith('>'):
# This is the beginning of a new sequence, but before starting it we must
# finish processing of the remaining segment of the previous sequence.
while len(segment) > minsize:
segment = segment[1:]
if matches(segment):
g.write(' ' + str(counter - len(segment) + 1))
if counter > 0:
g.write(' ' + str(counter)) # Close the previous sequence here.
firststr=re.split('\s+',line[1:])
g.write(endl+firststr[0])
segment = ''
counter = 0
endl = '\n'
continue
# Process next line of the sequence.
line = line.upper()
for symbol in line:
counter += 1
segment += symbol
while len(segment) > maxsize:
segment = segment[1:]
# Do pattern matching only if segment size equals maxsize.
if len(segment) == maxsize:
if matches(segment):
g.write(' ' + str(counter - maxsize + 1)) # maxsize == len(segment)
# Finish the last sequence.
while len(segment) > minsize:
segment = segment[1:]
if matches(segment):
g.write(' ' + str(counter - len(segment) + 1))
if counter > 0:
g.write(' ' + str(counter))
g.write('\n') # End the output file with a newline.
# Close files.
g.close()
f.close()
# ------------------------------------------------------------------------------
params = process_args(sys.argv)
process_input(params)