CoolFace
Modelpublic

OneScience-Group/DiffDock

sourceHugging Facemitupdated 1mo agoView on Hugging Face
0likes32downloads
aa_model.cpython-310.pyc149 linesDownload Raw Back to __pycache__
1o

2�aOjp��@s�ddlmZddlZddlmZddlmZddlmZm	Z	ddl3mZddlm
Z
ddlZddlmZmZmZdd	lmZmZd4dlmZmZd5dlmZmZzdd
lmZWneycdZYnwdd�dd�dd�dd�d�Z Gdd�dejj!�Z"dS)�)�o3N)�nn)�6functional)�radius�radius_graph)�subgraph)�scatter_mean)�lig_feature_dims�rec_residue_feature_dims�rec_atom_feature_dims)�so3�torus�)�GaussianSmearing�AtomEncoder)�
get_irrep_seq�TensorProductConvLayer)�load_model_and_alphabetcC�tj|dd�S�Nr�Zdim)�torch�mean��x�r�&modelscope/diffdock/models/aa_model.py�<lambda>�rcC�tj|dd�dS�Nrrr)r�maxrrrrr�cCrr )r�minrrrrrr"cCrr)r�stdrrrrrr)rr!r#r$cs�eZdZ									
					d/�fdd�	Zedd��Zdd�Zdd�Zdd�Zdd�Z	dd�Z7dd�Zdd �Zd!d"�Z
d#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Z�ZS)0�AAModel�� ������FT�Nrr�mean max min stdc0>s�tt|���|%r|dusJd��|-rJd��|.rJd��| dkr%|s%J�||_||_||r0dnd9}||_|8|_||_||_||_	|
|_9||_||_t
jj|d�|_|||_|_||_||_||_||_||_||_|	|_||_||_||_|	|_|)|_||_||_| |!� d�|_!|_"|$|_#|%|_$|'|_%|*|_&|,|_'|#|_(|/|_)||_*|dur�d}0n,|d	kr�d10}0n%t+dur�t,d��t+|�\}1}2|2�-�|_.t/j0�1�|1_2t/j0�1�|1_3|1j4}0|1|_5t6}3|3|t7|d�|_8t0�9t0�:||||�t0�;�t0�<|�t0�:||��|_=t0�9t0�:||�t0�;�t0�<|�t0�:||��|_>|3|t?d|0d
�|_@t0�9t0�:||�t0�;�t0�<|�t0�:||��|_A|3|tBdd�|_Ct0�9t0�:||�t0�;�t0�<|�t0�:||��|_Dt0�9t0�:|||�t0�;�t0�<|�t0�:||��|_Et0�9t0�:||�t0�;�t0�<|�t0�:||��|_Ft0�9t0�:|||�t0�;�t0�<|�t0�:||��|_GtHd|11|�|_ItHd||�|_JtHd||�|_KtL||||*�}4|&�r�Jd��g}5tM|)�D]?}6|4tN|6tO|4�d�}7|4tN|6dtO|4�d�}8tP|7|j|8d|d|d|||dk�o�||(|'�s�dndd�}9|5�Q|9��q�t0�R|5�|_S|+|_T|+�rRg}:tM|)�D]:}6|4tN|6tO|4�d�}7|4tN|6dtO|4�d�}8tP|7|j|8d|d|d|||dk�o@||(dd�}9|:�Q|9��qt0�R|:�|_Ug};tM|)|)|	�D]J}6|4tN|6tO|4�d�}7|4tN|6dtO|4�d�}8tP|7|j|8d|d|d|||dk�o�||(|'�s�dn|6|)|	dk�r�dndd�}9|;�Q|9��q[t0�R|;�|_V|j�r�|j�r�|j!dk�r�d|}<n|"d}<n|"}<|	|)dk�r�||*�r�|n|n|}=|j'�rt0�9t0�:|=|�|�s�t0�W|�nt0�1�t0�;�t0�<|�t0�:||�|�st0�W|�nt0�1�t0�;�t0�<|�t0�:||#|��	|_X|}=t0�9t0�:|=|�|�s't0�W|�nt0�1�t0�;�t0�<|�t0�:||�|�s>t0�W|�nt0�1�t0�;�t0�<|�t0�:||<��	|_Y|j!dk�r�t0�9t0�:tO|j"�||�|�sjt0�W|�nt0�1�t0�;�t0�<|�t0�:||�|�s�t0�W|�nt0�1�t0�;�t0�<|�t0�:|d��	|_ZdSdStHd|
|�|_[t0�9t0�:|||�t0�;�t0�<|�t0�:||��|_\tP|jVdj]|j|j�s�dndd|d||d�|_^t0�9t0�:d||�t0�<|�t0�;�t0�:|d��|__t0�9t0�:d||�t0�<|�t0�;�t0�:|d��|_`|�sgt0�9t0�:||�t0�;�t0�<|�t0�:||��|_at
�b|jd�|_ctP|jVdj]|jcjd|j�s5|�d|�d�n|�d�d|d||d�|_et0�9t0j:|j�sOd|n||dd�t0�f�t0�<|�t0j:|ddd��|_gdSdS)Nz(no language model emb without identitieszCsidechain prediction not implemented/makes sense for all atom modelz8depthwise convolution not implemented for all atom modelr�)Zlmax� r�precomputedizIesm is required when lm_embedding_type is not None and not 'precomputed'.)�emb_dim�feature_dims�sigma_embed_dim)r2r3r4�lm_embedding_dimr-zcurrently not supportedTr&)�	in_irreps�	sh_irreps�12out_irreps�n_edge_featuresZhidden_features�residual�13batch_norm�dropoutZfaster�tp_weights_layersZedge_groups�	�����z2x1o + 2x1ez1x1o + 1x1er(F)r6r7r8r9r:r<r;�2ezx0o + Zx0eZx0o)Zbias)h�superr%�__init__�14t_to_sigma�in_lig_edge_featuresr4�lig_max_radius�rec_max_radius�cross_max_distance�dynamic_max_cross�center_max_distance�distance_embed_dim�cross_distance_embed_dimrZIrreps�spherical_harmonicsr7�ns�nv�scale_by_sigma�
norm_by_sigma�device�15no_torsion�smooth_edges�16odd_parity�num_conv_layers�timestep_emb_func�separate_noise_schedule�confidence_mode�num_prot_emb_layers�asyncronous_noise_schedule�affinity_prediction�split�parallel�parallel_aggregators�fixed_center_conv�no_aminoacid_identities�differentiate_convolutions�reduce_pseudoscalars�atom_confidence�atom_num_confidence_outputs�crop_beyond�lm_embedding_typer�ImportErrorZget_batch_converter�batch_converterrrZIdentityZlm_headZcontact_headZ	embed_dim�lmrr	�lig_node_embeddingZ17SequentialZLinearZReLUZDropout�lig_edge_embedding�rec_sigma_embeddingr18�rec_node_embedding�rec_edge_embeddingr�atom_node_embedding�atom_edge_embedding�lr_edge_embedding�ar_edge_embedding�la_edge_embeddingr�lig_distance_expansion�rec_distance_expansion�cross_distance_expansionr�ranger#�lenr�appendZ19ModuleList�rec_emb_layers�embed_also_ligand�lig_emb_layers�conv_layersZBatchNorm1d�atom_confidence_predictor�confidence_predictor�affinity_predictor�center_distance_expansion�center_edge_embeddingr8�20final_conv�tr_final_layer�rot_final_layer�final_edge_embeddingZFullTensorProduct�final_tp_torZ21irreps_out�
tor_bond_convZTanh�tor_final_layer)>�selfrCrQrVrDr4Zsh_lmaxrMrNrUrErFrGrIrJrKrRrOrPZuse_second_order_reprr;rHr<rSrTrWrfrXZconfidence_dropoutZconfidence_no_batchnormrZr[r]r^Znum_confidence_outputsrdr_r`Zinclude_miscellaneous_atomsrar=rYrbr{rcZsidechain_predZdepthwise_convolutionrer5riZalphabetZatom_encoder_classZ	irrep_seqrz�ir6r8Zlayerr|r}Zoutput_confidence_dimZ22input_size��	__class__rrrBs�
�234,,,0,0�
�
"�
2425$2627�282930�313233��34�35�03603738�39 �	40��zAAModel.__init__cCsRt|dd�}||vr||S||vr||Sz||WSty(||YSw)N�41edge_typesr)�getattr�	Exception)�dataZprimary_keyZfallback_keyr�rrr�_resolve_edge_stores42�zAAModel._resolve_edge_storecC�|�|dd�S)N)�ligandr�)r�Zlig_bondr��r��r�r�rrr�_ligand_edge_store(�43�zAAModel._ligand_edge_storecCr�)N)�receptorr��r�Zrec_contactr�r�r�rrr�_receptor_edge_store/r�zAAModel._receptor_edge_storecCr�)N)�atomr�)r�Zatom_contactr�r�r�rrr�_atom_edge_store6r�zAAModel._atom_edge_storecCr�)N)r�r�)r�Zatom_rec_contactr�r�r�rrr�_atom_receptor_edge_store=r�z!AAModel._atom_receptor_edge_storec(sf|�|�}|�|�}|�|�}t|dd��s�|jdvr|dd�|djD��t�dt�r4dd��D��dd�t��D��|�	��\}}}|j44|�|djj
�|j45jgd	d46�}tj�fdd�t|d|j47j�D�dd
�}	tj|dj|	gdd
�|d_|�|�\}48}}}
|�|49�}50|�|�}|�|�\}}}}|�|�}|�|�}|�|�\}}}|�|�}|j��}|j��}|j��}tj|51|gdd
�}|dt|52�|d<tj|||t|53�tj|dgd�gdd
�}tj||||gdd
�}tj||||gdd
�}t�|
��r
tj|
|||gdd
�n
tjt|d�df|j
d�}t|d�t|d�t|d�t|d�t|d�t|d�}}}tt|j ��D]D} t�|||dd|j!�f||dd|j!�fgd�}!|j"�r|!d|�|!||�|!||�|!|d�g}!|j | |||!||d�}�qH|dt|54��|d_#||_$||_%|
|_&|t|55�d�|d_'||_(||_%||_&||_)||_%||_&|�*|�+|j,d��}"|dj#d}56|57dd�d|j!�f|"|dj-|58dd�d|j!�f<|j$|"|dj-|jd}|dj'd}|dd�d|j!�f|"|dj-|dd�d|j!�f<|j(|"|dj-|jd}|j)|"|dj-|jd}|�.|�\}#}$}%}&}'|�/|#�}#|�0|%�}%|j1�r�tt|j2��D]*} t�|%|#|$dd|j!�f|#|$dd|j!�fgd�}!|j2| |#|$|!|&|'d�}#�qYnt3�4|#d|59j5d|#j5df�}#|#|$|%|&|'|60|j||j%|j&||j||j%|j&|j||j%|j&fS)Nr��
rec_node_attr)Nr1cS�g|]	}|D]}|�qqSrr��.0�l�srrr�61<listcomp>J�z%AAModel.embedding.<locals>.<listcomp>rcSr�rrr�rrrr�Lr�cSsg|]\}}||f�qSrr)r�r�r�rrrr�M�F)Zrepr_layersZreturn_contactscs(g|]\}}|dt�|d���qS)Nr)rx)r�r��t��	sequencesrrr�Ps(Zrepresentationsrr?�Zdimsr�rQ��edge_weightr��tr)6r�r�r��hasattrrfZsequence�62isinstance�list�	enumeraterhri�torrQZ63num_layersr�cat�build_rec_conv_graphrmrn�build_atom_conv_graphrorp�build_cross_rec_conv_graphrr�64edge_index�clonerx�flip�	is_tensor�onesrwrzrMrar��
rec_edge_attr�edge_shr��atom_node_attr�atom_edge_attr�	edge_attrrlrV�	complex_t�batch�build_lig_conv_graphrjrkr{r|�FZpad�shape)(r�r�Zreceptor_edge_storeZatom_edge_storeZatom_receptor_edge_storeZbatch_labelsZ65batch_strsZbatch_tokens�outZ66rec_lm_embr�r��rec_edge_sh�rec_edge_weightr�r��atom_edge_sh�atom_edge_weight�ar_edge_attr�67ar_edge_sh�ar_edge_weight�rec_edge_index�atom_edge_index�
ar_edge_index�	node_attrr�r�r�r��s1�s2�s3r��68edge_attr_Z
rec_sigma_emb�
lig_node_attr�lig_edge_index�
lig_edge_attr�lig_edge_sh�lig_edge_weightrr�r�	embeddingDs�69707172$,7374757677787980*��L.�46681826� 83�zAAModel.embeddingcLs|	�jdurt��jr�djd�d_�js'�j
�fdd�d
D��\}}}n�fdd�d
D�\}}}����\}}	}84}}}
}}}}}}}}}}}}}�jrW|dd�d�n�j}���|�\}}}}} }!}"}#��|�}��|!�}!t|�t|
�}$}%tj||
|gdd�}&|��|��|��| ��|��f\}}}} }|d|$|d|$|d<|d<|d|$|%|d|$|%|d<|d<|d|$|d<| d|$|%| d<|d|$|%|d|$|d<|d<tj|	|| |tj|dgd�tj|dgd�|tj| dgd�|g	dd�}'tj|85||!|||||!|g	dd�}(tj|||"|||||"|g	dd�})t�|��r>tj|||#|||||#|g	dd�n
tjt|'d�df|'jd�}*tt�ttt|86||!|||||!|g	���� ��\	}+},}-}.}/}0}1}2}3t!t�j"��D]�}4|4t�j"�dk�r�t�|(|&|'dd�j#�f|&|'dd�j#�fgd�}5�j$�r�|5d|+�|5|+|,�|5|,|-�|5|-|.�|5|.|/�|5|/|0�|5|0|1�|5|1|2�|5|2d�g	}5�j"|4|&|'|5|)|*d�}&�qst�|(d|-�|&|'dd|-�fd�j#�f|&|'dd|-�fd�j#�fgd�}5�j$�r|5d|+�|5|+|,�|5|,|-�g}5�j"|4|&|'dd�d|-�f|5|)d|-�|*d|-�d�}&�qs|&dt|��}�j�r��j%�j&dk�rmtj|dd�d�j#�f|dd��j'�ra�j(n�j#d�fgdd�n87|dd�d�j#�f}6�j)�r���*|6�}6|6dd�d�j+�f}7|6dd��j+d�f}6ntj,t|�f|jd�}7��-t.|6�dj/dd��j0dd�}8�j1dk�r�|8dd�df|8dd�dd�f}8�|8�2�j3�j1�}8��2�j3�j1d��tj�fdd��j4D�dd����5��j0dd��|8�f}8|8|7fS�j1dk�sJ���6��\}9}:};��7|:�}:�j8�r)t�|:||9dd�j#�fgd�}:nt�|:||9dd�j#�fgd�}:�j9||9|:|;�j3d�}<|<dd�dd�f�j:�s\|<dd�dd�fnd}=|<dd�dd�f�j:�sv|<dd�dd�fnd}>�j;�r�tj��fdd�d
D�dd��_<n�j=�r���>�j?d��_<n	��>�j?d��_<tj@jA|=dd��d�}?|=|?��Btj|?�j<gdd��}=tj@jA|>dd��d�}@|>|@��Ctj|@�j<gdd��}>�jD�r�|=|�d�}=|>tE�F|�G���d��H�djj�}>�jI�s	�djJ��dk�r|=|>tjKd�jd�dfS��L��\}A}B}C}D}E�dj|Ad�dj|Ad}F||Ad||Ad}GtMjNd|Fd	dd�}H��O|D|H|Bd�}Dt�|C||Bdd�j#�f|G|Bdd�j#�fgd�}C�jP||B|C|D�djJ��d|Ed�}I��Q|I��0d�}I��R��}J|�dj/|Jj88d�djJ}K�jD�r�|It�St�TtU�F|K�G��V����W��H�djj��}I|=|>|IdfS) Nr�r�rrr(r?rr�T)Z
relabel_nodesc�g|]}�j|�qSr�r��r�Z89noise_type�r�rrr���z#AAModel.forward.<locals>.<listcomp>�r�ZrotZtorcr�rr�r�r�rrr��r�r/�r�r�r�csg|]}t|���qSr)�AGGREGATORS)r�Zagg)�affinityrrr��r�)�	out_nodes�r>csg|]90}���j|��qSr)rVr�r��r�r�rrr�sr�r�r@�	component��	normalizeZ
normalizationr)r��reducer�)Xre�NotImplementedError�posr�any�sum�	unsqueezerZside_chain_vecsrr�r`rXrCr�rHrG�build_cross_lig_conv_graphrqrsrxr�r�r�r�r�rQ�tuple�npZcumsumr��map�tolistrwr}rMrarUrYrbrNrcr~rd�zerosrrr��squeezer]Zreshape�91num_graphsr^r��build_center_conv_graphr�r_r�rTrWZgraph_sigma_embrZrVr�ZlinalgZvector_normr�r�rOrZ92score_normZcpur�rR�	edge_mask�empty�build_bond_conv_graphrrLr�r�r�r�ZsqrtZtensorr
�numpy�float)Lr�r�Z93ligand_posZreceptor_posZresidues_to_keepZtr_sigmaZ	rot_sigmaZ	tor_sigmar�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�Zcross_cutoff�
lr_edge_index�lr_edge_attr�94lr_edge_sh�lr_edge_weight�
la_edge_index�la_edge_attr�95la_edge_sh�la_edge_weightZn_ligZn_recr�r�r�r�r�r�r�r�Zs4Zs5Zs6Zs7Zs8�_r�r�Zscalar_lig_attrrcZ96confidenceZcenter_edge_indexZcenter_edge_attrZcenter_edge_shZglobal_predZtr_predZrot_predZtr_normZrot_normZ	tor_bondsZtor_edge_indexZ
tor_edge_attrZtor_edge_shZtor_edge_weightZtor_bond_vecZ
tor_bond_attrZtor_bonds_shZtor_pred�ligand_edge_storeZ97edge_sigmar)r�r�r�r�forward�s9899 �100101�102103,"*&�������������602�N*<F��104"&105$"44"""&4$���106" �zAAModel.forwardcCsD|jr tj|jdd�tj|tjd�}dt�|�d�d�SdS)Nr?r)r!g�?g�?)rSrZclip�normr�ZpiZcosr�)r��edge_vecZmax_normZnormalised_normrrr�get_edge_weight3s"zAAModel.get_edge_weightcs6�jrtj��fdd�dD�dd��d_n�jr'���djd��d_n
���djd��d_�jdkrGt�dj	�j107�dj�}nMtj�j
�djjd	������d108�djtjt�dj��djjd	������j
}���fdd�t|�D�}t�|��d_t�dj	�j109�dj�}����}t�|j|gd���}t�|jtj|jd�j�djjd	�gd110�}�dj|d111��}t�||gd�}t��dj�djgd�}	|\}112}�dj	|���dj	|113��}��|jdd��}
t�||
gd�}tj�j|d
dd�}�� |�j114�}|	||||fS)Ncs g|]}���dj|��qS)r�)rV�node_tr�r�rrr�=s z0AAModel.build_lig_conv_graph.<locals>.<listcomp>r�rrr�r�r�r�rcsRg|]%}t�j�D]}tj�|���j�djjd����j||�q	qS)r�r�)rwr]rr��itemrrQ�long)r�r��j�Zbatchesr�r�rrr�Ks115116�<�r?Tr�r�)!rWrr��node_sigma_embrZrVrr]rr�rEr�r�r�rrQrZ	index_addr�rxrwZbatch_parallelr�r�r�r�rDrtrrrLr7r)r�r�Zradius_edgesZ
outer_batches�br117r�r��edge_sigma_embr��src�dstr
Zedge_length_embr�r�rrrr�9sL�1181194�120��$zAAModel.build_lig_conv_graphc121C�|dj}|�|�j}|\}}|dj|��|dj|��}|�|jdd��}tj|j	|ddd�}|�122||j�}	||||	fS)Nr�r?rTr�r�)rr�r�r�rrurrrLr7rrF�123r�r�r�r�rrr
r�r�r�rrrr�d�124$zAAModel.build_rec_conv_graphc125Cr)Nr�r?rTr�r�)rr�r�r�rrtrrrLr7rrErrrrr�srzAAModel.build_atom_conv_graphcCs�t�|�r*t|dj||dj|dj||djd|dj|djdd�}nt|dj|dj||dj|djdd�}|dj|d��|dj|d��}|�|jdd��}|dj|d��}t�	||gd�}t126j|j|d	d127d�}t�|�r�||dj|d�
�n|}	|�||	�}128t|dj|dj|j|dj|djdd�}|dj|d��|dj|d��}|�|jdd��}
|dj|d��}t�	||
gd�}t129j|j|d	d130d�}|�||j�}||||131||||fS)
Nr�r�ri')Zmax_num_neighborsrr?rTr�r�r�)rr�rr�r�rrvrrr�rrLr7r�rrErt)r�r�Zlr_cross_distance_cutoffrZlr_edge_vecZlr_edge_length_embZlr_edge_sigma_embrrZcutoff_drrZla_edge_vecZla_edge_length_embZla_edge_sigma_embrrrrrrr��s<132��,���,�z"AAModel.build_cross_lig_conv_graphcCsl|�|�j}|dj|d��|dj|d��}|�|jdd��}tj|j|ddd	�}d}|||fS)133Nr�rr�rr?rTr�r�)	r�r�r�rrurrrLr7)r�r�r�Zar_edge_vecr�r�r�rrrr��s,134z"AAModel.build_cross_rec_conv_graphc	Cs*tj|dj�d�t�t|dj���|djj��d�gdd�}t�	|j135df��|djj�t�	|j136df��|djj�}}|jd|dj|djd�|t�
|dj��d�}|dj|d||d}|�|jdd��}|dj|d��}t�||gd�}tj|j|dd	d137�}|||fS)Nr�rrr/)�index�sourcerr?Tr�r�)rr�r�r�Zarangerxr�rrQr�r�Z138index_add_r�Zbincountr�rrrrrLr7)	r�r�r�Z139center_pos�countr
r�rr�rrrr��sB>140zAAModel.build_center_conv_graphc141Cs�|�|�jdd�|djf��}|dj|d|dj|dd}|dj|d}t|dj||j|dj|d�}|dj|d||d}|�|j	dd��}|�142|�}tj|j
|dd	d143�}|�||j�}	|||||	fS)Nr�rrr()Zbatch_xZbatch_yr?rTr�r�)r�r�r�rr�r�rrErtrr�rrLr7r)144r�r�ZbondsZbond_posZ145bond_batchr�r
r�r�r�rrrr��s"( 146zAAModel.build_bond_conv_graph),r&r'r(r)r&r(r*r+r,r+r'r'FTTFTFr-FFFNFrFFFrr.rrFFFTr(rFFFFFN)�__name__�147__module__�__qualname__rB�staticmethodr�r�r�r�r�r�rrr�r�r�r�r�r�r��
__classcell__rrr�rr%sD�148\+%r%)#Ze3nnrrrZtorch.nnrr�Z
torch_clusterrrZtorch_geometric.utilsrZ
torch_scatterrr�r�Z*onescience.datapipes.diffdock.process_molsr	r149rZonescience.utils.diffdockrr
ZlayersrrZ
tensor_layersrrZesm.pretrainedrrgr�ZModuler%rrrr�<module>s.��