o
    Rŀg                  
   @   s  d Z 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	 ddl
ZddlmZ dd	lmZ d
edeeef deedf fddZd
edeeef deeeeef f fddZde	deeef fddZde	deeef deeeef fddZG dd dZdS )zThis module contains a parser for PDBML (PDB XML) files.

PDBML is a representation of PDB data in XML format.

See https://pdbml.wwpdb.org/.
    )PathLike)TextIO)Union)ElementTree)ElementN)	Structure)StructureBuildertree
namespacesreturnc                 C   s4   dD ]}|  ||}|r|j}|rt|  S qd S )N)z2PDBx:refineCategory/PDBx:refine/PDBx:ls_d_res_highz9PDBx:refine_histCategory/PDBx:refine_hist/PDBx:d_res_highzKPDBx:em_3d_reconstructionCategory/PDBx:em_3d_reconstruction/PDBx:resolution)findtextfloat)r	   r
   	candidateelementr    r   G/var/www/html/myenv/lib/python3.10/site-packages/Bio/PDB/PDBMLParser.py_parse_resolution_from   s   r   c                 C   sR   |  d|j|  d|j|  d|jd |  d|j|  d|jd t| |dS )	Nz*PDBx:structCategory/PDBx:struct/PDBx:titlez;PDBx:struct_keywordsCategory/PDBx:struct_keywords/PDBx:textzPDBx:entryCategory/PDBx:entryidz^PDBx:pdbx_database_statusCategory/PDBx:pdbx_database_status/PDBx:recvd_initial_deposition_datezPDBx:exptlCategory/PDBx:exptlmethod)nameheadidcodedeposition_datestructure_method
resolution)r   r   attribr   )r	   r
   r   r   r   _parse_header_from(   s4   r   r   c                 C   s   |  d|j}t|  d|j}t|  d|j}t|  d|j}|t|||ftt|  d|jt|  d|j|  d|jpCd|t| jd	 |  d
|jdS )NzPDBx:label_atom_idzPDBx:Cartn_xzPDBx:Cartn_yzPDBx:Cartn_zzPDBx:B_iso_or_equivzPDBx:occupancyzPDBx:label_alt_id r   zPDBx:type_symbol)r   coordb_factor	occupancyaltlocfullnameserial_numberr   )r   r   r   nparrayintr   )r   r
   r   xyzr   r   r   _parse_atom_fromA   s   r+   c                 C   s   | j d|d  dksJ | d|j}| d|j}| d|}|dkr3|dks-|d	kr0d
}nd}nd}t| d|j}|d urE|jnd}|||fS )N{PDBxz
}atom_sitezPDBx:group_PDBPDBx:label_comp_idzPDBx:pdbx_PDB_ins_codeHETATMHOHWATWHr   zPDBx:auth_seq_id)tagr   r   r'   )r   r
   
atom_groupcomponent_idins_code_elementhetero_fieldsequence_idinsertion_coder   r   r   _parse_residue_id_fromS   s   
r;   c                   @   s8   e Zd ZdZdd Zdeeeee	e
f defddZdS )	PDBMLParsera,  A parser for PDBML (PDB XML) files. See https://pdbml.wwpdb.org/.

    This parser is based on the mmCIF parser also provided in the PDB package in the sense that the structure object
    returned by this parser is equal to the structure returned by the mmCIF parser for any given PDB structure.
    c                 C   s   t  | _dS )zInitialize a PDBML parser.N)r   structure_builder)selfr   r   r   __init__o   s   zPDBMLParser.__init__sourcer   c                 C   s`  t dd t|dgD }t|dr|d t|}t||}| jj|d d | j	| |
d|}d}d	}d	}d	}	d	}
|D ]g}t|
d
|j}|
d|j}|
d|j}t||}|rg||krx| j|| |d7 }|}d	}d	}	|r~||kr| j| |}d	}	||	ks||
kr| jj|g|R   |}	|}
| jjdi t|| qC| j S )zParse and return the PDB structure from XML source.

        :param Union[int, str, bytes, PathLike, TextIO] source: The XML representation of the PDB structure
        :return: the PDB structure
        :rtype: Bio.PDB.Structure.Structure
        c                 S   s   g | ]\}}|qS r   r   ).0_noder   r   r   
<listcomp>}   s    z-PDBMLParser.get_structure.<locals>.<listcomp>zstart-nsseekr   r   )structure_idzPDBx:atom_siteCategoryNzPDBx:pdbx_PDB_model_numzPDBx:auth_asym_idr.      r   )dictr   	iterparsehasattrrE   parser   r=   init_structure
set_headerr   r'   r   r;   
init_model
init_chaininit_residue	init_atomr+   get_structure)r>   r@   r
   r	   headeratom_elementsbuilder_model_countbuilder_model_numberbuilder_chain_idbuilder_residue_idbuilder_component_idr   model_numberchain_idr6   
residue_idr   r   r   rR   s   sH   	





zPDBMLParser.get_structureN)__name__
__module____qualname____doc__r?   r   r'   strbytesr   r   r   rR   r   r   r   r   r<   h   s    r<   )r`   osr   typingr   r   	xml.etreer   xml.etree.ElementTreer   numpyr%   Bio.PDB.Structurer   Bio.PDB.StructureBuilderr   rH   ra   r   r   r   r+   tupler'   r;   r<   r   r   r   r   <module>   s@   






