Log2 raw count matrix with one condition
2
0
Entering edit mode
7.1 years ago
User000 ▴ 690

Hello, I have a gene count matrix with 1 condition only. I have made log2(raw_count+1) with DESeq2, but when I filter off genes with zero expression my gene names disappear. How to fix this so that at the end I have my gene names and log2 values?

raw_count <- read.csv("gene_count_matrix.csv", header = TRUE, row.names = 1)
# Remove all gene which has 0 value in all sample
all <- apply(raw_count, 1, function(x) all(x==0))
newdata <- raw_count[!all,]
write.csv(newdata, file = "filtered_matrix.csv")
final_count <- read.csv("filtered_matrix.csv", header=TRUE, row.names = 1)
DESeq2 • 2.2k views
ADD COMMENT
4
Entering edit mode
7.1 years ago

You should use drop=FALSE to prevent droping the dimensions of the matrix.

newdata <- raw_count[!all, , drop=F]
ADD COMMENT
0
Entering edit mode

this works, thank you! strangely, when I have many conditions this does not happen, do you know why? thank you again

ADD REPLY
1
Entering edit mode

Because if you have only one column in your matrix/dataframe, the default behaviour of R is to convert it into a vector. Edit : more info in the R help :

?'['
ADD REPLY
1
Entering edit mode
7.1 years ago
russhh 5.7k

I can't reproduce your issue:

M <- matrix(c(0,0,0,1,0,1, 1, 2,3 ), nrow = 3, byrow = TRUE)
rownames(M) <- letters[1:3]
drop.rows <- apply(M, 1, function(x) all(x==0))  # best not to use the function-name `all`
M[!drop.rows, ]

> M[which(!drop.rows), ]
  [,1] [,2] [,3] 
b    1    0    1   
c    1    2    3

Could you post a minimal example, please.

ADD COMMENT
1
Entering edit mode

Try with only 1 column :

M <- matrix(c(0,0,0,1,0,1, 1, 2,3 ), nrow = 9, byrow = TRUE)
rownames(M) <- letters[1:9]
drop.rows <- apply(M, 1, function(x) all(x==0))  # best not to use the function-name `all`

M[!drop.rows, , drop=T]
d f g h i 
1 1 1 2 3 

M[!drop.rows, , drop=F]
  [,1]
d    1
f    1
g    1
h    2
i    3
ADD REPLY

Login before adding your answer.

Traffic: 1723 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