Python Programming for Biology: Bioinformatics and Beyond


Example: molecule, protein and amino acid classes



Download 7,75 Mb.
Pdf ko'rish
bet94/514
Sana30.12.2021
Hajmi7,75 Mb.
#91066
1   ...   90   91   92   93   94   95   96   97   ...   514
Bog'liq
[Tim J. Stevens, Wayne Boucher] Python Programming

Example: molecule, protein and amino acid classes

Putting the code for the class definitions all together in one place, we have:

class Molecule:

def __init__(self, name):

if not name:

raise Exception('name must be set to something')

self.name = name

def getName(self):

return self.name

def getCapitalisedName(self):

name = self.getName()

return name.capitalize()

class Protein(Molecule):

def __init__(self, name, sequence):

Molecule.__init__(self, name)

self.aminoAcids = []

for code in sequence:

aminoAcid = AminoAcid(code)

self.aminoAcids.append(aminoAcid)

def getAminoAcids(self):

return self.aminoAcids

def getSequence(self):

return [aminoAcid.code for aminoAcid in self.aminoAcids]

def getMass(self):

mass = 18.02 # N-terminus H and C-terminus OH

aminoAcids = self.getAminoAcids()

for aminoAcid in aminoAcids:

mass += aminoAcid.getMass()

return mass

class AminoAcid:

massDict = { "A": 71.07, "R":156.18, "N":114.08, "D":115.08,

"C":103.10, "Q":128.13, "E":129.11, "G": 57.05,




"H":137.14, "I":113.15, "L":113.15, "K":128.17,

"M":131.19, "F":147.17, "P": 97.11, "S": 87.07,

"T":101.10, "W":186.20, "Y":163.17, "V": 99.13 }

acceptableCodes = set(massDict.keys())

def __init__(self, code):

if code not in self.acceptableCodes:

text = 'code = "%s", must be in list %s'

raise Exception(text % (code, sorted(self.acceptableCodes)))

self.code = code

def getMass(self):

return self.massDict[self.code]

And  here  is  an  example  of  these  objects  being  used,  with  some  self-explanatory  print

statements:

water = Molecule('Aqua')

print('molecule attributes')

print('molecule name =', water.name)

print('molecule function calls')

print('molecule name =', water.getName())

print('molecule capitalisedName =', water.getCapitalisedName())

myProtein = Protein('Fictitious', 'MPKAILV')

print('protein attributes')

print('protein name =', myProtein.name)

print('protein amino acids =', myProtein.aminoAcids)

print('protein function calls')

print('protein name =', myProtein.getName())

print('protein amino acids =', myProtein.getAminoAcids())

print('protein sequence =', myProtein.getSequence())

print('protein mass = ', myProtein.getMass())

We will expand on the idea of using classes to describe molecules in

Chapter 8

, where

we create an object hierarchy to describe molecular structures and the coordinates of their

component atoms.

Further details

This next section discusses some of the finer technical details of Python classes. If you are

a newcomer to object orientation then this section can be skipped on a first reading, as all

of  the  subsequent  examples  in  this  book  that  involve  Python  objects  can  be  understood

using what we have discussed above.


Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   90   91   92   93   94   95   96   97   ...   514




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish