o
    Rŀg&                     @   sl   d Z ddl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 )az  Bio.Align support for BED (Browser Extensible Data) files.

The Browser Extensible Data (BED) format, stores a series of pairwise
alignments in a single file. Typically they are used for transcript to genome
alignments. BED files store the alignment positions and alignment scores, but
not the aligned sequences.

See http://genome.ucsc.edu/FAQ/FAQformat.html#format1

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

Coordinates in the BED format are defined in terms of zero-based start
positions (like Python) and aligning region sizes.

A minimal aligned region of length one and starting at first position in the
source sequence would have ``start == 0`` and ``size == 1``.

As we can see in this example, ``start + size`` will give one more than the
zero-based end position. We can therefore manipulate ``start`` and
``start + size`` as python list slice boundaries.
    N)	Alignment)
interfaces)Seq)	SeqRecordc                       s*   e Zd ZdZd fdd	Zdd Z  ZS )AlignmentWriterzHAlignment file writer for the Browser Extensible Data (BED) file format.   c                    s.   |dk s|dkrt dt | || _dS )zCreate an AlignmentWriter object.

        Arguments:
         - target    - output stream or file name
         - bedN      - number of columns in the BED file.
                       This must be between 3 and 12; default value is 12.

           r   zbedN must be between 3 and 12N)
ValueErrorsuper__init__bedN)selftargetr   	__class__ A/var/www/html/myenv/lib/python3.10/site-packages/Bio/Align/bed.pyr   *   s   	
zAlignmentWriter.__init__c                 C   s  t |ts	td|j}|jsdS | j}|j\}}z|j}W n ty)   d}Y nw |du r0d}|d |d krC|dddddf }|d |d	 krNd
}nd}g }g }	|dddf \}
}|ddddf 	 D ]$\}}|
|kru|}qj||kr||}
qj||
 }|	
|
 |
| |}
|}qjz|	d }|	d | }W n ty   |
 }}Y nw |t|t|g}|dkrd|d S z|j}W n ty   d}Y nw |du rd}|
| |dkrd|d S z|j}W n ty   d}Y nw |
t|d |dkr	d|d S |
| |dkrd|d S z|j}W n ty+   |}Y nw |
t| |dkr?d|d S z|j}W n tyP   |}Y nw |
t| |dkrdd|d S z|j}W n tyu   d}Y nw |
t| |dkrd|d S t|}|
t| |dkrd|d S |
dtt|d  |dkrd|d S |	|8 }	|
dtt|	d  d|d S )z;Return a string with one alignment formatted as a BED line.zExpected an Alignment object r   Nr   r   r   r   )   r   )r   r   -+r   r   r   	
query   g            0	   
   ,   )
isinstancer   	TypeErrorcoordinatessizer   	sequencesidAttributeError	transposeappend
IndexErrorstrjoinscoreformat
thickStartthickEnditemRgblenmap)r   	alignmentr*   r   r   r   chromstrand
blockSizesblockStartstStartqStarttEndqEnd	blockSize
chromStartchromEndfieldsnamer4   r6   r7   r8   
blockCountr   r   r   format_alignment8   s   


 















z AlignmentWriter.format_alignment)r   )__name__
__module____qualname____doc__r   rJ   __classcell__r   r   r   r   r   '   s    r   c                   @   s   e Zd ZdZdZdd ZdS )AlignmentIteratorzAlignment iterator for Browser Extensible Data (BED) files.

    Each line in the file contains one pairwise alignment, which are loaded
    and returned incrementally.  Additional alignment information is stored as
    attributes of each alignment.
    BEDc              	   C   s6  |D ]}|  }t|}|dk s|dkrtd| |d }t|d }t|d }|dkr4|d }nd }|dkr?|d }	nd}	|d	krt|d	 }
d
d |d d dD }dd |d d dD }t||
krytdt||
f t||
krtdt||
f t|}t|}d}d}||gg}t||D ] \}}||kr|||g |}||7 }||7 }|||g qt|	 }t
|}n|| }td|gd|gg}|}|dd d f  |7  < td |d}t||dd}td tjd}t||dd}||g}|	dkr||dd d f  |dd d f< ||d kr0td||d f ||d krAtd||d f t||}|dkrO|  S |d }zt|}W n
 tyc   Y nw ||_|dkrp|  S t|d |_|dkr|  S t|d |_|dkr|  S |d |_|  S d S )Nr   r   z+expected between 3 and 12 columns, found %dr   r      r   r   r$   c                 S      g | ]}t |qS r   int).0rD   r   r   r   
<listcomp>       z:AlignmentIterator._read_next_alignment.<locals>.<listcomp>r%   r&   c                 S   rS   r   rT   )rV   
blockStartr   r   r   rW      rX   r'   z:Inconsistent number of block sizes (%d found, expected %d)zDInconsistent number of block start positions (%d found, expected %d))lengthr   )r-   descriptionr   r   z/Inconsistent chromStart found (%d, expected %d)r   z-Inconsistent chromEnd found (%d, expected %d)r   r    r!   r"   )splitr9   r	   rU   rstripnparrayzipr0   r/   sumr   r   sysmaxsizer   floatr4   r6   r7   r8   )r   streamlinewordsr   r<   rE   rF   rH   r=   rI   r>   r?   	tPosition	qPositionr*   rD   rY   qSizequery_sequencequery_recordtarget_sequencetarget_recordrecordsr;   r4   r   r   r   _read_next_alignment   s   









 







z&AlignmentIterator._read_next_alignmentN)rK   rL   rM   rN   fmtrp   r   r   r   r   rP      s    rP   )rN   rb   numpyr^   	Bio.Alignr   r   Bio.Seqr   Bio.SeqRecordr   r   rP   r   r   r   r   <module>   s   v