Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nepa10
    Junior Member
    • Jun 2014
    • 8

    #1

    calculate TMM normalization

    Hello all,

    I would like to calculate TMM normalization for identifying DE genes. I know that edgeR does all the normalization when you run the code but I would like to calculate TMM for one or two genes to manually to better understand the statistic behind it. I found this formula to do so ( below) but I am not sure what the component, factor means?

    (raw.counts)*10^7/(librarysize*factors)

    librarysize=total number of mapped reads.

    I would really appreciate any suggestions and help.

    Thanks !
  • dpryan
    Devon Ryan
    • Jul 2011
    • 3478

    #2
    TMM wouldn't have any meaning (i.e., you couldn't calculate it) for one or two genes. The whole purpose of these sorts of normalization methods is to use larger datasets.

    Comment

    • Nepa10
      Junior Member
      • Jun 2014
      • 8

      #3
      Thank you for the quick response and clarification. Would you mind providing some insight into how the calculation is done and what that component factor stands for in the equation.


      Thanks!

      Comment

      • dpryan
        Devon Ryan
        • Jul 2011
        • 3478

        #4
        The algorithm for generating the normalization factor is described in the edgeR paper.

        Comment

        • Nepa10
          Junior Member
          • Jun 2014
          • 8

          #5
          Yes I got the function you use to calculate TMM from that paper. It uses R function, calcNormFactors().

          However I was wondering if I could manually calculate it using the formula I mentioned earlier since I do not know how to use R much.

          Thanks!

          Comment

          • dpryan
            Devon Ryan
            • Jul 2011
            • 3478

            #6
            You can just look at the R code, which you'll need to learn anyway if you want to work in bioinformatics.

            calcNormFactors:
            Code:
            function (object, method = c("TMM", "RLE", "upperquartile", "none"), 
                refColumn = NULL, logratioTrim = 0.3, sumTrim = 0.05, doWeighting = TRUE, 
                Acutoff = -1e+10, p = 0.75) 
            {
                if (is(object, "DGEList")) {
                    x <- as.matrix(object$counts)
                    lib.size <- object$samples$lib.size
                }
                else {
                    x <- as.matrix(object)
                    lib.size <- colSums(x)
                }
                method <- match.arg(method)
                allzero <- rowSums(x > 0) == 0
                if (any(allzero)) 
                    x <- x[!allzero, , drop = FALSE]
                if (nrow(x) == 0 || ncol(x) == 1) 
                    method = "none"
                f <- switch(method, TMM = {
                    f75 <- .calcFactorQuantile(data = x, lib.size = lib.size, 
                        p = 0.75)
                    if (is.null(refColumn)) refColumn <- which.min(abs(f75 - 
                        mean(f75)))
                    if (length(refColumn) == 0 | refColumn < 1 | refColumn > 
                        ncol(x)) refColumn <- 1
                    f <- rep(NA, ncol(x))
                    for (i in 1:ncol(x)) f[i] <- .calcFactorWeighted(obs = x[, 
                        i], ref = x[, refColumn], libsize.obs = lib.size[i], 
                        libsize.ref = lib.size[refColumn], logratioTrim = logratioTrim, 
                        sumTrim = sumTrim, doWeighting = doWeighting, Acutoff = Acutoff)
                    f
                }, RLE = .calcFactorRLE(x)/lib.size, upperquartile = .calcFactorQuantile(x, 
                    lib.size, p = p), none = rep(1, ncol(x)))
                f <- f/exp(mean(log(f)))
                if (is(object, "DGEList")) {
                    object$samples$norm.factors <- f
                    return(object)
                }
                else {
                    return(f)
                }
            }
            .calcFactorQuantile
            Code:
            function (data, lib.size, p = 0.75) 
            {
                y <- t(t(data)/lib.size)
                f <- apply(y, 2, function(x) quantile(x, p = p))
            }
            .calcFactorWeighted
            Code:
            function (obs, ref, libsize.obs = NULL, libsize.ref = NULL, logratioTrim = 0.3, 
                sumTrim = 0.05, doWeighting = TRUE, Acutoff = -1e+10) 
            {
                if (all(obs == ref)) 
                    return(1)
                obs <- as.numeric(obs)
                ref <- as.numeric(ref)
                if (is.null(libsize.obs)) 
                    nO <- sum(obs)
                else nO <- libsize.obs
                if (is.null(libsize.ref)) 
                    nR <- sum(ref)
                else nR <- libsize.ref
                logR <- log2((obs/nO)/(ref/nR))
                absE <- (log2(obs/nO) + log2(ref/nR))/2
                v <- (nO - obs)/nO/obs + (nR - ref)/nR/ref
                fin <- is.finite(logR) & is.finite(absE) & (absE > Acutoff)
                logR <- logR[fin]
                absE <- absE[fin]
                v <- v[fin]
                n <- sum(fin)
                loL <- floor(n * logratioTrim) + 1
                hiL <- n + 1 - loL
                loS <- floor(n * sumTrim) + 1
                hiS <- n + 1 - loS
                keep <- (rank(logR) >= loL & rank(logR) <= hiL) & (rank(absE) >= 
                    loS & rank(absE) <= hiS)
                if (doWeighting) 
                    2^(sum(logR[keep]/v[keep], na.rm = TRUE)/sum(1/v[keep], 
                        na.rm = TRUE))
                else 2^(mean(logR[keep], na.rm = TRUE))
            }

            Comment

            • Nepa10
              Junior Member
              • Jun 2014
              • 8

              #7
              Thank you very much. Appreciate it!

              Comment

              • yusufkhan
                Junior Member
                • Nov 2010
                • 2

                #8
                is it possible to calculate TMM and logFC value through tab delimited file.

                geneid file1 file2
                a 32 33
                b 68 410
                c 78 140
                d 31 340
                e 213 470
                f 211 29

                total number of reads
                file1 7538239
                file2 3828901

                total number of reads align
                file1 6583829
                file2 3029328

                Comment

                Latest Articles

                Collapse

                • SEQadmin2
                  Beyond CRISPR/Cas9: Understand, Choose, and Use the Right Genome Editing Tool
                  by SEQadmin2



                  CRISPR/Cas9 sparked the gene editing revolution for both research and therapeutics.1 But this system still showed severe issues that limited its applications. The most prominent were the heavy reliance on PAM sequences, delivery limitations, double-stranded breaks that prompt unintended edits and cell death, and editing inefficiency (both in targeting and in knock-in reliability).

                  Despite this, “CRISPR helped turn genome editing from a specialized technique into
                  ...
                  07-31-2026, 11:01 AM
                • SEQadmin2
                  Proteomic Platforms: How to Choose the Right Analytical Strategy to Improve Detection and Clinical Applications
                  by SEQadmin2


                  Proteomics platforms are evolving rapidly, with advances in mass spectrometry and affinity-based approaches expanding what researchers can detect and at what scale. As the field moves toward deeper proteome coverage and clinical applications, scientists face an increasingly complex landscape of tools. This article will explore how researchers are navigating these choices to find the right platform for their work.

                  The systematic characterization of the human proteome has
                  ...
                  07-20-2026, 11:48 AM
                • SEQadmin2
                  Advanced Sequencing Platforms Tackle Neuroscience’s Toughest Genomics Problems
                  by SEQadmin2



                  Genomics studies in neuroscience face a special challenge due to the brain’s complexity and scarcity of samples. Mapping changes in cell type and state using conventional next-generation sequencing methods remains challenging. Advances in technologies like single-cell sequencing, spatial transcriptomics, and long-read sequencing have opened the door to deeper studies of the brain and diseases like Alzheimer’s, amyotrophic lateral sclerosis (ALS), and schizophrenia.
                  ...
                  07-09-2026, 11:10 AM

                ad_right_rmr

                Collapse

                News

                Collapse

                Topics Statistics Last Post
                Started by SEQadmin2, Yesterday, 07:41 AM
                0 responses
                12 views
                0 reactions
                Last Post SEQadmin2  
                Started by SEQadmin2, 08-03-2026, 10:13 AM
                0 responses
                27 views
                0 reactions
                Last Post SEQadmin2  
                Started by SEQadmin2, 07-31-2026, 02:55 AM
                0 responses
                39 views
                0 reactions
                Last Post SEQadmin2  
                Started by SEQadmin2, 07-24-2026, 12:17 PM
                0 responses
                25 views
                0 reactions
                Last Post SEQadmin2  
                Working...