File size: 4,381 Bytes
a9a97c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import json

import datasets

_CITATION = """\
"""
_DESCRIPTION = """\
SwissIPG dataset containing three subsets: GO, IPR, and IPR_GO,
each with training and test sets.
"""


class SwissProtIPG(datasets.GeneratorBasedBuilder):
    BUILDER_CONFIGS = [
        datasets.BuilderConfig(
            name="GO",
            version=datasets.Version("1.0.0"),
            description="GO subset",
        ),
        datasets.BuilderConfig(
            name="IPR",
            version=datasets.Version("1.0.0"),
            description="IPR subset",
        ),
        datasets.BuilderConfig(
            name="IPR_GO",
            version=datasets.Version("1.0.0"),
            description="IPR+GO subset",
        ),
    ]

    def _info(self):
        if self.config.name == "GO":
            features = datasets.Features(
                {
                    "Gene Ontology (molecular function)": datasets.Sequence(
                        {
                            "GO-ID": datasets.Value("string"),
                            "GO-Name": datasets.Value("string"),
                        }
                    ),
                    "sequence": datasets.Value("string"),
                    "instruction": datasets.Value("string"),
                }
            )
        elif self.config.name == "IPR":
            features = datasets.Features(
                {
                    "InterPro": datasets.Sequence(
                        {
                            "InterPro-ID": datasets.Value("string"),
                            "InterPro-Name": datasets.Value("string"),
                            "InterPro-Type": datasets.Value("string"),
                            "Beg": datasets.Value("int32"),
                            "End": datasets.Value("int32"),
                        }
                    ),
                    "sequence": datasets.Value("string"),
                    "instruction": datasets.Value("string"),
                }
            )
        else:  # IPR_GO
            features = datasets.Features(
                {
                    "Gene Ontology (molecular function)": datasets.Sequence(
                        {
                            "GO-ID": datasets.Value("string"),
                            "GO-Name": datasets.Value("string"),
                        }
                    ),
                    "InterPro": datasets.Sequence(
                        {
                            "InterPro-ID": datasets.Value("string"),
                            "InterPro-Name": datasets.Value("string"),
                            "InterPro-Type": datasets.Value("string"),
                            "Beg": datasets.Value("int32"),
                            "End": datasets.Value("int32"),
                        }
                    ),
                    "sequence": datasets.Value("string"),
                    "instruction": datasets.Value("string"),
                }
            )

        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            supervised_keys=None,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        if self.config.name == "GO":
            train_file = dl_manager.download_and_extract("go_train.json")
            test_file = dl_manager.download_and_extract("go_test.json")
        elif self.config.name == "IPR":
            train_file = dl_manager.download_and_extract("ipr_train.json")
            test_file = dl_manager.download_and_extract("ipr_test.json")
        elif self.config.name == "IPR_GO":
            train_file = dl_manager.download_and_extract("ipr_go_train.json")
            test_file = dl_manager.download_and_extract("ipr_go_test.json")
        else:
            raise ValueError(f"Invalid config: {self.config.name}")

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,  # type: ignore
                gen_kwargs={"filepath": train_file},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,  # type: ignore
                gen_kwargs={"filepath": test_file},
            ),
        ]

    def _generate_examples(self, filepath):
        with open(filepath, encoding="utf-8") as f:
            data = json.load(f)
            for i, row in enumerate(data):
                yield i, row