Spaces:
Sleeping
Sleeping
saicharan2804
commited on
Commit
·
2cc9210
1
Parent(s):
2918cfd
First commit
Browse files- KmerTokenizer,py +32 -0
- app.py +5 -3
KmerTokenizer,py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def atomwise_tokenizer(smi, exclusive_tokens = None):
|
2 |
+
"""
|
3 |
+
Tokenize a SMILES molecule at atom-level:
|
4 |
+
(1) 'Br' and 'Cl' are two-character tokens
|
5 |
+
(2) Symbols with bracket are considered as tokens
|
6 |
+
|
7 |
+
exclusive_tokens: A list of specifical symbols with bracket you want to keep. e.g., ['[C@@H]', '[nH]'].
|
8 |
+
Other symbols with bracket will be replaced by '[UNK]'. default is `None`.
|
9 |
+
"""
|
10 |
+
import re
|
11 |
+
pattern = "(\[[^\]]+]|Br?|Cl?|N|O|S|P|F|I|b|c|n|o|s|p|\(|\)|\.|=|#|-|\+|\\\\|\/|:|~|@|\?|>|\*|\$|\%[0-9]{2}|[0-9])"
|
12 |
+
regex = re.compile(pattern)
|
13 |
+
tokens = [token for token in regex.findall(smi)]
|
14 |
+
|
15 |
+
if exclusive_tokens:
|
16 |
+
for i, tok in enumerate(tokens):
|
17 |
+
if tok.startswith('['):
|
18 |
+
if tok not in exclusive_tokens:
|
19 |
+
tokens[i] = '[UNK]'
|
20 |
+
return tokens
|
21 |
+
|
22 |
+
def kmer_tokenizer(smiles, ngram=4, stride=1, remove_last = False, exclusive_tokens = None):
|
23 |
+
units = atomwise_tokenizer(smiles, exclusive_tokens = exclusive_tokens) #collect all the atom-wise tokens from the SMILES
|
24 |
+
if ngram == 1:
|
25 |
+
tokens = units
|
26 |
+
else:
|
27 |
+
tokens = [tokens_to_mer(units[i:i+ngram]) for i in range(0, len(units), stride) if len(units[i:i+ngram]) == ngram]
|
28 |
+
|
29 |
+
if remove_last:
|
30 |
+
if len(tokens[-1]) < ngram: #truncate last whole k-mer if the length of the last k-mers is less than ngram.
|
31 |
+
tokens = tokens[:-1]
|
32 |
+
return tokens
|
app.py
CHANGED
@@ -1,7 +1,9 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
-
def
|
4 |
-
return
|
5 |
|
6 |
-
|
|
|
7 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from KmerTokenizer import kmer_tokenizer
|
3 |
|
4 |
+
def fn(name, num =3):
|
5 |
+
return name*num
|
6 |
|
7 |
+
|
8 |
+
iface = gr.Interface(fn=kmer_tokenizer, inputs=["text", "Dropdown" ], outputs="text")
|
9 |
iface.launch()
|