Пример #1
0
def get_ccc(native_assembly,
            assembly,
            resolution,
            voxel_size,
            threshold,
            write_maps=False):
    """
        Threshold - threshold used for the map of the native assembly. Pixels
        with values above this threshold in the native map are used for the
        calculation of the cross_correlation_coefficient
    """
    import IMP.em as em
    particles_native = atom.get_leaves(native_assembly)
    particles_solution = atom.get_leaves(assembly)
    bb_native = core.get_bounding_box(core.XYZs(particles_native))
    bb_solution = core.get_bounding_box(core.XYZs(particles_solution))
    # bounding box enclosing both the particles of the native assembly
    #  and the particles of the model
    bb_union = alg.get_union(bb_native, bb_solution)
    # add border of 4 voxels
    border = 4 * voxel_size
    bottom = bb_union.get_corner(0)
    bottom += alg.Vector3D(-border, -border, -border)
    top = bb_union.get_corner(1)
    top += alg.Vector3D(border, border, border)
    bb_union = alg.BoundingBox3D(bottom, top)

    mrw = em.MRCReaderWriter()
    header = em.create_density_header(bb_union, voxel_size)
    header.set_resolution(resolution)

    map_native = em.SampledDensityMap(header)
    map_native.set_particles(particles_native)
    map_native.resample()

    map_solution = em.SampledDensityMap(header)
    map_solution.set_particles(particles_solution)
    map_solution.resample()

    if (write_maps):
        em.write_map(map_solution, "map_solution.mrc", mrw)
        em.write_map(map_native, "map_native.mrc", mrw)
    map_native.calcRMS()
    map_solution.calcRMS()
    coarse_cc = em.CoarseCC()
    # base the calculation of the cross_correlation coefficient on the threshold]
    # for the native map, because the threshold for the map of the model changes
    # with each model
    threshold = 0.25  # threshold AFTER normalization using calcRMS()
    ccc = coarse_cc.cross_correlation_coefficient(map_solution, map_native,
                                                  threshold)
    log.debug(
        "cross_correlation_coefficient (based on native_map "
        "treshold %s) %s", threshold, ccc)
    return ccc
Пример #2
0
def apply_random_transform(rb, max_trans=100):
    """
        Apply a random transformation to the rigid body and change the reference
        frame
    """
    bb = alg.BoundingBox3D(alg.Vector3D(-max_trans, -max_trans, -max_trans),
                           alg.Vector3D(max_trans, max_trans, max_trans))
    Trand = alg.Transformation3D(alg.get_random_rotation_3d(),
                                 alg.get_random_vector_in(bb))
    ref = rb.get_reference_frame()
    Tr = ref.get_transformation_to()
    T = alg.compose(Trand, Tr)
    rb.set_reference_frame(alg.ReferenceFrame3D(T))
Пример #3
0
Файл: io.py Проект: sirusb/imp
 def __init__(self, text, delimiter="|"):
     vals = [float(x) for x in text.split(delimiter)]
     if (len(vals) != 7):
         raise ValueError("The text is not a transformation", vals)
     R = alg.Rotation3D(vals[0], vals[1], vals[2], vals[3])
     t = alg.Vector3D(vals[4], vals[5], vals[6])
     self.t = alg.Transformation3D(R, t)
Пример #4
0
Файл: io.py Проект: sirusb/imp
def get_vectors_from_points(points, vector_type="2d"):
    if (vector_type == "2d"):
        return [alg.Vector2D(p[0], p[1]) for p in points]
    elif (vector_type == "3d"):
        return [alg.Vector3D(p[0], p[1], p[2]) for p in points]
    else:
        raise ValueError("vector type not recognized")
Пример #5
0
    def test_distance_filter(self):
        """Test filtering distances with domino """
        IMP.base.set_log_level(IMP.base.VERBOSE)
        m = IMP.kernel.Model()
        particles = [IMP.kernel.Particle(m) for i in range(0, 3)]
        for i, p in enumerate(particles):
            p.set_name("pparticle_%d" % i)

        xyzs = [core.XYZ.setup_particle(p) for p in particles]
        positions = [alg.Vector3D(x * 1.0, 0., 0.) for x in range(0, 100, 10)]

        states = domino.XYZStates(positions)
        states_table = domino.ParticleStatesTable()
        for p in particles:
            states_table.set_particle_states(p, states)
            print p
        # Restraints
        sf = IMP.core.Harmonic(10.0, 1.0)
        r1 = IMP.core.DistanceRestraint(sf, particles[0], particles[1])
        m.add_restraint(r1)
        r3 = IMP.core.DistanceRestraint(sf, particles[1], particles[2])
        m.add_restraint(r3)
        ig = domino.get_interaction_graph(m.get_restraints(), states_table)
        # generate a junction tree from the interaction graph
        jt = domino.get_junction_tree(ig)
        # print jt.show_graphviz()

        # Filters
        maximum_distance = 21
        ftable2 = em2d.DistanceFilterTable(
            IMP.domino.Subset([particles[0], particles[1]]), states_table,
            maximum_distance)
        ftable2.set_name("my filtertable2")
        ftable3 = em2d.DistanceFilterTable(
            IMP.domino.Subset([particles[1], particles[2]]), states_table,
            maximum_distance)
        ftable3.set_name("my filtertable3")
        ftable1 = domino.ExclusionSubsetFilterTable(states_table)
        ftable1.set_name("my filtertable1")

        filter_tables = [ftable2, ftable3, ftable1]

        assignments_table = \
            domino.BranchAndBoundAssignmentsTable(states_table, filter_tables)

        sampler = domino.DominoSampler(m, states_table)
        sampler.set_assignments_table(assignments_table)
        sampler.set_subset_filter_tables(filter_tables)

        configuration_set = sampler.create_sample()
        n = configuration_set.get_number_of_configurations()
        print "number of possible_configurations", n
        for i in range(n):
            configuration_set.load_configuration(i)
            # Check that the distance between the particles is correct
            self.assertLess(core.get_distance(xyzs[0], xyzs[1]),
                            maximum_distance)
            self.assertLess(core.get_distance(xyzs[1], xyzs[2]),
                            maximum_distance)
Пример #6
0
def parse_relative_transform(row):
    """
        Returns an relative transform with the conventions used by IMP.
        row - A list containing a splitted line from the relative output file
    """
    euler = [-float(x) for x in row[8:11]]
    xyz = [float(x) for x in row[5:8]]
    R = alg.get_rotation_from_fixed_zyz(*euler)
    R = R.get_inverse()
    t = alg.Vector3D(*xyz)
    return alg.Transformation3D(R, t)
Пример #7
0
    def test_sampling_schema(self):
        """
            Test
        """
        subunits = ["subunitA", "subunitB", "subunitC", "subunitD"]
        anchored = [False, False, False, False]
        fixed = [False, False, False, False]

        n_transformations = 50
        db = database.Database2()
        fn = 'temp.db'
        db.create(fn, overwrite=True)
        db.connect(fn)

        transformations = []
        table_name = "results"
        db.create_table(table_name, ["reference_frames"], [str])
        for i in range(n_transformations):
            Ts = []
            for j in range(len(subunits)):
                center = alg.Vector3D(0, 0, 0)
                T = alg.Transformation3D(
                    alg.get_random_rotation_3d(),
                    alg.get_random_vector_in(alg.Sphere3D(center, 34)))
                Ts.append(T)
            transformations.append(Ts)

        data = []
        for Ts in transformations:
            text = [io.Transformation3DToText(T).get_text() for T in Ts]
            text = "/".join(text)
            data.append([
                text,
            ])
        db.store_data(table_name, data)
        db.close()

        sch = sampling.SamplingSchema(4, fixed, anchored)
        sch.read_from_database(fn)

        for i in range(len(transformations)):
            for j in range(len(subunits)):
                T = transformations[i][j]
                t = T.get_translation()
                q = T.get_rotation().get_quaternion()

                pos = sch.transformations[j][i].get_translation()
                ori = sch.transformations[j][i].get_rotation().get_quaternion()
                for k in range(3):
                    self.assertAlmostEqual(pos[k], t[k])
                for k in range(4):
                    self.assertAlmostEqual(q[k], ori[k])

        os.remove(fn)
Пример #8
0
Файл: io.py Проект: sirusb/imp
def get_particles_from_points(points, model):
    """ Simply creates IMP particles from a set of 2D points
        model - is the model to store the particles
    """
    particles = []
    for x in points:
        p = IMP.kernel.Particle(model)
        d = IMP.core.XYZR.setup_particle(p)
        d.set_coordinates(alg.Vector3D(x[0], x[1], x[2]))
        d.set_radius(2)
        particles.append(p)
    return particles
Пример #9
0
Файл: io.py Проект: sirusb/imp
def write_points_to_chimera(points, radius, fn, name="points"):
    """ Writes a bunch o particles to the file fn
        It is assumed that the particles can be decorated with XYZR
    """
    m = IMP.kernel.Model()
    ps = []
    for p in points:
        pa = IMP.kernel.Particle(m)
        xyzr = core.XYZR.setup_particle(pa)
        xyzr.set_radius(radius)
        xyzr.set_coordinates(alg.Vector3D(p[0], p[1], p[2]))
        ps.append(pa)
    write_particles_to_chimera(ps, fn, name)
Пример #10
0
    def __init__(self, model, rigid_bodies, anchored):
        log.info("Setting MonteCarloRelativeMoves")
        self.model = model
        self.rbs = rigid_bodies
        self.components = []
        self.best_models = []
        self.anchored = anchored
        self.parent_rbs = []
        # triplets with the information to build a relative mover using the
        # results from docking with HEX
        self.dock_transforms = None
        self.non_relative_move_prob = 0.1

        log.debug("Anchored components %s", self.anchored)
        T = alg.Transformation3D(alg.get_identity_rotation_3d(),
                                 alg.Vector3D(0., 0., 0.))
        origin = alg.ReferenceFrame3D(T)
        for rb in self.rbs:
            rb.set_reference_frame(origin)
Пример #11
0
    def read_from_database(self,
                           fn_database,
                           fields=["reference_frames"],
                           max_number=False,
                           orderby=False):
        """
            Read orientations and positions from a database file.
            self.anchored and self.fixed overwrite
            the positions and orientations read from the database
        """
        if not os.path.exists(fn_database):
            raise IOError(
                "read_from_database: Database file not found. "
                "Are you perhaps trying to run the DOMINO optimization without "
                "having the database yet?")

        db = solutions_io.ResultsDB()
        db.connect(fn_database)
        data = db.get_solutions(fields, max_number, orderby)
        db.close()
        self.transformations = [[] for T in range(self.n_components)]
        # Each record contains a reference frame for each of the
        # components. But here the states considered make sense as columns.
        # Each rigidbody is considered to have all the states in a column.
        for d in data:
            texts = d[0].split("/")
            for i, t in zip(range(self.n_components), texts):
                T = io.TextToTransformation3D(t).get_transformation()
                self.transformations[i].append(T)

        # Set the anchored components
        for i in range(self.n_components):
            if self.anchored[i]:
                origin = alg.Transformation3D(alg.get_identity_rotation_3d(),
                                              alg.Vector3D(0.0, 0.0, 0.))
                self.transformations[i] = [origin]

        # If fixed, only the first state is kept
        for i in range(self.n_components):
            if self.fixed[i]:
                if len(self.transformations[i]) == 0:
                    raise ValueError("There are positions to keep fixed")
                self.transformations[i] = [self.transformations[i][0]]
Пример #12
0
def get_random_transformation(max_distance, max_angle, seed=-1):
    """
        Return a random transformation
        @param distance Maximum translation allowed
        @param max_angle Maximum rotation angle allowed
    """

    if seed == -1:
        random.seed()
    else:
        random.seed(seed)

    phi = random.uniform(-max_angle, max_angle)
    theta = random.uniform(-max_angle, max_angle)
    psi = random.uniform(-max_angle, max_angle)
    trans_x = random.uniform(-max_distance, max_distance)
    trans_y = random.uniform(-max_distance, max_distance)
    trans_z = random.uniform(-max_distance, max_distance)
    trns = alg.Vector3D(trans_x, trans_y, trans_z)
    rot = alg.get_rotation_from_fixed_zyz(phi, theta, psi)
    transformation = alg.Transformation3D(rot, trns)
    return transformation
Пример #13
0
def create_sampling_grid_3d(diameter, n_axis_points):
    """
        Creates a grid of positions (Vector3Ds), centered at 0.
        @param diameter The shape of the grid is a sphere with this diameter.
        @param n_axis_points Number of points used alogn an axis for the grid.
        The axis X Y and Z will contain n_axis_points, equispaced.
        The other regions of space will contain only the points allowed by
        the size of the spere.
    """
    radius = diameter / 2.0
    step = diameter / n_axis_points
    points = [-radius + step * n for n in range(n_axis_points + 1)]
    # filter points closer than 1A
    points = [x for x in points if abs(x) > 1]
    log.info("Points along the axis %s", points)
    positions = []
    for x, y, z in itertools.product(points, points, points):
        d = (x**2. + y**2. + z**2.)**.5
        # allow 1% margin. Avoids approximation problems
        if (d < (1.01 * radius)):
            positions.append(alg.Vector3D(x, y, z))
    return positions
Пример #14
0
def create_sampling_grid_2d(diameter, n_axis_points):
    """
        Creates a grid of positions (Vector3Ds), centered at 0.
        The shape of the grid is a circle with diameter given by the parameter.
        n_axis_points is the number of points along an axis: The axis X Y
        will contain n_axis_points, equispaced. The other regions of space will
        contain only the points allowed by the size of the circle.
    """
    if (diameter < 0):
        raise ValueError("create_sampling_grid_2d: Negative diameter.")
    if (n_axis_points < 1):
        raise ValueError("create_sampling_grid_2d: Less than one axis point.")
    radius = diameter / 2.0
    step = diameter / n_axis_points
    points = [-radius + step * n for n in range(n_axis_points + 1)]
    log.info("Points along the axis %s", points)
    positions = []
    for x, y in itertools.product(points, points):
        d = (x**2. + y**2.)**.5
        # allow 1% margin. Avoids approximation problems
        if (d < (1.01 * radius)):
            positions.append(alg.Vector3D(x, y, 0.0))
    return positions
Пример #15
0
def do_packing(conf, op, pymol_file_name, verbose=True):
    m = I.Model()

    box_size = [op['box']['x'], op['box']['y'], op['box']['z']]
    corner0 = IAL.Vector3D(0, 0, 0)
    corner1 = IAL.Vector3D(box_size[0], box_size[1], box_size[2])
    box = IAL.BoundingBox3D(corner0, corner1)

    expand_n = 1
    corner0_expand = IAL.Vector3D(0, 0, 0)
    corner1_expand = IAL.Vector3D(box_size[0] * expand_n,
                                  box_size[1] * expand_n,
                                  box_size[2] * expand_n)
    box_expand = IAL.BoundingBox3D(corner0_expand, corner1_expand)

    ps = []

    for i, b in enumerate(conf):
        p = I.Particle(m)

        v = IAL.get_random_vector_in(box_expand)  # lsn: radom posotion

        s = IAL.Sphere3D(v, b['r'])  # lsn: position, radius

        pt = IC.XYZR.setup_particle(p, s)  # lsn: particle, parameters

        pt = IAT.Mass.setup_particle(p, b['mass'])

        p.add_attribute(
            I.FloatKey('vx'), 0.0
        )  # initial velocity attribute is needed for molecular dynamics simulation
        p.add_attribute(
            I.FloatKey('vy'), 0.0
        )  # initial velocity attribute is needed for molecular dynamics simulation
        p.add_attribute(I.FloatKey('vz'), 0.0)

        ps.append(p)
    '''for i, p in enumerate(ps):
        xv = m.get_attribute(I.FloatKey('vx'), p)
        yv = m.get_attribute(I.FloatKey('vy'), p)
        zv = m.get_attribute(I.FloatKey('vz'), p)

        if N.isnan(xv):
            m.set_attribute(I.FloatKey('vx'), p, 0.0)

        if N.isnan(yv):
            m.set_attribute(I.FloatKey('vy'), p, 0.0)

        if N.isnan(zv):
            m.set_attribute(I.FloatKey('vz'), p, 0.0)

        ps[i] = p'''

    #TODO: Group spheres together for multiBall packing

    lsc_ps = ICO.ListSingletonContainer(m, ps)

    sscell = IC.BoundingBox3DSingletonScore(IC.HarmonicUpperBound(0, 1), box)

    #TODO: New scoring method
    """sf = IC.RestraintsScoringFunction([ICO.SingletonsRestraint(sscell, lsc_ps), IC.ExcludedVolumeRestraint(lsc_ps)])"""

    temprature = op['temprature']

    o = IAT.MolecularDynamics(m)
    o.set_particles(ps)
    o.set_scoring_function(sf)
    o.assign_velocities(temprature)

    s = N.inf

    recent_scores = []

    while (s > op['min score']) and (temprature > 0):
        o.assign_velocities(temprature)
        md = IAT.VelocityScalingOptimizerState(m, ps, temprature)
        o.add_optimizer_state(md)
        s = o.optimize(op['step'])

        cx = N.array(get_center_coordinates(ps))

        is_in = N.array([True] * len(ps))

        for dim_i in range(3):
            is_in[cx[:, dim_i].flatten() < 0] = False
            is_in[cx[:, dim_i].flatten() >= box_size[dim_i]] = False

        print(
            '\r',
            'score:',
            s,
            '    ',
            'temprature:',
            temprature,
            '       ',
            'center inside box particles: ',
            is_in.sum(),
            '              ',
        )

        if True:
            recent_scores.append(s)
            while len(recent_scores) > op['recent_scores number']:
                recent_scores.pop(0)

            if len(recent_scores) == op['recent_scores number']:
                x = N.array(range(len(recent_scores)))
                y = N.array(recent_scores)
                recent_scores_slope, intercept, r_value, p_value, std_err = stats.linregress(
                    x, y)

                if N.abs(recent_scores_slope) < op['recent_scores slope min']:
                    temprature *= 1 - op['temprature decrease']
                    recent_scores = []
        sys.stdout.flush()

    cx = get_center_coordinates(ps)

    conf = copy.deepcopy(conf)
    for i, b in enumerate(conf):
        b['x'] = cx[i]

    return {
        'conf': conf,
        'score': s,
        'temprature': temprature,
        'inside_box_num': is_in.sum()
    }