apply(counts, 2, function(x) rpkm(x, lengths)) 错误:dim(X) 必须具有正长度

Error in apply(counts, 2, function(x) rpkm(x, lengths)) : dim(X) must have a positive length

我正在尝试使用脚本 tpm_rpkm.R 脚本。但我收到错误提示

Error in apply(counts, 2, function(x) rpkm(x, lengths)) : dim(X) must have a positive length.

(数据table应该没有任何错误,因为它是通过与脚本作者使用的相同程序生成的。)

这是脚本

#! /usr/bin/env Rscript

# Author: Andy Saurin (andrew.saurin@univ-amu.fr)
#
# Simple RScript to calculate RPKMs and TPMs
# based on method for RPKM/TPM calculations shown in http://www.rna-seqblog.com/rpkm-fpkm-and-tpm-clearly-explained/
#
# The input file is the output of featureCounts
#

rpkm <- function(counts, lengths) {
  pm <- sum(counts) /1e6
  rpm <- counts/pm
  rpm/(lengths/1000)
}

tpm <- function(counts, lengths) {
  rpk <- counts/(lengths/1000)
  coef <- sum(rpk) / 1e6
  rpk/coef
}


## read table from featureCounts output
args <- commandArgs(T)

tag <- tools::file_path_sans_ext(args[1])


cat('Reading in featureCounts data...')
ftr.cnt <- read.table(args[1], sep="\t", header=T, quote="") #Important to disable default quote behaviour or else genes with apostrophes will be taken as strings
cat(' Done\n')

if ( ncol(ftr.cnt) < 7 ) { 
    cat(' The input file is not the raw output of featureCounts (number of columns > 6) \n')
    quit('no')
}

lengths = ftr.cnt[,6]

counts <- ftr.cnt[,7:ncol(ftr.cnt)]

cat('Performing RPKM calculations...')

rpkms <- apply(counts, 2, function(x) rpkm(x, lengths) )
ftr.rpkm <- cbind(ftr.cnt[,1:6], rpkms)

rpkms <- apply(counts, 2, function(x) rpkm(x, lengths) )
ftr.rpkm <- cbind(ftr.cnt[,1:6], rpkms)
write.table(ftr.rpkm, file=paste0(tag, "_rpkm.txt"), sep="\t", row.names=FALSE, quote=FALSE)
cat(' Done.\n\tSaved as ')
cat ( paste0(tag, "_rpkm.txt", '\n') )

cat('Performing TPM calculations...')

tpms <- apply(counts, 2, function(x) tpm(x, lengths) )

ftr.tpm <- cbind(ftr.cnt[,1:6], tpms)

write.table(ftr.tpm, file=paste0(tag, "_tpm.txt"), sep="\t", row.names=FALSE, quote=FALSE)
cat(' Done.\n\tSaved as ')
cat ( paste0(tag, "_tpm.txt", '\n') )


quit('no')

命令输出:

Rscript tpm_rpkm.R 450-3-hard_filtered.featureCounts Reading in featureCounts data... Done Performing RPKM calculations...Error in apply(counts, 2, function(x) rpkm(x, lengths)) : dim(X) must have a positive length halt execution

我的特征数 table 看起来像这样:

基因 |铬 |开始|结束 |钢绞线 |长度 | 1_1 | NODE_1_length_59711_cov_84.026979_g0_i0 | 116 | 904 | + | 789 | 1981_2 | NODE_1_length_59711_cov_84.026979_g0_i0 |第1178章3514 | - | 2337 |第 2294 章NODE_1_length_59711_cov_84.026979_g0_i0 | 3618 | 4319 | + | 702 | 5021_4 | NODE_1_length_59711_cov_84.026979_g0_i0 | 4337 | 4921 | + | 585| 3201_5 | NODE_1_length_59711_cov_84.026979_g0_i0 | 4953 | 5906 | + |第954章第 799 章NODE_1_length_59711_cov_84.026979_g0_i0 | 5920 | 7056 | + |第1137章第 532 章NODE_1_length_59711_cov_84.026979_g0_i0 | 7061 | 8071 | + | 1011 |第761章【=50=】| NODE_1_length_59711_cov_84.026979_g0_i0 | 8068 | 8766 | + | 699 |第 188 话NODE_1_length_59711_cov_84.026979_g0_i0 | 8766 | 9656 | + | 891|第 217 话NODE_1_length_59711_cov_84.026979_g0_i0 | 9640 | 10710 | + | 1071 | 4081_11 | NODE_1_length_59711_cov_84.026979_g0_i0 | 10692 | 11348 | + |第657章第 162 话NODE_1_length_59711_cov_84.026979_g0_i0 | 11359 | 12282 | + |第924章342

有人知道怎么处理吗?

将计数定义更新为:

counts <- ftr.cnt[,7:ncol(ftr.cnt), drop=FALSE]

这应该确保它仍然是一个二维结构,apply 现在可以对其工作。