forked from dauparas/ProteinMPNN
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add masking chains by CDRs, fix naming of runction chain masking #3
Open
egor4k
wants to merge
11
commits into
main
Choose a base branch
from
egorchik/CBDBIO-796
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f6a5c9e
add masking chains by CDRs, fix naming of runction chain masking
egor4k 09a81e6
delete main from model_utils
egor4k f30868c
draft of train
egor4k 8ae0a8b
train
egor4k ca338c4
CDRH3 expriments
egor4k 6ad4cf7
before refactoring
egor4k 167c372
train CDRs
egor4k d703e9a
training works
egor4k 3e3d85c
train refactor
egor4k e409a79
giving code
egor4k b7d76b3
add types, fix variable names
egor4k File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,10 @@ | |
import random | ||
import itertools | ||
|
||
def get_index_of_masks(len_seq:int,max_parts:int,max_length:int)->list[int]: | ||
from pathlib import Path | ||
from proteinlib.structure.antibody_antigen_complex import AntibodyAntigenComplex, NumberingScheme | ||
|
||
def get_mask_random(len_seq:int,max_parts:int,max_length:int)->list[int]: | ||
""" | ||
Randomly generates mask with length equal to len_seq | ||
Args: | ||
|
@@ -36,13 +39,41 @@ def get_index_of_masks(len_seq:int,max_parts:int,max_length:int)->list[int]: | |
if s>=t[0] and s<=t[1] or e>=t[0] and e<=t[1]: | ||
f=True | ||
break | ||
if not f: | ||
if not f and (s==0 or not mask[s-1]) and (e==len_seq-1 or not mask[e+1]): | ||
tuples.append((s,e)) | ||
for i in range(s,e+1): | ||
mask[i]=1 | ||
return mask | ||
|
||
def featurize(batch, device,max_parts=6,max_length=6): | ||
def get_mask_cdrs_one_chain(chain,max_parts,max_length,indexes_of_cdrs): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Если в этой функции цель - получить бинарную маску, где нули стоят на месте нужного региона, то можно намного проще: создать массив из нулей, получить положение нужного региона, заменить на месте 0 на 1 через оператор среза, без циклов. |
||
r=chain.region_boundaries | ||
cdrs_index=[(r[i],r[i+1]) for i in range(1,len(r)-1,2)] | ||
chain_len=sum([t[1]-t[0] for t in cdrs_index]) | ||
#cdrs_mask=get_mask_random(chain_len,max_parts,max_length) | ||
mask=[0]*len(chain.sequence) | ||
for i in indexes_of_cdrs: | ||
t=cdrs_index[i] | ||
for i in range(t[0],t[1]): | ||
mask[i]=1 | ||
# j=0 | ||
# for t in cdrs_index: | ||
# for i in range(t[0],t[1]): | ||
# mask[i]=cdrs_mask[j] | ||
# j+=1 | ||
return mask | ||
|
||
def get_mask_cdrs(pdb,heavy_chain_id,light_chain_id,antigen_chain_ids,max_parts,max_length,indexes_of_cdrs,numbering=NumberingScheme.CHOTHIA,): | ||
ab_complex = AntibodyAntigenComplex.from_pdb( | ||
pdb=pdb, | ||
heavy_chain_id=heavy_chain_id, | ||
light_chain_id=light_chain_id, | ||
antigen_chain_ids=antigen_chain_ids, | ||
numbering=numbering, | ||
) | ||
return get_mask_cdrs_one_chain(ab_complex.antibody.heavy_chain,max_parts,max_length,indexes_of_cdrs),get_mask_cdrs_one_chain(ab_complex.antibody.light_chain,max_parts,max_length,indexes_of_cdrs) | ||
|
||
def featurize(batch, device,max_parts=8,max_length=20,mode='train',pdb_dir=Path('/mnt/sabdab/chothia'),cdr_indexes=[0,0]): | ||
ind,jnd=cdr_indexes | ||
alphabet = 'ACDEFGHIKLMNPQRSTVWYX' | ||
B = len(batch) | ||
lengths = np.array([len(b['seq']) for b in batch], dtype=np.int32) #sum of chain seq lengths | ||
|
@@ -86,6 +117,7 @@ def featurize(batch, device,max_parts=6,max_length=6): | |
c = 1 | ||
l0 = 0 | ||
l1 = 0 | ||
#d= | ||
for step, letter in enumerate(all_chains): | ||
if letter in visible_chains: | ||
chain_seq = b[f'seq_chain_{letter}'] | ||
|
@@ -106,7 +138,22 @@ def featurize(batch, device,max_parts=6,max_length=6): | |
chain_seq = b[f'seq_chain_{letter}'] | ||
chain_length = len(chain_seq) | ||
chain_coords = b[f'coords_chain_{letter}'] #this is a dictionary | ||
chain_mask=np.array(get_index_of_masks(len(chain_seq),max_parts,max_length)) | ||
if mode=='test': | ||
chain_mask=np.array(get_mask_random(chain_length,max_parts,max_length)) | ||
elif mode=='valid': | ||
if masked_chains.index(letter)==ind: | ||
chain_mask= np.array(get_mask_cdrs(pdb_dir/f"{b['name']}.pdb",b['masked_list'][0],b['masked_list'][1],b['visible_list'],max_parts,max_length,[jnd])[ind]) | ||
else: | ||
chain_mask = np.zeros(chain_length) | ||
if chain_length!=chain_mask.shape[0]: | ||
if masked_chains.index(letter)==ind: | ||
chain_mask= np.array(get_mask_cdrs(pdb_dir/f"{b['name']}.pdb",b['masked_list'][0],b['masked_list'][1],b['visible_list'],max_parts,max_length,[jnd])[ind]) | ||
else: | ||
chain_mask = np.zeros(chain_length) | ||
|
||
assert chain_length==chain_mask.shape[0] | ||
else: | ||
chain_mask=np.array(get_mask_cdrs(pdb_dir/f"{b['name']}.pdb",b['masked_list'][0],b['masked_list'][1],b['visible_list'],max_parts,max_length,[0,1,2])[masked_chains.index(letter)]) | ||
x_chain = np.stack([chain_coords[c] for c in [f'N_chain_{letter}', f'CA_chain_{letter}', f'C_chain_{letter}', f'O_chain_{letter}']], 1) #[chain_lenght,4,3] | ||
x_chain_list.append(x_chain) | ||
chain_mask_list.append(chain_mask) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Очень сложно понять, что в этой функции происходит, можно однобуквенные переменные заменить на что-то более содержательное? под сложные условия тоже хорошо заводить булевы переменные, если это позволяет лучше понять логику