-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenome_helper.py
207 lines (199 loc) · 6 KB
/
genome_helper.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
from collections import defaultdict
import sys
import random
import bisect
import cPickle
import gzip
import os
from trees import *
from generator_helper import *
def bisect_choiceTUP(items):
"""Returns a function that makes a weighted random choice from a list of tuples."""
added_weights = []
last_sum = 0.0
for item, weight in items:
weight = float(weight)
last_sum += weight
added_weights.append(last_sum)
def choice(rnd=random.random, bis=bisect.bisect):
return items[bis(added_weights, rnd() * last_sum)][0]
return choice
def parseModel(gzipFile, readlen):
"""prepares error models for input to mkErrors."""
try:
file = gzip.open(gzipFile, 'rb')
except IOError:
sys.exit()
modReadLen = cPickle.load(file)
if readlen != 'd' and readlen > modReadLen:
file.close()
sys.exit()
mx = cPickle.load(file)
insD = cPickle.load(file)
delD = cPickle.load(file)
gQualL = cPickle.load(file)
bQualL = cPickle.load(file)
iQualL = cPickle.load(file)
readCount = cPickle.load(file)
rdLenD = cPickle.load(file)
file.close()
return mx, insD, delD, gQualL, bQualL, iQualL, readCount, rdLenD
def mkErrors(read, readLen, mx, gQ, bQ, qual):
"""Adds random errors to read."""
inds = {'A': 0, 'T': 1, 'G': 2, 'C': 3, 'N': 4, 'a': 0, 't': 1, 'g': 2, 'c': 3, 'n': 4}
pos = 0
quals = ''
pos += 1
read2 = ""
for s in read:
if s in inds:
read2 += s
read = read2
while pos <= readLen and pos < len(read) - 4:
#print "read",read
#print "type",type(read)
prev = read[pos:pos + 4]
after = read[pos + 4]
d0 = pos
d1 = inds[prev[3]]
d2 = inds[prev[2]]
d3 = inds[prev[1]]
d4 = inds[prev[0]]
d5 = inds[after]
tot = float(mx[d0][d1][d2][d3][d4][d5][5])
# print tot
Mprobs = mx[d0][d1][d2][d3][d4][d5] / tot
val = random.random()
a = Mprobs[0]
t = Mprobs[1] + a
g = Mprobs[2] + t
c = Mprobs[3] + g
n = Mprobs[4] + c
success = False
if val > n or tot == 0:
gPos = pos - 1
while gPos >= 0:
try:
quals += gQ[gPos]()
success = True
break
except:
gPos -= 1
if success == False:
quals += chr(30 + qual)
elif val > c:
read = read[:pos + 3] + 'N' + read[pos + 4:]
bPos = pos - 1
while bPos >= 0:
try:
quals += bQ[bPos]()
success = True
break
except:
bPos - 1
if success == False:
quals += chr(2 + qual)
elif val > g:
read = read[:pos + 3] + 'C' + read[pos + 4:]
bPos = pos - 1
while bPos >= 0:
try:
quals += bQ[bPos]()
success = True
break
except:
bPos - 1
if success == False:
quals += chr(2 + qual)
elif val > t:
read = read[:pos + 3] + 'G' + read[pos + 4:]
bPos = pos - 1
while bPos >= 0:
try:
quals += bQ[bPos]()
success = True
break
except:
bPos - 1
if success == False:
quals += chr(2 + qual)
elif val > a:
read = read[:pos + 3] + 'T' + read[pos + 4:]
bPos = pos - 1
while bPos >= 0:
try:
quals += bQ[bPos]()
success = True
break
except:
bPos - 1
if success == False:
quals += chr(2 + qual)
else:
read = read[:pos + 3] + 'A' + read[pos + 4:]
bPos = pos - 1
while bPos >= 0:
try:
quals += bQ[bPos]()
success = True
break
except:
bPos - 1
if success == False:
quals += chr(2 + qual)
pos += 1
if quals == "":
#sys.exit()
return read, quals
#print "quals",quals,"quals"
quals += quals[-1]
read = read[4:readLen + 4]
quals = quals[:readLen]
if len(quals) != len(read):
sys.exit()
return read, quals
def Comp(sequence):
""" complements a sequence, preserving case."""
d = {'A': 'T', 'T': 'A', 'C': 'G', 'G': 'C', 'a': 't', 't': 'a', 'c': 'g', 'g': 'c', 'N': 'N', 'n': 'n'}
cSeq = ''
for s in sequence:
if s in d:
cSeq += d[s]
return cSeq
def GenReads(fasta = "", length = '', models = '', qual = '', out = ''):
out1=open(out+'.fastq','a')
mx1,insD1,delD1,gQualL,bQualL,iQualL,readCount,rdLenD=parseModel(models,length)
#choose good quality bases
gQList=[]
for i in (gQualL):
gL=[]
keys=i.keys()
keys.sort()
for k in keys:
gL.append((chr(k+qual),i[k]))
gQList.append(bisect_choiceTUP(gL))
#choose bad quality bases
bQList=[]
for i in (bQualL):
bL=[]
keys=i.keys()
keys.sort()
for k in keys:
bL.append((chr(k+qual),i[k]))
bQList.append(bisect_choiceTUP(bL))
end_pos = 100
cur_pos = 0
while end_pos < len(fasta):
#print "end_pos",end_pos
read = fasta[cur_pos:end_pos]
read1, quals1 = mkErrors(read, length, mx1, gQList, bQList, qual)
#print "read1",read1
#print "quals1",quals1
head1='@'+'r'+'_from_'+'_#0/1\n'
cur_pos = end_pos
end_pos += 100
out1.write(head1)
out1.write(read1+'\n')
out1.write('+\n')
out1.write(quals1+'\n')
return out1