RNA 速率学习笔记

最近学习画单细胞的拟时序轨迹图,有一些收获。大家经常使用的一般是monocle,DPT等;RNA velocity是一个比较复杂的方法,它通过细胞转录时细胞内成熟mRNA和mRNA前体的含量来计算细胞接下来生长分化的趋势。也就是说,它在计算细胞拟时序时考虑了细胞内在的结构,而非通过细胞中基因的表达量。所以它需要用到原始的bam文件。我们今天来解析一下这个方法的安装和使用。

一、安装指导

http://velocyto.org/velocyto.py/install/index.html

1、python环境下 velocyto.py

包括:

  1. 一个命令行工具,用于从mapping的bam文件中得到的loom文件【主要是得到spliced mRNA count矩阵和unspliced mRNA count矩阵】
  2. 一个分析工具,用于对loom文件做后续的RNA velocity分析

代码:
通过pip或者conda安装(如果出现错误类型:ImportError: dlopen: cannot load any more object with static TLS;有可能是导入包的顺序的问题,尝试退出,重新进入,然后首先导入sklearn包):

conda install numpy scipy cython numba matplotlib scikit-learn h5py click
pip install -U --no-deps velocyto
#或者:
git clone https://github.com/velocyto-team/velocyto.py.git
cd velocyto.py
pip install -e .  #注意后面的这个点

veocyto的安装成功与否和python版本有关,必须3.6.0以上的版本。R语言的velocyto不能独立使用,必须在python版本下生成loom文件,才可以使用。

2、R环境下 velocyto.R

包括:

R版本的velocyto没有办法根据bam文件计算得到成熟mRNA的spliced count矩阵和mRNA前体的unspliced count矩阵的loom文件。但是据说可以使用dropEst计算得到loom文件。
[不过本人并没有尝试过,如果今后有机会尝试成功了,一定回来告诉大家。]

  1. 一个分析工具,用于对loom文件做后续的RNA velocity分析

代码:

library(devtools)
install_github("velocyto-team/velocyto.R")

且需要安装其他的包作为辅助:

library(Seurat)
library(tidyverse)
library(SeuratWrappers)

3、scvelo

scVelo,是Volker Bergen团队2020年发表在NBT上的一个计算RNA velocity的工具,和velocyto相比,它的可视化结果更易解读且美观——scVelo安装指南地址。它除了基于steady-state的模型计算RNA velocity,还提供了基于dynamic模型计算RNA velocity。
但是,它也只能提供loom文件后续的计算,所以,还是需要先安装velocyto.py

安装代码:

#尽量先使用第一个方法安装
pip install -U scvelo
#开发者版本
#or
pip install git+https://github.com/theislab/scvelo@develop
#or
git clone https://github.com/theislab/scvelo && cd scvelo
git checkout --track origin/develop
pip install -e .

二、使用指南

(1)通过bam文件生成loom文件

1、10X

Usage: velocyto run10x [OPTIONS] SAMPLEFOLDER GTFFILE

          Runs the velocity analysis for a Chromium 10X Sample

          10XSAMPLEFOLDER specifies the cellranger sample folder

          GTFFILE genome annotation file

        Options:
          -s, --metadatatable FILE        Table containing metadata of the various samples (csv fortmated rows are samples and cols are entries)
          -m, --mask FILE                 .gtf file containing intervals to mask
          -l, --logic TEXT                The logic to use for the filtering (default: Default)
          -M, --multimap                  Consider not unique mappings (not reccomended)
          -@, --samtools-threads INTEGER  The number of threads to use to sort the bam by cellID file using samtools
          --samtools-memory INTEGER       The number of MB used for every thread by samtools to sort the bam file
          -t, --dtype TEXT                The dtype of the loom file layers - if more than 6000 molecules/reads per gene per cell are expected set uint32 to avoid truncation (default run_10x: uint16)
          -d, --dump TEXT                 For debugging purposes only: it will dump a molecular mapping report to hdf5. --dump N, saves a cell every N cells. If p is prepended a more complete (but huge) pickle report is printed (default: 0)
          -v, --verbose                   Set the vebosity level: -v (only warinings) -vv (warinings and info) -vvv (warinings, info and debug)
          --help                          Show this message and exit.

例子:

velocyto run10x  -m  mypath/GRCh38_rmsk.gtf  mypath2/Sample_01  somepath/refdata-gex-GRCh38-2020-A/genes/genes.gtf

其中:

  1. -m:是遮蔽指令,GRCh38_rmsk.gtf 是需要mark注释文件。
    可以从UCSA网站下载得到:hg38_rmsk.gtf(人类);
    可以直接点击链接:UCSC_hg38;然后点击get_output

    UCSC文件下载格式

  2. mypath2/Sample_01是cellranger 输出文件,Sample_01文件夹下包含一个outs文件夹,里面存储了bam文件。

  3. genes.gtf是cellranger用的基因组注释gtf文件,位于reference目录下的gene文件夹

注:

  1. 需要1.6以上版本的samtools,在命令行输入samtools --version 可以获取当前环境的版本信息。否则会出现以下错误提醒。
MemoryError: bam file #0 could not be sorted by cells.
                This is probably related to an old version of samtools, please install samtools >= 1.6.                
In alternative this could be a memory error, try to set the - your system.   Otherwise sort manually by samtools ``sort -l [compression] -m [mb_to_use]M -t [tagname] -O BAM -@ [threads_to_use] -o cellsorted_[bamfile] [bamfile]

输出:
在cellranger输出文件夹中,会出现一个velocyto文件夹,里面存储了该样本的loom文件:Sample_01.loom

(2)loom文件读取、输出、整合

1、python

#合并loom文件;
#somepath代表文件所存储的路径
import loompy as loompy
files = ["somepath/Sample_01.loom","somepath2/Sample_02.loom"]
loompy.combine(files, 'somepath/merge.loom',key="Accession")
#读取loom文件
import velocyto as vcy
vlm = vcy.VelocytoLoom("somepath/merge.loom")
2、R

读取:

library(velocyto.R)
library(Seurat)
library(SeuratWrappers)
#读取loom文件
input_loom <- "/velocyto/merge.loom"
sample.loom <- read.loom.matrices(input_loom,engine = "hdf5r")

可以将loom文件整合到seurat文件中,接下来直接在R中利用seurat对象分析

#'一定要注意,seurat对象中的细胞和基因一定要和loom文件中一致
seurat.obj[["spliced"]] <- CreateAssayObject(counts = sample.loom$spliced)
seurat.obj[["unspliced"]] <- CreateAssayObject(counts = sample.loom$unspliced)
seurat.obj[["ambiguous"]] <- CreateAssayObject(counts = sample.loom$ambiguous) 

输出:转化为H5格式输出

#
library(SeuratDisk)
SaveH5Seurat(seurat.obj, filename = "/velocyto/seurat_loom.h5Seurat")
Convert("/velocyto/seurat_loom.h5Seurat", dest = "h5ad")

(3)计算RNA速率

1、python

这里展示直接通过scvelo软件的分析

scvelo

import sklearn; sklearn.show_versions()
import scvelo as scv
adata = scv.read("/velocyto/seurat_loom.h5ad")
adata
#数据统计
scv.pl.proportions(adata, groupby='cellType',  highlight='unspliced',  show=True, save="/merge_data_statistics.pdf")
#对数据进行过滤
scv.pp.filter_and_normalize(adata, min_shared_counts=20, n_top_genes=2000)
scv.pp.moments(adata, n_pcs=30, n_neighbors=30)
scv.tl.velocity(adata)
scv.tl.velocity_graph(adata)
#流形
scv.pl.velocity_embedding_stream(adata, basis="umap",figsize = (14,10),color="cellType",show = True,save = "/merge_data_velo_stream.pdf")
#arrow
scv.pl.velocity_embedding(adata,arrow_length = 3,color="cellType",arrow_size = 2,dpi = 120,save = "/merge_data_velo_arrow.pdf")

2、R

#输入,前面生成的seurat_loom文件 
#速率分析
velo <- RunVelocity(seurat.obj, deltaT = 1, kCells = 25, fit.quantile = 0.02, 
                    spliced.average = 0.2, unspliced.average = 0.05, ncores = 18)
#全局速率可视化
emb = Embeddings(velo, reduction = "umap")
vel = Tool(velo, slot = "RunVelocity")
#可视化结果
show.velocity.on.embedding.cor(emb = emb, vel = vel, n = 200, scale = "sqrt", 
                               #cell.colors = ac(cell.colors, alpha = 0.5), 
                               cex = 0.8, arrow.scale = 3, 
                               show.grid.flow = TRUE, min.grid.cell.mass = 0.5, grid.n = 40, 
                               arrow.lwd = 1, do.par = FALSE, cell.border.alpha = 0.1)

你可能感兴趣的:(RNA 速率学习笔记)