
    DUf2                    *   d dl mZ d dlZd dlmZ d dlm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 dd
lmZmZ ddlmZ dgZ ee          Z G d de          Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#d Z$dddddd dddddd e%ddd!d"d@d?Z&e&Z'dS )A    )annotationsN)partial)add)Literal   )
get_logger)
MapFunctor)Cooler)	partitionsplit)madbalance_coolerc                      e Zd ZdS )ConvergenceWarningN)__name__
__module____qualname__     K/var/www/html/software/conda/lib/python3.11/site-packages/cooler/balance.pyr   r      s        Dr   r   c                B    t          j        | d         d                   S )Npixelscount)npcopy)chunks    r   _initr      s    75?7+,,,r   c                    d||dk    <   |S )Nr   r   r   )r   datas     r   	_binarizer       s    DOKr   c                n    |d         }t          j        |d         |d         z
            | k     }d||<   |S )Nr   bin1_idbin2_idr   )r   abs)n_diagsr   r   r   masks        r   _zero_diagsr'   "   s<    8_F6&#fY&77887BDDJKr   c                x    | d         d         }| d         }||d                  ||d                  k    }d||<   |S Nbinschromr   r"   r#   r   r   r   r   	chrom_idsr   r&   s        r   _zero_transr.   )   E    fg&I8_FVI&'9VI5F+GGDDJKr   c                x    | d         d         }| d         }||d                  ||d                  k    }d||<   |S r)   r   r,   s        r   	_zero_cisr1   1   r/   r   c                V    |d         }| |d                  | |d                  z  |z  }|S )Nr   r"   r#   r   )vecr   r   r   s       r   _timesouterproductr4   9   s3    8_Fvi !Cy(9$::TADKr   c                    t          | d         d                   }| d         }t          j        |d         ||          t          j        |d         ||          z   }|S )Nr*   r+   r   r"   )weights	minlengthr#   )lenr   bincount)r   r   nr   margs        r   _marginalizer<   ?   sj    E&M'"##A8_F;vi($!DDDr{y41H H H D Kr   c
                r   d}
t          |           }t          |          D ]7}t          ||||	                              t                                        |                              t          |                               t                                        t          t          j        |                    }||dk             }t          |          s!t          j        }
t          j        | d d <   d} nz||                                z  }d||dk    <   | |z  } |                                }t                              d|            ||k     r n9t#          j        dt&                     |                                }
t          j        | | dk    <   |r| t          j        |
          z  } | |
|fS )N      ?spansmapuse_lockr           r   variance is ,Iteration limit reached without convergence.)r8   ranger   preparer   piper4   r<   reducer   r   zerosnanmeanvarloggerinfowarningswarnr   sqrt)biasclrr@   filters	chunksizerA   tol	max_itersrescale_marginalsrB   scalen_bins_r;   nzmargrM   s                   r   _balance_genomewider^   H   s    EYYF9 
 
#Uh???WU^^T']]T$d++T,VC&))** 	 dai6{{ 	FEfDGCEfkkmm#TQYjjll(3(()))99E  	:<N	
 	
 	
 KKMMEfDO r   c
                |   |                                 d         d d          }
t          j        t          |                                                     }|                    d          }|                    d          }t          j        t          |                    }t          j        |t          j                  }t          |           }t          ||d d         |dd                    D ]G\  }}}t          
                    |
|                    ||         ||         }}t          t          |||                    }d}t          j        }t          |          D ]N}t          ||||	                              t                                         |                              t$          |                               t&                                        t*          t          j        |                    }|||         }||dk             }t          |          s!t          j        }t          j        | ||<   d	} n||                                z  }d||dk    <   | ||xx         |z  cc<   |                                }t          
                    d
|            ||k     r n&Pt3          j        d|
|          dt6                     |                                }| ||         }t          j        ||dk    <   |||<   |||<   |r$| ||xx         t          j        |          z  cc<   I| ||fS )Nnameindexes/chrom_offsetzindexes/bin1_offsetr   r>   r?   r   rC   rD   z/Iteration limit reached without convergence on .)chromsr   aranger8   
_load_dsetones	full_likerK   ziprN   rO   listr   rF   r   rG   r   rH   r4   r<   rI   r   rJ   rL   rM   rP   rQ   r   rR   )rS   rT   r@   rU   rV   rA   rW   rX   rY   rB   rd   r-   chrom_offsetsbin1_offsetsscales	variancesr[   cidlohiplophirZ   rM   r\   r;   r]   bs                               r   _balance_cisonlyru   }   s    ZZ\\&!!!!$F	#cjjll++,,INN#9::M>>"788LWS^^$$FVRV,,IYYF9mCRC&8-:KLL .* .*RF3K   #\"%5SYsC3344fy!! 	 	AcC(CCCg($//l##RXf--..  2;D$!)_Fv;;  fRU&++--'DDOBKKK4KKK**,,CKK,s,,---Syy  MP&+PPP"  
 BKF!q&	s	# 	*BKKK275>>)KKK""r   c
           
     V   d}
t          |           |                    d          }dt          j        fdt	          |d d         |dd                    D                       z  }t          |          D ]R}t          ||||	                              t                    	                    |          	                    t                    	                    t          | |z            	                    t                                        t          t          j                            }||dk             }t          |          s!t          j        }
t          j        | d d <   d} nz||                                z  }d||dk    <   | |z  } |                                }t&                              d	|            ||k     r nTt+          j        d
t.                     |                                }
t          j        | | dk    <   |r| t          j        |
          z  } | |
|fS )Nr>   ra   c                :    g | ]\  }}d ||z
  z  z
  g||z
  z  S )r   r   ).0rp   rq   r[   s      r   
<listcomp>z&_balance_transonly.<locals>.<listcomp>   sG     	
 	
 	
B 27f$$&"r'2	
 	
 	
r   rb   r   r?   r   rC   rD   rE   )r8   rf   r   concatenateri   rF   r   rG   r   rH   r1   r4   r<   rI   r   rJ   rK   rL   rM   rN   rO   rP   rQ   r   rR   )rS   rT   r@   rU   rV   rA   rW   rX   rY   rB   rZ   rk   cweightsr\   r;   r]   rM   r[   s                    @r   _balance_transonlyr|      s    EYYFNN#9::MR^	
 	
 	
 	
mCRC0-2CDD	
 	
 	
  H 9 
 
#Uh???WU^^T']]T)__T$dXo66T,VC&))** 	 dai6{{ 	FEfDGCEfkkmm#TQYjjll(3(()))99E  	:<N	
 	
 	
 KKMMEfDO r   F      
   Tgh㈵>   i weight)cis_only
trans_onlyignore_diagsmad_maxmin_nnz	min_count	blacklistrY   x0rW   rX   rV   rA   rB   store
store_namerT   r
   r   boolr   r   int | Literal[False]r   intr   r   r   
str | NonerY   r   np.ndarray | NonerW   floatrX   rV   rA   r	   rB   r   r   strreturntuple[np.ndarray, dict]c                  t          | j        d                   }||}d|fg}nFt          j        d||z   |          }t	          t          |dd         |dd                             }g }|r|                    t                     |r(|                    t          t          |                     t          | j        d                   }|	|	}d|t          j
        |          <   nt          j        |t                    }|dk    rt          g|}t          | |||                              t                                         |                              t$                                        t(          t          j        |                    }d|||k     <   |}t          | |||                              t                                         |                              t$                                        t(          t          j        |                    }|r	d|||k     <   |dk    r|                     d	          }t          |dd         |dd                   D ]=\  }}|||         }|||xx         t          j        ||dk                       z  cc<   >t          j        ||dk                       }t          j        |          }t3          |          } t          j        ||| z  z
            }!d|||!k     <   |d||<   |rt7          || |||||
|||
  
        \  }}"}#n;|rt9          || |||||
|||
  
        \  }}"}#nt;          || |||||
|||
  
        \  }}"}#|
||||||"|#|
k     |#d
d
}$|r|                     d          5 }%||%d         v r	|%d         |= ddd}& |%d         j        |fd|i|& |%d         |         j         !                    |$           ddd           n# 1 swxY w Y   ||$fS )a  
    Iterative correction or matrix balancing of a sparse Hi-C contact map in
    Cooler HDF5 format.

    Parameters
    ----------
    clr : cooler.Cooler
        Cooler object
    cis_only : bool, optional
        Do iterative correction on intra-chromosomal data only.
        Inter-chromosomal data is ignored.
    trans_only : bool, optional
        Do iterative correction on inter-chromosomal data only.
        Intra-chromosomal data is ignored.
    ignore_diags : int or False, optional
        Drop elements occurring on the first ``ignore_diags`` diagonals of the
        matrix (including the main diagonal).
    chunksize : int or None, optional
        Split the contact matrix pixel records into equally sized chunks to
        save memory and/or parallelize. Set to ``None`` to use all the pixels
        at once.
    mad_max : int, optional
        Pre-processing bin-level filter. Drop bins whose log marginal sum is
        less than ``mad_max`` median absolute deviations below the median log
        marginal sum.
    min_nnz : int, optional
        Pre-processing bin-level filter. Drop bins with fewer nonzero elements
        than this value.
    min_count : int, optional
        Pre-processing bin-level filter. Drop bins with lower marginal sum than
        this value.
    blacklist : list or 1D array, optional
        An explicit list of IDs of bad bins to filter out when performing
        balancing.
    rescale_marginals : bool, optional
        Normalize the balancing weights such that the balanced matrix has rows
        / columns that sum to 1.0. The scale factor is stored in the ``stats``
        output dictionary.
    map : callable, optional
        Map function to dispatch the matrix chunks to workers.
        Default is the builtin ``map``, but alternatives include parallel map
        implementations from a multiprocessing pool.
    x0 : 1D array, optional
        Initial weight vector to use. Default is to start with ones(n_bins).
    tol : float, optional
        Convergence criterion is the variance of the marginal (row/col) sum
        vector.
    max_iters : int, optional
        Iteration limit.
    store : bool, optional
        Whether to store the results in the file when finished. Default is
        False.
    store_name : str, optional
        Name of the column of the bin table to save to. Default name is
        'weight'.

    Returns
    -------
    bias : 1D array, whose shape is the number of bins in ``h5``.
        Vector of bin bias weights to normalize the observed contact map.
        Dropped bins will be assigned the value NaN.
        N[i, j] = O[i, j] * bias[i] * bias[j]
    stats : dict
        Summary of parameters used to perform balancing and the average
        magnitude of the corrected matrix's marginal sum at convergence.

    nnzNr   rb   r   nbins)dtyper?   ra   F)
rW   r   r   r   r   r   rZ   	convergedrM   divisive_weightszr+r*   gzip   )compressioncompression_optsr   )"r   rO   r   re   rj   ri   appendr.   r   r'   isnanrg   r   r    r   rG   r   rH   r<   rI   r   rJ   rf   medianlogr   expru   r|   r^   opencreate_datasetattrsupdate)'rT   r   r   r   r   r   r   r   rY   r   rW   rX   rV   rA   rB   r   r   r   r@   edgesbase_filtersr[   rS   rU   marg_nnzr;   offsetsrp   rq   c_marg	logNzMargmed_logNzMargdev_logNzMargcutoffrZ   rM   statsgrph5optss'                                          r   r   r     s   p chuo

C	S
	!S9_i88SssU122Y//00 L )K((( @GK>>??? '"##F	~ RXd^^wvU+++ {{,|,#Uh???WU^^T']]T,VC&))** 	 $%X GcC(;;;		g	l			RXf%%	&	& 	  #!"TI {{..!788'#2#,44 	9 	9FB"R%[FBKKK29VFQJ%7888KKKKF4q>**		),,I-(??@@TF] Y  &
+
 
eSS 
 
-
 
eSS /
 
eS $3Y! E  8XXd^^ 	8sS[((K
+%+CCF&CK&zGGGGGGK
#)00777	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 ;s   AO33O7:O7)$rT   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rY   r   r   r   rW   r   rX   r   rV   r   rA   r	   rB   r   r   r   r   r   r   r   )(
__future__r   rP   	functoolsr   operatorr   typingr   numpyr   _loggingr   _typingr	   apir
   parallelr   r   utilr   __all__r   rN   UserWarningr   r   r    r'   r.   r1   r4   r<   r^   ru   r|   rA   r   iterative_correctionr   r   r   <module>r      s   " " " " " "                                                & & & & & & & &      
	H			 	 	 	 	 	 	 	- - -  
          2 2 2jD# D# D#N; ; ;B )* " %V V V V V Vr &   r   