Linux备忘录

一些软件安装、环境配置之类的备忘录。

配置基本环境和源

WSL

打开windows power shell。一般win11在空白处右键就有“在终端打开”,或者按下Win+R,输入cmd。

在Power shell输入:

wsl --install  # 安装wsl

设置一下用户名密码。可以到Microsoft商店下载对应版本的Ubuntu,然后点击安装。

安装好了之后可以再次打开Power shell输入bash进入WSL,也可以用第三方的软件来进入local terminal。

一些其它Power shell命令:

wsl --list  # 查看wsl
wsl --unregister Ubuntu  # 卸载wsl
wsl --unregister Ubuntu  # 卸载wsl
wsl --export Ubuntu D:/wslbak.tar  # 导出wsl
wsl --import Ubuntu D:\wsl D:\wslbak.tar --version 2  # 导入wsl
ubuntu config --default-user xxx  # 设置默认用户

需要使用X11图形界面。

export DISPLAY=$(grep -m 1 nameserver /etc/resolv.conf | awk '{print $2}'):0.0  # 可以选择加入.bashrc

也可以通过ipconfig命令查找ipv4地址,然后DISPLAY设置为xxx.xxx.xxx.xxx:0.0

apt换源

清华大学开源软件镜像站 | Tsinghua Open Source Mirror

按照网站上的提示,将/etc/apt/sources.list替换为对应的源就行。比如Ubuntu-22.04

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak  # 先备份一下原始文件
sudo vim /etc/apt/sources.list

打开后删除所有内容,然后粘贴下面内容:

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse

# 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换
deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse
# deb-src http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse

保存并关闭,然后更新apt

sudo apt update

pip换源

修改 ~/.pip/pip.conf (没有就创建一个文件夹及文件)

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = https://pypi.tuna.tsinghua.edu.cn

安装conda

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh -b -p /path/to/installation

conda换源增加速度:

打开~/.condarc,输入

channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch
show_channel_urls: true
auto_activate_base: true

安装cuda

查看显卡驱动版本以及最高支持的cuda版本

nvidia-smi

NVIDIA官网下载安装包:CUDA Toolkit – Free Tools and Training | NVIDIA Developer

找到和操作系统合适的版本,下载runfile (local),一般会给一个地址,用wget命令下载。然后:

sudo sh cuda_xx.x.xxxxxx.run # 换成实际下载的名字

弹出来第一个输入accept。第二个让选择安装文件,如果已经有显卡驱动了,就只安装cudatoolkit就行,把driver取消掉。

安装成功后最后会输出一个路径,让你把这个路径加入环境变量,可以视情况把他加入环境变量就行。默认安装位置是/usr/local/cuda

下载对应版本的cudnn (Optional)

tar -xvJf cudnn-linux-x86_64-8.8.1.3_cuda12-archive.tar.xz
cd cudnn-linux-x86_64-8.8.1.3_cuda12-archive
sudo cp ./include/cudnn.h /usr/local/cuda/include
sudo cp ./lib/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*

gcc和g++版本问题

# 比如要用gcc-7
sudo apt install gcc-7
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 9  # 调整优先级
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 1
sudo update-alternatives --display gcc

# 同样g++
sudo apt install g++-7
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 9
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 1
sudo update-alternatives --display g++

# 查看版本
gcc --version
g++ --version

硬盘分区和挂载

fdisk -l  # 查看磁盘分区表
lsblk  # 列出所有可用的块设备信息

硬盘小于2T的用fdisk

sudo fdisk /dev/sda
m  # 菜单
n  # 创建新分区
p  # 主分区
1  # 分区编号
   # 直接回车使用起始扇区和结束扇区默认值
w  # 保存并退出

硬盘大于2T的用parted

sudo parted /dev/sda
print  # 查看分区表
mklabel gpt  # 创建gpt分区表
mkpart primary xfs 0% 100%  # 创建一个主分区,xfs或者ext4格式

重新读取分区表

partprobe /dev/sda

格式化

mkfs.xfs /dev/sda1

创建挂载点

mkdir /data01
mount /dev/sda1 /data01

系统启动自动挂载

lsblk -f  # 查看UUID,填入下面
vim /etc/fstab
UUID=xxxxxxxxxxxxxxxxxxxxxxx /data01 xfs defaults 0 0

防火墙

# 检查防火墙端口
firewall-cmd --query-port=<端口号>/tcp
# 开放防火墙端口
firewall-cmd --add-port=<端口号>/tcp --permanent
firewall-cmd --reload

电镜数据处理软件安装

安装RELION5

安装一些依赖:

如果是Debian、Ubuntu:

sudo apt install cmake git build-essential mpi-default-bin mpi-default-dev libfftw3-dev libtiff-dev libpng-dev ghostscript libxft-dev

如果是RHEL系的发行版,比如CentOS等:

sudo yum install cmake git gcc gcc-c++ openmpi-devel fftw-devel libtiff-devel libpng-devel ghostscript libXft-devel libX11-devel

下载源代码

git clone https://github.com/3dem/relion.git
# git checkout ver5.0  # 现在默认版本已经是5了,可以跳过这一步。也可以切换以前的版本
cd relion
conda env create -f environment.yml  # 如果是4.0以及更早的版本,跳过这一步
mkdir build
cd build
cmake ..  # 可以自定义安装位置,添加一个参数就行: -DCMAKE_INSTALL_PREFIX=/path/to/install/dir/
make  # make后面可以加参数-j 8加快编译速度,数字可以根据电脑配置更改
      # 如果报错,重新cmake,可以删除build里的内容然后make clean,重新cmake ..
make install

不报错的话relion默认安装在build/bin里了,把这个添加到环境变量,修改~/.bashrc:

cd ~
vim .bashrc
# 在里面添加下面这行,路径改成实际安装的路径
export PATH="/opt/relion/build/bin:$PATH"
# 保存并退出,然后source
source ~/.bashrc

这时候应该就可以运行relion,输入relion可以打开relion GUI。

relion

安装CryoSPARC

CryoSPARC官网申请LICENSE_ID。

下载cryosparc_master和cryosparc_worker。

LICENSE_ID="xxxxxxxxxxxxxx"
curl -L https://get.cryosparc.com/download/master-latest/$LICENSE_ID -o cryosparc_master.tar.gz
curl -L https://get.cryosparc.com/download/worker-latest/$LICENSE_ID -o cryosparc_worker.tar.gz
tar -xf cryosparc_master.tar.gz cryosparc_master
tar -xf cryosparc_worker.tar.gz cryosparc_worker

单机工作站安装方法(master和worker分开的安装方法参见官网

cd cryosparc_master

./install.sh    --standalone \ 
                --license $LICENSE_ID \ 
                --worker_path <worker path> \ 
                --ssdpath <ssd path> \ 
                --initial_email <user email> \
                --initial_username "<login username>" \
                --initial_firstname "<given name>" \
                --initial_lastname "<surname>" \
                [--port <port_number>] \
                [--initial_password <user password>] \
                [--dbpath <path to database>]
  1. worker_path是刚刚下载解压worker的绝对路径。

  2. ssd如果没有的话加上--nossd参数。

  3. [ ]是可选参数,路径写绝对路径。

  4. 账号密码什么的别忘了双引号。

  5. 默认的端口号是39000。不要多次安装使用相同端口。

  6. 配置合适的dbpath很重要,CryoSPARC的任务信息和图片都存储在数据库中(不包括数据处理过程,数据库崩溃可以任务还是可以重新导入回来),默认的话是在解压目录的cryosparc_database目录。备份数据库方法

  7. 如果安装v4.4.0之前的版本,需要另外指定cuda路径--cudapath <cuda path>

重载bash设置

source ~/.bashrc

然后就可以通过浏览器输入工作站的IP和端口http://<workstation_hostname>:39000访问cryosparc

通过cryosparcm命令管理cryosparc运行

cryosparcm status  # 查看状态
cryosparcm start  # 运行
cryosparcm stop  # 停止
cryosparcm restart  # 重启

关机前要停止cryosparc,开机重新运行。

安装AreTomo2

官网的安装方法

git clone https://github.com/czimaginginstitute/AreTomo2.git
cd AreTomo2
make exe -f makefile11 [CUDAHOME=path/cuda-xx.x]

个人安装遇到一些错误,修改了一些。

第一个是cutff.h找不到,

In file included from Util/CNextItem.cpp:1:
Util/CUtilInc.h:3:10: fatal error: cufft.h: No such file or directory
#include <cufft.h>
^~~~~~~~~

通过修改makefile11而不是指定CUDAHOME解决。

vim makefile11
# 把CUDAHOME改成自己的/usr/local/cuda。保存并退出
make exe -f makefile11

第二个问题是在编译的时候遇到错误,

/usr/bin/ld: failed to set dynamic section sizes: bad value
collect2: error: ld returned 1 exit status
make: *** [makefile:181: exe] Error 1

如下解决:

make clean
cd LibSrc/Mrcfile
make all
cd ../Util
make all
cd ../..
make exe -f makefile11

编译成功后AreTomo2的可执行文件就在当前目录下。

安装ctffind4

https://grigoriefflab.umassmed.edu/ctf_estimation_ctffind_ctftilt

下载,然后解压就有可执行文件,一般能直接用。也可以下载源代码手动编译。

安装IMOD

可以到IMOD官网下载最新版本或者别的版本。

wget https://bio3d.colorado.edu/imod/AMD64-RHEL5/imod_5.1.0_RHEL8-64_CUDA12.0.sh
# 装到/opt,也可以不加 -dir /opt 参数安装到默认位置
sudo sh imod_5.1.0_RHEL8-64_CUDA12.0.sh -dir /opt

安装cryoCARE

在RELION5中调用

git clone https://github.com/juglab/cryoCARE_pip.git
cd 
conda create -n cryocare_11 python=3.8 cudatoolkit=11.0 cudnn=8.0 -c conda-forge
conda activate cryocare_11
pip install tensorflow==2.4
pip install cryoCARE
# For CUDA 10
# conda create -n cryocare -c conda-forge -c anaconda python=3 keras-gpu=2.3.1
# conda activate cryocare
# pip install cryoCARE

为了能在relion中调用cryoCARE,创建几个脚本:

cryoCARE_train.py:

#!/bin/bash
source activate cryocare
CRYOCARE_BIN="/home/c319/anaconda3/envs/cryocare/bin"
${CRYOCARE_BIN}/cryoCARE_train.py $@

类似的创建cryoCARE_extract_train_data.py和cryoCARE_predict.py。用chmod +x更改可执行权限。然后就可以在RELION TOMO里调用这几个脚本。

如果单独使用

1、安装

For CUDA 11:

conda create -n cryocare_11 python=3.8 cudatoolkit=11.0 cudnn=8.0 -c conda-forge
conda activate cryocare_11
pip install tensorflow==2.4
pip install cryoCARE

For CUDA 10:

conda create -n cryocare -c conda-forge -c anaconda python=3 keras-gpu=2.3.1
conda activate cryocare
pip install cryoCARE

2、准备训练集

创建一个文件 train_data_config.json

{
  "even": [
    "/path/to/even.rec"
  ],
  "odd": [
    "/path/to/odd.rec"
  ],
  "mask": [
    "/path/to/mask.mrc"
  ],
  "patch_shape": [
    72,
    72,
    72
  ],
  "num_slices": 1200,
  "split": 0.9,
  "tilt_axis": "Y",
  "n_normalization_samples": 500,
  "path": "./"
}

参数:

  • "even":偶数tomograms的路径列表

  • "odd":奇数tomograms的路径列表,顺序要和偶数一致

  • "mask":可选用mask限制部分区域,不用的话可以不要

  • "patch_shape":不能小于64,64,64

  • "path":训练和检验结果保存路径

运行

 conda activate cryocare
 cryoCARE_extract_train_data.py --conf train_data_config.json

3、训练

创建一个文件 train_config.json

{
  "train_data": "./",
  "epochs": 100,
  "steps_per_epoch": 200,
  "batch_size": 16,
  "unet_kern_size": 3,
  "unet_n_depth": 3,
  "unet_n_first": 16,
  "learning_rate": 0.0004,
  "model_name": "model_name",
  "path": "./",
  "gpu_id": 0
}

参数:

  • "train_data":保存训练和检验结果的路径,和上一步path一样

  • "model_name":模型名字

  • "path":结果保存路径

  • "gpu_id":训练使用的GPU id,支持多GPU"gpu_id": [0,1,2,3]。也可以用环境变量CUDA_VISIBLE_DEVICES 指定。

运行

cryoCARE_train.py --conf train_config.json

结果是一个后缀为 .tar.gz 的压缩包,用于后续预测。

4、预测

创建一个文件 predict_config.json

{
  "path": "path/to/your/model/model_name.tar.gz",
  "even": "/path/to/even.rec",
  "odd": "/path/to/odd.rec",
  "n_tiles": [1,1,1],
  "output": "denoised.rec",
  "overwrite": false,
  "gpu_id": 0
}

参数:

  • "path":训练出来的model文件路径(刚刚那个tar.gz文件)

  • "even":偶数tomograms的路径列表

  • "odd":奇数tomograms的路径列表,顺序要和偶数一致

  • "output":结果输出路径

  • "gpu_id":同上

运行

cryoCARE_predict.py --conf predict_config.json

安装MemBrain-Seg

conda create -n membrain-seg python=3.9
conda activate membrain-seg
pip install membrain-seg
membrain  # validate installation

安装MemBrain

git clone https://github.com/CellArchLab/MemBrain.git
cd MemBrain
conda env create -f MemBrain_requirements_gpu.yml
conda activate MemBrain_GPU

安装crYOLO

# For CUDA 11
conda create -n cryolo pyqt=5 python=3 numpy=1.18.5 libtiff wxPython=4.1.1  adwaita-icon-theme 'setuptools<66' -c conda-forge -c anaconda 
conda activate cryolo
pip install nvidia-pyindex
pip install 'cryolo[c11]'

安装napari-boxmanager

# 这是官网的教程
conda create -n napari-cryolo python=3.10 napari=0.4.17 pyqt pip -c conda-forge
conda activate napari-cryolo
pip install napari-boxmanager

Link到cryolo的环境

conda activate cryolo
cryolo_dir=$(realpath $(dirname $(which cryolo_predict.py)))
napari_link_file=${cryolo_dir}/napari_boxmanager
conda activate napari-cryolo
echo -e "#\!/usr/bin/bash\nexport NAPARI_EXE=$(which napari)\nnapari_exe='$(which napari_boxmanager)'\n\${napari_exe} \"\${@}\""> ${napari_link_file}
chmod +x ${napari_link_file}
conda activate cryolo
# cbox2star.py
# Lai

import starfile
import numpy as np
import pandas as pd
import argparse
from pathlib import Path

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Convert crYOLO CBOX file to RELION5 TOMO STAR file.")
    parser.add_argument("-i", "--input", required=True, help="Input folder containing CBOX files.")
    parser.add_argument("-o", "--output", required=True, help="Output folder.")
    parser.add_argument("-p", type=float, required=True, help="Pixel size of tomograms")
    parser.add_argument("-x", type=float, required=True, help="X dimension of tomograms")
    parser.add_argument("-y", type=float, required=True, help="Y dimension of tomograms")
    parser.add_argument("-z", type=float, required=True, help="Z dimension of tomograms")
    parser.add_argument("-r", type=int, default=4, help="Remove prefix from tomogram name.")
    args = parser.parse_args()

    Path(args.output).mkdir(exist_ok=True)
    combine_list = []
    cbox_files = Path(args.input).glob("*.cbox")
    for cbox_file in cbox_files:
        df_cbox = starfile.read(cbox_file)["cryolo"]
        centered_coordinate_angst = ( df_cbox[["CoordinateX", "CoordinateY", "CoordinateZ"]].to_numpy() - np.array([args.x, args.y, args.z]) / 2 ) * args.pixel
        df_star = pd.DataFrame(centered_coordinate_angst, columns=["rlnCenteredCoordinateXAngst", "rlnCenteredCoordinateYAngst", "rlnCenteredCoordinateZAngst"])
        df_star.insert(0, "rlnTomoName", str(cbox_file.stem)[args.prefix:])
        combine_list.append(df_star)
    combined_df = pd.concat(combine_list, ignore_index=True)
    starfile.write({"particles":combined_df}, Path(args.output) / "particles_from_cbox.star")

安装EMAN2

先安装conda。

然后安装mamba,在base环境下:

conda install mamba

用mamba创建一个eman2的环境:

mamba create -n eman2 eman-dev --only-deps -c cryoem -c conda-forge
# 无GUI版本
# mamba create -n eman2 'eman-dev=*=nogui_*' --only-deps -c cryoem -c conda-forge

下载源码:

# mkdir -p /opt/eman2 && cd /opt/eman2
git clone https://github.com/cryoem/eman2.git

安装:

# cd /path/to/eman2
conda activate eman2
mkdir build && cd build
# 确保一些依赖没问题
# sudo apt update
# sudo apt install build-essential
# 安装openGL
# sudo apt-get install libgl1-mesa-dev freeglut3-dev
cmake /path/to/eman2/source/code -DENABLE_OPTIMIZE_MACHINE=ON
make -j 8
make install

安装Dynamo

Dynamo官网下载安装包。解压

tar -xf dynamo-v-1.1.157_MCR-9.0.0_GLNXA64.tar

Dynamo可以依赖Matlab运行,也有一个独立的运行程序。

source <DYNAMO_ROOT>/dynamo_activate_linux_shipped_MCR.sh
# 如果是使用服务器上独立的Matlab:
# source <DYNAMO_ROOT>/dynamo_activate_linux.sh
dynamo

Dynamo的常规发行版将包括GPU预编译的GPU可执行文件。由于它们需要链接到系统中可能不存在的库,建议重新编译它们。在DYNAMO_ROOT解压tar包后:

cd DYNAMO_ROOT/cuda
source config.sh

这将自动编辑文件夹中的makefile文件,告诉它CUDA在系统中的位置。还可以直接编辑makefile文本文件并确保链接到服务器上正确的CUDA库:

CUDA_ROOT=/usr/local/cuda

正确格式化makefile后,重新编译系统中的可执行文件。

make clean
make all

安装GAPSTOP

conda create -n gapstop
conda activate gapstop
# sudo apt install libopenmpi-dev openmpi-bin
conda install mpi4py
pip install "gapstop_tm@git+https://gitlab.mpcdf.mpg.de/bturo/gapstop_tm.git"

安装pytom_tm

如果环境中有cuda的话:

conda create -n pytom_tm python=3

如果环境没有cuda,或者想使用预设的cuda,出错的概率小一些,但是也需要占用一些存储,同时可能降低一定的性能:

conda create -n pytom_tm -c conda-forge python=3 cupy cuda-version=11.8

然后安装pytom,[plotting]的选项会同时安装matplotlib和seaborn图形库用于pytom_estimate_roc.py脚本的可视化输出,不影响核心算法。

conda activate pytom_tm
python -m pip install pytom-match-pick[plotting]

 

留下评论

您的邮箱地址不会被公开。 必填项已用 * 标注