etrop commited on
Commit
a2e188f
1 Parent(s): 0df0d3b

Update genomics-long-range-benchmark.py

Browse files
Files changed (1) hide show
  1. genomics-long-range-benchmark.py +114 -1
genomics-long-range-benchmark.py CHANGED
@@ -527,4 +527,117 @@ class VariantEffectPredictionHandler(GenomicLRATaskHandler):
527
  "alt_forward_sequence": standardize_sequence(alt_forward),
528
  "distance_to_nearest_tss": distance
529
  }
530
- key += 1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
527
  "alt_forward_sequence": standardize_sequence(alt_forward),
528
  "distance_to_nearest_tss": distance
529
  }
530
+ key += 1
531
+
532
+ """
533
+ --------------------------------------------------------------------------------------------
534
+ Dataset loader:
535
+ -------------------------------------------------------------------------------------------
536
+ """
537
+
538
+ _DESCRIPTION = """
539
+ Dataset for benchmark of genomic deep learning models.
540
+ """
541
+
542
+ _TASK_HANDLERS = {
543
+ "cage_prediction": CagePredictionHandler,
544
+ "bulk_rna_expression": BulkRnaExpressionHandler,
545
+ "variant_effect_gene_expression": VariantEffectPredictionHandler,
546
+ }
547
+
548
+
549
+ # define dataset configs
550
+ class GenomicsLRAConfig(datasets.BuilderConfig):
551
+ """
552
+ BuilderConfig.
553
+ """
554
+
555
+ def __init__(self, *args, task_name: str, **kwargs): # type: ignore
556
+ """BuilderConfig for the location tasks dataset.
557
+ Args:
558
+ **kwargs: keyword arguments forwarded to super.
559
+ """
560
+ super().__init__()
561
+ self.handler = _TASK_HANDLERS[task_name](task_name=task_name,**kwargs)
562
+
563
+
564
+ # DatasetBuilder
565
+ class GenomicsLRATasks(datasets.GeneratorBasedBuilder):
566
+ """
567
+ Tasks to annotate human genome.
568
+ """
569
+
570
+ VERSION = datasets.Version("1.1.0")
571
+ BUILDER_CONFIG_CLASS = GenomicsLRAConfig
572
+
573
+ def _info(self) -> DatasetInfo:
574
+ return self.config.handler.get_info(description=_DESCRIPTION)
575
+
576
+ def _split_generators(
577
+ self, dl_manager: datasets.DownloadManager
578
+ ) -> List[datasets.SplitGenerator]:
579
+ """
580
+ Downloads data files and organizes it into train/test/val splits
581
+ """
582
+ return self.config.handler.split_generators(dl_manager, self._cache_dir_root)
583
+
584
+ def _generate_examples(self, handler, split):
585
+ """
586
+ Read data files and create examples(yield)
587
+ Args:
588
+ handler: The handler for the current task
589
+ split: A string in ['train', 'test', 'valid']
590
+ """
591
+ yield from handler.generate_examples(split)
592
+
593
+
594
+ """
595
+ --------------------------------------------------------------------------------------------
596
+ Global Utils:
597
+ -------------------------------------------------------------------------------------------
598
+ """
599
+
600
+
601
+ def standardize_sequence(sequence: str):
602
+ """
603
+ Standardizes the sequence by replacing all unknown characters with N and
604
+ converting to all uppercase.
605
+ Args:
606
+ sequence: genomic sequence to standardize
607
+ """
608
+ pattern = "[^ATCG]"
609
+ # all characters to upper case
610
+ sequence = sequence.upper()
611
+ # replace all characters that are not A,T,C,G with N
612
+ sequence = re.sub(pattern, "N", sequence)
613
+ return sequence
614
+
615
+
616
+ def pad_sequence(chromosome, start, sequence_length, end=None, negative_strand=False):
617
+ """
618
+ Extends a given sequence to length sequence_length. If
619
+ padding to the given length is outside the gene, returns
620
+ None.
621
+ Args:
622
+ chromosome: Chromosome from pyfaidx extracted Fasta.
623
+ start: Start index of original sequence.
624
+ sequence_length: Desired sequence length. If sequence length is odd, the
625
+ remainder is added to the end of the sequence.
626
+ end: End index of original sequence. If no end is specified, it creates a
627
+ centered sequence around the start index.
628
+ negative_strand: If negative_strand, returns the reverse compliment of the sequence
629
+ """
630
+ if end:
631
+ pad = (sequence_length - (end - start)) // 2
632
+ start = start - pad
633
+ end = end + pad + (sequence_length % 2)
634
+ else:
635
+ pad = sequence_length // 2
636
+ end = start + pad + (sequence_length % 2)
637
+ start = start - pad
638
+
639
+ if start < 0 or end >= len(chromosome):
640
+ return
641
+ if negative_strand:
642
+ return chromosome[start:end].reverse.complement.seq
643
+ return chromosome[start:end].seq