#!/usr/bin/python

# define a dictionary which maps each letter to its complement
# these are key:value pairs separated by commas
complement = {
	'A':'T', 'C':'G', 'R':'Y', 'X':'X',
	'T':'A', 'G':'C', 'Y':'R' 
}

# ex. 0
#
# first step to the reverse complement is to reverse the sequence
# This prints each letter of the sequence
#

sequence = "ACGTacgtAcgtttata"

l = len(sequence)

for i in range(l):
	print sequence[i]


# ex. 1
#
# first step to the reverse complement is to reverse the sequence
# This prints each letter of the sequence in reverse order
#

sequence = "ACGTacgtAcgtttata"

l = len(sequence)

for i in range(l):
	print sequence[l-i-1]


# ex. 2
#
# first step to the reverse complement is to reverse the sequence
# This prints each letter of the sequence in reverse order
# and stores the letters in a list
#

sequence = "ACGTacgtAcgtttata"
reverse = []

l = len(sequence)

for i in range(l):
	reverse.append( sequence[l-i-1] )

print reverse

# ex. 3
#
# first step to the reverse complement is to reverse the sequence
# This prints each letter of the sequence in reverse order
# and stores the letters in a list
# and finally collapses the list into a string
#

sequence = "ACGTacgtAcgtttata"
reverse = []

l = len(sequence)

for i in range(l):
	reverse.append( sequence[l-i-1] )

reverse_string = ''.join(reverse)

print reverse_string

# ex. 4
#
# first step to the reverse complement is to reverse the sequence
# This prints each letter of the sequence in reverse order
# and stores the letters in a list
# and finally collapses the list into a string
# and now do the complement
#

sequence = "ACGTacgtAcgtttata".upper()
reverse = []

l = len(sequence)

for i in range(l):
	letter            = sequence[l-i-1] 
	letter_complement = complement.get(letter,'?') 
	reverse.append( letter_complement )

reverse_string = ''.join(reverse)

print reverse_string


# ex. 5
#
# A more compact version of ex. 4
#

sequence = "ACGTacgtAcgtttata".upper()
reverse = []

l = len(sequence)

for i in range(l):
	reverse.append( complement.get( sequence[l-i-1], '?') )

reverse_string = ''.join(reverse)

print reverse_string

# ex. 6
#
# a function made from ex. 5
#

def reverse_complement(sequence):

	reverse = []

	l = len(sequence)

	for i in range(l):
		reverse.append( complement.get( sequence[l-i-1], '?') )

	reverse_string = ''.join(reverse)

	return( reverse_string )

sequence = "ACGTacgtAcgtttata".upper()
r = reverse_complement(sequence)
print r


