title: Python program to map a series of chemical reactions with unique nodes only appearing once
body: <p>I have written a program in python to compose a map of a chemistry an example of which is the below text in the format: reactants > products.</p>
  
  <pre><code>H2 &gt; H2
  H2 &gt; H2
  H2 &gt; H2*
  H2 &gt; H2*
  H2 &gt; H + H
  H2 &gt; H2^
  H2 &gt; H* + H
  H2 &gt; H + H
  H2^ &gt; H2^
  H2^ &gt; H^ + H
  H2^ &gt; H + H
  H3^ &gt; H3^
  H3^ &gt; H^ + H2
  H3^ &gt; H + H2
  H &gt; H
  H &gt; H*
  H &gt; H^
  H^ &gt; H^
  H^ &gt; H
  H + H2^ &gt; H2 + H^
  H2 + H2^ &gt; H + H3^
  CF4 &gt; CF4
  CF4 &gt; F- + CF3
  </code></pre>
  
  <p>I want my program to create nodes on the map for every species in the chemistry and draw pathways between reactants and products with one species appearing only once on the map and the map having lines to represent a reaction from each reactant in the reaction to each product in the reaction.</p>
  
  <p>I have written the below code however this code is simply taking each reaction and drawing it on the map and it is not connecting them and I am unsure how best to proceed linking up the common species in the reactions.</p>
  
  <pre><code>import os
  import sys
  import numpy as np
  import networkx as nx
  import matplotlib.pyplot as plt
  import copy
  
  try:
  data = sys.argv[1]
  except IndexError:
      print('Insert filename')
      sys.exit(1)
  
  def parse_line(line):
      new=line.split('&gt;')
      reactants=new[0].split('+')
      products=new[1].split('+')
      return reactants,products
  
  
  
  all_edges=[]
  edge_labels={}
  all_reactants=[]
  all_products=[]
  
  with open(data) as fi:
      for line in fi.readlines():
          line = line.strip()
          if not line or line[0] in '#%!':
          # skip blank and comment lines
              continue
          reactants,products = parse_line(line)
  
          for i in np.arange(len(reactants)):
              other_reactants=copy.copy(reactants)
              other_reactants.remove(reactants[i])
              other_reactants=', '.join(other_reactants)
              for j in np.arange(len(products)):
                  edge=(reactants[i],products[j])
                  all_edges.append(edge)
                  edge_labels[edge]=other_reactants
  
  
  
  gr=nx.DiGraph()
  
  gr.add_edges_from(all_edges)
  
  pos=nx.random_layout(gr)
  
  nx.draw_networkx_nodes(gr,pos,node_size=2000,node_shape='o',node_color='0.75',alpha=10)
  nx.draw_networkx_edges(gr,pos, width=0.05,edge_color='b')
  
  nx.draw_networkx_labels(gr, pos,font_size=12, font_color='k', font_weight='normal', alpha=1.0, ax=None)
  nx.draw_networkx_edge_labels(gr,pos,edge_labels=edge_labels, label_pos=0.01, ax=None, rotate=False)
  plt.show()
  </code></pre>
  
  <p>Could anyone advise me on the best way to proceed with this, I need a function that will identify the common species in reactants and products in all reactions and create a single node for each unique species and create one line from each reactant to each product for each reaction.</p>
  
  <p>Any help would be hugely appreciated</p>
  
  <p>Thank you very much</p>