Download this file

406 lines (325 with data), 15.5 kB

  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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
from singlecellmultiomics.modularDemultiplexer.baseDemultiplexMethods import UmiBarcodeDemuxMethod, NonMultiplexable, IlluminaBaseDemultiplexer, phredToFastqHeaderSafeQualities
from singlecellmultiomics.modularDemultiplexer.demultiplexModules import CELSeq2_c8_u6
import re
from singlecellmultiomics.utils import reverse_complement
# SCCHIC using NLAIII adapter, 384 well format with 3bp UMI followed by
# "A" base
class SCCHIC_384w_c8_u3(UmiBarcodeDemuxMethod):
def __init__(self, barcodeFileParser, random_primer_read=1,random_primer_length=6, **kwargs):
self.barcodeFileAlias = 'maya_384NLA'
UmiBarcodeDemuxMethod.__init__(
self,
umiRead=0,
umiStart=0,
umiLength=3,
barcodeRead=0,
barcodeStart=3,
barcodeLength=8,
random_primer_read=random_primer_read,
random_primer_length=random_primer_length,
barcodeFileAlias=self.barcodeFileAlias,
barcodeFileParser=barcodeFileParser,
**kwargs)
self.shortName = 'scCHIC384C8U3'
self.longName = 'Single cell CHIC, 384well CB: 8bp UMI: 3bp, RP: 6BP'
self.autoDetectable = True
self.description = '384 well format. 3bp umi followed by 8bp barcode and a single A. R2 ends with a 6bp random primer'
self.sequenceCapture[0] = slice(
self.barcodeLength + self.umiLength + 1,
None) # dont capture the first base
def demultiplex(self, records, **kwargs):
if kwargs.get(
'probe') and records[0].sequence[self.barcodeLength + self.umiLength] != 'T':
raise NonMultiplexable
# add first 2 bases as ligation tag:
ligation_start = self.barcodeLength + self.umiLength
ligation_end = ligation_start + 2
ligation_sequence = records[0].sequence[ligation_start:ligation_end]
ligation_qualities = records[0].qual[ligation_start:ligation_end]
taggedRecords = UmiBarcodeDemuxMethod.demultiplex(
self, records, **kwargs)
taggedRecords[0].addTagByTag(
'lh',
ligation_sequence,
isPhred=False,
make_safe=False)
taggedRecords[0].addTagByTag(
'lq',
ligation_qualities,
isPhred=True,
make_safe=False)
taggedRecords[1].addTagByTag(
'lh',
ligation_sequence,
isPhred=False,
make_safe=False)
taggedRecords[1].addTagByTag(
'lq',
ligation_qualities,
isPhred=True,
make_safe=False)
#taggedRecords[0].sequence = taggedRecords[0].sequence[1:]
#taggedRecords[0].qualities = taggedRecords[0].qualities[1:]
return taggedRecords
class SCCHIC_384w_c8_u3_direct_ligation(UmiBarcodeDemuxMethod):
def __init__(self, barcodeFileParser, random_primer_read=None,random_primer_length=None, **kwargs):
self.barcodeFileAlias = 'maya_384NLA'
UmiBarcodeDemuxMethod.__init__(
self,
umiRead=0,
umiStart=0,
umiLength=3,
barcodeRead=0,
barcodeStart=3,
barcodeLength=8,
random_primer_read=random_primer_read,
random_primer_length=random_primer_length,
barcodeFileAlias=self.barcodeFileAlias,
barcodeFileParser=barcodeFileParser,
**kwargs)
self.shortName = 'scCHIC384C8U3l'
self.longName = 'Single cell CHIC, 384well CB: 8bp UMI: 3bp, no RP'
self.autoDetectable = False
self.description = '384 well format. 3bp umi followed by 8bp barcode and a single A. R2 does not contain a random primer'
self.sequenceCapture[0] = slice(
self.barcodeLength + self.umiLength + 1,
None) # dont capture the first base
def demultiplex(self, records, **kwargs):
if kwargs.get(
'probe') and records[0].sequence[self.barcodeLength + self.umiLength] != 'T':
raise NonMultiplexable
# add first 2 bases as ligation tag:
ligation_start = self.barcodeLength + self.umiLength
ligation_end = ligation_start + 2
ligation_sequence = records[0].sequence[ligation_start:ligation_end]
ligation_qualities = records[0].qual[ligation_start:ligation_end]
taggedRecords = UmiBarcodeDemuxMethod.demultiplex(
self, records, **kwargs)
taggedRecords[0].addTagByTag(
'lh',
ligation_sequence,
isPhred=False,
make_safe=False)
taggedRecords[0].addTagByTag(
'lq',
ligation_qualities,
isPhred=True,
make_safe=False)
taggedRecords[1].addTagByTag(
'lh',
ligation_sequence,
isPhred=False,
make_safe=False)
taggedRecords[1].addTagByTag(
'lq',
ligation_qualities,
isPhred=True,
make_safe=False)
#taggedRecords[0].sequence = taggedRecords[0].sequence[1:]
#taggedRecords[0].qualities = taggedRecords[0].qualities[1:]
return taggedRecords
class SCCHIC_384w_c8_u3_direct_ligation_SINGLE_END(UmiBarcodeDemuxMethod):
def __init__(self, barcodeFileParser, random_primer_read=None,random_primer_length=None, **kwargs):
self.barcodeFileAlias = 'maya_384NLA'
UmiBarcodeDemuxMethod.__init__(
self,
umiRead=0,
umiStart=0,
umiLength=3,
barcodeRead=0,
barcodeStart=3,
barcodeLength=8,
random_primer_read=None,
random_primer_length=None,
barcodeFileAlias=self.barcodeFileAlias,
barcodeFileParser=barcodeFileParser,
**kwargs)
self.shortName = 'scCHIC384C8U3se'
self.longName = 'Single cell CHIC, 384well CB: 8bp UMI: 3bp, single end, no RP'
self.autoDetectable = True
self.description = '384 well format. 3bp umi followed by 8bp barcode and a single A. No read 2'
self.sequenceCapture[0] = slice(
self.barcodeLength + self.umiLength + 1,
None) # dont capture the first base
def demultiplex(self, records, **kwargs):
if kwargs.get(
'probe') and records[0].sequence[self.barcodeLength + self.umiLength] != 'T':
raise NonMultiplexable
if len(records) != 1:
raise NonMultiplexable
# add first 2 bases as ligation tag:
ligation_start = self.barcodeLength + self.umiLength
ligation_end = ligation_start + 2
ligation_sequence = records[0].sequence[ligation_start:ligation_end]
ligation_qualities = records[0].qual[ligation_start:ligation_end]
taggedRecords = UmiBarcodeDemuxMethod.demultiplex(
self, records, **kwargs)
taggedRecords[0].addTagByTag(
'lh',
ligation_sequence,
isPhred=False,
make_safe=False)
taggedRecords[0].addTagByTag(
'lq',
ligation_qualities,
isPhred=True,
make_safe=False)
return taggedRecords
class SCCHIC_384w_c8_u3_pdt(IlluminaBaseDemultiplexer):
def __init__(
self,
barcodeFileParser=None,
indexFileParser=None,
indexFileAlias='illumina_merged_ThruPlex48S_RP',
**kwargs):
IlluminaBaseDemultiplexer.__init__(
self,
indexFileParser=indexFileParser,
indexFileAlias=indexFileAlias)
self.description = '384 well format, mixed transcriptome and CHiC. scCHiC: 3bp umi followed by 8bp barcode and a single A. R2 ends with a 6bp random primer. Transcriptome: cs2 + template switching oligo'
self.shortName = 'CHICTV'
self.autoDetectable = False
# The demultiplexer used for the chic reads:
self.chic_demux = SCCHIC_384w_c8_u3(barcodeFileParser=barcodeFileParser,random_primer_read=None,**kwargs)
self.barcodeSummary = self.chic_demux.barcodeSummary
self.longName = f'{self.chic_demux.longName} and TV primer '
def __repr__(self):
return f'{self.longName} {self.description}'
def demultiplex(self, records, **kwargs):
# Check if the supplied reads are mate-pair:
if len(records) != 2:
raise NonMultiplexable('Not mate pair')
# Check if the TSO oligo is present..
if 'AGACTCTTT' in records[0].sequence:
taggedRecords = self.chic_demux.demultiplex(records,**kwargs)
if not 'AGACTCTTT' in taggedRecords[0].sequence:
raise NonMultiplexable('No match to transcriptome or CHiC')
oligo_position = taggedRecords[0].sequence.index('AGACTCTTT')
umi_start = max(0,oligo_position-6)
umi = taggedRecords[0].sequence[umi_start:oligo_position]
# Set umi :
for r in taggedRecords:
r.tags['tu'] = umi
r.tags['MX'] = "CTV"
# Clip the read down:
taggedRecords[0].sequence = taggedRecords[0].sequence[:oligo_position]
taggedRecords[0].qualities = taggedRecords[0].qualities[:oligo_position]
return taggedRecords
raise NonMultiplexable('No match to transcriptome or CHiC')
class SCCHIC_384w_c8_u3_cs2(UmiBarcodeDemuxMethod):
def __init__(self, barcodeFileParser, random_primer_read=None,random_primer_length=None, **kwargs):
self.barcodeFileAlias = 'maya_384NLA'
UmiBarcodeDemuxMethod.__init__(
self,
umiRead=0,
umiStart=0,
umiLength=3,
barcodeRead=0,
barcodeStart=3,
barcodeLength=8,
random_primer_read=random_primer_read,
random_primer_length=random_primer_length,
barcodeFileAlias=self.barcodeFileAlias,
barcodeFileParser=barcodeFileParser,
**kwargs)
self.description = '384 well format, mixed transcriptome and CHiC. scCHiC: 3bp umi followed by 8bp barcode and a single A. R2 has no random primer. Transcriptome is VASA'
self.shortName = 'TCHIC'
self.autoDetectable = False
# The demultiplexer used for the transcriptome reads:
self.transcriptome_demux = CELSeq2_c8_u6(barcodeFileParser=barcodeFileParser,**kwargs)
# Contains expected bleedthrough sequence
self.id_to_cs2_barcode = { v:k + 'TTTTT' for k,v in barcodeFileParser['celseq2'].items() }
assert len(self.id_to_cs2_barcode)>0
self.tx_umi_len = 6
# The demultiplexer used for the chic reads:
self.chic_demux = SCCHIC_384w_c8_u3_direct_ligation(barcodeFileParser=barcodeFileParser,**kwargs)
self.barcodeSummary = f'{self.chic_demux.barcodeSummary} and {self.transcriptome_demux.barcodeSummary}'
self.longName = f'{self.chic_demux.longName} and {self.transcriptome_demux.longName}'
self.poly_length=10
self.poly_A = self.poly_length*'A'
self.poly_T = self.poly_length*'T'
self.poly_G = self.poly_length*'G'
self.r2_trimmer = re.compile('[GA]*$')
self.sequenceCapture[0] = slice(
self.barcodeLength + self.umiLength + 1,
None) # dont capture the first base
def trim_r2(self, sequence, qualities ):
start = sequence.find(self.poly_A)
if start != -1:
sequence, qualities = sequence[:start], qualities[:start]
start = sequence.find(self.poly_G)
if start != -1:
sequence, qualities = sequence[:start], qualities[:start]
# Trim any trailing A and G bases from the end and # Trim down 3 bases
sequence = self.r2_trimmer.sub('',sequence)[:-3]
qualities = qualities[:len(sequence)]
return sequence, qualities
def __repr__(self):
return f'{self.longName} {self.description}'
def extract_vasa_umi(self, sequence, vasa_barcode):
# Extract vasa umi from sequence given the vasa barcode
# Returns None when the it cannot be extracted
vasa_umi_end = sequence.find(vasa_barcode)
vasa_umi_start = max(0,vasa_umi_end - self.tx_umi_len)
if vasa_umi_end - vasa_umi_start > 0:
return sequence[vasa_umi_start:vasa_umi_end]
return None
def demultiplex(self, records, **kwargs):
# Check if the supplied reads are mate-pair:
if len(records) != 2:
raise NonMultiplexable('Not mate pair')
# add first 2 bases as ligation tag:
ligation_start = self.barcodeLength + self.umiLength
ligation_end = ligation_start + 2
ligation_sequence = records[0].sequence[ligation_start:ligation_end]
ligation_qualities = records[0].qual[ligation_start:ligation_end]
# Obtain the chic barcode and umi:
try:
taggedRecords = UmiBarcodeDemuxMethod.demultiplex(self,records, **kwargs)
except NonMultiplexable:
raise
# Trim ligation motif:
# add first 2 bases as ligation tag:
ud = {
'lh':ligation_sequence,
'lq':phredToFastqHeaderSafeQualities(ligation_qualities),
'dt':'CHIC'
}
taggedRecords[0].tags.update(ud)
taggedRecords[1].tags.update(ud)
# Check for tx contamination:
expected_barcode = self.id_to_cs2_barcode.get( taggedRecords[0].tags['bi'] )
if expected_barcode is None:
raise ValueError(taggedRecords[0].tags['bi'])
if expected_barcode in taggedRecords[0].sequence or expected_barcode in reverse_complement(taggedRecords[1].sequence):
# tx contaminant:
# Obtain vasa UMI:
if expected_barcode in taggedRecords[0].sequence:
# NNNNNNNNN [UMI] BC POLY T
tx_umi = self.extract_vasa_umi(taggedRecords[0].sequence, expected_barcode)
else:
rc2 = reverse_complement(taggedRecords[1].sequence)
tx_umi = self.extract_vasa_umi(rc2, expected_barcode)
# Trim read2 down
taggedRecords[1].sequence, taggedRecords[1].qualities = self.trim_r2(taggedRecords[1].sequence, taggedRecords[1].qualities)
for r in taggedRecords:
r.tags['dt'] = 'VASA'
if tx_umi is not None:
r.tags['rx'] = tx_umi
#r.tags['MX'] = 'CS2'
return taggedRecords
elif 'TTTTTTTTTTTTTTTTTTTTTTT' in taggedRecords[0].sequence or 'TTTTTTTTTTTTTTTTTTTTTTT' in taggedRecords[1].sequence:
raise NonMultiplexable('PolyT')
elif 'AGTCCGACGAT' in taggedRecords[0].sequence[:30] or 'GTTCTACAGT' in taggedRecords[0].sequence[:30] or 'TAATACGACTCACTATAGGG' in taggedRecords[0].sequence:
# desc = 'none?'
# if 'AGTCCGACGAT' in taggedRecords[0].sequence[:30]:
# desc = 'AGTCCGACGAT'
# elif 'GTTCTACAGT' in taggedRecords[0].sequence[:30]:
# desc = 'GTTCTACAGT'
# elif 'TAATACGACTCACTATAGGG' in taggedRecords[0].sequence:
# desc = 'TAATACGACTCACTATAGGG'
for r in taggedRecords:
r.tags['dt'] = 'VASA'
r.tags['RR'] = 'T7_found'
#r.tags['TR'] = desc
return taggedRecords