def overlap_plot(self, receptor, ligand, T, name='overlap'): import _Volume vt1 = receptor.sum(dim=0) vt2 = ligand.sum(dim=0) _Volume.Volume2Xplor(vt1.cpu(), "receptor_%s.xplor" % name, self.resolution) _Volume.Volume2Xplor(vt2.cpu(), "ligand_%s.xplor" % name, self.resolution) dx = int(T[0]) dy = int(T[1]) dz = int(T[2]) L = self.box_size def mabs(dx, dy, dz): import numpy as np return np.abs(dx), np.abs(dy), np.abs(dz) #all positive if dx > 0 and dy > 0 and dz > 0: dx, dy, dz = mabs(dx, dy, dz) vt1[dx:L, dy:L, dz:L] += vt2[0:L - dx, 0:L - dy, 0:L - dz] #one negative elif dx < 0 and dy > 0 and dz > 0: dx, dy, dz = mabs(dx, dy, dz) vt1[0:L - dx, dy:L, dz:L] += vt2[dx:L, 0:L - dy, 0:L - dz] elif dx > 0 and dy < 0 and dz > 0: dx, dy, dz = mabs(dx, dy, dz) vt1[dx:L, 0:L - dy, dz:L] += vt2[0:L - dx, dy:L, 0:L - dz] elif dx > 0 and dy > 0 and dz < 0: dx, dy, dz = mabs(dx, dy, dz) vt1[dx:L, dy:L, 0:L - dz] += vt2[0:L - dx, 0:L - dy, dz:L] #one positive elif dx < 0 and dy < 0 and dz > 0: dx, dy, dz = mabs(dx, dy, dz) vt1[0:L - dx, 0:L - dy, dz:L] += vt2[dx:L, dy:L, 0:L - dz] elif dx > 0 and dy < 0 and dz < 0: dx, dy, dz = mabs(dx, dy, dz) vt1[dx:L, 0:L - dy, 0:L - dz] += vt2[0:L - dx, dy:L, dz:L] elif dx < 0 and dy > 0 and dz < 0: dx, dy, dz = mabs(dx, dy, dz) vt1[0:L - dx, dy:L, 0:L - dz] += vt2[dx:L, 0:L - dy, dz:L] #all negative elif dx < 0 and dy < 0 and dz < 0: dx, dy, dz = mabs(dx, dy, dz) vt1[0:L - dx, 0:L - dy, 0:L - dz] += vt2[dx:L, dy:L, dz:L] _Volume.Volume2Xplor(vt1.cpu(), "%s.xplor" % name, self.resolution)
def runTest(self): volume_gpu = self.tc2v(self.coords, self.num_atoms_of_type, self.offsets) volume = volume_gpu.sum(dim=1).to(device='cpu', dtype=torch.float) if not os.path.exists('TestFig'): os.mkdir('TestFig') _Volume.Volume2Xplor(volume[0,:,:,:], "TestFig/total_b0_vtest_%d_%.1f.xplor"%(self.box_size, self.resolution), self.resolution)
def plot_double_volume(self, volume, name='volume'): import _Volume L = volume.size(1) halfL = int(L / 2) doubleL = L * 2 output_volume = torch.zeros(doubleL, doubleL, doubleL, device='cpu', dtype=torch.float) output_volume[L - halfL:L + halfL, L - halfL:L + halfL, L - halfL:L + halfL] = volume[0:L, 0:L, 0:L] _Volume.Volume2Xplor(output_volume, "%s.xplor" % name, self.resolution)
def plot_circ_volume(self, circ_volume, name='volume'): import _Volume L = circ_volume.size(1) L2 = int(L / 2) output_volume = torch.zeros(L, L, L, device='cpu', dtype=torch.float) # quadrants 1 <--> 8 output_volume[:L2, :L2, :L2] = circ_volume[L2:L, L2:L, L2:L] output_volume[L2:L, L2:L, L2:L] = circ_volume[:L2, :L2, :L2] # quadrants 2 <--> 9 output_volume[L2:L, :L2, :L2] = circ_volume[:L2, L2:L, L2:L] output_volume[:L2, L2:L, L2:L] = circ_volume[L2:L, :L2, :L2] # quadrants 3 <--> 5 output_volume[L2:L, L2:L, :L2] = circ_volume[:L2, :L2, L2:L] output_volume[:L2, :L2, L2:L] = circ_volume[L2:L, L2:L, :L2] # quadrants 4 <--> 6 output_volume[:L2, L2:L, :L2] = circ_volume[L2:L, :L2, L2:L] output_volume[L2:L, :L2, L2:L] = circ_volume[:L2, L2:L, :L2] _Volume.Volume2Xplor(output_volume, "%s.xplor" % name, self.resolution)
inp1_border = torch.zeros(1, 1, v_size, v_size, v_size, dtype=torch.float, device='cuda') inp2 = torch.zeros(1, 1, v_size, v_size, v_size, dtype=torch.float, device='cuda') out2 = torch.zeros(1, 1, v_size, v_size, v_size, dtype=torch.float, device='cuda') fill_V1(np.array(r0), np.array(r1), 8, 5, inp1) get_boundary(inp1, inp1_border) fill_V2(np.array(l0), 5, inp2) # v = conv_numpy(inp1, inp2) # xyz = (0, 0, 0) # image1 = np.concatenate((v[xyz[0],:,:], v[:,xyz[1],:], v[:,:,xyz[2]]), axis = 1) # plt.imshow(image1) # plt.colorbar() # plt.show() _Volume.Volume2Xplor(inp1.squeeze().cpu(), "receptor.xplor", 1.0) _Volume.Volume2Xplor(inp1_border.squeeze().cpu(), "receptor_border.xplor", 1.0) _Volume.Volume2Xplor(inp2.squeeze().cpu(), "ligand.xplor", 1.0) border_overlap = vc(inp1_border, inp2) bulk_overlap = vc(inp1, inp2) print(bulk_overlap.size(), torch.max(bulk_overlap), torch.min(bulk_overlap)) out = border_overlap - 0.5*bulk_overlap _Volume.Volume2Xplor(out.squeeze().cpu(), "out.xplor", 1.0) score, r = get_argmax(out[0,0,:,:,:]) print(r) r = list(r) if r[0]>=v_size: r[0] = -(2*v_size - r[0]) if r[1]>=v_size:
resolution = 1.0 sequence = ['GGAGRRRGGWG'] angles = torch.zeros(len(sequence), 7, len(sequence[0]), dtype=torch.double) angles[0,0,:] = -1.047 angles[0,1,:] = -0.698 angles[0,2:,:] = 110.4*np.pi/180.0 a2c = Angles2Coords() c2cc = Coords2CenteredCoords(rotate=False, translate=False, box_size=box_size, resolution=resolution) c2tc = Coords2TypedCoords() tc2v = TypedCoords2Volume(box_size=box_size, resolution=resolution) protein, res_names, atom_names, num_atoms = a2c(angles, sequence) protein = c2cc(protein, num_atoms) coords, num_atoms_of_type, offsets = c2tc(protein, res_names, atom_names, num_atoms) volume = tc2v(coords.cuda(), num_atoms_of_type.cuda(), offsets.cuda()) # print(volume.size()) # for i in range(0, res_names.size(1)): # print(res_names.data[0,i,:].numpy().astype(dtype=np.uint8).tostring().split('\0')[0], atom_names.data[0,i,:].numpy().astype(dtype=np.uint8).tostring().split('\0')[0]) for i in range(0,11): print(num_atoms_of_type.data[0,i], offsets.data[0,i]) volume = volume.sum(dim=1).squeeze().cpu() print(volume.size()) if not os.path.exists('TestFig'): os.mkdir('TestFig') _Volume.Volume2Xplor(volume.squeeze(), "TestFig/total_vtest_%d_%.1f.xplor"%(box_size, resolution), resolution)
def test_gradient(): vc = VolumeConvolution() v_size = 30 inp1 = torch.zeros(1, 1, v_size, v_size, v_size, dtype=torch.float, device='cuda', requires_grad=True) inp1tmp_p = torch.zeros(1, 1, v_size, v_size, v_size, dtype=torch.float, device='cuda') inp1tmp_m = torch.zeros(1, 1, v_size, v_size, v_size, dtype=torch.float, device='cuda') inp2 = torch.zeros(1, 1, v_size, v_size, v_size, dtype=torch.float, device='cuda', requires_grad=True) target = torch.zeros(1, 1, 2 * v_size, 2 * v_size, 2 * v_size, dtype=torch.float, device='cuda') fill_V2(np.array([12, 30, 50]), 5, target) r0 = [20, 15, 15] r1 = [23, 15, 7] l0 = [7, 24, 7] fill_V1(np.array(r0), np.array(r1), 8, 5, inp1) fill_V2(np.array(l0), 5, inp2) _Volume.Volume2Xplor(inp1.squeeze().cpu(), "receptor.xplor", 1.0) _Volume.Volume2Xplor(inp2.squeeze().cpu(), "ligand.xplor", 1.0) out = vc(inp1, inp2) E = torch.sum((out - target) * (out - target)) # E = torch.sum(torch.abs(out-target)) # E = out[:,:,8,40,0] + out[:,:,10,30,25] E.backward() back_grad_x0 = torch.zeros(inp2.grad.size(), dtype=torch.float, device='cpu').copy_(inp2.grad) print("Grad max min", torch.max(back_grad_x0), torch.min(back_grad_x0)) _Volume.Volume2Xplor(back_grad_x0[0, 0, :, :, :], "grad_conv.xplor", 1.00) num_grads = [] an_grads = [] for j in range(0, v_size): x = j y = 7 z = 7 dx = 0.1 inp1tmp_p.copy_(inp2) inp1tmp_m.copy_(inp2) inp1tmp_p[0, 0, x, y, z] += dx inp1tmp_m[0, 0, x, y, z] -= dx # out1_p = vc(inp1tmp_p, inp2) # out1_m = vc(inp1tmp_m, inp2) out1_p = vc(inp1, inp1tmp_p) out1_m = vc(inp1, inp1tmp_m) E1_p = torch.sum((out1_p - target) * (out1_p - target)) E1_m = torch.sum((out1_m - target) * (out1_m - target)) # E1_p = torch.sum(torch.abs(out1_p-target)) # E1_m = torch.sum(torch.abs(out1_m-target)) # E1 = (out1[:,:,8,40,0] + out1[:,:,10,30,25]) dy_dx = (E1_p.item() - E1_m.item()) / (2 * dx) num_grads.append(dy_dx) an_grads.append(back_grad_x0[0, 0, x, y, z].item()) fig = plt.figure() plt.plot(num_grads, 'r.-', label='num grad') plt.plot(an_grads, 'bo', label='an grad') plt.legend() plt.savefig('TestFig/test_backward.png')
labels = labels.squeeze() print(receptor[0]) print(ligand[0]) print(labels[0]) print(labels) vt1 = volume1[0, :, :, :, :].squeeze() vt2 = volume2[0, :, :, :, :].squeeze() vt1 = vt1.sum(dim=0) vt2 = vt2.sum(dim=0) dx = int(dT[0, 0]) dy = int(dT[0, 1]) dz = int(dT[0, 2]) _Volume.Volume2Xplor(vt1.cpu(), "receptor.xplor", 1.5) _Volume.Volume2Xplor(vt2.cpu(), "ligand.xplor", 1.5) print(dx, dy, dz) L = 80 def mabs(dx, dy, dz): import numpy as np return np.abs(dx), np.abs(dy), np.abs(dz) #all positive if dx > 0 and dy > 0 and dz > 0: dx, dy, dz = mabs(dx, dy, dz) vt1[dx:L, dy:L, dz:L] += vt2[0:L - dx, 0:L - dy, 0:L - dz] #one negative
angles.data[:, 0, :] = -1.047 angles.data[:, 1, :] = -0.698 angles.data[:, 2:, :] = 110.4 * np.pi / 180.0 #Converting angles to coordinates coords, res_names, atom_names, num_atoms = a2c(angles, sequences) #Translating the structure to fit inside the volume translation = torch.tensor([[60, 60, 60]], dtype=torch.double, device='cpu') coords = translate(coords, translation, num_atoms) #Converting to typed coords manually #We need dimension 1 equal to 11, because right now 11 atom types are implemented and #this layer expects 11 offsets and 11 num atoms of type per each structure coords = coords.to(dtype=torch.double, device='cuda') num_atoms_of_type = torch.zeros(1, 11, dtype=torch.int, device='cuda') num_atoms_of_type.data[0, 0] = num_atoms.data[0] offsets = torch.zeros(1, 11, dtype=torch.int, device='cuda') offsets.data[:, 1:] = num_atoms.data[0] #Projecting to volume tc2v = Volume.TypedCoords2Volume(box_size=120) volume = tc2v(coords, num_atoms_of_type, offsets) #Saving the volume to a file, we need to sum all the atom types, because #Volume2Xplor currently accepts only 3d volumes volume = volume.sum(dim=1).to(device='cpu').squeeze() _Volume.Volume2Xplor(volume, "volume.xplor")
#Generating protein and initial volume protein, res_names, atom_names, num_atoms = a2c(angles, sequence) protein = c2cc(protein, num_atoms) coords, num_atoms_of_type, offsets = c2tc(protein, res_names, atom_names, num_atoms) volume = tc2v(coords.cuda(), num_atoms_of_type.cuda(), offsets.cuda()) #Rotating protein center = torch.zeros(len(sequence), 3, dtype=torch.double, device='cpu').fill_((box_size - 0.5)*resolution/2.0) centered_coords = translate(coords, -center, num_atoms) rotated_centered_coords = rotate(centered_coords, R, num_atoms) rotated_coords = translate(rotated_centered_coords, center, num_atoms) #Reprojecting protein volume_protRot = tc2v(rotated_coords.cuda(), num_atoms_of_type.cuda(), offsets.cuda()) #Rotating volume volume_rot = volume_rotate(volume, R.to(dtype=torch.float, device='cuda')) err = torch.sum(torch.abs(volume_protRot - volume_rot))/torch.sum(volume_rot) print(err) volume = volume.sum(dim=1).squeeze().cpu() volume_protRot = volume_protRot.sum(dim=1).squeeze().cpu() volume_rot = volume_rot.sum(dim=1).squeeze().cpu() if not os.path.exists('TestFig'): os.mkdir('TestFig') _Volume.Volume2Xplor(volume_protRot.squeeze(), "TestFig/total_vPRot_%d_%.1f.xplor"%(box_size, resolution), resolution) _Volume.Volume2Xplor(volume_rot.squeeze(), "TestFig/total_vRot_%d_%.1f.xplor"%(box_size, resolution), resolution) _Volume.Volume2Xplor(volume.squeeze(), "TestFig/total_v_%d_%.1f.xplor"%(box_size, resolution), resolution)
angles = torch.zeros(len(sequence), 7, len(sequence[0]), dtype=torch.double) angles[0, 0, :] = -1.047 angles[0, 1, :] = -0.698 angles[0, 2:, :] = 110.4 * np.pi / 180.0 a2c = Angles2Coords() c2cc = Coords2CenteredCoords() c2tc = Coords2TypedCoords() tc2v = TypedCoords2Volume() protein, res_names, atom_names, num_atoms = a2c(angles, sequence) protein = c2cc(protein, num_atoms) coords, num_atoms_of_type, offsets = c2tc(protein, res_names, atom_names, num_atoms) volume = tc2v(coords.cuda(), num_atoms_of_type.cuda(), offsets.cuda()) # print(volume.size()) # for i in range(0, res_names.size(1)): # print(res_names.data[0,i,:].numpy().astype(dtype=np.uint8).tostring().split('\0')[0], atom_names.data[0,i,:].numpy().astype(dtype=np.uint8).tostring().split('\0')[0]) for i in range(0, 11): print(num_atoms_of_type.data[0, i], offsets.data[0, i]) volume = volume.sum(dim=1).squeeze().cpu() print(volume.size()) if not os.path.exists('TestFig'): os.mkdir('TestFig') _Volume.Volume2Xplor(volume.squeeze(), "TestFig/total_vtest.xplor")