2023-07-09-【记录】安装生信的代码编写环境.md 13 KB


title: 【记录】安装生信的代码编写环境 urlname: -ji-lu--an-zhuang-sheng-xin-de-dai-ma-bian-xie-huan-jing date: 2023-07-09 20:01:33 index_img: https://api.limour.top/randomImg?d=2023-07-09 20:01:33

tags: ['生信', 'docker', 'vscode', 'rstudio', 'jupyter']

安装 vscode-web

  • 安装好 docker
  • QUIC 转 SOCKS5

    mkdir -p ~/app/vscode && cd ~/app/vscode && nano docker-compose.yml
    sudo docker-compose up -d && sudo docker-compose logs
    
    version: "2.1"
    services:
    code-server:
    image: linuxserver/code-server:latest
    container_name: code-server
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=Asia/Shanghai
      - PASSWORD=password
      - SUDO_PASSWORD=password
      - PROXY_DOMAIN=code-server.my.domain #optional
      - DEFAULT_WORKSPACE=/config/workspace #optional
    volumes:
      - ./config:/config
    ports:
      - 2441:8443
    restart: unless-stopped
    

    配置代理和中文

  • 打开Visual Studio Code,点击Manage,在列表中选择Settings

  • 在弹出的搜索框中输入"proxy",即可看到代理的配置项"Http:Proxy"

  • 宿主机获取docker0的ip: ip address | grep docker0

  • 然后docker内设置代理 http://docker0的ip:port 拓展内搜索zh-cn,安装中文界面拓展

    安装 conda

  • 回到WORKSPACE,ctrl+~ 调出终端

  • sudo sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list

  • sudo apt update

  • sudo apt install wget

  • 安装 conda

    安装nodejs

    conda create -n node -c conda-forge nodejs
    conda activate node
    npm config set registry https://registry.npmmirror.com
    

    使用git

    npm create astro@latest
    git config --global user.email "youremail"
    git config --global user.name "yourname"
    git branch -M main && git add . && git commit -m 'Initial commit'
    git remote add origin https://github.com/Limour-dev/chatGPT.git
    git push --set-upstream origin main --force # Creating a personal access token
    git config --global credential.helper cache
    git push
    

    hello world

    ---
    const search = Astro.url.searchParams.get('search')! || '';
    ---
    <h1>{search}</h1>
    
  • Enabling SSR in Your Project

  • 编辑 chatGPT/src/pages/index.astro

  • npm run dev

  • 访问 https://vscode.domain/proxy/3000/?search=hello%20world 进行测试

    附加 win下集成conda

  • 进入设置页面,搜索 terminal.integrated.profiles.windows

  • 编辑 setting.json, 给 PowerShell 中添加 args

  • args 的值可以通过查看 conda 快捷方式的属性来获取

    "PowerShell": {
    "source": "PowerShell",
    "icon": "terminal-powershell",
    "args": [
        "${env:windir}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
        "-ExecutionPolicy ByPass -NoExit -Command",
        "\"& '${env:USERPROFILE}\\miniconda3\\shell\\condabin\\conda-hook.ps1' ; conda activate '${env:USERPROFILE}\\miniconda3' \""
    ]
    }
    

    安装 Jupyter

    持久化镜像存储

    mkdir -p ~/datascience && cd ~/datascience
    nano docker-compose.yml
    sudo docker-compose up -d
    sudo docker-compose logs
    sudo docker cp -a jupyterR:/opt /home/limour/upload/opt
    sudo docker cp -a jupyterR:/home/jovyan /home/limour/upload/home
    sudo docker-compose down && sudo docker volume prune
    
    version: '3.3'
    services:
    datascience-notebook:
        ports:
            - '57002:8888'
        container_name: jupyterR
        restart: always
        image: 'jupyter/datascience-notebook:r-4.3.1'
        command: start-notebook.sh --NotebookApp.token='***'
    

    启动镜像

    nano docker-compose.yml
    sudo chmod 777 -R /home/limour/upload/
    sudo docker-compose up -d
    sudo docker-compose logs
    
    version: '3.3'
    services:
    datascience-notebook:
        ports:
            - '57002:8888'
        container_name: jupyterR
        restart: always
        volumes:
            - '/home/limour/upload:/home/jovyan/upload'
            - '/home/limour/upload/opt:/opt'
            - '/home/limour/upload/home:/home/jovyan'
        image: 'jupyter/datascience-notebook:r-4.3.1'
        command: start-notebook.sh --NotebookApp.token='***'
    
  • NPS内网穿透加NPM面板反代

    R包镜像

    nano .Rprofile
    options()$repos ## 查看使用install.packages安装时的默认镜像
    options()$BioC_mirror ##查看使用bioconductor的默认镜像
    
    options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/") ##指定镜像,这个是中国科技大学镜像
    options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")) ##指定install.packages安装镜像,这个是清华镜像
    options(ggrepel.max.overlaps = Inf)
    

    安装 R 内核

    conda create -n seurat -c conda-forge r-seurat=4.1.1 -y
    conda activate seurat
    conda install -c conda-forge r-tidyverse -y
    conda install -c conda-forge r-irkernel -y
    Rscript -e "IRkernel::installspec(name='seurat', displayname='r-seurat')"
    conda install -c conda-forge r-devtools -y
    Rscript -e "BiocManager::install('glmGamPoi')"
    wget -e "https_proxy=http://172.17.0.1:8580" https://github.com/chris-mcginnis-ucsf/DoubletFinder/archive/refs/heads/master.zip -O DoubletFinder-master.zip
    Rscript -e "devtools::install_local('DoubletFinder-master.zip')"
    

    安装 python 内核

    conda create -n markdown2pptx -c conda-forge python -y
    conda install -n markdown2pptx ipykernel -c conda-forge -y
    conda run -n markdown2pptx python -m ipykernel install --user --name markdown2pptx
    conda run -n markdown2pptx pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
    

    安装 Golang 内核

  • 项目地址 gophernotes

    conda create -n golang -c conda-forge go -y
    conda activate golang
    go env -w GO111MODULE=on
    go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/
    go install github.com/gopherdata/gophernotes@v0.7.5 #去仓库查看最新版本号
    mkdir -p ~/.local/share/jupyter/kernels/golang
    cd ~/.local/share/jupyter/kernels/golang
    cp "$(go env GOPATH)"/pkg/mod/github.com/gopherdata/gophernotes@v0.7.5/kernel/* "."
    chmod +w ./kernel.json
    sed "s|gophernotes|$(go env GOPATH)/bin/gophernotes|" < kernel.json.in > kernel.json
    

    安装 jupyter-ai 内核

  • 获取端点和令牌

  • 填写端点时尾部需要加上 /v1

    conda create -n jupyter-ai -c conda-forge jupyter-ai-magics -y
    conda install -n jupyter-ai -c conda-forge openai -y
    conda install -n jupyter-ai ipykernel -c conda-forge -y
    conda run -n jupyter-ai python -m ipykernel install --user --name jupyter-ai
    # pip install --upgrade jupyterlab -i https://pypi.tuna.tsinghua.edu.cn/simple # 升级到 JupyterLab 4
    # pip install jupyter_ai -i https://pypi.tuna.tsinghua.edu.cn/simple
    
  • 启用拓展并配置端点和令牌

    %load_ext jupyter_ai_magics
    %env OPENAI_API_BASE=https://xxx/v1
    %env OPENAI_API_KEY=sk-xxx
    
  • 示例

    %%ai chatgpt -f code
    解决汉诺塔问题的函数
    

安装 rstudio

mkdir -p ~/app/rstudio && cd ~/app/rstudio && nano docker-compose.yml
sudo docker-compose up -d && sudo docker-compose logs
version: '3'
services:
  rstudio:
    image: dceoy/rstudio-server
    container_name: Rstudio
    deploy:
      resources:
        limits:
          cpus: '0.50'
          memory: 500M
        reservations:
          cpus: '0.25'
          memory: 200M
    restart: always
    ports:
      - 57022:8787
    volumes:
      - /home/gene/zl_liu/rstudio:/home/rstudio
      - /home/gene/upload:/home/rstudio/upload
    working_dir: /home/rstudio

更改 R 版本

  • 安装 conda

    # 容器内
    conda create -n r_4_1_3 -c conda-forge r-base=4.1.3 -y
    conda activate r_4_1_3
    whereis R
    # /home/rstudio/miniconda3/envs/r_4_1_3/bin/R
    # 容器外
    docker exec -it Rstudio /bin/bash
    chmod 777 -R  /etc/rstudio/
    exit 
    nano -K /etc/rstudio/rserver.conf
    # Server Configuration File
    rsession-which-r=/home/rstudio/miniconda3/envs/r_4_1_3/bin/R
    sudo docker-compose restart
    

    安装 seurat

    # 进入terminal,以下操作均在terminal中进行
    export R_LIBS_SITE=""
    # 在terminal中进入R
    .libPaths('/home/rstudio/miniconda3/envs/r_4_1_3/lib/R/library')
    .libPaths() 确保没有其他路径
    remove.packages("vctrs")
    install.packages("vctrs")
    install.packages('Seurat')
    remove.packages("cli")
    install.packages("cli")
    install.packages("tidyverse")
    install.packages("plotly")
    重启R session
    library(tidyverse)
    library(Seurat)
    

    绘制 3D-umap

    library(plotly)
    library(Seurat)
    sample13 <- readRDS("~/upload/zl_liu/work/Prognosis/scRNA/sample13.rds")
    sample13 <- RunUMAP(sample13, dims = 1:10, n.components = 3L)
    plot.data <- FetchData(object = sample13, vars = c("UMAP_1", "UMAP_2", "UMAP_3", "seurat_clusters"))
    plot.data$label <- paste(rownames(plot.data))
    # Plot your data, in this example my Seurat object had 21 clusters (0-20)
    plot_ly(data = plot.data, 
        x = ~UMAP_1, y = ~UMAP_2, z = ~UMAP_3, 
        color = ~seurat_clusters, 
        colors = c("lightseagreen",
                   "gray50",
                   "darkgreen",
                   "red4",
                   "red",
                   "turquoise4",
                   "black",
                   "yellow4",
                   "royalblue1",
                   "lightcyan3",
                   "peachpuff3",
                   "khaki3",
                   "gray20",
                   "orange2",
                   "royalblue4",
                   "yellow3",
                   "gray80",
                   "darkorchid1",
                   "lawngreen",
                   "plum2",
                   "darkmagenta")[1:7],
        type = "scatter3d", 
        mode = "markers", 
        marker = list(size = 5, width=2), # controls size of points
        text=~label, #This is that extra column we made earlier for which we will use for cell ID
        hoverinfo="text") #When you visualize your plotly object, hovering your mouse pointer over a point shows cell names
    

安装 notebook

mkdir -p /home/limour/upload/home && echo `id -u gene`:`id -g gene`
mkdir -p ~/app/notebook && cd ~/app/notebook && nano docker-compose.yml
sudo docker-compose up -d && sudo docker-compose logs
version: '3'
services:
  notebook:
    image: jupyter/r-notebook
    user: "1001:1001"  # 替换为gene的UID和GID
    container_name: notebook
    restart: unless-stopped
    extra_hosts:
      - 'host.docker.internal:host-gateway'
    environment:
      - DOCKER_STACKS_JUPYTER_CMD=nbclassic
      - TZ=Asia/Shanghai
      - JUPYTER_GATEWAY_REQUEST_TIMEOUT=600
      - JUPYTER_GATEWAY_CONNECT_TIMEOUT=600
    ports:
      - '57002:8888'
    volumes:
      - '/home/limour/upload:/home/jovyan/upload'
      - '/home/limour/upload/home:/home/jovyan'
      - '/dev/shm:/home/jovyan/tmp'
    command: start-notebook.py --NotebookApp.token='***'
conda init
source activate
conda config --add envs_dirs /home/jovyan/upload/envs
conda create -n mamba conda-forge::mamba=1.5.7
source activate mamba
proxy_connect_timeout   600s;
proxy_read_timeout      600s;
proxy_send_timeout      600s;
proxy_buffering         off;

R

source activate mamba
mamba create -n THCA_24_03_14 conda-forge::r-base=4.3.3
mamba install -n THCA_24_03_14 conda-forge::r-irkernel=1.3.2
mamba run -n THCA_24_03_14 Rscript -e "IRkernel::installspec(name='THCA_24_03_14', displayname='THCA_24_03_14')"
source activate THCA_24_03_14

proxychains

source activate mamba
mamba install conda-forge::unzip
mkidr -p ~/dev && mkidr -p ~/etc && cd ~/dev
wget https://github.com/rofl0r/proxychains-ng/archive/refs/heads/master.zip
unzip master.zip
mv proxychains-ng-master proxychains-ng && cd proxychains-ng && make
cat << EOF >$HOME/etc/proxychains4.conf
[ProxyList]
socks5  `cat /etc/hosts | grep host.docker.internal | awk '{print $1}'` 8580
EOF
alias proxychains='$HOME/dev/proxychains-ng/proxychains4 -q -f $HOME/etc/proxychains4.conf'
proxychains curl https://b.limour.top

附加 windows端口映射

netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=57002 connectaddress=192.168.243.129 connectport=57002
netsh interface portproxy show v4tov4
  • 防火墙入站规则放行端口57002
  • 假设宿主物理机是 192.168.10.247, 其内部的虚拟机是 192.168.243.129
  • 局域网内其他物理机即可访问 http://192.168.10.247:57002/nbclassic/tree?token=xxx