
    Ri:$                     r    d Z dZ G d d      Z G d d      Zd Zd Zd Zd	 Zed
k(  rddl	m
Z
  e
        yy)z:Tools to manipulate data from nmrview .xpk peaklist files.   c                       e Zd ZdZd Zy)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                     |j                         }|j                         }t        t        ||dd             | _        	 |d   | j                  d<   y# t        $ r Y yw xY w)Initialize the class.   N    entrynum)splitdictzipfields
IndexError)selfentryheadlinedatlistheadlists        H/home/agent/.friday_env/lib/python3.12/site-packages/Bio/NMR/xpktools.py__init__zXpkEntry.__init__$   s]     ++->>#3x56	&-ajDKK
# 		s   A 	AAN)__name__
__module____qualname____doc__r        r   r   r   
   s    2r   r   c                   "    e Zd ZdZd Zd Zd Zy)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                 p   t        |      5 }|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 cg c]  }|j                  d      d    c}| _	        ddd       yc c}w # 1 sw Y   yxY w)r   
r   N)
openreadliner
   	firstline
axislabelsdatasetswsf
datalabelsdata)r   infninfilelines       r   r   zPeaklist.__init__^   s   $Z 
	C6#__.44V<Q?DN$oo/55f=a@DO!??,226:1=DLoo'--f5a8DGoo'--f5a8DG$oo/55f=a@DO <BB4F+A.BDI
	C 
	C C
	C 
	Cs   C/D,;D'D,'D,,D5c                    d}d}i | _         | j                  D ]  }t        || j                        j                  |dz      }|j                  d      d   }t        |      }|dk(  r|}|dk(  r|}t        ||g      }t        ||g      }t        |      }	 | j                   |   j                  |        || j                   d<   || j                   d<   | j                   S # t        $ r |g| j                   |<   Y w xY w)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/   r0   r+   indkeyress           r   residue_dictzPeaklist.residue_dictl   s   6  	II 	(D4188FC))C.#Cc(C||&#'F&#'Fc(C(		#%%d+#	(, %		($		(yy  ("&		#(s   CC:9C:c                 V   t        |d      5 }|j                  | j                         |j                  d       |j                  | j                         |j                  d       |j                  | j                         |j                  d       |j                  | j
                         |j                  d       |j                  | j                         |j                  d       |j                  | j                         |j                  d       ddd       y# 1 sw Y   yxY w)z7Write header lines from input file to handle ``outfn``.wr   N)r    writer"   r#   r$   r%   r&   r'   )r   outfnoutfiles      r   write_headerzPeaklist.write_header   s    % 	"MM$..)MM&!MM$//*MM&!MM$,,'MM&!MM$''"MM&!MM$''"MM&!MM$//*MM&!	" 	" 	"s   D	DD(N)r   r   r   r   r   r;   rA   r   r   r   r   r   3   s    (TC9v"r   r   c                     t        | |      }t        | |d j                         d         }| d| t        |      z   | ||z   d z   }|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
   r4   )r+   fieldnnewentrystartlengnewlines         r   replace_entryrJ      sX     dF+EtEF|!!#A&'D6ElS]*T54<2B-CCGNr   c                     |dk(  ryd}t        |       }| d   dk(  rd}d}nd}d}||k  r<||k  r7|r%| |   dk(  r| |dz
     dk7  rd}n| |   dk7  rd}|dz  }|dz  }||k  r||k  r7|dz
  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)rD   )r+   ncrH   infieldfields         r   rC   rC      s     	Av 	
At9D Aw#~
d(uqyAw#~$q1u+"47c>"GQJE	Q d(uqy q5Lr   c                 z   g }t        | |      \  }}|d   d   }|d   d   }|D ]  }||d   k  r|d   }||d   kD  s|d   } |}	|	|k  rpd}
t        |	      }|}|D ];  }||
   }||v r&|dz   t        ||   d   |      j                  |   z   }n|dz  }|
dz  }
= |dz  }|j	                  |       |	dz  }	|	|k  rp|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   r0   r/   	z	*r   r   )_read_dictsr4   r   r   r5   )fn_list	datalabelkeyatomoutlist	dict_listlabel_line_listminrmaxr
dictionaryr:   countr9   r+   labels                 r   
data_tabler_      s'   & G!,Wg!>I Q<!DQ<!D (
*X&&h'D*X&&h'D	( C
+#h# 	J#E*Ej 4K(:c?1+=u"E"L"LY"WW  QJE	 	tq +" Nr   c                     g }g }| D ]J  }t        |      }|j                  |      }|j                  |       |j                  |j                         L ||gS )zBRead multiple files into a list of residue dictionaries (PRIVATE).)r   r;   r5   r'   )rT   rV   rX   datalabel_listfnpeaklistr\   s          r   rS   rS   )  sc    IN 3B<**73
$h112	3 ~&&r   __main__r   )run_doctestN)r   	HEADERLENr   r   rJ   rC   r_   rS   r   
Bio._utilsre   r   r   r   <module>rh      sU   
 A	& &RB" B"J)X3l
' z&M r   