【问题标题】:PCA using MDAanalysis (python3.7)使用 MDA 分析的 PCA (python3.7)
【发布时间】:2019-11-19 08:42:55
【问题描述】:

我开始从事计算化学领域的工作,并被要求对分子动力学的某些轨迹进行主成分分析。我被告知要使用 MDAnalysis 包,因此我在他们的页面上找到了一个教程并尝试遵循它(但我当然包含了我自己的输入),看看它是否会起作用。我从来没有做过像这个广告这样的分析我也是 python 编码的新手。 我附上了受教程启发的代码。但它对我不起作用,它引发了很多错误,其中一个错误是它不能接受我的输入(拓扑是 PDB 文件,坐标是 XTC 文件),但那些是以支持的格式列出的格式或其他错误是未定义“PCA 类”。 我没有找到很多关于使用其他人的 MDA 分析处理 PCA 的信息,因此我希望在这里我可以找到曾经做过类似事情的人,并且可以帮助我。我已经尝试过相关的 subreddits,但没有结果。

from __future__ import division, absolute_import
import MDAnalysis as mda
import MDAnalysis.analysis.pca as pca

from six.moves import range
import warnings

import numpy as np
import scipy.integrate

from MDAnalysis import Universe
from MDAnalysis.analysis.align import _fit_to
from MDAnalysis.lib.log import ProgressMeter


u = mda.Universe("L22trial.pdb", "L22trial.xtc") 


PCA = mda.analysis.pca.PCA
class PCA():
    pca = PCA(u, select='backbone').run()
    pca_space =  pca.transform(u.select_atoms('backbone'))

    def __init__(self, universe, select='all', align=False, mean=None,
                 n_components=None, **kwargs):
            super(PCA, self).__init__(universe.trajectory, **kwargs)
            self._u = universe

            self.align = align
            self._calculated = False
            self.n_components = n_components
            self._select = select
            self._mean = mean

    def _prepare(self):
        self._u.trajectory[self.start]
        self._reference = self._u.select_atoms(self._select)
        self._atoms = self._u.select_atoms(self._select)
        self._n_atoms = self._atoms.n_atoms

        if self._mean is None:
            self.mean = np.zeros(self._n_atoms*3)
            self._calc_mean = True
        else:
            self.mean = self._mean.positions
            self._calc_mean = False

        if self.n_frames == 1:
            raise ValueError('No covariance information can be gathered from a single trajectory  frame.\n')

        n_dim = self._n_atoms * 3
        self.cov = np.zeros((n_dim, n_dim))
        self._ref_atom_positions = self._reference.positions
        self._ref_cog = self._reference.center_of_geometry()
        self._ref_atom_positions -= self._ref_cog

        if self._calc_mean:
            interval = int(self.n_frames // 100)
            interval = interval if interval > 0 else 1
            format = ("Mean Calculation Step %(step)5d/%(numsteps)d [%(percentage)5.1f%%]")
            mean_pm = ProgressMeter(self.n_frames if self.n_frames else 1, interval=interval, verbose=self._verbose, format=format)
            for i, ts in enumerate(self._u.trajectory[self.start:self.stop:self.step]):
                if self.align:
                    mobile_cog = self._atoms.center_of_geometry()
                    mobile_atoms, old_rmsd = _fit_to(self._atoms.positions, self._ref_atom_positions, self._atoms, mobile_com=mobile_cog, ref_com=self._ref_cog)
                else:
                    self.mean += self._atoms.positions.ravel()
                mean_pm.echo(i)
            self.mean /= self.n_frames

        self.mean_atoms = self._atoms
        self.mean_atoms.positions = self._atoms.positions

    def _single_frame(self):
        if self.align:
            mobile_cog = self._atoms.center_of_geometry()
            mobile_atoms, old_rmsd = _fit_to(self._atoms.positions, self._ref_atom_positions, self._atoms, mobile_com=mobile_cog, ref_com=self._ref_cog)
            x = mobile_atoms.positions.ravel()
        else:
            x = self._atoms.positions.ravel()
        x -= self.mean
        self.cov += np.dot(x[:, np.newaxis], x[:, np.newaxis].T)

    def _conclude(self):
        self.cov /= self.n_frames - 1
        e_vals, e_vects = np.linalg.eig(self.cov)
        sort_idx = np.argsort(e_vals)[::-1]
        self.variance = e_vals[sort_idx]
        self.variance = self.variance[:self.n_components]
        self.p_components = e_vects[:self.n_components, sort_idx]
        self.cumulated_variance = (np.cumsum(self.variance) / np.sum(self.variance))

        self._calculated = True

    def transform(self, atomgroup, n_components=None, start=None, stop=None, step=None):
        if not self._calculated:
            raise ValueError('Call run() on the PCA before using transform')
        if isinstance(atomgroup, Universe):
            atomgroup = atomgroup.atoms
        if(self._n_atoms != atomgroup.n_atoms):
            raise ValueError('PCA has been fit for {} atoms. Your atomgroup has {} atoms'.format(self._n_atoms, atomgroup.n_atoms))
        if not (self._atoms.types == atomgroup.types).all():
            warnings.warn('Atom types do not match with types used to fit PCA')

        traj = atomgroup.universe.trajectory
        start, stop, step = traj.check_slice_indices(start, stop, step)
        n_frames = len(range(start, stop, step))

        dim = (n_components if n_components is not None else self.p_components.shape[1])
        dot = np.zeros((n_frames, dim))

        for i, ts in enumerate(traj[start:stop:step]):
            xyz = atomgroup.positions.ravel() - self.mean
            dot[i] = np.dot(xyz, self.p_components[:, :n_components])

        return dot

def cosine_content(pca_space, i):
    t = np.arange(len(pca_space))
    T = len(pca_space)
    cos = np.cos(np.pi * t * (i + 1) / T)
    return ((2.0 / T) * (scipy.integrate.simps(cos*pca_space[:, i])) ** 2 /
            scipy.integrate.simps(pca_space[:, i] ** 2))

【问题讨论】:

  • 能否提供错误堆栈信息。这可能会有所帮助。
  • ValueError: '' 不是有效的拓扑格式,也不是可以最小推断拓扑的坐标格式。您可以使用“Universe(topology, ..., topology_format=FORMAT)”明确指定格式并覆盖自动检测。已知的 FORMAT 有:dict_keys(['PSF', 'TOP', 'PRMTOP', 'PARM7', 'PDB', 'ENT', 'XPDB', 'PQR', 'GRO', 'CRD', 'PDBQT' 、'DMS'、'TPR'、'MOL2'、'DATA'、'LAMMPSDUMP'、'XYZ'、'TXYZ'、'ARC'、'GMS'、'CONFIG'、'HISTORY'、'XML'、' MMTF', 'GSD', 'MINIMAL'])
  • 您能解释一下为什么从 mda.analysis.pca.PCA 导入 PCA,然后定义一个名为 PCA 的类。您似乎愿意做的是从 PCA 派生一个类,在这种情况下重写 class PCA 具有以下 class MyPCA(PCA) 。您能否提供一个链接,您可以从该链接中获得启发您从这段代码中获得灵感的示例。
  • 我不确定,我应该删除PCA = mda.analysis.pca.PCA 吗?当我在脚本中仅使用“类 PCA”时,会引发未定义类 PCA 的错误。此外,在他们使用的原始脚本中,class PCA(AnalysisBase)。他们从模块“.base”导入了 AnalysisBase,但这是我的第一个错误,没有这样的模块。

标签: python python-3.x pca mdanalysis


【解决方案1】:

您似乎复制并粘贴了 PCA 类 itefl。我的猜测是您不需要这样做(我从未使用过该模块,所以这只是一个猜测)。 文档(https://www.mdanalysis.org/docs/documentation_pages/analysis/pca.html)似乎表明您唯一需要做的就是以下

    import MDAnalysis as mda
    import MDAnalysis.analysis.pca as pca

    u = mda.Universe("L22trial.pdb", "L22trial.xtc") 

    mypca = pca.PCA(u, select='backbone').run()
    pca_space =  mypca.transform(u.select_atoms('backbone'))

如果您有一条错误消息“没有名为 'MDAnalysis.analysis.pca.PCA' 的模块;'MDAnalysis.analysis.pca' 不是一个包”,这意味着它所说的 :-)。 这意味着您的计算机上没有名为 MDAnalysis 的软件包。要解决此问题,您需要使用 pip install 命令或 conda 如果您使用 conda 包管理器进行安装。看这个链接https://www.mdanalysis.org/pages/installation_quick_start/

查看链接https://www.mdanalysis.org/docs/_modules/MDAnalysis/analysis/pca.html,您从中获得灵感,它证实了我的第一个猜测,我认为我的回答应该允许您使用该软件包。

【讨论】:

  • 但这引发了我的错误:没有名为“MDAnalysis.analysis.pca.PCA”的模块; 'MDAnalysis.analysis.pca' 不是一个包
  • 链接mdanalysis.org/docs/_modules/MDAnalysis/analysis/pca.html首先描述了如何使用PCA类然后描述了PCA类的设计。在安装 MDAanalysis 包之后,试试我所说的,我回答中的简单代码,看看你得到了什么。如果你使用 windows 建议使用 conda 安装(见github.com/MDAnalysis/mdanalysis/wiki/MDAnalysis-on-Windows
  • 我使用 Linux 并且安装了 MDAnalysis 包(我再次检查了它)。不幸的是,仍然引发错误:没有名为“MDAnalysis.analysis.pca.PCA”的模块; 'MDAnalysis.analysis.pca' 不是一个包
  • 试试我刚刚修改的代码:import MDAnalysis.analysis.pca as pca 而不是 import MDAnalysis.analysis.pca.PCA as PCA .我还将变量 pca 重命名为 mypca 并更改了对 run() 的调用
  • 谢谢!现在它不返回任何错误。你使用 mda 包吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-07
  • 2015-10-24
  • 2021-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多