mdcompress的安装与使用

对于长效分子动力学模拟来说,如何去高效的存储分子轨迹,是一个重要的问题。前者有Gromacs的xtc格式文件,利用量化和比特级的压缩,将分子轨迹压缩到23%左右。而本文介绍的mdc工具,可以在xtc的基础上,再压缩到60%左右。

MDCompress的安装构建

如果本地有gcc-12和g++-12,以及make cmake build-essential python3 python3-dev rustc cargo git这些基本的环境依赖,那么就可以直接运行如下指令,先把仓库clone到本地,然后编译构建:

bash 复制代码
$ git clone --recurse-submodules https://github.com/refresh-bio/mdcompress
$ cd mdcompress
$ make -j
$ ./bin/mdcompress compress -i examples/data/example.xtc --topology examples/data/example.tpr -o example.mdc

这是顺利的情况,以下是几个有可能出现的问题,及其解决方案。

Git下载网络问题

在国内的网络环境下,git下载仓库这一步就有可能报错,很多时候是卡在chemfiles这个子模块。那么我推荐的一个路径是:将仓库映射到Gitee镜像库,然后把相应的链接修改为Gitee的路径。具体操作是先在Gitee上点击,从Github导入仓库:

然后填入这个链接地址:https://github.com/refresh-bio-dependencies/chemfiles.git。这就在Gitee上创建了一个镜像仓库:

这个仓库的地址从github链接变成了:https://gitee.com/dechin/chemfiles.git。然后用命令行或者手动的把这个链接配置到mdcompress的仓库中:

bash 复制代码
$ sed -i 's|https://github.com/refresh-bio-dependencies/chemfiles.git|https://gitee.com/dechin/chemfiles.git|g' .gitmodules
$ grep -A2 'libs/chemfiles' .gitmodules
[submodule "libs/chemfiles"]
        path = libs/chemfiles
        url = https://gitee.com/dechin/chemfiles.git
[submodule "libs/nanobind"]
$ git submodule sync libs/chemfiles
为 'libs/chemfiles' 同步子模组 url
$ rm -rf libs/chemfiles
$ git submodule update --init libs/chemfiles
正克隆到 '/home/xxx/workspace/mdcompress/libs/chemfiles'...
子模组路径 'libs/chemfiles':检出 '487cbdba3286aec25c'
$ ll ./libs/chemfiles/
总计 112
drwxrwxr-x 11 dechin dechin  4096  7月 17 18:09 ./
drwxrwxr-x  6 dechin dechin  4096  7月 17 18:09 ../
-rw-rw-r--  1 dechin dechin   394  7月 17 18:09 AUTHORS
-rw-rw-r--  1 dechin dechin 19675  7月 17 18:09 CHANGELOG.md
drwxrwxr-x  3 dechin dechin  4096  7月 17 18:09 cmake/
-rw-rw-r--  1 dechin dechin  9551  7月 17 18:09 CMakeLists.txt
-rw-rw-r--  1 dechin dechin   164  7月 17 18:09 .codecov.yml
-rw-rw-r--  1 dechin dechin  3200  7月 17 18:09 Contributing.md
drwxrwxr-x  6 dechin dechin  4096  7月 17 18:09 doc/
drwxrwxr-x  4 dechin dechin  4096  7月 17 18:09 examples/
drwxrwxr-x  2 dechin dechin  4096  7月 17 18:09 external/
-rw-rw-r--  1 dechin dechin    42  7月 17 18:09 .git
drwxrwxr-x  3 dechin dechin  4096  7月 17 18:09 .github/
-rw-rw-r--  1 dechin dechin    51  7月 17 18:09 .gitignore
drwxrwxr-x  3 dechin dechin  4096  7月 17 18:09 include/
-rw-rw-r--  1 dechin dechin  1541  7月 17 18:09 LICENSE
-rw-rw-r--  1 dechin dechin  7524  7月 17 18:09 README.md
drwxrwxr-x  4 dechin dechin  4096  7月 17 18:09 scripts/
drwxrwxr-x  6 dechin dechin  4096  7月 17 18:09 src/
drwxrwxr-x  9 dechin dechin  4096  7月 17 18:09 tests/
-rw-rw-r--  1 dechin dechin    11  7月 17 18:09 VERSION

到这里仓库就下载并配置完成,可以开始编译了。

gcc-12依赖

这个仓库依赖于gcc-12和g++-12及以上的版本,但是很多人本地可能已经有了一些gcc的版本,但是这里不用担心,gcc支持多版本共存的管理模式,可以按照如下方式在Ubuntu Linux平台安装gcc-12和g++-12(需要sudo权限):

bash 复制代码
$ sudo add-apt-repository ppa:ubuntu-toolchain-r/test
$ sudo apt update
$ sudo apt install gcc-12 g++-12
$ sudo apt install rustc cargo # 这个依赖很重要,必须执行

安装好基础环境之后,可以通过如下方式编译构建mdcompress,并指定使用gcc-12和g++-12:

bash 复制代码
$ make -j CC=gcc-12 CXX=g++-12

编译完成后,可以通过mdcompress --version测试编译是否成功:

bash 复制代码
$ ./bin/mdcompress --version
mdcompress version: 2.0.4
Date: 2026-06-22
Authors: Marek Kokot, Sebastian Deorowicz
Usage:
mdcompress <mode> [options]
Modes:
  compress    - compress a trajectory (XTC/TRR/DCD/...) into MDC format
  decompress  - decompress MDC file into a trajectory (XTC/TRR/DCD/...)
  select      - decompress some frames from MDC file into a trajectory (XTC/TRR/DCD/...)
  info        - info about contents of MDC file
  make_desc   - create description file (for -d) from a topology (TPR/PSF/...)
Options - compress mode
  -i <file_name>               - input trajectory (XTC, TRR, DCD, ...)
  -o <file_name>               - output file name (.mdc)
  -d <file_name>               - description of the segments of a frame.
                                 May be omitted if --topology is given: mdcompress then
                                 builds the description from the topology automatically.
  --topology <file_name>       - topology file (TPR, PSF, PDB, ...). It is stored inside the .mdc.
                                 If -d is not given, mdcompress infers the description from it.
                                 NOTE: coordinate-only trajectories (e.g. DCD) carry no topology,
                                 so either -d or --topology must be provided for them.
                                 NOTE 2: It may happen that mdcompress cannot build a description
                                 from the file specified with the --topology switch;
                                 in that case, -d must be provided.
  --only-mol                   - compress only the molecule segments, skipping water/ions/other
                                 from the description/topology. Usually not needed: if the
                                 trajectory contains only molecules, mdcompress detects this and
                                 enables --only-mol automatically (with a warning).
  (description check)          - whether -d or --topology is used, the description is validated
                                 against the trajectory's atom count before compressing:
                                   * exact match            -> used as-is;
                                   * only molecules match   -> --only-mol enabled automatically;
                                   * no match               -> compression is aborted with an
                                     explanation, and (when the description was inferred from a
                                     topology) a candidate is written to <output>.candidate.desc
                                     for you to edit and reuse with -d.
  -l <int>                     - compression level (default: 2)
  --preset <preset>            - use one of preset parameters for -b, --subsegment-size,
                                 available presets:
                                   * default,
                                   * archive - use if the main decompression use scheme is decompress all,
                                   * trajectory - use if the main decompression use scheme is tracking trajectory
                                                  of some atoms,
                                   * frames - use if the main decompression use scheme is selecting subset of frames.
                                Current usage message prints defaults in regards to configured preset.
  -t <int>                     - no. threads
  -b|--batch-size <int>        - no. of frames in a batch (default: 20)
  -h|--max-history-size <int>  - no. of previous framed used to predict the current one (default: 1; max: 3)
  --res <int>                  - min. resolution in fm (default: 1000; 1000 fm = 0.01 Angstrom)
  --subsegment <id1,id2,...>   - list of segments to split into subsegments (if no specified subsegment all)
  --subsegment-size <int>      - number of atoms in a single subsegment (default: 102, 0 means don't use subsegments)
  --n-frames-for-model         - no. of frames to build model (default: 50)
  --max-dist-in-model          - max. distance in segment of reference atoms (default: 100)
Options - decompress mode
  -i <file_name>               - input file name (.mdc)
  -o <file_name>               - output trajectory file name (format chosen by extension: .xtc/.trr/.dcd/...)
  --topology <file_name>       - if a topology was stored at compression time, write it out to
                                 this path (the extension must match the original topology file)
Options - select mode
  -i <file_name>               - input file name (.mdc)
  -o <file_name>               - output trajectory file name (format chosen by extension: .xtc/.trr/.dcd/...)
  --topology <file_name>       - if a topology was stored at compression time, write it out to
                                 this path (the extension must match the original topology file)
  --fid <int>                  - frame id (0-based)
  --fr <int> <int|MAX>         - range of frame ids (0-based) (`MAX` or 2147483647 means last frame)
  --stride <int>               - stride size (for range of frames) (default: 1)
  --segments <id1,id2,...>     - list of segment ids
  --atoms <id1,id2,...>        - list of atoms to tracks, `id<n>` may be single id or closed interval in format start-end
Options - info mode
  -i <file_name>               - input file name (.mdc)
  --full                       - print full info (include subsegments)
Options - make_desc mode
  -i <file_name>               - input topology file (TPR, PSF, ...)
  -o <file_name>               - output desc file name
  --only-mol                   - include only molecule (skip water and 'other')

如果有上述输出内容,就表示编译成功了。

MDCompress压缩

按照上述章节的内容完成编译构建之后,就可以开始使用MDCompress进行压缩了。比较常规的策略是先用xtc格式压缩一遍,然后再导入mdc进行进一步的压缩。mdc的压缩,需要xtc格式的轨迹文件,以及一个pdb格式的拓扑文件。

bash 复制代码
$ ../bin/mdcompress compress -i Pro_lig17.xtc --topology Pro_lig.pdb -o Pro_lig17.mdc
Completed frame 1530, with number of atoms 3410
Compressing time: 0.571706s
Segment sizes:
    Segment 0: 46999 bytes
    Segment 1: 9031 bytes
    Segment 2: 3644163 bytes
    Segment 3: 65469 bytes
    Segment 4: 4173376 bytes
    Segment 5: 65985 bytes
    Segment 6: 1767515 bytes
    Segment 7: 65262 bytes
    Segment 8: 1322871 bytes
    Segment 9: 70045 bytes
    Segment 10: 903313 bytes
    Segment 11: 214015 bytes
    Frame desc: 12716 bytes
Segment ratios:
    Segment 0: 18.9036 bits/atom
    Segment 1: 23.6105 bits/atom
    Segment 2: 18.8845 bits/atom
    Segment 3: 20.1366 bits/atom
    Segment 4: 18.9095 bits/atom
    Segment 5: 20.2953 bits/atom
    Segment 6: 18.7844 bits/atom
    Segment 7: 20.0729 bits/atom
    Segment 8: 18.8473 bits/atom
    Segment 9: 20.3471 bits/atom
    Segment 10: 18.8928 bits/atom
    Segment 11: 20.7228 bits/atom

压缩效果:

bash 复制代码
总计 32824
drwxrwxr-x  3 dechin dechin     4096  7月 20 11:08 ./
drwxrwxr-x 13 dechin dechin     4096  7月 20 10:59 ../
-rw-rw-r--  1 dechin dechin 12447261  7月 20 11:08 Pro_lig17.mdc
-rw-rw-r--  1 dechin dechin 20710652  7月 20 11:05 Pro_lig17.xtc
-rw-rw-r--  1 dechin dechin   276336  7月 20 11:08 Pro_lig.pdb

从结果中可以看到,这个压缩率,在xtc的基础上,还能够进一步有60%左右的压缩率,这是一个非常优秀的成绩了。

总结概要

对于长效分子动力学模拟来说,如何去高效的存储分子轨迹,是一个重要的问题。前者有Gromacs的xtc格式文件,利用量化和比特级的压缩,将分子轨迹压缩到23%左右。而本文介绍的mdc工具,可以在xtc的基础上,再压缩到60%左右。

版权声明

本文首发链接为:https://dechinphy.github.io/posts/75aab8ab.html

作者ID:DechinPhy

更多原著文章请参考:https://dechinphy.github.io/

参考链接

  1. https://github.com/refresh-bio/mdcompress