Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Python for the Life Sciences: A Gentle Introduction to Python for Life Scientists
Python for the Life Sciences: A Gentle Introduction to Python for Life Scientists
Python for the Life Sciences: A Gentle Introduction to Python for Life Scientists
Ebook597 pages5 hours

Python for the Life Sciences: A Gentle Introduction to Python for Life Scientists

Rating: 0 out of 5 stars

()

Read preview

About this ebook

Treat yourself to a lively, intuitive, and easy-to-follow introduction to computer programming in Python. The book was written specifically for biologists with little or no prior experience of writing code - with the goal of giving them not only a foundation in Python programming, but also the confidence and inspiration to start using Python in their own research.

Virtually all of the examples in the book are drawn from across a wide spectrum of life science research, from simple biochemical calculations and sequence analysis, to modeling the dynamic interactions of genes and proteins in cells, or the drift of genes in an evolving population.

Best of all, Python for the Life Sciences shows you how to implement all of these projects in Python, one of the most popular programming languages for scientific computing. If you are a life scientist interested in learning Python to jump-start your research, this is the book for you.


What You'll Learn 
Write Python scripts to automate your lab calculations
Search for important motifs in genome sequences
Use object-oriented programming with Python
Study mining interaction network data for patterns
Review dynamic modeling of biochemical switches

Who This Book Is For

Life scientists with little or no programming experience, including undergraduate and graduate students, postdoctoral researchers in academia and industry, medical professionals, and teachers/lecturers.

“A comprehensive introduction to using Python for computational biology... A lovely book with humor and perspective”

 -- John Novembre, Associate Professor of Human Genetics, University of Chicago and MacArthur Fellow

“Fun, entertaining, witty and darn useful. Amagical portal to the big data revolution” -- Sandro Santagata, Assistant Professor in Pathology, Harvard Medical School

“Alex and Gordon’s enthusiasm for Python is contagious”

-- Glenys Thomson Professor of Integrative Biology, University of California, Berkeley

LanguageEnglish
PublisherApress
Release dateSep 27, 2019
ISBN9781484245231
Python for the Life Sciences: A Gentle Introduction to Python for Life Scientists

Related to Python for the Life Sciences

Related ebooks

Programming For You

View More

Related articles

Reviews for Python for the Life Sciences

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Python for the Life Sciences - Alexander Lancaster

    © Alexander Lancaster and Gordon Webster 2019

    A. Lancaster, G. WebsterPython for the Life Scienceshttps://doi.org/10.1007/978-1-4842-4523-1_1

    1. Getting Started with Python

    Setting Yourself Up to Use Python

    Alexander Lancaster¹  and Gordon Webster¹

    (1)

    Amber Biology, Cambridge, MA, USA

    Getting Python on Your Computer

    This is obviously the first step for the reader of this book, in order to be able to run your Python code. As far as editing your code goes, there are numerous options including using the kind of plain text editor that you probably already have on your computer. This is not an optimal solution by any means, but we’ll talk more about what tools you might want to use to edit and manage your Python code, after we’ve dealt with the primary issue of actually getting a Python distribution on your computer.

    For the purposes of our discussion, we’re going to assume that your computer is an Apple Mac running Apple’s macOS operating system, a platform with PC architecture running Microsoft’s Windows operating system, or a machine running one of the many flavors of the GNU/​Linux operating system. As of the time of writing of this book, this currently accounts for approximately 95% of all the desktop and laptop computers being used in the world.

    For the purposes of this book, we are not specifically including any mobile platforms like smartphones and tablets in our discussion. There are some distributions of Python for these computing platforms as well, but most of them are still relatively immature, and in any case, mobile computing platforms come with a variety of additional features such as touch screens, accelerometers, and GPS that any mobile programming language must address and are beyond the scope of this book. Pretty much all of the basic Python covered in this book however is equally applicable to Python distributions on these platforms as well. If you are interested in mobile computing using Python, you might want to take a look at the very interesting Kivy¹ Python platform.

    Wait – there’s more than one Python?

    At this point, it is worth inserting a little note about the different versions of Python that are out there. Just to be clear, we’re not talking here about all of the flavors and implementations of Python that are alternatives to the official distribution. We’re only talking about the official Python distribution that can be found at the Python web site.²

    Back in 2008, a new major version of Python, Version 3.0, was launched. It included some (largely subtle) changes to the language that had the unfortunate effect of breaking Python’s compatibility with a number of the most popular code libraries that many people were using in their code. This particularly impacted the scientific and technical community because some of these libraries (like NumPy ³) were core components of a great deal of the scientific and technical Python code being used around the world by the Python community.

    For this reason, a very large proportion of the Python community was originally slow to adopt any of the 3.x versions of the official Python distribution, preferring to remain instead with the last official Python 2.x release which was Python 2.7. However, with Python 2.7.x being phased out in 2020, most of the holdouts have either converted or plan to convert before the phase out date (in the first edition, we were among those holdouts – no longer – this should make Guido happy).

    What this all boils down to is that the Python code in this book has now been ported to using Python 3. With the looming discontinuation of Python 2.7, readers should download and use Python 3 to write and run the code presented in the book. (Moving from Python 3.x if you happen to have already learned Python 2.x is an incremental process that involves for the most part, just learning some subtle syntactical and behavioral differences between the two major versions.)

    If you’re curious enough to want to know more about this branching of Python versions, there’s even a special page dedicated to this issue, at the official Python web site. The official view that is reflected on that page is that everybody should now be using Python 3.x, especially since many of the most important and widely used, third-party code libraries have now been updated to support it. There is however acknowledgment that not all of these issues have been resolved at the time of writing this book and that there are still legitimate reasons for some people to continue using Python 2.x.

    Downloading a Python Distribution for Your Computer

    Don’t do this yet!

    We mean it – really – don’t do it!

    Chances are that your machine already has a Python distribution on it, especially if you’re using a Mac or a Linux machine. So before forging ahead and downloading Python from the official Python web site, be sure to check out the following sections that describe the best route to having a working Python distribution on your computer. If after reading the section that applies to your type of computer (or if your computer is a different platform from the three major desktop/laptop platforms described here), you decide that you do need to download and install a Python distribution – you can find downloads and installation instructions for the official Python distribution on the Downloads⁴ page at the Python web site. Remember also when you’re choosing a version to download, we recommend using Python version 3.x.

    Getting Python on a macOS Computer

    If you’re working on an Apple Mac, the good news is that you already have a Python distribution on your machine. Unfortunately, the default version is Python 2.7, so you will need install Python 3 separately alongside the existing Python distribution. So you will first need visit the Python site and download⁵ the latest macOSX version and run the installer, which should install like any other standard macOS package. After this is complete, go to your Applications folder and open the Utilities subfolder. Inside that folder, you should find the Terminal application that opens a command line window on your desktop.

    If you type python3 at the command line, you should see a welcome message in your terminal window, followed by the Python prompt indicating that the Python interpreter is ready to accept some input – something like this:

    Python 3.7.2 (v3.7.2:9a3ffc0492, Dec 24 2018, 02:44:43)

    [Clang 6.0 (clang-600.0.57)] on darwin

    Type help, copyright, credits or license for more information.

    >>>

    If you want to quickly do a further check just to satisfy yourself that your Python distribution is working, try entering these two lines of code at the Python prompt (>>>) and hit return after each one.

    >>> a = 4

    >>> print(a)

    You should see something like this:

    4

    >>>

    One quick word of warning for macOS users – the Python distribution on your Mac is actually used by the macOS operating system for various housekeeping tasks, and it would be unwise to replace it with a different version of Python. Another elegant way to install Python 3.x is to create a virtual environment in which to run your new Python distribution in a way that will not impact any existing Python distributions or libraries on your machine. Creating virtual environments for Python is beyond the scope of this book, but if you’re curious about how to do this, check out the documentation for the Python virtualenv⁶ tool.

    Getting Python on a Linux Computer

    If you run any of the mainstream Linux distributions (e.g., Ubuntu,⁷ Fedora,⁸ Debian⁹), Python 2.7.x will almost certainly already be pre-installed, but you may need to install Python 3 separately (normally this is in a separate package called python3 that can be installed in parallel with Python 2.7 – just as with macOSX). Since most of the users of Linux tend to be already a little familiar with the Unix command line, these instructions are a little less step-by-step than for other operating systems. Also, the exact way that different Linux distributions package and install software is so varied that a comprehensive guide is almost impossible. Suffice to say that to get the most of out of this book, you should use your Linux package manager to install the additional packages for matplotlib (for plotting), NumPy (Numeric Python), and pip (for installing packages from the PyPI repository, see the following section). For example, in Fedora the relevant packages are python3-matplotlib, python3-numpy, and python3-pip. Each of these distributions has graphical package management tools, a search on these packages should quickly reveal the particular package in question. Installing them from the command line is usually also fairly simple. For example, in Fedora you would run

    $ sudo dnf install python3-matplotlib python3-numpy python3-pip

    For Ubuntu and Debian, apt-get is the equivalent tool, although the package names may be slightly different.

    Getting Python on a Windows Computer

    Unfortunately, Microsoft Windows does not come pre-packaged with a Python distribution in the way that Mac and Linux platforms do. Fortunately however, the good folks at python.org offer a set of easy-to-use Windows installers for pretty much any existing version of Python you could ever want. Just as there is more than one version of Python currently in current use, there is also more than one version of Windows in current use. For the purposes of this book, we are going to assume that you are not stranded in Jurassic Park , using a 400 MHz PC with 64MB RAM, running Windows 98. The instructions we provide will be based upon an installation under the current version of Windows at the time of writing this book, which is Windows 10.¹⁰

    Our first stop to get Python on your Windows platform is obviously the Python web site.2 The Python web site has a very large menu bar front and center on the home page, which contains a convenient link to the Downloads page. Clicking this link should display a pop-up menu in your browser offering you the option to download the current version of Python 3.x. Select the Python 3.x option and the download should begin automatically. Depending upon how you have your Windows system configured, the file (called something like python-3.x.y.exe) will normally be saved in your Downloads folder, should you need to find it again.

    Once the file has finished downloading, you can run it to start the install. If you have admin privileges for your Windows machine, you should allow the default option of installing Python for all users, and it is recommended that you also check the box to add Python 3.x to your default PATH. Once you have selected these options, click Install now in the installer menu and answer yes to the question that asks if you wish to grant permission for the Python install to proceed. While Python is installing, you will see a Setup Progress with a green bar that shows you how the installation is progressing, and if all goes well, you will see a Setup was successful message. Once setup is complete, click the small Close button in the message window to exit the installer.

    To make sure your Python 3.x installation worked correctly, use the Windows start menu to pull up the Command Prompt application and just type python at the prompt. If the install worked properly, you should find yourself in a Python shell and see a Python startup message that looks something like this:

    Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32

    Type help, copyright, credits or license for more information.

    >>>

    To get out of the Python shell and back to the Windows command prompt, just type quit().

    One word of caution for Windows users – you will have to separately install the excellent Python plotting library matplotlib¹¹ that we use in several chapters of the book. You can take a look at the subsequent section in this chapter on how to install additional Python modules using tools like the Python package manager pip, for instructions on how to do this, but if you are looking for a real batteries-included Python distribution that comes with a load of this useful, additional stuff bundled with it already, you might want to check out WinPython¹² (but you will probably want to make sure you download and install WinPython 3.x if you wish to stay completely in sync with the code examples in this book).

    Using the IDLE Python Shell

    IDLE¹³ is a Python graphical user interface (GUI) that runs Python code in much the same kind of way that a terminal windows on Linux or a Mac, or the Windows command line tool, allows you to run shell commands that you type into it. IDLE even allows you to save your Python code in files and also to load and run those files. Among other things, most versions of IDLE will also color-code your Python code to highlight things like Python keywords, strings, method names, and so on, making your code easier to read and

    Enjoying the preview?
    Page 1 of 1