o
    Uݢgb	                     @   s^   d Z ddlZddlZddlZddlmZ dd Z	dddZ
dd	 Zd
d Zdd Zdd ZdS )z'Utils for feature-barcoding technology.    Nc                 C   sx   t | gs	tdt }t| d}|D ]}||dd   qW d    n1 s-w   Y  t|}|  |S )Nz"Filtered barcodes file not presentrb   ,)	all_files_present
ValueErrorsetopenaddsplitstriplistsort)filtered_barcodesZgex_cell_listfline r   g/oak/stanford/groups/akundaje/marinovg/programs/cellranger-9.0.1/lib/python/cellranger/feature/utils.pyget_gex_cell_list   s   
r   Fc                 C   sZ   | j j}|rtj| jj dd |D | jd}|S tj| j dd |D | jd}|S )zJGiven a CountMatrix object with feature counts, return a pandas dataframe.c                 S      g | ]}|j qS r   id.0r   r   r   r   
<listcomp>(       z,get_feature_counts_as_df.<locals>.<listcomp>)columnsindexc                 S   r   r   r   r   r   r   r   r   .   r   )r   r   )feature_reffeature_defspd	DataFramemTtoarraybcs)Zfeature_counts_matrixget_transposer   Zfeature_counts_dfr   r   r   get_feature_counts_as_df"   s   
r&   c                 C   s(   | d u p|   d dkp|   d dkS )Nr      )	get_shape)matrixr   r   r   check_if_none_or_empty4   s   (r*   c                 C   sD   t |d}tj| |dddd W d    d S 1 sw   Y  d S )Nw   T),z: )indent	sort_keys
separators)r   tk_safe_json
dump_numpy)Z
input_dictZout_file_namer   r   r   r   write_json_from_dict8   s   "r3   c                 C   s   t tt| d dd S )Ni  r'   k)strnproundfloat)Znum_reads_per_cellr   r   r   get_depth_stringC   s   r9   c                 C   s<   | d u rdS t dd | D rdS tdd | D sdS dS )NFc                 s   s    | ]}|d u V  qd S Nr   r   fpathr   r   r   	<genexpr>K   s    z$all_files_present.<locals>.<genexpr>c                 s   s    | ]	}t j|V  qd S r:   )ospathisfiler;   r   r   r   r=   N   s    T)anyall)Zlist_file_pathsr   r   r   r   G   s   r   )F)__doc__r>   numpyr6   pandasr   tenkit.safe_json	safe_jsonr1   r   r&   r*   r3   r9   r   r   r   r   r   <module>   s   
