
:>"^8  ã               @   sp   d  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
 Gd d	 „  d	 e
 ƒ Z d
 S)aj  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 toola 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).
é    )Úprint_function)ÚSeq)Ú	SeqRecord)ÚMultipleSeqAlignmenté   )ÚAlignmentIteratorc               @   s(   e  Z d  Z d Z d Z d d „  Z d S)ÚMsfIteratorzGCG MSF alignment iterator.Nc                sœ  ˆ j  } ˆ j d k r' | j ƒ  } n ˆ j } d ˆ _ | sE t ‚ d d d g } | j ƒ  j ƒ  d | k rŸ t d | j ƒ  j ƒ  d d j | ƒ f ƒ ‚ x" | rÃ d | k rÃ | j ƒ  } q¢ W| sÖ t d	 ƒ ‚ | j d
 ƒ j ƒ  } | j d ƒ } | | d d k s.| d< d= k s.| d> d k r>t d | ƒ ‚ y t	 | | d ƒ } Wn t k
 rpd? } Yn X| d k  r•t d | | d ƒ ‚ | | d } | d@ k r¿t d | ƒ ‚ g  } g  }	 g  }
 g  } | j ƒ  } x‘| rv| j ƒ  d k rv| j ƒ  } | j ƒ  j
 d ƒ ræd | k rcd | k rcd | k rc| | j d ƒ d d … j ƒ  } | j d ƒ \ } } | j d ƒ \ } } | j d ƒ \ } } | j ƒ  } | j d ƒ rÐ| d dA … } | | k rìt d | ƒ ‚ d  | k rt d! | ƒ ‚ | j | ƒ |	 j t	 | j ƒ  ƒ ƒ |
 j t	 | j ƒ  ƒ ƒ | j t | j ƒ  ƒ ƒ qæt d" | ƒ ‚ qæW| s‰t d# ƒ ‚ | t |	 ƒ k rèt |	 ƒ ‰  t ‡  f d$ d% †  |	 Dƒ ƒ } t d& | | t | ƒ ˆ  f ƒ ‚ | j ƒ  } | st d' ƒ ‚ | j ƒ  rt d( ƒ ‚ d) d* „  | Dƒ } d } x×| | k  rx’t | ƒ D]„\ } } | j ƒ  } | d k rª| j ƒ  rªx# | r©| j ƒ  r©| j ƒ  } q‡W| s¼t d+ ƒ ‚ | j ƒ  j ƒ  } | d k r | r | d | k r y t	 | d ƒ } Wn t k
 rdB } Yn X| | d k rIt d, | d | f ƒ ‚ t | ƒ d k rt | ƒ d k rvdC } n/ y t	 | d ƒ } Wn t k
 r¤dD } Yn X| | d- | k  rÂ| d- n | k rt d. | d | d- | k  rò| d- n | | f ƒ ‚ | j ƒ  } | j ƒ  j ƒ  } | su|	 | | k  r\t d/ j | | ƒ ƒ |	 | k r\q×t d0 | | f ƒ ‚ qS| d | k rÁt | ƒ d k s£t | ƒ ‚ | | j | d d … ƒ qSt d1 | | f ƒ ‚ qSW| d- 7} | j ƒ  } | j ƒ  r:t d2 | ƒ ‚ q:Wxb | j ƒ  } | s*Pq| j ƒ  s9q| j ƒ  j ƒ  d | k rb| ˆ _ Pqt d3 | ƒ ‚ qWd4 d* „  | Dƒ } d5 } xo t t |	 | ƒ ƒ D]X \ } \ } } t | ƒ | k  r¥t | ƒ | k r¥d6 } | d7 | t | ƒ | | <q¥W| r3d d l } d d8 l m } | j d9 | ƒ ‡ f d: d% †  t | | | ƒ Dƒ } t | ˆ j ƒ } | j ƒ  | k r˜t d; | | j ƒ  f ƒ ‚ | S)Ez)Parse the next alignment from the handle.Nz!!NA_MULTIPLE_ALIGNMENTz!!AA_MULTIPLE_ALIGNMENTZPileUpr   z$%s is not a known GCG MSF header: %sz, z MSF: z6Reached end of file without MSF/Type/Check header lineÚ
zMSF:é   zType:é   úCheck:ú
CompCheck:r   z..zsGCG MSF header line should be '<optional text> MSF: <int> Type: <letter> <optional date> Check: <int> ..',  not: %rzCGCG 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 %rú zSpace in ID %rzMalformed GCG MSF name line: %rz4End of file while looking for end of header // line.c             3   s!   |  ] } | ˆ  k r d  Vq d S)r   N© )Ú.0Ú_)Ú
max_lengthr   ú6/tmp/pip-build-ww9dw3qa/biopython/Bio/AlignIO/MsfIO.pyú	<genexpr>À   s    z'MsfIterator.__next__.<locals>.<genexpr>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.c             S   s   g  |  ] } g  ‘ q Sr   r   )r   r   r   r   r   ú
<listcomp>Í   s   	 z(MsfIterator.__next__.<locals>.<listcomp>z*End of file where expecting sequence data.z5Expected GCG MSF coordinate line starting %i, got: %ré2   z2Expected GCG MSF coordinate line %i to %i, got: %rÚ z!Expected sequence for %s, got: %rz!Expected sequence for %r, got: %rzExpected blank line, got: %rz+Unexpected line after GCG MSF alignment: %rc             S   s7   g  |  ]- } d  j  | ƒ j d d ƒ j d d ƒ ‘ q S)r   ú~ú-Ú.)ÚjoinÚreplace)r   Úsr   r   r   r   /  s   	 FTr   )ÚBiopythonParserWarningzGOne of more alignment sequences were truncated and have been gap paddedc             3   sN   |  ]D \ } } } t  t | ˆ  j ƒ d  | d | d | d d | i ƒVq d S)ÚidÚnameÚdescriptionÚannotationsÚweightN)r   r   Úalphabet)r   Úir    Úw)Úselfr   r   r   A  s   z5GCG MSF headers said alignment length %i, but have %iéýÿÿÿ)r   r   éÿÿÿÿr,   )r   r   r+   r,   r,   r,   )ÚhandleÚ_headerÚreadlineÚStopIterationÚstripÚsplitÚ
ValueErrorr   ÚindexÚintÚ
startswithÚendswithÚNotImplementedErrorÚappendÚfloatÚmaxÚsumÚlenÚ	enumerateÚAssertionErrorÚextendÚzipÚwarningsZBior!   Úwarnr   r'   Zget_alignment_length)r*   r-   ÚlineZknown_headersÚpartsÚoffsetZ
aln_lengthZseq_typeÚidsZlengthsZchecksZweightsÚrestr#   ÚlengthÚcheckr&   Ú	max_countZseqsZcompleted_lengthÚidxÚwordsr(   Zpaddedr    rB   r!   ÚrecordsZalignr   )r   r*   r   Ú__next__$   s:   			)'

$#"		#
	($ zMsfIterator.__next__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r.   rO   r   r   r   r   r      s   r   N)rS   Ú
__future__r   ZBio.Seqr   ZBio.SeqRecordr   Z	Bio.Alignr   Z
Interfacesr   r   r   r   r   r   Ú<module>   s   