o
    Rŀg                     @   s\   d 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 )zBio.Align support for A2M files.

A2M files are alignment files created by align2model or hmmscore in the SAM
Sequence Alignment and Modeling Software System.
    )	Alignment)
interfaces)Seq)	SeqRecordc                   @   s$   e Zd ZdZdZdd ZejjZ	dS )AlignmentWriterz.Alignment file writer for the A2M file format.A2Mc              	   C   s  t |ts	tdg }|jd }t|j|D ]k\}}z|j}W n ty*   d}Y nw z|j}W n ty;   d}Y nw |rJ|	d| d|  n|	d|  d}t||D ]"\}	}
|
dkrh||	
 7 }qY|
dkr{|	dkru|d	7 }qY||	 7 }qY|	| qd
|d
 S )z:Return a string with the alignment in the A2M file format.zExpected an Alignment objectstate > DI-.
)
isinstancer   	TypeErrorcolumn_annotationszip	sequencesidAttributeErrordescriptionappendupperlowerjoin)self	alignmentlinesr   sequencelinenamer   scm r&   A/var/www/html/myenv/lib/python3.10/site-packages/Bio/Align/a2m.pyformat_alignment   s:   




z AlignmentWriter.format_alignmentN)
__name__
__module____qualname____doc__fmtr(   r   r   write_single_alignmentwrite_alignmentsr&   r&   r&   r'   r      s
    r   c                   @   s   e Zd ZdZdZdd ZdS )AlignmentIteratora  Alignment iterator for files in the A2M file format.

    An A2M file contains one multiple alignment. Matches are represented by
    upper case letters and deletions by dashes in alignment columns containing
    matches or deletions only. Insertions are represented by lower case letters,
    with gaps aligned to the insertion shown as periods.  Header lines start
    with '>' followed by the name of the sequence, and optionally a description.
    r   c              	   C   s   g }g }g }|D ]Q}| drO|dd   d d}z|d }W n ty,   d}Y nw z|d }W n ty>   d}Y nw || || |d q|d  | 7  < q|si| j dkrgtdd S d}	|d D ]!}
|
dksy|
	 r~|	d7 }	qo|
d	ks|

 r|	d
7 }	qotd|
 |dd  D ]/}t||	D ]'\}
}|dkr|
dks|
	 sJ q|d
kr|
d	ks|

 sJ qtd|
 qt|D ]\}}| d	d ||< qt|\}}g }t|||D ]\}}}t|}t|||d}|| qt||}i |_|	|jd< |S )Nr
      r   r	   zEmpty file.r   r   r   r   z#Unexpected letter '%s' in alignment)r   r   )
startswithrstripsplit
IndexErrorr   strip_streamtell
ValueErrorisupperislower	Exceptionr   	enumerater   replaceencoder   parse_printed_alignmentr   r   r   )r   streamnamesdescriptionsr   r!   partsr"   r   r   r$   r%   iseqdatacoordinatesrecordsseqrowr    recordr   r&   r&   r'   _read_next_alignmentF   sj   






z&AlignmentIterator._read_next_alignmentN)r)   r*   r+   r,   r-   rL   r&   r&   r&   r'   r0   :   s    	r0   N)
r,   	Bio.Alignr   r   Bio.Seqr   Bio.SeqRecordr   r   r0   r&   r&   r&   r'   <module>   s   '