o
    RŀgP                     @   s   d Z G dd dZG dd dZG dd dZG dd dZG d	d
 d
ZG dd dZG dd dZG dd dZG dd dZ	G dd dZ
G dd dZdd ZG dd dZdd ZdS )a  Parser for ACE files output by PHRAP.

Written by Frank Kauff (fkauff@duke.edu) and
Cymon J. Cox (cymon@duke.edu)

Usage:

There are two ways of reading an ace file:

1. The function 'read' reads the whole file at once;
2. The function 'parse' reads the file contig after contig.

First option, parse whole ace file at once::

        from Bio.Sequencing import Ace
        acefilerecord = Ace.read(open('my_ace_file.ace'))

This gives you:
 - acefilerecord.ncontigs (the number of contigs in the ace file)
 - acefilerecord.nreads (the number of reads in the ace file)
 - acefilerecord.contigs[] (one instance of the Contig class for each contig)

The Contig class holds the info of the CO tag, CT and WA tags, and all the reads used
for this contig in a list of instances of the Read class, e.g.::

        contig3 = acefilerecord.contigs[2]
        read4 = contig3.reads[3]
        RD_of_read4 = read4.rd
        DS_of_read4 = read4.ds

CT, WA, RT tags from the end of the file can appear anywhere are automatically
sorted into the right place.

see _RecordConsumer for details.

The second option is to iterate over the contigs of an ace file one by one
in the usual way::

    from Bio.Sequencing import Ace
    contigs = Ace.parse(open('my_ace_file.ace'))
    for contig in contigs:
        print(contig.name)
        ...

Please note that for memory efficiency, when using the iterator approach, only one
contig is kept in memory at once.  However, there can be a footer to the ACE file
containing WA, CT, RT or WR tags which contain additional meta-data on the contigs.
Because the parser doesn't see this data until the final record, it cannot be added to
the appropriate records.  Instead these tags will be returned with the last contig record.
Thus an ace file does not entirerly suit the concept of iterating. If WA, CT, RT, WR tags
are needed, the 'read' function rather than the 'parse' function might be more appropriate.
c                   @   s   e Zd ZdZdd ZdS )rdzRD (reads), store a read with its name, sequence etc.

    The location and strand each read is mapped to is held in the AF lines.
    c                 C   s"   d| _ d| _d| _d| _d| _dS )Initialize the class. N)namepadded_bases
info_items	read_tagssequenceself r   F/var/www/html/myenv/lib/python3.10/site-packages/Bio/Sequencing/Ace.py__init__C   s
   
zrd.__init__N__name__
__module____qualname____doc__r   r   r   r   r   r   =   s    r   c                   @      e Zd ZdZdddZdS )qazIQA (read quality), including which part if any was used as the consensus.Nc                 C   sd   d| _ d| _d| _d| _|r0| }t|d | _ t|d | _t|d | _t|d | _dS dS r   N            )qual_clipping_startqual_clipping_endalign_clipping_startalign_clipping_endsplitintr
   lineheaderr   r   r   r   O   s   zqa.__init__Nr   r   r   r   r   r   L       r   c                   @   r   )dsz:DS lines, include file name of a read's chromatogram file.Nc              	      s   d| _ d| _d| _d| _d| _d| _d| _ rcg d} fdd|D }tt||}d|v r2|d= t	|}t||dd t
 d g D ]\}}t| ||   |t
||  d |   qFdS dS )r   r   )CHROMAT_FILEPHD_FILETIMECHEMDYETEMPLATE	DIRECTIONc                    s   g | ]}  |qS r   )find.0xr!   r   r   
<listcomp>s   s    zds.__init__.<locals>.<listcomp>r   N)chromat_filephd_filetimechemdyetemplate	directiondictzipsortedlensetattrlowerstrip)r
   r!   tagsposstagpospsp1p2r   r1   r   r   `   s,   	(
zds.__init__r#   r   r   r   r   r   r%   ]   r$   r%   c                   @   r   )afzAF lines, define the location of the read within the contig.

    Note attribute coru is short for complemented (C) or uncomplemented (U),
    since the strand information is stored in an ACE file using either the
    C or U character.
    Nc                 C   sH   d| _ d| _d| _|r"| }|d | _ |d | _t|d | _dS dS r   r   Nr   r   r   )r   corupadded_startr   r   r    r   r   r   r      s   

zaf.__init__r#   r   r   r   r   r   rH      s    rH   c                   @   r   )bszKBS (base segment), which read was chosen as the consensus at each position.Nc                 C   sL   d| _ d| _d| _|r$| }t|d | _t|d | _|d | _ dS dS rI   )r   rK   
padded_endr   r   r    r   r   r   r      s   zbs.__init__r#   r   r   r   r   r   rL      r$   rL   c                   @   r   )rtz<RT (transient read tags), generated by crossmatch and phrap.Nc                 C   s   d| _ d| _d| _d| _d| _d| _g | _|r?| }|d | _ |d | _|d | _t|d | _t|d | _|d | _dS dS )	r   r   N    r   r   r   r      )	r   tag_typeprogramrK   rM   datecommentr   r   r    r   r   r   r      s    


zrt.__init__r#   r   r   r   r   r   rN      r$   rN   c                   @   r   )ctzCT (consensus tags).Nc                 C   s   d| _ d| _d| _d| _d| _d| _d| _g | _g | _|rP|	 }|d | _ |d | _|d | _t
|d | _t
|d | _|d | _t|d	krR|d
 | _dS dS dS )r   r   NrO   r   r   r   r   rP         )r   rQ   rR   rK   rM   rS   notransinforT   r   r   r>   r    r   r   r   r      s*   



zct.__init__r#   r   r   r   r   r   rU      r$   rU   c                   @   r   )wazGWA (whole assembly tag), holds the assembly program name, version, etc.Nc                 C   sJ   d| _ d| _d| _g | _|r#| }|d | _ |d | _|d | _dS dS )r   r   rO   r   r   N)rQ   rR   rS   rY   r   r    r   r   r   r      s   

zwa.__init__r#   r   r   r   r   r   rZ      r$   rZ   c                   @   r   )wrz	WR lines.Nc                 C   sT   d| _ d| _d| _g | _|r(| }|d | _ |d | _|d | _|d | _dS dS )r   r   rO   r   r   r   N)r   alignedrR   rS   r   r    r   r   r   r      s   


zwr.__init__r#   r   r   r   r   r   r[      r$   r[   c                   @   r   )Readsz8Holds information about a read supporting an ACE contig.Nc                 C   sv   d| _ d| _d| _d| _d| _|r9t  | _ | }|d | j _t|d | j _t|d | j _	t|d | j _
dS dS r   )r   r   r%   rN   r[   r   r   r   r   r   r   r    r   r   r   r      s   zReads.__init__r#   r   r   r   r   r   r]      r$   r]   c                   @   r   )Contigz4Holds information about a contig from an ACE record.Nc                 C   s   d| _ d| _d| _d| _d| _d| _g | _g | _g | _g | _	d| _
d| _|rK| }|d | _ t|d | _t|d | _t|d | _|d | _dS dS )r   r   Nr   r   r   r   rP   )r   nbasesnreads	nsegmentsuorcr   qualityrH   rL   readsrU   rZ   r   r   r    r   r   r   r   
  s(   
zContig.__init__r#   r   r   r   r   r   r^     r$   r^   c                 c   s   zt | }W n ty   | }|ddkrtddY nw zd}	 z	 |dr,nt|}q&W n tyG   Y W || urE|  dS dS w t|}|D ]}|	 }|sX n| j
|7  _
qN|D ]}|	 rj nqb|dsttd|D ]}|	 s~ n|jd	d
 | D  qv|D ]}|	 r nq	 |dsn|jt| zt|}W n ty   tddw q	 |	 rnzt|}W n ty   tddw q	 |dsn|jt| zt|}W n ty   tddw q	 z	 |drnt|}qW n ty   tddw |jt| |D ]}|	 }|s* n|jd j j
|7  _
q|D ]
}|	 rB nq9|dsNtdt||jd _|D ]
}|	 ra nqXn|drut||jd _d}	 z	 |	 r~nt|}qxW n ty   Y n]w |dr|jd jdu rg |jd _|D ]X}|	 }|dr|dd 	 r|jd jd j|dd  |D ]}|	 }|dr n|jd jd j| qϐq|dkr n|jd jt| qd}n|dr8|jd jdu rg |jd _|D ]}|	 }|dkr( n|jd jt| qd}n|dr|jdu rGg |_zt|}W n tyZ   tddw |jt| |D ]}|	 }|dkrr n|jd j| qed}nk|dr|jdu rg |_zt|}W n ty   tddw |jt| |D ]9}|	 }|dkr|D ]}|	 }|dr n|jd j| qq|dkr n|jd j| qd}nnqv|d snq|V  q$|| ur|  w w )!ak  Iterate of ACE file contig by contig.

    Argument source is a file-like object or a path to a file.

    This function returns an iterator that allows you to iterate
    over the ACE file record by record::

        records = parse(source)
        for record in records:
            # do something with the record

    where each record is a Contig object.
    rO   r   z&Ace files must be opened in text mode.NTCOBQzFailed to find BQ linec                 s   s    | ]}t |V  qd S r#   )r   r.   r   r   r   	<genexpr>S  s    zparse.<locals>.<genexpr>zAF zUnexpected end of AF blockzUnexpected end of filezBS zFailed to find end of BS blockzRD zFailed to find RD liner3   zQA zFailed to find QA linezDS zRT{zCOMMENT{   zC}}zWR{zWA{zFailed to read WA blockzCT{zFailed to read CT blockRD)open	TypeErrorread
ValueError
startswithnextStopIterationcloser^   rA   r   rc   extendr   rH   appendrL   rd   r]   r   r   r%   rN   rT   endswithr[   rZ   rY   rU   )sourcehandler!   recordr   r   r   parse!  s  

 8 


	






 






Pz  
@
ry   c                   @   s    e Zd ZdZdd Zdd ZdS )ACEFileRecordzHolds data of an ACE file.c                 C   s   d| _ d| _g | _d| _dS )r   N)ncontigsr`   contigsrZ   r	   r   r   r   r     s   
zACEFileRecord.__init__c                    sN  g }g }g }t | jD ]\}  jr| jsg | _| j j  jr? fdd jD }|D ]}| j| j| q.|| t  jD ]J\}jrkfddjD }|D ]}| j| j| j| qW|| jrfddjD }	|	D ]}| j| j| j| qz||	 qDqt | jD ]\} |D ]}
|
j	 j	kr| j| jdu rg | j| _| j| j
|
 q|s|r$t  jD ]^\}|D ])}|j	jj	kr| j| j| jdu rg | j| j| _| j| j| j
| q|D ]+}|j	jj	kr"| j| j| jdu rg | j| j| _| j| j| j
| qqqdS )zRSorts wr, rt and ct tags into the appropriate contig / read instance, if possible.c                    s   g | ]
}|j  j kr|qS r   )r   )r/   ct_tag)cr   r   r2     s    z&ACEFileRecord.sort.<locals>.<listcomp>c                       g | ]}|j  jj kr|qS r   r   r   )r/   rt_tagrr   r   r2         c                    r   r   r   )r/   wr_tagr   r   r   r2     r   N)	enumerater|   rZ   rs   rU   removerd   rN   r[   r   rt   r   )r
   rU   rN   r[   inewctsr0   jnewrtsnewwrsr}   r   r   r   )r~   r   r   sort  sd   



zACEFileRecord.sortN)r   r   r   r   r   r   r   r   r   r   rz     s    rz   c                 C   s   t | } t }zt| }W n ty   tddw |ds#td| }t|d |_t|d |_	t
t| |_|  |S )z-Parse a full ACE file into a list of contigs.zPremature end of fileNASzFile does not start with 'AS'.r   r   )iterrz   rp   rq   rn   ro   r   r   r{   r`   listry   r|   r   )rw   rx   r!   wordsr   r   r   rm   5  s   

rm   N)r   r   r   r%   rH   rL   rN   rU   rZ   r[   r]   r^   ry   rz   rm   r   r   r   r   <module>   s    6# [: