
    tf#                     8    d dl Zd dlZd dlZd Zd ZddZddZy)    Nc                 4   t        j                  |       }|j                         }|j                  d      j	                         }|t        |      dk(  r|d   }nd}|d   |d   |d   |d   |d	   |d
   |d   |d   dg|d   g|d}|j                          |S )NSELECT * from tileset_info	       r                        )	zoom_step
max_lengthassemblychrom_nameschrom_sizes	tile_sizemax_zoom	max_widthmin_posmax_posheader)sqlite3connectcursorexecutefetchonelenclose)db_fileconncrowr   tileset_infos         Z/var/www/html/software/conda/envs/higlass/lib/python3.12/site-packages/clodius/db_tiles.pyget_tileset_infor'      s    ??7#DA
))0
1
:
:
<C
3s8q=Q V!fF1v1vVFV3F8L 	JJL    c                    t        j                  |       }|j                         }|j                  d      j	                         }|d   |d   |d   |d   |d   |d   |d   |d	   ddg|d   |d   gd

}|j                          |S )Nr   r   r   r	   r
   r   r   r   r   )
r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r    )r!   r"   r#   r$   r%   s        r&   get_2d_tileset_infor*   "   s    ??7#DA
))0
1
:
:
<CV!fF1v1vVFVq6FCF#L 	JJLr(   c                    t        |       }t        j                  |       }|j                         }|d   d|z  z  }||z  }|||z  z   }	dj	                  |||	      }
|j                  |
      j                         }t        j                  t              }|D ]  }	 |d   j                  d      }|t        j                  |d   |z
  |z        z   }|d   }|d   }t        |||z         D ]P  }||z  }|dz   |z  }|}||k  s||k\  s||xx   |d   |d   |d   |d   ||d	   j                  d
      dgz  cc<   R  |j!                          |S # t        $ r |d   }Y w xY w)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	   z
    SELECT startPos, endPos, chrOffset, importance, fields, uid
    FROM intervals,position_index
    WHERE
        intervals.id=position_index.id AND
        zoomLevel <= {} AND
        rEndPos >= {} AND
        rStartPos <= {}
    r   utf-8r   r   r
   r   	)xStartxEnd	chrOffset
importanceuidfields)r'   r   r   r   formatr   fetchallcoldefaultdictlistdecodeAttributeErrormathfloorrangesplitr    )r!   zoom
tile_x_pos	num_tilesr%   r"   r#   
tile_widthtile_start_postile_end_posqueryrowsnew_rowsrr2   tile_posx_startx_enditile_x_start
tile_x_ends                        r&   	get_tilesrO   8   s   ( $G,L??7#DAk*Q$Y6J*,N!I
$::L 	nl	 
 99U$$&Dt$H 	A$++g&C 

AaD>,AZ+O PPA$!z:	#9: 	Az>La%:-JH#(=" #$A$ !!%&qT&'d""#A$**T"2
' 
"	8 	JJLO7  	A$C	s   EE E 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 ]u  }||	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   |d   j                  d      d	gz  cc<   w   |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	   a9  
    SELECT
        fromX, toX, fromY, toY, chrOffset, importance, fields, uid, intervals.id
    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/   yStartyEndr0   r1   r2   idr3   )r'   r   r   r   r4   r   r5   r6   r7   r8   r9   r:   r=   r>   r    )r!   r?   r@   
tile_y_posnumxnumyr%   r"   r#   rB   tile_x_start_postile_x_end_postile_y_start_postile_y_end_posrE   rF   rG   rH   r2   rJ   rK   y_starty_endrL   jrM   rN   tile_y_start
tile_y_ends                                r&   get_2d_tilesr`      s   0 $G,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$#&"#A$&'djj&6
) $	&N 	JJLOM  	A$C	s   )F  FF)r   )r   r   )collectionsr6   r;   r   r'   r*   rO   r`    r(   r&   <module>rc      s%      8,L^br(   