
    tf                      $   d dl Z d dlmc mZ d dlZd dlZd dlZ	ddl
mZmZmZ d dlmZ dZdZ ej$                  e      Zi Zddd	ed<   d
dd	ed<   ddd	ed<   ddd	ed<   i Zddd	ed<   ddd	ed<   d Zd ZddZd Z	 	 	 ddZi dfdZd Zy)    N   )get_quadtree_depthabs2genomic	natsorted)ThreadPoolExecutor   i   Meanmean)namevalueMinminMaxmaxzStandard DeviationstdzMin-MaxminMaxWhiskerwhiskerc                 r    t        t        | t              dz         D cg c]  }d|z  	 c}d d d   S c c}w )Nr      )ranger   	TILE_SIZE)
chromsizesxs     ^/var/www/html/software/conda/envs/higlass/lib/python3.12/site-packages/clodius/tiles/bigwig.pyget_zoom_resolutionsr      s8    !"4Z"Ka"OPQqAFQRVTVRVWWQs   4c                     t        j                  |       }t        |j                               }t	        j
                  |      |   }|S )zj
    TODO: replace this with negspy

    Also, return NaNs from any missing chromosomes in bbi.fetch

    )bbir   r   keyspdSeries)bwpathr   chromosomeschrom_seriess       r   get_chromsizesr&      s<     'JJOO-.K99Z(5L    c           	      4   |7t        |       }g }|j                         D ]  \  }}||t        |      ggz  } n|}|D cg c]  }t        |d          }}t        |t              }dgt        |      gt        d|z  z  t        ||t        t        d}|S c c}w )a*  
    Get the tileset info for a bigWig file

    Parameters
    ----------
    bwpath: string
        The path to the bigwig file from which to retrieve data
    chromsizes: [[chrom, size],...]
        A list of chromosome sizes associated with this tileset.
        Typically passed in to specify in what order data from
        the bigwig should be returned.

    Returns
    -------
    tileset_info: {'min_pos': [],
                    'max_pos': [],
                    'tile_size': 1024,
                    'max_zoom': 7
                    }
    r   r   r   )min_posmax_pos	max_width	tile_sizemax_zoomr   aggregation_modesrange_modes)r&   itemsintr   r   sumr.   r/   )r#   r   chromsizes_listchromsizecr-   tileset_infos           r   r7   r7   +   s    * #F+
%++- 	4KE4D	 233O	4 %)89Ac!A$i9
9!*i8H 3
O$h.%."	L  :s    Bc                 .   | \  }}}}}}}}t        t        j                  ||z
  |z              }	d}
|dk(  rd}
|dk(  rd}
|
dkD  rt        j                  |	|
f      nt        j                  |	      }	 |j                  |   }|j
                  |   }||||g}|	t        j                  d}|dk(  rOt        j                  |i t        |d      |d d d	f<   t        j                  |i t        |d
      |d d df<   n|dk(  rt        j                  |i t        |d      |d d d	f<   t        j                  |i t        |d
      |d d df<   t        j                  |i t        |d      |d d df<   t        j                  |i t        |d      |d d df<   n#t        j                  |i t        ||      |d d  ||k(  r||z  d	k7  r|d d }|S # t        $ r t        j                  |d d  Y |S t        $ r t        j                  |d d  Y |S w xY w)Nr   r   r   r   r   )binsmissingr   )summaryr   r   r
   r      r   )r1   npceilzerosindexvaluesnanr   fetchdict
IndexErrorKeyError)ar#   binsizer   aggregation_mode
range_modecidstartendn_binsn_dimr   r4   clenargskwargss                   r   
fetch_datarS   Y   s   STPVWj"2JUC#+012FEXY%*QY&%!BHHV4DA  %  %uc* RVV4!iiEfe)DEAadGiiEfe)DEAadG9$iiEfe)DEAadGiiEfe)DEAadGiiFff)EFAadGiiEfe)DEAadG 99dMd6;K&LMAaD $;4'>Q.#2A H   vv!
 H	  vv!H	s   2E"G H5HHc                 l   |t        |       }t        |      }||   }t        t        |||            }	t	        d      5 }
t        |
j                  t        |	D cg c]  }t        | ||||gt        |      z           c}            }d d d        t        j                        S c c}w # 1 sw Y   #xY w)N   )max_workers)
r&   r   listr   r   maprS   tupler=   concatenate)r#   
zoom_level	start_posend_posr   rI   rJ   resolutionsrH   cids_starts_endser6   arrayss                r   get_bigwig_tilerb      s     #F+
&z2K*%GK
IwGH		+ 
qEE .
 	 *6F
Sq'"	

 >>&!!	
 
s    B*#B%=B*%B**B3c           
      (   g }|D ]z  }|j                  d      dd }|j                  d      d   }|j                  d      }t        t        t        |dd             }	t	        |      dkD  r|d   nd}
|
t
        v r|
nd}|
t        v r|
nd}t        |D cg c]  }|j                  d       c}      }|rE|D cg c]  }|d   	 }}|D cg c]  }t        |d          }}t        j                  ||	      }nd}d
|v r|d
   }||v r||   }nd}|	d   }|	d   }|t        |       }t        |t              }t        d||z
  z  z  }||z  }||z   }t        | ||||||      }t        j                  |      }|||fgz  }} |S c c}w c c}w c c}w )a  
    Generate tiles from a bigwig file.

    Parameters
    ----------
    tileset: tilesets.models.Tileset object
        The tileset that the tile ids should be retrieved from
    tile_ids: [str,...]
        A list of tile_ids (e.g. xyx.0.0) identifying the tiles
        to be retrieved
    chromsizes_map: {uid: []}
        A set of chromsizes listings corresponding to the parameters of the
        tile_ids. To be used if a chromsizes id is passed in with the tile id
        with the `|cos:id` tag in the tile id
    chromsizes: [[chrom, size],...]
        A 2d array containing chromosome names and sizes. Overrides the
        chromsizes in chromsizes_map

    Returns
    -------
    tile_list: [(tile_id, tile_data),...]
        A list of tile_id, tile_data tuples
    |r   Nr   .r<   r
   :)r@   cosr   )rI   rJ   )splitrW   rX   r1   lenr.   r/   rD   r!   r"   r&   r   r   rb   hgfoformat_dense_tile)r#   tile_idschromsizes_mapr   generated_tilestile_idtile_option_partstile_no_optionstile_id_partstile_positionreturn_valuerI   rJ   otile_optionsr6   
chromnameschromlengthschromsizes_to_usechromsizes_idr[   tile_pos	max_depthr,   r\   r]   dense
tile_values                               r   tilesr      s   0 O 13#MM#.qr2!--,Q/'--c2SmAa&89:+.}+=+A}Q'v+7;L+L<RX%1[%@\d
3DEaQWWS\EF(231!A$3J3/9:!C!I:L: "		,j I M$ ,U 3.$2=$A!$(!"1%
 # $ .v 6&'8)D	i*&< ==	y(	i'-!
 ++E2
Wj122c13d Q F 4:s   F
9F
Fc                     	 t        |       }g }|j                         D ]  \  }}|j                  ||g        |S # t        $ r4}t        j                  |       t        dj                  |            d}~ww xY w)a,  
    Get a list of chromosome sizes from this [presumably] bigwig
    file.

    Parameters:
    -----------
    filename: string
        The filename of the bigwig file

    Returns
    -------
    chromsizes: [(name:string, size:int), ...]
        An ordered list of chromosome names and sizes
    z-Error loading chromsizes from bigwig file: {}N)r&   r0   append	Exceptionloggererrorformat)filenamer%   datar4   r5   exs         r   r   r      s}    T%h/'--/ 	'KE4KK&	' TRGNNrRSSTs   9< 	A9/A44A9)N)Nr
   N)r   clodius.tiles.formatr   r   rj   loggingnumpyr=   pandasr!   utilsr   r   r   concurrent.futuresr   MAX_THREADSr   	getLogger__name__r   r.   r/   r   r&   r7   rS   rb   r    r'   r   <module>r      s    
 # #    = = 1				8	$ %+f= & $)E: % $)E: % $85I % !*X>H "+i@I X
+\-j "B ,.$ K\Tr'   