
:>"^%                 @   s   d  Z  d d l m Z d Z Gd d   d e  Z Gd d   d e  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.    )print_function   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   s   i  |  _  | j   } | j   } d } x7 t t |  d  D] } | | d |  j  | | <q> W| d } y | d |  j  d <Wn% t k
 r } z WYd d } ~ Xn Xd S)zInitialize the class.r      ZentrynumN)fieldssplitrangelen
IndexError)selfentryZheadlineZdatlistZheadlistie r   5/tmp/pip-build-ww9dw3qa/biopython/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   } | j   j d  d |  _ | j   j d  d |  _ | j   j d  d |  _ | j   j d  d |  _ | j   j d  d |  _ | j   j d  d |  _ d d   | D |  _	 Wd QRXd S)zInitialize the class.r
r   c             S   s#   g  |  ] } | j  d   d  q S)r   r   )r   ).0liner   r   r   
<listcomp>s   s   	 z%Peaklist.__init__.<locals>.<listcomp>N)
openreadliner   	firstline
axislabelsdatasetswsf
datalabelsdata)r   Zinfninfiler   r   r   r   f   s    zPeaklist.__init__c       
      C   sH  d } d } i  |  _  xt t |  j   D] } |  j | } t | |  j  j | d } | j d  d } t |  } | d	 k r | } | d
 k r | } t	 | | g  } t
 | | g  } t |  |  j  k r|  j  t |  }	 |	 j |  |	 |  j  t |  <q+ | g |  j  t |  <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']

        r   z.L.r   maxresminresr)   r)   r)   )dictr   r	   r$   r   r#   r   r   intmaxminstrappend)
r   indexr'   r(   r   r   indkeyresZtemplstr   r   r   residue_dictu   s,    	zPeaklist.residue_dictc          	   C   s   t  | d   } | j |  j  | j d  | j |  j  | j d  | j |  j  | j d  | j |  j  | j d  | j |  j  | j d  | j |  j  | j d  Wd QRXd S)z7Write header lines from input file to handle ``outfn``.wr   N)r   writer   r   r    r!   r"   r#   )r   Zoutfnoutfiler   r   r   write_header   s    zPeaklist.write_headerN)r   r   r   r   r   r4   r8   r   r   r   r   r   ;   s   )>r   c             C   s_   t  |  |  } t |  | d  j   d  } |  d |  t |  |  | | d  } | S)zReplace 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_entryr	   r   r.   )r   ZfieldnZnewentrystartlengnewliner   r   r   replace_entry   s    	 ,r=   c             C   s   d } | d k r d Sd } t  |   } |  d d k rG d } d } n d } d } xz | | k  r | | k  r | r |  | d k r |  | d d k r d } n  |  | d k s d } | d } | d } qV W| 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    )r	   )r   nZinfieldcr;   fieldr   r   r   r9      s&    	%	
r9   c             C   s@  g  } t  |  |  \ } } | d d } | d d } xB | D]: } | | d k  r^ | d } | | d k r> | d } q> W| }	 x |	 | k r;d }
 t |	  } xq | D]i } | |
 } t |	  | k r | d t | t |	  d |  j | } n | d d } |
 d }
 q W| d } | j |  |	 d }	 q W| 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'   	*r   r   )_read_dictsr.   r   r   r/   )fn_listZ	datalabelkeyatomZoutlist	dict_listZlabel_line_listZminrZmaxr
dictionaryr3   countr   labelr   r   r   
data_table  s.    

/
rK   c             C   s_   g  } g  } xF |  D]> } t  |  } | j |  } | j |  | j | j  q W| | g S)zBRead multiple files into a list of residue dictionaries (PRIVATE).)r   r4   r/   r#   )rE   rF   rG   Zdatalabel_listfnZpeaklistr*   r   r   r   rD   8  s    rD   __main__)run_doctestN)r   
__future__r   Z	HEADERLENobjectr   r   r=   r9   rK   rD   r   Z
Bio._utilsrN   r   r   r   r   <module>   s   ..7