import re, sys

# pre: A nucleotide sequence.  no spaces no whitespace
# post Reverse complemented sequence
def rc(seq):
  if re.search('[uU]',seq) and re.search('[tT]',seq):
    print "Mix of Uu and Tt in sequence.  I don't know what it is."
    sys.exit()
  rna = 0
  if re.search('[uU]',seq):
    rna = 1
  o = ''
  for i in range(0,len(seq)):
    c = seq[i]
    if c == 'A' and rna == 0: o = 'T' + o
    elif c == 'A' and rna == 1: o = 'U' + o
    elif c == 'C': o = 'G' + o
    elif c == 'G': o = 'C' + o
    elif c == 'T': o = 'A' + o
    elif c == 'U': o = 'A' + o
    elif c == 'a' and rna == 0: o = 't' + o
    elif c == 'a' and rna == 1: o = 'u' + o
    elif c == 'c': o = 'g' + o
    elif c == 'g': o = 'c' + o
    elif c == 't': o = 'a' + o
    elif c == 'u': o = 'a' + o
    else: o = c + o  #just use this odd character as is.
  return o


# pre:  A fasta file name (input)
# post: Dictionary containing names and counts
#       names only the first non-whitespace, not the whole header
# Modifies: file IO

def counts_by_name_from_fasta(in_fasta):
  reads = {}
  with open(in_fasta) as f:
    for line in f:
      line = line.rstrip()
      m = re.match('^>([\S]+)',line)
      if m:
        if not m.group(1) in reads:
          reads[m.group(1)] = 0