
    DUf                        d dl Zd dlZd dl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mZ ddlmZ  ej                     ej        d	d
e           ej        dd edd          d           ej        dddde           ej        ddedd           ej        ddedd           ej        ddedd           ej        dd d!dd"#           ej        d$d%d&d'           ej        d(d)dd"#          d*                                                                                                                         ZdS )+    N   )	eigdecomp)make_cooler_view)read_viewframe_from_file   )TabularFilePathsniff_for_header)cli	cool_path	COOL_PATH)metavartypez--phasing-trackziPhasing track for orienting and ranking eigenvectors,provided as /path/to/track::track_value_column_name.T   )existsdefault_column_index
TRACK_PATH)helpr   r   z--viewz	--regionszPath to a BED file which defines which regions of the chromosomes to use (only implemented for cis contacts). Note that '--regions' is the deprecated name of the option. Use '--view' instead. )r   defaultr   z--n-eigsz"Number of eigenvectors to compute.)r   r   r   show_defaultz--clr-weight-namezkUse balancing weight with this name. Using raw unbalanced data is not currently supported for eigenvectors.weightz--ignore-diagszjThe number of diagonals to ignore. By default, equals the number of diagonals ignored during IC balancing.z-vz	--verbosezEnable verbose outputF)r   is_flagr   z-oz--out-prefixzSave compartment track as a BED-like file. Eigenvectors and corresponding eigenvalues are stored in out_prefix.contact_type.vecs.tsv and out_prefix.contact_type.lam.txt)r   requiredz--bigwigzZAlso save compartment track (E1) as a bigWig file with the name out_prefix.contact_type.bwc	                 j   t          j        |           }	|s|\  }
}t          |
          \  }}|Yt          |t                    s't          j        d                    |                    d}t          dddd|gddd	|g
          }nt          |t                    rS	 ||         }n# t          $ r< t          j        d                    |d
                    |                              w xY w||vr;t          j        d                    |d
                    |                              |}t          dddd	|g          }t          j        |fdt          dt          j        d	t          j        |t          j        id|d|}|}|t#          |	          }|}nt%          ||	d          }t'          j        |	|||||dd          \  }}|                    |dz   dz   dd           |                    |dz   dz   dd           |r%t-          j        ||	j        |dz   dz   d           dS dS )a  
    Perform eigen value decomposition on a cooler matrix to calculate
    compartment signal by finding the eigenvector that correlates best with the
    phasing track.


    COOL_PATH : the paths to a .cool file with a balanced Hi-C map. Use the
    '::' syntax to specify a group path in a multicooler file.

    TRACK_PATH : the path to a BedGraph-like file that stores phasing track as
    track-name named column.

    BedGraph-like format assumes tab-separated columns chrom, start, stop and
    track-name.

    Nz:No header found. Cannot find "{}" column without a header.refr   r   r   chromstartend)headerusecolsnamesz+Column #{} not compatible with header "{}".,z$Column "{}" not found in header "{}"infer)r   r   #)dtypecommentverboseT)check_sortinggX@)clrphasing_trackview_dfn_eigsclr_weight_nameignore_diagsclip_percentilesort_metricz.cisz.lam.txt	F)sepindexz	.vecs.tsvz.bwE1)value_field)coolerCoolerr	   
isinstanceintclickBadParameterformatdict
IndexErrorjoinpd
read_tablestrnpint64float64r   r   r   eigs_cisto_csvbioframe	to_bigwig
chromsizes)r   r)   viewr+   r,   r-   r&   
out_prefixbigwigr(   
track_pathcolbufr    
track_namekwargstrack_dfcooler_view_dfr*   eigvalseigvec_tables                        S/var/www/html/software/conda/lib/python3.11/site-packages/cooltools/cli/eigs_cis.pyrE   rE      s   l -	
"
"C  (
C%j11
U=c3'' (@@Fs  
 JAq#
;  FF #s## *CC!   ,ELL%    e##,>EE%    J7GUJ2WXXXF=
 rxBJ	 
 
 
 
 ! |)#.. *4DIII &.#'!	 	 	G\ NN:&3UNKKK
V+k9t5QQQ 
N%'		
 	
 	
 	
 	
 	

 
s    B) )AC/)pandasr?   numpyrB   r5   rG   apir   
lib.commonr   lib.ior   r9   utilr   r	    r
   commandargumentrA   optionr8   rE        rV   <module>rc      s                   ) ) ) ) ) ) - - - - - -  3 3 3 3 3 3 3 3       [s;;;
;	1	=	=	=   
Z 	   	-	   
M	   
<	   +3T5   
L    
0  q
 q
         <; vq
 q
 q
rb   