isoformer-anonymous commited on
Commit
12d0972
1 Parent(s): 19adc14

Upload tokenizer

Browse files
Files changed (2) hide show
  1. isoformer_tokenizer.py +92 -0
  2. tokenizer_config.json +6 -0
isoformer_tokenizer.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2022 Meta and The HuggingFace Inc. team. All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ """Tokenization classes for ESM."""
16
+ import os
17
+ from typing import List, Optional
18
+
19
+ #from transformers.models.esm.tokenization_esm import PreTrainedTokenizer
20
+ from transformers import EsmTokenizer, PreTrainedTokenizer
21
+
22
+
23
+ VOCAB_FILES_NAMES = {"vocab_file": "vocab.txt"}
24
+
25
+
26
+ def load_vocab_file(vocab_file):
27
+ with open(vocab_file, "r") as f:
28
+ lines = f.read().splitlines()
29
+ return [l.strip() for l in lines]
30
+
31
+
32
+ class IsoformerTokenizer(PreTrainedTokenizer):
33
+ """
34
+ Constructs Isoformer tokenizer.
35
+ """
36
+
37
+ def __init__(
38
+ self,
39
+ config,
40
+ **kwargs
41
+ ):
42
+
43
+ dna_hf_tokenizer = EsmTokenizer("dna_vocab_list.txt", model_max_length=196608)
44
+ dna_hf_tokenizer.eos_token = None # Stops the tokenizer adding an EOS/SEP token at the end
45
+ dna_hf_tokenizer.init_kwargs["eos_token"] = None # Ensures it doesn't come back when reloading
46
+ dna_hf_tokenizer.bos_token = None # Stops the tokenizer adding an BOS/SEP token at the end
47
+ dna_hf_tokenizer.init_kwargs["bos_token"] = None # Ensures it doesn't come back when reloading
48
+
49
+
50
+ rna_hf_tokenizer = EsmTokenizer("rna_vocab_list.txt", model_max_length=1024)
51
+ rna_hf_tokenizer.eos_token = None # Stops the tokenizer adding an EOS/SEP token at the end
52
+ rna_hf_tokenizer.init_kwargs["eos_token"] = None # Ensures it doesn't come back when reloading
53
+
54
+ protein_hf_tokenizer = EsmTokenizer("protein_vocab_list.txt", model_max_length=1024)
55
+ # protein_hf_tokenizer.eos_token = None # Stops the tokenizer adding an EOS/SEP token at the end
56
+ # protein_hf_tokenizer.init_kwargs["eos_token"] = None # Ensures it doesn't come back when reloading
57
+
58
+ self.num_tokens_per_seq_nuctf = config.num_tokens_per_seq_nuctf
59
+ self.num_tokens_per_seq_nuctf_rna = config.num_tokens_per_seq_nuctf_rna
60
+ self.num_protein_tokens_per_seq = config.num_protein_tokens_per_seq
61
+ self.dna_tokenizer = dna_hf_tokenizer
62
+ self.rna_tokenizer = rna_hf_tokenizer
63
+ self.protein_tokenizer = protein_hf_tokenizer
64
+
65
+ self.dna_tokens = open("dna_vocab_list.txt", "r").read() .split("\n")
66
+ self.rna_tokens = open("rna_vocab_list.txt", "r").read() .split("\n")
67
+ self.protein_tokens = open("protein_vocab_list.txt", "r").read() .split("\n")
68
+ self.config = config
69
+
70
+ super().__init__(**kwargs)
71
+
72
+ def __call__(self, dna_input, rna_input, protein_input):
73
+ dna_output = self.dna_tokenizer(dna_input) #, max_length=196608, padding="max_length")
74
+ rna_output = self.rna_tokenizer(rna_input, max_length=1024, padding="max_length")
75
+ protein_output = self.protein_tokenizer(protein_input, max_length=1024, padding="max_length")
76
+ return dna_output, rna_output, protein_output
77
+
78
+ def _add_tokens(self, *args, **kwargs):
79
+ pass # Override this with an empty method to stop errors
80
+
81
+ def save_vocabulary(self, save_directory, filename_prefix):
82
+ vocab_file_dna = os.path.join(save_directory, (filename_prefix + "-" if filename_prefix else "") + "dna_vocab_list.txt")
83
+ vocab_file_rna = os.path.join(save_directory, (filename_prefix + "-" if filename_prefix else "") + "rna_vocab_list.txt")
84
+ vocab_file_protein = os.path.join(save_directory, (filename_prefix + "-" if filename_prefix else "") + "protein_vocab_list.txt")
85
+
86
+ with open(vocab_file_dna, "w") as f:
87
+ f.write("\n".join(self.dna_tokens))
88
+ with open(vocab_file_rna, "w") as f:
89
+ f.write("\n".join(self.rna_tokens))
90
+ with open(vocab_file_protein, "w") as f:
91
+ f.write("\n".join(self.protein_tokens))
92
+ return (vocab_file_dna,vocab_file_rna,vocab_file_protein, )
tokenizer_config.json CHANGED
@@ -1,4 +1,10 @@
1
  {
 
 
 
 
 
 
2
  "clean_up_tokenization_spaces": true,
3
  "model_max_length": 1000000000000000019884624838656,
4
  "tokenizer_class": "IsoformerTokenizer"
 
1
  {
2
+ "auto_map": {
3
+ "AutoTokenizer": [
4
+ "isoformer_tokenizer.IsoformerTokenizer",
5
+ null
6
+ ]
7
+ },
8
  "clean_up_tokenization_spaces": true,
9
  "model_max_length": 1000000000000000019884624838656,
10
  "tokenizer_class": "IsoformerTokenizer"