3
s]                 @   s4   d dl Zdd Zdd Zdd Zdd	 Zd
d ZdS )    Nc                s  | j  }|j    fdd|D }g }g }x<|D ]4}|j| j|| j  j |j|j||j  j q0W tj|}tj|}|jd }tj|dd}tj|dd}	|tj	||df }
|tj	|	|df }tj
|
| }tjj|\}}}|j|j }| |j |	j }||fS )zqRecovers transformation needed to align structure1 with structure2. Modified from http://nghiaho.com/?page_id=671c                s   g | ]}| kr|qS  r   ).0num)abs_indices2r   q/oak/stanford/groups/akundaje/marinovg/Symbiodinium/2019-09-27-figures/multi_mds/miniMDS-master/linear_algebra.py
<listcomp>   s    z%getTransformation.<locals>.<listcomp>r   )axis   )nonzero_abs_indicesappendpointsoffsetposnpmatshapemeantile	transposelinalgsvdT)Z
structure1Z
structure2Zabs_indices1intersectionabr   nZ
centroid_aZ
centroid_baabbhusvtrtr   )r   r   getTransformation   s(    



r$   c             C   s@   | d |d  d | d |d  d  | d |d  d  d S )z&Euclidean distance between coordinatesr      r	   g      ?g      ?r   )Zcoord1Zcoord2r   r   r   calcDistance&   s    r&   c                s>   t j| j }t j|dd t fdd|D }|t| S )Nr   )r   c                s   g | ]}t | qS r   )r&   )r   coord)centroidr   r   r   -   s    z&radius_of_gyration.<locals>.<listcomp>)r   array	getCoordsr   sumlen)	structurecoordsZdist_sumr   )r(   r   radius_of_gyration*   s    r/   c             C   s   t | }tj|tj||f|  }|j| d j| d }tjj|\}}tj|ddd }|| }|dd|f }tj|dddf |d d  |dddf |d d  |dddf |d d	  gj	S )
zDModified from http://www.nervouscomputer.com/hfs/cmdscale-in-python/r%   Nr	   r   g      ?g      ?g      ?g      ?)
r,   r   eyeonesdotr   eighargsortr)   r   )distMatr   r   r   evalsZevecsidxr   r   r   cmds0   s    r9   c             C   s&  dddg}t | |}| | }dddg}dddg}dddg}tjtj||}tj||}	t |	|}
|	|
 }tj|d }tj|d |d  }tj|d |d  }tj|d |d  }tj|d |d  |d  |d  d|| ||   d|| ||   gd|| ||   |d |d  |d  |d  d|| ||   gd|| ||   d|| ||   |d |d  |d  |d  gg}tj|j }tj|j }tj|j }|| }|| }|| }tj	|}xDt
t|D ]4}|| }tj||tj||tj||g||< qW |S )z5Rotate 3-D coords such that vector n aligns to z-axisr   r	   r%   )r&   r   arccosr3   crosscossinmatrixr   
zeros_likeranger,   )r   r.   originZlen_nZn_hatZi_hatZj_hatZk_hatthetar   Zlen_bb_hatq0q1q2Zq3QZu_hatZv_hatZw_hatZ
new_coordsipr   r   r   change_coordinate_systemE   s4    






,rJ   )numpyr   r$   r&   r/   r9   rJ   r   r   r   r   <module>   s
   #