问题
I have a table like this:
> updownregtable
PIM WDR MYC OBX
ILMN_1651282 0 0 0 0
ILMN_1651354 0 0 0 0
ILMN_1651358 0 0 0 0
ILMN_1656638 0 0 0 0
ILMN_1657235 0 0 0 0
ILMN_1657639 -1 0 0 0
The rownames are codes for genes. The colnames are transfections in a cell.
I make a vennDiagram with the functions in the following link: http://bioinfo-mite.crb.wsu.edu/Rcode/Venn.R
Before making the vennDiagram, vennCounts results gives this output:
> vennCounts(regulationtable)
PIM WDR MYC OBX Counts
[1,] 0 0 0 0 740
[2,] 0 0 0 1 5
[3,] 0 0 1 0 1
[4,] 0 0 1 1 0
[5,] 0 1 0 0 4
[6,] 0 1 0 1 1
[7,] 0 1 1 0 0
[8,] 0 1 1 1 0
[9,] 1 0 0 0 6
[10,] 1 0 0 1 0
[11,] 1 0 1 0 0
[12,] 1 0 1 1 0
[13,] 1 1 0 0 1
[14,] 1 1 0 1 0
[15,] 1 1 1 0 0
[16,] 1 1 1 1 0
Now I want to create per row a list within all the genenames stored in that group. e.g. like this:
Group 1 - creates a list with 740 genenames
Group 2 - creates a list with 5 genenames
Group 3 - creates a list with 1 genenames
Group 5 - creates a list with 4 genenames
Group 6 - creates a list with 1 genenames
Group 9 - creates a list with 6 genenames
Group 13 - creates a list with 1 genename.
Can you help me?
回答1:
Here is one possible solution. Basically it involves converting the matrix to a character vector, since there are convenient functions for matching text strings.
## create an example matrix - analagous to your 'updownregtable'
nc <- 4
nr <- 1000
M <- matrix(rbinom(nr*nc,1,0.5),
nrow = nr, ncol = nc,
dimnames = list(sapply(1:nr, function(i) paste(sample(letters,5),collapse='')),paste('V',1:nc)))
## function for converting rows of a matrix to lines of text
matrix2text <- function(y) apply(y,1,function(x)paste(x,collapse=','))
## unique entries - analagous to the first four columns of your matrix
## vennCounts(regulationtable)
Mu <- matrix2text(unique(M))
names(Mu) <- NULL
## convert the full matrix to text
Mc <- matrix2text(M)
## find the matching groups
matching.groups <- sapply(Mu,function(x)names(grep(x,Mc,value=TRUE)))
## here are the counts per group
counts.per.group <- sapply(matching.groups,length)
来源:https://stackoverflow.com/questions/5375642/venndiagram-create-list-of-venncounts