forked from tyfei0216/ionChannel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathioutils.py
95 lines (79 loc) · 2.56 KB
/
ioutils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import re
import pandas as pd
re1 = re.compile(r"[-*\s]")
re2 = re.compile(r"[-* Xx.\s]")
re3 = re.compile(r"^[ACDEFGHIKLMNPQRSTVWYacdefghiklmnpqrstvwyXx -*.]*$")
re4 = re.compile(r"[^ACDEFGHIKLMNPQRSTVWYacdefghiklmnpqrstvwy]")
def parseline(line, remove="all"):
try:
if isinstance(line, bytes):
line = line.decode("utf-8").strip()
except:
return None
if line.startswith(">"):
return line
if remove == "all":
line = re4.sub("", line)
elif remove == "whitespaces":
line = re1.sub("", line)
elif remove == "none":
pass
else:
raise NotImplementedError
return line
# inspired by esm.data.readfasta
def readFasta(path, to_upper=True, remove="whitespaces", truclength=1500, checkseq=re3):
if checkseq is not None:
assert isinstance(checkseq, re.Pattern)
with open(path, "rb") as f:
line = None
seq = []
for t in f:
t = parseline(t, remove=remove)
if t is None:
line = None
continue
if line is None:
if t.startswith(">"):
line = t[1:]
continue
if t.startswith(">"):
s = "".join(seq)
seq = []
if len(s) > 0:
if to_upper:
s = s.upper()
if truclength is not None:
s = s[:truclength]
yield line, s
line = t[1:]
continue
if checkseq is not None:
if not checkseq.match(t):
line = None
seq = []
continue
seq.append(t)
def readNCBICsv(
path, to_upper=True, remove="whitespaces", truclength=1500, checkseq=re3
):
if checkseq is not None:
assert isinstance(checkseq, re.Pattern)
entries = []
df = pd.read_csv(path, header=0)
for i, r in df.iterrows():
if len(str(r["translation"])) > 5:
s = parseline(str(r["translation"]), remove=remove)
if checkseq is not None:
if not checkseq.match(s):
continue
if to_upper:
s = s.upper()
if truclength is not None:
s = s[:truclength]
if "definition" in df.columns:
defination = r["definition"]
else:
defination = ""
entries.append(((r["protein_id"], defination, r["organism"]), s))
return entries