def update_velocities(gro, vel_gro): """ Args: gro: vel_gro: """ gro_file = parser.read_gro(gro) vel_file = parser.read_gro(vel_gro) gro_coord = clean_fields(gro_file["coordinates"]) vel_coord = clean_fields(vel_file["coordinates"]) write = True new_coord = [] for i, x in enumerate(gro_coord): if (len(x) >= 8): print("Found velocities in gro - file! \n" + str(x) + "\n Abort transfer") write = False break else: if (len(vel_coord[i]) == 9): new_coord.append(x + vel_coord[i][6:]) elif (len(vel_coord[i]) == 8): new_coord.append(x + vel_coord[i][5:]) if (write): gro_file["coordinates"] = make_gro_atom_lines(new_coord) write_gro_file(gro_file, gro)
def append_structure_files(protein_gro_path, ligand_gro_path, output_path=False): """ Args: protein_gro_path: ligand_gro_path: output_path: """ gro_file_prot = parser.read_gro(protein_gro_path) gro_file_lig = parser.read_gro(ligand_gro_path) ligand_gro = clean_up_gro_file(gro_file_lig) protein_gro = clean_up_gro_file(gro_file_prot) combined_coordinates = (protein_gro["coordinates"] + ligand_gro["coordinates"]) print(combined_coordinates) compbined_gro = { "header": protein_gro["header"], "atoms": str( int(len(protein_gro["coordinates"])) + int(len(ligand_gro["coordinates"]))) + "\n", "coordinates": combined_coordinates, "term": protein_gro["term"] } if (not output_path): output_path = os.path.splitext(protein_gro_path)[0] + "_concat.gro" write_gro_file(compbined_gro, output_path) return output_path
def protonate_iterativley(gro_file_path, res_name="LIG", protonation_ph=7.4): """ Args: gro_file_path: res_name: protonation_ph: """ gro_file = parser.read_gro(gro_file_path) lines = clean_fields(gro_file["coordinates"]) resn_num, residue_nums, atom_nums = count_residue(lines) print("protonation found residues : " + str(resn_num)) tmp_res = -1 tmp_res_lines = [] protonated_list = [] for x in lines: if (res_name == x[1]): if (tmp_res != int(x[0])): if (len(tmp_res_lines)): protonated_list += protonate_lines(tmp_res_lines, gro_file_path, protonation_ph) resn_num -= 1 tmp_res = int(x[0]) tmp_res_lines = [x] else: tmp_res_lines.append(x) if (len(tmp_res_lines)): protonated_list += protonate_lines(tmp_res_lines, gro_file_path, protonation_ph) gro_file["atoms"] = str(len(protonated_list)) gro_file["coordinates"] = make_gro_atom_lines(protonated_list) gro_file_protonated_path = os.path.splitext( gro_file_path)[0] + "_protonated.gro" write_gro_file(gro_file, gro_file_protonated_path) return gro_file_protonated_path #
def H3O_to_H2O(protein_file, output, replace_times=1): #input """ Args: protein_file: output: replace_times: """ protein_gro = parser.read_gro(protein_file) # protein_coordinates = clean_fields(protein_gro["coordinates"]) #insert molecules - split coordinates in parts new_coordinates_H3O = [] new_coordinates_NA = [] new_coordinates_CL = [] new_coordinates_SOL = [] new_coordinates_rest = [] molecule = 1 if (replace_times == "all"): replace_times_atoms = len(protein_coordinates) else: replace_times_atoms = 6 * replace_times print("going to transform H3O -> H2O molecules: \tmols: " + str(replace_times) + "\t atoms: " + str(replace_times_atoms)) for line_prot in protein_coordinates: if ("H3O" in line_prot[1] and replace_times_atoms > 0): if (not "H31" in line_prot[2] and not "H32" in line_prot[2] and not "H33" in line_prot[2]): print("H3O replace: " + str(molecule)) print("Found: " + str(line_prot)) line_prot = [line_prot[0]] + ["SOL"] + line_prot[2:] new_coordinates_SOL.append(line_prot) molecule += 1 replace_times_atoms -= 1 print("remove atom " + str(replace_times_atoms) + " of " + str(replace_times)) continue else: print("ignore: " + str(line_prot)) replace_times_atoms -= 1 continue elif ("H3O" in line_prot[1]): new_coordinates_H3O.append(line_prot) elif ("SOL" in line_prot[1]): new_coordinates_SOL.append(line_prot) elif ("NA" in line_prot[1]): new_coordinates_NA.append(line_prot) elif ("CL" in line_prot[1]): new_coordinates_CL.append(line_prot) else: new_coordinates_rest.append(line_prot) new_coordinates = new_coordinates_rest + new_coordinates_H3O + new_coordinates_SOL + new_coordinates_NA + new_coordinates_CL print(" Length of new coordinates: " + str(len(new_coordinates))) protein_gro["atoms"] = str(len(new_coordinates)) protein_gro["coordinates"] = make_gro_atom_lines(new_coordinates) write_gro_file(protein_gro, output)
def update_system_mol_nums(gro_file, top_file): """ Args: gro_file: top_file: """ print("read in gro:") protein = parser.read_gro(gro_file) print("read in top:") top_segments = parser.read_top(top_file, coarse=True) print("original molecules" + str(top_segments["molecules"])) new_sys = [] for mols in top_segments["molecules"]: if (";" in mols): continue if ("protein" in mols or "Protein" in mols): new_sys.append(mols) continue else: name = mols.split(" ")[0].strip() residue_count, residue_nums, atom_nums = gro.count_residue( gro.clean_fields(protein["coordinates"]), residue_name=name) if (residue_count == 0): continue else: new_sys.append("{:<13}{:>10}\n".format(name, str(residue_count))) print("counted: \n" + str(new_sys)) top_segments["molecules"] = new_sys print("write updated top file") print_top_content(top_segments) write_super_top_file(top_segments, top_file, coarse=True)
def filter_file(input_gro_file, output_prefix=False, residue_name=False, atom_name=False, residue_number=False, atom_number=False): """ Args: input_gro_file: output_prefix: residue_name: atom_name: residue_number: atom_number: """ output_name = output_prefix if ( output_prefix) else os.path.splitext(input_gro_file)[0] gro_file = parser.read_gro(input_gro_file) filtered_lines = [] removed_lines = [] #get criteria for filtering: filter_dict = {} if (residue_number): filter_dict.update({0: residue_number}) if (residue_name): filter_dict.update({1: residue_name}) if (atom_name): filter_dict.update({2: atom_name}) if (atom_number): filter_dict.update({3: atom_number}) #filter: lines = clean_fields(gro_file["coordinates"]) for line in lines: if (all([ True if (line[key] == filter_dict[key]) else False for key in filter_dict ])): removed_lines.append(line) else: filtered_lines.append(line) filterd_name = output_name + "_filtered.gro" gro_file["atoms"] = str(len(filtered_lines)) gro_file["coordinates"] = make_gro_atom_lines(filtered_lines) write_gro_file(gro_file, filterd_name) removed_name = output_name + "_removed.gro" gro_file["atoms"] = str(len(removed_lines)) gro_file["coordinates"] = make_gro_atom_lines(removed_lines) write_gro_file(gro_file, removed_name) return filterd_name, removed_name
def protonate_lines(coordinate_lines, output_path, protonation_ph=7.4): """ Args: coordinate_lines: output_path: protonation_ph: """ lines = make_gro_atom_lines(coordinate_lines) tmp_gro = { "header": "tmp_file\n", "atoms": str(len(coordinate_lines)), "coordinates": lines, "term": "\n" } tmp_path = os.path.dirname(output_path) + "/tmp.gro" tmp_path = write_gro_file(tmp_gro, tmp_path) #THINK ABOUT HOW TO CHECK OPENBABEL! protonation_command = "module load openbabel/2.4.1\n module load gcc/5.3.0\n obabel -igro " + tmp_path + " -ogro -O " + tmp_path + " -p " + str( protonation_ph) + "\n" gf.execute_bash(protonation_command) tmp_gro_file = parser.read_gro(tmp_path) protonated_lines = clean_fields(tmp_gro_file["coordinates"]) return protonated_lines
def insert_h3O(selection_file, protein_file, insert_mol, output, selection_name="waterSel", insert_times=1): # input """ Args: selection_file: protein_file: insert_mol: output: selection_name: insert_times: """ selection_dict = parser.read_ndx_file(selection_file) protein_gro = parser.read_gro(protein_file) # protein_coordinates = gro.clean_fields(protein_gro["coordinates"]) insert_gro = parser.read_gro(insert_mol) # get random SOL residues from selection # getting precise selection keay selection_key = "" for x in selection_dict.keys(): if (selection_name in x): selection_key = x break positions = len(selection_dict[selection_key]) replace_residues = [] replace_positions = [] random.seed() for i in range(insert_times): x = random.randint(0, positions - 1) print("for pos " + str(x) + "\t\t" + str(selection_dict[selection_key][x])) replace_positions.append(x) replace_residues.append(selection_dict[selection_key][x]) print("Replace position: \t" + str(replace_positions) + " from total pos: " + str(positions)) print("\nReplace Residue: \t" + str(replace_residues)) # get residues, to be replaced replace_molecule = [] for i, line_prot in enumerate(protein_coordinates): for x in replace_residues: if (i == int(x)): print("found: " + str(x) + "\t" + str(line_prot)) replace_residues.remove(x) replace_molecule.append(line_prot[0]) print("\nreplace mol: " + str(replace_molecule)) # insert molecules - split coordinates in parts print("Map Coordinates on H2Os") new_coordinates_H3O = [] new_coordinates_NA = [] new_coordinates_CL = [] new_coordinates_SOL = [] new_coordinates_rest = [] molecule = 1 for line_prot in protein_coordinates: if (line_prot[0] in replace_molecule): if ("OW" in line_prot[1]): print("Found: " + str(line_prot)) insert_coordinates = gro.clean_fields( insert_gro["coordinates"]) new_coordinates_H3O += move_h3o_on_water( line_prot, insert_coordinates, molecule) molecule += 1 else: print("ignore: " + str(line_prot)) continue elif ("SOL" in line_prot[0]): new_coordinates_SOL.append(line_prot) elif ("NA" in line_prot[0]): new_coordinates_NA.append(line_prot) elif ("CL" in line_prot[0]): new_coordinates_CL.append(line_prot) else: new_coordinates_rest.append(line_prot) new_coordinates = new_coordinates_rest + new_coordinates_H3O + new_coordinates_NA + new_coordinates_CL + new_coordinates_SOL print(" Length of new coordinates: " + str(len(new_coordinates))) protein_gro["atoms"] = str(len(new_coordinates)) protein_gro["coordinates"] = gro.make_clean_gro_atom_lines(new_coordinates) gro.write_gro_file(protein_gro, output)
def build_lambda_file(output_folder, gro_file, initial_lambdas=[], options=False, barrier=False, his_atoms=False, h3o_atoms=False, residues=False, his_3state=True): """ Args: output_folder: STRING path to folder gro_file: STRING path to gro file initial_lambdas: LIST(float) of initial lambda numbers options: DICT barrier: FLOAT setting the simulation energy barrier his_atoms: LIST(int) h3o_atoms: LIST(int) residues: LIST(int) his_3state: """ print("Building Lambda file") if (options): his_atoms = options["his_atoms"] h3o_atoms = options["h3o_atoms"] residues = options["residues"] barrier = options["cphmd_barrier"] if (barrier and his_atoms and h3o_atoms and residues): raise Exception( "There are not all necessary arguments set for build_lambda_file.") print("importatnt his: " + str(his_atoms)) gro_lines = parser.read_gro(gro_file + ".gro") gro_coord = gro.clean_fields(gro_lines["coordinates"]) lambda_file = open(output_folder + "/lambda_groups.dat", "w") print("start_writing lambda_groups.dat") print("got: " + str(initial_lambdas)) if len(initial_lambdas) == 0: initial_lambdas = ["0" for x in range(2 * len(residues)) ] # init with double protonated all residues initial_lambdas.append("1") #set H3O to opposite of hisitidines print("init_l_values:" + str(initial_lambdas)) init_lambda_ndx = 0 for residue in residues: atoms_list = gro.get_residue_atoms(residue, "HIS", gro_coord, his_atoms) atoms = "" for x in atoms_list: atoms += str(x) + " " print(residue + " found: " + atoms) print("index " + str(init_lambda_ndx)) print("hu" + str(initial_lambdas[init_lambda_ndx])) HIA_line = ("name = HI2A\n" "residue_number = " + str(residue) + "\n" "initial_lambda = " + str(initial_lambdas[init_lambda_ndx]) + "\n" "barrier = " + str(barrier) + "\n" "points = 0\n" "interval_adapt_barr = 0\n" "number_of_atoms = 9\n" "" + atoms + "\n\n") init_lambda_ndx += 1 lambda_file.write(HIA_line) print("wrote_HIA") if (his_3state): HIB_line = ("name = HI2B\n" "residue_number = " + str(residue) + "\n" "initial_lambda = " + str(initial_lambdas[init_lambda_ndx]) + "\n" "barrier = " + str(barrier) + "\n" "points = 0\n" "interval_adapt_barr = 0\n" "number_of_atoms = 9\n" "" + atoms + "\n\n") lambda_file.write(HIB_line) print("wrote_HIB") init_lambda_ndx += 1 print("wrote_lines") atoms_list = gro.get_residue_atoms(False, "H3O", gro_coord, h3o_atoms) atoms = "" for x in atoms_list: atoms += str(x) + " " Buffer_line = ( # add buffer "name = H3OB\n" "residue_number = " + str(int(len(atoms_list) / 6)) + "\n" "initial_lambda = " + str(initial_lambdas[init_lambda_ndx]) + "\n" "barrier = 0\n" "points = 0\n" "interval_adapt_barr = 0\n" "number_of_atoms = " + str(len(atoms_list)) + "\n" "" + atoms + "\n") lambda_file.write(Buffer_line) lambda_file.close()
def map_ligand_atom_names_to_new_ligand_pos(ligands_gro_path, ligand_template_gro_path, res_name="LIG"): """maps all atom names of a template which has the same atom order to all atoms of another molecule :param ligands_gro_path: :param ligand_template_gro_path: :param res_name: :return: Args: ligands_gro_path: ligand_template_gro_path: res_name: """ #INPUT: print("start Mapping Process:\n Cleaning old_pos") ligand_positions = parser.read_gro(ligands_gro_path) clean_new_pos = clean_fields( ligand_positions["coordinates"]) # new complexfile # print(clean_ligand) print("\n Cleaning new_pos") ligand_template = parser.read_gro(ligand_template_gro_path) clean_ligand = clean_fields(ligand_template["coordinates"]) # print(clean_new_pos) #Initial Analyse #count_residuest to map. residue_count, resn_numbers, atoms_per_res = count_residue( clean_new_pos, res_name) print("found new residue_positions: " + str(residue_count)) print("each residue and its atom count: \n" + str(atoms_per_res)) print("start Mapping") #filter lines for residue and seperate them: lines_to_map = {} for residue in resn_numbers: lines_to_map.update({ residue: [line for line in clean_new_pos if (line[0] == residue)] }) print("total num of residues: " + str(len(resn_numbers))) for residue in resn_numbers: print("res: " + str(residue) + "\t" + str(len(lines_to_map[residue])) + "\n") #DO #vars: mapped_residues_lines = [] # map lines for each residue seperate: for residue in resn_numbers: tmp_ligand = clean_ligand map_list = lines_to_map[residue] atom_count = 0 mapped_sub = [] print("mapping residue: " + str(residue)) #Mapping process for y in tmp_ligand: for x in map_list: if (re.search(x[2], y[2])): mapped_line = [x[0]] + y[1:4] + x[4:] mapped_sub.append(mapped_line) map_list.remove(x) atom_count += 1 break if (len(map_list) == 0): mapped_residues_lines += mapped_sub else: print("skipping this molecule! it has more atoms!\n" + str(map_list)) print("len of mapped_list: " + str(len(mapped_residues_lines)) + " added lines: " + str(atom_count)) #Result gro_lines = make_gro_atom_lines(mapped_residues_lines, renumber=True) ligand_positions["atoms"] = str(len(gro_lines)) ligand_positions["coordinates"] = gro_lines mapped_path = os.path.splitext(ligands_gro_path)[0] + "_mapped_lines.gro" write_gro_file(ligand_positions, mapped_path) return mapped_path