forked from JamieHeather/tcr-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouse_vDCR.py
executable file
·1024 lines (805 loc) · 65.7 KB
/
mouse_vDCR.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
# mouse_vDCR v1.4 : "verbose Decombinator"
# James M. Heather, November 2014, UCL
##################
### BACKGROUND ###
##################
# This script reads in T-cell receptor (TCR) deep-sequencing data and assigns them a Decombinator (DCR) index
# This script is itself a modified version of Decombinator v1.4 (by Dr. Niclas Thomas)
# However I have modified it to allow downstream error-correction by making use of random DNA barcodes incorporated before PCR
# This version additionally incorporates other changes, including increased stringency filters and different input methods
# This version also provides the option to use a new set of Decombinator V/J gene tags (see 'tags' variable)
# These new extended tag sets are able to identity all IMGT-proscribed prototypic genes, regardless of functional status
# For details on the official version of Decombinator, see:
# http://dx.doi.org/10.1093/bioinformatics/btt004 (Thomas et al 2013 publication)
# https://github.com/uclinfectionimmunity/Decombinator/ (GitHub repository)
# NB Many of the changes made in this version have been included in the official v2 Decombinator releases
##################
###### INPUT #####
##################
# Takes fastq files in which the first 12 nucleotides are made up of random barcode sequence introduced before PCR amplification
# Such fastq files can be produced by DualIndexDemultiplexing.py or AddAllR2Hex.py
# Only works for mouse alpha and beta chain TCR sequence data
# Takes command line input, have to specify which chain to Decombine (a/b), e.g.:
# python vDCR.py FILE1.fq a
# Users can also pipe (likely gzipped) data in, e.g.:
# zcat FILE2.fq.gz | python vDCR.py FILE2.fq.gz b
# NB filename is required again after pipe in order to correctly name output files
# Note that this version also doesn't necessarily require the tag and V/J fasta files to be in the same directory as the script
# If it cannot find them locally, the script will instead attempt to download them from GitHub
##################
##### OUTPUT #####
##################
# Produces a '.n12' file, which is a standard comma-delimited Decombinator output file with several additional fields:
# V index, J index, # V deletions, # J deletions, insert, ID, tcr sequence, tcr quality, barcode sequence, barcode quality
# Also outputs some statistics on to stdout
# .n12 files can be processed by CollapseTCRs.py, to mitigate for PCR and sequencing errors and amplification
# NB By default the omitN option (which skips reads that contain an ambiguous base call in any position) is set to False
# Instead I recommend leaving these reads, and then only filtering out those that contain Ns in barcode or inter-tag region
# e.g.: for i in *n12; do awk '{FS=", "} {if ($7~/N/ || $9~/N/){next} else{ if (length($7) < 130){print}}}' $i > temp && mv temp $i; done
# This one-liner also removes TCRs that contain erroneous assignations with over-long inter-tag regions
# Output files are named based on input filename, using '.' dots to remove suffixes, thus these must not be included in filenames
# Performs no CDR3 translation or plotting; these functionalities are available in other scripts in the suite
##################
#### ANALYSIS ####
##################
import traceback
import sys
import os
import platform
import urllib2
tags = "original"
# No set of extended mouse tag exists yet, just using standard DCR mouse tags
def analysis( Sequence_Reads, results, chain, with_statistics=True, with_reverse_complement_search=True, omitN=False):
import numpy as np
import decimal as dec
import string
import operator as op
import collections as coll
from Bio import SeqIO
from acora import AcoraBuilder
from time import time, clock
from string import Template
from operator import itemgetter, attrgetter
import Levenshtein as lev
imposs_deletion = 0
tag_overlap = 0
results_file = open( str(results)+'.n12', "w")
Nseqs = 0
if chain=="alpha":
# Do not change - V tags are split at position 10, J at position 6, to look for half tags if no full tag is found.
v_half_split, j_half_split = [10,6]
################
print 'Commencing analysis on a total of', len(Sequence_Reads), 'file(s)'
################
print ('Importing known V and J gene segments and tags...')
# Look for tag and V/J fasta files: if cannot find locally, sources from GitHub repositories
handle = open("mouse_TRAV_region.fasta", "rU")
v_genes = list(SeqIO.parse(handle, "fasta"))
handle.close()
handle = open("mouse_TRAJ_region.fasta", "rU")
j_genes = list(SeqIO.parse(handle, "fasta"))
handle.close()
v_regions = []
for j in range(0, len(v_genes)):
v_regions.append(string.upper(v_genes[j].seq))
j_regions = []
for j in range(0, len(j_genes)):
j_regions.append(string.upper(j_genes[j].seq))
##############
## Build keyword tries of V and J tags for fast assignment
v_seqs, half1_v_seqs, half2_v_seqs, jump_to_end_v = get_v_tags(open("mousetags_trav.txt", "rU"), v_half_split)
j_seqs, half1_j_seqs, half2_j_seqs, jump_to_start_j = get_j_tags(open("mousetags_traj.txt", "rU"), j_half_split)
v_builder = AcoraBuilder()
for i in range(0,len(v_seqs)):
v_builder.add(str(v_seqs[i])) # Add all V tags to keyword trie
v_key = v_builder.build()
j_builder = AcoraBuilder()
for i in range(0,len(j_seqs)):
j_builder.add(str(j_seqs[i])) # Add all J tags to keyword trie
j_key = j_builder.build()
##############
## Build keyword tries for first and second halves of both V and J tags
v_half1_builder = AcoraBuilder()
for i in range(0,len(half1_v_seqs)):
v_half1_builder.add(str(half1_v_seqs[i]))
half1_v_key = v_half1_builder.build()
v_half2_builder = AcoraBuilder()
for i in range(0,len(half2_v_seqs)):
v_half2_builder.add(str(half2_v_seqs[i]))
half2_v_key = v_half2_builder.build()
j_half1_builder = AcoraBuilder()
for i in range(0,len(half1_j_seqs)):
j_half1_builder.add(str(half1_j_seqs[i]))
half1_j_key = j_half1_builder.build()
j_half2_builder = AcoraBuilder()
for i in range(0,len(half2_j_seqs)):
j_half2_builder.add(str(half2_j_seqs[i]))
half2_j_key = j_half2_builder.build()
###############
## Initialise variables
assigned_count = 0 # this will just increase by one every time we correctly assign a seq read with all desired variables
seq_count = 0 # this will simply track the number of sequences analysed in file
t0 = time() # Begin timer
###############
## Open .txt file created at the start of analysis
stemplate = Template('$v $j $del_v $del_j $nt_insert $seqid $tcr_seq $tcr_qual $barcode $barqual')
# Creates stemplate, a holder, for f. Each line will have the 5 variables separated by a space
###############
## Begin analysing sequences
for i in range(len(Sequence_Reads)):
print 'Importing sequences from', Sequence_Reads[i],' and assigning V and J regions...'
if sys.stdin.isatty() == True:
handle = open(Sequence_Reads[i], "rU")
else:
handle = sys.stdin
for record in SeqIO.parse(handle, "fastq"):
barcode_seq = str(record.seq)[:12]
barcode_qual = str(record.format("fastq")).split("\n")[3][:12]
if 'N' in str(record.seq) and omitN==True:
Nseqs += 1
else:
found_seq_match = 0
found_v_match = 0
found_j_match = 0
seq_count += 1
v_seq_start = 0
j_seq_end = 0
hold_v = v_key.findall(str(record.seq))
hold_j = j_key.findall(str(record.seq))
if hold_v:
v_match = v_seqs.index(hold_v[0][0]) # Assigns V
temp_end_v = hold_v[0][1] + jump_to_end_v[v_match] - 1 # Finds where the end of a full V would be
v_seq_start = hold_v[0][1]
if get_v_deletions( record.seq, v_match, temp_end_v, v_regions ): # If the number of deletions has been found
[ end_v, deletions_v] = get_v_deletions( record.seq, v_match, temp_end_v, v_regions )
else:
found_v_match = 0
hold_v1 = half1_v_key.findall(str(record.seq))
hold_v2 = half2_v_key.findall(str(record.seq))
for i in range(len(hold_v1)):
indices = [y for y, x in enumerate(half1_v_seqs) if x == hold_v1[i][0] ]
for k in indices:
if len(v_seqs[k]) == len(str(record.seq)[hold_v1[i][1]:hold_v1[i][1]+len(v_seqs[half1_v_seqs.index(hold_v1[i][0])])]):
if lev.hamming( v_seqs[k], str(record.seq)[hold_v1[i][1]:hold_v1[i][1]+len(v_seqs[k])] ) <= 1:
v_match = k
temp_end_v = hold_v1[i][1] + jump_to_end_v[v_match] - 1 # Finds where the end of a full V would be
found_v_match += 1
v_seq_start = hold_v1[i][1]
for i in range(len(hold_v2)):
indices = [y for y, x in enumerate(half2_v_seqs) if x == hold_v2[i][0] ]
for k in indices:
if len(v_seqs[k]) == len(str(record.seq)[hold_v2[i][1]-v_half_split:hold_v2[i][1]-v_half_split+len(v_seqs[half2_v_seqs.index(hold_v2[i][0])])]):
if lev.hamming( v_seqs[k], str(record.seq)[hold_v2[i][1]-v_half_split:hold_v2[i][1]+len(v_seqs[k])-v_half_split] ) <= 1:
v_match = k
temp_end_v = hold_v2[i][1] + jump_to_end_v[v_match] - v_half_split - 1 # Finds where the end of a full V would be
found_v_match += 1
v_seq_start = hold_v2[i][1] - v_half_split
if hold_j:
j_match = j_seqs.index(hold_j[0][0]) # Assigns J
temp_start_j = hold_j[0][1] - jump_to_start_j[j_match] # Finds where the start of a full J would be
j_seq_end = hold_j[0][1] + len(hold_j[0][0])
if get_j_deletions( record.seq, j_match, temp_start_j, j_regions ):
[ start_j, deletions_j] = get_j_deletions( record.seq, j_match, temp_start_j, j_regions )
else:
found_j_match = 0
hold_j1 = half1_j_key.findall(str(record.seq))
hold_j2 = half2_j_key.findall(str(record.seq))
for i in range(len(hold_j1)):
indices = [y for y, x in enumerate(half1_j_seqs) if x == hold_j1[i][0] ]
for k in indices:
if len(j_seqs[k]) == len(str(record.seq)[hold_j1[i][1]:hold_j1[i][1]+len(j_seqs[half1_j_seqs.index(hold_j1[i][0])])]):
if lev.hamming( j_seqs[k], str(record.seq)[hold_j1[i][1]:hold_j1[i][1]+len(j_seqs[k])] ) <= 1:
j_match = k
temp_start_j = hold_j1[i][1] - jump_to_start_j[j_match] # Finds where the start of a full J would be
found_j_match += 1
j_seq_end = hold_j1[i][1] + len(hold_j1[i][0]) + j_half_split
for i in range(len(hold_j2)):
indices = [y for y, x in enumerate(half2_j_seqs) if x == hold_j2[i][0] ]
for k in indices:
if len(j_seqs[k]) == len(str(record.seq)[hold_j2[i][1]-j_half_split:hold_j2[i][1]-j_half_split+len(j_seqs[half2_j_seqs.index(hold_j2[i][0])])]):
if lev.hamming( j_seqs[k], str(record.seq)[hold_j2[i][1]-j_half_split:hold_j2[i][1]+len(j_seqs[k])-j_half_split] ) <= 1:
j_match = k
temp_start_j = hold_j2[i][1] - jump_to_start_j[j_match] - j_half_split # Finds where the start of a full J would be
found_j_match += 1
j_seq_end = hold_j2[i][1] + len(hold_j2[i][0])
TCRseq = str(record.seq[v_seq_start:j_seq_end])
TCRqual = str(record.format("fastq")).split("\n")[3][v_seq_start:j_seq_end]
if hold_v and hold_j:
if get_v_deletions( record.seq, v_match, temp_end_v, v_regions ) and get_j_deletions( record.seq, j_match, temp_start_j, j_regions ):
f_seq = stemplate.substitute( v = str(v_match)+str(','), j = str(j_match)+str(','), del_v = str(deletions_v)+str(','), del_j = str(deletions_j)+str(','), nt_insert = str(record.seq[end_v+1:start_j])+str(','), seqid = str(record.id)+str(','), tcr_seq = TCRseq+str(','), tcr_qual = TCRqual+str(','), barcode = barcode_seq+str(','), barqual = barcode_qual )
if deletions_v > (jump_to_end_v[v_match] - len(v_seqs[v_match])) or deletions_j > jump_to_start_j[j_match]: # Impossible deletion filter
imposs_deletion += 1
elif ((temp_end_v - jump_to_end_v[v_match]) + deletions_v) > ((temp_start_j - deletions_j) + jump_to_start_j[j_match]): # overlapping tag filter
tag_overlap += 1
else:
print >> results_file, f_seq
assigned_count += 1
found_seq_match = 1
elif hold_v and found_j_match == 1:
if get_v_deletions( record.seq, v_match, temp_end_v, v_regions ) and get_j_deletions( record.seq, j_match, temp_start_j, j_regions ):
[ start_j, deletions_j] = get_j_deletions( record.seq, j_match, temp_start_j, j_regions )
f_seq = stemplate.substitute( v = str(v_match)+str(','), j = str(j_match)+str(','), del_v = str(deletions_v)+str(','), del_j = str(deletions_j)+str(','), nt_insert = str(record.seq[end_v+1:start_j])+str(','), seqid = str(record.id)+str(','), tcr_seq = TCRseq+str(','), tcr_qual = TCRqual+str(','), barcode = barcode_seq+str(','), barqual = barcode_qual )
if deletions_v > (jump_to_end_v[v_match] - len(v_seqs[v_match])) or deletions_j > jump_to_start_j[j_match]: # Impossible deletion filter
imposs_deletion += 1
elif ((temp_end_v - jump_to_end_v[v_match]) + deletions_v) > ((temp_start_j - deletions_j) + jump_to_start_j[j_match]): # overlapping tag filter
tag_overlap += 1
else:
print >> results_file, f_seq
assigned_count += 1
found_seq_match = 1
elif found_v_match == 1 and hold_j:
if get_v_deletions( record.seq, v_match, temp_end_v, v_regions ) and get_j_deletions( record.seq, j_match, temp_start_j, j_regions ):
[ end_v, deletions_v] = get_v_deletions( record.seq, v_match, temp_end_v, v_regions )
f_seq = stemplate.substitute( v = str(v_match)+str(','), j = str(j_match)+str(','), del_v = str(deletions_v)+str(','), del_j = str(deletions_j)+str(','), nt_insert = str(record.seq[end_v+1:start_j])+str(','), seqid = str(record.id)+str(','), tcr_seq = TCRseq+str(','), tcr_qual = TCRqual+str(','), barcode = barcode_seq+str(','), barqual = barcode_qual )
if deletions_v > (jump_to_end_v[v_match] - len(v_seqs[v_match])) or deletions_j > jump_to_start_j[j_match]: # Impossible deletion filter
imposs_deletion += 1
elif ((temp_end_v - jump_to_end_v[v_match]) + deletions_v) > ((temp_start_j - deletions_j) + jump_to_start_j[j_match]): # overlapping tag filter
tag_overlap += 1
else:
print >> results_file, f_seq
assigned_count += 1
found_seq_match = 1
elif found_v_match == 1 and found_j_match == 1:
if get_v_deletions( record.seq, v_match, temp_end_v, v_regions ) and get_j_deletions( record.seq, j_match, temp_start_j, j_regions ):
[ end_v, deletions_v] = get_v_deletions( record.seq, v_match, temp_end_v, v_regions )
[ start_j, deletions_j] = get_j_deletions( record.seq, j_match, temp_start_j, j_regions )
f_seq = stemplate.substitute( v = str(v_match)+str(','), j = str(j_match)+str(','), del_v = str(deletions_v)+str(','), del_j = str(deletions_j)+str(','), nt_insert = str(record.seq[end_v+1:start_j])+str(','), seqid = str(record.id)+str(','), tcr_seq = TCRseq+str(','), tcr_qual = TCRqual+str(','), barcode = barcode_seq+str(','), barqual = barcode_qual )
if deletions_v > (jump_to_end_v[v_match] - len(v_seqs[v_match])) or deletions_j > jump_to_start_j[j_match]: # Impossible deletion filter
imposs_deletion += 1
elif ((temp_end_v - jump_to_end_v[v_match]) + deletions_v) > ((temp_start_j - deletions_j) + jump_to_start_j[j_match]): # overlapping tag filter
tag_overlap += 1
else:
print >> results_file, f_seq
assigned_count += 1
found_seq_match = 1
if found_seq_match == 0 and with_reverse_complement_search == True:
#####################
# REVERSE COMPLEMENT
#####################
record_reverse = record.reverse_complement()
hold_v = v_key.findall(str(record_reverse.seq))
hold_j = j_key.findall(str(record_reverse.seq))
if hold_v:
v_match = v_seqs.index(hold_v[0][0]) # Assigns V
temp_end_v = hold_v[0][1] + jump_to_end_v[v_match] - 1 # Finds where the end of a full V would be
v_seq_start = hold_v[0][1]
if get_v_deletions( record_reverse.seq, v_match, temp_end_v, v_regions ): # If the number of deletions has been found
[ end_v, deletions_v] = get_v_deletions( record_reverse.seq, v_match, temp_end_v, v_regions )
else:
found_v_match = 0
hold_v1 = half1_v_key.findall(str(record_reverse.seq))
hold_v2 = half2_v_key.findall(str(record_reverse.seq))
for i in range(len(hold_v1)):
indices = [y for y, x in enumerate(half1_v_seqs) if x == hold_v1[i][0] ]
for k in indices:
if len(v_seqs[k]) == len(str(record_reverse.seq)[hold_v1[i][1]:hold_v1[i][1]+len(v_seqs[half1_v_seqs.index(hold_v1[i][0])])]):
if lev.hamming( v_seqs[k], str(record_reverse.seq)[hold_v1[i][1]:hold_v1[i][1]+len(v_seqs[k])] ) <= 1:
v_match = k
temp_end_v = hold_v1[i][1] + jump_to_end_v[v_match] - 1 # Finds where the end of a full V would be
found_v_match += 1
v_seq_start = hold_v1[i][1]
for i in range(len(hold_v2)):
indices = [y for y, x in enumerate(half2_v_seqs) if x == hold_v2[i][0] ]
for k in indices:
if len(v_seqs[k]) == len(str(record_reverse.seq)[hold_v2[i][1]-v_half_split:hold_v2[i][1]-v_half_split+len(v_seqs[half2_v_seqs.index(hold_v2[i][0])])]):
if lev.hamming( v_seqs[k], str(record_reverse.seq)[hold_v2[i][1]-v_half_split:hold_v2[i][1]+len(v_seqs[k])-v_half_split] ) <= 1:
v_match = k
temp_end_v = hold_v2[i][1] + jump_to_end_v[v_match] - v_half_split - 1 # Finds where the end of a full V would be
found_v_match += 1
v_seq_start = hold_v2[i][1] - v_half_split
if hold_j:
j_match = j_seqs.index(hold_j[0][0]) # Assigns J
temp_start_j = hold_j[0][1] - jump_to_start_j[j_match] # Finds where the start of a full J would be
j_seq_end = hold_j[0][1] + len(hold_j[0][0])
if get_j_deletions( record_reverse.seq, j_match, temp_start_j, j_regions ):
[ start_j, deletions_j] = get_j_deletions( record_reverse.seq, j_match, temp_start_j, j_regions )
else:
found_j_match = 0
hold_j1 = half1_j_key.findall(str(record_reverse.seq))
hold_j2 = half2_j_key.findall(str(record_reverse.seq))
for i in range(len(hold_j1)):
indices = [y for y, x in enumerate(half1_j_seqs) if x == hold_j1[i][0] ]
for k in indices:
if len(j_seqs[k]) == len(str(record_reverse.seq)[hold_j1[i][1]:hold_j1[i][1]+len(j_seqs[half1_j_seqs.index(hold_j1[i][0])])]):
if lev.hamming( j_seqs[k], str(record_reverse.seq)[hold_j1[i][1]:hold_j1[i][1]+len(j_seqs[k])] ) <= 1:
j_match = k
temp_start_j = hold_j1[i][1] - jump_to_start_j[j_match] # Finds where the start of a full J would be
found_j_match += 1
j_seq_end = hold_j1[i][1] + len(hold_j1[i][0]) + j_half_split
for i in range(len(hold_j2)):
indices = [y for y, x in enumerate(half2_j_seqs) if x == hold_j2[i][0] ]
for k in indices:
if len(j_seqs[k]) == len(str(record_reverse.seq)[hold_j2[i][1]-j_half_split:hold_j2[i][1]-j_half_split+len(j_seqs[half2_j_seqs.index(hold_j2[i][0])])]):
if lev.hamming( j_seqs[k], str(record_reverse.seq)[hold_j2[i][1]-j_half_split:hold_j2[i][1]+len(j_seqs[k])-j_half_split] ) <= 1:
j_match = k
temp_start_j = hold_j2[i][1] - jump_to_start_j[j_match] - j_half_split # Finds where the start of a full J would be
found_j_match += 1
j_seq_end = hold_j2[i][1] + len(hold_j2[i][0])
# Records the inter-tag sequence and quality for downstream error-correction
TCRseq = str(record_reverse.seq[v_seq_start:j_seq_end])
TCRqual = str(record_reverse.format("fastq")).split("\n")[3][v_seq_start:j_seq_end]
if hold_v and hold_j:
if get_v_deletions( record_reverse.seq, v_match, temp_end_v, v_regions ) and get_j_deletions( record_reverse.seq, j_match, temp_start_j, j_regions ):
f_seq = stemplate.substitute( v = str(v_match)+str(','), j = str(j_match)+str(','), del_v = str(deletions_v)+str(','), del_j = str(deletions_j)+str(','), nt_insert = str(record_reverse.seq[end_v+1:start_j])+str(','), seqid = str(record.id)+str(','), tcr_seq = TCRseq+str(','), tcr_qual = TCRqual+str(','), barcode = barcode_seq+str(','), barqual = barcode_qual )
if deletions_v > (jump_to_end_v[v_match] - len(v_seqs[v_match])) or deletions_j > jump_to_start_j[j_match]: # Impossible deletion filter
imposs_deletion += 1
elif ((temp_end_v - jump_to_end_v[v_match]) + deletions_v) > ((temp_start_j - deletions_j) + jump_to_start_j[j_match]): # overlapping tag filter
tag_overlap += 1
else:
print >> results_file, f_seq
assigned_count += 1
found_seq_match = 1
elif hold_v and found_j_match == 1:
if get_v_deletions( record_reverse.seq, v_match, temp_end_v, v_regions ) and get_j_deletions( record_reverse.seq, j_match, temp_start_j, j_regions ):
[ start_j, deletions_j] = get_j_deletions( record_reverse.seq, j_match, temp_start_j, j_regions )
f_seq = stemplate.substitute( v = str(v_match)+str(','), j = str(j_match)+str(','), del_v = str(deletions_v)+str(','), del_j = str(deletions_j)+str(','), nt_insert = str(record_reverse.seq[end_v+1:start_j])+str(','), seqid = str(record.id)+str(','), tcr_seq = TCRseq+str(','), tcr_qual = TCRqual+str(','), barcode = barcode_seq+str(','), barqual = barcode_qual )
if deletions_v > (jump_to_end_v[v_match] - len(v_seqs[v_match])) or deletions_j > jump_to_start_j[j_match]: # Impossible deletion filter
imposs_deletion += 1
elif ((temp_end_v - jump_to_end_v[v_match]) + deletions_v) > ((temp_start_j - deletions_j) + jump_to_start_j[j_match]): # overlapping tag filter
tag_overlap += 1
else:
print >> results_file, f_seq
assigned_count += 1
found_seq_match = 1
elif found_v_match == 1 and hold_j:
if get_v_deletions( record_reverse.seq, v_match, temp_end_v, v_regions ) and get_j_deletions( record_reverse.seq, j_match, temp_start_j, j_regions ):
[ end_v, deletions_v] = get_v_deletions( record_reverse.seq, v_match, temp_end_v, v_regions )
f_seq = stemplate.substitute( v = str(v_match)+str(','), j = str(j_match)+str(','), del_v = str(deletions_v)+str(','), del_j = str(deletions_j)+str(','), nt_insert = str(record_reverse.seq[end_v+1:start_j])+str(','), seqid = str(record.id)+str(','), tcr_seq = TCRseq+str(','), tcr_qual = TCRqual+str(','), barcode = barcode_seq+str(','), barqual = barcode_qual )
if deletions_v > (jump_to_end_v[v_match] - len(v_seqs[v_match])) or deletions_j > jump_to_start_j[j_match]: # Impossible deletion filter
imposs_deletion += 1
elif ((temp_end_v - jump_to_end_v[v_match]) + deletions_v) > ((temp_start_j - deletions_j) + jump_to_start_j[j_match]): # overlapping tag filter
tag_overlap += 1
else:
print >> results_file, f_seq
assigned_count += 1
found_seq_match = 1
elif found_v_match == 1 and found_j_match == 1:
if get_v_deletions( record_reverse.seq, v_match, temp_end_v, v_regions ) and get_j_deletions( record_reverse.seq, j_match, temp_start_j, j_regions ):
[ end_v, deletions_v] = get_v_deletions( record_reverse.seq, v_match, temp_end_v, v_regions )
[ start_j, deletions_j] = get_j_deletions( record_reverse.seq, j_match, temp_start_j, j_regions )
f_seq = stemplate.substitute( v = str(v_match)+str(','), j = str(j_match)+str(','), del_v = str(deletions_v)+str(','), del_j = str(deletions_j)+str(','), nt_insert = str(record_reverse.seq[end_v+1:start_j])+str(','), seqid = str(record.id)+str(','), tcr_seq = TCRseq+str(','), tcr_qual = TCRqual+str(','), barcode = barcode_seq+str(','), barqual = barcode_qual )
if deletions_v > (jump_to_end_v[v_match] - len(v_seqs[v_match])) or deletions_j > jump_to_start_j[j_match]: # Impossible deletion filter
imposs_deletion += 1
elif ((temp_end_v - jump_to_end_v[v_match]) + deletions_v) > ((temp_start_j - deletions_j) + jump_to_start_j[j_match]): # overlapping tag filter
tag_overlap += 1
else:
print >> results_file, f_seq
assigned_count += 1
found_seq_match = 1
handle.close()
results_file.close()
elif chain=="beta":
# Do not change - V tags are split at position 10, J at position 6 (for original tags)
# and at position 10 for extended tags.
if tags == "original":
v_half_split, j_half_split = [10,6]
################
print 'Commencing analysis on a total of', len(Sequence_Reads), 'file(s)'
################
print ('Importing known V, D and J gene segments and tags...')
# Look for tag and V/J fasta files: if cannot find locally, sources from GitHub repositories
handle = open("mouse_TRBV_region.fasta", "rU")
v_genes = list(SeqIO.parse(handle, "fasta"))
handle.close()
handle = open("mouse_TRBJ_region.fasta", "rU")
j_genes = list(SeqIO.parse(handle, "fasta"))
handle.close()
v_regions = []
for j in range(0, len(v_genes)):
v_regions.append(string.upper(v_genes[j].seq))
j_regions = []
for j in range(0, len(j_genes)):
j_regions.append(string.upper(j_genes[j].seq))
##############
## Build keyword tries of V and J tags for fast assignment
v_seqs, half1_v_seqs, half2_v_seqs, jump_to_end_v = get_v_tags(open("mousetags_trbv.txt", "rU"), v_half_split)
j_seqs, half1_j_seqs, half2_j_seqs, jump_to_start_j = get_j_tags(open("mousetags_trbj.txt", "rU"), j_half_split)
v_builder = AcoraBuilder()
for i in range(0,len(v_seqs)):
v_builder.add(str(v_seqs[i])) # Add all V tags to keyword trie
v_key = v_builder.build()
j_builder = AcoraBuilder()
for i in range(0,len(j_seqs)):
j_builder.add(str(j_seqs[i])) # Add all J tags to keyword trie
j_key = j_builder.build()
##############
## Build keyword tries for first and second halves of both V and J tags
v_half1_builder = AcoraBuilder()
for i in range(0,len(half1_v_seqs)):
v_half1_builder.add(str(half1_v_seqs[i]))
half1_v_key = v_half1_builder.build()
v_half2_builder = AcoraBuilder()
for i in range(0,len(half2_v_seqs)):
v_half2_builder.add(str(half2_v_seqs[i]))
half2_v_key = v_half2_builder.build()
j_half1_builder = AcoraBuilder()
for i in range(0,len(half1_j_seqs)):
j_half1_builder.add(str(half1_j_seqs[i]))
half1_j_key = j_half1_builder.build()
j_half2_builder = AcoraBuilder()
for i in range(0,len(half2_j_seqs)):
j_half2_builder.add(str(half2_j_seqs[i]))
half2_j_key = j_half2_builder.build()
###############
## Initialise variables
assigned_count = 0 # this will just increase by one every time we correctly assign a seq read with all desired variables
seq_count = 0 # this will simply track the number of sequences analysed in file
Nseqs = 0 # Number of raw reads containing N nucleotides
t0 = time() # Begin timer
###############
## Open .txt file created at the start of analysis
stemplate = Template('$v $j $del_v $del_j $nt_insert $seqid $tcr_seq $tcr_qual $barcode $barqual')
# Creates stemplate, a holder, for f. Each line will have the 5 variables separated by a space
###############
## Begin analysing sequences
for i in range(len(Sequence_Reads)):
print 'Importing sequences from', Sequence_Reads[i],' and assigning V and J regions...'
if sys.stdin.isatty() == True:
handle = open(Sequence_Reads[i], "rU")
else:
handle = sys.stdin
for record in SeqIO.parse(handle, "fastq"):
barcode_seq = str(record.seq)[0:12]
barcode_qual = str(record.format("fastq")).split("\n")[3][:12]
if 'N' in str(record.seq) and omitN==True:
Nseqs += 1
else:
found_seq_match = 0
found_v_match = 0
found_j_match = 0
seq_count += 1
v_seq_start = 0
j_seq_end = 0
hold_v = v_key.findall(str(record.seq))
hold_j = j_key.findall(str(record.seq))
if hold_v:
v_match = v_seqs.index(hold_v[0][0]) # Assigns V
temp_end_v = hold_v[0][1] + jump_to_end_v[v_match] - 1 # Finds where the end of a full V would be
v_seq_start = hold_v[0][1]
if get_v_deletions( record.seq, v_match, temp_end_v, v_regions ): # If the number of deletions has been found
[ end_v, deletions_v] = get_v_deletions( record.seq, v_match, temp_end_v, v_regions )
else:
found_v_match = 0
hold_v1 = half1_v_key.findall(str(record.seq))
hold_v2 = half2_v_key.findall(str(record.seq))
for i in range(len(hold_v1)):
indices = [y for y, x in enumerate(half1_v_seqs) if x == hold_v1[i][0] ]
for k in indices:
if len(v_seqs[k]) == len(str(record.seq)[hold_v1[i][1]:hold_v1[i][1]+len(v_seqs[half1_v_seqs.index(hold_v1[i][0])])]):
if lev.hamming( v_seqs[k], str(record.seq)[hold_v1[i][1]:hold_v1[i][1]+len(v_seqs[k])] ) <= 1:
v_match = k
temp_end_v = hold_v1[i][1] + jump_to_end_v[v_match] - 1 # Finds where the end of a full V would be
found_v_match += 1
v_seq_start = hold_v1[i][1]
for i in range(len(hold_v2)):
indices = [y for y, x in enumerate(half2_v_seqs) if x == hold_v2[i][0] ]
for k in indices:
if len(v_seqs[k]) == len(str(record.seq)[hold_v2[i][1]-v_half_split:hold_v2[i][1]+len(v_seqs[half2_v_seqs.index(hold_v2[i][0])])-v_half_split]):
if lev.hamming( v_seqs[k], str(record.seq)[hold_v2[i][1]-v_half_split:hold_v2[i][1]+len(v_seqs[k])-v_half_split] ) <= 1:
v_match = k
temp_end_v = hold_v2[i][1] + jump_to_end_v[v_match] - v_half_split - 1 # Finds where the end of a full V would be
found_v_match += 1
v_seq_start = hold_v2[i][1] - v_half_split
if hold_j:
j_match = j_seqs.index(hold_j[0][0]) # Assigns J
temp_start_j = hold_j[0][1] - jump_to_start_j[j_match] # Finds where the start of a full J would be
j_seq_end = hold_j[0][1] + len(hold_j[0][0])
if get_j_deletions( record.seq, j_match, temp_start_j, j_regions ):
[ start_j, deletions_j] = get_j_deletions( record.seq, j_match, temp_start_j, j_regions )
else:
found_j_match = 0
hold_j1 = half1_j_key.findall(str(record.seq))
hold_j2 = half2_j_key.findall(str(record.seq))
for i in range(len(hold_j1)):
indices = [y for y, x in enumerate(half1_j_seqs) if x == hold_j1[i][0] ]
for k in indices:
if len(j_seqs[k]) == len(str(record.seq)[hold_j1[i][1]:hold_j1[i][1]+len(j_seqs[half1_j_seqs.index(hold_j1[i][0])])]):
if lev.hamming( j_seqs[k], str(record.seq)[hold_j1[i][1]:hold_j1[i][1]+len(j_seqs[k])] ) <= 1:
j_match = k
temp_start_j = hold_j1[i][1] - jump_to_start_j[j_match] # Finds where the start of a full J would be
found_j_match += 1
j_seq_end = hold_j1[i][1] + len(hold_j1[i][0]) + j_half_split
for i in range(len(hold_j2)):
indices = [y for y, x in enumerate(half2_j_seqs) if x == hold_j2[i][0] ]
for k in indices:
if len(j_seqs[k]) == len(str(record.seq)[hold_j2[i][1]-j_half_split:hold_j2[i][1]+len(j_seqs[half2_j_seqs.index(hold_j2[i][0])])-j_half_split]):
if lev.hamming( j_seqs[k], str(record.seq)[hold_j2[i][1]-j_half_split:hold_j2[i][1]+len(j_seqs[k])-j_half_split] ) <= 1:
j_match = k
temp_start_j = hold_j2[i][1] - jump_to_start_j[j_match] - j_half_split # Finds where the start of a full J would be
found_j_match += 1
j_seq_end = hold_j2[i][1] + len(hold_j2[i][0])
TCRseq = str(record.seq[v_seq_start:j_seq_end])
TCRqual = str(record.format("fastq")).split("\n")[3][v_seq_start:j_seq_end]
if hold_v and hold_j:
if get_v_deletions( record.seq, v_match, temp_end_v, v_regions ) and get_j_deletions( record.seq, j_match, temp_start_j, j_regions ):
f_seq = stemplate.substitute( v = str(v_match)+str(','), j = str(j_match)+str(','), del_v = str(deletions_v)+str(','), del_j = str(deletions_j)+str(','), nt_insert = str(record.seq[end_v+1:start_j])+str(','), seqid = str(record.id)+str(','), tcr_seq = TCRseq+str(','), tcr_qual = TCRqual+str(','), barcode = barcode_seq+str(','), barqual = barcode_qual )
if deletions_v > (jump_to_end_v[v_match] - len(v_seqs[v_match])) or deletions_j > jump_to_start_j[j_match]: # Impossible deletion filter
imposs_deletion += 1
elif ((temp_end_v - jump_to_end_v[v_match]) + deletions_v) > ((temp_start_j - deletions_j) + jump_to_start_j[j_match]): # overlapping tag filter
tag_overlap += 1
else:
print >> results_file, f_seq
assigned_count += 1
found_seq_match = 1
elif hold_v and found_j_match == 1:
if get_v_deletions( record.seq, v_match, temp_end_v, v_regions ) and get_j_deletions( record.seq, j_match, temp_start_j, j_regions ):
[ start_j, deletions_j] = get_j_deletions( record.seq, j_match, temp_start_j, j_regions )
f_seq = stemplate.substitute( v = str(v_match)+str(','), j = str(j_match)+str(','), del_v = str(deletions_v)+str(','), del_j = str(deletions_j)+str(','), nt_insert = str(record.seq[end_v+1:start_j])+str(','), seqid = str(record.id)+str(','), tcr_seq = TCRseq+str(','), tcr_qual = TCRqual+str(','), barcode = barcode_seq+str(','), barqual = barcode_qual )
if deletions_v > (jump_to_end_v[v_match] - len(v_seqs[v_match])) or deletions_j > jump_to_start_j[j_match]: # Impossible deletion filter
imposs_deletion += 1
elif ((temp_end_v - jump_to_end_v[v_match]) + deletions_v) > ((temp_start_j - deletions_j) + jump_to_start_j[j_match]): # overlapping tag filter
tag_overlap += 1
else:
print >> results_file, f_seq
assigned_count += 1
found_seq_match = 1
elif found_v_match == 1 and hold_j:
if get_v_deletions( record.seq, v_match, temp_end_v, v_regions ) and get_j_deletions( record.seq, j_match, temp_start_j, j_regions ):
[ end_v, deletions_v] = get_v_deletions( record.seq, v_match, temp_end_v, v_regions )
f_seq = stemplate.substitute( v = str(v_match)+str(','), j = str(j_match)+str(','), del_v = str(deletions_v)+str(','), del_j = str(deletions_j)+str(','), nt_insert = str(record.seq[end_v+1:start_j])+str(','), seqid = str(record.id)+str(','), tcr_seq = TCRseq+str(','), tcr_qual = TCRqual+str(','), barcode = barcode_seq+str(','), barqual = barcode_qual )
if deletions_v > (jump_to_end_v[v_match] - len(v_seqs[v_match])) or deletions_j > jump_to_start_j[j_match]: # Impossible deletion filter
imposs_deletion += 1
elif ((temp_end_v - jump_to_end_v[v_match]) + deletions_v) > ((temp_start_j - deletions_j) + jump_to_start_j[j_match]): # overlapping tag filter
tag_overlap += 1
else:
print >> results_file, f_seq
assigned_count += 1
found_seq_match = 1
elif found_v_match == 1 and found_j_match == 1:
if get_v_deletions( record.seq, v_match, temp_end_v, v_regions ) and get_j_deletions( record.seq, j_match, temp_start_j, j_regions ):
[ end_v, deletions_v] = get_v_deletions( record.seq, v_match, temp_end_v, v_regions )
[ start_j, deletions_j] = get_j_deletions( record.seq, j_match, temp_start_j, j_regions )
f_seq = stemplate.substitute( v = str(v_match)+str(','), j = str(j_match)+str(','), del_v = str(deletions_v)+str(','), del_j = str(deletions_j)+str(','), nt_insert = str(record.seq[end_v+1:start_j])+str(','), seqid = str(record.id)+str(','), tcr_seq = TCRseq+str(','), tcr_qual = TCRqual+str(','), barcode = barcode_seq+str(','), barqual = barcode_qual )
if deletions_v > (jump_to_end_v[v_match] - len(v_seqs[v_match])) or deletions_j > jump_to_start_j[j_match]: # Impossible deletion filter
imposs_deletion += 1
elif ((temp_end_v - jump_to_end_v[v_match]) + deletions_v) > ((temp_start_j - deletions_j) + jump_to_start_j[j_match]): # overlapping tag filter
tag_overlap += 1
else:
print >> results_file, f_seq
assigned_count += 1
found_seq_match = 1
if found_seq_match == 0 and with_reverse_complement_search == True:
#####################
# REVERSE COMPLEMENT
#####################
record_reverse = record.reverse_complement()
hold_v = v_key.findall(str(record_reverse.seq))
hold_j = j_key.findall(str(record_reverse.seq))
if hold_v:
v_match = v_seqs.index(hold_v[0][0]) # Assigns V
temp_end_v = hold_v[0][1] + jump_to_end_v[v_match] - 1 # Finds where the end of a full V would be
v_seq_start = hold_v[0][1]
if get_v_deletions( record_reverse.seq, v_match, temp_end_v, v_regions ): # If the number of deletions has been found
[ end_v, deletions_v] = get_v_deletions( record_reverse.seq, v_match, temp_end_v, v_regions )
else:
found_v_match = 0
hold_v1 = half1_v_key.findall(str(record_reverse.seq))
hold_v2 = half2_v_key.findall(str(record_reverse.seq))
for i in range(len(hold_v1)):
indices = [y for y, x in enumerate(half1_v_seqs) if x == hold_v1[i][0] ]
for k in indices:
if len(v_seqs[k]) == len(str(record_reverse.seq)[hold_v1[i][1]:hold_v1[i][1]+len(v_seqs[half1_v_seqs.index(hold_v1[i][0])])]):
if lev.hamming( v_seqs[k], str(record_reverse.seq)[hold_v1[i][1]:hold_v1[i][1]+len(v_seqs[k])] ) <= 1:
v_match = k
temp_end_v = hold_v1[i][1] + jump_to_end_v[v_match] - 1 # Finds where the end of a full V would be
found_v_match += 1
v_seq_start = hold_v1[i][1]
for i in range(len(hold_v2)):
indices = [y for y, x in enumerate(half2_v_seqs) if x == hold_v2[i][0] ]
for k in indices:
if len(v_seqs[k]) == len(str(record_reverse.seq)[hold_v2[i][1]-v_half_split:hold_v2[i][1]+len(v_seqs[half2_v_seqs.index(hold_v2[i][0])])-v_half_split]):
if lev.hamming( v_seqs[k], str(record_reverse.seq)[hold_v2[i][1]-v_half_split:hold_v2[i][1]+len(v_seqs[k])-v_half_split] ) <= 1:
v_match = k
temp_end_v = hold_v2[i][1] + jump_to_end_v[v_match] - v_half_split - 1 # Finds where the end of a full V would be
found_v_match += 1
v_seq_start = hold_v2[i][1] - v_half_split
if hold_j:
j_match = j_seqs.index(hold_j[0][0]) # Assigns J
temp_start_j = hold_j[0][1] - jump_to_start_j[j_match] # Finds where the start of a full J would be
j_seq_end = hold_j[0][1] + len(hold_j[0][0])
if get_j_deletions( record_reverse.seq, j_match, temp_start_j, j_regions ):
[ start_j, deletions_j] = get_j_deletions( record_reverse.seq, j_match, temp_start_j, j_regions )
else:
found_j_match = 0
hold_j1 = half1_j_key.findall(str(record_reverse.seq))
hold_j2 = half2_j_key.findall(str(record_reverse.seq))
for i in range(len(hold_j1)):
indices = [y for y, x in enumerate(half1_j_seqs) if x == hold_j1[i][0] ]
for k in indices:
if len(j_seqs[k]) == len(str(record_reverse.seq)[hold_j1[i][1]:hold_j1[i][1]+len(j_seqs[half1_j_seqs.index(hold_j1[i][0])])]):
if lev.hamming( j_seqs[k], str(record_reverse.seq)[hold_j1[i][1]:hold_j1[i][1]+len(j_seqs[k])] ) <= 1:
j_match = k
temp_start_j = hold_j1[i][1] - jump_to_start_j[j_match] # Finds where the start of a full J would be
found_j_match += 1
j_seq_end = hold_j1[i][1] + len(hold_j1[i][0]) + j_half_split
for i in range(len(hold_j2)):
indices = [y for y, x in enumerate(half2_j_seqs) if x == hold_j2[i][0] ]
for k in indices:
if len(j_seqs[k]) == len(str(record_reverse.seq)[hold_j2[i][1]-j_half_split:hold_j2[i][1]+len(j_seqs[half2_j_seqs.index(hold_j2[i][0])])-j_half_split]):
if lev.hamming( j_seqs[k], str(record_reverse.seq)[hold_j2[i][1]-j_half_split:hold_j2[i][1]+len(j_seqs[k])-j_half_split] ) <= 1:
j_match = k
temp_start_j = hold_j2[i][1] - jump_to_start_j[j_match] - j_half_split # Finds where the start of a full J would be
found_j_match += 1
j_seq_end = hold_j2[i][1] + len(hold_j2[i][0])
# Records the inter-tag sequence and quality for downstream error-correction
TCRseq = str(record_reverse.seq[v_seq_start:j_seq_end])
TCRqual = str(record_reverse.format("fastq")).split("\n")[3][v_seq_start:j_seq_end]
if hold_v and hold_j:
if get_v_deletions( record_reverse.seq, v_match, temp_end_v, v_regions ) and get_j_deletions( record_reverse.seq, j_match, temp_start_j, j_regions ):
f_seq = stemplate.substitute( v = str(v_match)+str(','), j = str(j_match)+str(','), del_v = str(deletions_v)+str(','), del_j = str(deletions_j)+str(','), nt_insert = str(record_reverse.seq[end_v+1:start_j])+str(','), seqid = str(record.id)+str(','), tcr_seq = TCRseq+str(','), tcr_qual = TCRqual+str(','), barcode = barcode_seq+str(','), barqual = barcode_qual )
if deletions_v > (jump_to_end_v[v_match] - len(v_seqs[v_match])) or deletions_j > jump_to_start_j[j_match]: # Impossible deletion filter
imposs_deletion += 1
elif ((temp_end_v - jump_to_end_v[v_match]) + deletions_v) > ((temp_start_j - deletions_j) + jump_to_start_j[j_match]): # overlapping tag filter
tag_overlap += 1
else:
print >> results_file, f_seq
assigned_count += 1
found_seq_match = 1
elif hold_v and found_j_match == 1:
if get_v_deletions( record_reverse.seq, v_match, temp_end_v, v_regions ) and get_j_deletions( record_reverse.seq, j_match, temp_start_j, j_regions ):
[ start_j, deletions_j] = get_j_deletions( record_reverse.seq, j_match, temp_start_j, j_regions )
f_seq = stemplate.substitute( v = str(v_match)+str(','), j = str(j_match)+str(','), del_v = str(deletions_v)+str(','), del_j = str(deletions_j)+str(','), nt_insert = str(record_reverse.seq[end_v+1:start_j])+str(','), seqid = str(record.id)+str(','), tcr_seq = TCRseq+str(','), tcr_qual = TCRqual+str(','), barcode = barcode_seq+str(','), barqual = barcode_qual )
if deletions_v > (jump_to_end_v[v_match] - len(v_seqs[v_match])) or deletions_j > jump_to_start_j[j_match]: # Impossible deletion filter
imposs_deletion += 1
elif ((temp_end_v - jump_to_end_v[v_match]) + deletions_v) > ((temp_start_j - deletions_j) + jump_to_start_j[j_match]): # overlapping tag filter
tag_overlap += 1
else:
print >> results_file, f_seq
assigned_count += 1
found_seq_match = 1
elif found_v_match == 1 and hold_j:
if get_v_deletions( record_reverse.seq, v_match, temp_end_v, v_regions ) and get_j_deletions( record_reverse.seq, j_match, temp_start_j, j_regions ):
[ end_v, deletions_v] = get_v_deletions( record_reverse.seq, v_match, temp_end_v, v_regions )
f_seq = stemplate.substitute( v = str(v_match)+str(','), j = str(j_match)+str(','), del_v = str(deletions_v)+str(','), del_j = str(deletions_j)+str(','), nt_insert = str(record_reverse.seq[end_v+1:start_j])+str(','), seqid = str(record.id)+str(','), tcr_seq = TCRseq+str(','), tcr_qual = TCRqual+str(','), barcode = barcode_seq+str(','), barqual = barcode_qual )
if deletions_v > (jump_to_end_v[v_match] - len(v_seqs[v_match])) or deletions_j > jump_to_start_j[j_match]: # Impossible deletion filter
imposs_deletion += 1
elif ((temp_end_v - jump_to_end_v[v_match]) + deletions_v) > ((temp_start_j - deletions_j) + jump_to_start_j[j_match]): # overlapping tag filter
tag_overlap += 1
else:
print >> results_file, f_seq
assigned_count += 1
found_seq_match = 1
elif found_v_match == 1 and found_j_match == 1:
if get_v_deletions( record_reverse.seq, v_match, temp_end_v, v_regions ) and get_j_deletions( record_reverse.seq, j_match, temp_start_j, j_regions ):
[ end_v, deletions_v] = get_v_deletions( record_reverse.seq, v_match, temp_end_v, v_regions )
[ start_j, deletions_j] = get_j_deletions( record_reverse.seq, j_match, temp_start_j, j_regions )
f_seq = stemplate.substitute( v = str(v_match)+str(','), j = str(j_match)+str(','), del_v = str(deletions_v)+str(','), del_j = str(deletions_j)+str(','), nt_insert = str(record_reverse.seq[end_v+1:start_j])+str(','), seqid = str(record.id)+str(','), tcr_seq = TCRseq+str(','), tcr_qual = TCRqual+str(','), barcode = barcode_seq+str(','), barqual = barcode_qual )
if deletions_v > (jump_to_end_v[v_match] - len(v_seqs[v_match])) or deletions_j > jump_to_start_j[j_match]: # Impossible deletion filter
imposs_deletion += 1
elif ((temp_end_v - jump_to_end_v[v_match]) + deletions_v) > ((temp_start_j - deletions_j) + jump_to_start_j[j_match]): # overlapping tag filter
tag_overlap += 1
else:
print >> results_file, f_seq
assigned_count += 1
found_seq_match = 1
print "FOUND"
print f_seq
handle.close()
results_file.close()
timed = time() - t0
print seq_count, 'sequences were analysed'
print assigned_count, 'sequences were successfully assigned'
if omitN==True:
print Nseqs, 'sequences contained ambiguous N nucleotides'
print tag_overlap, "reads with overlapping tags discarded"
print imposs_deletion, "reads with impossible numbers of deletions discarded"
print "Output to " + name_results + ".n12"
print 'Time taken =', timed, 'seconds'
###################
# OTHER FUNCTIONS #
###################
def get_v_deletions( rc, v_match, temp_end_v, v_regions_cut ):
# This function determines the number of V deletions in sequence rc
# by comparing it to v_match, beginning by making comparisons at the
# end of v_match and at position temp_end_v in rc.
function_temp_end_v = temp_end_v
pos = -1
is_v_match = 0
while is_v_match == 0 and 0 <= function_temp_end_v < len(rc):
if str(v_regions_cut[v_match])[pos] == str(rc)[function_temp_end_v] and str(v_regions_cut[v_match])[pos-1] == str(rc)[function_temp_end_v-1] and str(v_regions_cut[v_match])[pos-2] == str(rc)[function_temp_end_v-2]:
is_v_match = 1
deletions_v = -pos - 1
end_v = function_temp_end_v
else:
pos -= 1
function_temp_end_v -= 1
if is_v_match == 1:
return [end_v, deletions_v]
else:
return []
def get_j_deletions( rc, j_match, temp_start_j, j_regions_cut ):
# This function determines the number of J deletions in sequence rc
# by comparing it to j_match, beginning by making comparisons at the
# end of j_match and at position temp_end_j in rc.
function_temp_start_j = temp_start_j
pos = 0
is_j_match = 0
while is_j_match == 0 and 0 <= function_temp_start_j+2 < len(str(rc)):
if str(j_regions_cut[j_match])[pos] == str(rc)[function_temp_start_j] and str(j_regions_cut[j_match])[pos+1] == str(rc)[function_temp_start_j+1] and str(j_regions_cut[j_match])[pos+2] == str(rc)[function_temp_start_j+2]:
is_j_match = 1
deletions_j = pos
start_j = function_temp_start_j
else:
pos += 1
function_temp_start_j += 1
if is_j_match == 1:
return [start_j, deletions_j]
else:
return []
def get_v_tags(file_v, half_split):
import string
v_seqs = [] # Holds all V tags
jump_to_end_v = [] # Holds the number of jumps to make to look for deletions for each V region once the corresponding tag has been found
for line in file_v:
elements = line.rstrip("\n") # Turns every element in a text file line separated by a space into elements in a list
v_seqs.append(string.split(elements)[0]) # Adds elements in first column iteratively
jump_to_end_v.append(int(string.split(elements)[1])) # Adds elements in second column iteratively
half1_v_seqs = []
half2_v_seqs = []
for i in range(len(v_seqs)):
half1_v_seqs.append(v_seqs[i][0:half_split])
half2_v_seqs.append(v_seqs[i][half_split:])
return [v_seqs, half1_v_seqs, half2_v_seqs, jump_to_end_v]
def get_j_tags(file_j, half_split):
import string
j_seqs = [] # Holds all J tags
jump_to_start_j = [] # Holds the number of jumps to make to look for deletions for each J region once the corresponding tag has been found
for line in file_j:
elements = line.rstrip("\n")
j_seqs.append(string.split(elements)[0])
jump_to_start_j.append(int(string.split(elements)[1]))
half1_j_seqs = []
half2_j_seqs = []
for j in range(len(j_seqs)):
half1_j_seqs.append(j_seqs[j][0:half_split])
half2_j_seqs.append(j_seqs[j][half_split:])
return [j_seqs, half1_j_seqs, half2_j_seqs, jump_to_start_j]
##################
### TAKE INPUT ###
##################
if (len(sys.argv) <> 3):
print "Please enter both a file-name and which chain to decombine (a/b)"
print "E.g. python vDCR.py FILE.fq a"
sys.exit()
else:
filename = str(sys.argv[1])
chaininput = str(sys.argv[2])
if chaininput == "a":
chain = "alpha"
elif chaininput == "b":
chain = "beta"
else:
print "Chain must be \'a\' for alpha or \'b\' for beta"
sys.exit()
print 'Running vDCR on ' + filename + '...'
filestoanalyse = []
try:
testopen = open(str(filename),"rU")
testopen.close()
correctpath = True
filestoanalyse.append(filename)
except:
print 'Cannot find the file you specified. Please try again'
sys.exit()
if tags == "original":
name_results = "vDCR_" + str(chain) + "_" + str(filename.split(".")[0])
elif tags == "extended":