def convert_db2_to_mol2(db2mols): allmol2s = [] # loop over each molecule for mol in db2mols: # loop over each conformer in the molcule mol2mols = [] for conf in mol.conformer_list: #print conf.seglist # the conformer is defined as a set of segement of the molecule mol2atomlist = [] residue_list = {} for segint in conf.seglist: segment = mol.seg_list[segint - 1] print segment.num, segment.start, segment.stop # the segement point to a bunch of coordenates, we know what atom the coordenate coresponds to. #print segment.start,segment.stop, range(segment.start,segment.stop) for coordint in range(segment.start, segment.stop + 1): coord = mol.coord_list[coordint - 1] print coord.num, coord.atomnum, coord.segnum, coord.X, coord.Y, coord.Z tempatom = mol.atom_list[coord.atomnum - 1] #X,Y,Z,Q,type,name,num,resnum,resname): res_num = 1 resname = "lig" mol2atom = mol2.atom(coord.X, coord.Y, coord.Z, tempatom.Q, tempatom.type, tempatom.name, tempatom.num, res_num, resname) if residue_list.has_key(res_num): residue_list[res_num].append(mol2atom) else: residue_list[res_num] = [mol2atom] #residue_list[res_num] = [tempatom] mol2atomlist.append(mol2atom) mol2bondlist = [] for bond in mol.bond_list: #,a1_num,a2_num,num,type mol2bond = mol2.bond(bond.a1_num, bond.a2_num, bond.num, bond.type) mol2bondlist.append(mol2bond) mol2mol = mol2.Mol("", "", mol2atomlist, mol2bondlist, residue_list) mol2mols.append(mol2mol) allmol2s.append(mol2mols) #exit() #return allmol2s return allmol2s
for j, mol_3 in enumerate(mol_3list): print "molecule %d" % (j) #mol_3_new = copy.deepcopy(mol_3) atomlist = [] for i, move in enumerate(maping): #print i,move,maping[i] atom = mol_3.atom_list[move] atom.num = i + 1 atomlist.append(atom) #mol_3_new.atom_list = atomlist #bondlist = copy.deepcopy(mol_2.bond_list) bondlist = mol_2.bond_list header = mol_3.header name = mol_3.name residuelist = mol_3.residue_list mol_3_new = mol2.Mol(header, name, atomlist, bondlist, residuelist) print mol_3.header, mol_3_new.header # for bond in mol_3_new.bond_list: # print bond.a1_num, bond.a2_num, bond.num, bond.type # print atomlist[bond.a1_num-1].num, atomlist[bond.a2_num-1].num, bond.num, bond.type if j == 0: mol2.write_mol2(mol_3_new, outfile) else: mol2.append_mol2(mol_3_new, outfile) #print len(mol_list) #mol = mol2.remove_hydrogens( mol_list[0] ) #mol2.write_mol2(mol,outfile)
def combine_q_and_remove_hydrogens(m): atom_list = [] bond_list = [] #residue_list = {} atom_keep_dict = {} # proceed with only bonds containing 1 heavy atom and 1 hydrogen atom # check if it is a polar hydrogen ( if so continue) # add hydrogen charge to that of the heavy atom for bond_id in range(len(m.bond_list)): #retain_bond = True bond = m.bond_list[bond_id] a1 = m.atom_list[bond.a1_num - 1] a2 = m.atom_list[bond.a2_num - 1] #print (a2.type, a1.type) if (a1.heavy_atom and a2.heavy_atom): bond_list.append(bond) atom_keep_dict[bond.a1_num - 1] = True atom_keep_dict[bond.a2_num - 1] = True continue # Atoms down here are always hydrogen elif (a1.heavy_atom): if (a1.type in polar_heavy): print("polar ", a2.type, a1.type) bond_list.append(bond) atom_keep_dict[bond.a1_num - 1] = True atom_keep_dict[bond.a2_num - 1] = True continue print bond.a1_num, bond.a1_num, m.atom_list[ bond.a1_num - 1].Q, m.atom_list[bond.a2_num - 1].Q m.atom_list[bond.a1_num - 1].Q = m.atom_list[bond.a1_num - 1].Q + m.atom_list[bond.a2_num - 1].Q m.atom_list[bond.a2_num - 1].Q = 0 atom_keep_dict[bond.a1_num - 1] = True atom_keep_dict[bond.a2_num - 1] = False elif (a2.heavy_atom): if (a2.type in polar_heavy): print(a2.type, a1.type) bond_list.append(bond) atom_keep_dict[bond.a1_num - 1] = True atom_keep_dict[bond.a2_num - 1] = True continue print bond.a1_num, bond.a1_num, m.atom_list[ bond.a1_num - 1].Q, m.atom_list[bond.a2_num - 1].Q m.atom_list[bond.a2_num - 1].Q = m.atom_list[bond.a2_num - 1].Q + m.atom_list[bond.a1_num - 1].Q m.atom_list[bond.a1_num - 1].Q = 0 atom_keep_dict[bond.a2_num - 1] = True atom_keep_dict[bond.a1_num - 1] = False // hydrogen for atom_id in range(len(m.atom_list)): if (atom_keep_dict[atom_id]): atom_list.append(m.atom_list[atom_id]) # Assuming that residue list does not change m2 = mol2.Mol(m.header, m.name, atom_list, bond_list, m.residue_list) #return data return m2