1. channels 使用
需要注意的是做生信分析的童鞋使用 conda 環(huán)境時(shí)一定要特別注意 conda channels 的設(shè)置,濫用 channels 很有可能會(huì)導(dǎo)致你的軟件升降級(jí)(甚至環(huán)境)錯(cuò)亂。推薦設(shè)置如下(~/.condarc):
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
- defaults
show_channels_urls: true
- mirrors.tuna.tsinghua.edu.cn/anaconda 是清華大學(xué)提供了一個(gè) conda 的鏡像;
- mirrors.ustc.edu.cn/anaconda 是中科大 conda 鏡像,有需要的也可以使用;
- 生物信息軟件 channel: mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda。
從 2019.04 起清華大學(xué)和中科大宣布停止 Anaconda 鏡像服務(wù),但是出于教育科研機(jī)構(gòu)使用的前提,在 2019-05-15 清華大學(xué)又宣布重新恢復(fù)了 Anaconda 鏡像!
因此原來使用國內(nèi)鏡像的 conda 可以根據(jù)自身需求決定是否需要變更新的 channels:
channels:
- conda-forge
- bioconda
- main
- free
- r
- pro
- defaults
show_channels_urls: true
更多 Anaconda channels,可以參考:https://repo.continuum.io/pkgs/。
2. 軟件安裝使用
conda 環(huán)境下的軟件盡量使用 conda、pip 命令去安裝。但同時(shí)也產(chǎn)生了一個(gè)問題,即 conda 中安裝了 R,有些使用了 install.packages() , install_github , biocLite 等方式安裝的 R 包,在環(huán)境遷移的時(shí)候,這些包如何遷移?
3. 環(huán)境激活
conda 4.6.x 切換環(huán)境使用的是:
$ source activate bio-base
conda 4.7.x 后切換環(huán)境變成了:
# To activate this environment, use:
> conda activate bio-base
# To deactivate an active environment, use:
> conda deactivate
問題是,conda-4.7.x 使用推薦的命令切換環(huán)境,還要你 init 初始化一下 conda,不想 init 的話可以用回 4.6.x 的方式切換環(huán)境:
$ conda --version
conda 4.7.5
$ conda activate bio-base
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run
$ conda init <SHELL_NAME>
Currently supported shells are:
- bash
- fish
- tcsh
- xonsh
- zsh
- powershell
See 'conda init --help' for more information and options.
IMPORTANT: You may need to close and restart your shell after running 'conda init'.
$ source activate bio-base
(bio-base) shenweiyan@ecs-steven 10:49:59 /home/shenweiyan
$ which python
/Bioinfo/Pipeline/SoftWare/Anaconda3/envs/bio-base/bin/python
$ source deactivate bio-base
DeprecationWarning: 'source deactivate' is deprecated. Use 'conda deactivate'.
shenweiyan@ecs-steven 11:03:40 /home/shenweiyan
$ source activate bio-base
(bio-base) shenweiyan@ecs-steven 11:03:50 /home/shenweiyan
$ conda deactivate
(bio-base) shenweiyan@ecs-steven 11:03:57 /home/shenweiyan
$
4. 環(huán)境導(dǎo)出與恢復(fù)
使用 conda 命令安裝的包,都可以使用下面的命令導(dǎo)出依賴包/環(huán)境并批量恢復(fù):
# To create a requirements.txt file
# Gives you list of packages used for the environment
conda list
# Save all the info about packages to your folder
conda list -e > requirements.txt
# To export environment file
activate <environment-name>
conda env export > <environment-name>.yml
# For other person to use the environment
conda env create -f <environment-name>.yml
# Install via `conda` directly.
# This will fail to install all dependencies. If one fails, all dependencies will fail to install.
conda install --yes --file requirements.txt
# To go around issue above, one can iterate over all lines in the requirements.txt file.
while read requirement; do conda install --yes $requirement; done < requirements.txt
5. R 與 R 包安裝
R Essentials 軟件包包含 IRKernel 和 80 多種最流行的數(shù)據(jù)科學(xué) R 軟件包,包括 dplyr,shiny,ggplot2,tidyr,caret 和 nnet 等。
5.1 R 軟件
conda 安裝 R 有很多種方法,如可以通過 r=3.6.x,或者 r-base、r-irkernel、r-essentials 都可以。需要注意:
如果需要在 Anaconda 的 Jupyter Notebook 中使用 R,建議使用 conda install -c r r-irkernel 或者 conda install -c r r-essentials 的方式安裝,因?yàn)?conda install -c r r=3.6.x/r-base 默認(rèn)不會(huì)安裝 irkernel,而且先安裝的 r=3.6.x/r-base 可能與后安裝的 r-irkernel/r-essentials 產(chǎn)生沖突。
5.2 R 包
通過 conda 安裝的 R,在安裝 R 包時(shí),最好使用 conda 命令去安裝,conda 無法安裝的(如 github 的包)再考慮其他的安裝方式。
1. install.packages
install.packages() 所有 R 包:
> data = read.table("r-packages.txt", header=T, check.names=F, quote="")
> soft = as.vector(data[,1])
> install.packages(soft)
2. bioconductor
Bioconductor 鏡像:
- Bioconductor 鏡像源配置文件之一是
.Rprofile(linux 下位于~/.Rprofile)。在文末添加如下語句:
#清華大學(xué)開源鏡像
options(BioC_mirror="https://mirrors.tuna.tsinghua.edu.cn/bioconductor")
- 也可以在安裝過程中指定鏡像:
source("http://bioconductor.org/biocLite.R")
#指定一個(gè)離你最近的國內(nèi)鏡像
options(BioC_mirror="http://mirrors.ustc.edu.cn/bioc/")
biocLite("包名")
biocLite 包安裝:
- 在 R-3.5(Bioconductor-3.7) 前,Bioconductor 都是通過 biocLite 安裝相關(guān)的 R 包:
> source("http://bioconductor.org/biocLite.R")
> options(BioC_mirror="http://mirrors.tuna.tsinghua.edu.cn/bioconductor")
> data = read.table("r-biocLite.txt", header=T, check.names=F, quote="")
> head(data)
biocLite_Packages_Name
1 RSQLite
2 KEGGREST
3 png
4 Rcpp
5 digest
6 AnnotationDbi
> soft = as.vector(data[,1])
> biocLite(soft)
- 從 R-3.5(Bioconductor-3.8) 起,Bioconductor 開始使用 BiocManager 安裝 R 包:
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install()
3. github_install
GitHub 上的一些最新 R 包,可以使用 devtools 進(jìn)行安裝:
install.packages("devtools")
library(devtools)
install_github("jokergoo/ComplexHeatmap")
6. 特別注意的軟件
6.1 gcc
對(duì)于使用 conda install --yes --file requirements.txt 重裝某一個(gè)環(huán)境的所有軟件時(shí),如果軟件中包含了 gcc,安裝了 R 后,在使用 R 時(shí)會(huì)可能會(huì)引發(fā)錯(cuò)誤:
/path/to/SoftWare/Anaconda/v2/lib/R/bin/exec/R: /path/to/SoftWare/Anaconda/v2/lib/R/bin/exec/../../lib/../../libgomp.so.1: version `GOMP_4.0' not found (required by /path/to/SoftWare/Anaconda/v2/lib/R/bin/exec/../../lib/libR.so)
6.2 glibc
glibc 是 GNU 發(fā)布的 libc 庫,即 c 運(yùn)行庫。glibc 是 linux 系統(tǒng)中最底層的 api,幾乎其它任何運(yùn)行庫都會(huì)依賴于 glibc。glibc 除了封裝 linux 操作系統(tǒng)所提供的系統(tǒng)服務(wù)外,它本身也提供了許多其它一些必要功能服務(wù)的實(shí)現(xiàn)。由于 glibc 囊括了幾乎所有的 UNIX 通行的標(biāo)準(zhǔn),可以想見其內(nèi)容包羅萬象。而就像其他的 UNIX 系統(tǒng)一樣,其內(nèi)含的檔案群分散于系統(tǒng)的樹狀目錄結(jié)構(gòu)中,像一個(gè)支架一般撐起整個(gè)作業(yè)系統(tǒng)。在 GNU/Linux 系統(tǒng)中,其 C 函式庫發(fā)展史點(diǎn)出了 GNU/Linux 演進(jìn)的幾個(gè)重要里程碑,用 glibc 作為系統(tǒng)的 C 函式庫,是 GNU/Linux 演進(jìn)的一個(gè)重要里程碑。
有一些軟件對(duì)于 glibc 的版本會(huì)有要求,如 cnvnator-0.3.3 要求 glibc >= 2.14。雖然在 anaconda 中有很多 channel 都提供了 glibc,但千萬注意一定要注意不要輕易去安裝,否則有很大的概率會(huì)導(dǎo)致整個(gè)環(huán)境掛掉,甚至?xí)?dǎo)致當(dāng)前環(huán)境中的 conda、python 出現(xiàn)動(dòng)態(tài)庫混亂錯(cuò)誤,恢復(fù)起來相當(dāng)麻煩!
我在《一次"膽戰(zhàn)心驚"的真實(shí)集群運(yùn)維經(jīng)歷》記錄了 gblic 的一些集群吐血經(jīng)歷,感興趣的可以了解一下。
7. 什么時(shí)候使用 Anaconda
對(duì)于 Anaconda(conda) 軟件安裝以及依賴解決的原理,我對(duì)這個(gè)黑盒子表示一頭霧水。真實(shí)的情況是,如果在一個(gè)環(huán)境中安裝了幾百個(gè)軟件(包),再去新裝軟件,這時(shí)候 Anaconda 常常會(huì)卡在 Collecting package metadata 和 Solving environment 過程中,甚至一個(gè)晚上都沒法解決環(huán)境的依賴。<br />
conda 官方說他們?cè)?conda-4.7 中花了很多的精力去提升了 conda 的速度(參考官方文章:《How We Made Conda Faster in 4.7》),但從 4.6 升級(jí)到 4.7 過程很容易導(dǎo)致環(huán)境崩潰,修復(fù)起來極其麻煩(反正我折騰了一個(gè)晚上都沒能把我的 base 給恢復(fù)回來,吐血的經(jīng)歷)!


對(duì)于新手而言,Anaconda 的確是非常簡單易用,如果對(duì)于多用戶的服務(wù)器端,或者是提供公共使用的軟件庫是否需要采用 Anaconda,個(gè)人覺得的確需要慎重考慮一下,最起碼需要考慮:
- 是否需要根據(jù)流程劃分環(huán)境?如果每個(gè)流程都需要使用 Python+R+Perl,每個(gè)環(huán)境都安裝這三個(gè)軟件(包),如何避免空間浪費(fèi)?
- 需要考慮如何備份和恢復(fù)環(huán)境。萬一某個(gè)環(huán)境崩潰,有什么快速替代的方案而不影響業(yè)務(wù)。
或許還有更多的問題,這里沒有列出來,如果大家有什么更好的想法或者建議,也歡迎留言交流。