U
    H$xe                     @   s   d dddddddgZ dd	lmZ dd
lmZmZ ddlmZmZm	Z	m
Z
mZ G dd  d eZeeedddZeeeeeeef  dddZeeee dddZeeeeeeef  dddZeeeeeeef  dddZdS )EndSkipAlignerPrefixComparerSuffixComparerhamming_spherehamming_environmentedit_environmentedit_distance    )IntFlag)IteratorTuple)r   r   r   r   r   c                   @   s$   e Zd ZdZdZdZdZdZdZdS )r   z
    Flags for the Aligner that indicate which ends of reference or query may be skipped at
    no cost. Setting all four flags at the same time results in semiglobal alignment.
                   N)	__name__
__module____qualname____doc__ZREFERENCE_STARTZQUERY_STARTZREFERENCE_ENDZ
QUERY_STOPZ
SEMIGLOBAL r   r   -lib/python3.8/site-packages/cutadapt/align.pyr      s   )streturnc           
      C   s   t | }t |}tt|d }td|d D ]}|d }|d  d7  < td|d D ]X}t| |d  ||d  k}t|d | || d ||d  d }	|| }|	||< qXq.|d S )z
    Return the edit distance between the strings s and t.
    The edit distance is the sum of the numbers of insertions, deletions,
    and mismatches that is minimally necessary to transform one string
    into the other.
    r   r	   )lenlistrangeintmin)
r   r   mncostsjprevimatchcr   r   r   r   %   s     

)r   kr   c                 c   s>   t | }t|d D ]$}t| |D ]}|||| fV  q"qdS )a	  
    Find all strings t for which the hamming distance between s and t is at most k,
    assuming the alphabet is A, C, G, T.

    Yield tuples (t, e, m), where e is the hamming distance between s and t and
    m is the number of matches (equal to len(t) - e).
    r   N)r   r   r   )r   r)   r"   er   r   r   r   r   ?   s    c                 c   s   | V  |dkrdS t | |d D ]} t| }dD ]T}t|D ]<}| d| | }|| |d  V  || |d d  V  q<| | V  q0t|D ]"}| d| | |d d  V  qq dS )z
    Apply all possible edits up to edit distance k to string s
    and yield the resulting strings.
    A string may be returned more than once.
    r	   Nr   ZACGT)naive_edit_environmentr   r   )r   r)   r"   chr&   prefixr   r   r   r+   M   s    r+   c           	      c   s|   | r|t |  nd}t| |dt | d}t }t| |D ]>}||krFq8|| ||}|dd \}}|||fV  q8dS )a  
    Find all strings t for which the edit distance between s and t is at most k,
    assuming the alphabet is A, C, G, T.

    Yield tuples (t, e, score), where e is the edit distance between s and t and
    score is the score of the optimal alignment.
    r	   )Zmax_error_rateflagsZmin_overlapN)r   r   setr+   addZlocate)	r   r)   ZrateZalignerseenr   resultZscoreerrorsr   r   r   py_edit_environmentc   s    

r5   c                 c   s  t | }d}dtt|d dg|d  fg}|r| \}}}t |}|d |krj||d |d fV  ||| krxq.t||krq.|D ]}	dg|d  }
t |d |
d< dg|d  }td|d D ]}| |d  |	krdnd}|dkst||d  | }|
|d  d }|| d }||krN||krN|||d  d|   }}n,||krl|||d   }}n|||  }}||
|< |||< q|||	 |
|f qq.dS )a2  
    Find all strings t for which the edit distance between s and t is at most k,
    assuming the alphabet is A, C, G, T.

    Yield tuples (t, e, m), where e is the edit distance between s and t and
    m is the number of matches in the optimal alignment.

    This is slow and only used in testing.
    ZTGCA r   r	   r   N)r   r   r   popr    AssertionErrorappend)r   r)   r"   ZalphabetZ
work_stackr   r#   Zmatchesr&   r,   Z
next_costsZnext_matchesr$   r'   ZdiagleftZupr(   r!   r   r   r   slow_edit_environmentw   sD    



r;   N)__all__enumr
   typingr   r   Zcutadapt._alignr   r   r   r   r   r   strr   r   r   r+   r5   r;   r   r   r   r   <module>   s"   	  