
    Ri$                     ,   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 ddlm	Z	 dZ
 ej                  d	      Z ej                  d
      Z ej                  d      Z ej                  d      Z ej                  d      ZdZdZdZ G d d      Zy)zIBio.SearchIO parser for HHSUITE version 2 and 3 plain text output format.    N)Hit)HSP)HSPFragment)QueryResult)read_forward)Hhsuite2TextParserz^Query\s+(.+)\s?$z^No +(\d+)\s+$z>(\S+)\s+(.*)$z,^Q\s+(.+) +(\d+) +([A-Z-]+) +(\d+) +\(\d+\)$z,^T\s+(.+) +(\d+) +([A-Z-]+) +(\d+) +\(\d+\)$zDone!HHSUITEi  c                   V    e Zd ZdZd Zd ZdefdZd Zd Z	d Z
ed	        Zd
 Zd Zy)r   z3Parser for the HHSUITE version 2 and 3 text output.c                 p    || _         t        | j                         | _        d| _        d| _        d| _        y)zInitialize the class.FN)handler   linedonequery_idseq_len)selfr   s     \/home/agent/.friday_env/lib/python3.12/site-packages/Bio/SearchIO/HHsuiteIO/hhsuite2_text.py__init__zHhsuite2TextParser.__init__/   s.     -		    c              #   @   K   | j                         E d{    y7 w)z9Iterate over query results - there will only ever be one.N)_parse_qresult)r   s    r   __iter__zHhsuite2TextParser.__iter__7   s     &&(((s   Tc                     d}	 |r| j                   sy || j                         ryt        | j                        | _         |dz  }||k\  rt        d      R)zERead the file handle until the given function returns True (PRIVATE).r   N   z&Exceeded max_read_until in _read_until)r   r   r   RuntimeError)r   	bool_funcstop_on_blankmax_read_untilcounts        r   _read_untilzHhsuite2TextParser._read_until;   sW    TYY#(5	QJE&"#KLL r   c                     g }| j                          | j                  d d       | j                  s.| j                         }|j	                  |       | j                  s.| j                  |      S )z$Parse HHSUITE output file (PRIVATE).c                 6    t        j                  t        |       S )N)research_RE_HIT_BLOCK_START)r   s    r   <lambda>z3Hhsuite2TextParser._parse_qresult.<locals>.<lambda>N   s    #6= r   F)r   )_parse_preambler   r   _parse_hit_blockappend_create_qresult)r   hit_block_datahit_dicts      r   r   z!Hhsuite2TextParser._parse_qresultI   sl    =U 	 	
 )),,.H!!(+ )) ##N33r   c                    i }| j                   rt        j                  t        | j                         }|r|j	                  d      | _        | j                   j                  d      r9t        | j                   j                         j                         d         | _
        | j                  j                         j                         | _         | j                   r|S )z%Parse metadata about query (PRIVATE).r   Match_columns)r   r"   r#   	_RE_QUERYgroupr   
startswithintstripsplitr   r   readline)r   metaregxs      r   r&   z"Hhsuite2TextParser._parse_preambleU   s    ii99Y		2D $

1yy##O4"499??#4#:#:#<Q#?@,,.446DI ii r   c                    t        | j                        | _        t        j                  t
        | j                        }|st        d| j                   d      |j                  d      |j                  d      j                  d      dddddddddd}| j                  j                         | _        | j                  | j                  |       	 t        | j                        | _        | j                  j                         r| j                  j                  t              r	d	| _        |S t        j                  t        | j                        r|S | j!                  |       )
zParse a hit block (PRIVATE).z*Unexpected content in HIT_BLOCK_DESC line''r      z ;N )hit_iddescriptionevalue	hit_starthit_endhit_seqprobquery_start	query_end	query_seqscoreT)r   r   r   r"   r#   _RE_HIT_BLOCK_DESCr   r/   lstripr4   _process_score_liner2   r0   _END_OF_FILE_MARKERr   r$   _parse_hit_match_block)r   matchhit_datas      r   r'   z#Hhsuite2TextParser._parse_hit_blocka   s     -			,dii8<TYYKqI  kk!n ;;q>006
 KK((*	  H5$T[[1DI99??$		(<(<=P(Q 	.		:++H5 r   c                     dddd}| j                         j                         D ]-  }|j                  d      \  }}||v s	 t        |      |||   <   / y# t        $ r t	        j
                  d| d|         Y Ww xY w)	a  Parse the scores from the line and populate hit_data dict (PRIVATE).

        Lines are of the form:
        Probab=99.95  E-value=3.7e-34  Score=210.31  Aligned_cols=171  Identities=100%  Similarity=2.050  Sum_probs=166.9

        E-value could be in decimal or scientific notation, so split the string rather then use regexp - this
        also means we should be tolerant of additional fields being added/removed
        r=   rE   rA   )zE-valueScoreProbab=z"HHsuite parser: unable to extract z from line: N)r2   r3   floatKeyErrorwarningswarn)r   rL   	score_map
score_pairkeyvalues         r   rH   z&Hhsuite2TextParser._process_score_line   s     !)7fM	**,,,. 		J#))#.JCi/4U|HYs^,			
   MM<SEdVTs   A$A=<A=c                    d }	 | j                   j                         syt        j                  t        | j                         }|rr ||      rj|dxx   |j                  d      j                         z  cc<   |d   t        |j                  d            |d<   t        |j                  d            |d<   nt        j                  t        | j                         }|rq ||      ri|d	xx   |j                  d      j                         z  cc<   |d
   t        |j                  d            |d
<   t        |j                  d            |d<   | j                  j                         | _         k)a  Parse a single block of hit sequence data (PRIVATE).

        Parses block such as ::

            Q ss_pred             ceecchHHHHHHHHHHHHHHHHHHHhhhhhcCCCCccc
            Q 4P79:A|PDBID|C  160 YELGPALYLGWSASLLSILGGICVFSTAAASSKEEPAT  197 (198)
            Q Consensus       160 ~~~g~sf~l~~~~~~l~~~~~~l~~~~~~~~~~~~~~~  197 (198)
                                  .++|||||++|++.++.+++++++++..+..++++..+
            T Consensus       327 ~~~GwS~~l~~~s~~l~lia~~l~~~~~~~~~~~~~~~  364 (364)
            T 5B2G_A          327 REMGASLYVGWAASGLLLLGGGLLCCSGPSSGENLYFQ  364 (364)
            T ss_dssp             EEECTHHHHHHHHHHHHHHHHHHHHCC-----------
            T ss_pred             cccchHHHHHHHHHHHHHHHHHHHHhcCCCCCCccccC

        c                 F    | j                  d      j                         dk7  S )zReturn True if match is not a Consensus column (PRIVATE).

            It's not possible to distinguish a sequence line from a Consensus line with
            a regexp, so need to check the ID column.
            r   	Consensus)r/   r2   )rK   s    r   match_is_validzAHhsuite2TextParser._parse_hit_match_block.<locals>.match_is_valid   s      ;;q>'')[88r   NrD      rB   r9      rC   r@   r>   r?   )
r   r2   r"   rK   _RE_MATCH_BLOCK_QUERY_SEQr/   r1   _RE_MATCH_BLOCK_HIT_SEQr   r4   )r   hit_match_datar\   rK   s       r   rJ   z)Hhsuite2TextParser._parse_hit_match_block   s(    	9 99??$HH6		BE.{+u{{1~/C/C/EE+!-0847A4GN=1.1%++a..A{+!8$))D^E2"9-Q1E1E1GG-%k2:69%++a.6I{303EKKN0CN9-,,.DI! r   c                    | j                   }i }t        |      D ]  \  }}|d   }t        ||      }d|_        |d   dz
  |_        |d   |_        |d   dz
  |_        |d   |_        |d   |_        |d	   |_	        t        |g      }||_        ||_        ||_         |d
   |_        d}	|	|_        |d   |_        |d   |_        |d   |_        ||vr8t%        |g|      }
|d
   |
_        |	|
_        |d   |
_        |d   |
_        |
||<   ||   j)                  |        t+        |j-                         |      }t.        |_        | j2                  |_        |gS )zDCreate the Biopython data structures from the parsed data (PRIVATE).r;   proteinrB   r   rC   r>   r?   r@   rD   r<   Tr=   rE   rA   )r   	enumerater   molecule_typerB   rC   r>   r?   hitqueryr   r;   output_indexhit_descriptionis_includedr=   rE   rA   r   r<   r(   r   values_PROGRAMprogramr   )r   
hit_blocksr   r+   rh   blockr;   fraghsprj   rf   qresults               r   r)   z"Hhsuite2TextParser._create_qresult   s   ==#,Z#8 	-L%8_Fvx0D!*D$]3a7D";/DN";/!3DN +DLY'DH{+DJtf+CCJ+C#CL"'"6CK)COxCJgCIV}CHX%3%("'"6"-"8_
!'N	#&  '',?	-B hoo/:",,yr   N)__name__
__module____qualname____doc__r   r   MAX_READ_UNTILr   r   r&   r'   staticmethodrH   rJ   r)    r   r   r   r   ,   sK    =) 48 M
4
 6D  *(/T)r   r   )rv   r"   rS   Bio.SearchIO._modelr   r   r   r   Bio.SearchIO._utilsr   __all__compiler.   r$   rF   r_   r`   rI   rl   rw   r   ry   r   r   <module>r~      s    P 	  # # + + ,
! BJJ+,	 !bjj!23   RZZ 12  'BJJ'VW $"**%TU  
 @ @r   