def chimera_to_tempy_model(model): """ Convert a chimera collection of atoms (model) into a tempy biopy structure """ atomlist = [] for atom in model.atoms: atomlist.append(chimera_to_tempy_atom(atom, len(atomlist))) return BioPy_Structure(atomlist)
def _biommCIF_strcuture_to_TEMpy(filename, structure, hetatm=False, water=False): #imported if and when the function is executed. """ PRIVATE FUNCTION to convert to Structure Instance filename = name of mmCIF file hetatm = Boolean representing whether to add hetatm to the structure.Default and Raccomanded is False. water = Boolean representing whether to add water to the structure.Default and Raccomanded is False. """ from Bio.PDB import MMCIFParser as MMCIFParserBiopy p = MMCIFParserBiopy() atomList = [] hetatomList = [] wateratomList = [] footer = '' header = '' cif_code = filename.split("/")[-1] #use os.1FAT.cif structure_id = "%s" % cif_code[:-4] structure = p.get_structure(structure_id, filename) residues = structure.get_residues() for res in residues: hetfield = res.get_id()[0] if hetfield[0] == "H": for atom in res: BioPyAtom(atom) hetatomList.append(BioPyAtom(atom)) elif hetfield[0] == "W": for atom in res: BioPyAtom(atom) wateratomList.append(BioPyAtom(atom)) else: for atom in res: BioPyAtom(atom) atomList.append(BioPyAtom(atom)) if hetatm: atomList = append(atomList, hetatomList) if water: atomList = append(atomList, wateratomList) return BioPy_Structure(atomList, filename=filename, header=header, footer=footer)
def score(session, atomic_model, map_model, rez): ''' Perform the CCC score. Takes a session, a single model and map.''' print("Calculating CCC Score") # make class instances for density simulation (blurring), scoring and plot scores blurrer = StructureBlurrer() scorer = ScoringFunctions() atomlist = [] for atom in atomic_model.atoms: atomlist.append(chimera_to_tempy_atom(atom, len(atomlist))) bio_atom_structure = BioPy_Structure(atomlist) bio_map_structure = chimera_to_tempy_map(map_model) map_probe = blurrer.gaussian_blur(bio_atom_structure, rez, densMap=bio_map_structure) score = scorer.CCC(bio_map_structure, map_probe) print(score) return score
def score(session, atomic_models, map_model, rigid_filename, rez, sim_sigma=0.187, window=9, colour_atoms=True): # TODO - rigid_filename might be optional? # TODO - this function is too long sc = ScoringFunctions() rvals = [] for atomic_model in atomic_models: atomlist = [] for atom in atomic_model.atoms: atomlist.append(chimera_to_tempy_atom(atom, len(atomlist))) bio_atom_structure = BioPy_Structure(atomlist) bio_map_structure = chimera_to_tempy_map(map_model) slow = 0.50 shigh = 0.25 # fraction of structure fitted reasonably well initially list_zscores = [] curdir = os.getcwd() rerun_ct = 0 flag_rerun = 0 it = 0 dict_reslist = {} dict_chains_scores = {} dict_ch_scores, dict_chain_res = sc.SMOC(bio_map_structure, rez, bio_atom_structure, window, rigid_filename, sim_sigma) for ch in dict_ch_scores: flagch = 1 dict_res_scores = dict_ch_scores[ch] #get res number list (for ref) if it == 0: dict_reslist[ch] = dict_chain_res[ch][:] try: if len(dict_reslist[ch]) == 0: print('Chain missing:', out_iter_pdb, ch) flagch = 0 continue except KeyError: print('Chain not common:', ch, out_iter_pdb) flagch = 0 continue try: reslist = dict_reslist[ch] except KeyError: print('Chain not common:', ch, out_iter_pdb) flagch = 0 continue if not ch in dict_chains_scores: dict_chains_scores[ch] = {} scorelist = [] for res in reslist: try: scorelist.append(dict_res_scores[res]) except KeyError: if reslist.index(res) <= 0: scorelist.append( dict_res_scores[reslist[reslist.index(res) + 1]]) else: try: scorelist.append( dict_res_scores[reslist[reslist.index(res) - 1]]) except IndexError: scorelist.append(0.0) #save scores for each chain curscore = "{0:.2f}".format(round(scorelist[-1], 2)) try: dict_chains_scores[ch][res][it] = str(curscore) except KeyError: dict_chains_scores[ch][res] = [str(0.0)] dict_chains_scores[ch][res][it] = str(curscore) #calc ratio between current and prev scores if it > 0: score_cur = scorelist[:] score_inc = [(1 + x) / (1 + y) for x, y in zip(score_cur, score_prev)][:] score_diff = [(x - y) for x, y in zip(score_cur, score_prev)][:] #calculate z-scores npscorelist = np.array(scorelist) try: list_zscores.append( (npscorelist - np.mean(npscorelist)) / np.std(npscorelist)) except: list_zscores.append((npscorelist - np.mean(npscorelist))) #calculate low and high score bounds list_sccc = scorelist[:] score_prev = scorelist[:] list_sccc.sort() #save avg of highest and lowest 20% avglow = list_sccc[int(len(list_sccc) * slow)] if avglow == 0.0: avglow = 0.00001 avghigh = list_sccc[int(len(list_sccc) * (1 - shigh))] if it == 0: avghigh1 = list_sccc[int(len(list_sccc) * (1 - shigh))] curratio = avghigh / avglow #print it, 'Num of good scoring residues', len(goodset) print(ch, 'avg-top25%, avg-low25%, avg-high/avg-low', avghigh, avglow, avghigh / avglow) print(ch, 'avg', sum(scorelist) / len(scorelist)) #include smoc scores as b-factor records for x in bio_atom_structure.atomList: cur_chain = x.chain cur_res = x.get_res_no() if not cur_chain in dict_reslist.keys(): continue if cur_chain in dict_chains_scores.keys(): try: x.temp_fac = dict_chains_scores[cur_chain][cur_res][it] except: print('Residue missing: ', cur_res, ch, out_iter_pdb) x.temp_fac = 0.0 else: x.temp_fac = 0.0 rvals.append((dict_chains_scores, dict_reslist)) return rvals
def score(session, atomic_model, map_model, rigid_filename, rez, sim_sigma=0.187, colour_atoms=True): """ Perform the SCCC score Takes a session, a single model, map, rigid file path and some tuneable optional variables """ print("Calculating SCCC Score") # make class instances for density simulation (blurring), scoring and plot scores blurrer = StructureBlurrer() scorer = ScoringFunctions() atomlist = [] # Pre-defines bio_atom_structure = "" bio_map_structure = "" try: for atom in atomic_model.atoms: atomlist.append(chimera_to_tempy_atom(atom, len(atomlist))) bio_atom_structure = BioPy_Structure(atomlist) bio_map_structure = chimera_to_tempy_map(map_model) # read rigid body file and generate structure instances for each segment listRB = RBParser.read_FlexEM_RIBFIND_files(rigid_filename, bio_atom_structure) except Exception as e: print(e) print( "Error in reading Model and Map. Make sure you have selected one model and one map, and the rigid file is correct." ) return # score each rigid body segment listsc_sccc = [] print('calculating SCCC') for RB in listRB: # sccc score score_SCCC = scorer.SCCC(bio_map_structure, rez, sim_sigma, bio_atom_structure, RB, c_mode=False) print('>>', score_SCCC) listsc_sccc.append((RB, score_SCCC)) # Colour the atoms based on the rating from white (1.0) to red (0.0) # TODO - maybe a faster way? Also 'all_atoms' mentioned in the API doesnt exist but atoms does! :S # TODO - move this to somewhere better maybe? if colour_atoms: dr = 255 dg = 255 db = 255 if score_SCCC >= 0.5: dr = 255 - int(math.floor(255 * ((score_SCCC - 0.5) * 2.0))) dg = dr else: db = int(math.floor(255 * (score_SCCC * 2.0))) dg = db residues = [] for a in RB.atomList: if a.res_no not in residues: residues.append(a.res_no) for r in residues: cr = atomic_model.residues[r] for catm in cr.atoms: catm.color = [dr, dg, db, 255] cr.ribbon_color = [dr, dg, db, 255] return listsc_sccc