Update human_reference_genome.py
Browse files- human_reference_genome.py +30 -11
human_reference_genome.py
CHANGED
@@ -16,7 +16,6 @@
|
|
16 |
|
17 |
from typing import List
|
18 |
import datasets
|
19 |
-
import gzip
|
20 |
from Bio import SeqIO
|
21 |
import regex as re
|
22 |
|
@@ -49,7 +48,7 @@ _URLS = {
|
|
49 |
f"fasta": "https://ftp.ncbi.nlm.nih.gov/genomes/all/GCF/000/001/405/GCF_000001405.40_GRCh38.p14/GCF_000001405.40_GRCh38.p14_genomic.fna.gz"
|
50 |
}
|
51 |
|
52 |
-
|
53 |
_OVERLAP = 100
|
54 |
_THRESHOLD_FILTER_N = 0.05
|
55 |
|
@@ -93,12 +92,33 @@ def continue_loop(split: str, chromosome: str) -> bool:
|
|
93 |
return False
|
94 |
|
95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
class HumanReferenceGenome(datasets.GeneratorBasedBuilder):
|
97 |
"""Human reference genome, filtered and split into chunks of consecutive
|
98 |
nucleotides. The test set corresponds to chromosome 22, the validation set to
|
99 |
chromosome 21 and all other chromosomes are used for training."""
|
100 |
|
101 |
VERSION = datasets.Version("1.1.0")
|
|
|
|
|
|
|
102 |
|
103 |
def _info(self):
|
104 |
|
@@ -128,13 +148,13 @@ class HumanReferenceGenome(datasets.GeneratorBasedBuilder):
|
|
128 |
downloaded_files = dl_manager.download_and_extract(urls_to_download)
|
129 |
|
130 |
return [
|
131 |
-
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files['fasta'], "split": "train"}),
|
132 |
-
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files['fasta'], "split": "validation"}),
|
133 |
-
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": downloaded_files['fasta'], "split": "test"}),
|
134 |
]
|
135 |
|
136 |
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
137 |
-
def _generate_examples(self, filepath, split):
|
138 |
with open(filepath, 'rt') as f:
|
139 |
fasta_sequences = SeqIO.parse(f, 'fasta')
|
140 |
# regex to filter lines of interest in the FASTA
|
@@ -160,14 +180,14 @@ class HumanReferenceGenome(datasets.GeneratorBasedBuilder):
|
|
160 |
seq_length = len(sequence)
|
161 |
|
162 |
# split into chunks
|
163 |
-
num_chunks = (seq_length - 2 * _OVERLAP) //
|
164 |
-
sequence = sequence[:(
|
165 |
seq_length = len(sequence)
|
166 |
|
167 |
for i in range(num_chunks):
|
168 |
# get chunk
|
169 |
-
start_pos = i *
|
170 |
-
end_pos = min(seq_length, (i+1) *
|
171 |
chunk_sequence = sequence[start_pos:end_pos]
|
172 |
|
173 |
# compute ratio of Ns
|
@@ -182,4 +202,3 @@ class HumanReferenceGenome(datasets.GeneratorBasedBuilder):
|
|
182 |
'end_pos': end_pos
|
183 |
}
|
184 |
key += 1
|
185 |
-
|
|
|
16 |
|
17 |
from typing import List
|
18 |
import datasets
|
|
|
19 |
from Bio import SeqIO
|
20 |
import regex as re
|
21 |
|
|
|
48 |
f"fasta": "https://ftp.ncbi.nlm.nih.gov/genomes/all/GCF/000/001/405/GCF_000001405.40_GRCh38.p14/GCF_000001405.40_GRCh38.p14_genomic.fna.gz"
|
49 |
}
|
50 |
|
51 |
+
_CHUNK_LENGTHS = [6000, 12000]
|
52 |
_OVERLAP = 100
|
53 |
_THRESHOLD_FILTER_N = 0.05
|
54 |
|
|
|
92 |
return False
|
93 |
|
94 |
|
95 |
+
class HumanReferenceGenomeConfig(datasets.BuilderConfig):
|
96 |
+
"""BuilderConfig for The Human Reference Genome."""
|
97 |
+
|
98 |
+
def __init__(self, *args, chunk_length: int, **kwargs):
|
99 |
+
"""BuilderConfig for The Pile.
|
100 |
+
Args:
|
101 |
+
chunk_length (:obj:`int`): Chunk length.
|
102 |
+
**kwargs: keyword arguments forwarded to super.
|
103 |
+
"""
|
104 |
+
num_kbp = int(chunk_length/1000)
|
105 |
+
super().__init__(
|
106 |
+
*args,
|
107 |
+
name="+".join(f'{num_kbp}kbp'),
|
108 |
+
**kwargs,
|
109 |
+
)
|
110 |
+
self.chunk_length = chunk_length
|
111 |
+
|
112 |
+
|
113 |
class HumanReferenceGenome(datasets.GeneratorBasedBuilder):
|
114 |
"""Human reference genome, filtered and split into chunks of consecutive
|
115 |
nucleotides. The test set corresponds to chromosome 22, the validation set to
|
116 |
chromosome 21 and all other chromosomes are used for training."""
|
117 |
|
118 |
VERSION = datasets.Version("1.1.0")
|
119 |
+
BUILDER_CONFIG_CLASS = HumanReferenceGenomeConfig
|
120 |
+
BUILDER_CONFIGS = [HumanReferenceGenomeConfig(chunk_length=chunk_length) for chunk_length in _CHUNK_LENGTHS]
|
121 |
+
DEFAULT_CONFIG_NAME = "6kbp"
|
122 |
|
123 |
def _info(self):
|
124 |
|
|
|
148 |
downloaded_files = dl_manager.download_and_extract(urls_to_download)
|
149 |
|
150 |
return [
|
151 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files['fasta'], "split": "train", "chunk_length": self.config.chunk_length}),
|
152 |
+
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files['fasta'], "split": "validation", "chunk_length": self.config.chunk_length}),
|
153 |
+
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": downloaded_files['fasta'], "split": "test", "chunk_length": self.config.chunk_length}),
|
154 |
]
|
155 |
|
156 |
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
157 |
+
def _generate_examples(self, filepath, split, chunk_length):
|
158 |
with open(filepath, 'rt') as f:
|
159 |
fasta_sequences = SeqIO.parse(f, 'fasta')
|
160 |
# regex to filter lines of interest in the FASTA
|
|
|
180 |
seq_length = len(sequence)
|
181 |
|
182 |
# split into chunks
|
183 |
+
num_chunks = (seq_length - 2 * _OVERLAP) // chunk_length
|
184 |
+
sequence = sequence[:(chunk_length * num_chunks + 2 * _OVERLAP)]
|
185 |
seq_length = len(sequence)
|
186 |
|
187 |
for i in range(num_chunks):
|
188 |
# get chunk
|
189 |
+
start_pos = i * chunk_length
|
190 |
+
end_pos = min(seq_length, (i+1) * chunk_length + 2 * _OVERLAP)
|
191 |
chunk_sequence = sequence[start_pos:end_pos]
|
192 |
|
193 |
# compute ratio of Ns
|
|
|
202 |
'end_pos': end_pos
|
203 |
}
|
204 |
key += 1
|
|