o
    Rŀg:$                     @   sf   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r1ddl	m
Z
 e
  dS 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 dictionary 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   sR   |  }|  }tt||dd | _z
|d | jd< W dS  ty(   Y dS w )Initialize the class.   N    entrynum)splitdictzipfields
IndexError)selfentryheadlinedatlistheadlist r   D/var/www/html/myenv/lib/python3.10/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 |M}| dd | _| dd | _| dd | _| dd | _| dd | _| dd | _dd |D | _	W d   dS 1 sTw   Y  dS )r   
r   c                 S   s   g | ]	}| d d qS )r   r   )r   ).0liner   r   r   
<listcomp>j   s    z%Peaklist.__init__.<locals>.<listcomp>N)
openreadliner   	firstline
axislabelsdatasetswsf
datalabelsdata)r   infninfiler   r   r   r   ^   s   
"zPeaklist.__init__c              	   C   s   d}d}i | _ | jD ]M}t|| jj|d  }|dd }t|}|dkr(|}|dkr.|}t||g}t||g}t	|}z
| j | 
| W q
 tyW   |g| j |< Y 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   indkeyresr   r   r   residue_dictl   s,   


zPeaklist.residue_dictc                 C   s   t |dK}|| j |d || j |d || j |d || j |d || j |d || j |d W d   dS 1 sSw   Y  dS )z7Write header lines from input file to handle ``outfn``.wr   N)r   writer   r    r!   r"   r#   r$   )r   outfnoutfiler   r   r   write_header   s   




"zPeaklist.write_headerN)r   r   r   r   r   r6   r;   r   r   r   r   r   3   s
    *;r   c                 C   sJ   t | |}t| |d  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_entrylenr   r/   )r   fieldnnewentrystartlengnewliner   r   r   replace_entry   s   
	$rC   c                 C   s   |dkrdS d}t | }| d dkrd}d}nd}d}||k rN||k rN|rB| | dkr6| |d  dkr6d}n| | dkrBd}|d7 }|d7 }||k rN||k s#|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)r=   )r   ncrA   infieldfieldr   r   r   r<      s&   r<   c                 C   s   g }t | |\}}|d d }|d d }|D ]}||d k r#|d }||d kr-|d }q|}	|	|krsd}
t|	}|}|D ]#}||
 }||v rY|d t|| d |j|  }n|d7 }|
d7 }
q>|d7 }|| |	d7 }	|	|ks4|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
   r0   )fn_list	datalabelkeyatomoutlist	dict_listlabel_line_listminrmaxr
dictionaryr5   countr4   r   labelr   r   r   
data_table   s6   

rV   c                 C   sB   g }g }| D ]}t |}||}|| ||j q||gS )zBRead multiple files into a list of residue dictionaries (PRIVATE).)r   r6   r0   r$   )rK   rM   rO   datalabel_listfnpeaklistrS   r   r   r   rJ   )  s   

rJ   __main__r   )run_doctestN)r   	HEADERLENr   r   rC   r<   rV   rJ   r   
Bio._utilsr[   r   r   r   r   <module>   s   ) ,6
