Giter Club home page Giter Club logo

Comments (5)

ghutchis avatar ghutchis commented on June 12, 2024

This would be awesome, since it would show "more" of the molecule in the default view. Do you need help with this?

from 3dmol.js.

dkoes avatar dkoes commented on June 12, 2024

If you'd like to work on it, that would be great. It's not a super high priority for me. You'd probably want to sample the atoms for larger structures. Unfortunately, our matrix class doesn't have eigenvalue decomposition so this would need to be pulled in from somewhere else (numeric.js?).

from 3dmol.js.

dkoes avatar dkoes commented on June 12, 2024

Here's some python for calculating the principal moments of inertia of a molecule:


def alignmol(mol):
    coordinates = []
    masses = []
    coords = OEFloatArray(mol.GetMaxAtomIdx() * 3)
    mol.GetCoords(coords)
    totalMass = 0
    for atom in mol.GetAtoms():
        idx = atom.GetIdx()
        coordinates.append((coords[idx * 3], coords[idx * 3 + 1], coords[idx * 3 + 2]))
        m = 1
        if options.usevolumes:
            r = OEGetBondiVdWRadius(atom.GetAtomicNum())
            v = 4.0 * r * r * r * math.pi / 3.0
            m *= v
        if options.useweights:
            m *= OEGetAverageWeight(atom.GetAtomicNum())
        masses.append(m)
        totalMass += m

    coordinates = numpy.array(coordinates)
    masses = numpy.array(masses)
    trans = -numpy.sum(coordinates * masses[:, numpy.newaxis], axis=0) / totalMass
    OETranslate(mol, OEFloatArray(trans))
    coordinates += trans
    #this is shamelessly stolen from the mdanalysis package
    values = zip(masses, coordinates)
    # Create the inertia tensor
    # m_i = mass of atom i
    # (x_i, y_i, z_i) = pos of atom i
    # Ixx = sum(m_i*(y_i^2+z_i^2)); Iyy = sum(m_i*(x_i^2+z_i^2)); Izz = sum(m_i*(x_i^2+y_i^2))
    # Ixy = Iyx = -1*sum(m_i*x_i*y_i)
    # Ixz = Izx = -1*sum(m_i*x_i*z_i)
    # Iyz = Izy = -1*sum(m_i*y_i*z_i)
    Ixx = reduce(lambda t, a: t + a[0] * (a[1][1] * a[1][1] + a[1][2] * a[1][2]), values, 0.)
    Iyy = reduce(lambda t, a: t + a[0] * (a[1][0] * a[1][0] + a[1][2] * a[1][2]), values, 0.)
    Izz = reduce(lambda t, a: t + a[0] * (a[1][0] * a[1][0] + a[1][1] * a[1][1]), values, 0.)
    Ixy = Iyx = -1 * reduce(lambda t, a: t + a[0] * a[1][0] * a[1][1], values, 0.)
    Ixz = Izx = -1 * reduce(lambda t, a: t + a[0] * a[1][0] * a[1][2], values, 0.)
    Iyz = Izy = -1 * reduce(lambda t, a: t + a[0] * a[1][1] * a[1][2], values, 0.)
    inertia = numpy.array([[Ixx, Ixy, Ixz], [Iyx, Iyy, Iyz], [Izx, Izy, Izz]])

    eigenval, eigenvec = eig(inertia)
        # Sort
    indices = numpy.argsort(eigenval)
        # Return transposed in more logical form. See Issue 33.
    principalAxes = eigenvec[:,indices].T
    if det(principalAxes) < 0:
        principalAxes = -principalAxes #remove reflection

    rotmat = OEFloatArray(principalAxes.flatten())
    OERotate(mol, rotmat)
    return

from 3dmol.js.

ghutchis avatar ghutchis commented on June 12, 2024

I can probably greatly simplify that for you without needing OpenEye calls.

from 3dmol.js.

ghutchis avatar ghutchis commented on June 12, 2024

My guess is that I won't get to this until later this summer, but I'll task Ritwik to pull together an implementation. (I'd probably use a best-fit plane routine, which would avoid eigenvalues.)

from 3dmol.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.