diff --git a/haptools/data/genotypes.py b/haptools/data/genotypes.py index 953d8210..dce237a3 100644 --- a/haptools/data/genotypes.py +++ b/haptools/data/genotypes.py @@ -805,7 +805,13 @@ def write(self): record.samples[sample].phased = self.data[samp_idx, var_idx, 2] # write the record to a file vcf.write(record) - vcf.close() + try: + vcf.close() + except OSError as e: + if e.errno == 9 and len(self.variants) == 0: + self.log.warning("No variants to write to VCF") + else: + raise e class TRRecordHarmonizerRegion(trh.TRRecordHarmonizer): diff --git a/haptools/data/haplotypes.py b/haptools/data/haplotypes.py index ba33d0a5..1723be0e 100644 --- a/haptools/data/haplotypes.py +++ b/haptools/data/haplotypes.py @@ -1198,9 +1198,13 @@ def __iter__( indexed = True try: haps_file = TabixFile(str(self.fname)) + if region is not None: + haps_file.fetch(region=region, multiple_iterators=True) except OSError: indexed = False - # if the user requested a specific region or subset of haplotypes and the file + except ValueError: + indexed = False + # If the user requested a specific region or subset of haplotypes and the file # is indexed, then we should handle it using tabix # else, we use a regular text opener - b/c there's no benefit to using tabix if (region or haplotypes) and indexed: