Answers.Body: <p>I don't think there is a problem</p>
  <pre class="lang-r prettyprint-override"><code>&gt; dds_spiders
  class: DESeqDataSet 
  dim: 5 3 
  metadata(1): version
  assays(1): counts
  rownames(5): gene1 gene2 gene3 gene4 gene5
  rowData names(0):
  colnames(3): rsem_outdir_NAi13t3n2_S1 rsem_outdir_NAi13t3n14_S2 rsem_outdir_NAi13t3n26_S3
  colData names(2): collection_times pulse
  &gt; colData(dds_spiders)
  DataFrame with 3 rows and 2 columns
                            collection_times    pulse
                                    &lt;factor&gt; &lt;factor&gt;
  rsem_outdir_NAi13t3n2_S1                 3      yes
  rsem_outdir_NAi13t3n14_S2                3      yes
  rsem_outdir_NAi13t3n26_S3                3      yes
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-09-26T21:07:55.570Z
Answers.Text: yeah, im braindead. Thanks...
Answers.UserDisplayName: Alehman
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-09-26T18:41:32.007Z
Answers.LastActivityDate: 2024-09-26T21:11:41.297Z
Answers.LastEditDate: 2024-09-26T21:11:41.297Z
Answers.UserDisplayName: swbarnes2
Body: <p>I am trying to make a Deseq2 object for analysis but cannot seem to get my metadata df to properly add to the object.</p>
  <pre class="lang-r prettyprint-override"><code># Simulate reading a gene expression count matrix
  spiders_rawcounts &lt;- data.frame(
    gene = paste0(&quot;gene&quot;, 1:5),  # Example gene names
    rsem_outdir_NAi13t3n2_S1 = rpois(5, 100),
    rsem_outdir_NAi13t3n14_S2 = rpois(5, 150),
    rsem_outdir_NAi13t3n26_S3 = rpois(5, 200),
    stringsAsFactors = FALSE
  )
  rownames(spiders_rawcounts) &lt;- spiders_rawcounts$gene
  spiders_rawcounts &lt;- spiders_rawcounts[, -1]
  
  ## Meta data 
  sample_names &lt;- colnames(spiders_rawcounts)
  get_pulse &lt;- function(sample_name) {
    if (grepl(&quot;i13&quot;, sample_name)) {
      return(&quot;yes&quot;)
    } else {
      return(&quot;no&quot;)
    }
  }
  get_collection_time &lt;- function(sample_name) {
    matches &lt;- regmatches(sample_name, gregexpr(&quot;t[0-9]+&quot;, sample_name))
    return(as.numeric(sub(&quot;t&quot;, &quot;&quot;, matches[[1]])))
  }
  # Create the metadata dataframe
  spiders_metadata &lt;- data.frame(sample_name = sample_names,collection_times = sapply(sample_names, get_collection_time),
    pulse = sapply(sample_names, get_pulse))
  spiders_metadata &lt;- subset(spiders_metadata, select = -sample_name)
  spiders_metadata<span class="math-container">$collection_times &lt;- factor(spiders_metadata$</span>collection_times)
  spiders_metadata<span class="math-container">$pulse &lt;- factor(spiders_metadata$</span>pulse)
  ## Reordering 
  reorder_indices &lt;- match(rownames(spiders_metadata),colnames(spiders_rawcounts))
  reordered_spider_rawcounts &lt;- spiders_rawcounts[ , reorder_indices]
  ## DESeq Object
  dds_spiders &lt;-DESeqDataSetFromMatrix(countData = spiders_rawcounts,colData = spiders_metadata,design = ~ collection_times + pulse,)
  summary(dds_spiders)
  
  all(colnames(reordered_spider_rawcounts) == rownames(spiders_metadata))
  </code></pre>
  <p>The summary returns <code>DESeqDataSet object of length 184076 with 0 metadata columns</code> and the last line returns <code>TRUE</code>. I have tried troubleshooting and am not sure what to do from here, any help would be greatly appreciated.</p>
  <hr />
  <p><strong>Response</strong>
  Alehman responded:</p>
  <pre class="lang-r prettyprint-override"><code>head(spiders_metadata)
                            collection_times pulse
  rsem_outdir_NAi13t3n2_S1                 3   yes
  rsem_outdir_NAi13t3n14_S2                3   yes
  rsem_outdir_NAi13t3n26_S3                3   yes
  rsem_outdir_NAi13t3n27_S4                3   yes
  rsem_outdir_NAi13t3n31_S5                3   yes
  rsem_outdir_NAi6t3n1_S6                  3    no
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-09-26T15:03:43.493Z
Comments.Text: Can you just show us what the top few lines of your metadata are?
Comments.UserDisplayName: swbarnes2
Tags: <deseq2>
Title: Beginner having issues adding metadata to DEseq2 object
ThreadText: # Beginner having issues adding metadata to DEseq2 object
  
  (Asked by: username_0 on 2024-09-26)
  
  I am trying to make a Deseq2 object for analysis but cannot seem to get my metadata df to properly add to the object.
  
  ```r
  # Simulate reading a gene expression count matrix
  spiders_rawcounts <- data.frame(
    gene = paste0("gene", 1:5),  # Example gene names
    rsem_outdir_NAi13t3n2_S1 = rpois(5, 100),
    rsem_outdir_NAi13t3n14_S2 = rpois(5, 150),
    rsem_outdir_NAi13t3n26_S3 = rpois(5, 200),
    stringsAsFactors = FALSE
  )
  rownames(spiders_rawcounts) <- spiders_rawcounts$gene
  spiders_rawcounts <- spiders_rawcounts[, -1]
  
  ## Meta data 
  sample_names <- colnames(spiders_rawcounts)
  get_pulse <- function(sample_name) {
    if (grepl("i13", sample_name)) {
      return("yes")
    } else {
      return("no")
    }
  }
  get_collection_time <- function(sample_name) {
    matches <- regmatches(sample_name, gregexpr("t[0-9]+", sample_name))
    return(as.numeric(sub("t", "", matches[[1]])))
  }
  # Create the metadata dataframe
  spiders_metadata <- data.frame(sample_name = sample_names,collection_times = sapply(sample_names, get_collection_time),
    pulse = sapply(sample_names, get_pulse))
  spiders_metadata <- subset(spiders_metadata, select = -sample_name)
  spiders_metadata$collection_times <- factor(spiders_metadata$collection_times)
  spiders_metadata$pulse <- factor(spiders_metadata$pulse)
  ## Reordering 
  reorder_indices <- match(rownames(spiders_metadata),colnames(spiders_rawcounts))
  reordered_spider_rawcounts <- spiders_rawcounts[ , reorder_indices]
  ## DESeq Object
  dds_spiders <-DESeqDataSetFromMatrix(countData = spiders_rawcounts,colData = spiders_metadata,design = ~ collection_times + pulse,)
  summary(dds_spiders)
  
  all(colnames(reordered_spider_rawcounts) == rownames(spiders_metadata))
  
  ```
  
  The summary returns `DESeqDataSet object of length 184076 with 0 metadata columns` and the last line returns `TRUE`. I have tried troubleshooting and am not sure what to do from here, any help would be greatly appreciated.
  
  ---
  
  **Response**
  username_0 responded:
  
  ```r
  head(spiders_metadata)
                            collection_times pulse
  rsem_outdir_NAi13t3n2_S1                 3   yes
  rsem_outdir_NAi13t3n14_S2                3   yes
  rsem_outdir_NAi13t3n26_S3                3   yes
  rsem_outdir_NAi13t3n27_S4                3   yes
  rsem_outdir_NAi13t3n31_S5                3   yes
  rsem_outdir_NAi6t3n1_S6                  3    no
  
  ```
  
  **Tags:** deseq2
  
  > **Comments:**
  > **username_1**: Can you just show us what the top few lines of your metadata are?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-09-26. Score: 2 | Accepted answer
  
  I don't think there is a problem
  
  ```r
  > dds_spiders
  class: DESeqDataSet 
  dim: 5 3 
  metadata(1): version
  assays(1): counts
  rownames(5): gene1 gene2 gene3 gene4 gene5
  rowData names(0):
  colnames(3): rsem_outdir_NAi13t3n2_S1 rsem_outdir_NAi13t3n14_S2 rsem_outdir_NAi13t3n26_S3
  colData names(2): collection_times pulse
  > colData(dds_spiders)
  DataFrame with 3 rows and 2 columns
                            collection_times    pulse
                                    <factor> <factor>
  rsem_outdir_NAi13t3n2_S1                 3      yes
  rsem_outdir_NAi13t3n14_S2                3      yes
  rsem_outdir_NAi13t3n26_S3                3      yes
  
  ```
  
  > **Comments:**
  > **username_0**: yeah, im braindead. Thanks...  
  
  ---
  
  