hdallatorre
commited on
Commit
•
b47b74e
1
Parent(s):
25aa1e9
feat: Commit the configurations file
Browse files
nucleotide_transformer_downstream_tasks.py
ADDED
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 all the downstream tasks from the Nucleotide
|
16 |
+
Transformer paper. There are 18 downstream tasks."""
|
17 |
+
|
18 |
+
from typing import List
|
19 |
+
import datasets
|
20 |
+
from Bio import SeqIO
|
21 |
+
|
22 |
+
# Find for instance the citation on arxiv or on the dataset repo/website
|
23 |
+
_CITATION = """\
|
24 |
+
@article{dalla2023nucleotide,
|
25 |
+
title={The Nucleotide Transformer: Building and Evaluating Robust Foundation Models for Human Genomics},
|
26 |
+
author={Dalla-Torre, Hugo and Gonzalez, Liam and Mendoza-Revilla, Javier and Carranza, Nicolas Lopez and Grzywaczewski, Adam Henryk and Oteri, Francesco and Dallago, Christian and Trop, Evan and Sirelkhatim, Hassan and Richard, Guillaume and others},
|
27 |
+
journal={bioRxiv},
|
28 |
+
pages={2023--01},
|
29 |
+
year={2023},
|
30 |
+
publisher={Cold Spring Harbor Laboratory}
|
31 |
+
}
|
32 |
+
"""
|
33 |
+
|
34 |
+
# You can copy an official description
|
35 |
+
_DESCRIPTION = """\
|
36 |
+
18 classification downstream tasks from the Nucleotide Transformer paper. Each task
|
37 |
+
corresponds to a dataset configuration.
|
38 |
+
"""
|
39 |
+
|
40 |
+
_HOMEPAGE = "https://github.com/instadeepai/nucleotide-transformer"
|
41 |
+
|
42 |
+
_LICENSE = "https://github.com/instadeepai/nucleotide-transformer/LICENSE.md"
|
43 |
+
|
44 |
+
_TASKS = [
|
45 |
+
'H4ac',
|
46 |
+
'H3K36me3',
|
47 |
+
'splice_sites_donors',
|
48 |
+
'splice_sites_acceptors',
|
49 |
+
'H3',
|
50 |
+
'H4',
|
51 |
+
'H3K4me3',
|
52 |
+
'splice_sites_all',
|
53 |
+
'H3K4me1',
|
54 |
+
'H3K14ac',
|
55 |
+
'enhancers_types',
|
56 |
+
'promoter_no_tata',
|
57 |
+
'H3K79me3',
|
58 |
+
'H3K4me2',
|
59 |
+
'promoter_tata',
|
60 |
+
'enhancers',
|
61 |
+
'H3K9ac',
|
62 |
+
'promoter_all'
|
63 |
+
]
|
64 |
+
|
65 |
+
|
66 |
+
class NucleotideTransformerDownstreamTasksConfig(datasets.BuilderConfig):
|
67 |
+
"""BuilderConfig for The Nucleotide Transformer downstream taks dataset."""
|
68 |
+
|
69 |
+
def __init__(self, *args, task: str, **kwargs):
|
70 |
+
"""BuilderConfig downstream tasks dataset.
|
71 |
+
Args:
|
72 |
+
task (:obj:`str`): Task name.
|
73 |
+
**kwargs: keyword arguments forwarded to super.
|
74 |
+
"""
|
75 |
+
super().__init__(
|
76 |
+
*args,
|
77 |
+
name=f'{task}',
|
78 |
+
**kwargs,
|
79 |
+
)
|
80 |
+
self.task = task
|
81 |
+
|
82 |
+
|
83 |
+
class NucleotideTransformerDownstreamTasks(datasets.GeneratorBasedBuilder):
|
84 |
+
"""Genomes from 850 species, filtered and split into chunks of consecutive
|
85 |
+
nucleotides. 50 genomes are taken for test, 50 for validation and 800
|
86 |
+
for training."""
|
87 |
+
|
88 |
+
VERSION = datasets.Version("1.1.0")
|
89 |
+
BUILDER_CONFIG_CLASS = NucleotideTransformerDownstreamTasksConfig
|
90 |
+
BUILDER_CONFIGS = [
|
91 |
+
NucleotideTransformerDownstreamTasksConfig(task=task) for task in _TASKS
|
92 |
+
]
|
93 |
+
DEFAULT_CONFIG_NAME = "enhancers"
|
94 |
+
|
95 |
+
def _info(self):
|
96 |
+
|
97 |
+
features = datasets.Features(
|
98 |
+
{
|
99 |
+
"sequence": datasets.Value("string"),
|
100 |
+
"name": datasets.Value("string"),
|
101 |
+
"label": datasets.Value("int32"),
|
102 |
+
}
|
103 |
+
)
|
104 |
+
return datasets.DatasetInfo(
|
105 |
+
# This is the description that will appear on the datasets page.
|
106 |
+
description=_DESCRIPTION,
|
107 |
+
# This defines the different columns of the dataset and their types
|
108 |
+
features=features,
|
109 |
+
# Homepage of the dataset for documentation
|
110 |
+
homepage=_HOMEPAGE,
|
111 |
+
# License for the dataset if available
|
112 |
+
license=_LICENSE,
|
113 |
+
# Citation for the dataset
|
114 |
+
citation=_CITATION,
|
115 |
+
)
|
116 |
+
|
117 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
118 |
+
|
119 |
+
train_file = dl_manager.download_and_extract(self.config.task + '/train.fna')
|
120 |
+
test_file = dl_manager.download_and_extract(self.config.task + '/test.fna')
|
121 |
+
|
122 |
+
return [
|
123 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN,
|
124 |
+
gen_kwargs={"file": train_file}
|
125 |
+
),
|
126 |
+
datasets.SplitGenerator(name=datasets.Split.TEST,
|
127 |
+
gen_kwargs={"file": test_file}
|
128 |
+
),
|
129 |
+
]
|
130 |
+
|
131 |
+
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
132 |
+
def _generate_examples(self, file):
|
133 |
+
key = 0
|
134 |
+
with open(file, 'rt') as f:
|
135 |
+
fasta_sequences = SeqIO.parse(f, 'fasta')
|
136 |
+
|
137 |
+
for record in fasta_sequences:
|
138 |
+
|
139 |
+
# parse descriptions in the fasta file
|
140 |
+
sequence, name = str(record.seq), str(record.name)
|
141 |
+
label = int(name.split("|")[-1])
|
142 |
+
|
143 |
+
# yield example
|
144 |
+
yield key, {
|
145 |
+
'sequence': sequence,
|
146 |
+
'name': name,
|
147 |
+
'label': label,
|
148 |
+
}
|
149 |
+
key += 1
|