
:>"^m8                 @   s   d  Z  d d l m Z d d l Z y d d l Z Wn. e k
 rb d d l m Z e d   Yn Xd d l m	 Z	 d d l
 m Z d d l
 m Z d d	 l m Z d d
 l m Z Gd d   d e  Z d S)zParser for PDB files.    )print_functionN)MissingPythonDependencyErrorz0Install NumPy if you want to use the PDB parser.)	as_handle)PDBConstructionException)PDBConstructionWarning)StructureBuilder)_parse_pdb_header_listc               @   s   e  Z d  Z d Z d d d d d d  Z d d   Z d	 d
   Z d d   Z d d   Z d d   Z	 d d   Z
 d d   Z d S)	PDBParserz/Parse a PDB file and return a Structure object.TFNc             C   sa   | d k	 r | |  _  n t   |  _  d |  _ d |  _ d |  _ t |  |  _ t |  |  _ d S)a  Create a PDBParser object.

        The PDB parser call a number of standard methods in an aggregated
        StructureBuilder object. Normally this object is instanciated by the
        PDBParser object itself, but if the user provides his/her own
        StructureBuilder object, the latter is used instead.

        Arguments:
         - PERMISSIVE - Evaluated as a Boolean. If false, exceptions in
           constructing the SMCRA data structure are fatal. If true (DEFAULT),
           the exceptions are caught, but some residues or atoms will be missing.
           THESE EXCEPTIONS ARE DUE TO PROBLEMS IN THE PDB FILE!.
         - get_header - unused argument kept for historical compatibilty.
         - structure_builder - an optional user implemented StructureBuilder class.
         - QUIET - Evaluated as a Boolean. If true, warnings issued in constructing
           the SMCRA data will be suppressed. If false (DEFAULT), they will be shown.
           These warnings might be indicative of problems in the PDB file!

        Nr   )structure_builderr   headertrailerline_counterbool
PERMISSIVEQUIET)selfr   
get_headerr
   r    r   6/tmp/pip-build-ww9dw3qa/biopython/Bio/PDB/PDBParser.py__init__$   s    			zPDBParser.__init__c             C   s   t  j    |  j r) t  j d d t d |  _ d |  _ |  j j |  t	 | d d 2 } | j
   } | s~ t d   |  j |  Wd QRX|  j j |  j  |  j j   } Wd QRX| S)zReturn the structure.

        Arguments:
         - id - string, the id that will be used for the structure
         - file - name of the PDB file OR an open filehandle

        ignorecategoryNmoderUzEmpty file.)warningscatch_warningsr   filterwarningsr   r   r   r
   Zinit_structurer   	readlines
ValueError_parseZ
set_headerget_structure)r   idfilehandlelinesZ	structurer   r   r   r    G   s    			zPDBParser.get_structurec             C   s   |  j  S)zReturn the header.)r   )r   r   r   r   r   d   s    zPDBParser.get_headerc             C   s   |  j  S)zReturn the trailer.)r   )r   r   r   r   get_trailerh   s    zPDBParser.get_trailerc             C   s.   |  j  |  \ |  _ } |  j |  |  _ d S)zParse the PDB file (PRIVATE).N)_get_headerr   _parse_coordinatesr   )r   header_coords_trailercoords_trailerr   r   r   r   n   s    zPDBParser._parsec       	      C   s   |  j  } d } xU t d t |   D]> } | j | d  | | } | d d  } | d k r% Pq% W| d |  } | |  _ | | d  } t |  } | | f S)	z:Get the header of the PDB file, return the rest (PRIVATE).r         ATOM  HETATMMODEL N)r,   r-   r.   )r
   rangelenset_line_counterr   r   )	r   r(   r
   ilinerecord_typer   r)   Zheader_dictr   r   r   r&   u   s    	
	zPDBParser._get_headerc       )   ?   C   sc  d } |  j  } d } d } d } d } d } d }	 xt d t |   D]}
 | |
 j d  } | d d  } |  j | d } | j |  | d k s | d k r| s | j |  | d 7} d } | d d	  } | j   } t |  d k r| } n
 | d } | d	 } | d
 d  } | d } y t | d d   } Wn t	 k
 rdd } Yn Xt | d d  j   d  } | d } | d k r| d k s| d k rd } qd } n d } | | | f } yF t
 | d d   } t
 | d d   } t
 | d d   } Wn" t	 k
 rDt d |   Yn Xt j | | | f d  } y t
 | d d   } Wn( t	 k
 r|  j d |  d } Yn X| d k	 r| d k  rt j d t  y t
 | d d   } Wn( t	 k
 r|  j d |  d  } Yn X| d! d"  } | d" d#  j   j   }  | | k r]| } | j |  | | k r| } | j |  | } | }	 y | j | | | |  WqSt k
 r}! z |  j |! |  WYd d }! ~! XqSXnv | | k s|	 | k rS| } | }	 y | j | | | |  Wn5 t k
 rR}! z |  j |! |  WYd d }! ~! Xn Xy& | j | | | | | | | |   WqAt k
 r}! z |  j |! |  WYd d }! ~! XqAXn| d$ k rPd% d&   | d' d(  | d( d)  | d* d+  | d+ d,  | d, d-  | d- d.  f D }" t j |" d  d/ j d  }# | j |#  n| d0 k ry t | d1 d2   }$ Wn( t	 k
 r|  j d3 |  d }$ Yn X| j | |$  | d 7} d } d } d } nq| d4 k s| d5 k r|  j | 7_ | | d  S| d6 k r&d } d } d } n| d7 k rd8 d&   | d' d(  | d( d)  | d) d+  | d+ d,  | d, d-  | d- d.  f D }% t j |% d  d/ j d  }& | j |&  n | d9 k rAd: d&   | d d  | d d;  | d d  | d d  | d d  f D }' t j |' d  }( | j |(  | d 7} qI W|  j | |  _ g  S)<z0Parse the atomic data in the PDB file (PRIVATE).r   N
r+   r*   zATOM  r-                           ZHOHZWATWH    &   .   6   z,Invalid or missing coordinate(s) at line %i.f<   zInvalid or missing occupancyz'Negative occupancy in one or more atomsB   zInvalid or missing B factorg        H   L   N   ZANISOUc             S   s   g  |  ] } t  |   q Sr   )float).0xr   r   r   
<listcomp>  s   	z0PDBParser._parse_coordinates.<locals>.<listcomp>   #   *   +   1   8   ?   F   g     @zMODEL 
      z&Invalid or missing model serial numberzEND   ZCONECTZENDMDLZSIGUIJc             S   s   g  |  ] } t  |   q Sr   )rK   )rL   rM   r   r   r   rN   *  s   	ZSIGATMc             S   s   g  |  ] } t  |   q Sr   )rK   )rL   rM   r   r   r   rN   :  s   	-   )r
   r/   r0   rstripr   r1   Z
init_modelsplitint	ExceptionrK   r   numpyarray_handle_PDB_exceptionr   warnr   stripupperZinit_segZ
init_chainZinit_residueZ	init_atomZastypeZ
set_anisouZ
set_siguijZ
set_sigatm))r   r)   Zlocal_line_counterr
   Zcurrent_model_idZ
model_openZcurrent_chain_idZcurrent_segidZcurrent_residue_idZcurrent_resnamer2   r3   r4   Zglobal_line_counterfullnameZ
split_listnameZaltlocresnameZchainidZserial_numberZresseqZicodeZhetero_flagZ
residue_idrM   yzZcoordZ	occupancyZbfactorZsegidelementmessageZanisouZanisou_arrayZ
serial_numZsiguijZsiguij_arrayZsigatmZsigatm_arrayr   r   r   r'      s*   	
	


 
		

&#&	

				zPDBParser._parse_coordinatesc             C   s@   d | | f } |  j  r0 t j d | t  n t |   d S)zHandle exception (PRIVATE).

        This method catches an exception that occurs in the StructureBuilder
        object (if PERMISSIVE), or raises it again, this time adding the
        PDB line number to the error message.
        z%s at line %i.zlPDBConstructionException: %s
Exception ignored.
Some atoms or residues may be missing in the data structure.N)r   r   ra   r   r   )r   rj   r   r   r   r   r`   J  s    	
zPDBParser._handle_PDB_exception)__name__
__module____qualname____doc__r   r    r   r%   r   r&   r'   r`   r   r   r   r   r	   !   s   "r	   )rn   
__future__r   r   r^   ImportErrorZBior   ZBio.Filer   ZBio.PDB.PDBExceptionsr   r   ZBio.PDB.StructureBuilderr   ZBio.PDB.parse_pdb_headerr   objectr	   r   r   r   r   <module>   s   