示例#1
0
def setup_system_and_integrator_files(target, template, temperature,
                                      collision_rate, timestep):
    logger.debug(
        'Copying system and integrator files for template {}'.format(template))
    models_target_dir = os.path.join(default_project_dirnames.models,
                                     target.id)
    template_dir = os.path.join(models_target_dir, template)
    target_project_dir = os.path.join(fah_projects_dir, target.id)
    source_system_filepath = os.path.join(template_dir, 'explicit-system.xml')
    source_state_filepath = os.path.join(template_dir, 'explicit-state.xml')
    dest_system_filepath = os.path.join(target_project_dir, 'system.xml')
    dest_integrator_filepath = os.path.join(target_project_dir,
                                            'integrator.xml')

    system = mm.XmlSerializer.deserialize(
        read_file_contents_gz_or_not(source_system_filepath))
    state = mm.XmlSerializer.deserialize(
        read_file_contents_gz_or_not(source_state_filepath))

    # Substitute default box vectors in system with those from state.
    box_vectors = state.getPeriodicBoxVectors()
    system.setDefaultPeriodicBoxVectors(*box_vectors)

    # Set PME parameters explicitly to minimize discrepancy between Reference and OpenCL/CUDA if not already set explicitly.
    ensure_pme_parameters_are_explicit(system)

    # Create new integrator to use.
    integrator = mm.LangevinIntegrator(temperature, collision_rate, timestep)

    # Make sure MonteCarloBarostat temperature matches set temperature.
    forces = {
        system.getForce(index).__class__.__name__: system.getForce(index)
        for index in range(system.getNumForces())
    }
    if 'MonteCarloBarostat' in forces:
        forces['MonteCarloBarostat'].setTemperature(temperature)

    # Serialize System.
    with open(dest_system_filepath, 'w') as dest_system_file:
        dest_system_file.write(mm.XmlSerializer.serialize(system))

    # Serialize Integrator
    with open(dest_integrator_filepath, 'w') as dest_integrator_file:
        dest_integrator_file.write(mm.XmlSerializer.serialize(integrator))

    return system
示例#2
0
def setup_system_and_integrator_files(target,
                                      template,
                                      temperature,
                                      collision_rate,
                                      timestep
                                      ):
    logger.debug('Copying system and integrator files for template {}'.format(template))
    models_target_dir = os.path.join(default_project_dirnames.models, target.id)
    template_dir = os.path.join(models_target_dir, template)
    target_project_dir = os.path.join(fah_projects_dir, target.id)
    source_system_filepath = os.path.join(template_dir, 'explicit-system.xml')
    source_state_filepath = os.path.join(template_dir, 'explicit-state.xml')
    dest_system_filepath = os.path.join(target_project_dir, 'system.xml')
    dest_integrator_filepath = os.path.join(target_project_dir, 'integrator.xml')

    system = mm.XmlSerializer.deserialize(
        read_file_contents_gz_or_not(source_system_filepath)
    )
    state = mm.XmlSerializer.deserialize(
        read_file_contents_gz_or_not(source_state_filepath)
    )

    # Substitute default box vectors in system with those from state.
    box_vectors = state.getPeriodicBoxVectors()
    system.setDefaultPeriodicBoxVectors(*box_vectors)

    # Set PME parameters explicitly to minimize discrepancy between Reference and OpenCL/CUDA if not already set explicitly.
    ensure_pme_parameters_are_explicit(system)

    # Create new integrator to use.
    integrator = mm.LangevinIntegrator(temperature, collision_rate, timestep)

    # Make sure MonteCarloBarostat temperature matches set temperature.
    forces = { system.getForce(index).__class__.__name__ : system.getForce(index) for index in range(system.getNumForces()) }
    if 'MonteCarloBarostat' in forces:
        forces['MonteCarloBarostat'].setTemperature(temperature)

    # Serialize System.
    with open(dest_system_filepath, 'w') as dest_system_file:
        dest_system_file.write(mm.XmlSerializer.serialize(system))

    # Serialize Integrator
    with open(dest_integrator_filepath, 'w') as dest_integrator_file:
        dest_integrator_file.write(mm.XmlSerializer.serialize(integrator))

    return system
示例#3
0
def generate_fah_run(
    target_project_dir,
    template,
    source_dir,
    system,
    run_index,
    nclones,
    temperature,
    collision_rate,
    timestep,
    openmm_platform,
    renumbered_resnums,
):
    """
    Build Folding@Home RUN and CLONE subdirectories from (possibly compressed) OpenMM serialized XML files.

    ARGUMENTS

    run (int) - run index
    """
    logger.debug("Building RUN %d" % run_index)

    try:
        # Determine directory and pathnames.
        run_dir = os.path.join(target_project_dir, 'RUN%d' % run_index)
        run_template_id_filepath = os.path.join(run_dir, 'template.txt')
        run_seqid_filepath = os.path.join(run_dir, 'sequence-identity.txt')
        run_protein_structure_filepath = os.path.join(run_dir, 'protein.pdb')
        run_system_structure_filepath = os.path.join(run_dir, 'system.pdb')
        run_final_state_filepath = os.path.join(run_dir,
                                                'state%d.xml' % (nclones - 1))
        source_seqid_filepath = os.path.join(source_dir,
                                             'sequence-identity.txt')
        source_protein_structure_filepath = os.path.join(
            source_dir, 'implicit-refined.pdb.gz')
        source_system_structure_filepath = os.path.join(
            source_dir, 'explicit-refined.pdb.gz')
        source_openmm_state_filepath = os.path.join(source_dir,
                                                    'explicit-state.xml')

        # Return if this directory has already been set up.
        if os.path.exists(run_dir):
            if (os.path.exists(run_template_id_filepath)
                    and os.path.exists(run_seqid_filepath)
                    and os.path.exists(run_protein_structure_filepath)
                    and os.path.exists(run_system_structure_filepath)
                    and os.path.exists(run_final_state_filepath)):
                return
        else:
            # Construct run directory if it does not exist.
            if not os.path.exists(run_dir):
                os.makedirs(run_dir)

        # Write template ID
        with open(run_template_id_filepath, 'w') as outfile:
            outfile.write(template + '\n')

        # Write the protein and system structure pdbs
        if 'implicit' in renumbered_resnums:
            write_renumbered_structure(
                source_protein_structure_filepath,
                run_protein_structure_filepath,
                renumbered_resnums['implicit'],
            )
        else:
            with open(run_protein_structure_filepath,
                      'w') as protein_structure_file:
                protein_structure_file.write(
                    read_file_contents_gz_or_not(
                        source_protein_structure_filepath))

        if 'explicit' in renumbered_resnums:
            write_renumbered_structure(
                source_system_structure_filepath,
                run_system_structure_filepath,
                renumbered_resnums['explicit'],
            )
        else:
            with open(run_system_structure_filepath,
                      'w') as system_structure_file:
                system_structure_file.write(
                    read_file_contents_gz_or_not(
                        source_system_structure_filepath))

        state = mm.XmlSerializer.deserialize(
            read_file_contents_gz_or_not(source_openmm_state_filepath))

        # Write sequence identity.
        with open(run_seqid_filepath, 'w') as run_seqid_file:
            run_seqid_file.write(
                read_file_contents_gz_or_not(source_seqid_filepath))

        # Create new integrator to use.
        integrator = mm.LangevinIntegrator(temperature, collision_rate,
                                           timestep)

        # Create Context so we can randomize velocities.
        platform = mm.Platform.getPlatformByName(openmm_platform)
        context = mm.Context(system, integrator, platform)
        context.setPositions(state.getPositions())
        box_vectors = state.getPeriodicBoxVectors()
        context.setPeriodicBoxVectors(*box_vectors)

        # Create clones with different random initial velocities.
        for clone_index in range(nclones):
            state_filename = os.path.join(run_dir, 'state%d.xml' % clone_index)
            if os.path.exists(state_filename):
                continue
            context.setVelocitiesToTemperature(temperature)
            state = context.getState(getPositions=True,
                                     getVelocities=True,
                                     getForces=True,
                                     getEnergy=True,
                                     getParameters=True,
                                     enforcePeriodicBox=True)
            with open(state_filename, 'w') as state_file:
                state_file.write(mm.XmlSerializer.serialize(state))

    except Exception as e:
        import traceback
        print(traceback.format_exc())
        print(str(e))
示例#4
0
def generate_fah_run(target_project_dir,
                     template,
                     source_dir,
                     system,
                     run_index,
                     nclones,
                     temperature,
                     collision_rate,
                     timestep,
                     openmm_platform,
                     renumbered_resnums,
                     ):
    """
    Build Folding@Home RUN and CLONE subdirectories from (possibly compressed) OpenMM serialized XML files.

    ARGUMENTS

    run (int) - run index
    """
    logger.debug("Building RUN %d" % run_index)

    try:
        # Determine directory and pathnames.
        run_dir = os.path.join(target_project_dir, 'RUN%d' % run_index)
        run_template_id_filepath = os.path.join(run_dir, 'template.txt')
        run_seqid_filepath = os.path.join(run_dir, 'sequence-identity.txt')
        run_protein_structure_filepath = os.path.join(run_dir, 'protein.pdb')
        run_system_structure_filepath = os.path.join(run_dir, 'system.pdb')
        run_final_state_filepath = os.path.join(run_dir, 'state%d.xml' % (nclones - 1))
        source_seqid_filepath = os.path.join(source_dir, 'sequence-identity.txt')
        source_protein_structure_filepath = os.path.join(source_dir, 'implicit-refined.pdb.gz')
        source_system_structure_filepath = os.path.join(source_dir, 'explicit-refined.pdb.gz')
        source_openmm_state_filepath = os.path.join(source_dir, 'explicit-state.xml')

        # Return if this directory has already been set up.
        if os.path.exists(run_dir):
            if (
                    os.path.exists(run_template_id_filepath)
                    and os.path.exists(run_seqid_filepath)
                    and os.path.exists(run_protein_structure_filepath)
                    and os.path.exists(run_system_structure_filepath)
                    and os.path.exists(run_final_state_filepath)
                    ):
                return
        else:
            # Construct run directory if it does not exist.
            if not os.path.exists(run_dir):
                os.makedirs(run_dir)

        # Write template ID
        with open(run_template_id_filepath, 'w') as outfile:
            outfile.write(template + '\n')

        # Write the protein and system structure pdbs
        if 'implicit' in renumbered_resnums:
            write_renumbered_structure(
                source_protein_structure_filepath,
                run_protein_structure_filepath,
                renumbered_resnums['implicit'],
            )
        else:
            with open(run_protein_structure_filepath, 'w') as protein_structure_file:
                protein_structure_file.write(
                    read_file_contents_gz_or_not(source_protein_structure_filepath)
                )

        if 'explicit' in renumbered_resnums:
            write_renumbered_structure(
                source_system_structure_filepath,
                run_system_structure_filepath,
                renumbered_resnums['explicit'],
            )
        else:
            with open(run_system_structure_filepath, 'w') as system_structure_file:
                system_structure_file.write(
                    read_file_contents_gz_or_not(source_system_structure_filepath)
                )

        state = mm.XmlSerializer.deserialize(
            read_file_contents_gz_or_not(source_openmm_state_filepath)
        )

        # Write sequence identity.
        with open(run_seqid_filepath, 'w') as run_seqid_file:
            run_seqid_file.write(read_file_contents_gz_or_not(source_seqid_filepath))

        # Create new integrator to use.
        integrator = mm.LangevinIntegrator(temperature, collision_rate, timestep)

        # Create Context so we can randomize velocities.
        platform = mm.Platform.getPlatformByName(openmm_platform)
        context = mm.Context(system, integrator, platform)
        context.setPositions(state.getPositions())
        box_vectors = state.getPeriodicBoxVectors()
        context.setPeriodicBoxVectors(*box_vectors)

        # Create clones with different random initial velocities.
        for clone_index in range(nclones):
            state_filename = os.path.join(run_dir, 'state%d.xml' % clone_index)
            if os.path.exists(state_filename):
                continue
            context.setVelocitiesToTemperature(temperature)
            state = context.getState(
                getPositions=True,
                getVelocities=True,
                getForces=True,
                getEnergy=True,
                getParameters=True,
                enforcePeriodicBox=True
            )
            with open(state_filename, 'w') as state_file:
                state_file.write(mm.XmlSerializer.serialize(state))

    except Exception as e:
        import traceback
        print(traceback.format_exc())
        print(str(e))