File size: 16,845 Bytes
dfced34
16fdcf4
dfced34
 
 
16fdcf4
dfced34
 
 
16fdcf4
 
 
 
 
dfced34
 
16fdcf4
 
dfced34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
299744f
dfced34
 
d4aacab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
330ab17
 
 
 
 
 
 
 
 
 
 
dfced34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec8f16b
8116cdb
 
ec8f16b
dfced34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30358a6
dfced34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
299744f
dfced34
 
8116cdb
 
dfced34
 
 
 
 
b6b01c8
330ab17
dfced34
 
 
330ab17
dfced34
 
330ab17
dfced34
 
 
 
330ab17
dfced34
 
 
330ab17
 
dfced34
330ab17
 
 
dfced34
 
 
299744f
dfced34
 
 
 
 
 
 
 
8116cdb
 
 
 
dfced34
 
 
 
7e72cb6
 
 
dfced34
76fe695
 
dfced34
 
 
 
 
 
 
 
 
 
299744f
dfced34
 
 
 
 
 
 
 
8116cdb
e586f01
8116cdb
 
 
 
dfced34
 
 
 
 
 
 
 
 
d4aacab
64541c9
330ab17
 
 
7895590
d4aacab
330ab17
 
 
 
dfced34
8116cdb
330ab17
 
 
 
 
 
 
 
 
 
 
 
 
8116cdb
dfced34
d4aacab
3225696
 
 
8116cdb
3225696
 
dfced34
 
330ab17
dfced34
 
 
 
3225696
dfced34
 
 
 
8116cdb
 
dfced34
a9fd16c
343ac16
 
 
 
dfced34
 
 
a815b7e
 
 
dfced34
 
 
64541c9
dfced34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98ec642
dfced34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
330ab17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
import gzip
import logging
import os
import re
import shutil
import ssl
import urllib
from abc import ABC, abstractmethod
from pathlib import Path
from typing import List

import datasets
import pandas as pd
from datasets import DatasetInfo
from pyfaidx import Fasta
from tqdm import tqdm

ssl._create_default_https_context = ssl._create_unverified_context

"""
--------------------------------------------------------------------------------------------
Reference Genome URLS:
-------------------------------------------------------------------------------------------
"""
H38_REFERENCE_GENOME_URL = (
    "https://hgdownload.soe.ucsc.edu/goldenPath/hg38/bigZips/" "hg38.fa.gz"
)

"""
--------------------------------------------------------------------------------------------
Task Specific Handlers:
-------------------------------------------------------------------------------------------
"""

logger = logging.getLogger("multi_omics_transcript_expression")
logger.setLevel("INFO")

LABELS_V1 = [
    "Adipose Tissue",
    "Adrenal Gland",
    "Bladder",
    "Blood",
    "Blood Vessel",
    "Brain",
    "Breast",
    "Cervix Uteri",
    "Colon",
    "Esophagus",
    "Fallopian Tube",
    "Heart",
    "Kidney",
    "Liver",
    "Lung",
    "Muscle",
    "Nerve",
    "Ovary",
    "Pancreas",
    "Pituitary",
    "Prostate",
    "Salivary Gland",
    "Skin",
    "Small Intestine",
    "Spleen",
    "Stomach",
    "Testis",
    "Thyroid",
    "Uterus",
    "Vagina",
]

LABELS_V2 = [
    "Adipose_Subcutaneous",
    "Adipose_Visceral (Omentum)",
    "Adrenal Gland",
    "Artery_Aorta",
    "Artery_Coronary",
    "Artery_Tibial",
    "Bladder",
    "Brain_Amygdala",
    "Brain_Anterior cingulate cortex (BA24)",
    "Brain_Caudate (basal ganglia)",
    "Brain_Cerebellar Hemisphere",
    "Brain_Cerebellum",
    "Brain_Cortex",
    "Brain_Frontal Cortex (BA9)",
    "Brain_Hippocampus",
    "Brain_Hypothalamus",
    "Brain_Nucleus accumbens (basal ganglia)",
    "Brain_Putamen (basal ganglia)",
    "Brain_Spinal cord (cervical c-1)",
    "Brain_Substantia nigra",
    "Breast_Mammary Tissue",
    "Cells_Cultured fibroblasts",
    "Cells_EBV-transformed lymphocytes",
    "Cervix_Ectocervix",
    "Cervix_Endocervix",
    "Colon_Sigmoid",
    "Colon_Transverse",
    "Esophagus_Gastroesophageal Junction",
    "Esophagus_Mucosa",
    "Esophagus_Muscularis",
    "Fallopian Tube",
    "Heart_Atrial Appendage",
    "Heart_Left Ventricle",
    "Kidney_Cortex",
    "Kidney_Medulla",
    "Liver",
    "Lung",
    "Minor Salivary Gland",
    "Muscle_Skeletal",
    "Nerve_Tibial",
    "Ovary",
    "Pancreas",
    "Pituitary",
    "Prostate",
    "Skin_Not Sun Exposed (Suprapubic)",
    "Skin_Sun Exposed (Lower leg)",
    "Small Intestine_Terminal Ileum",
    "Spleen",
    "Stomach",
    "Testis",
    "Thyroid",
    "Uterus",
    "Vagina",
    "Whole Blood",
]

# Add after LABELS_V2 definition
LABELS_LIGHT = [
    "Adipose Tissue",
    "Brain",
    "Heart",
    "Liver",
    "Lung",
    "Muscle",
    "Pancreas",
    "Skin",
]

class GenomicLRATaskHandler(ABC):
    """
    Abstract method for the Genomic LRA task handlers. Each handler
    """

    @abstractmethod
    def __init__(self, **kwargs):
        pass

    @abstractmethod
    def get_info(self, description: str) -> DatasetInfo:
        """
        Returns the DatasetInfo for the task
        """
        pass

    def split_generators(
        self, dl_manager, cache_dir_root
    ) -> List[datasets.SplitGenerator]:
        """
        Downloads required files using dl_manager and separates them by split.
        """
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"handler": self, "split": "train"},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST, gen_kwargs={"handler": self, "split": "test"}
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={"handler": self, "split": "test"},
            ),
        ]

    @abstractmethod
    def generate_examples(self, split):
        """
        A generator that yields examples for the specified split.
        """
        pass

    @staticmethod
    def hook(t):
        last_b = [0]

        def inner(b=1, bsize=1, tsize=None):
            """
            b  : int, optional
                Number of blocks just transferred [default: 1].
            bsize  : int, optional
                Size of each block (in tqdm units) [default: 1].
            tsize  : int, optional
                Total size (in tqdm units). If [default: None] remains unchanged.
            """
            if tsize is not None:
                t.total = tsize
            t.update((b - last_b[0]) * bsize)
            last_b[0] = b

        return inner

    def download_and_extract_gz(self, file_url, cache_dir_root):
        """
        Downloads and extracts a gz file into the given cache directory. Returns the full file path
        of the extracted gz file.
        Args:
            file_url: url of the gz file to be downloaded and extracted.
            cache_dir_root: Directory to extract file into.
        """
        file_fname = Path(file_url).stem
        file_complete_path = os.path.join(cache_dir_root, "downloads", file_fname)

        if not os.path.exists(file_complete_path):
            if not os.path.exists(file_complete_path + ".gz"):
                os.makedirs(os.path.dirname(file_complete_path), exist_ok=True)
                with tqdm(
                    unit="B",
                    unit_scale=True,
                    unit_divisor=1024,
                    miniters=1,
                    desc=file_url.split("/")[-1],
                ) as t:
                    urllib.request.urlretrieve(
                        file_url, file_complete_path + ".gz", reporthook=self.hook(t)
                    )
            with gzip.open(file_complete_path + ".gz", "rb") as file_in:
                with open(file_complete_path, "wb") as file_out:
                    shutil.copyfileobj(file_in, file_out)
        return file_complete_path


class TranscriptExpressionHandler(GenomicLRATaskHandler):
    """
    Handler for the Transcript Expression task.
    """

    DEFAULT_LENGTH = 200_000
    DEFAULT_FILTER_OUT_LENGTH = 196_608

    def __init__(
        self,
        sequence_length: int = DEFAULT_LENGTH,
        filter_out_sequence_length: int = DEFAULT_FILTER_OUT_LENGTH,
        expression_method: str = "read_counts_old",
        light_version: bool = False,
        **kwargs,
    ):
        """
        Creates a new handler for the Transcript Expression Prediction Task.
        Args:
            sequence_length: Length of the sequence around the TSS_CAGE start site
            light_version: If True, uses a smaller subset of tissues and fewer samples
        """
        self.reference_genome = None
        self.coordinate_csv_file = None
        self.labels_csv_file = None
        self.light_version = light_version
        self.sequence_length = sequence_length
        self.filter_out_sequence_length = filter_out_sequence_length

        if self.filter_out_sequence_length is not None:
            assert isinstance(self.filter_out_sequence_length, int)
            assert (
                self.sequence_length <= self.filter_out_sequence_length
            ), f"{self.sequence_length=} > {self.filter_out_sequence_length=}"
        assert isinstance(self.sequence_length, int)

    def get_info(self, description: str) -> DatasetInfo:
        """
        Returns the DatasetInfor for the Transcript Expression dataset. Each example
        includes a genomic sequence and a list of label values.
        """
        features = datasets.Features(
            {
                # DNA sequence
                "DNA": datasets.Value("string"),
                # list of expression values in each tissue
                "labels": datasets.Sequence(datasets.Value("float32")),
                "m_t": datasets.Sequence(datasets.Value("float32")),
                "sigma_t": datasets.Sequence(datasets.Value("float32")),
                "m_g": datasets.Sequence(datasets.Value("float32")),
                "sigma_g": datasets.Sequence(datasets.Value("float32")),
                "labels_name": datasets.Sequence(datasets.Value("string")),
                # chromosome number
                "chromosome": datasets.Value(dtype="string"),
                "RNA": datasets.Value("string"),
                "five_prime_utr": datasets.Value("string"),
                "coding_sequence": datasets.Value("string"),
                "three_prime_utr": datasets.Value("string"),
                "Protein": datasets.Value("string"),
                "transcript_id": datasets.Value("string"),
                "gene_id": datasets.Value("string"),
            }
        )
        return datasets.DatasetInfo(
            description=description,
            features=features,
        )

    def split_generators(self, dl_manager, cache_dir_root):
        """
        Separates files by split and stores filenames in instance variables.
        The Transcript Expression dataset requires the reference hg19 genome, coordinate
        csv file,and label csv file to be saved.
        """
        # Manually download the reference genome since there are difficulties when streaming
        reference_genome_file = self.download_and_extract_gz(
            H38_REFERENCE_GENOME_URL, cache_dir_root
        )
        self.reference_genome = Fasta(reference_genome_file, one_based_attributes=False)

        self.df_csv_file = dl_manager.download_and_extract(
            "transcript_expression/GTEx_final.csv"
        )
        self.normalization_values_csv_file = dl_manager.download_and_extract(
            "transcript_expression/normalization_values.csv"
        )

        return super().split_generators(dl_manager, cache_dir_root)

    def generate_examples(self, split):
        """
        A generator which produces examples for the given split, each with a sequence
        and the corresponding labels. The sequences are padded to the correct sequence
        length and standardized before returning.
        """
        df = pd.read_csv(self.df_csv_file)
        df = df.loc[df["chr"] != "chrMT"]
        
        # Use light version labels if specified
        labels_name = LABELS_LIGHT if self.light_version else LABELS_V1

        split_df = df.loc[df["split"] == split]
        
        # For light version, take only a subset of the data
        if self.light_version:
            split_df = split_df.sample(n=min(1000, len(split_df)), random_state=42)

        norm_values_df = pd.read_csv(self.normalization_values_csv_file)
        
        # Select appropriate columns based on version
        label_columns = [f"m_t_{tissue}" for tissue in labels_name]
        m_t = norm_values_df[label_columns].to_numpy().reshape(-1)
        
        label_columns = [f"sigma_t_{tissue}" for tissue in labels_name]
        sigma_t = norm_values_df[label_columns].to_numpy().reshape(-1)
        
        label_columns = [f"m_g_{tissue}" for tissue in labels_name]
        m_g = norm_values_df[label_columns].to_numpy().reshape(-1)
        
        label_columns = [f"sigma_g_{tissue}" for tissue in labels_name]
        sigma_g = norm_values_df[label_columns].to_numpy().reshape(-1)

        key = 0
        for idx, coordinates_row in split_df.iterrows():
            negative_strand = coordinates_row["strand"] == "-"

            if negative_strand:
                start = coordinates_row["end"] - 1
            else:
                start = coordinates_row["start"] - 1  # -1 since vcf coords are 1-based

            chromosome = coordinates_row["chr"]
            labels_row = coordinates_row[labels_name]
            padded_sequence = pad_sequence(
                chromosome=self.reference_genome[chromosome],
                start=start,
                sequence_length=self.sequence_length,
                negative_strand=negative_strand,
                filter_out_sequence_length=self.filter_out_sequence_length,
            )
            if padded_sequence:
                yield key, {
                    "transcript_id": coordinates_row["transcript_id_gtex"],
                    "gene_id": coordinates_row["gene_id_gtex"],
                    "labels_name": labels_name,
                    "labels": labels_row.to_numpy(),
                    "m_t": m_t,
                    "sigma_t": sigma_t,
                    "m_g": m_g,
                    "sigma_g": sigma_g,
                    "DNA": standardize_sequence(padded_sequence),
                    "chromosome": re.sub("chr", "", chromosome),
                    "RNA": coordinates_row["RNA"],
                    "five_prime_utr": coordinates_row["5UTR"],
                    "coding_sequence": coordinates_row["CDS"],
                    "three_prime_utr": coordinates_row["3UTR"],
                    "Protein": coordinates_row["Protein"],
                }
                key += 1
        logger.info(f"filtering out {len(split_df)-key} " f"elements from the dataset")


"""
--------------------------------------------------------------------------------------------
Dataset loader:
-------------------------------------------------------------------------------------------
"""

_DESCRIPTION = """
Dataset for benchmark of genomic deep learning models. 
"""


# define dataset configs
class GenomicsLRAConfig(datasets.BuilderConfig):
    """
    BuilderConfig.
    """

    def __init__(self, *args, **kwargs):  # type: ignore
        """BuilderConfig for the location tasks dataset.
        Args:
            **kwargs: keyword arguments forwarded to super.
        """
        super().__init__()
        self.handler = TranscriptExpressionHandler(**kwargs)


# DatasetBuilder
class GenomicsLRATasks(datasets.GeneratorBasedBuilder):
    """
    Tasks to annotate human genome.
    """

    VERSION = datasets.Version("1.1.0")
    BUILDER_CONFIG_CLASS = GenomicsLRAConfig

    def _info(self) -> DatasetInfo:
        return self.config.handler.get_info(description=_DESCRIPTION)

    def _split_generators(
        self, dl_manager: datasets.DownloadManager
    ) -> List[datasets.SplitGenerator]:
        """
        Downloads data files and organizes it into train/test/val splits
        """
        return self.config.handler.split_generators(dl_manager, self._cache_dir_root)

    def _generate_examples(self, handler, split):
        """
        Read data files and create examples(yield)
        Args:
            handler: The handler for the current task
            split: A string in ['train', 'test', 'valid']
        """
        yield from handler.generate_examples(split)


"""
--------------------------------------------------------------------------------------------
Global Utils:
-------------------------------------------------------------------------------------------
"""


def standardize_sequence(sequence: str):
    """
    Standardizes the sequence by replacing all unknown characters with N and
    converting to all uppercase.
    Args:
        sequence: genomic sequence to standardize
    """
    pattern = "[^ATCG]"
    # all characters to upper case
    sequence = sequence.upper()
    # replace all characters that are not A,T,C,G with N
    sequence = re.sub(pattern, "N", sequence)
    return sequence


def pad_sequence(
    chromosome,
    start,
    sequence_length,
    negative_strand=False,
    filter_out_sequence_length=None,
):
    """
    Extends a given sequence to length sequence_length. If
    padding to the given length is outside the gene, returns
    None.
    Args:
        chromosome: Chromosome from pyfaidx extracted Fasta.
        start: Start index of original sequence.
        sequence_length: Desired sequence length. If sequence length is odd, the
            remainder is added to the end of the sequence.
        end: End index of original sequence. If no end is specified, it creates a
            centered sequence around the start index.
        negative_strand: If negative_strand, returns the reverse compliment of the sequence
    """

    pad = sequence_length // 2
    end = start + pad + (sequence_length % 2)
    start = start - pad

    if filter_out_sequence_length is not None:
        filter_out_pad = filter_out_sequence_length // 2
        filter_out_end = start + filter_out_pad + (filter_out_sequence_length % 2)
        filter_out_start = start - filter_out_pad

        if filter_out_start < 0 or filter_out_end >= len(chromosome):
            return

    if start < 0 or end >= len(chromosome):
        return

    if negative_strand:
        return chromosome[start:end].reverse.complement.seq
    return chromosome[start:end].seq