B
    ‰°b*D  ã               @   s’   d Z ddlZyddlZW n* ek
rB   ddlmZ edƒd‚Y nX ddlmZ ddlm	Z	 ddlm
Z
 ddlmZ dd	lmZ G d
d„ dƒZdS )zParser for PDB files.é    N)ÚMissingPythonDependencyErrorz0Install NumPy if you want to use the PDB parser.)Ú	as_handle)ÚPDBConstructionException)ÚPDBConstructionWarning)ÚStructureBuilder)Ú_parse_pdb_header_listc               @   sR   e Zd ZdZ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   sL   |dk	r|| _ ntƒ | _ d| _d| _d| _t|ƒ| _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!
         - is_pqr - Evaluated as a Boolean. Specifies the type of file to be parsed.
           If false (DEFAULT) a .pdb file format is assumed. Set it to true if you
           want to parse a .pqr file instead.

        Nr   )	Ústructure_builderr   ÚheaderÚtrailerÚline_counterÚboolÚ
PERMISSIVEÚQUIETÚis_pqr)Úselfr   Ú
get_headerr	   r   r   © r   ú0lib/python3.7/site-packages/Bio/PDB/PDBParser.pyÚ__init__#   s    

zPDBParser.__init__c          
   C   sŽ   t  ¡ | | jrt jdtd d| _d| _| j |¡ t	|ƒ$}| 
¡ }|sTtdƒ‚|  |¡ W dQ R X | j | j¡ | j ¡ }W dQ R X |S )z³Return 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)ÚcategoryNzEmpty file.)ÚwarningsÚcatch_warningsr   Úfilterwarningsr   r
   r   r	   Zinit_structurer   Ú	readlinesÚ
ValueErrorÚ_parseZ
set_headerÚget_structure)r   ÚidÚfileZhandleÚlinesZ	structurer   r   r   r   O   s    

zPDBParser.get_structurec             C   s   | j S )zReturn the header.)r
   )r   r   r   r   r   l   s    zPDBParser.get_headerc             C   s   | j S )zReturn the trailer.)r   )r   r   r   r   Úget_trailerp   s    zPDBParser.get_trailerc             C   s    |   |¡\| _}|  |¡| _dS )zParse the PDB file (PRIVATE).N)Ú_get_headerr
   Ú_parse_coordinatesr   )r   Úheader_coords_trailerÚcoords_trailerr   r   r   r   v   s    zPDBParser._parsec       	      C   s|   | j }d}xBtdt|ƒƒD ]0}| |d ¡ || }|dd… }|dkrP qW |d|… }|| _||d… }t|ƒ}||fS )z:Get the header of the PDB file, return the rest (PRIVATE).r   é   é   )zATOM  ÚHETATMzMODEL N)r	   ÚrangeÚlenÚset_line_counterr   r   )	r   r%   r	   ÚiÚlineÚrecord_typer
   r&   Zheader_dictr   r   r   r#   }   s    zPDBParser._get_headerc       ,      C   s>  ddddddddd	h	}d
}| j }d
}d
}d}d}d}	d}
xòtd
t|ƒƒD ]Þ}||  d¡}|d
d… }| j| d }| |¡ | ¡ sqJn’|dks¢|dkr|s¼| |¡ |d7 }d}|dd… }| ¡ }t|ƒdkrâ|}n|d
 }|d }|dd…  ¡ }|d }yt	|dd… ƒ}W n t
k
r8   d
}Y nX t	|dd…  ¡ d
 ƒ}|d }|dkr„|dksx|dkr~d}nd}nd}|||f}y4t|dd… ƒ}t|dd… ƒ}t|dd… ƒ}W n$ t
k
rê   td | ƒd‚Y nX t |||fd!¡}| js yt|dd"… ƒ}W n& t
k
r@   |  d#|¡ d}Y nX |dk	rb|d
k rbt d$t¡ yt|d"d%… ƒ}W n& t
k
rœ   |  d&|¡ d'}Y nX n¨| jrHyt|dd(… ƒ} W n& t
k
râ   |  d)|¡ d} Y nX yt|d(d*… ƒ}!W n& t
k
r   |  d+|¡ d}!Y nX |!dk	rH|!d
k rHd,}"|  |"|¡ d}!|d-d.… }#|d.d/…  ¡  ¡ }$||#kr€|#}| |¡ ||krè|}| |¡ |}	|}
y| ||||¡ W n0 tk
rä }" z|  |"|¡ W dd}"~"X Y nX nb|	|ksü|
|krJ|}	|}
y| ||||¡ W n0 tk
rH }" z|  |"|¡ W dd}"~"X Y nX | js¢y| ||||||||$¡ W n0 tk
rž }" z|  |"|¡ W dd}"~"X Y nX n^| jr"y$| ||| |!||||$| |!| j¡ W n0 tk
rþ }" z|  |"|¡ W dd}"~"X Y nX n|dkr|d0d1„ |d2d3… |d3d4… |d5d6… |d6d7… |d7d8… |d8d*… fD ƒ}%t |%d!¡d9  d!¡}&| |&¡ n¦|dkræyt	|d:d;… ƒ}'W n& t
k
rÀ   |  d<|¡ d
}'Y nX | ||'¡ |d7 }d}d}d}	n<|d=ksú|d>kr|  j|7  _||d… S |dkr,d
}d}d}	nö|dkr¢d?d1„ |d2d3… |d3d4… |d4d6… |d6d7… |d7d8… |d8d*… fD ƒ}(t |(d!¡d9  d!¡})| |)¡ n€|dkrd@d1„ |dd… |dd… |dd… |dd"… |d"d%… fD ƒ}*t |*d!¡}+| |+¡ n||kr"t dA ||¡t¡ |d7 }qJW | j| | _g S )Bz0Parse the atomic data in the PDB file (PRIVATE).zATOM  r)   zMODEL ZENDMDLzTER   ZANISOUZSIGATMZSIGUIJZMASTERr   NÚ
r(   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 atomséB   zInvalid or missing B factorg        é>   zInvalid or missing chargeéF   zInvalid or missing radiuszNegative atom radiuséH   éL   éN   c             S   s   g | ]}t |ƒ‘qS r   )Úfloat)Ú.0Úxr   r   r   ú
<listcomp>J  s   z0PDBParser._parse_coordinates.<locals>.<listcomp>é   é#   é*   é+   é1   é8   é?   g     ˆÃ@é
   é   z&Invalid or missing model serial numberzEND   ZCONECTc             S   s   g | ]}t |ƒ‘qS r   )rH   )rI   rJ   r   r   r   rK   o  s   c             S   s   g | ]}t |ƒ‘qS r   )rH   )rI   rJ   r   r   r   rK     s   z,Ignoring unrecognized record '{}' at line {})r	   r*   r+   Úrstripr   r,   ÚstripZ
init_modelÚsplitÚintÚ	ExceptionrH   r   ÚnumpyZarrayr   Ú_handle_PDB_exceptionr   Úwarnr   ÚupperZinit_segZ
init_chainZinit_residueZ	init_atomZastypeZ
set_anisouZ
set_siguijZ
set_sigatmÚformat),r   r&   Zallowed_recordsZlocal_line_counterr	   Zcurrent_model_idZ
model_openZcurrent_chain_idZcurrent_segidZcurrent_residue_idZcurrent_resnamer-   r.   r/   Zglobal_line_counterÚfullnameZ
split_listÚnameZaltlocZresnameZchainidZserial_numberZresseqZicodeZhetero_flagZ
residue_idrJ   ÚyÚzZcoordZ	occupancyZbfactorZ
pqr_chargeZradiusÚmessageZsegidÚelementZanisouZanisou_arrayZ
serial_numZsiguijZsiguij_arrayZsigatmZsigatm_arrayr   r   r   r$   Ž   s’   












 
 "




















zPDBParser._parse_coordinatesc             C   s2   d||f }| j r$t d| t¡ n
t|ƒd‚dS )zéHandle 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   r\   r   r   )r   rc   r   r   r   r   r[   –  s    zPDBParser._handle_PDB_exception)TFNFF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r"   r   r#   r$   r[   r   r   r   r   r       s       
&  
r   )rh   r   rZ   ÚImportErrorZBior   ZBio.Filer   ZBio.PDB.PDBExceptionsr   r   ZBio.PDB.StructureBuilderr   ZBio.PDB.parse_pdb_headerr   r   r   r   r   r   Ú<module>   s   
