Python Forum
Mass of a protein - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Mass of a protein (/thread-32995.html)



Mass of a protein - tfak - Mar-21-2021

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.


RE: Mass of a protein - buran - Mar-21-2021

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)



RE: Mass of a protein - tfak - Mar-22-2021

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 .