Skip to content

Commit

Permalink
Fix Path handling issues in VCFWriter, BEDWriter and PGENWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
miriambt committed Nov 15, 2024
1 parent bdd8758 commit 392fef9
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 deletions snputils/snp/io/write/bed.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def write(self):

# Save .bed file
if self.__filename.suffix != '.bed':
self.__filename = self.__filename.with_name(self.__filename.name + '.bed')
self.__filename = self.__filename.with_name(self.__filename.with_suffix('.bed'))

log.info(f"Writing .bed file: {self.__filename}")

Expand All @@ -40,7 +40,7 @@ def write(self):
samples, variants = self.__snpobj.calldata_gt.shape

# Define the PgenWriter to save the data
data_save = pg.PgenWriter(filename=self.__filename.encode('utf-8'),
data_save = pg.PgenWriter(filename=str(self.__filename).encode('utf-8'),
sample_ct=samples,
variant_ct=variants,
nonref_flags=True,
Expand Down Expand Up @@ -70,7 +70,7 @@ def write(self):
fam_file['fid'] = self.__snpobj.samples

# Save .fam file
fam_file.to_csv(self.__filename + '.fam', sep='\t', index=False, header=False)
fam_file.to_csv(self.__filename.with_suffix('.fam'), sep='\t', index=False, header=False)
log.info(f"Finished writing .fam file: {self.__filename}")

# Save .bim file
Expand All @@ -87,5 +87,5 @@ def write(self):
bim_file['a1'] = self.__snpobj.variants_ref

# Save .bim file
bim_file.to_csv(self.__filename + '.bim', sep='\t', index=False, header=False)
bim_file.to_csv(self.__filename.with_suffix('.bim'), sep='\t', index=False, header=False)
log.info(f"Finished writing .bim file: {self.__filename}")
2 changes: 1 addition & 1 deletion snputils/snp/io/write/pgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def write_pgen(self):
flat_genotypes = self.__snpobj.__calldata_gt

with pg.PgenWriter(
f"{self.__filename}.pgen".encode("utf-8"),
filename=str(self.__filename).encode('utf-8'),
sample_ct=num_samples,
variant_ct=num_variants,
hardcall_phase_present=phased,
Expand Down
4 changes: 2 additions & 2 deletions snputils/snp/io/write/vcf.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ def write_chromosome_data(self, chrom, data_chrom):

# Format output file
if chrom == "All":
file = self.__filename + self.__file_extension
file = self.__filename.with_suffix(self.__file_extension)
else:
file = f'{self.__filename}_{chrom}{self.__file_extension}'
file = self.__filename.parent / f"{self.__filename.stem}_{chrom}{self.__file_extension}"

# Write header
with open(file, "w") as f:
Expand Down

0 comments on commit 392fef9

Please sign in to comment.