-
Notifications
You must be signed in to change notification settings - Fork 2
/
isclipped.py
1333 lines (1172 loc) · 60.3 KB
/
isclipped.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
# Implement resolve position conflict function when two IS elements are inserted at the same position.
import string
import pandas as pd
import numpy as np
from scipy.stats import fisher_exact
from Bio.Blast.Applications import NcbiblastnCommandline
import random
import re
import os
import gff
from functools import lru_cache
import pysamstats
from statistics import mean
from sklearn.cluster import AgglomerativeClustering
import logging
# specify class for clipped reads
class ISClipped:
def __init__(self, aln, ref_name, gff_name, workdir='ijump_wd', pairs_df_path='ijump_junction_pairs.txt'):
# Input files:
# pysam file
self.aln = aln
# File with the reference genome in FASTA format
self.ref_name = ref_name
# A file with GFF annotations
self.gff_name = gff_name
# Set a GFF object for GFF file.
self.gff = gff.gff(self.gff_name)
# Work directory directory
self.workdir = workdir
# Path to the output pairs table.
# In case of preliminary exit an empty table will be generated.
self.pairs_df_path = pairs_df_path
# Tables:
# Clipped reads from the forward search (IS -> Ref_genome)
# Dictionary is used for speed purposes.
self.clipped_reads = self._cltbl_init()
self.clipped_reads_dict = {}
# Clipped reads from the backward search for precise pipeline (Ref_genome -> IS)
# Dictionary is used for speed purposes.
self.clipped_reads_bwrd = self._cltbl_init()
self.clipped_reads_bwrd_dict = {}
# Filtered BLAST output from search juction location for unaligned part of clipped reads
self.blastout_filtered = self._blastout_filtered_init()
# Junction positions table
self.junctions = self._jtbl_init()
# Table with genetic element (GE) centic representation: for each GE number of supporting reads
# for each IS is shown
self.sum_by_region = pd.DataFrame()
# Table of each insertion event
self.report_table = pd.DataFrame()
# Create pairs table.
# Used in a precise mode to collect information of insertion coordinates in reference.
self.pairs_df = self._pairs_table_init()
# Helper structures:
# Length of reference contigs
self.ref_len = dict()
# Keep track an unique indeces of the reads
self._index = 0
# Dictionary of depth attriduted to unclipped reads in given positions
# unclipped reads do not contain "S" in CIGAR string
self.unclipped_depth = {}
# Depth of coverage from clipped reads on the position of junctions.
# Only clipped reads that DO NOT have junction in the position of interest are count.
# Required to separate close insertions of IS elements.
self.cl_read_cov_overlap = {}
# Boundaries of clipped reads
self.boundaries = list()
# Coordinates of provided IS elements. IS name => [chrom, start, stop]
self.is_coords = dict()
# List of lengths for matched segments
# Used to calculate correction coefficients
self.match_lengths = list()
# Initialize nested dictionaries as for each dictionary we need contig->position id.
for contig_i in range(len(self.aln.references)):
self.ref_len[self.aln.references[contig_i]] = self.aln.lengths[contig_i]
self.unclipped_depth[self.aln.references[contig_i]] = {}
self.cl_read_cov_overlap[self.aln.references[contig_i]] = {}
# Parameters:
# Show junctions only with this frequency or more
self.cutoff = 0.005
# Minimum match in sequences
self.min_match = 150
# Average read length
self.av_read_len = 150
# Total length of reads
self.read_lengths = 0
# Number of analyzed reads
self.n_reads_analyzed = 0
# Minimum length of clipped part to use in BLAST
self.blast_min = 10
# Maximum expected length of duplication created from the insertion event
self.max_is_dup_len = 20
# Minimum identity of clipped read BLAST hit to accept it
self.blast_min_ident = 75
# Circos parameters and helper structures:
# Colors used for IS elements and contig representation
self._cirocs_colors = ('green',
'red',
'blue',
'purple',
'orange',
'yellow',
'grey')
# Colors assigned to each chromosome
self._ref_colours = dict()
# Colours assigned to each IS element
self._is_colours = dict()
# Data folder for circos files
self.data_folder = './ijump_data/'
# Id of the session (used in a data folder and config names)
self.session_id = ''
# Initialize a pairs table.
# Used in a precise mode to collect information of insertion coordinates in reference.
@staticmethod
def _pairs_table_init():
return pd.DataFrame( # prototype of pairs table
{'IS_name': ['-'],
'Position_l': [0],
'Position_r': [0],
'Count_mapped_to_IS_l': [0],
'Count_mapped_to_IS_r': [0],
'Chrom': ['-']
}
)
# Generate an empty output table
@staticmethod
def pairs_table_empty():
return pd.DataFrame(
{'Position_l': [],
'Position_r': [],
'Count_mapped_to_IS_l': [],
'Count_mapped_to_IS_r': [],
'Chrom': [],
'IS_name': [],
'Dist': [],
'N_unclipped_l': [],
'N_clipped_l': [],
'N_unclipped_r': [],
'N_clipped_r': [],
'N_overlap_l': [],
'N_overlap_r': [],
'N_clipped_l_correction': [],
'N_clipped_r_correction': [],
'N_overlap_l_correction': [],
'N_overlap_r_correction': [],
'N_clipped_l_corrected': [],
'N_overlap_l_corrected': [],
'N_clipped_r_corrected': [],
'N_overlap_r_corrected': [],
'N_overlap_formula_l': [],
'N_overlap_formula_r': [],
'Frequency_l': [],
'Frequency_r': [],
'Frequency': [],
'Depth': []
}
)
# Initialize a report table.
@staticmethod
def report_table_init():
return pd.DataFrame(columns=['IS Name',
'Annotation',
'Chromosome',
'Start',
'Stop',
'Frequency',
'Depth'])
# Initialize a new copy of clipped reads table.
@staticmethod
def _cltbl_init():
return pd.DataFrame(columns=['ID',
'IS name',
'IS_chrom',
'Read name',
'left pos', # left position of a clipped segment in a read
'right pos', # right position of a clipped segment in a read
'clip_position', # clip position in a reference
'junction_in_read', # side of a clipped segment connected to a read (l/r)
'reverse', # is read reverse
'sequence']) # sequence of a clipped read
# Initialize a filtered blast output table.
@staticmethod
def _blastout_filtered_init():
return pd.DataFrame(columns=['qseqid',
'sseqid',
'pident',
'length',
'mismatch',
'gapopen',
'qstart',
'qend',
'sstart',
'send',
'evalue',
'bitscore',
'pos_in_ref',
'orientation'])
# Create summary dataframe.
def sum_by_reg_tbl_init(self):
sbrcolumns = ['ann', 'chrom', 'start', 'stop']
sbrcolumns.extend(list(self.is_coords.keys()))
return pd.DataFrame(columns=sbrcolumns)
# Collect information about IS elements.
def iscollect(self, file):
logging.info(f'Read file with IS elements: {file}')
with open(file, 'r') as is_coords_file:
for coord in is_coords_file.readlines():
c = coord.split()
self.is_coords[c[0]] = c[1:]
# Initialize junction table.
@staticmethod
def _jtbl_init(n_rows=0):
return pd.DataFrame(columns=['ID',
'IS name',
'IS pos',
'IS_chrom',
'Read name',
'Chrom',
'Position',
'Orientation', # where non-clipped region is relative to position
'Note',
'Locus tag',
'Gene'],
index=[i for i in range(n_rows)])
# For a clipped segment return left, right positions, junction side, coordinate of adjacent non-clipped nucleotide.
@staticmethod
def _clboundaries(read):
positions = read.get_reference_positions(full_length=True)
clipped_segments = list()
# Start of clipped segments
start_clipped_segment = 0
# Is previous position clipped?
is_cl_prev = False
# Is this clipped segment left or right?
# The segment is "left" if unmapped part of the read is not aligned at current position.
is_left = None
# Which part of the read is clipped?
clipped_part = ''
# Position of a junction in an aligned part of a read
junction_pos = 0
# Collect all clipped segments in a read.
# Sometimes there are more than one clipped segment (e.g. CIGAR: 30S90M30S).
for pos_index in range(len(positions)):
# Check if the position is a start pf a clipped part
if positions[pos_index] is None and is_cl_prev is False:
if pos_index == 0:
is_left = True
else:
is_left = False
clipped_part = 'right'
junction_pos = positions[pos_index - 1]
start_clipped_segment = pos_index + 1
is_cl_prev = True
# If current position is (1a) in aligned part of the read or (1b) it is an end of the read
# and (2) previous position was in unaligned part of the read then (3) assign this position as the end of
# the clipped part.
elif (isinstance(positions[pos_index], int) or (pos_index + 1) == len(positions)) and is_cl_prev is True:
# End of a clipped segment
end_clipped_segment = pos_index
if (pos_index + 1) == len(positions):
end_clipped_segment = pos_index + 1
is_cl_prev = False
if is_left is True:
junction_pos = positions[pos_index]
clipped_part = 'left'
clipped_segments.append([start_clipped_segment, end_clipped_segment, clipped_part, junction_pos])
return clipped_segments
# return clipped portion of a read
@staticmethod
def _clipped_seq(read, left, right):
return read.query_sequence[(left - 1):right]
# For each IS element set boundaries where to search clipped reads.
def set_is_boundaries(self, radius):
logging.info('Set area near IS elements boundaries to search clipped reads.')
for is_name in self.is_coords.keys():
# For a IS element take its contig and coordinates.
chrom = self.is_coords[is_name][0]
start = int(self.is_coords[is_name][1])
stop = int(self.is_coords[is_name][2])
# Check if search area for clipped reads goes outside contig boundaries.
# ASSUMPTION OF COMPLETENESS!: If it does so consider all contigs as circular.
# Check for a start coordinate.
if start - radius < 0:
self.boundaries.append(
[self.ref_len[chrom] - radius + start, self.ref_len[chrom], "start", is_name, chrom])
self.boundaries.append([0, start + radius, "start", is_name, chrom])
elif start + radius > self.ref_len[chrom]:
self.boundaries.append([start - radius, self.ref_len[chrom], "start", is_name, chrom])
self.boundaries.append([0, start + radius - self.ref_len[chrom], "start", is_name, chrom])
else:
self.boundaries.append([start - radius, start + radius, "start", is_name, chrom])
# Check for an end coordinate.
if stop + radius > self.ref_len[chrom]:
self.boundaries.append([stop - radius, self.ref_len[chrom], "stop", is_name, chrom])
self.boundaries.append([0, stop + radius - self.ref_len[chrom], "stop", is_name, chrom])
elif stop - radius < 0:
self.boundaries.append(
[self.ref_len[chrom] - radius + stop, self.ref_len[chrom], "stop", is_name, chrom])
self.boundaries.append([0, stop + radius, "stop", is_name, chrom])
else:
self.boundaries.append([stop - radius, stop + radius, "stop", is_name, chrom])
# Collect clipped reads and check if IS elements are on boundaries of contigs.
def collect_clipped_reads(self):
for b in self.boundaries:
logging.info('Collect clipped reads: ' + ' '.join(str(x) for x in b))
# Set parameters for clipped reads search
chrom = b[4]
start_collection = b[0]
stop_collection = b[1]
edge_of_is = b[2]
name_of_is = b[3]
# Collect clipped reads
self._crtable_ungapped(chrom,
start_collection,
stop_collection,
edge_of_is,
name_of_is,
1)
# Collect information about coverage that comes from clipped reads outside junction position
def _cl_read_cov_overlap(self, aln_pairs, chrom):
if len(aln_pairs) < 3:
return 0
read_pos = [a_pair[0] for a_pair in aln_pairs]
ref_pos = [a_pair[1] for a_pair in aln_pairs]
for i in read_pos[1:-1]:
# If nucleotide is not aligned - skip it
if i is None:
continue
else:
# If the position is junction - skip it
if ref_pos[i - 1] is None or ref_pos[i + 1] is None:
continue
else:
self.cl_read_cov_overlap[chrom][ref_pos[i]] = self.cl_read_cov_overlap[chrom].get(ref_pos[i], 0) + 1
# Collect clipped reads from the intervals that do not cross boundaries of a contig.
# The more correct way to collect junction positions would be to find another part of the
# clipped read in the alignment and take its coordinates. CIGAR strings for both parts could
# be not mirrored due to some short repeats (1+nt size) near junction positions.
# direction: 1 => IS->Ref, 0 => Ref->IS (in precise pipeline)
def _crtable_ungapped(self, chrom, start, stop, edge, is_name, direction): # generate clipped read table
# One is added to convert from 0-based to 1-based system
for read in self.aln.fetch(chrom, start + 1, stop + 1):
# Add read length to collection of lengths.
if direction:
if read.infer_read_length():
self.read_lengths += read.infer_read_length()
self.n_reads_analyzed += 1
# Skip unmapped read
if read.is_unmapped:
continue
# Skip if the read is not clipped
if 'S' not in read.cigarstring:
continue
if direction:
# Collect lengths of read segments that match reference to calculate correction coefficient.
m_len = [int(x) for x in re.findall(r'(\d+)M', read.cigarstring)]
# Leave only the longest match from read.
m_len = max(m_len)
# Add lengths to collection.
self.match_lengths.append(m_len)
else:
# If it is Ref->IS direction of search:
# Add coverage from aligned positions of clipped reads that are not junctions.
self._cl_read_cov_overlap(read.aligned_pairs, read.reference_name)
# Get clipped segments coordinates from the read
boundaries = self._clboundaries(read)
for cl_seg in boundaries:
# On the IS->Ref search check if read was collected on the correct side of the IS element
if direction and \
not ((cl_seg[2] == "left" and edge == "start") or (cl_seg[2] == "right" and edge == "stop")):
continue
clip_temp = {
# Unique read ID
'ID': self._index,
# IS name
'IS name': is_name if direction else '-',
# Contig where IS element is located for IS->Ref search and clipped read of Ref->IS
'IS_chrom': chrom,
'Read name': read.query_name,
# Coordinate of clipped segment start
'left pos': cl_seg[0],
# Coordinate of clipped segment end
'right pos': cl_seg[1],
# IS clipped segment on "left" from alignment or on "right"
'clip_position': cl_seg[2],
# Coordinate of junction nucleotide on contig.
# At IS side for IS->Ref search and for contig for Ref->IS search
'junction_in_read': cl_seg[3],
# Is read forward or reverse
'reverse': True if read.is_reverse else False,
# Sequence of clipped segment
'sequence': self._clipped_seq(read, cl_seg[0], cl_seg[1])
}
# Add clipped read information to dictionary to build DataFrame.
# It is faster than append segments-by-segments to the existing DataFrame.
# As we don't know number of clipped segments we could not create and empty DataFrame of required size.
if direction:
self.clipped_reads_dict[self._index] = clip_temp
else:
self.clipped_reads_bwrd_dict[self._index] = clip_temp
self._index = self._index + 1
# Write clipped parts of reads to FASTA file. Use only parts > min_length.
# direction: 1 => IS->Ref, 0 => Ref->IS
def _write_cl_fasta(self, cl_fasta_name, min_len, direction):
if direction:
cl_table = self.clipped_reads
else:
cl_table = self.clipped_reads_bwrd
with open(cl_fasta_name, 'w') as fasta_file:
cl_table.index = cl_table['ID']
for index in cl_table.index:
if len(cl_table.at[index, 'sequence']) >= min_len:
fasta_file.write('>' + str(cl_table.at[index, 'ID']) + '\n')
fasta_file.write(str(cl_table.at[index, 'sequence']) + '\n')
fasta_file.write('\n')
# run blast and write output to xml
# direction: 1 => IS->Ref, 0 => Ref->IS
def runblast(self, in_file, out_file, direction):
logging.info('Run BLAST for clipped parts of the reads')
fasta_file = os.path.join(self.workdir, in_file)
blast_out_file = os.path.join(self.workdir, out_file)
self._write_cl_fasta(fasta_file, self.blast_min, direction)
# Run BLAST
blastn_cl = NcbiblastnCommandline(query=fasta_file, db=self.ref_name, evalue=0.001, out=blast_out_file,
outfmt=6, word_size=10)
blastn_cl()
# Choose left or right coordinate as a clipped junction and orientation relative to junction.
@staticmethod
def _choosecoord(qleft, qright, lr):
qcoord = [qleft, qright]
qorientation = ['left', 'right']
coord = int(qcoord[lr == 'left'])
orientation = qorientation[not (qcoord[1] > qcoord[0]) ^ (lr == 'left')]
return coord, orientation
# Parse BALST output.
# direction: 1=>IS->Ref, 0=>Ref->IS. For Ref->IS we don't need to remove duplicates, we need only one of them
def parseblast(self, blast_out_file, direction):
logging.info('Collect information from BLAST')
blast_out_path = os.path.join(self.workdir, blast_out_file)
# Check if Blast is not empty
if os.path.isfile(blast_out_path) and os.path.getsize(blast_out_path) > 0:
blast_out = pd.read_csv(blast_out_path, sep='\t')
else:
logging.info('No BLAST hits were found.')
pairs_df = self.pairs_table_empty()
pairs_df.to_csv(self.pairs_df_path, sep='\t', index=False)
exit(0)
blast_out.columns = ['qseqid',
'sseqid',
'pident',
'length',
'mismatch',
'gapopen',
'qstart',
'qend',
'sstart',
'send',
'evalue',
'bitscore']
# Filter only hits with identity [self.blast_min_ident]% or higher. Default: 75%.
blast_out = blast_out[blast_out['pident'] >= self.blast_min_ident]
idx_max = blast_out.groupby('qseqid')['bitscore'].transform(max) == blast_out['bitscore']
# Temporary dataframe for filtering with only best hits by bitscore.
temp = blast_out[idx_max].copy()
if direction:
temp['count'] = temp.groupby('qseqid')['qseqid'].transform('count')
# Leave only hits with one best hit.
temp = temp[temp['count'] == 1]
temp = temp.drop(columns=['count'])
cl_table = self.clipped_reads
else:
temp['rank'] = temp.groupby('qseqid')['bitscore'].rank(method="first", ascending=True)
# Leave only first best hit.
temp = temp[temp['rank'] == 1]
temp = temp.drop(columns=['rank'])
cl_table = self.clipped_reads_bwrd
for index in temp.index:
pos, orient = self._choosecoord(temp.at[index, 'sstart'],
temp.at[index, 'send'],
cl_table.at[temp.at[index, 'qseqid'], 'clip_position'])
temp.at[index, 'pos_in_ref'] = pos
temp.at[index, 'orientation'] = orient
# Check if temp has any entries.
if temp.size:
temp['pos_in_ref'] = temp['pos_in_ref'].astype(int)
else:
logging.info('No significant BLAST hits.')
pairs_df = self.pairs_table_empty()
pairs_df.to_csv(self.pairs_df_path, sep='\t', index=False)
exit(0)
self.blastout_filtered = temp
# Check if position close to the IS element
def _check_is_boundary_proximity(self, chrom, position):
for b in self.boundaries:
if b[4] == chrom:
boundary_width = b[1] - b[0]
# if b[0] - boundary_width / 2 <= position <= b[1] + boundary_width / 2: # use doubled boundaries
if b[0] <= position <= b[1]:
return 'IS element', b[3]
return '-', '-'
# Cluster positions together to form seed for backwards clipped read search.
@staticmethod
def _hclust(X):
# If only one sample is present – clustering will not work
if len(X) == 1:
return [0]
hcl = AgglomerativeClustering(n_clusters=None, distance_threshold=30, linkage='single'). \
fit(X.to_numpy().reshape(-1, 1))
return hcl.labels_
# Use hierarchical clustering to cluster close positions in the chromosome.
def make_gene_side_regions(self):
logging.info('Cluster close positions in the chromosome')
# Remove positions close to the IS elements boundaries from the analysis
ref_cl_reads = self.blastout_filtered[['sseqid', 'pos_in_ref']].copy()
ref_cl_reads = ref_cl_reads.rename(columns={'sseqid': 'Chrom', 'pos_in_ref': 'Position'})
ref_cl_reads['Note'] = ref_cl_reads.apply(
lambda x: self._check_is_boundary_proximity(x['Chrom'], x['Position'])[0],
axis=1
)
ref_cl_reads = ref_cl_reads[ref_cl_reads['Note'] == '-']
ref_cl_reads = ref_cl_reads.drop(columns=['Note'])
# If no hits point outside IS elements boudaries there is no insertions to find
if ref_cl_reads.size == 0:
logging.info("No BLAST hits point oustide IS elements. No significant new insertions could be found.")
pairs_df = self.pairs_table_empty()
pairs_df.to_csv(self.pairs_df_path, sep='\t', index=False)
exit(0)
ref_cl_reads['Cluster'] = ref_cl_reads. \
sort_values(by=['Chrom', 'Position']). \
groupby(['Chrom'])['Position']. \
transform(self._hclust)
ref_regions = ref_cl_reads.groupby(['Cluster', 'Chrom']). \
aggregate(['min', 'max']). \
reset_index()
ref_regions.columns = ['Cluster', 'Chrom', 'Position_left', 'Position_right']
ref_regions = ref_regions.drop(columns=['Cluster'])
# Extend regions by 5nt if possible
ref_regions['Position_left'] = ref_regions['Position_left']. \
apply(lambda x: max(x - 5, 0))
ref_regions['Position_right'] = ref_regions. \
apply(lambda x: min(x['Position_right'] + 5, self.ref_len[x['Chrom']]), axis=1)
return ref_regions
# Collect reads from reference regions (backwards mapping of clipped reads to their IS elements).
def crtable_bwds(self, ref_regions):
logging.info('Collect clipped reads from the reference location')
ref_regions.apply(
lambda x: self._crtable_ungapped(
x['Chrom'],
x['Position_left'],
x['Position_right'],
'-',
'-',
0
),
axis=1)
# Create table for description of junctions.
# direction: 1=>IS->Ref, 0=>Ref->IS.
def call_junctions(self, direction):
logging.info('Create junction table')
self.junctions = self._jtbl_init(self.blastout_filtered.shape[0])
index = 0
for hit in self.blastout_filtered.itertuples(index=False):
read_id = hit.qseqid
if direction:
pos = hit.pos_in_ref
chrom = hit.sseqid
is_name = self.clipped_reads['IS name'][read_id]
is_chrom = self.clipped_reads['IS_chrom'][read_id]
is_pos = self.clipped_reads['clip_position'][read_id]
is_elem_border_mark, _ = self._check_is_boundary_proximity(chrom, pos)
orientation = hit.orientation
read_name = self.clipped_reads['Read name'][read_id]
else:
pos = self.clipped_reads_bwrd['junction_in_read'][read_id]
chrom = self.clipped_reads_bwrd['IS_chrom'][read_id]
is_chrom = hit.sseqid
is_pos = hit.pos_in_ref
_, is_name = self._check_is_boundary_proximity(is_chrom, is_pos)
is_elem_border_mark = '-'
orientation = self.clipped_reads_bwrd['clip_position'][read_id]
read_name = self.clipped_reads_bwrd['Read name'][read_id]
self.junctions.at[index, 'ID'] = read_id
self.junctions.at[index, 'IS name'] = is_name
self.junctions.at[index, 'IS_chrom'] = is_chrom
self.junctions.at[index, 'IS pos'] = is_pos
self.junctions.at[index, 'Read name'] = read_name
self.junctions.at[index, 'Chrom'] = chrom
self.junctions.at[index, 'Position'] = pos
self.junctions.at[index, 'Orientation'] = orientation
self.junctions.at[index, 'Locus tag'] = self.gff.gff_pos[chrom][pos][0]
self.junctions.at[index, 'Gene'] = self.gff.gff_pos[chrom][pos][1]
self.junctions.at[index, 'Note'] = is_elem_border_mark
index += 1
self.junctions = self.junctions.reset_index()
# Make clusters of left and right insertions junctions from positions.
# Outputs table of right and left positions of of junctions pairs with counts of clipped
# reads accounted to each junction.
# Similar results could be achieved by KNN search, but this algorithm shows slightly better performance on
# tests.
@staticmethod
def _find_pair(pos_l, pos_r, pos_l_count, pos_r_count, chrom_len, max_is_dup_len, chrom):
# Check if both left and right junctions present. If not - process just present part of junctions.
if pos_l.size == 0 or pos_r.size == 0:
n_pairs = pos_l.size + pos_r.size
pairs_df = pd.DataFrame(
{'Position_l': [0] * n_pairs,
'Position_r': [0] * n_pairs,
'Count_mapped_to_IS_l': [0] * n_pairs,
'Count_mapped_to_IS_r': [0] * n_pairs,
'Chrom': chrom}
)
if pos_r.size == 0:
for pos_l_index, pos in enumerate(pos_l):
pairs_df.iloc[pos_l_index, :] = [
pos,
0,
pos_l_count[pos_l_index],
0,
chrom
]
else:
for pos_r_index, pos in enumerate(pos_r):
pairs_df.iloc[pos_r_index, :] = [
pos,
0,
pos_r_count[pos_r_index],
0,
chrom
]
return pairs_df
# Store close positions in the matrix where rows are left positions and columns are right positions.
# The value is 1 if two positions are closer then max_is_dup_len value.
closeness_matrix = np.zeros((pos_l.size, pos_r.size))
# Check if any position close to the contig ends.
if pos_r[-1] - pos_l[0] > chrom_len / 2:
if chrom_len - (pos_r[-1] - pos_l[0]) <= max_is_dup_len:
closeness_matrix[0, -1] = 1
if pos_l[-1] - pos_r[0] > chrom_len / 2:
if chrom_len - (pos_l[-1] - pos_r[0]) <= max_is_dup_len:
closeness_matrix[-1, 0] = 1
# Populate closeness matrix.
for pos_index, pos in enumerate(pos_l):
closeness_matrix[pos_index] = (np.ones_like(pos_r) * (np.abs(pos_r - pos) < max_is_dup_len)).astype(np.int0)
# Assign clusters and sort in each cluster by junction representation in descending order.
# Build dataframe to populate pairs.
# We will use maximum number of rows (if all positions do not have pairs).
n_pairs = np.sum(closeness_matrix.shape)
pairs_df = pd.DataFrame(
{'Position_l': [0] * n_pairs,
'Position_r': [0] * n_pairs,
'Count_mapped_to_IS_l': [0] * n_pairs,
'Count_mapped_to_IS_r': [0] * n_pairs,
'Chrom': chrom}
)
# Build clusters of close positions.
# Clusters are attributed to the left joints.
cluster_ids = np.zeros(len(closeness_matrix))
cluster_cur_id = 0
column_index = 0
# Itrerate through all closeness_matrix columns or until all left joints will be assigned to clusters.
while not (column_index >= closeness_matrix.shape[1] or np.all(cluster_ids > 0)):
# Check if the column not zero (orphan right position).
if closeness_matrix[:, column_index].any():
# If any left position has several right positions in proximity
# unite clusters.
if np.any(closeness_matrix[:, column_index][cluster_ids > 0] == 1):
cluster_ids[closeness_matrix[:, column_index] == 1] = cluster_cur_id
else:
# If cluster is first or clusters do not overlap add cluster id.
cluster_cur_id += 1
cluster_ids[closeness_matrix[:, column_index] == 1] = cluster_cur_id
column_index += 1
# Sort each cluster.
for cluster_id in np.unique(cluster_ids[cluster_ids > 0]):
# Sort positions sub-list.
cluster_pos_l = pos_l[np.where(cluster_ids == cluster_id)]
cluster_pos_l = \
cluster_pos_l[np.argsort(pos_l_count[np.where(cluster_ids == cluster_id)])[::-1]]
pos_l[np.where(cluster_ids == cluster_id)] = cluster_pos_l
# Sort closeness sub-matrix.
cluster_closeness_matrix = closeness_matrix[np.where(cluster_ids == cluster_id)]
cluster_closeness_matrix = \
cluster_closeness_matrix[np.argsort(pos_l_count[np.where(cluster_ids == cluster_id)])[::-1]]
closeness_matrix[np.where(cluster_ids == cluster_id), :] = cluster_closeness_matrix
# Sort counsts sub-list.
cluster_pos_l_count = pos_l_count[np.where(cluster_ids == cluster_id)]
cluster_pos_l_count = \
cluster_pos_l_count[np.argsort(pos_l_count[np.where(cluster_ids == cluster_id)])[::-1]]
pos_l_count[np.where(cluster_ids == cluster_id)] = cluster_pos_l_count
# Collect right indexes.
pos_r_orphan = np.arange(pos_r.size)
# Populate pairs table.
for pos_l_index, pos_l_cur in enumerate(pos_l):
if np.sum(closeness_matrix[pos_l_index, :]):
# Find right index that has minimum difference in counts.
# Penalize non-cluster items difference by 10000.
pos_r_index = np.argmin(
np.abs(pos_r_count - pos_l_count[pos_l_index]) + ~(closeness_matrix[pos_l_index, :] == 1) * 10000
)
pairs_df.iloc[pos_l_index, :] = [
pos_l_cur,
pos_r[pos_r_index],
pos_l_count[pos_l_index],
pos_r_count[pos_r_index],
chrom
]
closeness_matrix[:, pos_r_index] = 0
pos_r_orphan[pos_r_index] = -1
# Write orhphan peaks.
else:
pairs_df.iloc[pos_l_index, :] = [
pos_l_cur,
0,
pos_l_count[pos_l_index],
0,
chrom
]
df_offset = len(pos_l)
# Add right orphan peaks.
for shift, pos_r_index_orphan in enumerate(pos_r_orphan[pos_r_orphan != -1]):
pairs_df.iloc[df_offset + shift, :] = [
0,
pos_r[pos_r_index_orphan],
0,
pos_r_count[pos_r_index_orphan],
chrom
]
# Remove empty rows.
pairs_df = pairs_df.query('Position_l > 0 or Position_r > 0')
return pairs_df
# Find positions of insertions.
def search_insert_pos(self):
logging.info('Serach for junction pairs')
position_tbl = self.junctions[self.junctions['IS name'] != '-'].copy()
# It is much better to work with when IS elements collapsed by their copy
# than to work with each copy separately.
# Remove copy tags from the IS element names like "_1", "_2".
position_tbl['IS'] = position_tbl['IS name'].apply(lambda x: re.search(r'(.+)_\d+', x).group(1))
position_tbl = position_tbl.groupby(['Chrom', 'Position', 'IS', 'Orientation'])['Position']. \
count(). \
reset_index(name='Counts')
# Collect dataframes for pairs of junctions (or orphan junctions) that should mark IS elements insertions.
is_pairs_collection = []
# Find pairs
for chrom in position_tbl['Chrom'].drop_duplicates().tolist():
# Take IS elements only from the selected chromosome.
position_tbl_chrom = position_tbl.query('Chrom == @chrom')
for is_name in position_tbl_chrom['IS'].drop_duplicates().tolist():
positions_left = position_tbl_chrom.query(
'Orientation == "left" & IS == @is_name'
).sort_values('Position')
positions_left_pos = positions_left['Position'].to_numpy()
positions_left_counts = positions_left['Counts'].to_numpy()
positions_right = position_tbl_chrom.query(
'Orientation == "right" & IS == @is_name'
).sort_values('Position')
positions_right_pos = positions_right['Position'].to_numpy()
positions_right_counts = positions_right['Counts'].to_numpy()
logging.info(f'Find pairs for {is_name} and {chrom} contig ')
# Calculate table of pairs
pair_tbl_chunk = self._find_pair(
positions_left_pos,
positions_right_pos,
positions_left_counts,
positions_right_counts,
self.ref_len[chrom],
self.max_is_dup_len,
chrom
)
pair_tbl_chunk['IS_name'] = is_name
is_pairs_collection.append(pair_tbl_chunk)
# Concatenate all pair tables into one table.
self.pairs_df = pd.concat(is_pairs_collection, ignore_index=True)
# Calculate frequency of IS insertion based on frequencies of boundaries junctions.
@staticmethod
def _calc_freq_precise(freq_l, freq_r):
if freq_l == 0:
return freq_r
elif freq_r == 0:
return freq_l
else:
return (freq_l + freq_r) / 2
# Make read count matrices.
@staticmethod
def _read_count_mtx(pairs_df, orientation):
if orientation == 'left':
pos_df = pairs_df.query('Position_l > 0').copy()
pos_df = pos_df.rename(columns={'Position_l': 'Position', 'Count_mapped_to_IS_l': 'Count'})
elif orientation == 'right':
pos_df = pairs_df.query('Position_r > 0').copy()
pos_df = pos_df.rename(columns={'Position_r': 'Position', 'Count_mapped_to_IS_r': 'Count'})
else:
logging.error('Error: the parameter should be "left" or "right"')
exit(1)
# Dictionary to translate positions (Contig name/Coordinate) to matrix row indeces.
pos = {}
for chrom in pos_df.Chrom.unique():
pos[chrom] = {}
i = 0
for pos_row in pos_df[['Position', 'Chrom']].drop_duplicates().itertuples():
pos[pos_row.Chrom][pos_row.Position] = i
i += 1
# Dictionary to translate IS names to matrix row indeces.
is_names = dict(
zip(
pos_df['IS_name'].unique().tolist(),
[i for i in range(pos_df['IS_name'].unique().size)]
)
)
counts = np.zeros((
len(pos_df[['Position', 'Chrom']].drop_duplicates()),
len(is_names)
))
for row in pos_df.itertuples():
counts[pos[row.Chrom][row.Position], is_names[row.IS_name]] = row.Count
return pos, is_names, counts
# Translate matrix of read count proportions to the original read counts.
# (calculated from the second pass of clipped reads collection)
@staticmethod
def _resore_orig_counts(counts_mtx, original_rc_counts, pos_dict):
counts_mtx /= counts_mtx.sum(1).reshape(-1, 1)
for chr in pos_dict.keys():
for junct_pos in pos_dict[chr].keys():
counts_mtx[pos_dict[chr][junct_pos]] *= original_rc_counts[chr].get(junct_pos, 0)
return counts_mtx
# Count depth at the position using only unclipped reads.
# Input is a data frame with columns 'Position' and 'Chrom'.
def count_depth_unclipped(self, position_tbl):
logging.info('Count depth attributed to unclipped reads')
for position in position_tbl.itertuples():
chrom = position.Chrom
pos = position.Position
ins_pos_distance = position.Dist
if pos == 0:
continue
for read in self.aln.fetch(chrom, pos, pos + 1):
if read.is_unmapped:
# Skip unmapped read
continue
# No soft- or hard-clipped reads
elif ('S' not in read.cigarstring) and ('H' not in read.cigarstring):
# Test if position is near the end of the read. If it is near skip the read as
# it is impossible to distinguish unmapped
read_edges = np.array([read.aligned_pairs[0][1], read.aligned_pairs[-1][1]])
if np.min(np.abs(pos - read_edges)) > ins_pos_distance:
self.unclipped_depth[chrom][pos] = \
self.unclipped_depth[chrom].get(pos, 0) + 1
def _add_total_depth(self, depth_l, depth_r):
if depth_l == 0:
return depth_r
elif depth_r == 0:
return depth_l
else:
return (depth_l + depth_r) / 2
# Add test of excessive reads count that were not mapped to IS elements.
# The expected difference between N_clipped_l + N_clipped_r (clipped reads collected at junction)
# and Count_mapped_to_IS_l + Count_mapped_to_IS_r (clipped reads collected at junction that were able
# to be mapped to IS elements) should be drawn from the uniform distribution and equal
# P(X <= min_match) = (N_clipped_l + N_clipped_r) * min_match/av_read_len
# We can compare observed and expected differences with Fisher exact test.
def fisher_test_clr_number(self, observation):
sum_clr_mapped_to_is = observation.Count_mapped_to_IS_l + observation.Count_mapped_to_IS_r
sum_clr_count_at_jnc = observation.N_clipped_l + observation.N_clipped_r
# We do not expect number of reads mapped to the IS element exceed
# total number of reads found at the region.
if sum_clr_count_at_jnc - sum_clr_mapped_to_is < 0:
return 0
# Contingency table:
# [[(a), (c)]
# [(b), (d)]]
# (a) Number of clipped reads mapped to the IS element
# (b) Number of clipped reads that were not mapped to IS element but found at insertion position
# (c) Estimated number of reads mapped to the IS element
# (d) Estimated number of reads not mapped to the IS element
# As we test if indels account to the significant portion of clipped reads we expect
# (a) to be smaller than expected
contingency_table = np.array(
[
[sum_clr_mapped_to_is, sum_clr_count_at_jnc * (1 - self.min_match / self.av_read_len)],
[sum_clr_count_at_jnc - sum_clr_mapped_to_is, sum_clr_count_at_jnc * self.min_match / self.av_read_len]
]
)
contingency_table.round(0).astype(int)
_, pvalue = fisher_exact(contingency_table)
return pvalue