o
    tf                     @  sN   d dl mZ d dlZd dlmZ d dlmZmZ eee	f Z
G dd dZdS )    )annotationsN)bisect_right)IterableTuplec                   @  sv   e Zd ZdZ	d!d"ddZed#ddZed$ddZd$ddZd%ddZ	d&ddZ
d'ddZd(ddZd)ddZd S )*Scalea  
    A bidirectional mapping between a composite genomic coordinate system and
    a partition of that coordinate system into a sequence of bins.

    The partition is a sequence of bins of a fixed size, with the exception of
    the last bin in each chromosome, which may be smaller than the fixed size.
    Bins do not cross chromosome boundaries. The scale provides a mapping from
    a genomic coordinate to the index of the bin in which it falls, and an
    inverse mapping from a bin index to the genomic coordinate of the bin's
    start.

    Parameters
    ----------
    chromsizes : dict | list[tuple]
        A dictionary of chromosome names and lengths or a list of tuples
        of chromosome names and lengths.
    binsize : int, optional
        The size of each bin in the partition in bp (default: 1).

    Notes
    -----
    The genomic coordinates are 0-based and the bins of the partition are
    half-open intervals, i.e. the start coordinate of a bin is included in the
    bin, but the end is not.
       
chromsizes*dict[str, int] | Iterable[tuple[str, int]]binsizeintc                   sp   t |}t|  \}} fdd|D }ttj|dd}|| _|| _|| _t t||d d | _	 | _
d S )Nc                   s   g | ]
}|  d    qS r    ).0lengthr
   r   W/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/higlass/_scale.py
<listcomp>,   s    z"Scale.__init__.<locals>.<listcomp>r   )initial)dictzipitemslist	itertools
accumulate_chrom_names_chrom_offsets_chrom_lengths_map_chrom_offsets_map_binsize)selfr   r
   nameslengthsZlengths_binnedZchrom_offsetsr   r   r   __init__%   s   
zScale.__init__returndict[str, int]c                 C     | j S )zK
        A dictionary of the ordered chromosome names and lengths.
        )r   r    r   r   r   r   4      zScale.chromsizesc                 C  r&   )z>
        The size of each bin in the partition in bp.
        )r   r'   r   r   r   r
   ;   r(   zScale.binsizec                 C  s
   | j d S )Nr   )r   r'   r   r   r   __len__B   s   
zScale.__len__strc                 C  s   d| j  d| j dS )NzScale(chromsizes=z
, binsize=)r   r
   r'   r   r   r   __repr__E   s   zScale.__repr__Iterable[tuple]c                 c  s    d| j fV  d| jfV  d S )Nr   r
   r,   r'   r   r   r   __rich_repr__H   s   zScale.__rich_repr__gposGenomicPositionc                 C  s>   |\}}| j | }| j| }tdt||d }||| j  S )zY
        Returns the index of the bin in which the given genomic position falls.
        r   r   )r   r   maxminr   )r    r0   chromposZchrom_offsetZclenr   r   r   __call__L   s
   

zScale.__call__indexc                 C  sX   | j d }tdt||d }t| j |}| j|d  }|| j |d   }||| j fS )zZ
        Returns the genomic position of the start of the bin at the given index.
        r   r   r   )r   r2   r3   r   r   r   )r    r7   Zn_binsir4   Z
rel_offsetr   r   r   invertV   s   
zScale.invertc                 C  s   t | j|S )zo
        Returns a new scale with the same genomic coordinate system but a
        different bin size.
        )r   r   )r    r
   r   r   r   rebina   s   zScale.rebinNr   )r   r	   r
   r   )r$   r%   )r$   r   )r$   r*   )r$   r.   )r0   r1   r$   r   )r7   r   r$   r1   )r
   r   r$   r   )__name__
__module____qualname____doc__r#   propertyr   r
   r)   r-   r/   r6   r9   r:   r   r   r   r   r   
   s    





r   )
__future__r   r   bisectr   typingr   r   r*   r   r1   r   r   r   r   r   <module>   s    