#!/usr/bin/env python 

import argparse

nuc = {'A':'T','T':'A','G':'C','C':'G','K':'M','M':'K','R':'Y','Y':'R','S':'W','W':'W','B':'V','V':'B','H':'G','D':'C','X':'N','N':'N'}

def revComp(seq):
    rev = ''
    for i in range(len(seq) - 1,-1,-1):
        rev += nuc[seq[i]]

    return rev

def rc_fasta(args):
    header = ''
    seq = ''
    for line in args.filepath:
        if line[0] == ">":
            if header != '':
                print >>args.outputfile, header
                print >>args.outputfile, revComp(seq.upper())

            header = line.strip()
            seq = ''
        else:
            seq += line.strip()

    print >>args.outputfile, header
    print >>args.outputfile, revComp(seq.upper())

if __name__ == '__main__':
    parser = argparse.ArgumentParser( description="Reverse complement reads i a fasta file." )
    parser.add_argument( 'filepath', type=argparse.FileType('r'), help='Path to fasta file.' )
    parser.add_argument( 'outputfile', type=argparse.FileType('w'), help='Outputprefix. e.g. /path/to/output/fastafile_rc.fa' )


    args = parser.parse_args( )
    rc_fasta(args)
