B
    b                 @   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 d	d
 Zdd Zdd Zdd Zdd ZG dd de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)Seq)FeatureLocation)
SeqFeature)	SeqRecord   )SequenceIteratorc             C   s*   |  |}t||k r&td| d|S )z9Read the specified number of bytes from the given handle.zCannot read z bytes from handle)readlen
ValueError)handlelengthdata r   .lib/python3.7/site-packages/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 | |d}|S )zeRead a Pascal string.

    A Pascal string is one byte for length followed by the actual string.
    r   z>Br   ASCII)r   r   decode)r   r   r   r   r   r   _read_pstring.   s    
r   c             C   s,   t | d}td|d }t | |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_p4string9   s    
r   c             c   sD  |  d}|stdt|dk r*tdt| \}}td|d d d }||d kr`td|dd  d}tt|}t|  t| \}}td	|d d
 \}}|t|krtd|d
 |d krtdxtd|D ]}d
|d  }	||	|	d  }
td|
\}}}}}}}|dkr(d}nd}t	|||d}|dkrJd}nd}i }|dkrnt
| }|g|d< |dkrt| }|g|d< |dkrqt|||d}|j| qW t| \}}td	|d d
 \}}|d
 |d krtdx^td|D ]P}d
|d  }	||	|	d  }td|\}}}}|r<t
|  |rt|  qW t|  t| d}td|d }t| |d }xTtd|D ]F}|d }	||	|	d  }td|dd  d }|dkrt|  qW t| d t
| }|dd  |_|_||_t| d}td |d }|dkr0d!|jd"< n
d#|jd"< |V  d S )$N   zEmpty file.z1Improper header, cannot read 24 bytes from handlez>Ir   r   z"Conflicting sequence length valuesr   z>IH   \   z9Features packet size inconsistent with number of featuresz>II6xH14xB17xII35xBr   )strandZCDSZmisc_featureZ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   ranger   r   r   r   Zfeaturesappendr   splitnameidZdescriptionZannotations)r   r   Zpacketr   Z
seq_lengthZsequencerecordZnum_featuresioffsetZfeature_datastartendr   r   Zhas_nameZhas_commentversionlocationr   r'   commentZfeatureZ	num_sitesZ	site_dataZnum_versionsZversionsZversion_dataflagsZcircularityr   r   r   _parseD   s    













r2   c                   s(   e Zd ZdZ fddZdd Z  ZS )GckIteratorzParser for GCK files.c                s   t  j|ddd dS )z+Break up a GCK file into SeqRecord objects.bZGCK)modeZfmtN)super__init__)selfsource)	__class__r   r   r7      s    zGckIterator.__init__c             C   s   t |}|S )zStart parsing the file, and return a SeqRecord generator.

        Note that a GCK file can only contain one sequence, so this
        iterator will always return a single record.
        )r2   )r8   r   Zrecordsr   r   r   parse   s    zGckIterator.parse)__name__
__module____qualname____doc__r7   r;   __classcell__r   r   )r:   r   r3      s   r3   N)r?   Zstructr   ZBio.Seqr   ZBio.SeqFeaturer   r   ZBio.SeqRecordr   Z
Interfacesr   r   r   r   r   r2   r3   r   r   r   r   <module>   s    