forked from molgenis/VaSeBuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
variant_context_file.py
1738 lines (1507 loc) · 66.5 KB
/
variant_context_file.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
"""VariantContextFile object class.
The VariantContextFile class stores VariantContext objects and is used to
write variants contexts to an external file. This class also defines
methods accessing data from multiple VariantContexts.
"""
import logging
import statistics as stats
import sys
from variant_context import VariantContext
from overlap_context import OverlapContext
from read_id_object import ReadIdObject
from inclusion_filter import InclusionVariant
class VariantContextFile:
"""The VariantContextFile saves variant contexts.
Attributes
----------
vaselogger
The logger displaying information of activities
variant_context_file_location : str
THe location of a read variant context file
variant_contexts : dict
Saves variant contexts by context identifier
variant_context_statistics
varcon_fields : dict
Number representation of each variant context data field
"""
def __init__(self, fileloc=None, samplefilter=None,
varconfilter=None, chromfilter=None):
self.vaselogger = logging.getLogger("VaSe_Logger")
self.variant_context_file_location = fileloc
self.variant_contexts = {}
self.variant_context_statistics = None
self.varcon_fields = {1: "variant context id",
2: "sample id",
3: "chromosome",
4: "origin",
5: "start pos",
6: "end pos",
7: "acceptor context",
8: "donor context",
9: "number of acceptor reads",
10: "number of donor reads",
11: "acceptor/donor ratio",
12: "acceptor read ids",
13: "donor read ids"}
self.template_alignment_file = ""
self.contributing_alignment_files = []
self.contributing_variant_files = []
# Check whether to read a provided variant context file with set optional parameters
if fileloc is not None:
self.read_variant_context_file(fileloc, samplefilter, varconfilter, chromfilter)
# ===METHODS TO GET DATA FROM THE VARIANT CONTEXT FILE=====================
def get_variant_contexts(self, asdict=False):
"""Return all variant contexts.
By default all variant contexts are gathered in a list and returned.
If the asdict parameter is set to True, the variant contexts are
returned as a dictionary.
Returns
-------
list or dict
Variant context as list by default, as dict when parameter is set to True
"""
if asdict:
return self.variant_contexts
return list(self.variant_contexts.values())
def get_variant_contexts_by_sampleid(self):
"""Return all variant contexts as dictionary grouped by sample name.
Returns
-------
dict
"""
varcons = self.get_variant_contexts()
return {x.get_variant_context_sample(): [
y for y in varcons
if y.get_variant_context_sample() == x.get_variant_context_sample()
] for x in varcons}
def get_number_of_contexts(self):
"""Count variant contexts.
Returns
-------
int
Number of variant contexts
"""
return len(self.variant_contexts)
def get_variant_context_ids(self):
"""Return all variant context identifiers.
Returns
-------
list of str
Context identifiers of all variant contexts
"""
return list(self.variant_contexts.keys())
def get_variant_context(self, contextid):
"""Return a variant context using its ID, if the context exists.
Parameters
----------
contextid : str
Identifier of context to obtain
Returns
-------
VariantContext or None
VariantContext if it exists, None if not
"""
if contextid in self.variant_contexts:
return self.variant_contexts[contextid]
return None
def has_variant_context(self, contextid):
"""Check if a variant context ID exists.
Parameters
----------
contextid : str
Context identifier to check
Returns
-------
bool
True if the variant context is present, False if not
"""
return contextid in self.variant_contexts
def get_acceptor_context(self, contextid):
"""Return the acceptor context of the specified variant context.
Parameters
----------
contextid : str
Context identifier of acceptor context to return
Returns
-------
OverlapContext or None
The acceptor context if it exists, None if not
"""
if contextid in self.variant_contexts:
return self.variant_contexts[contextid].get_acceptor_context()
return None
def get_donor_context(self, contextid):
"""Return the donor context of the specified variant context.
Parameters
----------
contextid : str
Context identifier of donor context to return
Returns
-------
OverlapContext or None
The donor context if it exists, None if not
"""
if contextid in self.variant_contexts:
return self.variant_contexts[contextid].get_donor_context()
return None
def get_all_variant_context_acceptor_reads(self):
"""Return all acceptor reads from all variant contexts.
Returns
-------
list of pysam.AlignedSegment
Acceptor reads of all variant contexts
"""
acceptorreads = []
for varcon in self.variant_contexts.values():
acceptorreads.extend(varcon.get_acceptor_reads())
return acceptorreads
def get_all_variant_context_donor_reads(self):
"""Return all donor reads from all variant contexts as string tuples.
Returns
-------
list of tuples
Variant context donor reads
"""
dbrs = []
donorreads = []
for varcon in self.variant_contexts.values():
dbrs.extend(varcon.get_donor_reads())
for dbr in dbrs:
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_all_variant_context_donor_reads_2(self):
"""Return all donor reads from all variant contexts.
Returns
-------
donor_reads : list of pysam.AlignedSegment objects
List of all reads in all variant contexts
"""
donor_reads = []
for varcon in self.variant_contexts.values():
donor_reads.extend(varcon.get_donor_reads())
return donor_reads
def get_all_variant_context_acceptor_read_ids(self):
"""Return all variant context acceptor read IDs.
Returns
-------
acceptorreadids : list of str
Variant context acceptor read IDs
"""
acceptorreadids = []
for varcon in self.variant_contexts.values():
acceptorreadids.extend(varcon.get_acceptor_read_ids())
return acceptorreadids
def get_all_variant_context_donor_read_ids(self):
"""Return all variant context donor read IDs.
Returns
-------
donorreadids : list of str
Variant context donor read IDs
"""
donorreadids = []
for varcon in self.variant_contexts.values():
donorreadids.extend(varcon.get_donor_read_ids())
return donorreadids
def get_all_variant_context_variant_records(self):
"""Return all variant records from all variant contexts, sorted."""
variants = []
for varcon in self.variant_contexts.values():
variants.extend(varcon.variants)
variants.sort(key=lambda x: x.pos)
chr_sort_order = [str(x) for x in range(1, 23)] + ["X", "Y", "MT"]
variants.sort(key=lambda x: chr_sort_order.index(x.chrom))
return variants
def get_variant_context_fields(self):
"""Return the number representations of the variant context data.
Returns
-------
self.varcon_fields : dict
Number representation of each variant context field
"""
return self.varcon_fields
# ===METHODS TO OBTAIN VARIANT CONTEXT DATA================================
def get_variant_context_areads(self, contextid):
"""Return the variant context acceptor reads of a specified variant context.
Parameters
----------
contextid : str
Variant context identifier
Returns
-------
list of pysam.AlignedSegment
Variant context acceptor reads, empty list of context does not exist
"""
if contextid in self.variant_contexts:
return self.variant_contexts[contextid].get_acceptor_reads()
return []
def get_variant_context_dreads(self, contextid):
"""Return the variant context donor reads of a specified variant context.
Parameters
----------
contextid : str
Variant context identifier
Returns
-------
list of pysam.AlignedSegment
Variant context donor reads, empty list if context does not exist
"""
if contextid in self.variant_contexts:
return self.variant_contexts[contextid].get_donor_reads()
return []
def get_acceptor_context_reads(self, contextid):
"""Return the reads of a specified acceptor context.
Parameters
----------
contextid : str
Acceptor context identifier
Returns
-------
list of pysam.AlignedSegment
Acceptor context reads
"""
if contextid in self.variant_contexts:
if self.variant_contexts[contextid].has_acceptor_context():
return self.variant_contexts[contextid].get_acceptor_context_reads()
return []
def get_donor_context_reads(self, contextid):
"""Return the reads of a specified donor context.
Parameters
----------
contextid : str
Donor context identifier
Returns
-------
list of pysam.AlignedSegment
Donor context reads, empty list if context does not exist
"""
if contextid in self.variant_contexts:
if self.variant_contexts[contextid].has_donor_context():
return self.variant_contexts[contextid].get_donor_context_reads()
return []
# ===BASIC VARIANTCONTEXTFILE METHODS======================================
def read_variant_context_file(self, fileloc, samplefilter=None,
idfilter=None, chromfilter=None):
"""Read a provided variant context file and save the data.
Filter can be set for reading the variant context file. The sample
filter can be used to specify which samples to save. Samples not in
the samplefilter will be skipped. Similarly filter for variant
contexts and chromosome names can be set.
Parameters
----------
fileloc : str
Path to variant context file to read
samplefilter : list of str
Sample names/identifiers to include
idfilter : list of str
Variant contexts to include
chromfilter : list of str
Chromosome names to include
"""
try:
with open(fileloc, "r") as vcfile:
varcon_records = vcfile.readlines()
except IOError as ioe:
self.vaselogger.critical(f"Could not read varcon file {ioe.filename}")
sys.exit()
varcon_records = [x.strip().split("\t")
for x in varcon_records if not x.startswith("#")]
for record in varcon_records:
if record[0] in self.variant_contexts:
continue
id_pass = self.passes_filter(record[0], idfilter)
samplepass = self.passes_filter(record[1], samplefilter)
chrompass = self.passes_filter(record[2], chromfilter)
if not (id_pass and samplepass and chrompass):
continue
acceptor_reads = [ReadIdObject(readid) for readid in record[11].split(";")]
donor_reads = [ReadIdObject(readid) for readid in record[12].split(";")]
donor_variants = [InclusionVariant(record[1], *var.split("_"))
for var in record[13].split(";")]
new_varcon = VariantContext(*record[:6], acceptor_reads, donor_reads,
variants=donor_variants)
self.variant_contexts[record[0]] = new_varcon
def read_acceptor_context_file(self, accconfileloc, samplefilter=None,
contextfilter=None, chromfilter=None):
"""Add a provided acceptor context from file to already existing variant contexts.
Parameters
----------
accconfileloc : str
Path to the acceptor contexts file
samplefilter : list of str
Sample names/identifiers to include
contextfilter : list of str
Acceptor contexts to include
chromfilter : list of str
Chromosome names to include
"""
try:
with open(accconfileloc, "r") as accconfile:
filelines = accconfile.readlines()
except IOError:
self.vaselogger.warning(f"Could not read acceptor context file: {accconfileloc}")
filelines = [line.strip().split("\t") for line in filelines
if not line.startswith("#")]
for fileline in filelines:
samplepass = self.passes_filter(fileline[1], samplefilter)
contextpass = self.passes_filter(fileline[0], contextfilter)
chrompass = self.passes_filter(fileline[2], chromfilter)
if not (samplepass and contextpass and chrompass):
continue
if not fileline[0] in self.variant_contexts:
continue
self.variant_contexts[fileline[0]].add_acceptor_context(
fileline[0], fileline[1], fileline[2], int(fileline[3]),
int(fileline[4]), int(fileline[5]), fileline[7].split(";")
)
# Reads a donor context file.
def read_donor_context_file(self, donconfileloc, samplefilter=None,
contextfilter=None, chromfilter=None):
"""Add a provided donor context from file to already existing variant contexts.
Parameters
----------
donconfileloc : str
Path to the donor contexts file
samplefilter : list of str
Sample names/identifiers to include
contextfilter : list of str
Donor contexts to include
chromfilter : list of str
Chromosome names to include
"""
try:
with open(donconfileloc, "r") as donconfile:
filelines = donconfile.readlines()
except IOError:
self.vaselogger.warning(f"Could not read acceptor context file: {donconfileloc}")
filelines = [line.strip().split("\t") for line in filelines
if not line.startswith("#")]
for fileline in filelines:
samplepass = self.passes_filter(fileline[1], samplefilter)
contextpass = self.passes_filter(fileline[0], contextfilter)
chrompass = self.passes_filter(fileline[2], chromfilter)
if not (samplepass and contextpass and chrompass):
continue
if not fileline[0] in self.variant_contexts:
continue
self.variant_contexts[fileline[0]].add_donor_context(
fileline[0], fileline[1], fileline[2], int(fileline[3]),
int(fileline[4]), int(fileline[5]), fileline[7].split(";")
)
@staticmethod
def passes_filter(valtocheck, filterlist):
"""Test if a provided value passes a provided inclusion filter.
Filters are expected to be inclusion filters, denoting a list of values
that should be used/included.
Parameters
----------
valtocheck : str
Value to check against a filter list
filterlist : list of str
Values to filter with
Returns
-------
bool
True if value is in filter, False otherwise
"""
if filterlist is not None:
return valtocheck in filterlist
return True
# ===VARIANT CONTEXT SET OPERATIONS (UNION, INTERSECT, DIFFERENCE)=========
def get_variant_contexts_union(self, other_varcon_file):
"""Return all variant context identifiers from both variant context files.
Parameters
----------
other_varcon_file : VariantContextFile
A VariantContextFile object with VariantContexts
Returns
-------
list of str
All variant context identifiers of both VariantContextFile objects
"""
own_varcon_ids = self.get_variant_context_ids()
other_varcon_ids = other_varcon_file.get_variant_context_ids()
return list(set(own_varcon_ids) | set(other_varcon_ids))
def get_variant_contexts_intersect(self, other_varcon_file):
"""Return variant context identifiers present in both variant context files.
Parameters
----------
other_varcon_file : VariantContextFile
VariantContextFile with VariantContext objects
Returns
-------
list of str
Shared variant context identifiers
"""
own_varcon_ids = self.get_variant_context_ids()
other_varcon_ids = other_varcon_file.get_variant_context_ids()
return list(set(own_varcon_ids) & set(other_varcon_ids))
def get_variant_contexts_difference(self, other_varcon_file):
"""Return the variant context identifiers not present in the other variant context file.
Parameters
----------
other_varcon_file : VariantContextFile
VariantContextFile with variant contexts
Returns
-------
list of str
Variant context identifiers not present in the other variant context file
"""
own_varcon_ids = self.get_variant_context_ids()
other_varcon_ids = other_varcon_file.get_variant_context_ids()
return list(set(own_varcon_ids) - set(other_varcon_ids))
def get_variant_contexts_symmetric_difference(self, other_varcon_file):
"""Return 'xor' of variant contexts between two variant context files.
THe normal difference only returns the context identifiers in A but not
in B. The symmetric difference returns the context identifiers in A but
not in B as well as context identifiers in B but not in A.
Parameters
----------
other_varcon_file : VariantContextFile
Returns
-------
list of str
Variant context read IDs
"""
own_varcon_ids = self.get_variant_context_ids()
other_varcon_ids = other_varcon_file.get_variant_context_ids()
return list(set(own_varcon_ids) ^ set(other_varcon_ids))
# ===METHODS TO OBTAIN VARIANT CONTEXT DATA BASED ON FILTERS===============.
def get_variant_contexts2(self, aslist=False, varconfilter=None,
samplefilter=None, chromfilter=None):
"""Return all variant contexts in the VariantContextFile.
Inclusion filters can be set to subset the returned variant contexts.
Filters include sample names/identifiers, variant context identifiers,
and chromosome names. Variant contexts can be returned as list or
dictionary.
Parameters
----------
aslist : bool
Return variant context in a list
varconfilter : list of str
Contexts to include
samplefilter : list of str
Samples to include
chromfilter : list of str
Returns
-------
list or dict of VariantContext
Returns variant contexts as list if aslist set to True, dict otherwise
"""
if aslist:
return [x for x in self.variant_contexts.values()
if (self.passes_filter(x.get_variant_context_id(),
varconfilter)
and self.passes_filter(x.get_variant_context_sample(),
samplefilter)
and self.passes_filter(x.get_variant_context_chrom(),
chromfilter))]
return {k: v for k, v in self.variant_contexts
if (self.passes_filter(k, varconfilter)
and self.passes_filter(v.get_variant_context_sample(),
samplefilter)
and self.passes_filter(v.get_variant_context_chrom(),
chromfilter))}
# ====METHODS TO ASSESS WHETHER A VARIANT IS IN AN EXISTING CONTEXT========
def variant_is_in_context(self, varianttype, searchchrom,
searchstart, searchstop):
"""Check if a variant is located in an already existing context.
If the variant type is set to 'snp' the method will check and return
whether the SNP overlaps with a variant context. If the variant is set
to 'indel' the method will check and return whether the area from start
to end of the indel overlaps with a variant context.
Parameters
----------
varianttype : str
Type of variant (snp/indel)
searchchrom : str
Chromosome name the variant is located on
searchstart : int
Leftmost genomic search window position
searchstop : int
Rightmost genomic search window position
Returns
-------
VariantContext or None
The variant overlapping with the variant, None if no overlap
"""
if varianttype == "snp":
return self.snp_variant_is_in_context(searchchrom, searchstart)
if varianttype == "indel":
return self.indel_variant_is_in_context(searchchrom, searchstart,
searchstop)
return None
def snp_variant_is_in_context(self, varchrom, vcfvarpos):
"""Check if a SNP is located in an already existing variant context.
For each variant context it is first checked whether the chromosome
name of the context and SNP are the same. If so, it is then checked
whether the SNP genomic position is equal or larger than the context
start and smaller or equal than the context end.
Parameters
----------
varchrom : str
Chromosome name the SNP is located on
vcfvarpos : int
Genomic position of the SNP
Returns
-------
VariantContext or None
Variant context overlapping with the variant, None if no context overlaps
"""
for varcon in self.variant_contexts.values():
if varchrom == varcon.get_variant_context_chrom():
start = varcon.get_variant_context_start()
stop = varcon.get_variant_context_end()
if start <= vcfvarpos <= stop:
return varcon
return None
def indel_variant_is_in_context(self, indelchrom, indelleftpos, indelrightpos):
"""Check if an indel is located in an already existing variant context.
For each variant context, first checks whether the chromosome name of
the context and indel are the same. If so, it is then checked whether
the indel range from indel start to end overlaps with the context.
:Parameters
-----------
indelchrom : str
The chromosome name the indel is located on
indelleftpos : int
The leftmost genomic position of the indel
indelrightpos : int
The rightmost genomic position of the indel
Returns
-------
VariantContext or None
Variant context overlapping with the variant, None if no overlap
"""
for varcon in self.variant_contexts.values():
if indelchrom == varcon.get_variant_context_chrom():
if (indelleftpos <= varcon.get_variant_context_start()
and indelrightpos >= varcon.get_variant_context_start()):
return varcon
if (indelleftpos <= varcon.get_variant_context_end()
and indelrightpos >= varcon.get_variant_context_end()):
return varcon
if (indelleftpos >= varcon.get_variant_context_start()
and indelrightpos <= varcon.get_variant_context_end()):
return varcon
return None
def context_collision(self, context_arr):
"""Check if a potential context overlaps with an existing context.
Parameters
----------
context_arr: list
Essential context data (chrom, variant pos, start, end)
Returns
-------
bool
True if provided context overlaps with an already existing context, False if not
"""
if f"{context_arr[0]}_{context_arr[1]}" in self.variant_contexts:
return True
for varcon in self.variant_contexts.values():
if varcon.get_variant_context_chrom() == context_arr[0]:
if (varcon.get_variant_context_start() <= context_arr[3]
and context_arr[2] <= varcon.get_variant_context_end()):
return True
return False
def context_collision_v2(self, context_arr):
"""Check if a potential context overlaps with an existing context.
Parameters
----------
context_arr: list
Essential context data (chrom, variant pos, start, end)
Returns
-------
VariantContext or None
Variant context in which the overlap occurs, None if there is no overlap
"""
if f"{context_arr[0]}_{context_arr[1]}" in self.variant_contexts:
return self.variant_contexts[f"{context_arr[0]}_{context_arr[1]}"]
for varcon in self.variant_contexts.values():
if varcon.get_variant_context_chrom() == context_arr[0]:
if (varcon.get_variant_context_start() <= context_arr[3]
and context_arr[2] <= varcon.get_variant_context_end()):
return varcon
return None
# ===METHODS TO ADD DATA/VARIANT CONTEXTS TO THE VARIANT CONTEXT FILE======
def set_variant_context(self, varconid, varcontext):
"""Set a provided variant context with the provided context identifier.
Will overwrite the previous value for the provided context identifier.
Parameters
----------
varconid : str
Identifier of the variant context to add
varcontext: VariantContext
The VariantContext to set
"""
self.variant_contexts[varconid] = varcontext
def set_variant_context_donor_reads(self, varconid, donor_reads):
"""Set the variant context donor reads for a specified variant context.
Parameters
----------
varconid : str
Variant context identifier to set donor reads for
donor_reads : list of DonorBamRead
List of donor reads to set
"""
if varconid in self.variant_contexts:
self.variant_contexts[varconid].set_donor_reads(donor_reads)
def add_variant_context(self, varconid, sampleid,
varconchrom, varconorigin,
varconstart, varconend,
varcon_areads, varcon_dreads,
acceptor_context=None, donor_context=None):
"""Construct and add a VariantContext from the provided parameters.
Parameters
----------
varconid : str
Identifier of the variant context
sampleid : str
Identifier of the sample
varconchrom : str
Chromosome name of the variant context
varconorigin : int
The variant position the context is based on
varconstart : int
Leftmost genomic position of the variant context
varconend : int
Rightmost genomic position of the variant context
varcon_areads : list of DonorBamRead
Variant context acceptor reads
varcon_dreads : list of DonorBamRead
Variant context donor reads
acceptor_context : OverlapContext
Acceptor context belonging to the variant context
donor_context : OverlapContext
Donor context belonging to the variant context
"""
varcon_obj = VariantContext(varconid, sampleid,
varconchrom, varconorigin,
varconstart, varconend,
varcon_areads, varcon_dreads,
acceptor_context, donor_context)
self.variant_contexts[varconid] = varcon_obj
def add_existing_variant_context(self, varconid, varconobj):
"""Add an already created variant context to the variant context file.
The variant context to add should be a valid VariantContext object.
It will be added to the variant context file under the context
identifier.
Parameters
----------
varconid : str
Identifier of the context
varconobj : VariantContext
The created variant context
"""
if varconobj is not None:
self.variant_contexts[varconid] = varconobj
def set_acceptor_context(self, varconid, acceptor_context):
"""Add an existing acceptor context to a variant context.
Parameters
----------
varconid : str
Context identifier
acceptor_context : OverlapContext
The acceptor context to add
"""
if varconid in self.variant_contexts:
self.variant_contexts[varconid].set_acceptor_context(acceptor_context)
def add_acceptor_context(self, contextid, sampleid,
contextchrom, contextorigin,
contextstart, contextend,
acceptorreads):
"""Construct and add an acceptor context to a specified variant context.
Parameters
----------
contextid : str
Context identifier
sampleid: str
Sample name/identifier
contextchrom: str
Chromosome name the context is located on
contextorigin: int
Variant genomic position the context is based on
contextstart: int
Leftmost genomic position of the context
contextend: int
Rightmost genomic position of the context
acceptorreads: list of DonorBamRead
List of reads associated with the context
"""
if contextid in self.variant_contexts:
self.variant_contexts[contextid].add_acceptor_context(
contextid, sampleid,
contextchrom, contextorigin,
contextstart, contextend,
acceptorreads
)
def set_donor_context(self, varconid, donor_context):
"""Add an already existing donor context to a specified variant context.
Parameters
----------
varconid : str
Context identifier
donor_context : OverlapContext
Donor context to add
"""
if varconid in self.variant_contexts:
self.variant_contexts[varconid].set_donor_context(donor_context)
def add_donor_context(self, contextid, sampleid,
contextchrom, contextorigin,
contextstart, contextend,
donorreads):
"""Construct and add a donor context to a specified variant context.
Parameters
----------
contextid : str
Variant context identifier
sampleid : str
Sample name/identifier
contextchrom : str
Chromosome name the context is located on
contextorigin : int
Variant position that constructed the context
contextstart : int
Donor context leftmost genomic position
contextend : int
Donor context rightmost genomic position
donorreads : list of DonorBamRead
Donor context reads
"""
if contextid in self.variant_contexts:
self.variant_contexts[contextid].add_donor_context(
contextid, sampleid,
contextchrom, contextorigin,
contextstart, contextend,
donorreads
)
# ===METHODS TO ADD UNMAPPED MATE IDS TO ACCEPTOR, DONOR AND VARIANT CONTEXT=======
def set_acceptor_context_unmapped_mate_ids(self, contextid, mateids):
"""Set the read IDs that have unmapped read mates for a specified acceptor context.
Parameters
----------
contextid : str
Acceptor context to set unmapped read IDs for
mateids : lit of str
read IDs with unmapped mates
"""
if contextid in self.variant_contexts:
self.variant_contexts[contextid].set_acceptor_context_unmapped_mates(mateids)
def set_donor_context_unmapped_mate_ids(self, contextid, mateids):
"""Set the read IDs that have unmapped read mates for a specified donor context.
Parameters
----------
contextid : str
Donor context to set unmapped read IDs for
mateids : list of str
Donor context read IDs with unmapped mates
"""
if contextid in self.variant_contexts:
self.variant_contexts[contextid].set_donor_context_unmapped_mates(mateids)
def set_unmapped_acceptor_mate_ids(self, contextid, mateids):
"""Set the acceptor read IDs that have unmapped mates for a specified variant context.
Parameters
----------
contextid : str
Variant context identifier to set unmapped acceptor read identifiers for
mateids : list of str
Variant context acceptor read IDs with unmapped mates
"""
if contextid in self.variant_contexts:
self.variant_contexts[contextid].set_unmapped_acceptor_mate_ids(mateids)
def set_unmapped_donor_mate_ids(self, contextid, mateids):
"""Set the donor read IDs that have unmapped mates for a specified variant context.
Parameters
----------
contextid : str
Variant context identifier to set unmapped donor read IDs for
mateids: list of str
Variant context donor read IDs with unmapped mates
"""
if contextid in self.variant_contexts:
self.variant_contexts[contextid].set_unmapped_donor_mate_ids(mateids)
# ===METHODS TO GET UNMAPPED MATE IDS OF AN ACCEPTOR, DONOR AND VARIANT CONTEXT============
def get_acceptor_context_unmapped_mate_ids(self, contextid):
"""Return the acceptor context read IDs that have unmapped read mates.
Parameters
----------
contextid : str
Acceptor context identifier
Returns
-------
list of str
Acceptor context read IDs, empty list if context does not exist
"""
if contextid in self.variant_contexts:
return self.variant_contexts[contextid].get_acceptor_context_unmapped_mate_ids()
return []
def get_donor_context_unmapped_mate_ids(self, contextid):
"""Return the donor context read IDs that have unmapped read mates.
Parameters
----------
contextid : str
Donor context identifier
Returns
-------
list of str
Donor contexts read IDs, empty list if context does not exist
"""
if contextid in self.variant_contexts:
return self.variant_contexts[contextid].get_donor_context_unmapped_mate_ids()
return []
def get_unmapped_acceptor_mate_ids(self, contextid):
"""Return the variant context acceptor read IDs that have unmapped read mates.