o
    Uݢg                     @  sd  d Z ddlmZ ddlZddlmZmZ ddlZddl	m
Z ddlmZ ddlmZ g dZdZd:d	d
Zed;d<ddZed=d>ddZed;d?ddZd;d@ddZed;dAddZed;dBd dZd;dCd"dZdDd$d%ZdEd'd(ZG d)d* d*eZed;dFd+d,Ze	dGdHd-d,Ze	d;dId.d,ZedJd/d,ZdGdKd0d,Z	dLdMd4d5Z	dNdOd8d9ZdS )PzBarcode handling functions.    )annotationsN)Literaloverload)z
odin-5K-v2z	visium-v1z	visium-v2z	visium-v3z	visium-v4z	visium-v5zomni-v1zomni-hires-v1zomni-pat7-v1zpseudo-xl-v1zthor-v1zthor-v2zthor-v3z
thor-XL-v1z
thor-XL-v2i  whitelist_namestrc                 C  s   | t v S )a9  Is the barcode a spatial (visium) whitelist?

    Method returns True if the name is present on a predefined list
    of spatial barcode whitelist names.

    Args:
        whitelist_name: string of the whitelist name

    Returns:
        boolean value indicating if the whitelist is from the spatial assay.
    )SPATIAL_WHITELIST)r    r   h/oak/stanford/groups/akundaje/marinovg/programs/cellranger-9.0.1/lib/python/cellranger/barcodes/utils.pyis_whitelist_spatial)   s   r
   Ffilenamestr | bytesas_setLiteral[False]returnlist[bytes]c                 C     d S Nr   r   r   r   r   r	   load_barcode_tsv8      r   .Literal[True]
set[bytes]c                 C  r   r   r   r   r   r   r	   r   <   r   boolset[bytes] | list[bytes]c                 C  r   r   r   r   r   r   r	   r   @   r   c                 C  sl   t | d}dd |D }W d   n1 sw   Y  t|}t|t|kr0td|  |r4|S |S )zLoad barcodes from a tsv file.rbc                 S  s   g | ]
}d |vr|  qS )   #)strip).0xr   r   r	   
<listcomp>G   s    z$load_barcode_tsv.<locals>.<listcomp>Nz'Duplicates found in barcode whitelist: )cr_ioopen_maybe_gzipsetlen	Exception)r   r   Zbc_filebarcodesZbarcode_setr   r   r	   r   D   s   Nonetranslationc                 C  r   r   r   r   r'   r   r   r	   get_barcode_whitelist_pathN   r   r)   c                 C  r   r   r   r(   r   r   r	   r)   R   r   
str | Nonec                 C  sl   | d u rd S t j| r| S tj}|rt j|d}t j|| d }t j|r+|S t j|| d }|S )Nr'   .txt.gz.txt)ospathexistscr_constantsBARCODE_WHITELIST_PATHjoin)r   r'   Zrootdirgztxtr   r   r	   r)   V   s   	list[str]c                  C  s`   t tj} d}g }| D ]!}t jtj|}||r-|ddkr-t j|r-|	| q|S )zFunction to return the set of all whitelist names included in the product.

    Uses some heuristics to search through folders in this directory.

    Returns:
        A list of strings, one per filename
    r,   r+   coordinatesr   )
r-   listdirr0   r1   r.   r2   endswithcountisfileappend)Z	basenamesZallowed_suffixesZ	all_filesnameZ	full_namer   r   r	   get_all_whitelist_filenamesi   s   

r>   	filenamesc                 C  s   |  d}ddd |D S )N,c                 S  s   g | ]}t |qS r   )r)   )r   pr   r   r	   r      s    z/get_barcode_whitelist_paths.<locals>.<listcomp>)splitr2   )r?   pathsr   r   r	   get_barcode_whitelist_paths   s   
rD   c                   @  s   e Zd ZdZdS )WhitelistNotFoundz+Raised when a named whitelist is not found.N)__name__
__module____qualname____doc__r   r   r   r	   rE      s    rE   c                 C  r   r   r   r   r   r   r	   load_barcode_whitelist   r   rJ   c                 C  r   r   r   r   r   r'   r   r   r	   rJ         c                 C  r   r   r   rK   r   r   r	   rJ      rL   c                 C  r   r   r   rK   r   r   r	   rJ      rL   c                 C  s:   t | |}|d u rd S tj|std| t||S )Nz"Unable to find barcode whitelist: )r)   r-   r.   r;   rE   r   )r   r   r'   r.   r   r   r	   rJ      s   

r=   r.   dict[str, str] | Nonec                 C  s   | du r
|du r
dS | dur|durJ | dur4d}dD ]}t jtj| | }t j|r2|} nqn|}|du r<dS i }t|dD ]}|drLqD|	 
 }|d ||d < qD|S )zMap probe BC ids to their collaped sequences.

    If the barcode whitelist exists return the mapping dictionary,
    else, return None.
    Nr6   r#      )r-   r.   r2   r0    BARCODE_WHITELIST_TRANSLATE_PATHr/   r    r!   
startswithr   rB   )r=   r.   	file_path	extensionZfile_extZtranslate_maplinebcsr   r   r	   load_probe_barcode_map   s,   
rX   Tbarcode_whitelist_fnc                 C  s   t | }|r|d u rtj}ntj}nd}|d u r|S |d ur't|t| }nt|}|r9tt|d| t S tt||t S )Nr   rQ   )	rJ   h5_constantsMIN_MEM_GB_NOWHITELIST
MIN_MEM_GBr#   maxnpceilNUM_BARCODES_PER_MEM_GB)rY   
gem_groupsZuse_mindoublebarcode_whitelistZ
min_mem_gbnum_bcsr   r   r	   )get_mem_gb_request_from_barcode_whitelist   s   re   )r   r   )F)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'   r   r   r&   )r   r   r'   r   r   r   )r   r*   r'   r   r   r*   )r   r5   )r?   r   )r   r&   r   r&   )FF)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   r*   r   r   r'   r   )NN)r=   r*   r.   r*   r   rM   )NTF)rY   r*   )rI   
__future__r   r-   typingr   r   numpyr^   cellranger.constants	constantsr0   cellranger.cr_ior    cellranger.h5_constantsrZ   r   r`   r
   r   r)   r>   rD   r$   rE   rJ   rX   re   r   r   r   r	   <module>   sR   



$