Skip to main content
VierApparatus
On this page
  1. References

Level 07 · Bioinformatics and Computational Biology

Python for Biological Research

Programming in Python for biological data, covering the core data types, reading and writing sequence files with Biopython, numerical work with NumPy and SciPy, writing functions and scripts that can be rerun, testing and version control, handling data too large for memory and reporting analyses so that they can be reproduced.

1 min read · Updated

Listen to this moduleComing soon

Learning objectives

  • Write Python code that reads a sequence file, filters or transforms its records and writes the results.
  • Structure an analysis as tested, versioned functions and scripts that another person can rerun.

Python is the most widely used language for biological data analysis, because it is readable, has a large library of scientific packages and can be used for small scripts and for large pipelines alike. This topic teaches the language in the form that biological work needs. It begins with the data types that hold sequences, annotations and measurements, and then moves to the libraries that read biological file formats and perform numerical work on the results.

The chapters build a working habit as well as a set of skills. Biopython provides the readers and the sequence objects that the bioinformatics topics rely on, and NumPy and SciPy provide the arrays and statistical routines for quantitative analysis. Code is organised into functions and scripts, tested, placed under version control and run from the command line, so that an analysis can be repeated.

The topic also addresses the scale of biological data. A sequencing run can contain millions of records, and a program that loads everything into memory will fail on a laptop. Iterators and streaming methods allow the same analysis to be performed on data of any size.

The final chapter covers reporting. Cock et al. (2009) described the Biopython package, and the practices that make a Python analysis trustworthy are those that Taschuk and Wilson (2017) and Sandve et al. (2013) describe for research software in general.

Chapters in this module

  • Why Python in Biology

    Why Python has become the common language of biological data analysis, the balance between readability and speed in a scientific language, the role of the interpreter, the packages that extend it to sequences and statistics and how a Python environment is set up so that the same code runs on different machines.

  • Data Types and Containers

    The Python data types used in biological analysis, including strings for sequences, integers and floats for counts and measurements, lists for ordered records, tuples for fixed groups, dictionaries for mapping identifiers to values and sets for membership tests, with examples of how each represents sequences, gene names and expression values.

  • Reading and Writing Sequence Files

    How to read FASTA and FASTQ files with Biopython, how the sequence records and their annotations are represented in memory, how to filter records by length or quality and write the results to a new file, and the common errors that occur when a file is malformed or when its quality encoding is misread.

  • Numerical Work with NumPy and SciPy

    How NumPy arrays store and operate on biological measurements without explicit loops, how SciPy provides statistical tests and optimisation routines, how a matrix of expression values is normalised and summarised and the statistical cautions needed when many tests are performed on the same data.

  • Functions and Scripts

    How to organise biological analysis code into functions with clear inputs and outputs, how to combine functions into a script that runs from the command line with parameters given as arguments, how to avoid global state and hard-coded paths, and how the structure of the code makes each step easier to test and to change.

  • Testing and Version Control

    Why biological analysis code must be tested against inputs with known outputs, how unit tests are written and run, the use of version control to record every change to the code and its results, the practices of committing small changes with clear messages and the way these practices protect a result from silent errors introduced by later edits.

  • Working with Large Data

    How to process biological files that are too large to load into memory, the use of iterators and generators to stream records one at a time, the choice between in-memory and on-disk structures, the use of indexes to reach a record without reading the whole file and the trade-offs between speed, memory and the complexity of the code.

  • Reporting Python Analyses

    How a Python analysis is reported so that its results can be reproduced, including the environment file that fixes package versions, the use of notebooks and their limits for reproducible work, the logging of parameters and data versions and the structure of a project directory that keeps raw data, code and results separate.

References

  1. Cock, P. J. A., Antao, T., Chang, J. T., Chapman, B. A., Cox, C. J., Dalke, A., Friedberg, I., Hamelryck, T., Kauff, F., Wilczynski, B., & de Hoon, M. J. L. (2009). Biopython: freely available Python tools for computational molecular biology and bioinformatics. Bioinformatics, 25(11), 1422-1423. https://doi.org/10.1093/bioinformatics/btp163
  2. Virtanen, P., Gommers, R., Oliphant, T. E., Haberland, M., Reddy, T., Cournapeau, D., Burovski, E., Peterson, P., Weckesser, W., Bright, J., van der Walt, S. J., Brett, M., Wilson, J., Millman, K. J., Mayorov, N., Nelson, A. R. J., Jones, E., Kern, R., Larson, E., . . . Vázquez-Baeza, Y. (2020). SciPy 1.0: fundamental algorithms for scientific computing in Python. Nature Methods, 17(3), 261-272. https://doi.org/10.1038/s41592-019-0686-2
  3. Taschuk, M., & Wilson, G. (2017). Ten simple rules for making research software more robust. PLOS Computational Biology, 13(4), e1005412. https://doi.org/10.1371/journal.pcbi.1005412
  4. Sandve, G. K., Nekrutenko, A., Taylor, J., & Hovig, E. (2013). Ten Simple Rules for Reproducible Computational Research. PLoS Computational Biology, 9(10), e1003285. https://doi.org/10.1371/journal.pcbi.1003285

Callouts marked Commentary or Speculation in the lesson separate interpretation and open questions from established findings.