converting a matrix of gene links to a normal matrix
1
0
Entering edit mode
7.6 years ago
fi1d18 ★ 4.2k

hi,

sorry i have a list of interaction between my genes like below

from                      to
AT5G05410    AT2G26150    0.0636682705541425
AT4G34410    AT1G12610    0.055895368934279
AT2G46830    AT1G01060    0.0554920876837871


how i can convert the above file to something like below

            AT1G01060  AT1G01170   AT1G01260
AT1G01060  1.00000000  0.3885284 -0.14720327
AT1G01170  0.38852841  1.0000000 -0.29069241
AT1G01260 -0.14720327 -0.2906924  1.00000000
AT1G01380 -0.01865947  0.2699235  0.30973373
AT1G01490  0.24681279  0.3955740 -0.07497821
AT1G01500  0.05720335 -0.1786700 -0.26813919


gene R • 876 views
0
Entering edit mode

are you sure your input is that data only?

0
Entering edit mode

no it is a big matrix but i pasted only a few rows for example, may you tell me the solution please?

0
Entering edit mode

How can one know what to do without looking into the complete data (complete rows of a toy set). There is one numerical column in your input data but 3 in expected output.

2
Entering edit mode
7.6 years ago
Michael 53k

Untested, please check for minor errors:

my.dimn <- unique(as.character(c(row.matrix[,1], row.matrix[,2] ) ))
### find the appropriate dim names for the matrix, having a matrix with these
### dimnames provides all possible combinations
my.dist <- matrix(NA, nrow=length(dimn), ncol=length(dimn), dimnames=list(my.dimn, my.dimn)) ## make a quadratic matrix of NA

diag(my.dist) <- 1 # diagonale will be 1

my.dist[row.matrix[,1:2]] <- row.matrix[,3] # yes this works, everything not defined in the row matrix is NA


This code converts a sparse matrix in row coordinate notation into a full matrix, see the Matrix package for more options, e.g. sparse matrix.

0
Entering edit mode

thank you Michael