as.vector(x) 中的错误:没有将此 S4 class 强制转换为向量的方法
Error in as.vector(x) : no method for coercing this S4 class to a vector
我正在尝试 运行 在 bash(我使用的是 CentOS 8)的命令行上使用以下命令创建 R 脚本:
cat 1_myScript.R | R --slave --args $SAMPLE"_x" $SAMPLE"_y"
其中 $SAMPLE 是我在 R 脚本中指定的参数,如下所示
> args<-commandArgs()
> aaa<-args[4]
此语法对我的所有脚本都有效,但现在出现以下错误:
Error in as.vector(x) : no method for coercing this S4 class to a vector
Calls: setdiff -> setdiff.default -> -> as.vector
Execution halted
奇怪的是,如果我尝试在 R 控制台中 运行 这个脚本
>source("1_myScript.R")
它继续没有错误。我查了一下,它似乎是一个链接到我在脚本中使用的库“GenomicRanges”的功能。
这是我的脚本的主体(请注意,我不知道它失败的确切行):
#!/usr/bin/env Rscript
library(rtracklayer)
library(data.table)
library(tidyverse)
#args <- commandArgs()
#aaa<-args[4]
aaa<-gsub("_T.finalSorted.bam_CNVs","",args[1])
targDir<-"/srv/ngsdata/dalteriog/SV_analysis/example/WGS_NB_novogene/CNVDir/"
dataTable <-fread((paste0(targDir,args[2]), header=TRUE)
ratio<-data.frame(dataTable)
dataTable <-fread(paste0(targDir,args[1]), header=FALSE)
cnvs<- data.frame(dataTable)
ratio$Ratio[which(ratio$Ratio==-1)]=NA
cnvs.bed=GRanges(cnvs[,1],IRanges(cnvs[,2],cnvs[,3])) # primo ogetto del GRange obj --> chr; secondo --> range
ratio.bed=GRanges(ratio$Chromosome,IRanges(ratio$Start,ratio$Start),score=ratio$Ratio) # score è un metadata
overlaps <- subsetByOverlaps(ratio.bed,cnvs.bed) # regioni overlappanti i due df
normals <- setdiff(ratio.bed,cnvs.bed) # regioni diverse
normals <- subsetByOverlaps(ratio.bed,normals) # la stessa cosa, ma con lo score associato
#mu <- mean(score(normals),na.rm=TRUE)
#sigma<- sd(score(normals),na.rm=TRUE)
#hist(score(normals),n=500,xlim=c(0,2))
#hist(log(score(normals)),n=500,xlim=c(-1,1))
#shapiro.test(score(normals)[which(!is.na(score(normals)))][5001:10000])
#qqnorm (score(normals)[which(!is.na(score(normals)))],ylim=(c(0,10)))
#qqline(score(normals)[which(!is.na(score(normals)))], col = 2)
#shapiro.test(log(score(normals))[which(!is.na(score(normals)))][5001:10000])
#qqnorm (log(score(normals))[which(!is.na(score(normals)))],ylim=(c(-6,10)))
#qqline(log(score(normals))[which(!is.na(score(normals)))], col = 2)
numberOfCol=length(cnvs)
for (i in c(1:length(cnvs[,1]))) {
values <- score(subsetByOverlaps(ratio.bed,cnvs.bed[i])) #score bayesiano della iesima CNV che overlappa con il file ratio
#wilcox.test(values,mu=mu)
W <- function(values,normals){resultw <- try(wilcox.test(values,score(normals)), silent = TRUE)
if(class(resultw)=="try-error") return(list("statistic"=NA,"parameter"=NA,"p.value"=NA,"null.value"=NA,"alternative"=NA,"method"=NA,"data.name"=NA)) else resultw}
KS <- function(values,normals){resultks <- try(ks.test(values,score(normals)), silent = TRUE)
if(class(resultks)=="try-error") return(list("statistic"=NA,"p.value"=NA,"alternative"=NA,"method"=NA,"data.name"=NA)) else resultks}
#resultks <- try(KS <- ks.test(values,score(normals)), silent = TRUE)
# if(class(resultks)=="try-error") NA) else resultks
cnvs[i,numberOfCol+1]=W(values,normals)$p.value
cnvs[i,numberOfCol+2]=KS(values,normals)$p.value
}
if (numberOfCol==5) {
names(cnvs)=c("chr","start","end","copy number","status","WilcoxonRankSumTestPvalue","KolmogorovSmirnovPvalue")
}
if (numberOfCol==7) {
names(cnvs)=c("chr","start","end","copy number","status","genotype","uncertainty","WilcoxonRankSumTestPvalue","KolmogorovSmirnovPvalue")
}
if (numberOfCol==9) {
names(cnvs)=c("chr","start","end","copy number","status","genotype","uncertainty","somatic/germline","precentageOfGermline","WilcoxonRankSumTestPvalue","KolmogorovSmirnovPvalue")
}
cnvs$Wfdr <- p.adjust(cnvs$WilcoxonRankSumTestPvalue, method="BH",n=nrow(cnvs))
cnvs$KSfdr <- p.adjust(cnvs$KolmogorovSmirnovPvalue, method="BH",n=nrow(cnvs))
cnvs<-subset(cnvs, Wfdr <= 0.05 & KSfdr <= 0.05)
samp<-gsub("_T.finalSorted.bam_CNVs","",aaa)
cnvs<-add_column(cnvs, Sample=samp, .before=1)
write.table(cnvs, file=paste(targDir,aaa,"CNV.p.filtered.txt",sep=""),sep="\t",quote=F,row.names=F)
看来问题出在第22行:
normals <- setdiff(ratio.bed,cnvs.bed)
在这种情况下,我们想要找到两个 granges 对象之间的差异,但是执行此操作的函数是 GenomicRanges 的 setdiff
,它被包 base 屏蔽了。答案的解决方案是以这种方式编辑这样的行:
normals <- GenomicRanges::setdiff(ratio.bed,cnvs.bed)
再次感谢您的努力!
我正在尝试 运行 在 bash(我使用的是 CentOS 8)的命令行上使用以下命令创建 R 脚本:
cat 1_myScript.R | R --slave --args $SAMPLE"_x" $SAMPLE"_y"
其中 $SAMPLE 是我在 R 脚本中指定的参数,如下所示
> args<-commandArgs()
> aaa<-args[4]
此语法对我的所有脚本都有效,但现在出现以下错误:
Error in as.vector(x) : no method for coercing this S4 class to a vector
Calls: setdiff -> setdiff.default -> -> as.vector
Execution halted
奇怪的是,如果我尝试在 R 控制台中 运行 这个脚本
>source("1_myScript.R")
它继续没有错误。我查了一下,它似乎是一个链接到我在脚本中使用的库“GenomicRanges”的功能。 这是我的脚本的主体(请注意,我不知道它失败的确切行):
#!/usr/bin/env Rscript
library(rtracklayer)
library(data.table)
library(tidyverse)
#args <- commandArgs()
#aaa<-args[4]
aaa<-gsub("_T.finalSorted.bam_CNVs","",args[1])
targDir<-"/srv/ngsdata/dalteriog/SV_analysis/example/WGS_NB_novogene/CNVDir/"
dataTable <-fread((paste0(targDir,args[2]), header=TRUE)
ratio<-data.frame(dataTable)
dataTable <-fread(paste0(targDir,args[1]), header=FALSE)
cnvs<- data.frame(dataTable)
ratio$Ratio[which(ratio$Ratio==-1)]=NA
cnvs.bed=GRanges(cnvs[,1],IRanges(cnvs[,2],cnvs[,3])) # primo ogetto del GRange obj --> chr; secondo --> range
ratio.bed=GRanges(ratio$Chromosome,IRanges(ratio$Start,ratio$Start),score=ratio$Ratio) # score è un metadata
overlaps <- subsetByOverlaps(ratio.bed,cnvs.bed) # regioni overlappanti i due df
normals <- setdiff(ratio.bed,cnvs.bed) # regioni diverse
normals <- subsetByOverlaps(ratio.bed,normals) # la stessa cosa, ma con lo score associato
#mu <- mean(score(normals),na.rm=TRUE)
#sigma<- sd(score(normals),na.rm=TRUE)
#hist(score(normals),n=500,xlim=c(0,2))
#hist(log(score(normals)),n=500,xlim=c(-1,1))
#shapiro.test(score(normals)[which(!is.na(score(normals)))][5001:10000])
#qqnorm (score(normals)[which(!is.na(score(normals)))],ylim=(c(0,10)))
#qqline(score(normals)[which(!is.na(score(normals)))], col = 2)
#shapiro.test(log(score(normals))[which(!is.na(score(normals)))][5001:10000])
#qqnorm (log(score(normals))[which(!is.na(score(normals)))],ylim=(c(-6,10)))
#qqline(log(score(normals))[which(!is.na(score(normals)))], col = 2)
numberOfCol=length(cnvs)
for (i in c(1:length(cnvs[,1]))) {
values <- score(subsetByOverlaps(ratio.bed,cnvs.bed[i])) #score bayesiano della iesima CNV che overlappa con il file ratio
#wilcox.test(values,mu=mu)
W <- function(values,normals){resultw <- try(wilcox.test(values,score(normals)), silent = TRUE)
if(class(resultw)=="try-error") return(list("statistic"=NA,"parameter"=NA,"p.value"=NA,"null.value"=NA,"alternative"=NA,"method"=NA,"data.name"=NA)) else resultw}
KS <- function(values,normals){resultks <- try(ks.test(values,score(normals)), silent = TRUE)
if(class(resultks)=="try-error") return(list("statistic"=NA,"p.value"=NA,"alternative"=NA,"method"=NA,"data.name"=NA)) else resultks}
#resultks <- try(KS <- ks.test(values,score(normals)), silent = TRUE)
# if(class(resultks)=="try-error") NA) else resultks
cnvs[i,numberOfCol+1]=W(values,normals)$p.value
cnvs[i,numberOfCol+2]=KS(values,normals)$p.value
}
if (numberOfCol==5) {
names(cnvs)=c("chr","start","end","copy number","status","WilcoxonRankSumTestPvalue","KolmogorovSmirnovPvalue")
}
if (numberOfCol==7) {
names(cnvs)=c("chr","start","end","copy number","status","genotype","uncertainty","WilcoxonRankSumTestPvalue","KolmogorovSmirnovPvalue")
}
if (numberOfCol==9) {
names(cnvs)=c("chr","start","end","copy number","status","genotype","uncertainty","somatic/germline","precentageOfGermline","WilcoxonRankSumTestPvalue","KolmogorovSmirnovPvalue")
}
cnvs$Wfdr <- p.adjust(cnvs$WilcoxonRankSumTestPvalue, method="BH",n=nrow(cnvs))
cnvs$KSfdr <- p.adjust(cnvs$KolmogorovSmirnovPvalue, method="BH",n=nrow(cnvs))
cnvs<-subset(cnvs, Wfdr <= 0.05 & KSfdr <= 0.05)
samp<-gsub("_T.finalSorted.bam_CNVs","",aaa)
cnvs<-add_column(cnvs, Sample=samp, .before=1)
write.table(cnvs, file=paste(targDir,aaa,"CNV.p.filtered.txt",sep=""),sep="\t",quote=F,row.names=F)
看来问题出在第22行:
normals <- setdiff(ratio.bed,cnvs.bed)
在这种情况下,我们想要找到两个 granges 对象之间的差异,但是执行此操作的函数是 GenomicRanges 的 setdiff
,它被包 base 屏蔽了。答案的解决方案是以这种方式编辑这样的行:
normals <- GenomicRanges::setdiff(ratio.bed,cnvs.bed)
再次感谢您的努力!