-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdemultiplex.py
executable file
·1666 lines (1492 loc) · 67.4 KB
/
demultiplex.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
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
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Aim: demultiplex samples in fastq files
# Copyright (C) 2014-2017 Institut National de la Recherche Agronomique
# License: GPL-3+
# Persons: Timothée Flutre [cre,aut], Laurène Gay [ctb], Nicolas Rode [ctb]
# Versioning: https://github.com/timflutre/quantgen
# Inspired from:
# https://gist.github.com/seandavi/3015625 by Sean Devi
# http://bcb.io/2009/08/09/trimming-adaptors-from-short-read-sequences/ by Brad Chapman
# http://news.open-bio.org/news/2009/09/biopython-fast-fastq/ by Peter Cock
# Tests:
# $ python -m doctest demultiplex.py
# $ ./test_demultiplex.py -p ~/src/quantgen/demultiplex.py
# TODO:
# when --subst > 0, allow to decrease nb subst depending on the pattern (i.e. not for all)
# when --subst > 0, if several possible assignments, choose best (lower subst, higher length)
# try https://github.com/faircloth-lab/splitaake/blob/master/bin/splitaake_reads_many_gz.py
# try https://humgenprojects.lumc.nl/svn/fastools/trunk/fastools/demultiplex.py
# try https://github.com/pelinakan/UBD
# try http://hannonlab.cshl.edu/fastx_toolkit/commandline.html#fastx_barcode_splitter_usage
# try http://www.dnabaser.com/download/nextgen-fastq-editor/index.html
# to allow code to work with Python 2 and 3
from __future__ import print_function # print is a function in python3
from __future__ import unicode_literals # avoid adding "u" to each string
from __future__ import division # avoid writing float(x) when dividing by x
import sys
import os
import getopt
import time
import datetime
from subprocess import Popen, PIPE
import math
import gzip
import re
import itertools
import pip
import pkg_resources
try:
import regex
regexIsImported = True
except ImportError:
regexIsImported = False
if sys.version_info[0] == 2:
if sys.version_info[1] < 7:
msg = "ERROR: Python should be in version 2.7 or higher"
sys.stderr.write("%s\n\n" % msg)
sys.exit(1)
## check dependencies
installed_packages = pip.get_installed_distributions()
versions = {package.key: package.version for package in installed_packages}
deps = {"biopython": "1.64"}
for depN,depV in deps.items():
msg = "ERROR: %s depends on %s in version %s or higher" % \
(os.path.basename(sys.argv[0]), depN, depV)
if depN not in versions:
sys.stderr.write("%s\n" % msg)
sys.exit(1)
if pkg_resources.parse_version(versions[depN]) \
< pkg_resources.parse_version(depV):
sys.stderr.write("%s\n" % msg)
sys.exit(1)
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from Bio.Alphabet import IUPAC
from Bio import Restriction
from Bio.Data.IUPACData import ambiguous_dna_values
from Bio.SeqIO.QualityIO import FastqGeneralIterator
progVersion = "1.15.0" # http://semver.org/
class Demultiplex(object):
"""
Class performing demultiplexing.
"""
def __init__(self):
self.verbose = 1
self.inDir = "."
self.inFqFile1 = None
self.inFqFile2 = None # optional for single reads
self.tagFile = None
self.outPrefix = None
self.method = None
self.dist = -1
self.restrictEnzyme = None # object from Biopython
self.findChimeras = "1"
self.clipIdx = True
self.dTags = {}
# keys are tag sequences as string from the input file
# values are dictionaries:
# key "sample" contains the sample identifier as string
# key "re" contains the pattern as compiled regexp
# e.g. with method 4c:
# self.dTags["AATAG"] = {"sample": "ind32",
# "re": <object>}
self.cutMotif = "" # e.g. "G^CWG_C"
self.regexpCutMotif = "" # used if findChimeras != 0; e.g. "GC[AT]GC"
self.regexpCompilCutMotif = "" # used if findChimeras != 0; object
self.lenRemainMotif = -1 # e.g. 4
self.regexpRemainMotif = "" # regexp of remain motif as string; e.g. "C[AT]GC"
self.dInd2NbAssigned = {}
self.nbSubstitutionsAllowed = 0 # requires module "regex"
self.enforceSubst = "lenient"
self.onlyComparePatterns = False
self.inFqHandle1 = None
self.inFqHandle2 = None
self.reads1 = None
self.reads2 = None
self.dOutFqHandles = {}
self.nbPairs = 0
self.nbAssignedPairs = 0
self.nbAssignedPairsTwoTags = 0
self.nbAssignedPairsOneTag = 0
self.nbChimeras = 0
self.nbChimerasSite = 0
self.nbChimerasTags = 0
self.nbUnassignedPairs = 0
self.nbUnassignedPairsChimeras = 0
def help(self):
"""
Display the help on stdout.
The format complies with help2man (http://www.gnu.org/s/help2man)
"""
msg = "`%s' demultiplexes samples in fastq files.\n" % os.path.basename(sys.argv[0])
msg += "\n"
msg += "Usage: %s [OPTIONS] ...\n" % os.path.basename(sys.argv[0])
msg += "\n"
msg += "Options:\n"
msg += " -h, --help\tdisplay the help and exit\n"
msg += " -V, --version\toutput version information and exit\n"
msg += " -v, --verbose\tverbosity level (0/default=1/2/3)\n"
msg += " --idir\tpath to the input directory with the fastq files\n"
msg += "\t\tdefault=. (current directory)\n"
msg += "\t\toptional with --compp\n"
msg += " --ifq1\tpath to the first input fastq file\n"
msg += "\t\tcan be compressed with gzip\n"
msg += "\t\toptional with --compp\n"
msg += " --ifq2\tpath to the second input fastq file\n"
msg += "\t\tcan be compressed with gzip\n"
msg += "\t\tabsent means single reads, present means paired-end reads\n"
msg += "\t\terror raised if reads not in same order as --ifq1\n"
msg += "\t\toptional with --compp\n"
msg += " --it\tpath to the tag file\n"
msg += "\t\tonly A, T, G and C (no ambiguous nucleotide allowed)\n"
msg += "\t\tcan be in 2 formats (automatically detected)\n"
msg += "\t\t fasta: put sample names in the fasta headers\n"
msg += "\t\t table: 2 columns, header line should be 'id\\ttag'\n"
msg += "\t\talways compulsory\n"
msg += "\t\tonly 'table' allows to have multiple tags for the same sample\n"
msg += " --ofqp\tprefix for the output fastq files\n"
msg += "\t\t2 for assigned reads per sample if paired-ends, 1 otherwise\n"
msg += "\t\t1 for unassigned reads\n"
msg += "\t\t1 for chimeras\n"
msg += "\t\tsuffix will be \".fastq.gz\" (not \".fq\" because of FastQC)\n"
msg += "\t\twill be compressed with gzip\n"
msg += "\t\tthis prefix will also be used for a gzipped text file\n"
msg += "\t\t counting assigned read pairs per individual\n"
msg += "\t\toptional with --compp\n"
msg += " --met\tmethod to assign pairs of reads to individuals via tags\n"
msg += "\t\tonly forward strand is considered\n"
msg += "\t\talways compulsory\n"
msg += "\t\t1: assign pair if both reads start with the tag\n"
msg += "\t\t requires --ifq2\n"
msg += "\t\t2: assign pair if at least one read starts with the tag\n"
msg += "\t\t requires --ifq2\n"
msg += "\t\t3: same as 2, and also count if one or both reads start with the tag\n"
msg += "\t\t requires --ifq2\n"
msg += "\t\t4a: assign pair if first read starts with tag\n"
msg += "\t\t ignore second read for assignment, but save it if --ifq2 is present\n"
msg += "\t\t4b: assign pair if first read has tag in its first N bases (see --dist)\n"
msg += "\t\t ignore second read for assignment, but save it if --ifq2 is present\n"
msg += "\t\t4c: assign pair if first read has tag and remaining cut site\n"
msg += "\t\t in its first N bases (see --dist and --re)\n"
msg += "\t\t ignore second read for assignment, but save it if --ifq2 is present\n"
msg += "\t\t4d: assign pair if first and/or second read has tag and\n"
msg += "\t\t remaining cut site in its first N bases (see --dist and --re)\n"
msg += "\t\t requires --ifq2\n"
msg += "\t\t PCR chimeras (R1 tag is different than R2 tag) are detected\n"
msg += "\t\t and saved in distinct files than the unassigned\n"
msg += " --subst\tnumber of substitutions allowed\n"
msg += "\t\tdefault=1\n"
msg += "\t\tif > 0, the 'regex' module is required\n"
msg += " --ensubst\tenforce the nb of substitutions allowed\n"
msg += "\t\tdefault=lenient/strict\n"
msg += "\t\t'lenient' starts from the value given via '--subst'\n"
msg += "\t\tand decreases it until all tags are distinguishable\n"
msg += " --dist\tdistance from the read start to search for the tag (in bp)\n"
msg += "\t\tany value > 0 is incompatible with --met 1/2/3/4a\n"
msg += "\t\tany value <= 0 disables it for --met 4b/4c/4d\n"
msg += " --re\tname of the restriction enzyme (e.g. 'ApeKI')\n"
msg += " --chim\tsearch if full restriction site found in R1 and/or R2\n"
msg += "\t\tdefault=1, see --re\n"
msg += "\t\t0: don't search (some chimeras may still be detected if --met 4d)\n"
msg += "\t\t1: if chimera, count as such, try to assign, and save in same files as others\n"
msg += "\t\t2: if chimera, don't even try to assign and save in distinct files\n"
msg += " --nci\tdo not clip the tag when saving the assigned reads\n"
msg += " --compp\tonly compare patterns to be searched\n"
msg += "\t\tuseful to choose how to set '--subst'\n"
msg += "\n"
msg += "Examples:\n"
msg += " %s --ifq1 reads1.fastq.gz --ifq2 reads2.fastq.gz --ifat tags.fa --ofqp test --met 3\n" % os.path.basename(sys.argv[0])
msg += "\n"
msg += "Dependencies:\n"
msg += "Python >= 2.7; Biopython\n"
msg += "\n"
msg += "Report bugs to <[email protected]>."
print(msg); sys.stdout.flush()
def version(self):
"""
Display version and license information on stdout.
The person roles complies with R's guidelines (The R Journal Vol. 4/1, June 2012).
"""
msg = "%s %s\n" % (os.path.basename(sys.argv[0]), progVersion)
msg += "\n"
msg += "Copyright (C) 2014-2017 Institut National de la Recherche Agronomique (INRA).\n"
msg += "License GPL-3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n"
msg += "\n"
msg += "Written by Timothée Flutre [cre,aut], Laurène Gay [ctb], Nicolas Rode [ctb]."
print(msg.encode("utf8")); sys.stdout.flush()
def setAttributesFromCmdLine(self):
"""
Parse the command-line arguments.
"""
try:
opts, args = getopt.getopt(sys.argv[1:], "hVv:",
["help", "version", "verbose=",
"idir=", "ifq1=", "ifq2=", "it=",
"ofqp=", "met=", "subst=", "ensubst=",
"re=", "chim=", "dist=", "nci",
"compp"])
except getopt.GetoptError as err:
sys.stderr.write("%s\n\n" % str(err))
self.help()
sys.exit(2)
for o, a in opts:
if o == "-h" or o == "--help":
self.help()
sys.exit(0)
elif o == "-V" or o == "--version":
self.version()
sys.exit(0)
elif o == "-v" or o == "--verbose":
self.verbose = int(a)
elif o == "--idir":
self.inDir = a
elif o == "--ifq1":
self.inFqFile1 = a
elif o == "--ifq2":
self.inFqFile2 = a
elif o == "--it":
self.tagFile = a
elif o == "--ofqp":
self.outPrefix = a
elif o == "--met":
self.method = a
elif o == "--subst":
self.nbSubstitutionsAllowed = int(a)
elif o == "--ensubst":
self.enforceSubst = a
elif o == "--dist":
self.dist = int(a)
elif o == "--re":
try:
self.restrictEnzyme = Restriction.__dict__[a]
except KeyError:
msg = "ERROR: restriction enzyme %s not recognized" % a
sys.stderr.write("%s\n\n" % msg)
sys.exit(1)
elif o == "--chim":
self.findChimeras = a
elif o == "--nci":
self.clipIdx = False
elif o == "--compp":
self.onlyComparePatterns = True
else:
assert False, "invalid option"
def checkAttributes(self):
"""
Check the values of the command-line parameters.
"""
if not self.onlyComparePatterns:
if not self.inDir:
msg = "ERROR: missing compulsory option --idir"
sys.stderr.write("%s\n\n" % msg)
self.help()
sys.exit(1)
elif not os.path.exists(self.inDir):
msg = "ERROR: can't find dir %s" % self.inDir
sys.stderr.write("%s\n\n" % msg)
self.help()
sys.exit(1)
if not self.inFqFile1:
msg = "ERROR: missing compulsory option --ifq1"
sys.stderr.write("%s\n\n" % msg)
self.help()
sys.exit(1)
else:
self.inFqFile1 = "%s/%s" % (self.inDir, self.inFqFile1)
if not os.path.exists(self.inFqFile1):
msg = "ERROR: can't find file %s" % self.inFqFile1
sys.stderr.write("%s\n\n" % msg)
self.help()
sys.exit(1)
if self.inFqFile2: # optional for single reads
self.inFqFile2 = "%s/%s" % (self.inDir, self.inFqFile2)
if not os.path.exists(self.inFqFile2):
msg = "ERROR: can't find file %s" % self.inFqFile2
sys.stderr.write("%s\n\n" % msg)
self.help()
sys.exit(1)
if not self.outPrefix:
msg = "ERROR: missing compulsory option --ofqp"
sys.stderr.write("%s\n\n" % msg)
self.help()
sys.exit(1)
if not self.tagFile:
msg = "ERROR: missing compulsory option --it"
sys.stderr.write("%s\n\n" % msg)
self.help()
sys.exit(1)
elif not os.path.exists(self.tagFile):
msg = "ERROR: can't find file %s" % self.tagFile
sys.stderr.write("%s\n\n" % msg)
self.help()
sys.exit(1)
if not self.method:
msg = "ERROR: missing compulsory option --met"
sys.stderr.write("%s\n\n" % msg)
self.help()
sys.exit(1)
if self.method not in ["1","2","3","4a","4b","4c","4d","chim"]:
msg = "ERROR: unknown option --met %s" % self.method
sys.stderr.write("%s\n\n" % msg)
self.help()
sys.exit(1)
if self.method in ["1","2","3","4d"] and not self.inFqFile2:
msg = "ERROR: missing compulsory option --ifq2"
sys.stderr.write("%s\n\n" % msg)
self.help()
sys.exit(1)
if (self.method in ["4c","4d"] or self.findChimeras != "0") \
and self.restrictEnzyme == None:
msg = "ERROR: missing compulsory option --re"
sys.stderr.write("%s\n\n" % msg)
self.help()
sys.exit(1)
if self.findChimeras not in ["0","1","2"]:
msg = "ERROR: --chim %s is unknown" % self.findChimeras
sys.stderr.write("%s\n\n" % msg)
self.help()
sys.exit(1)
if self.dist <= 0:
self.dist = -1
if self.method in ["1","2","3","4a"] and self.dist > 0:
msg = "ERROR: --dist %i is incompatible with --met %s" \
% (self.dist, self.method)
sys.stderr.write("%s\n\n" % msg)
self.help()
sys.exit(1)
if self.nbSubstitutionsAllowed > 0 and not regexIsImported:
msg = "ERROR: --subst > 0 but module 'regex' can't be imported"
sys.stderr.write("%s\n\n" % msg)
self.help()
sys.exit(1)
if self.enforceSubst not in ["lenient", "strict"]:
msg = "ERROR: --ensubst %s is unknown" % self.enforceSubst
sys.stderr.write("%s\n\n" % msg)
self.help()
sys.exit(1)
def findTagFileFormatFromLine(self, line):
"""
>>> i = Demultiplex()
>>> line = "id\ttag"
>>> i.findTagFileFormatFromLine(line)
u'table'
>>> line = ">ind001"
>>> i.findTagFileFormatFromLine(line)
u'fasta'
"""
tagFileFormat = None
tokens = line.split()
if line[0] == ">":
tagFileFormat = "fasta"
elif tokens == ["id", "tag"]:
tagFileFormat = "table"
else:
if len(tokens) == 2:
msg = "WARNING: tag file should have a header line 'id\\ttag'"
sys.stderr.write("%s\n" % msg)
tagFileFormat = "table"
else:
msg = "ERROR: tag file seems to be neither in 'fasta' nor 'table' format"
msg += "\n%s" % line
sys.stderr.write("%s\n" % msg)
sys.exit(1)
return tagFileFormat
def findTagFileFormat(self):
"""
Look for a '>' in the tag file and decide to return a 'fasta' or 'table' file format.
"""
tagFileFormat = "table"
tagHandle = open(self.tagFile)
line = tagHandle.readline()
tagHandle.close()
tagFileFormat = self.findTagFileFormatFromLine(line)
return tagFileFormat
def loadTags(self):
tagFileFormat = self.findTagFileFormat()
if self.verbose > 0:
msg = "load tag file (format=%s)..." % tagFileFormat
print(msg); sys.stdout.flush()
if tagFileFormat == "fasta":
## can't handle one individual with multiple tags
tmp = SeqIO.to_dict(SeqIO.parse(self.tagFile, "fasta",
alphabet=IUPAC.unambiguous_dna))
for tag in tmp:
tagSeq = str(tmp[tag].seq)
tagId = tmp[tag].id
if tagSeq in self.dTags:
msg = "ERROR: tag sequence '%s' is present several times" \
% tagSeq
sys.stderr.write("%s\n" % msg)
sys.exit(1)
self.dTags[tagSeq] = {"sample":tagId}
elif tagFileFormat == "table":
tagHandle = open(self.tagFile)
line = tagHandle.readline()
if line == "id\ttag\n":
line = tagHandle.readline()
while True:
if line == "":
break
tokens = line.split()
tagId = tokens[0]
tagSeq = tokens[1]
if tagSeq in self.dTags:
msg = "ERROR: tag sequence '%s' is present several times" \
% tagSeq
sys.stderr.write("%s\n" % msg)
sys.exit(1)
if not all([l in "GATC" for l in set(tagSeq)]):
msg = "ERROR: tag sequence '%s' has ambiguous DNA letters" \
% tagSeq
sys.stderr.write("%s\n" % msg)
sys.exit(1)
self.dTags[tagSeq] = {"sample":tagId}
line = tagHandle.readline()
tagHandle.close()
for tagSeq in self.dTags:
tagId = self.dTags[tagSeq]["sample"]
self.dInd2NbAssigned[tagId] = 0
if self.verbose > 0:
msg = "nb of tag sequences: %i" % len(self.dTags)
msg += "\nnb of samples: %i" % len(self.dInd2NbAssigned)
print(msg); sys.stdout.flush()
def retrieveRestrictionEnzyme(self):
"""
Set self.cutMotif and, if self.findChimeras != 0, self.regexpCutMotif and self.regexpCompilCutMotif.
>>> i = Demultiplex()
>>> i.verbose = 0
>>> i.restrictEnzyme = Restriction.__dict__["ApeKI"]
>>> i.retrieveRestrictionEnzyme()
>>> i.cutMotif
u'G^CWG_C'
>>> i.regexpCutMotif
u'GC[AT]GC'
"""
if self.verbose > 0:
print("retrieve restriction enzyme from Biopython...")
self.cutMotif = u"%s" % self.restrictEnzyme.elucidate() # e.g. "G^CWG_C"
if self.verbose > 0:
print("enzyme %s: motif=%s" % (self.restrictEnzyme, self.cutMotif))
if self.findChimeras != "0":
for nt in list(self.cutMotif):
if nt in ["A", "T", "G", "C"]:
self.regexpCutMotif += nt
elif nt in ["^", "_"]:
continue
else: # ambiguous letter from IUPAC code, e.g. W
self.regexpCutMotif += "[" \
+ ambiguous_dna_values[nt] \
+ "]" # e.g. [AT] for W
if self.verbose > 0:
print("regexp of full motif: %s" % self.regexpCutMotif)
self.regexpCompilCutMotif = re.compile(self.regexpCutMotif,
flags=re.IGNORECASE)
def prepareRemainingMotif(self):
"""
Set self.regexpRemainMotif and self.lenRemainMotif.
>>> i = Demultiplex()
>>> i.verbose = 0
>>> i.restrictEnzyme = Restriction.__dict__["ApeKI"]
>>> i.retrieveRestrictionEnzyme()
>>> i.prepareRemainingMotif()
>>> i.regexpRemainMotif
u'C[AT]GC'
>>> i.lenRemainMotif
4
"""
if self.verbose > 0:
print("prepare remaining motif..."); sys.stdout.flush()
coordCutSense = self.cutMotif.find("^")
# e.g. self.restrictEnzyme.site == "GCWGC"
remainMotifAmbig = self.restrictEnzyme.site[
coordCutSense:len(self.restrictEnzyme.site)] # e.g. "CWGC"
self.lenRemainMotif = len(remainMotifAmbig)
for nt in list(remainMotifAmbig):
if nt in ["A", "T", "G", "C"]:
self.regexpRemainMotif += nt
else: # ambiguous letter from IUPAC code, e.g. W
self.regexpRemainMotif += "[" \
+ ambiguous_dna_values[nt] \
+ "]" # e.g. [AT] for W
if self.verbose > 0:
print("regexp of remaining motif: %s" % self.regexpRemainMotif)
def getPatternLength(self, pattern):
"""
Return the length of a pattern, dealing with possible degenerate sequences.
>>> i = Demultiplex()
>>> i.verbose = 0
>>> i.getPatternLength(u'AAA')
3
>>> i.getPatternLength(u'AAAC[AT]GC')
7
>>> i.getPatternLength(u'AAAC[AT]G[AT]C')
8
>>> i.getPatternLength(u'AAAC[AT][AT]GC')
8
"""
patLen = 0
if "[" not in pattern:
patLen = len(pattern)
else:
isInsideAmbig = False
for i in list(pattern):
if i not in ["[", "]"]:
if not isInsideAmbig:
patLen += 1
elif i == "[":
patLen += 1
isInsideAmbig = True
elif i == "]":
isInsideAmbig = False
return patLen
def checkDist(self):
"""
Check that the length of the whole sequence to be search for (tag or tag + remain cut site) is less than or equal to self.dist.
>>> i = Demultiplex()
>>> i.verbose = 0
>>> i.dTags = {u'AAA':{"sample":u'ind1'},
... u'TTT':{"sample":u'ind2'}}
>>> i.method = u'1'
>>> i.dist = 4
>>> i.checkDist()
>>> i.method = u'4c'
>>> i.restrictEnzyme = Restriction.__dict__["ApeKI"]
>>> i.retrieveRestrictionEnzyme()
>>> i.prepareRemainingMotif()
>>> i.checkDist()
Traceback (most recent call last):
ValueError: --dist 4 is too short for sample ind1
with tag AAA and method 4c
because the whole sequence AAAC[AT]GC
has length 7
"""
if self.verbose > 0:
if self.method not in ["4c", "4d"]:
msg = "check that '--dist' is compatible with tag lengths..."
else:
msg = "check that '--dist' is compatible with tag lengths" \
+ " and remaining of cut site..."
print(msg); sys.stdout.flush()
for tagSeq in self.dTags:
tmpSeq = tagSeq
tmpLen = len(tagSeq)
if self.method in ["4c", "4d"]:
tmpSeq += self.regexpRemainMotif
tmpLen += self.lenRemainMotif
if self.dist > 0 and tmpLen > self.dist:
msg = "--dist %i is too short for sample %s" \
% (self.dist, self.dTags[tagSeq]["sample"])
msg += "\nwith tag %s and method %s" \
% (tagSeq, self.method)
msg += "\nbecause the whole sequence %s" % tmpSeq
msg += "\nhas length %i" % tmpLen
raise ValueError(msg)
def compilePatterns(self):
"""
Compile patterns, that is each tag (+ remaining right of cut site, depending on self.method) and possibly substitutions, as regular expression for quicker searches.
>>> i = Demultiplex()
>>> i.verbose = 0
>>> i.dTags = {u'AAA':{"sample":u'ind1'}}
>>> i.restrictEnzyme = Restriction.__dict__["ApeKI"]
>>> i.retrieveRestrictionEnzyme()
>>> i.prepareRemainingMotif()
>>> # test --met 1 --subst 0 ---------------------------------
>>> i.method = u'1'
>>> i.nbSubstitutionsAllowed = 0
>>> i.compilePatterns()
>>> i.dTags[u'AAA'][u're'].pattern
u'^AAA'
>>> # test --met 1 --subst 1 ---------------------------------
>>> i.method = u'1'
>>> i.nbSubstitutionsAllowed = 1
>>> i.compilePatterns()
>>> i.dTags[u'AAA'][u're'].pattern
u'^(AAA){s<=1}'
>>> # test --met 2 --subst 0 ---------------------------------
>>> i.method = u'2'
>>> i.nbSubstitutionsAllowed = 0
>>> i.compilePatterns()
>>> i.dTags[u'AAA'][u're'].pattern
u'^AAA'
>>> # test --met 3 --subst 0 ---------------------------------
>>> i.method = u'3'
>>> i.nbSubstitutionsAllowed = 0
>>> i.compilePatterns()
>>> i.dTags[u'AAA'][u're'].pattern
u'^AAA'
>>> # test --met 4a --subst 0 --------------------------------
>>> i.method = u'4a'
>>> i.nbSubstitutionsAllowed = 0
>>> i.compilePatterns()
>>> i.dTags[u'AAA'][u're'].pattern
u'^AAA'
>>> # test --met 4a --subst 1 --------------------------------
>>> i.method = u'4a'
>>> i.nbSubstitutionsAllowed = 1
>>> i.compilePatterns()
>>> i.dTags[u'AAA'][u're'].pattern
u'^(AAA){s<=1}'
>>> # test --met 4b --subst 0 --------------------------------
>>> i.method = u'4b'
>>> i.dist = 20
>>> i.nbSubstitutionsAllowed = 0
>>> i.compilePatterns()
>>> i.dTags[u'AAA'][u're'].pattern
u'AAA'
>>> # test --met 4c --subst 0 --------------------------------
>>> i.method = u'4c'
>>> i.dist = 20
>>> i.nbSubstitutionsAllowed = 0
>>> i.compilePatterns()
>>> i.dTags[u'AAA'][u're'].pattern
u'AAAC[AT]GC'
>>> # test --met 4d --subst 0 --------------------------------
>>> i.method = u'4d'
>>> i.dist = 20
>>> i.nbSubstitutionsAllowed = 0
>>> i.compilePatterns()
>>> i.dTags[u'AAA'][u're'].pattern
u'AAAC[AT]GC'
>>> # test --met 4d --subst 1 --------------------------------
>>> i.method = u'4d'
>>> i.dist = 20
>>> i.nbSubstitutionsAllowed = 1
>>> i.compilePatterns()
>>> i.dTags[u'AAA'][u're'].pattern
u'(AAAC[AT]GC){s<=1}'
>>> i.dist = 0
>>> i.compilePatterns()
>>> i.dTags[u'AAA'][u're'].pattern
u'^(AAAC[AT]GC){s<=1}'
"""
if self.verbose > 0:
msg = "compile patterns"
msg += " (method %s" % self.method
msg += ", distance %i bp" % self.dist
msg += ", %i substitution" % self.nbSubstitutionsAllowed
if self.nbSubstitutionsAllowed > 1:
msg += "s"
msg += " allowed)..."
print(msg); sys.stdout.flush()
for tagSeq in self.dTags:
## build the pattern
pattern = tagSeq # e.g. "AAA"
if self.method in ["4c", "4d"]:
pattern += self.regexpRemainMotif # e.g. += "C[AT]GC"
if self.nbSubstitutionsAllowed > 0:
pattern = "(%s){s<=%i}" % (pattern,
self.nbSubstitutionsAllowed)
if self.method in ["1", "2", "3", "4a"] or self.dist <= 0:
pattern = "^" + pattern
## compile the pattern
if self.nbSubstitutionsAllowed > 0:
self.dTags[tagSeq]["re"] \
= regex.compile(pattern, flags=regex.IGNORECASE)
else:
self.dTags[tagSeq]["re"] \
= re.compile(pattern, flags=re.IGNORECASE)
def makeSeqsToCompareTwoTags(self, tagSeq):
"""
Return a list of DNA sequence(s) as they can appear in a read as string(s) so that they can be compared to another sequence.
>>> i = Demultiplex()
>>> i.verbose = 0
>>> i.method = "1"
>>> i.makeSeqsToCompareTwoTags("AAA")
[u'AAA']
>>> i.method = "4c"
>>> i.regexpRemainMotif = "TTGC"
>>> i.lenRemainMotif = 4
>>> i.makeSeqsToCompareTwoTags("AAA")
[u'AAATTGC']
>>> i.regexpRemainMotif = "C[AT]GC"
>>> i.lenRemainMotif = 4
>>> i.makeSeqsToCompareTwoTags("AAA")
[u'AAACAGC', u'AAACTGC']
"""
seqs = []
if self.method not in ["4c", "4d"]:
seqs.append(tagSeq)
else:
if re.search("^[ATGCN]+$", self.regexpRemainMotif) is not None:
seqs.append(tagSeq + self.regexpRemainMotif)
elif re.search("^[ATGCN\[\]]+$", self.regexpRemainMotif) is not None:
idx1 = self.regexpRemainMotif.find("[")
idx2 = self.regexpRemainMotif.find("]")
nbSeqs = idx2 - (idx1 + 1)
for idxSeq in range(nbSeqs):
seqs.append(tagSeq)
idxNt = 0
while True:
if idxNt >= len(self.regexpRemainMotif):
break
if idxNt < idx1 or idxNt > idx2:
seqs[idxSeq] += self.regexpRemainMotif[idxNt]
elif idxNt > idx1 and idxNt < idx2:
seqs[idxSeq] += self.regexpRemainMotif[idxNt+idxSeq]
idxNt = idx2
idxNt += 1
else:
msg = "self.regexpRemainMotif %s contains other symbols" % \
self.regexpRemainMotif
msg += "\nthan only A, T, G, C, [, and ]"
raise ValueError(msg)
return seqs
def comparePatternsTwoTags(self, tagSeq1, tagId1, tagSeq2, tagId2):
"""
>>> i = Demultiplex()
>>> i.verbose = 0
>>> i.restrictEnzyme = Restriction.__dict__["ApeKI"]
>>> i.retrieveRestrictionEnzyme()
>>> i.prepareRemainingMotif()
>>> i.dTags = {u'TTAC':{"sample":u'ind1'},
... u'TTAGCTT':{"sample":u'ind2'}}
>>> i.method = u'4c'
>>> i.dist = 20
>>> i.nbSubstitutionsAllowed = 2
>>> i.compilePatterns()
>>> i.comparePatternsTwoTags(u'TTAC', u'ind1', u'TTAGCTT', u'ind2')
Traceback (most recent call last):
ValueError: with 2 allowed substitutions, tag TTAC corresponding to ind1
is indistinguishable from tag TTAGCTT corresponding to ind2
pattern: (TTACC[AT]GC){s<=2}
string: TTAGCTTCAGC
start-end: 0-8
>>> i.dTags = {u'GTGGA':{"sample":u'ind1'},
... u'TTGCAGGA':{"sample":u'ind2'}}
>>> i.compilePatterns()
>>> i.comparePatternsTwoTags(u'GTGGA', u'ind1', u'TTGCAGGA', u'ind2')
Traceback (most recent call last):
ValueError: with 2 allowed substitutions, tag GTGGA corresponding to ind1
is indistinguishable from tag TTGCAGGA corresponding to ind2
pattern: (GTGGAC[AT]GC){s<=2}
string: TTGCAGGACAGC
start-end: 3-12
>>> i.dist = 0
>>> i.compilePatterns()
>>> i.comparePatternsTwoTags(u'GTGGA', u'ind1', u'TTGCAGGA', u'ind2')
>>> i.dTags = {u'AAAA':{"sample":u'ind1'},
... u'ATAA':{"sample":u'ind1'}}
>>> i.nbSubstitutionsAllowed = 1
>>> i.compilePatterns()
>>> i.comparePatternsTwoTags(u'AAAA', u'ind1', u'ATAA', u'ind1')
"""
if tagId2 != tagId1:
seqs = self.makeSeqsToCompareTwoTags(tagSeq2)
for seq in seqs:
tmpRe = self.dTags[tagSeq1]["re"].search(seq)
if tmpRe:
msg = "with %i allowed substitution" % \
self.nbSubstitutionsAllowed
if self.nbSubstitutionsAllowed > 1:
msg += "s"
msg += ", tag %s" % tagSeq1
msg += " corresponding to %s" % tagId1
msg += "\nis indistinguishable from tag %s" % tagSeq2
msg += " corresponding to %s" % tagId2
msg += "\npattern: %s" % self.dTags[tagSeq1]["re"].pattern
msg += "\nstring: %s" % seq
msg += "\nstart-end: %i-%i" % (tmpRe.start(), tmpRe.end())
raise ValueError(msg)
def comparePatterns(self):
"""
Raise an exception if, when allowing substitutions, two patterns (for different samples) are indistinguishable.
>>> i = Demultiplex()
>>> i.verbose = 0
>>> i.restrictEnzyme = Restriction.__dict__["ApeKI"]
>>> i.retrieveRestrictionEnzyme()
>>> i.prepareRemainingMotif()
>>> # test --met 1 --subst 0 ---------------------------------
>>> i.dTags = {u'TTAC':{"sample":u'ind1'},
... u'TTAG':{"sample":u'ind2'}}
>>> i.method = u'1'
>>> i.nbSubstitutionsAllowed = 0
>>> i.compilePatterns()
>>> i.comparePatterns()
>>> # test --met 1 --subst 1 ; diff samples ------------------
>>> i.dTags = {u'TTAC':{"sample":u'ind1'},
... u'TTAG':{"sample":u'ind2'}}
>>> i.method = u'1'
>>> i.nbSubstitutionsAllowed = 1
>>> i.compilePatterns()
>>> i.comparePatterns()
Traceback (most recent call last):
ValueError: with 1 allowed substitution, tag TTAG corresponding to ind2
is indistinguishable from tag TTAC corresponding to ind1
pattern: ^(TTAG){s<=1}
string: TTAC
start-end: 0-4
>>> # test --met 1 --subst 1 ; same sample -------------------
>>> i.dTags = {u'TTAC':{"sample":u'ind1'},
... u'TTAG':{"sample":u'ind1'}}
>>> i.method = u'1'
>>> i.nbSubstitutionsAllowed = 1
>>> i.compilePatterns()
>>> i.comparePatterns()
>>> # test --met 4c --subst 2 --------------------------------
>>> i.dTags = {u'TTAC':{"sample":u'ind1'},
... u'TTAGCTT':{"sample":u'ind2'}}
>>> i.method = u'4c'
>>> i.dist = 20
>>> i.nbSubstitutionsAllowed = 2
>>> i.compilePatterns()
>>> i.comparePatterns()
Traceback (most recent call last):
ValueError: with 2 allowed substitutions, tag TTAC corresponding to ind1
is indistinguishable from tag TTAGCTT corresponding to ind2
pattern: (TTACC[AT]GC){s<=2}
string: TTAGCTTCAGC
start-end: 0-8
"""
if self.nbSubstitutionsAllowed > 0:
if self.verbose > 0:
msg = "check that searched patterns are distinguishable"
msg += "\nwith %i substitution" % self.nbSubstitutionsAllowed
if self.nbSubstitutionsAllowed > 1:
msg += "s"
msg += " allowed..."
print(msg)
sys.stdout.flush()
for i in range(len(self.dTags)):
tagSeq1 = self.dTags.keys()[i]
tagId1 = self.dTags[tagSeq1]["sample"]
lenSeq1 = len(tagSeq1)
if self.method in ["4c", "4d"]:
lenSeq1 += self.lenRemainMotif
for j in range(len(self.dTags)):
if j == i:
continue
tagSeq2 = self.dTags.keys()[j]
tagId2 = self.dTags[tagSeq2]["sample"]
if tagId2 == tagId1:
continue
lenSeq2 = len(tagSeq2)
if self.method in ["4c", "4d"]:
lenSeq2 += self.lenRemainMotif
if lenSeq1 > lenSeq2:
continue
self.comparePatternsTwoTags(tagSeq1, tagId1,
tagSeq2, tagId2)
def flexCompileComparePatterns(self):
"""
Run compilePatterns() and comparePatterns() by decreasing the number of substitutions allowed, until it doesn't raise any exception, if possible.
"""
if self.verbose > 0:
msg = "compile and compare patterns by automatically decreasing"
msg += "\nthe number of substitutions if they are indistinguishable..."
print(msg); sys.stdout.flush()
for nbSubsts in range(self.nbSubstitutionsAllowed, -1, -1):
self.nbSubstitutionsAllowed = nbSubsts
try:
self.compilePatterns()
self.comparePatterns()
except Exception as e:
print(e.args[0])
if nbSubsts > 0:
continue
else:
msg = "some tags are indistinguishable even when" \
+ " no substitution is allowed"
e.args += (msg,)
raise e
if self.verbose > 0:
msg = "nb of substitutions allowed: %i" \
% self.nbSubstitutionsAllowed
print(msg); sys.stdout.flush()
break
def prepareInReads(self):
"""
Open input fastq file(s) and return iterator(s)
"""
if self.inFqFile1.endswith(".gz"):
self.inFqHandle1 = gzip.open(self.inFqFile1, "r")
else:
self.inFqHandle1 = open(self.inFqFile1, "r")
self.reads1 = FastqGeneralIterator(self.inFqHandle1)
if self.inFqFile2:
if self.inFqFile2.endswith(".gz"):
self.inFqHandle2 = gzip.open(self.inFqFile2, "r")
else:
self.inFqHandle2 = open(self.inFqFile2, "r")
self.reads2 = FastqGeneralIterator(self.inFqHandle2)
def closeFqHandles(self):
self.inFqHandle1.close()
if self.inFqFile2: