o
    Nrf                     @   sz   d dl Zd dlZddlmZ dd Zdd Zdd	 Zd
d Zdde	de	de	fddZ
dd Zdd ZdddZdd ZdS )    N   tiles_wrapper_2dc                 C   sv   t | }| }|d }|d |d |d |d |d |d |d |d	 ddg|d |d gd

}|  |S )NzSELECT * from tileset_infor   r                     )
Z	zoom_step
max_lengthZassemblyZchrom_namesZchrom_sizesZ	tile_sizeZmax_zoom	max_widthZmin_posZmax_pos)sqlite3connectcursorexecuteZfetchoneclose)filepathconncrowtileset_info r   ^/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/clodius/tiles/bed2ddb.pyr      s    
r   c                 C   s   t | S N)r   r   r   r   r   get_2d_tileset_info      r   c                 C   s>   t |dkrg S t |d ddk }|rt| |S t| |S )Nr   .r   )lensplittiles_1dtiles_2d)r   tile_idsZis_1dr   r   r   tiles"   s   

r#   c                 C   sH   g }|D ]}| d\}}}||t| t|t|t| fg7 }q|S )a<  
    Generate 1D tiles from this dataset.
    Parameters
    ----------
    filepath: str
        The filename of the sqlite db file
    tile_ids: [str...]
        A list of tile ids of the form
    Returns
    -------
    tiles: [(tile_id, tile_value),...]
        A list of values indexed by the tile position
    r   )r   get_1d_tilesint)r   r"   Z	to_returnZtile_id_zxr   r   r   r    .   s
   (r    zoom
tile_x_pos	num_tilesc                 C   sR  t | }t| }| }|d d|  }|| }|||  }	d| d| d|	 d| d| d|	 d	}
||
 }tt}|D ]`}z	|d
 	d}W n t
yZ   |d
 }Y nw |d }|d }|d }|d }t||| D ]/}|| }|d | }||k r||kr||  |||||d |d ||d ddg7  < qrqB|  |S )a  
    Retrieve a contiguous set of tiles from a 2D db tile file.

    Parameters
    ----------
    filepath: str
        The filename of the sqlite db file
    zoom: int
        The zoom level
    tile_x_pos: int
        The x position of the first tile
    num_tiles: int
        The number of tiles to retrieve

    Returns
    -------
    tiles: {pos: tile_value}
        A set of tiles, indexed by position
    r   r   z
    SELECT fromX, toX, fromY, toY, chrOffset, importance, fields, uid
    FROM intervals, position_index
    WHERE
        intervals.id=position_index.id AND
        zoomLevel <= z AND
        rToX >= z AND
        rFromX <= z
    UNION
    SELECT fromX, toX, fromY, toY, chrOffset, importance, fields, uid
    FROM intervals, position_index
    WHERE
        intervals.id=position_index.id AND
        zoomLevel <= z AND
        rToY >= z AND
        rFromY <= z
    r
   utf-8r   r   r   r   r   r	   	ZxStartZxEndZyStartZyEndZ	chrOffsetZ
importanceuidfields)r   r   r   r   r   fetchallcoldefaultdictlistdecodeAttributeErrorranger   r   )r   r)   r*   r+   Zts_infor   r   
tile_widthtile_x_start_postile_x_end_posqueryrowsnew_rowsrr/   x_startx_endy_starty_enditile_x_start
tile_x_endr   r   r   r$   E   sd   

r$   c                  G      t |  S r   )r$   argsr   r   r   get_1D_tiles   r   rI   c                    s   t | fddS )Nc                    s   t  | ||||f S r   get_2d_tiles)r'   r(   yr   r   r   <lambda>   s    ztiles_2d.<locals>.<lambda>r   )r   r"   r   r   r   r!      s   r!   c                 C   s  t | }t| }| }|d d|  }	|	| }
|
||	  }|	| }|||	  }d||
|||}|| }tt	}|D ]}z	|d 
d}W n tyX   |d }Y nw |d }|d }|d }|d }t||| D ]U}t||| D ]K}||	 }|d |	 }||	 }|d |	 }||k r||kr||k r||kr|||f  |d |d |d |d |d	 |d
 ||d ddg7  < qyqpq@|  |S )a%  
    Retrieve a contiguous set of tiles from a 2D db tile file.

    Parameters
    ----------
    db_file: str
        The filename of the sqlite db file
    zoom: int
        The zoom level
    tile_x_pos: int
        The x position of the first tile
    tile_y_pos: int
        The y position of the first tile
    numx: int
        The width of the block of tiles to retrieve
    numy: int
        The height of the block of tiles to retrieve

    Returns
    -------
    tiles: {pos: tile_value}
        A set of tiles, indexed by position
    r   r   a  
    SELECT fromX, toX, fromY, toY, chrOffset, importance, fields, uid
    FROM intervals,position_index
    WHERE
        intervals.id=position_index.id AND
        zoomLevel <= {} AND
        rToX >= {} AND
        rFromX <= {} AND
        rToY >= {} AND
        rFromY <= {}
    r
   r,   r   r   r   r   r   r	   r-   r.   )r   r   r   r   formatr   r1   r2   r3   r4   r5   r6   r7   r   r   )Zdb_filer)   r*   Z
tile_y_posZnumxZnumyr   r   r   r8   r9   r:   Ztile_y_start_posZtile_y_end_posr;   r<   r=   r>   r/   r?   r@   rA   rB   rC   jrD   rE   Ztile_y_startZ
tile_y_endr   r   r   rK      sf   



rK   c                  G   rF   r   rJ   rG   r   r   r   get_2D_tiles  r   rP   )r   )r   r   )collectionsr2   r   utilsr   r   r   r#   r    r%   r$   rI   r!   rK   rP   r   r   r   r   <module>   s    V
b