-
Notifications
You must be signed in to change notification settings - Fork 31
/
epinano_modules.py
1484 lines (1412 loc) · 48.1 KB
/
epinano_modules.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
from collections import defaultdict
from collections import OrderedDict
import numpy as np
import pysam, sys, re, os, re, gzip, bz2, datetime
import argparse as ap
from collections import deque
from itertools import repeat
import shutil,datetime
import fileinput
import subprocess
__version__ = '0.1-2020-04-04'
__Author__ = '[email protected]'
def openfile(f):
if f.endswith ('.gz'):
fh = gzip.open (f,'rt')
elif f.endswith ('bz') or f.endswith ('bz2'):
fh = bz2.open(f,'rt')
else:
fh = open(f,'rt')
return fh
def now ():
return datetime.datetime.now().strftime('%D:%H:%M:%S')
def window (seq,size=5):
it = iter(seq)
win = deque ((next (it,None) for _ in range (size)), maxlen=size)
yield win
append = win.append
for e in it:
append(e)
yield win
def reference_from_bam (bam):
# bam is pysam.AlignmentFile read bam
bamfh = pysam.AlignmentFile (bam, 'rb')
return set (bamfh.header.references)
def filt_bam_with_pysam (bam_in):
# filt out non-primary alignments
# bam_in is a pysam.AlignmentFile bam file hand
# bam_out output bam fileanme
bamfh = pysam.AlignmentFile(bam_in,'rb')
bam_out = re.sub (r'.bam$','',bam_in) + '.filt.bam'
outfh = pysam.AlignmentFile(bam_out,'wb',header=bamfh.header)
bai_out = bam_out + '.bai'
for read in bamfh.fetch():
cond1 = read.mapping_quality < 1
cond2 = read.is_duplicate
cond3 = read.is_qcfail
cond4 = read.is_secondary
cond5 = read.is_supplementary
cond6 = read.is_unmapped
if any ([cond1,cond2,cond3,cond4,cond5,cond6]):
continue
outfh.write (read)
outfh.close()
pysam.index (bam_out,bai_out)
return bam_out, bai_out
def filt_bam (bam):
'''
with csamtools
filt out bad alignemnts
'''
out = bam.replace('bam','filt.bam') if bam.endswith ('bam') else bam +'.filt.bam'
pysam.view ('-F', '3844', '-h','-b','-o',out, bam, catch_stdout=False)
pysam.index (out,out+'.bai')
return out, out+'.bai'
def split_bam (bam,refid):
'''
split bam file on individual reference sequence
'''
newbam = re.sub(r'.bam$','',bam)+'.{}.bam'.format(refid)
pysam.view (bam, refid, '-h','-b', '-o', newbam, catch_stdout = False)
pysam.index (newbam,newbam+'.bai')
return newbam, newbam+'.bai'
def clean_soft_hard_clippings (ref_query_pair):
'''
soft-clipped: bases in 5' and 3' of the read are NOT part of the alignment.
hard-clipped: bases in 5' and 3' of the read are NOT part of the alignment AND those bases have been removed from the read sequence in the BAM file. The 'real' sequence length would be length(SEQ)+ count-of-hard-clipped-bases
:param ref_query_pair: list of tuples, each tuple contains read_pos, ref_pos, ref_base;
'''
for x,y in enumerate (ref_query_pair):
if y[1] == None and y[2] == None:
continue
else:
return ref_query_pair[x:]
break
def variant_typing (ref_query_pair_tuple):
'''
:param ref_query_pair_tuple:
'''
if isinstance (ref_query_pair_tuple[0], int) and ref_query_pair_tuple[1] is None and ref_query_pair_tuple[2] == None:
return 'I'
elif ref_query_pair_tuple[0] is None and (ref_query_pair_tuple[1], int) and ref_query_pair_tuple[2] in 'AGCT':
return 'D'
elif isinstance(ref_query_pair_tuple[0], int) and isinstance( ref_query_pair_tuple[1], int) and ref_query_pair_tuple[2] in 'agct':
return 'M'
elif isinstance (ref_query_pair_tuple[0], int) and isinstance(ref_query_pair_tuple[1], int) and ref_query_pair_tuple[2] in 'AGCT':
return 'm' #
def bam_to_tsv (bam):
bamfh = pysam.AlignmentFile(bam,'rb')
out_tsv_fh = open (bam + '.tsv', 'w')
header = "{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\n".format("#READ_NAME","FLAG","CHROM","READ_POS","BASE", "QUAL","REF_POS","REF","OP",'STRAND')
out_tsv_fh.write(header)
for read in bamfh.fetch():
o1, o2, o3 = read.query_name, read.flag, read.reference_name
query_seq = read.query_sequence
pairs = read.get_aligned_pairs(with_seq=True)
pairs = clean_soft_hard_clippings(pairs)
pairs = clean_soft_hard_clippings(pairs[::-1])
pairs = pairs[::-1]
strand = '-' if read.is_reverse else '+'
op =''
for p in pairs:
try:
o9 = variant_typing(p)
op = o9
except:
sys.stderr.write ("{}\t{}\t{} is problematic\n".format (read.reference_name, read.query_name, p) )
exit()
if op in ['D']:
o4, o5, o6 = '.', '.', '.'
o7, o8 = p[1] + 1, p[2]
elif op in ['I'] :
o4,o5,o6 = p[0],query_seq[int(p[0])],read.query_qualities[p[0]]
o7,o8 = '.','.'
else:
o4,o5,o6, o7, o8= p[0], query_seq[int(p[0])].upper(), read.query_qualities [p[0]], int (p[1]) + 1, p[2].upper()
out_tsv_fh.write ("{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\n".format(o1,o2,o3,o4,o5,o6,o7,o8,op,strand))
out_tsv_fh.close()
return (bam+'.tsv')
def spot_empty_tsv (tsv):
ary = []
cnt = 0
with open (tsv,'r') as fh:
for l in fh:
if cnt <2:
ary.append (l)
else:
break
cnt += 1
return True if len (ary)>1 else False
def split_tsv (tsv, tmp_dir, number_of_reads_in_each_file=3000):
output_prefix = 'small'
small_files = set ()
smallfile = None
file_idx = 0
last_seen = ''
new_start= ''
reads_cnt = 0
zero_counts = dict()
small_filename = "{}/{}_{}.tsv".format (tmp_dir,output_prefix,file_idx)
small_files.add (small_filename)
smallfile = open (small_filename,'a')
with openfile (tsv) as fh:
for l in fh:
if l.startswith ('#'):
continue
rd = l.split()[0]
if rd != last_seen:
last_seen = rd
reads_cnt += 1
#print (rd, reads_cnt)
if all ([reads_cnt > number_of_reads_in_each_file, reads_cnt % number_of_reads_in_each_file == 1, rd != new_start]):
smallfile.close()
new_start = rd
file_idx += 1
small_filename = "{}/{}_{}.tsv".format (tmp_dir,output_prefix,file_idx)
smallfile = open (small_filename,'a')
small_files.add (small_filename)
smallfile.write(l)
smallfile.close()
sys.stderr.write ("{} reads splitted to {} files\n".format (reads_cnt, len(small_files)))
return (small_files)
def split_tsv_for_per_site_var_freq(tsv, q, number_threads, num_reads_per_chunk=4000):
head = next(tsv)
firstline = next (tsv)
current_rd = firstline.split()[0]
rd_cnt = 1
idx = 0
chunk_out = [] # open ("CHUNK_{}.txt".format(idx),'w')
chunk_out.append(firstline)
try:
for line in tsv:
rd = line.split()[0]
if current_rd != rd:
rd_cnt += 1
current_rd = rd
if ((rd_cnt-1) % num_reads_per_chunk == 0 and rd_cnt >= num_reads_per_chunk):
q.put ((idx, chunk_out)) #.close()
idx += 1
chunk_out = [] #open ("CHUNK_{}.txt".format(idx),'w')
chunk_out.append(line)
q.put ((idx, chunk_out))
except:
raise
sys.stderr.write("split tsv file on reads failed\n")
finally:
for _ in range(number_threads ):
q.put(None)
def split_tsv_for_per_site_var_freq_1(tsv, q, number_threads, number_of_reads_in_each_file=2000):
'''only computing per read features need small tsv files to be kept
input is a generator
fh = iter ([])
if isinstance (tsv,str): #intput is tsv text file
fh = openfile (tsv)
elif isinstance (tsv,subprocess.Popen): #return from subproces.Popen
fh = tsv.stdout
elif hasattr(tsv, 'read'):
fh = tsv
'''
small_chunk = []
idx = 0
last_seen = ''
reads_cnt = 0
new_start = ''
try:
if True:
for l in tsv:
if l and l.startswith ('#'):
continue
rd = l.split()[0]
if rd != last_seen:
last_seen = rd
reads_cnt += 1
if (reads_cnt > number_of_reads_in_each_file and reads_cnt % number_of_reads_in_each_file == 1): #, rd != new_start]):
idx += 1
new_start = rd
q.put ((idx,small_chunk))
small_chunk=[]
small_chunk.append(l.strip())
small_chunk.append (l.strip())
q.put((idx,small_chunk))
except:
raise
sys.stderr.write("split tsv file on reads failed\n")
finally:
for _ in range(number_threads ):
q.put(None)
def tsv_to_freq_multiprocessing_without_manager (tsv_reads_chunk_q, out_dir):
'''
produced with sam2tsv.jar with strand information added
read read-flags reference read-pos read-base read-qual ref-pos ref-base cigar-op strand
a3194184-d809-42dc-9fa1-dfb497d2ed6a 0 cc6m_2244_T7_ecorv 0 C # 438 G S +
'''
for idx, tsv_small_chunk in iter (tsv_reads_chunk_q.get,None):
#sys.stderr.write("idx-{}\tq-{}\n".format(idx, tsv_small_chunk))
filename = "{}/small_{}.freq".format(out_dir, idx)
#small_freq_file_list.append(filename)
outh = open (filename,'w')
mis = defaultdict(int) # mismatches
mat = defaultdict (int) #matches
ins = defaultdict(int) # insertions
dele = defaultdict(int) # deletions
cov = OrderedDict () # coverage
ins_q = defaultdict(list)
aln_mem = [] #read, ref, refpos; only store last entry not matching insertion
pos = defaultdict(list) # reference positions
base = {} # ref base
qual = defaultdict(list)
#READ_NAME FLAG CHROM READ_POS BASE QUAL REF_POS REF OP STRAND
#read read-flags reference read-pos read-base read-qual ref-pos ref-base cigar-op strand
for line in tsv_small_chunk:
if line.startswith ('#'):
continue
ary = line.strip().split()
if ary[-2] in ['M','m']:
k = (ary[2], int (ary[-4]), ary[-1]) #
cov[k] = cov.get(k,0) + 1
aln_mem = []
aln_mem.append((ary[0],ary[2],int(ary[-4]), ary[-1]))
qual[k].append (ord(ary[-5])-33)
base[k] = ary[-3].upper()
if (ary[-3] != ary[4]):
mis[k] += 1
else:
mat[k] += 1
if ary[-2] == 'D':
k = (ary[2], int(ary[-4]), ary[-1])
cov[k] = cov.get(k,0) + 1
aln_mem = []
aln_mem.append((ary[0],ary[2],int(ary[-4]), ary[-1]))
base[k] = ary[-3].upper()
dele[k] = dele.get(k,0) + 1
if ary[-2] == 'I':
last_k = aln_mem[-1][1],aln_mem[-1][2],aln_mem[-1][3] # last alignment with match/mismatch/del
next_k = (ary[2], last_k[1] + 1,last_k[2])
if last_k[0] != ary[2]:
sys.stderr.write (line.strip())
ins_k_up = (ary[0], ary[2], last_k[1],last_k[2])
ins_k_down = (ary[0], ary[2], last_k[1] + 1,last_k[2])
if (ins_k_down) not in ins_q:
ins[next_k] = ins.get(next_k,0) + 1
ins_q[ins_k_down].append(ord(ary[-5])-33)
if (ins_k_up) not in ins_q:
ins[last_k] = ins.get(last_k,0) + 1
ins_q[ins_k_up].append(ord(ary[-5])-33)
header = '#Ref,pos,base,cov,mat,mis,ins,del,qual,strand\n'
#cc6m_2244_T7_ecorv,7,A,1.0,1,0,0,0,15,+
outh.write(header)
for k in cov.keys():
depth = cov.get (k,0)
Mis = mis.get (k,0)
Mat = mat.get (k,0)
Del = dele.get (k,0)
q_lst = qual.get (k,[0])
try:
q_lst = ':'.join (map (str, q_lst)) +':' #qual[k] #becos of dask sum function
num_ins = ins.get (k,0)
inf = "{},{},{},{},{},{},{},{},{},{}\n".format (k[0], k[1], base[k], depth, Mat, Mis, num_ins, Del, q_lst, k[2])
outh.write (inf)
except:
sys.stderr.write ("file {} {} does not work\n".format (tsv,k))
def tsv_to_freq_multiprocessing_with_manager (tsv_reads_chunk_q, out_dir):
'''
mutliprocessing
produced with sam2tsv.jar with strand information added
read read-flags reference read-pos read-base read-qual ref-pos ref-base cigar-op strand
a3194184-d809-42dc-9fa1-dfb497d2ed6a 0 cc6m_2244_T7_ecorv 0 C # 438 G S +
'''
for idx, tsv_small_chunk in iter (tsv_reads_chunk_q.get, None):
filename = "{}/small_{}.freq".format(out_dir, idx)
#tsv_file = open ("{}/small_{}.tsv".format(out_dir, idx),'w')
outh = open (filename,'w')
mis = defaultdict(int) # mismatches
mat = defaultdict (int) #matches
ins = defaultdict(int) # insertions
dele = defaultdict(int) # deletions
cov = OrderedDict () # coverage
ins_q = defaultdict(list)
aln_mem = [] #read, ref, refpos; only store last entry not matching insertion
pos = defaultdict(list) # reference positions
base = {} # ref base
qual = defaultdict(list)
#READ_NAME FLAG CHROM READ_POS BASE QUAL REF_POS REF OP STRAND
#read read-flags reference read-pos read-base read-qual ref-pos ref-base cigar-op strand
for line in tsv_small_chunk:
if line.startswith ('#'):
continue
#tsv_file.write (line+'\n')
ary = line.strip().split()
#sys.stdout.write(line+'\n')
if ary[-2] in ['M','m']:
k = (ary[2], int (ary[-4]), ary[-1]) #
cov[k] = cov.get(k,0) + 1
aln_mem = []
aln_mem.append((ary[0],ary[2],int(ary[-4]), ary[-1]))
qual[k].append (ord(ary[-5])-33)
base[k] = ary[-3].upper()
if (ary[-3] != ary[4]):
mis[k] += 1
else:
mat[k] += 1
if ary[-2] == 'D':
k = (ary[2], int(ary[-4]), ary[-1])
cov[k] = cov.get(k,0) + 1
aln_mem = []
aln_mem.append((ary[0],ary[2],int(ary[-4]), ary[-1]))
base[k] = ary[-3].upper()
dele[k] = dele.get(k,0) + 1
if ary[-2] == 'I':
#print (aln_mem)
last_k = aln_mem[-1][1],aln_mem[-1][2],aln_mem[-1][3] # last alignment with match/mismatch/del
#last_k = list (cov.keys())[-1]
next_k = (ary[2], last_k[1] + 1,last_k[2])
if last_k[0] != ary[2]:
pass
#sys.stderr.write (line.strip())
ins_k_up = (ary[0], ary[2], last_k[1],last_k[2])
ins_k_down = (ary[0], ary[2], last_k[1] + 1,last_k[2])
if (ins_k_down) not in ins_q:
ins[next_k] = ins.get(next_k,0) + 1
ins_q[ins_k_down].append(ord(ary[-5])-33)
if (ins_k_up) not in ins_q:
ins[last_k] = ins.get(last_k,0) + 1
ins_q[ins_k_up].append(ord(ary[-5])-33)
header = '#Ref,pos,base,cov,mat,mis,ins,del,qual,strand\n'
#cc6m_2244_T7_ecorv,7,A,1.0,1,0,0,0,15,+
outh.write(header)
for k in cov.keys():
depth = cov.get (k,0)
Mis = mis.get (k,0)
Mat = mat.get (k,0)
Del = dele.get (k,0)
q_lst = qual.get (k,[0])
try:
q_lst = ':'.join (map (str, q_lst))+':' # dataframe sum
num_ins = ins.get (k,0)
inf = "{},{},{},{},{},{},{},{},{},{}\n".format (k[0], k[1], base[k], depth, Mat, Mis, num_ins, Del, q_lst, k[2])
outh.write (inf)
except:
sys.stderr.write ("file {} {} does not work\n".format (tsv,k))
def tsv_to_freq (tsv):
'''
single thread
produced with sam2tsv.jar && with strand information added
read read-flags reference read-pos read-base read-qual ref-pos ref-base cigar-op strand
a3194184-d809-42dc-9fa1-dfb497d2ed6a 0 cc6m_2244_T7_ecorv 0 C # 438 G S +
'''
out = re.sub(r'.tsv$','',tsv) + '.freq'
outh = open (out,'w')
mis = defaultdict(int) # mismatches
mat = defaultdict (int) # matches
ins = defaultdict(int) # insertions
dele = defaultdict(int) # deletions
cov = OrderedDict () # coverage
ins_q = defaultdict(list)
aln_mem = [] #read, ref, refpos; only store last entry not matching insertion
pos = defaultdict(list) # reference positions
base = {} # ref base
qual = defaultdict(list)
#READ_NAME FLAG CHROM READ_POS BASE QUAL REF_POS REF OP STRAND
#read read-flags reference read-pos read-base read-qual ref-pos ref-base cigar-op strand
with openfile (tsv) as fh:
for line in fh:
if line.startswith ('#'):
continue
ary = line.strip().split()
if ary[-2] in ['M','m']:
k = (ary[2], int (ary[-4]), ary[-1]) #
cov[k] = cov.get(k,0) + 1
aln_mem = []
aln_mem.append((ary[0],ary[2],int(ary[-4]), ary[-1]))
qual[k].append (ord(ary[-5])-33)
base[k] = ary[-3].upper()
if (ary[-3] != ary[4]):
mis[k] += 1
else:
mat[k] += 1
if ary[-2] == 'D':
k = (ary[2], int(ary[-4]), ary[-1])
cov[k] = cov.get(k,0) + 1
aln_mem = []
aln_mem.append((ary[0],ary[2],int(ary[-4]), ary[-1]))
base[k] = ary[-3].upper()
dele[k] = dele.get(k,0) + 1
if ary[-2] == 'I':
last_k = aln_mem[-1][1],aln_mem[-1][2],aln_mem[-1][3] # last alignment with match/mismatch/del
next_k = (ary[2], last_k[1] + 1,last_k[2])
if last_k[0] != ary[2]:
sys.stderr.write (line.strip())
ins_k_up = (ary[0], ary[2], last_k[1],last_k[2])
ins_k_down = (ary[0], ary[2], last_k[1] + 1,last_k[2])
if (ins_k_down) not in ins_q:
ins[next_k] = ins.get(next_k,0) + 1
ins_q[ins_k_down].append(ord(ary[-5])-33)
if (ins_k_up) not in ins_q:
ins[last_k] = ins.get(last_k,0) + 1
ins_q[ins_k_up].append(ord(ary[-5])-33)
header = '#Ref,pos,base,cov,mat,mis,ins,del,qual,strand\n'
#cc6m_2244_T7_ecorv,7,A,1.0,1,0,0,0,15,+
outh.write(header)
for k in cov.keys():
depth = cov.get (k,0)
Mis = mis.get (k,0)
Mat = mat.get (k,0)
Del = dele.get (k,0)
q_lst = qual.get (k,[0])
try:
q_lst = ':'.join (map (str, q_lst)) #qual[k]
num_ins = ins.get (k,0)
inf = "{},{},{},{},{},{},{},{},{},{}\n".format (k[0], k[1], base[k], depth, Mat, Mis, num_ins, Del, q_lst, k[2])
outh.write (inf)
except:
sys.stderr.write ("file {} {} does not work\n".format (tsv,k))
return out
def tsv_to_var (tsv):
'''
reference base was complemented if aligned on reverse strand
'''
header = "#Ref,pos,base,strand,cov,q_mean,q_median,q_std,mis,ins,del"
out = '.'.join (tsv.split('.')[:-1]) + '.per.site.var.csv'
outh = open (out,'w')
outh.write('#Ref,pos,base,strand,cov,q_mean,q_median,q_std,mis,ins,del\n')
mis = defaultdict(int) # mismatches
mat = defaultdict (int) #matches
ins = defaultdict(int) # insertions
dele = defaultdict(int) # deletions
cov = OrderedDict () # coverage
ins_q = defaultdict(list)
aln_mem = [] #read, ref, refpos; only store last entry not matching insertion
pos = defaultdict(list) # reference positions
base = {} # ref base
Q = defaultdict(list)
qual = defaultdict(list)
basesdict = {'A':'T', 'G':'C','C':'G','T':'A','N':'N'}
#READ_NAME FLAG CHROM READ_POS BASE QUAL REF_POS REF OP STRAND
with openfile (tsv) as fh:
for line in fh:
if line.startswith ('#'):
continue
ary = line.strip().split()
if ary[-2] in ['M','m']:
k = (ary[2], int (ary[-4]), ary[-1]) #
cov[k] = cov.get(k,0) + 1
aln_mem = []
aln_mem.append((ary[0],ary[2],int(ary[-4]), ary[-1]))
qual[k].append (ary[-5])
Q[k].append(ary[-5])
base[k] = ary[-3].upper()
if (ary[-3] != ary[4]):
mis[k] += 1
else:
mat[k] += 1
if ary[-2] == 'D':
k = (ary[2], int(ary[-4]), ary[-1])
cov[k] = cov.get(k,0) + 1
aln_mem = []
aln_mem.append((ary[0],ary[2],int(ary[-4]), ary[-1]))
base[k] = ary[-3].upper()
dele[k] = dele.get(k,0) + 1
if ary[-2] == 'I':
last_k = aln_mem[-1][1],aln_mem[-1][2],aln_mem[-1][3] # last alignment with match/mismatch/del
next_k = (ary[2], last_k[1] + 1,last_k[2])
if last_k[0] != ary[2]:
sys.stderr.write (line.strip())
ins_k_up = (ary[0], ary[2], last_k[1],last_k[2])
ins_k_down = (ary[0], ary[2], last_k[1] + 1,last_k[2])
if (ins_k_down) not in ins_q:
ins[next_k] = ins.get(next_k,0) + 1
ins_q[ins_k_down].append(ary[-5])
if (ins_k_up) not in ins_q:
ins[last_k] = ins.get(last_k,0) + 1
ins_q[ins_k_up].append(ary[-5])
#header = '#Ref,pos,base,cov,mis,ins,del,q_sum,strand'
for k in cov.keys():
depth = float (cov.get (k,0) )
Mis = mis.get (k,0)
Mat = mat.get (k,0)
Del = dele.get (k,0)
q_lst = qual.get (k,[0])
try:
num_ins = ins.get (k,0)
q_mn, q_md, qstd = np.mean (np.array (q_lst).astype(np.float)), np.median (np.array (q_lst).astype(np.float)), np.std (np.array (q_lst).astype(np.float))
m,i,d = np.array ([Mis,num_ins,Del])/depth
ref_base = base[k] if k[2] == '+' else basesdict.get (k,'N')
inf = "{},{},{},{},{},{},{},{},{},{},{}\n".format (k[0], k[1], ref_base, k[2], depth,q_mn,q_md, qstd,m,i,d)
outh.write (inf)
except:
raise
sys.stderr.write ("problematic {} in {}\n".format (k,tsv))
return out
def combine_freq (list_of_freq_files):
'''
reference base was complemented if reads aligned on reverse strand
cc6m_2244_T7_ecorv,31,A,4.0,4,0,0,0,4:16:7:3,+
'''
mem = defaultdict(lambda: defaultdict(list))
Var=defaultdict (list)
Qual = defaultdict (list)
ks = OrderedDict()
outfile = os.path.dirname (list_of_freq_files[0]) if os.path.dirname (list_of_freq_files[0]) else '_'
outfile=outfile.replace ('_tmp_splitted','') +'.per.site.var.csv'
outh = open (outfile,'w')
basesdict = {'A':'T', 'G':'C','C':'G','T':'A','N':'N'}
for l in fileinput.input(list_of_freq_files):
ary = l.strip().split(',')
k = (ary[0],ary[1],ary[2],ary[-1])
ks[k] = True
c,_,m,i,d = map (float,ary[3:8])
c_m_i_d = np.array ([c,m,i,d])
q_lst = []
q_lst = [0] if (len(ary[8])) == 0 else [ float (x) for x in ary[8].split(':')]
Var[k] = Var.get(k,np.array([0])) + c_m_i_d
Qual[k] = Qual.get(k,[]) + q_lst
outh.write ('#Ref,pos,base,strand,cov,q_mean,q_median,q_std,mis,ins,del\n')
basesdict = {'A':'T', 'G':'C','C':'G','T':'A','N':'N'}
for k in ks:
cov = Var[k][0]
q_lst = Qual[k]
var_freq = Var[k][1:]/cov
var_freq = ",".join (var_freq.astype (str))
k = list (k)
if k[3] == '-':
k[2] = basesdict.get(k[2],'N')
outh.write ("{},{},{},{},{},{}\n".format (','.join(k),cov,'%0.5f'%np.mean(q_lst),'%0.5f'%np.median(q_lst),'%0.5f'%np.std(q_lst), var_freq))
outh.close()
return (outfile)
def slide_per_site_var_for_unsorted_data (per_site_var,win=5):
'''
#Ref,pos,base,strand,cov,q_mean,q_median,q_std,mis,ins,del
cc6m_2244_T7_ecorv,7,A,+,1.0,15.0,15.0,0.0,0.0,0.0,0.0
kmer sequences will be reversed if reads aligned on the minus strand
bases mapped to reverse strand have alredy been complemented during above processing
consume a lot of ram for unsorted data
'''
mem = {}
contents = OrderedDict()
dist = int (win)//2 + 1
fh = open (per_site_var,'r')
for line in fh:
if line.startswith ('#'):
continue
if re.match ('\s+',line):
continue
ary = line.strip().split(',')
ref,pos,strand = (ary[0],ary[1],ary[3])
contents[(ref,pos,strand)] = line.rstrip()
prefix = per_site_var.replace ('.per_site.var.csv','') # ".".join (per_site_var.split('.')[:-1])
out_tmp = prefix +'.per.site.var.{}mer.tmp'.format(win)
outh1= open (out_tmp,'w')
header = '#Kmer,window,Relative_pos,Ref,Ref_Pos,base,strand,cov,q_mean,q_median,q_std,mis,ins,del'
outh1.write (header+'\n')
for k in contents.keys():
ref, pos, strand = k
try:
pos = int(pos)
except:
sys.stderr.write("wrong ref pos {}".format(k))
continue
POS = []
LINES = []
upper = ''
down = ''
for i in list (reversed (range(1,dist))):
POS.append (str(pos-i))
kk = (ref,str(pos-i),strand)
if kk in contents:
base = contents[kk].split(',')[2]
upper += base
LINES.append ('-'+str(i)+','+ contents[kk])
else:
upper += 'N'
LINES.append('-'+str(i)+','+'Null')
LINES.append ('+0'+','+contents[k])
POS.append (str(pos))
for j in range (1,dist):
POS.append (str(pos + j))
kk = (ref,str(pos+j),strand)
if kk in contents:
base = contents[kk].split(',')[2]
down += base
LINES.append ('+'+str(j)+','+ contents[kk])
else:
down += 'N'
LINES.append ('+'+str(j)+','+'None')
positions = '-'.join ([POS[0],POS[-1]])
for l in LINES:
kmer = upper+contents[k].split(',')[2] +down
kmer = kmer if strand == '+' else kmer[::-1]
outh1.write (kmer+',' +positions+','+ l+'\n')
outh1.close()
### sum up slided per site variants from multiple lines into single lines ############
'''
#Kmer,window,Relative_pos,Ref,Ref_Pos,base,strand,cov,q_mean,q_median,q_std,mis,ins,del
TAGGT,1852:1853:1854:1855:1856,-2,cc6m_2459_T7_ecorv,1852,T,+,6490.0,8.62139,8.00000,4.70584,0.1869029275808937,0.3489984591679507,0.02773497688751926
TAGGT,1852:1853:1854:1855:1856,-1,cc6m_2459_T7_ecorv,1853,A,+,6508.0,6.96834,6.00000,3.65965,0.21204671173939765,0.13844499078057776,0.019668100799016593
'''
mem_window = defaultdict (defaultdict(list).copy)
# !!!!!!!!!!!!!!!!!!! should rewrite below
k_pool = []
f = open (out_tmp,'r')
for l in f:
if l.startswith ('N'):
continue
elif l.startswith ('#'):
continue
ary = l.strip().split(',')
if ary[0].endswith ('N'):
continue
if (len(ary) < 14):
continue
k = ''
try:
ks = (ary[0], ary[1],ary[3],ary[6]) # #Kmer,window,Ref,strand
k = ",".join (ks)
k_pool.append (k)
except:
pass
try:
mem_window[k]['q'].append(ary[8])
except:
sys.stderr.write ("problematic line for q: " + l.strip())
try:
mem_window[k]['m'].append(ary[11])
except:
sys.stderr.write ("problematic line for m" + l.strip())
try:
mem_window[k]['i'].append(ary[12])
except:
sys.stderr.write ("problematic line for i:" + l.strip())
try:
mem_window[k]['d'].append(ary[13])
except:
sys.stderr.write ("problematic line for d" + l.strip())
try:
mem_window[k]['cov'].append(ary[7])
except:
sys.stderr.write ('problematic line for cov:' + l.strip())
f.close()
out2 = prefix + '.per.site.{}mer.csv'.format(win)
outh2 = open (out2,'w')
q_in_head = ",".join (["q{}".format(i) for i in range(1,win+1)])
mis_in_head = ",".join (["mis{}".format(i) for i in range(1,win+1)])
ins_in_head = ",".join (["ins{}".format(i) for i in range(1,win+1)])
del_in_head = ",".join (["del{}".format(i) for i in range(1,win+1)])
outh2.write ('#Kmer,Window,Ref,Strand,Coverage,{},{},{},{}\n'.format(q_in_head, mis_in_head, ins_in_head, del_in_head))
for k in set (k_pool):
Qs = ",".join (mem_window[k]['q'])
Mis = ",".join (mem_window[k]['m'])
Ins = ",".join (mem_window[k]['i'])
Del = ",".join (mem_window[k]['d'])
Cov = ":".join (mem_window[k]['cov'])
outh2.write (",".join ([k,Cov,Qs,Mis,Ins,Del])+'\n')
outh2.close()
os.remove (out_tmp)
return (out2)
def print_last_consecutive_lines (lines, outfh):
contents = OrderedDict()
for line in lines:
ary = line.strip().split(',')
ref,pos,strand = (ary[0], ary[1], ary[3])
contents[(ref,pos,strand)] = line.rstrip()
win = len (lines)
middle = lines [win//2].rstrip().split(',')
window = str(int(middle[1]) - win//2)+'-'+str(int(middle[1]) + win//2 )
kmer = ''
consecutive_lines = []
ref,pos,base,strand = middle[:4]
for i in reversed (list (range(1, win//2+1))):
k = (ref,str(int(pos) -i), strand)
relative_pos = '-'+str(i)
if k in contents:
kmer = kmer + contents[k].split(',')[2]
consecutive_lines.append (window + ',' + relative_pos +','+contents[k])
else:
kmer = kmer + 'N'
consecutive_lines.append (window +','+relative_pos+','+","+",".join (['NA']*12))
consecutive_lines.append (window +',' + '+0' +','+",".join (middle))
kmer = kmer + middle[2]
for i in range(1,win//2+1):
k = (ref,str(int(pos) + i),strand)
relative_pos = '+'+str(i)
if k in contents:
kmer = kmer + contents[k].split(',')[2]
consecutive_lines.append (window + ',' + relative_pos+','+contents[k])
else:
kmer = kmer + 'N'
consecutive_lines.append (window +',' + relative_pos+','+",".join (['NA']*12))
for l in consecutive_lines:
print (kmer+','+l, file=outfh)
def slide_per_site_var (per_site_var,win=5):
'''
#Ref,pos,base,strand,cov,q_mean,q_median,q_std,mis,ins,del
cc6m_2244_T7_ecorv,7,A,+,1.0,15.0,15.0,0.0,0.0,0.0,0.0
kmer sequences will be reversed if reads aligned on the minus strand
bases mapped to reverse strand have alredy been complemented during above processing
'''
#Ref,pos,base,strand,cov,q_mean,q_median,q_std,mis,ins,del
prefix = re.sub(r'.per.site.\S+','',per_site_var)# , .replace ('.per.site.csv','') # ".".join (per_site_var.split('.')[:-1])
out_tmp = prefix +'.per_site_var.{}mer.tmp'.format(win)
if os.path.exists (out_tmp):
os.remove (out_tmp)
outfh = open (out_tmp,'w')
fh = open (per_site_var, 'rb' )
eof = fh.seek (-1,2)
fh.seek(0,0)
head = fh.readline ()
lines = []
for _ in range (win):
l = fh.readline().decode('utf-8').rstrip()
if l:
lines.append (l)
if len (lines) < win:
print ('not enough sites to be slided',file=sys.stderr)
contents = OrderedDict()
for line in lines:
ary = line.strip().split(',')
ref,pos,strand = (ary[0], ary[1], ary[3])
contents[(ref,pos,strand)] = line.rstrip()
while (fh.tell() <= eof):
middle = lines [win//2].split(',')
window = str(int(middle[1]) - win//2)+'-'+str(int(middle[1]) + win//2 )
consecutive_lines = []
kmer=''
ref,pos,base,strand = middle[:4]
k_to_del = (ref,str(int(pos)-win),strand)
for i in reversed (list (range(1, win//2+1))):
k = (ref,str(int(pos) -i),strand)
relative_pos = '-'+str(i)
if k in contents:
kmer = kmer +contents[k].split(',')[2]
consecutive_lines.append (window+','+relative_pos +','+contents[k])
else:
consecutive_lines.append (window+','+relative_pos+','+ "," . join ([ref, str(int(pos) -i), 'N', strand, '0', 'NaN,NaN,NaN,NaN,NaN,NaN']))
kmer = kmer+'N'
consecutive_lines.append (window+',+0' +','+",".join (middle))
kmer = kmer +middle[2]
for i in range(1,win//2+1):
k = (ref,str(int(pos)+i),strand)
relative_pos = '+'+str(i)
if k in contents:
kmer = kmer +contents[k].split(',')[2]
consecutive_lines.append (window+','+relative_pos+','+contents[k])
else:
kmer = kmer+'N'
consecutive_lines.append (window+','+relative_pos+','+ "," . join ([ref, str(int(pos) +i), 'N', strand, '0', 'NaN,NaN,NaN,NaN,NaN,NaN']))
#consecutive_lines.append (window+','+relative_pos+','+ "," . join (['NaN']*11))
for l in consecutive_lines:
print (kmer+','+l,file = outfh)
keys = list(contents.keys())
del consecutive_lines
if k_to_del in contents:
del contents[k_to_del]
lines = lines[1:]
new_line = fh.readline().decode('utf-8').rstrip()
lines.append (new_line)
ref,pos,base,strand = new_line.split(',')[:4]
contents[(ref,pos,strand)] = new_line
print_last_consecutive_lines (lines, outfh)
outfh.close()
#out2 = prefix + '.per_site.{}mer.csv'.format(win)
out2 = prefix + '.per.site.{}mer.csv'.format(win)
outh2 = open (out2,'w')
q_in_head = ",".join (["q{}".format(i) for i in range(1,win+1)])
mis_in_head = ",".join (["mis{}".format(i) for i in range(1,win+1)])
ins_in_head = ",".join (["ins{}".format(i) for i in range(1,win+1)])
del_in_head = ",".join (["del{}".format(i) for i in range(1,win+1)])
outh2.write ('#Kmer,Window,Ref,Strand,Coverage,{},{},{},{}\n'.format(q_in_head, mis_in_head, ins_in_head, del_in_head))
tmpfh = open (out_tmp,'r')
cov, q, mis, ins, dele = [], [], [], [], []
firstline = tmpfh.readline().rstrip().split(',')
current_win = (firstline[0], firstline[1], firstline[3], firstline[6])
lines = []
lines.append (firstline)
ary = []
for l in tmpfh:
ary = l.rstrip().split(',')
try:
window = (ary[0], ary[1], ary[3], ary[6])
except:
print (l.rstrip())
if window != current_win:
for ele in lines:
q.append (ele[8])
mis.append (ele[11])
ins.append (ele[12])
dele.append (ele[13])
cov.append(ele[7])
Qs = ",".join (q)
Mis = ",".join (mis)
Ins = ",".join (ins)
Del = ",".join(dele)
Cov = ":".join (cov)
print (",".join (current_win), Cov, Qs, Mis, Ins, Del, sep=",", file= outh2)
cov, q, mis, ins, dele = [], [], [], [], []
current_win = window
lines = []
lines.append (ary)
# last 5 lines
cov, q, mis, ins, dele = [], [], [], [], []
for ele in lines:
q.append (ele[8])
mis.append (ele[11])
ins.append (ele[12])
dele.append (ele[13])
cov.append (ele[7])
Qs = ",".join (q)
Mis = ",".join (mis)
Ins = ",".join (ins)
Del = ",".join(dele)
Cov = ":".join (cov)
print (",".join (window), Cov, Qs, Mis, Ins, Del, sep=",", file= outh2)
tmpfh.close()
outh2.close()
os.remove (out_tmp)
return (out2)
def per_read_var (tsv):
''' single thread'''
rdnames = []
qualities = {}
dels = defaultdict(int)
mis = {}
ins = {}
match = {}
ref_pos = {}
k = ''
next_k = ''
if tsv.endswith (".gz"):
fh = gzip.open (tsv)
else:
fh = open (tsv)
for line in fh:
if re.match ('\s+',line):
continue
if re.match('#',line):
continue
if re.match (':',line):
continue
ary = line.rstrip().split()
try:
if not re.match (r'[MID]',ary[8]):
continue
except:
print ('problematic line:', line)
if len (ary) != 10:
continue
if ary[6].startswith('-'):
continue
if re.match (r'[HS]',ary[8]):
continue
#READ_NAME FLAG CHROM READ_POS BASE QUAL REF_POS REF OP
if ary[6] != '.':
ary[6] = str(int(ary[6])) # ref pos is 1-based
ref = ary[2]
ref_pos = ary[6]
ref_base = ary[7]
rd = ary[0]
rd_pos = ''
if ary[8] != 'D':
rd_pos = str (int (ary[3]) + 1)#turn read_pos into 1-based
rd_base = ary[4]
strand = ary[-1]
k = ','.join ([ref,ref_pos,strand,rd,rd_pos]) #include reference to account for multi-mappings
qualities[k] = ord(ary[5]) - 33
dels[k] = dels.get(k,0) + 0
rdnames.append(k)
if ary[8].upper() == 'M' and ary[4] == ary[7]: # or re.match ('I', ary[-1].upper()):
mis[k] = '0'
ins[k] = '0'
elif ary[8].upper() == 'M' and ary[4] != ary[7]: # or re.match ('I', ary[-1].upper()):
mis[k] = '1'
ins[k] = '0'
elif ary[8].upper() == 'I':
ins[k] = '1'
mis[k] = '0'
elif ary[8].upper() == 'D':
dels[k]= dels.get(k,0) + 1
mis[k] = '0'
ins[k] = '0'
else:
continue
prefix = re.sub(r'.tsv$', '', tsv)
del_tmp = prefix + '.per_read_var.tmp.csv'
tmp_fh = open (del_tmp,'w')
if tsv.endswith (".gz"):
fh = gzip.open (tsv)
else:
fh = open (tsv)
for l in fh:
if l.startswith ('#'):
continue
if re.match ('\s+',l):
continue
if re.match('#',l):
continue
if re.match (':',l):
continue
ary = l.rstrip().split()
if len (ary) != 10:
continue
if ary[6].startswith('-') :
continue
ref = ary[2]
ref_pos = ary[6]