def make_cg_mol(aa_mol, mapping, bead_names, bead_types): molname = aa_mol.graph['name'] cg_mol = Molecule(nrexcl=1, meta=dict(moltype=molname)) mapdict = defaultdict(list) for bd_idx, at_idxs in enumerate(mapping): name = bead_names[bd_idx] for member in at_idxs: mapdict[member].append(bd_idx) subgraph = aa_mol.subgraph(at_idxs) charge = sum(nx.get_node_attributes(subgraph, 'charge').values()) position = np.mean([ subgraph.nodes[idx].get('position', (np.nan, np.nan, np.nan)) for idx in subgraph ], axis=0) cg_mol.add_node(bd_idx, atomname=name, resname=molname, resid=1, atype=bead_types[bd_idx], charge_group=bd_idx + 1, graph=subgraph, charge=charge, position=position) for aa_idx, aa_jdx in aa_mol.edges: cg_idxs = mapdict[aa_idx] cg_jdxs = mapdict[aa_jdx] for cg_idx, cg_jdx in product(cg_idxs, cg_jdxs): if cg_idx != cg_jdx: cg_mol.add_edge(cg_idx, cg_jdx) for idx, jdx in cg_mol.edges: cg_mol.add_interaction('bonds', [idx, jdx], []) return cg_mol
def test_peptide(): """ Test multiple cg beads in residue """ gly = {'C': {'BB': 1}, 'N': {'BB': 1}, 'O': {'BB': 1}, 'CA': {'BB': 1}} ile = {'C': {'BB': 1}, 'N': {'BB': 1}, 'O': {'BB': 1}, 'CA': {'BB': 1}, 'CB': {'SC1': 1}, 'CG1': {'SC1': 1}, 'CG2': {'SC1': 1}, 'CD': {'SC1': 1}} leu = {'C': {'BB': 1}, 'N': {'BB': 1}, 'O': {'BB': 1}, 'CA': {'BB': 1}, 'CB': {'SC1': 1}, 'CG': {'SC1': 1}, 'CD1': {'SC1': 1}, 'CD2': {'SC1': 1}} extra = () mappings = {'universal': {'martini22': {}}} mappings['universal']['martini22']['GLY'] = Mapping(FF_UNIVERSAL.blocks['GLY'], FF_MARTINI.blocks['GLY'], mapping=gly, references={}, ff_from=FF_UNIVERSAL, ff_to=FF_MARTINI, names=('GLY',), extra=extra) mappings['universal']['martini22']['ILE'] = Mapping(FF_UNIVERSAL.blocks['ILE'], FF_MARTINI.blocks['ILE'], mapping=ile, references={}, ff_from=FF_UNIVERSAL, ff_to=FF_MARTINI, names=('ILE',), extra=extra) mappings['universal']['martini22']['LEU'] = Mapping(FF_UNIVERSAL.blocks['LEU'], FF_MARTINI.blocks['LEU'], mapping=leu, references={}, ff_from=FF_UNIVERSAL, ff_to=FF_MARTINI, names=('LEU',), extra=extra) peptide = Molecule(force_field=FF_UNIVERSAL) aa = FF_UNIVERSAL.blocks['GLY'].to_molecule() peptide.merge_molecule(aa) aa = FF_UNIVERSAL.blocks['ILE'].to_molecule() peptide.merge_molecule(aa) aa = FF_UNIVERSAL.blocks['LEU'].to_molecule() peptide.merge_molecule(aa) peptide.add_edge(list(peptide.find_atoms(atomname='N', resid=1))[0], list(peptide.find_atoms(atomname='C', resid=2))[0]) peptide.add_edge(list(peptide.find_atoms(atomname='N', resid=2))[0], list(peptide.find_atoms(atomname='C', resid=3))[0]) for node in peptide: peptide.nodes[node]['atomid'] = node + 1 peptide.nodes[node]['chain'] = '' cg = do_mapping(peptide, mappings, FF_MARTINI, attribute_keep=('chain',)) expected = Molecule(force_field=FF_MARTINI) expected.add_nodes_from({1: {'atomname': 'BB', 'atype': 'P5', 'chain': '', 'charge': 0.0, 'charge_group': 1, 'resid': 1, 'resname': 'GLY'}, 2: {'atomname': 'BB', 'atype': 'P5', 'chain': '', 'charge': 0.0, 'charge_group': 2, 'resid': 2, 'resname': 'ILE'}, 3: {'atomname': 'SC1', 'atype': 'AC1', 'chain': '', 'charge': 0.0, 'charge_group': 3, 'resid': 2, 'resname': 'ILE'}, 4: {'atomname': 'BB', 'atype': 'P5', 'chain': '', 'charge': 0.0, 'charge_group': 4, 'resid': 3, 'resname': 'LEU'}, 5: {'atomname': 'SC1', 'atype': 'AC1', 'chain': '', 'charge': 0.0, 'charge_group': 5, 'resid': 3, 'resname': 'LEU'}}.items() ) expected.add_edges_from([(1, 2), (2, 3), (2, 4), (4, 5)]) for node in expected: expected.nodes[node]['atomid'] = node + 1 print(cg.nodes(data=True)) print(cg.edges()) print('-'*80) print(expected.nodes(data=True)) print(expected.edges()) assert equal_graphs(cg, expected)
def test_peptide(): """ Test multiple cg beads in residue """ gly = { (0, 'C'): [(0, 'BB')], (0, 'N'): [(0, 'BB')], (0, 'O'): [(0, 'BB')], (0, 'CA'): [(0, 'BB')] } ile = { (0, 'C'): [(0, 'BB')], (0, 'N'): [(0, 'BB')], (0, 'O'): [(0, 'BB')], (0, 'CA'): [(0, 'BB')], (0, 'CB'): [(0, 'SC1')], (0, 'CG1'): [(0, 'SC1')], (0, 'CG2'): [(0, 'SC1')], (0, 'CD'): [(0, 'SC1')] } leu = { (0, 'C'): [(0, 'BB')], (0, 'N'): [(0, 'BB')], (0, 'O'): [(0, 'BB')], (0, 'CA'): [(0, 'BB')], (0, 'CB'): [(0, 'SC1')], (0, 'CG1'): [(0, 'SC1')], (0, 'CD1'): [(0, 'SC1')], (0, 'CD2'): [(0, 'SC1')] } extra = () mappings = { 'universal': { 'martini22': { 'GLY': (gly, _map_weights(gly), extra), 'ILE': (ile, _map_weights(ile), extra), 'LEU': (leu, _map_weights(leu), extra), } } } peptide = Molecule(force_field=FF_UNIVERSAL) aa = FF_UNIVERSAL.blocks['GLY'].to_molecule() peptide.merge_molecule(aa) aa = FF_UNIVERSAL.blocks['ILE'].to_molecule() peptide.merge_molecule(aa) aa = FF_UNIVERSAL.blocks['LEU'].to_molecule() peptide.merge_molecule(aa) peptide.add_edge( list(peptide.find_atoms(atomname='N', resid=1))[0], list(peptide.find_atoms(atomname='C', resid=2))[0]) peptide.add_edge( list(peptide.find_atoms(atomname='N', resid=2))[0], list(peptide.find_atoms(atomname='C', resid=3))[0]) for node in peptide: peptide.nodes[node]['atomid'] = node + 1 peptide.nodes[node]['chain'] = '' cg = do_mapping(peptide, mappings, FF_MARTINI) expected = Molecule(force_field=FF_MARTINI) expected.add_nodes_from({ 1: { 'atomname': 'BB', 'atype': 'P5', 'chain': '', 'charge': 0.0, 'charge_group': 1, 'resid': 1, 'resname': 'GLY' }, 2: { 'atomname': 'BB', 'atype': 'P5', 'chain': '', 'charge': 0.0, 'charge_group': 2, 'resid': 2, 'resname': 'ILE' }, 3: { 'atomname': 'SC1', 'atype': 'AC1', 'chain': '', 'charge': 0.0, 'charge_group': 2, 'resid': 2, 'resname': 'ILE' }, 4: { 'atomname': 'BB', 'atype': 'P5', 'chain': '', 'charge': 0.0, 'charge_group': 3, 'resid': 3, 'resname': 'LEU' }, 5: { 'atomname': 'SC1', 'atype': 'AC1', 'chain': '', 'charge': 0.0, 'charge_group': 3, 'resid': 3, 'resname': 'LEU' } }.items()) expected.add_edges_from([(1, 2), (2, 3), (2, 4), (4, 5)]) for node in expected: expected.nodes[node]['atomid'] = node + 1 assert _equal_graphs(cg, expected)