
    DUfQ                     l    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 dlZdddd ed          fdZ	d Z
dS )    N   )fastsavetxtFg    חAc           
      `   |st          j        d          }t          t          |          t                    s"t          t          |          t
                    rR|                    d          r#t          j        |          }|j	        }|j
        }n8t          |d          }|}|j        }nt          |d          r|}t          j        }t          t          |           t          j                  st          j        |           }	n| }	|	j        d         }
|	j        d         |s|	                                dd         n&|	                                                    |          }t)          |          }d                    d	                    ||          gfd
|                                D             z                                             }fd|                                D             }t3          d||z            }t5          ||z  dz             D ]}t7          |||z            }t7          ||dz   |z            }||k    r n|s|	                    |          n(|	                    |                              |          ||         }|r |s||	                    d          d         z  }t          j        |||rdnd|dk    r|nd|||                    t          t          |          t           j                  r|                                S  |             dS )a  
    Dump a genome-wide contact matrix from cooler into a CWorld-format
    text matrix.

    Parameters
    ----------
    in_cooler : str or cooler
        A cooler object or the path to the file.

    out : str or file object
        Either:
        -- a path to the output file. If ends with .gz the output is gzipped
        -- a file object
        -- a stdin of a Popen object
        -- None, in which case the data is dumped into a string and returned
        TIP: when using files/stdin do not forget to flush()/communicate().

    region : str
        The region to dump. By default is None, dump the genome-wide matrix.

    iced : bool, optional
        If True, dump the balanced matrix.

    iced_unity : bool, optional
        If True and `iced` is True, dump the matrix balanced to a unity.

    buffer_size : int
        The chunk size for iterating over the rows of the Hi-C matrix.
        z.gzwbwritebin-sizezgenome-assemblyN	z{}x{}c           	      l    g | ]0\  }}d                      ||j        |j        dz   |j                  1S z{}|{}|{}:{}-{}r   )formatchromstartend).0binidxbgnames      Z/var/www/html/software/conda/lib/python3.11/site-packages/cooltools/sandbox/cool2cworld.py
<listcomp>zdump_cworld.<locals>.<listcomp>P   sM     
 
 
 ##FE17AGaKOO
 
 
r   c           	          g | ]B\  }}d                      ||j        |j        dz   |j                                                  CS r   )r   r   r   r   encode)r   binidx1b1r   s      r   r   zdump_cworld.<locals>.<listcomp>V   sY       GR 	"(Q,OOVVXX  r   r   )balancez/bins/weightscales   %.8fs   %.4lfr   )format_stringheaderrow_headers) ioBytesIO
issubclasstypestr	bytearrayendswithr   
gzipWriterstdincommunicateopenflushhasattr
empty_funccoolerCoolerinfobinsfetchlenjoinr   iterrowsr   maxrangeminmatrix_load_attrs	array2txtgetvalue)	in_cooleroutregioniced
iced_unitybuffer_sizewriterout_pipeclose_out_funccresr1   nbinscol_headersr   nrows_per_stepilohimatr   s                       @r   dump_cworldrO      sJ   N  joo$s))S!! 0ZS		9%E%E 0<< 	* +C00F|H#/NN#t__FH#\NN	g		 0$/ d9oov}55 M)$$
&
CF$%E%B16688AAA;;AFFHHNN6,B,BDIIE))	u	%	%&
 
 
 
!]]__
 
 
	
  fhh    ==??  K K5011N5N*Q.// 
 
N*++Q.01188E 6AHHTH"""$''--f55
R%	
  	: 	:1==0099C%/=''X"#q&&;;d#BrE*	
 	
 	
 	
 	
 $s))RZ(( ||~~r   c                 p   t           j                            t           j                            |          d                   d         }t	          j                    5 }| D ]}t          j        |          j        d         }t          j	        t           j        
                    |dt          |          z                        dD ]\  }}t           j        
                    |dt          |          z   |          }t          j	        |           t           j        
                    |d                    |||                    }	t          ||	|d           t          j        |d	
          5 }
|
                    ||d           ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w Y   dS )a  
    Makes a CWorld .tar archive with binned contact maps at multiple resolutions
    in .matrix.txt.gz format.

    Parameters
    ----------
    cooler_paths : a list of str
        The paths to all coolers to dump into a single CWorld tar archive.
        Must correspond to the same dataset and have different resolutions.

    out_path : str
        The path to the output file.

    r   r   r	   zC-))Tr@   )Frawz{}__C-{}-{}.matrix.gzF)r=   r>   r@   rA   w)modeT)arcname	recursiveN)ospathsplitextsplittempfileTemporaryDirectoryr.   r/   r0   mkdirr4   r$   r   rO   tarfiler*   add)cooler_pathsout_pathdataset_namecworld_tmp_pathcooler_pathrG   r@   
iced_labelfolder_pathmat_patharchives              r   dump_cworld_tarrh   x   s3   & 7##BGMM($;$;A$>??BL		$	&	& O/' 	 	K-,,1*=CHRW\\/4#c((?CCDDD$D   j gll?D3s88OZXX%%%7<<+22<jQQ 
 )xdu     \(--- 	OKKKNNN	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O#O O O O O O O O O O O O O O O O O Os7   DF+.FF+F	F+F	F++F/2F/)rV   r    gzipr]   rZ    r   r.   intrO   rh    r   r   <module>rm      s    				 				          
 		Ci i i iX'O 'O 'O 'O 'Or   