o
    Nrf²  ã                   @   s6   d dl Zd dlZd dlZdd„ Zd	dd„Zdd„ ZdS )
é    Nc                 C   s˜   g }|D ]E}|  d¡}t|d ƒ}t|d ƒ}d}i }g ||< td| ƒD ]}	d| | |	 }
t| || |
ƒ}||  ||
 7  < q%|||fg7 }q|S )a;  
    Generate 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
    Ú.é   é   é   )ÚsplitÚintÚrangeÚget_1D_tiles)ÚfilepathZtile_idsZ	to_returnZtile_idÚpartsÚzoomZxposZ
extra_zoomÚnew_rowsÚjZhigher_xposZold_rows© r   ú`/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/clodius/tiles/bedarcsdb.pyÚtiles   s   
r   r   c                 C   s¼  t | ƒ}t | ¡}| ¡ }|d d|  }|| }	|	||  }
d ||	|
¡}| |¡ ¡ }d ||	|
¡}| |¡ ¡ }tƒ }t 	t
¡}t
tt ||¡dd„ dƒ}d}|D ]‚}z	|d	  d
¡}W n tym   |d	 }Y nw ||v rsqU| |¡ t|ƒ|kr€ nX|d }|d }|d }|d }t||| ƒD ]?}|| }|d | }||k r«||ks³||k rÖ||krÖ||  |d |d |d |d |d |d ||d  d¡dœg7  < q—qU| ¡  |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
    numx: int
        The width of the block of tiles to retrieve

    Returns
    -------
    tiles: {pos: tile_value}
        A set of tiles, indexed by position
    Ú	max_widthr   zþ
    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 <= {} ORDER BY importance
    zþ
    SELECT fromX, toX, fromY, toY, chrOffset, importance, fields, uid
    FROM intervals,position_index
    WHERE
        intervals.id=position_index.id AND
        zoomLevel <= {} AND
        rToY >= {} AND
        rFromY <= {} ORDER BY importance
    c                 S   s
   | d  S )Né   r   )Úxr   r   r   Ú<lambda>j   s   
 zget_1D_tiles.<locals>.<lambda>)Úkeyé   é   zutf-8r   r   r   é   r   é   ú	)ZxStartZxEndZyStartZyEndZ	chrOffsetZ
importanceÚuidÚfields)Úget_2d_tileset_infoÚsqlite3ÚconnectÚcursorÚformatÚexecuteZfetchallÚsetÚcolÚdefaultdictÚlistÚsortedÚitÚchainÚdecodeÚAttributeErrorÚaddÚlenr   r   Úclose)Údb_filer   Z
tile_x_posZnumxZnumyÚtileset_infoÚconnÚcZ
tile_widthZtile_x_start_posZtile_x_end_posÚqueryÚrowsZquery1Zrows1Z	seen_uidsr   Zall_rowsZMAX_ROWSÚrr   Zx_startZx_endZy_startZy_endÚiZtile_x_startZ
tile_x_endr   r   r   r	   -   sr   
ÿøÿø
ÿ
øÿ€ør	   c                 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   r   r   r   r   r   r   )
Z	zoom_stepÚ
max_lengthZassemblyZchrom_namesZchrom_sizesZ	tile_sizeZmax_zoomr   Zmin_posZmax_pos)r   r    r!   r#   Zfetchoner/   )r0   r2   r3   Úrowr1   r   r   r   r   ›   s    
ör   )r   r   )Úcollectionsr%   Ú	itertoolsr)   r   r   r	   r   r   r   r   r   Ú<module>   s    
'n