Answers.Body: <p>How do you know the &quot;family&quot; to which your motifs belong?  If the source for the motifs that FIMO matched is, say, <a href="https://jaspar.elixir.no/search?q=&amp;collection=CORE&amp;tax_group=vertebrates" rel="nofollow noreferrer">JASPAR Vertebrata CORE</a>, then you might reasonably decide to use JASPAR's &quot;structural class&quot; as provided by their pre-computed &quot;Annotation table&quot; in <a href="https://jaspar.elixir.no/matrix-clusters/vertebrates/?detail=true" rel="nofollow noreferrer">JASPAR CORE Vertebrates clustering</a>.</p>
  <p>Assuming you are using JASPAR and you do make this decision...</p>
  <p>Your &quot;raw .tsv file&quot; is in <a href="https://meme-suite.org/meme/doc/fimo-output-format.html#tsv_results" rel="nofollow noreferrer">FIMO Results TSV Format</a>.  &quot;Joining&quot; it with a downloaded copy of the &quot;Annotation table&quot; serves to annotate it with additional columns, having the effect you seek of clustering the FIMO results on the &quot;structural class&quot; of each matched motif.  Joining can be implemented following <a href="https://sqlite-utils.datasette.io/en/stable/cli.html#id16" rel="nofollow noreferrer">Running queries directly against CSV or JSON</a> using the <a href="https://sqlite-utils.datasette.io/en/stable/index.html" rel="nofollow noreferrer">sqlite-utils</a> command-line utility.</p>
  <p>How you would then plot these features and encode the class as a color requires more detailed description what the plot is supposed to represent or look like.  For instance, do you &quot;want to graph the positions of these motifs along a horizontal graph&quot; with coordinates taken with respect to the TSS of the transcript from which the promoter is taken?</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-02-07T18:37:25.983Z
Answers.LastActivityDate: 2024-02-07T18:49:21.880Z
Answers.LastEditDate: 2024-02-07T18:49:21.880Z
Answers.UserDisplayName: malcook
Answers.Body: <p>Comment: I don't understand what you mean by &quot;I want to graph the positions of these motifs along a horizontal graph&quot;? Are you talking about a Manhatten plot? Or something else? And I'm not sure about &quot;filter the data by clustering the motifs by family&quot;. What type of clustering do you want to perform?</p>
  <p>Without further information/description it's difficult to understand what you're trying to do. If possible, please edit your question with an example of what your desired output. Also please indicate the language you're using for your visualisation, e.g. R, python, other.</p>
  <hr />
  <p>Answer: I may have completely misunderstood, but perhaps this approach could be adapted to suit. Using the example FIMO output file from <a href="https://meme-suite.org/meme/doc/examples/fimo_example_output_files/fimo.html?man_type=web" rel="nofollow noreferrer">https://meme-suite.org/meme/doc/examples/fimo_example_output_files/fimo.html?man_type=web</a>:</p>
  <pre class="lang-r prettyprint-override"><code>library(tidyverse)
  library(qqman)
  #&gt; 
  #&gt; For example usage please run: vignette('qqman')
  #&gt; 
  #&gt; Citation appreciated but not required:
  #&gt; Turner, (2018). qqman: an R package for visualizing GWAS results using Q-Q and manhattan plots. Journal of Open Source Software, 3(25), 731
  #&gt; 
  
  fimo_df &lt;- read_tsv(&quot;~/Desktop/fimo.tsv&quot;, comment = &quot;#&quot;) %&gt;%
    rename(&quot;SNP&quot; = motif_id, &quot;CHR&quot; = sequence_name, &quot;BP&quot; = start,
           &quot;P&quot; = `p-value`, &quot;zscore&quot; = `q-value`) %&gt;%
    mutate(CHR = case_when(str_detect(CHR, &quot;X&quot;) ~ &quot;23&quot;,
                           str_detect(CHR, &quot;Y&quot;) ~ &quot;24&quot;,
                           TRUE ~ CHR)) %&gt;%
    mutate(CHR = parse_number(CHR)) %&gt;%
    select(-c(stop, strand, matched_sequence))
  #&gt; Rows: 1494 Columns: 10
  #&gt; ── Column specification ────────────────────────────────────────────────────────
  #&gt; Delimiter: &quot;\t&quot;
  #&gt; chr (5): motif_id, motif_alt_id, sequence_name, strand, matched_sequence
  #&gt; dbl (5): start, stop, score, p-value, q-value
  #&gt; 
  #&gt; ℹ Use `spec()` to retrieve the full column specification for this data.
  #&gt; ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
  
  manhattan(fimo_df, p = &quot;zscore&quot;, logp = TRUE, ylab = &quot;q-score&quot;,
            genomewideline = FALSE, suggestiveline = FALSE,
            main = &quot;Manhattan plot of TF motifs&quot;, 
            chrlabs = c(1:19, &quot;X&quot;, &quot;Y&quot;))
  </code></pre>
  <p><img src="https://i.imgur.com/n1DHEK6.png" alt="" /></p>
  <p>If you want to colour the points by TF motif (or by 'cluster'), perhaps ggplot would be appropriate, e.g.</p>
  <pre class="lang-r prettyprint-override"><code>library(gghighlight)
  axis_set &lt;- fimo_df |&gt;
    group_by(CHR) |&gt;
    summarize(center = mean(BP, na.rm = TRUE))
  
  ylim &lt;- fimo_df |&gt;
    filter(P == min(P)) |&gt;
    mutate(ylim = abs(floor(log10(P))) + 2) |&gt;
    pull(ylim)
  
  ggplot(fimo_df, aes(x = BP, y = -log10(P),
                      color = as.factor(CHR), 
                      size = -log10(P))) +
    geom_point(alpha = 0.75) +
    # scale_x_continuous(label = sort(axis_set<span class="math-container">$CHR), breaks = sort(axis_set$</span>center)) +
    scale_y_continuous(expand = c(0, 0), limits = c(3.95, ylim)) +
    scale_color_manual(values = rep(
      c(&quot;#276FBF&quot;, &quot;#183059&quot;),
      unique(length(axis_set$CHR))
    )) +
    scale_size_continuous(range = c(0.5, 3)) +
    theme_minimal() +
    theme(
      legend.position = &quot;none&quot;,
      panel.grid.major.x = element_blank(),
      panel.grid.minor.x = element_blank(),
    ) +
    gghighlight(motif_alt_id == &quot;TBP&quot;)
  #&gt; Warning: Tried to calculate with group_by(), but the calculation failed.
  #&gt; Falling back to ungrouped filter operation...
  </code></pre>
  <p><img src="https://i.imgur.com/oCDLI84.png" alt="" /></p>
  <p><sup>Created on 2024-02-05 with <a href="https://reprex.tidyverse.org" rel="nofollow noreferrer">reprex v2.1.0</a></sup></p>
  <p>Does that help at all?</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-02-05T05:47:03.390Z
Answers.LastActivityDate: 2024-02-05T05:47:03.390Z
Answers.UserDisplayName: jared_mamrot
Body: <p>I have a FIMO output, the input sequences were putative promoter sequences of several species. I want to graph the positions of these motifs along a horizontal graph, but I want to filter the data by clustering the motifs by family, e.g. all the zinc finger motifs being represented by one color, all the helix loop helix motifs being represented by one color, etc. How would I go about clustering the raw .tsv file?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-02-04T18:54:23.990Z
Comments.Text: Please explain what FIMO is, and show the first few lines from your raw .tsv file. What language / tool would you like to use for clustering? Is this something you'd do directly in the FIMO tool, or externally using another program?
Comments.UserDisplayName: gringer
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-02-07T21:56:47.730Z
Comments.Text: JohnDoe23, are you looking for an output like the one shown here: https://bioinformatics.stackexchange.com/a/20132/3967
Comments.UserDisplayName: acvill
Tags: <sequence-analysis><motifs>
Title: How to classify TF motifs by family
ThreadText: # How to classify TF motifs by family
  
  (Asked by: username_0 on 2024-02-04)
  
  I have a FIMO output, the input sequences were putative promoter sequences of several species. I want to graph the positions of these motifs along a horizontal graph, but I want to filter the data by clustering the motifs by family, e.g. all the zinc finger motifs being represented by one color, all the helix loop helix motifs being represented by one color, etc. How would I go about clustering the raw .tsv file?
  
  **Tags:** sequence-analysis, motifs
  
  > **Comments:**
  > **username_1**: Please explain what FIMO is, and show the first few lines from your raw .tsv file. What language / tool would you like to use for clustering? Is this something you'd do directly in the FIMO tool, or externally using another program?  
  > **username_2**: username_0, are you looking for an output like the one shown here: https://bioinformatics.stackexchange.com/a/20132/3967  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_3 on 2024-02-07. Score: 1
  
  How do you know the "family" to which your motifs belong? If the source for the motifs that FIMO matched is, say, [JASPAR Vertebrata CORE](https://jaspar.elixir.no/search?q=&collection=CORE&tax_group=vertebrates), then you might reasonably decide to use JASPAR's "structural class" as provided by their pre-computed "Annotation table" in [JASPAR CORE Vertebrates clustering](https://jaspar.elixir.no/matrix-clusters/vertebrates/?detail=true).
  
  Assuming you are using JASPAR and you do make this decision...
  
  Your "raw .tsv file" is in [FIMO Results TSV Format](https://meme-suite.org/meme/doc/fimo-output-format.html#tsv_results). "Joining" it with a downloaded copy of the "Annotation table" serves to annotate it with additional columns, having the effect you seek of clustering the FIMO results on the "structural class" of each matched motif. Joining can be implemented following [Running queries directly against CSV or JSON](https://sqlite-utils.datasette.io/en/stable/cli.html#id16) using the [sqlite-utils](https://sqlite-utils.datasette.io/en/stable/index.html) command-line utility.
  
  How you would then plot these features and encode the class as a color requires more detailed description what the plot is supposed to represent or look like. For instance, do you "want to graph the positions of these motifs along a horizontal graph" with coordinates taken with respect to the TSS of the transcript from which the promoter is taken?
  
  
  ---
  
  ### Answer by username_4 on 2024-02-05. Score: 0
  
  Comment: I don't understand what you mean by "I want to graph the positions of these motifs along a horizontal graph"? Are you talking about a Manhatten plot? Or something else? And I'm not sure about "filter the data by clustering the motifs by family". What type of clustering do you want to perform?
  
  Without further information/description it's difficult to understand what you're trying to do. If possible, please edit your question with an example of what your desired output. Also please indicate the language you're using for your visualisation, e.g. R, python, other.
  
  ---
  
  Answer: I may have completely misunderstood, but perhaps this approach could be adapted to suit. Using the example FIMO output file from <https://meme-suite.org/meme/doc/examples/fimo_example_output_files/fimo.html?man_type=web>:
  
  ```r
  library(tidyverse)
  library(qqman)
  #> 
  #> For example usage please run: vignette('qqman')
  #> 
  #> Citation appreciated but not required:
  #> Turner, (2018). qqman: an R package for visualizing GWAS results using Q-Q and manhattan plots. Journal of Open Source Software, 3(25), 731
  #> 
  
  fimo_df <- read_tsv("~/Desktop/fimo.tsv", comment = "#") %>%
    rename("SNP" = motif_id, "CHR" = sequence_name, "BP" = start,
           "P" = `p-value`, "zscore" = `q-value`) %>%
    mutate(CHR = case_when(str_detect(CHR, "X") ~ "23",
                           str_detect(CHR, "Y") ~ "24",
                           TRUE ~ CHR)) %>%
    mutate(CHR = parse_number(CHR)) %>%
    select(-c(stop, strand, matched_sequence))
  #> Rows: 1494 Columns: 10
  #> ── Column specification ────────────────────────────────────────────────────────
  #> Delimiter: "\t"
  #> chr (5): motif_id, motif_alt_id, sequence_name, strand, matched_sequence
  #> dbl (5): start, stop, score, p-value, q-value
  #> 
  #> ℹ Use `spec()` to retrieve the full column specification for this data.
  #> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
  
  manhattan(fimo_df, p = "zscore", logp = TRUE, ylab = "q-score",
            genomewideline = FALSE, suggestiveline = FALSE,
            main = "Manhattan plot of TF motifs", 
            chrlabs = c(1:19, "X", "Y"))
  
  ```
  
  ![](https://i.imgur.com/n1DHEK6.png)
  
  If you want to colour the points by TF motif (or by 'cluster'), perhaps ggplot would be appropriate, e.g.
  
  ```r
  library(gghighlight)
  axis_set <- fimo_df |>
    group_by(CHR) |>
    summarize(center = mean(BP, na.rm = TRUE))
  
  ylim <- fimo_df |>
    filter(P == min(P)) |>
    mutate(ylim = abs(floor(log10(P))) + 2) |>
    pull(ylim)
  
  ggplot(fimo_df, aes(x = BP, y = -log10(P),
                      color = as.factor(CHR), 
                      size = -log10(P))) +
    geom_point(alpha = 0.75) +
    # scale_x_continuous(label = sort(axis_set$CHR), breaks = sort(axis_set$center)) +
    scale_y_continuous(expand = c(0, 0), limits = c(3.95, ylim)) +
    scale_color_manual(values = rep(
      c("#276FBF", "#183059"),
      unique(length(axis_set$CHR))
    )) +
    scale_size_continuous(range = c(0.5, 3)) +
    theme_minimal() +
    theme(
      legend.position = "none",
      panel.grid.major.x = element_blank(),
      panel.grid.minor.x = element_blank(),
    ) +
    gghighlight(motif_alt_id == "TBP")
  #> Warning: Tried to calculate with group_by(), but the calculation failed.
  #> Falling back to ungrouped filter operation...
  
  ```
  
  ![](https://i.imgur.com/oCDLI84.png)
  
  Created on 2024-02-05 with [reprex v2.1.0](https://reprex.tidyverse.org)
  
  Does that help at all?
  
  
  ---
  
  