def pdb():
    """Get PDB content of selection.

    .. image:: ../../rna_tools/utils/PyMOL4RNA/doc/pdb.png"""
    tmpfn = '/tmp/pymol_get_pdb.pdb'
    cmd.save(tmpfn, '(sele)')
    s = RNAStructure(tmpfn)
    for l in s.lines:
        print(l)
Esempio n. 2
0
    # get version
    version = os.path.basename(os.path.dirname(
        os.path.abspath(__file__))), get_version(__file__)
    version = version[1].strip()

    # get parser and arguments
    parser = get_parser()

    args = parser.parse_args()

    # quick fix for one files vs file-s
    if list == type(args.file) and len(args.file) == 1:
        args.file = args.file[0]

    if args.report:
        s = RNAStructure(args.file)
        print(s.get_report)
        print(s.get_preview())
        print(s.get_info_chains())

    if args.clean:
        s = RNAStructure(args.file)
        s.decap_gtp()
        s.std_resn()
        s.remove_hydrogen()
        s.remove_ion()
        s.remove_water()
        s.renum_atoms()
        s.fix_O_in_UC()
        s.fix_op_atoms()
        # print s.get_preview()
Esempio n. 3
0
    def run(self, pdb_file, numSteps, verbose=False):
        """Compute SimRNA energy for a single file

        Arguments:
            * pdb_file = path to a PDB file
            * numSteps, number of SimRNA steps
            
        Output:
            * global energy as a floating point number
        """
        self.log('Running program')
        self.log('input - pdb_file: %s' % pdb_file)

        #copyfile(pdb_file, self.sandbox_dir + os.sep + 'query.pdb')
        s = RNAStructure(pdb_file)
        s.get_rnapuzzle_ready()
        s.write(self.sandbox_dir + os.sep + 'query.pdb', False)

        try:
            old_pwd = os.getcwd()
        except OSError:
            old_pwd = None
        os.chdir(self.sandbox_dir)

        if verbose: print('simrna::', self.sandbox_dir)

        # This is the place why you run SimRNA (self.src_bin)
        # you can add options like '-c', '~/tmp/config'
        # following flags mean:
        # -n 0 = 0 steps of simulation
        # -R 0 = set random seed to a constant,
        # results should be the same every time
        if run_command(
                self.src_bin,
            [
                '-p', self.sandbox_dir + os.sep + 'query.pdb', '-n',
                str(numSteps), '-R', '0'
            ],  # 16000
                stderr_file=self.sandbox_dir + os.sep + 'output.txt'):
            self.log('Run failed', 'error')

            # the place where I read the output file (stderr_file) of SimRNA
            f = open(self.sandbox_dir + os.sep + 'output.txt')
            stderr = f.read()
            f.close()
            self.log('STDERR:\n' + stderr, 'debug')
            if old_pwd:
                os.chdir(old_pwd)
            return -1
        if old_pwd:
            os.chdir(old_pwd)
        self.log('Run finished')

        # parse output
        scores = []
        f = open(self.sandbox_dir + os.sep + 'output.txt')
        lines_temp = f.read().split('\n')
        lines = lines_temp

        if verbose: print('simrna::', '\n'.join(lines))

        # the output is dicombosed into score
        total_enrg = [l for l in lines
                      if 'Total energy of system' in l][-1].split(' ')[-1]
        bb = [l for l in lines
              if 'Base-Base interactions energy' in l][-1].split(' ')[-1]
        ss = [l for l in lines if 'where:  short stacking energy' in l
              ][-1].strip().split(' ')[-1]
        bbc = [l for l in lines if 'Base-Backbone interact. energy' in l
               ][-1].strip().split(' ')[-1]
        lge = [l for l in lines
               if 'Local geometry energy' in l][-1].strip().split(' ')[-1]
        bcp = [l for l in lines
               if "bonds (distance)  C4'-P" in l][-1].strip().split(' ')[-1]
        bpc = [l for l in lines
               if "bonds (distance)  P-C4'" in l][-1].strip().split(' ')[-1]
        fa = [l for l in lines
              if "flat angles   C4'-P-C4'" in l][-1].strip().split(' ')[-1]
        fa2 = [l for l in lines
               if "flat angles    P-C4'-P" in l][-1].strip().split(' ')[-1]
        tor = [l for l in lines
               if "tors. eta vs tors. theta" in l][-1].strip().split(' ')[-1]
        lim = ''  # empty at the moment '' # [l for l in lines if "limit. sphere exceed penalty:" in l][-1].strip().split(' ')[-1]
        cc = [l for l in lines
              if "current chain energy:" in l][-1].strip().split(' ')[-1]
        scores += [numSteps] + [total_enrg] + [bb] + [ss] + [bbc] + [lge] + [
            bcp
        ] + [bpc] + [fa] + [fa2] + [tor] + [lim] + [cc]
        f.close()
        if verbose: print(' simrna::steps:', numSteps)
        #s = '\t'.join(scores)
        #if verbose: print ' simrna::scores:', s
        #return s
        return scores
#!/usr/bin/env python

import sys
from rna_tools.rna_tools_lib import RNAStructure


if __name__ == '__main__':
    files = sys.argv[1:]
    if not files:
        print('rna_pdb_merge_into_one.py test_in/*.pdb')
        sys.exit(1)

    c = 1
    for f in files:
        s = RNAStructure(f)
        print("MODEL        " + str(c))
        print(s.get_text(add_end=False))
        print('ENDMDL')
        c += 1
    print('END')
Esempio n. 5
0
            nmdp += l
    if verbose: print(nmdp)

    with open(mdp_out, 'w') as f:
        f.write(nmdp)


def get_parser():
        parser = argparse.ArgumentParser(
            description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)

        #parser.add_argument('-', "--", help="", default="")
        parser.add_argument("-v", "--verbose",
                            action="store_true", help="be verbose")
        parser.add_argument("file", help="", default="")
        return parser

# main
if __name__ == '__main__':
    #fn = 'test_data/decoy0165_amb_clx.pdb'
    fn = 'test_data/1duq.pdb'
    fn = 'test_data/1duq_rpr.pdb'
    # format_score_mdp('out.txt', '', '')
    # print(open('out.txt').read())
    from rna_tools.rna_tools_lib import RNAStructure
    r = RNAStructure(fn)
    for i in r.get_all_chain_ids():
        chain = r.get_chain(i)
        nchain = make_rna_gromacs_ready(chain)
        print(nchain).strip()
    def get_rmsd_to(self,
                    other_rnamodel,
                    way="",
                    triple_mode=False,
                    save=True):
        """Calc rmsd P-atom based rmsd to other rna model"""
        sup = Bio.PDB.Superimposer()
        if way == 'backbone+sugar':
            self.atoms_for_rmsd = []
            for a in self.atoms:
                if a.name in "P,OP1,OP2,C5',O5',C4',O4',C3',O3',C2',O2',C1'".split(
                        ','):
                    self.atoms_for_rmsd.append(a)
            if args.debug: print('atoms_for_rmsd', len(self.atoms_for_rmsd))

            other_atoms_for_rmsd = []

            for a in other_rnamodel.atoms:
                if a.name in "P,OP1,OP2,C5',O5',C4',O4',C3',O3',C2',O2',C1'".split(
                        ','):
                    other_atoms_for_rmsd.append(a)
            if args.debug:
                print('other_atoms_for_rmsd', len(other_atoms_for_rmsd))
        else:
            self.atoms_for_rmsd = self.atoms
            other_atoms_for_rmsd = other_rnamodel.atoms

        if triple_mode:

            def chunks(lst, n):
                """Yield successive n-sized chunks from lst.
                https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks
                """
                for i in range(0, len(lst), n):
                    yield lst[i:i + n]

            rmsd_min = 10000  # ugly
            import itertools
            per = list(itertools.permutations([0, 1, 2]))
            lst = list(chunks(other_atoms_for_rmsd, 12))
            sup_min = None

            for p in per:
                patoms = []
                for i in p:  # p=(1, 2, 3)
                    patoms.extend(lst[i])
                #print(self.atoms_for_rmsd)
                ## print('patoms')
                ## for a in patoms:
                ##     print(a, a.get_parent().get_id())
                ## print('self.atoms_for_rmsd')
                ## for a in self.atoms_for_rmsd:
                ##     print(a, a.get_parent().get_id())
                #sup.set_atoms(patoms, self.atoms_for_rmsd)
                sup.set_atoms(self.atoms_for_rmsd, patoms)
                rms = round(sup.rms, 3)

                rt = None
                seq = ''
                for a in patoms:
                    r = a.get_parent()
                    if r != rt:
                        rt = r
                        seq += r.get_resname().strip()

                if rms < rmsd_min:
                    rmsd_min = rms
                    sup_min = copy.copy(sup)
                    suffix = seq
                    p_min = p
                    seq_min = seq

                if args.debug:
                    print(p, '', [i + 1 for i in p], end=' ')
                    print(seq, 'seq_min:', seq_min, rms)

            index = [0, 0, 0]
            index[0] = p_min.index(0)
            index[1] = p_min.index(1)
            index[2] = p_min.index(2)

            # ugly re-set 123 to crazy id ! + 100, so this will
            # fill up 1 2 3 for the second for
            rs = other_rnamodel.struc[0].get_residues()
            for i, r in enumerate(rs):
                r.id = (' ', index[i] + 153, ' ')  # ugly, some random offset

            for i, r in enumerate(other_rnamodel.struc[0].get_residues()):
                r.id = (' ', index[i] + 1, ' ')
                if args.debug: print(r)

            io = Bio.PDB.PDBIO()
            sup_min.apply(other_rnamodel.struc.get_atoms())
            # if args.debug: print(p_min, [i + 1 for i in p_min])
            io.set_structure(other_rnamodel.struc)
            fout = other_rnamodel.fpath.replace('.pdb',
                                                '_aligned' + suffix + '.pdb')
            # if args.debug: print(fout)
            io.save(fout)

            # ugly set chain to A
            set_chain_for_struc(fout, 'A', save_file_inplace=True)
            # and now run this to sort into 1 2 3

            r = RNAStructure(fout)
            remarks = r.get_remarks_text()
            r1 = r.get_res_text('A', 1)
            r2 = r.get_res_text('A', 2)
            r3 = r.get_res_text('A', 3)
            with open(fout, 'w') as f:
                f.write(remarks)
                f.write(r1)
                f.write(r2)
                f.write(r3)
            r.reload()
            r.get_rnapuzzle_ready()
            r.write()
            return str(rmsd_min) + ',' + seq_min

        else:
            sup.set_atoms(self.atoms_for_rmsd, other_atoms_for_rmsd)
            rms = round(sup.rms, 2)

            if save:
                ## io = Bio.PDB.PDBIO()
                ## sup.apply(self.struc.get_atoms())
                ## io.set_structure(self.struc)
                ## io.save("aligned.pdb")

                io = Bio.PDB.PDBIO()
                sup.apply(other_rnamodel.struc.get_atoms())
                io.set_structure(other_rnamodel.struc)
                io.save(
                    other_rnamodel.fpath.replace('.pdb',
                                                 '_' + args.suffix + '.pdb'))
        return rms
Esempio n. 7
0
    def get_rmsd_to(self,
                    other_rnamodel,
                    way="",
                    triple_mode=False,
                    save=True,
                    tseq=''):
        """Calc rmsd P-atom based rmsd to other rna model

        sugar now 10 atoms ;-) """
        sup = Bio.PDB.Superimposer()
        if way in ['c1', 'backbone+sugar', 'sugar']:
            if way == 'c1':
                atomslist = ["C1'"]  # ,"C2'","O4'"] #, "C2'"]

            elif way == 'sugar':
                atomslist = "C5',O5',C4',O4',C3',O3',C2',O2',C1'".split(',')

            elif way == 'backbone+sugar':
                atomslist = "P,OP1,OP2,C5',O5',C4',O4',C3',O3',C2',O2',C1'".split(
                    ',')

            self.atoms_for_rmsd = []
            for a in self.atoms:
                nt = a.get_parent().get_resname().strip()
                if nt in ['G', 'A']:
                    atomslistx = atomslist + ['N9']
                if nt in ['C', 'U']:
                    atomslistx = atomslist + ['N1']
                if a.name in atomslistx:
                    self.atoms_for_rmsd.append(a)
            if args.debug: print('atoms_for_rmsd', len(self.atoms_for_rmsd))

            other_atoms_for_rmsd = []
            for a in other_rnamodel.atoms:
                nt = a.get_parent().get_resname().strip()
                if nt in ['G', 'A']:
                    atomslistx = atomslist + ['N9']
                if nt in ['C', 'U']:
                    atomslistx = atomslist + ['N1']
                if a.name in atomslistx:
                    other_atoms_for_rmsd.append(a)

            if args.debug:
                print('other_atoms_for_rmsd', len(other_atoms_for_rmsd))

        elif way == 'c1+Nx':
            self.atoms_for_rmsd = []
            for a in self.atoms:
                nt = a.get_parent().get_resname().strip()  # G
                if nt in ['G', 'A']:
                    atomslist = ["C1'", 'N9']  # , 'N1']
                if nt in ['C', 'U']:
                    atomslist = ["C1'", 'N1']  # , 'N1']
                if a.name in atomslist:
                    self.atoms_for_rmsd.append(a)

            if args.debug: print('atoms_for_rmsd', len(self.atoms_for_rmsd))

            other_atoms_for_rmsd = []
            for a in other_rnamodel.atoms:
                nt = a.get_parent().get_resname().strip()  # G
                if nt in ['G', 'A']:
                    atomslist = ["C1'", 'N9']  # , 'N1']
                if nt in ['C', 'U']:
                    atomslist = ["C1'", 'N1']  # , 'N1']
                if a.name in atomslist:
                    other_atoms_for_rmsd.append(a)
            if args.debug:
                print('other_atoms_for_rmsd', len(other_atoms_for_rmsd))

        else:
            self.atoms_for_rmsd = self.atoms
            other_atoms_for_rmsd = other_rnamodel.atoms

        # calc rmsd #
        if not tseq:
            tseq = ''
            rt = None
            for a in self.atoms_for_rmsd:
                r = a.get_parent()
                if r != rt:
                    rt = r
                    tseq += r.get_resname().strip()

        if triple_mode:

            def chunks(lst, n):
                """Yield successive n-sized chunks from lst.
                https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks
                """
                for i in range(0, len(lst), n):
                    yield lst[i:i + n]

            rmsd_min = 10000  # ugly
            import itertools
            # ok, for different residues now it's a problem
            per = list(
                itertools.permutations([0, 1, 2])
            )  # [(0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0)]
            lst = list(
                chunks(other_atoms_for_rmsd,
                       int(len(other_atoms_for_rmsd) /
                           3)))  # for 1 atom, this will be 1 x 3 [3 residues]
            # so so len is 3 atoms so / by 3 to get how many atoms per residue
            print(lst)

            sup_min = None
            seq_min = 'not yet obtained, rmsd rejected!'
            p_min = None

            rms = -1

            for p in per:
                patoms = []
                for i in p:  # p=(1, 2, 3)
                    patoms.extend(lst[i])

                #print(self.atoms_for_rmsd)
                ## print('patoms')
                ## for a in patoms:
                ##     print(a, a.get_parent().get_id())
                ## print('self.atoms_for_rmsd')
                ## for a in self.atoms_for_rmsd:
                ##     print(a, a.get_parent().get_id())
                #sup.set_atoms(patoms, self.atoms_for_rmsd)

                rt = None
                seq = ''
                for a in patoms:
                    r = a.get_parent()
                    if r != rt:
                        rt = r
                        seq += r.get_resname().strip()

                ic(tseq.lower(), seq.lower(), other_rnamodel.fpath)
                # dont' even calc rmsd if the curr seq and tseq are not the same
                if tseq.lower() == seq.lower():  # only if seq is the same
                    print(self.atoms_for_rmsd)
                    print(patoms)
                    print(len(self.atoms_for_rmsd), len(patoms))
                    for a, b in zip(self.atoms_for_rmsd, patoms):
                        print(a,
                              a.get_parent().id,
                              a.get_parent().get_resname(),
                              b.get_parent().id,
                              b.get_parent().get_resname())
                    print(len(self.atoms_for_rmsd), len(patoms))
                    try:
                        sup.set_atoms(self.atoms_for_rmsd, patoms)
                    except:
                        pass
                    rms = round(sup.rms, 2)
                    if rms < rmsd_min:
                        rmsd_min = rms
                        sup_min = copy.copy(sup)
                        suffix = seq
                        p_min = p
                        seq_min = seq
                        if args.debug: 'set new rmsd_min', rmsd_min

                    if args.debug:
                        print(p, '', [i + 1 for i in p], end=' ')
                        print(seq, 'seq_min: ' + seq_min, rms)

            if p_min:  # found target sequence
                # what is this? ;-)
                index = [0, 0, 0]
                index[0] = p_min.index(0)
                index[1] = p_min.index(1)
                index[2] = p_min.index(2)

                # ugly re-set 123 to crazy id ! + 100, so this will
                # fill up 1 2 3 for the second for
                rs = other_rnamodel.struc[0].get_residues()
                for i, r in enumerate(rs):
                    r.id = (' ', index[i] + 253, ' '
                            )  # ugly, some random offset

                for i, r in enumerate(other_rnamodel.struc[0].get_residues()):
                    r.id = (' ', index[i] + 1, ' ')
                    if args.debug: print('r', r)

                io = Bio.PDB.PDBIO()
                sup_min.apply(other_rnamodel.struc.get_atoms())
                # if args.debug: print(p_min, [i + 1 for i in p_min])
                io.set_structure(other_rnamodel.struc)

                args.save_here = True
                if args.save_here and save:
                    folder = os.path.basename(
                        self.fpath.replace(
                            '.pdb', '_' + args.folder_prefix + '_aligned'))
                    # print(f)
                    try:
                        os.mkdir(folder)
                    except:
                        pass
                    fout = folder + os.sep + "{:1.2f}".format(
                        rmsd_min) + '-' + os.path.basename(
                            other_rnamodel.fpath
                        )  #.replace('.pdb', '-' + str(rms) + '.pdb'))
                    #_s' + suffix + '.pdb'))
                else:
                    fout = other_rnamodel.fpath.replace(
                        '.pdb', '_aligned.pdb')  #_s' + suffix + '.pdb')

                if args.debug: print(fout)

                if save:
                    io.save(fout)
                    # ugly set chain to A
                    set_chain_for_struc(fout, 'A', save_file_inplace=True)
                    # and now run this to sort into 1 2 3

                    r = RNAStructure(fout)
                    remarks = r.get_remarks_text()
                    r1 = r.get_res_text('A', 1)
                    r2 = r.get_res_text('A', 2)
                    r3 = r.get_res_text('A', 3)
                    with open(fout, 'w') as f:
                        f.write(remarks)
                        f.write(r1)
                        f.write(r2)
                        f.write(r3)
                    r.reload()
                    r.get_rnapuzzle_ready()
                    if rmsd_min < 1:  # !!!!!!!!!!!! ugly
                        r.write()
                return str(rmsd_min
                           )  # + ',s' + seq_min + ',' + os.path.basename(fout)
        else:
            # check if number of the same atoms #
            # if not the same then return -1    #
            # print(len(self.atoms_for_rmsd), len(other_atoms_for_rmsd))
            if len(self.atoms_for_rmsd) != len(other_atoms_for_rmsd):
                return -1
            sup.set_atoms(self.atoms_for_rmsd, other_atoms_for_rmsd)
            rms = round(sup.rms, 2)

            if save:
                ## io = Bio.PDB.PDBIO()
                ## sup.apply(self.struc.get_atoms())
                ## io.set_structure(self.struc)
                ## io.save("aligned.pdb")

                io = Bio.PDB.PDBIO()
                sup.apply(other_rnamodel.struc.get_atoms())
                io.set_structure(other_rnamodel.struc)

            args.save_here = True
            if args.save_here and save:
                f = os.path.basename(self.fpath.replace('.pdb', '_aligned'))
                # print(f)
                try:
                    os.mkdir(f)
                except:
                    pass
                # fout = f + os.sep + os.path.basename(other_rnamodel.fpath.replace('.pdb', '_aligned_s' + suffix + '.pdb'))
                fout = f + os.sep + os.path.basename(
                    other_rnamodel.fpath)  #.replace('.pdb', '_aligned'))
            else:
                fout = other_rnamodel.fpath.replace('.pdb', '_aligned.pdb')
            if save:
                io.save(fout)
        return rms
Esempio n. 8
0
if __name__ == '__main__':
    # get version
    version = os.path.basename(os.path.dirname(os.path.abspath(__file__))), get_version(__file__)
    version = version[1].strip()

    # get parser and arguments
    parser = get_parser()

    args = parser.parse_args()

    # quick fix for one files vs file-s
    if list == type(args.file) and len(args.file) == 1:
        args.file = args.file[0]

    if args.report:
        s = RNAStructure(args.file)
        print(s.get_report)
        print(s.get_preview())
        print(s.get_info_chains())

    if args.clean:
        s = RNAStructure(args.file)
        s.decap_gtp()
        s.std_resn()
        s.remove_hydrogen()
        s.remove_ion()
        s.remove_water()
        s.renum_atoms()
        s.fix_O_in_UC()
        s.fix_op_atoms()
        # print s.get_preview()
Esempio n. 9
0
#!/usr/bin/env python

import sys
from rna_tools.rna_tools_lib import RNAStructure

if __name__ == '__main__':
    files = sys.argv[1:]
    if not files:
        print('rna_pdb_merge_into_one.py test_in/*.pdb')
        sys.exit(1)

    c = 1
    for f in files:
        s = RNAStructure(f)
        print("MODEL        " + str(c))
        print(s.get_text(add_end=False))
        print('ENDMDL')
        c += 1
    print('END')
Esempio n. 10
0
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)

    #parser.add_argument('-', "--", help="", default="")

    parser.add_argument("-v",
                        "--verbose",
                        action="store_true",
                        help="be verbose")
    parser.add_argument("file", help="", default="")  # nargs='+')
    parser.add_argument("insert", help="", default="")  # nargs='+')
    parser.add_argument("--output",
                        help="by default <file>_rpl.pdb",
                        default="")  # nargs='+')
    return parser


if __name__ == '__main__':
    parser = get_parser()
    args = parser.parse_args()

    f1 = args.file
    f2 = args.insert
    r1 = RNAStructure(f1)
    r2 = RNAStructure(f2)

    t = replace_atoms(f1, f2)
    if not args.output:
        with open(args.file.replace('.pdb', '_rpl.pdb'), 'w') as f:
            f.write(t)