forked from molgenis/VaSeBuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
variant_context.py
1240 lines (1041 loc) · 39.9 KB
/
variant_context.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
"""Variant Context object class.
This module creates the VariantContext object, which stores all information
related to each individual variant context. It also defines numerous methods
related to its functionality.
"""
import statistics
from overlap_context import OverlapContext
class VariantContext:
"""Save a variant context and allows data to be obtained and calculated.
Attributes
----------
context_id : str
Identifier of the context
sample_id : str
The ID of the donor sample used to construct the variant context
variant_context_chrom : str
Chromosome name the context is located on
variant_context_origin : int
Variant position the context is constructed from
variant_context_start : int
Leftmost genomic position of the variant context
variant_context_end : int
Rightmost genomic position of the variant context
variant_context_areads : list of pysam.AlignedSegment
Acceptor reads associated with the variant context
variant_context_dreads : list of pysam.AlignedSegment
Donor reads associated with the variant context
variant_acceptor_context : OverlapContext
Acceptor context used to construct the variant context
variant_donor_context : OverlapContext
Donor context used to construct the variant context
unmapped_donor_mate_ids : list of str
IDs of reads with an unmapped mate
"""
def __init__(self, varconid, sampleid,
varconchrom, varconorigin,
varconstart, varconend,
acceptorreads, donorreads,
acceptor_context=None, donor_context=None,
variants=None):
"""Save the provided variant context data.
Acceptor and donor contexts are optional when creating the variant
context.
Parameters
----------
varconid : str
Variant context identifier
sampleid : str
Sample name/identifier
varconchrom : str
Chromosome name the variant context is located on
varconorigin:
Variant position the context is constructed from
varconstart : int
Leftmost genomic position of the variant context
varconend : int
Rightmost genomic position of the variant context
acceptorreads : list of pysam.AlignedSegment
Variant context acceptor reads
donorreads : list of pysam.AlignedSegment
Variant context donor reads
acceptor_context : OverlapContext
Acceptor context associated with the variant context
donor_context : OverlapContext
Donor context associated with the variant context
"""
self.context_id = varconid
self.sample_id = sampleid
self.variant_context_chrom = varconchrom
self.variant_context_origin = int(varconorigin)
self.variant_context_start = int(varconstart)
self.variant_context_end = int(varconend)
# Acceptor reads that overlap with the entire variant context.
self.variant_context_areads = acceptorreads
# Donor reads that overlap with the entire variant context.
self.variant_context_dreads = donorreads
# Context from acceptor reads overlapping with the variant itself.
self.variant_acceptor_context = acceptor_context
# Context from donor reads overlapping with the variant itself.
self.variant_donor_context = donor_context
self.unmapped_acceptor_mate_ids = []
self.unmapped_donor_mate_ids = []
# self.priority_label = None
# self.priority_level = None
self.priorities = []
self.variants = variants
if self.variants is None:
self.variants = []
# ===METHODS TO OBTAIN DATA FROM THE VARIANT CONTEXT DATA==================
def get_context(self):
"""Return the essential data of the variant context.
Returns
-------
list of str and int
Variant context window
"""
return [self.variant_context_chrom, self.variant_context_origin,
self.variant_context_start, self.variant_context_end]
def get_variant_context_id(self):
"""Return the variant context identifier.
Returns
-------
self.context_id : str
Variant context identifier
"""
return self.context_id
def get_variant_context_sample(self):
"""Return the name/identifier of the sample.
Returns
-------
self.sample_id : str
Sample name/identifier
"""
return self.sample_id
def get_variant_context_chrom(self):
"""Return the chromosome name the variant context is located on.
Returns
-------
self.variant_context_chrom : str
Chromosome name of the variant context
"""
return self.variant_context_chrom
def get_variant_context_origin(self):
"""Return the position of the variant used to construct the variant context.
Returns
-------
self.variant_context_origin : int
Variant genomic position
"""
return self.variant_context_origin
def get_variant_context_start(self):
"""Return the leftmost genomic position of the variant context.
Returns
-------
self.variant_context_start
Variant context starting position
"""
return self.variant_context_start
def get_variant_context_end(self):
"""Return the variant context end position.
Returns
-------
self.variant_context_end : int
Variant context rightmost genomic position
"""
return self.variant_context_end
def get_acceptor_reads(self):
"""Return the variant context acceptor reads.
Returns
-------
self.variant_context_areads : list of pysam.AlignedSegment
Variant context acceptor reads
"""
return self.variant_context_areads
def get_donor_reads(self):
"""Return the variant context donor reads.
Returns
-------
self.variant_context_dreads : list of pysam.AlignedSegment
Variant context acceptor reads
"""
return self.variant_context_dreads
def set_donor_reads(self, donor_reads):
"""Set a list of reads as the variant context donor reads.
Parameters
----------
donor_reads: list of pysam.AlignedSegment
Donor reads to set
"""
self.variant_context_dreads = donor_reads
def get_donor_read_strings(self):
"""Return all donor reads as tuples of strings.
Tuples each contain 4 strings: Read name, pair number, sequence,
and quality line.
Returns
-------
list(set(donorreads))
List of donor read string tuples.
"""
donorreads = []
for dbr in self.variant_context_dreads:
readpn = "2"
if dbr.is_read1:
readpn = "1"
donorreads.append(
(dbr.query_name,
readpn,
dbr.query_sequence,
"".join([chr(x+33) for x in dbr.query_qualities]))
)
return list(set(donorreads))
def get_acceptor_context(self):
"""Return the acceptor context used to construct the variant context.
Returns
-------
self.variant_acceptor_context : OverlapContext
Acceptor context associated with the variant context
"""
return self.variant_acceptor_context
def get_donor_context(self):
"""Return the donor context used to construct the variant context.
Returns
-------
self.variant_donor_context : OverlapContext
Donor context associated with the variant context
"""
return self.variant_donor_context
def get_unmapped_acceptor_mate_ids(self):
"""Return the IDs of variant context acceptor reads with unmapped mates.
Returns
-------
self.unmapped_acceptor_mate_ids : list of str
Variant context acceptor read IDs with unmapped mates
"""
return self.unmapped_acceptor_mate_ids
# Returns a list of donor reads overlapping with the variant context.
def get_unmapped_donor_mate_ids(self):
"""Return the IDs of variant context donor reads with unmapped mates.
Returns
-------
self.unmapped_donor_mate_ids : list of str
Variant context donor read IDs with an unmapped mate
"""
return self.unmapped_donor_mate_ids
# =============================================================================
# def get_priority_label(self):
# """Return the priority label of the variant context.
#
# Returns
# -------
# self.priority_label : str
# The priority label associated with the variant context
# """
# return self.priority_label
#
# def get_priority_level(self):
# """Return the priority level of the variant context.
#
# Returns
# -------
# self.priority_level : int
# Priority level of the variant context
# """
# return self.priority_level
#
# def set_priority_label(self, prlabel):
# """Set the priority label for the variant context.
#
# Parameters
# ----------
# prlabel : str
# Priority label to set for the variant context
# """
# self.priority_label = prlabel
#
# def set_priority_level(self, prlevel):
# """Set the priority level for the variant context.
#
# Parameters
# ----------
# prlevel : int
# Priority level to set for the variant context
# """
# self.priority_level = prlevel
# =============================================================================
# ===METHODS TO GET CALCULATED DATA OF THE VARIANT CONTEXT=================
def get_variant_context_length(self):
"""Calculate the length of the variant context.
The length if determined by subtracting the leftmost genomic (start)
position from the rightmost genomic (end) position.
Returns
-------
int
Variant context length
"""
return abs(self.variant_context_end - self.variant_context_start)
def get_start_distance_from_origin(self):
"""Calculate the distance from variant context start to variant locus.
Returns
-------
int
Distance between variant context start and variant start positions
"""
return abs(self.variant_context_origin - self.variant_context_start)
def get_end_distance_from_origin(self):
"""Calculate the distance from variant locus to variant context end.
Returns
-------
int
Distance between variant start and variant context end positions
"""
return abs(self.variant_context_end - self.variant_context_origin)
# ===METHODS TO OBTAIN VARIANT CONTEXT ACCEPTOR READ DATA==================
def get_number_of_acceptor_reads(self):
"""Return the number of variant context acceptor reads.
Returns
-------
int
Number of variant context acceptor reads
"""
if self.variant_context_areads is None:
return 0
return len(self.variant_context_areads)
def get_acceptor_read_ids(self):
"""Return the variant context acceptor read IDs.
Returns
-------
list of str or None
Variant context acceptor read IDs, None if there are none
"""
if self.variant_context_areads is None:
return [None]
return list({x.query_name for x in self.variant_context_areads})
def get_acceptor_read_starts(self):
"""Return the variant context acceptor read starting positions.
Returns
-------
list of int or None
Variant context acceptor read leftmost genomic positions,
None if there are no acceptor reads
"""
if self.variant_context_areads is None:
return [None]
return [x.reference_start for x in self.variant_context_areads]
def get_acceptor_read_left_positions(self):
"""Return the leftmost genomic positions of all variant context R1 acceptor reads.
Returns
-------
list of int or None
Variant context R1 acceptor read leftmost genomic positions,
None if there are no acceptor reads
"""
if self.variant_context_areads is None:
return [None]
return [x.reference_start for x in self.variant_context_areads if x.is_read1]
def get_acceptor_read_ends(self):
"""Return the variant context acceptor read rightmost positions.
Returns
-------
list of int or None
Variant context R2 acceptor read rightmost genomic positions,
None if there are no acceptor reads
"""
if self.variant_context_areads is None:
return [None]
return [x.reference_end for x in self.variant_context_areads]
def get_acceptor_read_right_positions(self):
"""Return the rightmost genomic positions for all variant context R2 acceptor reads.
Returns
-------
list of int or None
Variant context
"""
if self.variant_context_areads is None:
return [None]
return [x.reference_end for x in self.variant_context_areads
if x.is_read2]
# ===METHODS TO OBTAIN VARIANT CONTEXT DONOR READ DATA=====================
def get_number_of_donor_reads(self):
"""Return the number of variant context donor reads.
Returns
-------
int
Number of variant context donor reads
"""
return len(self.variant_context_dreads)
def get_donor_read_ids(self):
"""Return the IDs of donor reads overlapping with the variant context.
Returns
-------
list of str
Variant context donor read IDs
"""
return list({x.query_name for x in self.variant_context_dreads})
def get_donor_read_starts(self):
"""Return the list of variant context donor read starting positions.
Returns
-------
list of int
Variant context donor read leftmost genomic positions
"""
return [x.reference_start for x in self.variant_context_dreads]
def get_donor_read_left_positions(self):
"""Return the list of variant context R1 donor read leftmost positions.
Returns
-------
list of int
Variant context R1 donor read leftmost genomic positions
"""
return [x.reference_start for x in self.variant_context_dreads
if x.is_read1]
# Returns a list of all donor read ending positions.
def get_donor_read_ends(self):
"""Return variant context donor read rightmost positions.
Returns
-------
list of int
Variant context donor read rightmost genomic positions
"""
return [x.reference_end for x in self.variant_context_dreads]
def get_donor_read_right_positions(self):
"""Return the list of variant context R2 donor reads.
Returns
-------
list of int
Variant context R2 donor reads rightmost genomic positions
"""
return [x.reference_end for x in self.variant_context_dreads
if x.is_read2]
# ===METHODS TO ADD DATA TO THE VARIANT CONTEXT============================
def set_acceptor_context(self, acceptor_context):
"""Set the acceptor context from an existing context.
Parameters
----------
acceptor_context : OverlapContext
Acceptor context to add to the variant context
"""
self.variant_acceptor_context = acceptor_context
def set_donor_context(self, donor_context):
"""Set the donor context of the variant context with the one provided.
Parameters
----------
donor_context : OverlapContext
Donor context to add to the variant context
"""
self.variant_donor_context = donor_context
def add_acceptor_context(self, contextid, sampleid,
contextchrom, contextorigin,
contextstart, contextend,
acceptorreads):
"""Construct and set the acceptor context from the provided parameters.
Parameters
----------
contextid : str
Acceptor context identifier
sampleid : str
Sample name/identifier
contextchrom : str
Chromosome name the context is located on
contextorigin : int
Variant position the context is constructed from
contextstart : int
Leftmost genomic position of the acceptor context
contextend : int
Rightmost genomic position of the acceptor context
acceptorreads : list of pysam.AlignedSegment
Acceptor context reads
"""
self.variant_acceptor_context = OverlapContext(
contextid, sampleid,
contextchrom, contextorigin,
contextstart, contextend,
acceptorreads
)
def add_donor_context(self, contextid, sampleid,
contextchrom, contextorigin,
contextstart, contextend,
donorreads):
"""Construct and set the donor context from the provided parameters.
Parameters
----------
contextid : str
Donor context identifier
sampleid : str
Sample name/identifier
contextchrom : str
Chromosome name the context is located on
contextorigin : int
Variant genomic position the context is constructed from
contextstart : int
Leftmost genomic position of the donor context
contextend : int
Rightmost genomic position of the donor context
donorreads : list of pysam.AlignedSegment
Donor context reads
"""
self.variant_donor_context = OverlapContext(
contextid, sampleid,
contextchrom, contextorigin,
contextstart, contextend,
donorreads
)
# ===METHODS TO OBTAIN VARIANT CONTEXT UNMAPPED MATE READ DATA=============
def get_unmapped_acceptor_read_ids(self):
"""Return the read IDs of variant context acceptor reads with unmapped mates.
Returns
-------
self.unmapped_acceptor_mate_ids : list of str
Variant context acceptor read IDs with an unmapped mate
"""
return self.unmapped_acceptor_mate_ids
def get_unmapped_donor_read_ids(self):
"""Return the read IDs of variant context donor reads with unmapped mates.
Returns
-------
self.unmapped_donor_mate_ids : list of str
Variant context donor read IDs with an unmapped mate
:return:
"""
return self.unmapped_donor_mate_ids
def add_unmapped_acceptor_mate_id(self, mateid):
"""Add a variant context acceptor mate ID.
Parameters
----------
mateid : str
Variant context acceptor read ID with an unmapped mate
"""
self.unmapped_acceptor_mate_ids.append(mateid)
def add_unmapped_donor_mate_id(self, mateid):
"""Add a variant context donor mate ID.
Parameters
----------
mateid : str
Variant context donor read ID with an unmapped mate
"""
self.unmapped_donor_mate_ids.append(mateid)
def set_unmapped_acceptor_mate_ids(self, mateids):
"""Set the variant context unmapped acceptor mate ids.
Parameters
----------
mateids : list of str
Variant context acceptor read IDs with unmapped mate
"""
self.unmapped_acceptor_mate_ids = mateids
def set_unmapped_donor_mate_ids(self, mateids):
"""Set the variant context unmapped donor mate ids.
Parameters
----------
mateids : list of str
Variant context donor read IDs with unmapped mates
"""
self.unmapped_donor_mate_ids = mateids
def acceptor_read_has_unmapped_mate(self, readid):
"""Check if a specified variant context acceptor read has an unmapped mate.
Parameters
----------
readid : str
Acceptor read ID
Returns
-------
bool
True if acceptor read has unmapped mate, False if not
"""
return readid in self.unmapped_acceptor_mate_ids
def donor_read_has_unmapped_mate(self, readid):
"""Check if a specified variant context donor read has an unmapped mate.
Parameters
----------
readid : str
Donor read ID
Returns
-------
bool
True if donor read has unmapped mate, False if not
"""
return readid in self.unmapped_donor_mate_ids
def get_number_of_unmapped_acceptor_mates(self):
"""Return the number of variant context acceptor reads with unmapped mates.
Returns
-------
int
Number of variant context acceptor reads with an unmapped mate.
"""
return len(self.unmapped_acceptor_mate_ids)
def get_number_of_unmapped_donor_mates(self):
"""Return the number of variant context donor reads with unmapped mates.
Returns
-------
int
Number of variant context donor reads with an unmapped mate.
"""
return len(self.unmapped_donor_mate_ids)
# ===METHODS TO ADD UNMAPPED MATES TO THE ACCEPTOR AND DONOR CONTEXT=======
def set_acceptor_context_unmapped_mates(self, mateids):
"""Set the unmapped mate ids for the acceptor context.
Returns
-------
mateids : list of str
Acceptor context read IDs with an unmapped mate
"""
self.variant_acceptor_context.set_unmapped_mate_ids(mateids)
def add_acceptor_context_unmapped_mate(self, ureadid):
"""Add an unmapped read id to the acceptor context.
Parameters
----------
ureadid : str
Acceptor context read ID with an unmapped mate
"""
self.variant_acceptor_context.add_unmapped_mate_id(ureadid)
def set_donor_context_unmapped_mates(self, mateids):
"""Set the unmapped mate ids for the donor context.
Parameters
----------
mateids : list of str
Donor context read IDs with an unmapped mate
"""
self.variant_donor_context.set_unmapped_mate_ids(mateids)
def add_donor_context_unmapped_mate(self, ureadid):
"""Add an unmapped read id to the donor context.
Parameters
----------
ureadid : str
Donor context read ID with an unmapped mate
"""
self.variant_donor_context.add_unmapped_mate_id(ureadid)
# ===METHODS TO OBTAIN STATISTICS OF THE VARIANT CONTEXT===================
def get_average_and_median_acceptor_read_qual(self):
"""Calculate the mean and median variant context acceptor read quality.
Returns
-------
qual_stats : list of float or list of None
Average and median read quality
"""
qual_stats = self.get_average_and_median_read_qual(self.variant_context_areads)
return qual_stats
def get_average_and_median_donor_read_qual(self):
"""Calculate the mean and median variant context donor read quality.
Returns
-------
qual_stats : list of float or list of None
Average and median read quality
"""
qual_stats = self.get_average_and_median_read_qual(self.variant_context_dreads)
return qual_stats
@staticmethod
def get_average_and_median_read_qual(contextreads):
"""Calculate the mean and median read quality score.
Parameters
----------
contextreads : list or reads
Reads to calculate mean and median quality score of
Returns
-------
list of float or list of None
Average and median read quality
"""
if contextreads is not None:
avgmedqual = []
for contextread in contextreads:
avgmedqual.append(statistics.mean(list(contextread.query_qualities)))
return ([statistics.mean(avgmedqual),
statistics.median(avgmedqual)])
return [None, None]
def get_average_and_median_acceptor_read_mapq(self):
"""Calculate the mean and median variant context acceptor read MAPQ values.
Returns
-------
list of int
Mean and median MAPQ, None if there are no acceptor reads
"""
return self.get_average_and_median_read_mapq(self.variant_context_areads)
def get_average_and_median_donor_read_mapq(self):
"""Calculate the mean and median variant context donor read MAPQ values.
Returns
-------
list of int
Mean and median variant context donor read MAPQ, None if there are no donor reads
"""
return self.get_average_and_median_read_mapq(self.variant_context_dreads)
@staticmethod
def get_average_and_median_read_mapq(contextreads):
"""Calculate the mean and median MAPQ value of provided reads.
Parameters
----------
contextreads : list of pysam.AlignedSegment
Reads to calculate mean and median MAPQ of
Returns
-------
list of int
Mean and median read MAPQ, None if no reads provided
"""
if contextreads is not None:
avgmedmapq = []
for contextread in contextreads:
avgmedmapq.append(contextread.mapping_quality)
return ([statistics.mean(avgmedmapq),
statistics.median(avgmedmapq)])
return [None, None]
def get_average_and_median_acceptor_read_length(self):
"""Calculate the mean and median variant context acceptor read length.
Returns
-------
list of int
Mean and median variant context acceptor read length, None if
there are no acceptor reads
"""
return self.get_average_and_median_read_length(self.variant_context_areads)
def get_average_and_median_donor_read_length(self):
"""Calculate the mean and median variant context donor read length.
Returns
-------
list of int
Mean and median variant context donor read length, None if there are no donor reads
"""
return self.get_average_and_median_read_length(self.variant_context_dreads)
@staticmethod
def get_average_and_median_read_length(contextreads):
"""Calculate the mean and median read length of a specified list of reads.
Parameters
----------
contextreads : list of pysam.AlignedSegment
Reads to to calculate mean and median length of
Returns
-------
list of int
Mean and median read length, None if no reads are supplied
"""
if contextreads is not None:
avgmedlen = []
for contextread in contextreads:
if contextread.reference_length is not None:
avgmedlen.append(contextread.reference_length)
return [statistics.mean(avgmedlen), statistics.median(avgmedlen)]
return [None, None]
# ===METHODS TO OBTAIN ACCEPTOR CONTEXT DATA===============================
def has_acceptor_context(self):
"""Return whether the variant context has an acceptor context.
Returns
-------
bool
True if variant context has an acceptor context, False if not
"""
return self.variant_acceptor_context is not None
def get_acceptor_context_id(self):
"""Return the acceptor context identifier.
Returns
-------
str
Acceptor context identifier
"""
return self.variant_acceptor_context.get_context_id()
def get_acceptor_context_sample_id(self):
"""Return the acceptor context sample id.
Returns
-------
str
Sample name/identifier of the acceptor context
"""
return self.variant_acceptor_context.get_sample_id()
def get_acceptor_context_chrom(self):
"""Return the chromosome name of the acceptor context.
Returns
-------
str
Chromosome name the acceptor context is located on
"""
return self.variant_acceptor_context.get_context_chrom()
def get_acceptor_context_origin(self):
"""Return the origin position of the acceptor context.
Returns
-------
int
Variant genomic position the context is based on
"""
return self.variant_acceptor_context.get_context_origin()
def get_acceptor_context_start(self):
"""Return the starting position of the acceptor context.
Returns
-------
int
Acceptor context leftmost genomic position
"""
return self.variant_acceptor_context.get_context_start()
def get_acceptor_context_end(self):
"""Return the ending position of the acceptor context.
Returns
-------
int
Acceptor context rightmost genomic position
"""
return self.variant_acceptor_context.get_context_end()
def get_acceptor_context_length(self):
"""Return the length of the acceptor context.
Returns
-------
int
Acceptor context length
"""
return self.variant_acceptor_context.get_context_length()
def get_acceptor_context_reads(self):
"""Return the acceptor context reads.
Returns
-------
list of pysam.AlignedSegment
Acceptor context reads
"""
return self.variant_acceptor_context.get_context_bam_reads()
def get_acceptor_context_read_ids(self):
"""Return the acceptor context read IDs.
Returns
-------
list of str
Acceptor context read IDs
"""
return self.variant_acceptor_context.get_context_read_ids()
def get_acceptor_context_read_starts(self):
"""Return the leftmost genomic positions of the acceptor context reads.
Returns
-------
list of int
List of read start positions
"""
return self.variant_acceptor_context.get_context_read_starts()
def get_acceptor_context_read_left_positions(self):
"""Return the leftmost genomic positions of all R1 acceptor context reads.
Returns
-------
list of int
Acceptor context leftmost genomic R1 read positions
"""
return self.variant_acceptor_context.get_context_read_left_positions()
def get_acceptor_context_read_ends(self):
"""Return the rightmost genomic positions of all acceptor context reads.
Returns
-------
list of int
Acceptor context rightmost genomic read positions.
"""
return self.variant_acceptor_context.get_context_read_ends()
def get_acceptor_context_read_right_positions(self):
"""Return a list of all acceptor context R2 BAM read end positions.
Returns
-------
list of int
Acceptor context rightmost genomic R2 read positions
"""
return self.variant_acceptor_context.get_context_read_right_positions()
def get_acceptor_context_read_lengths(self):
"""Return the lengths of the acceptor context reads.
Returns
-------
list of int
Acceptor context read lengths
"""
return self.variant_acceptor_context.get_context_read_lengths()
def get_acceptor_context_unmapped_mate_ids(self):
"""Return the acceptor context read IDs with unmapped mates.
Returns