B
    ‰°b;$  ã               @   sb   d Z dZG dd„ dƒZG dd„ dƒZdd„ Zdd	„ Zd
d„ Zdd„ Zedkr^ddl	m
Z
 e
ƒ  dS )z:Tools to manipulate data from nmrview .xpk peaklist files.é   c               @   s   e Zd ZdZdd„ ZdS )ÚXpkEntrya‰  Provide dictonary access to single entry from nmrview .xpk file.

    This class is suited for handling single lines of non-header data
    from an nmrview .xpk file. This class provides methods for extracting
    data by the field name which is listed in the last line of the
    peaklist header.

    Parameters
    ----------
    xpkentry : str
        The line from an nmrview .xpk file.
    xpkheadline : str
        The line from the header file that gives the names of the entries.
        This is typically the sixth line of the header, 1-origin.

    Attributes
    ----------
    fields : dict
        Dictionary of fields where key is in header line, value is an entry.
        Variables are accessed by either their name in the header line as in
        self.field["H1.P"] will return the H1.P entry for example.
        self.field["entrynum"] returns the line number (1st field of line)

    c             C   sT   |  ¡ }|  ¡ }tt||dd… ƒƒ| _y|d | jd< W n tk
rN   Y nX dS )zInitialize the class.é   Né    Zentrynum)ÚsplitÚdictÚzipÚfieldsÚ
IndexError)ÚselfÚentryZheadlineZdatlistZheadlist© r   ú/lib/python3.7/site-packages/Bio/NMR/xpktools.pyÚ__init__%   s    zXpkEntry.__init__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r      s   r   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚPeaklista;  Provide access to header lines and data from a nmrview xpk file.

    Header file lines and file data are available as attributes.

    Parameters
    ----------
    infn : str
        The input nmrview filename.

    Attributes
    ----------
    firstline  : str
        The first line in the header.
    axislabels : str
        The axis labels.
    dataset    : str
        The label of the dataset.
    sw         : str
        The sw coordinates.
    sf         : str
        The sf coordinates.
    datalabels : str
        The labels of the entries.

    data : list
        File data after header lines.

    Examples
    --------
    >>> from Bio.NMR.xpktools import Peaklist
    >>> peaklist = Peaklist('../Doc/examples/nmr/noed.xpk')
    >>> peaklist.firstline
    'label dataset sw sf '
    >>> peaklist.dataset
    'test.nv'
    >>> peaklist.sf
    '{599.8230 } { 60.7860 } { 60.7860 }'
    >>> peaklist.datalabels
    ' H1.L  H1.P  H1.W  H1.B  H1.E  H1.J  15N2.L  15N2.P  15N2.W  15N2.B  15N2.E  15N2.J  N15.L  N15.P  N15.W  N15.B  N15.E  N15.J  vol  int  stat '

    c          	   C   s    t |ƒŽ}| ¡  d¡d | _| ¡  d¡d | _| ¡  d¡d | _| ¡  d¡d | _| ¡  d¡d | _| ¡  d¡d | _dd„ |D ƒ| _	W dQ R X dS )zInitialize the class.Ú
r   c             S   s   g | ]}|  d ¡d ‘qS )r   r   )r   )Ú.0Úliner   r   r   ú
<listcomp>l   s    z%Peaklist.__init__.<locals>.<listcomp>N)
ÚopenÚreadliner   Ú	firstlineÚ
axislabelsÚdatasetÚswÚsfÚ
datalabelsÚdata)r
   ZinfnZinfiler   r   r   r   _   s    
zPeaklist.__init__c          	   C   sÒ   d}d}i | _ x¨| jD ]ž}t|| jƒj|d  }| d¡d }t|ƒ}|dkrR|}|dkr^|}t||gƒ}t||gƒ}t	|ƒ}y| j |  
|¡ W q tk
r²   |g| j |< Y qX qW || j d< || j d< | j S )aª  Return a dict of lines in 'data' indexed by residue number or a nucleus.

        The nucleus should be given as the input argument in the same form as
        it appears in the xpk label line (H1, 15N for example)

        Parameters
        ----------
        index : str
            The nucleus to index data by.

        Returns
        -------
        resdict : dict
            Mappings of index nucleus to data line.

        Examples
        --------
        >>> from Bio.NMR.xpktools import Peaklist
        >>> peaklist = Peaklist('../Doc/examples/nmr/noed.xpk')
        >>> residue_d = peaklist.residue_dict('H1')
        >>> sorted(residue_d.keys())
        ['10', '3', '4', '5', '6', '7', '8', '9', 'maxres', 'minres']
        >>> residue_d['10']
        ['8  10.hn   7.663   0.021   0.010   ++   0.000   10.n   118.341   0.324   0.010   +E   0.000   10.n   118.476   0.324   0.010   +E   0.000  0.49840 0.49840 0']

        éÿÿÿÿz.LÚ.r   ÚmaxresÚminres)r   r    r   r   r   r   ÚintÚmaxÚminÚstrÚappendÚKeyError)r
   Úindexr#   r$   r   ZindÚkeyÚresr   r   r   Úresidue_dictn   s*    

zPeaklist.residue_dictc          	   C   sž   t |dƒŠ}| | j¡ | d¡ | | j¡ | d¡ | | j¡ | d¡ | | j¡ | d¡ | | j¡ | d¡ | | j¡ | d¡ W dQ R X dS )z7Write header lines from input file to handle ``outfn``.Úwr   N)r   Úwriter   r   r   r   r   r   )r
   ZoutfnZoutfiler   r   r   Úwrite_header©   s    




zPeaklist.write_headerN)r   r   r   r   r   r.   r1   r   r   r   r   r   4   s   );r   c             C   sJ   t | |ƒ}t| |d…  ¡ d ƒ}| d|… t|ƒ | || d…  }|S )zËReplace an entry in a string by the field number.

    No padding is implemented currently.  Spacing will change if
    the original field entry and the new field entry are of
    different lengths.
    Nr   )Ú_find_start_entryÚlenr   r(   )r   ZfieldnZnewentryÚstartÚlengÚnewliner   r   r   Úreplace_entryº   s    	
$r7   c             C   sš   |dkrdS d}t | ƒ}| d dkr.d}d}nd}d}xZ||k r||k r|r†| | dkrn| |d  dkrnd}n| | dkr†d}|d7 }|d7 }q8W |d S )a"  Find the starting character for entry ``n`` in a space delimited ``line`` (PRIVATE).

    n is counted starting with 1.
    The n=1 field by definition begins at the first character.

    Returns
    -------
    starting character : str
        The starting character for entry ``n``.

    r   r   ú FT)r3   )r   ÚnÚcr5   ZinfieldZfieldr   r   r   r2   É   s$    r2   c             C   sð   g }t | |ƒ\}}|d d }|d d }x4|D ],}||d k rH|d }||d kr0|d }q0W |}	x†|	|krêd}
t|	ƒ}|}xN|D ]F}||
 }||krº|d t|| d |ƒj|  }n|d7 }|
d7 }
q„W |d7 }| |¡ |	d7 }	qfW |S )az  Generate a data table from a list of input xpk files.

    Parameters
    ----------
    fn_list : list
        List of .xpk file names.
    datalabel : str
        The data element reported.
    keyatom : str
        The name of the nucleus used as an index for the data table.

    Returns
    -------
    outlist : list
       List of table rows indexed by ``keyatom``.

    r   r$   r#   ú	z	*r   r   )Ú_read_dictsr(   r   r   r)   )Úfn_listZ	datalabelÚkeyatomZoutlistÚ	dict_listZlabel_line_listZminrZmaxrÚ
dictionaryr-   Úcountr,   r   Zlabelr   r   r   Ú
data_tableõ   s0    


"
rB   c             C   sF   g }g }x4| D ],}t |ƒ}| |¡}| |¡ | |j¡ qW ||gS )zBRead multiple files into a list of residue dictionaries (PRIVATE).)r   r.   r)   r   )r=   r>   r?   Zdatalabel_listÚfnZpeaklistr@   r   r   r   r<   +  s    


r<   Ú__main__r   )Úrun_doctestN)r   Z	HEADERLENr   r   r7   r2   rB   r<   r   Z
Bio._utilsrE   r   r   r   r   Ú<module>   s   ) ,6