Question: creating design and count matrix for rna-seq differential expression
gravatar for marongiu.luigi
3.1 years ago by
Germany, Mannheim, UMM
marongiu.luigi520 wrote:


I am trying to understand how to run a differential expression using R and for that I am referring to Smyth and other's "limma: linear models for microarray and RNA-seq data user guide" (2016). This vignette refers to data sets provided by WEHI bioinfo. The latter provides the link to a dataset made of four libraries (A_1, A_2, B_1 and B_2), a chromosome reference fasta file and a Targets.txt file in the form:

CellType    InputFile   OutputFile
A           A_1.txt.gz  A_1.bam
A           A_2.txt.gz  A_2.bam
B           B_1.txt.gz  B_1.bam
B           B_2.txt.gz  B_2.bam

The case study prepares a design object using a model.matrix function based on the CellType field of the Targets.txt file, then creates an index reference and performs alignment, summarization, filtering, normalization and finally linear model fitting using the following commands:

targets <- readTargets(file="Targets.txt") 
celltype <- factor(targets$CellType)
design <- model.matrix(~celltype)
buildindex(basename = "chr1", reference = "hg19_chr1.fa") 
align(index = "chr1", readfile1 = targets$InputFile, input_format = "gzFASTQ", output_format = "BAM", output_file = targets$OutputFile, unique = TRUE, indels = 5)
fc <- featureCounts(files = targets$OutputFile, annot.inbuilt = "hg19") 
x <- DGEList(counts = fc$counts, genes = fc$annotation[,c("GeneID", "Length")])
isexpr <- rowSums(cpm(x) > 10) >= 2
x <- x[isexpr,]
y <- voom(x, design, plot = TRUE) 
fit <- eBayes(lmFit(y, design))

This all works fine -- although it is unfeasible to attach the actual data herein -- and I wanted to extend this example to the Smith's example (page 69). Here the first step is to create a matrix of read counts; I believe this should come from the featureCounts function thus it is represented by the fc object. The second step is to remove the low counts and to apply scale normalization. I therefore used:

dge <- DGEList(counts = fc$counts, genes = fc$annotation[,c("GeneID", "Length")])
isexpr <- rowSums(cpm(dge) > 10) >= 2
dge <- dge[isexpr,]
dge <- calcNormFactors(dge)
logCPM <- cpm(dge, log = TRUE, prior.count = 3)
fit <- eBayes(logCPM, design)

but I get an error in the final step:

> fit <- eBayes(logCPM, design)
Error: $ operator is invalid for atomic vectors

The same is true if I take off the annotation feature of dge with

dge <- DGEList(counts = fc$counts)

I imagine the problem is due to the design object, so my question is how can I handle the model.matrix function so that I can apply it to different cases? Thank you.

rna-seq • 2.0k views
ADD COMMENTlink modified 3.1 years ago by Devon Ryan97k • written 3.1 years ago by marongiu.luigi520
gravatar for Devon Ryan
3.1 years ago by
Devon Ryan97k
Freiburg, Germany
Devon Ryan97k wrote:

I imagine the issue is related to you skipping the lmFit() step after cpm().

ADD COMMENTlink written 3.1 years ago by Devon Ryan97k

Nice catch Devon!! Upvote to you. Definitely need a linear model prior to running eBayes.

ADD REPLYlink written 3.1 years ago by theobroma221.1k

ops, i did miss that line. Running:

logCPM <- cpm(dge, log = TRUE, prior.count = 3)
fit <- lmFit(logCPM, design)
fit <- eBayes(fit, trend = TRUE)

as in the vignette goes smoothly. my bad.

ADD REPLYlink written 3.1 years ago by marongiu.luigi520
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1280 users visited in the last hour