文章目录

  • 1.创建环境
  • 2.安装、配置Jupyter notebook
  • 3.配置ipyparallel
  • 4. 远程链接Jupyter notebook
  • 5.安装python包
  • R Studio中升级R
  • 6.R包安装
  • 向Jupyter中添加conda虚拟环境
  • seurat降级


1.创建环境

#查看存在的内核
jupyter-kernelspec list
#添加一个内核
python -m ipykernel install --name py38
#删除一个内核
jupyter kernelspec remove py38
#列出所有环境
conda info --envs

#创建环境,安装py3.8, R4.0.3
conda create -n zpy38 python=3.8
conda activate zpy38
conda install -c conda-forge r-base
conda install -c conda-forge/label/gcc7 r-base
conda deactivate

#查看系统当前R版本
which R
#利用conda搜索R
conda search r-base

#安装R语言
conda install -c conda-forge -y r-base=4.0.0
#再次搜索默认R
which R
~/miniconda3/bin/R
#卸载R
conda uninstall r-base

2.安装、配置Jupyter notebook

#安装
conda install -n zpy38 jupyter notebook
  • 生成一个notebook配置文件
    默认情况下,配置文件 ~/.jupyter/jupyter_notebook_config.py 并不存在,需要自行创建。使用下列命令生成配置文件。如果文件已存在,恢复为默认设置。
    注意:在notebook的配置目录下无法正常开启Jupyter notebook。
jupyter notebook --generate-config
  • l.生成密码
    从 Jupyter notebook 5.0 版本开始,提供了一个命令来设置密码:jupyter notebook password,生成的密码密文存储在 jupyter_notebook_config.json
jupyter notebook password
## Enter password:  **** 
## Verify password: ****
[NotebookPasswordApp] Wrote hashed password to /public/home/xxx/.jupyter/jupyter_notebook_config.json
  • passwd()生成密匙,保存密匙,后面有用
ipython

In [1]: from notebook.auth import passwd
In [2]: passwd()
Enter password:
Verify password:
Out[2]: 'argon2:$argon2id$v=19$m=10240,t=10,p=8$hJgNAjLqiKiI9p7Ys7tymg$CmDCTHmcOryMsxIHNzhSFA'

输入exit()退出

  • 修改配置文件
    #/public/home/xxxx/.jupyter/jupyter_notebook_config.py
    jupyter_notebook_config.py 中找到下面的行,取消注释并修改。
cd /public/home/xxx/.jupyter #Jupyter访问路径
  c.NotebookApp.ip='localhost' # 生成我们想要的目标ip
  c.NotebookApp.password = u'argon2:$argon2id$v=19$m=10240,t=10,p=8$hJgNAjLqiKiI9p7Ys7tymg$CmDCTHmcOryMsxIHNzhSFA' # 上一步产生的密文
  c.NotebookApp.open_browser = False # 目的打开jupyternotebook时候不要打开网页。
  c.NotebookApp.port = 8080 # 可自行指定一个端口, 访问时使用该端口
#建议设置一个不常用的端口作为默认端口,之后打开Jupyter notebook就不会因为端口占用的问题换用端口。

3.配置ipyparallel

由于缺少ipyparallel,可能无法正常打开 Jupyter notebook。解决方法如下:

  • 安装ipyparallel
conda install -n zpy38 ipyparallel

启用ipyparallel

conda activate zpy38

jupyter serverextension enable --py ipyparallel --user
jupyter nbextension install --py ipyparallel --user 
jupyter nbextension enable --py ipyparallel --user
  • R安装iRkernel
    (1)
install.packages(c('repr', 'IRdisplay', 'IRkernel'), type = 'source', repos= "https://mirrors.tuna.tsinghua.edu.cn/CRAN/")
  
  # 用来设置IRkernel,使得可以在jupyte notebook使用R
  IRkernel::installspec()

(2)

#docker images里用这个
#devtools::install_github('IRkernel/IRkernel')
install.packages("IRkernel")
IRkernel::installspec(name = 'ir40', displayname = 'R 4.0', user = F)
  • 更换内核中的python版本
jupyter-kernelspec list
#进入需要修改的内核路径
cd /opt/conda/share/jupyter/kernels/python3
#找到kernel.json文件,修改"/opt/conda/envs/py38/bin/python"对应版本路径
{
 "argv": [
  "/opt/conda/envs/py38/bin/python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python 3",
 "language": "python"
}

4. 远程链接Jupyter notebook

准备工作完成后,我们就可以在本地使用服务器的计算资源了。由于一些服务器只能由登陆节点传输数据,如果我们想连接计算节点的Jupyter notebook,就需要将计算节点的端口映射到登陆节点,再由登陆节点映射到本地。

  • 登陆服务器
    通过ssh远程登陆服务器,用-L参数将登陆节点映射到本地端口。
    ssh -L 本地端口:目标ip:目标端口
ssh -L8080:localhost:2999 xxx@xxx.xxx.x.xx
ssh -L2999:localhost:8566 f2
source activate zpy38
jupyter notebook --port=8566
http://localhost:8080

5.安装python包

  • 查看python包安装位置
import sys
print(sys.path)

#查看python版本
import sys
sys.version
  • 安装pip

网址:https://pip.pypa.io/en/stable/installing/

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py

#更新pip
pip install --upgrade pip
#更新conda
conda update conda
  • 从GitHub安装
pip install git+https://github.com/theislab/scvelo

##
git clone https://github.com/theislab/scvelo
pip install -e scvelo
  • 部分常用python包安装
pip list

pip3 install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install tensorflow
pip --default-timeout=100 install tensorflow -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
pip --default-timeout=100 install stlearn -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python
pip --default-timeout=100 install torch -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
  • 使用豆瓣快速安装
#使用豆瓣快速安装
pip --default-timeout=100 install jupyter_contrib_nbextensions -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com 
#
pip install opencv-python -i https://pypi.douban.com/simple
#如果使用豆瓣出错
pip install 库名称 -i https://pypi.douban.com/simple --trust -host=pypi.douban.com

#其他pip常用国内镜像网站
阿里云:http://mirrors.aliyun.com/pypi/simple/
中国科技大学:https://pypi.mirrors.ustc.edu.cn/simple/
清华大学:https://pypi.tuna.tsinghua.edu.cn/simple/
华中科技大学:http://pypi.hustunique.com/
山东理工大学:http://pypi.sdutlinux.org/ 
豆瓣:http://pypi.douban.com/simple/

R Studio中升级R

# 升级R
install.packages("installr")
library(installr)
updateR()

devtools::install_github("YuLab-SMU/clusterProfiler",force = TRUE)
packageVersion('clusterProfiler')
# kegg 网络问题
 install.packages("R.utils")
R.utils::setOption("clusterProfiler.download.method",'auto')

6.R包安装

查看R包安装的位置

.libPaths()

已安装的R包

installed.packages()

查看已安装的R包版本

sessionInfo()

可安装的R包

available.packages()

卸载R包

remove.packages("ggplot2")
  • install.packages()安装
#指定国内镜像源
#如果没有指定,选择17,广州
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
install.packages("testthat")
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
install.packages("NNLM",INSTALL_opts = '--no-lock')
  • 安装Bioconductor上的R包
#指定一个离你最近的国内镜像
options(BioC_mirror="http://mirrors.ustc.edu.cn/bioc/")
library('BiocManager', quietly = TRUE)
BiocManager::install("units")
  • devtools
library(devtools)
install_github('jbergenstrahle/STUtility')

devtools::install_github('satijalab/seurat-data')
  • 本地安装包
install.packages("C:/Users/12580/Downloads/ggplot2_3.3.2.tar.gz",repos = NULL)

git clone https://github.com.cnpmjs.org/renozao/xbioc
install.packages("/home/R/lib/xbioc",repos = NULL)
  • 使用conda 安装
conda install r-hdf5r
conda install r-sf

向Jupyter中添加conda虚拟环境

使用命令jupyter kernelspec remove 'kernelname'可以删除指定的kernel
使用命令jupyter kernelspec list可以查看jupyter所有的kernel
利用conda创建了虚拟环境,但是启动jupyter notebook之后却找不到虚拟环境。实际上是由于在虚拟环境下缺少kernel.json文件。解决方案如下:

首先安装ipykernel:conda install ipykernel

在虚拟环境下创建kernel文件:conda install -n 环境名称 ipykernel

如果经常需要用jupyter notebook,那么最好在创建虚拟环境的时候便安装好ipykernel:conda create -n 环境名称 python=3.9 ipykernel

激活conda环境:conda activate 环境名称

将环境写入notebook的kernel中:

python -m ipykernel install --user --name 环境名称 --display-name "你想为kernel添加的名称"
python -m pip install jupyter_nbextensions_configurator
seurat降级
remove.packages('Seurat')
pkgs = c( 'mixtools', 'lars', 'dtw', 'doSNOW', 'hdf5r' ) 
#pkgs=c('jackstraw','slingshot')
BiocManager::install(pkgs,ask = F,update = F)
packageurl <- "https://cran.r-project.org/src/contrib/Archive/Seurat/Seurat_2.3.4.tar.gz"
install.packages(packageurl, repos=NULL, type="source")
library(Seurat)