0

I have a R code like this:

compute_enrichment <- function(dz_vec) {
        dz_vec <- dz_vec[!is.na(dz_vec)]
        n_module_genes <- length(intersect(module_genes,names(dz_vec)))
        module_genes_pct <- n_module_genes/length(module_genes)
        result <- list(escore=NA,norm_escore=NA,pvalue=NA,pct_module_genes=module_genes_pct)
        if (module_genes_pct >= MIN_PCT_MODULE_GENES) {
            result$escore <- abs(sum(dz_vec[module_genes],na.rm=T))
            rand_escores <- sapply(1:N_PERMUTATIONS, function(i) {
                abs(sum(sample(dz_vec,n_module_genes),na.rm=T))
            })
            result$norm_escore <- (result$escore - mean(rand_escores))/sd(rand_escores)
            result$pvalue <- length(which(rand_escores > result$escore))/length(rand_escores)
        }
        result
    }

I want to convert this code into Python. Is there some sort of script available for this? Little heads up to get started would be great. Thanks

4

3 に答える 3

4

一般的な翻訳の問題は難しいでしょう(そして私は自動翻訳メカニズムを知りません)、そして他の人が使用するために行った提案はrpy素晴らしいものです。

ただし、この特定のコードをPythonに変換する必要がある場合は、ベクトル化された操作が多く含まれていないため、このコードの作業が簡単になります。使用するパターンは次のとおりです。

  1. likeのコードはリスト内包表記になります(ただし、Pythonには存在しない、dz_vec <- dz_vec[!is.na(dz_vec)]に何を使用するかについての規則が必要であり、したがって、その場合をテストする方法が必要です)。NA
  2. length()になりlen()ます。
  3. sapplyリスト内包になります。
  4. meanのような関数sdはで利用できますnumpy(または自分で書くのに十分簡単です)。
于 2013-03-08T21:53:02.193 に答える
3

You don't need to convert it, you can call it from python using rpy

于 2013-03-08T21:41:31.247 に答える
3

My answer to this question is always: scriptify it, then invoke the script with python using subprocess. I like this approach (rather than installing RPy) because RPy won't work with all versions of R (which means recreating your installation if you're not lucky enough to be using the right version), and you won't have to install anything if your R script already runs.

于 2013-03-08T21:46:43.543 に答える