Ejemplo n.º 1
0
def test_box_vectors():
    box_vector_q = unit.Quantity(
        [[64.0, 0.0, 0.0], [-21.0, 61.0, 0.0], [-21.0, -30.0, 53.0]],
        unit=unit.angstrom)
    box_vector = base.Box_vectors()
    box_vector.from_quantity(box_vector_q)
    assert np.isclose(box_vector.ax, 6.4)
    assert np.isclose(box_vector.ay, 0.0)
    assert np.isclose(box_vector.az, 0.0)
    assert np.isclose(box_vector.bx, -2.1)
    assert np.isclose(box_vector.by, 6.1)
    assert np.isclose(box_vector.bz, 0.0)
    assert np.isclose(box_vector.cx, -2.1)
    assert np.isclose(box_vector.cy, -3.0)
    assert np.isclose(box_vector.cz, 5.3)
    box_vector_q2 = box_vector.to_quantity()
    assert np.isclose(box_vector_q2.value_in_unit(unit.nanometers)[0][0], 6.4)
    assert np.isclose(box_vector_q2.value_in_unit(unit.nanometers)[0][1], 0.0)
    assert np.isclose(box_vector_q2.value_in_unit(unit.nanometers)[0][2], 0.0)
    assert np.isclose(box_vector_q2.value_in_unit(unit.nanometers)[1][0], -2.1)
    assert np.isclose(box_vector_q2.value_in_unit(unit.nanometers)[1][1], 6.1)
    assert np.isclose(box_vector_q2.value_in_unit(unit.nanometers)[1][2], 0.0)
    assert np.isclose(box_vector_q2.value_in_unit(unit.nanometers)[2][0], -2.1)
    assert np.isclose(box_vector_q2.value_in_unit(unit.nanometers)[2][1], -3.0)
    assert np.isclose(box_vector_q2.value_in_unit(unit.nanometers)[2][2], 5.3)
Ejemplo n.º 2
0
def make_amber_params(anchor, building_dir, engine="openmm"):
    anchor.amber_params = base.Amber_params()

    this_dir = os.path.dirname(os.path.realpath(__file__))

    if engine == "openmm":
        prmtop_src = os.path.join(this_dir,
                                  "../data/hostguest_files/hostguest.parm7")
    elif engine == "namd":
        prmtop_src = os.path.join(
            this_dir, "../data/hostguest_files/hostguest_for_NAMD.parm7")
    else:
        raise Exception("Engine not implemented: %s" % engine)
    inpcrd_src = os.path.join(this_dir,
                              "../data/hostguest_files/hostguest.rst7")
    pdb_coord_src = os.path.join(
        this_dir, "../data/hostguest_files/hostguest_at1.5.pdb")

    prmtop_dest = os.path.join(building_dir, "hostguest.parm7")
    #inpcrd_dest = os.path.join(building_dir, "hostguest.rst7")
    pdb_coord_dest = os.path.join(building_dir, "hostguest_at1.5.pdb")

    copyfile(prmtop_src, prmtop_dest)
    #copyfile(inpcrd_src, inpcrd_dest)
    copyfile(pdb_coord_src, pdb_coord_dest)

    anchor.amber_params.prmtop_filename = "hostguest.parm7"
    #anchor.amber_params.inpcrd_filename = "hostguest.rst7"
    anchor.amber_params.pdb_coordinates_filename = "hostguest_at1.5.pdb"
    anchor.amber_params.box_vectors = base.Box_vectors()
    anchor.amber_params.box_vectors.from_quantity(
        parmed.load_file(pdb_coord_src).box_vectors)
    return
Ejemplo n.º 3
0
def check_pre_sim_MD_and_BD_salt_concentration(model):
    """
    Users might inadvertently define different salt concentrations 
    between the MD and BD stages. 
    Examine BD settings and count the numbers of ions in MD starting
    structures to ensure that ion concentrations are relatively 
    consistent to avoid situations where different salt concentrations
    exist between the various anchors and scales.
    """

    RELATIVE_TOLERANCE = 0.25
    ABSOLUTE_TOLERANCE = 0.1
    if model.k_on_info:
        bd_ionic_strength = 0.0
        for ion in model.k_on_info.ions:
            bd_ionic_strength += 0.5 * ion.conc * ion.charge**2
    else:
        # No BD to check
        return True
    for anchor in model.anchors:
        md_ionic_strength = 0.0
        structure = load_structure_with_parmed(model, anchor)
        if structure is None:
            continue
        box_6_vector = structure.get_box()
        assert box_6_vector is not None, "Unable to compute box volume for "\
            "structures in anchor {}".format(anchor.index)
        box_vectors = base.Box_vectors()
        box_vectors.from_6_vector(box_6_vector[0])
        box_volume = box_vectors.get_volume()
        particle_concentration = 1.0e24 / (AVOGADROS_NUMBER * box_volume)
        for index, atom in enumerate(structure.atoms):
            if is_ion(atom):
                found_ion = False
                for key in ION_CHARGE_DICT:
                    if atom.name.lower().startswith(key):
                        charge = ION_CHARGE_DICT[key]
                        md_ionic_strength += particle_concentration * charge**2
                        found_ion = True

                if not found_ion:
                    print("Found unbonded atom with index: "\
                          "{} and name: {}.".format(index, atom.name)\
                          +"Charge is uncertain, assuming zero.")
        if not np.isclose(md_ionic_strength,
                          bd_ionic_strength,
                          rtol=RELATIVE_TOLERANCE,
                          atol=ABSOLUTE_TOLERANCE):
            print("""CHECK FAILURE: BD simulation has significantly different
    ionic strength of {} M*e^2 than MD simulation ionic strength of 
    {} M*e^2 for anchor {}. Please check the ion concentrations in the 
    BD simulation settings, and also count the number of ions 
    in the MD simulations.""".format(bd_ionic_strength, md_ionic_strength,
                                     anchor.index))
            return False

    return True
Ejemplo n.º 4
0
def make_forcefield_params(anchor, building_dir):
    anchor.forcefield_params = base.Forcefield_params()

    this_dir = os.path.dirname(os.path.realpath(__file__))

    xml_src = os.path.join(this_dir, "../data/hostguest_files/hostguest.xml")
    pdb_coord_src = os.path.join(
        this_dir, "../data/hostguest_files/hostguest_for_xml.pdb")

    xml_dest = os.path.join(building_dir, "hostguest.xml")
    pdb_coord_dest = os.path.join(building_dir, "hostguest_for_xml.pdb")

    copyfile(xml_src, xml_dest)
    copyfile(pdb_coord_src, pdb_coord_dest)

    anchor.forcefield_params.built_in_forcefield_filenames = \
        ["amber14/tip3pfb.xml"]
    anchor.forcefield_params.custom_forcefield_filenames = ["hostguest.xml"]
    anchor.forcefield_params.pdb_filename = "hostguest_for_xml.pdb"
    anchor.forcefield_params.box_vectors = base.Box_vectors()
    anchor.forcefield_params.box_vectors.from_quantity(
        parmed.load_file(pdb_coord_src).box_vectors)
    return
Ejemplo n.º 5
0
def copy_building_files_by_anchor(anchor, input_anchor, rootdir):
    """
    For each of the anchors and other directories, copy the necessary
    initial simulation (building) files for the simulations into those
    directories.
    
    Parameters:
    -----------
    anchor : Anchor
        the Anchor to copy the building files into.
        
    input_anchor : Input_anchor
        Input_model's Input_anchor() objects which contain the input 
        files to copy.
        
    rootdir : str
        A path to the model's root directory.
    
    """
    if not anchor.md:
        return
    
    if anchor.__class__.__name__ in ["MMVT_toy_anchor", "Elber_toy_anchor"]:
        anchor.starting_positions = input_anchor.starting_positions
        return
    
    anchor_building_dir = os.path.join(rootdir, anchor.directory, 
                                    anchor.building_directory)
    assert os.path.exists(anchor_building_dir)
    
    amber = input_anchor.starting_amber_params
    new_prmtop_filename = None
    
    if amber is not None:
        anchor.amber_params = base.Amber_params()
        if amber.prmtop_filename is not None and \
                amber.prmtop_filename != "":
            amber.prmtop_filename = os.path.expanduser(
                amber.prmtop_filename)
            assert os.path.exists(amber.prmtop_filename), \
                "Provided file does not exist: {}".format(
                    amber.prmtop_filename)
            prmtop_filename = os.path.basename(amber.prmtop_filename)
            new_prmtop_filename = os.path.join(anchor_building_dir, 
                                               prmtop_filename)
            copyfile(amber.prmtop_filename, new_prmtop_filename)
            anchor.amber_params.prmtop_filename = prmtop_filename
            
        if amber.pdb_coordinates_filename is not None and \
                amber.pdb_coordinates_filename != "":
            pdb_filename = os.path.basename(amber.pdb_coordinates_filename)
            new_pdb_filename = os.path.join(anchor_building_dir, 
                                            pdb_filename)
            copyfile(os.path.expanduser(amber.pdb_coordinates_filename), 
                     new_pdb_filename)
            anchor.amber_params.pdb_coordinates_filename = pdb_filename
            anchor.amber_params.box_vectors = amber.box_vectors
            if anchor.amber_params.box_vectors is None:
                anchor.amber_params.box_vectors = base.Box_vectors()
                box_vectors = base.get_box_vectors_from_pdb(new_pdb_filename)
                anchor.amber_params.box_vectors.from_quantity(
                    box_vectors)
        
    forcefield = input_anchor.starting_forcefield_params
        
    if forcefield is not None:
        assert amber is None, "Parameters may not be included for both "\
            "Amber and Forcefield inputs."
        anchor.forcefield_params = base.Forcefield_params()
        if forcefield.built_in_forcefield_filenames is not None and \
                len(forcefield.built_in_forcefield_filenames) > 0:
            for filename in forcefield.built_in_forcefield_filenames:
                anchor.forcefield_params.built_in_forcefield_filenames.\
                    append(os.path.expanduser(filename))
                    
        if forcefield.custom_forcefield_filenames is not None and \
                len(forcefield.custom_forcefield_filenames) > 0:
            for filename in forcefield.custom_forcefield_filenames:
                ff_filename = os.path.basename(filename)
                new_ff_filename = os.path.join(anchor_building_dir, 
                                               ff_filename)
                copyfile(filename, new_ff_filename)
                anchor.forcefield_params.custom_forcefield_filenames.\
                    append(os.path.expanduser(ff_filename))
        
        if forcefield.pdb_coordinates_filename is not None and \
                forcefield.pdb_coordinates_filename != "":
            forcefield.pdb_coordinates_filename = os.path.expanduser(
                forcefield.pdb_coordinates_filename)
            assert os.path.exists(forcefield.pdb_coordinates_filename), \
                "Provided file does not exist: {}".format(
                    forcefield.pdb_coordinates_filename)
            pdb_filename = os.path.basename(forcefield.pdb_coordinates_filename)
            new_pdb_filename = os.path.join(anchor_building_dir, 
                                            pdb_filename)
            copyfile(forcefield.pdb_coordinates_filename, new_pdb_filename)
            anchor.forcefield_params.pdb_coordinates_filename = pdb_filename
            anchor.forcefield_params.box_vectors = forcefield.box_vectors
            if anchor.forcefield_params.box_vectors is None:
                anchor.forcefield_params.box_vectors = base.Box_vectors()
                box_vectors = base.get_box_vectors_from_pdb(new_pdb_filename)
                anchor.forcefield_params.box_vectors.from_quantity(
                    box_vectors)
    
    return
Ejemplo n.º 6
0
def copy_building_files(model, input_model, rootdir):
    """
    For each of the anchors and other directories, copy the necessary
    files for the simulations into those directories.
    
    Parameters:
    -----------
    model : list
        the Model to prepare files for
        
    input_anchors : list
        An equivalent list of the Input_model's Input_anchor() objects.
        Which contain the input files to copy.
        
    rootdir : str
        A path to the model's root directory.
    
    """
    input_anchors = input_model.cv_inputs[0].input_anchors
    for anchor, input_anchor in zip(model.anchors, input_anchors):
        if not anchor.md:
            continue
        anchor_building_dir = os.path.join(rootdir, anchor.directory,
                                           anchor.building_directory)
        print(anchor_building_dir)
        assert os.path.exists(anchor_building_dir)

        try:  # TODO: fix simple XML parser so this isn't necessary
            amber = input_anchor.starting_amber_params
        except AttributeError:
            amber = None

        if amber is not None:
            anchor.amber_params = base.Amber_params()
            #assert amber.prmtop_filename is not None, \
            #    "Amber PRMTOP file must be provided for anchor {}".format(
            #        anchor.index)
            #assert amber.prmtop_filename != "", \
            #    "Amber PRMTOP file must be provided for anchor {}".format(
            #        anchor.index)
            if amber.prmtop_filename is not None and \
                    amber.prmtop_filename != "":
                assert os.path.exists(amber.prmtop_filename)
                prmtop_filename = os.path.basename(amber.prmtop_filename)
                new_prmtop_filename = os.path.join(anchor_building_dir,
                                                   prmtop_filename)
                copyfile(amber.prmtop_filename, new_prmtop_filename)
                anchor.amber_params.prmtop_filename = prmtop_filename
            """# TODO: remove
            if amber.inpcrd_filename is not None and \
                    amber.inpcrd_filename != "":
                assert os.path.exists(amber.inpcrd_filename)
                inpcrd_filename = os.path.basename(amber.inpcrd_filename)
                new_inpcrd_filename = os.path.join(anchor_building_dir, 
                                                   inpcrd_filename)
                copyfile(amber.inpcrd_filename, new_inpcrd_filename)
                anchor.amber_params.inpcrd_filename = inpcrd_filename
                if anchor.amber_params.box_vectors is None:
                    assert new_prmtop_filename is not None
                    anchor.amber_params.box_vectors = base.Box_vectors()
                    inpcrd_structure = parmed.load_file(new_prmtop_filename, 
                                                xyz=new_inpcrd_filename)
                    anchor.amber_params.box_vectors.from_quantity(
                        inpcrd_structure.box_vectors)
            """

            #assert amber.pdb_coordinates_filename is not None and \
            #    amber.pdb_coordinates_filename != "", \
            #    "PDB file must be provided for anchor {}".format(
            #        anchor.index)
            #amber.pdb_coordinates_filename = os.path.expanduser(
            #    amber.pdb_coordinates_filename)
            #assert os.path.exists(amber.pdb_coordinates_filename), \
            #    "Provided file does not exist: {}".format(
            #        amber.pdb_coordinates_filename)
            if amber.pdb_coordinates_filename is not None and \
                    amber.pdb_coordinates_filename != "":
                pdb_filename = os.path.basename(amber.pdb_coordinates_filename)
                new_pdb_filename = os.path.join(anchor_building_dir,
                                                pdb_filename)
                copyfile(amber.pdb_coordinates_filename, new_pdb_filename)
                anchor.amber_params.pdb_coordinates_filename = pdb_filename
                if amber.box_vectors is not None:
                    if not isinstance(amber.box_vectors, base.Box_vectors):
                        flattened_box_vectors = np.array(
                            [val for row in amber.box_vectors for val in row])
                        box_vectors = base.Box_vectors()
                        box_vectors.ax, box_vectors.ay, box_vectors.az, \
                        box_vectors.bx, box_vectors.by, box_vectors.bz, \
                        box_vectors.cx, box_vectors.cy, box_vectors.cz = \
                        flattened_box_vectors
                        amber.box_vectors = box_vectors
                    anchor.amber_params.box_vectors = amber.box_vectors
                elif anchor.amber_params.box_vectors is None:
                    anchor.amber_params.box_vectors = base.Box_vectors()
                    pdb_structure = parmed.load_file(new_pdb_filename)
                    anchor.amber_params.box_vectors.from_quantity(
                        pdb_structure.box_vectors)

        try:  # TODO: fix simple XML parser so this isn't necessary
            forcefield = input_anchor.starting_forcefield_params
        except AttributeError:
            forcefield = None

        if forcefield is not None:
            assert amber is None, "Parameters may not be included for both "\
                "Amber and Forcefield inputs."
            anchor.forcefield_params = base.Forcefield_params()
            if forcefield.built_in_forcefield_filenames is not None and \
                    len(forcefield.built_in_forcefield_filenames) > 0:
                for filename in forcefield.built_in_forcefield_filenames:
                    anchor.forcefield_params.built_in_forcefield_filenames.\
                        append(filename)

            if forcefield.custom_forcefield_filenames is not None and \
                    len(forcefield.custom_forcefield_filenames) > 0:
                for filename in forcefield.custom_forcefield_filenames:
                    ff_filename = os.path.basename(filename)
                    new_ff_filename = os.path.join(anchor_building_dir,
                                                   ff_filename)
                    copyfile(filename, new_ff_filename)
                    anchor.forcefield_params.custom_forcefield_filenames.\
                        append(ff_filename)

            if forcefield.pdb_filename is not None and \
                    forcefield.pdb_filename != "":
                assert os.path.exists(forcefield.pdb_filename)
                pdb_filename = os.path.basename(forcefield.pdb_filename)
                new_pdb_filename = os.path.join(anchor_building_dir,
                                                pdb_filename)
                copyfile(forcefield.pdb_filename, new_pdb_filename)
                anchor.forcefield_params.pdb_filename = pdb_filename
                anchor.forcefield_params.box_vectors = forcefield.box_vectors
                if anchor.forcefield_params.box_vectors is None:
                    pdb_structure = parmed.load_file(new_pdb_filename)
                    anchor.forcefield_params.box_vectors.from_quantity(
                        pdb_structure.box_vectors)

    if model.k_on_info is not None:
        bd_settings = model.browndye_settings
        bd_input_settings = input_model.browndye_settings_input
        k_on_info = model.k_on_info
        b_surface_dir = os.path.join(rootdir, k_on_info.b_surface_directory)

        ligand_pqr_filename = os.path.basename(bd_settings.ligand_pqr_filename)
        ligand_pqr_dest_filename = os.path.join(b_surface_dir,
                                                ligand_pqr_filename)
        copyfile(bd_input_settings.ligand_pqr_filename,
                 ligand_pqr_dest_filename)

        receptor_pqr_filename = os.path.basename(
            bd_settings.receptor_pqr_filename)
        receptor_pqr_dest_filename = os.path.join(b_surface_dir,
                                                  receptor_pqr_filename)
        copyfile(bd_input_settings.receptor_pqr_filename,
                 receptor_pqr_dest_filename)
    return