|
| 1 | +import torch |
| 2 | +import torch.nn.functional as F |
| 3 | + |
| 4 | +def exists(val): |
| 5 | + return val is not None |
| 6 | + |
| 7 | +def identity(t): |
| 8 | + return t |
| 9 | + |
| 10 | +def cast_list(t): |
| 11 | + return t if isinstance(t, list) else [t] |
| 12 | + |
| 13 | +def str_to_seq_indices(seq_strs, padding = '.'): |
| 14 | + seq_strs = cast_list(seq_strs) |
| 15 | + char_to_index_map = {'a': 0, 'c': 1, 'g': 2, 't': 3, 'n': 4, padding: -1} |
| 16 | + seq_strs = map(lambda x: x.lower(), seq_strs) |
| 17 | + seq_indices = list(map(lambda seq_str: torch.Tensor(list(map(lambda char: char_to_index_map[char], seq_str))), seq_strs)) |
| 18 | + return torch.stack(seq_indices).long() |
| 19 | + |
| 20 | +def seq_indices_to_one_hot(t, padding = -1): |
| 21 | + is_padding = t == padding |
| 22 | + t = t.clamp(min = 0) |
| 23 | + one_hot = F.one_hot(t, num_classes = 5) |
| 24 | + out = one_hot[..., :4].float() |
| 25 | + out = out.masked_fill(is_padding[..., None], 0.25) |
| 26 | + return out |
| 27 | + |
| 28 | +# processing bed files |
| 29 | + |
| 30 | +import pandas as pd |
| 31 | +from pathlib import Path |
| 32 | +from pyfaidx import Fasta |
| 33 | +from torch.utils.data import Dataset |
| 34 | + |
| 35 | +class GenomeIntervalDataset(Dataset): |
| 36 | + def __init__( |
| 37 | + self, |
| 38 | + bed_file, |
| 39 | + fasta_file, |
| 40 | + context_length = None, |
| 41 | + filter_df_fn = identity |
| 42 | + ): |
| 43 | + super().__init__() |
| 44 | + bed_path = Path(bed_file) |
| 45 | + fasta_file = Path(fasta_file) |
| 46 | + |
| 47 | + assert bed_path.exists(), 'path to .bed file must exist' |
| 48 | + assert fasta_file.exists(), 'path to fasta file must exist' |
| 49 | + |
| 50 | + df = pd.read_csv(str(bed_path), sep = '\t', header = None, names = ['chr', 'start', 'end', 'type']) |
| 51 | + df = filter_df_fn(df) |
| 52 | + |
| 53 | + self.df = df |
| 54 | + self.seqs = Fasta(str(fasta_file)) |
| 55 | + self.context_length = context_length |
| 56 | + |
| 57 | + def __len__(self): |
| 58 | + return len(self.df) |
| 59 | + |
| 60 | + def __getitem__(self, ind): |
| 61 | + interval = self.df.iloc[ind] |
| 62 | + chr_name, start, end = (interval.chr, interval.start, interval.end) |
| 63 | + interval_length = end - start |
| 64 | + |
| 65 | + chromosome = self.seqs[chr_name] |
| 66 | + chromosome_length = len(chromosome) |
| 67 | + |
| 68 | + left_padding = right_padding = 0 |
| 69 | + |
| 70 | + if exists(self.context_length) and interval_length < self.context_length: |
| 71 | + extra_seq = self.context_length - interval_length |
| 72 | + |
| 73 | + extra_left_seq = extra_seq // 2 |
| 74 | + extra_right_seq = extra_seq - extra_left_seq |
| 75 | + |
| 76 | + start -= extra_left_seq |
| 77 | + end += extra_right_seq |
| 78 | + |
| 79 | + if start < 0: |
| 80 | + left_padding = -start |
| 81 | + start = 0 |
| 82 | + |
| 83 | + if end > chromosome_length: |
| 84 | + right_padding = end - chromosome_length |
| 85 | + end = chromosome_length |
| 86 | + |
| 87 | + seq = ('.' * left_padding) + str(chromosome[start:end]) + ('.' * right_padding) |
| 88 | + seq_indices = str_to_seq_indices(seq) |
| 89 | + seq_onehot = seq_indices_to_one_hot(seq_indices) |
| 90 | + return seq_onehot.squeeze(0) |
0 commit comments