Home > Blockchain >  Making multiple contrast from a list
Making multiple contrast from a list

Time:04-05

I have different condition to test which are as such

coldata$FAB
  [1] M4 M0 M4 M2 M1 M3 M5 M1 M0 M5 M4 M0 M4 M0 M4 M5 M2 M4 M1 M4 M2 M2 M2 M0 M2 M0 M0 M0 M5 M3 M1 M4 M3 M2 M1 M2 M1 M2 M2 M0 M4 M1 M1 M2 M1 M4 M3
 [48] M5 M4 M2 M0 M2 M4 M4 M2 M4 M1 M1 M0 M4 M1 M1 M2 M1 M4 M3 M3 M5 M2 M2 M4 M1 M1 M1 M2 M4 M1 M2 M5 M0 M1 M2 M1 M2 M2 M5 M1 M1 M4 M2 M2 M2 M1 M4
 [95] M1 M3 M0 M5 M5 M2 M2 M4 M5 M1 M0 M4 M1 M3 M4 M1 M5 M5 M2 M3 M4 M2 M1 M3 M5 M1 M4 M3 M1 M3 M4 M2 M2 M2 M2 M4 M1 M1 M2 M0 M2 M2 M1 M1 M5 M3 M1
[142] M3 M3 M2 M1 M4 M4
Levels: M0 M1 M2 M3 M4 M5

These are my different sub-types.

M0 M1 M2 M3 M4 M5

To generate a multiple contrast

I got a function like this

unique_combn <- combn(c("M0", "M1", "M2", "M3", "M4" ,"M5"), 2)
unique_combn
contrasts <- lapply(1:ncol(unique_combn), function(x){
  
  c("FAB", unique_combn[1,x], unique_combn[2,x])
  
})
names(contrasts) <- unlist(lapply(contrasts, function(x) paste(x[2], x[3], sep = "_vs_")))

contrasts

names(contrasts) <- unlist(lapply(contrasts, function(x) paste(x[2], x[3], sep = "_vs_")))

contrasts

The contrasts is generated as such

contrasts
$M0_vs_M1
[1] "FAB" "M0"  "M1" 

$M0_vs_M2
[1] "FAB" "M0"  "M2" 

$M0_vs_M3
[1] "FAB" "M0"  "M3" 

$M0_vs_M4
[1] "FAB" "M0"  "M4" 

$M0_vs_M5
[1] "FAB" "M0"  "M5" 

$M1_vs_M2
[1] "FAB" "M1"  "M2" 

$M1_vs_M3
[1] "FAB" "M1"  "M3" 

$M1_vs_M4
[1] "FAB" "M1"  "M4" 

$M1_vs_M5
[1] "FAB" "M1"  "M5" 

$M2_vs_M3
[1] "FAB" "M2"  "M3" 

$M2_vs_M4
[1] "FAB" "M2"  "M4" 

$M2_vs_M5
[1] "FAB" "M2"  "M5" 

$M3_vs_M4
[1] "FAB" "M3"  "M4" 

$M3_vs_M5
[1] "FAB" "M3"  "M5" 

$M4_vs_M5
[1] "FAB" "M4"  "M5" 

Where each of them are my all possible combinations.

So now I would like to pass the list either one by one which is doable if the list is small but I have more samples as well to compare.

For example I want to compare the first condition I use this code below to get my desired output.

M0_vs_M1 <- results(dds, contrast=contrasts[["M0_vs_M1"]])

This generates my desired output.

Now I would like to do the same for other generated contrasts and then save those output into different tables.

Any suggestion or help would be really appreciated.

UPDATE

My dds object

head(dds)
class: DESeqDataSet 
dim: 6 147 
metadata(1): version
assays(6): counts mu ... replaceCounts replaceCooks
rownames(6): ENSG00000223972 ENSG00000227232 ... ENSG00000237613 ENSG00000268020
rowData names(39): baseMean baseVar ... maxCooks replace
colnames(147): TCGA-AB-2856 TCGA-AB-2849 ... TCGA-AB-2970 TCGA-AB-2933
colData names(3): FAB sizeFactor replaceable

Since my dds object I can't show because I'm not sure if i can put that in a dataframe kind of.Here is a dummy data to show the dds

dds <- makeExampleDESeqDataSet(n = 1000, m = 12, betaSD = 2)
dds$colour <- factor(rep(c("pink", "white"), each = 6))
dds$colour <- relevel(dds$colour, "white")
dds$condition <- factor(rep(c("sun", "shade"), 6))
dds <- dds[, order(dds$colour, dds$condition)]
colnames(dds) <- paste0("sample", 1:ncol(dds))


design(dds) <- ~ 1   colour   condition   colour:condition
dds <- DESeq(dds)
resultsNames(dds)

CodePudding user response:

not an answer ;-), but too long for a comment

I see no sample data of dds, so using lapply() is the best generic answer I can give...

There are some improvements in your code to create contrasts that I would like to hand to you.

# make smart use of the possible arguments from the combn-function
contrasts <- combn( c("M0", "M1", "M2", "M3", "M4" ,"M5"), 
                    FUN = function(x) c("FAB", x),
                    m = 2, 
                    simplify = FALSE)


names(contrasts) <- sapply(
  combn( c("M0", "M1", "M2", "M3", "M4" ,"M5"), m = 2, simplify = FALSE),
  paste0, collapse = "_vs_")

# $M0_vs_M1
# [1] "FAB" "M0"  "M1" 
# 
# $M0_vs_M2
# [1] "FAB" "M0"  "M2" 
# 
# $M0_vs_M3
# [1] "FAB" "M0"  "M3" 
# 
# $M0_vs_M4
# [1] "FAB" "M0"  "M4" 
# 
# $M0_vs_M5
# [1] "FAB" "M0"  "M5" 
#
# ......

CodePudding user response:

You can just use lapply again

results <- lapply(contrasts, function(x) results(dds, contrast=x))

  •  Tags:  
  • r
  • Related