o
    NrfF!                     @   s   d dl Z d dlZd dlZd dlmZ d dlZd dlZdZdZ	dZ
dZdZdd Zd	d
 Zdd Zdd Zdd Zd(ddZdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% ZG d&d' d'eZdS ))    Nzhiglass-manage-containerzhiglass-manage-networkzhiglass-manage-redisz/usr/local/etc/redis/redis.confz
db.sqlite3c                    sb   t  }t| d t fdddD ]}|| qW d    | S 1 s(w   Y  | S )Nrbc                      s
     dS )Ni   )read fr   ^/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/higlass_manage/common.py<lambda>   s   
 zmd5.<locals>.<lambda>    )hashlibmd5openiterupdate	hexdigest)fnameZhash_md5chunkr   r   r   r      s   
r   c                 C      d t| S Nz{}-{})formatCONTAINER_PREFIXhg_namer   r   r   hg_name_to_container_name      r   c                 C   r   r   )r   NETWORK_PREFIXr   r   r   r   hg_name_to_network_name   r   r   c                 C   r   r   )r   REDIS_PREFIXr   r   r   r   hg_name_to_redis_name   r   r   c                 C   s8   t  }t| }|j|}|d d d d d }|S )NZ
HostConfigZPortBindingsz80/tcpr   ZHostPortdockerfrom_envr   apiinspect_container)r   clientcontainer_nameconfigportr   r   r   get_port#   s
   r'   SITE_URLc                    sp   t  }t| }|j|} fdd|d d D }|sdS t|dkr,td||\}|d\}}|S )	z
    get SITE_URL for a given container
    using container docker-config, assuming
    there is not more than one SITE_URL in
    container's env.

    Yields "localhost" when no SITE_URL entries
    detected.
    c                    s   g | ]} |v r|qS r   r   ).0s	_SITE_URLr   r   
<listcomp>=   s    z get_site_url.<locals>.<listcomp>ZConfigZEnvzhttp://localhost   z+There are multiple SITE_URL entry in {} env=)	r   r    r   r!   r"   len
ValueErrorr   split)r   r,   r#   r$   r%   Zsite_url_entriesZsite_url_r   r+   r   get_site_url-   s   r4   c                 C   s   |du r)t | }td| |du r)t| }tdtjd |dur'td| dS |du rPt|}td| |du rPt|}tdtjd |durPtd	| ||fS )
a  
    If no filetype or datatype are provided, add them
    based on the given filename.

    Parameters:
    ----------
    filename: str
        The name of the file
    filetype: str
        The type of the file (can be None)
    datatype: str
        The datatype for the data in the file (can be None)

    Returns:
    --------
    (filetype, datatype): (str, str)
        Filled in filetype and datatype based on the given filename
    NzInferred filetype:z<Unknown filetype, please specify using the --filetype optionfilez;Based on the filename, you may want to try the filetype: {}NNzInferred datatype:z<Unknown datatype, please specify using the --datatype optionz;Based on the filetype, you may want to try the datatype: {})infer_filetypeprintrecommend_filetypesysstderrr   infer_datatyperecommend_datatype)filenamefiletypedatatypeZrecommended_filetypeZrecommended_datatyper   r   r   fill_filetype_and_datatypeM   s@   

rB   c                 C   s:   t | }t | d dkrdS t | d dkrdS d S )Nr.   z.bedbedfilez.bedpeZbedpe)opsplitext)r?   extr   r   r   r:      s   
r:   c                 C   s   | dkrdS d S )NrC   bedliker   r@   r   r   r   r>      s   r>   c                 C   sL   | dkrdS | dkrdS | dkrdS | dkrdS | d	krd
S | dkr$dS dS )Nmatrix)Zheatmapcentervector)zhorizontal-bartopzgene-annotations)zhorizontal-gene-annotationsrL   Z
chromsizes)zhorizontal-chromosome-labelsrL   2d-rectangle-domains)rM   rJ   rG   )rG   rL   r7   r   )rA   r   r   r   datatype_to_tracktype   s   rN   c                 C   sz   t | \}}| dks| dkrdS | dks!| dkr#dS | dkr+dS | d	kr3d
S | dkr;dS d S )Nz.bwz.bigwigbigwigz.mcoolz.coolcoolerz.htimetime-interval-jsonz.hitilehitilez.beddbbeddb)rD   rE   lower)r?   r3   rF   r   r   r   r8      s   r8   c                 C   s@   | dkrdS | dkrdS | dkrdS | dkrdS | dkrd	S d S )
NrP   rI   rO   rK   rQ   ztime-intervalrR   rS   rG   r   rH   r   r   r   r=      s   r=   c	                 C   s  |s7t | }	t|	st|	 t|d }
t|	|
}||kr6t|r0td| t| t	|| n|}
|d urBd
|nd}|d urMd
|nd}|d urXd
|nd}td
| t }td|  t| }|j|}|rd	d

|
dd d
||||| }nd	d
|
dd d
||||| }|d ur|d
|7 }nt }|d
|7 }td| ||\}}|dkrtd|dtjd d S |S )Nr.   z(Removing existing file in temporary dir:z--coordSystem {} z--name "{}"z--project-name "{}"zname_text: {}zhg_name:z9python higlass-server/manage.py ingest_tileset --filenamez {} z\ z1 --filetype {} --datatype {} {} {} {} --no-uploadz /tmp/{}z% --filetype {} --datatype {} {} {} {}z	 --uid {}zcommand:r   zERROR:utf8r5   )get_temp_dirrD   existsosmakedirsr2   joinr9   removelinkr   r   r    r   
containersgetreplaceslugidniceZexec_rundecoder;   r<   )r   filepathr@   rA   ZassemblynameuidZ	no_uploadproject_nametemp_dirr?   Zto_import_pathZcoordSystemZ	name_textZproject_name_textr#   r$   	containercommand	exit_codeoutputr   r   r   import_file   sb   






	

rn   c                 C   sn   t  }t| }|j|}td|d d  |d d dkr"t |d D ]}|d dkr4|d   S q&d S )	NstateStateZRunningTMountsDestinationz/tmpSource)r   r    r   r!   r"   r9   HiGlassNotRunningExceptionr   r#   r$   r%   mountr   r   r   rX     s   rX   c                 C   sF   t  }t| }|j|}|d D ]}|d dkr |d   S qd S )Nrq   rr   z/datars   r   ru   r   r   r   get_data_dir  s   rw   c                   @   s   e Zd ZdS )rt   N)__name__
__module____qualname__r   r   r   r   rt   $  s    rt   )r(   )r   r
   rZ   Zos.pathpathrD   rb   r;   r   r   r   Z
REDIS_CONFZSQLITEDBr   r   r   r   r'   r4   rB   r:   r>   rN   r8   r=   rn   rX   rw   	Exceptionrt   r   r   r   r   <module>   s6    

 <F
