"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"nbsphinx": "hidden",
"tags": []
},
"outputs": [],
"source": [
"# Colab setup ------------------\n",
"import os, sys, subprocess\n",
"if \"google.colab\" in sys.modules:\n",
" cmd = \"pip install --upgrade watermark\"\n",
" process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)\n",
"# ------------------------------"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Conditionals** are used to tell your computer to do a set of instructions depending on whether or not a Boolean is `True`. In other words, we are telling the computer:\n",
"\n",
" if something is true:\n",
" do task a\n",
" otherwise:\n",
" do task b\n",
"\n",
"In fact, the syntax in Python is almost exactly the same. As an example, let's ask whether or not a codon is the canonical start codon (`AUG`)."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This codon is the start codon.\n"
]
}
],
"source": [
"codon = 'AUG'\n",
"\n",
"if codon == 'AUG':\n",
" print('This codon is the start codon.')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The syntax of the `if` statement is apparent in the above example. The Boolean expression, `codon == 'AUG'`, is called the **condition**. If it is `True`, the indented statement below it is executed. This brings up a very important aspect of Python syntax.\n",
"\n",
"
\n",
"\n",
"Indentation matters.\n",
"\n",
"
\n",
"\n",
" \n",
"Any lines with the same level of indentation will be evaluated together."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This codon is the start codon.\n",
"Same level of intentation, so still printed!\n"
]
}
],
"source": [
"codon = 'AUG'\n",
"\n",
"if codon == 'AUG':\n",
" print('This codon is the start codon.')\n",
" print('Same level of intentation, so still printed!')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What happens if our codon is not the start codon?"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"codon = 'AGG'\n",
"\n",
"if codon == 'AUG':\n",
" print('This codon is the start codon.')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Nothing is printed. This is because we did not tell Python what to do if the Boolean expression `codon == 'AUG'` evaluated `False`. We can add that with an `else` **clause** in the conditional."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This codon is not the start codon.\n"
]
}
],
"source": [
"codon = 'AGG'\n",
"\n",
"if codon == 'AUG':\n",
" print('This codon is the start codon.')\n",
"else:\n",
" print('This codon is not the start codon.')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Great! Now, we have a construction that can choose which action to take depending on a value. So, if we're zooming along an RNA sequence, we could pick out the start codon and infer where translation would start. Now, what if we want to know if we hit a canonical stop codon (`UAA`, `UAG`, or `UGA`)? We can nest the conditionals!"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This codon is a stop codon.\n"
]
}
],
"source": [
"codon = 'UAG'\n",
"\n",
"if codon == 'AUG':\n",
" print('This codon is the start codon.')\n",
"else:\n",
" if codon == 'UAA' or codon == 'UAG' or codon == 'UGA':\n",
" print('This codon is a stop codon.')\n",
" else:\n",
" print('This codon is neither a start nor stop codon.')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice that the indentation defines which clause the statement belongs to. E.g., the second `if` statement is executed as part of the first `else` clause.\n",
"\n",
"We can be more concise and avoid the nesting by using an `elif` clause."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This codon is neither a start nor stop codon.\n"
]
}
],
"source": [
"codon = 'UGG'\n",
"\n",
"if codon == 'AUG':\n",
" print('This codon is the start codon.')\n",
"elif codon == 'UAA' or codon == 'UAG' or codon == 'UGA':\n",
" print('This codon is a stop codon.')\n",
"else:\n",
" print('This codon is neither a start nor stop codon.')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Computing environment"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Python implementation: CPython\n",
"Python version : 3.9.12\n",
"IPython version : 8.4.0\n",
"\n",
"jupyterlab: 3.4.4\n",
"\n"
]
}
],
"source": [
"%load_ext watermark\n",
"%watermark -v -p jupyterlab"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}