| import os |
| import numpy as np |
| import pandas as pd |
| import torch |
| import json |
|
|
| import sys |
| |
| project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| sys.path.insert(0, project_root) |
|
|
| import pdbutils |
| import cifutils |
| from na_data_utils import PDBDataset |
|
|
| |
| params_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "preprocess_dataset.json") |
| params = json.load(open(params_path)) |
|
|
| |
| na_side_chain_atoms_len = len(['N9', 'C8', 'C7', 'N7', 'C6', 'N6', 'O6', 'C5', 'C4', 'N4', 'O4', 'N3', 'C2', 'N2', 'O2', 'N1']) |
| residue_cutoff = params["BATCH_TOKENS"] |
| num_neighbors = params["NUM_NEIGHBORS"] |
| interface_distance_cutoff = 5.0 |
|
|
| if params["ATOMS_TO_LOAD"] == "backbone": |
| atom_list_to_save = ['N', 'CA', 'C', 'O', |
| 'OP1', 'OP2', 'P', "O5'", "C5'", "C4'", "O4'", "C3'", "O3'", "C2'", "O2'", "C1'" |
| ] |
| elif params["ATOMS_TO_LOAD"] == "all": |
| atom_list_to_save = ['N', 'CA', 'C', 'CB', 'O', 'CG', 'CG1', 'CG2', 'OG', 'OG1', 'SG', 'CD', 'CD1', 'CD2', 'ND1', 'ND2', 'OD1', 'OD2', 'SD', 'CE', 'CE1', 'CE2', 'CE3', 'NE', 'NE1', 'NE2', 'OE1', 'OE2', 'CH2', 'NH1', 'NH2', 'OH', 'CZ', 'CZ2', 'CZ3', 'NZ', 'OXT', |
| 'OP1', 'OP2', 'P', "O5'", "C5'", "C4'", "O4'", "C3'", "O3'", "C2'", "O2'", "C1'", 'N9', 'C8', 'C7', 'N7', 'C6', 'N6', 'O6', 'C5', 'C4', 'N4', 'O4', 'N3', 'C2', 'N2', 'O2', 'N1' |
| ] |
|
|
| |
| cif_parser = cifutils.CIFParser(skip_res=params["EXCLUDE_RES"], |
| randomize_nmr_model=params["RANDOMIZE_NMR_MODEL"]) |
| pdb_parser = pdbutils.PDBParser() |
|
|
| pdb_dataset = PDBDataset(cif_parser=cif_parser, |
| pdb_parser=pdb_parser, |
| atom_list_to_save=atom_list_to_save, |
| parse_protein=params["PARSE_PROTEIN"], |
| parse_dna=params["PARSE_DNA"], |
| parse_rna=params["PARSE_RNA"], |
| parse_rna_as_dna=params["PARSE_RNA_AS_DNA"], |
| na_shared_tokens=params["NA_SHARED_TOKENS"], |
| protein_backbone_occ_cutoff=params["PROTEIN_BACKBONE_OCC_CUTOFF"], |
| protein_side_chain_occ_cutoff=params["PROTEIN_SIDE_CHAIN_OCC_CUTOFF"], |
| dna_backbone_occ_cutoff=params["DNA_BACKBONE_OCC_CUTOFF"], |
| dna_side_chain_occ_cutoff=params["DNA_SIDE_CHAIN_OCC_CUTOFF"], |
| rna_backbone_occ_cutoff=params["RNA_BACKBONE_OCC_CUTOFF"], |
| rna_side_chain_occ_cutoff=params["RNA_SIDE_CHAIN_OCC_CUTOFF"], |
| crop_large_structures=params["CROP_LARGE_STRUCTURES"], |
| batch_tokens=params["BATCH_TOKENS"], |
| na_ref_atom=params["NA_REF_ATOM"] |
| ) |
|
|
| |
| side_chain_mask = np.zeros(len(pdb_dataset.atom_dict), dtype = np.int32) |
| for atom_name in pdb_dataset.atom_dict: |
| if (atom_name not in pdb_dataset.protein_backbone_list) and \ |
| (atom_name not in pdb_dataset.dna_backbone_list) and \ |
| (atom_name not in pdb_dataset.rna_backbone_list): |
| side_chain_mask[pdb_dataset.atom_dict[atom_name]] = 1 |
|
|
| side_chain_pairwise_mask = side_chain_mask[:, None] * side_chain_mask[None, :] |
|
|
| def write_text_file(path, contents): |
| with open(path, mode = "wt") as f: |
| f.write(contents) |
|
|
| class HB_data: |
| |
|
|
| |
| num2aa=[ |
| 'ALA','ARG','ASN','ASP','CYS', |
| 'GLN','GLU','GLY','HIS','ILE', |
| 'LEU','LYS','MET','PHE','PRO', |
| 'SER','THR','TRP','TYR','VAL', |
| 'UNK','MAS', |
| ' DA',' DC',' DG',' DT', ' DX', |
| ' RA',' RC',' RG',' RU', ' RX', |
| 'HIS_D', |
| 'Al', 'As', 'Au', 'B', |
| 'Be', 'Br', 'C', 'Ca', 'Cl', |
| 'Co', 'Cr', 'Cu', 'F', 'Fe', |
| 'Hg', 'I', 'Ir', 'K', 'Li', 'Mg', |
| 'Mn', 'Mo', 'N', 'Ni', 'O', |
| 'Os', 'P', 'Pb', 'Pd', 'Pr', |
| 'Pt', 'Re', 'Rh', 'Ru', 'S', |
| 'Sb', 'Se', 'Si', 'Sn', 'Tb', |
| 'Te', 'U', 'W', 'V', 'Y', 'Zn', |
| 'ATM' |
| ] |
| aa2num= {x:i for i,x in enumerate(num2aa)} |
| aa2num['MEN'] = 20 |
| aa2num_stripped = {x.strip():i for i,x in enumerate(num2aa)} |
| aa2num_stripped['MEN'] = 20 |
| |
| |
| NTOTAL = 36 |
| aa2long=[ |
| (" N "," CA "," C "," O "," CB ", None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None," H "," HA ","1HB ","2HB ","3HB ", None, None, None, None, None, None, None, None), |
| (" N "," CA "," C "," O "," CB "," CG "," CD "," NE "," CZ "," NH1"," NH2", None, None, None, None, None, None, None, None, None, None, None, None," H "," HA ","1HB ","2HB ","1HG ","2HG ","1HD ","2HD "," HE ","1HH1","2HH1","1HH2","2HH2"), |
| (" N "," CA "," C "," O "," CB "," CG "," OD1"," ND2", None, None, None, None, None, None, None, None, None, None, None, None, None, None, None," H "," HA ","1HB ","2HB ","1HD2","2HD2", None, None, None, None, None, None, None), |
| (" N "," CA "," C "," O "," CB "," CG "," OD1"," OD2", None, None, None, None, None, None, None, None, None, None, None, None, None, None, None," H "," HA ","1HB ","2HB ", None, None, None, None, None, None, None, None, None), |
| (" N "," CA "," C "," O "," CB "," SG ", None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None," H "," HA ","1HB ","2HB "," HG ", None, None, None, None, None, None, None, None), |
| (" N "," CA "," C "," O "," CB "," CG "," CD "," OE1"," NE2", None, None, None, None, None, None, None, None, None, None, None, None, None, None," H "," HA ","1HB ","2HB ","1HG ","2HG ","1HE2","2HE2", None, None, None, None, None), |
| (" N "," CA "," C "," O "," CB "," CG "," CD "," OE1"," OE2", None, None, None, None, None, None, None, None, None, None, None, None, None, None," H "," HA ","1HB ","2HB ","1HG ","2HG ", None, None, None, None, None, None, None), |
| (" N "," CA "," C "," O ", None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None," H ","1HA ","2HA ", None, None, None, None, None, None, None, None, None, None), |
| (" N "," CA "," C "," O "," CB "," CG "," ND1"," CD2"," CE1"," NE2", None, None, None, None, None, None, None, None, None, None, None, None, None," H "," HA ","1HB ","2HB ","2HD ","1HE ","2HE ", None, None, None, None, None, None), |
| (" N "," CA "," C "," O "," CB "," CG1"," CG2"," CD1", None, None, None, None, None, None, None, None, None, None, None, None, None, None, None," H "," HA "," HB ","1HG2","2HG2","3HG2","1HG1","2HG1","1HD1","2HD1","3HD1", None, None), |
| (" N "," CA "," C "," O "," CB "," CG "," CD1"," CD2", None, None, None, None, None, None, None, None, None, None, None, None, None, None, None," H "," HA ","1HB ","2HB "," HG ","1HD1","2HD1","3HD1","1HD2","2HD2","3HD2", None, None), |
| (" N "," CA "," C "," O "," CB "," CG "," CD "," CE "," NZ ", None, None, None, None, None, None, None, None, None, None, None, None, None, None," H "," HA ","1HB ","2HB ","1HG ","2HG ","1HD ","2HD ","1HE ","2HE ","1HZ ","2HZ ","3HZ "), |
| (" N "," CA "," C "," O "," CB "," CG "," SD "," CE ", None, None, None, None, None, None, None, None, None, None, None, None, None, None, None," H "," HA ","1HB ","2HB ","1HG ","2HG ","1HE ","2HE ","3HE ", None, None, None, None), |
| (" N "," CA "," C "," O "," CB "," CG "," CD1"," CD2"," CE1"," CE2"," CZ ", None, None, None, None, None, None, None, None, None, None, None, None," H "," HA ","1HB ","2HB ","1HD ","2HD ","1HE ","2HE "," HZ ", None, None, None, None), |
| (" N "," CA "," C "," O "," CB "," CG "," CD ", None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None," HA ","1HB ","2HB ","1HG ","2HG ","1HD ","2HD ", None, None, None, None, None, None), |
| (" N "," CA "," C "," O "," CB "," OG ", None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None," H "," HG "," HA ","1HB ","2HB ", None, None, None, None, None, None, None, None), |
| (" N "," CA "," C "," O "," CB "," OG1"," CG2", None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None," H "," HG1"," HA "," HB ","1HG2","2HG2","3HG2", None, None, None, None, None, None), |
| (" N "," CA "," C "," O "," CB "," CG "," CD1"," CD2"," NE1"," CE2"," CE3"," CZ2"," CZ3"," CH2", None, None, None, None, None, None, None, None, None," H "," HA ","1HB ","2HB ","1HD ","1HE "," HZ2"," HH2"," HZ3"," HE3", None, None, None), |
| (" N "," CA "," C "," O "," CB "," CG "," CD1"," CD2"," CE1"," CE2"," CZ "," OH ", None, None, None, None, None, None, None, None, None, None, None," H "," HA ","1HB ","2HB ","1HD ","1HE ","2HE ","2HD "," HH ", None, None, None, None), |
| (" N "," CA "," C "," O "," CB "," CG1"," CG2", None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None," H "," HA "," HB ","1HG1","2HG1","3HG1","1HG2","2HG2","3HG2", None, None, None, None), |
| (" N "," CA "," C "," O "," CB ", None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None," H "," HA ","1HB ","2HB ","3HB ", None, None, None, None, None, None, None, None), |
| (" N "," CA "," C "," O "," CB ", None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None," H "," HA ","1HB ","2HB ","3HB ", None, None, None, None, None, None, None, None), |
|
|
| (" O4'"," C1'"," C2'"," OP1"," P "," OP2"," O5'"," C5'"," C4'"," C3'"," O3'"," N9 "," C4 "," N3 "," C2 "," N1 "," C6 "," C5 "," N7 "," C8 "," N6 ", None, None,"H5''"," H5'"," H4'"," H3'","H2''"," H2'"," H1'"," H2 "," H61"," H62"," H8 ", None, None), |
| (" O4'"," C1'"," C2'"," OP1"," P "," OP2"," O5'"," C5'"," C4'"," C3'"," O3'"," N1 "," C2 "," O2 "," N3 "," C4 "," N4 "," C5 "," C6 ", None, None, None, None,"H5''"," H5'"," H4'"," H3'","H2''"," H2'"," H1'"," H42"," H41"," H5 "," H6 ", None, None), |
| (" O4'"," C1'"," C2'"," OP1"," P "," OP2"," O5'"," C5'"," C4'"," C3'"," O3'"," N9 "," C4 "," N3 "," C2 "," N1 "," C6 "," C5 "," N7 "," C8 "," N2 "," O6 ", None,"H5''"," H5'"," H4'"," H3'","H2''"," H2'"," H1'"," H1 "," H22"," H21"," H8 ", None, None), |
| (" O4'"," C1'"," C2'"," OP1"," P "," OP2"," O5'"," C5'"," C4'"," C3'"," O3'"," N1 "," C2 "," O2 "," N3 "," C4 "," O4 "," C5 "," C7 "," C6 ", None, None, None,"H5''"," H5'"," H4'"," H3'","H2''"," H2'"," H1'"," H3 "," H71"," H72"," H73"," H6 ", None), |
| (" O4'"," C1'"," C2'"," OP1"," P "," OP2"," O5'"," C5'"," C4'"," C3'"," O3'", None, None, None, None, None, None, None, None, None, None, None, None,"H5''"," H5'"," H4'"," H3'","H2''"," H2'"," H1'", None, None, None, None, None, None), |
| (" O4'"," C1'"," C2'"," OP1"," P "," OP2"," O5'"," C5'"," C4'"," C3'"," O3'"," O2'"," N1 "," C2 "," N3 "," C4 "," C5 "," C6 "," N6 "," N7 "," C8 "," N9 ", None," H5'","H5''"," H4'"," H3'"," H2'","HO2'"," H1'"," H2 "," H61"," H62"," H8 ", None, None), |
| (" O4'"," C1'"," C2'"," OP1"," P "," OP2"," O5'"," C5'"," C4'"," C3'"," O3'"," O2'"," N1 "," C2 "," O2 "," N3 "," C4 "," N4 "," C5 "," C6 ", None, None, None," H5'","H5''"," H4'"," H3'"," H2'","HO2'"," H1'"," H42"," H41"," H5 "," H6 ", None, None), |
| (" O4'"," C1'"," C2'"," OP1"," P "," OP2"," O5'"," C5'"," C4'"," C3'"," O3'"," O2'"," N1 "," C2 "," N2 "," N3 "," C4 "," C5 "," C6 "," O6 "," N7 "," C8 "," N9 "," H5'","H5''"," H4'"," H3'"," H2'","HO2'"," H1'"," H1 "," H22"," H21"," H8 ", None, None), |
| (" O4'"," C1'"," C2'"," OP1"," P "," OP2"," O5'"," C5'"," C4'"," C3'"," O3'"," O2'"," N1 "," C2 "," O2 "," N3 "," C4 "," O4 "," C5 "," C6 ", None, None, None," H5'","H5''"," H4'"," H3'"," H2'","HO2'"," H1'"," H3 "," H5 "," H6 ", None, None, None), |
| (" O4'"," C1'"," C2'"," OP1"," P "," OP2"," O5'"," C5'"," C4'"," C3'"," O3'"," O2'", None, None, None, None, None, None, None, None, None, None, None," H5'","H5''"," H4'"," H3'"," H2'","HO2'"," H1'", None, None, None, None, None, None), |
|
|
| (" N "," CA "," C "," O "," CB "," CG "," NE2"," CD2"," CE1"," ND1", None, None, None, None, None, None, None, None, None, None, None, None, None," H "," HA ","1HB ","2HB ","2HD ","1HE ","1HD ", None, None, None, None, None, None), |
| ] |
| aa2long_stripped = [] |
| for aa_tuple in aa2long: |
| aa_tuple_stripped = tuple(map(lambda atom_name: atom_name.strip() if atom_name is not None else atom_name, aa_tuple)) |
| aa2long_stripped.append(aa_tuple_stripped) |
|
|
| def __init__(self, seq, xyz, idx=None, **kwargs): |
| |
| self.seq = seq |
| self.xyz = xyz |
| |
| if not idx: |
| self.idx = torch.arange(len(seq)) |
|
|
| |
| self.incl_protein = kwargs.get('incl_protein', True) |
| self.eps = kwargs.get('eps', 1e-8) |
| |
| |
| self.edges_to_compute = kwargs.get('edges_to_compute', ['S']) |
| self.perp_base_edge = kwargs.get('perp_base_edge', 'S') |
|
|
| self.hbond_da_upper = kwargs.get('hbond_da_upper', 3.9) |
| self.hbond_ha_upper = kwargs.get('hbond_ha_upper', 2.5) |
|
|
| self.seq_cutoff = kwargs.get('seq_cutoff', 2) |
| |
| compute_local_base_params = kwargs.get('compute_local_base_params', False) |
| compute_pairwise_base_params = kwargs.get('compute_pairwise_base_params', False) |
| compute_paired_bases = kwargs.get('compute_paired_bases', False) |
| compute_helical_params = kwargs.get('compute_helical_params', False) |
|
|
|
|
| |
| self.base_geometry_limits = {} |
| self.base_geometry_limits['D_ij'] = kwargs.get('D_ij_limit', 20.0) |
| self.base_geometry_limits['H_ij'] = kwargs.get('H_ij_limit', 1.5) |
| self.base_geometry_limits['P_ij'] = kwargs.get('P_ij_limit', np.pi/5) |
| self.base_geometry_limits['B_ij'] = kwargs.get('B_ij_limit', np.pi/5) |
| |
| |
| self.bp_val_cutoff= kwargs.get('bp_val_cutoff', 0.5) |
| |
| |
| |
| self.hbond_same_coeff = kwargs.get('hbond_same_coeff', 0.0) |
| self.hbond_diff_coeff = kwargs.get('hbond_diff_coeff', 1.0) |
| self.min_hbonds_for_bp = kwargs.get('min_hbonds_for_bp', 2.0) |
| self.bp_hbond_coeff = kwargs.get('bp_hbond_coeff', 8.0) |
|
|
| self.clamp_pairwise_params = kwargs.get('clamp_pairwise_params', False) |
| |
| |
| self._init_hb_chemdata() |
| self._compute_initial_values() |
| self._compute_hbnets(store_hb_data_dict=kwargs.get('store_hb_data_dict', False)) |
| |
| |
| if self.is_na.sum() > 0: |
| self._init_nuc_chemdata() |
| self._edges_to_compute = list(set(self.edges_to_compute) | {self.perp_base_edge}) |
| self._compute_local_base_params() |
| |
| if compute_pairwise_base_params or compute_paired_bases: |
| self._compute_pairwise_base_params() |
| self._compute_paired_bases() |
| |
| if compute_helical_params: |
| self._compute_helical_params() |
|
|
| def _compute_initial_values(self): |
| self.len_s = int(self.seq.shape[0]) |
| self.sel = torch.arange(self.len_s) |
| self.seq_neighbors = torch.le(torch.abs(self.sel[:, None] - self.sel[None, :]), self.seq_cutoff) |
| self.is_protein = torch.logical_and((0 <= self.seq), (self.seq <= 21)) |
| self.is_dna = torch.logical_and((22 <= self.seq), (self.seq <= 25)) |
| self.is_rna = torch.logical_and((27 <= self.seq), (self.seq <= 30)) |
| self.is_na = torch.logical_or(self.is_dna, self.is_rna) |
|
|
| self.na_inds = [i for i,is_na_i in enumerate(self.is_na) if is_na_i] |
| self.na_tensor_inds = {na_i:i for i,na_i in enumerate(self.na_inds)} |
| |
| frame_xyz = self.xyz[:,1,:] |
| padded_centers = torch.cat([frame_xyz[:1], frame_xyz[:], frame_xyz[-1:]]) |
| |
| self.D_ij_vec = frame_xyz.unsqueeze(0) - frame_xyz.unsqueeze(1) |
| self.D_ij = self.D_ij_vec.norm(dim=-1) |
| self.M_i = ((padded_centers[1:-1] - padded_centers[:-2]) + (padded_centers[2:] - padded_centers[1:-1])) / 2 |
| self.M_i_doublet = padded_centers[1:] - padded_centers[:-1] |
| |
| def _compute_hbnets(self, store_hb_data_dict=False): |
| |
| |
| D_ij_filter = (self.D_ij <= self.base_geometry_limits['D_ij']) |
| |
| |
| neighbor_inds = torch.triu(D_ij_filter.bool(),diagonal=1).nonzero(as_tuple=True) |
| |
| pairwise_indices = list(zip(neighbor_inds[0].tolist(), neighbor_inds[1].tolist())) |
| bp_pred_summation = torch.zeros_like(self.D_ij) |
|
|
| |
| hb_data_dict = {i:{j:{} for j in range(self.len_s)} for i in range(self.len_s) } |
|
|
|
|
| self.hbond_summation = torch.zeros_like(D_ij_filter, dtype=torch.float) |
|
|
| for i,j in pairwise_indices: |
| for a_i, is_donor_i in zip(self.hbond_atoms[HB_data.num2aa[self.seq[i]]]['names'],self.hbond_atoms[HB_data.num2aa[self.seq[i]]]['donor']): |
| for a_j, is_donor_j in zip(self.hbond_atoms[HB_data.num2aa[self.seq[j]]]['names'],self.hbond_atoms[HB_data.num2aa[self.seq[j]]]['donor']): |
| atom_pair = f"{a_i}-{a_j}" |
| if (is_donor_i+is_donor_j)==1 and (atom_pair not in hb_data_dict[i][j].keys()): |
| |
|
|
| a_i_ind = HB_data.aa2long[self.seq[i]].index(a_i) |
| a_j_ind = HB_data.aa2long[self.seq[j]].index(a_j) |
|
|
| |
| d_ijk_vec = self.xyz[i,a_i_ind] - self.xyz[j,a_j_ind] |
| d_ijk_vec_norm = d_ijk_vec/d_ijk_vec.norm(dim=-1) |
|
|
| |
| a_i_vec = torch.cat( |
| [(self.xyz[i,a_i_ind]-self.xyz[i,HB_data.aa2long[self.seq[i]].index(r_i)])[:,None] for r_i in self.rear_atoms[HB_data.num2aa[self.seq[i]]][a_i]], |
| dim=1).mean(dim=1) |
| a_i_vec_norm = a_i_vec/(a_i_vec.norm(dim=-1) + self.eps) |
|
|
| a_j_vec = torch.cat( |
| [(self.xyz[j,a_j_ind]-self.xyz[j,HB_data.aa2long[self.seq[j]].index(r_j)])[:,None] for r_j in self.rear_atoms[HB_data.num2aa[self.seq[j]]][a_j]], |
| dim=1).mean(dim=1) |
| a_j_vec_norm = a_j_vec/(a_j_vec.norm(dim=-1) + self.eps) |
|
|
|
|
| num_rear_i = len(self.rear_atoms[HB_data.num2aa[self.seq[i]]][a_i]) |
| element_i = ''.join([_ for _ in a_i if _.isalpha()])[0] |
| ideal_angle_i = self.ideal_angle_dict[element_i][num_rear_i] |
|
|
| num_rear_j = len(self.rear_atoms[HB_data.num2aa[self.seq[j]]][a_j]) |
| element_j = ''.join([_ for _ in a_j if _.isalpha()])[0] |
| ideal_angle_j = self.ideal_angle_dict[element_j][num_rear_j] |
|
|
| ideal_angle_h = torch.tensor((is_donor_i*ideal_angle_i) + (is_donor_j*ideal_angle_j)) |
|
|
| xyz_d_ijk = ( is_donor_i * self.xyz[i,a_i_ind] ) + ( is_donor_j * self.xyz[j,a_j_ind] ) |
| xyz_a_ijk = ((1-is_donor_i) * self.xyz[i,a_i_ind] ) + ((1-is_donor_j) * self.xyz[j,a_j_ind] ) |
|
|
| |
| rd_ijk_vec = (is_donor_i * a_i_vec_norm) + (is_donor_j * a_j_vec_norm) |
| rd_ijk_vec_norm = rd_ijk_vec/(rd_ijk_vec.norm(dim=-1) + self.eps) |
|
|
| |
| da_ijk_vec = xyz_a_ijk - xyz_d_ijk |
| da_ijk_vec_norm = da_ijk_vec/(da_ijk_vec.norm(dim=-1) + self.eps) |
|
|
| |
| ar_ijk_vec = ((is_donor_i-1)*a_i_vec_norm) + ((is_donor_j-1)*a_j_vec_norm) |
| ar_ijk_vec_norm = ar_ijk_vec/(ar_ijk_vec.norm(dim=-1) + self.eps) |
|
|
|
|
| norm_vec = torch.cross(-rd_ijk_vec_norm, da_ijk_vec_norm, dim=-1) |
| norm_unit = norm_vec / (norm_vec.norm() + self.eps) |
| perp_vec = torch.cross(norm_unit, -rd_ijk_vec_norm, dim=-1) |
| perp_unit = perp_vec / (perp_vec.norm() + self.eps) |
|
|
|
|
| |
| dh_ijk_vec = (torch.sin(ideal_angle_h) * perp_unit) - (torch.cos(ideal_angle_h) * rd_ijk_vec_norm) |
| dh_ijk_vec_norm = dh_ijk_vec / (dh_ijk_vec.norm() + self.eps) |
| ideal_xyz_h_ijk = xyz_d_ijk + dh_ijk_vec_norm |
|
|
| |
| ha_ijk_vec = xyz_a_ijk - ideal_xyz_h_ijk |
| ha_ijk_vec_norm = ha_ijk_vec / (ha_ijk_vec.norm() + self.eps) |
|
|
|
|
| t_rdh = torch.acos( ( -rd_ijk_vec_norm * dh_ijk_vec_norm ).sum(dim=-1) ) |
| t_rda = torch.acos( ( -rd_ijk_vec_norm * da_ijk_vec_norm ).sum(dim=-1) ) |
| t_dha = torch.acos( ( -dh_ijk_vec_norm * ha_ijk_vec_norm ).sum(dim=-1) ) |
| t_dar = torch.acos( ( -da_ijk_vec_norm * ar_ijk_vec_norm ).sum(dim=-1) ) |
| t_har = torch.acos( ( -ha_ijk_vec_norm * ar_ijk_vec_norm ).sum(dim=-1) ) |
|
|
|
|
| da_ijk = da_ijk_vec.norm(dim=-1) |
| ha_ijk = ha_ijk_vec.norm(dim=-1) |
|
|
| hbond_da_filter = ( da_ijk <= self.hbond_da_upper ) |
| hbond_ha_filter = ( ha_ijk <= self.hbond_ha_upper ) |
| |
| hbond_t_rda_filter = ( t_rda >= 5*np.pi/9 ) |
| hbond_t_dar_filter = ( t_dar >= 5*np.pi/9 ) |
| hbond_t_dha_filter = ( t_dha >= np.pi/2 ) |
| |
| bond_prob_ij = (hbond_ha_filter * hbond_da_filter * hbond_t_rda_filter * hbond_t_dar_filter).float() |
|
|
| self.hbond_summation[i,j] += bond_prob_ij |
| self.hbond_summation[j,i] += bond_prob_ij |
| |
| hb_data_dict[i][j][atom_pair] = {'d': da_ijk, 'l': ha_ijk, "t_rdh": t_rdh, "t_rda": t_rda, "t_dha": t_dha, "t_dar": t_dar, "t_har": t_har, 'atoms': atom_pair, "bonded": bond_prob_ij, } |
| hb_data_dict[j][i][atom_pair] = {'d': da_ijk, 'l': ha_ijk, "t_rdh": t_rdh, "t_rda": t_rda, "t_dha": t_dha, "t_dar": t_dar, "t_har": t_har, 'atoms': atom_pair, "bonded": bond_prob_ij, } |
|
|
| if store_hb_data_dict: |
| self.hb_data_dict = hb_data_dict |
| |
| def _compute_local_base_params(self): |
| """ |
| local base params , based on interaction-edges |
| |
| """ |
| xyz_na = self.xyz[self.is_na] |
| seq_na = self.seq[self.is_na] |
| |
|
|
| """ |
| (1). Compute base normals and correct orientation based on backbone direction. |
| """ |
| base_atom_xyz = torch.stack([xyz_na[i,self.ring_atom_inds[HB_data.num2aa[s_i]],:] for i,s_i in enumerate(seq_na)] ) |
| base_atom_centers = torch.mean(base_atom_xyz, dim=1) |
| |
| centered_points = base_atom_xyz - base_atom_centers.unsqueeze(1) |
| cov_matrix = torch.einsum('bij,bik->bjk', centered_points, centered_points) / (centered_points.shape[1] - 1) |
| eigenvalues, eigenvectors = torch.linalg.eigh(cov_matrix) |
| |
| |
| N_i = eigenvectors[:, :, 0] / eigenvectors[:, :, 0].norm(dim=1, keepdim=True) |
|
|
| |
| self.Z_i = N_i * torch.sum(self.M_i[self.is_na] * N_i, dim=-1, keepdim=True) |
| self.Z_i = self.Z_i / (torch.norm(self.Z_i, dim=-1, keepdim=True) + self.eps) |
| |
| """ |
| (2). Compute the desired edge-vectors for the bases (watson-crick, hoogstein, sugar, etc) |
| * W edge: N1 of purine, N3 of pyrimidine |
| * H edge: N7 of purine, C5 of pyrimidine |
| * S edge: N3 of purine, C1' of pyrimidine |
| * B (pseudo)-edge: connects C1' to first base-atom (N1 or N3?) |
| """ |
| |
| self.edge_X_vecs, self.edge_Y_vecs = {}, {} |
| for edge in self.edges_to_compute: |
| self.edge_X_vecs[edge] = torch.stack([xyz_na[i,self.vec_atom_inds[HB_data.num2aa[s_i]][f'{edge}_stop'],:] - xyz_na[i,self.vec_atom_inds[HB_data.num2aa[s_i]][f'{edge}_start'],:] for i, s_i in enumerate(seq_na)]) |
| self.edge_X_vecs[edge] = self.edge_X_vecs[edge] / (torch.norm(self.edge_X_vecs[edge], dim=-1, keepdim=True) + self.eps) |
| |
| |
| self.edge_Y_vecs[edge] = torch.cross(self.edge_X_vecs[edge], self.Z_i, dim=-1) |
| self.edge_Y_vecs[edge] = self.edge_Y_vecs[edge] / (torch.norm(self.edge_Y_vecs[edge], dim=-1, keepdim=True) + self.eps) |
| |
| """ |
| (3). Define canonical base frames in terms of one specific edge. |
| The sugar edge generally works best here, as it most often points towards binding partner |
| (orthogonal to both major groove and helical axis) |
| """ |
| |
| self.X_i = torch.cross(self.Z_i, self.edge_X_vecs[self.perp_base_edge], dim=-1) |
| self.X_i = self.X_i / (torch.norm(self.X_i, dim=-1, keepdim=True) + self.eps) |
|
|
| self.Y_i = torch.cross(self.X_i, self.Z_i, dim=-1) |
| self.Y_i = self.Y_i / (torch.norm(self.Y_i, dim=-1, keepdim=True) + self.eps) |
| self.base_atom_centers = base_atom_centers |
| |
| def _compute_pairwise_base_params(self): |
| |
| D_ij_vec_na = self.D_ij_vec[torch.arange(self.is_na.sum()).unsqueeze(1), torch.arange(self.is_na.sum())] |
| base_D_ij_vec = self.base_atom_centers.unsqueeze(0) - self.base_atom_centers.unsqueeze(1) |
| |
| |
| Z_ij_oris = 0.5*torch.stack((self.Z_i.unsqueeze(1) + self.Z_i.unsqueeze(0), self.Z_i.unsqueeze(1) - self.Z_i.unsqueeze(0) ), dim=0) |
|
|
| |
| bases_are_antiparallel = (Z_ij_oris[1].norm(dim=-1) > Z_ij_oris[0].norm(dim=-1)).long() |
|
|
| |
| Z_ij = Z_ij_oris[bases_are_antiparallel, torch.arange(self.is_na.sum()).unsqueeze(1), torch.arange(self.is_na.sum())] |
| Z_ij = Z_ij / (torch.norm(Z_ij, dim=-1, keepdim=True) + self.eps) |
| |
| Y_ij = D_ij_vec_na / (torch.norm(D_ij_vec_na, dim=-1, keepdim=True) + self.eps) |
| X_ij = torch.cross(Z_ij, Y_ij, dim=-1) |
|
|
| X_ij = X_ij / (torch.norm(X_ij, dim=-1, keepdim=True) + self.eps) |
| |
| self.H_ij = torch.sum(base_D_ij_vec * Z_ij, dim=-1) |
| self.H_ij_vec = self.H_ij[...,None] * Z_ij |
|
|
| |
| proj_X_i_XY = ((self.X_i[:, None, :] * X_ij).sum(dim=-1, keepdim=True) * X_ij) + ((self.X_i[:, None, :] * Y_ij).sum(dim=-1, keepdim=True) * Y_ij) |
| proj_X_i_XY_norm = proj_X_i_XY / (torch.norm(proj_X_i_XY, dim=-1, keepdim=True) + self.eps) |
| cos_opening = (proj_X_i_XY_norm * proj_X_i_XY_norm.transpose(1,0)).sum(dim=-1) |
| if self.clamp_pairwise_params: |
| cos_opening = torch.clamp(cos_opening, -1.0, 1.0) |
| O_ij = torch.acos(cos_opening) |
|
|
| |
| proj_Z_i_YZ = ((self.Z_i[:, None, :] * Y_ij).sum(dim=-1, keepdim=True) * Y_ij) + ((self.Z_i[:, None, :] * Z_ij).sum(dim=-1, keepdim=True) * Z_ij) |
| proj_Z_i_YZ_norm = proj_Z_i_YZ / (torch.norm(proj_Z_i_YZ, dim=-1, keepdim=True) + self.eps) |
| cos_buckle = (proj_Z_i_YZ_norm * -proj_Z_i_YZ_norm.transpose(1,0)).sum(dim=-1) |
| if self.clamp_pairwise_params: |
| cos_buckle = torch.clamp(cos_buckle, -1.0, 1.0) |
| cos_buckle = torch.clamp(cos_buckle, -1.0, 1.0) |
| B_ij = torch.acos(cos_buckle) |
|
|
| |
| proj_Z_i_ZX = ((self.Z_i[:, None, :] * Z_ij).sum(dim=-1, keepdim=True) * Z_ij) + ((self.Z_i[:, None, :] * X_ij).sum(dim=-1, keepdim=True) * X_ij) |
| proj_Z_i_ZX_norm = proj_Z_i_ZX / (torch.norm(proj_Z_i_ZX, dim=-1, keepdim=True) + self.eps) |
| cos_propeller = (proj_Z_i_ZX_norm * -proj_Z_i_ZX_norm.transpose(1,0)).sum(dim=-1) |
| if self.clamp_pairwise_params: |
| cos_propeller = torch.clamp(cos_propeller, -1.0, 1.0) |
| P_ij = torch.acos(cos_propeller) |
| |
| |
| self.X_ij = X_ij |
| self.Y_ij = Y_ij |
| self.Z_ij = Z_ij |
| |
| |
| self.O_ij = O_ij |
| self.B_ij = B_ij |
| self.P_ij = P_ij |
| self.bases_are_antiparallel = bases_are_antiparallel |
|
|
| def _compute_paired_bases(self): |
| |
| |
| bp_preds = torch.sigmoid(self.bp_hbond_coeff * (self.hbond_summation - (self.min_hbonds_for_bp - 1))) |
| |
| |
| both_nucleic_filter = self.is_na[:,None] * self.is_na[None,:] |
| |
| |
| |
| |
| |
| H_ij_filter = (self.H_ij >= -self.base_geometry_limits['H_ij']) & (self.H_ij <= self.base_geometry_limits['H_ij']) |
| |
| |
| B_ij_filter = (self.B_ij <= (np.pi - self.base_geometry_limits['B_ij'])) | (self.B_ij >= self.base_geometry_limits['B_ij']) |
| |
| P_ij_filter = (self.P_ij <= (np.pi - self.base_geometry_limits['P_ij'])) | (self.P_ij >= self.base_geometry_limits['P_ij']) |
| |
| |
| |
| bp_geom_filter = torch.zeros(self.len_s, self.len_s, dtype=torch.bool) |
| bp_geom_filter[torch.outer(self.is_na, self.is_na)] = (H_ij_filter * B_ij_filter * P_ij_filter).flatten() |
| |
| |
| |
| self.basepairs_ij = both_nucleic_filter * bp_geom_filter * bp_preds |
|
|
| def _compute_helical_params(self): |
| |
| len_na = self.Z_i.shape[0] |
| nucleic_frames = self.xyz[self.is_na, 1, :] |
| doublet_inds = [(i,j) for (i,j) in zip(range(0,len_na-1),range(1,len_na))] |
| |
| |
| Zm_i = torch.zeros_like(self.Z_i) |
| Zh_i = torch.zeros_like(self.Z_i) |
| |
| |
| tilt_i = torch.zeros(len_na) |
| roll_i = torch.zeros(len_na) |
| twist_i = torch.zeros(len_na) |
| shift_i = torch.zeros(len_na) |
| slide_i = torch.zeros(len_na) |
| rise_i = torch.zeros(len_na) |
| |
| |
| |
| inclination_i = torch.zeros(len_na) |
| tip_i = torch.zeros(len_na) |
| helical_twist_i = torch.zeros(len_na) |
| x_disp_i = torch.zeros(len_na) |
| y_disp_i = torch.zeros(len_na) |
| helical_rise_i = torch.zeros(len_na) |
| |
| |
| avg_factor = torch.zeros(len_na) |
| for i,j in doublet_inds: |
| avg_factor[i] += 1.0 |
| avg_factor[j] += 1.0 |
| |
| |
| basepaired_inds = (self.basepairs_ij >= self.bp_val_cutoff).bool().nonzero(as_tuple=True) |
| |
| pairwise_indices = list(zip(basepaired_inds[0].tolist(), basepaired_inds[1].tolist())) |
| |
| |
| partner_info_dict = {i:{'partner_ind':[], 'orientation':[], 'num_hbonds':[], 'bp_score': []} for i in self.na_inds} |
| |
| for i, j in pairwise_indices: |
| _i,_j = self.na_tensor_inds[i], self.na_tensor_inds[j] |
| partner_info_dict[i]['partner_ind'].append(j) |
| partner_info_dict[i]['orientation'].append(self.bases_are_antiparallel[_i,_j]) |
| partner_info_dict[i]['num_hbonds'].append(self.hbond_summation[_i,_j]) |
| partner_info_dict[i]['bp_score'].append(self.basepairs_ij[_i,_j]) |
| |
| |
|
|
|
|
| |
| |
| |
| for i_1, i_2 in doublet_inds: |
|
|
| partners_i_1 = [self.na_tensor_inds[_] for _ in partner_info_dict[self.na_inds[i_1]]['partner_ind']] |
| partners_i_2 = [self.na_tensor_inds[_] for _ in partner_info_dict[self.na_inds[i_2]]['partner_ind']] |
|
|
| |
| |
| |
| |
| num_partners_i_1 = len(partners_i_1) |
| num_partners_i_2 = len(partners_i_2) |
| for j_1 in partners_i_1: |
| |
| for j_2 in partners_i_2: |
| X_1 = self.X_ij[i_1,j_1] |
| Y_1 = self.Y_ij[i_1,j_1] |
| X_2 = self.X_ij[i_2,j_2] |
| Y_2 = self.Y_ij[i_2,j_2] |
|
|
| Xp = X_2 + X_1 |
| Xn = X_2 - X_1 |
| Yp = Y_2 + Y_1 |
| Yn = Y_2 - Y_1 |
| |
| M_12 = 0.5*((nucleic_frames[i_2]+nucleic_frames[j_2]) - (nucleic_frames[i_1]+nucleic_frames[j_1])) |
|
|
| Zm = torch.cross(Xp, Yp, dim=-1) / ((Xp.norm(dim=-1) * Yp.norm(dim=-1)) + self.eps) |
| Zh = torch.cross(Xn, Yn, dim=-1) / ((Xn.norm(dim=-1) * Yn.norm(dim=-1)) + self.eps) |
|
|
| Zm_i[i_1] += Zm / (avg_factor[i_1]+self.eps) |
| Zh_i[i_1] += Zh / (avg_factor[i_1]+self.eps) |
|
|
| Zm_i[i_2] += Zm / (avg_factor[i_2]+self.eps) |
| Zh_i[i_2] += Zh / (avg_factor[i_2]+self.eps) |
|
|
|
|
| tilt_ij = -torch.arcsin(torch.sum(Zm * X_1 , dim=-1)) |
| roll_ij = torch.arcsin(torch.sum(Zm * Y_1 , dim=-1)) |
| twist_ij = torch.arccos(torch.sum(torch.cross(X_1 , Zm, dim=-1) * torch.cross(X_2 , Zm, dim=-1), dim=-1)) |
| shift_ij = torch.sum(M_12 * (Xp / (torch.norm(Xp, dim=-1)+self.eps)), dim=-1) |
| slide_ij = torch.sum(M_12 * (Yp / (torch.norm(Yp, dim=-1)+self.eps)), dim=-1) |
| rise_ij = torch.sum(M_12 * Zm , dim=-1) |
|
|
| inclination_ij = torch.arcsin(torch.sum(Zh * X_1 , dim=-1)) |
| tip_ij = -torch.arcsin(torch.sum(Zh * Y_1 , dim=-1)) |
| helical_twist_ij = -torch.arccos(torch.sum(torch.cross(X_1 , Zh, dim=-1) * torch.cross(X_2 , Zh, dim=-1), dim=-1)) |
| x_disp_ij = torch.sum(M_12 * Xn / (torch.norm(Xn, dim=-1)+self.eps), dim=-1) |
| y_disp_ij = torch.sum(M_12 * Yn / (torch.norm(Yn, dim=-1)+self.eps), dim=-1) |
| helical_rise_ij = -torch.sum(M_12 * Zh, dim=-1) |
|
|
| |
| |
| avg_factor[i_1] += 1.0 |
| tilt_i[i_1] += tilt_ij |
| roll_i[i_1] += roll_ij |
| twist_i[i_1] += twist_ij |
| shift_i[i_1] += shift_ij |
| slide_i[i_1] += slide_ij |
| rise_i[i_1] += rise_ij |
| inclination_i[i_1] += inclination_ij |
| tip_i[i_1] += tip_ij |
| helical_twist_i[i_1] += helical_twist_ij |
| x_disp_i[i_1] += x_disp_ij |
| y_disp_i[i_1] += y_disp_ij |
| helical_rise_i[i_1] += helical_rise_ij |
|
|
| |
| avg_factor[i_2] += 1.0 |
| tilt_i[i_2] += tilt_ij |
| roll_i[i_2] += roll_ij |
| twist_i[i_2] += twist_ij |
| shift_i[i_2] += shift_ij |
| slide_i[i_2] += slide_ij |
| rise_i[i_2] += rise_ij |
| inclination_i[i_2] += inclination_ij |
| tip_i[i_2] += tip_ij |
| helical_twist_i[i_2] += helical_twist_ij |
| x_disp_i[i_2] += x_disp_ij |
| y_disp_i[i_2] += y_disp_ij |
| helical_rise_i[i_2] += helical_rise_ij |
|
|
|
|
| self.tilt_i = tilt_i / (avg_factor + self.eps) |
| self.roll_i = roll_i / (avg_factor + self.eps) |
| self.twist_i = twist_i / (avg_factor + self.eps) |
| self.shift_i = shift_i / (avg_factor + self.eps) |
| self.slide_i = slide_i / (avg_factor + self.eps) |
| self.rise_i = rise_i / (avg_factor + self.eps) |
| self.inclination_i = inclination_i / (avg_factor + self.eps) |
| self.tip_i = tip_i / (avg_factor + self.eps) |
| self.helical_twist_i = helical_twist_i / (avg_factor + self.eps) |
| self.x_disp_i = x_disp_i / (avg_factor + self.eps) |
| self.y_disp_i = y_disp_i / (avg_factor + self.eps) |
| self.helical_rise_i = helical_rise_i / (avg_factor + self.eps) |
|
|
| def _init_hb_chemdata(self): |
|
|
|
|
|
|
| |
| self.hbond_atoms = { |
| "ALA": {"names":[ ], |
| "donor":[ ]}, |
| "ARG": {"names":[" NH1"," NH2" ], |
| "donor":[ 1 , 1 ]}, |
| "ASN": {"names":[" ND2", " OD1" ], |
| "donor":[ 1 , 0 ]}, |
| "ASP": {"names":[" OD2", " OD1"," OD2" ], |
| "donor":[ 1 , 0 , 0 ]}, |
| "CYS": {"names":[" SG " ], |
| "donor":[ 1 ]}, |
| "GLN": {"names":[" NE2", " OE1" ], |
| "donor":[ 1 , 0 ]}, |
| "GLU": {"names":[" OE2", " OE1"," OE2" ], |
| "donor":[ 1 , 0 , 0 ]}, |
| "GLY": {"names":[ ], |
| "donor":[ ]}, |
| "HIS": {"names":[" ND1"," NE2", " ND1"," NE2" ], |
| "donor":[ 1 , 1 , 0 , 0 ]}, |
| "ILE": {"names":[ ], |
| "donor":[ ]}, |
| "LEU": {"names":[ ], |
| "donor":[ ]}, |
| "LYS": {"names":[" NZ " ], |
| "donor":[ 1 ]}, |
| "MET": {"names":[ " SD " ], |
| "donor":[ 0 ]}, |
| "PHE": {"names":[ ], |
| "donor":[ ]}, |
| "PRO": {"names":[ ], |
| "donor":[ ]}, |
| "SER": {"names":[" OG " ], |
| "donor":[ 1 ]}, |
| "THR": {"names":[" OG1" ], |
| "donor":[ 1 ]}, |
| "TRP": {"names":[ " NE1" ], |
| "donor":[ 0 ]}, |
| "TYR": {"names":[" OH " ], |
| "donor":[ 1 ]}, |
| "VAL": {"names":[ ], |
| "donor":[ ]}, |
| "UNK": {"names":[ ], |
| "donor":[ ]}, |
| "MAS": {"names":[ ], |
| "donor":[ ]}, |
| " DA": {"names":[" N6 ", " N1 "," N3 "," N7 " ], |
| "donor":[ 1 , 0 , 0 , 0 ]}, |
| " DG": {"names":[" N1 "," N2 "," N7 ", " O6 "," N1 "," N3 "," N7 " ], |
| "donor":[ 1 , 1 , 1 , 0 , 0 , 0 , 0 ]}, |
| " DC": {"names":[" N4 "," N3 ", " O2 "," N3 " ], |
| "donor":[ 1 , 1 , 0 , 0 ]}, |
| " DT": {"names":[" N3 ", " O2 "," O4 " ], |
| "donor":[ 1 , 0 , 0 ]}, |
| " DX": {"names":[ ], |
| "donor":[ ]}, |
| " RA": {"names":[" O2'"," N6 ", " N1 "," N3 "," N7 " ], |
| "donor":[ 1 , 1 , 0 , 0 , 0 ]}, |
| " RG": {"names":[" O2'"," N1 "," N2 "," N7 ", " O6 "," N1 "," N3 "," N7 "], |
| "donor":[ 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 ]}, |
| " RC": {"names":[" O2'"," N4 "," N3 ", " O2 "," N3 " ], |
| "donor":[ 1 , 1 , 1 , 0 , 0 ]}, |
| " RU": {"names":[" O2'"," N3 ", " O2 "," O4 " ], |
| "donor":[ 1 , 1 , 0 , 0 ]}, |
| " RX": {"names":[" O2'", ], |
| "donor":[ 1 , ]}, |
| } |
|
|
|
|
|
|
| |
| self.rear_atoms = { |
| "ALA": {}, |
| "ARG": {" NH1":[" CZ "], " NH2":[" CZ "],}, |
| "ASN": {" OD1":[" CG "], " ND2":[" CG "],}, |
| "ASP": {" OD1":[" CG "], " OD2":[" CG "],}, |
| "CYS": {" SG ":[" CB "],}, |
| "GLN": {" OE1":[" CD "], " NE2":[" CD "],}, |
| "GLU": {" OE1":[" CD "], " OE2":[" CD "],}, |
| "GLY": {}, |
| "HIS": {" ND1":[" CG "," CE1"], " NE2":[" CD2"," CE1"],}, |
| "ILE": {}, |
| "LEU": {}, |
| "LYS": {" NZ ":[" CE "],}, |
| "MET": {" SD ":[" CG "," CE "],}, |
| "PHE": {}, |
| "PRO": {}, |
| "SER": {" OG ":[" CB "],}, |
| "THR": {" OG1":[" CB "],}, |
| "TRP": {" NE1":[" CD1"," CE2"],}, |
| "TYR": {" OH ":[" CZ "],}, |
| "VAL": {}, |
| "UNK": {}, |
| "MAS": {}, |
| " DA": {" N6 ":[" C6 ",], " N1 ":[" C2 "," C6 "], " N3 ":[" C2 "," C4 "], " N7 ":[" C5 "," C8 "],}, |
| " DG": {" N1 ":[" C2 "," C6 "], " N2 ":[" C2 ",], " N7 ":[" C5 "," C8 "], " O6 ":[" C6 ",], " N3 ":[" C2 "," C4 "], " N7 ":[" C5 "," C8 "],}, |
| " DC": {" N4 ":[" C4 ",], " N3 ":[" C2 "," C5 "], " O2 ":[" C2 ",],}, |
| " DT": {" N3 ":[" C2 "," C4 "], " O2 ":[" C2 ",], " O4 ":[" C4 ",],}, |
| " DX": {}, |
| " RA": {" O2'":[" C2'",], " N6 ":[" C6 ",], " N1 ":[" C2 "," C6 "], " N3 ":[" C2 "," C4 "], " N7 ":[" C5 "," C8 "],}, |
| " RG": {" O2'":[" C2'",], " N1 ":[" C2 "," C6 "], " N2 ":[" C2 ",], " N7 ":[" C5 "," C8 "], " O6 ":[" C6 ",], " N3 ":[" C2 "," C4 "], " N7 ":[" C5 "," C8 "],}, |
| " RC": {" O2'":[" C2'",], " N4 ":[" C4 ",], " N3 ":[" C2 "," C5 "], " O2 ":[" C2 ",],}, |
| " RU": {" O2'":[" C2'",], " N3 ":[" C2 "," C4 "], " O2 ":[" C2 ",], " O4 ":[" C4 ",],}, |
| " RX": {" O2'":[" C2'",], }, |
| } |
|
|
|
|
|
|
| self.ideal_angle_dict = { |
| 'O': { |
| 1: 109.5*(np.pi/180), |
| 2: 180.0*(np.pi/180)}, |
| 'N': { |
| 1: 120.0*(np.pi/180), |
| 2: 180.0*(np.pi/180)}, |
| 'S': { |
| 1: 109.5*(np.pi/180), |
| 2: 180.0*(np.pi/180)}, |
| 'P': { |
| 1: 120.0*(np.pi/180), |
| 2: 180.0*(np.pi/180)}, |
| } |
|
|
|
|
| def _init_nuc_chemdata(self): |
|
|
| self.nuc_resi_3letter = [" DA"," DG"," DC"," DT"," RA"," RG"," RC"," RU"] |
|
|
| |
| self.vec_atom_dict = { |
| " DA": {"W_start":" N1 ","W_stop":" N6 ", "H_start":" N7 ","H_stop":" N6 ", "S_start":" C1'","S_stop":" N3 ", "B_start":" C1'","B_stop":" N9 " }, |
| " DG": {"W_start":" N1 ","W_stop":" O6 ", "H_start":" N7 ","H_stop":" O6 ", "S_start":" C1'","S_stop":" N3 ", "B_start":" C1'","B_stop":" N9 " }, |
| " DC": {"W_start":" N3 ","W_stop":" N4 ", "H_start":" C5 ","H_stop":" N4 ", "S_start":" C1'","S_stop":" O2 ", "B_start":" C1'","B_stop":" N1 " }, |
| " DT": {"W_start":" N3 ","W_stop":" O4 ", "H_start":" C5 ","H_stop":" O4 ", "S_start":" C1'","S_stop":" O2 ", "B_start":" C1'","B_stop":" N1 " }, |
| " RA": {"W_start":" N1 ","W_stop":" N6 ", "H_start":" N7 ","H_stop":" N6 ", "S_start":" C1'","S_stop":" N3 ", "B_start":" C1'","B_stop":" N9 " }, |
| " RG": {"W_start":" N1 ","W_stop":" O6 ", "H_start":" N7 ","H_stop":" O6 ", "S_start":" C1'","S_stop":" N3 ", "B_start":" C1'","B_stop":" N9 " }, |
| " RC": {"W_start":" N3 ","W_stop":" N4 ", "H_start":" C5 ","H_stop":" N4 ", "S_start":" C1'","S_stop":" O2 ", "B_start":" C1'","B_stop":" N1 " }, |
| " RU": {"W_start":" N3 ","W_stop":" O4 ", "H_start":" C5 ","H_stop":" O4 ", "S_start":" C1'","S_stop":" O2 ", "B_start":" C1'","B_stop":" N1 " }, |
| } |
|
|
| self.vec_atom_inds = {s_i: {k_ij: HB_data.aa2long[HB_data.aa2num[s_i]].index(a_ij) for k_ij, a_ij in self.vec_atom_dict[s_i].items() } for s_i in self.nuc_resi_3letter} |
|
|
| self.edge_to_ind = {'W':0 , 'H':1 , 'S':2 ,'B':3} |
| self.ring_atom_list = [" N1 "," C2 "," N3 "," C4 "," C6 "," C5 "] |
| self.ring_atom_inds = {s_i: [HB_data.aa2long[HB_data.aa2num[s_i]].index(a_ij) for a_ij in self.ring_atom_list ] for s_i in self.nuc_resi_3letter} |
|
|
| def convert_mpnn_representation(S, X, X_m, rna_mask): |
| """ |
| Given a sequence, atom coordinates, and atom mask in the NA-MPNN format, |
| output the sequence, atom coordinates, and atom mask in an RFaa-like format. |
| |
| Arguments: |
| S (np.int32 np.ndarray): an L length array representing the sequence of |
| the biomolecular assembly. |
| X (np.float32 np.ndarray): an L x num_atom_types x 3 array representing |
| the coordinates of each atom for each residue in the biomolecular |
| assembly. |
| X_m (np.int32 np.ndarray): an L x num_atom_types x 3 array mask that is |
| 1 if the corresponding atom in the specified residue was loaded |
| and 0 otherwise. |
| rna_mask (np.int32 np.ndarray): an L length array mask representing |
| whether the residue is an RNA residue. |
| |
| Returns: |
| S_rfaa (np.int32 np.ndarray): an L length array representing the |
| sequence of the biomolecular assembly in the RFaa format. |
| X_rfaa (np.float32 np.ndarray): an L x num_atom_types x 3 array |
| representing the coordinates of each atom for each residue in the |
| biomolecular assembly in the RFaa format. |
| """ |
| atom_idx_to_name = {atom_idx:atom_name for (atom_name, atom_idx) in pdb_dataset.atom_dict.items()} |
|
|
| |
| |
| S_rfaa = [] |
| for i in range(S.shape[0]): |
| restype_int = S[i] |
|
|
| restype = pdb_dataset.int_to_restype[restype_int] |
|
|
| if rna_mask[i]: |
| |
| |
| |
| if restype == "DA" or restype == "A": |
| restype_rfaa = "RA" |
| elif restype == "DC" or restype == "C": |
| restype_rfaa = "RC" |
| elif restype == "DG" or restype == "G": |
| restype_rfaa = "RG" |
| elif restype == "DT" or restype == "U": |
| restype_rfaa = "RU" |
| elif restype == "DX" or restype == "RX": |
| restype_rfaa = "RX" |
| else: |
| raise Exception("RNA restype not recognized.") |
| else: |
| restype_rfaa = restype |
| |
| restype_int_rfaa = HB_data.aa2num_stripped[restype_rfaa] |
| |
| S_rfaa.append(restype_int_rfaa) |
| |
| S_rfaa = np.array(S_rfaa, dtype = np.int64) |
|
|
| |
| X_rfaa = np.zeros((X.shape[0], HB_data.NTOTAL, 3), dtype = np.float32) |
| for i in range(X.shape[0]): |
| restype_int_rfaa = S_rfaa[i] |
| for atom_idx in range(X.shape[1]): |
| if X_m[i, atom_idx] == 1: |
| atom_type = atom_idx_to_name[atom_idx] |
|
|
| |
| if (HB_data.num2aa[restype_int_rfaa] in ["UNK", " DX", " RX"]) and \ |
| (atom_type not in HB_data.aa2long_stripped[restype_int_rfaa]): |
| continue |
|
|
| |
| |
| |
| |
| if (HB_data.num2aa[restype_int_rfaa] in [" DA", " DC", " DG", " DT"]) and \ |
| (atom_type == "O2'"): |
| continue |
|
|
| |
| if atom_type == "OXT": |
| continue |
|
|
| |
| atom_idx_rfaa = HB_data.aa2long_stripped[restype_int_rfaa].index(atom_type) |
| X_rfaa[i, atom_idx_rfaa] = X[i, atom_idx] |
| |
| return S_rfaa, X_rfaa |
|
|
| def get_base_pair_mask_and_index(S, X, X_m, rna_mask): |
| """ |
| Given a sequence, atom coordinates, and atom mask, compute the base pairing |
| residues and the canonical base pairing residues (represented as a mask |
| and index of the base pairing partner). |
| |
| Arguments: |
| S (np.int32 np.ndarray): an L length array representing the sequence of |
| the biomolecular assembly. |
| X (np.float32 np.ndarray): an L x num_atom_types x 3 array representing |
| the coordinates of each atom for each residue in the biomolecular |
| assembly. |
| X_m (np.int32 np.ndarray): an L x num_atom_types x 3 array mask that is |
| 1 if the corresponding atom in the specified residue was loaded |
| and 0 otherwise. |
| rna_mask (np.int32 np.ndarray): an L length array mask representing |
| whether the residue is an RNA residue. |
| |
| Returns: |
| base_pair_mask (np.int32 np.ndarray): an L length array mask that is |
| 1 if the corresponding residue is involved in a base pair |
| interaction and 0 otherwise. |
| base_pair_index (np.int64 np.ndarray): an L length array that |
| represents the index of the partner residue in a base pairing |
| interaction. For residues not in a base pairing interaction, |
| defined as 0, but it is necessary to the base_pair_mask in |
| conjunction. |
| canonical_base_pair_mask (np.int32 np.ndarray): similar to |
| base_pair_mask, but limited to positions that make canonical base |
| pairing interactions. |
| canonical_base_pair_index (np.int64 np.ndarray): similar to |
| base_pair_index, but limited to positions that make canonical base |
| pairing interactions. |
| """ |
| |
| S_rfaa, X_rfaa = convert_mpnn_representation(S, X, X_m, rna_mask) |
|
|
| hb_data = HB_data(torch.tensor(S_rfaa), |
| torch.tensor(X_rfaa), |
| compute_paired_bases=True, |
| compute_helical_params=True |
| ) |
| |
| |
| if hb_data.is_na.sum() > 0: |
| base_pairs_prob = hb_data.basepairs_ij.detach().cpu().numpy() |
| base_pairs_binary = (base_pairs_prob > 0.5).astype(np.int32) |
|
|
| |
| base_pair_mask = (np.sum(base_pairs_binary, axis = -1) == 1).astype(np.int32) |
| base_pair_index = np.argmax(base_pairs_binary, axis = -1).astype(np.int64) |
| else: |
| base_pair_mask = np.zeros(S_rfaa.shape[0], dtype = np.int32) |
| base_pair_index = np.zeros(S_rfaa.shape[0], dtype = np.int64) |
|
|
| |
| |
| base_pair_mask = base_pair_mask * base_pair_mask[base_pair_index] |
|
|
| |
| base_pair_index = base_pair_index * base_pair_mask |
|
|
| |
| |
| canonical_base_pair_mask = np.copy(base_pair_mask) |
| canonical_base_pair_index = np.copy(base_pair_index) |
| for i in range(len(S)): |
| if base_pair_mask[i] == 1: |
| restype_i = S[i] |
| restype_j = S[base_pair_index[i]] |
| if (restype_i, restype_j) not in pdb_dataset.na_canonical_base_pair_ints: |
| canonical_base_pair_mask[i] = 0 |
| canonical_base_pair_mask[base_pair_index[i]] = 0 |
| |
| |
| |
| canonical_base_pair_index = canonical_base_pair_index * canonical_base_pair_mask |
| |
| return base_pair_mask, base_pair_index, canonical_base_pair_mask, canonical_base_pair_index |
|
|
| |
| def get_nearest_interface_neighbors_to_res_i(X, protein_mask, na_mask, i, eps = 1E-6): |
| if protein_mask[i] == 1: |
| mask = na_mask |
| elif na_mask[i] == 1: |
| mask = protein_mask |
| dX = X - X[i] |
| D = mask * torch.sqrt(torch.sum(dX ** 2, 1) + eps) |
| D_max, _ = torch.max(D, -1, keepdim=True) |
| D_adjust = D + (1. - mask) * (D_max + eps) |
| D_neighbors, E_idx = torch.topk(D_adjust, np.minimum(num_neighbors, X.shape[0]), dim=-1, largest=False) |
| return E_idx |
| |
| def get_interface_masks(X, X_m, protein_mask, dna_mask, rna_mask): |
| L = X.shape[0] |
| na_mask = dna_mask + rna_mask |
|
|
| interface_mask = np.zeros(L, dtype = np.int32) |
|
|
| Ca = X[:,pdb_dataset.atom_dict["CA"],:] |
| na_ref_atom = X[:,pdb_dataset.atom_dict[params["NA_REF_ATOM"]],:] |
|
|
| side_chain_interface_mask = np.zeros(L, dtype = np.int32) |
| nearest_protein_side_chain_index = np.zeros(L, dtype = np.int64) |
| for i in range(L): |
| nearest_neighbor_idx = get_nearest_interface_neighbors_to_res_i(torch.tensor(Ca + na_ref_atom), torch.tensor(protein_mask), torch.tensor(na_mask), i) |
| nearest_protein_side_chain_distance = None |
| for j in nearest_neighbor_idx: |
| if not (na_mask[i] == 1 or na_mask[j] == 1): |
| continue |
|
|
| res_i_X = X[i] |
| res_i_X_m = X_m[i] |
|
|
| res_j_X = X[j] |
| res_j_X_m = X_m[j] |
|
|
| |
| dX = res_i_X[:,None,:] - res_j_X[None,:,:] |
| pairwise_atom_distances = np.sqrt(np.sum(dX ** 2, axis = -1)) |
|
|
| |
| X_m_pairwise = res_i_X_m[:,None] * res_j_X_m[None,:] |
| |
| min_distance = np.min(pairwise_atom_distances[(X_m_pairwise == 1)]) |
| if min_distance < interface_distance_cutoff: |
| if (protein_mask[i] == 1 and na_mask[j] == 1) or (protein_mask[j] == 1 and na_mask[i] == 1): |
| interface_mask[i] = 1 |
| interface_mask[j] = 1 |
| |
| X_m_side_chain_pairwise = X_m_pairwise * side_chain_pairwise_mask |
|
|
| if np.count_nonzero(X_m_side_chain_pairwise) > 0: |
| min_side_chain_distance = np.min(pairwise_atom_distances[(X_m_side_chain_pairwise == 1)]) |
| if min_side_chain_distance < interface_distance_cutoff: |
| if (protein_mask[i] == 1 and na_mask[j] == 1) or (protein_mask[j] == 1 and na_mask[i] == 1): |
| side_chain_interface_mask[i] = 1 |
| side_chain_interface_mask[j] = 1 |
| |
| if protein_mask[j] == 1 and \ |
| (nearest_protein_side_chain_distance == None or \ |
| min_side_chain_distance < nearest_protein_side_chain_distance): |
| nearest_protein_side_chain_index[i] = j |
| nearest_protein_side_chain_distance = min_side_chain_distance |
|
|
| return interface_mask, side_chain_interface_mask, nearest_protein_side_chain_index |
|
|
| if __name__ == "__main__": |
| |
| input_csv_path = sys.argv[1] |
| output_directory = sys.argv[2] |
| modulo = int(sys.argv[3]) |
| remainder = int(sys.argv[4]) |
|
|
| |
| df = pd.read_csv(input_csv_path) |
|
|
| |
| sequences_directory = os.path.join(output_directory, "sequences") |
| asmb_lengths_directory = os.path.join(output_directory, "asmb_lengths") |
| asmb_interface_masks_directory = os.path.join(output_directory, "asmb_interface_masks") |
| asmb_side_chain_interface_masks_directory = os.path.join(output_directory, "asmb_side_chain_interface_masks") |
| asmb_nearest_protein_side_chain_index_directory = os.path.join(output_directory, "asmb_nearest_protein_side_chain_index") |
| asmb_base_pair_masks_directory = os.path.join(output_directory, "asmb_base_pair_masks") |
| asmb_base_pair_index_directory = os.path.join(output_directory, "asmb_base_pair_index") |
| asmb_canonical_base_pair_masks_directory = os.path.join(output_directory, "asmb_canonical_base_pair_masks") |
| asmb_canonical_base_pair_index_directory = os.path.join(output_directory, "asmb_canonical_base_pair_index") |
| bad_directory = os.path.join(output_directory, "bad") |
|
|
| |
| os.makedirs(sequences_directory, exist_ok = True) |
| os.makedirs(asmb_lengths_directory, exist_ok = True) |
| os.makedirs(asmb_interface_masks_directory, exist_ok = True) |
| os.makedirs(asmb_side_chain_interface_masks_directory, exist_ok = True) |
| os.makedirs(asmb_nearest_protein_side_chain_index_directory, exist_ok = True) |
| os.makedirs(asmb_base_pair_masks_directory, exist_ok = True) |
| os.makedirs(asmb_base_pair_index_directory, exist_ok = True) |
| os.makedirs(asmb_canonical_base_pair_masks_directory, exist_ok = True) |
| os.makedirs(asmb_canonical_base_pair_index_directory, exist_ok = True) |
| os.makedirs(bad_directory, exist_ok = True) |
|
|
| |
| for iii in range(len(df)): |
| if (iii + 1) % modulo != remainder: |
| continue |
| |
| example_dict = df.iloc[iii].to_dict() |
|
|
| structure_file_name = os.path.basename(example_dict["structure_path"]) |
| |
| if structure_file_name.endswith(".gz"): |
| structure_name = os.path.splitext(os.path.splitext(structure_file_name)[0])[0] |
| else: |
| structure_name = os.path.splitext(structure_file_name)[0] |
| |
| sequences_path = os.path.join(sequences_directory, structure_name + ".csv") |
| asmb_lengths_path = os.path.join(asmb_lengths_directory, structure_name + ".npy") |
| asmb_interface_masks_path = os.path.join(asmb_interface_masks_directory, structure_name + ".npy") |
| asmb_side_chain_interface_masks_path = os.path.join(asmb_side_chain_interface_masks_directory, structure_name + ".npy") |
| asmb_nearest_protein_side_chain_index_path = os.path.join(asmb_nearest_protein_side_chain_index_directory, structure_name + ".npy") |
| asmb_base_pair_masks_path = os.path.join(asmb_base_pair_masks_directory, structure_name + ".npy") |
| asmb_base_pair_index_path = os.path.join(asmb_base_pair_index_directory, structure_name + ".npy") |
| asmb_canonical_base_pair_masks_path = os.path.join(asmb_canonical_base_pair_masks_directory, structure_name + ".npy") |
| asmb_canonical_base_pair_index_path = os.path.join(asmb_canonical_base_pair_index_directory, structure_name + ".npy") |
| bad_path = os.path.join(bad_directory, structure_name + ".txt") |
|
|
| try: |
| assemblies, chain_sequences = pdb_dataset.load_for_structure_preprocessing(example_dict) |
| except Exception as e: |
| write_text_file(bad_path, str(e)) |
| continue |
|
|
| if assemblies == "pass" or (len(assemblies) == 0): |
| write_text_file(bad_path, "cifutils_failed_to_load_assemblies") |
| continue |
|
|
| asmb_lengths_dict = {} |
| asmb_interface_masks_dict = {} |
| asmb_side_chain_interface_masks_dict = {} |
| asmb_nearest_protein_side_chain_index_dict = {} |
| asmb_base_pair_masks_dict = {} |
| asmb_base_pair_index_dict = {} |
| asmb_canonical_base_pair_masks_dict = {} |
| asmb_canonical_base_pair_index_dict = {} |
| missing_na_count = 0 |
| for (assembly_id, out_dict) in assemblies: |
| |
| if (out_dict["dna_L"] == 0) and (out_dict["rna_L"] == 0): |
| missing_na_count += 1 |
| continue |
|
|
| |
| |
| |
| if out_dict["S"].shape[0] > residue_cutoff: |
| base_pair_mask = np.zeros(out_dict["S"].shape, dtype = np.int32) |
| base_pair_index = np.zeros(out_dict["S"].shape, dtype = np.int64) |
| canonical_base_pair_mask = np.zeros(out_dict["S"].shape, dtype = np.int32) |
| canonical_base_pair_index = np.zeros(out_dict["S"].shape, dtype = np.int64) |
| else: |
| base_pair_mask, base_pair_index, canonical_base_pair_mask, canonical_base_pair_index = \ |
| get_base_pair_mask_and_index(out_dict["S"], |
| out_dict["X"], |
| out_dict["X_m"], |
| out_dict["rna_mask"]) |
| |
| |
| interface_mask, side_chain_interface_mask, nearest_protein_side_chain_index = \ |
| get_interface_masks(out_dict["X"], |
| out_dict["X_m"], |
| out_dict["protein_mask"], |
| out_dict["dna_mask"], |
| out_dict["rna_mask"]) |
| |
| |
| asmb_lengths_dict[assembly_id] = (out_dict["macromolecule_L"], out_dict["protein_L"], out_dict["dna_L"], out_dict["rna_L"]) |
| asmb_interface_masks_dict[assembly_id] = interface_mask |
| asmb_side_chain_interface_masks_dict[assembly_id] = side_chain_interface_mask |
| asmb_nearest_protein_side_chain_index_dict[assembly_id] = nearest_protein_side_chain_index |
| asmb_base_pair_masks_dict[assembly_id] = base_pair_mask |
| asmb_base_pair_index_dict[assembly_id] = base_pair_index |
| asmb_canonical_base_pair_masks_dict[assembly_id] = canonical_base_pair_mask |
| asmb_canonical_base_pair_index_dict[assembly_id] = canonical_base_pair_index |
|
|
| if len(list(asmb_lengths_dict)) > 0: |
| chain_sequences_lines = ["chain_id,chain_type,sequence"] |
| for chain_sequence_line in chain_sequences: |
| chain_sequence_line = tuple(map(lambda x: "" if x is None else x, chain_sequence_line)) |
| chain_sequences_lines.append(",".join(chain_sequence_line)) |
| chain_sequences_str = "\n".join(chain_sequences_lines) |
| write_text_file(sequences_path, chain_sequences_str) |
|
|
| np.save(asmb_lengths_path, asmb_lengths_dict) |
| np.save(asmb_interface_masks_path, asmb_interface_masks_dict) |
| np.save(asmb_side_chain_interface_masks_path, asmb_side_chain_interface_masks_dict) |
| np.save(asmb_nearest_protein_side_chain_index_path, asmb_nearest_protein_side_chain_index_dict) |
| np.save(asmb_base_pair_masks_path, asmb_base_pair_masks_dict) |
| np.save(asmb_base_pair_index_path, asmb_base_pair_index_dict) |
| np.save(asmb_canonical_base_pair_masks_path, asmb_canonical_base_pair_masks_dict) |
| np.save(asmb_canonical_base_pair_index_path, asmb_canonical_base_pair_index_dict) |
| elif missing_na_count == len(assemblies): |
| write_text_file(bad_path, "all_assemblies_no_resolved_and_occupied_nucleic_acids") |
| continue |
| else: |
| write_text_file(bad_path, "all_assemblies_failed") |
| continue |