MsAlEhR commited on
Commit
5d60f19
·
verified ·
1 Parent(s): 2618f79

Delete .ipynb_checkpoints

Browse files
.ipynb_checkpoints/__init__-checkpoint.py DELETED
@@ -1 +0,0 @@
1
- from .tokenizer import KmerTokenizer
 
 
.ipynb_checkpoints/tokenizer-checkpoint.py DELETED
@@ -1,113 +0,0 @@
1
- import itertools
2
- from transformers import PreTrainedTokenizer
3
- import json
4
- import os
5
-
6
- class KmerTokenizer(PreTrainedTokenizer):
7
- def __init__(self, vocab_file=None, kmerlen=6, overlapping=True, maxlen=400, **kwargs):
8
- self.kmerlen = kmerlen
9
- self.overlapping = overlapping
10
- self.maxlen = maxlen
11
-
12
- # Initialize vocabulary
13
- self.VOCAB = [''.join(i) for i in itertools.product(*(['ATCG'] * int(self.kmerlen)))]
14
- self.VOCAB_SIZE = len(self.VOCAB) + 5
15
-
16
- self.tokendict = dict(zip(self.VOCAB, range(5, self.VOCAB_SIZE)))
17
- self.tokendict['[UNK]'] = 0
18
- self.tokendict['[SEP]'] = 1
19
- self.tokendict['[CLS]'] = 2
20
- self.tokendict['[MASK]'] = 3
21
- self.tokendict['[PAD]'] = 4
22
-
23
- super().__init__(**kwargs)
24
-
25
- def _tokenize(self, text):
26
- tokens = []
27
- stoprange = len(text) - (self.kmerlen - 1)
28
- if self.overlapping:
29
- for k in range(0, stoprange):
30
- kmer = text[k:k + self.kmerlen]
31
- if set(kmer).issubset('ATCG'):
32
- tokens.append(kmer)
33
- else:
34
- for k in range(0, stoprange, self.kmerlen):
35
- kmer = text[k:k + self.kmerlen]
36
- if set(kmer).issubset('ATCG'):
37
- tokens.append(kmer)
38
- return tokens
39
-
40
- def _convert_token_to_id(self, token):
41
- return self.tokendict.get(token, self.tokendict['[UNK]'])
42
-
43
- def _convert_id_to_token(self, index):
44
- inv_tokendict = {v: k for k, v in self.tokendict.items()}
45
- return inv_tokendict.get(index, '[UNK]')
46
-
47
- def convert_tokens_to_string(self, tokens):
48
- return ' '.join(tokens)
49
-
50
- def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None):
51
- if token_ids_1 is None:
52
- return [self.tokendict['[CLS]']] + token_ids_0 + [self.tokendict['[SEP]']]
53
- return [self.tokendict['[CLS]']] + token_ids_0 + [self.tokendict['[SEP]']] + token_ids_1 + [self.tokendict['[SEP]']]
54
-
55
- def get_vocab(self):
56
- return self.tokendict
57
-
58
- def kmer_tokenize(self, seq_list):
59
- seq_ind_list = []
60
- for seq in seq_list:
61
- tokens = self._tokenize(seq)
62
- token_ids = [self._convert_token_to_id(token) for token in tokens]
63
- if len(token_ids) < self.maxlen:
64
- token_ids.extend([self.tokendict['[PAD]']] * (self.maxlen - len(token_ids)))
65
- else:
66
- token_ids = token_ids[:self.maxlen]
67
- seq_ind_list.append(token_ids)
68
- return seq_ind_list
69
-
70
- def save_vocabulary(self, save_directory, filename_prefix=None):
71
- if not os.path.isdir(save_directory):
72
- os.makedirs(save_directory)
73
-
74
- vocab_file = os.path.join(save_directory, (filename_prefix + '-' if filename_prefix else '') + 'vocab.json')
75
-
76
- with open(vocab_file, 'w') as f:
77
- json.dump(self.tokendict, f)
78
-
79
- return (vocab_file,)
80
-
81
- def save_pretrained(self, save_directory, **kwargs):
82
- special_tokens_map_file = os.path.join(save_directory, "special_tokens_map.json")
83
- with open(special_tokens_map_file, "w") as f:
84
- json.dump({
85
- "kmerlen": self.kmerlen,
86
- "overlapping": self.overlapping,
87
- "maxlen": self.maxlen
88
- }, f)
89
- vocab_files = self.save_vocabulary(save_directory)
90
- return (special_tokens_map_file,) + vocab_files
91
-
92
- @classmethod
93
- def from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs):
94
- # Load tokenizer using the parent class method
95
- tokenizer = super().from_pretrained(pretrained_model_name_or_path, *model_args, **kwargs)
96
-
97
- # Load special tokens map
98
- special_tokens_map_file = os.path.join(pretrained_model_name_or_path, "special_tokens_map.json")
99
- if os.path.isfile(special_tokens_map_file):
100
- with open(special_tokens_map_file, "r") as f:
101
- special_tokens_map = json.load(f)
102
- tokenizer.kmerlen = special_tokens_map.get("kmerlen", 6)
103
- tokenizer.overlapping = special_tokens_map.get("overlapping", True)
104
- tokenizer.maxlen = special_tokens_map.get("maxlen", 400)
105
-
106
- # Load vocabulary
107
- vocab_file = os.path.join(pretrained_model_name_or_path, "vocab.json")
108
- if os.path.isfile(vocab_file):
109
- with open(vocab_file, "r") as f:
110
- tokendict = json.load(f)
111
- tokenizer.tokendict = tokendict
112
-
113
- return tokenizer