a
    CPÎjÐ*  ã                   @   sz  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m	Z	m
Z
mZmZ d dlZ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 G dd	„ d	eƒZG d
d„ deƒZd*ee ee ee eedœdd„Zeedœdd„Z d+eeef eddœdd„Z!d,eeef eddœdd„Z"ee#e dœdd„Z$dd„ Z%dd„ Z&d-ee ee ed œd!d"„Z'd#d$„ Z(d%d&„ Z)ed'œd(d)„Z*dS ).é    N)Úpartial)Úislice)ÚPath)ÚIterableÚIteratorÚOptionalÚUnion)Úlogger)Ú	betabinomc                   @   s   e Zd ZdZdS )ÚConfigzBConfig contains the parameters related to specific alignment file.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   ú]/mounts/lovelace/software/anaconda3/envs/metadmg/lib/python3.9/site-packages/metaDMG/utils.pyr      s   r   c                   @   s\   e Zd ZdZee dœdd„Zeedœdd„Zedœdd	„Z	edœd
d„Z
ddœdd„ZdS )ÚConfigszkConfigs contains the parameters related to config file.
    Inherits from dict. Implements iterations.
    )Úreturnc                 c   sæ   | d d }| d d }| d   ¡ }|D ]¸}t| ƒ}||d< |d | |d< ||› d� |d< |d	 dkr~||› d
� |d< n||› d� |d< ||› d� |d< ||› d� |d< |d d | |d< ||› d� |d< |V  q(dS )zjIteration

        Yields
        ------
        Iterator[Config]
            Allow for iteration
        Ú
output_dirÚlcaZpmdÚsamplesÚsampleÚbamz.mismatches.txt.gzZpath_mismatches_txtÚdamage_modez.mismatches.stat.txt.gzZpath_mismatches_statz	.stat.txtz.lca.txt.gzZpath_lcaz.log.txtZpath_lca_logÚtmpÚpath_tmpz.pmd.txt.gzÚpath_pmdN)Úkeysr   )ÚselfZdir_lcaZdir_pmdr   r   Úconfigr   r   r   Ú__iter__    s"    ÿzConfigs.__iter__)Únr   c                 C   s   t t| |dƒƒS )z·Gets the n'th config

        Parameters
        ----------
        n
            The index

        Returns
        -------
        Config
            A single configuration
        N)Únextr   )r   r!   r   r   r   Úget_nthA   s    zConfigs.get_nthc                 C   s   | j dd�S )zpGet the first config

        Returns
        -------
        Config
            A single configuration
        r   )r!   )r#   ©r   r   r   r   Ú	get_firstP   s    zConfigs.get_firstc                 C   s   t | d  ¡ ƒS )zmThe number of configs

        Returns
        -------
        int
            The number of configs
        r   )Úlenr   r$   r   r   r   Ú__len__Z   s    zConfigs.__len__Nc                 C   s„   t | d t| d ƒƒ}| d }|| }tjdd�}tjdd�}||kr`t d|› d|› d	�¡ n ||kr€t d|› d
|› d�¡ dS )zECompare the number of configs to the number of parallel_samples used.Úparallel_samplesr   Úcores_per_sampleT)ZlogicalFzThe total number of jobs z0 are higher than the number of parallel_samples z. Do not do this unless you know what you are doing. Try decreasing either 'parallel_samples' or 'parallel_samples-per-sample'.z5 are higher than the real number of parallel_samples z1 (non-logical). This might decrease performance. N)Úminr&   ÚpsutilÚ	cpu_countr	   ÚwarningÚinfo)r   r(   r)   ZN_jobsZ	max_coresZmax_cores_realr   r   r   Úcheck_number_of_jobsd   s"    ÿÿÿÿzConfigs.check_number_of_jobs)r   r   r   r   r   r   r    Úintr#   r%   r'   r/   r   r   r   r   r      s   !

r   F)Úconfig_fileÚlog_portÚlog_pathÚforcer   c           
      C   sd  | du rt dƒ} |  ¡ s*t d¡ t ¡ ‚t d| › d�¡ t| dƒ�}t 	|¡}W d  ƒ n1 sf0    Y  t
|ƒ}||d< ||d< | d	d
¡ | dd¡ | dd¡ | dd¡ ||d< g d¢}|D ]}|| rÌt || ƒ||< qÌ|d  ¡ D ]\}}	t |	ƒ|d |< qö| ¡ D ],\}}	t|	tƒ�r|	 ¡ �rt|ƒ||< �q|d �rTdnd|d< t|ƒS )aÅ  Create an instance of Configs from a config file

    Parameters
    ----------
    config_file
        The config file to load
    log_port
        Optional log port, by default None
    log_path
        Optional log path, by default None
    force
        Whether or not the computations are force, by default False

    Returns
    -------
        An instance of Configs

    Raises
    ------
    typer.Abort
        If not a proper config file
    Núconfig.yamlz*Error! Please select a proper config file!zUsing z as config file.Úrr2   r3   Úforward_onlyFr)   é   r   r   Z	min_readsr   r4   )ÚnamesÚnodesZacc2taxr   r1   r   Úcustom_database)r   Úexistsr	   ÚerrorÚtyperÚAbortr.   ÚopenÚyamlZ	safe_loadÚupdate_old_configÚ
setdefaultÚitemsÚ
isinstanceÚstrÚisdigitr0   r   )
r1   r2   r3   r4   ÚfileÚdÚpathsÚpathÚkeyÚvalr   r   r   Úmake_configs|   s8    
(
rN   )rI   r   c                 C   sr   d| v r| S t  d¡ ddddddd	d
d
dddddœ}i }|  ¡ D ] \}}||v rZ|| }|||< qB| d¡ |S )NÚversionzGUsing an old version of the config file. Please remake the config file.ÚmetaDMG_cppZmin_mapping_qualityZmin_edit_distZmax_edit_distZmin_similarity_scoreZmax_similarity_scoreZweight_typer   r;   r(   r)   r1   )zmetaDMG-lcaZminmapqZeditdistminZeditdistmaxZsimscorelowZsimscorehighZ
weighttypeZstorage_dirÚdirZfix_ncbiZcoresr)   Zconfig_pathZforced)r	   r-   rD   Úpop)rI   Z	d_old2newZd_newrL   Úvaluer   r   r   rB   Ã   s4    ÿó

rB   )rH   Ú
missing_okr   c                 C   s   t | ƒj|d� d S )N)rT   )r   Úunlink)rH   rT   r   r   r   Úremove_fileë   s    rV   )rK   rT   r   c                 C   sZ   z<t | ƒ} |  ¡ D ]}| ¡ r(t|ƒ qt|ƒ q|  ¡  W n tyT   |sP‚ Y n0 dS )zmRemove everything in a directory

    Parameters
    ----------
    path
        Directory to be deleted
    N)r   ÚiterdirÚis_filerV   Úremove_directoryÚrmdirÚFileNotFoundError)rK   rT   Úchildr   r   r   rY   ð   s    	

rY   )Úsr   c                 C   s   t  d| ¡S )z˜Split a string by comma, space, or both.

    Parameters
    ----------
    s
        Input string

    Returns
    -------
        List of strings
    z[^,\s]+)ÚreÚfindall)r]   r   r   r   Úsplit_string	  s    r`   c                  C   s4   zdd l } W n" ty.   tdƒ t ¡ ‚Y n0 d S )Nr   zAThe 'fit' extras has to be installed: pip install "metaDMG[fit]" )ZmetaDMG.fitÚModuleNotFoundErrorÚprintr>   r?   ©ZmetaDMGr   r   r   Úcheck_metaDMG_fit!  s
    rd   c                  C   s4   zdd l } W n" ty.   tdƒ t ¡ ‚Y n0 d S )Nr   zAThe 'viz' extras has to be installed: pip install "metaDMG[viz]" )ZmetaDMG.vizra   rb   r>   r?   rc   r   r   r   Úcheck_metaDMG_viz*  s
    re   )r1   Úresults_dirr   c                 C   sD   | dur|durt dƒ‚|r |S | du r0tdƒ} t| ƒ}|d d S )a¶  Helper function that gets the results directory from either the
    config file or the results directory directly.

    Parameters
    ----------
    config_file
        Config file, by default None
    results_dir
        Results directory, by default None

    Returns
    -------
        Path to the results directory

    Raises
    ------
    AssertionError
        If both config file and results directory are set, raise error
    Nz2'config_file' and 'results_dir' cannot both be setr5   r   Úresults)ÚAssertionErrorr   rN   )r1   rf   Zconfigsr   r   r   Úget_results_dir6  s    ri   c                 C   sÎ  t dd„ | jD ƒƒ}|rd}nd}d| jv r2d}nd}| |› d� j}| |› d	� j}| |› d
� j}| |› d� j}tdd„ | jD ƒƒ}|ržt |¡d }	n(t t |¡d t d| d d¡g¡}	|	 d¡}	dd„ | jD ƒ}
| jd d …|
f j}|d| t 	|	¡d   | }|| }d| | }t
||j|jd�}t ¡ �& t d¡ | ¡ | }W d   ƒ n1 �sn0    Y  d|t |¡< tjtj|jdd„ |	 ¡ D ƒd�tj|dd„ |	 ¡ D ƒd�fdd�}|S )Nc                 S   s   g | ]}|d k‘qS )Zdamager   )Ú.0Úcolumnr   r   r   Ú
<listcomp>a  ó    z-get_single_fit_prediction.<locals>.<listcomp>Ú ZMAP_zk-1FTÚAÚqÚcÚphic                 s   s*   | ]"}|  d ¡rt| d¡d ƒV  qdS )zk+ú+r8   N)Ú
startswithr0   Úsplit©rj   Únamer   r   r   Ú	<genexpr>r  s   z,get_single_fit_prediction.<locals>.<genexpr>r8   éÿÿÿÿ)ry   r8   c                 S   s    g | ]}|  d ¡p|  d¡‘qS )zN+zN-)rt   rv   r   r   r   rl   ~  s   )r!   ÚaÚbÚignorer   c                 S   s   g | ]}d |d›�‘qS )ÚDxrs   r   ©rj   Úxir   r   r   rl   “  rm   )Úcolumnsc                 S   s   g | ]}d |d›�‘qS )ZDx_stdrs   r   r~   r   r   r   rl   ”  rm   ©Zaxis)Úanyr€   ÚvaluesÚmaxÚnpZarangeZhstackZreshapeZilocÚabsÚsp_betabinomÚTÚwarningsÚcatch_warningsÚfilterwarningsÚstdÚisnanÚpdÚconcatZ	DataFrameÚflatten)Ú
df_resultsZBayesianÚprefixr7   ro   rp   rq   rr   Zmax_positionÚxZmask_NÚNr}   ÚalphaÚbetaÚdistrŒ   Údf_Dxr   r   r   Úget_single_fit_prediction_  sN    
ÿ ÿ
ÿ

,ýú	r™   c                 C   s"   t | ƒ}tj| jdd�|fdd�S )NT)Zdropr8   r�   )r™   rŽ   r�   Zreset_index)r‘   r˜   r   r   r   Úappend_fit_predictionsœ  s    rš   )r   c                 C   s¦   ddl }ddl}ddl}| d }|jjddd� | |› d¡�T}|j| | d › d| d	 › �¡|jd
�}|jdg|j	|d�}| 
¡  W d  ƒ n1 s˜0    Y  dS )zîRun the PMD command from metaDMG-cpp and output the result to the gzipped txt_out

    Parameters
    ----------
    alignment_file
        Alignment file to compute the PMD scores on
    txt_out
        The (gzipped) output txt file
    r   Nr   T)ÚparentsÚexist_okZwtrP   z pmd r   )ÚstdoutÚgzip)Ústdinr�   )rž   ÚshlexÚ
subprocessÚparentÚmkdirr@   ÚPopenru   ÚPIPEr�   Úcommunicate)r   rž   r    r¡   Ztxt_outZzip_outÚcppÚzipr   r   r   Úrun_PMD¤  s     þýr©   )NNF)F)F)NN)+r^   r‰   Ú	functoolsr   Ú	itertoolsr   Úpathlibr   Útypingr   r   r   r   Únumpyr…   ZpandasrŽ   r+   r>   rA   Z	logger_ttr	   Zscipy.statsr
   r‡   Údictr   r   r0   rF   ÚboolrN   rB   rV   rY   Úlistr`   rd   re   ri   r™   rš   r©   r   r   r   r   Ú<module>   sP   c   üûG(	  þý)=