Skip to content

Commit

Permalink
Add encoding="utf-8" (thanks @yb520abab, #295, #296)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanghaibao authored May 22, 2024
1 parent 80b09ec commit a2b7e34
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions goatools/cli/find_enrichment.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def _check_input_files(nspc, parser):
{} Actual files: {}""".format(
len(nspc.filenames), " ".join(nspc.filenames)
)
raise Exception(msg)
raise ValueError(msg)
for fin in nspc.filenames:
if not os.path.exists(fin):
return "*{}* does not exist".format(fin)
Expand Down Expand Up @@ -405,7 +405,7 @@ def _init_itemid2name(self):
if fin_id2sym is not None and os.path.exists(fin_id2sym):
id2sym = {}
cmpl = re.compile(r"^\s*(\S+)[\s,;]+(\S+)")
with open(fin_id2sym) as ifstrm:
with open(fin_id2sym, encoding="utf-8") as ifstrm:
for line in ifstrm:
mtch = cmpl.search(line)
if mtch:
Expand Down Expand Up @@ -569,11 +569,12 @@ def rd_files(self, study_fn, pop_fn):
print(f"Study: {len(study)} vs. Population {len(pop)}\n")
return study, pop

def _read_geneset(self, study_fn, pop_fn):
def _read_geneset(self, study_fn: str, pop_fn: str):
"""Open files containing genes. Return study genes and population genes."""
## pop = read_geneset(study_fn)
pop = set(_.strip() for _ in open(pop_fn) if _.strip())
study = frozenset(_.strip() for _ in open(study_fn) if _.strip())
pop = set(_.strip() for _ in open(pop_fn, encoding="utf-8") if _.strip())
study = frozenset(
_.strip() for _ in open(study_fn, encoding="utf-8") if _.strip()
)
if next(iter(pop)).isdigit():
pop = set(int(g) for g in pop)
study = frozenset(int(g) for g in study)
Expand Down Expand Up @@ -714,7 +715,7 @@ def wr_xlsx(self, fout_xlsx):

def wr_tsv(self, fout_tsv):
"""Print grouped GOEA results into a tab-separated file."""
with open(fout_tsv, "w") as prt:
with open(fout_tsv, "w", encoding="utf-8") as prt:
kws_tsv = {
"fld2fmt": {f: "{:8.2e}" for f in self.flds_cur if f[:2] == "p_"},
"prt_flds": self.flds_cur,
Expand All @@ -724,7 +725,7 @@ def wr_tsv(self, fout_tsv):

def wr_txt(self, fout_txt):
"""Write to a file GOEA results in an ASCII text format."""
with open(fout_txt, "w") as prt:
with open(fout_txt, "w", encoding="utf-8") as prt:
for line in self.ver_list:
prt.write("{LINE}\n".format(LINE=line))
self.prt_txt(prt)
Expand Down

0 comments on commit a2b7e34

Please sign in to comment.