
     iC                     V   d dl Z d dlZd dlZd dlmZmZmZmZ d dlm	Z	m
Z
mZ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mZ d	 Z G d
 de j                  Z G d de j                  Z G d de j                  Z G d de j                  Zedk    r e j                     dS dS )    N)fetchparse
parse_gzip
converters)ungzip_data
write_mmtfMMTFDecoder_internet_on)encoders)parse_header)BASE_URL)
codec_dict)numpy_decodersc           	      8   	 |                      t          |                             ||                                          |                                           nN#  |                      t          |                             ||          |                                           Y nxY w	 |                      t          |                             t          |                             ||          |                                          |                                           nm#  |                      t          |                             t          |                             ||          |          |                                           Y nxY w|                      t          |                             ||          |           |                      t          |                             t          |                             ||          |          |           dS )zRTest that a given codec can work in the forward backward and round trip both ways.N)assertEqualr   decodetolistencode)	unit_testencoded_datadecoded_dataparamcodec_ids        `/srv/www/vhosts/g4struct/public_html/venv/lib/python3.11/site-packages/mmtf/tests/codec_tests.pyrun_allr      s   gj299,NNUUWWYeYlYlYnYnoooogj299,NNP\PcPcPePefffffNx ''
8(<(C(CLRW(X(X/46 66<fhh@S@S@U@U	W 	W 	W 	WNj299*X:N:U:UVbdi:j:j/46 67C7J7J7L7L	N 	N 	N 	N 	N*X.55lEJJLYYY*X.55j6J6Q6QR^`e6f6f=BD DEQS S S S Ss   AA A	B'+A8D$ $A(Fc                   8    e Zd Zd Zd Zd Zd Zd Zd Zd Z	dS )		CodecTestc                 \    d}t          j        g d          }t          | ||dd           d S )Ns   D)g?5^I,I@g(\_I@g rXI@i  
   numpyarrayr   self	test_dataoutput_datas      r   test_delt_rec_floatzCodecTest.test_delt_rec_float!   s9    4	k":":":;;idB77777    c                 \    d}t          j        g d          }t          | ||dd           d S )Ns      d   )      ?r*   r*   d   	   r    r#   s      r   test_run_len_floatzCodecTest.test_run_len_float&   s9    4	k"2"2"233ic155555r(   c                 \    d}t          j        g d          }t          | ||dd           d S )Ns         )                     r      r    r#   s      r   test_run_len_delta_intz CodecTest.test_run_len_delta_int+   s6    7	k///22ia33333r(   c                 \    d}t          j        g d          }t          | ||dd           d S )Ns      A   )Ar9   r9   r9   r   r4   r    r#   s      r   test_run_len_charzCodecTest.test_run_len_char0   s9    7	k"3"3"344ia33333r(   c                 \    d}t          j        g d          }t          | ||dd           d S )Ns   B   A   C   A   A   A   )Br9   Cr9   r9   r9   r   r3   r    r#   s      r   test_enc_strzCodecTest.test_enc_str5   s9    e	{#<#<#<==ia33333r(   c                 \    d}t          j        g d          }t          | ||dd           d S )N   r5   r4   r4   r5   r5   r   r0   r    r#   s      r   test_byte_to_intzCodecTest.test_byte_to_int:   s6    ,	k+++..ia33333r(   c                 \    d}t          j        g d          }t          | ||dd           d S )N               r/     r   r0   r   r2   r    r#   s      r   test_four_byte_intzCodecTest.test_four_byte_int?   s9    W	k"3"3"344ia33333r(   N)
__name__
__module____qualname__r'   r-   r7   r:   r>   rB   rG    r(   r   r   r       s}        8 8 8
6 6 6
4 4 4
4 4 4
4 4 4
4 4 4
4 4 4 4 4r(   r   c                   &    e Zd Zd Zd Zd Zd ZdS )DecoderTestsc                     t          j        g d          }g d}t          j        |                                          }|                     ||           d S N)   r1   r+   r0   o   r2   '  r4   )rP   rP   rP   r+   r+   rQ   rQ   rQ   rQ   rR   rR   rR   rR   rR   rR   r!   r"   decodersrun_length_decoder   r   r$   
input_dataoutput_data_testr&   s       r   test_run_length_decodez#DecoderTests.test_run_length_decodeE   s]    [!;!;!;<<
aaa0<<CCEE&677777r(   c                     t          j        g           }g }t          j        |                                          }|                     ||           d S NrS   rV   s       r   test_empty_run_length_decodez)DecoderTests.test_empty_run_length_decodeK   sN    [__
0<<CCEE&677777r(   c                     t          j        g dt           j                  }g d}t          j        |                                          }|                     ||           d S )NrP   r1   r+      r2   dtyperP      v   u         r!   asarrayint32rT   delta_decoder   r   rV   s       r   test_delta_decodezDecoderTests.test_delta_decodeR   sd    ]#5#5#5EKHHH
222+J77>>@@&677777r(   c                     t          j        g t           j                  }g }t          j        |                                          }|                     ||           d S )Nra   ri   rV   s       r   test_empty_delta_decodez$DecoderTests.test_empty_delta_decodeX   sW    ]2EK888
+J77>>@@&677777r(   N)rH   rI   rJ   rY   r\   rm   ro   rK   r(   r   rM   rM   D   sP        8 8 88 8 88 8 88 8 8 8 8r(   rM   c                   &    e Zd Zd Zd Zd Zd ZdS )EncoderTestsc                 j    g d}g d}t          j        |          }|                     ||           d S rO   r   run_length_encoder   r$   rX   rW   r&   s       r   test_run_length_encodez#EncoderTests.test_run_length_encode_   sF    <<<iii
0<<&677777r(   c                 b    g }g }t          j        |          }|                     ||           d S r[   rs   rV   s       r   test_empty_run_length_encodez)EncoderTests.test_empty_run_length_encodee   s:    
0<<&677777r(   c                 j    g d}g d}t          j        |          }|                     ||           d S )Nr^   rc   r   delta_encoder   ru   s       r   test_delta_encodezEncoderTests.test_delta_encodek   sF    222111
+J77&677777r(   c                 b    g }g }t          j        |          }|                     ||           d S r[   rz   rV   s       r   test_empty_delta_encodez$EncoderTests.test_empty_delta_encodeq   s:    
+J77&677777r(   N)rH   rI   rJ   rv   rx   r|   r~   rK   r(   r   rq   rq   ^   sP        8 8 88 8 88 8 88 8 8 8 8r(   rq   c                       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 Zd Zd Zd Zd Zd Zd ZdS )ConverterTestsc                     d}g d}|                      |t          j        |                     |                      |t          j        |                     d S )Ns   A   A   A   A   A   A   )r9   r9   r9   r9   r9   r9   )r   r   decode_chain_listencode_chain_list)r$   in_bytesout_strings_tests      r   test_convert_chain_listz&ConverterTests.test_convert_chain_listz   s]    d666):+G+Q+QRRR:#?@P#Q#QRRRRRr(   c                    t          j        g d          }g d}t          j        |d                                          }t          t          |                    D ]&}|                     ||         ||         d           '|                     |                                t          j	        |d                     d S )N)i'  ik i~ )gn $@gEY@g "_@g     @@r1   )places)
r!   rj   r   convert_ints_to_floatsr   rangelenassertAlmostEqualr   convert_floats_to_ints)r$   in_arrayout_array_test	convertedis        r   test_convert_int_to_floatz(ConverterTests.test_convert_int_to_float   s    =!6!6!6771115hGGNNPP	s>**++ 	M 	MA"">!#4il!"LLLL**J,Mn^d,e,efffffr(   c                 f    g d}g d}|                      |t          j        |                     d S )Nr/       x    iχ  	r/   r   r   r   r   r   r   r   i  )r   r   recursive_index_encoder$   in_arrr   s      r   test_recursive_encz!ConverterTests.test_recursive_enc   s?    ///@@@)J6)R)RSSSSSr(   c                     t          j        g dt           j                  }g d}|                     |t	          j        |                                                     d S )Nr   ra   r   )r!   rj   rk   r   r   recursive_index_decoder   r   s      r   test_recursive_decz!ConverterTests.test_recursive_dec   s]    FFFU[YYY777)J6)R)R)Y)Y)[)[\\\\\r(   c           	      R   d}g d}|                      |t          j        |d                                                     |                      |t          j        |d                     |                      |t          j        t          j        |d          d                     d S )Nr@   rA   r/   r   r   convert_bytes_to_intsr   convert_ints_to_bytesr$   r   r   s      r   test_convert_one_byte_intz(ConverterTests.test_convert_one_byte_int   s    *$)I(ST)U)U)\)\)^)^___:#CNST#U#UVVV*"B:CcdlmnCoCopq"r"rsssssr(   c           	      R   d}g d}|                      |t          j        |d                                                     |                      |t          j        |d                     |                      |t          j        t          j        |d          d                     d S )NrD   )r   r/   r0   r/   r   r   r   r0   r0   r   r   s      r   test_convert_two_byte_intz(ConverterTests.test_convert_two_byte_int   s    V***)I(ST)U)U)\)\)^)^___:#CNTU#V#VWWW*"B:CcdlmnCoCopq"r"rsssssr(   c           	      R   d}g d}|                      |t          j        |d                                                     |                      |t          j        |d                     |                      |t          j        t          j        |d          d                     d S )NrD   rE   r2   r   r   s      r   test_convert_four_byte_intz)ConverterTests.test_convert_four_byte_int   s    V***)I(ST)U)U)\)\)^)^___:#CNST#U#UVVV*"B:CcdlmnCoCopq"r"rsssssr(   c                     d}t          |          \  }}}}|                     |d           |                     |d           |                     t          |          d           d S )NrD   rF   r   r2   )r   r   r   )r$   r   codeclengthr   	bytearrays         r   test_parse_headerz ConverterTests.test_parse_header   sk    V(4X(>(>%fUI'''q!!!Y*****r(   c                     g d}g d}|                      |t          j        |                     |                      |t          j        |                     d S )N)B   ?   C   )r<   ?r=   )r   r   convert_ints_to_charsconvert_chars_to_ints)r$   	int_arrayr   s      r   test_convert_int_to_charz'ConverterTests.test_convert_int_to_char   s\    ZZ	')I))T)TUUUJ$D^$T$TUUUUUr(   c                 $    t          d          }d S Nz"mmtf/tests/testdatastore/4CUP.mmtf)r   r$   decodeds     r   test_decoderzConverterTests.test_decoder   s    <==r(   c                 $    t          d          }d S )N%mmtf/tests/testdatastore/4CUP.mmtf.gz)r   r   s     r   test_gz_decoderzConverterTests.test_gz_decoder   s    DEEr(   c                     t          d          }|                                }|                    t          j        |                     d S r   )r   get_msgpackdecode_datamsgpackunpackb)r$   r   packeds      r   test_round_tripzConverterTests.test_round_trip   sE    <==$$&&GOF3344444r(   c                     t          dd          5 }t          |                                           d d d            d S # 1 swxY w Y   d S )Nr   rb)openr   read)r$   fhs     r   test_gzip_openzConverterTests.test_gzip_open   s    9$?? 	#2		"""	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	#s   "A  AAc                 ~    t          t                    rt          d          }d S t          dt          z              d S )N4CUPWarning - cannot connect to )r
   r   r   printr   s     r   
test_fetchzConverterTests.test_fetch   s;    !! 	;FmmGGG09:::::r(   c                     dd l }d |                    ||          D             rdS 	 |s|sdS n# t          $ r Y nw xY wt          |           t          |           t          d           dS )Nr   c                     g | ]}||S rK   rK   ).0xs     r   
<listcomp>z+ConverterTests.array_eq.<locals>.<listcomp>   s    <<<!!<A<<<r(   TzArrays not equalF)r!   isclose
ValueErrorr   r$   	array_one	array_twonps       r   array_eqzConverterTests.array_eq   s    <<rzz)I66<<< 	4     4   ))$%%%5s   / 
<<c                 6    dd l }|                    ||          S )Nr   )r!   array_equalr   s       r   char_arr_eqzConverterTests.char_arr_eq   s"    ~~i	222r(   c                    t          |d           }t          |d           }t          |          }|t          |          k    r|                     dd           t          |          D ]c}||         ||         k    r*t	          ||                    t	          ||                    |                     ||         ||         k               dd S )Nc                 D    t          |                                           S r[   sortedkeysr   s    r   <lambda>z0ConverterTests.dict_list_equal.<locals>.<lambda>       1A1A r(   )keyc                 D    t          |                                           S r[   r   r   s    r   r   z0ConverterTests.dict_list_equal.<locals>.<lambda>   r   r(   FzLists of different lengths)r   r   
assertTruer   r   )r$   list_onelist_twolen_oner   s        r   dict_list_equalzConverterTests.dict_list_equal   s    ((A(ABBB((A(ABBBh--c(mm##OOE">???w 	6 	6A{HQK''hqk"""hqk"""OOHQK!45555		6 	6r(   c           	         d}d}d}d}|j         D ]}t          |          D ]}t          |j        |                   D ]x}	|                     |j        |j        |                  d         |j        |j        |                  d                    |                     |j        |j        |                  d         |j        |j        |                  d                    |                     |j        |j        |                  d         |j        |j        |                  d                    |                     |j        |j        |                  d         |j        |j        |                  d                    |                     |j        |j        |                  d         |j        |j        |                  d                    |                     |j        |j        |                  d         |j        |j        |                  d                    |                     |j        |j        |                  d         |j        |j        |                  d                    |                     |j        |j        |                  d	         |j        |j        |                  d	                    |d
z  }z|d
z  }dS )Nr   atomNameListelementListbondOrderListbondAtomListformalChargeList	groupNamesingleLetterCodechemCompTyper/   T)chains_per_modelr   groups_per_chainr   
group_listgroup_type_listr   r   )
r$   data_onedata_two	chain_ind	group_indatom_ind_oneatom_ind_twomodelchaingroups
             r   iteratezConverterTests.iterate   s   		. 	 	Eu  "8#<Y#GHH ! !E$$X%89QR[9\%]^l%m"*"5h6Ny6Y"Z[i"jl l l$$X%89QR[9\%]^k%l%-%89QR[9\%]^k%ln n nMM("5h6Ny6Y"Z[j"k%-%89QR[9\%]^m%np p pMM("5h6Ny6Y"Z[i"j%-%89QR[9\%]^l%mo o oMM("5h6Ny6Y"Z[m"n%-%89QR[9\%]^p%qs s s$$X%89QR[9\%]^i%j%-%89QR[9\%]^i%jl l l$$X%89QR[9\%]^p%q%-%89QR[9\%]^p%qs s s$$X%89QR[9\%]^l%m%-%89QR[9\%]^l%mo o oqLII1		'( tr(   c                 
   |                      |                     |j        |j                             |                      |                     |j        |j                             |                      |                     |j        |j                             |                      |                     |j        |j                             |                      |                     |j        |j                             |                      |                     |j        |j                             |                      |                     |j	        |j	                             |                      |                     |j
        |j
                             |                      |                     |j        |j                             |                     |j        |j                   |                     |j        |j                   |                      |                     |j        |j                             |                     |j        |j                   |                     |j        |j                   |                     |j        |j                   |                     |j        |j                   |                     |j        |j                   |                      |                     |j        |j                             |                      |                     |j        |j                             |                     |j        |j                   |                     |j        |j                   |                      |                     |j        |j                             |                     |j        |j                   |                     |j        |j                   |                      |                     |j        |j                             |                     |j        |j                   |                     |j        |j                   |                     |j         |j                    |                     |j!        |j!                   |                     |j"        |j"                   |                     |j#        |j#                   |                     |j$        |j$                   |                     |j%        |j%                   |                     |j&        |j&                   |                      | '                    ||                     d S r[   )(r   r   x_coord_listy_coord_listz_coord_listb_factor_listoccupancy_listatom_id_listr   alt_loc_listins_code_listgroup_id_listr   entity_listbio_assemblysequence_index_listr   r   r   chain_name_listchain_id_listspace_groupbond_atom_listbond_order_liststructure_idtitleexperimental_methodsdeposition_daterelease_datesec_struct_listr_freer_work
resolution	unit_cell	num_bonds
num_chains
num_models	num_atoms
num_groupsr  )r$   r   r   s      r   check_equalzConverterTests.check_equal  sl   h&;H<QRRSSSh&;H<QRRSSSh&;H<QRRSSSh&<X=STTUUUh&=h>UVVWWWh&;H<QRRSSS(()>x?TUUVVV(()?@VWWXXXh&<X=STTUUUX1(2FGGGX283HIIIh&B8C_``aaa2H4MNNN2H4MNNN183KLLL/1GHHH-h.BCCCh&=h>UVVWWWh&>x?WXXYYY.x/DEEE777(()FxGdeefff1(2JKKK.x/DEEEh&>x?WXXYYY999999,X-@AAA+H,>???+X-?@@@,h.ABBB,h.ABBB+X-?@@@,h.ABBBXx8899999r(   c                     t          d          }t          d|t          j                   t	          d          }|                     ||           d S )Nr   z	test.mmtf)r   r   r	   pass_data_onr   r)  )r$   data_indata_rts      r   r   zConverterTests.test_round_trip+  sN    DEE;)ABBB$$'*****r(   c                 
   t          t                    rWt          |          }t          |dz   |t          j                   t          |dz             }|                     ||           d S t          dt          z              d S )Nz.mmtfr   )	r
   r   r   r   r	   r+  r   r)  r   )r$   pdb_idr,  r-  s       r   
round_tripzConverterTests.round_trip1  s}    !! 	;FmmGvg~w0HIIIF7N++GWg.....09:::::r(   c                 B    g d}|D ]}|                      |           d S )N)1a1q9pti2ja53zxw1nty2eax4pdj4a1i1cdr3zyb4cup1o2f1bna4y601skm1lpv1msh1r9v5emg4ck44v5a5esw3njw1ema)r0  )r$   id_listr/  s      r   test_round_trip_listz#ConverterTests.test_round_trip_list:  sD    / / /`  	$ 	$FOOF####	$ 	$r(   N)rH   rI   rJ   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r)  r0  rK  rK   r(   r   r   r   x   sz       S S Sg g gT T T
] ] ]
t t tt t tt t t+ + +V V V> > >F F F5 5 5
# # #; ; ;  3 3 3
6 
6 
6  8#: #: #:J+ + +; ; ;2$ 2$ 2$ 2$ 2$r(   r   __main__) unittestr   r!   mmtfr   r   r   r   mmtf.api.default_apir   r   r	   r
   mmtf.codecsr   mmtf.utils.codec_utilsr   mmtf.utils.constantsr   mmtf.codecs.default_codecr   mmtf.codecs.decodersr   rT   r   TestCaser   rM   rq   r   rH   mainrK   r(   r   <module>rW     s      3 3 3 3 3 3 3 3 3 3 3 3 P P P P P P P P P P P P             / / / / / / ) ) ) ) ) ) 0 0 0 0 0 0 ; ; ; ; ; ;S S S""4 "4 "4 "4 "4! "4 "4 "4H8 8 8 8 88$ 8 8 848 8 8 8 88$ 8 8 84t$ t$ t$ t$ t$X& t$ t$ t$l zHMOOOOO r(   