def atom_information(pdbdata, mode): #analyze pdb file parser = PDBParser(QUIET=True, PERMISSIVE=True) structure = parser.get_structure('model', pdbdata) #DSSP prediction pmodel = structure[0] dssp = DSSP(pmodel, pdbdata) #Set variables global coordinates global color global radius global chains global chain_coords global chain_colors if mode == 'cpk': #list of atoms atoms = [atom for atom in structure.get_atoms()] natoms = len(atoms) #atom coordinates coordinates = np.array([atom.coord for atom in atoms]) center = centroid(coordinates) coordinates -= center #atom color color = [colorrgba(atom.get_id()) for atom in atoms] #atom radius radius = np.array([vrad(atom.get_id()) for atom in atoms]) elif mode == 'aminoacid': #list of atoms atoms = [ atom for atom in structure.get_atoms() if atom.get_parent().resname != 'HOH' ] natoms = len(atoms) #atom coordinates coordinates = np.array([atom.coord for atom in atoms]) center = centroid(coordinates) coordinates -= center #atom color color = [ colorrgba(restype(atom.get_parent().resname)) for atom in atoms ] #atom radius radius = np.array([vrad(atom.get_id()) for atom in atoms]) elif mode == 'backbone': #list of atoms atoms = [ atom for atom in structure.get_atoms() if atom.get_name() == 'CA' or atom.get_name() == 'N' ] natoms = len(atoms) #atom coordinates coordinates = np.array([atom.coord for atom in atoms]) center = centroid(coordinates) coordinates -= center #atom color color = [] #list of arrays of coordinates and colors for each chain chains = [] chain_colors = [] chain_coords = [] for chain in structure.get_chains(): chains.append(chain) can_coord = np.array([ atom.coord for atom in chain.get_atoms() if atom.get_name() == 'CA' or atom.get_name() == 'N' ]) can_coord -= center chain_coords.append(can_coord) chain_length = len(can_coord) chain_color = np.append(np.random.rand(1, 3), [1.0]) chain_colors.append(chain_color) color.append(np.tile(chain_color, (chain_length, 1))) if len(chains) > 1: color = np.concatenate(color) #atom radius radius = np.array([vrad(atom.get_id()) for atom in atoms]) elif mode == 'dssp': #list of atoms atoms = [ atom for atom in structure.get_atoms() if atom.get_name() == 'CA' or atom.get_name() == 'N' ] natoms = len(atoms) #atom coordinates coordinates = np.array([atom.coord for atom in atoms]) center = centroid(coordinates) coordinates -= center #atom color struct3 = [dssp[key][2] for key in list(dssp.keys())] residues = [ residue for residue in structure.get_residues() if residue.get_resname() in resdict.keys() ] color = [] for i in range(len(struct3)): dsspcolor = crgbaDSSP(struct3[i]) n_atoms = len([ atom for atom in residues[i] if atom.get_name() == 'CA' or atom.get_name() == 'N' ]) color.append(np.tile(dsspcolor, (n_atoms, 1))) if len(struct3) > 1: color = np.concatenate(color) #list of arrays of coordinates and colors for each chain chains = [] chain_colors = [] chain_coords = [] for chain in structure.get_chains(): chains.append(chain) chain_color = np.append(np.random.rand(1, 3), [1.0]) chain_colors.append(chain_color) can_coord = np.array([ atom.coord for atom in chain.get_atoms() if atom.get_name() == 'CA' or atom.get_name() == 'N' ]) can_coord -= center chain_coords.append(can_coord) #atom radius radius = np.array([vrad(atom.get_id()) for atom in atoms])
def aa2d(): #Seleccionamos los residuos, solo contando los aminoacidos for resname, residuetype in resdict.iteritems(): residues = [residue for residue in structure.get_residues() if residue.get_resname() == resname] rescoord = [] color = colorrgba(residuetype) for residue in residues: atoms = [atom for atom in residue.get_atoms()] coordinates = [atom.coord for atom in atoms] rescoord.append(np.array(coordinates)) if len(rescoord)>1: rescoord = np.concatenate(rescoord) if not len(residues)==0: x, y, z =zip(*rescoord) ax.scatter(x, y, z, c=color, marker='o') #Seleccionamos el resto de 'residuos' que ha determinado el parser, sin incluir aguas residues_1 = [residue for residue in structure.get_residues() if residue.get_resname() != 'HOH'] residues_2 = [residue for residue in structure.get_residues() if residue.get_resname() in resdict.keys()] residues_pink = list(set(residues_1)-set(residues_2)) rescoordpink = [] for residue in residues_pink: atomspink = [atom for atom in residue.get_atoms()] coordinatespink = [atom.coord for atom in atomspink] rescoordpink.append(np.array(coordinatespink)) if len(rescoordpink)>1: rescoordpink = np.concatenate(rescoordpink) xp, yp, zp = zip(*rescoordpink) ax.scatter(xp, yp, zp, c='pink', marker='o') ax.axis("off") plt.show()
def dssp2d(): ax = fig.add_subplot(111, projection='3d') #Creamos las listas de residuos vinculadas a su prediccion residues = [residue for residue in structure.get_residues() if residue.get_resname() in resdict.keys()] struct3 = [dssp[key][2] for key in list(dssp.keys())] respred = zip(struct3,residues) #Creamos la nube de puntos por prediccion for prediction, color in colorsDSSP.iteritems(): residuesp = [residue[1] for residue in respred if residue[0] == prediction] predcoord_can = [] for residue in residuesp: atomsp = [atom for atom in residue.get_atoms() if atom.get_name() == 'CA' or atom.get_name() == 'N'] coordinatesp = [atom.coord for atom in atomsp] predcoord_can.append(np.array(coordinatesp)) if len(predcoord_can)>1: predcoord_can = np.concatenate(predcoord_can) if not len(residuesp)==0: x, y, z = zip(*predcoord_can) ax.scatter(x, y, z, c=color, marker='o') #Creamos las cadenas que unen los atomos for chain in structure.get_chains(): can_atoms = [atom for atom in chain.get_atoms() if atom.get_name() == 'CA' or atom.get_name() == 'N'] can_coordinates = [atom.coord for atom in can_atoms] x, y, z = zip(*can_coordinates) ccolor = np.random.rand(3,1) ax.plot(x, y, z, c=ccolor, linewidth=1) ax.axis("off") plt.show()
def aa2d(self): """Draws atoms using a colour palette depending on the type of residue""" fig = plt.figure() ax = fig.add_subplot(111, projection='3d') #Select residues only if they're aminoacids for resname, residuetype in resdict.iteritems(): residues = [residue for residue in self.structure.get_residues() if residue.get_resname() == resname] rescoord = [] color = colorrgba(residuetype) for residue in residues: atoms = [atom for atom in residue.get_atoms()] coordinates = [atom.coord for atom in atoms] rescoord.append(np.array(coordinates)) if len(rescoord)>1: rescoord = np.concatenate(rescoord) if not len(residues)==0: x, y, z =zip(*rescoord) ax.scatter(x, y, z, c=color, marker='o') #Select residues that are not aminoacids, skipping water residues_1 = [residue for residue in self.structure.get_residues() if residue.get_resname() != 'HOH'] residues_2 = [residue for residue in self.structure.get_residues() if residue.get_resname() in resdict.keys()] residues_pink = list(set(residues_1)-set(residues_2)) rescoordpink = [] for residue in residues_pink: atomspink = [atom for atom in residue.get_atoms()] coordinatespink = [atom.coord for atom in atomspink] rescoordpink.append(np.array(coordinatespink)) if len(rescoordpink)>1: rescoordpink = np.concatenate(rescoordpink) xp, yp, zp = zip(*rescoordpink) ax.scatter(xp, yp, zp, c='pink', marker='o') ax.axis("off") plt.show()
def dssp2d(self): """Draw CA-N atoms linked by lines, coloured by their tertiary structure prediction""" fig = plt.figure() ax = fig.add_subplot(111, projection='3d') #Create the residue lists linked to their predictions residues = [residue for residue in self.structure.get_residues() if residue.get_resname() in resdict.keys()] struct3 = [self.dssp[key][2] for key in list(self.dssp.keys())] respred = zip(struct3,residues) #Create the point cloud depending on the prediction for prediction, color in colorsDSSP.iteritems(): residuesp = [residue[1] for residue in respred if residue[0] == prediction] predcoord_can = [] for residue in residuesp: atomsp = [atom for atom in residue.get_atoms() if atom.get_name() == 'CA' or atom.get_name() == 'N'] coordinatesp = [atom.coord for atom in atomsp] predcoord_can.append(np.array(coordinatesp)) if len(predcoord_can)>1: predcoord_can = np.concatenate(predcoord_can) if not len(residuesp)==0: x, y, z = zip(*predcoord_can) ax.scatter(x, y, z, c=color, marker='o') #Create the chains linking the aminoacids for chain in self.structure.get_chains(): can_atoms = [atom for atom in chain.get_atoms() if atom.get_name() == 'CA' or atom.get_name() == 'N'] can_coordinates = [atom.coord for atom in can_atoms] x, y, z = zip(*can_coordinates) ccolor = np.random.rand(3,1) ax.plot(x, y, z, c=ccolor, linewidth=1) ax.axis("off") plt.show()
def __init__(self): ShowBase.__init__(self) self.cloud = False self.help = False self.screen_text = [] #Desglosamos archivo PDB pdbdata = sys.argv[1] parser = PDBParser(QUIET=True, PERMISSIVE=True) structure = parser.get_structure('model', pdbdata) #Hacemos la prediccion DSSP model = structure[0] dssp = DSSP(model, pdbdata) #Creamos los modelos self.cpknode = render.attachNewNode("CPK") self.aanode = render.attachNewNode("Aminoacids") self.bbnode = render.attachNewNode("BackBone") self.dsspnode = render.attachNewNode("DSSP") self.nnode = render.attachNewNode("Cloud") #CPK for atom in structure.get_atoms(): x, y, z = atom.coord atomid = atom.get_id() a = loader.loadModel("data/atom_sphere") a.setPos(x, y, z) a.reparentTo(self.cpknode) a.setColor(colorrgba(atomid)) a.setScale(vrad(atomid)) self.cpknode.flattenStrong() #Aminoacids self.residues = [ residue for residue in structure.get_residues() if residue.get_resname() in resdict.keys() ] for residue in self.residues: resid = residue.get_resname() color = colorrgba(restype(resid)) atoms = [atom for atom in residue.get_atoms()] for atom in atoms: x, y, z = atom.coord atomid = atom.get_id() a = loader.loadModel("data/atom_sphere") a.setPos(x, y, z) a.setColor(color) a.setScale(vrad(atomid)) a.reparentTo(self.aanode) self.residues2 = [ residue for residue in structure.get_residues() if not residue in self.residues and residue.get_resname() != 'HOH' ] for residue in self.residues2: atoms = [atom for atom in residue.get_atoms()] for atom in atoms: x, y, z = atom.coord atomid = atom.get_id() a = loader.loadModel("data/atom_sphere") a.setPos(x, y, z) a.setColor(colorrgba(atomid)) a.setScale(vrad(atomid)) a.reparentTo(self.aanode) self.aanode.flattenStrong() self.aanode.hide() #Backbone for chain in structure.get_chains(): carr = np.random.rand(3, 1) ccolor = float(carr[0]), float(carr[1]), float(carr[2]), 1.0 can_atoms = [ atom for atom in chain.get_atoms() if atom.get_name() == 'CA' or atom.get_name() == 'N' ] can_coordinates = [atom.coord for atom in can_atoms] for atom in can_atoms: x, y, z = atom.coord atomid = atom.get_id() a = loader.loadModel("data/atom_sphere") a.setPos(x, y, z) a.reparentTo(self.bbnode) a.setColor(ccolor) a.setScale(vrad(atomid) / 2.5) lines = LineSegs() lines.setColor(ccolor) lines.moveTo(can_coordinates[0][0], can_coordinates[0][1], can_coordinates[0][2]) for i in range(len(can_atoms))[1:]: lines.drawTo(can_coordinates[i][0], can_coordinates[i][1], can_coordinates[i][2]) lines.setThickness(6) lnode = lines.create() self.linenp = NodePath(lnode) self.linenp.instanceTo(self.bbnode) #Cloud catoms = [atom for atom in chain.get_atoms()] for atom in catoms: x, y, z = atom.coord atomid = atom.get_id() a = loader.loadModel("data/atom_sphere") a.setPos(x, y, z) a.reparentTo(self.nnode) a.setColor(ccolor) a.setScale(vrad(atomid) * 1.1) self.bbnode.flattenStrong() self.bbnode.hide() self.nnode.setTransparency(TransparencyAttrib.MAlpha) self.nnode.setAlphaScale(0.3) self.nnode.hide() #DSSP self.linenp.instanceTo(self.dsspnode) self.struct3 = [dssp[key][2] for key in list(dssp.keys())] for i in range(len(self.struct3)): dsspcolor = crgbaDSSP(self.struct3[i]) can_atoms = [ atom for atom in self.residues[i] if atom.get_name() == 'CA' or atom.get_name() == 'N' ] for atom in can_atoms: x, y, z = atom.coord atomid = atom.get_id() a = loader.loadModel("data/atom_sphere") a.setPos(x, y, z) a.reparentTo(self.dsspnode) a.setColor(dsspcolor) a.setScale(vrad(atomid) / 2.5) self.dsspnode.flattenStrong() self.dsspnode.hide() #Colocamos la proteina en el centro self.cpknode.setPos(0, 0, 0) self.bbnode.setPos(0, 0, 0) self.aanode.setPos(0, 0, 0) self.nnode.setPos(0, 0, 0) #Colocamos la camara en el centro xc, yc, zc = self.cpknode.getBounds().getCenter() self.center = xc, yc, zc self.pradius = self.cpknode.getBounds().getRadius() self.center_camera() #Creamos la iluminacion de ambiente self.ambient = AmbientLight('alight') self.ambient.setColor(LVecBase4f(0.16, 0.16, 0.17, 1.0)) self.alight = render.attachNewNode(self.ambient) render.setLight(self.alight) #Creamos la iluminacion direccional self.directional = DirectionalLight('dlight') self.directional.setColor(LVecBase4f(0.8, 0.7, 0.75, 1.0)) self.directional.setShadowCaster(True, 512, 512) render.setShaderAuto() self.dlight = render.attachNewNode(self.directional) self.dlight.setPos(0, -50, 0) render.setLight(self.dlight) self.dlight.lookAt(self.cpknode.getBounds().getCenter()) # Post procesado render.setAntialias(AntialiasAttrib.MAuto) #Teclado self.accept('c', self.toggle_cloud) self.accept('1', self.showmodel, [self.cpknode]) self.accept('2', self.showmodel, [self.aanode]) self.accept('3', self.showmodel, [self.bbnode]) self.accept('4', self.showmodel, [self.dsspnode]) self.accept('x', self.center_camera) self.accept('arrow_left', self.taskMgr.add, [self.spinCameraTaskX, "SpinCameraTaskX"]) self.accept('arrow_up', self.taskMgr.add, [self.spinCameraTaskY, "SpinCameraTaskY"]) self.accept('arrow_down', self.stop_camera) self.accept('escape', sys.exit)
chain_length = len(can_coord) chain_color = np.random.rand(1, 3) chain_colors.append(chain_color) color.append(np.tile(chain_color, (chain_length, 1))) chain_radius.append([ vrad(atom.get_id()) for atom in chain.get_atoms() if atom.get_name() == 'CA' or atom.get_name() == 'N' ]) if len(chains) > 1: color = np.concatenate(color) #dssp color2 = [] struct3 = [dssp[key][2] for key in list(dssp.keys())] residues = [ residue for residue in structure.get_residues() if residue.get_resname() in resdict.keys() ] for i in range(len(struct3)): dsspcolor = crgbaDSSP(struct3[i])[0:3] n_atoms = len([ atom for atom in residues[i] if atom.get_name() == 'CA' or atom.get_name() == 'N' ]) color2.append(np.tile(dsspcolor, (n_atoms, 1))) if len(struct3) > 1: color2 = np.concatenate(color2) #atom radius radius = [ 4 * vrad(atom.get_id()) for atom in atoms if atom.get_name() == 'CA' or atom.get_name() == 'N' ]
def atom_information(self): """Determines the coordinates, colors and sizes of the atoms depending on the mode""" if self.mode == 'cpk': #list of atoms self.atoms = [atom for atom in self.structure.get_atoms()] self.natoms = len(self.atoms) #atom coordinates self.coordinates = np.array([atom.coord for atom in self.atoms]) self.center = centroid(self.coordinates) self.coordinates -= self.center #atom color self.color = [ np.array(colorrgba(atom.get_id())[0:3]) for atom in self.atoms ] #atom radius self.radius = [vrad(atom.get_id()) for atom in self.atoms] elif self.mode == 'aminoacid': #list of atoms self.atoms = [ atom for atom in self.structure.get_atoms() if atom.get_parent().resname != 'HOH' ] self.natoms = len(self.atoms) #atom coordinates self.coordinates = np.array([atom.coord for atom in self.atoms]) self.center = centroid(self.coordinates) self.coordinates -= self.center #atom color self.color = [ colorrgba(restype(atom.get_parent().resname))[0:3] for atom in self.atoms ] #atom radius self.radius = [vrad(atom.get_id()) for atom in self.atoms] elif self.mode == 'backbone': #list of atoms self.atoms = [ atom for atom in self.structure.get_atoms() if atom.get_name() == 'CA' or atom.get_name() == 'N' ] self.natoms = len(self.atoms) #atom coordinates self.coordinates = np.array([atom.coord for atom in self.atoms]) self.center = centroid(self.coordinates) self.coordinates -= self.center #atom color self.color = [] self.chains = [] for chain in self.structure.get_chains(): self.chains.append(chain) self.chain_length = len([ atom for atom in chain.get_atoms() if atom.get_name() == 'CA' or atom.get_name() == 'N' ]) self.chain_color = np.random.rand(1, 3) self.color.append( np.tile(self.chain_color, (self.chain_length, 1))) if len(self.chains) > 1: self.color = np.concatenate(self.color) #atom radius self.radius = [vrad(atom.get_id()) for atom in self.atoms] elif self.mode == 'dssp': #list of atoms self.atoms = [ atom for atom in self.structure.get_atoms() if atom.get_name() == 'CA' or atom.get_name() == 'N' ] self.natoms = len(self.atoms) #atom coordinates self.coordinates = np.array([atom.coord for atom in self.atoms]) self.center = centroid(self.coordinates) self.coordinates -= self.center #atom color self.struct3 = [ self.dssp[key][2] for key in list(self.dssp.keys()) ] self.residues = [ residue for residue in self.structure.get_residues() if residue.get_resname() in resdict.keys() ] self.color = [] for i in range(len(self.struct3)): self.dsspcolor = crgbaDSSP(self.struct3[i])[0:3] self.n_atoms = len([ atom for atom in self.residues[i] if atom.get_name() == 'CA' or atom.get_name() == 'N' ]) self.color.append(np.tile(self.dsspcolor, (self.n_atoms, 1))) if len(self.struct3) > 1: self.color = np.concatenate(self.color) #atom radius self.radius = [vrad(atom.get_id()) for atom in self.atoms]