【问题标题】:Running DESeq2 from rpy2从 rpy2 运行 DESeq2
【发布时间】:2021-08-12 22:17:09
【问题描述】:

我第一次尝试通过 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")

规范对象是&lt;class 'rpy2.robjects.methods.RS4'&gt;

这里一定有我遗漏的东西,如果有正确的方向,我们将不胜感激!

【问题讨论】:

    标签: python rpy2


    【解决方案1】:

    如果你打开 R 并输入:

    library(DESeq2)
    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)
    

    【讨论】:

    • 比我使用的 object.do_slot() 方法简单多了:),谢谢!
    猜你喜欢
    • 2017-06-08
    • 1970-01-01
    • 2017-02-18
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 2019-08-27
    • 2019-02-03
    • 1970-01-01
    相关资源
    最近更新 更多