mahdibaghbanzadeh commited on
Commit
5e43408
·
verified ·
1 Parent(s): 941fd7a

Create genome_origin_datasets.py

Browse files
Files changed (1) hide show
  1. genome_origin_datasets.py +112 -0
genome_origin_datasets.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script
2
+ # contributor.
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
+ """Script for the dataset containing tasks for genome origin detection."""
16
+
17
+ from typing import List
18
+
19
+ import datasets
20
+
21
+
22
+ # Custom function to parse lines in the dataset
23
+ def parse_text(fp):
24
+ for line in fp:
25
+ line = line.strip()
26
+ if not line: # Skip empty lines
27
+ continue
28
+ seq, label = line.split(",")
29
+ yield seq, label
30
+
31
+
32
+ # Dataset metadata
33
+ _CITATION = """TBD"""
34
+ _DESCRIPTION = """TBD"""
35
+ _HOMEPAGE = "TBD"
36
+ _LICENSE = "TBD"
37
+
38
+ # Tasks available in the dataset
39
+ _TASKS = [
40
+ "four_kingdoms",
41
+ "plasmid_detection",
42
+ ]
43
+
44
+
45
+ class GenomeOriginTasksConfig(datasets.BuilderConfig):
46
+ """BuilderConfig for Genome Origin tasks dataset."""
47
+
48
+ def __init__(self, *args, task: str, **kwargs):
49
+ """
50
+ BuilderConfig for Genome Origin tasks dataset.
51
+
52
+ Args:
53
+ task (str): Task name.
54
+ **kwargs: Additional keyword arguments forwarded to super.
55
+ """
56
+ super().__init__(
57
+ *args,
58
+ name=f"{task}",
59
+ **kwargs,
60
+ )
61
+ self.task = task
62
+
63
+
64
+ class GenomeOriginTasks(datasets.GeneratorBasedBuilder):
65
+ VERSION = datasets.Version("1.1.0")
66
+ BUILDER_CONFIG_CLASS = GenomeOriginTasksConfig
67
+ BUILDER_CONFIGS = [GenomeOriginTasksConfig(task=task) for task in _TASKS]
68
+ DEFAULT_CONFIG_NAME = "four_kingdoms"
69
+
70
+ def _info(self):
71
+ return datasets.DatasetInfo(
72
+ description=_DESCRIPTION,
73
+ features=datasets.Features(
74
+ {
75
+ "sequence": datasets.Value("string"),
76
+ "label": datasets.Value("int32"),
77
+ }
78
+ ),
79
+ homepage=_HOMEPAGE,
80
+ license=_LICENSE,
81
+ citation=_CITATION,
82
+ )
83
+
84
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
85
+ """Returns SplitGenerators."""
86
+
87
+ task_dir = dl_manager.download_and_extract(self.config.task)
88
+ # base_url = "https://huggingface.co/datasets/mahdibaghbanzadeh/genome_origin_datasets/resolve/main"
89
+ # task_dir = f"{base_url}/{self.config.task}"
90
+
91
+ train_file = f"{task_dir}/train.txt"
92
+ test_file = f"{task_dir}/test.txt"
93
+
94
+ return [
95
+ datasets.SplitGenerator(
96
+ name=datasets.Split.TRAIN, gen_kwargs={"file_path": train_file}
97
+ ),
98
+ datasets.SplitGenerator(
99
+ name=datasets.Split.TEST, gen_kwargs={"file_path": test_file}
100
+ ),
101
+ ]
102
+
103
+ def _generate_examples(self, file_path):
104
+ """Generates examples from a given file."""
105
+ key = 0
106
+ with open(file_path, "r") as f:
107
+ for seq, label in parse_text(f):
108
+ yield key, {
109
+ "sequence": seq.upper(), # Ensure sequences are uppercase
110
+ "label": int(label), # Convert label to integer
111
+ }
112
+ key += 1