Remove NaNs from ExpressionSet class object
3
0
Entering edit mode
5.3 years ago

Hi everyone,

Sometimes in microarray analysis, some datasets contain NaN values, I need to remove NaNs from gset to ex matrix will be without NaNs too.

How to remove NaNs from ExpressionSet (gset)?

I would be very pleased if you could guide me.

R Bioconductor • 4.0k views
ADD COMMENT
3
Entering edit mode
5.3 years ago
zx8754 12k

We need to use Biobase::exprs() to access matrix data, then assign as usual, see this reproducible example:

Reproducible example from the manual - An Introduction to Bioconductor’s ExpressionSet Class

library(Biobase)

dataDirectory <- system.file("extdata", package="Biobase")
exprsFile <- file.path(dataDirectory, "exprsData.txt")
exprs <- as.matrix(read.table(exprsFile, header=TRUE, sep="\t",
                              row.names=1,
                              as.is=TRUE))

minimalSet <- ExpressionSet(assayData=exprs)

# check the data
exprs(minimalSet)[1:3, 1:3]
#                        A         B        C
# AFFX-MurIL2_at  192.7420  85.75330 176.7570
# AFFX-MurIL10_at  97.1370 126.19600  77.9216
# AFFX-MurIL4_at   45.8192   8.83135  33.0632

# introduce some NaN
exprs(minimalSet)[1:2, 1:2] <- NaN

# check the data
exprs(minimalSet)[1:3, 1:3]
#                       A       B        C
# AFFX-MurIL2_at      NaN     NaN 176.7570
# AFFX-MurIL10_at     NaN     NaN  77.9216
# AFFX-MurIL4_at  45.8192 8.83135  33.0632

# replace NaN with zero
exprs(minimalSet)[ is.nan(exprs(minimalSet)) ] <- 0

# check the data
exprs(minimalSet)[1:3, 1:3]
#                       A       B        C
# AFFX-MurIL2_at   0.0000 0.00000 176.7570
# AFFX-MurIL10_at  0.0000 0.00000  77.9216
# AFFX-MurIL4_at   45.8192   8.83135  33.0632
ADD COMMENT
2
Entering edit mode
5.3 years ago

You should be able to subset the ExpressionSet like a base-R matrix, i.e.

ExpressionSet[is.na(ExpressionSet)] <- 0

If that doesn't work, try the same approach with exprs(ExpressionSet) or assayData(ExpressionSet). See this website for more ways of replacing NA's.

ADD COMMENT
0
Entering edit mode

Hi Friederike,

Thank you for your guidance and useful link that you introduced. I mean NaNs, not NAs, NaNs is different from NAs.

ADD REPLY
0
Entering edit mode

Change is.na in Friederike's command to is.nan and you'll replace NaNs instead of NAs

ADD REPLY
0
Entering edit mode

Hi,

Thank you for your attention, while I use is.nan instead is.na, encounter the following error:

> ExpressionSet[is.nan(ExpressionSet)] <- 0
Error in is.nan(ExpressionSet) : 
  default method not implemented for type 'closure'
ADD REPLY
0
Entering edit mode

Hi,

try this code maybe it will work for you

ExpressionSet[ExpressionSet == NaN] <- 0

ADD REPLY
1
Entering edit mode

Hi,

Comparing to NaN with == won't work unless you've redefined ==.

> NaN == NaN
[1] NA
> is.nan(NaN)
[1] TRUE
ADD REPLY
0
Entering edit mode

Hi, Thank you for your answer. I use the above code and encounter the following error:

> ExpressionSet[ExpressionSet == NaN] <- 0
Error in ExpressionSet == NaN : 
  comparison (1) is possible only for atomic and list types
ADD REPLY
0
Entering edit mode

I think we have a bit of misunderstanding here. Is your object called gset or ExpressionSet? If it's gset, you should be using gset[is.nan(gset)]<-0.

If that doesn't work, Friederike's exact code could work for you. is.na() matches NA as well as NaN.

If gset contains NaN but not NA, you can simply do gset[is.na(gset)] <- 0. If your object contains both NAs and NaN, (and gset is a data.frame like object), you can do something like:

gset.tmp <- gset #So we don't end up corrupting the original object
gset.tmp[do.call(cbind, lapply(gset.tmp, is.nan))] <- 0 # This should replace NaNs with 0s

Please try both approaches above and let us know how that works out for you.

ADD REPLY
0
Entering edit mode

Hi, Thanks for your follow up. gset class is ExpressionSet.

> class(gset)
[1] "ExpressionSet"
attr(,"package")
[1] "Biobase"

The above-mentioned codes are for data.farame object, not ExpressionSet. See an example of the above codes below.

> gset[is.nan(gset)]<-0
Error in is.nan(gset) : default method not implemented for type 'S4'
> gset[is.na(gset)] <- 0
Error in gset[is.na(gset)] <- 0 : object of type 'S4' is not subsettable
In addition: Warning message:
In is.na(gset) : is.na() applied to non-(list or vector) of type 'S4'
> gset.tmp <- gset
> gset.tmp[do.call(cbind, lapply(gset.tmp, is.nan))] <- 0
Error in as.list.default(X) : 
  no method for coercing this S4 class to a vector
> ExpressionSet[ExpressionSet == NaN] <- 0
Error in ExpressionSet == NaN : 
  comparison (1) is possible only for atomic and list types
ADD REPLY
1
Entering edit mode

This needs for someone to read the manual and figure out what sort of class ExpressionSet is.

From a quick read, it looks like the numeric data in an ExpressionSet object is stored in its AssayData slot. You should be able to use:

dfES<-as(gset, 'data.frame');
dfES[do.call(cbind, lapply(dfES, is.nan))] <- 0

or

aD <- assayData(gset);
aD[do.call(cbind, lapply(aD, is.nan))] <- 0

P.S.: Please do not use ExpressionSet like you would a variable, it's not. It's the class of a variable. Using it as a variable is like using data.frame[is.na(data.frame)]<-0, it shouldn't work, and if it does, there's a bigger problem with the code.

ADD REPLY
1
Entering edit mode
5.3 years ago

Thanks for all the suggestions and valuable guides. I convert ExpressionSet into a Matrix, following replace NaNs with zero and convert the matrix into ExpressionSet again.

> ex <- exprs(gset)
> ex[is.nan(ex)] <- 0
> exprs(gset) <- ex
> dim(ex)
[1] 47323    86

> dim(gset)
Features  Samples 
   47323       86
ADD COMMENT
2
Entering edit mode

Just to be precise, some advice on your wording: The ExpressionSet object (in your case named "gset") contains a matrix of values, which can be accessed via exprs() (as you show).

ADD REPLY
0
Entering edit mode

Thank you and everyone else, for sharing your wisdom with me.

ADD REPLY
1
Entering edit mode

Thank you for posting the answer. Can you please up-vote and / or Accept the other answers that have assisted you?

Upvote|Bookmark|Accept

ADD REPLY
0
Entering edit mode

Thanks for your follow up. The answers were valuable and helpful.

ADD REPLY
1
Entering edit mode

Then you should accept the answers that solved your question and/or pointed you in the right direction.

ADD REPLY

Login before adding your answer.

Traffic: 1665 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6