Ejemplo n.º 1
0
def _cgoArrowhead(viewer,
                  tail,
                  head,
                  radius,
                  color,
                  label,
                  headFrac=0.3,
                  nSteps=10,
                  aspect=.5):
    global _globalArrowCGO
    delta = head - tail
    normal = _getVectNormal(delta)
    delta.Normalize()

    dv = head - tail
    dv.Normalize()
    dv *= headFrac
    startP = head

    normal *= headFrac * aspect

    cgo = [
        BEGIN, TRIANGLE_FAN, COLOR, color[0], color[1], color[2], NORMAL, dv.x,
        dv.y, dv.z, VERTEX, head.x + dv.x, head.y + dv.y, head.z + dv.z
    ]
    base = [
        BEGIN, TRIANGLE_FAN, COLOR, color[0], color[1], color[2], NORMAL,
        -dv.x, -dv.y, -dv.z, VERTEX, head.x, head.y, head.z
    ]
    v = startP + normal
    cgo.extend([NORMAL, normal.x, normal.y, normal.z])
    cgo.extend([VERTEX, v.x, v.y, v.z])
    base.extend([VERTEX, v.x, v.y, v.z])
    for i in range(1, nSteps):
        v = FeatDirUtils.ArbAxisRotation(360. / nSteps * i, delta, normal)
        cgo.extend([NORMAL, v.x, v.y, v.z])
        v += startP
        cgo.extend([VERTEX, v.x, v.y, v.z])
        base.extend([VERTEX, v.x, v.y, v.z])

    cgo.extend([NORMAL, normal.x, normal.y, normal.z])
    cgo.extend([
        VERTEX, startP.x + normal.x, startP.y + normal.y, startP.z + normal.z
    ])
    base.extend([
        VERTEX, startP.x + normal.x, startP.y + normal.y, startP.z + normal.z
    ])
    cgo.append(END)
    base.append(END)
    cgo.extend(base)

    #viewer.server.renderCGO(cgo,label)
    _globalArrowCGO.extend(cgo)
Ejemplo n.º 2
0
def ShowMolFeats(mol,
                 factory,
                 viewer,
                 radius=0.5,
                 confId=-1,
                 showOnly=True,
                 name='',
                 transparency=0.0,
                 colors=None,
                 excludeTypes=[],
                 useFeatDirs=True,
                 featLabel=None,
                 dirLabel=None,
                 includeArrowheads=True,
                 writeFeats=False,
                 showMol=True,
                 featMapFile=False):
    global _globalSphereCGO
    if not name:
        if mol.HasProp('_Name'):
            name = mol.GetProp('_Name')
        else:
            name = 'molecule'
    if not colors:
        colors = _featColors

    if showMol:
        viewer.ShowMol(mol, name=name, showOnly=showOnly, confId=confId)

    molFeats = factory.GetFeaturesForMol(mol)
    if not featLabel:
        featLabel = f'{name}-feats'
        viewer.server.resetCGO(featLabel)
    if not dirLabel:
        dirLabel = featLabel + "-dirs"
        viewer.server.resetCGO(dirLabel)

    for feat in molFeats:
        family = feat.GetFamily()
        if family in excludeTypes:
            continue
        pos = feat.GetPos(confId)
        color = colors.get(family, (.5, .5, .5))

        if transparency:
            _globalSphereCGO.extend([ALPHA, 1 - transparency])
        else:
            _globalSphereCGO.extend([ALPHA, 1])
        _globalSphereCGO.extend([
            COLOR, color[0], color[1], color[2], SPHERE, pos.x, pos.y, pos.z,
            radius
        ])
        if writeFeats:
            aidText = ' '.join([str(x + 1) for x in feat.GetAtomIds()])
            print(
                f'{family}\t{pos.x:.3f}\t{pos.y:.3f}\t{pos.z:.3f}\t1.0\t# {aidText}'
            )

        if featMapFile:
            print(
                f"  family={family} pos=({pos.x:.3f}, {pos.y:.3f}, {pos.z:.3f}) weight=1.0",
                end='',
                file=featMapFile)

        if useFeatDirs:
            ps = []
            if family == 'Aromatic':
                ps, _ = FeatDirUtils.GetAromaticFeatVects(
                    mol.GetConformer(confId),
                    feat.GetAtomIds(),
                    pos,
                    scale=1.0)

            elif family == 'Donor':
                aids = feat.GetAtomIds()
                if len(aids) == 1:
                    FeatVectsDictMethod = {
                        1: FeatDirUtils.GetDonor1FeatVects,
                        2: FeatDirUtils.GetDonor2FeatVects,
                        3: FeatDirUtils.GetDonor3FeatVects,
                    }
                    featAtom = mol.GetAtomWithIdx(aids[0])
                    numHvyNbrs = len([
                        1 for x in featAtom.GetNeighbors()
                        if x.GetAtomicNum() > 1
                    ])
                    ps, _ = FeatVectsDictMethod[numHvyNbrs](
                        mol.GetConformer(confId), aids, scale=1.0)

            elif family == 'Acceptor':
                aids = feat.GetAtomIds()
                if len(aids) == 1:
                    FeatVectsDictMethod = {
                        1: FeatDirUtils.GetDonor1FeatVects,
                        2: FeatDirUtils.GetDonor2FeatVects,
                        3: FeatDirUtils.GetDonor3FeatVects,
                    }
                    featAtom = mol.GetAtomWithIdx(aids[0])
                    numHvyNbrs = len([
                        x for x in featAtom.GetNeighbors()
                        if x.GetAtomicNum() > 1
                    ])
                    ps, _ = FeatVectsDictMethod[numHvyNbrs](
                        mol.GetConformer(confId), aids, scale=1.0)

            for tail, head in ps:
                ShowArrow(viewer,
                          tail,
                          head,
                          radius,
                          color,
                          dirLabel,
                          transparency=transparency,
                          includeArrowhead=includeArrowheads)
                if featMapFile:
                    vect = head - tail
                    print(f'dir=({vect.x:.3f}, {vect.y:.3f}, {vect.z:.3f})',
                          end='',
                          file=featMapFile)

        if featMapFile:
            aidText = ' '.join([str(x + 1) for x in feat.GetAtomIds()])
            print(f'# {aidText}', file=featMapFile)
Ejemplo n.º 3
0
def ShowMolFeats(mol,
                 factory,
                 viewer,
                 radius=0.5,
                 confId=-1,
                 showOnly=True,
                 name='',
                 transparency=0.0,
                 colors=None,
                 excludeTypes=[],
                 useFeatDirs=True,
                 featLabel=None,
                 dirLabel=None,
                 includeArrowheads=True,
                 writeFeats=False,
                 showMol=True,
                 featMapFile=False):
    global _globalSphereCGO
    if not name:
        if mol.HasProp('_Name'):
            name = mol.GetProp('_Name')
        else:
            name = 'molecule'
    if not colors:
        colors = _featColors

    if showMol:
        viewer.ShowMol(mol, name=name, showOnly=showOnly, confId=confId)

    molFeats = factory.GetFeaturesForMol(mol)
    if not featLabel:
        featLabel = '%s-feats' % name
        viewer.server.resetCGO(featLabel)
    if not dirLabel:
        dirLabel = featLabel + "-dirs"
        viewer.server.resetCGO(dirLabel)

    for i, feat in enumerate(molFeats):
        family = feat.GetFamily()
        if family in excludeTypes:
            continue
        pos = feat.GetPos(confId)
        color = colors.get(family, (.5, .5, .5))
        nm = '%s(%d)' % (family, i + 1)

        if transparency:
            _globalSphereCGO.extend([ALPHA, 1 - transparency])
        else:
            _globalSphereCGO.extend([ALPHA, 1])
        _globalSphereCGO.extend([
            COLOR, color[0], color[1], color[2], SPHERE, pos.x, pos.y, pos.z,
            radius
        ])
        if writeFeats:
            aidText = ' '.join([str(x + 1) for x in feat.GetAtomIds()])
            print('%s\t%.3f\t%.3f\t%.3f\t1.0\t# %s' %
                  (family, pos.x, pos.y, pos.z, aidText))

        if featMapFile:
            print("  family=%s pos=(%.3f,%.3f,%.3f) weight=1.0" %
                  (family, pos.x, pos.y, pos.z),
                  end='',
                  file=featMapFile)

        if useFeatDirs:
            ps = []
            if family == 'Aromatic':
                ps, fType = FeatDirUtils.GetAromaticFeatVects(
                    mol.GetConformer(confId),
                    feat.GetAtomIds(),
                    pos,
                    scale=1.0)
            elif family == 'Donor':
                aids = feat.GetAtomIds()
                if len(aids) == 1:
                    featAtom = mol.GetAtomWithIdx(aids[0])
                    hvyNbrs = [
                        x for x in featAtom.GetNeighbors()
                        if x.GetAtomicNum() != 1
                    ]
                    if len(hvyNbrs) == 1:
                        ps, fType = FeatDirUtils.GetDonor1FeatVects(
                            mol.GetConformer(confId), aids, scale=1.0)
                    elif len(hvyNbrs) == 2:
                        ps, fType = FeatDirUtils.GetDonor2FeatVects(
                            mol.GetConformer(confId), aids, scale=1.0)
                    elif len(hvyNbrs) == 3:
                        ps, fType = FeatDirUtils.GetDonor3FeatVects(
                            mol.GetConformer(confId), aids, scale=1.0)
            elif family == 'Acceptor':
                aids = feat.GetAtomIds()
                if len(aids) == 1:
                    featAtom = mol.GetAtomWithIdx(aids[0])
                    hvyNbrs = [
                        x for x in featAtom.GetNeighbors()
                        if x.GetAtomicNum() != 1
                    ]
                    if len(hvyNbrs) == 1:
                        ps, fType = FeatDirUtils.GetAcceptor1FeatVects(
                            mol.GetConformer(confId), aids, scale=1.0)
                    elif len(hvyNbrs) == 2:
                        ps, fType = FeatDirUtils.GetAcceptor2FeatVects(
                            mol.GetConformer(confId), aids, scale=1.0)
                    elif len(hvyNbrs) == 3:
                        ps, fType = FeatDirUtils.GetAcceptor3FeatVects(
                            mol.GetConformer(confId), aids, scale=1.0)

            for tail, head in ps:
                ShowArrow(viewer,
                          tail,
                          head,
                          radius,
                          color,
                          dirLabel,
                          transparency=transparency,
                          includeArrowhead=includeArrowheads)
                if featMapFile:
                    vect = head - tail
                    print('dir=(%.3f,%.3f,%.3f)' % (vect.x, vect.y, vect.z),
                          end='',
                          file=featMapFile)

        if featMapFile:
            aidText = ' '.join([str(x + 1) for x in feat.GetAtomIds()])
            print('# %s' % (aidText), file=featMapFile)