Ejemplo n.º 1
0
 def test_gle( self ):
     from OpenGL.GLE import (
         gleSetJoinStyle,
         TUBE_NORM_EDGE, TUBE_JN_ANGLE, TUBE_JN_CAP,
         glePolyCone,
     )
     if (gleSetJoinStyle):
         gleSetJoinStyle(TUBE_NORM_EDGE | TUBE_JN_ANGLE | TUBE_JN_CAP)
         glePolyCone(((-6.0, 6.0, 0.0), (6.0, 6.0, 0.0), (6.0, -6.0, 0.0), (-6.0, -6.0, 0.0), (-6.0, 6.0, 0.0), (6.0, 6.0, 0.0)),
                             ((0.0, 0.0, 0.0), (0.0, 0.8, 0.3), (0.8, 0.3, 0.0), (0.2, 0.3, 0.9), (0.2, 0.8, 0.5), (0.0, 0.0, 0.0)), (1, 1, 3, 0.5, 2, 1))
     else:
         logging.warn("No GLE extrusion library")
Ejemplo n.º 2
0
    def drawchunk(self, glpane, chunk, memo, highlighted):
        """
        Draws reduced representation of a protein chunk.
        """

        structure, total_length, ca_list, n_sec = memo

        style = self.proteinStyle
        scaleFactor = self.proteinStyleScaleFactor
        resolution = self.proteinStyleQuality
        scaling = self.proteinStyleScaling
        smooth = self.proteinStyleSmooth

        gleSetJoinStyle(TUBE_JN_ANGLE | TUBE_NORM_PATH_EDGE | TUBE_JN_CAP | TUBE_CONTOUR_CLOSED ) 

        current_sec = 0
        for sec, secondary in structure:
            # Number of atoms in SS element including dummy atoms.
            n_atoms = len(sec) 
            # The length should be at least 3.
            if n_atoms >= 3:
                # Alpha carbon trace styles. Simple but fast.
                if style == PROTEIN_STYLE_CA_WIRE or \
                   style == PROTEIN_STYLE_CA_CYLINDER or \
                   style == PROTEIN_STYLE_CA_BALL_STICK:
                    for n in range( 1, n_atoms-2 ):
                        pos0, ss0, aa0, idx0, dpos0, cbpos0 = sec[n - 1]
                        pos1, ss1, aa1, idx1, dpos1, cbpos1 = sec[n]
                        pos2, ss2, aa2, idx2, dpos2, cbpos2 = sec[n + 1]
                        color = self._get_aa_color(chunk, 
                                                   idx1, 
                                                   total_length, 
                                                   ss1, 
                                                   aa1,
                                                   current_sec,
                                                   n_sec)
                        if style == PROTEIN_STYLE_CA_WIRE:
                            if pos0:
                                drawline(color, 
                                         pos1 + 0.5 * (pos0 - pos1), 
                                         pos1,
                                         width=5,
                                         isSmooth=True)
                            if pos2:
                                drawline(color, 
                                         pos1, 
                                         pos1 + 0.5 * (pos2 - pos1),
                                         width=5, 
                                         isSmooth=True)
                        else:
                            if pos0:
                                drawcylinder(color, 
                                             pos1 + 0.5 * (pos0 - pos1), 
                                             pos1,
                                             0.25 * scaleFactor, 
                                             capped=1)

                            if style == PROTEIN_STYLE_CA_BALL_STICK:
                                drawsphere(color, pos1, 0.5 * scaleFactor, 2)
                            else:
                                drawsphere(color, pos1, 0.25 * scaleFactor, 2)

                            if pos2:
                                drawcylinder(color, 
                                             pos1, 
                                             pos1 + 0.5 * (pos2 - pos1),
                                             0.25 * scaleFactor, 
                                             capped=1)

                elif style == PROTEIN_STYLE_PEPTIDE_TILES:
                    for n in range( 1, n_atoms-2 ):
                        pos0, ss0, aa0, idx0, dpos0, cbpos0 = sec[n - 1]
                        pos1, ss1, aa1, idx1, dpos1, cbpos1 = sec[n]
                        color = self._get_aa_color(chunk, 
                                                   idx1, 
                                                   total_length, 
                                                   ss1, 
                                                   aa1,
                                                   current_sec,
                                                   n_sec)
                        tri = []
                        nor = []
                        col = []


                elif style == PROTEIN_STYLE_TUBE or \
                     style == PROTEIN_STYLE_LADDER or \
                     style == PROTEIN_STYLE_ZIGZAG or \
                     style == PROTEIN_STYLE_FLAT_RIBBON or \
                     style == PROTEIN_STYLE_SOLID_RIBBON or \
                     style == PROTEIN_STYLE_SIMPLE_CARTOONS or \
                     style == PROTEIN_STYLE_FANCY_CARTOONS:

                    tube_pos = []
                    tube_col = []
                    tube_rad = []
                    tube_dpos = []

                    for n in range( 2, n_atoms-2 ):
                        pos00, ss00, a00, idx00, dpos00, cbpos00 = sec[n - 2]
                        pos0, ss0, aa0, idx0, dpos0, cbpos0 = sec[n - 1]
                        pos1, ss1, aa1, idx1, dpos1, cbpos1 = sec[n]
                        pos2, ss2, aa2, idx2, dpos2, cbpos2 = sec[n + 1]
                        pos22, ss22, aa22, idx22, dpos22, cbpos22 = sec[n + 2]

                        color = self._get_aa_color(chunk, 
                                                   idx1, 
                                                   total_length, 
                                                   ss1, 
                                                   aa1,
                                                   current_sec,
                                                   n_sec)

                        rad = 0.25 * scaleFactor
                        if style == PROTEIN_STYLE_TUBE and \
                           scaling == 1:
                            if secondary > 0: 
                                rad *= 2.0

                        if n == 2:
                            if pos0:
                                tube_pos.append(pos00)
                                tube_col.append(V(color))
                                tube_rad.append(rad)
                                tube_dpos.append(dpos1)
                                tube_pos.append(pos0)
                                tube_col.append(V(color))
                                tube_rad.append(rad)
                                tube_dpos.append(dpos1)


                        if style == PROTEIN_STYLE_LADDER:
                            drawcylinder(color, pos1, cbpos1, rad * 0.75)
                            drawsphere(color, cbpos1, rad * 1.5, 2)

                        if pos1:
                            tube_pos.append(pos1)
                            tube_col.append(V(color))
                            tube_rad.append(rad)
                            tube_dpos.append(dpos1)

                        if n == n_atoms - 3:
                            if pos2:
                                tube_pos.append(pos2)
                                tube_col.append(V(color))
                                tube_rad.append(rad)
                                tube_dpos.append(dpos1)
                                tube_pos.append(pos22)
                                tube_col.append(V(color))
                                tube_rad.append(rad)
                                tube_dpos.append(dpos1)

                    # For smoothed helices we need to add virtual atoms
                    # located approximately at the centers of peptide bonds
                    # but slightly moved away from the helix axis.

                    new_tube_pos = []
                    new_tube_col = []
                    new_tube_rad = []
                    new_tube_dpos = []
                    if smooth and \
                       secondary == 1:
                        for p in range(len(tube_pos)):
                            new_tube_pos.append(tube_pos[p])
                            new_tube_col.append(tube_col[p])
                            new_tube_rad.append(tube_rad[p])                        
                            new_tube_dpos.append(tube_dpos[p])

                            if p > 1 and p < len(tube_pos) - 3:
                                pv = tube_pos[p-1] - tube_pos[p]
                                nv = tube_pos[p+2] - tube_pos[p+1]
                                mi = 0.5 * (tube_pos[p+1] + tube_pos[p])
                                # The coefficient below was handpicked to make  
                                # the helices approximately round.
                                mi -= 0.75 * norm(nv+pv)                            
                                new_tube_pos.append(mi)
                                new_tube_col.append(0.5*(tube_col[p]+tube_col[p+1]))
                                new_tube_rad.append(0.5*(tube_rad[p]+tube_rad[p+1]))                        
                                new_tube_dpos.append(0.5*(tube_dpos[p]+tube_dpos[p+1]))

                        tube_pos = new_tube_pos
                        tube_col = new_tube_col
                        tube_rad = new_tube_rad
                        tube_dpos = new_tube_dpos

                    if secondary != 1 or \
                       style != PROTEIN_STYLE_SIMPLE_CARTOONS:
                        tube_pos, tube_col, tube_rad, tube_dpos = make_tube(
                            tube_pos, 
                            tube_col, 
                            tube_rad, 
                            tube_dpos, 
                            resolution=resolution)

                        if style == PROTEIN_STYLE_ZIGZAG or \
                           style == PROTEIN_STYLE_FLAT_RIBBON or \
                           style == PROTEIN_STYLE_SOLID_RIBBON or \
                           style == PROTEIN_STYLE_SIMPLE_CARTOONS or \
                           style == PROTEIN_STYLE_FANCY_CARTOONS:

                            last_pos = None
                            last_width = 1.0
                            reset = False

                            # Find SS element widths and determine width increment.
                            if secondary == 0:
                                # Coils have a constant width.
                                width = scaleFactor * 0.1
                                dw = 0.0
                            elif secondary == 1:
                                # Helices expand and shrink at the ends.
                                width = scaleFactor * 0.1
                                dw = (1.0 * scaleFactor) / (resolution - 3)
                            else:
                                # Strands just shrink at the C-terminal end.
                                width = scaleFactor * 1.0
                                dw = (1.6 * scaleFactor) / (1.5 * resolution - 3)

                            if style == PROTEIN_STYLE_FLAT_RIBBON or \
                               style == PROTEIN_STYLE_SOLID_RIBBON or \
                               style == PROTEIN_STYLE_SIMPLE_CARTOONS or \
                               style == PROTEIN_STYLE_FANCY_CARTOONS:

                                tri_arr0 = []
                                nor_arr0 = []
                                col_arr0 = []

                                if style == PROTEIN_STYLE_SOLID_RIBBON or \
                                   style == PROTEIN_STYLE_SIMPLE_CARTOONS or \
                                   style == PROTEIN_STYLE_FANCY_CARTOONS: 

                                    tri_arr1 = []
                                    nor_arr1 = []
                                    col_arr1 = []

                                    tri_arr2 = []
                                    nor_arr2 = []
                                    col_arr2 = []

                                    tri_arr3 = []
                                    nor_arr3 = []
                                    col_arr3 = []

                            from copy import copy
                            new_tube_dpos = copy(tube_dpos)

                            for n in range(1, len(tube_pos)-1):
                                pos = tube_pos[n]

                                col = tube_col[n][0]
                                col2 = tube_col[n+1][0]
                                if last_pos:
                                    next_pos = tube_pos[n+1]
                                    dpos1 = last_width * tube_dpos[n-1]
                                    dpos2 = width * tube_dpos[n]
                                    ddpos = dpos1-dpos2
                                    if reset:
                                        dpos1 = dpos2
                                        reset = False

                                    if self.proteinStyle == PROTEIN_STYLE_ZIGZAG:
                                        drawline(col, last_pos-dpos1, pos-dpos2, width=3)
                                        drawline(col, last_pos+dpos1, pos+dpos2, width=3)
                                        drawline(col, last_pos-dpos1, pos+dpos2, width=1)
                                        drawline(col, pos-dpos2, pos+dpos2, width=1)
                                        drawline(col, last_pos-dpos1, last_pos+dpos1, width=1)

                                    if self.proteinStyle == PROTEIN_STYLE_FLAT_RIBBON:
                                        if pos != last_pos:

                                            nvec1 = norm(cross(dpos1, pos-last_pos))
                                            if next_pos != pos:
                                                nvec2 = norm(cross(dpos2, next_pos-pos))
                                            else:
                                                nvec2 = nvec1

                                            nor_arr0.append(nvec1)
                                            nor_arr0.append(nvec1)
                                            nor_arr0.append(nvec2)
                                            nor_arr0.append(nvec2)

                                            tri_arr0.append(last_pos-dpos1) 
                                            tri_arr0.append(last_pos+dpos1)
                                            tri_arr0.append(pos-dpos2) 
                                            tri_arr0.append(pos+dpos2)

                                            col_arr0.append(col)
                                            col_arr0.append(col)
                                            col_arr0.append(col2)
                                            col_arr0.append(col2)

                                    if self.proteinStyle == PROTEIN_STYLE_SOLID_RIBBON or \
                                       self.proteinStyle == PROTEIN_STYLE_SIMPLE_CARTOONS or \
                                       self.proteinStyle == PROTEIN_STYLE_FANCY_CARTOONS:

                                        if secondary > 0:

                                            col3 = col4 = V(gray)

                                            if pos != last_pos:

                                                nvec1 = norm(cross(dpos1, pos-last_pos))
                                                if next_pos != pos:
                                                    nvec2 = norm(cross(dpos2, next_pos-pos))
                                                else:
                                                    nvec2 = nvec1

                                                nor_arr0.append(nvec1)
                                                nor_arr0.append(nvec1)
                                                nor_arr0.append(nvec2)
                                                nor_arr0.append(nvec2)

                                                if self.proteinStyle == PROTEIN_STYLE_FANCY_CARTOONS:
                                                    dn1 = 0.15 * nvec1 * scaleFactor
                                                    dn2 = 0.15 * nvec2 * scaleFactor
                                                else:
                                                    dn1 = 0.15 * nvec1 * scaleFactor
                                                    dn2 = 0.15 * nvec2 * scaleFactor

                                                tri_arr0.append(last_pos - dpos1 - dn1) 
                                                tri_arr0.append(last_pos + dpos1 - dn1)
                                                tri_arr0.append(pos - dpos2 - dn2) 
                                                tri_arr0.append(pos + dpos2 - dn2)

                                                col_arr0.append(col)
                                                col_arr0.append(col)
                                                col_arr0.append(col2)
                                                col_arr0.append(col2)

                                                nor_arr1.append(nvec1)
                                                nor_arr1.append(nvec1)
                                                nor_arr1.append(nvec2)
                                                nor_arr1.append(nvec2)

                                                tri_arr1.append(last_pos - dpos1 + dn1) 
                                                tri_arr1.append(last_pos + dpos1 + dn1)
                                                tri_arr1.append(pos - dpos2 + dn2) 
                                                tri_arr1.append(pos + dpos2 + dn2)

                                                if secondary == 1:
                                                    col_arr1.append(0.5 * col + 0.5 * V(white))
                                                    col_arr1.append(0.5 * col + 0.5 * V(white))
                                                    col_arr1.append(0.5 * col2 + 0.5 * V(white))
                                                    col_arr1.append(0.5 * col2 + 0.5 * V(white))
                                                else:
                                                    col_arr1.append(col)
                                                    col_arr1.append(col)
                                                    col_arr1.append(col2)
                                                    col_arr1.append(col2)

                                                nor_arr2.append(-dpos1)
                                                nor_arr2.append(-dpos1)
                                                nor_arr2.append(-dpos2)
                                                nor_arr2.append(-dpos2)

                                                tri_arr2.append(last_pos - dpos1 - dn1) 
                                                tri_arr2.append(last_pos - dpos1 + dn1)
                                                tri_arr2.append(pos - dpos2 - dn2) 
                                                tri_arr2.append(pos - dpos2 + dn2)

                                                col_arr2.append(col3)
                                                col_arr2.append(col3)
                                                col_arr2.append(col4)
                                                col_arr2.append(col4)

                                                nor_arr3.append(-dpos1)
                                                nor_arr3.append(-dpos1)
                                                nor_arr3.append(-dpos2)
                                                nor_arr3.append(-dpos2)

                                                tri_arr3.append(last_pos + dpos1 - dn1) 
                                                tri_arr3.append(last_pos + dpos1 + dn1)
                                                tri_arr3.append(pos + dpos2 - dn2) 
                                                tri_arr3.append(pos + dpos2 + dn2)

                                                col_arr3.append(col3)
                                                col_arr3.append(col3)
                                                col_arr3.append(col4)
                                                col_arr3.append(col4)


                                last_pos = pos
                                last_width = width

                                if secondary == 1:
                                    if n > len(tube_pos) - resolution:
                                        width -= dw 
                                    elif width < 1.0 * scaleFactor:
                                        width += dw

                                if secondary == 2:
                                    if n == len(tube_pos) - 1.5 * resolution:
                                        width = scaleFactor * 1.6
                                        reset = True
                                    if n > len(tube_pos) - 1.5 * resolution:
                                        width -= dw 

                                new_tube_dpos[n] = width * tube_dpos[n]

                        ###drawcylinder(white, tube_pos[0], tube_pos[10], 1.0)

                        if self.proteinStyle == PROTEIN_STYLE_FLAT_RIBBON:
                            drawtriangle_strip([1.0,1.0,0.0,-2.0], tri_arr0, nor_arr0, col_arr0)

                        if self.proteinStyle == PROTEIN_STYLE_SOLID_RIBBON or \
                           self.proteinStyle == PROTEIN_STYLE_SIMPLE_CARTOONS or \
                           self.proteinStyle == PROTEIN_STYLE_FANCY_CARTOONS:
                            if secondary == 0:
                                drawpolycone_multicolor([0,0,0,-2], tube_pos, tube_col, tube_rad)
                            else:
                                if (secondary == 1 and self.proteinStyle == PROTEIN_STYLE_SOLID_RIBBON) or \
                                   secondary == 2:
                                    drawtriangle_strip([1.0,1.0,0.0,-2.0], tri_arr0, nor_arr0, col_arr0)
                                    drawtriangle_strip([1.0,1.0,0.0,-2.0], tri_arr1, nor_arr1, col_arr1)
                                    drawtriangle_strip([1.0,1.0,0.0,-2.0], tri_arr2, nor_arr2, col_arr2)
                                    drawtriangle_strip([1.0,1.0,0.0,-2.0], tri_arr3, nor_arr3, col_arr3)
                                    # Fill in the strand N-terminal end.
                                    quad_tri = []
                                    quad_nor = []
                                    quad_col = []
                                    quad_tri.append(tri_arr2[0])
                                    quad_tri.append(tri_arr3[0])
                                    quad_tri.append(tri_arr2[1])
                                    quad_tri.append(tri_arr3[1])
                                    quad_nor.append(nor_arr2[0])
                                    quad_nor.append(nor_arr3[0])
                                    quad_nor.append(nor_arr2[1])
                                    quad_nor.append(nor_arr3[1])
                                    quad_col.append(col_arr2[0])
                                    quad_col.append(col_arr3[0])
                                    quad_col.append(col_arr2[1])
                                    quad_col.append(col_arr3[1])
                                    drawtriangle_strip([1.0,1.0,1.0,-2.0],quad_tri,quad_nor,quad_col)

                                if (secondary == 1 and self.proteinStyle == PROTEIN_STYLE_FANCY_CARTOONS):
                                    drawtriangle_strip([1.0,1.0,0.0,-2.0], tri_arr0, nor_arr0, col_arr0)
                                    drawtriangle_strip([1.0,1.0,0.0,-2.0], tri_arr1, nor_arr1, col_arr1)
                                    tube_pos_left = []
                                    tube_pos_right = []
                                    new_tube_dpos[0] *= 0.1
                                    new_tube_dpos[1] *= 0.2
                                    new_tube_dpos[-1] *= 0.1
                                    new_tube_dpos[-2] *= 0.2
                                    for p in range(len(tube_pos)):
                                        tube_pos_left.append(tube_pos[p] - new_tube_dpos[p])
                                        tube_pos_right.append(tube_pos[p] + new_tube_dpos[p])
                                        tube_rad[p] *= 0.75
                                    drawpolycone_multicolor([0,0,0,-2], tube_pos_left, tube_col, tube_rad)
                                    drawpolycone_multicolor([0,0,0,-2], tube_pos_right, tube_col, tube_rad)

#                    else:                               
                    if (secondary == 1 and style == PROTEIN_STYLE_SIMPLE_CARTOONS):
                        drawcylinder(tube_col[0][0], tube_pos[1], tube_pos[-3], 2.5, capped=1)
                        #print "hopsa"

                    if style == PROTEIN_STYLE_LADDER or \
                       style == PROTEIN_STYLE_TUBE:
                        # Draw tube.
                        drawpolycone_multicolor([0,0,0,-2], tube_pos, tube_col, tube_rad)

            # increase Sec. Str. element counter
            current_sec += 1