Hello fellow community,
I cannot figure out the problem here and therefore would like to have some imput from your side.
Here is my code:
from collections import defaultdict
class peptide():
aa_lib={
'A': {'H': 5, 'C': 3, 'O': 1, 'N': 1},
'C': {'H': 5, 'C': 3, 'S': 1, 'O': 1, 'N': 1},
'D': {'H': 5, 'C': 4, 'O': 3, 'N': 1},
'E': {'H': 7, 'C': 5, 'O': 3, 'N': 1},
'F': {'H': 9, 'C': 9, 'O': 1, 'N': 1},
'G': {'H': 3, 'C': 2, 'O': 1, 'N': 1},
'H': {'H': 7, 'C': 6, 'N': 3, 'O': 1},
'I': {'H': 11, 'C': 6, 'O': 1, 'N': 1},
'K': {'H': 12, 'C': 6, 'N': 2, 'O': 1},
'L': {'H': 11, 'C': 6, 'O': 1, 'N': 1},
'M': {'H': 9, 'C': 5, 'S': 1, 'O': 1, 'N': 1},
'N': {'H': 6, 'C': 4, 'O': 2, 'N': 2},
'P': {'H': 7, 'C': 5, 'O': 1, 'N': 1},
'Q': {'H': 8, 'C': 5, 'O': 2, 'N': 2},
'R': {'H': 12, 'C': 6, 'N': 4, 'O': 1},
'S': {'H': 5, 'C': 3, 'O': 2, 'N': 1},
'T': {'H': 7, 'C': 4, 'O': 2, 'N': 1},
'V': {'H': 9, 'C': 5, 'O': 1, 'N': 1},
'W': {'C': 11, 'H': 10, 'N': 2, 'O': 1},
'Y': {'H': 9, 'C': 9, 'O': 2, 'N': 1},
'H-': {'H': 1},
'-OH': {'O': 1, 'H': 1}
}
def __init__(self,sequence,rtime,intensity,protein):
self.sequence=sequence
self.carbon_atoms=0
self.nitrogen_atoms=0
self.hydrogen_atoms=0
self.sulfur_atoms=0
self.oxygen_atoms=0
self.rt=rtime
self.intensity=intensity
self.protein=protein
#self.charge=charge
def sumForm(self, aa_lib):
atom_comp = defaultdict(int)
for aa in self.sequence:
if aa in aa_lib.keys():
for atom, count in aa_lib[aa].items():
atom_comp[atom]+=count
self.carbon_atoms=atom_comp['C']
self.nitrogen_atoms=atom_comp['N']
self.hydrogen_atoms=atom_comp['H']
self.sulfur_atoms=atom_comp['S']
self.oxygen_atoms=atom_comp['O']
test=peptide("FKDDLA", 2.5, 2E+7, "OmpF")
test.sumForm
I do not understand why the atoms are not updated. The init values (0) still remain instead of the updated values. Outside of the class I can make it work.
You have two problems:
test.sumForm()
.You can either do test.sumForm(peptide.aa_lib)
, or you can modify your method to read the variable from the class directly, like this:
def sumForm(self): # note, no more aa_lib here
atom_comp = defaultdict(int)
for aa in self.sequence:
if aa in peptide.aa_lib.keys(): # peptide.aa_lib
for atom, count in peptide.aa_lib[aa].items():
atom_comp[atom]+=count
self.carbon_atoms=atom_comp['C']
self.nitrogen_atoms=atom_comp['N']
self.hydrogen_atoms=atom_comp['H']
self.sulfur_atoms=atom_comp['S']
self.oxygen_atoms=atom_comp['O']
You should either modify the call, or modify the method. Don't do both (otherwise you'll have a whole new set of errors).