[45ad7e]: / singlecellmultiomics / modularDemultiplexer / baseDemultiplexMethods.py

Download this file

782 lines (638 with data), 30.4 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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
import singlecellmultiomics.fastqProcessing.fastqIterator as fastqIterator
import string
from singlecellmultiomics.utils.sequtils import hamming_distance
from singlecellmultiomics.tags import *
complement = {'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A'}
TagDefinitions = {tag.tag: tag for tag in tags}
# Obtain metadata from read
def metaFromRead(read, tag):
if tag == 'chrom':
return read.reference_name
if read.has_tag(tag):
return read.get_tag(tag)
# Backwards compatibility with BI > bi tag:
if tag=='BI' and read.has_tag('bi'):
return read.get_tag('bi')
# Forwards compatibility:
if tag=='bi' and read.has_tag('BI'):
return read.get_tag('BI')
try:
return getattr(read, tag)
except Exception as e:
pass
# print(e)
return None
# Clean a string to be able to be used in a fastq file header
fastqCleanerRegex = re.compile('[^a-zA-Z0-9-_]', re.UNICODE)
def fqSafe(string) -> str:
"""
Convert input string into a representation which can be stored in a fastq header
Input:
string(str) : string to clean
Returns:
cleaned(str)
"""
global fastqCleanerRegex
return(fastqCleanerRegex.sub('', string))
illuminaHeaderSplitRegex = re.compile(':| ', re.UNICODE)
class TaggedRecord():
def __init__(
self,
tagDefinitions,
rawRecord=False,
library=None,
reason=None,
**kwargs):
self.tags = {} # 2 character Key -> value
self.tagDefinitions = tagDefinitions
if rawRecord is not False:
try:
self.fromRawFastq(rawRecord, **kwargs)
except NonMultiplexable:
raise
if library is not None and not 'LY' in self.tags:
self.tags['LY'] = library
if reason is not None:
self.tags['RR'] = reason
if isinstance(rawRecord, fastqIterator.FastqRecord):
self.sequence = rawRecord.sequence
self.qualities = rawRecord.qual
self.plus = rawRecord.plus
def addTagByTag(
self,
tagName,
value,
isPhred=None,
decodePhred=False,
cast_type=str,
make_safe=True):
if isPhred is None:
isPhred = self.tagDefinitions[tagName].isPhred
if cast_type and not isinstance(value, cast_type):
value = cast_type(value)
if isPhred:
if decodePhred:
# Convert encoded phred scores back to original ascii
self.tags[tagName] = fastqHeaderSafeQualitiesToPhred(
value, method=3)
else:
self.tags[tagName] = phredToFastqHeaderSafeQualities(
value, method=3)
else:
if cast_type is str:
if make_safe:
self.tags[tagName] = fqSafe(value)
else:
self.tags[tagName] = value
else:
self.tags[tagName] = value
def __repr__(self):
return self.asFastq()
def asFastq(
self,
sequence=None,
dirAtt=None,
baseQualities=None,
format='illumina'):
if sequence is None:
if self.sequence is None:
raise ValueError()
sequence = self.sequence
if dirAtt is None:
if self.plus is None:
raise ValueError()
dirAtt = self.plus
if baseQualities is None:
if self.qualities is None:
raise ValueError()
baseQualities = self.qualities
header = ";".join([f"{attribute}:{value}" for attribute, value in self.tags.items(
) if not self.tagDefinitions[attribute].doNotWrite])
if len(header) > 255: # the header length is stored as uint_8 and includes a null character. The maximum length is thus 255
raise ValueError(
f"The length of the demultiplexed header is longer than 255 characters. Try to keep your library name below 60 characters. Reduce the length of the header. For example by using -merge _ which will not put the flow cell in the sample name. The header looks like this: {header}")
return f'@{header}\n{sequence}\n{dirAtt}\n{baseQualities}\n'
def has_tag(self, tag):
return tag in self.tags
def asIlluminaHeader(self):
return '{Is}:{RN}:{Fc}:{La}:{Ti}:{CX}:{CY}'.format(**self.tags)
def parse_3dec_header(self,fastqRecord, indexFileParser, indexFileAlias):
instrument = 'UNK'
runNumber = 'UNK'
flowCellId = 'UNK'
indexSequence = 'N'
lane = 'UNK'
tile = 'UNK'
clusterXpos = '-1'
clusterYpos = '-1'
readPairNumber = '0'
isFiltered = '0'
controlNumber = '0'
# 3-DEC: @Cluster_s_1_1101_2
if fastqRecord.header.count('_') == 4:
_cluster_, _s_, lane, tile, readPairNumber = fastqRecord.header.split(
'_')
# check that this s thingy is at the right place
assert(_s_ == 's')
else:
raise
self.tags.update({
'Is': instrument,
'RN': runNumber,
'Fc': flowCellId,
'La': lane,
'Ti': tile,
'CX': clusterXpos,
'CY': clusterYpos,
'RP': readPairNumber,
'Fi': isFiltered,
'CN': controlNumber
})
def _parse_illumina_header(self,header, indexFileParser = None, indexFileAlias = None):
try:
instrument, runNumber, flowCellId, lane, tile, clusterXpos, clusterYpos, readPairNumber, isFiltered, controlNumber, indexSequence = header.replace(' ',':').split(':')
except BaseException:
try:
instrument, runNumber, flowCellId, lane, tile, clusterXpos, clusterYpos, readPairNumber, isFiltered, controlNumber = illuminaHeaderSplitRegex.split(
header.replace('::', ''))
indexSequence = "N"
except BaseException:
try:
instrument, runNumber, flowCellId, lane, tile, clusterXpos, clusterYpos = header.split(':')
indexSequence = "N"
readPairNumber = 1
isFiltered = 0
controlNumber = 0
except BaseException:
raise
self.tags.update({
'Is': instrument,
'RN': runNumber,
'Fc': flowCellId,
'La': lane,
'Ti': tile,
'CX': clusterXpos,
'CY': clusterYpos,
'RP': readPairNumber,
'Fi': isFiltered,
'CN': controlNumber
})
if indexFileParser is not None and indexFileAlias is not None:
# Check if the index is an integer:
try:
indexInteger = int(indexSequence)
indexIdentifier, correctedIndex, hammingDistance = indexSequence, indexSequence, 0
except ValueError:
indexIdentifier, correctedIndex, hammingDistance = indexFileParser.getIndexCorrectedBarcodeAndHammingDistance(
alias=indexFileAlias, barcode=indexSequence)
self.tags['aa'] = indexSequence
if correctedIndex is not None:
#
#self.addTagByTag('aA',correctedIndex, isPhred=False)
#self.addTagByTag('aI',indexIdentifier, isPhred=False)
self.tags.update({'aA': correctedIndex, 'aI': indexIdentifier})
else:
raise NonMultiplexable(
'Could not obtain index for %s %s %s' %
(indexSequence, correctedIndex, indexIdentifier))
# self.addTagByTag('aA',"None")
# self.addTagByTag('aI',-1)
# self.addTagByTag('ah',hammingDistance)
else:
#self.addTagByTag('aA',indexSequence, isPhred=False)
self.tags['aa'] = indexSequence
def parse_illumina_header(self,fastqRecord, indexFileParser = None, indexFileAlias = None):
return self._parse_illumina_header(fastqRecord.header, indexFileParser=indexFileParser, indexFileAlias=indexFileAlias )
def parse_scmo_header(self, fastqRecord, indexFileParser, indexFileAlias):
self.tags.update( dict( kv.split(':') for kv in fastqRecord.header.strip()[1:].split(';') ) )
def fromRawFastq(
self,
fastqRecord,
indexFileParser=None,
indexFileAlias=None):
try:
self.parse_illumina_header(fastqRecord, indexFileParser, indexFileAlias)
except BaseException:
if fastqRecord.header.startswith('@Is'):
self.parse_scmo_header(fastqRecord, indexFileParser, indexFileAlias)
else:
self.parse_3dec_header(fastqRecord, indexFileParser, indexFileAlias)
# NS500413:32:H14TKBGXX:2:11101:16448:1664 1:N:0::
""" This is the nice and safe way:
self.addTagByTag( 'Is',instrument, isPhred=False)
self.addTagByTag('RN',runNumber, isPhred=False)
self.addTagByTag('Fc',flowCellId, isPhred=False)
self.addTagByTag('La',lane, isPhred=False)
self.addTagByTag('Ti',tile, isPhred=False)
self.addTagByTag('CX',clusterXpos, isPhred=False)
self.addTagByTag('CY',clusterYpos, isPhred=False)
self.addTagByTag('RP',readPairNumber, isPhred=False)
self.addTagByTag('Fi',isFiltered, isPhred=False)
self.addTagByTag('CN',controlNumber, isPhred=False)
"""
def tagPysamRead(self, read):
moleculeIdentifier = ""
moleculeQuality = ""
moleculeIdentifiyingTags = [
('BC', 'QT', False),
('RX', 'RQ', False),
('aA', None, True)
]
try:
QT_missing=False
for tag, qualityTag, required in moleculeIdentifiyingTags:
if self.has_tag(tag) and not self.tags.get(tag) is None:
moleculeIdentifier += self.tags[tag]
if qualityTag is None: # Padding:
moleculeQuality += ('o' * len(self.tags[tag]))
else:
if qualityTag in self.tags:
moleculeQuality += self.tags[qualityTag]
elif qualityTag=='QT':
QT_missing = True
if required and not self.has_tag(tag):
raise NonMultiplexable('Tag was defined to be required')
# if len(moleculeQuality)!=len(moleculeIdentifier):
# raise ValueError('Could not reconstruct molecule identifier')
# @todo set this back when we recover QT
correctedIndex = None if not self.has_tag(
'aA') else self.tags['aA']
indexSequence = None if not self.has_tag('aa') else self.tags['aa']
if correctedIndex is not None and indexSequence is not None:
hd = hamming_distance(indexSequence, correctedIndex)
if hd is None:
raise ValueError(
"Could not resolve hamming distance between {correctedIndex} and {indexSequence}")
self.addTagByTag('ah', hd, isPhred=False, cast_type=int)
self.addTagByTag('MI', moleculeIdentifier, isPhred=False)
if not QT_missing:
self.addTagByTag('QM', moleculeQuality, isPhred=False)
except NonMultiplexable:
# Its bulk
self.tags['BK'] = True
# Add sample tag: (If possible)
# If the bi tag is present it means we know the index of the cell
# if no bi tag is present, assume the sample is bulk
if 'bi' in self.tags:
self.addTagByTag(
'SM',
f'{self.tags["LY"]}_{self.tags["bi"]}',
isPhred=False)
elif 'BI' in self.tags:
self.addTagByTag(
'SM',
f'{self.tags["LY"]}_{self.tags["BI"]}',
isPhred=False)
# Remove BI tag
self.tags['bi'] = self.tags["BI"]
del self.tags['BI']
elif "LY" in self.tags:
self.addTagByTag('SM', f'{self.tags["LY"]}_BULK', isPhred=False)
# Now we defined the desired values of the tags. Write them to the
# record:
for tag, value in self.tags.items():
# print(tag,value)
if tag in self.tagDefinitions and self.tagDefinitions[tag].isPhred:
value = fastqHeaderSafeQualitiesToPhred(value, method=3)
read.set_tag(tag, value)
if not QT_missing and read.has_tag('QM') and len(
read.get_tag('QM')) != len(
read.get_tag('MI')):
raise ValueError('QM and MI tag length not matching')
def fromTaggedFastq(self, fastqRecord):
for keyValue in fastqRecord.header.replace('@', '').strip().split(';'):
key, value = keyValue.split(':')
self.addTagByTag(key, value, decodePhred=True)
def fromTaggedBamRecord(self, pysamRecord):
try:
for keyValue in pysamRecord.query_name.strip().split(';'):
key, value = keyValue.split(':')
self.addTagByTag(key, value, isPhred=False)
except ValueError:
# Try to parse "Single Cell Discoveries" header
# These have the following header:
#NBXXXXXX:530:HXXXXXX:2:2:17:6;SS:GTCATTAG;CB:GTCATTAG;QT:eeeeeeee;RX:CTGAAC;RQ:aaaaae;SM:SAMPLE_NAME
illumina_header, attributes = pysamRecord.query_name.strip().split(';',1)
self._parse_illumina_header(illumina_header, indexFileParser=None, indexFileAlias=None )
for keyValue in attributes.split(';'):
key, value = keyValue.split(':')
self.addTagByTag(key, value, isPhred=False)
def reverseComplement(seq):
global complement
return("".join(complement.get(base, base) for base in reversed(seq)))
def phredToFastqHeaderSafeQualities(asciiEncodedPhredScores, method=3):
""" Convert ASCII encoded pred string to fastq safe string.
numeric encoded string (method 0),
or 65 shifted (method 1) which is safe to use in the fastq header"""
if method == 0:
return(",".join([str(ord(phred) - 33) for phred in asciiEncodedPhredScores]))
elif method == 1:
return("".join([chr(ord(phred) + 32) for phred in asciiEncodedPhredScores]))
else:
return("".join([string.ascii_letters[min(max(0, ord(phred) - 33), len(string.ascii_letters))] for phred in asciiEncodedPhredScores]))
def fastqHeaderSafeQualitiesToPhred(phred, method=3):
# print(phred)
return "".join((chr(string.ascii_letters.index(v) + 33) for v in phred))
class NonMultiplexable(Exception):
pass
# The demultplexing strategy converts a tuple if fastq record(s) into a
# demultiplexed record
# Every demultiplexing strategy has :
# a full name: the name shown in the tool
# shortName : a short name, will be put into EVERY fastq record, so keep it really short
# autoDetectable: a flag indicating if this method should be auto detected;
# for example for whole genome sequencing reads, we cannot tell from the
# reads that it is this data, and the flag should be False
# Method demultiplex( *records (R1, R2, R3 ... )
# Raises NonMultiplexable Exception if the records do not yield a valid
# result (which is used to determine if the demultplexing method is valid
# to use)
# Upon initialisation the strategies recieve a dictionary containing the barcodes loaded by the barcode file parser
# (barcodeMapping)
class DemultiplexingStrategy(object):
def __init__(self):
self.shortName = 'place holder demultiplexing method'
self.longName = 'placeHolder'
self.autoDetectable = False
self.description = 'inherit this class to build your own demultipexing strategy'
self.indexSummary = ''
self.barcodeSummary = ''
def demultiplex(self, records, **kwargs):
raise NotImplementedError()
def __repr__(self):
return f'{self.longName} {self.shortName} {self.description} DemultiplexingStrategy'
def getParserSummary(self):
return(' ' + self.indexSummary + '\n barcodes:' + self.barcodeSummary)
class IlluminaBaseDemultiplexer(DemultiplexingStrategy):
def __init__(
self,
indexFileParser,
indexFileAlias='illumina_merged_ThruPlex48S_RP',
**kwargs):
DemultiplexingStrategy.__init__(self)
self.indexFileParser = indexFileParser
self.illuminaIndicesAlias = indexFileAlias
self.shortName = 'ILLU'
self.longName = 'IlluminaDemux'
self.description = 'Demultiplex as a bulk sample'
self.indexSummary = f'sequencing indices: {indexFileAlias}'
def demultiplex(
self,
records,
inherited=False,
library=None,
reason=None,
**kwargs):
global TagDefinitions
try:
if inherited:
return [
TaggedRecord(
rawRecord=record,
tagDefinitions=TagDefinitions,
indexFileParser=self.indexFileParser,
indexFileAlias=self.illuminaIndicesAlias,
library=library,
reason=reason) for record in records]
else:
return [
TaggedRecord(
rawRecord=record,
tagDefinitions=TagDefinitions,
indexFileParser=self.indexFileParser,
indexFileAlias=self.illuminaIndicesAlias,
library=library,
reason=reason).asFastq(
record.sequence,
record.plus,
record.qual) for record in records]
except NonMultiplexable:
raise
# Base strategy for read pairs which have both an umi and sample barcode, the barcode and umi are next to another and continuous
class UmiBarcodeDemuxMethod(IlluminaBaseDemultiplexer):
def __init__(
self,
umiRead=0,
umiStart=0,
umiLength=6,
barcodeRead=0,
barcodeStart=6,
barcodeLength=8,
barcodeFileParser=None,
barcodeFileAlias=None,
indexFileParser=None,
indexFileAlias='illumina_merged_ThruPlex48S_RP',
random_primer_read=None,
random_primer_length=6,
random_primer_end=False, # True for end, False for start
**kwargs):
self.description = ''
self.barcodeFileAlias = barcodeFileAlias
self.barcodeFileParser = barcodeFileParser
IlluminaBaseDemultiplexer.__init__(
self,
indexFileParser=indexFileParser,
indexFileAlias=indexFileAlias)
self.barcodeSummary = self.barcodeFileAlias
self.umiRead = umiRead # 0:Read 1, 1: Read 2 etc
self.umiStart = umiStart # First base
self.umiLength = umiLength
self.barcodeRead = barcodeRead
self.barcodeStart = barcodeStart
self.barcodeLength = barcodeLength
self.autoDetectable = False
self.random_primer_read = random_primer_read
self.random_primer_length = random_primer_length
self.random_primer_end = random_primer_end
# ranges to capture for read 1 and read 2
self.sequenceCapture = [slice(None), slice(None)]
if umiLength == 0:
# Barcode only
if barcodeStart != 0:
raise NotImplementedError(
'Complicated slice where we need to capture around a region')
self.sequenceCapture[barcodeRead] = slice(barcodeLength, None)
else:
if umiRead != barcodeRead:
raise NotImplementedError()
if not(umiStart == 0 or barcodeStart == 0):
raise NotImplementedError(
'Complicated slice where we need to capture around a region')
self.sequenceCapture[barcodeRead] = slice(
barcodeLength + umiLength, None)
if random_primer_read is not None:
if self.sequenceCapture[random_primer_read].stop is not None:
raise NotImplementedError()
if random_primer_end:
self.sequenceCapture[random_primer_read] = slice(
self.sequenceCapture[random_primer_read].start,
-random_primer_length,
self.sequenceCapture[random_primer_read].step
)
self.random_primer_slice = slice(-random_primer_length, None, None)
else:
self.sequenceCapture[random_primer_read] = slice(
random_primer_length,
None,
self.sequenceCapture[random_primer_read].step
)
self.random_primer_slice = slice(0, random_primer_length, None)
def __repr__(self):
try:
return f'{self.longName} bc: {self.barcodeStart}:{self.barcodeLength}, umi: {self.umiStart}:{self.umiLength} {self.description}'
except AttributeError: # Happens when this class is inherited and some of the attributes might have not been set
return f'{self.longName}, {self.description}'
def demultiplex(self, records, **kwargs):
# Check if the supplied reads are mate-pair or single end
if len(records) not in (1, 2):
raise NonMultiplexable('Not mate pair or single end')
# Perform first pass demultiplexing of the illumina fragments:
try:
taggedRecords = IlluminaBaseDemultiplexer.demultiplex(
self, records, inherited=True, **kwargs)
except NonMultiplexable:
raise
rawBarcode = records[self.barcodeRead].sequence[self.barcodeStart:
self.barcodeStart + self.barcodeLength]
barcodeQual = records[self.barcodeRead].qual[self.barcodeStart:
self.barcodeStart + self.barcodeLength]
barcodeIdentifier, barcode, hammingDistance = self.barcodeFileParser.getIndexCorrectedBarcodeAndHammingDistance(
alias=self.barcodeFileAlias, barcode=rawBarcode)
#print(self.barcodeFileParser,self.barcodeFileAlias,rawBarcode,barcodeIdentifier, barcode, hammingDistance)
if barcodeIdentifier is None:
raise NonMultiplexable(
f'bc:{rawBarcode}_not_matching_{self.barcodeFileAlias}')
random_primer = None
if self.random_primer_read is not None:
random_primer = records[self.random_primer_read].sequence[self.random_primer_slice]
if self.umiLength != 0:
umi = records[self.umiRead].sequence[self.umiStart:self.umiStart + self.umiLength]
umiQual = records[self.umiRead].qual[self.umiStart:self.umiStart + self.umiLength]
for tr in taggedRecords:
#tr.addTagByTag('uL', self.umiLength, isPhred=False)
if self.umiLength == 0:
#tr.addTagByTag('MI', barcode, isPhred=False)
#tr.addTagByTag('QM', barcodeQual, isPhred=True)
pass
else:
tr.tags['RX'] = umi
tr.addTagByTag('RQ', umiQual, isPhred=True,cast_type=None)
#tr.addTagByTag('MI', barcode+umi, isPhred=False)
#tr.addTagByTag('QM', barcodeQual+umiQual, isPhred=True)
""" These can be updated at once
tr.addTagByTag('bi', barcodeIdentifier, isPhred=False)
tr.addTagByTag('bc', rawBarcode, isPhred=False)
tr.addTagByTag('MX', self.shortName, isPhred=False)
tr.addTagByTag('BC', barcode, isPhred=False )
"""
tr.tags.update({
'bi': barcodeIdentifier,
'bc': rawBarcode,
'MX': self.shortName,
'BC': barcode
})
#tr.addTagByTag('hd', hammingDistance, isPhred=False)
if random_primer is not None:
tr.addTagByTag('rS',
random_primer,
isPhred=False,
make_safe=False)
#tr.addTagByTag('QT', barcodeQual, isPhred=True)
if len(barcode) != len(barcodeQual):
raise ValueError()
for rid, (record, taggedRecord) in enumerate(
zip(records, taggedRecords)):
taggedRecord.sequence = record.sequence[self.sequenceCapture[rid]]
taggedRecord.qualities = record.qual[self.sequenceCapture[rid]]
taggedRecord.plus = record.plus
return taggedRecords
# return [ tr.asFastq(record.sequence[self.sequenceCapture[rid]], record.plus, record.qual[self.sequenceCapture[rid]]) for rid,(tr,record) in enumerate(zip(taggedRecords, records))]
# Add information and rebuild header
#header = f'@UMI:{umi};UMIQ:{umiQual};CBI:{barcodeIdentifier};CB:{barcode};CBQ:{barcodeQual};'
# return fastqIterator.FastqRecord(header, records[1].sequence,
# records[1].plus, records[1].qual )
# Base strategy for read pairs which have both an umi and sample barcode, but are not contiguous,
# for example UMI-BCA-UMI_BCB
def apply_slices_seq(record, slices):
if len(slices)==0 or record is None:
return ''
return ''.join( (record.sequence[sc] for sc in slices))
def apply_slices_qual(record, slices):
if len(slices)==0 or record is None:
return ''
return ''.join( (record.qual[sc] for sc in slices))
class ScatteredUmiBarcodeDemuxMethod(IlluminaBaseDemultiplexer):
def __init__(
self,
barcode_slices = None, # 2-len Tuple of List of slices where the cell barcode is present in the read (slice(), slice(), ...], [ slice())
umi_slices = None, # 2-len List of slices where the UMI is present in the read [slice(), slice(), ...], [slice()] #
capture_slices = None, # Slices which indicate what region of read 1 and read2 to store in the final read (required) (slice, slice)
# Note that there is only one slice per read for the capture
barcodeFileParser=None,
barcodeFileAlias=None,
indexFileParser=None,
indexFileAlias='illumina_merged_ThruPlex48S_RP',
random_primer_read=None,
random_primer_length=6,
random_primer_end=False, # True for end, False for start
**kwargs):
self.description = ''
self.barcodeFileAlias = barcodeFileAlias
self.barcodeFileParser = barcodeFileParser
IlluminaBaseDemultiplexer.__init__(
self,
indexFileParser=indexFileParser,
indexFileAlias=indexFileAlias)
self.barcodeSummary = self.barcodeFileAlias
self.barcode_slices = barcode_slices
self.umi_slices = umi_slices
self.capture_slices = capture_slices
self.random_primer_read = random_primer_read
self.random_primer_length = random_primer_length
self.random_primer_end = random_primer_end
def demultiplex(self, records, **kwargs):
# Check if the supplied reads are mate-pair or single end
if len(records) not in (1, 2):
raise NonMultiplexable('Not mate pair or single end')
# Perform first pass demultiplexing of the illumina fragments:
try:
taggedRecords = IlluminaBaseDemultiplexer.demultiplex(
self, records, inherited=True, **kwargs)
except NonMultiplexable:
raise
# Extract the UMI barcode_slices
umi, umi_qual = ''.join( (apply_slices_seq(record, slicer) for record, slicer in zip(records, self.umi_slices)) ), ''.join( (apply_slices_qual(record, slicer) for record, slicer in zip(records, self.umi_slices)) )
raw_barcode, raw_barcode_qual = ''.join( (apply_slices_seq(record, slicer) for record, slicer in zip(records, self.barcode_slices)) ), ''.join( (apply_slices_qual(record, slicer) for record, slicer in zip(records, self.barcode_slices)) )
barcodeIdentifier, barcode, hammingDistance = self.barcodeFileParser.getIndexCorrectedBarcodeAndHammingDistance(
alias=self.barcodeFileAlias, barcode=raw_barcode)
if barcodeIdentifier is None:
raise NonMultiplexable(
f'bc:{raw_barcode}_not_matching_{self.barcodeFileAlias}')
random_primer = None
if self.random_primer_read is not None:
random_primer = records[self.random_primer_read].sequence[self.random_primer_slice]
for tr in taggedRecords:
#tr.addTagByTag('uL', self.umiLength, isPhred=False)
if len(umi)>0:
tr.tags['RX'] = umi
tr.addTagByTag('RQ', umi_qual, isPhred=True,cast_type=None)
tr.tags.update({
'bi': barcodeIdentifier,
'bc': raw_barcode,
'MX': self.shortName,
'BC': barcode
})
if random_primer is not None:
tr.addTagByTag('rS',
random_primer,
isPhred=False,
make_safe=False)
for rid, (record, taggedRecord) in enumerate(
zip(records, taggedRecords)):
taggedRecord.sequence = record.sequence[self.capture_slices[rid]]
taggedRecord.qualities = record.qual[self.capture_slices[rid]]
taggedRecord.plus = record.plus
return taggedRecords