Skip to content

Commit

Permalink
f-strings
Browse files Browse the repository at this point in the history
  • Loading branch information
lemieuxl committed Feb 12, 2024
1 parent 4389cd9 commit 2dc8d3e
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions pyplink/pyplink.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ def __init__(self, prefix, mode="r", bed_format="SNP-major"):

# The bed format
if bed_format not in {"SNP-major", "INDIVIDUAL-major"}:
raise ValueError("invalid bed format: {}".format(bed_format))
raise ValueError(f"invalid bed format: {bed_format}")
self._bed_format = bed_format

# These are the name of the files
self.bed_filename = "{}.bed".format(prefix)
self.bim_filename = "{}.bim".format(prefix)
self.fam_filename = "{}.fam".format(prefix)
self.bed_filename = f"{prefix}.bed"
self.bim_filename = f"{prefix}.bim"
self.fam_filename = f"{prefix}.fam"

if self._mode == "r":
if self._bed_format != "SNP-major":
Expand All @@ -112,7 +112,7 @@ def __init__(self, prefix, mode="r", bed_format="SNP-major"):
for filename in (self.bed_filename, self.bim_filename,
self.fam_filename):
if not os.path.isfile(filename):
raise IOError("No such file: '{}'".format(filename))
raise IOError(f"No such file: '{filename}'")

# Setting BIM and FAM to None
self._bim = None
Expand All @@ -135,14 +135,14 @@ def __init__(self, prefix, mode="r", bed_format="SNP-major"):
self._write_bed_header()

else:
raise ValueError("invalid mode: '{}'".format(self._mode))
raise ValueError(f"invalid mode: '{self._mode}'")

def __repr__(self):
"""The representation of the PyPlink object."""
if self._mode == "r":
return "PyPlink({:,d} samples; {:,d} markers)".format(
self.get_nb_samples(),
self.get_nb_markers(),
return (
f"PyPlink({self.get_nb_samples():,d} samples; "
f"{self.get_nb_markers():,d} markers)"
)

return 'PyPlink(mode="w")'
Expand Down Expand Up @@ -210,7 +210,7 @@ def seek(self, n):

else:
# Invalid seek value
raise ValueError("invalid position in BED: {}".format(n))
raise ValueError(f"invalid position in BED: {n}")

def _get_seek_position(self, n):
"""Gets the seek position in the file (including special bytes).
Expand Down Expand Up @@ -254,15 +254,15 @@ def _read_bim(self):
# Logging a warning
logger.warning("Duplicated markers found")
for marker, count in duplicated_marker_counts.items():
logger.warning(" - {}: {:,d} times".format(marker, count))
logger.warning(" - %s: %s times", marker, count)
logger.warning("Appending ':dupX' to the duplicated markers "
"according to their location in the BIM file")

# Renaming the markers
counter = Counter()
for i, marker in duplicated_markers.items():
counter[marker] += 1
new_name = "{}:dup{}".format(marker, counter[marker])
new_name = f"{marker}:dup{counter[marker]}"
bim.loc[i, "snp"] = new_name

# Updating the dictionary containing the duplicated markers
Expand Down Expand Up @@ -373,13 +373,12 @@ def _read_bed(self):
with open(self.bed_filename, "rb") as bed_file:
# Checking that the first two bytes are OK
if (ord(bed_file.read(1)) != 108) or (ord(bed_file.read(1)) != 27):
raise ValueError("not a valid BED file: "
"{}".format(self.bed_filename))
raise ValueError(f"not a valid BED file: {self.bed_filename}")

# Checking that the format is SNP-major
if ord(bed_file.read(1)) != 1:
raise ValueError("not in SNP-major format (please recode): "
"{}".format(self.bed_filename))
raise ValueError(f"not in SNP-major format (please recode): "
f"{self.bed_filename}")

# Checking the last entry (for BED corruption)
seek_index = self._get_seek_position(self._bim.iloc[-1, :].i)
Expand Down Expand Up @@ -489,7 +488,7 @@ def get_geno_marker(self, marker, return_index=False):

# Check if the marker exists
if marker not in self._bim.index:
raise ValueError("{}: marker not in BIM".format(marker))
raise ValueError(f"{marker}: marker not in BIM")

# Seeking to the correct position
seek_index = self._bim.loc[marker, "i"]
Expand Down Expand Up @@ -531,10 +530,10 @@ def write_genotypes(self, genotypes):

# Checking the expected number of samples
if self._nb_values != len(genotypes):
raise ValueError("{:,d} samples expected, got {:,d}".format(
self._nb_values,
len(genotypes),
))
raise ValueError(
f"{self._nb_values:,d} samples expected, got "
f"{len(genotypes):,d}"
)

# Writing to file
byte_array = [
Expand Down

0 comments on commit 2dc8d3e

Please sign in to comment.