Upload human_reference_genome.py
Browse files- human_reference_genome.py +185 -0
human_reference_genome.py
ADDED
@@ -0,0 +1,185 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 human reference genome dataset.."""
|
16 |
+
|
17 |
+
from typing import List
|
18 |
+
import datasets
|
19 |
+
import gzip
|
20 |
+
from Bio import SeqIO
|
21 |
+
import regex as re
|
22 |
+
|
23 |
+
|
24 |
+
# Find for instance the citation on arxiv or on the dataset repo/website
|
25 |
+
_CITATION = """\
|
26 |
+
@article{o2016reference,
|
27 |
+
title={Reference sequence (RefSeq) database at NCBI: current status, taxonomic expansion, and functional annotation},
|
28 |
+
author={O'Leary, Nuala A and Wright, Mathew W and Brister, J Rodney and Ciufo, Stacy and Haddad, Diana and McVeigh, Rich and Rajput, Bhanu and Robbertse, Barbara and Smith-White, Brian and Ako-Adjei, Danso and others},
|
29 |
+
journal={Nucleic acids research},
|
30 |
+
volume={44},
|
31 |
+
number={D1},
|
32 |
+
pages={D733--D745},
|
33 |
+
year={2016},
|
34 |
+
publisher={Oxford University Press}
|
35 |
+
}
|
36 |
+
"""
|
37 |
+
|
38 |
+
# You can copy an official description
|
39 |
+
_DESCRIPTION = """\
|
40 |
+
Genome Reference Consortium Human Build 38 patch release 14 (GRCh38.p14)
|
41 |
+
filtered and split into chunks.
|
42 |
+
"""
|
43 |
+
|
44 |
+
_HOMEPAGE = "https://www.ncbi.nlm.nih.gov/assembly/GCF_000001405.40"
|
45 |
+
|
46 |
+
_LICENSE = "https://www.ncbi.nlm.nih.gov/home/about/policies/"
|
47 |
+
|
48 |
+
_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 |
+
_CHUNK_LENGTH = 6100
|
53 |
+
_OVERLAP = 100
|
54 |
+
_THRESHOLD_FILTER_N = 0.05
|
55 |
+
|
56 |
+
|
57 |
+
def filter_fn(char: str) -> str:
|
58 |
+
"""
|
59 |
+
Transforms any letter different from a base nucleotide into an 'N'.
|
60 |
+
"""
|
61 |
+
if char in {'A', 'T', 'C', 'G'}:
|
62 |
+
return char
|
63 |
+
else:
|
64 |
+
return 'N'
|
65 |
+
|
66 |
+
|
67 |
+
def clean_sequence(seq: str) -> str:
|
68 |
+
"""
|
69 |
+
Process a chunk of DNA to have all letters in upper and restricted to
|
70 |
+
A, T, C, G and N.
|
71 |
+
"""
|
72 |
+
seq = seq.upper()
|
73 |
+
seq = map(filter_fn, seq)
|
74 |
+
seq = ''.join(list(seq))
|
75 |
+
return seq
|
76 |
+
|
77 |
+
|
78 |
+
def continue_loop(split: str, chromosome: str) -> bool:
|
79 |
+
"""
|
80 |
+
Use to associate split and chromosome when looping over fasta file.
|
81 |
+
"""
|
82 |
+
validation_chromosome = '21'
|
83 |
+
test_chromosome = '22'
|
84 |
+
train_chromosomes = set(str(i) for i in range(1, 21))
|
85 |
+
train_chromosomes.update({'X', 'Y'})
|
86 |
+
if split == 'validation' and chromosome == validation_chromosome:
|
87 |
+
return True
|
88 |
+
elif split == 'test' and chromosome == test_chromosome:
|
89 |
+
return True
|
90 |
+
elif split == 'train' and chromosome in train_chromosomes:
|
91 |
+
return True
|
92 |
+
else:
|
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 |
+
|
105 |
+
features = datasets.Features(
|
106 |
+
{
|
107 |
+
"sequence": datasets.Value("string"),
|
108 |
+
"chromosome": datasets.Value("string"),
|
109 |
+
"start_pos": datasets.Value("int32"),
|
110 |
+
"end_pos": datasets.Value("int32"),
|
111 |
+
}
|
112 |
+
)
|
113 |
+
return datasets.DatasetInfo(
|
114 |
+
# This is the description that will appear on the datasets page.
|
115 |
+
description=_DESCRIPTION,
|
116 |
+
# This defines the different columns of the dataset and their types
|
117 |
+
features=features,
|
118 |
+
# Homepage of the dataset for documentation
|
119 |
+
homepage=_HOMEPAGE,
|
120 |
+
# License for the dataset if available
|
121 |
+
license=_LICENSE,
|
122 |
+
# Citation for the dataset
|
123 |
+
citation=_CITATION,
|
124 |
+
)
|
125 |
+
|
126 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
127 |
+
urls_to_download = _URLS
|
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, "split": "train"}),
|
132 |
+
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files, "split": "validation"}),
|
133 |
+
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files, "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 gzip.open(filepath, 'rt') as f:
|
139 |
+
fasta_sequences = SeqIO.parse(f, 'fasta')
|
140 |
+
# regex to filter lines of interest in the FASTA
|
141 |
+
prog = re.compile("NC_\d*.\d* Homo sapiens chromosome (\d*|\w), GRCh38.p14 Primary Assembly")
|
142 |
+
|
143 |
+
key = 0
|
144 |
+
for record in fasta_sequences:
|
145 |
+
|
146 |
+
# parse descriptions in the fasta file
|
147 |
+
sequence, description = str(record.seq), record.description
|
148 |
+
regex_match = prog.match(description)
|
149 |
+
|
150 |
+
if regex_match is not None:
|
151 |
+
|
152 |
+
# get chromosome
|
153 |
+
chromosome = regex_match[1]
|
154 |
+
|
155 |
+
# continue if the chromosome belongs to this split
|
156 |
+
if continue_loop(split=split, chromosome=chromosome):
|
157 |
+
|
158 |
+
# clean chromosome sequence
|
159 |
+
sequence = clean_sequence(sequence)
|
160 |
+
seq_length = len(sequence)
|
161 |
+
|
162 |
+
# split into chunks
|
163 |
+
num_chunks = (seq_length - 2 * _OVERLAP) // _CHUNK_LENGTH
|
164 |
+
sequence = sequence[:(_CHUNK_LENGTH * num_chunks + 2 * _OVERLAP)]
|
165 |
+
seq_length = len(sequence)
|
166 |
+
|
167 |
+
for i in range(num_chunks):
|
168 |
+
# get chunk
|
169 |
+
start_pos = i * _CHUNK_LENGTH
|
170 |
+
end_pos = min(seq_length, (i+1) * _CHUNK_LENGTH + 2 * _OVERLAP)
|
171 |
+
chunk_sequence = sequence[start_pos:end_pos]
|
172 |
+
|
173 |
+
# compute ratio of Ns
|
174 |
+
n_ratio = chunk_sequence.count("N") / len(chunk_sequence)
|
175 |
+
|
176 |
+
# yield chunk only if not too many Ns
|
177 |
+
if n_ratio < _THRESHOLD_FILTER_N:
|
178 |
+
yield key, {
|
179 |
+
'sequence': chunk_sequence,
|
180 |
+
'chromosome': chromosome,
|
181 |
+
'start_pos': start_pos,
|
182 |
+
'end_pos': end_pos
|
183 |
+
}
|
184 |
+
key += 1
|
185 |
+
|