-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastq_analysis.py
313 lines (295 loc) · 13.4 KB
/
fastq_analysis.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
302
303
304
305
306
307
308
309
310
311
312
313
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 24 15:29:39 2012
@author: hervemn
"""
import os
import csv
import sys
import getopt
import numpy
import string
import time
import pymongo
def extract_tag(in_a,in_b,l_tag,experience_name):
connection = pymongo.Connection('localhost',27017)
db = connection[experience_name]
collection = db.reads_tags
collection.drop()
collection = db.reads_tags
input_a = open(in_a,'r')
input_b = open(in_b,'r')
start = time.clock()
i = 0
while 1:
line_a = input_a.readline()
line_b = input_b.readline()
if (i%4==0):
title_a = line_a[1:-1]
elif (i%4==1):
tag_a = line_a[:l_tag-1]
tag_b = line_b[:l_tag-1]
read = {"_id" : title_a,
"tag_a": tag_a,
"tag_b": tag_b}
collection.insert(read,{"upsert":"true"})
i = i+1
if not line_a:
elapsed = (time.clock() - start)
print 'data base loaded in '+str(elapsed)+' s'
break
def bank_split_by_tag(in_a,in_b,tag_bank_1,tag_bank_2,len_random_tag,name_garbage,dir_bank_1,dir_bank_2,dir_garbage):
import string
start = time.clock()
len_tag_bank = len(tag_bank_1)
rev_tag_bank_1 =tag_bank_1.translate(string.maketrans('TAGCtagc', 'ATCGATCG'))
if not(os.path.exists(dir_bank_1)):
os.mkdir(dir_bank_1)
if not(os.path.exists(dir_garbage)):
os.mkdir(dir_garbage)
if len(tag_bank_2)>0:
rev_tag_bank_2 =tag_bank_2.translate(string.maketrans('TAGCtagc', 'ATCGATCG'))
if not(os.path.exists(dir_bank_2)):
os.mkdir(dir_bank_2)
bank_b_1= open(os.path.join(dir_bank_2,tag_bank_2+'_'+os.path.basename(in_a)), "w")
bank_b_2= open(os.path.join(dir_bank_2,tag_bank_2+'_'+os.path.basename(in_b)), "w")
bank_a_1= open(os.path.join(dir_bank_1,tag_bank_1+'_'+os.path.basename(in_a)), "w")
bank_a_2= open(os.path.join(dir_bank_1,tag_bank_1+'_'+os.path.basename(in_b)), "w")
bank_garbage_1= open(os.path.join(dir_garbage,name_garbage+'_'+os.path.basename(in_a)), "w")
bank_garbage_2= open(os.path.join(dir_garbage,name_garbage+'_'+os.path.basename(in_b)), "w")
input_a = open(in_a,'r')
input_b = open(in_b,'r')
i = 0
while 1:
line_a = input_a.readline()
line_b = input_b.readline()
if not line_a and not line_b:
bank_a_1.close()
bank_a_2.close()
bank_garbage_1.close()
bank_garbage_2.close()
if len(tag_bank_2)>0:
bank_b_1.close()
bank_b_2.close()
break
elapsed = (time.clock() - start)
print 'bank splited in ' + str(elapsed)+ ' s'
if (i%4==0):
title_a = line_a[1:]
title_b = line_b[1:]
elif (i%4==1):
seq_a = line_a
seq_b = line_b
elif (i%4==3):
qual_a = line_a
qual_b = line_b
random_tag_a = seq_a[0:len_random_tag]
random_tag_b = seq_b[0:len_random_tag]
tag_a = seq_a[len_random_tag:len_random_tag+len_tag_bank]
tag_b = seq_b[len_random_tag:len_random_tag+len_tag_bank]
block_a = seq_a[len_random_tag+len_tag_bank:]
block_b = seq_b[len_random_tag+len_tag_bank:]
block_a_qual = qual_a[:len_random_tag]+qual_a[len_random_tag+len_tag_bank:]
block_b_qual = qual_b[:len_random_tag]+qual_b[len_random_tag+len_tag_bank:]
garbage_block_a = seq_a[len_random_tag:]
garbage_block_b = seq_b[len_random_tag:]
qual_garbage_a = qual_a[len_random_tag:]
qual_garbage_b = qual_b[len_random_tag:]
# print "tag read 1"+tag_a
# print "tag read 2"+tag_b
if ((tag_a[0:2] == tag_bank_1[0:2]) or (tag_a[0:2] == rev_tag_bank_1[0:2])) and ((tag_b[0:2] == tag_bank_1[0:2]) or (tag_b[0:2] == rev_tag_bank_1[0:2])):
bank_a_1.write("@%s%s+%s%s" % (title_a, random_tag_a+block_a,title_a,
block_a_qual))
bank_a_2.write("@%s%s+%s%s" % (title_b, random_tag_b+block_b,title_b,
block_b_qual))
elif (len(tag_bank_2)>0) and ((tag_a[0:2] == tag_bank_2[0:2]) or (tag_a[0:2] == rev_tag_bank_2[0:2])) and\
((tag_b[0:2] == tag_bank_2[0:2]) or (tag_b[0:2] == rev_tag_bank_2[0:2])):
bank_b_1.write("@%s%s+%s%s" % (title_a, random_tag_a+block_a,title_a,
block_a_qual))
bank_b_2.write("@%s%s+%s%s" % (title_b, random_tag_b+block_b,title_b,
block_b_qual))
else:
bank_garbage_1.write("@%s%s+%s%s" % (title_a, garbage_block_a,title_a,
qual_garbage_a))
bank_garbage_2.write("@%s%s+%s%s" % (title_b,garbage_block_b,title_b,
qual_garbage_b))
i = i+1
def bank_split_by_tag_1(in_a,in_b,dir_bank_1,dir_bank_2,motif1):
import string
rev_motif1 = motif1.translate(string.maketrans('TAGCtagc', 'ATCGATCG'))
if not(os.path.exists(dir_bank_1)):
os.mkdir(dir_bank_1)
if not(os.path.exists(dir_bank_2)):
os.mkdir(dir_bank_2)
trim =len(motif1)
handle_a_1= open(os.path.join(dir_bank_1,motif1+'_'+os.path.basename(in_a)), "w")
handle_a_2= open(os.path.join(dir_bank_2,os.path.basename(in_a)), "w")
handle_b_1= open(os.path.join(dir_bank_1,motif1+'_'+os.path.basename(in_b)), "w")
handle_b_2= open(os.path.join(dir_bank_2,os.path.basename(in_b)), "w")
i = 0;
input_a = open(in_a,'r')
input_b = open(in_b,'r')
start = time.clock()
while 1:
line_a = input_a.readline()
line_b = input_b.readline()
if (i%4==0):
title_a = line_a[1:]
title_b = line_b[1:]
elif (i%4==1):
seq_a = line_a
seq_b = line_b
elif (i%4==3):
qual_a = line_a
qual_b = line_b
if ((seq_a[:trim] == motif1) or (seq_a[:trim] == rev_motif1)) and ((seq_b[:trim] == motif1) or (seq_b[:trim] == rev_motif1)):
handle_a_1.write("@%s%s+%s%s" % (title_a, seq_a[trim:],title_a,
qual_a[trim:]))
handle_b_1.write("@%s%s+%s%s" % (title_b, seq_b[trim:],title_b,
qual_b[trim:]))
else:
handle_a_2.write("@%s%s+%s%s" % (title_a, seq_a,title_a,
qual_a))
handle_b_2.write("@%s%s+%s%s" % (title_b, seq_b,title_b,
qual_b))
i = i+1
if not line_a and not line_b:
handle_a_1.close()
handle_b_1.close()
handle_a_2.close()
handle_b_2.close()
break
elapsed = (time.clock() - start)
print 'bank splited in ' + str(elapsed)+ ' s'
def remove_bank_tag(in_a,in_b,dir_bank_1,start_random_tag,end_random_tag,start_tag,end_tag):
if not(os.path.exists(dir_bank_1)):
os.mkdir(dir_bank_1)
motif1 = 'trimmed'
handle_a_1= open(os.path.join(dir_bank_1,motif1+'_'+os.path.basename(in_a)), "w")
handle_b_1= open(os.path.join(dir_bank_1,motif1+'_'+os.path.basename(in_b)), "w")
i = 0
input_a = open(in_a,'r')
input_b = open(in_b,'r')
start = time.clock()
while 1:
line_a = input_a.readline()
line_b = input_b.readline()
if (i%4==0):
title_a = line_a[1:]
title_b = line_b[1:]
elif (i%4==1):
seq_a = line_a
seq_b = line_b
elif (i%4==3):
qual_a = line_a
qual_b = line_b
random_tag_a = seq_a[start_random_tag-1:end_random_tag]
random_tag_b = seq_b[start_random_tag-1:end_random_tag]
tag_a = seq_a[start_tag-1:end_tag]
tag_b = seq_b[start_tag-1:end_tag]
block_a = seq_a[end_tag:]
block_b = seq_b[end_tag:]
block_a_qual = qual_a[start_random_tag-1:end_random_tag]+qual_a[end_tag:]
block_b_qual = qual_b[start_random_tag-1:end_random_tag]+qual_b[end_tag:]
handle_a_1.write("@%s%s+%s%s" % (title_a, random_tag_a+block_a,title_a,
block_a_qual))
handle_b_1.write("@%s%s+%s%s" % (title_b, random_tag_b+block_b,title_b,
block_b_qual))
i = i+1
if not line_a and not line_b:
handle_a_1.close()
handle_b_1.close()
break
elapsed = (time.clock() - start)
print 'bank trimmed in ' + str(elapsed)+ ' s'
def bank_split_by_tag_gene(in_a,in_b,info_bank,len_random_tag,info_garbage):
import string
import numpy as np
import gzip
list_bank = info_bank.keys()
n_bank = len(list_bank)
for ele in list_bank:
tag = info_bank[ele]['tag']
len_tag_bank = len(tag)
rev_tag =tag.translate(string.maketrans('TAGCtagc', 'ATCGATCG'))
info_bank[ele]['rev_tag'] = rev_tag
dir_bank = info_bank[ele]['dir_bank']
if not(os.path.exists(dir_bank)):
os.mkdir(dir_bank)
info_bank[ele]['handle_1'] = open(os.path.join(dir_bank,tag+'_'+os.path.basename(in_a)+'bet'), "w")
info_bank[ele]['handle_2'] = open(os.path.join(dir_bank,tag+'_'+os.path.basename(in_b)+'bet'), "w")
dir_garbage = info_garbage['dir_bank']
if not(os.path.exists(dir_garbage)):
os.mkdir(dir_garbage)
info_garbage['handle_1'] = open(os.path.join(dir_garbage,'garbage_'+os.path.basename(in_a)), "w")
info_garbage['handle_2'] = open(os.path.join(dir_garbage,'garbage_'+os.path.basename(in_b)), "w")
ext_a = in_a.split('.')[-1]
ext_b = in_b.split('.')[-1]
if ((ext_a == 'gz') or (ext_b == 'gz')):
input_a = gzip.open(in_a,'r')
input_b = gzip.open(in_b,'r')
else:
input_a = open(in_a,'r')
input_b = open(in_b,'r')
i = 0
start = time.clock()
while 1:
line_a = input_a.readline()
line_b = input_b.readline()
if not line_a and not line_b:
for ele in list_bank:
info_bank[ele]['handle_1'].close()
info_bank[ele]['handle_2'].close()
info_garbage['handle_1'].close()
info_garbage['handle_2'].close()
elapsed = (time.clock() - start)
print 'bank splited in ' + str(elapsed)+ ' s'
break
if (i%4==0):
title_a = line_a[1:]
title_b = line_b[1:]
elif (i%4==1):
seq_a = line_a
seq_b = line_b
elif (i%4==3):
qual_a = line_a
qual_b = line_b
random_tag_a = seq_a[0:len_random_tag]
random_tag_b = seq_b[0:len_random_tag]
tag_a = seq_a[len_random_tag:len_random_tag+len_tag_bank]
tag_b = seq_b[len_random_tag:len_random_tag+len_tag_bank]
block_a = seq_a[len_random_tag+len_tag_bank:]
block_b = seq_b[len_random_tag+len_tag_bank:]
block_a_qual = qual_a[:len_random_tag]+qual_a[len_random_tag+len_tag_bank:]
block_b_qual = qual_b[:len_random_tag]+qual_b[len_random_tag+len_tag_bank:]
# garbage_block_a = seq_a[len_random_tag:]
# garbage_block_b = seq_b[len_random_tag:]
# qual_garbage_a = qual_a[len_random_tag:]
# qual_garbage_b = qual_b[len_random_tag:]
res_test = np.zeros(n_bank)
for k in range(0,n_bank):
tmp_t1 = len({l for l, (left, right) in enumerate(zip(tag_a,info_bank[list_bank[k]]['tag'])) if left == right})
tmp_t1p = len({l for l, (left, right) in enumerate(zip(tag_a,info_bank[list_bank[k]]['rev_tag'])) if left == right})
tmp_t2 = len({l for l, (left, right) in enumerate(zip(tag_b,info_bank[list_bank[k]]['tag'])) if left == right})
tmp_t2p = len({l for l, (left, right) in enumerate(zip(tag_b,info_bank[list_bank[k]]['rev_tag'])) if left == right})
t1 = np.max([tmp_t1,tmp_t1p])
t2 = np.max([tmp_t2,tmp_t2p])
# score = t1 + t2
score = np.max([t1,t2])
# res_test[k] = score/(len_tag_bank *2.)
res_test[k] = score/(len_tag_bank)
if res_test.max()>=0.65:
""" write corresponding bank"""
index_bank = res_test.argmax()
name_bank = list_bank[index_bank]
info_bank[name_bank]['handle_1'].write("@%s%s+%s%s" % (title_a, random_tag_a+block_a,title_a,
block_a_qual))
info_bank[name_bank]['handle_2'].write("@%s%s+%s%s" % (title_b, random_tag_b+block_b,title_b,
block_b_qual))
else:
""" write garbage"""
info_garbage['handle_1'].write("@%s%s+%s%s" % (title_a, random_tag_a+block_a,title_a,
block_a_qual))
info_garbage['handle_2'].write("@%s%s+%s%s" % (title_b, random_tag_b+block_b,title_b,
block_b_qual))
i = i+1