I have a DNA sequence and would like to get the reverse complement to it using Python. It is located in one of the columns of the CSV file, and I would like to write the opposite complement to another column in the same file. The hard part is that there are several cells with something other than A, T, G, and C. I managed to get the opposite complement with this part of the code:
def complement(seq):
complement = {'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A'}
bases = list(seq)
bases = [complement[base] for base in bases]
return ''.join(bases)
def reverse_complement(s):
return complement(s[::-1])
print "Reverse Complement:"
print(reverse_complement("TCGGGCCC"))
However, when I try to find an item that is not in the add-ons dictionary using the code below, I just get the add-on to the latest database. This is not an iteration. I would like to know how I can fix this.
def complement(seq):
complement = {'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A'}
bases = list(seq)
for element in bases:
if element not in complement:
print element
letters = [complement[base] for base in element]
return ''.join(letters)
def reverse_complement(seq):
return complement(seq[::-1])
print "Reverse Complement:"
print(reverse_complement("TCGGGCCCCX"))