o
    Uݢg#                     @  sV  d Z ddlmZ ddlZddlZddlmZmZm	Z	m
Z
 ddlmZ ddlmZmZmZ ddlZddlmZ ddlmZ ddlmZ d	Zd
ZdVddZdWddZdXddZdYd d!ZdZd)d*Zed+ed,Z d[d-d.Z!d\d0d1Z"d]d3d4Z#d5d6 Z$d^d:d;Z%d_d?d@Z&d`dAdBZ'dadHdIZ(dbdJdKZ)dcdMdNZ*edddRdSZ+ededTdSZ+dfdUdSZ+dS )gz)Functions for interacting with h5 tables.    )annotationsN)Callable	GeneratorIterableMapping)	ExitStack)
NamedTupleTypeVaroverload)PCA)tablesversion   ftables.Filefieldstrgroupkeysubgrouptables.Groupc                 C  s   t |trt|dd}n
t |trt|}t|ds(t| d| dtj	
|j}t|jdkrV|jdkrE| ||||j}n	| ||||j}||d d < d S | ||| d S )Nasciixmlcharrefreplacedtype/z  must be a numpy array or scalarr   )
isinstancer   npbytes_encodebyteshasattr
ValueErrorr   Atom
from_dtyper   lenshapesizecreate_carrayZcreate_earraycreate_array)r   arrr   r   r   r   atomZ
dest_array r+   e/oak/stanford/groups/akundaje/marinovg/programs/cellranger-9.0.1/lib/python/cellranger/analysis/io.py__encode_h5_arr   s   




r-   
namedtupler   c                 C  s   t | jv rtt| jt }|tkrtdt|f n| | jt tt | 	|d| }|j
D ]}t| t|||||| q0dS )z;Save a namedtuple to an h5 file under a group and subgroup.zEAttempted to write analysis HDF5 version %d data to a version %d file_N)VERSION_KEYrootintgetattrVERSIONr!   r(   r   int64create_group_fieldsr-   )r   r   r   r.   r   r   r   r+   r+   r,   save_h50   s   

r8   valuestr | bytes | int | floatc                 C  s$   t | tr| S t | tr|  S | S N)r   r   r   decoder9   r+   r+   r,   _string_or_numA   s
   

r>   ?str | bytes | int | float | Iterable[str | bytes | int | float]returnlist[str | int | float]c                 C  s8   t | ttB tB tB st| drdd | D S t| gS )a  Convert a string or collection into a list of str or numbers.

    `bytes` are decoded to `str`s.

    `str` or numbers are wrapped in lists.

    For iterables (other than strings), a list is returned where any `bytes`
    elements have been decoded to `str`.

    Args:
        value (str, bytes, int, float, iterable): The value to convert.

    Returns:
        list: A list containing `str` or numeric values.
    __iter__c                 S  s   g | ]}t |qS r+   )r>   ).0vr+   r+   r,   
<listcomp>\   s    z'_string_or_num_list.<locals>.<listcomp>)r   r   r   r2   floatr    r>   r=   r+   r+   r,   _string_or_num_listI   s    
rG   filenamestr | bytesr)   IIterable[str | bytes | int | float | Iterable[str | bytes | int | float]]headerIterable[str]prefixesc           	      C  s|   t | d/}tj|dd}|| t||D ]\}}t|}|t| || qW d   dS 1 s7w   Y  dS )a}  Save a csv file of the matrix.

    Args:
        filename (Union[str, bytes]): The destination filename.
        arr (Iterable[Union[str, bytes, int, float, Iterable[Union[str, bytes, int, float]]]]): _description_
        header (Iterable[str]): _description_
        prefixes (Iterable[Union[str, bytes, int, float, Iterable[Union[str, bytes, int, float]]]]): _description_
    w
)lineterminatorN)opencsvwriterwriterowziprG   extend)	rH   r)   rK   rM   r   rS   Zrow_vecprefixrowr+   r+   r,   save_matrix_csv`   s   
"rY   _T1)boundc                 C  sL   zt | | }|jdkr| W S |W S  tjy%   t | j|d  Y S w )Nr+   )r3   readr%   itemr   NoSuchNodeError_v_attrs)r   r   Zfield_valuer+   r+   r,   __get_table_node{   s   

r`   	type[_T1]c                   s   |  fdd|jD S )z*Load a single namedtuple from an h5 group.c                   s   g | ]}t  |qS r+   )r`   )rC   r   r   r+   r,   rE      s    z&load_h5_namedtuple.<locals>.<listcomp>)_maker7   )r   r.   r+   rb   r,   load_h5_namedtuple   s   rd   &Generator[tuple[str, _T1], None, None]c                 c  s*    | D ]}|j dd t||fV  qdS )a  Iterate through the subgroups of a group, converting each to the given type.

    Args:
        group (tables.Group): _description_
        namedtuple (Type[_T1]): _description_

    Yields:
        Generator[Tuple[str, _T1], None, None]: _description_
       N)_v_namerd   )r   r.   r   r+   r+   r,   load_h5_iter   s   
rh   c                 C  s   t j| dS )Nzanalysis.h5)ospathjoin)	base_pathr+   r+   r,   h5_path   s   rm   finsIterable[tables.File]foutc                 C  sD   | |j|}| D ]}||jvrq	|jj| }|j||dd q	d S )NT)	recursive)r6   r1   	_v_groupsZcopy_children)rn   rp   r   Z	group_outZfinZgroup_inr+   r+   r,   _combine_h5_group   s   
rs   in_filesout_filegroupsc              	     s   t  ;  fdd| D }t|d}|D ]}t||| qW d   n1 s)w   Y  W d   dS W d   dS 1 sAw   Y  dS )zMerge a set of groups from a set of h5 files.

    Args:
        in_files (Iterable[str]): The source files.
        out_file (str): The destination file.
        groups (Iterable[str]): The names of the groups.
    c                   s   g | ]}  t|d qS )r)enter_contextr   	open_file)rC   rH   Z	fin_stackr+   r,   rE      s    z$combine_h5_files.<locals>.<listcomp>rN   N)r   r   ry   rs   )rt   ru   rv   rn   rp   r   r+   rz   r,   combine_h5_files   s   
"r{   c                 C  s   t jtjd}t j| d|dS N)	complevelrN   )filters)r   Filtersh5_constantsH5_COMPRESSION_LEVELry   )rH   r~   r+   r+   r,   open_h5_for_writing   s   r   data_mapMapping[int, _T1]fname
group_nameCallable[[int], str]c           	      C  sv   t jtjd}t j|d|d"}||j|}|  D ]\}}t||||| qW d    d S 1 s4w   Y  d S r|   )	r   r   r   r   ry   r6   r1   itemsr8   )	r   r   r   r   r~   r   r   n_components	reductionr+   r+   r,   _save_dimension_reduction_h5   s   "r   c                 C  s   t | ||t d S r;   )r   r   )r   r   r   r+   r+   r,   save_dimension_reduction_h5   s   r   library_typec                   s   t | || fdd dS )z*Save PCA dimension reduction info to HDF5.c                   s     d|  S )Nr/   r+   )r   r   r+   r,   <lambda>   s    z2save_pca2_dimension_reduction_h5.<locals>.<lambda>N)r   )r   r   r   r   r+   r   r,    save_pca2_dimension_reduction_h5   s   r   ntuple	type[PCA]	list[PCA]c                 C     d S r;   r+   rH   r   r   r+   r+   r,    load_dimension_reduction_from_h5   s   r   c                 C  r   r;   r+   r   r+   r+   r,   r      s   c                 C  s  t | dq}|jj| }|tkr%t||D ]\}}|  W  d   S d\}}}	t||D ]\}
}|
dr;|}q/|
dsD|
 rG|}q/|
drN|}	q/|rX|rX||g}n|r^|g}n|rd|g}n
|	rj|	g}ntd|W  d   S 1 szw   Y  dS )zUIterate over the output of dim reduction and return a list of all projections if PCA.rw   N)NNNantibody_capturegene_expressionpeakszDExpected at least one of gene_expression, antibody_capture, or peaks)	r   ry   r1   rr   r   rh   
startswithisdigitr!   )rH   r   r   r   r   r/   ZpcaZgex_pcaZab_pcaZatac_pcaZpca_typeZpca_componentsr+   r+   r,   r      s4   



$)
r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r.   r   )r9   r:   )r9   r?   r@   rA   )rH   rI   r)   rJ   rK   rL   rM   rJ   )r   r   r   r   )r   r   r.   ra   r@   rZ   )r   r   r.   ra   r@   re   )rn   ro   rp   r   r   r   )rt   rL   ru   r   rv   rL   )rH   r   r@   r   )r   r   r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   r   r   )rH   r   r   r   r   r   r@   r   )rH   r   r   r   r   ra   r@   rZ   )rH   r   r   r   r   ra   ),__doc__
__future__r   rR   os.pathri   collections.abcr   r   r   r   
contextlibr   typingr   r	   r
   numpyr   Zcellranger.h5_constantsr   "cellranger.analysis.analysis_typesr   cellranger.wrapped_tablesr   r0   r4   r-   r8   r>   rG   rY   rZ   r`   rd   rh   rm   rs   r{   r   r   r   r   r   r+   r+   r+   r,   <module>   sD   














