o
    Nrf                     @   sN  d dl Z d dlZd dlmZ d dlZddlmZ ddlmZ ddl	m
Z
mZmZmZ ddlmZ dd	lmZmZ dd
lmZ dd Ze ejdddejdddddedejdddeedddejddddejdd dd!d"ejd#d$ed%ejd&d'd(dd)ejd*d+d,dejd-d.edd/ejd0d1dd!d"d2d3 ZdS )4    N)ceil   )apilock)HIGLASS_TILE_DIMlegacy_zoomifypreferred_sequencezoomify_cooler)parse_cooler_uri   )cli
get_loggerparse_field_paramc           	      C   s   ddl m } tt}| d u rg } nt| } |d|   |D ]I}|d t| }dt|	 v r4q |
d|  z|j|g| dd W q  tyi } z|j}|d u rYd	}|d	kr_|W Y d }~q d }~ww d S )
Nr   balanceBalancing args: z::resolutions/weightz#Balancing zoom level with bin size coolerargsZ	prog_namer   )r   r   __name__shlexsplitdebugstrr   Coolerbinsinfomain
SystemExitcode)	r   resolutionsoutfilebalance_cmdloggerresurie	exit_code r+   [/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/cooler/cli/zoomify.pyinvoke_balance   s.   
r-   cool_uriZ	COOL_PATH)metavarz--nprocz-nz-pzcNumber of processes to use for batch processing chunks of pixels [default: 1, i.e. no process pool])helpdefaulttypez--chunksizez-cz*Number of pixels allocated to each processg    cAT)r0   r2   r1   Zshow_defaultz--resolutionsz-rak  Comma-separated list of target resolutions. Use suffixes B or N to specify a progression: B for binary (geometric steps of factor 2), N for nice (geometric steps of factor 10 interleaved with steps of 2 and 5). Examples: 1000B=1000,2000,4000,8000,... 1000N=1000,2000,5000,10000,... 5000N=5000,10000,25000,50000,... 4DN is an alias for 1000,2000,5000N [default: B])r0   z	--balancez3Apply balancing to each zoom level. Off by default.F)r0   Zis_flagr1   z--balance-argszAdditional arguments to pass to cooler balance. To deal with space ambiguity, use quotes to pass multiple arguments, e.g. ``--balance-args '--nproc 8 --ignore-diags 3'``. Note that nproc for balancing must be specified independently of zoomify arguments.)r0   r2   z
--base-uriz-izAOne or more additional base coolers to aggregate from, if needed.)r0   multiplez--outz-ozOutput file or URIz--fieldzSpecify the names of value columns to merge as '<name>'. Repeat the ``--field`` option for each one. Use '<name>:dtype=<dtype>' to specify the dtype. Include ',agg=<agg>' to specify an aggregation function different from 'sum'.)r0   r2   r3   z--legacyz5Use the legacy layout of integer-labeled zoom levels.c
           "      C   s  t t}
t| \}}|	du r|dd}nt|	\}}|
d|  d |
d| d |rt| |||td\}}|rdd	lm} |du rIg }nt	|}|

d
|  tt| D ]V\}}|d t| }|t|krzdt| v rzq^|
d| d|  z|j|g|dd W q^ ty } z|j}|du rd}|dkr|W Y d}~q^d}~ww dS dS t| }|jj }|jrtt|t }|j}n"| ddg dd }|d |d   }tt|| t }d}|du rd}g |}}dd |	dD D ]u}d|v sd|v r||k rtd |dkr*t||d}nM|dkr6t||d}nA|dkrFddgtd|d}n1| dr\t|	dd }t||d}n| drrt|	dd }t||d}nt|g}|!| qt"|rd d |D }t#| \}}} }!t|}d!d" t#|| D } d#d" t#||!D }!n	d$gdd}} }!t$| gt|||||t|| |!d%	 |rt%||| dS dS )&zu
    Generate a multi-resolution cooler file by coarsening.

    COOL_PATH : Path to a COOL file or Cooler URI.

    Nz.coolz.mcoolzRecursively aggregating ""zWriting to "r   r   r   r   z::r   zBalancing zoom level z, bin size r   r   r   startendbc                 S   s   g | ]}|   qS r+   )striplower).0sr+   r+   r,   
<listcomp>   s    zzoomify.<locals>.<listcomp>,nzWMap is already < 256 x 256. Provide resolutions explicitly if you want to coarsen more.nicebinaryZ4dni  i  i  c                 S   s   g | ]}t |d dqS )F)Zincludes_colnumr   )r:   argr+   r+   r,   r<      s    c                 S      i | ]\}}|d ur||qS Nr+   )r:   coldtr+   r+   r,   
<dictcomp>       zzoomify.<locals>.<dictcomp>c                 S   rB   rC   r+   )r:   rD   fr+   r+   r,   rF      rG   count)nprocr   columnsdtypesagg)&r   r   r   replacer   r   r   r   r   r   r   reversedlistitemsr   r   r   r   r    r!   r"   Z
chromsizesvaluessumZbinsizeintr   r   meanwarningswarnr	   endswithextendlenzipr
   r-   )"r.   rJ   	chunksizer#   r   Zbalance_argsfieldlegacyZbase_urioutr&   infile_r$   Zn_zoomsZzoom_levelsr%   levelr'   r(   r)   r*   ZclrZgenome_lengthZmaxresZcurresr   Zmean_fragsizeZrstringrZfield_specifiersrK   rL   rM   r+   r+   r,   zoomify1   s   S


	






rd   )r   rV   mathr   Zclick r   Zparallelr   reducer   r   r	   r
   utilr   r   r   Z_utilr   r-   commandargumentoptionrT   r   rd   r+   r+   r+   r,   <module>   s    	
	"