o
    RŀgO                     @   s&   d Z dd Zdd ZG dd dZdS )a  Large file parsing of Genepop files.

The standard parser loads the whole file into memory. This parser
provides an iterator over data.

Classes:
- LargeRecord - Holds GenePop data.

Functions:
- read - Parses a GenePop record (file) into a Record object.

c                    s   |  d\}}|dd d}dd |D }t|d dv r"d nd	 z fd
d|D }W n tyA    fdd|D }Y nw || fS )z Get individual's data from line.,	 c                 S   s   g | ]}|d kr|qS )  .0markerr   r   V/var/www/html/myenv/lib/python3.10/site-packages/Bio/PopGen/GenePop/LargeFileParser.py
<listcomp>   s    zget_indiv.<locals>.<listcomp>    )      r      c                    s,   g | ]}t |d   t | d fqS )r   Nintr   
marker_lenr   r	   r
       s     c                    s   g | ]}t |d   fqS )r   r   r   r   r   r	   r
   $   s    )splitreplacelen
ValueError)line
indiv_namemarker_linemarkersallele_listr   r   r	   	get_indiv   s   


r   c                 C   s   t | }t|  |_t|  dd}|d}|j| |  }|dkrG| }|	 dkr9|j
d n|j| |  }|dks(|   }t|\}}|_|j
| |S )zParse a handle containing a GenePop file.

    Arguments:
    - handle is a file-like object that contains a GenePop record.

    r   r   r   POP)Recordnextrstripcomment_liner   r   	loci_listextendreadlineupperstackappendr   r   )handlerecordsample_loci_lineall_locir   	next_liner   r   r   r   r	   read(   s$   
r-   c                   @   s    e Zd ZdZdd Zdd ZdS )r   a  Hold information from a GenePop record.

    Members:
    marker_len         The marker length (2 or 3 digit code per allele).

    comment_line       Comment line.

    loci_list          List of loci names.

    data_generator     Iterates over population data.

    The generator will only work once. If you want to read a handle
    twice you have to re-open it!

    data_generator can either be () - an empty tuple - marking a new
    population or an individual. An individual is something like
    ('Ind1', [(1,1), (3,None), (200,201)],
    In the case above the individual is called Ind1,
    has three diploid loci. For the second loci, one of the alleles
    is unknown.

    c                 C   s(   || _ d| _d| _g | _g | _g | _dS )zInitialize the class.r   r   N)r(   r   r!   r"   populationsr&   )selfr(   r   r   r	   __init__]   s   
zRecord.__init__c           
      c   s    | j | jfD ]E}|D ]@}| }| dkrdV  qt|\}}}g }|D ]}g }|D ]}	|	dkr8|d q,||	 q,|t| q&||fV  qqdS )zExtract population data.r   r   r   N)r&   r(   r    r%   r   r'   tuple)
r/   r(   r   r   r   r   
clean_listlocusmk_realalr   r   r	   data_generatorf   s$   zRecord.data_generatorN)__name__
__module____qualname____doc__r0   r6   r   r   r   r	   r   E   s    	r   N)r:   r   r-   r   r   r   r   r	   <module>   s   