def fixResidueSeqNumbers(mol): curr_seq_no = 0 res_start = 0 res_end = 0 num_atoms = mol.getNumAtoms() old_res_seq_nos = [] old_to_new_seq_no_map = {} for atom in mol.atoms: old_res_seq_nos.append(Biomol.getResidueSequenceNumber(atom)) while res_start < num_atoms: while res_end < num_atoms and (old_res_seq_nos[res_start] == old_res_seq_nos[res_end]): res_end += 1 if not old_res_seq_nos[res_start] in old_to_new_seq_no_map: old_to_new_seq_no_map[old_res_seq_nos[res_start]] = curr_seq_no while res_start < res_end: Biomol.setResidueSequenceNumber(mol.getAtom(res_start), curr_seq_no) res_start += 1 curr_seq_no += 1 return old_res_seq_nos, old_to_new_seq_no_map
def setResidueSeqNumbers(mol, seq_nos): i = 0 while i < len(seq_nos): Biomol.setResidueSequenceNumber(mol.getAtom(i), seq_nos[i]) i += 1
def process(): if len(sys.argv) < 4: print >> sys.stderr, 'Usage:', sys.argv[ 0], '[input topology-file] [input coordinates-file] [output CDF-file]' sys.exit(2) print >> sys.stderr, '- Processing topology-file', sys.argv[ 1], 'and coordinates-file', sys.argv[2], '...' u = MDAnalysis.Universe(sys.argv[1], sys.argv[2]) cdf_mol = Chem.BasicMolecule() cdf_mol.reserveMemoryForAtoms(len(u.atoms)) cdf_mol.reserveMemoryForBonds(len(u.bonds)) print >> sys.stderr, '- Num. atoms:', len(u.atoms) print >> sys.stderr, '- Num. bonds:', len(u.bonds) num_frames = len(u.trajectory) print >> sys.stderr, '- Num. frames:', num_frames # construct atoms print >> sys.stderr, '- Building atoms ...' waters = {} i = 0 for md_atom in u.atoms: atom = cdf_mol.addAtom() sym = MDAnalysis.topology.guessers.guess_atom_element(md_atom.name) Chem.setSymbol(atom, sym.title()) Chem.setImplicitHydrogenCount(atom, 0) Biomol.setChainID(atom, md_atom.segid) if md_atom.resname == 'WAT': Biomol.setResidueCode(atom, 'HOH') else: Biomol.setResidueCode(atom, md_atom.resname) if Biomol.getResidueCode(atom) == 'HOH': if md_atom.resid in waters: waters[md_atom.resid].append(i) else: waters[md_atom.resid] = [i] Biomol.setResidueSequenceNumber(atom, int(md_atom.resid)) Biomol.setResidueAtomName(atom, md_atom.name) # fix positive charge on arginin nitrogen if md_atom.resname == 'ARG' and md_atom.name == 'NH2': Chem.setFormalCharge(atom, 1) coords = [] for coord in md_atom.position: coords.append(float(coord)) Chem.set3DCoordinates(atom, coords) coords_array = Math.Vector3DArray() coords_array.reserve(num_frames) Chem.set3DCoordinatesArray(atom, coords_array) Chem.setPEOECharge(atom, float(md_atom.charge)) i += 1 Chem.setAtomTypesFromSymbols(cdf_mol, True) # construct bonds print >> sys.stderr, '- Building bonds ...' for md_bond in u.bonds: cdf_mol.addBond(int(md_bond.atoms[0].index), int(md_bond.atoms[1].index)) print >> sys.stderr, '- Building water atom bonds ...' for water in waters.values(): if len(water) < 2: continue for atom_idx in water: if Chem.getType(cdf_mol.atoms[atom_idx]) == Chem.AtomType.O: if atom.numBonds > 1: break for atom_idx2 in water: if Chem.getType( cdf_mol.atoms[atom_idx2]) == Chem.AtomType.H: cdf_mol.addBond(atom_idx, atom_idx2) break # make sane biomolecule Chem.perceiveSSSR(cdf_mol, True) Chem.setRingFlags(cdf_mol, True) Chem.perceiveBondOrders(cdf_mol, True) Chem.perceiveHybridizationStates(cdf_mol, True) Chem.setAromaticityFlags(cdf_mol, True) Chem.calcFormalCharges(cdf_mol, True) # read timsteps and write cdf print >> sys.stderr, '- Importing coordinates ...' i = 0 traj_coords = [] atom_coords = Math.Vector3D() for ts in u.trajectory: print >> sys.stderr, '- Processing time step', i, '...' for md_atom in u.atoms: del traj_coords[:] for coord in md_atom.position: traj_coords.append(float(coord)) coords_array = Chem.get3DCoordinatesArray( cdf_mol.getAtom(int(md_atom.index))) atom_coords[0] = traj_coords[0] atom_coords[1] = traj_coords[1] atom_coords[2] = traj_coords[2] coords_array.addElement(atom_coords) i += 1 print >> sys.stderr, '- Writing output file:' if not Chem.FileCDFMolecularGraphWriter(sys.argv[3]).write(cdf_mol): print >> sys.stderr, '!! Could not write output file' sys.exit(2)