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
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.
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 variables
Cn and ʻUn
.
Here, if you want to update the values of variables Cn
and ʻUn by calling the function, use
globaland
Cn 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.
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
What is the programming language Python? Can it be used for AI and machine learning?
Recommended Posts