o
    DfZ                  
   @   s  g d Z ddlmZmZ ddlmZmZmZ dZdZ	dZ
dZee	B e
B eB Zded	ed
efddZdeded
ee fddZdeded
eeeeef  fddZdeded
ee fddZdeded
eeeeef  fddZdeded
eeeeef  fddZdS ))AlignerPrefixComparerSuffixComparerhamming_spherehamming_environmentedit_environmentedit_distance    )IteratorTuple)r   r   r               streturnc           
      C   s   t | }t |}tt|d }td|d D ]B}|d }|d  d7  < td|d D ],}t| |d  ||d  k}t|d | || d ||d  d }	|| }|	||< q,q|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c r    W/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/cutadapt/align.pyr   !   s"   


	r   kc           
      c   s    |dksJ |dkr| V  dS t | }t|| d D ]9}| d| }| | }| |d d }dD ]"}||kr9q2t||d D ]}|| | }	t |	|ksPJ |	V  q@q2qdS )z
    Yield all strings t for which the hamming distance between s and t is exactly k,
    assuming the alphabet is A, C, G, T.
    r   Nr   ACGTr   r   r   )
r   r"   r   r   prefixr   suffixchr   yr    r    r!   r   ;   s(   r   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   Nr$   )r   r"   r   er   r    r    r!   r   T   s   r   c                 c   s    | V  |dkr
dS t | |d D ]I} t| }dD ]*}t|D ]}| d| | }|| |d  V  || |d d  V  q| | V  qt|D ]}| d| | |d d  V  qHqdS )zr
    Apply all possible edits up to edit distance k to string s.
    A string may be returned more than once.
    r   Nr   r#   )naive_edit_environmentr   r   )r   r"   r   r'   r   r%   r    r    r!   r*   b   s     r*   c           	      c   s~    | r	|t |  nd}t| |dt | d}t }t| |D ]}||v r$q|| ||}|dd \}}|||fV  qdS )  
    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.
    r   )Zmax_error_rateflagsZmin_overlapN)r   r   setr*   addZlocate)	r   r"   ZrateZalignerseenr   resultmatcheserrorsr    r    r!   r   w   s   

r   c                 c   s   t | }d}dtt|d dg|d  fg}|r| \}}}t |}|d |kr5||d |d fV  ||| kr<qt||krCq|D ]}	dg|d  }
t |d |
d< dg|d  }td|d D ]]}| |d  |	krpdnd}|dksxJ ||d  | }|
|d  d }|| d }||kr||kr|||d  d|  }}n||kr|||d  }}n||| }}||
|< |||< qd|||	 |
|f qE|sdS dS )r+   ZTGCA r   r   r   N)r   r   r   popr   append)r   r"   r   alphabetZ
work_stackr   r   r2   r   r'   Z
next_costsZnext_matchesr   r   Zdiagleftupr   r   r    r    r!   slow_edit_environment   sF   
r:   N)__all__typingr	   r
   Zcutadapt._alignr   r   r   ZSTART_WITHIN_SEQ1ZSTART_WITHIN_SEQ2ZSTOP_WITHIN_SEQ1ZSTOP_WITHIN_SEQ2Z
SEMIGLOBALstrr   r   r   r   r*   r   r:   r    r    r    r!   <module>   s    

$$(