
    {Ti7                     N    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e      Z	y	)
ai  Bio.AlignIO support for GCG MSF format.

The file format was produced by the GCG PileUp and and LocalPileUp tools,
and later tools such as T-COFFEE and MUSCLE support it as an optional
output format.

The original GCG tool would write gaps at ends of each sequence which could
be missing data as tildes (``~``), whereas internal gaps were periods (``.``)
instead. This parser replaces both with minus signs (``-``) for consistency
with the rest of ``Bio.AlignIO``.

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).
    )MultipleSeqAlignment)Seq)	SeqRecord   )AlignmentIteratorc                       e Zd ZdZdZd Zy)MsfIteratorzGCG MSF alignment iterator.Nc                 J   | j                   }| j                  |j                         }n| j                  }d| _        |st        g d}|j	                         j                         d   |vr>t        |j	                         j                         d   ddj                  |            |rd|vr|j                         }|rd|vr|st        d      |j	                  d      j                         }|j                  d	      }||d
z      dk7  s|d   dvs|d   dk7  rt        d|z        	 t        ||dz            }|dk  rt        d||dz      z        ||dz      }|dvrt        d|z        g }g }	g }
g }|j                         }|r|j	                         dk7  r|j                         }|j	                         j                  d      rRd|v r?d|v r:d|v r5||j                  d      dz   d j	                         }|j                  d      \  }}|j                  d      \  }}|j                  d      \  }}|j	                         }|j                  d      r|dd }||v rt        d|      d|v rt        d|      |j                  |       |	j                  t        |j	                                      |
j                  t        |j	                                      |j                  t        |j	                                      nt        d |      |r|j	                         dk7  r|st        d!      |t        |	      k7  r:t        |	      t!        fd"|	D              }t        d#||t#        |      fz        |j                         }|st        d$      |j	                         rt        d%      |D cg c]  }g  }}d}||k  r!t%        |      D ]  \  }}|j                         }|dk(  rE|j	                         s5|r3|j	                         s#|j                         }|r|j	                         s#|st        d&      |j	                         j                         }|dk(  r|r|d   |k7  r	 t        |d         }||dz   k7  rt        d'|dz   |fz        t#        |      dkD  rSt#        |      d
k7  rd}n	 t        |d         }||d(z   |k  r|d(z   n|k7  r!t        d)|dz   |d(z   |k  r|d(z   n||fz        |j                         }|j	                         j                         }|s>|	|   |k  r%t#        d*j                  ||               |	|   k(  rt        d+| d,|      |d   |k(  r.t#        |      dkD  sJ |       ||   j'                  |dd        t        d+|d,|       |d(z  }|j                         }|j	                         rt        d-|      ||k  r!	 |j                         }|snK|j	                         sn9|j	                         j                         d   |v r|| _        nt        d/|      ^|D cg c]3  }d*j                  |      j)                  d0d1      j)                  d2d1      5 }}d3}t%        t+        |	|            D ]?  \  }\  }}t#        |      |k  st#        |      |k(  s'd.}|d1|t#        |      z
  z  z   ||<   A |rddl}dd4lm} |j3                  d5|       d6 t+        |||      D        }t5        |      }|j7                         |k7  rt        d7||j7                         fz        |S # t        $ r d}Y 4w xY wc c}w # t        $ r d}Y w xY w# t        $ r d}Y w xY wc c}w )8z)Parse the next alignment from the handle.N)z!!NA_MULTIPLE_ALIGNMENTz!!AA_MULTIPLE_ALIGNMENTPileUpr   z  is not a known GCG MSF header: z, z MSF: z6Reached end of file without MSF/Type/Check header line
zMSF:   zType:)zCheck:z
CompCheck:z..zsGCG MSF header line should be '<optional text> MSF: <int> Type: <letter> <optional date> Check: <int> ..',  not: %rr   zCGCG MSF header line should have MDF: <int> for column count, not %r   )PNz]GCG MSF header line should have 'Type: P' (protein) or 'Type: N' (nucleotide), not 'Type: %s'z//zName: z Len: z Check: z	 Weight:    z oozDuplicated ID of  zSpace in ID zMalformed GCG MSF name line: z4End of file while looking for end of header // line.c              3   .   K   | ]  }|k(  s	d   yw)r   N ).0_
max_lengths     D/home/agent/.local/lib/python3.12/site-packages/Bio/AlignIO/MsfIO.py	<genexpr>z'MsfIterator.__next__.<locals>.<genexpr>   s     B!!z/ABs   
zLGCG MSF header said alignment length %i, but %s of %i sequences said Len: %sz.End of file after // line, expected sequences.z4After // line, expected blank line before sequences.z*End of file where expecting sequence data.z5Expected GCG MSF coordinate line starting %i, got: %r2   z2Expected GCG MSF coordinate line %i to %i, got: %r zExpected sequence for z, got: zExpected blank line, got: Tz)Unexpected line after GCG MSF alignment: ~-.F)BiopythonParserWarningzGOne of more alignment sequences were truncated and have been gap paddedc           	   3   Z   K   | ]#  \  }}}t        t        |      |||d |i       % yw)weight)idnamedescriptionannotationsN)r   r   )r   isws       r   r   z'MsfIterator.__next__.<locals>.<genexpr>@  s7      
Aq c!fRS}UU
s   )+z5GCG MSF headers said alignment length %i, but have %i)handle_headerreadlineStopIterationstripsplit
ValueErrorjoinindexint
startswithendswithNotImplementedErrorappendfloatmaxsumlen	enumerateextendreplacezipwarningsBior!   warnr   get_alignment_length)selfr+   lineknown_headerspartsoffset
aln_lengthseq_typeidslengthschecksweightsrestr%   lengthcheckr#   	max_countr   seqscompleted_lengthidxwordsr(   r)   paddedrA   r!   recordsalignr   s                                 @r   __next__zMsfIterator.__next__#   s(   <<??$D <<DDL Y ::<"-7::<%%'*DIIm,DF 
 xt+??$D xt+ UVVN 

4 &&(V$&1*(Ry 88RyD !" 
	U6A:./J >U
#$  !$:%<>FG &  tzz|t+??$Dzz|&&x0t#
d(:{d?R

8 4q 8 :;AACD!%H!5JD$#'::j#9LFD$(JJ{$;ME6::<D}}U+#CRys{(+<TH)EFFd{1L2IJJJJt$NN3v||~#67MM#ekkm"45NN5#89$'DTH%MNN- tzz|t+. STTW% WJBwBBI^y#c(J?@ 
  MNN::<STT   q  +&s^ EW	T(!8DJJLtzz|%0 tzz|$%QRR

**,!8%(d*:aM ,q00(S/!3T:;  5zA~u:? "A'$'aM /"4zA -r1!+
 #- T$4q$8 ,<b+@:+M )92(=-7$(#"	!"#  "??,D JJL..0E   z1S	 23ws|C (+A$wth)WXX1X%u:>/4/>I$$U12Y/$'=dXWTH%UVVKEWP "??$Dzz| #=dX!FGG[ +` ??$DZZ\##%a(M9# #LTH!UVV   IMM1
""3,44S#>MM  )#gt*< = 	<C&!1v
"s1v'7zCF': ;;S		< 2MMY&

 dG4
 %W-%%':5Gu99;<=  [  	J	X !( &  $. '$&'@ NsB   .]% 	]7]<^"8^ %]43]4<^
^^^)__name__
__module____qualname____doc__r,   r[   r       r   r	   r	      s    %Gjr`   r	   N)
r_   	Bio.Alignr   Bio.Seqr   Bio.SeqRecordr   
Interfacesr   r	   r   r`   r   <module>re      s'    +  # )o# or`   