def make_polyglycine( chain_lengths, no_reserve=True): from pyxmolpp2.polymer import Frame from pyxmolpp2.polymer import ChainName from pyxmolpp2.polymer import AtomName from pyxmolpp2.polymer import ResidueName, ResidueId from pyxmolpp2.geometry import XYZ aid=1 rid=1 frame = Frame(0) for chainId, N in chain_lengths: if no_reserve: c = frame.emplace(ChainName(chainId)) else: c = frame.emplace(ChainName(chainId),N) for i in range(N): if no_reserve: r = c.emplace(ResidueName("GLY"),ResidueId(rid)) else: r = c.emplace(ResidueName("GLY"),ResidueId(rid),7) rid+=1 for aname in ["N","H","CA","HA2","HA3","C","O"]: r.emplace(AtomName(aname),aid,XYZ(1,2,3)) aid+=1 return frame
def test_dumb_copy_lookup(): from pyxmolpp2.polymer import Frame frame = make_polyglycine([("A", 3),("B", 3)]) frame2 = Frame(2) for c in frame.asChains: frame2.emplace(c) assert frame.asChains.size == frame2.asChains.size assert frame.asResidues.size == frame2.asResidues.size assert frame.asAtoms.size == frame2.asAtoms.size for x in frame2.asAtoms: assert x.residue[x.name] == x for x in frame2.asResidues: assert x.chain[x.id] == x for x in frame2.asChains: assert x.frame[x.name] == x assert x.frame == frame2
def test_Frame(): from pyxmolpp2.polymer import Frame from pyxmolpp2.polymer import ChainName from pyxmolpp2.polymer import AtomName from pyxmolpp2.polymer import ResidueName, ResidueId from pyxmolpp2.geometry import XYZ f = Frame(5) c = f.emplace(ChainName("A"), 1) r = c.emplace(ResidueName("LYS"), ResidueId(1)) a = r.emplace(AtomName("CA"), 1, XYZ(1,2,3)) assert a.residue == r assert a.chain == c assert a.frame == f assert r.asAtoms[0] == a assert c.asAtoms[0] == a assert f.asAtoms[0] == a assert r.asAtoms.asResidues[0] == r assert c.asAtoms.asResidues[0] == r assert f.asAtoms.asResidues[0] == r assert r.asAtoms.asResidues.asChains[0] == c assert c.asAtoms.asResidues.asChains[0] == c assert f.asAtoms.asResidues.asChains[0] == c assert r.chain == c assert r.frame == f assert c.frame == f assert c.asResidues[0] == r assert f.asResidues[0] == r assert f.asChains[0] == c
# For sake of simplicity let's number them in reverse order atoms = frame.asAtoms for i, a in enumerate(atoms): a.id = atoms.size - i # New atom ids: print([a.id for a in frame.asAtoms]) ############################################################################## # As you can see `atom.id` does not affect atom order. # To change that we need to construct new Frame with desired order of atoms: from pyxmolpp2.polymer import Frame new_frame = Frame(0) # create empty frame with index=0 for chain in frame.asChains: new_chain = new_frame.emplace( chain.name) # create empty chain with same name for residue in frame.asResidues: new_residue = new_chain.emplace( residue.name, residue.id) # create empty residue with same name and id for a in sorted(list(residue.asAtoms), key=lambda a: a.id): new_atom = new_residue.emplace(a) # create a copy of atom `a` # New frame atoms ids: print([a.id for a in new_frame.asAtoms])