Answers.Body: <p>One way simply is to use VARNA, here is a link ... <a href="https://varna.lri.fr" rel="nofollow noreferrer">https://varna.lri.fr</a>. This is well known for its power to view secondary structure (e.g. its in Jalview). It is generally considered the best viewer app for secondary structure.</p>
  <p>However, the solution by @nquinoneso whilst simple, is preferable. The output is here:</p>
  <p><a href="https://i.sstatic.net/MC2ii.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/MC2ii.png" alt="enter image description here" /></a></p>
  <p>The y-axis is the 3' and the x-axis is the 5', the dots represent hairpins. Whilst its simple, the power of this approach is the automation by direct access to the (pandas) dataframe.</p>
  <p><em>I do recommend upvoting the post.</em></p>
  <p>Complex secondary structure would not work because it will confuse the 5' stems with the immediate 3' stems which don't hybridise and  a more advanced solution using nucleotide code would be needed. Thats the bit for further development, using the index both for the dot-bracket and the nucleotide sequence to confirm the hairpin. VARNA does perform this calculation (because it identifies canonical mutations)... but you can't access the underlying array/dataframe which is certainly a limitation.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-29T14:31:02.477Z
Answers.Text: Sorry, I got the x,y the wrong way round. The y-axis is the 5' end and the x-axis is the 3' end ... making the plot difficult to read. Obviously easy to correct.
Answers.UserDisplayName: M__
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-28T20:59:05.883Z
Answers.LastActivityDate: 2022-06-28T23:41:48.237Z
Answers.LastEditDate: 2022-06-28T23:41:48.237Z
Answers.UserDisplayName: M__
Answers.Body: <p>I wrote a function to do that a while ago, maybe it helps. It only works with dot-bracket notation, and it turns it into ordered pairs which then you can easily plot.</p>
  <pre class="lang-py prettyprint-override"><code>import pandas as pd
  import seaborn as sns
  
  
  def dot_bracket_to_pairs(ss_string):
      '''
      Takes dot and bracket string, returns dataframe
      with paired bases.
      If any invalid characters are in the structure, it
      will interpret them as dots, as it only reads parentheses.
      '''
      index_list = []
      pairs = {}
  
      for index, char in enumerate(ss_string):
          if char == '(':
              index_list.append(index)
          if char == ')':
              try:
                  # pair to last item in the list in dictionary
                  pairs[index_list.pop()] = index
              except IndexError:
                  print(f'Invalid structure, found extra \')\' in position {index}')
  
      if len(index_list) != 0:
          for item in index_list:
              print(f'Invalid structure, found extra in \'(\' in position {item}')
  
      df_pairs = pd.DataFrame(pairs.items(), columns=['Y', 'X'])
  
      return df_pairs
  
  db_string = '((((((((((..(((((((.......)))))))......).((((((.......))))))..)))))))))'
  df = dot_bracket_to_pairs(db_string)
  
  sns.scatterplot(data=df, x=&quot;X&quot;, y=&quot;Y&quot;)
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-28T21:16:55.310Z
Answers.Text: A cool, yet simple, solution, thank you. The plot is y-axis 5' and x-axis 3' ... switching would be clearer. A lot of coding is just the concept and this is cool.
Answers.UserDisplayName: M__
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-28T20:06:27.213Z
Answers.LastActivityDate: 2022-06-28T20:07:38.610Z
Answers.LastEditDate: 2022-06-28T20:07:38.610Z
Answers.UserDisplayName: nquinoneso
Body: <p>I need some help with forgi library for visualisation of RNA secondary structure.</p>
  
  <p>I have an RNA sequence and dot-bracket notation of the secondary structure and I would like to plot 2D graph like that you can find <a href="https://viennarna.github.io/forgi/graph_tutorial.html" rel="nofollow noreferrer">here</a>.</p>
  
  <p>The input file is a <code>.fx</code> file, or a fasta file where the sequence is single-line and the third line is the <a href="http://eternawiki.org/wiki/index.php5/Dot-Bracket_Notation" rel="nofollow noreferrer">dot-bracket notation</a> of the RNA structure.</p>
  
  <pre><code>&gt;1y26
  CGCUUCAUAUAAUCCUAAUGAUAUGGUUUGGGAGUUUCUACCAAGAGCCUUAAACUCUUGAUUAUGAAGUG
  ((((((((((..(((((((.......)))))))......).((((((.......))))))..)))))))))
  </code></pre>
  
  <p><a href="https://i.sstatic.net/OlrLz.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/OlrLz.png" alt="enter image description here"></a></p>
  
  <p>If there any other possibilities to plot it in real-time (for instance, in jupyter notebook avoiding any crossing external scripts), I will be very thankful to learn them!</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-12-11T14:13:47.370Z
Comments.Text: Hi there, do I understand right that you got an RNA sequence and you with to predict the secondary structure and plot it within a jupyter notebook? Could you maybe [edit] your question to clarify what is exactly your input and desired output?
Comments.UserDisplayName: Kamil S Jaron
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-12-11T17:54:46.570Z
Comments.Text: cross posted : https://www.biostars.org/p/353919/
Comments.UserDisplayName: Pierre
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-12-12T08:28:37.477Z
Comments.Text: @KamilSJaron, hi!
  
  I have an RNA sequence with it dot-bracket notation, for instance:
  seq = 'AAAACCGGGCCUUUUACCCCAAAUUGGAA'
   dot_bracket = '((((..(((..)))..))))...((..))'
  
  And for this seq and dot_bracket I want to plot 2D graph where edges will be biult according to dot_bracket and top will show the necessary nucleotide
Comments.UserDisplayName: kitsune_breeze
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-12-12T12:24:50.643Z
Comments.Text: @kitsune_breeze Ah, I got it. Just note that you can [edit] your question to improve and I would strongly encourage you to do that if you find new clues or to add more details. For instance, what was the problem with forgi library? Could you share what you tried and what was the error you got?
Comments.UserDisplayName: Kamil S Jaron
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-12-12T12:56:50.067Z
Comments.Text: @KamilSJaron, thanks. I cannot really understand how to rewrite, for instance, this command 'python examples/rnaConvert.py examples/input/1y26_ss.dotbracket -T neato | neato -Tpng -o 1y26_neato.png' into something that I can use in jupyter notebook. 
  
  I mean what functions I should call from forgi library that allows me to draw appropriate graph
Comments.UserDisplayName: kitsune_breeze
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-12-12T15:04:00.617Z
Comments.Text: Awesome, that's something we can work at. Just [edit] your question and add this information. Something like, "I know how to create the plot in terminal by running .... However, I don't know how to call the command from jupyter notebook". I am trying to convince you here, that you should not write more information in comments but directly to question. ;-)
Comments.UserDisplayName: Kamil S Jaron
Tags: <python><visualization><secondary-structure>
Title: How to plot a 2D RNA structure in python using dot-bracket input?
ThreadText: # How to plot a 2D RNA structure in python using dot-bracket input?
  
  (Asked by: username_0 on 2018-12-11)
  
  I need some help with forgi library for visualisation of RNA secondary structure.
  
  I have an RNA sequence and dot-bracket notation of the secondary structure and I would like to plot 2D graph like that you can find [here](https://viennarna.github.io/forgi/graph_tutorial.html).
  
  The input file is a `.fx` file, or a fasta file where the sequence is single-line and the third line is the [dot-bracket notation](http://eternawiki.org/wiki/index.php5/Dot-Bracket_Notation) of the RNA structure.
  
  ```
  >1y26
  CGCUUCAUAUAAUCCUAAUGAUAUGGUUUGGGAGUUUCUACCAAGAGCCUUAAACUCUUGAUUAUGAAGUG
  ((((((((((..(((((((.......)))))))......).((((((.......))))))..)))))))))
  
  ```
  
  [![enter image description here](https://i.sstatic.net/OlrLz.png)](https://i.sstatic.net/OlrLz.png)
  
  If there any other possibilities to plot it in real-time (for instance, in jupyter notebook avoiding any crossing external scripts), I will be very thankful to learn them!
  
  **Tags:** python, visualization, secondary-structure
  
  > **Comments:**
  > **username_1**: Hi there, do I understand right that you got an RNA sequence and you with to predict the secondary structure and plot it within a jupyter notebook? Could you maybe [edit] your question to clarify what is exactly your input and desired output?  
  > **username_2**: cross posted : https://www.biostars.org/p/353919/  
  > **username_0**: @username_1, hi!  I have an RNA sequence with it dot-bracket notation, for instance: seq = 'AAAACCGGGCCUUUUACCCCAAAUUGGAA'  dot_bracket = '((((..(((..)))..))))...((..))'  And for this seq and dot_bracket I want to plot 2D graph where edges will be biult according to dot_bracket and top will show the necessary nucleotide  
  > **username_1**: @username_0 Ah, I got it. Just note that you can [edit] your question to improve and I would strongly encourage you to do that if you find new clues or to add more details. For instance, what was the problem with forgi library? Could you share what you tried and what was the error you got?  
  > **username_0**: @username_1, thanks. I cannot really understand how to rewrite, for instance, this command 'python examples/rnaConvert.py examples/input/1y26_ss.dotbracket -T neato | neato -Tpng -o 1y26_neato.png' into something that I can use in jupyter notebook.   I mean what functions I should call from forgi library that allows me to draw appropriate graph  
  > **username_1**: Awesome, that's something we can work at. Just [edit] your question and add this information. Something like, "I know how to create the plot in terminal by running .... However, I don't know how to call the command from jupyter notebook". I am trying to convince you here, that you should not write more information in comments but directly to question. ;-)  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_3 on 2022-06-28. Score: 1
  
  One way simply is to use VARNA, here is a link ... <https://varna.lri.fr>. This is well known for its power to view secondary structure (e.g. its in Jalview). It is generally considered the best viewer app for secondary structure.
  
  However, the solution by @username_4 whilst simple, is preferable. The output is here:
  
  [![enter image description here](https://i.sstatic.net/MC2ii.png)](https://i.sstatic.net/MC2ii.png)
  
  The y-axis is the 3' and the x-axis is the 5', the dots represent hairpins. Whilst its simple, the power of this approach is the automation by direct access to the (pandas) dataframe.
  
  *I do recommend upvoting the post.*
  
  Complex secondary structure would not work because it will confuse the 5' stems with the immediate 3' stems which don't hybridise and a more advanced solution using nucleotide code would be needed. Thats the bit for further development, using the index both for the dot-bracket and the nucleotide sequence to confirm the hairpin. VARNA does perform this calculation (because it identifies canonical mutations)... but you can't access the underlying array/dataframe which is certainly a limitation.
  
  > **Comments:**
  > **username_3**: Sorry, I got the x,y the wrong way round. The y-axis is the 5' end and the x-axis is the 3' end ... making the plot difficult to read. Obviously easy to correct.  
  
  ---
  
  ### Answer by username_4 on 2022-06-28. Score: 1
  
  I wrote a function to do that a while ago, maybe it helps. It only works with dot-bracket notation, and it turns it into ordered pairs which then you can easily plot.
  
  ```py
  import pandas as pd
  import seaborn as sns
  
  def dot_bracket_to_pairs(ss_string):
      '''
      Takes dot and bracket string, returns dataframe
      with paired bases.
      If any invalid characters are in the structure, it
      will interpret them as dots, as it only reads parentheses.
      '''
      index_list = []
      pairs = {}
  
      for index, char in enumerate(ss_string):
          if char == '(':
              index_list.append(index)
          if char == ')':
              try:
                  # pair to last item in the list in dictionary
                  pairs[index_list.pop()] = index
              except IndexError:
                  print(f'Invalid structure, found extra \')\' in position {index}')
  
      if len(index_list) != 0:
          for item in index_list:
              print(f'Invalid structure, found extra in \'(\' in position {item}')
  
      df_pairs = pd.DataFrame(pairs.items(), columns=['Y', 'X'])
  
      return df_pairs
  
  db_string = '((((((((((..(((((((.......)))))))......).((((((.......))))))..)))))))))'
  df = dot_bracket_to_pairs(db_string)
  
  sns.scatterplot(data=df, x="X", y="Y")
  
  ```
  
  > **Comments:**
  > **username_3**: A cool, yet simple, solution, thank you. The plot is y-axis 5' and x-axis 3' ... switching would be clearer. A lot of coding is just the concept and this is cool.  
  
  ---
  
  