text: user16872: I'm trying to figure out the right way to do differential gene expression analysis with a discrete variable with three groups. The context is, I want to assess differential expression as a function of whether samples are WT, have heterozygous loss, or homozygous loss for a particular gene. A key part of it is comparing the differences between the WT and het-loss samples with the differences between het-loss and hom-loss samples.
  I tried doing just doing differential expression with DESeq2, treating it as a categorical variable, but I got a strange result: differential expression between het and hom samples seemed to usually go in the opposite direction as in wt vs. het (so if a gene was upregulated in het relative to WT, it was downregulated in hom samples relative to het). This was very counterintuitive; why would losing the second copy of a gene consistently have the opposite effect on overall gene expression that losing the first copy has? So to test if something was going awry, I did the same analysis, but randomly assigning samples into three groups, and found the same thing: the log fold change comparing group 3 to 2 was strongly negatively correlated with the log FC comparing group 2 to 1, even though the groups are entirely random. So this must be some artifact of the model being used (note that I observe the same thing when used limma voom instead of DESeq 2). I include an example here from data loaded from TCGA via TCGAbiolinks:
  library(TCGAbiolinks)
  library(DESeq2)
  
  #Loading KIRC data from TCGA:
  query_TCGA = GDCquery(
    project = "TCGA-KICH",
    data.type = "Gene Expression Quantification",
    data.category = "Transcriptome Profiling", # parameter enforced by GDCquery
    experimental.strategy = "RNA-Seq",
    workflow.type = "STAR - Counts")
  download <- GDCdownload(query = query_TCGA)
  dat <- GDCprepare(query = query_TCGA, save = FALSE)
  rna <- as.data.frame(SummarizedExperiment::assay(dat))
  
  #Randomly assigning samples to groups 1, 2, and 3 and running DESeq2:
  set.seed(1234)
  xvar <- factor(sample(1:3, ncol(rna), replace = TRUE), levels = c(1,2,3))
  columnData <- data.frame('Patient'=colnames(rna),'xvar'=xvar)
  columnData$xvar <- factor(columnData$xvar, levels = c('1','2','3'))
  deseq2Data <- DESeqDataSetFromMatrix(countData=rna, colData=columnData, design= ~     xvar)
  deseq2Data <- deseq2Data[rowSums(counts(deseq2Data)) > 5, ]
  deseq2Data <- DESeq(deseq2Data)
  res.1_v_3 <- as.data.frame(results(deseq2Data, contrast = c('xvar','1','3')))
  res.1_v_2 <- as.data.frame(results(deseq2Data, contrast = c('xvar','1','2')))
  res.2_v_3 <- as.data.frame(results(deseq2Data, contrast = c('xvar','2','3')))
  
  #Merging comparisons of 1 v 2, 1 v 3, and 2 v 3 into one data frame:
  merged <- merge(data.frame('Gene'=rownames(res.1_v_2),     'log2FC_1_v_2'=res.1_v_2$log2FoldChange, 'padj_1_v_2'=res.1_v_2$padj), 
                  data.frame('Gene'=rownames(res.2_v_3),     'log2FC_2_v_3'=res.2_v_3$log2FoldChange, 'padj_2_v_3'=res.2_v_3$padj), by='Gene')
  merged <- merge(merged, data.frame('Gene'=rownames(res.1_v_3),     'log2FC_1_v_3'=res.1_v_3$log2FoldChange, 
                                     'padj_1_v_3'=res.1_v_3$padj), by='Gene')
  #Color coding for illustration:
  merged$color <- 'gray'
  merged$sig_1_v_2 <- merged$padj_1_v_2 < .05
  merged$sig_1_v_3 <- merged$padj_1_v_3 < .05
  merged$sig_2_v_3 <- merged$padj_2_v_3 < .05
  merged$color[which(merged$padj_1_v_2 < .05 & merged$padj_2_v_3 > .05)] <- 'blue'
  merged$color[which(merged$padj_1_v_2 > .05 & merged$padj_2_v_3 < .05)] <- 'red'
  merged$color[which(merged$padj_1_v_2 < .05 & merged$padj_2_v_3 < .05)] <- 'purple'
  maxim <- max(abs(c(merged$log2FC_1_v_2, merged$log2FC_2_v_3)))
  coef <- cor(merged$log2FC_1_v_2, merged$log2FC_2_v_3)
  p <- try(round(chisq.test(table(merged[,c('sig_1_v_2','sig_2_v_3')]))$p.value,     digits = 4), silent = TRUE)
  if(class(p) == 'try-error'){p <- NA}
  
  plot(merged$log2FC_1_v_2, merged$log2FC_2_v_3, col=merged$color, pch=16,     cex=.5,main='Log2 FC',
       xlim=c(-maxim1,maxim), ylim=c(-maxim,maxim),xlab=c('LogFC:     Het/WT'),ylab=c('LogFC: Biallelic/Het'))
  abline(a=0,b=coef1,col='red')
  legend('topright',legend = c('Coefficient','Significant Group 2 v. Group 1',
                               'Significant Group 3 vs. group 2','Significant for     both comparisons'), 
         lty=c(1, NA, NA,NA), pch=c(NA, 16, 16,16),     col=c('red','blue','red','purple'))
  text(x=-maxim*.8, y=-maxim, labels=paste('p=',p,' (chi-sq. test',sep=''))
  
  
  Is there something particular I'm doing wrong? And what would be the correct way to do differential expression analysis with an independent variable that is  essentially an ordinal variable? Thanks.
  
  user16676: I will try to answer this open-ended question with general statements towards the analysis of larger cohorts:
  Larger cohorts are usually not homogeneous.
  Based on the provided annotations (colData(dat)) there are cancer samples and normal controls in this dataset, and they're not equal in size. It is 66 Primary solid Tumor and 25 Solid Tissue Normal. Running a PCA, for example via plotPCA() after running vst() will demonstrate that this is the main source of variation in this dataset, separating along PC1. That means a random split of the dataset will still have more or fewer cancers or normals in one or the other groups, and at that sample size and based on the expected strong expression differences between cancer and normal this will get DEGs almost certainly.
  Here the PCA via plotPCA(vst(dds, blind=TRUE), "definition")
  
  That having said, you need to inspect the data you deal with in depth before throwing any test on it. If not you might test some sort of confounders or factors that bias the analysis without noticing it. Based on the PCA there is also notable heterogeneity in the cancer samples, and the annotations list additional factors that can contribute to this, be it gender, smoking status, tumor stage, age and others. You would need to ensure that your random assignment does not nest any of these factors or the tumor/normal type with the random groups.
  Data exploration is key. There was a recent paper that got quite some attention on Twitter claiming that common testing frameworks (incl DESeq2) lead to exaggerated false positive rates. However, the DESeq2 author (analysis here) showed that this paper missed aforementioned proper data exploration, and the dataset infact had unaddressed technical variation and samples forming groups within that dataset. Addressing that (he did it via RUVSeq) then deemphasized the strong claims in that paper.
  Summarizing: Large cohorts, especially human ones, often have internal nesting and confounding with other factors that are unwanted. That can be the aforementioned obvious ones like age or gender (sex) but also more subtle ones often not recorded, such as dietary status, unreported drug consumption, amount of physical exercise the person does (if any), genetic predisposition, previous disease or accidents/trauma and so on. That having said, I am quite sure that the "random" DEGs you see are nested unwanted variation rather than anything "wrong" with the testing frameworks.
  
original_text: 16872: <p>I'm trying to figure out the right way to do differential gene expression analysis with a discrete variable with three groups. The context is, I want to assess differential expression as a function of whether samples are WT, have heterozygous loss, or homozygous loss for a particular gene. A key part of it is comparing the differences between the WT and het-loss samples with the differences between het-loss and hom-loss samples.</p>
  <p>I tried doing just doing differential expression with DESeq2, treating it as a categorical variable, but I got a strange result: differential expression between het and hom samples seemed to usually go in the opposite direction as in wt vs. het (so if a gene was upregulated in het relative to WT, it was downregulated in hom samples relative to het). This was very counterintuitive; why would losing the second copy of a gene consistently have the opposite effect on overall gene expression that losing the first copy has? So to test if something was going awry, I did the same analysis, but randomly assigning samples into three groups, and found the same thing: the log fold change comparing group 3 to 2 was strongly negatively correlated with the log FC comparing group 2 to 1, even though the groups are entirely random. So this must be some artifact of the model being used (note that I observe the same thing when used limma voom instead of DESeq 2). I include an example here from data loaded from TCGA via TCGAbiolinks:</p>
  <pre><code>library(TCGAbiolinks)
  library(DESeq2)
  
  #Loading KIRC data from TCGA:
  query_TCGA = GDCquery(
    project = &quot;TCGA-KICH&quot;,
    data.type = &quot;Gene Expression Quantification&quot;,
    data.category = &quot;Transcriptome Profiling&quot;, # parameter enforced by GDCquery
    experimental.strategy = &quot;RNA-Seq&quot;,
    workflow.type = &quot;STAR - Counts&quot;)
  download &lt;- GDCdownload(query = query_TCGA)
  dat &lt;- GDCprepare(query = query_TCGA, save = FALSE)
  rna &lt;- as.data.frame(SummarizedExperiment::assay(dat))
  
  #Randomly assigning samples to groups 1, 2, and 3 and running DESeq2:
  set.seed(1234)
  xvar &lt;- factor(sample(1:3, ncol(rna), replace = TRUE), levels = c(1,2,3))
  columnData &lt;- data.frame('Patient'=colnames(rna),'xvar'=xvar)
  columnData<span class="math-container">$xvar &lt;- factor(columnData$</span>xvar, levels = c('1','2','3'))
  deseq2Data &lt;- DESeqDataSetFromMatrix(countData=rna, colData=columnData, design= ~     xvar)
  deseq2Data &lt;- deseq2Data[rowSums(counts(deseq2Data)) &gt; 5, ]
  deseq2Data &lt;- DESeq(deseq2Data)
  res.1_v_3 &lt;- as.data.frame(results(deseq2Data, contrast = c('xvar','1','3')))
  res.1_v_2 &lt;- as.data.frame(results(deseq2Data, contrast = c('xvar','1','2')))
  res.2_v_3 &lt;- as.data.frame(results(deseq2Data, contrast = c('xvar','2','3')))
  
  #Merging comparisons of 1 v 2, 1 v 3, and 2 v 3 into one data frame:
  merged &lt;- merge(data.frame('Gene'=rownames(res.1_v_2),     'log2FC_1_v_2'=res.1_v_2<span class="math-container">$log2FoldChange, 'padj_1_v_2'=res.1_v_2$</span>padj), 
                  data.frame('Gene'=rownames(res.2_v_3),     'log2FC_2_v_3'=res.2_v_3<span class="math-container">$log2FoldChange, 'padj_2_v_3'=res.2_v_3$</span>padj), by='Gene')
  merged &lt;- merge(merged, data.frame('Gene'=rownames(res.1_v_3),     'log2FC_1_v_3'=res.1_v_3<span class="math-container">$log2FoldChange, 
                                     'padj_1_v_3'=res.1_v_3$</span>padj), by='Gene')
  #Color coding for illustration:
  merged<span class="math-container">$color &lt;- 'gray'
  merged$</span>sig_1_v_2 &lt;- merged<span class="math-container">$padj_1_v_2 &lt; .05
  merged$</span>sig_1_v_3 &lt;- merged<span class="math-container">$padj_1_v_3 &lt; .05
  merged$</span>sig_2_v_3 &lt;- merged<span class="math-container">$padj_2_v_3 &lt; .05
  merged$</span>color[which(merged<span class="math-container">$padj_1_v_2 &lt; .05 &amp; merged$</span>padj_2_v_3 &gt; .05)] &lt;- 'blue'
  merged<span class="math-container">$color[which(merged$</span>padj_1_v_2 &gt; .05 &amp; merged<span class="math-container">$padj_2_v_3 &lt; .05)] &lt;- 'red'
  merged$</span>color[which(merged<span class="math-container">$padj_1_v_2 &lt; .05 &amp; merged$</span>padj_2_v_3 &lt; .05)] &lt;- 'purple'
  maxim &lt;- max(abs(c(merged<span class="math-container">$log2FC_1_v_2, merged$</span>log2FC_2_v_3)))
  coef &lt;- cor(merged<span class="math-container">$log2FC_1_v_2, merged$</span>log2FC_2_v_3)
  p &lt;- try(round(chisq.test(table(merged[,c('sig_1_v_2','sig_2_v_3')]))$p.value,     digits = 4), silent = TRUE)
  if(class(p) == 'try-error'){p &lt;- NA}
  
  plot(merged<span class="math-container">$log2FC_1_v_2, merged$</span>log2FC_2_v_3, col=merged$color, pch=16,     cex=.5,main='Log2 FC',
       xlim=c(-maxim1,maxim), ylim=c(-maxim,maxim),xlab=c('LogFC:     Het/WT'),ylab=c('LogFC: Biallelic/Het'))
  abline(a=0,b=coef1,col='red')
  legend('topright',legend = c('Coefficient','Significant Group 2 v. Group 1',
                               'Significant Group 3 vs. group 2','Significant for     both comparisons'), 
         lty=c(1, NA, NA,NA), pch=c(NA, 16, 16,16),     col=c('red','blue','red','purple'))
  text(x=-maxim*.8, y=-maxim, labels=paste('p=',p,' (chi-sq. test',sep=''))
  </code></pre>
  <p><a href="https://i.stack.imgur.com/hDQxp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hDQxp.png" alt="enter image description here" /></a></p>
  <p>Is there something particular I'm doing wrong? And what would be the correct way to do differential expression analysis with an independent variable that is  essentially an ordinal variable? Thanks.</p>
   16676: <p>I will try to answer this open-ended question with general statements towards the analysis of larger cohorts:</p>
  <p><strong>Larger cohorts are usually not homogeneous.</strong></p>
  <p>Based on the provided annotations (<code>colData(dat)</code>) there are cancer samples <strong>and</strong> normal controls in this dataset, and they're not equal in size. It is 66 <code>Primary solid Tumor</code> and 25 <code>Solid Tissue Normal</code>. Running a PCA, for example via <code>plotPCA()</code> after running <code>vst()</code> will demonstrate that this is the main source of variation in this dataset, separating along PC1. That means a random split of the dataset will still have more or fewer cancers or normals in one or the other groups, and at that sample size and based on the expected strong expression differences between cancer and normal this will get DEGs almost certainly.</p>
  <p>Here the PCA via <code>plotPCA(vst(dds, blind=TRUE), &quot;definition&quot;)</code></p>
  <p><a href="https://i.stack.imgur.com/Fvv8b.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Fvv8b.png" alt="enter image description here" /></a></p>
  <p>That having said, you need to inspect the data you deal with in depth before throwing any test on it. If not you might test some sort of confounders or factors that bias the analysis without noticing it. Based on the PCA there is also <strong>notable heterogeneity</strong> in the cancer samples, and the annotations list additional factors that can contribute to this, be it <strong>gender</strong>, <strong>smoking status</strong>, <strong>tumor stage</strong>, <strong>age</strong> and others. You would need to ensure that your random assignment does not nest any of these factors or the tumor/normal type with the random groups.</p>
  <p>Data exploration is key. There was <a href="https://genomebiology.biomedcentral.com/articles/10.1186/s13059-022-02648-4" rel="nofollow noreferrer">a recent paper</a> that got quite some attention on Twitter claiming that common testing frameworks (incl DESeq2) lead to exaggerated false positive rates. However, the DESeq2 author (<a href="https://github.com/mikelove/preNivolumabOnNivolumab/blob/main/preNivolumabOnNivolumab.knit.md" rel="nofollow noreferrer">analysis here</a>) showed that this paper missed aforementioned proper data exploration, and the dataset infact had unaddressed technical variation and samples forming groups within that dataset. Addressing that (he did it via <code>RUVSeq</code>) then deemphasized the strong claims in that paper.</p>
  <p>Summarizing: Large cohorts, especially human ones, often have internal nesting and confounding with other factors that are unwanted. That can be the aforementioned obvious ones like age or gender (sex) but also more subtle ones often not recorded, such as dietary status, unreported drug consumption, amount of physical exercise the person does (if any), genetic predisposition, previous disease or accidents/trauma and so on. That having said, I am quite sure that the &quot;random&quot; DEGs you see are nested unwanted variation rather than anything &quot;wrong&quot; with the testing frameworks.</p>
  