Exemple #1
0
 def motif_from_res(self, res, bps):
     m = motif.Motif()
     chains = chain.connect_residues_into_chains(res)
     m.structure.chains = chains
     m.basepairs = bps
     ends = self._setup_basepair_ends(m.structure, bps)
     m.ends = ends
     self._setup_secondary_structure(m)
     return m
Exemple #2
0
 def motif_from_chains(self, chains, bps):
     m = motif.Motif()
     m.structure.chains = chains
     m.basepairs = bps
     ends = self._setup_basepair_ends(m.structure, bps)
     m.ends = ends
     try:
         self._setup_secondary_structure(m)
     except:
         pass
     return m
Exemple #3
0
 def motif_from_bps(self, bps):
     m = motif.Motif()
     res = []
     for bp in bps:
         res.extend(bp.residues())
     chains = chain.connect_residues_into_chains(res)
     m.structure.chains = chains
     m.basepairs = bps
     m.ends = self._setup_basepair_ends(m.structure, m.basepairs)
     self._setup_secondary_structure(m)
     return m
Exemple #4
0
    def motif_from_file(self, path, include_protein=0):
        filename = util.filename(path)
        # is a motif directory
        if os.path.isdir(path):
            s = structure.structure_from_pdb(path + "/" + filename + ".pdb")

        else:
            s = structure.structure_from_pdb(path)
            filename = filename[:-4]

        basepairs = rna_structure.basepairs_from_x3dna(path, filename, s)
        ends      = rna_structure.ends_from_basepairs(s, basepairs)

        r_struct = rna_structure.RNAStructure(s, basepairs, ends, filename,
                                              path, motif_type.UNKNOWN)

        #if len(r_struct.residues()) == 0:
        #    raise exceptions.MotifFactoryException(
        #   )

        m           = motif.Motif(r_struct)
        m.score     = self.scorer.score(m)

        #try:
        self._setup_secondary_structure(m)
        #except:
        #    print "did not parse secondary_structure", m.name

        if include_protein:
            if os.path.isdir(path):
                p_residues = pdb_parser.parse(path + "/" + filename + ".pdb",
                                              protein=1, rna=0)
            else:
                p_residues = pdb_parser.parse(path, protein=1, rna=0)
            beads = []
            for r in p_residues:
                a = r.get_atom("CA")
                beads.append(residue.Bead(a.coords, residue.BeadType.BASE))
            m.protein_beads = beads

        return m
Exemple #5
0
    def mdmodule(self, motif_widths=(7, 10, 13, 16, 19), width=600):
        """Run a de novo motif scan of ChIP regions.
        
        De novo motifs will be returned as a MotifList object.
        
        Adapted from Cliff Meyer's ChIP_region.MDscan() method.
        Directly calls Xiaole Shirley Liu's _MDmod program."""
        motifs = motif.MotifList()
        # prepare input sequences
        input = self.copy()
        input.unique()
        input.trim(int(width / 2))
        input.read_sequence(True)

        #OBSOLETE: DELETE!!
        # prepare background sequences
        background = self.copy()
        background.unique()
        background.trim(int(width))
        background.read_sequence(True)

        # scan for motifs
        for motif_width in motif_widths:
            raw_pssms = MDmod(i=input.sequence,
                              b=background.sequence,
                              w=motif_width,
                              t=50,
                              s=50,
                              n=100,
                              r=10)
            for raw_pssm in raw_pssms:
                motif_id = 'denovo%d' % len(motifs)
                tmp = motif.Motif()
                tmp.id = motif_id
                tmp.pssm = raw_pssm
                motifs.append(tmp)
        return motifs
Exemple #6
0
 def _ConvertToOldMotif(self, motifid):
     #import mdseqpos.motif as motif
     p = motif.Motif()
     p = p.from_dict(self.motifs[motifid])
     return p
Exemple #7
0
    def pose_from_motif_tree_new(self, structure, basepairs, nodes,
                                 designable):
        p = pose.Pose()
        p.designable = designable
        p.name = "assembled"
        p.path = "assembled"
        p.structure = structure
        p.basepairs = basepairs
        p.ends = motif_factory.factory._setup_basepair_ends(
            structure, basepairs)
        ss = secondary_structure_factory.factory.secondary_structure_from_motif(
            p)
        p.secondary_structure = ss
        p.end_ids = ["" for x in p.ends]
        for i, end in enumerate(p.ends):
            res1 = ss.get_residue(uuid=end.res1.uuid)
            res2 = ss.get_residue(uuid=end.res2.uuid)
            ss_end = ss.get_bp(res1, res2)
            p.end_ids[i] = secondary_structure.assign_end_id(ss, ss_end)

        #update all motifs in pose, make sure that the correct basepairs are used
        #since basepairs are used to align there are two choices of which basepair to
        #be used
        p_res = p.residues()
        for j, n in enumerate(nodes):
            m = n.data
            residue_map = {}
            print j
            chains = []
            res = []
            for c in m.chains():
                bounds = [0, len(c.residues)]
                first = 0
                start_pos = 0
                found = 0
                for i, r in enumerate(c.residues):
                    r_new = p.get_residue(uuid=r.uuid)
                    if r_new is not None:
                        first = i
                        start_pos = p_res.index(r_new)
                        found = 1
                        break

                bounds[0] = start_pos - first
                bounds[1] = bounds[0] + len(c.residues)
                print bounds[0], bounds[1]

                chains.append(chain.Chain(p_res[bounds[0]:bounds[1]]))
                res.extend(chains[-1].residues)

            bps = self._subselect_bps_from_res(p.basepairs, res)
            m_copy = motif_factory.factory.motif_from_chains(chains, bps)
            m_copy.to_pdb("m." + str(j) + ".pdb")
            m.to_pdb("org." + str(j) + ".pdb")
            continue

            exit()

            if len(bps) != len(m.basepairs):
                raise ValueError(
                    "something went horribly wrong: did not find all basepairs"
                )

            m_copy = motif.Motif()
            m_copy.mtype = m.mtype
            m_copy.name = m.name
            if m_copy.mtype is not motif_type.HELIX:
                best_ends = []
                best_end_ids = []
                for i, end in enumerate(m.ends):
                    best = 1000
                    best_end = None
                    best_end_id = None
                    for c_end in m_copy.ends:
                        dist = util.distance(end.d(), c_end.d())
                        if dist < best:
                            best_end = c_end
                            best_end_id = m.end_ids[i]
                            best = dist
                    best_ends.append(best_end)
                    best_end_ids.append(best_end_id)

                m_copy.ends = best_ends
                m_copy.end_ids = best_end_ids

            p.motif_dict[m_copy.mtype].append(m_copy)
            p.motif_dict[motif_type.ALL].append(m_copy)

        self._add_secondary_structure_motifs(p)
        self.standardize_pose(p)

        return p
Exemple #8
0
    def pose_from_motif_tree(self, structure, basepairs, nodes, designable):

        p = pose.Pose()
        p.designable = designable
        p.name = "assembled"
        p.path = "assembled"
        p.structure = structure
        p.basepairs = basepairs
        p.ends = motif_factory.factory._setup_basepair_ends(
            structure, basepairs)
        ss = secondary_structure_factory.factory.secondary_structure_from_motif(
            p)
        p.secondary_structure = ss
        p.end_ids = ["" for x in p.ends]
        for i, end in enumerate(p.ends):
            res1 = ss.get_residue(uuid=end.res1.uuid)
            res2 = ss.get_residue(uuid=end.res2.uuid)
            ss_end = ss.get_bp(res1, res2)
            p.end_ids[i] = secondary_structure.assign_end_id(ss, ss_end)

        #update all motifs in pose, make sure that the correct basepairs are used
        #since basepairs are used to align there are two choices of which basepair to
        #be used
        for j, n in enumerate(nodes):
            m = n.data
            bps = []
            residue_map = {}
            for bp in m.basepairs:
                new_bp = p.get_basepair(bp_uuid=bp.uuid)
                if len(new_bp) != 0:
                    bps.append(new_bp[0])
                    continue
                best = 1000
                best_bp = None
                for c in n.connections:
                    if c is None:
                        continue
                    if bp not in m.ends:
                        raise ValueError("cannot find a non end basepair")
                    if c.end_index(n.index) != m.ends.index(bp):
                        continue
                    partner = c.partner(n.index)
                    alt_bp = partner.data.ends[c.end_index(partner.index)]
                    pose_bps = p.get_basepair(bp_uuid=alt_bp.uuid)
                    if len(pose_bps) == 0:
                        raise ValueError("cannot find partner end bp in pose")
                    #TODO Check if this is okay, not sure if I have to try and match
                    #TODO residues correctly
                    residue_map[bp.res1] = pose_bps[0].res1
                    residue_map[bp.res2] = pose_bps[0].res2
                    bps.append(pose_bps[0])
                    break

            chains = []
            for c in m.chains():
                res = []
                for r in c.residues:
                    new_r = p.get_residue(uuid=r.uuid)
                    if new_r is not None:
                        res.append(new_r)
                    elif r in residue_map:
                        res.append(residue_map[r])
                    else:
                        print r, r.uuid
                        raise ValueError("cannot find residue")
                chains.append(chain.Chain(res))
            if len(bps) != len(m.basepairs):
                raise ValueError(
                    "something went horribly wrong: did not find all basepairs"
                )

            m_copy = motif.Motif()
            m_copy.mtype = m.mtype
            m_copy.name = m.name
            m_copy.structure.chains = chains
            m_copy.basepairs = bps
            m_copy.ends = motif_factory.factory._setup_basepair_ends(
                m_copy.structure, bps)
            motif_factory.factory._setup_secondary_structure(m_copy)
            if m_copy.mtype is not motif_type.HELIX:
                best_ends = []
                best_end_ids = []
                for i, end in enumerate(m.ends):
                    best = 1000
                    best_end = None
                    best_end_id = None
                    for c_end in m_copy.ends:
                        dist = util.distance(end.d(), c_end.d())
                        if dist < best:
                            best_end = c_end
                            best_end_id = m.end_ids[i]
                            best = dist
                    best_ends.append(best_end)
                    best_end_ids.append(best_end_id)

                m_copy.ends = best_ends
                m_copy.end_ids = best_end_ids

            p.motif_dict[m_copy.mtype].append(m_copy)
            p.motif_dict[motif_type.ALL].append(m_copy)

        self._add_secondary_structure_motifs(p)
        self.standardize_pose(p)
        return p
Exemple #9
0
    def pose_from_motif_tree_old(self, structure, basepairs, motifs,
                                 designable):

        p = pose.Pose()
        p.designable = designable
        p.name = "assembled"
        p.path = "assembled"
        p.structure = structure
        p.basepairs = basepairs
        p.ends = motif_factory.factory._setup_basepair_ends(
            structure, basepairs)
        ss = secondary_structure_factory.factory.secondary_structure_from_motif(
            p)
        p.secondary_structure = ss
        p.end_ids = ["" for x in p.ends]
        for i, end in enumerate(p.ends):
            res1 = ss.get_residue(uuid=end.res1.uuid)
            res2 = ss.get_residue(uuid=end.res2.uuid)
            ss_end = ss.get_bp(res1, res2)
            p.end_ids[i] = secondary_structure.assign_end_id(ss, ss_end)

        #update all motifs in pose, make sure that the correct basepairs are used
        #since basepairs are used to align there are two choices of which basepair to
        #be used
        for j, m in enumerate(motifs):
            bps = []
            residue_map = {}
            for bp in m.basepairs:
                new_bp = p.get_basepair(bp_uuid=bp.uuid)
                #print bp.residues(), len(new_bp)
                if len(new_bp) != 0:
                    bps.append(new_bp[0])
                    continue
                best = 1000
                best_bp = None
                for m2 in motifs:
                    if m == m2:
                        continue
                    for end in m2.ends:
                        alt_bp = p.get_basepair(bp_uuid=end.uuid)
                        if len(alt_bp) == 0:
                            continue
                        dist = util.distance(bp.d(), alt_bp[0].d())
                        if dist < best:
                            best_bp = alt_bp[0]
                            best = dist
                if best_bp is None:
                    continue
                new_bp = [best_bp]
                for r1 in best_bp.residues():
                    r1_cent = util.center(r1.atoms)
                    best_match = None
                    best_dist = 1000
                    for r2 in bp.residues():
                        r2_cent = util.center(r2.atoms)
                        dist = util.distance(r1_cent, r2_cent)
                        if dist < best_dist:
                            best_dist = dist
                            best_match = r2
                    residue_map[best_match] = r1
                bps.append(new_bp[0])

            chains = []
            #print m.name
            for c in m.chains():
                res = []
                for r in c.residues:
                    new_r = p.get_residue(uuid=r.uuid)
                    if new_r is not None:
                        res.append(new_r)
                    elif r in residue_map:
                        res.append(residue_map[r])
                    else:
                        print r, r.uuid
                        raise ValueError("cannot find residue")
                chains.append(chain.Chain(res))

            if len(bps) != len(m.basepairs):
                raise ValueError(
                    "something went horribly wrong: did not find all basepairs"
                )

            m_copy = motif.Motif()
            m_copy.mtype = m.mtype
            m_copy.name = m.name
            m_copy.structure.chains = chains
            m_copy.basepairs = bps
            m_copy.ends = motif_factory.factory._setup_basepair_ends(
                m_copy.structure, bps)
            motif_factory.factory._setup_secondary_structure(m_copy)
            if m_copy.mtype is not motif_type.HELIX:
                best_ends = []
                best_end_ids = []
                for i, end in enumerate(m.ends):
                    best = 1000
                    best_end = None
                    best_end_id = None
                    for c_end in m_copy.ends:
                        dist = util.distance(end.d(), c_end.d())
                        if dist < best:
                            best_end = c_end
                            best_end_id = m.end_ids[i]
                            best = dist
                    best_ends.append(best_end)
                    best_end_ids.append(best_end_id)

                m_copy.ends = best_ends
                m_copy.end_ids = best_end_ids

            p.motif_dict[m_copy.mtype].append(m_copy)
            p.motif_dict[motif_type.ALL].append(m_copy)

        self._add_secondary_structure_motifs(p)
        self.standardize_pose(p)
        return p