
:>"^ÛK  ã            -   @   sf  d  Z  d d l m Z d d l Z d d l m Z d d l Z d d l Z d d l m	 Z	 d d l
 m Z d d l m Z d d l m Z e j d	 ƒ Z d
 d d d d d d d d d d d d d d d d d d d d d d  d! d" d# d$ d% d& d' d( d) d* d+ d, d- d. d/ d0 d1 d2 i d3 d d4 d d5 d d6 d d7 d d8 d d9 d d: d d; d d< d d= d d> d! d? d# d< d% d@ d' dA d) dB d+ dC d- dD d/ dE d1 dF i dG d dH d dI d dJ d dK d dL d dM d d d dN d dO d dP d dQ d! dR d# dS d% d= d' dT d) dU d+ dV d- dW d/ dX d1 dV i i Z dY dZ „  Z d[ d\ d] „ Z d^ d_ „  Z d` da „  Z Gdb dc „  dc e	 ƒ Z d S)daS	  Use the DSSP program to calculate secondary structure and accessibility.

You need to have a working version of DSSP (and a license, free for academic
use) in order to use this. For DSSP, see http://swift.cmbi.ru.nl/gv/dssp/.

The following Accessible surface area (ASA) values can be used, defaulting
to the Sander and Rost values:

    Miller
        Miller et al. 1987 https://doi.org/10.1016/0022-2836(87)90038-6
    Sander
        Sander and Rost 1994 https://doi.org/10.1002/prot.340200303
    Wilke
        Tien et al. 2013 https://doi.org/10.1371/journal.pone.0080635

The DSSP codes for secondary structure used here are:

    =====     ====
    Code      Structure
    =====     ====
     H        Alpha helix (4-12)
     B        Isolated beta-bridge residue
     E        Strand
     G        3-10 helix
     I        Pi helix
     T        Turn
     S        Bend
     \-       None
    =====     ====

Usage
-----
The DSSP class can be used to run DSSP on a pdb file, and provides a
handle to the DSSP secondary structure and accessibility.

**Note** that DSSP can only handle one model, and will only run
calculations on the first model in the provided PDB file.

Examples
--------
Typical use::

    from Bio.PDB import PDBParser
    from Bio.PDB.DSSP import DSSP
    p = PDBParser()
    structure = p.get_structure("1MOT", "/local-pdb/1mot.pdb")
    model = structure[0]
    dssp = DSSP(model, "/local-pdb/1mot.pdb")

Note that the recent DSSP executable from the DSSP-2 package was
renamed from ``dssp`` to ``mkdssp``. If using a recent DSSP release,
you may need to provide the name of your DSSP executable::

    dssp = DSSP(model, '/local-pdb/1mot.pdb', dssp='mkdssp')

DSSP data is accessed by a tuple - (chain id, residue id)::

    a_key = list(dssp.keys())[2]
    dssp[a_key]

The dssp data returned for a single residue is a tuple in the form:

    ============ ===
    Tuple Index  Value
    ============ ===
    0            DSSP index
    1            Amino acid
    2            Secondary structure
    3            Relative ASA
    4            Phi
    5            Psi
    6            NH-->O_1_relidx
    7            NH-->O_1_energy
    8            O-->NH_1_relidx
    9            O-->NH_1_energy
    10           NH-->O_2_relidx
    11           NH-->O_2_energy
    12           O-->NH_2_relidx
    13           O-->NH_2_energy
    ============ ===

é    )Úprint_functionN)ÚStringIO)ÚAbstractResiduePropertyMap)ÚPDBException)Ú	PDBParser)Úthree_to_onez[a-z]ZMillerZALAg     @\@ZARGg      n@ZASNg     Àc@ZASPg     àb@ZCYSg     €a@ZGLNg      g@ZGLUg     àf@ZGLYg     @U@ZHISg     @h@ZILEg     Àf@ZLEUg     €f@ZLYSg     `j@ZMETg     €i@ZPHEg     @k@ZPROg     àa@ZSERg     €^@ZTHRg     @b@ZTRPg     0p@ZTYRg      l@ZVALg      d@ZWilkeg      `@g      q@g     `h@g      h@g     àd@g      l@g     àk@g      Z@g      l@g      h@g      i@g     €m@g      n@g     àc@g     `c@g     €e@g     Ðq@g     pp@g     Àe@ÚSanderg     €Z@g      o@g      c@g     `d@g     à`@g     Àh@g      U@g      g@g      e@g     €d@g      i@g     €g@g      a@g     @`@g     Àa@g     `l@g     Àk@c             C   s@   |  d k r d S|  d k r  d S|  d k r0 d Sd s< t  ‚ d S)zBSecondary structure symbol to index.

    H=0
    E=1
    C=2
    ÚHr   ÚEé   ÚCé   N)ÚAssertionError)Úss© r   ú1/tmp/pip-build-ww9dw3qa/biopython/Bio/PDB/DSSP.pyÚss_to_index¶   s    r   Údsspc             C   sç   y1 t  j | |  g d d d t  j d t  j ƒ} WnN t k
 r | d k rP ‚  t  j d |  g d d d t  j d t  j ƒ} Yn X| j ƒ  \ } } | j ƒ  rÅ t j | ƒ | j ƒ  sÅ t d ƒ ‚ t	 t
 | ƒ ƒ \ } } | | f S)aƒ  Create a DSSP dictionary from a PDB file.

    Parameters
    ----------
    in_file : string
        pdb file

    DSSP : string
        DSSP executable (argument to os.system)

    Returns
    -------
    (out_dict, keys) : tuple
        a dictionary that maps (chainid, resid) to
        amino acid type, secondary structure code and
        accessibility.

    Examples
    --------
    How dssp_dict_frompdb_file could be used::

        from Bio.PDB.DSSP import dssp_dict_from_pdb_file
        dssp_tuple = dssp_dict_from_pdb_file("/local-pdb/1fat.pdb")
        dssp_dict = dssp_tuple[0]
        print(dssp_dict['A',(' ', 1, ' ')])

    Úuniversal_newlinesTÚstdoutÚstderrÚmkdsspz DSSP failed to produce an output)Ú
subprocessÚPopenÚPIPEÚOSErrorÚcommunicateÚstripÚwarningsÚwarnÚ	ExceptionÚ_make_dssp_dictr   )Úin_fileÚDSSPÚpÚoutÚerrZout_dictÚkeysr   r   r   Údssp_dict_from_pdb_fileÆ   s*    !		r(   c          	   C   s'   t  |  d ƒ  } t | ƒ SWd QRXd S)zöDSSP dictionary mapping identifiers to properties.

    Return a DSSP dictionary that maps (chainid, resid) to
    aa, ss and accessibility, from a DSSP file.

    Parameters
    ----------
    filename : string
        the DSSP output file

    ÚrN)Úopenr!   )ÚfilenameÚhandler   r   r   Úmake_dssp_dict  s    r-   c             C   së  i  } d } g  } xÌ|  j  ƒ  D]¾} | j ƒ  } t | ƒ d k  rF q | d d k r_ d } q | sh q | d d k r{ q t | d d … ƒ } t | d d	 … ƒ } | d	 } | d
 }	 | d }
 | d } | d k rá d } yö t | d d … ƒ } t | d d … ƒ } t | d d … ƒ } t | d d … ƒ } t | d d … ƒ } t | d d … ƒ } t | d d … ƒ } t | d d … ƒ } t | d d … ƒ } t | d d … ƒ } t | d d … ƒ } Wn§t k
 r€} z‡| d d k rb| d d … j d ƒ } t | d | d | … ƒ } t | d | d | … ƒ } t | d | d | … ƒ } t | d | d | … ƒ } t | d | d | … ƒ } t | d | d | … ƒ } t | d | d | … ƒ } t | d | d | … ƒ } t | d | d | … ƒ } t | d | d | … ƒ } t | d | d | … ƒ } n t | ƒ ‚ WYd d } ~ Xn Xd | | f } |
 | | | | | | | | | | | | | f | |	 | f <| j |	 | f ƒ q W| | f S)a  Return a DSSP dictionary, used by mask_dssp_dict (PRIVATE).

    DSSP dictionary maps (chainid, resid) to an amino acid,
    secondary structure symbol, solvent accessibility value, and hydrogen bond
    information (relative dssp indices and hydrogen bond energies) from an open
    DSSP file object.

    Parameters
    ----------
    handle : file
        the open DSSP output file handle

    r   r   r   ZRESIDUEé	   ú Né   é
   é   é   é   ú-é&   é-   é.   é2   é8   é9   é=   éC   éD   éH   éN   éO   éS   é"   ég   ém   és   )Ú	readlinesÚsplitÚlenÚintÚfloatÚ
ValueErrorÚfindÚappend)r,   r   Ústartr'   ÚlÚslÚ
dssp_indexZresseqZicodeZchainidÚaar   ÚNH_O_1_relidxÚNH_O_1_energyÚO_NH_1_relidxÚO_NH_1_energyÚNH_O_2_relidxÚNH_O_2_energyÚO_NH_2_relidxÚO_NH_2_energyÚaccÚphiÚpsiÚexcZshiftÚres_idr   r   r   r!     s„    



!r!   c               @   s+   e  Z d  Z d Z d d d d d „ Z d S)r#   a]  Run DSSP and parse secondary structure and accessibility.

    Run DSSP on a pdb file, and provide a handle to the
    DSSP secondary structure and accessibility.

    **Note** that DSSP can only handle one model.

    Examples
    --------
    How DSSP could be used::

        from Bio.PDB import PDBParser
        from Bio.PDB.DSSP import DSSP
        p = PDBParser()
        structure = p.get_structure("1MOT", "/local-pdb/1mot.pdb")
        model = structure[0]
        dssp = DSSP(model, "/local-pdb/1mot.pdb")
        # DSSP data is accessed by a tuple (chain_id, res_id)
        a_key = list(dssp.keys())[2]
        # (dssp index, amino acid, secondary structure, relative ASA, phi, psi,
        # NH_O_1_relidx, NH_O_1_energy, O_NH_1_relidx, O_NH_1_energy,
        # NH_O_2_relidx, NH_O_2_energy, O_NH_2_relidx, O_NH_2_energy)
        dssp[a_key]

    r   r   ÚPDBc       &   /   C   sÊ  t  | |  _  | j ƒ  } | d# k s+ t ‚ | d k rª y t | | ƒ \ } } Wn? t k
 r‘ | d k ru d } n | d k rŠ d } n ‚  Yn Xt | | ƒ \ } } n | d k rÈ t | ƒ \ } } i  } g  }	 d d „  }
 xÍ| D]Å} | \ } } | | } y | | } Wnj t k
 r}|
 | ƒ } xI | D]5 } | j d d$ k r4|
 | j ƒ | k r4| } Pq4Wt | ƒ ‚ Yn X| j ƒ  d
 k rþx| j	 ƒ  D]C } | j
 | j ƒ  d j ƒ  } | t d ƒ k r| j | ƒ PqW| j | j	 ƒ  d ƒ n¨ | j ƒ  d k r¦d d „  | j ƒ  Dƒ } | j d ƒ r¦|
 | ƒ } x_ | D]W } | j d d% k rK|
 | j ƒ | k rK| j ƒ  d j ƒ  t d ƒ k rK| } PqKW| | \ } } } } } } } } } } } }  }! }" | | j d <| | j d <| | j d <| | j d <| | j d <| | j d <| | j d <| | j d <| | j d <| | j d <|  | j d <|! | j d <|" | j d <| j ƒ  }# y | |  j  |# }$ Wn t k
 r¾d }$ Yn X|$ d k rÑd }$ |$ | j d <y t |# ƒ }# Wn t k
 rd }# Yn X|# d  k r*t j | ƒ r*d  } |# | k re| j d d k sU| d k ret d! | ƒ ‚ | | | |$ | | | | | | | |  |! |" f }% |% | | <|	 j |% ƒ qç Wt j |  | | |	 ƒ d" S)&aj  Create a DSSP object.

        Parameters
        ----------
        model : Model
            The first model of the structure
        in_file : string
            Either a PDB file or a DSSP file.
        dssp : string
            The dssp executable (ie. the argument to os.system)
        acc_array : string
            Accessible surface area (ASA) from either Miller et al. (1987),
            Sander & Rost (1994), or Wilke: Tien et al. 2013, as string
            Sander/Wilke/Miller. Defaults to Sander.
        file_type: string
            File type switch, either PDB or DSSP with PDB as default.

        ra   r#   r   r   c             S   s   d |  d |  d f S)z;Serialize a residue's resseq and icode for easy comparison.z%s%sr   r   r   )r`   r   r   r   Ú
resid2code¾  s    z!DSSP.__init__.<locals>.resid2coder   r/   ÚWr   zA1 r   c             S   s   h  |  ] } | j  ƒ  ’ q Sr   )Ú
get_altloc)Ú.0Úar   r   r   ú	<setcomp>ù  s   	 z DSSP.__init__.<locals>.<setcomp>ZSS_DSSPZEXP_DSSP_ASAZPHI_DSSPZPSI_DSSPZ
DSSP_INDEXZNH_O_1_RELIDX_DSSPZNH_O_1_ENERGY_DSSPZO_NH_1_RELIDX_DSSPZO_NH_1_ENERGY_DSSPZNH_O_2_RELIDX_DSSPZNH_O_2_ENERGY_DSSPZO_NH_2_RELIDX_DSSPZO_NH_2_ENERGY_DSSPZNAg      ð?ZEXP_DSSP_RASAÚXr   zStructure/DSSP mismatch at %sN)ra   zDSSP)r/   rc   )r/   rc   )Úresidue_max_accÚupperr   r(   r   r-   ÚKeyErrorÚidZis_disorderedZdisordered_get_id_listZ
child_dictZget_listrd   ÚtupleZdisordered_selectZget_unpacked_listÚ
isdisjointZxtraZget_resnamer   Ú	_dssp_cysÚmatchr   rN   r   Ú__init__)&ÚselfÚmodelr"   r   Z	acc_arrayÚ	file_typeZ	dssp_dictZ	dssp_keysZdssp_mapZ	dssp_listrb   ÚkeyZchain_idr`   ÚchainÚresZres_seq_icoder)   ZrkZaltlocZaltlocsrS   r   r\   r]   r^   rR   rT   rU   rV   rW   rX   rY   rZ   r[   ÚresnameZrel_accZ	dssp_valsr   r   r   rq   Ž  sÂ    		
	
4+	
zDSSP.__init__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rq   r   r   r   r   r#   s  s   r#   )r|   Ú
__future__r   ÚreZ	Bio._py3kr   r   r   ZBio.PDB.AbstractPropertyMapr   ZBio.PDB.PDBExceptionsr   ZBio.PDB.PDBParserr   ZBio.PDB.Polypeptider   Úcompilero   ri   r   r(   r-   r!   r#   r   r   r   r   Ú<module>Y   sœ   			=`