Exemple #1
0
    def test_extract_subchain(self):
        '''
        chain = ftup.get_biggest_chain('test/forgi/threedee/data/1y26_two_chains.pdb')

        self.assertTrue(15 in chain)
        self.assertTrue(16 in chain)
        self.assertTrue(17 in chain)

        new_chain = ftup.extract_subchain(chain, 15, 17)

        self.assertTrue(15 in chain)
        self.assertTrue(16 in chain)
        self.assertTrue(17 in chain)

        self.assertTrue(15 in new_chain)
        self.assertTrue(16 in new_chain)
        self.assertTrue(17 in new_chain)
        '''

        chain = ftup.get_biggest_chain(
            'test/forgi/threedee/data/RS_308_S_7.pdb')

        new_chain = ftup.extract_subchain(chain, (' ', 35, ' '),
                                          (' ', 39, ' '))
        self.assertTrue((' ', 35, ' ') in new_chain)
        self.assertTrue((' ', 36, ' ') in new_chain)
        self.assertTrue((' ', 37, ' ') in new_chain)
        self.assertTrue((' ', 38, ' ') in new_chain)
        self.assertTrue((' ', 39, ' ') in new_chain)
Exemple #2
0
    def test_extract_subchain(self):
        '''
        chain = ftup.get_biggest_chain('test/forgi/threedee/data/1y26_two_chains.pdb')

        self.assertTrue(15 in chain)
        self.assertTrue(16 in chain)
        self.assertTrue(17 in chain)

        new_chain = ftup.extract_subchain(chain, 15, 17)

        self.assertTrue(15 in chain)
        self.assertTrue(16 in chain)
        self.assertTrue(17 in chain)

        self.assertTrue(15 in new_chain)
        self.assertTrue(16 in new_chain)
        self.assertTrue(17 in new_chain)
        '''

        chain = ftup.get_biggest_chain('test/forgi/threedee/data/RS_308_S_7.pdb')

        new_chain = ftup.extract_subchain(chain, (' ', 35, ' '), (' ', 39, ' '))
        self.assertTrue((' ', 35, ' ') in new_chain)
        self.assertTrue((' ', 36, ' ') in new_chain)
        self.assertTrue((' ', 37, ' ') in new_chain)
        self.assertTrue((' ', 38, ' ') in new_chain)
        self.assertTrue((' ', 39, ' ') in new_chain)
Exemple #3
0
    def test_trim_chain_between(self):
        chain = ftup.get_biggest_chain('test/forgi/threedee/data/1y26_two_chains.pdb')

        self.assertTrue(15 in chain)
        self.assertTrue(16 in chain)
        self.assertTrue(17 in chain)

        ftup.trim_chain_between(chain, 15, 17)

        self.assertFalse(15 in chain)
        self.assertFalse(16 in chain)
        self.assertFalse(17 in chain)
Exemple #4
0
    def test_trim_chain_between(self):
        chain = ftup.get_biggest_chain(
            'test/forgi/threedee/data/1y26_two_chains.pdb')

        self.assertTrue(15 in chain)
        self.assertTrue(16 in chain)
        self.assertTrue(17 in chain)

        ftup.trim_chain_between(chain, 15, 17)

        self.assertFalse(15 in chain)
        self.assertFalse(16 in chain)
        self.assertFalse(17 in chain)
def main():
    usage = """
    python get_biggest_rna_chain.py in.pdb out.pdb

    Extract the largest RNA chain from the file in.pdb and store it in out.pdb.
    """
    num_args = 2
    parser = OptionParser(usage=usage)

    #parser.add_option('-o', '--options', dest='some_option', default='yo', help="Place holder for a real option", type='str')
    #parser.add_option('-u', '--useless', dest='uselesss', default=False, action='store_true', help='Another useless option')

    (options, args) = parser.parse_args()

    if len(args) < num_args:
        parser.print_help()
        sys.exit(1)

    c = ftup.get_biggest_chain(args[0])
    ftup.output_chain(c, args[1])
def main():
    usage = """
    python get_biggest_rna_chain.py in.pdb out.pdb

    Extract the largest RNA chain from the file in.pdb and store it in out.pdb.
    """
    num_args= 2
    parser = OptionParser(usage=usage)

    #parser.add_option('-o', '--options', dest='some_option', default='yo', help="Place holder for a real option", type='str')
    #parser.add_option('-u', '--useless', dest='uselesss', default=False, action='store_true', help='Another useless option')

    (options, args) = parser.parse_args()

    if len(args) < num_args:
        parser.print_help()
        sys.exit(1)

    c = ftup.get_biggest_chain(args[0])
    ftup.output_chain(c, args[1])
Exemple #7
0
def reconstruct_with_fragment(chain, sm, ld, fragment_library=dict(), move_all_angles=False, close_loop=True):
    '''
    Reconstruct a loop with the fragment its statistics were derived from.

    @param chain: The chain containing the reconstructed stems.
    @param sm: The SpatialModel containing the information about the sampled
        stems and angles
    @param ld: The name of the loop to reconstruct.
    '''

    #find some potential sides
    #both ways should work
    #i.e. if [0][1] is present, [0][1] should also be present
    reverse = False

    if ld in sm.elem_defs:
        angle_def = sm.angle_defs[ld]
    if ld[0]=="f":
        close_loop = False
        reverse=True
    elif ld[0]=="t":
        close_loop = False

    cg_filename = op.expanduser(op.join("/home/mescalin/pkerp/doarse/", angle_def.pdb_name, "temp.cg"))
    pdb_filename = op.expanduser(op.join("/home/mescalin/pkerp/doarse/", angle_def.pdb_name, "temp.pdb"))

    cg_to = sm.bg
    cg_from = ftmc.CoarseGrainRNA(cg_filename) 

    chain_to = chain
    chain_from = ftup.get_biggest_chain(pdb_filename)

    elem_to = ld
    elem_from = cg_from.get_node_from_residue_num(angle_def.define[0])

    new_chains = reconstruct_element(cg_to, cg_from, elem_to, elem_from, chain_to, chain_from, 
                                     close_loop, reverse)

    return chain
Exemple #8
0
def main():
    usage = """
    ./visualize_pdb.py pdb_file

    Display a pdb file along with its coarse grain representation in pymol.
    """
    num_args = 0
    parser = OptionParser(usage=usage)

    parser.add_option('-s',
                      '--secondary-structure',
                      dest='secondary_structure',
                      default='',
                      help="Enter a dot-bracket string for the \
                      secondary structure of this model",
                      type=str)
    parser.add_option('-x',
                      '--text',
                      dest='text',
                      default=False,
                      action='store_true',
                      help="Add labels to the figure.")
    parser.add_option('-r',
                      '--longrange',
                      dest='longrange',
                      default=False,
                      action='store_true',
                      help="Display long-range interactions")
    parser.add_option('-p',
                      '--pseudoknots',
                      dest='pseudoknots',
                      default=False,
                      action='store_true',
                      help='Allow pseudoknots in the CG structure')
    #parser.add_option('', '--batch', dest='batch', default=False, action='store_true', help='Start pymol in batch mode') #Crashes currently
    parser.add_option('',
                      '--virtual-residues',
                      dest='virtual_residues',
                      default=False,
                      action='store_true',
                      help='Display the virtual residues as spheres')
    parser.add_option(
        '',
        '--color-residues',
        dest='color_residues',
        default=False,
        action='store_true',
        help="Color the residues according to the element they're in")

    #parser.add_option('-u', '--useless', dest='uselesss', default=False, action='store_true', help='Another useless option')

    parser.add_option('-l',
                      '--loops',
                      dest='loops',
                      default=True,
                      action='store_false',
                      help="Don't display the coarse-grain hairpin loops")
    parser.add_option('-d',
                      '--distance',
                      dest='distance',
                      default=None,
                      help="Draw the lines between specified virtual residues")
    parser.add_option('-o',
                      '--output',
                      dest='output',
                      default=None,
                      help="Create a picture of the scene and exit",
                      type='str')
    parser.add_option('',
                      '--only-elements',
                      dest='only_elements',
                      default=None,
                      help='Display only these elements '
                      'element names should be '
                      'separated by commas')
    parser.add_option('-v',
                      '--virtual-atoms',
                      dest='virtual_atoms',
                      default=False,
                      action='store_true',
                      help='Display the virtual atoms')

    (options, args) = parser.parse_args()

    if len(args) < 1:
        parser.print_help()
        sys.exit(1)

    if not op.exists(args[0]):
        print >> sys.stderr, "File doesn't exist: %s" % (args[0])
        sys.exit(1)

    cg = ftmc.from_pdb(args[0],
                       options.secondary_structure.strip("\"'"),
                       remove_pseudoknots=not options.pseudoknots)
    pp = ftvp.PymolPrinter()
    pp.display_virtual_residues = options.virtual_residues
    pp.virtual_atoms = options.virtual_atoms

    if options.only_elements is not None:
        orig_only_elements = options.only_elements.split(',')
        only_elements = set(orig_only_elements[::])
        for c in orig_only_elements:
            for e in cg.edges[c]:
                only_elements.add(e)
        pp.only_elements = only_elements

    pp.add_loops = options.loops
    pp.add_longrange = options.longrange
    #sys.exit(1)
    pp.coordinates_to_pymol(cg)
    pp.print_text = options.text
    print >> sys.stderr, "virtual_residues:", options.virtual_residues
    #pp.print_text = False
    #pp.output_pymol_file()

    if options.only_elements is not None:
        pp.only_elements = options.only_elements.split(',')

    with tf.NamedTemporaryFile() as f:
        with tf.NamedTemporaryFile(suffix='.pml') as f1:
            with tf.NamedTemporaryFile(suffix='.pdb') as f2:
                # extract just the biggest chain and renumber it so
                # the nucleotides start at 1
                chain = ftup.get_biggest_chain(args[0])
                #chain = ftup.renumber_chain(chain)
                ftup.output_chain(chain, f2.name)
                f2.flush()

                # display the distances between nucleotides
                if options.distance is not None:
                    for dist_pair in options.distance.split(':'):
                        fr, to = dist_pair.split(',')

                        fr = int(fr)
                        to = int(to)

                        try:
                            vec1 = chain[fr]["C1*"].get_vector().get_array()
                            vec2 = chain[to]["C1*"].get_vector().get_array()
                        except KeyError:
                            # Rosetta produces atoms with non-standard names
                            vec1 = chain[fr]["C1*"].get_vector().get_array()
                            vec2 = chain[to]["C1*"].get_vector().get_array()

                        pp.add_dashed(vec1, vec2, width=1.2)

                f.write(pp.pymol_string())
                f.flush()

                pymol_cmd = 'hide all\n'
                pymol_cmd += 'show cartoon, all\n'
                pymol_cmd += 'set cartoon_ring_mode\n'
                pymol_cmd += 'set cartoon_tube_radius, .3\n'

                if options.only_elements is not None:
                    pymol_cmd += "hide all\n"

                    for constraint in only_elements:
                        color = pp.get_element_color(constraint)

                        for r in cg.define_residue_num_iterator(constraint,
                                                                seq_ids=True):
                            pymol_cmd += "show sticks, resi %r\n" % (r[1])
                            pymol_cmd += "color %s, resi %r\n" % (color, r[1])

                if options.color_residues:
                    for d in cg.defines:
                        color = pp.get_element_color(d)

                        for r in cg.define_residue_num_iterator(d,
                                                                seq_ids=True):
                            pymol_cmd += "color %s, resi %r\n" % (color, r[1])

                pymol_cmd += 'run %s\n' % (f.name)
                pymol_cmd += 'bg white\n'
                pymol_cmd += 'clip slab, 10000\n'
                pymol_cmd += 'orient\n'

                if options.output is not None:
                    pymol_cmd += 'ray\n'
                    pymol_cmd += 'png %s\n' % (options.output)
                    #pymol_cmd += 'quit\n' #This would lead to an error message

                f1.write(pymol_cmd)
                f1.flush()

                #if options.batch:
                #    p = sp.Popen(['pymol', '-cq', f2.name, f1.name], stdout=sp.PIPE, stderr=sp.PIPE)
                if True:  #else:
                    p = sp.Popen(['pymol', f2.name, f1.name],
                                 stdout=sp.PIPE,
                                 stderr=sp.PIPE)

                out, err = p.communicate()
Exemple #9
0
    def test_is_covalent(self):
        c = ftup.get_biggest_chain('test/forgi/threedee/data/2mis.pdb')

        self.assertTrue(ftup.is_covalent([c[10]["C3'"], c[10]["C4'"]]))
        self.assertFalse(ftup.is_covalent([c[10]["C3'"], c[10]["C5'"]]))
Exemple #10
0
 def test_get_biggest_chain(self):
     chain = ftup.get_biggest_chain(
         'test/forgi/threedee/data/1y26_two_chains.pdb')
     self.assertEqual(chain.id, 'X')
Exemple #11
0
    def test_is_covalent(self):
        c = ftup.get_biggest_chain('test/forgi/threedee/data/2mis.pdb')

        self.assertTrue(ftup.is_covalent([c[10]["C3'"], c[10]["C4'"]]))
        self.assertFalse(ftup.is_covalent([c[10]["C3'"], c[10]["C5'"]]))
Exemple #12
0
 def test_get_biggest_chain(self):
     chain = ftup.get_biggest_chain('test/forgi/threedee/data/1y26_two_chains.pdb')
     self.assertEqual(chain.id, 'X')
Exemple #13
0
def load_cg_from_pdb_in_dir(pdb_filename, output_dir, secondary_structure='', 
                            chain_id=None, remove_pseudoknots=True):
    '''
    Create the coarse grain model from a pdb file and store all
    of the intermediate files in the given directory.

    @param pdb_filename: The name of the pdb file to be coarseified
    @param output_dir: The name of the output directory
    @param secondary_structure: Specify a particular secondary structure
                                for this coarsification.
    @param chain_id: The id of the chain to create the CG model from
    '''
    #chain = ftup.load_structure(pdb_filename)
    if chain_id == None:
        chain = ftup.get_biggest_chain(pdb_filename)
    else:
        chain = ftup.get_particular_chain(pdb_filename, chain_id)

    chain = ftup.rename_modified_ress(chain)
    chain = ftup.rename_rosetta_atoms(chain)
    chain = ftup.remove_hetatm(chain)

    # output the biggest RNA chain
    pdb_base = op.splitext(op.basename(pdb_filename))[0]
    output_dir = op.join(output_dir, pdb_base + "_" + chain.id)

    if not op.exists(output_dir):
        os.makedirs(output_dir)

    with open(op.join(output_dir, 'temp.pdb'), 'w') as f:
        ftup.output_chain(chain, f.name)
        f.flush()

        pdb_base = op.splitext(op.basename(pdb_filename))[0]
        pdb_base += "_" + chain.id

        cg = CoarseGrainRNA()
        cg.name = pdb_base

        if len(chain.get_list()) == 0:
            return cg

        # first we annotate the 3D structure
        p = sp.Popen(['MC-Annotate', f.name], stdout=sp.PIPE)
        out, err = p.communicate()

        with open(op.join(output_dir, 'temp.mcannotate'), 'w') as f3:
            f3.write(out)

        lines = out.strip().split('\n')
        # convert the mcannotate output into bpseq format
        try:
            (dotplot, residue_map) = ftum.get_dotplot(lines)
        except Exception as e:
            print >>sys.stderr, e
            return cg

        # f2 will store the dotbracket notation
        with open(op.join(output_dir, 'temp.bpseq'), 'w') as f2:
            f2.write(dotplot)
            f2.flush()

            # remove pseudoknots
            '''
            p = sp.Popen(['aux/k2n_standalone/knotted2nested.py', '-f', 'bpseq', 
                          '-F', 'vienna', f2.name], stdout = sp.PIPE)

            out, err = p.communicate()
            '''
            if remove_pseudoknots:
                out = cak.k2n_main(f2.name, input_format='bpseq',
                                   #output_format = 'vienna',
                                   output_format = 'bpseq',
                                   method = cak.DEFAULT_METHOD,
                                   opt_method = cak.DEFAULT_OPT_METHOD,
                                   verbose = cak.DEFAULT_VERBOSE,
                                   removed= cak.DEFAULT_REMOVED)

                out = out.replace(' Nested structure', pdb_base)
            else:
                out = dotplot
            #(out, residue_map) = add_missing_nucleotides(out, residue_map)

            '''
            if secondary_structure != '':
                lines = out.split('\n')

                if len(secondary_structure) != len(lines[1].strip()):
                    print >>sys.stderr, "The provided secondary structure \
                            does not match the length of the 3D structure"
                    print >>sys.stderr, "Sequence:", lines[1]
                    print >>sys.stderr, "ss_struc:", secondary_structure
                    sys.exit(1)

                lines[-1] = secondary_structure
                out = "\n".join(lines)
            '''

            # Add the 3D information about the starts and ends of the stems
            # and loops
            with warnings.catch_warnings():
                warnings.simplefilter("ignore")
                s = bpdb.PDBParser().get_structure('temp', f.name)
                chains = list(s.get_chains())
                if len(chains) < 1:
                    raise Exception("No chains in the PDB file")

                chain = chains[0]

            #cg.from_fasta(out, dissolve_length_one_stems=1)
            cg.from_bpseq_str(out, dissolve_length_one_stems=False)
            cg.name = pdb_base
            cg.seqids_from_residue_map(residue_map)
            ftug.add_stem_information_from_pdb_chain(cg, chain)
            ftug.add_bulge_information_from_pdb_chain(cg, chain)
            ftug.add_loop_information_from_pdb_chain(cg, chain)

            cg.chain = chain

            add_longrange_interactions(cg, lines)

            with open(op.join(output_dir, 'temp.cg'), 'w') as f3:
                f3.write(cg.to_cg_string())
                f3.flush()

            return cg
    print >>sys.stderr, "Uh oh... couldn't generate the coarse-grain structure."
    print >>sys.stderr, "Prepare for an incoming exception."
Exemple #14
0
def main():
    usage = """
    ./visualize_pdb.py pdb_file

    Display a pdb file along with its coarse grain representation in pymol.
    """
    num_args = 0
    parser = OptionParser(usage=usage)

    parser.add_option('-s',
                      '--secondary-structure',
                      dest='secondary_structure',
                      default='',
                      help="Enter a dot-bracket string for the \
                      secondary structure of this model",
                      type=str)
    parser.add_option('-x',
                      '--text',
                      dest='text',
                      default=False,
                      action='store_true',
                      help="Add labels to the figure.")
    parser.add_option('-r',
                      '--longrange',
                      dest='longrange',
                      default=False,
                      action='store_true',
                      help="Display long-range interactions")
    parser.add_option('-c',
                      '--constraints',
                      dest='constraints',
                      default=None,
                      help="Only visualize the elements passed as parameters",
                      type='str')

    #parser.add_option('-u', '--useless', dest='uselesss', default=False, action='store_true', help='Another useless option')

    parser.add_option('-l',
                      '--loops',
                      dest='loops',
                      default=True,
                      action='store_false',
                      help="Don't display the coarse-grain hairpin loops")

    (options, args) = parser.parse_args()

    if len(args) < 1:
        parser.print_help()
        sys.exit(1)

    if not op.exists(args[0]):
        print >> sys.stderr, "File doesn't exist: %s" % (args[0])
        sys.exit(1)

    cg = ftmc.from_pdb(args[0], options.secondary_structure.strip("\"'"))
    pp = ftvp.PymolPrinter()

    if options.constraints is not None:
        orig_constraints = options.constraints.split(',')
        constraints = set(orig_constraints[::])
        for c in orig_constraints:
            for e in cg.edges[c]:
                constraints.add(e)
        pp.constraints = constraints

    pp.add_loops = options.loops
    pp.add_longrange = options.longrange
    #sys.exit(1)
    pp.coordinates_to_pymol(cg)
    pp.print_text = options.text
    #pp.print_text = False
    #pp.output_pymol_file()

    with tf.NamedTemporaryFile() as f:
        with tf.NamedTemporaryFile(suffix='.pml') as f1:
            with tf.NamedTemporaryFile(suffix='.pdb') as f2:
                # extract just the biggest chain and renumber it so
                # the nucleotides start at 1
                chain = ftup.get_biggest_chain(args[0])
                #chain = ftup.renumber_chain(chain)
                ftup.output_chain(chain, f2.name)
                f2.flush()

                f.write(pp.pymol_string())
                f.flush()

                pymol_cmd = 'hide all\n'
                pymol_cmd += 'show cartoon, all\n'
                pymol_cmd += 'set cartoon_ring_mode\n'
                pymol_cmd += 'set cartoon_tube_radius, .3\n'

                if options.constraints is not None:
                    pymol_cmd += "hide all\n"

                    for constraint in constraints:
                        color = pp.get_element_color(constraint)

                        for r in cg.define_residue_num_iterator(constraint):
                            pymol_cmd += "show sticks, resi %r\n" % (r)
                            pymol_cmd += "color %s, resi %r\n" % (color, r)

                pymol_cmd += 'run %s\n' % (f.name)

                f1.write(pymol_cmd)
                f1.flush()

                p = sp.Popen(['pymol', f2.name, f1.name])
                out, err = p.communicate()
Exemple #15
0
def main():
    usage = """
    ./visualize_pdb.py pdb_file

    Display a pdb file along with its coarse grain representation in pymol.
    """
    num_args= 0
    parser = OptionParser(usage=usage)

    parser.add_option('-s', '--secondary-structure', 
                      dest='secondary_structure', default='', 
                      help="Enter a dot-bracket string for the \
                      secondary structure of this model", type=str)
    parser.add_option('-x', '--text', dest='text', default=False, action='store_true', help="Add labels to the figure.")
    parser.add_option('-r', '--longrange', dest='longrange', default=False, action='store_true', help="Display long-range interactions")
    parser.add_option('-p', '--pseudoknots', dest='pseudoknots', default=False, action='store_true', help='Allow pseudoknots in the CG structure')
    #parser.add_option('', '--batch', dest='batch', default=False, action='store_true', help='Start pymol in batch mode') #Crashes currently
    parser.add_option('', '--virtual-residues', dest='virtual_residues', default=False, action='store_true', help='Display the virtual residues as spheres')
    parser.add_option('', '--color-residues', dest='color_residues', default=False, action='store_true', help="Color the residues according to the element they're in")

    #parser.add_option('-u', '--useless', dest='uselesss', default=False, action='store_true', help='Another useless option')

    parser.add_option('-l', '--loops', dest='loops', default=True, action='store_false', help="Don't display the coarse-grain hairpin loops")
    parser.add_option('-d', '--distance', dest='distance', default=None, help="Draw the lines between specified virtual residues")
    parser.add_option('-o', '--output', dest='output', default=None, help="Create a picture of the scene and exit", type='str')
    parser.add_option('', '--only-elements', dest='only_elements', default=None, help='Display only these elements '
                                                                                      'element names should be '
                                                                                      'separated by commas')
    parser.add_option('-v', '--virtual-atoms', dest='virtual_atoms', default=False, action='store_true',
                      help='Display the virtual atoms')

    (options, args) = parser.parse_args()

    if len(args) < 1:
        parser.print_help()
        sys.exit(1)

    if not op.exists(args[0]):
        print >>sys.stderr, "File doesn't exist: %s" % (args[0])
        sys.exit(1)


    cg = ftmc.from_pdb(args[0], options.secondary_structure.strip("\"'"),
                      remove_pseudoknots=not options.pseudoknots)
    pp = ftvp.PymolPrinter()
    pp.display_virtual_residues = options.virtual_residues
    pp.virtual_atoms = options.virtual_atoms

    if options.only_elements is not None:
        orig_only_elements = options.only_elements.split(',')
        only_elements = set(orig_only_elements[::])
        for c in orig_only_elements:
            for e in cg.edges[c]:
                only_elements.add(e)
        pp.only_elements = only_elements

    pp.add_loops = options.loops
    pp.add_longrange = options.longrange
    #sys.exit(1)
    pp.coordinates_to_pymol(cg)
    pp.print_text = options.text
    print >>sys.stderr, "virtual_residues:", options.virtual_residues
    #pp.print_text = False
    #pp.output_pymol_file()

    if options.only_elements is not None:
        pp.only_elements = options.only_elements.split(',')

    with tf.NamedTemporaryFile() as f:
        with tf.NamedTemporaryFile(suffix='.pml') as f1:
            with tf.NamedTemporaryFile(suffix='.pdb') as f2:
                # extract just the biggest chain and renumber it so
                # the nucleotides start at 1
                chain = ftup.get_biggest_chain(args[0])
                #chain = ftup.renumber_chain(chain)
                ftup.output_chain(chain, f2.name)
                f2.flush()

                # display the distances between nucleotides
                if options.distance is not None:
                    for dist_pair in options.distance.split(':'):
                        fr,to = dist_pair.split(',')

                        fr = int(fr)
                        to = int(to)

                        try:
                            vec1 = chain[fr]["C1*"].get_vector().get_array() 
                            vec2 = chain[to]["C1*"].get_vector().get_array()
                        except KeyError:
                            # Rosetta produces atoms with non-standard names
                            vec1 = chain[fr]["C1*"].get_vector().get_array() 
                            vec2 = chain[to]["C1*"].get_vector().get_array()

                        pp.add_dashed(vec1, vec2, width=1.2)

                f.write(pp.pymol_string())
                f.flush()

                pymol_cmd = 'hide all\n'
                pymol_cmd += 'show cartoon, all\n'
                pymol_cmd += 'set cartoon_ring_mode\n'
                pymol_cmd += 'set cartoon_tube_radius, .3\n'

                if options.only_elements is not None:
                    pymol_cmd += "hide all\n"

                    for constraint in only_elements:
                        color = pp.get_element_color(constraint)

                        for r in cg.define_residue_num_iterator(constraint, seq_ids=True):
                            pymol_cmd += "show sticks, resi %r\n" % (r[1])
                            pymol_cmd += "color %s, resi %r\n" % (color, r[1])

                if options.color_residues:
                    for d in cg.defines:
                        color = pp.get_element_color(d)

                        for r in cg.define_residue_num_iterator(d, seq_ids=True):
                            pymol_cmd += "color %s, resi %r\n" % (color, r[1])


                pymol_cmd += 'run %s\n' % (f.name)
                pymol_cmd += 'bg white\n'
                pymol_cmd += 'clip slab, 10000\n'
                pymol_cmd += 'orient\n'

                if options.output is not None:
                    pymol_cmd += 'ray\n'
                    pymol_cmd += 'png %s\n' % (options.output)
                    #pymol_cmd += 'quit\n' #This would lead to an error message

                f1.write(pymol_cmd)
                f1.flush()

                #if options.batch:
                #    p = sp.Popen(['pymol', '-cq', f2.name, f1.name], stdout=sp.PIPE, stderr=sp.PIPE)
                if True: #else:
                    p = sp.Popen(['pymol', f2.name, f1.name], stdout=sp.PIPE, stderr=sp.PIPE)

                out, err = p.communicate()
Exemple #16
0
def main():
    usage = """
    ./visualize_pdb.py pdb_file

    Display a pdb file along with its coarse grain representation in pymol.
    """
    num_args= 0
    parser = OptionParser(usage=usage)

    parser.add_option('-s', '--secondary-structure', 
                      dest='secondary_structure', default='', 
                      help="Enter a dot-bracket string for the \
                      secondary structure of this model", type=str)
    parser.add_option('-x', '--text', dest='text', default=False, action='store_true', help="Add labels to the figure.")
    parser.add_option('-r', '--longrange', dest='longrange', default=False, action='store_true', help="Display long-range interactions")
    parser.add_option('-c', '--constraints', dest='constraints', default=None, help="Only visualize the elements passed as parameters", type='str')

    #parser.add_option('-u', '--useless', dest='uselesss', default=False, action='store_true', help='Another useless option')

    parser.add_option('-l', '--loops', dest='loops', default=True, action='store_false', help="Don't display the coarse-grain hairpin loops")

    (options, args) = parser.parse_args()

    if len(args) < 1:
        parser.print_help()
        sys.exit(1)

    if not op.exists(args[0]):
        print >>sys.stderr, "File doesn't exist: %s" % (args[0])
        sys.exit(1)

    cg = ftmc.from_pdb(args[0], options.secondary_structure.strip("\"'"))
    pp = ftvp.PymolPrinter()

    if options.constraints is not None:
        orig_constraints = options.constraints.split(',')
        constraints = set(orig_constraints[::])
        for c in orig_constraints:
            for e in cg.edges[c]:
                constraints.add(e)
        pp.constraints = constraints

    pp.add_loops = options.loops
    pp.add_longrange = options.longrange
    #sys.exit(1)
    pp.coordinates_to_pymol(cg)
    pp.print_text = options.text
    #pp.print_text = False
    #pp.output_pymol_file()

    with tf.NamedTemporaryFile() as f:
        with tf.NamedTemporaryFile(suffix='.pml') as f1:
            with tf.NamedTemporaryFile(suffix='.pdb') as f2:
                # extract just the biggest chain and renumber it so
                # the nucleotides start at 1
                chain = ftup.get_biggest_chain(args[0])
                #chain = ftup.renumber_chain(chain)
                ftup.output_chain(chain, f2.name)
                f2.flush()

                f.write(pp.pymol_string())
                f.flush()

                pymol_cmd = 'hide all\n'
                pymol_cmd += 'show cartoon, all\n'
                pymol_cmd += 'set cartoon_ring_mode\n'
                pymol_cmd += 'set cartoon_tube_radius, .3\n'

                if options.constraints is not None:
                    pymol_cmd += "hide all\n"

                    for constraint in constraints:
                        color = pp.get_element_color(constraint)

                        for r in cg.define_residue_num_iterator(constraint):
                            pymol_cmd += "show sticks, resi %r\n" % (r)
                            pymol_cmd += "color %s, resi %r\n" % (color, r)

                pymol_cmd += 'run %s\n' % (f.name)

                f1.write(pymol_cmd)
                f1.flush()

                p = sp.Popen(['pymol', f2.name, f1.name])
                out, err = p.communicate()