o
    Rŀg%                     @   sd   d 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	 G dd dej
Z
G d	d
 d
ejZdS )aU  Bio.Align support for the "chain" pairwise alignment format.

As described by UCSC, a chain file stores a series of pairwise alignments in a
single file. Typically they are used for genome to genome alignments. Chain
files store the lengths of the aligned segments, alignment gaps, and alignment
scores, but do not store the aligned sequences.

See https://genome.ucsc.edu/goldenPath/help/chain.html.

You are expected to use this module via the Bio.Align functions.

Coordinates in the chain file format are defined in terms of zero-based start
positions (like Python) and aligning region sizes.
    N)	Alignment)
interfaces)Seq)	SeqRecordc                   @   s   e Zd ZdZdZdd ZdS )AlignmentWriterz5Alignment file writer for the UCSC chain file format.chainc                 C   s  t |ts	td|j}|jsdS |j\}}z|j}W n ty&   d}Y nw z|j}W n	 ty5   Y nw z|j}W n tyF   d}Y nw z|j}W n	 tyU   Y nw t	|}t	|}g }	z|j
}
W n typ   d}
Y nw |d |d krd}| }||dd	d	f  |dd	d	f< nd
}|d |d krd}| }||dd	d	f  |dd	d	f< nd
}|d }|d }|d }|d }z|jd }W n/ ttfy   d|
dd| d| d| d| d| d| d| d| d| d| }Y n(w d|
dd| d| d| d| d| d| d| d| d| d| d| }|	| |d	d	df \}}d}d}d}|d	d	dd	f  D ]\}}||kr|dkrj|| }| d| d| }|	| d}d}d}n|dkr| d| d| }|	| d}|| }n|| }|}qB||kr|dkr|| }| d| d| }|	| d}d}d}n|dkr| d| d| }|	| d}|| }n|| }|}qB|dkr|| }|| }|dkr|dkr|}n1| d| d| }|	| d}d}n| d| d| }|	| || }|| }|}d}d}||kr/td| d| d|}|}qB|dks?|dkrLd| d| }|	| | }|	| d}|	| d|	d }|S )z>Return a string with one alignment formatted as a chain block.zExpected an Alignment object querytargetr   )r   r   )r   -N+)   r   )r   r   r   idzchain g 	z4Expected equal step size in target and query (found z and )z0	
)
isinstancer   	TypeErrorcoordinatessize	sequencesr   AttributeErrorseqlenscorecopyannotationsKeyErrorappend	transpose
ValueErrorjoin)self	alignmentr   r
   r	   qNametNametSizeqSizelinesr   tStrandqStrandtStarttEndqStartqEndchainIDlinestepqGaptGaptStepqStepblock r:   C/var/www/html/myenv/lib/python3.10/site-packages/Bio/Align/chain.pyformat_alignment#   s   






""LN
 


















z AlignmentWriter.format_alignmentN)__name__
__module____qualname____doc__fmtr<   r:   r:   r:   r;   r      s    r   c                   @   s$   e Zd ZdZdZdd Zdd ZdS )AlignmentIteratora[  Alignment iterator for UCSC chain files.

    Each chain block in the file contains one pairwise alignment, which are
    loaded and returned incrementally.  The alignment score is scored as an
    attribute of the alignment; the ID is stored under the key "id" in the
    dictionary referred to by the annotations attribute of the alignment.
    r   c                 C   s,   zt || _W d S  ty   d | _Y d S w )N)next_lineStopIteration)r%   streamr:   r:   r;   _read_header   s
   zAlignmentIterator._read_headerc                 C   s,  | j d u rd S | j }d | _ | }t|dkrd }nt|dkr%|d }ntd|d dkr3tdt|d }|d }t|d	 }|d
 }t|d }	t|d }
|d }t|d }|d }t|d }t|d }d}d}|g}|g}|D ]a}| }|d dkr|| _  nQt|d }||7 }||7 }|| || t|dkr n1t|d }t|d }|dkr||7 }|| || |dkr||7 }|| || qytd |d}td |d}t||dd}t||dd}||g}t	
||g}|dd d f  |	7  < |dd d f  |7  < |dkrn|dkr4||dd d f  |dd d f< ntd| d|dkrBn|dkrX||dd d f  |dd d f< ntd| dt||}||_|d urrd|i|_zt|}| dkrt|}|| _ W |S  ty   d | _ Y |S w )N      zEexpected line with 12 or 13 columns at the beginning of a chain blockr   r   z9expected first line of chain block to start with 'chain 'r                        	   
      )lengthr   )r   descriptionr   r   z%tStrand should be '+' or '-' (found 'z')z%qStrand should be '+' or '-' (found 'r   )rD   splitr   r#   floatintr!   r   r   nparrayr   r   r   rC   striprE   )r%   rF   r3   wordsr2   r   r(   r)   r,   r.   r/   r'   r*   r-   r0   r1   	qPosition	tPositionqStartstStartsr4   r6   r5   target_sequencequery_sequencetarget_recordquery_recordrecordsr   r&   r:   r:   r;   _read_next_alignment   s   









"

"


z&AlignmentIterator._read_next_alignmentN)r=   r>   r?   r@   rA   rG   rf   r:   r:   r:   r;   rB      s
    rB   )r@   numpyrY   	Bio.Alignr   r   Bio.Seqr   Bio.SeqRecordr   r   rB   r:   r:   r:   r;   <module>   s    