运行 来自 rpy2 的 DESeq2
Running DESeq2 from rpy2
我第一次尝试通过 rpy2 运行 DESeq2,但遇到了一些困难。
class py_DESeq2:
def __init__(self, count_matrix):
self.dds = None
self.normalized_count_matrix = None
self.vsd = None
self.count_matrix = robjects.conversion.py2rpy(count_matrix)
self.design_matrix = robjects.conversion.py2rpy(pd.DataFrame({'treatment':['ctrl' for i in range(count_matrix.shape[1])]}))
self.design_formula = Formula('~ 1')
def norm_counts(self, **kwargs):
self.dds = deseq.DESeqDataSetFromMatrix(countData=self.count_matrix, colData=self.design_matrix, design=self.design_formula)
self.vsd = deseq.varianceStabilizingTransformation(self.dds, blind=True)
self.normed_count_matrix = deseq.assay(self.vsd)
self.normed_count_matrix = to_dataframe(self.normed_count_matrix)
self.normed_count_matrix = robjects.conversion.rpy2py(self.normed_count_matrix)
我在 self.normed_count_matrix = deseq.assay(self.vsd)
收到以下错误:
module 'DESeq2' has no attribute 'assay'
R 中的以下代码 运行 没问题:
library(DESeq2)
countData <- read.delim("0.333404867983521.R.data.in.txt")
colData <- read.delim("0.333404867983521.R.groups.in.txt")
dds <- DESeqDataSetFromMatrix(countData, colData,design=~Treatment,tidy=TRUE)
norm <- varianceStabilizingTransformation(dds,blind=TRUE)
norm_matrix <- assay(norm)
norm_df <- data.frame(Gene=rownames(norm_matrix), norm_matrix)
write.table(norm_df, "0.333404867983521.R.data.out.txt", row.names = FALSE,sep="\t")
规范对象是<class 'rpy2.robjects.methods.RS4'>
。
这里一定有我遗漏的地方,不胜感激!
如果您打开 R 并输入:
library(DESeq2)
assay
你会看到 assay 函数实际上并不是来自 DESeq2,而是来自它的依赖项 SummarizedExperiment
:
> assay
standardGeneric for "assay" defined from package "SummarizedExperiment"
function (x, i, withDimnames = TRUE, ...)
standardGeneric("assay")
<bytecode: 0x5586a354db90>
<environment: 0x5586a3535e20>
Methods may be defined for arguments: x, i
Use showMethods("assay") for currently available ones.
您可以通过在 R:
中使用显式命名空间来确认 assay
不是 DESeq2
的一部分
> DESeq2::assay
Error: 'assay' is not an exported object from 'namespace:DESeq2'
并确认它确实是 SummarizedExperiment
的一部分:
> SummarizedExperiment::assay
standardGeneric for "assay" defined from package "SummarizedExperiment"
因此在 rpy2 中你可以这样使用它:
from rpy2.robjects.packages import importr
summarized_experiment = importr('SummarizedExperiment')
summarized_experiment.assay(self.vsd)
我第一次尝试通过 rpy2 运行 DESeq2,但遇到了一些困难。
class py_DESeq2:
def __init__(self, count_matrix):
self.dds = None
self.normalized_count_matrix = None
self.vsd = None
self.count_matrix = robjects.conversion.py2rpy(count_matrix)
self.design_matrix = robjects.conversion.py2rpy(pd.DataFrame({'treatment':['ctrl' for i in range(count_matrix.shape[1])]}))
self.design_formula = Formula('~ 1')
def norm_counts(self, **kwargs):
self.dds = deseq.DESeqDataSetFromMatrix(countData=self.count_matrix, colData=self.design_matrix, design=self.design_formula)
self.vsd = deseq.varianceStabilizingTransformation(self.dds, blind=True)
self.normed_count_matrix = deseq.assay(self.vsd)
self.normed_count_matrix = to_dataframe(self.normed_count_matrix)
self.normed_count_matrix = robjects.conversion.rpy2py(self.normed_count_matrix)
我在 self.normed_count_matrix = deseq.assay(self.vsd)
收到以下错误:
module 'DESeq2' has no attribute 'assay'
R 中的以下代码 运行 没问题:
library(DESeq2)
countData <- read.delim("0.333404867983521.R.data.in.txt")
colData <- read.delim("0.333404867983521.R.groups.in.txt")
dds <- DESeqDataSetFromMatrix(countData, colData,design=~Treatment,tidy=TRUE)
norm <- varianceStabilizingTransformation(dds,blind=TRUE)
norm_matrix <- assay(norm)
norm_df <- data.frame(Gene=rownames(norm_matrix), norm_matrix)
write.table(norm_df, "0.333404867983521.R.data.out.txt", row.names = FALSE,sep="\t")
规范对象是<class 'rpy2.robjects.methods.RS4'>
。
这里一定有我遗漏的地方,不胜感激!
如果您打开 R 并输入:
library(DESeq2)
assay
你会看到 assay 函数实际上并不是来自 DESeq2,而是来自它的依赖项 SummarizedExperiment
:
> assay
standardGeneric for "assay" defined from package "SummarizedExperiment"
function (x, i, withDimnames = TRUE, ...)
standardGeneric("assay")
<bytecode: 0x5586a354db90>
<environment: 0x5586a3535e20>
Methods may be defined for arguments: x, i
Use showMethods("assay") for currently available ones.
您可以通过在 R:
中使用显式命名空间来确认assay
不是 DESeq2
的一部分
> DESeq2::assay
Error: 'assay' is not an exported object from 'namespace:DESeq2'
并确认它确实是 SummarizedExperiment
的一部分:
> SummarizedExperiment::assay
standardGeneric for "assay" defined from package "SummarizedExperiment"
因此在 rpy2 中你可以这样使用它:
from rpy2.robjects.packages import importr
summarized_experiment = importr('SummarizedExperiment')
summarized_experiment.assay(self.vsd)