Python classes learned in chemoinformatics

Introduction

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

Creating and using classes

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.

Class inheritance

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`.

Summary

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

Reference materials / links

What is the programming language Python? Can it be used for AI and machine learning?

Recommended Posts

Python classes learned in chemoinformatics
Python functions learned in chemoinformatics
Refactoring Learned in Python (Basic)
What I learned in Python
Character code learned in Python
Python data structures learned with chemoinformatics
I learned about processes in Python
Elementary ITK usage learned in Python
Quadtree in Python --2
Basic Linear Algebra Learned in Python (Part 1)
Python in optimization
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Pharmaceutical company researchers summarized classes in Python
Conditional branching of Python learned by chemoinformatics
Epoch in Python
Discord in Python
Sudoku in Python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Statistical test grade 2 probability distribution learned in Python ②
Survival time analysis learned in Python 2 -Kaplan-Meier estimator
Statistical test grade 2 probability distribution learned in Python ①
TensorFlow: Run data learned in Python on Android
Sorted list in Python
Daily AtCoder # 36 in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 18 in Python