
    XfU.                     6    d Z ddlmZ d Z G d d          ZdS )a  Code to parse BIG GenePop files.

The difference between this class and the standard Bio.PopGen.GenePop.Record
class is that this one does not read the whole file to memory.
It provides an iterator interface, slower but consuming much mess memory.
Should be used with big files (Thousands of markers and individuals).

See http://wbiomed.curtin.edu.au/genepop/ , the format is documented
here: http://wbiomed.curtin.edu.au/genepop/help_input.html .

Classes:
 - FileRecord           Holds GenePop data.

Functions:


    )	get_indivc                 $    t          |           }|S )zfParse a file containing a GenePop file.

    fname is a file name that contains a GenePop record.
    )
FileRecord)fnamerecords     =lib/python3.11/site-packages/Bio/PopGen/GenePop/FileParser.pyreadr	      s    
 FM    c                   Z    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd ZdS )r   a`  Hold information from a GenePop record.

    Attributes:
    - marker_len         The marker length (2 or 3 digit code per allele).
    - comment_line       Comment line.
    - loci_list          List of loci names.

    Methods:
    - get_individual     Returns the next individual of the current population.
    - skip_population    Skips the current population.

    skip_population skips the individuals of the current population, returns
    True if there are more populations.

    get_individual returns an individual of the current population (or None
    if the list ended).

    Each individual is a pair composed by individual name and a list of alleles
    (2 per marker or 1 for haploid data). Examples::

        ('Ind1', [(1,2),    (3,3), (200,201)]
        ('Ind2', [(2,None), (3,3), (None,None)]
        ('Other1', [(1,1),  (4,3), (200,200)]

    c                 X    d| _         g | _        || _        |                                  dS )zInitialize the class. N)comment_line	loci_listr   
start_read)selfr   s     r   __init__zFileRecord.__init__A   s.    
r
   c                 z   d}| j         dz   g}|                    d                    | j                  dz              | j        }| j        }| j                            d           |                                  |                    d           d}|r| 	                                }|du r|                    d           n|du rd}n|\  }}|                    |           |                    d           |D ]}	|                    d           |	D ]g}
|
d
}
t          |
          }t          |          |k     r*d                    d
|g          }t          |          |k     *|                    |           h|                    d           ||                     ||           d                    |          S )zReturn (reconstructs) a GenePop textual representation.

        This might take a lot of memory.
        Marker length will be 3.
           
r   zPop
TF, N0r   )r   appendjoinr   current_popcurrent_ind_handleseekskip_headerget_individualstrlenseek_position)r   
marker_lenrepr   r   moreresnamemarkersmarkeralaStrs               r   __str__zFileRecord.__str__H   s    
 4'(

499T^,,t3444&&!

7 	!%%''Cd{{

7#### #g

4   

3% ) )FJJsOOO$ ) ):!$B"2ww!$ii*44#%77C;#7#7D "$ii*44

4(((() 

4   '  	!( 	;444wws||r
   c                 R   t          | j                  | _        | j                                                                        | _        | j                                                                                            dd          }|                    d          }| j        	                    |           | j        D ]J}|                                }|
                                dk    r n*| j                            |           Kt          d          d| _        d| _        dS )z/Start parsing a file containing a GenePop file.r   r   r   POPz;No population data found, file probably not GenePop relatedr   N)openr   r   readlinerstripr   replacesplitr   extendupperr   
ValueErrorr   r   )r   sample_loci_lineall_locilines       r   r   zFileRecord.start_readn   s   DJ'' L1133::<<  <002299;;CCCLL#))#..h'''L 	 	D;;==Dzz||u$$N!!$''''M   r
   c                     d| _         d| _        | j        D ]/}|                                                                dk    r dS 0dS )z,Skip the Header. To be done after a re-open.r   r/   N)r   r   r   r2   r6   r   r:   s     r   r   zFileRecord.skip_header   sX    L 	 	D{{}}""$$-- .	 	r
   c                     | j                             d           |                                  |dk    r|                                  |dz  }|dk    |dk    r!|                                  |dz  }|dk    dS dS )zSeek a certain position in the file.

        Arguments:
         - pop - pop position (0 is first)
         - indiv - individual in pop

        r      N)r   r   r   skip_populationr    )r   popindivs      r   r#   zFileRecord.seek_position   s     	!Agg  """1HC Agg aii!!!QJE aiiiiiir
   c                     | j         D ]Q}|dk    r dS |                                }|                                dk    r| xj        dz  c_        d| _         dS RdS )zBSkip the current population. Returns true if there is another pop.r   Fr/   r>   r   TN)r   r2   r6   r   r   r<   s     r   r?   zFileRecord.skip_population   su    L 	 	Drzzuu;;==Dzz||u$$  A%  #$ tt %		 	r
   c                     | j         D ]p}|                                }|                                dk    r| xj        dz  c_        d| _         dS | xj        dz  c_        t          |          \  }}}||fc S dS )a\  Get the next individual.

        Returns individual information if there are more individuals
        in the current population.
        Returns True if there are no more individuals in the current
        population, but there are more populations. Next read will
        be of the following pop.
        Returns False if at end of file.
        r/   r>   r   TF)r   r2   r6   r   r   r   )r   r:   
indiv_nameallele_listignores        r   r    zFileRecord.get_individual   s     L 		/ 		/D;;==Dzz||u$$  A%  #$ tt  A%  2;D///
K!;....ur
   c                 X   t          | j                  }t          |d          5 }|                    | j        dz              |j        D ]}|                    |dz              d}|                                }d}|r||k    r|                                 |dz  }n|du r|dz  }d}n|r|                    d           d}|\  }	}
|                    |	dz              |
D ]}|                    d	           |D ]g}|d}t          |          }t          |          dk     r*d
                    d|g          }t          |          dk     *|                    |           h|                    d           |                                }|d
d
d
           d
S # 1 swxY w Y   d
S )zRemove a population (by position).

        Arguments:
         - pos - position
         - fname - file to be created with population removed

        wr   r   Tr>   POP
Fr   r   Nr   r   r   )r	   r   r0   writer   r   r    r?   r!   r"   r   )r   posr   old_recflocuscurr_popl_parser	start_popr(   r)   r*   r+   r,   s                 r   remove_populationzFileRecord.remove_population   s$    tz""%  	4GGD%,--- * & &%%%%H--//HI 4s??++---MHH4'' A$(		$ .GGG,,,(-I(0gs
+++&- . .FGGCLLL&, . .#%:),B'*2ww&)$ii!mm+-77C;+?+?D '*$ii!mm !. "11333  4 	4  	4  	4  	4  	4  	4  	4  	4  	4  	4  	4  	4  	4  	4  	4  	4  	4  	4s   E-FF#&F#c                 P   t          | j                  }t          |d          5 }|                    | j        dz              |j        }||= |D ]}|                    |dz              |                                }|                    d           |r|du r|                    d           n|\  }}	|                    |dz              d}
|	D ]}|
|k    r|
dz  }
|
dz  }
|                    d           |D ]g}|d
}t          |          }t          |          dk     r*d	                    d
|g          }t          |          dk     *|                    |           h|                    d           |                                }|d	d	d	           d	S # 1 swxY w Y   d	S )zRemove a locus by position.

        Arguments:
         - pos - position
         - fname - file to be created with locus removed

        rH   r   rI   Tr   r   r>   r   Nr   r   r   )
r	   r   r0   rJ   r   r   r    r!   r"   r   )r   rK   r   rL   rM   r   rN   rP   r(   r)   
marker_posr*   r+   r,   s                 r   remove_locus_by_positionz#FileRecord.remove_locus_by_position   s    tz""% 	4GGD%,---)I#" & &%%%%--//HGGG 4t##GGG$$$$$,MD'GGD3J'''!"J") * *%,,&!OJ$"a
"( * *B!z%(#&r77D"%d))a--')wwT{';'; #&d))a--GGDMMMM* GGDMMM"1133-  4	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4s   E)FF"Fc                    t          | j                  }t          |d          5 }|                    | j        dz              |j        }|                                 |                                 t                      }|D ]}||= |	                    |           |D ]}|                    |dz              |
                                }	|                    d           |	r|	du r|                    d           n|	\  }
}|                    |
dz              d}|D ]}||v r|dz  }|dz  }|                    d           |D ]g}|d
}t          |          }t          |          dk     r*d                    d
|g          }t          |          dk     *|                    |           h|                    d           |
                                }	|	d	d	d	           d	S # 1 swxY w Y   d	S )zRemove a set of loci by position.

        Arguments:
         - positions - positions
         - fname - file to be created with locus removed

        rH   r   rI   Tr   r   r>   r   Nr   r   r   )r	   r   r0   rJ   r   r   sortreversesetaddr    r!   r"   r   )r   	positionsr   rL   rM   r   posSetrK   rN   rP   r(   r)   rT   r*   r+   r,   s                   r   remove_loci_by_positionz"FileRecord.remove_loci_by_position  sh    tz""% #	4GGD%,---)INNUUF     cN

3" & &%%%%--//HGGG 4t##GGG$$$$$,MD'GGD3J'''!"J") * *%//&!OJ$"a
"( * *B!z%(#&r77D"%d))a--')wwT{';'; #&d))a--GGDMMMM* GGDMMM"1133-  4#	4 #	4 #	4 #	4 #	4 #	4 #	4 #	4 #	4 #	4 #	4 #	4 #	4 #	4 #	4 #	4 #	4 #	4s   F7G))G-0G-c                 x    t          | j                  D ]$\  }}||k    r|                     ||            dS %dS )zRemove a locus by name.

        Arguments:
         - name - name
         - fname - file to be created with locus removed

        N)	enumerater   rU   )r   r(   r   irN   s        r   remove_locus_by_namezFileRecord.remove_locus_by_nameA  sV     "$.11 	 	HAu}}--a777 	 	r
   c                     g }t          | j                  D ]\  }}||v r|                    |           |                     ||           dS )zRemove a loci list (by name).

        Arguments:
         - names - names
         - fname - file to be created with loci removed

        N)r_   r   r   r]   )r   namesr   r[   r`   rN   s         r   remove_loci_by_namezFileRecord.remove_loci_by_nameP  s`     	!$.11 	$ 	$HAu~~  ###$$Y66666r
   N)__name__
__module____qualname____doc__r   r-   r   r   r#   r?   r    rR   rU   r]   ra   rd    r
   r   r   r   &   s         4  $ $ $L  .    "	 	 	  ,)4 )4 )4V'4 '4 '4R,4 ,4 ,4\  7 7 7 7 7r
   r   N)rh   Bio.PopGen.GenePopr   r	   r   ri   r
   r   <module>rk      sn    $ ) ( ( ( ( (  v7 v7 v7 v7 v7 v7 v7 v7 v7 v7r
   