3
cU                 @   s   d dl Z d dljjZd dlZd dlZd dlZ	d dl
Zd dlZd dlZd dlZyd dlZW n ek
rt   d dlZY nX d dlmZ d dlT d dlmZ d dljZd dlmZ d dlmZ G dd dZdS )	    N)
csr_matrix)*)cm)check_random_state)MulticoreTSNEc               @   s>   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdddZdS )MNAEmbeddingc          &      sb  i }|}t |}	x|	D ]}
|
j jd}|d }|d }t||krTt |t|< |t| jtd t||krt |t|< |t| jtd qW W d Q R X i }|}t |}	x|	D ]}
y|
j jd}|d }|d }t||krt |t|< |t| jtd t||kr0t |t|< |t| jtd W q   Y qX qW W d Q R X tj|}tj| _|}|}i  _	i  _
i  _t |dddV}tj|dd	d
gd}x:|D ]2}t|d	  j	|d
 < |d
  j
t|d	 < qW W d Q R X t |ddd@}tj|dd	d
gd}x$|D ]}t|d	  j|d
 < q(W W d Q R X g }g }x0|D ](}y|j j	|  W n   Y nX q`W x0|D ](}y|j j	|  W n   Y nX qW tj|j  _tj|j  _tj| j  _tj| j  _td t jj td t jj dg} fdd|D }tj|} fdd|D }d S )N	r      i%  ij'   zutf-8)newlineencodingidname)	delimiterZ
fieldnameszNetwork 1 shapezNetwork 2 shapez
ppi::Humanc                s   g | ]} j | qS  )relation_map).0treat)selfr   4/home/warith/Documents/LMNAKG_2/MNAmaster/mna_emb.py
<listcomp>m   s    z)MNAEmbedding.__init__.<locals>.<listcomp>c                s   g | ]}t j j| qS r   )thtensorrelation_emb)r   rid)r   r   r   r   o   s    )openstripsplitstrsetaddnploadr   
entity_mapentity_id_mapr   csvZ
DictReaderintappendr   r   longhumanproteins_idsMouseproteins_ids	human_emb	Mouse_embprintshape)r   ppin1_filenameppin2_filenameentity_emb_filerelation_emb_fileZent_idmap_fileZrel_idmap_fileZppiSource_id_DGLKE_ListZpidSource_idmap_fileflineitZp1idZp2idZppitarget_id_DGLKE_ListZpidTarget_idmap_file
entity_embentity_idmap_filerelation_idmap_fileZcsvfilereaderZrow_valZhumanproteins_Initi_idsZMouseproteins_Initi_idsZ	protHumanZ	protMouse	treatmenttreatment_ridtreatment_embsr   )r   r   __init__   s    

$



&
&


zMNAEmbedding.__init__c             C   s   | j | jfS )N)r+   r,   )r   r   r   r   get_embed_speciesq   s    zMNAEmbedding.get_embed_speciesc             C   s   | j | j| jfS )N)r#   r   r$   )r   r   r   r   !get_entity_relation_map_embrddings   s    z.MNAEmbedding.get_entity_relation_map_embrddingc             C   s   t ||d dd| _| jS )NZcosine)Znum_topZsim_measure)Zget_embedding_similaritiesalignment_matrix)r   Zspecie1Zspecie2r   r   r   get_embed_similartitiesv   s    
z$MNAEmbedding.get_embed_similartitiesc             C   s   | j S )N)r@   )r   r   r   r   get_alignment_emb{   s    zMNAEmbedding.get_alignment_emb皙?c             C   s   t |d}xtt|D ]z}xttt|| D ]`}|| | t|kr0|jdjt| jt| j	|  t| jt| j
|  t|| |  q0W qW W d Q R X d S )Nzw+z	{}	{}	{}
)r   rangelenfloat
writelinesformatr   r$   r&   r)   r*   )r   Z
resultsembpathSEED_THRESHOLDZfrkgijr   r   r   write_to_file_mna_emb}   s    z"MNAEmbedding.write_to_file_mna_embN)rC   )	__name__
__module____qualname__r=   r>   r?   rA   rB   rM   r   r   r   r   r      s   [r   )r%   Ztorch.nn.functionalnn
functionalfnnumpyr!   argparsetorchr   ZnetworkxnxtimeossyscPicklepickleImportErrorZscipy.sparser   ZMNAmaster.alignments
matplotlibr   matplotlib.pyplotpyplotpltZsklearn.utilsr   r   ZTSNEr   r   r   r   r   <module>   s&    
