Python functions learned in chemoinformatics

Introduction

Following on from Iterative processing of Python learned by chemoinformatics, I will explain "functions" 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 functions in Python

Creating and using functions

A function is a collection of a series of processes, and can be created by writing def function name (formal argument):. By the way, def is an abbreviation for" define ". Once you have created a function, all you have to do is call the function name to execute the processing described in the function. The return value (return value) can be written using return.

def show_smiles_pa():
    return 'OC(CCCCCCCCCCCCCCC)=O'


show_smiles_pa() # OC(CCCCCCCCCCCCCCC)=O

In the above example, it is a function that returns palmitic acid described in SMILES notation. After defining the function, call the function name show_smiles_pa to get the return value.

In the above example, there is no formal argument when creating a function, but it is possible to create a function with a formal argument. An example is shown below.

def oxidize_fatty_acid(smiles):
    
    if smiles.count('=') <= 1:
        return 'This fatty acid is not oxidized. '
    else:
        return smiles.replace('/C=C\\', 'C(O)C')


smiles_pa = 'OC(CCCCCCCCCCCCCCC)=O'
oxidize_fatty_acid(smiles_pa) # This fatty acid is not oxidized. 

smiles_la = 'OC(CCCCCCC/C=C\C/C=C\CCCCC)=O'
oxidize_fatty_acid(smiles_la) # OC(CCCCCCCC(O)CCC(O)CCCCCC)=O

In the above example, SMILES (character string) is received as a formal argument, and if it is a saturated fatty acid, This fatty acid is not oxidized. Is returned, and if it is an unsaturated fatty acid, the carbon chain is doubled. The return value is SMILES, which is a compound whose bond is oxidized.

It is also possible to set multiple formal arguments. An example is shown below.

def calculate_exact_mass(Cn, Un):
    return 12 * Cn + 1.00783 * (2 * Cn - 2 * Un) + 15.99491 * 2


calculate_exact_mass(16, 0)

calculate_exact_mass(18, 2)

You can also take keyword arguments, with keyword name = value. An example is shown below.

def abbreviate_fatty_acid(Cn, Un, sep=':'):
    return str(Cn) + sep + str(Un)


abbreviate_fatty_acid(16, 0) # 16:0

abbreviate_fatty_acid(16, 0, sep='_') # 16_0

In the above example, if the keyword argument sep is not specified when calling the function, the default value: when defining the function is used, and if sep is specified when calling the function, that value is used. It will be updated.

Variable scope

When using a function, you need to pay attention to the "variable scope". The scope of a variable is the range in which the variable can be used. In some cases, it can be used only inside a function (private variable), and in other cases it can be used outside a function (global variable).

def abbreviate_fatty_acid(a, b):
    Cn = a
    Un = b
    return Cn, Un


Cn = 16
Un = 0

abbreviate_fatty_acid(18, 2) # Cn = 18, Un = 2

print(Cn, Un) # Cn = 16, Un = 0

In the above example, the variables Cn and ʻUn that appear inside the function ʻabbreviate_fatty_acid are both private variables and are different from the Cn and ʻUndefined outside the function. So calling the function does not update the values of the variablesCn and ʻUn.

Here, if you want to update the values of variables Cn and ʻUn by calling the function, use globalandCn and ʻUn in the function are global variables instead of private variables. You need to show that there is.

def abbreviate_fatty_acid(a, b):
    global Cn
    global Un
    Cn = a
    Un = b
    return Cn, Un


Cn = 16
Un = 0

abbreviate_fatty_acid(18, 2) # Cn = 18, Un = 2

print(Cn, Un) # Cn = 18, Un = 2

In the above example, Cn and ʻUn are treated as global variables, so the values of Cn and ʻUn are also updated when the function is called.

Summary

Here, we have explained Python functions, focusing on practical knowledge that can be used in chemoinformatics. Let's review the main points again.

--A function is a set of processes that are described together. Once created, the process can be executed simply by calling it. --When dealing with variables within a function, you need to be careful about the scope of the variables.

Next, the following article explains Python classes.

Python classes learned with chemoinformatics

Reference materials / links

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

Recommended Posts

Python functions learned in chemoinformatics
Refactoring Learned in Python (Basic)
Overriding library functions in Python
What I learned in Python
Character code learned in Python
Python functions
[python] Manage functions in a list
Python data structures learned with chemoinformatics
I learned about processes in Python
Elementary ITK usage learned in Python
Dynamically define functions (methods) in Python
Quadtree in Python --2
Basic Linear Algebra Learned in Python (Part 1)
Python in optimization
[Python3] Dynamically define global variables in functions
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Easily use your own functions in Python
Conditional branching of Python learned by chemoinformatics
Pharmaceutical company researchers summarized functions in Python
Epoch in Python
Discord in Python
Sudoku in Python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
New features in Python 3.4.0 (3)-Single-dispatch generic functions
#Python basics (functions)
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
[Beginner] Python functions
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Python Easy-to-use functions
Quad-tree in Python
Python basics: functions
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
[Tips] Easy-to-read writing when connecting functions in Python
Statistical test grade 2 probability distribution learned in Python ②
Survival time analysis learned in Python 2 -Kaplan-Meier estimator