B
    §d4q  ã               @   sz  d dl mZmZmZ d dlmZ d dlm  mZ	 d dl
mZ d dl
mZ e	 d¡Zd dlT d dlZdd	d
ddddgZG dd„ dƒZd dlmZ d dlmZ d dlmZ d dlZd dlZe	 eje	j¡ dd„ Ze	 ej ¡G dd„ dƒƒZ!G dd„ de"ƒZ#G dd„ de"ƒZ$dd„ Z%e	 e&¡G dd„ dƒƒZ!G dd „ d ƒZ'e	 e(¡G d!d„ dƒƒZ!e	 e)¡G d"d„ dƒƒZ!e	 e*¡G d#d„ dƒƒZ!e	 ej+¡G d$d„ dƒƒZ!e	 ej,¡G d%d„ dƒƒZ!G d&d'„ d'ej-ƒZ.G d(d)„ d)ej-ƒZ/d*d+„ Z0e	 ej1¡G d,d„ dƒƒZ!e	 ej2¡G d-d„ dƒƒZ!e	 e3¡G d.d„ dƒƒZ!e	 e4¡G d/d„ dƒƒZ!e	 e5¡G d0d„ dƒƒZ!G d1d2„ d2ej-ƒZ6d3d4„ Z7dS )5é    )Úabsolute_importÚdivisionÚprint_function)ÚuctbxN)Úrange)ÚzipÚcctbx_sgtbx_ext)Ú*zR 3zR -3zR 3 2zR 3 mzR 3 czR -3 mzR -3 cc               @   s   e Zd ZdS )ÚemptyN)Ú__name__Ú
__module__Ú__qualname__© r   r   ús/mnt/filia/a/genomebrowser/www/genomebrowser/fleming/tools/molprobity/modules/cctbx_project/cctbx/sgtbx/__init__.pyr
      s    r
   )Úflex)Úmatrix)Úrationalc             C   sˆ   |   d¡}t|ƒdkst‚g }xd|D ]\}| d¡}|dk s>t‚|dkrVt|ƒd }}ndd„ |  d¡D ƒ\}}| t ||¡¡ q$W |S )	Nú,é   ú/é   r   é   c             S   s   g | ]}t |ƒ‘qS r   )Úint)Ú.0Útr   r   r   ú
<listcomp>)   s    z%vec3_rat_from_str.<locals>.<listcomp>)ÚsplitÚlenÚAssertionErrorÚcountr   Úappendr   )ÚsÚfldsÚresultÚfldZslash_countÚnÚdr   r   r   Úvec3_rat_from_str   s    


r'   c               @   s6   e Zd Zddd„Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )Ú_Fc             c   s:   |rd|   ¡  }n|   ¡ }xt|ƒD ]}| |ƒV  q$W d S )Nr   )Ún_smxr   )ÚselfZwith_inversionr%   Zi_smxr   r   r   Úsmx0   s
     z_.smxc             c   s,   x&t |  ¡ ƒD ]}| |ddƒ ¡ V  qW d S )Nr   )r   Zn_ltrr   )r*   Úir   r   r   Últr6   s    z_.ltrc             C   s
   t | dS )N)Úgroup)Úspace_group_info)r*   r   r   r   Úinfo:   s    z_.infoc             C   s   t | ddS )NT)Úspace_groupZreciprocal_space)Ztensor_rank_2_constraints)r*   r   r   r   Úadp_constraints=   s    z_.adp_constraintsc             C   s
   t || ƒS )N)Z#tensor_rank_2_cartesian_constraints)r*   Ú	unit_cellr   r   r   Úcartesian_adp_constraints@   s    z_.cartesian_adp_constraintsN)F)r   r   r   r+   r-   r0   r2   r4   r   r   r   r   r(   -   s
   
r(   c               @   s   e Zd Zeefdd„ZdS )Úany_generator_setc             C   s6   || _ tj|ttd}| ¡  | ¡ | _| ¡ | _	d S )N)r1   Z	z2p_r_denZ	z2p_t_den)
r1   Úextr5   Úcb_r_denÚcb_t_denZset_primitiveZz_genZnon_primitive_generatorsZp_genÚprimitive_generators)r*   r1   Z	z2c_r_denZ	z2c_t_denZgen_setr   r   r   Ú__init__F   s    
zany_generator_set.__init__N)r   r   r   r7   r8   r:   r   r   r   r   r5   D   s   r5   c               @   s&  e Zd ZdZdHdd„Zdd„ Zdd„ Zd	d
„ Zdd„ ZdIdd„Z	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d „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ ZdJd/d0„Zd1d2„ Zd3d4„ Zd5d6„ ZdKd8d9„Zd:d;„ Zd<d=„ Z d>d?„ Z!dLd@dA„Z"dMdBdC„Z#dNdFdG„Z$dS )Or/   TNc             C   sÚ   |||g  d ¡dkst‚|d k	r(t|ƒ}|d kr\|d ks<t‚|d k	rT|| ¡ ksTt‚|| _n^|d ksht‚|d krzt|ƒ}nt|tƒrŒt|ƒ}t||ƒ}|d kr¬t|d| _nt||d| _| jd k	rÎ| j 	¡  t
ƒ | _d S )Nr   )Úspace_group_symbols)r;   Út_den)r   r   Ústrr<   Ú_groupr;   Ú
isinstancer   r1   Ú	make_tidyr
   Ú_space_group_info_cache)r*   ÚsymbolZtable_idr.   ÚnumberZspace_group_t_denZsymbolsr   r   r   r:   U   s,    

 


zspace_group_info.__init__c             C   s   |j | _ |j| _d S )N)r>   rA   )r*   Úotherr   r   r   Ú_copy_constructort   s    z"space_group_info._copy_constructorc             C   s   | j fS )N)r>   )r*   r   r   r   Ú__getstate__x   s    zspace_group_info.__getstate__c             C   s   |d | _ tƒ | _d S )Nr   )r>   r
   rA   )r*   Ústater   r   r   Ú__setstate__{   s    
zspace_group_info.__setstate__c             C   s   | j S )N)r>   )r*   r   r   r   r.      s    zspace_group_info.groupc       	      C   sª   | j }t|dd ƒ}|d krJ| j ¡ t }|d kr8|t }|d krp|t }n&|j}|d kr`|d }|d krp|d }|||f}|d ksŠ||kr¦||_t| jf| Ž  |_	}|S )NÚ_typer   r   )
rA   Úgetattrr>   r<   Zsg_t_denr7   r8   Z_type_paramsÚspace_group_typerI   )	r*   Z
tidy_cb_opÚr_denr<   ÚcacheZ
cache_typeÚfZcache_type_paramsZtype_paramsr   r   r   Útype‚   s$      
  
zspace_group_info.typec             C   s6   | j }y|jS  tk
r0   t|  ¡ ƒ|_|jS X d S )N)rA   Z_generatorsÚAttributeErrorr5   r.   )r*   rM   r   r   r   r5   “   s    z"space_group_info.any_generator_setc             C   s$   | j }t|dƒst|  ¡ ƒ|_|jS )NÚ_reciprocal_space_asu)rA   ÚhasattrÚreciprocal_space_asurO   rQ   )r*   rM   r   r   r   rS   ›   s    
z%space_group_info.reciprocal_space_asuc             C   sH   ddl m} | j}t|dƒsB| |  ¡  ¡ ¡}| |  ¡  	¡ ¡|_
|j
S )Nr   )Úreference_tableÚ_direct_space_asu)Zcctbx.sgtbx.direct_space_asurT   rA   rR   Zget_asurO   rC   Úchange_basisÚ'change_of_basis_op_to_reference_settingÚinverserU   )r*   rT   rM   Zreference_asur   r   r   Údirect_space_asu¡   s    
z!space_group_info.direct_space_asuc             C   s$   | j }t|dƒst|  ¡ ƒ|_|jS )NÚ_brick)rA   rR   ÚbrickrO   rZ   )r*   rM   r   r   r   r[   ª   s    
zspace_group_info.brickc             C   s$   | j }t|dƒst|  ¡ ƒ|_|jS )NÚ_wyckoff_table)rA   rR   Úwyckoff_tablerO   r\   )r*   rM   r   r   r   r]   °   s    
zspace_group_info.wyckoff_tablec             C   s"   | j }t|dƒst| jƒ|_|jS )NÚ_structure_seminvariants)rA   rR   Ústructure_seminvariantsr>   r^   )r*   rM   r   r   r   r_   ¶   s    
z(space_group_info.structure_seminvariantsc             C   s*   | j }t|dƒs$t|  ¡  ¡ d|_|jS )NÚ_reference_setting)rB   )rA   rR   r/   rO   rC   r`   )r*   rM   r   r   r   Úreference_setting¼   s    
z"space_group_info.reference_settingc             C   s   |   ¡  ¡ S )N)rO   Úcb_op)r*   r   r   r   rW   Â   s    z8space_group_info.change_of_basis_op_to_reference_settingc             C   s   |   ¡  ¡ S )N)rW   Zis_identity_op)r*   r   r   r   Úis_reference_settingÅ   s    z%space_group_info.is_reference_settingc             C   s   |   |  ¡ ¡S )N)rV   rW   )r*   r   r   r   Úas_reference_settingÈ   s    z%space_group_info.as_reference_settingc             C   s&   t |tƒrt|ƒ}t|  ¡  |¡dS )N)r.   )r?   r=   Úchange_of_basis_opr/   r.   rV   )r*   rb   r   r   r   rV   Ë   s    
zspace_group_info.change_basisc             C   s   |   ¡  ¡ S )N)rO   Zchange_of_hand_op)r*   r   r   r   Ú change_of_basis_op_to_other_handÐ   s    z1space_group_info.change_of_basis_op_to_other_handc             C   s   |   |  ¡ ¡S )N)rV   rf   )r*   r   r   r   Úchange_handÓ   s    zspace_group_info.change_handc             C   s   |   ¡  ¡ S )N)r.   Úz2p_op)r*   r   r   r   Ú'change_of_basis_op_to_primitive_settingÖ   s    z8space_group_info.change_of_basis_op_to_primitive_settingc             C   s   |   |  ¡ ¡S )N)rV   ri   )r*   r   r   r   Úprimitive_settingÙ   s    z"space_group_info.primitive_settingc             C   sâ  |   ¡ }|  ¡ }|  |¡}| |¡}| ¡ | ¡ kr8dS | ¡ | }| ¡  ¡  ¡ rX|S |  ¡ }| ¡ }|  |¡}	| |¡}
tdd„ |	 ¡ D ƒƒtdd„ |
 ¡ D ƒƒkrÞt	dd„ |	 ¡ D ƒƒ}t
 d¡}x¦|
 ¡ D ]š}|| ¡  }t|ƒ}| ¡ | ¡ krd}P | ¡  | ¡ ¡ ¡ }| ¡ }x8tdƒD ],}|| dkr,d| ¡  ||  ||< q,W |j d¡dkrÔP qÔW |dk	rÞt|dd	}| ¡ sÞtt|ƒƒ dd
¡}| ¡ | | }| ¡  |  |¡}| ¡ | ¡ krÞ|S |S )zd The change of basis from self to other.
    This method strives to return a mere origin shift.
    Nc             S   s   g | ]}|  ¡ ‘qS r   )Úr)r   Úopr   r   r   r   ó   s    z:space_group_info.change_of_basis_op_to.<locals>.<listcomp>c             S   s   g | ]}|  ¡ ‘qS r   )rk   )r   rl   r   r   r   r   ô   s    c             S   s   g | ]}|  ¡ t|ƒf‘qS r   )rk   Útranslation_part_info)r   rl   r   r   r   r   õ   s   r   r   é   )Ztr_dené   )rW   rV   r.   rX   Úcrk   Ú
is_unit_mxri   ÚsetÚdictr   Úmutable_zerosrm   Úintrinsic_partÚorigin_shiftÚminusÚmod_positiveÚnumr   ÚdenÚelemsr   Útr_vecÚis_zerore   Úrt_mxÚnew_denominatorsZmod_positive_in_place)r*   rD   Zself_to_ref_opZother_to_ref_opZself_as_referenceZother_as_referenceZobvious_resultZself_z2p_opZother_z2p_opÚprimitive_selfZprimitive_otherZself_tr_info_for_rrv   rl   Zself_tiZother_tiÚdeltaZ	delta_numr,   rb   Ztentative_otherr   r   r   Úchange_of_basis_op_toÜ   sV    

  


 


z&space_group_info.change_of_basis_op_toc             C   s~   g }|   ¡  ¡ }| ¡ }|j|d}| ¡  xLtƒ D ]B}| ¡ |krFq4t|ƒ}|j|d|kr4| | |  	¡  
¡ ¡¡ q4W |S )N)Úanomalous_flag)ra   r.   Zcrystal_systemÚ(build_derived_reflection_intensity_groupr@   Úspace_group_symbol_iteratorr1   r    rV   rW   rX   )r*   rƒ   r#   Zreference_groupZreference_crystal_systemZ$reference_reflection_intensity_groupr;   Zother_sgr   r   r   Ú&reflection_intensity_equivalent_groups  s     z7space_group_info.reflection_intensity_equivalent_groupsc             C   s$   | j }t|dƒs|  ¡  ¡ |_|jS )NÚ_lookup_symbol)rA   rR   rO   Úlookup_symbolr‡   )r*   rM   r   r   r   Ú__str__%  s    
zspace_group_info.__str__c             C   s(   | j }t|dƒs|  ¡  ¡ |_d|j S )Nr‡   z#sgtbx.space_group_info(symbol="%s"))rA   rR   rO   rˆ   r‡   )r*   rM   r   r   r   Ú__repr__+  s    
zspace_group_info.__repr__c             C   s   dt | ƒ|  ¡  ¡ f S )Nz%s (No. %d))r=   rO   rC   )r*   r   r   r   Úsymbol_and_number1  s    z"space_group_info.symbol_and_numberúSpace group: c             C   s*   |d krt j}td||  ¡ f |d d S )Nz%s%s)Úfile)ÚsysÚstdoutÚprintr‹   )r*   rN   Úprefixr   r   r   Úshow_summary4  s     zspace_group_info.show_summaryc             C   s   |   ¡  ¡ S )N)r_   Ú*number_of_continuous_allowed_origin_shifts)r*   r   r   r   r“   8  s    z;space_group_info.number_of_continuous_allowed_origin_shiftsc             C   s,   |   ¡ }| ¡ |  ¡  ¡ j| ¡ | d S )N)Ztranslation)rW   Úc_invra   r_   Z$subtract_principal_continuous_shiftsrp   )r*   Ztranslation_fracrb   r   r   r   Ú)subtract_continuous_allowed_origin_shifts<  s    z:space_group_info.subtract_continuous_allowed_origin_shiftsc       
         s„   ddl m‰  ‡ ‡fdd„}|  ¡  ¡ }|  |¡}| ¡ | }||ƒrHdS x6| ¡ jD ]$}| ¡  	¡ }|| }	||	ƒsTdS qTW dS dS )a4  
    Determine whether the specified fractional coordinate shift is allowed
    under the space group rules.

    :param shift: tuple specifying fractional coordinate shift
    :param tolerance: tolerance for coordinate shifts outside the allowed
                      range
    :returns: Python boolean
    r   )Úiroundc                s   t ‡ fdd„| D ƒƒˆk S )Nc                s   g | ]}t |ˆ |ƒ ƒ‘qS r   )Úabs)r   Úx)r–   r   r   r   M  s    zNspace_group_info.is_allowed_origin_shift.<locals>.<lambda>.<locals>.<listcomp>)Úmax)Úv)r–   Ú	tolerancer   r   Ú<lambda>M  ó    z:space_group_info.is_allowed_origin_shift.<locals>.<lambda>TFN)
Úlibtbx.math_utilsr–   r.   rh   rV   rp   r5   r9   rk   Zminus_unit_mx)
r*   Úshiftr›   Zis_ltrrh   r€   Zprimitive_shiftr!   Zw_m_ir   r   )r–   r›   r   Úis_allowed_origin_shiftB  s    

z(space_group_info.is_allowed_origin_shiftc             C   sæ   ||g  d¡dkst‚|dkr.||  ¡  ¡  }|  ¡  ¡ }|dk rHd}n<|dk rVd}n.|dk rdd}n |d	k rrd
}n|dk r€d}nd}t |¡j|  	¡  
¡ d}|| ¡  d }t| ¡ ƒ}x tdƒD ]}||  |9  < qÄW t |¡S )a0  
    Generate a unit cell of arbitrary dimensions (optionally filling the
    desired volume) with parameters compatible with the specified space
    group.

    :param volume: desired unit cell volume
    :param asu_volume: desired volume of the asymmetric unit
    :returns: uctbx.unit_cell object
    Nr   r   )g      ð?gÍÌÌÌÌÌô?g333333û?éS   ém   é   é   )g      ð?gÍÌÌÌÌÌô?g333333û?éZ   r¢   r¥   éK   )g      ð?gÍÌÌÌÌÌô?g333333û?r¥   r¥   r¥   é   )g      ð?g      ð?g333333û?r¥   r¥   r¥   éÃ   )g      ð?g      ð?g333333û?r¥   r¥   éx   )g      ð?g      ð?g      ð?r¥   r¥   r¥   )rb   gUUUUUUÕ?)r   r   r.   Zorder_zrO   rC   r   r3   rV   rW   rX   ÚvolumeÚlistÚ
parametersr   )r*   rª   Ú
asu_volumeZ	sg_numberÚparamsr3   rN   r,   r   r   r   Úany_compatible_unit_cell[  s,    

 z)space_group_info.any_compatible_unit_cellc             C   s$   ddl m} |j| j||d| dS )Nr   )Úcrystal)rª   r­   )r3   r/   )Úcctbxr°   Úsymmetryr¯   )r*   rª   r­   r°   r   r   r   Úany_compatible_crystal_symmetry|  s
    z0space_group_info.any_compatible_crystal_symmetryFr(   c             C   sð   | j }| j}t|dƒs^i }x<t|ƒD ]0\}}| | ¡ i ¡ ||| ¡  | ¡  ¡ < q$W ||_| 	| 
¡ | ¡ ¡}|j| ¡  }	| ¡  ¡ }
|	|
 }| ¡  ||  ¡ ¡}|sÂ| ¡ rÂd|d  S tdd„ | ¡ D ƒƒ}d|d |f d|  S )aR   The symmetry code for the given rt_mx in the given space group as
        required by the CIF:

        http://www.iucr.org/__data/iucr/cifdic_html/1/cif_core.dic/Igeom_angle_site_symmetry_.html

        With the default full_code=False, if the translation term is zero,
        just the index of the symmetry operation is returned.
    Ú_index_lookup_tablez%ir   c             S   s   g | ]}t |ƒd  ‘qS )é   )r   )r   r,   r   r   r   r   ›  s    z6space_group_info.cif_symmetry_code.<locals>.<listcomp>z%i%sz%i%i%i)r>   rA   rR   Ú	enumerateÚ
setdefaultrk   r   rx   r´   r   rL   r<   rw   r}   ÚtupleÚ	as_double)r*   r~   Z	full_codeÚsepr.   rM   Úlookupr,   rl   Ztr_vecsZt_mod_positiveÚidxZt_diffZklmr   r   r   Úcif_symmetry_codeƒ  s"    	
z"space_group_info.cif_symmetry_code)NNNNN)TNN)T)NrŒ   )NN)NN)Fr(   )%r   r   r   Ú__safe_for_unpickling__r:   rE   rF   rH   r.   rO   r5   rS   rY   r[   r]   r_   ra   rW   rc   rd   rV   rf   rg   ri   rj   r‚   r†   r‰   rŠ   r‹   r’   r“   r•   r    r¯   r³   r½   r   r   r   r   r/   Q   sL       

	7


!
r/   c              c   s$   xt ddƒD ]} t| dV  qW d S )Nr   éç   )rC   )r   r/   )rC   r   r   r   Úreference_space_group_infosž  s    rÀ   c               @   s   e Zd Zdd„ ZdS )r(   c             C   s   t  t |  ¡ |  ¡ ¡¡S )N)r   Úcolr   Úvectorry   rz   )r*   r   r   r   Úas_rational¥  s    z_.as_rationalN)r   r   r   rÃ   r   r   r   r   r(   ¢  s   c               @   s   e Zd Zddd„ZdS )Úle_page_1982_delta_detailsFc             C   s    t  | ¡ ¡}t  | ¡ ¡}| ¡ }| ¡ | _| ¡  ¡ | _| ¡  ¡  ¡ | _	|t  
| j¡ | _t  | j	¡| | _t| jƒdkrˆd| _n| jj| j|d| _d S )Nr   g        )Údeg)r   ÚsqrÚorthogonalization_matrixÚfractionalization_matrixr0   rO   ÚevÚuÚ	transposeÚhrÁ   r   ÚrowÚtaur—   r   Úaccute_angle)r*   Úreduced_cellÚrot_mxrÅ   ÚorthÚfracÚr_infor   r   r   r:   ª  s    
z#le_page_1982_delta_details.__init__N)F)r   r   r   r:   r   r   r   r   rÄ   ¨  s   rÄ   c               @   s0   e Zd Zdd„ Zddd„Zddd„Zdd	„ Zd
S )r(   c             C   s   t  t |  ¡ |  ¡ ¡¡S )N)r   rÆ   r   rÂ   ry   rz   )r*   r   r   r   rÃ   »  s    z_.as_rationalFc             C   s   t || |dS )N)rÐ   rÑ   rÅ   )rÄ   )r*   rÐ   rÅ   r   r   r   rÄ   ¾  s    z_.le_page_1982_delta_detailsc             C   s   | j ||djS )N)rÐ   rÅ   )rÄ   r   )r*   rÐ   rÅ   r   r   r   Úle_page_1982_deltaÂ  s    z_.le_page_1982_deltac             C   sP   t j| ¡ d}|  ¡  ¡ }| ¡ | | }| ¡ ||  }||  ¡ d d S )N)Úsym_mat3é   g      à?)r   ÚsymZmetrical_matrixrÃ   Úas_floatrË   rX   Útrace)r*   rÐ   r!   Úmrk   Zsirmsr   r   r   Úlebedev_2005_perturbationÆ  s
    z_.lebedev_2005_perturbationN)F)F)r   r   r   rÃ   rÄ   rÕ   rÜ   r   r   r   r   r(   ¸  s   

c               @   s   e Zd Zdd„ Zdd„ ZdS )r(   c             C   sj   ddl m} t| dƒsd|  ¡ dkr(d}n6|  ¡ dkrB||  ¡ ƒ}n|  ¡ dk rTd}n
|  ¡ f}|| _| jS )Nr   )Ú!basis_of_mirror_plane_with_normalÚ_basis_of_invariantr   ))r   r   r   )r   r   r   )r   r   r   éþÿÿÿr   )Zcctbx.math_modulerÝ   rR   rO   rÉ   rÞ   )r*   rÝ   Úbasisr   r   r   Úbasis_of_invariantÑ  s    

z_.basis_of_invariantc             C   s<   d|   ¡  }|  ¡ dkr(|d|  ¡  7 }|d|  ¡  7 }|S )Nz% iéÿÿÿÿz^%iz |(%i, %i, %i))rO   ZsenserÉ   )r*   r#   r   r   r   r‰   ß  s
     z	_.__str__N)r   r   r   rá   r‰   r   r   r   r   r(   Î  s   c               @   s   e Zd Zdd„ ZdS )r(   c             C   s*   d|   ¡  ¡ |  ¡  ¡ |  ¡  ¡ f }|S )Nz+(%s) @(%s) t_l=(%s))ru   rx   rv   Zlocation_part)r*   r#   r   r   r   r‰   è  s    
z	_.__str__N)r   r   r   r‰   r   r   r   r   r(   å  s   c               @   s.   e Zd Zdd„ Zdd„ Zdd„ Zd
dd	„ZdS )r(   c             C   s(   t  |  ¡ |  ¡  ¡ |  ¡  ¡ f ¡fS )N)r   r   Zas_int_arrayrk   rz   r   )r*   r   r   r   Ú__getinitargs__ò  s    z_.__getinitargs__c             C   s   t  |  ¡  ¡ |  ¡  ¡ f¡S )N)r   Úrtrk   rÃ   r   )r*   r   r   r   rÃ   õ  s    z_.as_rationalc             C   sŒ   |   ¡  ¡ j}|  ¡  ¡ j}t d¡}t d¡}t |d |d |d |d |d |d |d |d |d |d |d	 |d ||||fd
¡S )Nr   r   r   r   é   rµ   é   é   é   )rå   rå   )rk   rÃ   r{   r   r   r   r   Úrec)r*   rk   r   ÚzeroÚoner   r   r   Úas_4x4_rationalø  s    

z_.as_4x4_rationalNc             C   s:   |d krt j}|  ¡  ¡ }t| ƒ}td||f |d d S )Nz%s %s)r   )rŽ   r   rk   r0   rm   r   )r*   ÚoutrÔ   Zt_infor   r   r   Úshow_geometrical_elements  s
     z_.show_geometrical_elements)N)r   r   r   rã   rÃ   rì   rî   r   r   r   r   r(   ï  s   c               @   s   e Zd Zddd„ZdS )r(   Nc             C   sl   |d krt j}td|  ¡ |d td|  ¡ |d td|  ¡ |d td|  ¡ |d td|  ¡ |d d S )Nzuse_space_group_symmetry:)r   zuse_space_group_ltr:zuse_normalizer_k2l:zuse_normalizer_l2n:zuse_seminvariants:)rŽ   r   r   Úuse_space_group_symmetryZuse_space_group_ltrZuse_normalizer_k2lZuse_normalizer_l2nZuse_seminvariants)r*   rN   r   r   r   r’     s     z_.show_summary)N)r   r   r   r’   r   r   r   r   r(   	  s   c               @   s.   e Zd ZdddgZdd„ Zdd„ Zdd	„ Zd
S )Úspecial_op_simplified_termÚi_varsÚmultipliersÚconstantc             C   s<   t |ƒt |ƒkst‚| d¡dks&t‚|| _|| _|| _d S )Nr   )r   r   r   rñ   rò   ró   )ÚOrñ   rò   ró   r   r   r   r:     s
    z#special_op_simplified_term.__init__c             C   s6   t | jƒdkrdS | jd dkr$dS | jdkr2dS dS )Nr   Fr   T)r   rò   ró   )rô   r   r   r   Úis_identity  s      
 z&special_op_simplified_term.is_identityc             C   sÆ   d}xjt | j| jƒD ]X\}}|dk r4|d7 }| }nt|ƒdkrH|d7 }|dkr`|t|ƒd 7 }|d| 7 }qW | j}|dksŠt|ƒdkrÂ|dk r¢|d7 }| }nt|ƒdkr¶|d7 }|t|ƒ7 }|S )NÚ r   ú-ú+r   r	   Úxyz)r   rñ   rò   r   r=   ró   )rô   r!   r,   rÛ   rp   r   r   r   r‰   %  s&    z"special_op_simplified_term.__str__N)r   r   r   Ú	__slots__r:   rõ   r‰   r   r   r   r   rð     s   
rð   c               @   s,   e Zd ZdgZdd„ Zdd„ Zd
dd„Zd	S )Úspecial_op_simplifiedÚtermsc             C   s
   || _ d S )N)rü   )rô   rü   r   r   r   r:   >  s    zspecial_op_simplified.__init__c             C   s   d  dd„ | jD ƒ¡S )Nr   c             S   s   g | ]}t |ƒ‘qS r   )r=   )r   Útermr   r   r   r   B  s    z1special_op_simplified.__str__.<locals>.<listcomp>)Újoinrü   )rô   r   r   r   r‰   A  s    zspecial_op_simplified.__str__çñhãˆµøä>c             C   s   ddl m} |j| |||dS )Nr   )Úfvar_encoding)rô   ÚfvarsÚsiteÚp_tolerance)Úiotbx.shelxr   Z&site_constraints_special_op_simplified)r*   r  r  r  r   r   r   r   Úshelx_fvar_encodingD  s    z)special_op_simplified.shelx_fvar_encodingN)rÿ   )r   r   r   rú   r:   r‰   r  r   r   r   r   rû   :  s   rû   c          	   C   s|  |   ¡ }|j}|j}|d d… |dd… |dd … g}d d d g}t d¡}t d¡}d}x8t|ƒD ],\}	}
|
dkrbtg g ||	 ƒ||	< |d7 }qbW |dkr¤t|dS |dkr¶g g  }}x@tdƒD ]4}| 	||d  ||d  g¡ | 	||d  ¡ qÂW ddl
m} | ||¡}t|ƒdkr¶| |||d gd ¡}|d k	r¶| d¡dkr¶x"d	D ]}	t|	g|g|ƒ||	< qVW tddg||d |d |d   |d |d   ƒ|d< t|dS xºtdƒD ]®}	||	 d k	rÖqÀt|	g|g|ƒ||	< x€t|	d dƒD ]n}|| d k	rqútj||	 || d
}|d kr4qú|dksBt‚t|	g|g|| |||	   ƒ||< qúW qÀW t|dS )Nr   ræ   r   r   )r   r   r   )rü   )Úrow_echelonr   )r   r   )Úvector_1Úvector_2)rÃ   rk   r   r   r   r¶   rð   rû   r   r    Úscitbx.matrixr  Zform_rationalr   Zback_substitution_rationalr   r   Ú&linearly_dependent_pair_scaling_factorr   )Ú
special_oprä   rk   r   Úrowsrü   Zr0Úr1Zn_doneÚi_rowrÍ   rÛ   rš   r,   r  Z	free_varsZsolZj_rowr   r   r   Úspecial_op_simplifierI  sX    "






6
  
 .r  c               @   s&   e Zd Zdd„ Zdd„ Zd	dd„ZdS )
r(   c             C   s   |   ¡ |  ¡ |  ¡ fS )N)Úmultiplicityr  Úmatrices)r*   r   r   r   rã   w  s    z_.__getinitargs__c             C   s   t |  ¡ dS )N)r  )r  r  )r*   r   r   r   rû   z  s    z_.special_op_simplifiedçñhãˆµøä>c             C   s   ddl m} |j| |||dS )Nr   )r   )rô   r  r  r  )r  r   Z"site_constraints_site_symmetry_ops)r*   r  r  r  r   r   r   r   r  }  s    z_.shelx_fvar_encodingN)r  )r   r   r   rã   rû   r  r   r   r   r   r(   t  s   c               @   sN   e Zd Zdd„ Zdd„ Zddd„Zd	d
„ Zdd„ Zdd„ Zdd„ Z	dd„ Z
dS )r(   c             C   s   |   ¡ |  ¡ |  ¡ fS )N)ÚindicesÚtableÚspecial_position_indices)r*   r   r   r   rã   …  s    z_.__getinitargs__c             C   sF   |j |d}x,|  ¡ D ] }| j|d ¡ ||  ||< qW |j|dS )N)Ú
sites_cart)Úi_seq)Ú
sites_frac)Úfractionalizer  Úgetr  Úorthogonalize)r*   r3   r  r  r  r   r   r   Úapply_symmetry_sitesˆ  s
    z_.apply_symmetry_sitesNrö   c	          
   C   sÔ  ||g  d ¡dkst‚||g  d ¡dks,t‚|d kr:tj}t|d |  ¡  ¡ |d |  ¡  ¡ dkrÐd}	x"|  ¡ D ]}
t|	t||
 ƒƒ}	qtW d|	 }t|d| 	¡   |d t|d |d	  d
 |d | 
¡ }|d kræ|j|d}|d krú|j|d}xÔ|  ¡ D ]È}
||
 }||
 }| j|
d}t|d |||
   d| ¡ | ||¡f|   |d t||d  d| |¡ ¡   d|  |d t| ¡ ƒ}t||d  ddtddt|ƒ d ƒ   ||d qW d S )Nr   z%Number of sites at special positions:)r   r   rµ   z%%-%dsz?  Minimum distance between symmetrically equivalent sites: %.4gz  ZLabelz)   Mult   Shift    Fractional coordinates)r  )r  z)  %4d  %7.3f (%8.4f %8.4f %8.4f) originalrö   z   site sym %-6sz(%8.4f %8.4f %8.4f) exactú é   é   r   )r   r   rŽ   r   r   r  Úsizer™   r   Úmin_distance_sym_equivr3   r  r  r  ÚdistanceÚsite_symmetryZpoint_group_typer=   r  )r*   Úspecial_position_settingsZsite_labelsZsites_frac_originalZsites_cart_originalZsites_frac_exactZsites_cart_exactrí   r‘   Z	label_lenr  Z	label_fmtÚucÚsoZseZspecial_opsr!   r   r   r   Úshow_special_position_shifts  s@    	 ",z_.show_special_position_shiftsc             C   s`   t |  ¡ ƒdkst‚|  ¡ d }| ¡ s,t‚tt |  ¡  ¡ d¡t	d| 
¡ | ¡ ƒgt ¡ dS )Nr   r   )r  r  r  )r   r  r   Zis_point_group_1Úsite_symmetry_tabler   Úsize_tr  r   Úsite_symmetry_opsr  r  )r*   Zsite_symr   r   r   Údiscard_symmetryº  s    z_.discard_symmetryc             C   s    t |  |¡|  |¡||k|dS )N)Úsite_symmetry_ops_iÚsite_symmetry_ops_jÚi_seq_eq_j_seqÚrt_mx_ji)Ú%symmetry_equivalent_pair_interactionsr  )r*   r  Új_seqr/  r   r   r   r0  Ã  s
    z'_.symmetry_equivalent_pair_interactionsc             C   sp   |  ¡ |  ¡   ¡ kst‚t ¡ }xJt|ƒD ]>\}}|  |¡rP|  |¡ ¡  	|¡}x|D ]}| 
|¡ qVW q*W |S )N)r   r  r   r   Údoubler¶   Úis_special_positionr  Úsite_constraintsZindependent_paramsr    )r*   r  r#   r  r˜   r(   r   r   r   Úpack_coordinatesÊ  s    

 z_.pack_coordinatesc             C   s˜   t  ¡ }d}d}xr|  ¡ D ]f}|dkr@|d }t|||… ƒ}n2|  ¡ |  ¡ }|| ¡  }| t|||… ƒ¡}|}| |¡ qW || 	¡ ks”t
‚|S )Nr   r   )r   Úvec3_doubler  r«   r  r4  Zn_independent_paramsZ
all_paramsr    r   r   )r*   Zpacked_coordinatesr#   ÚixZjxZi_tabr˜   Úscr   r   r   Úunpack_coordinatesÓ  s    z_.unpack_coordinatesc             C   sp   |  ¡ |  ¡   ¡ kst‚t ¡ }xJt|ƒD ]>\}}|  |¡rP|  |¡ ¡  	|¡}x|D ]}| 
|¡ qVW q*W |S )N)r   r  r   r   r2  r¶   r3  r  r4  Zindependent_gradientsr    )r*   Zg_fracr#   r  Úgr(   r   r   r   Úpack_gradientsä  s    

 z_.pack_gradients)NNNNNrö   )r   r   r   rã   r  r'  r+  r0  r5  r9  r;  r   r   r   r   r(   ‚  s   
     
#		c               @   s   e Zd Zdd„ ZdS )r(   c             C   s   t |  ¡ dS )N)r  )r  r  )r*   r   r   r   rû   ð  s    z_.special_op_simplifiedN)r   r   r   rû   r   r   r   r   r(   í  s   c               @   s   e Zd Zddd„ZdS )r(   ©éûÿÿÿræ   ç:Œ0âŽyE>c       	         sŽ   |   |¡}d}xz|d7 }|dk s$t‚| ¡ dd„ tdƒD ƒ }ˆ d k	rX‡ fdd„|D ƒ}| |¡}| ¡ |k r| ¡ | ¡ ks‚t‚|S qW d S )Nr   r   iè  c             S   s   g | ]}t   ¡ ‘qS r   )Úrandom)r   r,   r   r   r   r      s    z*_.random_site_symmetry.<locals>.<listcomp>r   c                s   g | ]}|t jˆ Ž  ‘qS r   )r?  Ú	randrange)r   r˜   )Úunit_shift_ranger   r   r     s    )Úpositionr   r  r   r#  Zdistance_movedr  )	r*   r$  Z
i_positionrA  r›   rB  Zrun_away_counterr  r#  r   )rA  r   Úrandom_site_symmetryö  s    

z_.random_site_symmetryN)r<  r>  )r   r   r   rC  r   r   r   r   r(   ó  s    c               @   s   e Zd Zdd„ Zdd„ ZdS )r(   c             C   s>   g }x$|   ¡ D ]}| |jf|j ¡ qW d dd„ |D ƒ¡S )NÚ
c             S   s   g | ]}d | ‘qS )z%i: (%i, %i, %i)r   )r   Úitemr   r   r   r     s    z_.__str__.<locals>.<listcomp>)Zvectors_and_modulir    rÛ   rš   rþ   )r*   r#   Zvmr   r   r   r‰     s    z	_.__str__c             C   s   |   d¡ ¡ S )NF)Úselectr   )r*   r   r   r   r“     s    z,_.number_of_continuous_allowed_origin_shiftsN)r   r   r   r‰   r“   r   r   r   r   r(     s   c               @   s,   e Zd ZddgZdd„ Zdd„ Zdd„ Zd	S )
r0  r-  Úregistryc       	         sª   |ˆ _ i ˆ _| ¡ }ˆ j  ¡ ‰|sPt|ƒdkrPtˆƒdkrP| ¡ }|ˆ j|< d S ˆ j  ¡ ‰‡ ‡‡fdd„}x|D ]}|||ƒ qpW |r¦| ¡ }x|D ]}|||ƒ q”W d S )Nr   c                sN   |   |¡}|  ˆ¡}|ˆ jkrJ|}xˆD ]}t||  |¡ƒ}q(W |ˆ j|< d S )N)ÚmultiplyrG  Úmin)Úmir/  Zrt_mx_ji_eqÚrsÚbestZmj)rô   Ússm_jÚsso_jr   r   Úadd'  s    



z;symmetry_equivalent_pair_interactions.__init__.<locals>.add)r-  rG  r  r   Úcancelr  rX   )	rô   r,  r-  r.  r/  Zssm_irO  rJ  Zrt_mx_ji_invr   )rô   rM  rN  r   r:     s     




z.symmetry_equivalent_pair_interactions.__init__c             C   s   t | j ¡ ƒ}| ¡  |S )N)r«   rG  ÚvaluesÚsort)rô   r#   r   r   r   r  6  s    z)symmetry_equivalent_pair_interactions.getc             C   s   | j  ¡ }| |¡}|| jkS )N)r-  r  rH  rG  )rô   r/  rN  rK  r   r   r   Úis_equivalent;  s    

z3symmetry_equivalent_pair_interactions.is_equivalentN)r   r   r   rú   r:   r  rS  r   r   r   r   r0    s   r0  c             C   s>   t | ƒt |ƒk rdS t | ƒt |ƒkr(dS ddlm} || |ƒS )Nrâ   r   r   )Úcmp)r   rž   rT  )ÚaÚbrT  r   r   r   Úcompare_cb_op_as_hkl@  s      rW  )8Ú
__future__r   r   r   r±   r   Úboost_adaptbx.boost.pythonÚboostÚpythonÚbpÚ	six.movesr   r   Ú
import_extr6   r   ÚlibtbxZ$rhombohedral_hermann_mauguin_symbolsr
   Úcctbx.array_familyr   Úscitbxr   Úboost_adaptbx.boostr   r?  rŽ   Úinjectr…   Úpy3_make_iteratorr'   Úinject_intor1   r(   Úobjectr5   r/   rÀ   r|   rÄ   rÑ   Zrot_mx_inform   r~   Zsearch_symmetry_flagsÚslots_getstate_setstaterð   rû   r  r*  r(  Zwyckoff_positionr]   r_   r0  rW  r   r   r   r   Ú<module>   sZ   
  O
&+k,