def flip_ss(ss):
  """ Flips a secondary structure 
  Only flips the unpaired bases
  e.g. (.((....))....( ->
       ).((....))....)
  """
  bp_list = []
  unmatch_list = []
  ss = list(ss)
  for idx, c in enumerate(ss):
    if c == '(':
      bp_list.append(idx)
    elif c == ')':
      if len(bp_list):
        bp_list.pop()
      else:
        unmatch_list.append(idx)
  unmatch_list += bp_list
  for idx in unmatch_list:
    if ss[idx] == '(':
      ss[idx] = ')'
    else:
      ss[idx] = '('
  ss = ''.join(ss)
  return ss