a
    d                     @   s   d dl Z d dlZd dlmZmZ d dlmZ d dlZd dlZd dlZdddZ	dd Z
d	d
 Zdd Zdd Zdd Zdd ZdS )    N)CalledProcessError
check_call)__version__Rscriptsc                 C   s   t jtjd || S )z=Construct a path to the mapdamage package data given the namer   )ospathjoin	mapdamage__path__)namefolder r   0lib/python3.9/site-packages/mapdamage/rscript.pyconstruct_path
   s    r   c                 C   s   | j d d }| j d d }| j d d }td}d|||| j|| j| j| j | jtg}tt	t
t|}|dkr| jstd|  dS td	 d
S dS )z^
  Calls the R script to make the plots, takes in the arguments
  from parameter processing
  /misincorporation.txtzdnacomp.txtzFragmisincorporation_plot.pdfzmapDamage.RRscriptr   zpdf %s generatedError: plotting with R failed   N)r   r   ZrefplotZreadplotZymaxtitler   
subprocesscalllistmapstrquietprint)optfmutZfcompr   scriptr   coder   r   r   plot   s    r!   c              
   C   s   | j d d }| j d d }| j d d }td}d||||| j| jtt| jg	}tt	t
t|}|dkrrdS td d	S d
S )zS optional plots for length distribution
  and cumulative C>T mutations, per strand r   r   zlgdistribution.txtzLength_plot.pdfz	lengths.Rr   r   r   r   N)r   r   lengthr   r   bool_to_intr   r   r   r   r   r   r   )r   r   Zflengthoutputr   r   r    r   r   r   	opt_plots0   s    r%   c                 C   sP   t tjd0}td}tjd|d| g||dW  d   S 1 sB0    Y  dS )z(Checks if a necessary R library is here.wzstats/checkLibraries.Rr   --args)stdoutstderrN)openr   devnullr   r   r   )r   r+   Zrpar   r   r   check_R_one_libC   s    r,   c                  C   s   g d} g }| D ]}t |r|| qt|dkr^| }tdd| d | d  dS t|dkr~td|d   dS dS d	S )
zL
    Checks if the necessary R libraries are here, signal
    otherwise
    )ZinlineZggplot2ZgamZRcppZRcppGSLr   z#Missing the following R libraries 'z', 'z' and ''z Missing the following R library r   N)r,   appendlenpopr   r   )ZlibsZmissing_liblibZlast_eler   r   r   check_R_libL   s&    r2   c                 C   s>  dt dd| j| j| j| jt| jt| jt| j t| j	 dt| j
t| j | j| jd t d| jd t| jt| jt| j| jd t| jt| jg}d	d
 |D }tt}|d |dd|f  t }zt| W n4 ty  } z|d |W Y d}~n
d}~0 0 |dt | f  dS )zE
    Runs the Bayesian estimation program, using the options opt
    r   zstats/runGeneral.Rr'   r   r   zstats/z
/Stats_outz/acgt_ratio.csvc                 S   s   g | ]}t |qS r   )r   ).0ir   r   r   
<listcomp>       zrun_stats.<locals>.<listcomp>zPerforming Bayesian estimateszCall: %s z0The Bayesian statistics program failed to finishNz*Bayesian estimates completed in %f seconds)r   ZrandZburnZadjustiterr#   ZforwardreverseZvar_dispZ
diff_hangsZ	fix_nicksZsingle_strandedZ
seq_lengthr   intverboser   Zjukes_cantorZuse_raw_nick_freqZtheme_bwloggingZ	getLogger__name__infodebugr   timer   r   error)r   argloggerZ
start_timeer   r   r   	run_statsf   sJ    





rE   c                 C   s   | rdnd}|S )z# return 0 for False and 1 for True r   r   r   )Zbooleanresultr   r   r   r#      s    r#   )r   )r   r   r   r   Zmapdamage.versionr   r	   r<   r@   r   r!   r%   r,   r2   rE   r#   r   r   r   r   <module>   s   
"	.