o
    Nrf  ã                   @   s0   d dl Z dd„ Zdd„ Zddd„Zdd	d
„ZdS )é    Nc                 C   sÜ   t  | ¡}| ¡ }| d¡ ¡ }tt|jŽ ƒ}d|vrd}n	t|| 	d¡ ƒ}d|vr.d}n|| 	d¡ }|d |d |d |d |d	 |d
 dg|d g||t
t|d  d¡dd„ |d  d¡D ƒƒƒddœ}| ¡  |S )NzSELECT * from tileset_infoÚversioné   ÚheaderÚ r   é   é   é   é   é   ú	c                 S   s   g | ]}t |ƒ‘qS © )Úint)Ú.0Úcsr   r   ú\/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/clodius/tiles/beddb.pyÚ
<listcomp>"   s    z tileset_info.<locals>.<listcomp>é   Ú2)Z	zoom_stepÚ
max_lengthZassemblyZ	tile_sizeZmax_zoomÚ	max_widthZmin_posZmax_posr   r   Z
chromsizesZinfo_version)Úsqlite3ÚconnectÚcursorÚexecuteZfetchoneÚnextÚzipÚdescriptionr   ÚindexÚlistÚsplitÚclose)Údb_fileÚconnr   ÚrowZcolnamesr   r   Úts_infor   r   r   Útileset_info   s6   
&ÿòr%   c                 C   s–   g }|D ]D}|  d¡d }|  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
    ú|r   Ú.r   r   )r   r   ÚrangeÚget_1D_tiles)ÚfilepathZtile_idsZ	to_returnZtile_idZtile_no_optionsÚpartsÚzoomZxposZ
extra_zoomÚnew_rowsÚjZhigher_xposZold_rowsr   r   r   Útiles+   s   

r/   r   c              
   C   sh  t | ƒ}|d }t | ¡}| ¡ }|d d|  }|| }	|	||  }
d ||	|
¡}|dkr5d ||	|
¡}|dkr@d ||	|
¡}| |¡ ¡ }g }|D ]b}z	|d  d	¡}W n tyc   |d }Y nw |d
 }|d }t	||| ƒD ]9}|| }|d | }||k r¬||kr¬|d
 |d |d |d ||d  
d¡dœ}|dkr§|d |d< ||g7 }qsqK| ¡  |S )a™  
    Retrieve a contiguous set of tiles from a db tile file.

    Parameters
    ----------
    db_file: str
        The filename of the sqlite db file
    zoom: int
        The zoom level
    tile_x_pos: int
        The 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   r   úê
    SELECT startPos, endPos, chrOffset, importance, fields, uid
    FROM intervals,position_index
    WHERE
        intervals.id=position_index.id AND
        zoomLevel <= {} AND
        rEndPos >= {} AND
        rStartPos <= {}
    á3  
        SELECT startPos, endPos, chrOffset, importance, fields, uid
        FROM intervals,position_index
        WHERE
            intervals.id=position_index.id AND
            rStartZoomLevel <= {} AND
            rEndZoomLevel >= 0 AND
            rEndPos >= {} AND
            rStartPos <= {}
        r
   á9  
        SELECT startPos, endPos, chrOffset, importance, fields, uid, name
        FROM intervals,position_index
        WHERE
            intervals.id=position_index.id AND
            rStartZoomLevel <= {} AND
            rEndZoomLevel >= 0 AND
            rEndPos >= {} AND
            rStartPos <= {}
        r   úutf-8r   r   r   r   ©ZxStartZxEndZ	chrOffsetZ
importanceÚuidÚfieldsr   Úname)r%   r   r   r   Úformatr   ÚfetchallÚdecodeÚAttributeErrorr(   r   r    )r!   r,   Z
tile_x_posZ	num_tilesr$   r   r"   ÚcZ
tile_widthZtile_start_posZtile_end_posÚqueryÚrowsr-   Úrr5   Zx_startZx_endÚiZtile_x_startZ
tile_x_endÚto_addr   r   r   r)   V   sh   
ÿø	ÿ÷	ÿ÷ÿú	
€ïr)   c              	   C   s  t | ƒ}|d }t | ¡}| ¡ }d}d |||¡}	|dkr%d |||¡}	|dkr0d |||¡}	|dur;|	d	 |¡7 }	| |	¡ ¡ }
g }|
D ]>}z	|d
  d¡}W n ty^   |d
 }Y nw |d |d |d |d ||d  	d¡dœ}|dkr|d |d< ||g7 }qF| 
¡  |S )aP  
    List the entries between start and end

    Parameters
    ----------
    db_file: str
        The filename of the sqlite db file
    start_pos: int
        The start position from where to retrieve data
    end_pos: int
        The end position to get data
    max_entries: int
        The maximum number of results to return
    r   i † r0   r   r1   r
   r2   Nz	 LIMIT {}r   r3   r   r   r   r   r4   r   r7   )r%   r   r   r   r8   r   r9   r:   r;   r   r    )r!   ÚstartÚendZmax_entriesr$   r   r"   r<   r,   r=   r>   r-   r?   r5   rA   r   r   r   Ú
list_itemsÅ   sX   
ÿø	ÿ÷	ÿ÷ÿú	rD   )r   )N)r   r%   r/   r)   rD   r   r   r   r   Ú<module>   s
    '
+o