B
    b                 @   s   d Z ddlZddlmZ ddlmZ dZdZdZ	dZ
dd	d
gZdd ZdZdd ZedZdd Zdd ZG dd dZeee	e
fddZdd Zdd Zedkre  e  dS )aw  Run and process output from the Wise2 package tool dnal.

Bio.Wise contains modules for running and processing the output of
some of the models in the Wise2 package by Ewan Birney available from:
ftp://ftp.ebi.ac.uk/pub/software/unix/wise2/
http://www.ebi.ac.uk/Wise2/

Bio.Wise.psw is for protein Smith-Waterman alignments
Bio.Wise.dnal is for Smith-Waterman DNA alignments
    N)	getoutput)Wise   Zdnalz-albz	-noprettyc             C   s\   t d d  }|dt| g |dt|g |dt| g |dt| g |S )Nz-matchz-misz-gapz-ext)_CMDLINE_DNALextendstr)matchmismatchgap	extensionZres r   ,lib/python3.7/site-packages/Bio/Wise/dnal.py_build_dnal_cmdline#   s    r   zfgrep -c '%s' %sc             C   s   t tt| |f S )N)int
_getoutput_CMDLINE_FGREP_COUNT)patternfiler   r   r   _fgrep_count0   s    r   z^\[([^:]+):[^\[]+\[([^:]+):c             C   s   t dd t|  D S )Nc             s   s   | ]}t |d  V  qdS )   N)r   ).0Zcoordr   r   r   	<genexpr>9   s   z#_alb_line2coords.<locals>.<genexpr>)tuple_re_alb_line2coordsr
   groups)liner   r   r   _alb_line2coords7   s    r   c             C   s\   t | }d }d }x$|D ]}|dr|s.|}q|}qW |d krFddgS tttt||g S )N[)r   r   )open
startswithlistzipmapr   )filenameZalbZ
start_lineZend_liner   r   r   r   _get_coords>   s    

r&   c               @   s,   e Zd ZdZdd Zdd ZdZdd Zd	S )

Statisticsz(Calculate statistics from an ALB report.c             C   s   t d| || _t d| || _t d| || _||kr@d| _nt d| || _|| j || j  || j  || j  | _| js| js| js| jrt|| _n
ddg| _dS )zInitialize the class.z"SEQUENCE" %sz"INSERT" %sr   )r   r   N)r   matches
mismatchesgaps
extensionsZscorer&   coords)selfr%   r
   r   r   r   r   r   r   __init__V   s    *zStatistics.__init__c             C   s   | j | j | j  S )z"Calculate the fraction of matches.)r(   r)   )r-   r   r   r   identity_fractionm   s    zStatistics.identity_fractionz4identity_fraction	matches	mismatches	gaps	extensionsc             C   s*   d dd |  | j| j| j| jfD S )z%Statistics as a tab separated string.	c             s   s   | ]}t |V  qd S )N)r	   )r   xr   r   r   r   v   s   z%Statistics.__str__.<locals>.<genexpr>)joinr/   r(   r)   r*   r+   )r-   r   r   r   __str__s   s    zStatistics.__str__N)__name__
__module____qualname____doc__r.   r/   headerr3   r   r   r   r   r'   S   s
   r'   c             K   sn   t ||||}tj|| f|}yt|j||||S  tk
rh   y|d  dS  tk
rb    Y nX Y nX dS )zSAlign a pair of DNA files using dnal and calculate the statistics of the alignment.Zdry_runN)r   r   alignr'   nameAttributeErrorKeyError)Zpairr
   r   r   r   keywdsZcmdlineZ	temp_filer   r   r   r9      s    	r9   c                 sX   ddl } t| jdd  td fdddD  td	    td
 j  dS )zCommand line implementation.r   Nr      
c             3   s    | ]}d |t  |f V  qdS )z%s: %sN)getattr)r   attr)statsr   r   r      s   zmain.<locals>.<genexpr>)r(   r)   r*   r+   zidentity_fraction: %sz
coords: %s)sysr9   argvprintr2   r/   r,   )rC   r   )rB   r   main   s    
rF   c              O   s,   dd l }dd l}|j|jt f| | d S )Nr   )doctestrC   Ztestmodmodulesr4   )argsr=   rG   rC   r   r   r   _test   s    rJ   __main__)r7   re
subprocessr   r   ZBior   Z_SCORE_MATCHZ_SCORE_MISMATCHZ_SCORE_GAP_STARTZ_SCORE_GAP_EXTENSIONr   r   r   r   compiler   r   r&   r'   r9   rF   rJ   r4   r   r   r   r   <module>   s0   


0