from datasets import DatasetBuilder, GenerateMode from Bio import SeqIO from typing import Any, Dict, List, Tuple from Bio.SeqUtils import gc_fraction import os import gzip class GenomeDataset(DatasetBuilder): VERSION = datasets.Version("1.0.0") def _info(self): return datasets.DatasetInfo( features=datasets.Features({ "DNA_id": datasets.Value("string"), "organism": datasets.Value("string"), "year": datasets.Value("string"), "region_type": datasets.Value("string"), "specific_class": datasets.Value("string"), "product": datasets.Value("string"), "sequence": datasets.Value("string"), "gc_content": datasets.Value("float"), "translation_code": datasets.Value("string"), }) ) def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]: downloaded_files = dl_manager.download_and_extract('https://ftp.ncbi.nih.gov/genbank/') train_files = downloaded_files[:int(len(downloaded_files) * 0.8)] # first 80% for training test_files = downloaded_files[int(len(downloaded_files) * 0.8):] # last 20% for testing return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={"filepaths": train_files} ), datasets.SplitGenerator( name=datasets.Split.TEST, gen_kwargs={"filepaths": test_files} ) ] def _generate_examples(self, filepaths: List[str]) -> Tuple[str, Dict[str, Any]]: for filepath in filepaths: if filepath.endswith(".seq.gz"): with gzip.open(filepath, 'rt') as handle: for record in SeqIO.parse(handle, "genbank"): if 'molecule_type' in record.annotations and record.annotations['molecule_type'] == 'DNA': organism = record.annotations.get('organism', 'unknown') collection_date = record.annotations.get('date', 'unkown') year = collection_date.split('-')[-1] if '-' in collection_date else collection_date for feature in record.features: if feature.type in ['rRNA', 'tRNA','CDS','tmRNA']: region_type = 'coding' product = feature.qualifiers.get('product', ['Unknown'])[0] seq = feature.extract(record.seq) gc_content = gc_fraction(seq) if feature.type == 'CDS': translation = feature.qualifiers.get('translation', [''])[0] specific_class = 'Protein' else: translation = 'NA' specific_class = feature.type elif feature.type == 'regulatory': region_type = feature.type specific_class = feature.qualifiers.get('regulatory_class', ['regulatory'])[0] seq = feature.extract(record.seq) gc_content = gc_fraction(seq) #gene_tag = feature.qualifiers.get('locus_tag', [''])[0] translation = 'NA' product = 'NA' elif feature.type == 'gene': continue else: region_type = feature.type seq = feature.extract(record.seq) gc_content = gc_fraction(seq) specific_class = 'NA' translation = 'NA' product = 'NA' yield record.id, { 'DNA_id': record.id, 'organism': organism, 'year': year, 'region_type': region_type, 'specific_class': specific_class, 'product': product, 'sequence': str(seq), 'gc_content': gc_content, 'translation_code': translation }