
:>"^                  @   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
 d d l m Z m Z d d l m Z d	 d
   Z d d   Z d d   Z d d   Z d d   Z d S)zBio.SeqIO support for the "gck" file format.

The GCK binary format is generated by the Gene Construction Kit software
from Textco BioSoftware, Inc.
    )unpack)Alphabet)_read_header)	as_handle)Seq)
SeqFeatureFeatureLocation)	SeqRecordc             C   s:   |  j  |  } t |  | k  r6 t d j |    | S)z9Read the specified number of bytes from the given handle.z Cannot read {} bytes from handle)readlen
ValueErrorformat)handlelengthdata r   4/tmp/pip-build-ww9dw3qa/biopython/Bio/SeqIO/GckIO.py_read   s    r   c             C   s;   t  |  d  } t d |  d } t  |  |  } | | f S)aL  Read a length-prefixed packet.

    Parts of a GCK file are made of "packets" comprising of 4 bytes
    giving the packet's size, followed by the packet's data.

    There is no type tag. The type of a packet, and thus the type of data
    it contains, is solely indicated by the position of the packet within
    the GCK file.
       z>Ir   )r   r   )r   r   r   r   r   r   _read_packet    s    
r   c             C   s>   t  |  d  } t d |  d } t  |  |  j d  } | S)zeRead a Pascal string.

    A Pascal string is one byte for length followed by the actual string.
       z>Br   ASCII)r   r   decode)r   r   r   r   r   r   _read_pstring0   s    r   c             C   s>   t  |  d  } t d |  d } t  |  |  j d  } | S)zcRead a 32-bit Pascal string.

    Similar to a Pascal string but length is encoded on 4 bytes.
    r   z>Ir   r   )r   r   r   )r   r   r   r   r   r   _read_p4string;   s    r   c             c   sU  t  |  d  ?} t |  d  t |   \ } } t d | d d   d } | | d k rj t d   | d d  j d  } t t | d	 t j	  } t |   t |   \ } } t d
 | d d   \ } } | t
 |  k r t d   | d | d k rt d   x/t d |  D]} d | d }	 | |	 |	 d  }
 t d |
  \ } } } } } } } | d k rd$ } n d } t | | d | } | d k rd } n d } i  } | d k rt |   } | g | d <| d k rt |   } | g | d <| d k rq't | d | d | } | j j |  q'Wt |   \ } } t d
 | d d   \ } } | d | d k rt d   xt t d |  D]c } d | d }	 | |	 |	 d  } t d |  \ } } } } | rt |   | rt |   qWt |   t |  d  } t d |  d } t |  | d  } xh t d |  D]W } | d }	 | |	 |	 d  } t d | d% d   d } | d k r`t |   q`Wt |  d  t |   } | j d  d | _ | _ | | _ t |  d  } t d  |  d } | d k r8d! | j d" <n d# | j d" <| VWd QRXd S)&zParse a GCK file and return a SeqRecord object.

    Note that a GCK file can only contain one sequence, so this
    iterator will always return a single record.
    rb   z>INr   r   z"Conflicting sequence length valuesr   Zalphabetz>IH   \   z9Features packet size inconsistent with number of featuresz>II6xH14xB17xII35xBr   strandZCDSZmisc_featurelabelZnotetype
qualifiersX   z3Sites packet size inconsistent with number of sitesz>II24xII48x   z>Hi  i      z>16xBZcircularZtopologyZlinear)r   r   r   r   r   r   r	   r   r   Zgeneric_dnar   ranger   r   r   r   featuresappendr   splitnameiddescriptionannotations)r   fppacketr   Z
seq_lengthsequencerecordZnum_featuresioffsetZfeature_datastartendr!   r   Zhas_nameZhas_commentversionlocationr"   r-   commentfeatureZ	num_sitesZ	site_dataZnum_versionsversionsZversion_dataflagsZcircularityr   r   r   GckIteratorF   s    
	!		


	r?   N)__doc__structr   ZBior   Z
Bio._utilsr   ZBio.Filer   ZBio.Seqr   ZBio.SeqFeaturer   r   ZBio.SeqRecordr	   r   r   r   r   r?   r   r   r   r   <module>   s   