
    tf#                     P    d dl Z d dlZd dlZd dlZd dlZddlmZ d Zd Z	d Z
d Zy)    N   )abs2genomicc                    t        j                  d      }|j                  |j                  }}g }|D ]3  }|j	                  d      dd D cg c]  }t        |       }}t        | |      }	t        j                  |	      j                         }
|	j                  r|	j                         nd}|	j                  r|	j                         nd}|
 xr ||kD  xr ||k  }|	j                  |rt         j                  nt         j                        }	t        j                  |	j                               j!                  d      }||rdnd|	j"                  d}|j%                  ||f       6 |S c c}w )	a   
    Retrieve multiple multivec tiles from tids.
    ----------
    filename: string
        The multires file containing the multivec data
    tile_ids: [str,...]
        A list of tile_ids (e.g. xyx.0.0) identifying the tiles
        to be retrieved
    float16.r      r   zutf-8float32)densedtypeshape)npfinfominmaxsplitintget_single_tileisnananysizeastyper   r	   base64	b64encoderaveldecoder   append)filenametile_idsf16f16_minf16_maxgenerated_tilestile_iditile_posmahas_nanma_maxma_minuse_f16	ma_base64
tile_values                   `/var/www/html/software/conda/envs/higlass/lib/python3.12/site-packages/clodius/tiles/multivec.pytilesr.      s6    ((9
CwwWGO 6$+MM#$6q$;<qCF<<Xx0((2,""$WW!WW!+I6G#3#H8HYYWrzz"**=$$RXXZ077@	")YyXX


 	456   =s   E+c           	         t        |       }t        j                  | d      }|d   |d      }|d   }|d   |z  |z  }|||z  z   }t        t	        |d   d   |d   d               }t        ||||||d	         }	t        |	      |d   k  rAt        j                  |	t        j                  |d   t        |	      z
  |d	   d   f      g      }	|j                          |	j                  S )
z
    Retrieve a single multivec tile from a multires file
    Parameters
    ----------
    filename: string
        The multires file containing the multivec data
    tile_pos: (z, x)
        The zoom level and position of this tile
    rresolutionsr   	tile_sizer   chromsnamelengthr   )tileset_infoh5pyFilelistzipget_tilelenr   vstackzeroscloseT)
r   r%   tsinfof
resolutionr2   
tile_starttile_end
chromsizesr
   s
             r-   r   r   +   s    (#F		(C A
 &x{3J{#I !y(:5JI
22Hc!H+f-q{8/DEFJ Q
J
HfWoVE 5zF;''		BHHf[1CJ>wPQ@RSTU
 GGI
 77N    c                    |}g }d}d}	d}
d}d}t        |D cg c]  }|d   	 c}||      D ]  \  }}}t        t        j                  ||z
  |z              }|||z
  z  }	 ||   d   }|
||z
  z  }
|dz  }t	        j
                  ||z        }t	        j                  ||z        }||k\  r	 | d   t        |         d   |   || }|	|||z
  z  z  }	|	|
z
  }||kD  r|dd }	 t        |      r|t        |      z  }|j                  |        t        j                  |      d|d    S c c}w # t        $ r t        j                  ||d   f      }Y Xw xY w)a  
    Get the tile value given the start and end positions and
    chromosome positions.

    Drop bins at the ends of chromosomes if those bins aren't
    full.

    Parameters:
    -----------
    f: h5py.File
        An hdf5 file containing the data
    chromsizes: [('chr1', 1000), ....]
        An array listing the chromosome sizes
    resolution: int
        The size of each bin, except for the last bin in each
        chromosome.
    start_pos: int
        The start_position of the interval to return
    end_pos: int
        The end position of the interval to return

    Returns
    -------
    return_vals: [...]
        A subset of the original genome-wide values containing
        the values for the portion of the genome that is visible.
    r   r   r1   valuesN)r   r   r   ceilmathfloorstrr<   
IndexErrorr>   r   concatenate)rB   rF   rC   	start_posend_posr   binsizearrayscountcurrent_binned_data_positioncurrent_data_position	num_addedtotal_lengthccidstartendn_binschromxoffsets                        r-   r;   r;   W   s   8 G FE $% IL&j'A!'A9gV EUCRWWcEkW456e#>	- sOA&E!S5[0!QJE

57?3Iiig.GG#
 - Z1(;EB9WUA(Gw7J,KK(
 24IIFcrF 1vSV#	 	aKEX >>&!*E!H--Y (B|  	-
 &%(+,A	-s   D)!A
D.,AD..#EEc                    t        j                  | d      }t        |d   j                         D cg c]  }t	        |       c}      ddd   }dg}t	        t        |d   d   dd             g}t	        |d   j                  d	         }|d   d
   d   }t        |d   t        |d            d   |   j                        }||d<   |||||d}	d|d   t        |d            j                  v r|d   t        |d            j                  d   }
t        |
d         t        k(  r(	 |
D cg c]  }t        j                  |       c}|	d<   na	 |
D cg c]&  }t        j                  |j                  d            ( c}|	d<   n*d|d   v r#|d   d   d   }t        j                  |      |	d<   |j                          |	S c c}w c c}w # t        j                  $ r |
D cg c]  }| nc c}w c}|	d<   Y Jw xY wc c}w # t        j                  $ r+ |
D cg c]  }|j                  d       nc c}w c}|	d<   Y w xY w)aO  
    Return some information about this tileset that will
    help render it in on the client.

    Parameters
    ----------
    filename: str
      The filename of the h5py file containing the tileset info.

    Returns
    -------
    tileset_info: {}
      A dictionary containing the information describing
      this dataset
    r0   r1   NrJ   r   r3   r5   infoz	tile-sizer4   rI   )r1   min_posmax_posr2   r   	row_infosutf8 )r7   r8   sortedkeysr   sumattrsr9   rN   r   typejsonloadsJSONDecodeErrorr   r?   )r   rB   r0   r1   rd   re   r2   first_chromr   r6   rf   row_infos_encodeds               r-   r6   r6      sU   " 			(C A !M*:*?*?*ABQ#a&BCDbDIK
 cG3q{8,Q/012G AfIOOK01IH+f%a(K=!#k!n"56x@MSSTEE!H #L a&s;q>':;AAAm$SQ%89??L		!$CDM,NqTZZ],N[)R:C-56DJJqxx/0-[) 
&		!fIk226$(JJ/@$A[!GGIc C@ -O'' C8A,B1Q,B,B[)C- '' RGP,Q!QXXf-=,Q,Q[)Rse   G(G ,GG H +G>?H G G;#	G-,G;:G;>H IH32I I)r   rn   rL   r7   numpyr   utilsr   r.   r   r;   r6   rh   rG   r-   <module>ru      s0         @)Xy.xHrG   