Skip to content

Commit eaaedce

Browse files
committed
Add iter_contig
1 parent fc2b074 commit eaaedce

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

bio2zarr/vcf2zarr/icf.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,19 @@ def iter_filters(self, start, stop):
943943
) from None
944944
yield filters
945945

946+
def iter_contig(self, start, stop):
947+
source_field = self.fields["CHROM"]
948+
lookup = {
949+
contig.id: index for index, contig in enumerate(self.metadata.contigs)
950+
}
951+
952+
for value in source_field.iter_values(start, stop):
953+
# Note: because we are using the indexes to define the lookups
954+
# and we always have an index, it seems that we the contig lookup
955+
# will always succeed. However, if anyone ever does hit a KeyError
956+
# here, please do open an issue with a reproducible example!
957+
yield lookup[value[0]]
958+
946959
def iter_field(self, field_name, shape, start, stop):
947960
source_field = self.fields[field_name]
948961
sanitiser = source_field.sanitiser_factory(shape)

bio2zarr/writer.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -557,18 +557,12 @@ def encode_filters_partition(self, partition_index):
557557
self.finalise_partition_array(partition_index, var_filter)
558558

559559
def encode_contig_partition(self, partition_index):
560-
lookup = {contig.id: index for index, contig in enumerate(self.schema.contigs)}
561560
contig = self.init_partition_array(partition_index, "variant_contig")
562561
partition = self.metadata.partitions[partition_index]
563-
field = self.source.fields["CHROM"]
564562

565-
for value in field.iter_values(partition.start, partition.stop):
563+
for contig_index in self.source.iter_contig(partition.start, partition.stop):
566564
j = contig.next_buffer_row()
567-
# Note: because we are using the indexes to define the lookups
568-
# and we always have an index, it seems that we the contig lookup
569-
# will always succeed. However, if anyone ever does hit a KeyError
570-
# here, please do open an issue with a reproducible example!
571-
contig.buff[j] = lookup[value[0]]
565+
contig.buff[j] = contig_index
572566

573567
self.finalise_partition_array(partition_index, contig)
574568

0 commit comments

Comments
 (0)