[docs]defcreate_np_interval_vector(intervals:"IRanges",with_reverse_map:bool=False,force_size:Optional[int]=None,dont_sum:bool=False,value:Union[int,float]=1,)->Tuple[np.ndarray,Optional[List]]:"""Represent intervals and calculate coverage. Args: intervals: Input intervals. with_reverse_map: Return map of indices? Defaults to False. force_size: Force size of the array. dont_sum: Do not sum. Defaults to False. value: Default value to increment. Defaults to 1. Returns: A numpy array representing coverage from the intervals and optionally the index map. """iflen(intervals)==0:returnnp.zeros(0),Nonemax_end=force_sizeifmax_endisNone:max_end=intervals.get_end().max()else:max_end+=1_type=np.int32ifisinstance(value,float):_type=np.float32cov=np.zeros(max_end,dtype=_type)revmap=Noneifwith_reverse_map:revmap=[[]for_inrange(max_end)]counter=0forname,rowinintervals:_start=row.start[0]_end=row.end[0]ifdont_sum:cov[_start:_end]=valueelse:cov[_start:_end]+=valueifwith_reverse_map:_=[revmap[x].append(nameifnameisnotNoneelsecounter+1)forxinrange(_start,_end)]counter+=1returncov[1:],revmap
[docs]defcalc_gap_and_overlap(first:Tuple[int,int],second:Tuple[int,int])->Tuple[Optional[int],Optional[int]]:"""Calculate gap and/or overlap between two intervals. Args: first: Interval containing start and end positions. `end` is non-inclusive. second: Interval containing start and end positions. `end` is non-inclusive. """ifmin(first[1],second[1])>max(first[0],second[0]):_overlap=min(first[1],second[1])-max(first[0],second[0])return(None,_overlap)_gap=Noneifsecond[0]>=first[1]:_gap=second[0]-first[1]eliffirst[0]>=second[1]:_gap=first[0]-second[1]return(_gap,None)