Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mass of a protein
#1
Hi

the exercise is the following:

Write a program to determine the mass in dalton of a protein. You can use the given dictionary with the masses (in Dalton) of the individual amino acids.

aa_mass = {
'A': 71.03711, 'G': 57.02146, 'M': 131.04049, 'S': 87.03203,
'C': 103.00919, 'H': 137.05891, 'N': 114.04293, 'T': 101.04768,
'D': 115.02694, 'I': 113.08406, 'P': 97.05276, 'V': 99.06841,
'E': 129.04259, 'K': 128.09496, 'Q': 128.05858, 'W': 186.07931,
'F': 147.06841, 'L': 113.08406, 'R': 156.10111, 'Y': 163.06333
}


Input
The first line contains the name (ID) of a protein. The subsequenct lines the amino acid sequence of a protein (60 characters at a time). The sequence ends with an empty line.

Output
A single line with a string: Protein #### has a mass of ####.## Da.

I don't know how the string (amino_acid) that the user writes in the program can be attached to the keys in the dictionary and then added up. The following code is what i already have: I know I have to use a for loop to go trough the dictionary but I don't know how the characters of the string amino_acid are compatible with the characters of the dictionary and added up.

aa_mass = {
    'A': 71.03711,   'G': 57.02146,   'M': 131.04049, 'S': 87.03203,
    'C': 103.00919,  'H': 137.05891,  'N': 114.04293, 'T': 101.04768,
    'D': 115.02694,  'I': 113.08406,  'P': 97.05276,  'V': 99.06841,
    'E': 129.04259,  'K': 128.09496,  'Q': 128.05858, 'W': 186.07931,
    'F': 147.06841,  'L': 113.08406,  'R': 156.10111, 'Y': 163.06333
}

gen_id = str(input())

amino_acid= str(input())
for i in aa_mass:
    

mass=sum(aa_mass[amino_acid])

print("Protein {} has a mass of {:.2f} Da.".format(gen_id, mass))```
Thank you in advance.
Reply
#2
aa_mass = {
    'A': 71.03711,   'G': 57.02146,   'M': 131.04049, 'S': 87.03203,
    'C': 103.00919,  'H': 137.05891,  'N': 114.04293, 'T': 101.04768,
    'D': 115.02694,  'I': 113.08406,  'P': 97.05276,  'V': 99.06841,
    'E': 129.04259,  'K': 128.09496,  'Q': 128.05858, 'W': 186.07931,
    'F': 147.06841,  'L': 113.08406,  'R': 156.10111, 'Y': 163.06333
}

gen_id = input('Enter gen ID:')
sequence = input('Enter amino acid sewuence:')
mass=sum(aa_mass.get(aa.upper(), 0) for aa in sequence)
print(f"Protein {gen_id} has a mass of {mass:.2f} Da.") # better use f-string
if you prefer ordinary loop instead of list comprehension

mass = 0
for aa in sequence:
    mass += aa_mass.get(aa.upper(), 0)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks alot! I'm glad you posted the code for ordinary loop because I haven't learned about list comprehension. And also thans for the tip to use f-strings, I searched about it and they easier and faster to use than .format Smile .
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020