
:>"^.)                 @   s   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
 m Z Gd d	   d	 e  Z Gd
 d   d e
  Z d S)a\  Bio.AlignIO support for "emboss" alignment output from EMBOSS 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 EMBOSS pairs/simple file format, for
example from the alignret, water and needle tools.
    )print_function)Seq)	SeqRecord)MultipleSeqAlignment   )AlignmentIteratorSequentialAlignmentWriterc               @   s:   e  Z d  Z d Z d d   Z d d   Z d d   Z d S)	EmbossWriterzEmboss alignment writer (WORK IN PROGRESS).

    Writes a simplfied version of the EMBOSS pairs/simple file format.
    A lot of the information their tools record in their headers is not
    available and is omitted.
    c             C   sa   |  j  } | j d  | j d  y | j d | j  Wn t k
 rO Yn X| j d  d S)zWrite header for the file.z)########################################
z# Program: Biopython
z# Report_file: %s
N)handlewritenameAttributeError)selfr
    r   9/tmp/pip-build-ww9dw3qa/biopython/Bio/AlignIO/EmbossIO.pywrite_header    s    	zEmbossWriter.write_headerc             C   s'   |  j  } | j d  | j d  d S)zWrite footer for the file.z)#---------------------------------------
N)r
   r   )r   r
   r   r   r   write_footer+   s    	zEmbossWriter.write_footerc             C   s   |  j  } | j d  | j d  | j d t |   x8 t |  D]* \ } } | j d | d | j f  qG W| j d  | j d | j    | j d  | j d  | j d  t d   d	 S)
z=Use this to write (another) single alignment to an open file.z)#=======================================
z#
z# Aligned_sequences: %i
z	# %i: %s
r   z# Length: %i

z9The subclass should implement the write_alignment method.N)r
   r   len	enumerateidZget_alignment_lengthNotImplementedError)r   	alignmentr
   irecordr   r   r   write_alignment1   s    	"zEmbossWriter.write_alignmentN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r	      s   r	   c               @   s(   e  Z d  Z d Z d Z d d   Z d S)EmbossIteratorzEmboss alignment iterator.

    For reading the (pairwise) alignments from EMBOSS tools in what they
    call the "pairs" and "simple" formats.
    Nc          	   C   se  |  j  } |  j d k r' | j   } n |  j } d |  _ | sE t  x. | j   d k ru | j   } | sH t  qH Wd } d } g  } g  } i  } x| d d k r| d d  j d d  } | d j   j   }	 |	 d k rt | d j    } t	 |  d k st
  x t |  D]q }
 | j   } | d d  j   j d d  } |
 d t | d j    k szt
  | j | d j    q Wt	 |  | k st
  |	 d k rt | d j    } |	 d	 k rt | d j   j d
  d  | d	 <|	 d k r5t | d j   j d
  d  | d <|	 d k rht | d j   j d
  d  | d <|	 d k rt | d j    | d <| j   } q W| d k rt d   | d k rt d   |  j d k	 r|  j | k rt d | |  j f   d d   | D } g  } d } xd| rt	 |  d k r(| d d  j   j d d  } | d d  j   j d d  } t	 |  d k rMt	 |  d k rM| \ } } | \ } } | | k r| j d d  d k rt |  } t |  } qct |  d } t |  } nF | j d d  d k sGt
 t |    t |  d } t |  } | d k  s{| | k rt d | | f   | | | k s| | | d t	 |   k st
  t	 |  | k r| j |  | | k r| j d d  d k st
 |   nl | | | t	 | | j d d   k rt d t	 | | j d d   | | t | |  | | f   | | | 7<| | | t	 | | j d d   k rt d t	 | | j d d   | | t | |  | | | | f   | d 7} | | k r%d } qMn% | j   d k r=n t d |   | j   } | j   d k s}| j   d k r'| |  _ Pq'W| d k st
  |  j d k	 r|  j t	 |  k rt d t	 |  |  j f   g  } xf t | |  D]U \ } } t	 |  | k r t d   | j t t | |  j  d | d |  qWt | |  j d | S) z)Parse the next alignment from the handle.Nz(#=======================================r   #r   :Zaligned_sequenceslengthidentity/Z
similarityZgapsscorezNumber of sequences missing!zLength of sequences missing!z5Found %i records in this alignment, told to expect %ic             S   s   g  |  ] } d   q S) r   ).0r   r   r   r   
<listcomp>   s   	 z+EmbossIterator.__next__.<locals>.<listcomp>      -r'   z!Expected index %i in range [0,%i)zFFound %i chars so far for sequence %i (%s, %s), line says start %i:
%szNFound %i chars so far for sequence %i (%s, %s, start=%i), file says end %i:
%sz&Unrecognised EMBOSS pairwise line: %r
z(#---------------------------------------zeError parsing alignment - sequences of different length? You could be using an old version of EMBOSS.r   descriptionannotations)r
   _headerreadlineStopIterationrstripsplitlowerstripintr   AssertionErrorrangeappendfloat
ValueErrorZrecords_per_alignmentreplacereprzipr   r   Zalphabetr   )r   r
   lineZlength_of_seqsZnumber_of_seqsidsZseqsZheader_dictpartskeyr   Z
seq_startsindexZid_startZseq_endr   startseqendrecordsr   r   r   __next__L   s    			
"&'''	""$*6'**
			/zEmbossIterator.__next__)r   r   r   r   r/   rH   r   r   r   r   r    C   s   r    N)r   
__future__r   ZBio.Seqr   ZBio.SeqRecordr   Z	Bio.Alignr   Z
Interfacesr   r   r	   r    r   r   r   r   <module>   s   +