File size: 3,459 Bytes
d2a5d42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import random

import datasets

# You can copy an official description
_DESCRIPTION = """\
A dataset of all autosomal and sex chromosomes sequences from reference assembly GRCh38/hg38 1 and reached a total of 3.2 billion nucleotides.
"""

_HOMEPAGE = "https://www.ncbi.nlm.nih.gov/assembly/GCF_000001405.26"

FILES = ["intervals.jsonl"]

class PubchemSelfies(datasets.GeneratorBasedBuilder):
    """A dataset of all autosomal and sex chromosomes sequences from reference assembly GRCh38/hg38 and reached a total of 3.2 billion nucleotides."""

    VERSION = datasets.Version("1.1.0")

    # You will be able to load one or the other configurations in the following list with
    BUILDER_CONFIG = datasets.BuilderConfig(
        version=VERSION, description="A dataset of all autosomal and sex chromosomes sequences from reference assembly GRCh38/hg38 and reached a total of 3.2 billion nucleotides."
    )

    def _info(self):
        return datasets.DatasetInfo(
            # This is the description that will appear on the datasets page.
            description=_DESCRIPTION,
            # This defines the different columns of the dataset and their types
            features=datasets.Features(
                {
                    "chr": datasets.Value("string"),
                    "description": datasets.Value("string"),
                    "seq": datasets.Value("string"),
                    "split": datasets.Value("string"),
                }
            ),
            # Homepage of the dataset for documentation
            homepage=_HOMEPAGE,
        )

    def _split_generators(self, dl_manager):
        downloaded_files = dl_manager.download(FILES)

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "filename": downloaded_files[0]
                },
            ),
        ]

    # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
    def _generate_examples(self, filename):
        # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
        with open(filename) as jsonl_file:
            for row, line in enumerate(jsonl_file):
                data = json.loads(line)

                # 5% of the time the data is validation so we set the split accordingly
                # This is kind of a hacky but it's so we can load in streaming 
                split = "valid" if random.random() < 0.05 else "train"

                yield row, {
                    "chr": data["chr"],
                    "description": data["description"],
                    "seq": data["seq"],
                    "split": split,
                }