forked from albertwcheng/albert-bioinformatics-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DivideBarcodeReads.py
executable file
·295 lines (234 loc) · 8.2 KB
/
DivideBarcodeReads.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
#!/usr/bin/env python
from sys import *
from getopt import getopt
from operator import itemgetter
from os.path import exists
complDict={'A':'T','a':'t','C':'G','c':'g','T':'A','t':'a','G':'C','g':'c','U':'A','u':'a'}
def complementBase(c):
global complDict
try:
return complDict[c]
except:
return c
def editDistance(s1,s2):
if len(s1)!=len(s2):
return 100000
dist=0
for i in range(0,len(s1)):
x=s1[i]
y=s2[i]
if x.upper()!=y.upper():
dist+=1
return dist
#return None or the (barcodeMatched,ToFileString)
def findBestBarcodeMatchedEntry(barcodeListing,code,editDistanceMax,noTie=True):
if editDistanceMax==0: #no mismatch allowed
if not barcodeListing.has_key(code):
return None
else:
return (code,barcodeListing[code])
else:
editDists=[]
for codeEntry,codeToFile in barcodeListing.items():
editDists.append((editDistance(code,codeEntry),codeEntry,codeToFile))
#now sort edit Dist on first item
editDists.sort(key=itemgetter(0))
#now ascending
#the first item is the best
if noTie and editDists[1][0]==editDists[0][0]: #tie arising
return None #tie assume no matched
if editDists[0][0]>editDistanceMax:
return None #not good match
return (editDists[0][1],editDists[0][2]) #return the code matched and the toFile String
def reverse_complement(S):
rc=""
for i in range(len(S)-1,-1,-1):
rc+=complementBase(S[i])
return rc
'''
@HWI-ST333_0096_FC:7:1101:1326:2106#CATGCC/1
the CATGCC after the # sign is corresponding to the reverse
complement of the barcode sequence GGCATG
'''
def printUsageAndExit(programName):
print >> stderr,programName,"[--barcodeListing listfile|--barcode barcode --tofile x.fastq] [OtherOptions] in.fastq ..."
print >> stderr,"listfile: lines of"
print >> stderr,"barCode[tab]filename"
print >> stderr,"[options]:"
print >> stderr,"--append append to file instead of creating a file"
print >> stderr,"--prefixOutFileName add prefix to out file name"
print >> stderr,"--suffixOutFileName add suffix to out file name"
print >> stderr,"--unclassifiedReadTo x output unclassified reads to file x"
print >> stderr,"--editDistanceMax mx. allow max mismatch to barcode (and no tie of the best two matched)"
print >> stderr,"--outActualCodeCount suffix. write the actual barcode counts for each uniq barcode sequence to a file of the outfile name with this suffix"
exit(1)
def loadBarCodeListing(barCodeListing,filename):
fil=open(filename)
for lin in fil:
barCode,outfile=lin.rstrip("\r\n").split("\t")
if barCodeListing.has_key(barCode):
print >> stderr,"barcode",barCode,"already existed. Abort"
exit(1)
barCodeListing[barCode]=outfile
fil.close()
def writeToCurFile(curFile,content):
if curFile:
print >> curFile,content
if __name__=='__main__':
programName=argv[0]
opts,args=getopt(argv[1:],'',['outActualCodeCount=','editDistMax=','barcodeListing=','barcode=','tofile=','append','suffixOutFileName=','prefixOutFileName=','unclassifiedReadTo='])
barCodeListing=dict() # dict[barcode]=outfile
appendOutFile=False
suffixOutFileName=""
barcodeCur=""
toFileCur=""
unclassTo=None
editDistanceMax=0 #default is exact match
noTie=True
prefixOutFileName=""
outActualCodeCount=None
for o,v in opts:
if o=='--barcodeListing':
loadBarCodeListing(barCodeListing,v)
elif o=='--barcode':
barcodeCur=v
if barCodeListing.has_key(barcodeCur):
print >> stderr,"barcode",barCode,"already existed. Abort"
exit(1)
elif o=='--tofile':
if len(barcodeCur)<1:
print >> stderr,"--tofile is not preceded by a --barcode argument. Abort"
exit(1)
barCodeListing[barcodeCur]=v
barcodeCur=""
elif o=='--append':
appendOutFile=True
elif o=='--suffixOutFileName':
suffixOutFileName=v
elif o=='--unclassifiedReadTo':
unclassTo=v
elif o=='--editDistMax':
editDistanceMax=int(v)
elif o=='--prefixOutFileName':
prefixOutFileName=v
elif o=='--outActualCodeCount':
outActualCodeCount=v
if len(args)==0:
print >> stderr,"no in.fastq specified. Abort"
printUsageAndExit(programName)
#now reverseComplement the barcode listing
newBarCodeListing=dict()
for k,v in barCodeListing.items():
newBarCodeListing[reverse_complement(k)]=v
#replace
barCodeListing=newBarCodeListing
writeCount=dict()
seqBarCodeCount=dict()
writeCountUnclassified=0
#openFiles!
barCodeFiles=dict()
for barCode,toFile in barCodeListing.items():
if appendOutFile:
barCodeFiles[barCode]=open(prefixOutFileName+toFile+suffixOutFileName,"w+")
else:
barCodeFiles[barCode]=open(prefixOutFileName+toFile+suffixOutFileName,"w")
writeCount[barCode]=0
if outActualCodeCount:
seqBarCodeCount[barCode]=dict()
unclassToFile=None
if unclassTo!="":
if appendOutFile:
unclassToFile=open(prefixOutFileName+unclassTo+suffixOutFileName,"w+")
else:
unclassToFile=open(prefixOutFileName+unclassTo+suffixOutFileName,"w")
readname=""
curFile=None
for infile in args:
print >> stderr,"processing",infile
fil=open(infile)
lino=0
for lin in fil:
lin=lin.rstrip("\r\n")
if len(lin)<1:
print >> stderr,"fastq formating error. lin has zero len. abort"
exit(1)
lino+=1
linoMod4=lino%4
if linoMod4==1: #seq-read-name-line
if lin[0]!="@":
print >> stderr,"fastq formating error. first line not starting with @. Abort"
exit(1)
readname=lin[1:]
afterpound=readname.split("#")[1]
barCodeSeq=afterpound.split("/")[0]
#if barCodeSeq not in barCodeFiles:
# curFile=unclassToFile
# writeCountUnclassified+=1
#else:
# curFile=barCodeFiles[barCodeSeq]
# writeCount[barCodeSeq]+=1
#
matched=findBestBarcodeMatchedEntry(barCodeFiles,barCodeSeq,editDistanceMax,noTie)
if not matched:
curFile=unclassToFile
writeCountUnclassified+=1
else:
barCodeMatched,toFileObj=matched
curFile=toFileObj #1 is the file
writeCount[barCodeMatched]+=1
if outActualCodeCount:
try:
seqBarCodeCount[barCodeMatched][barCodeSeq]+=1
except:
seqBarCodeCount[barCodeMatched][barCodeSeq]=1
writeToCurFile(curFile,lin)
elif linoMod4==2: #seq line
writeToCurFile(curFile,lin)
elif linoMod4==3: #qual-read-name-lin, put "+"
if lin[0]!="+":
print >> stderr,"fastq formating error. Third line not starting with +. Abort"
exit(1)
if len(lin)>1:
thisRName=lin[1:]
if thisRName!=readname:
print >> stderr,"fastq formating error. Third line read line not consistent with first line read line. Abort"
exit(1)
writeToCurFile(curFile,lin)
else: #qual line
writeToCurFile(curFile,lin)
fil.close()
for barCode,toFile in barCodeFiles.items():
toFile.close()
totalProc=0
#now summarize:
for barCode,toFile in barCodeListing.items():
totalProc+=writeCount[barCode]
print >> stderr,writeCount[barCode],"reads within",editDistanceMax,"edits of barcode",reverse_complement(barCode),"read name tagged with family of",barCode,"written to",prefixOutFileName+toFile+suffixOutFileName,
if outActualCodeCount:
print >> stderr,"actual code count written to",prefixOutFileName+toFile+outActualCodeCount
thisActualCodeCountDict=seqBarCodeCount[barCode]
if appendOutFile and exists(prefixOutFileName+toFile+outActualCodeCount):
#read in the existing code count file
finActualCodeCount=open(prefixOutFileName+toFile+outActualCodeCount)
for lin in finActualCodeCount:
radCode,radCount=lin.rstrip("\r\n").split("\t")
if radCode in thisActualCodeCountDict:
thisActualCodeCountDict[radCode]+=int(radCount)
else:
thisActualCodeCountDict[radCode]=int(radCount)
finActualCodeCount.close()
#output
filActualCodeCount=open(prefixOutFileName+toFile+outActualCodeCount,"w")
for radCode,radCount in thisActualCodeCountDict.items():
print >> filActualCodeCount,radCode+"\t"+str(radCount)
filActualCodeCount.close()
else:
print >> stderr,"" #just to complete the sentence
print >> stderr,totalProc,"reads classified"
print >> stderr,writeCountUnclassified,"reads unclassified",
totalProc+=writeCountUnclassified
if unclassToFile:
print >> stderr,"and written to",unclassTo+suffixOutFileName
else:
print >> stderr,""
print >> stderr,totalProc,"reads processed"