Following on from Python functions learned by chemoinformatics, we will explain "classes" with the theme of lipidomics (comprehensive analysis of lipids). We will mainly explain practical examples of chemoinformatics, so if you want to check the basics, please read the following article before reading this article.
Pharmaceutical researcher summarized classes in Python
A class is an image like an object that holds variables and functions together.
It can be created by writing class class name:
.
In addition, there is an "initialization method" to describe the initial settings common to all the instances generated from the class, and it is described using __init__
.
class FattyAcid:
def __init__(self, c, u):
self.Cn = c
self.Un = u
self.abbreviation = str(c) + ':' + str(u)
self.exact_mass = 12 * c + 1.00783 * (2 * c - 2 * u) + 15.99491 * 2
def describe(self):
return f'The exact mass value of {self.abbreviation} is {self.exact_mass}.'
palmitic_acid = FattyAcid(16, 0) #Create an instance of "palmitic acid"
linoleic_acid = FattyAcid(18, 2) #Create an instance of "linoleic acid"
print(palmitic_acid.Cn) # 16
print(palmitic_acid.Un) # 0
print(palmitic_acid.abbreviation) # 16:0
print(palmitic_acid.exact_mass) # 256.24238
print(palmitic_acid.describe()) # The exact mass value of 16:0 is 256.24238.
print(linoleic_acid.Cn) # 18
print(linoleic_acid.Un) # 2
print(linoleic_acid.abbreviation) # 18:2
print(linoleic_acid.exact_mass) # 280.24238
print(linoleic_acid.describe()) # The exact mass value of 18:2 is 280.24238.
In the above example, we define a "class" called FattyAcid
(fatty acid) and generate" instances "called palmitic_acid
(palmitic acid) and linoleic_acid
(linoleic acid) from it.
It's a good idea to have the image that a "class" is like a template and an "instance" is like a concrete example.
In the initialization method, the number of carbon atoms, the number of double bonds, the precise mass, etc. are set, and they are automatically calculated when an instance is created.
Classes have the concept of "inheritance", and you can make an existing class a "parent class" and create a "child class" that gives the parent class additional functionality.
class FattyAcid:
def __init__(self, c, u):
self.Cn = c
self.Un = u
self.abbreviation = str(c) + ':' + str(u)
self.exact_mass = 12 * c + 1.00783 * (2 * c - 2 * u) + 15.99491 * 2
def describe(self):
return f'The exact mass value of {self.abbreviation} is {self.exact_mass}.'
class SaturatedFattyAcid(FattyAcid):
def oxidize(self):
return f'{self.abbreviation} cannot be oxidized. '
class UnsaturatedFattyAcid(FattyAcid):
def oxidize(self):
return f'{self.Un} double bond(s) can be oxidized. '
palmitic_acid = SaturatedFattyAcid(16, 0)
linoleic_acid = UnsaturatedFattyAcid(18, 2)
print(palmitic_acid.abbreviation)
print(palmitic_acid.describe())
print(palmitic_acid.oxidize())
print(linoleic_acid.abbreviation)
print(linoleic_acid.describe())
print(linoleic_acid.oxidize())
In the above example, the parent class FattyAcid
(fatty acid) produces the child classes SaturatedFattyAcid
(saturated fatty acid) and ʻUnsaturatedFattyAcid (unsaturated fatty acid). Saturated fatty acids are not oxidized at the double bond moiety, but unsaturated fatty acids are oxidized at the double bond moiety, so the difference is expressed by a method called ʻoxidize
.
Also, regardless of whether it is saturated fatty acid or unsaturated fatty acid, ʻexact_mass etc. can be calculated, so these are set by the initialization method of the parent class
FattyAcid`.
Here, we have explained Python classes, focusing on practical knowledge that can be used in chemoinformatics. Let's review the main points again.
--A class is like an object that holds variables and functions together. An instance is created based on the class. --Classes can also be inherited to create child classes.
Next, I will explain about NumPy in Python in the following article.
Learn with Cheminformatics NumPy
What is the programming language Python? Can it be used for AI and machine learning?
Recommended Posts