-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBarcode.py
282 lines (200 loc) · 10.6 KB
/
Barcode.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
import logging
import pandas as pd
from Bio import SeqIO
from Bio import pairwise2
import Bio as bio
logger = logging.getLogger(__name__)
class Barcode():
thisunit = 'thisunit is at its initial value'
def __init__(self,runname,thread=0):
"""
Constructor
Parameters:
runname = name of the run, will be used to store tmp files
thread = 0, will be used to store unique tmp files
"""
logger.info('Barcode module initialized')
self.runname = runname
self.thread = thread
def reverse(input):
"""
Will give the reverse text of a string
Parameters:
input = input string
"""
buildString = ''
for x in input:
buildString = x+buildString
return buildString
def splitBarcode(self,inputFasta,inputBarcode,scoreList,barcodeEdge):
"""
Starts a loop through 1. the sequence file 2. the barcodes
Parameters:
inputFasta = path to fasta file containing nanopore sequenes
inputBarcode = dict containing (asymetric) barcodes
scoreList = list containing [match,mismatch,gapopen,gapextend] scores for alignment of barcode to sequence record
barcodeEdge = distance from the read end to look for barcodes
"""
logger.info('Start with %s in thread %s',inputFasta,self.thread)
logger.info('Runname is %s',self.runname )
listHighBarcodeScore = []
listHighBarcodeID = []
dfCollector = pd.DataFrame()
for record in SeqIO.parse(inputFasta, "fasta"): #Walk through all the fasta sequences
#for x in range(1,2):
logger.debug('Parsing: %s', record.description)
currentHigh = 0
currentHighR = 0
barcodeScore = {}
barcodeScoreF = {}
barcodeScoreR = {}
df = pd.DataFrame()
for thisBarcodeID,thisBarcodeSeq in inputBarcode.iteritems(): #Walk through the barcodes
alignmentsF = self.runPairwiseAlignment(str(record.seq)[0:barcodeEdge],thisBarcodeSeq[0],scoreList)
alignmentsR = self.runPairwiseAlignment(str(record.seq)[-barcodeEdge:],thisBarcodeSeq[1],scoreList)
alignmentsCF = self.runPairwiseAlignment(bio.Seq.reverse_complement(str(record.seq)[-barcodeEdge:]),thisBarcodeSeq[0],scoreList)
alignmentsCR = self.runPairwiseAlignment(bio.Seq.reverse_complement(str(record.seq)[0:barcodeEdge]),thisBarcodeSeq[1],scoreList)
#TODO: set barcode directly as index, prevents sizing up the dataframe every append
df = df.append({'barcode': thisBarcodeID,
'tF': alignmentsF[0][2],
'tR': alignmentsR[0][2],
'cF': alignmentsCF[0][2],
'cR': alignmentsCR[0][2],
'pos_tF_begin': alignmentsF[0][3],
'pos_tF_end': alignmentsF[0][4],
'pos_tR_begin': alignmentsR[0][3],
'pos_tR_end': alignmentsR[0][4],
'pos_cF_begin': alignmentsCF[0][3],
'pos_cF_end': alignmentsCF[0][4],
'pos_cR_begin': alignmentsCR[0][3],
'pos_cR_end': alignmentsCR[0][4]
}
, ignore_index=True)
df = df.set_index('barcode') #set the barcode as the index
#logger.info(df)
hit = self.findHit(df)
hit.update({'seqID': record.description})
logger.debug(hit)
dfCollector = dfCollector.append(hit,ignore_index=True)
s = str(record.description)
#print 'Saving pickle of thread %s' % thread
dfCollector.to_pickle(self.runname + 'dfCollector.p.' + str(self.thread) + '.tmp')
logger.info('Done with thread %s',self.thread)
return True
#return dfCollector
def runPairwiseAlignment(self,inputSeq,barcodeSeq,scoring):
"""
Does the actual calling of the pairwise2 algorithm
"""
return pairwise2.align.localms( str(inputSeq), str(barcodeSeq), *scoring)
def getWinner(self,inputDf,column):
"""
Get winner
Returns list with
barcode
"""
hit = inputDf[[column]].sum(axis=1).sort_values(ascending=False).head(1).index[0]
return [
str(hit),
float(inputDf[[column]].sum(axis=1).sort_values(ascending=False).head(1))
]
def findHit(self,inputDf):
"""
1. Checks if two barcodes are the top 1 score -> return this set
2. If not, check which pair is highest scoring -> return this set
Returns
barcode,
direction, #complement or reverse
scoreF,
scoreR,
wasMatch, #case 1 (Above)
rankF, #usefull in case 2 (in case one this is 1)
rankR # "
"""
if self.getWinner(inputDf,'tF')[0] == self.getWinner(inputDf,'tR')[0]:
logger.debug("forward and reverse match in template strand")
##
## Check this piece, and then insert in complement
##
return { 'barcode': self.getWinner(inputDf,'tF')[0],
'direction': 't',
'scoreF': self.getWinner(inputDf,'tF')[1],
'scoreR': self.getWinner(inputDf,'tR')[1],
'wasMatch': True,
'rankF':1,
'rankR':1,
'pos_F_begin': int(inputDf.loc[self.getWinner(inputDf,'tF')[0]].pos_tF_begin),
'pos_F_end': int(inputDf.loc[self.getWinner(inputDf,'tF')[0]].pos_tF_end),
'pos_R_begin': int(inputDf.loc[self.getWinner(inputDf,'tR')[0]].pos_tR_begin),
'pos_R_end': int(inputDf.loc[self.getWinner(inputDf,'tR')[0]].pos_tR_end),
}
if self.getWinner(inputDf,'cF')[0] == self.getWinner(inputDf,'cR')[0]:
logger.debug("forward and reverse match in compelement strand")
##
## Check this piece
##
return { 'barcode': self.getWinner(inputDf,'cF')[0],
'direction': 'c',
'scoreF': self.getWinner(inputDf,'cF')[1],
'scoreR': self.getWinner(inputDf,'cR')[1],
'wasMatch': True,
'rankF':1,
'rankR':1,
'pos_F_begin': int(inputDf.loc[self.getWinner(inputDf,'cF')[0]].pos_cF_begin),
'pos_F_end': int(inputDf.loc[self.getWinner(inputDf,'cF')[0]].pos_cF_end),
'pos_R_begin': int(inputDf.loc[self.getWinner(inputDf,'cR')[0]].pos_cR_begin),
'pos_R_end': int(inputDf.loc[self.getWinner(inputDf,'cR')[0]].pos_cR_end)
}
else:
logger.debug("no double forward and reverse match found")
topT = inputDf[['tF','tR']].sum(axis=1).sort_values(ascending=False).head(1)
topC = inputDf[['cF','cR']].sum(axis=1).sort_values(ascending=False).head(1)
if float(topT) >= float(topC): #Template score is higher than complement score
logger.debug( 'Template strand has highest scoring duo')
winner = inputDf.ix[inputDf[['tF','tR']].sum(axis=1).sort_values(ascending=False).index].head(1)
#print df[['tF','tR']].ix[df[['tF','tR']].sum(axis=1).sort_values(ascending=False).index]
rankWinner = inputDf.rank(axis=0, #over the rows
numeric_only=True, #only values, sanaty check should render this not needed
ascending =False #high to low
).ix[winner.index.tolist()[0]]
#print df.rank(axis=0, #over the rows
# numeric_only=True, #only values, sanaty check should render this not needed
# ascending =False #high to low
# )
#print 'Winner barcode:',
#print winner.index.tolist()[0]
#print 'Rank dataframe:'
#print df.rank(axis=0,numeric_only=True,ascending=False )
#print 'Rank of the winner:'
#print rankWinner
return { 'barcode': winner.index.tolist()[0],
'direction': 't',
'scoreF': float(winner['tF']),
'scoreR': float(winner['tR']),
'wasMatch': False,
'rankF':rankWinner['tF'],
'rankR':rankWinner['tR'],
'pos_F_begin':int(winner.pos_tF_begin),
'pos_F_end': int(winner.pos_tF_end),
'pos_R_begin': int(winner.pos_tR_begin),
'pos_R_end': int(winner.pos_tR_end)
}
else:
logger.debug( 'Complement strand has highest scoring duo')
winner = inputDf.ix[inputDf[['cF','cR']].sum(axis=1).sort_values(ascending=False).index].head(1)
rankWinner = inputDf.rank(axis=0, #over the rows
numeric_only=True, #only values, sanaty check should render this not needed
ascending =False #high to low
).ix[winner.index.tolist()[0]]
return { 'barcode': winner.index.tolist()[0],
'direction': 'c',
'scoreF': float(winner['cF']),
'scoreR': float(winner['cR']),
'wasMatch': False,
'rankF':rankWinner['cF'], #todo
'rankR':rankWinner['cR'],
'pos_F_begin': int(winner.pos_cF_begin),
'pos_F_end': int(winner.pos_cF_end),
'pos_R_begin': int(winner.pos_cR_begin),
'pos_R_end': int(winner.pos_cR_end)
}