| import os |
| import numpy as np |
| import random |
| import torch |
| from torch.utils.data import Dataset, DataLoader |
|
|
| class Hg38VariableDataset(Dataset): |
| def __init__(self, data_dir='data', window_size=10_000): |
| self.data_dir = data_dir |
| self.window_size = window_size |
| self.chromosomes = self._load_chromosomes() |
| self.chromosome_lengths = {k: v.shape[-1] for k, v in self.chromosomes.items()} |
| self.chromosome_names = list(self.chromosomes.keys()) |
| self.nucleotides = np.array(['A', 'T', 'G', 'C', 'N']) |
| |
| def _load_chromosomes(self): |
| chromosomes = {} |
| for filename in sorted(os.listdir(self.data_dir)): |
| if filename.endswith(".npz"): |
| chrom_name = filename.split(".")[0] |
| matrix = np.load(os.path.join(self.data_dir, filename))['data'] |
| chromosomes[chrom_name] = matrix |
| return chromosomes |
|
|
| def select_nucleotide_fast(self, matrix): |
| choices = np.argmax(matrix * np.random.rand(*matrix.shape), axis=0) |
| return ''.join(self.nucleotides[choices]) |
|
|
| def __len__(self): |
| return sum(self.chromosome_lengths.values()) |
|
|
| def __getitem__(self, idx): |
| chrom = random.choice(self.chromosome_names) |
| chrom_length = self.chromosome_lengths[chrom] |
| start = np.random.randint(0, chrom_length - self.window_size) |
| sequence = self.select_nucleotide_fast(self.chromosomes[chrom][:, start:start + self.window_size]) |
|
|
| return { |
| 'chromosome': chrom, |
| 'start': start, |
| 'sequence': sequence |
| } |
|
|
| |
| |
| |
|
|
| |
| |
| |
|
|
|
|
|
|