-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGC_compo_matching.py
301 lines (258 loc) · 10.5 KB
/
GC_compo_matching.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
""" Module matching %GC compo distribution b/w fg and bg. """
import sys
import random
from Bio import SeqIO
from utils import GC
def fg_GC_bins(fg_file):
"""
Compute G+C content for all sequences in the foreground.
It computes the %GC content and store the information in a list. To each
G+C percentage bin, we associate the number of sequences falling in the
corresponding bin.
Return lists of GC contents, GC bins, and lengths distrib.
"""
stream = open(fg_file)
gc_bins = [0] * 101
gc_list = []
lengths = []
for record in SeqIO.parse(stream, "fasta"):
gc = GC(record.seq)
gc_list.append(gc)
gc_bins[gc] += 1
lengths.append(len(record.seq))
stream.close()
return gc_list, gc_bins, lengths
def fg_len_GC_bins(fg_file):
"""
Compute G+C content for all sequences in the foreground.
Computes %GC contant and store the information in a list. To each G+C
percentage bin, we associate the number of sequences falling in the
corresponding bin.
Return lists of GC contents, GC bins, and lengths distrib.
"""
stream = open(fg_file)
gc_bins = []
for _ in range(0, 101):
gc_bins.append({})
gc_list = []
lengths = []
for record in SeqIO.parse(stream, "fasta"):
gc = GC(record.seq)
gc_list.append(gc)
length = len(record)
lengths.append(length)
if length in gc_bins[gc]:
gc_bins[gc][length] += 1
else:
gc_bins[gc][length] = 1
stream.close()
return gc_list, gc_bins, lengths
def print_rec(rec, stream):
""" Print a record to a stream output. """
stream.write("{0}\n".format(rec.format("fasta")))
def print_in_bg_dir(gc_bins, bg_dir, with_len=False):
""" Print the sequences in the bg directory in bin files. """
for percent in xrange(0, 101):
with open("{0}/bg_bin_{1}.txt".format(bg_dir, percent), 'w') as stream:
if with_len:
for length in gc_bins[percent]:
for rec in gc_bins[percent][length]:
print_rec(rec, stream)
else:
for rec in gc_bins[percent]:
print_rec(rec, stream)
def bg_GC_bins(bg_file, bg_dir):
"""
Compute G+C content for all sequences in the background.
Compute and store the GC information in a list. To each G+C percentage bin,
we associate the corresponding sequence names.
Files representing the binning are stored in the "bg_dir" directory.
Return lists of GC contents, GC bins, and lengths distrib.
"""
stream = open(bg_file)
gc_bins = []
gc_list = []
lengths = []
for _ in xrange(0, 101):
gc_bins.append([])
for record in SeqIO.parse(stream, "fasta"):
gc = GC(record.seq)
gc_list.append(gc)
gc_bins[gc].append(record)
lengths.append(len(record.seq))
stream.close()
print_in_bg_dir(gc_bins, bg_dir)
return gc_list, gc_bins, lengths
def bg_len_GC_bins(bg_file, bg_dir):
"""
Compute G+C content for all sequences in the background.
Compute and store the %GC information in a list. To each G+C percentage
bin, we associate the corresponding sequence names.
Return lists of GC contents, GC bins, and lengths distrib.
"""
stream = open(bg_file)
gc_bins = []
gc_list = []
lengths = []
for _ in range(0, 101):
gc_bins.append({})
for record in SeqIO.parse(stream, "fasta"):
gc = GC(record.seq)
gc_list.append(gc)
if len(record) in gc_bins[gc]:
gc_bins[gc][len(record)].append(record)
else:
gc_bins[gc][len(record)] = [record]
lengths.append(len(record.seq))
stream.close()
print_in_bg_dir(gc_bins, bg_dir, True)
return gc_list, gc_bins, lengths
def get_bins_from_bg_dir(bg_dir, percent):
""" Return the sequences from the corresponding bin file. """
with open("{0}/bg_bin_{1:d}.txt".format(bg_dir, percent)) as stream:
bin_seq = []
for record in SeqIO.parse(stream, "fasta"):
bin_seq.append(record)
return bin_seq
def generate_sequences(fg_bins, bg_bins, bg_dir, nfold):
"""
Choose randomly the background sequences in each bin of %GC.
Follow the same distribution as the one of foreground sequences with a
nfold ratio.
Return the list of %GC and length distrib.
"""
lengths = []
gc_list = []
for percent in range(0, 101):
if fg_bins[percent]:
random.seed()
try:
nb = fg_bins[percent] * nfold
if bg_bins:
bin_seq = bg_bins[percent]
else:
bin_seq = get_bins_from_bg_dir(bg_dir, percent)
sample = random.sample(bin_seq, nb)
gc_list.extend([percent] * nb)
except ValueError:
sys.stderr.write("""*** WARNING ***
Sample larger than population for {0:d}% G+C content:
{1:d} needed and {2:d} obtained\n""".format(
percent, fg_bins[percent] * nfold, len(bin_seq)))
sample = bin_seq
gc_list.extend([percent] * len(bin_seq))
for r in sample:
print r.format("fasta"),
lengths.append(len(r.seq))
return gc_list, lengths
def extract_seq_rec(size, nb, bg_keys, bg, accu, index):
"""
Extract "nb" sequences with sizes equal to "size" nt.
We try to get exact size or as close as possible to "size" nt. This is a
tail recursive function with the accumulator "accu" looking for sizes
"bg_keys" in the bg set "bg".
Return the accumulator and the number of found sequences.
"""
if not (bg_keys and nb): # End of the recursion since we have no sequence
# in the bg or enough retrieved (nb=0)
return accu, nb
if index > len(bg_keys) - 1:
return extract_seq_rec(size, nb, bg_keys, bg, accu, index - 1)
if not bg_keys: # No more size in the background to be checked so return
# what was in the previous size bin
if bg[bg_keys[index - 1]]:
random.shuffle(bg[bg_keys[index - 1]])
accu.extend(bg[bg_keys[index - 1]][0:nb])
bg[bg_keys[index - 1]] = bg[index - 1][nb:]
return accu, nb - len(bg[bg_keys[index - 1]][0:nb])
else:
return accu, nb
if bg_keys[index] >= size: # No need to go further in the different sizes
# within the background
if (index == 0 or not bg[bg_keys[index - 1]] or
bg_keys[index] - size < size - bg_keys[index - 1]): # Which
# size is the closest to the expected one? => we go for the current one
# if YES
random.shuffle(bg[bg_keys[index]])
accu.extend(bg[bg_keys[index]][0:nb])
new_nb = nb - len(bg[bg_keys[index]][0:nb])
if bg[bg_keys[index]][nb:]: # Check that there is sequences in the
# background for this size bin
bg[bg_keys[index]] = bg[bg_keys[index]][nb:]
return extract_seq_rec(size, new_nb, bg_keys, bg, accu, index)
else:
bg[bg_keys[index]] = bg[bg_keys[index]][nb:]
del bg_keys[index]
return extract_seq_rec(size, new_nb, bg_keys, bg, accu, index)
else: # The previous size was the closest
random.shuffle(bg[bg_keys[index - 1]])
accu.extend(bg[bg_keys[index - 1]][0:nb])
new_nb = nb - len(bg[bg_keys[index - 1]][0:nb])
if bg[bg_keys[index - 1]][nb:]: # Check that there is sequences in
# the background for this size bin
bg[bg_keys[index - 1]] = bg[bg_keys[index - 1]][nb:]
return extract_seq_rec(size, new_nb, bg_keys, bg, accu, index)
else:
bg[bg_keys[index - 1]] = bg[bg_keys[index - 1]][nb:]
del bg_keys[index - 1]
return extract_seq_rec(size, new_nb, bg_keys, bg, accu,
index - 1)
elif index == len(bg_keys) - 1:
random.shuffle(bg[bg_keys[index]])
accu.extend(bg[bg_keys[index]][0:nb])
new_nb = nb - len(bg[bg_keys[index]][0:nb])
if bg[bg_keys[index]][nb:]:
bg[bg_keys[index]] = bg[bg_keys[index]][nb:]
return extract_seq_rec(size, new_nb, bg_keys, bg, accu, index)
else:
bg[bg_keys[index]] = bg[bg_keys[index]][nb:]
del bg_keys[index]
return extract_seq_rec(size, new_nb, bg_keys, bg, accu, index)
else:
return extract_seq_rec(size, nb, bg_keys, bg, accu, index + 1)
def get_bins_len_from_bg_dir(bg_dir, percent):
""" Return the sequences from the corresponding bin file. """
with open("{0}/bg_bin_{1:d}.txt".format(bg_dir, percent)) as stream:
bin_seq = {}
for record in SeqIO.parse(stream, "fasta"):
length = len(record)
if length in bin_seq:
bin_seq[length].append(record)
else:
bin_seq[length] = [record]
return bin_seq
def generate_len_sequences(fg, bg, bg_dir, nfold):
"""
Extract the sequences from the bg with similar sizes as in the fg.
Return the %GC list and length distrib.
"""
sys.setrecursionlimit(10000)
random.seed()
lengths = []
gc_list = []
for percent in range(0, 101):
if fg[percent]:
nb = sum(fg[percent].values()) * nfold
sequences = []
for size in fg[percent].keys():
nb_to_retrieve = fg[percent][size] * nfold
if bg:
bg_bins = bg[percent]
else:
bg_bins = get_bins_len_from_bg_dir(bg_dir, percent)
bg_keys = sorted(bg_bins.keys())
seqs, _ = extract_seq_rec(size, nb_to_retrieve, bg_keys,
bg_bins, [], 0)
sequences.extend(seqs)
nb_match = len(sequences)
gc_list.extend([percent] * nb_match)
if nb_match != nb:
sys.stderr.write("""*** WARNING ***
Sample larger than population for {0:d}% G+C content:
{1:d} needed and {2:d} obtained\n""".format(percent,
nb,
nb_match))
for s in sequences:
lengths.append(len(s))
print "{0:s}".format(s.format("fasta")),
return gc_list, lengths