Create Genome_database.py
Browse files- Genome_database.py +90 -0
Genome_database.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datasets import DatasetBuilder, GenerateMode
|
2 |
+
from Bio import SeqIO
|
3 |
+
from typing import Any, Dict, List, Tuple
|
4 |
+
from Bio.SeqUtils import gc_fraction
|
5 |
+
import os
|
6 |
+
import gzip
|
7 |
+
|
8 |
+
class GenomeDataset(DatasetBuilder):
|
9 |
+
VERSION = datasets.Version("1.0.0")
|
10 |
+
|
11 |
+
def _info(self):
|
12 |
+
return datasets.DatasetInfo(
|
13 |
+
features=datasets.Features({
|
14 |
+
"DNA_id": datasets.Value("string"),
|
15 |
+
"organism": datasets.Value("string"),
|
16 |
+
"year": datasets.Value("string"),
|
17 |
+
"region_type": datasets.Value("string"),
|
18 |
+
"specific_class": datasets.Value("string"),
|
19 |
+
"product": datasets.Value("string"),
|
20 |
+
"sequence": datasets.Value("string"),
|
21 |
+
"gc_content": datasets.Value("float"),
|
22 |
+
"translation_code": datasets.Value("string"),
|
23 |
+
})
|
24 |
+
)
|
25 |
+
|
26 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
27 |
+
downloaded_files = dl_manager.download_and_extract('https://ftp.ncbi.nih.gov/genbank/')
|
28 |
+
train_files = downloaded_files[:int(len(downloaded_files) * 0.8)] # first 80% for training
|
29 |
+
test_files = downloaded_files[int(len(downloaded_files) * 0.8):] # last 20% for testing
|
30 |
+
|
31 |
+
return [
|
32 |
+
datasets.SplitGenerator(
|
33 |
+
name=datasets.Split.TRAIN,
|
34 |
+
gen_kwargs={"filepaths": train_files}
|
35 |
+
),
|
36 |
+
datasets.SplitGenerator(
|
37 |
+
name=datasets.Split.TEST,
|
38 |
+
gen_kwargs={"filepaths": test_files}
|
39 |
+
)
|
40 |
+
]
|
41 |
+
|
42 |
+
def _generate_examples(self, filepaths: List[str]) -> Tuple[str, Dict[str, Any]]:
|
43 |
+
for filepath in filepaths:
|
44 |
+
if filepath.endswith(".seq.gz"):
|
45 |
+
with gzip.open(filepath, 'rt') as handle:
|
46 |
+
for record in SeqIO.parse(handle, "genbank"):
|
47 |
+
if 'molecule_type' in record.annotations and record.annotations['molecule_type'] == 'DNA':
|
48 |
+
organism = record.annotations.get('organism', 'unknown')
|
49 |
+
collection_date = record.annotations.get('date', 'unkown')
|
50 |
+
year = collection_date.split('-')[-1] if '-' in collection_date else collection_date
|
51 |
+
for feature in record.features:
|
52 |
+
if feature.type in ['rRNA', 'tRNA','CDS','tmRNA']:
|
53 |
+
region_type = 'coding'
|
54 |
+
product = feature.qualifiers.get('product', ['Unknown'])[0]
|
55 |
+
seq = feature.extract(record.seq)
|
56 |
+
gc_content = gc_fraction(seq)
|
57 |
+
if feature.type == 'CDS':
|
58 |
+
translation = feature.qualifiers.get('translation', [''])[0]
|
59 |
+
specific_class = 'Protein'
|
60 |
+
else:
|
61 |
+
translation = 'NA'
|
62 |
+
specific_class = feature.type
|
63 |
+
elif feature.type == 'regulatory':
|
64 |
+
region_type = feature.type
|
65 |
+
specific_class = feature.qualifiers.get('regulatory_class', ['regulatory'])[0]
|
66 |
+
seq = feature.extract(record.seq)
|
67 |
+
gc_content = gc_fraction(seq)
|
68 |
+
#gene_tag = feature.qualifiers.get('locus_tag', [''])[0]
|
69 |
+
translation = 'NA'
|
70 |
+
product = 'NA'
|
71 |
+
elif feature.type == 'gene':
|
72 |
+
continue
|
73 |
+
else:
|
74 |
+
region_type = feature.type
|
75 |
+
seq = feature.extract(record.seq)
|
76 |
+
gc_content = gc_fraction(seq)
|
77 |
+
specific_class = 'NA'
|
78 |
+
translation = 'NA'
|
79 |
+
product = 'NA'
|
80 |
+
yield record.id, {
|
81 |
+
'DNA_id': record.id,
|
82 |
+
'organism': organism,
|
83 |
+
'year': year,
|
84 |
+
'region_type': region_type,
|
85 |
+
'specific_class': specific_class,
|
86 |
+
'product': product,
|
87 |
+
'sequence': str(seq),
|
88 |
+
'gc_content': gc_content,
|
89 |
+
'translation_code': translation
|
90 |
+
}
|