-
Notifications
You must be signed in to change notification settings - Fork 31
/
IlluminaBeadArrayFiles.py
1059 lines (939 loc) · 38.6 KB
/
IlluminaBeadArrayFiles.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
## Copyright (c) 2016, Illumina
## All rights reserved.
##
## Redistribution and use in source and binary forms, with or without
## modification, are permitted provided that the following conditions are met:
##
## 1. Redistributions of source code must retain the above copyright notice, this
## list of conditions and the following disclaimer.
## 2. Redistributions in binary form must reproduce the above copyright notice,
## this list of conditions and the following disclaimer in the documentation
## and/or other materials provided with the distribution.
##
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
## ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
## WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
## DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
## ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
## (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
## LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
## SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
##
## The views and conclusions contained in the software and documentation are those
## of the authors and should not be interpreted as representing official policies,
## either expressed or implied, of the FreeBSD Project.
__version__ = "1.2.0"
import struct
from numpy import cos, sin, pi, arctan2, float32, uint16, int32, seterr, frombuffer, dtype
seterr(divide='ignore', invalid='ignore')
nan = float32('nan')
code2genotype = [
"NC",
"AA",
"AB",
"BB",
"NULL",
"A",
"B",
"AAA",
"AAB",
"ABB",
"BBB",
"AAAA",
"AAAB",
"AABB",
"ABBB",
"BBBB",
"AAAAA",
"AAAAB",
"AAABB",
"AABBB",
"ABBBB",
"BBBBB",
"AAAAAA",
"AAAAAB",
"AAAABB",
"AAABBB",
"AABBBB",
"ABBBBB",
"BBBBBB",
"AAAAAAA",
"AAAAAAB",
"AAAAABB",
"AAAABBB",
"AAABBBB",
"AABBBBB",
"ABBBBBB",
"BBBBBBB",
"AAAAAAAA",
"AAAAAAAB",
"AAAAAABB",
"AAAAABBB",
"AAAABBBB",
"AAABBBBB",
"AABBBBBB",
"ABBBBBBB",
"BBBBBBBB"
]
NC = chr(0)
AA = chr(1)
AB = chr(2)
BB = chr(3)
class GenotypeCalls:
"""Class to parse gtc files as produced by Illumina AutoConvert
and AutoCall software.
Attributes:
supported_versions: Supported file versions as a list of integers
"""
__ID_NUM_SNPS = 1
__ID_PLOIDY = 2
__ID_PLOIDY_TYPE = 3
__ID_SAMPLE_NAME = 10
__ID_SAMPLE_PLATE = 11
__ID_SAMPLE_WELL = 12
__ID_CLUSTER_FILE = 100
__ID_SNP_MANIFEST = 101
__ID_IMAGING_DATE = 200
__ID_AUTOCALL_DATE = 201
__ID_AUTOCALL_VERSION = 300
__ID_NORMALIZATION_TRANSFORMS = 400
__ID_CONTROLS_X = 500
__ID_CONTROLS_Y = 501
__ID_RAW_X = 1000
__ID_RAW_Y = 1001
__ID_GENOTYPES = 1002
__ID_BASE_CALLS = 1003
__ID_GENOTYPE_SCORES = 1004
__ID_SCANNER_DATA = 1005
__ID_CALL_RATE = 1006
__ID_GENDER = 1007
__ID_LOGR_DEV = 1008
__ID_GC10 = 1009
__ID_GC50 = 1011
__ID_B_ALLELE_FREQS = 1012
__ID_LOGR_RATIOS = 1013
__ID_PERCENTILES_X = 1014
__ID_PERCENTILES_Y = 1015
__ID_SLIDE_IDENTIFIER = 1016
supported_version = [3,4,5];
def __init__(self, filename, ignore_version = False, check_write_complete = True):
"""Constructor
Args:
filename (string): GTC filename
ignore_version (bool): boolean to ignore automated checks on
file version, not recommended (default: False)
Returns:
GenotypeCalls object
"""
self.filename = filename
with open(self.filename, "rb") as gtc_handle:
identifier = gtc_handle.read(3)
if identifier != b'gtc':
raise Exception("GTC format error: bad format identifier")
self.version = read_byte(gtc_handle)
if self.version not in GenotypeCalls.supported_version and not ignore_version:
raise Exception("Unsupported GTC File version ("+str(self.version)+")")
number_toc_entries = read_int(gtc_handle)
#
# Parse the table of contents and map the toc entry
# to the lookup
#
self.toc_table = {}
for toc_idx in range(number_toc_entries):
(id, offset) = struct.unpack("<hI",gtc_handle.read(6))
self.toc_table[id] = offset
if check_write_complete and not self.is_write_complete():
raise Exception("GTC file is incomplete")
def __get_generic(self, toc_entry, parse_function):
"""Internal helper function to access a data element in a
generic fashion.
Args:
toc_entry (int): Identifier for entry in table of contents
parse_function (function): Used to parse the value
from a file handle
Returns:
A single value parsed from the file (type dependent on
parse_function)
"""
with open(self.filename, "rb") as gtc_handle:
gtc_handle.seek(self.toc_table[toc_entry])
return parse_function(gtc_handle)
def __get_generic_array(self, toc_entry, parse_function):
"""Internal helper function to access a data array in a generic
fashion.
Args:
toc_entry (int): Identifier for entry in table of contents
parse_function (function): A function used to parse the value
from a file handle
Returns:
An array parsed from the file (type dependent on parse_function)
"""
with open(self.filename, "rb") as gtc_handle:
gtc_handle.seek(self.toc_table[toc_entry])
num_entries = read_int(gtc_handle)
result = []
for idx in range(num_entries):
result.append( parse_function( gtc_handle ) )
return result
def __get_generic_array_numpy(self, toc_entry, numpy_type):
"""Internal helper function to access a data array in a generic
fashion.
Args:
toc_entry (int): Identifier for entry in table of contents
parse_function (function): A function used to parse the value
from a file handle
Returns:
An array parsed from the file (type dependent on parse_function)
"""
numpy_type = dtype(numpy_type)
with open(self.filename, "rb") as gtc_handle:
gtc_handle.seek(self.toc_table[toc_entry])
num_entries = read_int(gtc_handle)
return frombuffer(gtc_handle.read(num_entries*numpy_type.itemsize), dtype=numpy_type)
def get_num_snps(self):
"""Returns:
The number of SNPs in the file as an integer
"""
return self.toc_table[GenotypeCalls.__ID_NUM_SNPS]
def get_ploidy(self):
"""Returns:
The ploidy of the sample
"""
return self.toc_table[GenotypeCalls.__ID_PLOIDY]
def get_ploidy_type(self):
"""Returns:
The ploidy type of the sample
"""
return self.toc_table[GenotypeCalls.__ID_PLOIDY_TYPE]
def get_sample_name(self):
"""Returns:
The name of the sample as a string
"""
return self.__get_generic(GenotypeCalls.__ID_SAMPLE_NAME, read_string)
def get_slide_identifier(self):
"""Returns:
The name of the sample as a string
"""
return self.__get_generic(GenotypeCalls.__ID_SLIDE_IDENTIFIER, read_string)
def get_sample_plate(self):
"""Returns:
The name of the sample plate as a string
"""
return self.__get_generic(GenotypeCalls.__ID_SAMPLE_PLATE, read_string)
def get_sample_well(self):
"""Returns:
The name of the sample well as a string
"""
return self.__get_generic(GenotypeCalls.__ID_SAMPLE_WELL, read_string)
def get_cluster_file(self):
"""Returns:
The name of the cluster file used for genotyping as a string
"""
return self.__get_generic(GenotypeCalls.__ID_CLUSTER_FILE, read_string)
def get_snp_manifest(self):
"""Returns:
The name of the manifest used for genotyping as a string
"""
return self.__get_generic(GenotypeCalls.__ID_SNP_MANIFEST, read_string)
def get_imaging_date(self):
"""Returns:
The imaging date of scanning as a string
For example
Monday, December 01, 2014 4:51:47 PM
"""
return self.__get_generic(GenotypeCalls.__ID_IMAGING_DATE, read_string)
def get_autocall_date(self):
"""Returns:
The imaging date of scanning as a string
For example
2/17/2015 1:47 PM
"""
return self.__get_generic(GenotypeCalls.__ID_AUTOCALL_DATE, read_string)
def get_autocall_version(self):
"""Returns:
The version of AutoCall used for genotyping as a string
For example
1.6.2.2
"""
return self.__get_generic(GenotypeCalls.__ID_AUTOCALL_VERSION, read_string)
def get_genotypes(self):
""" Returns:
A byte list (string) of genotypes. See code2genotype for mapping
"""
return self.__get_generic_array(GenotypeCalls.__ID_GENOTYPES, read_byte)
def get_base_calls_generic(self, snps, strand_annotations, report_strand, unknown_annotation):
"""
Get base calls on arbitrary strand
Args:
snps (list<string>) : A list of string representing the snp on the design strand for the loci (e.g. [A/C])
strand_annotations (list<int>) : A list of strand annotations for the loci
report_strand (int) : The strand to use for reporting (must match encoding of strand_annotations)
unknown_annotation (int) : The encoding used in strand annotations for an unknown strand
Returns:
The genotype basecalls on the report strand as a list of strings.
The characters are A, C, G, T, or - for a no-call/null.
"""
genotypes = self.get_genotypes()
if len(genotypes) != len(snps):
raise ValueError("The number of SNPs must match the number of loci in the GTC file")
if len(genotypes) != len(strand_annotations):
raise ValueError("The number of reference strand annotations must match the number of loci in the GTC file")
result = []
for (genotype, snp, strand_annotation) in zip(genotypes, snps, strand_annotations):
ab_genotype = code2genotype[genotype]
a_nucleotide = snp[1]
b_nucleotide = snp[-2]
if a_nucleotide == "N" or b_nucleotide == "N" or strand_annotation == unknown_annotation or ab_genotype == "NC" or ab_genotype == "NULL":
result.append("-")
else:
report_strand_nucleotides = []
for ab_allele in ab_genotype:
nucleotide_allele = a_nucleotide if ab_allele == "A" else b_nucleotide
report_strand_nucleotides.append(nucleotide_allele if strand_annotation == report_strand else complement(nucleotide_allele))
result.append("".join(report_strand_nucleotides))
return result
def get_base_calls_plus_strand(self, snps, ref_strand_annotations):
"""
Get base calls on plus strand of genomic reference. If you only see no-calls returned from this method,
please verify that the reference strand annotations passed as argument are not unknown (RefStrand.Unknown)
Args:
snps (list<string>) : A list of string representing the snp on the design strand for the loci (e.g. [A/C])
ref_strand_annotations (list<int>) : A list of strand annotations for the loci (e.g., RefStrand.Plus)
Returns:
The genotype basecalls on the report strand as a list of strings.
The characters are A, C, G, T, or - for a no-call/null.
"""
return self.get_base_calls_generic(snps, ref_strand_annotations, RefStrand.Plus, RefStrand.Unknown)
def get_base_calls_forward_strand(self, snps, forward_strand_annotations):
"""
Get base calls on the forward strand.
Args:
snps (list<string>) : A list of string representing the snp on the design strand for the loci (e.g. [A/C])
forward_strand_annotations (list<int>) : A list of strand annotations for the loci (e.g., SourceStrand.Forward)
Returns:
The genotype basecalls on the report strand as a list of strings.
The characters are A, C, G, T, or - for a no-call/null.
"""
return self.get_base_calls_generic(snps, forward_strand_annotations, SourceStrand.Forward, RefStrand.Unknown)
def get_base_calls(self):
"""Returns:
The genotype basecalls as a list of strings.
The characters are A, C, G, T, or - for a no-call/null.
The calls are relative to the top strand.
"""
try:
ploidy_type = self.get_ploidy_type()
except:
ploidy_type = 1
if ploidy_type != 1:
genotypes = self.get_genotypes()
with open(self.filename, "rb") as gtc_handle:
gtc_handle.seek(self.toc_table[GenotypeCalls.__ID_BASE_CALLS])
num_entries = read_int(gtc_handle)
result = []
for idx in range(num_entries):
if ploidy_type == 1:
result.append( gtc_handle.read(2) )
else:
byte_string = gtc_handle.read(2)
ab_genotype = code2genotype[genotypes[idx]]
if ab_genotype == "NC" or ab_genotype == "NULL":
result.append("-")
else:
top_genotype = "".join([ byte_string[0] if allele == "A" else byte_string[1] for allele in ab_genotype])
result.append( top_genotype )
return result
def get_genotype_scores(self):
"""Returns:
The genotype scores as a list of floats
"""
return self.__get_generic_array_numpy(GenotypeCalls.__ID_GENOTYPE_SCORES, float32)
def get_scanner_data(self):
"""Returns:
Information about scanner as ScannerData object
"""
return self.__get_generic(GenotypeCalls.__ID_SCANNER_DATA, read_scanner_data)
def get_control_x_intensities(self):
"""Returns:
The x intensities of control bead types as a list of integers
"""
return self.__get_generic_array_numpy(GenotypeCalls.__ID_CONTROLS_X, uint16)
def get_control_y_intensities(self):
"""Returns:
The y intensities of control bead types as a list of integers
"""
return self.__get_generic_array_numpy(GenotypeCalls.__ID_CONTROLS_Y, uint16)
def get_raw_x_intensities(self):
"""Returns:
The raw x intensities of assay bead types as a list of integers
"""
return self.__get_generic_array_numpy(GenotypeCalls.__ID_RAW_X, uint16)
def get_raw_y_intensities(self):
"""Returns:
The raw y intensities of assay bead types as a list of integers
"""
return self.__get_generic_array_numpy(GenotypeCalls.__ID_RAW_Y, uint16)
def get_call_rate(self):
"""Returns:
The call rate as a float
"""
return self.__get_generic(GenotypeCalls.__ID_CALL_RATE, read_float)
def get_gender(self):
"""Returns:
The gender as a char
M - Male, F - Female, U-Unknown
"""
return self.__get_generic(GenotypeCalls.__ID_GENDER, read_char)
def get_logr_dev(self):
"""Returns:
The logR deviation as a float
"""
return self.__get_generic(GenotypeCalls.__ID_LOGR_DEV, read_float)
def get_gc10(self):
"""Returns:
The GC10 (GenCall score - 10th percentile) as a float
"""
return self.__get_generic(GenotypeCalls.__ID_GC10, read_float)
def get_gc50(self):
"""Returns:
The GC50 (GenCall score - 50th percentile) as a float
"""
return self.__get_generic(GenotypeCalls.__ID_GC50, read_float)
def get_num_calls(self):
"""Returns:
The number of calls as an integer
"""
with open(self.filename, "rb") as gtc_handle:
gtc_handle.seek(self.toc_table[GenotypeCalls.__ID_GC50] + 4)
return read_int(gtc_handle)
def get_num_no_calls(self):
"""Returns:
The number of no calls as an integer
"""
with open(self.filename, "rb") as gtc_handle:
gtc_handle.seek(self.toc_table[GenotypeCalls.__ID_GC50] + 8)
return read_int(gtc_handle)
def get_num_intensity_only(self):
"""Returns:
The number of intensity only SNPs
"""
with open(self.filename, "rb") as gtc_handle:
gtc_handle.seek(self.toc_table[GenotypeCalls.__ID_GC50] + 12)
return read_int(gtc_handle)
def get_ballele_freqs(self):
"""Returns:
The B allele frequencies as a list of floats
"""
if self.version < 4:
raise Exception("B allele frequencies unavailable in GTC File version ("+str(self.version)+")")
return self.__get_generic_array_numpy(GenotypeCalls.__ID_B_ALLELE_FREQS, float32)
def get_logr_ratios(self):
"""Returns:
The logR ratios as a list of floats
"""
if self.version < 4:
raise Exception("LogR ratios unavailable in GTC File version ("+str(self.version)+")")
return self.__get_generic_array_numpy(GenotypeCalls.__ID_LOGR_RATIOS, float32)
def get_percentiles_x(self):
"""Returns:
An array of length three representing 5th, 50th and 95th percentiles for
x intensity
"""
if self.version < 5:
raise Exception("Percentile intensities unavailable in GTC File version ("+str(self.version)+")")
result = []
with open(self.filename, "rb") as gtc_handle:
gtc_handle.seek(self.toc_table[GenotypeCalls.__ID_PERCENTILES_X])
result = []
for idx in range(3):
result.append(read_ushort(gtc_handle))
return result
def get_percentiles_y(self):
"""Returns:
An array of length three representing 5th, 50th and 95th percentiles for
y intensity
"""
if self.version < 5:
raise Exception("Percentile intensities unavailable in GTC File version ("+str(self.version)+")")
result = []
with open(self.filename, "rb") as gtc_handle:
gtc_handle.seek(self.toc_table[GenotypeCalls.__ID_PERCENTILES_Y])
result = []
for idx in range(3):
result.append(read_ushort(gtc_handle))
return result
def get_normalized_intensities(self, normalization_lookups):
"""Calculate and return the normalized intensities
Args:
normalization_lookups (list of ints): Map from each SNP to a normalization transform.
This list can be obtained from the BeadPoolManifest object.
Return:
The normalized intensities for the sample as a list of (x,y) float tuples
"""
normalization_transforms = self.get_normalization_transforms()
return [normalization_transforms[lookup].normalize_intensities(x_raw, y_raw) for (x_raw, y_raw, lookup) in zip(self.get_raw_x_intensities(), self.get_raw_y_intensities(), normalization_lookups)]
def get_normalization_transforms(self):
"""Returns:
The normalization transforms used during genotyping (as a lit of NormalizationTransforms)
"""
return self.__get_generic_array(GenotypeCalls.__ID_NORMALIZATION_TRANSFORMS, NormalizationTransform.read_normalization_transform)
def is_write_complete(self):
"""Check for last item written to GTC file to verify that write
has successfully completed
Args:
None
Returns
Whether or not write is complete (bool)
"""
if self.version == 3:
try:
self.get_num_intensity_only()
return True
except:
return False
elif self.version == 4:
try:
self.get_logr_ratios()
return True
except:
return False
elif self.version == 5:
try:
self.get_slide_identifier()
return True
except:
return False
else:
raise Exception("Unable to test for write completion on version " + str(self.version) + " GTC file")
class NormalizationTransform:
def __init__(self, buffer):
"""Constructor
Args:
buffer (string): byte string containing binary data for transform
Returns:
NormalizationTransform object
"""
(self.version, ) = struct.unpack("<i", buffer[:4])
(self.offset_x, self.offset_y, self.scale_x, self.scale_y, self.shear, self.theta,) = frombuffer(buffer[4:28], dtype=float32)
@staticmethod
def read_normalization_transform(handle):
"""Static helper function to read normalization transform from file handle
Args:
handle (file handle): File handle with position at start of normalization transform entry
Returns:
NormalizationTransform object
"""
return NormalizationTransform(handle.read(52))
@staticmethod
def rect_to_polar(x_y):
"""Converts normalized x,y intensities to (pseudo) polar co-ordinates (R, theta)
Args:
x_y (float, float): Normalized x,y intensities for probe
Returns:
(R,theta) polar values as tuple of floats
"""
x,y = x_y
if x == 0 and y == 0:
return (nan, nan)
return (x + y, arctan2(y,x) * 2.0 / pi)
def normalize_intensities(self, x, y, threshold = True):
"""Apply this normalization transform to raw intensities
Args:
x (int): Raw x intensities
y (int): Raw y intensities
Returns:
(xn, yn) normalized intensities as tuple of floats
"""
if x <= 0 and y <= 0:
return (nan, nan)
tempx = x - self.offset_x
tempy = y - self.offset_y
tempx2 = cos(self.theta) * tempx + sin(self.theta) * tempy
tempy2 = -sin(self.theta) * tempx + cos(self.theta) * tempy
tempx3 = tempx2 - self.shear * tempy2
tempy3 = tempy2
xn = tempx3 / self.scale_x
yn = tempy3 / self.scale_y
if threshold:
xn = 0 if 0 > xn else xn
yn = 0 if 0 > yn else yn
return (xn, yn)
class ScannerData:
def __init__(self, name, pmt_green, pmt_red, version, user):
"""Constructor
Args:
name (string): scanner identifier
pmt_green (int): gain setting (green channel)
pmt_red (int): gain setting (red channel)
version (string): version of scanner software
user (string): user of the scanner software
Returns:
ScannerData object
"""
self.name = name
self.pmt_green = pmt_green
self.pmt_red = pmt_red
self.version = version
self.user = user
class BeadPoolManifest:
"""Class for parsing binary (BPM) manifest file.
Attributes:
names (list of strings): Names of loci from manifest
snps (list of strings) : SNP values of loci from manifest
chroms (list of string) : Chromosome values for loci
map_infos = (list of ints) : Map infor values for loci
addresses (list of ints): AddressA IDs of loci from manifest
normalization_lookups (list of ints): Normalization lookups from manifest. This indexes into
list of normalization transforms read from GTC file
ref_strands (list of ints) : Reference strand annotation for loci (see RefStrand class)
source_strands (list of ints) : Source strand annotations for loci (see SourceStrand class)
num_loci (int): Number of loci in manifest
manifest_name (string): Name of manifest
control_config (string): Control description from manifest
"""
def __init__(self, filename):
"""Constructor
Args:
filename (string): Locations of BPM (bead pool manifest) file
Returns:
BeadPoolManifest
"""
self.names = []
self.snps = []
self.chroms = []
self.map_infos = []
self.addresses = []
self.normalization_ids = []
self.assay_types = []
self.normalization_lookups = []
self.ref_strands = []
self.source_strands = []
self.num_loci = 0
self.manifest_name = ""
self.control_config = ""
self.__parse_file(filename)
def __parse_file(self, manifest_file):
"""Helper function to initialize this object from a file.
Args:
manifest_file (string): Location of BPM (bead pool manifest) file
Returns:
None
"""
with open(manifest_file, "rb") as manifest_handle:
header = manifest_handle.read(3)
if len(header) != 3 or header != b"BPM":
raise Exception("Invalid BPM format")
version = read_byte(manifest_handle)
if version != 1:
raise Exception("Unknown BPM version (" + str(ord(version)) + ")")
version = read_int(manifest_handle)
version_flag = 0x1000
if version & version_flag == version_flag:
version = version ^ version_flag
if version > 5 or version < 3:
raise Exception("Unsupported BPM version (" + str(version) + ")")
self.manifest_name = read_string(manifest_handle)
if version > 1:
self.control_config = read_string(manifest_handle)
self.num_loci = read_int(manifest_handle)
manifest_handle.seek(4 * self.num_loci, 1)
name_lookup = {}
for idx in range(self.num_loci):
self.names.append(read_string(manifest_handle))
name_lookup[self.names[-1]] = idx
for idx in range(self.num_loci):
normalization_id = read_byte(manifest_handle)
if normalization_id >= 100:
raise Exception("Manifest format error: read invalid normalization ID")
self.normalization_ids.append(normalization_id)
self.assay_types = [0] * self.num_loci
self.addresses = [0] * self.num_loci
self.snps = [""] * self.num_loci
self.chroms = [""] * self.num_loci
self.map_infos = [0] * self.num_loci
self.ref_strands = [RefStrand.Unknown] * self.num_loci
self.source_strands = [SourceStrand.Unknown] * self.num_loci
for idx in range(self.num_loci):
locus_entry = LocusEntry(manifest_handle)
self.assay_types[name_lookup[locus_entry.name]] = locus_entry.assay_type
self.addresses[name_lookup[locus_entry.name]] = locus_entry.address_a
self.snps[name_lookup[locus_entry.name]] = locus_entry.snp
self.chroms[name_lookup[locus_entry.name]] = locus_entry.chrom
self.map_infos[name_lookup[locus_entry.name]] = locus_entry.map_info
self.ref_strands[name_lookup[locus_entry.name]] = locus_entry.ref_strand
self.source_strands[name_lookup[locus_entry.name]] = locus_entry.source_strand
if len(self.normalization_ids) != len(self.assay_types):
raise Exception("Manifest format error: read invalid number of assay entries")
all_norm_ids = set()
for locus_idx in range(self.num_loci):
self.normalization_ids[locus_idx] += 100 * self.assay_types[locus_idx]
# To mimic the byte-wrapping behavior from GenomeStudio, AutoCall, IAAP take the mod of 256
self.normalization_ids[locus_idx] %= 256
all_norm_ids.add(self.normalization_ids[locus_idx])
sorted_norm_ids = sorted(all_norm_ids)
lookup_dictionary = {}
for idx in range(len(sorted_norm_ids)):
lookup_dictionary[sorted_norm_ids[idx]] = idx
self.normalization_lookups = [lookup_dictionary[normalization_id] for normalization_id in self.normalization_ids]
class SourceStrand:
Unknown = 0
Forward = 1
Reverse = 2
@staticmethod
def to_string(source_strand):
"""Get an integer representation of source strand annotation
Args:
source_strand (str) : string representation of source strand annotation (e.g., "F")
Returns:
int : int representation of source strand annotation (e.g. SourceStrand.Forward)
"""
if source_strand == SourceStrand.Unknown:
return "U"
elif source_strand == SourceStrand.Forward:
return "F"
elif source_strand == SourceStrand.Reverse:
return "R"
else:
raise Exception("Unexpected value for source strand " + source_strand)
@staticmethod
def from_string(source_strand):
"""Get a string representation of source strand annotation
Args:
source_strand (int) : int representation of source strand (e.g., SourceStrand.Forward)
Returns:
str : string representation of source strand annotation
"""
if source_strand == "U" or source_strand == "":
return SourceStrand.Unknown
if source_strand == "F":
return SourceStrand.Forward
elif source_strand == "R":
return SourceStrand.Reverse
else:
raise Exception("Unexpected value for source strand " + source_strand)
class RefStrand:
Unknown = 0
Plus = 1
Minus = 2
@staticmethod
def to_string(ref_strand):
"""Get a string reprensetation of ref strand annotation
Args:
ref_strand (int) : int representation of ref strand (e.g., RefStrand.Plus)
Returns:
str : string representation of reference strand annotation
"""
if ref_strand == RefStrand.Unknown:
return "U"
elif ref_strand == RefStrand.Plus:
return "+"
elif ref_strand == RefStrand.Minus:
return "-"
else:
raise Exception("Unexpected value for reference strand " + ref_strand)
@staticmethod
def from_string(ref_strand):
"""Get an integer representation of ref strand annotation
Args:
ref_strand (str) : string representation of reference strand annotation (e.g., "+")
Returns:
int : int representation of reference strand annotation (e.g. RefStrand.Plus)
"""
if ref_strand == "U" or ref_strand == "":
return RefStrand.Unknown
if ref_strand == "+":
return RefStrand.Plus
elif ref_strand == "-":
return RefStrand.Minus
else:
raise Exception("Unexpected value for reference strand " + ref_strand)
class LocusEntry:
"""Helper class representing a locus entry within a bead pool manifest. Current only support version
6,7, and 8.
Attributes:
ilmn_id (string) : IlmnID (probe identifier) of locus
name (string): Name (variant identifier) of locus
snp (string) : SNP value for locus (e.g., [A/C])
chrom (string) : Chromosome for the locus (e.g., XY)
map_info (int) : Mapping location of locus
assay_type (int) : Identifies type of assay (0 - Infinium II , 1 - Infinium I (A/T), 2 - Infinium I (G/C)
address_a (int) : AddressA ID of locus
address_b (int) : AddressB ID of locus (0 if none)
ref_strand (int) : See RefStrand class
source_strand (int) : See SourceStrand class
"""
def __init__(self, handle):
"""Constructor
Args:
handle (file handle): File handle at start of locus entry record
Returns:
LocusEntry
"""
self.ilmn_id = ""
self.name = ""
self.snp = ""
self.chrom = ""
self.map_info = -1
self.assay_type = -1
self.address_a = -1
self.address_b = -1
self.ref_strand = RefStrand.Unknown
self.source_strand = SourceStrand.Unknown
self.__parse_file(handle)
def __parse_file(self, handle):
"""Helper function to initialize this object from a file handle
Args:
handle (file handle): File handle at start of locus entry record
Returns:
None
"""
version = read_int(handle)
if version == 6:
self.__parse_locus_version_6(handle)
elif version == 7:
self.__parse_locus_version_7(handle)
elif version == 8:
self.__parse_locus_version_8(handle)
else:
raise Exception("Manifest format error: unknown version for locus entry (" + str(version) + ")")
def __parse_locus_version_6(self,handle):
"""Helper function to parse version 6 locus entry
Args:
handle (file handle): File handle at start of locus entry record
Returns:
None
"""
self.ilmn_id = read_string(handle)
self.source_strand = SourceStrand.from_string(self.ilmn_id.split("_")[-2])
self.name = read_string(handle)
for idx in range(3):
read_string(handle)
handle.read(4)
for idx in range(2):
read_string(handle)
self.snp = read_string(handle)
self.chrom = read_string(handle)
for idx in range(2):
read_string(handle)
self.map_info = int(read_string(handle))
for idx in range(2):
read_string(handle)
self.address_a = read_int(handle)
self.address_b = read_int(handle)
for idx in range(7):
read_string(handle)
handle.read(3)
self.assay_type = read_byte(handle)
if self.assay_type not in [0,1,2]:
raise Exception("Format error in reading assay type from locus entry")
if self.address_b == 0:
if self.assay_type != 0:
raise Exception("Manifest format error: Assay type is inconsistent with address B")
else:
if self.assay_type == 0:
raise Exception("Manifest format error: Assay type is inconsistent with address B")
def __parse_locus_version_7(self, handle):
"""Helper function to parse version 7 locus entry
Args:
handle (file handle): File handle at start of locus entry record
Returns:
None
"""
self.__parse_locus_version_6(handle)
handle.read(4 * 4)
def __parse_locus_version_8(self, handle):
"""Helper function to parse version 8 locus entry
Args:
handle (file handle): File handle at start of locus entry record
Returns:
None
"""
self.__parse_locus_version_7(handle)
self.ref_strand = RefStrand.from_string(read_string(handle))
complement_map = {"A": "T", "T":"A", "C":"G", "G":"C", "D":"D", "I":"I"}
def complement(nucleotide):
"""Complement a single nucleotide. Complements of D(eletion) and I(nsertion) are D and I, respectively.
Args:
nucleotide (string) : Nucleotide, must be A, C, T, G, D, or I
Returns:
str : Complemented nucleotide
"""
if nucleotide in complement_map:
return complement_map[nucleotide]
raise ValueError("Nucleotide must be one of A, C, T, G, D, or I")
def read_char(handle):
"""Helper function to parse character from file handle
Args:
handle (file handle): File handle
Returns:
char value read from handle
"""
return handle.read(1)
def read_ushort(handle):
"""Helper function to parse ushort from file handle
Args:
handle (file handle): File handle
Returns:
numpy.int16 value read from handle
"""
return frombuffer(handle.read(2), dtype=uint16)[0]
def read_int(handle):
"""Helper function to parse int from file handle
Args:
handle (file handle): File handle