
     iT                         d dl mZ d dlZd dlmZ d Z G d de          Zd Z G d d	e          Z	 G d
 de	          Z
dS )    )encode_arrayN)	constantsc                 2    i }||d<   ||d<   | |d<   ||d<   |S )NdescriptiontypechainIndexListsequence )chain_indicesr	   r   entity_typeout_ds        ^/srv/www/vhosts/g4struct/public_html/venv/lib/python3.11/site-packages/mmtf/api/mmtf_writer.pymake_entity_dictr      s4    E&E-E&M+E
 E*L    c                        e Zd Zd Zd Zd ZdS )Groupc                 &   | j         |j         k    rdS | j        |j        k    rdS | j        |j        k    rdS | j        |j        k    rdS | j        |j        k    rdS | j        |j        k    rdS | j        |j        k    rdS | j        |j        k    rdS dS )zFunction to define equalityFT)atom_name_listcharge_listelement_list
group_type
group_namesingle_letter_codebond_atom_listbond_order_list)selfothers     r   __eq__zGroup.__eq__   s    %"6665u0005 2225?e...5?e...5"e&>>>5%"66655#8885tr   c                     g | _         g | _        g | _        g | _        g | _        t
          j        | _        t
          j        | _	        t
          j
        | _        d S )N)r   r   r   r   r   r   UNKOWN_GROUP_NAMEr   UNKOWN_GROUP_TYPEr   
UNKNOWN_SLr   r   s    r   __init__zGroup.__init__#   sO     ! #5#5"+"6r   c                     i }| j         |d<   | j        |d<   | j        |d<   | j        |d<   | j        |d<   | j        |d<   | j        |d<   | j        |d<   |S )	z/Convert the group object to an appropriate DICT	groupNameatomNameListelementListbondOrderListbondAtomListformalChargeListsingleLetterCodechemCompType)r   r   r   r   r   r   r   r   )r   out_dicts     r   convert_to_dictzGroup.convert_to_dict-   st     $#'#6 "&"3$($8!#'#6 '+'7#$'+'>#$#'? r   N)__name__
__module____qualname__r   r$   r/   r
   r   r   r   r      sA          (7 7 7    r   r   c                 F    g }| D ]}||vr|                     |           |S )z(Function to get a unique list of groups.)append)
input_listout_listitems      r   get_unique_groupsr8   ;   s9    H " "xOOD!!!Or   c                   Z    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd ZdS )TemplateEncoderzSTemplate class to be used by third parties to pass data into other data structures.c                     t           )  Initialise the structure object.
        :param total_num_bonds: the number of bonds in the structure
        :param total_num_atoms: the number of atoms in the structure
        :param total_num_groups: the number of groups in the structure
        :param total_num_chains: the number of chains in the structure
        :param total_num_models: the number of models in the structure
        :param structure_id the: id of the structure (e.g. PDB id)
        NotImplementedErrorr   total_num_bondstotal_num_atomstotal_num_groupstotal_num_chainstotal_num_modelsstructure_ids          r   init_structurezTemplateEncoder.init_structureG   
     "!r   c                     t           )  Create an atom object an set the information.
        :param atom_name: the atom name, e.g. CA for this atom
        :param serial_number: the serial id of the atom (e.g. 1)
        :param alternative_location_id: the alternative location id for the atom, if present
        :param x: the x coordiante of the atom
        :param y: the y coordinate of the atom
        :param z: the z coordinate of the atom
        :param occupancy: the occupancy of the atom
        :param temperature_factor: the temperature factor of the atom
        :param element: the element of the atom, e.g. C for carbon. According to IUPAC. Calcium  is Ca
        :param charge: the formal atomic charge of the atom
        r=   r   	atom_nameserial_numberalternative_location_idxyz	occupancytemperature_factorelementcharges              r   set_atom_infozTemplateEncoder.set_atom_infoT   s
     "!r   c                     t           )Set the chain information.
        :param chain_id: the asym chain id from mmCIF
        :param chain_name: the auth chain id from mmCIF
        :param num_groups: the number of groups this chain has
        r=   r   chain_id
chain_name
num_groupss       r   set_chain_infozTemplateEncoder.set_chain_infoe   
     "!r   c                     t           )N  Set the entity level information for the structure.
        :param chain_indices: the indices of the chains for this entity
        :param sequence: the one letter code sequence for this entity
        :param description: the description for this entity
        :param entity_type: the entity type (polymer,non-polymer,water)
        r=   r   r   r	   r   r   s        r   set_entity_infozTemplateEncoder.set_entity_infoo   s
     "!r   c
                     t           )E  Set the information for a group
        :param group_name: the name of this group,e.g. LYS
        :param group_number: the residue number of this group
        :param insertion_code: the insertion code for this group
        :param group_type: a string indicating the type of group (as found in the chemcomp dictionary.
        Empty string if none available.
        :param atom_count: the number of atoms in the group
        :param bond_count: the number of unique bonds in the group
        :param single_letter_code: the single letter code of the group
        :param sequence_index: the index of this group in the sequence defined by the enttiy
        :param secondary_structure_type: the type of secondary structure used (types are according to DSSP and
        number to type mappings are defined in the specification)
        r=   
r   r   group_numberinsertion_coder   
atom_count
bond_countr   sequence_indexsecondary_structure_types
             r   set_group_infozTemplateEncoder.set_group_infoy   s
      "!r   c                     t           )Set the information for a model.
        :param model_id: the index for the model
        :param chain_count: the number of chains in the model
        r=   r   model_idchain_counts      r   set_model_infozTemplateEncoder.set_model_info   r]   r   c                     t           )Set the crystallographic information for the structure
        :param space_group: the space group name, e.g. "P 21 21 21"
        :param unit_cell: an array of length 6 with the unit cell parameters in order: a, b, c, alpha, beta, gamma
        r=   r   space_group	unit_cells      r   set_xtal_infozTemplateEncoder.set_xtal_info   
    
 "!r   c                     t           )  Sets the header information.
        :param r_free: the measured R-Free for the structure
        :param r_work: the measure R-Work for the structure
        :param resolution: the resolution of the structure
        :param title: the title of the structure
        :param deposition_date: the deposition date of the structure
        :param release_date: the release date of the structure
        :param experimnetal_methods: the list of experimental methods in the structure
        r=   r   r_freer_work
resolutiontitledeposition_daterelease_dateexperimental_methodss           r   set_header_infozTemplateEncoder.set_header_info   rG   r   c                     t           )v  Set the Bioassembly transformation information. A single bioassembly can have multiple transforms,
        :param bio_assembly_index: the integer index of the bioassembly
        :param input_chain_indices: the list of integer indices for the chains of this bioassembly
        :param input_transformation: the list of doubles for  the transform of this bioassmbly transformr=   )r   bio_assembly_indexinput_chain_indicesinput_transforms       r   set_bio_assembly_transz&TemplateEncoder.set_bio_assembly_trans   rx   r   c                     t           ).Any functions needed to cleanup the structure.r=   r#   s    r   finalize_structurez"TemplateEncoder.finalize_structure   s    !!r   c                     t           )!  Add bonds within a group.
        :param atom_index_one: the integer atom index (in the group) of the first partner in the bond
        :param atom_index_two: the integer atom index (in the group) of the second partner in the bond
        :param bond_order: the integer bond order
        r=   r   atom_index_oneatom_index_two
bond_orders       r   set_group_bondzTemplateEncoder.set_group_bond   r]   r   c                     t           )   Add bonds between groups.
        :param atom_index_one: the integer atom index (in the structure) of the first partner in the bond
        :param atom_index_two: the integer atom index (in the structure) of the second partner in the bond
        :param bond_order the bond order
        r=   r   s       r   set_inter_group_bondz$TemplateEncoder.set_inter_group_bond   r]   r   N)r0   r1   r2   __doc__rF   rU   r\   ra   rk   rq   rw   r   r   r   r   r   r
   r   r   r:   r:   D   s        ]]" " "" " """ " "" " "" " "(" " "" " "" " "" " "" " "" " "" " " " "r   r:   c                   h    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd ZdS )MMTFEncoderc                    i }t          | j        dd          |d<   t          | j        dd          |d<   t          | j        dd          |d<   t          | j        dd          |d<   t          | j        dd	          |d
<   t          | j        dd	          |d<   t          | j        dd          |d<   t          | j        dd          |d<   t          | j	        dd          |d<   t          | j
        dd          |d<   | j        |d<   t          | j        dd          |d<   t          | j        dd          |d<   t          | j        dd          |d<   t          | j        dd          |d<   t          | j        dd          |d<   t          | j        dd          |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        |d(<   | j        |d)<   | j         |d*<   | j!        |d+<   | j"        |d,<   | j#        |d-<   | j$        |d.<   | j%        |d/<   | j&        |d0<   |S )1z!Encode the data back into a dict.   r   groupTypeList
   i  
xCoordList
yCoordList
zCoordListd   bFactorList	   occupancyList   
atomIdList   
altLocListinsCodeListgroupIdList	groupListsequenceIndexList   chainNameListchainIdListr*      r)   secStructListchainsPerModelgroupsPerChain
spaceGroupmmtfVersionmmtfProducerstructureId
entityListbioAssemblyListrFreerWorkr~   r   experimentalMethodsdepositionDatereleaseDateunitCellnumBonds	numChains	numModelsnumAtoms	numGroups)'r   group_type_listx_coord_listy_coord_listz_coord_listb_factor_listoccupancy_listatom_id_listalt_loc_listins_code_listgroup_id_list
group_listsequence_index_listchain_name_listchain_id_listr   r   sec_struct_listchains_per_modelgroups_per_chainru   mmtf_versionmmtf_producerrE   entity_listbio_assemblyr|   r}   r~   r   r   r   r   rv   	num_bonds
num_chains
num_models	num_atomsr[   )r   output_datas     r   encode_datazMMTFEncoder.encode_data   s   '3D4H!Q'O'OO$$01BB$M$ML!$01BB$M$ML!$01BB$M$ML!%1$2Db#%N%NM"'3D4GC'P'PO$$01BAq$I$IL!$01BAq$I$IL!%1$2Da%K%KM"%1$2Da%K%KM"#'?K +78PRSUV+W+W'('3D4H!Q'O'OO$%1$2Da%K%KM"&243F1&M&MN#'3D4H!Q'O'OO$'3D4H!Q'O'OO$(,(=$%(,(=$%$($4L!%)%6M"&*&8N#%)%6M"$($4L!)-):%&#{G#{G$(OL!#zG-1-F)*(,(<$%%)%6M""&.J"&.J#'?K #'?K "&.J#'?K r   c                 R    t          j        |                                 d          S )z$Get the msgpack of the encoded data.T)use_bin_type)msgpackpackbr   r#   s    r   get_msgpackzMMTFEncoder.get_msgpack   s#    }T--//dCCCCr   c                     t          |d          5 }|                    |                                            d d d            d S # 1 swxY w Y   d S )Nwb)openwriter   )r   	file_pathout_fs      r   
write_filezMMTFEncoder.write_file  s    )T"" 	,eKK((**+++	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,s   (AA
A
c                    t           j        | _        t           j        | _        || _        || _        || _        || _        || _	        || _
        g | _        g | _        g | _        g | _        g | _        g | _        g | _        g | _        g | _        g | _        g | _        g | _        g | _        g | _        g | _        g | _        g | _        g | _        g | _        g | _        d| _        g | _         dS )r<   N)!r   MMTF_VERSIONr   PRODUCERr   r   r   r[   r   r   rE   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   current_groupr   r?   s          r   rF   zMMTFEncoder.init_structure  s     &2&/((***(! #% ! !! " "!r   c                 ,   | j                             |           | j                            |           | j                            |           | j                            |           | j                            |           | j                            |           | j                            |           | j        j	                            |           | j        j
                            |
           | j        j                            |	           dS )rI   N)r   r4   r   r   r   r   r   r   r   r   r   r   rJ   s              r   rU   zMMTFEncoder.set_atom_info3  s     	  ###  ###  ###  ///  !8999""9---!!"4555)00;;;&--f555'..w77777r   c                     | j                             |           | j                            |           | j                            |           dS )rW   N)r   r4   r   r   rX   s       r   r\   zMMTFEncoder.set_chain_infoN  sO     	!!(+++##J///$$Z00000r   c                 Z    | j                             t          ||||                     dS )r_   N)r   r4   r   r`   s        r   ra   zMMTFEncoder.set_entity_infoY  s1     	 0xT_ ` `aaaaar   c
                    | j         | j                            | j                    | j                            |           | j                            |           | j                            |           | j                            |	           t                      | _         || j         _        || j         _	        || j         _
        dS )rc   N)r   r   r4   r   r   r   r   r   r   r   r   rd   s
             r   rk   zMMTFEncoder.set_group_infoc  s    " )O""4#5666 	!!,///!!.111 ''777##$<==="WW(2%(2%0B---r   c                 :    | j                             |           dS )rm   N)r   r4   rn   s      r   rq   zMMTFEncoder.set_model_info  s!     	$$[11111r   c                 "    || _         || _        dS )rs   N)ru   rv   rt   s      r   rw   zMMTFEncoder.set_xtal_info  s    
 '"r   c                 h    || _         || _        || _        || _        || _        || _        || _        dS )rz   N)r|   r}   r~   r   r   r   r   r{   s           r   r   zMMTFEncoder.set_header_info  s>     $
.($8!!!r   c                 &   d}| j         D ]}|d         t          |          k    r|} n |st          |          g d}n| j                             |           |d                             ||d           | j                             |           dS )r   Nname)r   transformListr   )r   matrix)r   strremover4   )r   r   r   r   this_bioassbioasss         r   r   z"MMTFEncoder.set_bio_assembly_trans  s    
 ' 	 	Ff~%7!8!888$ 9  	2#&'9#:#:RPPKK$$[111O$++>Q\k,l,lmmm  -----r   c                     | j                             | j                   t          | j                   }| j         D ]/}| j                            |                    |                     0d |D             | _         dS )r   c                 6    g | ]}|                                 S r
   )r/   ).0rN   s     r   
<listcomp>z2MMTFEncoder.finalize_structure.<locals>.<listcomp>  s$    BBB11,,..BBBr   N)r   r4   r   r8   r   index)r   	group_setr7   s      r   r   zMMTFEncoder.finalize_structure  sz    t1222%do66	O 	? 	?D ''	(=(=>>>>BB	BBBr   c                     | j         j                            |           | j         j                            |           | j         j                            |           dS )r   N)r   r   r4   r   r   s       r   r   zMMTFEncoder.set_group_bond  sX     	)00@@@)00@@@*11*=====r   c                     | j                             |           | j                             |           | j                            |           dS )r   N)r   r4   r   r   s       r   r   z MMTFEncoder.set_inter_group_bond  sO     	"">222"">222##J/////r   N)r0   r1   r2   r   r   r   rF   rU   r\   ra   rk   rq   rw   r   r   r   r   r   r
   r   r   r   r      s       ) ) )XD D D
, , ,
) ) )X8 8 861 1 1b b bC C C<2 2 2# # #9 9 9(. . .$C C C> > >0 0 0 0 0r   r   )mmtf.codecsr   r   
mmtf.utilsr   r   objectr   r8   r:   r   r
   r   r   <module>r
     s    $ $ $ $ $ $               + + + + +F + + +\  G" G" G" G" G"f G" G" G"VE0 E0 E0 E0 E0/ E0 E0 E0 E0 E0r   