
    DUf                         d dl Z  e j        e j                   d dlmZ d dlZd dlZd dl	Z	ddl
mZ ddlmZ d Zedd	efd
            ZdS )    N)level)partial   )is_cooler_balanced)pool_decoratorc                    g }|rd}nd}|                     |dd                              | |          }|d         |dg<   |d         |dg<   |d	         |d
g<   |                    g dd          |                                                                         }ddd|g|_        |j        d         dk    r|                    |           |                     |dd                              ||           }|d         |dg<   |d         |dg<   |d	         |dg<   |                    g dd          |                                                                         }ddd|g|_        |j        d         dk    r|                    |           t          |          dk    rt          j
        ddd|g          S t          j        |d          S )NbalancedcountT)balance	as_pixelsjoinr   chrom2   start2r   end2)chrom1start1end1)observedchromstartendr   r   r   )r   r   r   )columnsignore_index)matrixfetchgroupbymeanreset_indexr   shapeappendlenpd	DataFrameconcat)r   clrclr_weight_name	viewpoint	to_returncolnamepxls1pxls2s           T/var/www/html/software/conda/lib/python3.11/site-packages/cooltools/api/virtual4c.py_extract_profiler/      s   I JJ$TJJJPPy E "!E8*!!E8*lE6(O 	222TBB7K		 

 gug6EM{1~JJ$TJJJPP5 E "!E8*!!E8*lE6(O222TBB7K		 

 gug6EM{1~
9~~|Wgug$FGGGGy6666    weightr   c                    |dvr=	 t          | |d          }n&# t          $ r}t          d| d          |d}~ww xY wd}nd}t          j        j                            |          }t          t          | ||	          }t           ||| j
                            }	t          j        |	d
          }
|
j        d         dk    rLt          j        d|            |                                 dd         g d         }
t"          j        |
|<   n?|
d                             d          |
d<   |
d                             t(                    |
d<   |
d                             t(                    |
d<   t          j        |
t          j        | j                             t"          j        |
j        |
d         |d         k    |
d         |d         k    z  |
d         |d         k    z  |f<   t          j        |                                 dd         g d         |
g dd                                                              d          }
|
S )a   Generate genome-wide contact profile for a given viewpoint.

    Extract all contacts of a given viewpoint from a cooler file.

    Parameters
    ----------
    clr : cooler.Cooler
        A cooler with balanced Hi-C data.
    viewpoint : tuple or str
        Coordinates of the viewpoint.
    clr_weight_name : str
        Name of the column in the bin table with weight
    nproc : int, optional
        How many processes to use for calculation. Ignored if map_functor is passed.
    map_functor : callable, optional
        Map function to dispatch the matrix chunks to workers.
        If left unspecified, pool_decorator applies the following defaults: if nproc>1 this defaults to multiprocess.Pool;
        If nproc=1 this defaults the builtin map. 

    Returns
    -------
    v4C_table : pandas.DataFrame
        A table containing the interaction frequency of the viewpoint with the rest of
        the genome

    Note
    ----
    Note: this is a new (experimental) function, the interface or output might change in
    a future version.
    )NFT)raise_errorsz#provided cooler is not balanced or z is missingNr	   r
   )r'   r(   r)   r   r   z No contacts found for viewpoint )r   r   r   r   categoryr   r   )view_dfr   r   left)onhow)drop)r   	Exception
ValueErrorbioframecore	stringopsparse_regionr   r/   list
chromnamesr$   r&   r!   loggingwarnbinsnpnanastypeintsort_bedframemake_viewframe
chromsizeslocmergedrop_duplicatesr    )r'   r)   r(   nprocmap_functor_er+   fcountsv4cs              r.   	virtual4crV   :   s|   L m++	"3dKKKAA 	 	 	RoRRR 	 '44Y??Ic?i	 	 	A ++a0011F )F
.
.
.C
y|qC	CCDDDhhjjm5556vG7|**:66G7|**3//GZ&&s++E
+CN;;	
 	
 	
 	
 F 	\Yq\)7|y|+-5zYq\)+ 	
 H

1117778,,,	   _[d[## 	 Js    
<7<)rB   basicConfigINFO	functoolsr   numpyrE   pandasr$   r<   
lib.checksr   
lib.commonr   r/   maprV    r0   r.   <module>r`      s      ', ' ' ' '                , + + + + + ' ' ' ' ' ''7 '7 '7R  
W W W W W Wr0   