Esempio n. 1
0
    def test_should_call_single_mover_correct_number_of_times(self):
        n_trials = 10
        mock_mover = mock.Mock()
        mock_mover.trial.return_value = (mock.Mock, True)
        mock_state = mock.Mock()
        mock_runner = mock.Mock()

        scheduler = mc.MonteCarloScheduler([(mock_mover, 1.0)], n_trials)
        scheduler.update(mock_state, mock_runner)

        self.assertEqual(mock_mover.trial.call_count, n_trials)
Esempio n. 2
0
    def test_should_return_correct_state(self):
        n_trials = 1000
        weight1 = 1
        weight2 = 1

        scheduler = mc.MonteCarloScheduler([(self.mock_mover1, weight1),
                                            (self.mock_mover2, weight2)],
                                           n_trials)
        result = scheduler.update(self.mock_state, self.mock_runner)

        # our mock always returns True, so accepted should == attempted
        self.assertIs(result, self.mock_structure)
Esempio n. 3
0
    def test_should_track_total_count_correctly(self):
        n_trials = 1000
        weight1 = 1
        weight2 = 1

        scheduler = mc.MonteCarloScheduler([(self.mock_mover1, weight1),
                                            (self.mock_mover2, weight2)],
                                           n_trials)
        scheduler.update(self.mock_state, self.mock_runner)

        self.assertEqual(scheduler.trial_counts[0],
                         self.mock_mover1.trial.call_count)
        self.assertEqual(scheduler.trial_counts[1],
                         self.mock_mover2.trial.call_count)
Esempio n. 4
0
    def test_should_track_accepted_count_correctly(self):
        n_trials = 1000
        weight1 = 1
        weight2 = 1

        scheduler = mc.MonteCarloScheduler([(self.mock_mover1, weight1),
                                            (self.mock_mover2, weight2)],
                                           n_trials)
        scheduler.update(self.mock_state, self.mock_runner)

        # our mock always returns True, so accepted should == attempted
        self.assertEqual(scheduler.accepted_counts[0],
                         self.mock_mover1.trial.call_count)
        self.assertEqual(scheduler.accepted_counts[1],
                         self.mock_mover2.trial.call_count)
Esempio n. 5
0
    def test_should_call_pair_of_movers_correct_number_of_times(self):
        n_trials = 100000
        weight1 = 1
        weight2 = 2

        scheduler = mc.MonteCarloScheduler([(self.mock_mover1, weight1),
                                            (self.mock_mover2, weight2)],
                                           n_trials)
        scheduler.update(self.mock_state, self.mock_runner)

        frac1 = self.mock_mover1.trial.call_count / float(n_trials)
        expected1 = weight1 / float(weight1 + weight2)
        frac2 = self.mock_mover2.trial.call_count / float(n_trials)
        expected2 = weight2 / float(weight1 + weight2)
        self.assertAlmostEqual(frac1, expected1, 2)
        self.assertAlmostEqual(frac2, expected2, 2)
Esempio n. 6
0
def setup_system():

    # ECO settings
    #eco_cutoff = 0.8 # the distance (in nm) that qualifies as a connection in the graph
    eco_cutoff = 1.0  # the distance (in nm) that qualifies as a connection in the graph
    doing_eco_hydrophobe = True
    doing_eco_hbond = True
    doing_eco_2ndary = False
    doing_eco_strand_pairing = True
    doing_eco_knob = False
    doing_eco_evolutionary = False
    #eco_factor = 4.0 # the factor by which we multiply the eco energy adjustment
    eco_factor = 1  # the factor by which we multiply the eco energy adjustment
    eco_constant = 0.0  # In theory, these could be changed for any restraint
    eco_linear = 0.0

    # load the sequence
    sequence = parse.get_sequence_from_AA1(filename='sequence.dat')
    n_res = len(sequence.split())

    # build the system
    p = system.ProteinMoleculeFromSequence(sequence)
    b = system.SystemBuilder(forcefield="ff14sbside")
    s = b.build_system_from_molecules([p])
    s.temperature_scaler = system.GeometricTemperatureScaler(
        0, 0.6, 300., 450.)

    #
    # Secondary Structure
    #
    ss_scaler = s.restraints.create_scaler('constant')
    ss_rests = parse.get_secondary_structure_restraints(
        filename='ss.dat',
        system=s,
        ramp=LinearRamp(0, 100, 0, 1),
        scaler=ss_scaler,
        torsion_force_constant=2.5,
        distance_force_constant=2.5,
        doing_eco=doing_eco_2ndary,
        eco_factor=eco_factor,
        eco_constant=eco_constant,
        eco_linear=eco_linear)
    n_ss_keep = int(len(ss_rests) * 0.70)  #We enforce 70% of restrains
    s.restraints.add_selectively_active_collection(ss_rests, n_ss_keep)

    #
    # Confinement Restraints
    #
    #conf_scaler = s.restraints.create_scaler('nonlinear', alpha_min=0.4, alpha_max=1.0, factor=4.0,strength_at_alpha_min=0.0, strength_at_alpha_max=1.0)
    #confinement_rests = []
    #confinement_dist = (16.9*np.log(s.residue_numbers[-1])-15.8)/28.*1.2
    #for index in range(n_res):
    #    rest = s.restraints.create_restraint('confine', conf_scaler, LinearRamp(0,100,0,1),res_index=index+1, atom_name='CA', radius=confinement_dist, force_const=250.0)
    #    confinement_rests.append(rest)
    #s.restraints.add_as_always_active_list(confinement_rests)

    #
    # Distance Restraints
    #
    dist_scaler = s.restraints.create_scaler('nonlinear',
                                             alpha_min=0.4,
                                             alpha_max=1.0,
                                             factor=4.0)

    # High reliability
    #
    #
    # Create Plateau kind of scalers
    #
    low_2 = make_CO_scaler(system=s,
                           scaler_type='plateaunonlinear',
                           alpha_min=0.70,
                           alpha_one=0.85,
                           alpha_two=0.85,
                           alpha_max=1.0,
                           strength_at_alpha_min=1.0,
                           strength_at_alpha_max=0.0)
    low_4 = make_CO_scaler(system=s,
                           scaler_type='plateaunonlinear',
                           alpha_min=0.55,
                           alpha_one=0.70,
                           alpha_two=0.70,
                           alpha_max=0.85,
                           strength_at_alpha_min=1.0,
                           strength_at_alpha_max=0.0)
    low_6 = s.restraints.create_scaler('plateaunonlinear',
                                       alpha_min=0.40,
                                       alpha_one=0.55,
                                       alpha_two=0.55,
                                       alpha_max=0.7,
                                       factor=4.0,
                                       strength_at_alpha_min=1.0,
                                       strength_at_alpha_max=0.0)
    low_8 = s.restraints.create_scaler('nonlinear',
                                       alpha_min=0.40,
                                       alpha_max=0.55,
                                       factor=4.0)
    #
    # Heuristic Restraints
    #
    subset = np.array(range(n_res)) + 1

    #
    # Hydrophobic
    #

    create_hydrophobes(s,
                       ContactsPerHydroph=1.2,
                       scaler=dist_scaler,
                       group_1=subset,
                       CO=False,
                       doing_eco=doing_eco_hydrophobe,
                       eco_factor=eco_factor,
                       eco_constant=eco_constant,
                       eco_linear=eco_linear)

    #create_hydrophobes(s,ContactsPerHydroph=1.2/4.,scaler=low_2,group_1=subset,CO=True)
    #create_hydrophobes(s,ContactsPerHydroph=1.2/2.,scaler=low_4,group_1=subset,CO=True)
    #create_hydrophobes(s,ContactsPerHydroph=1.2*3/4.,scaler=low_6,group_1=subset,CO=False)
    #create_hydrophobes(s,ContactsPerHydroph=1.2,scaler=low_8,group_1=subset,CO=False)

    #
    # HBonds
    #

    create_HydrogenBond(s,
                        HBPerResidue=0.10,
                        scaler=dist_scaler,
                        group_1=subset,
                        CO=False,
                        doing_eco=doing_eco_hbond,
                        eco_factor=eco_factor,
                        eco_constant=eco_constant,
                        eco_linear=eco_linear)

    #create_HydrogenBond(s,HBPerResidue=0.10/4.,scaler=low_2,group_1=subset,CO=True)
    #create_HydrogenBond(s,HBPerResidue=0.10/2.,scaler=low_4,group_1=subset,CO=True)
    #create_HydrogenBond(s,HBPerResidue=0.10*3/4.,scaler=low_6,group_1=subset,CO=False)
    #create_HydrogenBond(s,HBPerResidue=0.10,scaler=low_8,group_1=subset,CO=False)

    #
    # Strand Pairing
    #
    sse, active = make_ss_groups(subset=subset)
    try:

        generate_strand_pairs(s,
                              sse,
                              float(active),
                              subset=subset,
                              scaler=dist_scaler,
                              CO=False,
                              doing_eco=doing_eco_strand_pairing,
                              eco_factor=eco_factor,
                              eco_constant=eco_constant,
                              eco_linear=eco_linear)

        #generate_strand_pairs(s,sse,float(active)/4.,subset=subset,scaler=low_2,CO=True)
        #generate_strand_pairs(s,sse,float(active)/2.,subset=subset,scaler=low_4,CO=True)
        #generate_strand_pairs(s,sse,float(active)*3/4.,subset=subset,scaler=low_6,CO=False)
        #generate_strand_pairs(s,sse,float(active),subset=subset,scaler=low_8,CO=False)
    except:
        print "Not using Strand Pairing Heuristic"
        pass

        #
        # Evolutionary restraints
        #
    try:
        create_Evolution(s,
                         scaler=dist_scaler,
                         fname='evolution_contacts.dat',
                         doing_eco=doing_eco_evolutionary,
                         eco_factor=eco_factor,
                         eco_constant=eco_constant,
                         eco_linear=eco_linear)
    except:
        print "Not using Evolutionary restraints"
        pass

    #
    # Distance Restraints
    #
    #
    # Knob restraints
    #
    try:
        knobs, knob_accuracy = get_knob_restraints('Knob.data',
                                                   s,
                                                   scaler=dist_scaler,
                                                   doing_eco=doing_eco_knob,
                                                   eco_factor=eco_factor,
                                                   eco_constant=eco_constant,
                                                   eco_linear=eco_linear)
        n_knobs = int(len(knobs) * knob_accuracy)
        s.restraints.add_selectively_active_collection(knobs, n_knobs)
    except:
        print "Not using Knob-Socket predictions"
        pass

    # setup mcmc at startup
    movers = []
    n_atoms = s.n_atoms
    for i in range(1, n_res + 1):
        n = s.index_of_atom(i, 'N') - 1
        ca = s.index_of_atom(i, 'CA') - 1
        c = s.index_of_atom(i, 'C') - 1

        mover = mc.DoubleTorsionMover(n, ca, list(range(ca, n_atoms)), ca, c,
                                      list(range(c, n_atoms)))

        movers.append((mover, 1))

    sched = mc.MonteCarloScheduler(movers, n_res * 60)

    # create the options
    options = system.RunOptions()
    options.implicit_solvent_model = 'gbNeck2'
    options.use_big_timestep = False
    options.use_bigger_timestep = True
    options.cutoff = 1.8
    #options.eco_cutoff = eco_cutoff
    # set eco_output very high so that log file does not print
    options.eco_params = {
        'eco_cutoff': 1.0,
        'eco_output_freq': 10000000,
        'print_avg_eco': False,
        'print_eco_value_array': False,
    }

    options.use_amap = False
    options.amap_beta_bias = 1.0
    options.timesteps = 11111
    options.minimize_steps = 20000
    options.min_mc = sched
    options.make_alpha_carbon_list(s.atom_names)
    print "alpha_carbon_indeces:", options.alpha_carbon_indeces

    # create a store
    store = vault.DataStore(s.n_atoms,
                            N_REPLICAS,
                            s.get_pdb_writer(),
                            block_size=BLOCK_SIZE)
    store.initialize(mode='w')
    store.save_system(s)
    store.save_run_options(options)

    # create and store the remd_runner
    l = ladder.NearestNeighborLadder(n_trials=48 * 48)
    policy = adaptor.AdaptationPolicy(2.0, 50, 50)
    a = adaptor.EqualAcceptanceAdaptor(n_replicas=N_REPLICAS,
                                       adaptation_policy=policy)

    remd_runner = master_runner.MasterReplicaExchangeRunner(N_REPLICAS,
                                                            max_steps=N_STEPS,
                                                            ladder=l,
                                                            adaptor=a)
    store.save_remd_runner(remd_runner)

    # create and store the communicator
    c = comm.MPICommunicator(s.n_atoms, N_REPLICAS)
    store.save_communicator(c)

    # create and save the initial states
    states = [gen_state(s, i) for i in range(N_REPLICAS)]
    store.save_states(states, 0)

    # save data_store
    store.save_data_store()

    return s.n_atoms
Esempio n. 7
0
def setup_system():
    # load the sequence
    sequence = parse.get_sequence_from_AA1(filename='sequence.dat')
    n_res = len(sequence.split())

    # build the system
    p = system.ProteinMoleculeFromSequence(sequence)
    b = system.SystemBuilder(forcefield="ff14sbside")
    s = b.build_system_from_molecules([p])
    s.temperature_scaler = system.GeometricTemperatureScaler(0, 0.6, 300., 450.)

    #
    # Secondary Structure
    #
    ss_scaler = s.restraints.create_scaler('constant')
    ss_rests = parse.get_secondary_structure_restraints(filename='ss.dat', system=s,ramp=LinearRamp(0,100,0,1), scaler=ss_scaler,
            torsion_force_constant=2.5, distance_force_constant=2.5)
    n_ss_keep = int(len(ss_rests) * 1.0) #We enforce 100% of restrains 
    s.restraints.add_selectively_active_collection(ss_rests, n_ss_keep)


    #
    # Distance Restraints
    #
    dist_scaler = s.restraints.create_scaler('nonlinear', alpha_min=0.4, alpha_max=1.0, factor=4.0)

    # High reliability
    #
    #
    old_protocol = s.restraints.create_scaler('nonlinear', alpha_min=0.40, alpha_max=1.00, factor=4.0)
    #
    # Heuristic Restraints
    #
    subset= np.array(list(range(n_res))) + 1

        #
        # Hydrophobic
        #
    create_hydrophobes(s,ContactsPerHydroph=1.2,scaler=old_protocol,group_1=subset,CO=False)
        #
        # Strand Pairing
        #
    sse,active = make_ss_groups(subset=subset)
    try:
        generate_strand_pairs(s,sse,float(active),subset=subset,scaler=old_protocol,CO=False)
    except:
        print("Not using Strand Pairing Heuristic")
        pass

        #
        # Evolutionary restraints
        #
    try:
        create_Evolution(s,accuracy=0.7,scaler=dist_scaler,fname='evolution_contacts.dat')
    except:
        print("Not using Evolutionary restraints")
        pass

    #
    # Distance Restraints
    #
        #
        # Knob restraints
        #
    try:
        knobs,knob_accuracy = get_knob_restraints('Knob.data',s,scaler=dist_scaler)
        n_knobs = int(len(knobs) * knob_accuracy) 
        s.restraints.add_selectively_active_collection(knobs,n_knobs)
    except:
        print("Not using Knob-Socket predictions")
        pass



    # setup mcmc at startup
    movers = []
    n_atoms = s.n_atoms
    for i in range(1, n_res + 1):
        n = s.index_of_atom(i, 'N') - 1
        ca = s.index_of_atom(i, 'CA') - 1
        c = s.index_of_atom(i, 'C') - 1

        mover = mc.DoubleTorsionMover(n, ca, list(range(ca, n_atoms)),
                                      ca, c, list(range(c, n_atoms)))

        movers.append((mover, 1))

    sched = mc.MonteCarloScheduler(movers, n_res * 60)

    # create the options
    options = system.RunOptions()
    options.implicit_solvent_model = 'gbNeck2'
    options.use_big_timestep = False
    options.use_bigger_timestep = True
    options.cutoff = 1.8

    options.use_amap = False
    options.amap_alpha_bias = 1.0
    options.amap_beta_bias = 1.0
    options.timesteps = 11111
    options.minimize_steps = 20000
   # for i in range(30):
   #     print("Heads up! using MC minimizer!")
#    options.min_mc = sched

    # create a store
    store = vault.DataStore(s.n_atoms, N_REPLICAS, s.get_pdb_writer(), block_size=BLOCK_SIZE)
    store.initialize(mode='w')
    store.save_system(s)
    store.save_run_options(options)

    # create and store the remd_runner
    l = ladder.NearestNeighborLadder(n_trials=100)
    policy = adaptor.AdaptationPolicy(2.0, 50, 50)
    a = adaptor.EqualAcceptanceAdaptor(n_replicas=N_REPLICAS, adaptation_policy=policy)

    remd_runner = master_runner.MasterReplicaExchangeRunner(N_REPLICAS, max_steps=N_STEPS, ladder=l, adaptor=a)
    store.save_remd_runner(remd_runner)

    # create and store the communicator
    c = comm.MPICommunicator(s.n_atoms, N_REPLICAS)
    store.save_communicator(c)

    # create and save the initial states
    states = [gen_state(s, i) for i in range(N_REPLICAS)]
    store.save_states(states, 0)

    # save data_store
    store.save_data_store()

    return s.n_atoms