def _load_mapping(file_name):
    """Internal function to load a mapping from file."""

    mapping = {}
    with open(file_name, "r") as file:
        for line in file:
            indices = line.split()
            mapping[AtomIdx(int(indices[0]))] = AtomIdx(int(indices[1]))

    return mapping
Exemple #2
0
def loadMapping(mapping_file):
    """Parse a text file that specifies mappings between atomic indices in input1 --> atoms in input2"""
    stream = open(mapping_file, 'r')
    buffer = stream.readlines()
    stream.close()
    mapping = {}
    for line in buffer:
        if line.startswith("#"):
            continue
        elems = line.split(",")
        idx1 = int(elems[0])
        idx2 = int(elems[1])
        mapping[AtomIdx(idx1)] = AtomIdx(idx2)

    return mapping
Exemple #3
0
def writeLog(ligA, ligB, mapping):
    """ Human readable report on atoms used for the mapping."""
    atoms_in_A = list(mapping.keys())
    stream = open('somd.mapping', 'w')
    atAdone = []
    atBdone = []
    for atAidx in atoms_in_A:
        atA = ligA._sire_object.select(AtomIdx(atAidx))
        atB = ligB._sire_object.select(AtomIdx(mapping[atAidx]))
        stream.write("%s %s --> %s %s\n" %
                     (atA.index(), atA.name(), atB.index(), atB.name()))
        atAdone.append(atA)
        atBdone.append(atB)
    for atom in ligA._sire_object.atoms():
        if atom in atAdone:
            continue
        stream.write("%s %s --> dummy\n" % (atom.index(), atom.name()))
    for atom in ligB._sire_object.atoms():
        if atom in atBdone:
            continue
        stream.write("dummy --> %s %s\n" % (atom.index(), atom.name()))
    stream.close()
Exemple #4
0
#print (custom_mapping)
if custom_mapping is not None:
    do_mapping = False
    mapping = loadMapping(custom_mapping)
    #print (mapping)

# In[10]:

# Optional input, dictionary of Atom indices that should be matched in the search.
prematch = {}
prematchstring = node.getInput("prematch")
if len(prematchstring) > 0:
    entries = prematchstring.split(",")
    for entry in entries:
        idxA, idxB = entry.split("-")
        prematch[AtomIdx(int(idxA))] = AtomIdx(int(idxB))
#print (prematch)

# In[11]:

# Load system 1
system1 = BSS.IO.readMolecules(node.getInput("input1"))

# In[12]:

# Load system 2
system2 = BSS.IO.readMolecules(node.getInput("input2"))

# In[13]:

# We assume the molecules to perturb are the first molecules in each system
Exemple #5
0
    BSS.IO.glob("%s/ligands_aligned/parametrised/CatS_%s.*" %
                (job_dir, num1))).getMolecules()[0]

# If a mapping file exists, then load the mapping. Otherwise, use BioSimSpace
# to create the mapping.
mapping = {}

# Forward mapping.
if os.path.isfile("%s/FESetup_mappings/merge_errors/%s_%s.txt" %
                  (job_dir, num0, num1)):
    with open(
            "%s/FESetup_mappings/merge_errors/%s_%s.txt" %
        (job_dir, num0, num1), "r") as file:
        for line in file:
            pair = line.strip().split()
            mapping[AtomIdx(int(pair[0]))] = AtomIdx(int(pair[1]))

# Reverse mapping.
elif os.path.isfile("%s/FESetup_mappings/merge_errors/%s_%s.txt" %
                    (job_dir, num1, num0)):
    with open(
            "%s/FESetup_mappings/merge_errors/%s_%s.txt" %
        (job_dir, num1, num0), "r") as file:
        for line in file:
            pair = line.strip().split()
            # Invert the indices.
            mapping[AtomIdx(int(pair[1]))] = AtomIdx(int(pair[0]))

# No mapping, generate it ourselves.
else:
    # Find the best mapping of atoms between the ligands.
Exemple #6
0
def test_merge():
    # Load the ligands.
    s0 = BSS.IO.readMolecules(BSS.IO.glob("test/io/ligands/ligand31*"))
    s1 = BSS.IO.readMolecules(BSS.IO.glob("test/io/ligands/ligand38*"))

    # Extract the molecules.
    m0 = s0.getMolecules()[0]
    m1 = s1.getMolecules()[0]

    # Get the best mapping between the molecules.
    mapping = BSS.Align.matchAtoms(m0, m1, timeout=BSS.Units.Time.second)

    # Align m0 to m1 based on the mapping.
    m0 = BSS.Align.rmsdAlign(m0, m1, mapping)

    # Create the merged molecule.
    m2 = BSS.Align.merge(m0, m1, mapping, allow_ring_breaking=True)

    # Store the number of atoms in m0.
    n0 = m0._sire_object.nAtoms()

    # Test that the intramolecular energies area the same.

    # IntraCLJFF:
    #  Old interface. Uses the "intrascale" matrix. Validate that this
    #  is consistent.
    # IntraFF:
    #  New interface. Uses atom "connectivity". Validate that the bonding
    #  is consistent.

    intraclj0 = IntraCLJFF("intraclj")
    intraclj0.add(m0._sire_object)

    intraff0 = IntraFF("intraclj")
    intraff0.add(m0._sire_object)

    intraclj1 = IntraCLJFF("intraclj")
    intraclj1.add(m1._sire_object)

    intraff1 = IntraFF("intraclj")
    intraff1.add(m1._sire_object)

    intraclj2 = IntraCLJFF("intraclj")
    intraff2 = IntraFF("intraclj")

    # Create maps between property names: { "prop" : "prop0" }, { "prop" : "prop1" }
    pmap0 = {}
    pmap1 = {}
    for prop in m2._sire_object.propertyKeys():
        if prop[-1] == "0":
            pmap0[prop[:-1]] = prop
        elif prop[-1] == "1":
            pmap1[prop[:-1]] = prop

    intraclj2.add(m2._sire_object, pmap0)
    intraff2.add(m2._sire_object, pmap0)

    assert intraclj0.energy().value() == pytest.approx(
        intraclj2.energy().value())
    assert intraff0.energy().value() == pytest.approx(
        intraff2.energy().value())

    intraclj2 = IntraCLJFF("intraclj")
    intraff2 = IntraFF("intraclj")

    intraclj2.add(m2._sire_object, pmap1)
    intraff2.add(m2._sire_object, pmap1)

    assert intraclj1.energy().value() == pytest.approx(
        intraclj2.energy().value())
    assert intraff1.energy().value() == pytest.approx(
        intraff2.energy().value())

    # Test that the internal energies are consistent. This will validate that
    # bond, angle, dihedral, and improper energies are correct.

    internalff0 = InternalFF("internal")
    internalff0.setStrict(True)
    internalff0.add(m0._sire_object)

    internalff1 = InternalFF("internal")
    internalff1.setStrict(True)
    internalff1.add(m1._sire_object)

    # First extract a partial molecule using the atoms from molecule0 in
    # the merged molecule.
    selection = m2._sire_object.selection()
    selection.deselectAll()
    for atom in m0._sire_object.atoms():
        selection.select(atom.index())
    partial_mol = PartialMolecule(m2._sire_object, selection)

    internalff2 = InternalFF("internal")
    internalff2.setStrict(True)
    internalff2.add(partial_mol, pmap0)

    assert internalff0.energy().value() == pytest.approx(
        internalff2.energy().value())

    # Now extract a partial molecule using the atoms from molecule1 in
    # the merged molecule.
    selection = m2._sire_object.selection()
    selection.deselectAll()
    for idx in mapping.keys():
        selection.select(AtomIdx(idx))
    for idx in range(n0, m2._sire_object.nAtoms()):
        selection.select(AtomIdx(idx))
    partial_mol = PartialMolecule(m2._sire_object, selection)

    internalff2 = InternalFF("internal")
    internalff2.setStrict(True)
    internalff2.add(partial_mol, pmap1)

    assert internalff1.energy().value() == pytest.approx(
        internalff2.energy().value())
from Sire.MM import InternalFF, IntraCLJFF, IntraFF
from Sire.Mol import AtomIdx, PartialMolecule

import BioSimSpace as BSS

import pytest

# Parameterise the function with a set of valid atom pre-matches.
@pytest.mark.parametrize("prematch", [{AtomIdx(3) : AtomIdx(1)},
                                      {AtomIdx(5) : AtomIdx(9)},
                                      {AtomIdx(4) : AtomIdx(5)},
                                      {AtomIdx(1) : AtomIdx(0)}])
def test_prematch(prematch):
    # Load the ligands.
    s0 = BSS.IO.readMolecules(BSS.IO.glob("test/io/ligands/ligand01*"))
    s1 = BSS.IO.readMolecules(BSS.IO.glob("test/io/ligands/ligand02*"))

    # Extract the molecules.
    m0 = s0.getMolecules()[0]
    m1 = s1.getMolecules()[0]

    # Get the best mapping between the molecules that contains the prematch.
    mapping = BSS.Align.matchAtoms(m0, m1, timeout=BSS.Units.Time.second,
                                   prematch=prematch)

    # Check that the prematch key:value pair is in the mapping.
    for key, value in prematch.items():
        assert mapping[key] == value

# Parameterise the function with a set of invalid atom pre-matches.
@pytest.mark.parametrize("prematch", [{AtomIdx(-1) : AtomIdx(1)},