Repository navigation
Ch 0.b Programming Basics
In this chapter we will explain our motivation for using python and go over some of the basics of programming.
This course is designed to teach you relevant programming and AI model development in Python. I've chosen Python as the language for this course because it is very commonly used in science applications, there are many modules available that can perform functions related to chemistry, biochemistry, and AI, and because it is a relatively beginner friendly programming language. Python can also be written in different styles of programming, it can be used to write simple scripts in a procedural style or be used in an object oriented or functional style of programming. If you are not familiar with these terms, that's ok. We will briefly discuss object oriented programming later in this section and it is not necessary to understand the other terms to complete the assignments in this course. But if you are interested in learning more, this is a good resource: https://www.scaler.com/topics/java/oop-vs-functional-vs-procedural/
Python does have several disadvantages. The main disadvantage is its speed - Python is slow compared to many other programming languages. If you are developing an application that needs to run quickly then it may be good to consider an alternative language such as C or C++. The good news is that many Python modules actually include code written in these faster languages, so that the most time consuming aspects of your program will run faster.
There are many other useful programming languages and some are more commonly used in science, the table below describes some common programming languages used in science applications. There are many more programming languages than the ones described below, I've just chosen to highlight those I have frequently encountered in science applications.
| language | common applications |
|---|---|
| R | statistical analysis, data visualization |
| C | applications that require speed, systems level programming (operating systems, running devices) |
| C++ | similar to C but with more features |
| Matlab | math, data visualization, processing of instrument data (common for chemistry instrumentation) |
| Java | used for many things, especially useful for apps where graphical user interface is needed |
| Perl | previously used often in bioinformatics, but python is more common now |
| Fortran | older language but still commonly used for math and modeling, including molecular modeling applications such as molecular dynamics |
There are several different ways to program in python. You can write the code as you execute it a terminal, you can prewrite your code as a .py file and then run it, or you can use Jupyter Notebooks. You can use any text editor to prewrite your code as a .py but it is easier to use what is called an integrated development environment (IDE). These will generally color code your code in a way that is helpful for the language you are using and also provide a range of development tools. You can also generally run your code directly through an IDE, although you can also use a terminal to run your python code using the command:
$python script_name.py
Writing a .py file is best for complex programs or programs that have long run times, this allows you to run your code on a computing cluster so that you do not need to leave your computer running. Vanderbilt's computing cluster is ACCRE. I use the IDE spyder, which is available through the Anaconda distribution of python, which you can download here: https://www.anaconda.com/
For instructional purposes, Jupyter notebooks are useful. They break the code into blocks in cells that can be run one at time with the code output being displayed in after the cell. Therefore, our interactive code examples will be written in Jupyter notebooks. Notebooks can also be run through Google Colab, which use the Google Cloud to run your code. This is useful if you need to use a GPU to run your code (as we will for some lessons) and to provide a standardized computer to install any modules which can help avoid issues that might happen on specific computers or operating systems with some modules. All notebooks are provided through this Github and a version is also available through GoogleColab.
Variable names should be informative and you should have an understanding of the information a variable stores from the name. Exceptions to this rule are index variables used in a loop which can be single characters generally, i, j, and k. Programming languages have specific guidelines for the style their code should be written in. These include things like capitalization of variable or function names, number of spaces for an indent, etc. We will not cover Python style in this course and you will not be graded on Python style. But if you would like to learn it you can see the official style guidelines here: https://peps.python.org/pep-0008/
Comments are lines in your code that are not actually run as part of the code and can be used to provide information about what the code is doing. You should comment your code, line comments can be indicated with a # character and block comments begin and end with three " characters, comments should be informative and can be used to remind yourself of what different parts of the code do and also communicate to others who may be reading your code. Example of how comments look:
#single line comment
"""
Block comment, everything in between the lines with the quotes will be in the comment.
"""
You should always test your code to make sure it is working as expected. This can be done manually, but the more formal way to do it is through unit testing, where you write tests to make sure individual components and methods in your code work as expected. Debugging is the process of fixing bugs or portions of your code that cause unexpected or undesired behavior. It can sometimes be challenging to find the source of a bug. One fast way of doing this is to add print statements to print variable values at various points in your code, to make sure your variables have the value you expect. A more thorough way to debug is to use debugging tools, often available in IDEs. These tools allow you to go through your code step by step and make sure it is behaving as expected. Because the code we will be writing for this class is relatively short, we will not be covering unit testing and debugging tools.
To learn some of the basics of python, I have provided two Jupyter notebooks with example code, you can find these in the 0_ProgrammingBasics folder of this repository. To run the code, you can either download or clone the whole repository, or download these individual notebook files. If you have Anaconda installed, you can then start Jupyter and open these notebooks to run them. You can also run these notebooks through Google Colab. You will need a gmail/google account to use Google Colab.
The first notebook can be accessed on Google Colab through the following URL:
https://colab.research.google.com/github/aswalker-lab/AIChemBiochem/blob/main/0_ProgrammingBasics/0.1%20Programming%20Basics%20in%20Python.ipynb
And the second through:
https://colab.research.google.com/github/aswalker-lab/AIChemBiochem/blob/main/0_ProgrammingBasics/0.2%20Python%20classes%20and%20functions.ipynb
If you would like to make changes and save them, you can make your own copy of the notebook through File->Save a copy in Drive
This will save a copy of your notebook in your Google Drive
Before running your code in Google Colab, you will need to connect to a runtime. This gives you access to a CPU or GPU on Google cloud servers where the code will run. To check what type of runtime you will get click the dropdown arrow in the upper right corner and then click "Change runtime type". You should have Python 3 as the runtime type and CPU as the Hardware Accelerator as shown in the image. For some future examples, you may want to use GPU to make the code run faster - I will specify when you should use GPU. Otherwise, assume you can use CPU.
How to select runtime type, the red circle indicates the arrow you need to click on to get this window.
Once you have confirmed you have the correct runtime type, click the Connect or Resume button next to that arrow to start the run time.
You can then run all cells by clicking "Run All" in the top menu. Or click the play button next to individual cells to just run those. This latter option is useful if you want to edit the code in one cell and then see how it changes the output.
To practice what we've learned, complete Exercise 0 where you will write code to calculate molecular weight from molecular formula. Starter code is provided here: https://github.com/aswalker-lab/AIChemBiochem/tree/main/0_ProgrammingBasics
You can run this on Google colab here: https://colab.research.google.com/github/aswalker-lab/AIChemBiochem/blob/main/0_ProgrammingBasics/Exercise%200.ipynb
If you are using this for a course, follow any course-specific instructions for submitting code.