Python3: Using function - Amino Acid - Part 2 need help. Part 1 I was able to answer.?

Part 1:Write a function that takes two arguments – a protein sequence and an amino acid residue code – and returns the percentage of the protein that the amino acid makes up. Use the following assertions to test your function:

assert my_function("MSRSLLLRFLLFLLLLPPLP", "M") == 5

assert my_function("MSRSLLLRFLLFLLLLPPLP", "r") == 10

assert my_function("msrslllrfllfllllpplp", "L") == 50

assert my_function("MSRSLLLRFLLFLLLLPPLP", "Y") == 0

Part2:

Modify the function from part one so that it accepts a list of amino acid residues rather than a single one. If no list is given, the function should return the percentage of hydrophobic amino acid residues (A, I, L, M, F, W, Y and V). Your function should pass the following assertions: If no list is given, the function should return the percentage of hydrophobic amino acid residues (A, I, L, M, F, W, Y and V). Your function should pass the following assertions:

assert my_function("MSRSLLLRFLLFLLLLPPLP", ["M"]) == 5

assert my_function("MSRSLLLRFLLFLLLLPPLP", ['M', 'L']) == 55

assert my_function("MSRSLLLRFLLFLLLLPPLP", ['F', 'S', 'L']) == 70

assert my_function("MSRSLLLRFLLFLLLLPPLP") == 65

2 Answers

Relevance
  • 6 months ago
    Favorite Answer

    Your part 1 program has a test for a character from the string being equal to a given character. Maybe it's something like:

    if prot_seq[i] == aa_letter:

    .... count += 1

    There are more "pythonic" ways to do this, but that's the sort of code you'll probably be taught first and transfers well to other programming languages. The change you want to make is to add 1 to the count if the selected letter is any one of from a list instead of simply equal to a single letter, right? That looks like:

    if (prot_seq[i] in aa_list):

    .... count += 1

    That's a different use of the "in" keyword that with the "for i in range(n):" loop statement. In this case, it's an operator that tests to see if the value on the left occurs anywhere in the collection on the right:

    'a' in 'tanstaafl' # returns True

    'i' in 'team' # returns False

    As for "more pythonic" ways, I'd solve the first problem (counting occurrences of a single letter) with the built-in .count() method. Every sequence (list, string, tuple, etc.) supports this:

    count = prot_seq.count(aa_letter) # count occurrences of selected amino acid letter

    You don't need to write a loop--it's there, but part of the count method.

    That idea doesn't convert directly to counting occurrences of "any from this list", though. For that I'd use:

    count = sum(int(c in aa_list) for c in prot_seq )

    That looks at each character c from the given prot_seq sequence, tests to see if that character is one of the listed letters in aa_list, converts the True/False to an integer (1=True, 0=False) and adds up all of those integers. The result is a count of how many times the (c in aa_list) test was True(1).

  • EddieJ
    Lv 7
    6 months ago

    Part 2 says to modify the function from Part 1.

    You claim that you did Part 1, but you need to show us what you did. Isn't that obvious to you?

    Can you create an assignment statement that contains a string with all the hydrophobic amino acid residues?

Still have questions? Get your answers by asking now.