11from __future__ import annotations
22
3- from typing import Any , Dict , List , Literal , Optional , Sequence , Union
3+ from collections .abc import Sequence
4+ from typing import Any , Literal , Union
45from warnings import warn
56
67import biocframe
@@ -92,13 +93,13 @@ class RangedSummarizedExperiment(SummarizedExperiment):
9293
9394 def __init__ (
9495 self ,
95- assays : Dict [str , Any ] = None ,
96- row_ranges : Optional [ GRangesOrGRangesList ] = None ,
97- row_data : Optional [ biocframe .BiocFrame ] = None ,
98- column_data : Optional [ biocframe .BiocFrame ] = None ,
99- row_names : Optional [ List [ str ]] = None ,
100- column_names : Optional [ List [ str ]] = None ,
101- metadata : Optional [ Union [ Dict [ str , Any ], ut .NamedList ]] = None ,
96+ assays : dict [str , Any ] = None ,
97+ row_ranges : GRangesOrGRangesList | None = None ,
98+ row_data : biocframe .BiocFrame | None = None ,
99+ column_data : biocframe .BiocFrame | None = None ,
100+ row_names : list [ str ] | None = None ,
101+ column_names : list [ str ] | None = None ,
102+ metadata : dict [ str , Any ] | ut .NamedList | None = None ,
102103 _validate : bool = True ,
103104 ) -> None :
104105 """Initialize a `RangedSummarizedExperiment` (RSE) object.
@@ -274,7 +275,7 @@ def __str__(self) -> str:
274275 )
275276 output += f"column_names({ 0 if self ._column_names is None else len (self ._column_names )} ): { ' ' if self ._column_names is None else ut .print_truncated_list (self ._column_names )} \n "
276277
277- output += f"metadata({ str ( len (self .metadata )) } ): { ut .print_truncated_list (list (self .metadata .keys ()), sep = ' ' , include_brackets = False , transform = lambda y : y )} "
278+ output += f"metadata({ len (self .metadata )!s } ): { ut .print_truncated_list (list (self .metadata .keys ()), sep = ' ' , include_brackets = False , transform = lambda y : y )} "
278279
279280 return output
280281
@@ -291,7 +292,7 @@ def get_row_ranges(self) -> GRangesOrGRangesList:
291292 return self ._row_ranges
292293
293294 def set_row_ranges (
294- self , row_ranges : Optional [ GRangesOrGRangesList ] , in_place : bool = False
295+ self , row_ranges : GRangesOrGRangesList | None , in_place : bool = False
295296 ) -> RangedSummarizedExperiment :
296297 """Set new genomic features.
297298
@@ -351,7 +352,7 @@ def start(self) -> np.ndarray:
351352 return self .row_ranges .start
352353
353354 @property
354- def seqnames (self ) -> List [str ]:
355+ def seqnames (self ) -> list [str ]:
355356 """Get sequence or chromosome names.
356357
357358 Returns:
@@ -392,21 +393,18 @@ def seq_info(self) -> SeqInfo:
392393
393394 # rest of them are inherited from BaseSE.
394395
395- def _normalize_row_slice (self , rows : Union [ str , int , bool , Sequence ] ):
396+ def _normalize_row_slice (self , rows : str | int | bool | Sequence ):
396397
397- if isinstance (rows , (GenomicRanges , CompressedGenomicRangesList )):
398- hits = self .row_ranges .find_overlaps (query = rows )
399- rows = hits .get_column ("self_hits" )
400- elif hasattr (rows , "find_overlaps" ):
398+ if isinstance (rows , (GenomicRanges , CompressedGenomicRangesList )) or hasattr (rows , "find_overlaps" ):
401399 hits = self .row_ranges .find_overlaps (query = rows )
402400 rows = hits .get_column ("self_hits" )
403401
404402 return super ()._normalize_row_slice (rows )
405403
406404 def get_slice (
407405 self ,
408- rows : Optional [ Union [ str , int , bool , Sequence ]] ,
409- columns : Optional [ Union [ str , int , bool , Sequence ]] ,
406+ rows : str | int | bool | Sequence | None ,
407+ columns : str | int | bool | Sequence | None ,
410408 ) -> RangedSummarizedExperiment :
411409 """Alias for :py:attr:`~__getitem__`, for back-compatibility."""
412410
@@ -431,7 +429,7 @@ def get_slice(
431429 ######>> range ops <<#######
432430 ############################
433431
434- def coverage (self , shift : int = 0 , width : Optional [ int ] = None , weight : int = 1 ) -> Dict [str , np .ndarray ]:
432+ def coverage (self , shift : int = 0 , width : int | None = None , weight : int = 1 ) -> dict [str , np .ndarray ]:
435433 """Calculate coverage for each chromosome.
436434
437435 Args:
@@ -456,7 +454,7 @@ def nearest(
456454 query : GRangesOrRangeSE ,
457455 select : Literal ["all" , "arbitrary" ] = "all" ,
458456 ignore_strand : bool = False ,
459- ) -> Optional [ List [ Optional [ int ]]] :
457+ ) -> list [ int | None ] | None :
460458 """Search nearest positions both upstream and downstream that overlap with each range in ``query``.
461459
462460 Args:
@@ -496,7 +494,7 @@ def precede(
496494 query : GRangesOrRangeSE ,
497495 select : Literal ["all" , "arbitrary" ] = "all" ,
498496 ignore_strand : bool = False ,
499- ) -> Optional [ List [ Optional [ int ]]] :
497+ ) -> list [ int | None ] | None :
500498 """Search nearest positions only downstream that overlap with each range in ``query``.
501499
502500 Args:
@@ -536,7 +534,7 @@ def follow(
536534 query : GRangesOrRangeSE ,
537535 select : Literal ["all" , "arbitrary" ] = "all" ,
538536 ignore_strand : bool = False ,
539- ) -> Optional [ List [ Optional [ int ]]] :
537+ ) -> list [ int | None ] | None :
540538 """Search nearest positions only upstream that overlap with each range in ``query``.
541539
542540 Args:
@@ -617,7 +615,7 @@ def flank(
617615
618616 def resize (
619617 self ,
620- width : Union [ int , List [int ], np .ndarray ] ,
618+ width : int | list [int ] | np .ndarray ,
621619 fix : Literal ["start" , "end" , "center" ] = "start" ,
622620 ignore_strand : bool = False ,
623621 in_place : bool = False ,
@@ -654,7 +652,7 @@ def resize(
654652 output ._row_ranges = new_ranges
655653 return output
656654
657- def shift (self , shift : Union [ int , List [int ], np .ndarray ] = 0 , in_place : bool = False ) -> RangedSummarizedExperiment :
655+ def shift (self , shift : int | list [int ] | np .ndarray = 0 , in_place : bool = False ) -> RangedSummarizedExperiment :
658656 """Shift all intervals.
659657
660658 ``shift`` may be be negative.
@@ -708,8 +706,8 @@ def promoters(
708706
709707 def restrict (
710708 self ,
711- start : Optional [ Union [ int , List [int ], np .ndarray ]] = None ,
712- end : Optional [ Union [ int , List [int ], np .ndarray ]] = None ,
709+ start : int | list [int ] | np .ndarray | None = None ,
710+ end : int | list [int ] | np .ndarray | None = None ,
713711 keep_all_ranges : bool = False ,
714712 in_place : bool = False ,
715713 ) -> RangedSummarizedExperiment :
@@ -742,9 +740,9 @@ def restrict(
742740
743741 def narrow (
744742 self ,
745- start : Optional [ Union [ int , List [int ], np .ndarray ]] = None ,
746- width : Optional [ Union [ int , List [int ], np .ndarray ]] = None ,
747- end : Optional [ Union [ int , List [int ], np .ndarray ]] = None ,
743+ start : int | list [int ] | np .ndarray | None = None ,
744+ width : int | list [int ] | np .ndarray | None = None ,
745+ end : int | list [int ] | np .ndarray | None = None ,
748746 in_place : bool = False ,
749747 ) -> RangedSummarizedExperiment :
750748 """Narrow genomic positions by provided ``start``, ``width`` and ``end`` parameters.
0 commit comments