Example #1
0
 def test_update_positions():
     ff = vermouth.forcefield.ForceField(name='test_ff')
     meta_mol = MetaMolecule(name="test", force_field=ff)
     meta_mol.add_monomer(0, "PEO", [])
     meta_mol.add_monomer(1, "PEO", [(1, 0)])
     meta_mol.add_monomer(2, "PEO", [(1, 2)])
     meta_mol.nodes[0]["position"] = np.array([0, 0, 0])
     meta_mol.nodes[1]["position"] = np.array([0, 0, 0.5])
     meta_mol.volumes = {}
     meta_mol.volumes["PEO"] = 0.5
     vectors = polyply.src.linalg_functions.norm_sphere(50)
     update_positions(vectors, meta_mol, 2, 1)
     assert "position" in meta_mol.nodes[2]
Example #2
0
def gen_itp(args):

    # Import of Itp and FF files
    force_field = load_library(args.name, args.lib, args.inpath)

    # Generate the MetaMolecule
    if args.seq:
        monomers = split_seq_string(args.seq)
        meta_molecule = MetaMolecule.from_monomer_seq_linear(
            monomers=monomers, force_field=force_field, mol_name=args.name)
    #ToDo
    # fix too broad except
    elif args.seq_file:
        extension = args.seq_file.suffix.casefold()[1:]
        try:
            parser = getattr(MetaMolecule, 'from_{}'.format(extension))
            meta_molecule = parser(json_file=args.seq_file,
                                   force_field=force_field,
                                   mol_name=args.name)
        except AttributeError:
            raise IOError(
                "Cannot parse file with extension {}.".format(extension))

    # Do transformationa and apply link
    meta_molecule = MapToMolecule().run_molecule(meta_molecule)
    meta_molecule = ApplyLinks().run_molecule(meta_molecule)

    with open(args.outpath, 'w') as outpath:
        vermouth.gmx.itp.write_molecule_itp(meta_molecule.molecule,
                                            outpath,
                                            moltype=args.name,
                                            header=["polyply-itp"])
    DeferredFileWriter().write()
Example #3
0
 def test_run_molecule():
     ff = vermouth.forcefield.ForceField(name='test_ff')
     meta_mol = MetaMolecule(name="test", force_field=ff)
     meta_mol.add_monomer(0, "PEO", [])
     meta_mol.add_monomer(1, "PEO", [(1, 0)])
     meta_mol.add_monomer(2, "PEO", [(1, 2)])
     meta_mol.volumes = {}
     meta_mol.volumes["PEO"] = 0.5
     RandomWalk().run_molecule(meta_mol)
     for node in meta_mol.nodes:
         assert "position" in meta_mol.nodes[node]
Example #4
0
def test_backmapping():
    meta_molecule = MetaMolecule()
    meta_molecule.add_edges_from([(0, 1), (1, 2)])
    nx.set_node_attributes(meta_molecule, {0: {"resname": "test",
                                               "position": np.array([0, 0, 0]),
                                               "resid": 1, "build": True},
                                           1: {"resname": "test",
                                               "position": np.array([0, 0, 1.0]),
                                               "resid": 2, "build": True},
                                           2: {"resname": "test",
                                               "position": np.array([0, 0, 2.0]),
                                               "resid": 3, "build": False}})
    # test if disordered template works
    meta_molecule.templates = {"test": {"B": np.array([0, 0, 0]),
                                        "A": np.array([0, 0, 0.5]),
                                        "C": np.array([0, 0.5, 0])}}
    meta_molecule.molecule = vermouth.molecule.Molecule()
    meta_molecule.molecule.add_edges_from(
        [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7)])
    nx.set_node_attributes(meta_molecule.molecule, {
        1: {"resname": "test", "resid": 1, "atomname": "A"},
        2: {"resname": "test", "resid": 1, "atomname": "B"},
        3: {"resname": "test", "resid": 1, "atomname": "C"},
        4: {"resname": "test", "resid": 2, "atomname": "A"},
        5: {"resname": "test", "resid": 2, "atomname": "B"},
        6: {"resname": "test", "resid": 2, "atomname": "C"},
        7: {"resname": "test", "resid": 3, "atomname": "C",
            "position": np.array([4., 4., 4.])}
        })

    Backmap().run_molecule(meta_molecule)
    for node in meta_molecule.molecule.nodes:
        assert "position" in meta_molecule.molecule.nodes[node]
    assert norm(meta_molecule.molecule.nodes[7]["position"] - np.array([4., 4., 4.])) == 0
Example #5
0
 def test__is_overlap(new_point, tol, fudge, reference):
     ff = vermouth.forcefield.ForceField(name='test_ff')
     meta_mol = MetaMolecule(name="test", force_field=ff)
     meta_mol.add_monomer(0, "PEO", [])
     meta_mol.add_monomer(1, "PEO", [(1, 0)])
     meta_mol.add_monomer(2, "PEO", [(1, 2)])
     meta_mol.nodes[0]["position"] = np.array([0, 0, 0])
     meta_mol.nodes[1]["position"] = np.array([0, 0, 0.5])
     result = _is_overlap(meta_mol,
                          new_point,
                          tol,
                          current_node=2,
                          fudge=fudge)
     assert result == reference
Example #6
0
def test_find_missing_links():
    fname = TEST_DATA + "/gen_params/ref/P3HT_10.itp"
    ff = vermouth.forcefield.ForceField("test")
    meta_mol = MetaMolecule.from_itp(ff, fname, "P3HTref")
    meta_mol.molecule.remove_edge(39, 45)  # resid 7,8
    meta_mol.molecule.remove_edge(15, 21)  # resid 3,4
    missing = list(find_missing_edges(meta_mol, meta_mol.molecule))
    assert len(missing) == 2
    for edge, ref in zip(missing, [(3, 4), (7, 8)]):
        assert edge["resA"] == "P3HTref"
        assert edge["resB"] == "P3HTref"
        assert edge["idxA"] == ref[0]
        assert edge["idxB"] == ref[1]
Example #7
0
def gen_params(name, outpath, inpath=None, lib=None, seq=None, seq_file=None):
    """
    Top level function for running the polyply parameter generation.
    Parameters seq and seq_file are mutually exclusive. Set the other
    to None. Of inpath and lib only one has to be given. Set the other
    to None if not used.

    Parameters
    ----------
    name: str
        name of the molecule in the itp file
    outpath: :class:`pathlib.Path`
        file path for output file
    inpath: list[pathlib.Path]
        list of paths to files with input definitions
    library: str
        name of the force field library to use
    seq: list[str]
        list of strings with format "resname:#monomers"
    seqf: :class:`pathlib.Path`
        file path to valid sequence file (.json/.fasta/.ig/.txt)
    """
    # Import of Itp and FF files
    LOGGER.info("reading input and library files", type="step")
    force_field = load_library(name, lib, inpath)

    # Generate the MetaMolecule
    if seq:
        LOGGER.info("reading sequence from command", type="step")
        monomers = split_seq_string(seq)
        meta_molecule = MetaMolecule.from_monomer_seq_linear(
            monomers=monomers, force_field=force_field, mol_name=name)
    elif seq_file:
        LOGGER.info("reading sequence from file", type="step")
        meta_molecule = MetaMolecule.from_sequence_file(
            force_field, seq_file, name)

    # Do transformationa and apply link
    LOGGER.info("mapping sequence to molecule", type="step")
    meta_molecule = MapToMolecule(force_field).run_molecule(meta_molecule)
    LOGGER.info("applying links between residues", type="step")
    meta_molecule = ApplyLinks().run_molecule(meta_molecule)

    # Raise warning if molecule is disconnected
    if not nx.is_connected(meta_molecule.molecule):
        LOGGER.warning("Your molecule consists of disjoint parts."
                       "Perhaps links were not applied correctly.")
        msg = "Missing link between residue {idxA} {resA} and residue {idxB} {resB}"
        for missing in find_missing_edges(meta_molecule,
                                          meta_molecule.molecule):
            LOGGER.warning(msg, **missing)

    with deferred_open(outpath, 'w') as outfile:
        header = [' '.join(sys.argv) + "\n"]
        header.append("Please cite the following papers:")
        for citation in meta_molecule.molecule.citations:
            cite_string = citation_formatter(
                meta_molecule.molecule.force_field.citations[citation])
            LOGGER.info("Please cite: " + cite_string)
            header.append(cite_string)

        vermouth.gmx.itp.write_molecule_itp(meta_molecule.molecule,
                                            outfile,
                                            moltype=name,
                                            header=header)
    DeferredFileWriter().write()