2

So my translation works just fine, but when I run through assertion check, it doesn't pass it with an error saying: it should be string not tuple. I get the problem, but I just don't know how to solve it.

AssertionError:

     <class 'tuple'> != <class 'str'>

def frequency(dna_sequence):
    '''
    takes a DNA sequence (in string format) as input, parses it into codons using parse_sequence(),
    counts each type of codon and returns the codons' frequency as a dictionary of counts;
    the keys of the dictionary must be in string format
    '''
    codon_freq = dict()

    # split string with parse_sequence()
    parsed = parse_sequence(dna_sequence) # it's a function made previously, which actually makes a sequence of string to one-element tuple.  

    # count each type of codons in DNA sequence
    from collections import Counter
    codon_freq = Counter(parsed)

    return codon_freq

codon_freq1 = codon_usage(dna_sequence1)
print("Sequence 1 Codon Frequency:\n{0}".format(codon_freq1))

codon_freq2 = codon_usage(dna_sequence2)
print("\nSequence 2 Codon Frequency:\n{0}".format(codon_freq2))

Assertion check

assert_equal(codon_usage('ATATTAAAGAATAATTTTATAAAAATATGT'), 
             {'AAA': 1, 'AAG': 1, 'AAT': 2, 'ATA': 3, 'TGT': 1, 'TTA': 1, 'TTT': 1})
assert_equal(type((list(codon_frequency1.keys()))[0]), str)

About parse_sequence:

def parse_sequence(dna_sequence):
    codons = []

    if len(dna_sequence) % 3 == 0:
        for i in range(0,len(dna_sequence),3):
            codons.append((dna_sequence[i:i + 3],))

    return codons
OneCricketeer
  • 126,858
  • 14
  • 92
  • 185
colbyjackson
  • 165
  • 1
  • 10

2 Answers2

3

You might find it easier to use a Counter with a comprehension directly. e.g.

>>> s = 'ATATTAAAGAATAATTTTATAAAAATATGT'
>>> [s[3*i:3*i+3] for i in xrange(0, len(s)/3)]
['ATA', 'TTA', 'AAG', 'AAT', 'AAT', 'TTT', 'ATA', 'AAA', 'ATA', 'TGT']
>>> from collections import Counter
>>> Counter([s[3*i:3*i+3] for i in xrange(0, len(s)/3)])
Counter({'ATA': 3, 'AAT': 2, 'AAG': 1, 'AAA': 1, 'TGT': 1, 'TTT': 1, 'TTA': 1})
jq170727
  • 9,278
  • 2
  • 32
  • 50
2

You parsed correctly, however the results are tuples instead of desired strings, e.g.

>>> s = "ATATTAAAGAATAATTTTATAAAAATATGT"
>>> parse_sequence(s)
[('ATA',),
 ('TTA',),
 ('AAG',),
 ('AAT',),
 ('AAT',),
 ('TTT',),
 ('ATA',),
 ('AAA',),
 ('ATA',),
 ('TGT',)]

Just remove the trailing comma from this line:

    ...
    codons.append((dna_sequence[i:i + 3],))
    ...

FYI, sliding window is a technique that can be applied to codon matching. Here is a complete, simplified example using more_itertools.windowed (a third-party tool):

import collections as ct

import more_itertools as mit


def parse_sequence(dna_sequence):
    """Return a generator of codons."""
    return ("".join(codon) for codon in mit.windowed(dna_sequence, 3, step=3))

def frequency(dna_sequence):
    """Return a Counter of codon frequency."""
    parsed = parse_sequence(dna_sequence)
    return ct.Counter(parsed)

Tests

s = "ATATTAAAGAATAATTTTATAAAAATATGT"
expected = {'AAA': 1, 'AAG': 1, 'AAT': 2, 'ATA': 3, 'TGT': 1, 'TTA': 1, 'TTT': 1}
assert frequency(s) == expected
pylang
  • 28,402
  • 9
  • 97
  • 94