
    tf                     h    d dl Zd dlZddlmZ d Zd Zd Zd Zdde	d	e	d
e	fdZ
d Zd ZddZd Zy)    N   tiles_wrapper_2dc                    t        j                  |       }|j                         }|j                  d      j	                         }|d   |d   |d   |d   |d   |d   |d   |d	   ddg|d   |d   gd

}|j                          |S )NzSELECT * from tileset_infor   r                     )
	zoom_step
max_lengthassemblychrom_nameschrom_sizes	tile_sizemax_zoom	max_widthmin_posmax_pos)sqlite3connectcursorexecutefetchoneclose)filepathconncrowtileset_infos        _/var/www/html/software/conda/envs/higlass/lib/python3.12/site-packages/clodius/tiles/bed2ddb.pyr!   r!      s    ??8$DA
))0
1
:
:
<CV!fF1v1vVFVq6FCF#L 	JJL    c                     t        |       S N)r!   )r   s    r"   get_2d_tileset_infor&      s    !!r#   c                     t        |      dk(  rg S t        |d   j                  d            dk  }|rt        | |      S t        | |      S )Nr   .r	   )lensplittiles_1dtiles_2d)r   tile_idsis_1ds      r"   tilesr/   "   sN    
8}	!!#&'!+E(++Hh''r#   c           
          g }|D ]H  }|j                  d      \  }}}||t        | t        |      t        |            t        |         fgz  }J |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-   	to_returntile_id_zxs          r"   r+   r+   .   sb     I Q--$1awXs1vs1v Fs1v NOPP	Q r#   zoom
tile_x_pos	num_tilesc                    t        |       }t        j                  |       }|j                         }|d   d|z  z  }||z  }|||z  z   }	d| d| d|	 d| d| d|	 d	}
|j	                  |
      j                         }t        j                  t              }|D ]  }	 |d
   j                  d      }|d   }|d   }|d   }|d   }t        |||z         D ]J  }||z  }|dz   |z  }||k  s||k\  s||xx   |||||d   |d   ||d   j                  d      dgz  cc<   L  |j                          |S # t        $ r |d
   }Y w xY w)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   	xStartxEndyStartyEnd	chrOffset
importanceuidfields)r!   r   r   r   r   fetchallcoldefaultdictlistdecodeAttributeErrorranger*   r   )r   r8   r9   r:   ts_infor   r   
tile_widthtile_x_start_postile_x_end_posqueryrowsnew_rowsrrE   x_startx_endy_starty_enditile_x_start
tile_x_ends                         r"   r1   r1   E   s   ( 8$G??8$DA%T	1J!J.%i)?@N
 V !" #!" # V !" #!" #E$ 99U$$&Dt$H 	A$++g&C A$!A$!z:	#9: 	Az>La%:-J#(=") %") %%&qT&'d""#A$**T"2	  	: 	JJLO9  	A$C	s   D44EEc                      t        |  S r%   )r1   argss    r"   get_1D_tilesr`          r#   c                 "     t        | fd      S )Nc                 *    t        | ||      ||f   S r%   get_2d_tiles)r6   r7   yr   s      r"   <lambda>ztiles_2d.<locals>.<lambda>   s    ,xAq"A1a&"I r#   r   )r   r-   s   ` r"   r,   r,      s    I r#   c                     t        |       }t        j                  |       }|j                         }|d   d|z  z  }	|	|z  }
|
||	z  z   }|	|z  }|||	z  z   }dj	                  ||
|||      }|j                  |      j                         }t        j                  t              }|D ]  }	 |d   j                  d      }|d   }|d   }|d   }|d   }t        |||z         D ]  }t        |||z         D ]q  }||	z  }|dz   |	z  }||	z  }|dz   |	z  }||k  s#||k\  s)||k  s/||k\  s5|||fxx   |d   |d   |d   |d   |d	   |d
   ||d   j                  d      dgz  cc<   s   |j                          |S # t        $ r |d   }Y w xY w)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   rG   rH   rI   rJ   rK   rL   rM   r*   r   )db_filer8   r9   
tile_y_posnumxnumyr!   r   r   rO   rP   rQ   tile_y_start_postile_y_end_posrR   rS   rT   rU   rE   rV   rW   rX   rY   rZ   jr[   r\   tile_y_start
tile_y_ends                                r"   re   re      s   0 'w/L??7#DAk*Q$Y6J!J.%
):;N!J.%
):;N
 	0@.	 
 99U$$&Dt$H %	A$++g&C A$!A$!z:#45 	A:zD'89  :~!ez1
 :~!ez1
 j(-*,- aV$&'d$%aD&'d$%aD)*1*+A$#&&'djj&6	) $	%L 	JJLOK  	A$C	s   )E<<FFc                      t        |  S r%   rd   r^   s    r"   get_2D_tilesrt     ra   r#   )r   )r   r   )collectionsrH   r   utilsr   r!   r&   r/   r+   r2   r1   r`   r,   re   rt    r#   r"   <module>rx      sW      #."	(.S S# S# Sl_Dr#   