o
    Rŀg+4                     @   s>   d Z ddlmZ ddlmZ ddlmZ dd Zd
dd	ZdS )a!  Bio.AlignIO support for "fasta-m10" output from Bill Pearson's FASTA tools.

You are expected to use this module via the Bio.AlignIO functions (or the
Bio.SeqIO functions if you want to work directly with the gapped sequences).

This module contains a parser for the pairwise alignments produced by Bill
Pearson's FASTA tools, for use from the Bio.AlignIO interface where it is
referred to as the "fasta-m10" file format (as we only support the machine
readable output format selected with the -m 10 command line option).

This module does NOT cover the generic "fasta" file format originally
developed as an input format to the FASTA tools.  The Bio.AlignIO and
Bio.SeqIO both use the Bio.SeqIO.FastaIO module to deal with these files,
which can also be used to store a multiple sequence alignments.
    )MultipleSeqAlignment)Seq)	SeqRecordc                 C   s   |  d}t|d }t|d t|d kr*t|d | }t|d | d }n|t|d  }|t|d  d }||d7 }|dk sQ||ksQ|t|kr[td| |||f ||| S )a_  Extract alignment region (PRIVATE).

    Helper function for the main parsing code.

    To get the actual pairwise alignment sequences, we must first
    translate the un-gapped sequence based coordinates into positions
    in the gapped sequence (which may have a flanking region shown
    using leading - characters).  To date, I have never seen any
    trailing flanking region shown in the m10 file, but the
    following code should also cope with that.

    Note that this code seems to work fine even when the "sq_offset"
    entries are present as a result of using the -X command line option.
    -al_display_startal_startal_stop   r   z.Problem with sequence start/stop,
%s[%i:%i]
%s)stripintcountlen
ValueError)alignment_seq_with_flanking
annotationalign_strippeddisplay_startstartend r   G/var/www/html/myenv/lib/python3.10/site-packages/Bio/AlignIO/FastaIO.py_extract_alignment_region   s   

r   Nc                 #   s   d}d}d}d}d}d}d} 	
fdd	}	|}
d
	d
ddi i i  i i d
dd}D ]}d|v rx| dsx	rPrP|	 V  |}
||dd d
  d
dd 	d
i i  i i d
dd}q<| dr|
|ksJ rJ  rJ rJ rJ d
u sJ 
rJ rJ |rJ d
	q<| dv rˈ	rr|	 V  |}
d
	d
i i  i i d
dd}q<| dr	d
usJ |dd
 ddd 	ksJ |d
u sJ rJ  rJ rJ rJ rJ 
r	J |rJ |}
q<| dr@	r!r!|	 V  i  i i d
dd}|dd
  d
dd |}
q<| drc	rLsPJ ||	 V  i  i i d
dd}|}
q<| dr|
|kr	d
uswJ |d
usJ |	 |dd
 d
dd sJ ||}
q<|
|krň	d
usJ |d
usJ | |dd
 d
dd sJ ||}
q<|
|krq<td|
|f | dr|
|ksJ ||}
q<| drkd|v rdd |dd
 ddD \}}n7dd
l}ddlm} |d|| zdd |dd
 ddD \}}W n t	y8   t	d|d
w |
|krC||< q<|
|krM| |< q<|
|krW||< q<|
|kra||< q<td|
d||
|krw
| 7 
q<|
|kr| 7 q<|
|kr||d7 }q<|
|kr| d r|dd
  d!< q<| d"r|d#d
  d$< q<d%|v r|d
|d%  d&< q<d'|v r|d
|d'  d&< q<	 q<d
S )(a}  Alignment iterator for the FASTA tool's pairwise alignment output.

    This is for reading the pairwise alignments output by Bill Pearson's
    FASTA program when called with the -m 10 command line option for machine
    readable output.  For more details about the FASTA tools, see the website
    http://fasta.bioch.virginia.edu/ and the paper:

         W.R. Pearson & D.J. Lipman PNAS (1988) 85:2444-2448

    This class is intended to be used via the Bio.AlignIO.parse() function
    by specifying the format as "fasta-m10" as shown in the following code::

        from Bio import AlignIO
        handle = ...
        for a in AlignIO.parse(handle, "fasta-m10"):
            assert len(a) == 2, "Should be pairwise!"
            print("Alignment length %i" % a.get_alignment_length())
            for record in a:
                print("%s %s %s" % (record.seq, record.name, record.id))

    Note that this is not a full blown parser for all the information
    in the FASTA output - for example, most of the header and all of the
    footer is ignored.  Also, the alignments are not batched according to
    the input queries.

    Also note that there can be up to about 30 letters of flanking region
    included in the raw FASTA output as contextual information.  This is NOT
    part of the alignment itself, and is not included in the resulting
    MultipleSeqAlignment objects returned.
    r   r	               c                     s  sst d	dsJ sJ  d} dd }t
}|dv r;tt|kr;}nt}t|t|krpt d| d
 d	 d
| dt| d d d
| dt| dj d
i }g }| |  tt|	ddt	d id}t	d |_
t	d |_dv rd dkrd|jd< nd dkrd|jd< || tt|ddt	d id}t	d |_
t	d |_dv rd dkrd|jd< nd dkrd|jd< || t||dS )NzNo data for query z, match 	fa_expecttool )TFASTXz5Darn... amino acids vs nucleotide coordinates?
tool: z
query_seq: z
query_tags: 
z	 length: z
match_seq: z
match_tags: z
handle.name: queryoriginal_lengthsq_len)idnamedescriptionannotationsr   r   sq_typeDDNAmolecule_typepproteinmatch)r(   )r   getupperr   r   r&   updater   r   r   	_al_start_al_stopr(   appendr   )evaluer   qmr(   recordsrecord
align_tagsglobal_tagshandleheader_tagsmatch_descrmatch_id	match_seq
match_tagsquery_descrquery_id	query_seq
query_tagsr   r   	build_hspe   s   


	





z#FastaM10Iterator.<locals>.build_hspNr   z>>>z!! No )z>>><<<z>>>///z, z>>z>-->zstate %i got %rz	; al_consz; z: c                 s       | ]}|  V  qd S Nr
   .0sr   r   r   	<genexpr>1      z#FastaM10Iterator.<locals>.<genexpr>)BiopythonParserWarningzMissing colon in line: c                 s   rJ   rK   rL   rM   r   r   r   rP   =  rQ    z
Bad line: zUnexpected state r!   #commandz	 version 	   versionz compares a r   z searches a )

startswithfindr
   splitRuntimeErrorwarningsBiorR   warnr   )r>   	seq_countstate_PREAMBLE
state_NONEstate_QUERY_HEADERstate_ALIGN_HEADERstate_ALIGN_QUERYstate_ALIGN_MATCHstate_ALIGN_CONSrH   statecons_seqlinekeyvaluer\   rR   r   r;   r   FastaM10Iterator>   s2  "R

$



(
(

$
&













 rl   rK   )	__doc__	Bio.Alignr   Bio.Seqr   Bio.SeqRecordr   r   rl   r   r   r   r   <module>   s   "