Answers.Body: <p>The figure caption says "the top 40 genes per celltype" - so we can simply use the code you provided for the top 40 genes for a single celltype, repeat this for all celltypes, and then remove any duplicate genes. Here's an example:</p>
  
  <pre><code>&gt; expr &lt;- matrix(runif(9*24000), nrow=24000, ncol=9)
  &gt; top_genes_each_celltype &lt;- sapply(1:ncol(expr), function(i) order(expr[,i],decreasing =T)[1:40])
  &gt; dim(top_genes_each_celltype)
  [1] 40 9
  &gt; top_genes_all_celltypes &lt;- unique(as.vector(top_genes_each_celltype))
  &gt; length(top_genes_all_celltypes)
  [1] 358
  </code></pre>
  
  <p>Then you can build a biclustered heatmap with <code>expr[top_genes_all_celltypes,]</code>, for example, using <code>heatmap2</code>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-23T15:07:38.283Z
Answers.LastActivityDate: 2018-10-23T15:07:38.283Z
Answers.UserDisplayName: Scott Gigante
Body: <p>I was wondering if some one can help me with pseudo r code for my approach here based on this <a href="https://www.researchgate.net/publication/265550961_An_RNA-Sequencing_Transcriptome_and_Splicing_Database_of_Glia_Neurons_and_Vascular_Cells_of_the_Cerebral_Cortex" rel="noreferrer">paper</a> to plot a heatmap</p>
  <h1>Figure 3</h1>
  <p><a href="https://i.sstatic.net/Hna8E.png" rel="noreferrer"><img src="https://i.sstatic.net/Hna8E.png" alt="enter image description here" /></a></p>
  <p><strong>Figure description</strong>
  The top 40 enriched genes percelltypeare shown in a heat map.Onlyhighlyexpressed genes with FPKM ⬎20 areincludedin this analysis. Fold enrichment iscalculatedasFPKM of one cell type divided by the average FPKM of all other cell types. The majority of these genes showed specific expression by only one cell type, with the exception that some are expressed during more than one maturation stage in the oligodendrocyte lineage.</p>
  <p><strong>Similar description in their methods:</strong>
  Differential expression was calculated as the FPKM of a given cell type
  divided by the average FPKM of all other cell types. Genes were ranked by
  their fold enrichment in each cell type, and top enriched genes for each
  cell type were identified</p>
  <p>In my case I have 9 cell types(columns) and ~24000 genes(rows) from mouse and the values are TPMs.</p>
  <p>I know to order celltype only based on one column like this</p>
  <pre><code>expr.order = expr[order(expr[,1],decreasing =T),]
  </code></pre>
  <p>Which clearly not the right way and would need some help with code in r</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-09-20T15:54:17.540Z
Comments.Text: It’s unclear from your text what you actually want to do: *there’s no question*. For ordering the expression values based on one column you already found the solution. If that’s not what you mean you need to be more specific.
Comments.UserDisplayName: Konrad Rudolph
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-09-20T16:37:11.010Z
Comments.Text: I do not know how to achieve this *Genes were ranked by their fold enrichment in each cell type*  which I guess means rank individual column independently not sure how to do this
Comments.UserDisplayName: novicebioinforesearcher
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-09-21T11:23:41.567Z
Comments.Text: You can't rank individual columns independently (of the same data), but you can order them by several factors.
Comments.UserDisplayName: llrs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-09-21T12:03:33.910Z
Comments.Text: Well in that case you first need to compute fold changes using one of the existing differential expression packages, e.g. DESeq2 or edgeR.
Comments.UserDisplayName: Konrad Rudolph
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-09-24T20:52:49.630Z
Comments.Text: BTW, what else have you tried with R? Did you compute the fold change and found the top 40 as described in the image?
Comments.UserDisplayName: llrs
Tags: <r><rna-seq>
Title: cell type specific genes - heatmap using rank based approach
ThreadText: # cell type specific genes - heatmap using rank based approach
  
  (Asked by: username_0 on 2017-09-20)
  
  I was wondering if some one can help me with pseudo r code for my approach here based on this [paper](https://www.researchgate.net/publication/265550961_An_RNA-Sequencing_Transcriptome_and_Splicing_Database_of_Glia_Neurons_and_Vascular_Cells_of_the_Cerebral_Cortex) to plot a heatmap
  
  # Figure 3
  
  [![enter image description here](https://i.sstatic.net/Hna8E.png)](https://i.sstatic.net/Hna8E.png)
  
  **Figure description**
  The top 40 enriched genes percelltypeare shown in a heat map.Onlyhighlyexpressed genes with FPKM ⬎20 areincludedin this analysis. Fold enrichment iscalculatedasFPKM of one cell type divided by the average FPKM of all other cell types. The majority of these genes showed specific expression by only one cell type, with the exception that some are expressed during more than one maturation stage in the oligodendrocyte lineage.
  
  **Similar description in their methods:**
  Differential expression was calculated as the FPKM of a given cell type
  divided by the average FPKM of all other cell types. Genes were ranked by
  their fold enrichment in each cell type, and top enriched genes for each
  cell type were identified
  
  In my case I have 9 cell types(columns) and ~24000 genes(rows) from mouse and the values are TPMs.
  
  I know to order celltype only based on one column like this
  
  ```
  expr.order = expr[order(expr[,1],decreasing =T),]
  
  ```
  
  Which clearly not the right way and would need some help with code in r
  
  **Tags:** r, rna-seq
  
  > **Comments:**
  > **username_1**: It’s unclear from your text what you actually want to do: *there’s no question*. For ordering the expression values based on one column you already found the solution. If that’s not what you mean you need to be more specific.  
  > **username_0**: I do not know how to achieve this *Genes were ranked by their fold enrichment in each cell type*  which I guess means rank individual column independently not sure how to do this  
  > **username_2**: You can't rank individual columns independently (of the same data), but you can order them by several factors.  
  > **username_1**: Well in that case you first need to compute fold changes using one of the existing differential expression packages, e.g. DESeq2 or edgeR.  
  > **username_2**: BTW, what else have you tried with R? Did you compute the fold change and found the top 40 as described in the image?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_3 on 2018-10-23. Score: 2 | Accepted answer
  
  The figure caption says "the top 40 genes per celltype" - so we can simply use the code you provided for the top 40 genes for a single celltype, repeat this for all celltypes, and then remove any duplicate genes. Here's an example:
  
  ```
  > expr <- matrix(runif(9*24000), nrow=24000, ncol=9)
  > top_genes_each_celltype <- sapply(1:ncol(expr), function(i) order(expr[,i],decreasing =T)[1:40])
  > dim(top_genes_each_celltype)
  [1] 40 9
  > top_genes_all_celltypes <- unique(as.vector(top_genes_each_celltype))
  > length(top_genes_all_celltypes)
  [1] 358
  
  ```
  
  Then you can build a biclustered heatmap with `expr[top_genes_all_celltypes,]`, for example, using `heatmap2`.
  
  
  ---
  
  