def listen_for_mouse(self, state):
        if state.m_event:
            m1 = state.m_event  # get event

            if m1.drop:
                self.pick = None
                self._update_path()

            if m1.button == 'left' and m1.alt and m1.pick:
                if m1.pick in self.points and m1.drag:
                    dist = util.distance(self.dragpos, m1.pickpos)
                    if dist < 1:
                        return

                    self.previous_points.append(
                        [vector(p.pos) for p in self.points])

                    self.dragpos = m1.pickpos
                    self.pick = m1.pick

                else:
                    self._add_points_on_rna(m1, state)

            elif m1.button == 'left' and m1.alt and not m1.drag:

                pos = m1.pos
                if len(self.points) > 0:
                    dist = util.distance(pos, self.points[-1].pos)
                    if dist < 1:
                        return

                #need points on RNA first
                if len(self.points) == 0:
                    return

                #no points after reconnecting to RNA
                if len(self.highlighted_ends) == 2:
                    dist = util.distance(self.points[-1].pos,
                                         self.highlighted_ends[1][1].d)
                    if dist < 0.1:
                        return

                p = sphere(pos=pos, radius=7.5, color=color.cyan)
                self.points.append(p)

                if len(self.points) > 1:
                    line = cylinder(pos=self.points[-2].pos,
                                    axis=(p.pos - self.points[-2].pos) / 2,
                                    color=color.cyan,
                                    radius=7)
                    self.lines.append(line)

                    new_pos = self.points[-2].pos + (p.pos -
                                                     self.points[-2].pos) / 2
                    line = cylinder(pos=new_pos,
                                    axis=(new_pos - self.points[-2].pos),
                                    color=color.cyan,
                                    radius=7)
                    self.lines.append(line)
    def listen_for_mouse(self, state):
        if  state.m_event:
            m1 = state.m_event# get event

            if m1.drop:
                self.pick = None
                self._update_path()

            if m1.button == 'left' and m1.alt and m1.pick:
                if m1.pick in self.points and m1.drag:
                    dist = util.distance(self.dragpos, m1.pickpos)
                    if dist < 1:
                        return

                    self.previous_points.append([vector(p.pos) for p in self.points])

                    self.dragpos = m1.pickpos
                    self.pick = m1.pick

                else:
                    self._add_points_on_rna(m1, state)

            elif m1.button == 'left' and m1.alt and not m1.drag:

                pos = m1.pos
                if len(self.points) > 0:
                    dist = util.distance(pos, self.points[-1].pos)
                    if dist < 1:
                        return

                #need points on RNA first
                if len(self.points) == 0:
                    return

                #no points after reconnecting to RNA
                if len(self.highlighted_ends) == 2:
                    dist = util.distance(self.points[-1].pos, self.highlighted_ends[1][1].d)
                    if dist < 0.1:
                        return

                p = sphere(pos=pos, radius=7.5, color=color.cyan)
                self.points.append(p)


                if len(self.points) > 1:
                    line = cylinder(pos=self.points[-2].pos,
                                    axis=(p.pos - self.points[-2].pos)/2,
                                    color=color.cyan,
                                    radius = 7)
                    self.lines.append(line)

                    new_pos = self.points[-2].pos + (p.pos - self.points[-2].pos)/2
                    line = cylinder(pos=new_pos,
                                    axis=(new_pos - self.points[-2].pos),
                                    color=color.cyan,
                                    radius = 7)
                    self.lines.append(line)
    def listen(self):
        self.state.key = self.listen_for_keys()
        self.state.m_event = self.listen_for_mouse()

        if self.state.update_center:
            self._center_view()
            self.state.update_center = 0

        if self.dragging:
            vec1 = util.normalize(self.scene.mouse.pos - self.lastpos)

            if util.distance(vec1, [0, 0, 0]) > 0.1:
                vec2 = self.scene.forward
                rotation = cross(vec1, vec2)
                mag = util.distance(self.scene.mouse.pos - self.lastpos,
                                    [0, 0, 0])
                self.scene.forward = self.scene.forward.rotate(angle=0.0007 *
                                                               mag,
                                                               axis=rotation)

                if self.scene.forward.y > 0.90:
                    self.scene.forward.y = 0.90
                if self.scene.forward.y < -0.90:
                    self.scene.forward.y = -0.90

        if self.zooming:
            vec1 = util.normalize(self.scene.mouse.pos - self.lastpos)

            if util.distance(vec1, [0, 0, 0]) > 0.1:

                mag = util.distance(self.scene.mouse.pos - self.lastpos,
                                    [0, 0, 0])
                if vec1[1] > 0:
                    self.scene.range = self.range / (1.000 + 0.0005 * mag)
                    self.range = self.range / (1.000 + 0.0005 * mag)
                if vec1[1] < 0:
                    self.scene.range = self.range / (1.000 - 0.0005 * mag)
                    self.range = self.range / (1.000 - 0.0005 * mag)

        val = self.tc.GetValue()
        spl = val.split("\n")

        if len(spl) > 1:
            self._parse_commands(spl[0])
            self.tc.SetValue("")

        for f in self.functions:
            if not f.activated:
                continue
            f.listen(self.state)
    def listen(self):
        self.state.key = self.listen_for_keys()
        self.state.m_event = self.listen_for_mouse()

        if self.state.update_center:
            self._center_view()
            self.state.update_center = 0

        if self.dragging:
            vec1 = util.normalize(self.scene.mouse.pos - self.lastpos)

            if util.distance(vec1, [0, 0, 0]) > 0.1:
                vec2 = self.scene.forward
                rotation = cross(vec1, vec2)
                mag = util.distance(self.scene.mouse.pos - self.lastpos, [0,0,0])
                self.scene.forward = self.scene.forward.rotate(angle=0.0007*mag,
                                                               axis=rotation)

                if self.scene.forward.y > 0.90:
                    self.scene.forward.y = 0.90
                if self.scene.forward.y < -0.90:
                    self.scene.forward.y = -0.90

        if self.zooming:
            vec1 = util.normalize(self.scene.mouse.pos - self.lastpos)

            if util.distance(vec1, [0, 0, 0]) > 0.1:

                mag = util.distance(self.scene.mouse.pos - self.lastpos, [0,0,0])
                if vec1[1] > 0:
                    self.scene.range = self.range/(1.000 + 0.0005*mag)
                    self.range = self.range/(1.000 + 0.0005*mag)
                if vec1[1] < 0:
                    self.scene.range = self.range/(1.000 - 0.0005*mag)
                    self.range = self.range/(1.000 - 0.0005*mag)


        val = self.tc.GetValue()
        spl = val.split("\n")

        if len(spl) > 1:
            self._parse_commands(spl[0])
            self.tc.SetValue("")

        for f in self.functions:
            if not f.activated:
                continue
            f.listen(self.state)
Example #5
0
    def _get_segments(self, path):

        direction = [0, 0, 0]
        new_direction = None
        segments = []
        segment = []
        for i, p in enumerate(path):
            if i < 2:
                segment.append(p)
                if i == 1:
                    direction = util.normalize(segment[0] - segment[1])
                continue

            new_direction = util.normalize(segment[-1] - p)
            if util.distance(new_direction, direction) > 0.1:
                segments.append(segment)
                segment = [p]
                direction = new_direction
            else:
                segment.append(p)

        if len(segment) > 0:
            segments.append(segment)

        return segments
    def _get_segments(self, path):

        direction = [0, 0, 0]
        new_direction = None
        segments = []
        segment = []
        for i, p in enumerate(path):
            if i < 2:
                segment.append(p)
                if i == 1:
                    direction = util.normalize(segment[0] - segment[1])
                continue

            new_direction = util.normalize(segment[-1] - p)
            if util.distance(new_direction, direction) > 0.1:
                segments.append(segment)
                segment = [p]
                direction = new_direction
            else:
                segment.append(p)

        if len(segment) > 0:
            segments.append(segment)

        return segments
    def _run_path_finding(self, state):
        if self.built:
            state.vmg.remove_node_level(1)
            state.vmg.mg.decrease_level()

        all_points = []
        reg_points = []
        for p in self.points:
            reg_points.append(np.array(p.pos))

        for i in range(len(reg_points) - 1):
            all_points.append(reg_points[i])
            diff = reg_points[i + 1] - reg_points[i]
            unit_vector = util.normalize(diff)
            current = reg_points[i] + unit_vector * 5.0
            while util.distance(reg_points[i + 1], current) > 5.0:
                all_points.append(current)
                current = current + unit_vector * 5.0

        s = basic_io.points_to_str(all_points)
        f = open("all_points.str", "w")
        f.write(s)
        f.close()
        basic_io.points_to_pdb("all_points.pdb", all_points)

        f = open("mg.top", "w")
        f.write(state.vmg.mg.topology_to_str() + "\n")
        f.write(
            str(self.highlighted_ends[0][0]) + " " +
            self.highlighted_ends[0][1].name())
        f.close()

        self.path_builder.build()

        f = open("mt_out.top")
        lines = f.readlines()
        f.close()

        mt = motif_tree.motif_tree_from_topology_str(lines[0])
        state.vmg.mg.increase_level()
        state.vmg.add_motif_tree(mt, self.highlighted_ends[0][0],
                                 self.highlighted_ends[0][1].name())

        for p in self.points:
            p.opacity = 0.5
        for l in self.lines:
            l.opacity = 0.5

        self.built = 1
        state.update_center = 1
    def _run_path_finding(self, state):
        if self.built:
            state.vmg.remove_node_level(1)
            state.vmg.mg.decrease_level()

        all_points = []
        reg_points = []
        for p in self.points:
            reg_points.append(np.array(p.pos))

        for i in range(len(reg_points)-1):
            all_points.append(reg_points[i])
            diff = reg_points[i+1] - reg_points[i]
            unit_vector = util.normalize(diff)
            current = reg_points[i] + unit_vector*5.0
            while util.distance(reg_points[i+1], current) > 5.0:
                all_points.append(current)
                current = current + unit_vector*5.0

        s = basic_io.points_to_str(all_points)
        f = open("all_points.str", "w")
        f.write(s)
        f.close()
        basic_io.points_to_pdb("all_points.pdb", all_points)

        f = open("mg.top", "w")
        f.write(state.vmg.mg.topology_to_str() + "\n")
        f.write(str(self.highlighted_ends[0][0]) + " " + self.highlighted_ends[0][1].name())
        f.close()

        self.path_builder.build()

        f = open("mt_out.top")
        lines = f.readlines()
        f.close()

        mt = motif_tree.motif_tree_from_topology_str(lines[0])
        state.vmg.mg.increase_level()
        state.vmg.add_motif_tree(mt,
                                 self.highlighted_ends[0][0],
                                 self.highlighted_ends[0][1].name())

        for p in self.points:
            p.opacity = 0.5
        for l in self.lines:
            l.opacity = 0.5

        self.built = 1
        state.update_center = 1
Example #9
0
def contact_map_from_pdbs(pdbs, res_seperation=8, max_dist=14, max=100000):
    if len(pdbs) == 0:
        raise ValueError("no pdbs supplied")

    s = structure.structure_from_pdb(pdbs[0])
    num_res = len(s.residues())
    matrix = []

    for i in range(num_res):
        matrix.append([0 for j in range(num_res)])

    for k, pdb in enumerate(pdbs):
        if k > max:
            break
        s = structure.structure_from_pdb(pdb)
        residues = s.residues()

        for i, r1 in enumerate(residues):
            try:
                beads = r1.get_beads()
                a1 = beads[-1]
            except:
                continue
            for j, r2 in enumerate(residues):
                if abs(i - j) < res_seperation:
                    continue
                if i >= j:
                    continue
                try:
                    beads = r2.get_beads()
                    a2 = beads[-1]
                except:
                    continue

                dist = util.distance(a1.center, a2.center)
                if dist < max_dist:
                    matrix[i][j] += 1
    return matrix
        continue
    m.name = r.side1 + "=" + r.side2

    seqs = m.sequence().split("&")
    flank1_5prime, flank1_3prime = get_final_flank_sequences(
        r.flank1_5prime, r.flank1_3prime, seqs[0])
    flank2_5prime, flank2_3prime = get_final_flank_sequences(
        r.flank2_5prime, r.flank2_3prime, seqs[1])
    front_bps = get_bp_steps_from_seqs(flank1_5prime, flank2_3prime)
    end_bps = get_bp_steps_from_seqs(flank1_3prime, flank2_5prime)

    rs = get_combined_structure(front_bps, end_bps, m)
    #print rs.name + "." + r.side1_all + "." + r.side2_all
    motifs[rs.name + "." + r.side1_all + "." + r.side2_all] = rs

    dist_1 = util.distance(origin, rs.ends[0].d())
    dist_2 = util.distance(origin, rs.ends[1].d())

    if dist_1 > dist_2:
        diff.append(basic_io.point_to_str(rs.ends[0].d()))
        rot_diff.append(basic_io.matrix_to_str(rs.ends[0].r()))
    else:
        diff.append(basic_io.point_to_str(rs.ends[1].d()))
        rot_diff.append(basic_io.matrix_to_str(rs.ends[1].r()))

#df['translation'] = diff
#df['rotation'] = rot_diff

#fname = util.filename(args.csv)
#df.to_csv(fname[:-4]+"_diff.csv", index=False)
    def listen_for_keys(self, state):
        if state.key == 'u':
            if len(self.previous_points) > 0:
                pos = self.previous_points.pop()
                for i, p in enumerate(self.points):
                    p.pos = pos[i]
                self._update_path()

        if state.key == 'd':
            if len(self.points) == 0:
                return

            if len(self.highlighted_ends) == 2:
                dist = util.distance(self.points[-1].pos,
                                     self.highlighted_ends[1][1].d)
                if dist < 0.1:
                    self.highlighted_ends.pop()

                    for i in range(2):
                        last = self.points.pop()
                        last.visible = False
                        del last

                    for i in range(3):
                        last = self.lines.pop()
                        last.visible = False
                        del last
                    return

            last = self.points.pop()
            last.visible = False
            del last

            if len(self.lines) != 0:
                last_l = self.lines.pop()
                last_l.visible = False
                del last_l

                if len(self.lines) != 0:
                    last_l = self.lines.pop()
                    last_l.visible = False
                    del last_l

            if len(self.points) == 1:
                last = self.points.pop()
                last.visible = False
                del last

        if state.key == 'r':
            self._run_path_finding(state)

        if state.key == 's':
            print "saving session: rnamake.save"
            f = open("rnamake.save", "w")
            f.write(state.vmg.mg.topology_to_str() + "\n")
            for n in state.vmg.mg.graph.nodes:
                f.write(str(n.level) + " ")
            f.write("\n")

            point_pos = []
            for p in self.points:
                point_pos.append([p.pos.x, p.pos.y, p.pos.z])

            f.write(basic_io.points_to_str(point_pos) + "\n")
            f.write(
                str(self.highlighted_ends[0][0]) + " " +
                self.highlighted_ends[0][1].name() + "\n")
            if len(self.highlighted_ends) > 1:
                f.write(
                    str(self.highlighted_ends[1][0]) + " " +
                    self.highlighted_ends[1][1].name() + "\n")

            f.close()

        if state.key == 'f':
            pass

        if state.key == 'p':
            print "printing structure: rnamake.pdb"
            state.vmg.mg.to_pdb("rnamake.pdb", close_chain=1, renumber=1)
    def _add_points_on_rna(self, m1, state):
        for ni, v_end in state.vmg.open_bp_ends:
            if not v_end.clicked(m1.pick):
                continue

            included = 0
            for i, high_end in self.highlighted_ends:
                if v_end == high_end:
                    included = 1

            if included:
                continue

            self.highlighted_ends.append([ni, v_end])

            if len(self.points) > 1:
                """pos = v_end.d
                p = sphere(pos=pos, radius=0.1, color=color.cyan)
                self.points.append(p)

                line = cylinder(pos=self.points[-2].pos,
                                axis=(p.pos - self.points[-2].pos)/2,
                                color=color.cyan,
                                radius = 7)
                self.lines.append(line)

                new_pos = self.points[-2].pos + (p.pos - self.points[-2].pos)/2
                line = cylinder(pos=new_pos,
                                axis=(new_pos - self.points[-2].pos),
                                color=color.cyan,
                                radius = 7)
                self.lines.append(line)"""

                pos = v_end.d
                p1 = sphere(pos=pos, radius=0.1, color=color.cyan)

                dir = v_end.r[2]

                new_pos = pos - dir * 20
                p2 = sphere(pos=new_pos, radius=7.5, color=color.cyan)

                line = cylinder(pos=self.points[-1].pos,
                                axis=(p2.pos - self.points[-1].pos) / 2,
                                color=color.cyan,
                                radius=7)

                new_pos = self.points[-1].pos + (p2.pos -
                                                 self.points[-1].pos) / 2
                line2 = cylinder(pos=new_pos,
                                 axis=(new_pos - self.points[-1].pos),
                                 color=color.cyan,
                                 radius=7)
                self.lines.append(line)
                self.lines.append(line2)

                line3 = cylinder(pos=pos,
                                 axis=-dir * 20,
                                 color=color.cyan,
                                 radius=7)

                self.points.append(p2)

                self.points.append(p1)
                self.lines.append(line3)
                #self._update_path()
                return

            if len(self.highlighted_ends) > 2:
                return

            pos = v_end.d
            if len(self.points) > 0:
                dist = util.distance(pos, self.points[-1].pos)
                if dist < 1:
                    return

            p = sphere(pos=pos, radius=0.1, color=color.cyan)
            self.points.append(p)

            dir = v_end.r[2]
            line = cylinder(pos=pos,
                            axis=-dir * 20,
                            color=color.cyan,
                            radius=7)

            self.lines.append(line)

            new_pos = pos - dir * 20
            p = sphere(pos=new_pos, radius=7.5, color=color.cyan)
            self.points.append(p)
    def listen_for_keys(self, state):
        if state.key == 'u':
            if len(self.previous_points) > 0:
                pos = self.previous_points.pop()
                for i, p in enumerate(self.points):
                    p.pos = pos[i]
                self._update_path()

        if state.key == 'd':
            if len(self.points) == 0:
                return

            if len(self.highlighted_ends) == 2:
                dist = util.distance(self.points[-1].pos, self.highlighted_ends[1][1].d)
                if dist < 0.1:
                    self.highlighted_ends.pop()

                    for i in range(2):
                        last = self.points.pop()
                        last.visible = False
                        del last

                    for i in range(3):
                        last = self.lines.pop()
                        last.visible = False
                        del last
                    return

            last = self.points.pop()
            last.visible = False
            del last

            if len(self.lines) != 0:
                last_l = self.lines.pop()
                last_l.visible = False
                del last_l

                if len(self.lines) != 0:
                    last_l = self.lines.pop()
                    last_l.visible = False
                    del last_l

            if len(self.points) == 1:
                last  = self.points.pop()
                last.visible = False
                del last

        if state.key == 'r':
            self._run_path_finding(state)

        if state.key == 's':
            print "saving session: rnamake.save"
            f = open("rnamake.save", "w")
            f.write(state.vmg.mg.topology_to_str() + "\n")
            for n in state.vmg.mg.graph.nodes:
                f.write(str(n.level) + " ")
            f.write("\n")

            point_pos = []
            for p in self.points:
                point_pos.append([p.pos.x, p.pos.y, p.pos.z])

            f.write(basic_io.points_to_str(point_pos) + "\n")
            f.write(str(self.highlighted_ends[0][0]) + " "  +
                    self.highlighted_ends[0][1].name() + "\n")
            if len(self.highlighted_ends) > 1:
                f.write(str(self.highlighted_ends[1][0]) + " "  +
                    self.highlighted_ends[1][1].name() + "\n")

            f.close()

        if state.key == 'f':
            pass

        if state.key == 'p':
            print "printing structure: rnamake.pdb"
            state.vmg.mg.to_pdb("rnamake.pdb", close_chain=1,renumber=1)
    def _add_points_on_rna(self, m1, state):
        for ni, v_end in state.vmg.open_bp_ends:
            if not v_end.clicked(m1.pick):
                continue

            included = 0
            for i, high_end in self.highlighted_ends:
                if v_end == high_end:
                    included = 1

            if included:
                continue

            self.highlighted_ends.append([ni, v_end])

            if len(self.points) > 1:
                """pos = v_end.d
                p = sphere(pos=pos, radius=0.1, color=color.cyan)
                self.points.append(p)

                line = cylinder(pos=self.points[-2].pos,
                                axis=(p.pos - self.points[-2].pos)/2,
                                color=color.cyan,
                                radius = 7)
                self.lines.append(line)

                new_pos = self.points[-2].pos + (p.pos - self.points[-2].pos)/2
                line = cylinder(pos=new_pos,
                                axis=(new_pos - self.points[-2].pos),
                                color=color.cyan,
                                radius = 7)
                self.lines.append(line)"""

                pos = v_end.d
                p1 = sphere(pos=pos, radius=0.1, color=color.cyan)

                dir = v_end.r[2]


                new_pos = pos - dir*20
                p2 = sphere(pos=new_pos, radius=7.5, color=color.cyan)

                line = cylinder(pos=self.points[-1].pos,
                                axis=(p2.pos - self.points[-1].pos)/2,
                                color=color.cyan,
                                radius = 7)

                new_pos = self.points[-1].pos + (p2.pos - self.points[-1].pos)/2
                line2 = cylinder(pos=new_pos,
                                axis=(new_pos - self.points[-1].pos),
                                color=color.cyan,
                                radius = 7)
                self.lines.append(line)
                self.lines.append(line2)

                line3 = cylinder(pos=pos, axis=-dir*20,
                                color=color.cyan, radius=7)

                self.points.append(p2)

                self.points.append(p1)
                self.lines.append(line3)
                #self._update_path()
                return

            if len(self.highlighted_ends) > 2:
                return

            pos = v_end.d
            if len(self.points) > 0:
                dist = util.distance(pos, self.points[-1].pos)
                if dist < 1:
                    return

            p = sphere(pos=pos, radius=0.1, color=color.cyan)
            self.points.append(p)

            dir = v_end.r[2]
            line = cylinder(pos=pos, axis=-dir*20,
                            color=color.cyan, radius=7)

            self.lines.append(line)

            new_pos = pos - dir*20
            p = sphere(pos=new_pos, radius=7.5, color=color.cyan)
            self.points.append(p)