diff --git a/src/pygenomeviz/__init__.py b/src/pygenomeviz/__init__.py index ce8c8b7..768f3ca 100644 --- a/src/pygenomeviz/__init__.py +++ b/src/pygenomeviz/__init__.py @@ -2,7 +2,7 @@ from pygenomeviz.genomeviz import GenomeViz -__version__ = "1.4.0" +__version__ = "1.4.1" __all__ = [ "GenomeViz", diff --git a/src/pygenomeviz/align/coord.py b/src/pygenomeviz/align/coord.py index 6d8eb57..f3fcd58 100644 --- a/src/pygenomeviz/align/coord.py +++ b/src/pygenomeviz/align/coord.py @@ -233,7 +233,7 @@ def parse_pmauve_file( align_coords : list[AlignCoord] Align coord list """ - with open(bbone_file) as f: + with open(bbone_file, encoding="utf-8") as f: reader = csv.reader(f, delimiter="\t") header_row = next(reader) genome_num = int(len(header_row) / 2) @@ -316,7 +316,7 @@ def write( elif isinstance(outfile, io.BytesIO): outfile.write(bytes(contents, encoding="utf-8")) else: - with open(outfile, "w") as f: + with open(outfile, "w", encoding="utf-8") as f: f.write(contents) @staticmethod @@ -334,7 +334,7 @@ def read(align_coords_file: str | Path) -> list[AlignCoord]: Alignment coords """ align_coords = [] - with open(align_coords_file) as f: + with open(align_coords_file, encoding="utf-8") as f: reader = csv.reader(f, delimiter="\t") next(reader) for row in reader: diff --git a/src/pygenomeviz/align/tool/base.py b/src/pygenomeviz/align/tool/base.py index 37678d5..1f122e5 100644 --- a/src/pygenomeviz/align/tool/base.py +++ b/src/pygenomeviz/align/tool/base.py @@ -111,7 +111,7 @@ def run_cmd( # Write stdout result if stdout_file is set if stdout_file: logger.info(f"> Save cmd stdout results to '{stdout_file}'") - with open(stdout_file, "w") as f: + with open(stdout_file, "w", encoding="utf-8") as f: f.write(cmd_res.stdout) else: logger.error("Failed to run command below!!") @@ -183,7 +183,7 @@ def _parse_input_gbk_and_fasta_seqs( if isinstance(seq, Path): suffix = seq.suffix if suffix == ".gz" and len(seq.suffixes) >= 2: - suffix = seq.suffixes[-2].join("") + suffix = "".join(seq.suffixes[-2]) if suffix in gbk_suffixes: parse_seqs.append(Genbank(seq)) elif suffix in fasta_suffixes: diff --git a/src/pygenomeviz/align/tool/mmseqs.py b/src/pygenomeviz/align/tool/mmseqs.py index a0ed246..878b80a 100644 --- a/src/pygenomeviz/align/tool/mmseqs.py +++ b/src/pygenomeviz/align/tool/mmseqs.py @@ -129,7 +129,7 @@ def _parse_coords_file( """ align_coords = [] dup_check_list = [] - with open(rbh_result_file) as f: + with open(rbh_result_file, encoding="utf-8") as f: reader = csv.reader(f, delimiter="\t") for row in reader: # Query [e.g. `GENE000001_NC_XXXXXX.X|name|8205_8559_1|`] diff --git a/src/pygenomeviz/genomeviz.py b/src/pygenomeviz/genomeviz.py index 461eb57..10e6db7 100644 --- a/src/pygenomeviz/genomeviz.py +++ b/src/pygenomeviz/genomeviz.py @@ -632,7 +632,7 @@ def savefig_html( elif isinstance(html_outfile, io.BytesIO): html_outfile.write(bytes(viewer_html, encoding="utf-8")) else: - with open(html_outfile, "w") as f: + with open(html_outfile, "w", encoding="utf-8") as f: f.write(viewer_html) ############################################################ diff --git a/src/pygenomeviz/parser/fasta.py b/src/pygenomeviz/parser/fasta.py index fa5a596..8d5fbdc 100644 --- a/src/pygenomeviz/parser/fasta.py +++ b/src/pygenomeviz/parser/fasta.py @@ -120,7 +120,7 @@ def write_genome_fasta(self, outfile: str | Path) -> None: outfile : str | Path Output genome fasta file """ - with open(outfile, "w") as f: + with open(outfile, "w", encoding="utf-8") as f: for seqid, seq in self.get_seqid2seq().items(): f.write(f">{seqid}\n{seq}\n") @@ -142,14 +142,16 @@ def _parse_fasta_file(self, fasta_file: str | Path) -> list[SeqRecord]: SeqRecord list """ if Path(fasta_file).suffix == ".gz": - with gzip.open(fasta_file, mode="rt") as f: + with gzip.open(fasta_file, mode="rt", encoding="utf-8") as f: return list(SeqIO.parse(f, "fasta")) elif Path(fasta_file).suffix == ".bz2": - with bz2.open(fasta_file, mode="rt") as f: + with bz2.open(fasta_file, mode="rt", encoding="utf-8") as f: return list(SeqIO.parse(f, "fasta")) elif Path(fasta_file).suffix == ".zip": with zipfile.ZipFile(fasta_file) as zip: with zip.open(zip.namelist()[0]) as f: - return list(SeqIO.parse(TextIOWrapper(f), "fasta")) + io = TextIOWrapper(f, encoding="utf-8") + return list(SeqIO.parse(io, "fasta")) else: - return list(SeqIO.parse(fasta_file, "fasta")) + with open(fasta_file, encoding="utf-8") as f: + return list(SeqIO.parse(f, "fasta")) diff --git a/src/pygenomeviz/parser/genbank.py b/src/pygenomeviz/parser/genbank.py index ca564a8..2bf6e4f 100644 --- a/src/pygenomeviz/parser/genbank.py +++ b/src/pygenomeviz/parser/genbank.py @@ -368,7 +368,7 @@ def write_genome_fasta(self, outfile: str | Path) -> None: outfile : str | Path Output genome fasta file """ - with open(outfile, "w") as f: + with open(outfile, "w", encoding="utf-8") as f: for seqid, seq in self.get_seqid2seq().items(): f.write(f">{seqid}\n{seq}\n") @@ -391,19 +391,23 @@ def _parse_gbk_source( list[SeqRecord] Genbank SeqRecords """ - # Parse compressed file + # Parse file if isinstance(gbk_source, (str, Path)): if Path(gbk_source).suffix == ".gz": - with gzip.open(gbk_source, mode="rt") as f: + with gzip.open(gbk_source, mode="rt", encoding="utf-8") as f: return list(SeqIO.parse(f, "genbank")) elif Path(gbk_source).suffix == ".bz2": - with bz2.open(gbk_source, mode="rt") as f: + with bz2.open(gbk_source, mode="rt", encoding="utf-8") as f: return list(SeqIO.parse(f, "genbank")) elif Path(gbk_source).suffix == ".zip": with zipfile.ZipFile(gbk_source) as zip: with zip.open(zip.namelist()[0]) as f: - return list(SeqIO.parse(TextIOWrapper(f), "genbank")) - # Parse no compressed file or TextIOWrapper + io = TextIOWrapper(f, encoding="utf-8") + return list(SeqIO.parse(io, "genbank")) + else: + with open(gbk_source, encoding="utf-8") as f: + return list(SeqIO.parse(f, "genbank")) + # Parse TextIOWrapper return list(SeqIO.parse(gbk_source, "genbank")) def _is_straddle_feature(self, feature: SeqFeature) -> bool: diff --git a/src/pygenomeviz/parser/gff.py b/src/pygenomeviz/parser/gff.py index 8308077..24b9d7f 100644 --- a/src/pygenomeviz/parser/gff.py +++ b/src/pygenomeviz/parser/gff.py @@ -287,18 +287,18 @@ def _parse_gff( """ gff_file = Path(gff_file) if gff_file.suffix == ".gz": - with gzip.open(gff_file, mode="rt") as f: + with gzip.open(gff_file, mode="rt", encoding="utf-8") as f: gff_records, start, end = self._parse_gff_textio(f, target_seqid) elif gff_file.suffix == ".bz2": - with bz2.open(gff_file, mode="rt") as f: + with bz2.open(gff_file, mode="rt", encoding="utf-8") as f: gff_records, start, end = self._parse_gff_textio(f, target_seqid) elif gff_file.suffix == ".zip": with zipfile.ZipFile(gff_file) as zip: with zip.open(zip.namelist()[0]) as f: - io = TextIOWrapper(f) + io = TextIOWrapper(f, encoding="utf-8") gff_records, start, end = self._parse_gff_textio(io, target_seqid) else: - with open(gff_file) as f: + with open(gff_file, encoding="utf-8") as f: gff_records, start, end = self._parse_gff_textio(f, target_seqid) return gff_records, start, end diff --git a/src/pygenomeviz/utils/download.py b/src/pygenomeviz/utils/download.py index b3f0cd2..6459eea 100644 --- a/src/pygenomeviz/utils/download.py +++ b/src/pygenomeviz/utils/download.py @@ -287,7 +287,7 @@ def fetch_genbank_by_accid( ) if gbk_outfile is not None: gbk_text = gbk_fetch_data.read() - with open(gbk_outfile, "w") as f: + with open(gbk_outfile, "w", encoding="utf-8") as f: f.write(gbk_text) gbk_fetch_data = StringIO(gbk_text) diff --git a/src/pygenomeviz/viewer/__init__.py b/src/pygenomeviz/viewer/__init__.py index 3b12637..a45072f 100644 --- a/src/pygenomeviz/viewer/__init__.py +++ b/src/pygenomeviz/viewer/__init__.py @@ -14,7 +14,7 @@ def _concat_target_files_contents(files: list[Path], target_ext: str) -> str: contents = "\n" target_files = [file for file in files if file.suffix == target_ext] for target_file in target_files: - with open(target_file) as f: + with open(target_file, encoding="utf-8") as f: contents += f.read() + "\n" return contents @@ -70,7 +70,7 @@ def setup_viewer_html( err_msg = "Failed to save HTML viewer. Check if target figure is generated by 'gv.plotfig(fast_render=False)' method call." # noqa: E501 raise ValueError(err_msg) # Read template html file - with open(TEMPLATE_HTML_FILE) as f: + with open(TEMPLATE_HTML_FILE, encoding="utf-8") as f: viewer_html = f.read() # Replace template strings return ( diff --git a/tests/parser/test_fasta.py b/tests/parser/test_fasta.py index f773e83..a14eb33 100644 --- a/tests/parser/test_fasta.py +++ b/tests/parser/test_fasta.py @@ -24,7 +24,7 @@ def test_fasta_property(tmp_path: Path): """Test Fasta instance properties""" fasta_file = tmp_path / "test.fa" - with open(fasta_file, "w") as f: + with open(fasta_file, "w", encoding="utf-8") as f: f.write(fasta_txt) fasta = Fasta(fasta_file) diff --git a/tests/parser/test_genbank.py b/tests/parser/test_genbank.py index a67a853..7a965e0 100644 --- a/tests/parser/test_genbank.py +++ b/tests/parser/test_genbank.py @@ -82,7 +82,7 @@ def test_genbank_write_genome_fasta(multi_record_gbk_file: Path, tmp_path: Path) assert outfile.exists() # Check number of genome fasta - with open(outfile) as f: + with open(outfile, encoding="utf-8") as f: lines = f.read().splitlines() fasta_count = len([line for line in lines if line.startswith(">")]) assert fasta_count == 8