Scientific Computing with NumPy and SciPy in Bioinformatics

  • Scientific computing is the application of numerical methods, mathematical models, algorithms, and computational tools to solve scientific problems. In bioinformatics, scientific computing enables researchers to analyze large biological datasets, perform statistical calculations, process genomic and transcriptomic measurements, model biological systems, and investigate relationships among genes, proteins, cells, and organisms. Python is widely used for scientific computing because it combines a readable programming language with specialized libraries for numerical analysis, statistics, optimization, signal processing, and scientific visualization.
  • NumPy and SciPy are two fundamental libraries in the Python scientific computing ecosystem. NumPy provides efficient multidimensional arrays, mathematical operations, linear algebra, random number generation, and tools for numerical data manipulation. SciPy builds on NumPy by providing advanced scientific algorithms for optimization, statistical analysis, numerical integration, interpolation, signal processing, sparse matrices, and other computational tasks. Together, these libraries support many analytical methods used in genomics, transcriptomics, proteomics, structural bioinformatics, systems biology, and computational evolution.
  • NumPy, short for Numerical Python, is designed to perform numerical calculations efficiently. Its central data structure is the ndarray, a multidimensional array that stores elements of a defined data type. Unlike ordinary Python lists, NumPy arrays generally store homogeneous numerical data in compact memory layouts and support vectorized operations. These properties make them especially useful when processing large matrices of gene-expression measurements, genetic variants, molecular descriptors, or experimental observations.
  • NumPy can be installed using Python’s package manager:
  • python -m pip install numpy scipy
  • Both libraries can then be imported into a Python program:

import numpy as np

import scipy

print(“NumPy version:”, np.__version__)

print(“SciPy version:”, scipy.__version__)

  • The conventional alias np is used for NumPy, while SciPy subpackages are commonly imported directly when required. Installing packages within an appropriate virtual environment helps avoid dependency conflicts between bioinformatics projects.
  • A NumPy array can be created from a Python list, a range of values, or a mathematical function. For example, a list of DNA sequence measurements, gene-expression values, or protein properties can be converted into an array for numerical analysis.

import numpy as np

gene_expression = np.array([12.5, 18.2, 9.7, 24.1, 16.8])

print(gene_expression)

print(“Number of measurements:”, gene_expression.size)

print(“Mean expression:”, np.mean(gene_expression))

print(“Standard deviation:”, np.std(gene_expression))

  • This example creates a one-dimensional array containing five illustrative expression measurements. NumPy calculates the mean and standard deviation without requiring an explicit loop. By default, np.std() calculates the population standard deviation using a divisor of (n). For the usual sample standard deviation, use np.std(gene_expression, ddof=1), provided the sample contains at least two observations. Choosing the appropriate definition depends on the statistical purpose of the analysis.
  • NumPy arrays can have one, two, or more dimensions. A one-dimensional array can represent measurements for a single gene across several samples, while a two-dimensional array can represent a gene-expression matrix with genes as rows and samples as columns. Higher-dimensional arrays can represent more complex data, such as measurements across genes, samples, time points, and experimental conditions.

import numpy as np

expression = np.array([

    [12, 15, 18],

    [25, 22, 28],

    [8,  10, 9]

])

print(“Matrix shape:”, expression.shape)

print(“Gene means:”, np.mean(expression, axis=1))

print(“Sample means:”, np.mean(expression, axis=0))

  • In this example, the matrix contains three genes measured in three samples. The axis parameter determines the direction of a calculation. Setting axis=1 calculates one mean for each gene across samples, whereas axis=0 calculates one mean for each sample across genes. Understanding array dimensions and axes is essential for avoiding errors when analyzing high-dimensional biological datasets.
  • Array indexing and slicing allow researchers to access specific measurements efficiently. Individual elements can be selected using their row and column positions, and subsets can be extracted without writing repetitive loops. Boolean indexing also allows values to be selected according to a condition. For example, researchers can identify genes whose expression exceeds a threshold, measurements outside an expected range, or variants with allele frequencies above a specified value. Careful handling of array shapes is important because some operations return views of existing data while others create copies.
  • Vectorization is one of NumPy’s most important features. Vectorized operations apply mathematical calculations to entire arrays using optimized underlying implementations. This reduces the need for Python-level loops and can substantially improve performance. For example, multiplying every expression value by a scaling factor can be accomplished using a single array operation. Broadcasting allows arrays with compatible shapes to participate in the same operation without manually repeating their values.

import numpy as np

expression = np.array([10, 20, 30, 40])

normalized = expression / np.mean(expression)

print(“Original:”, expression)

print(“Normalized:”, normalized)

  • This example scales each measurement by the overall mean. The result is a set of relative values centered around one, but it is not equivalent to every commonly used gene-expression normalization method. Real transcriptomic analyses may require library-size normalization, transformation, batch-effect assessment, and statistical models that account for sequencing depth and biological variability. NumPy supplies numerical operations, but the scientific meaning of the normalization must be determined separately.
  • NumPy provides functions for common mathematical operations, including sums, products, logarithms, exponentials, square roots, trigonometric functions, and element-wise comparisons. In bioinformatics, logarithmic transformations are often used to reduce skewness in measurement distributions, while exponentials arise in growth models and probability calculations. Researchers must select transformations that match the data type and analytical objective. For example, logarithms cannot be applied directly to negative values in ordinary real-valued calculations, and zero counts require special consideration when a logarithmic transformation is used.
  • Statistical summaries are another major application of NumPy. Functions such as np.mean(), np.median(), np.var(), np.std(), np.percentile(), and np.quantile() help summarize biological measurements. Researchers can use these functions to characterize gene expression, protein abundance, genomic feature lengths, sequencing quality metrics, or distributions of molecular properties. Missing values require particular care because many ordinary NumPy calculations propagate NaN values. Functions such as np.nanmean() can ignore NaN entries, but ignoring missing data is not always statistically appropriate and should be justified by the study design.
  • Linear algebra is central to scientific computing and many bioinformatics algorithms. NumPy provides matrix multiplication, matrix decomposition, determinant calculations, eigenvalue calculations, and solutions to systems of linear equations. These operations are used in dimensionality reduction, multivariate statistics, population genetics, molecular modeling, and the analysis of relationships among biological variables.

import numpy as np

A = np.array([

    [3, 1],

    [1, 2]

])

b = np.array([9, 8])

solution = np.linalg.solve(A, b)

print(“Solution:”, solution)

print(“Verification:”, A @ solution)

  • The example solves a system of linear equations represented by the matrix A and vector b. The @ operator performs matrix multiplication. Similar methods appear in regression models, numerical simulations, and transformations of biological measurements. Researchers should check whether a system is well-conditioned and whether a solution is scientifically meaningful, particularly when dealing with large or nearly singular matrices.
  • NumPy also supports random-number generation, which is important for simulation studies, resampling, permutation tests, and stochastic biological models. Its modern random-number interface uses np.random.default_rng() to create a random-number generator. Setting a seed can make a simulation reproducible, provided the software environment and relevant implementation details remain consistent.

import numpy as np

rng = np.random.default_rng(42)

simulated_expression = rng.normal(

    loc=10,

    scale=2,

    size=1000

)

print(“Simulated mean:”, simulated_expression.mean())

print(“Simulated standard deviation:”, simulated_expression.std())

  • This example generates 1,000 simulated values from a normal distribution with a mean of 10 and a standard deviation of 2. Such simulations can help evaluate statistical procedures or illustrate expected variability. Simulated values are not experimental observations, and the chosen distribution and parameters should reflect the scientific question being investigated.
  • While NumPy supplies fundamental numerical operations, SciPy provides higher-level scientific algorithms. Its functionality is organized into specialized modules, including scipy.stats for statistical distributions and tests, scipy.optimize for optimization, scipy.integrate for numerical integration, scipy.interpolate for interpolation, scipy.signal for signal processing, scipy.sparse for sparse matrices, and scipy.linalg for linear algebra. These tools are useful when a bioinformatics problem requires more than basic array arithmetic.
  • Statistical analysis is a major application of SciPy. The scipy.stats module provides probability distributions, statistical tests, correlation measures, summary statistics, and tools for estimating parameters. Researchers can use these functions to compare biological groups, investigate associations, characterize variability, and test hypotheses. For example, an independent-samples t-test can compare the means of two groups under its assumptions.

from scipy import stats

control = [5.2, 5.5, 4.9, 5.1, 5.4]

treated = [6.1, 6.4, 5.9, 6.5, 6.2]

result = stats.ttest_ind(

    control,

    treated,

    equal_var=False

)

print(“Test statistic:”, result.statistic)

print(“P-value:”, result.pvalue)

  • This example uses Welch’s t-test, which does not assume equal population variances. The resulting p-value measures how incompatible the observed test statistic is with the null hypothesis under the test’s assumptions. It does not measure the probability that the null hypothesis is true or establish biological importance. Real experiments require appropriate study design, checks of assumptions, effect-size estimates, confidence intervals, and corrections for multiple testing when many genes or proteins are examined.
  • SciPy also provides tools for correlation analysis. Pearson correlation measures the strength of a linear association between two variables, while Spearman correlation assesses the strength of a monotonic association using ranks. These methods can be used to investigate relationships between gene-expression levels, protein abundance, phenotypic traits, and environmental measurements. Correlation does not establish causation, and results may be affected by outliers, confounding variables, restricted ranges, and non-independent observations.
  • Optimization is another important scientific computing task. Many biological models involve parameters that must be estimated from observed data. For example, researchers may fit a mathematical model to gene-expression time courses, estimate kinetic parameters for biochemical reactions, or determine parameters that best explain population-growth measurements. SciPy’s scipy.optimize module includes routines for minimizing functions, solving equations, and fitting nonlinear models.

import numpy as np

from scipy.optimize import curve_fit

def growth_model(time, initial_value, rate):

    return initial_value * np.exp(rate * time)

time = np.array([0, 1, 2, 3, 4], dtype=float)

observed = np.array([2.0, 2.8, 4.1, 5.9, 8.2])

parameters, covariance = curve_fit(

    growth_model,

    time,

    observed,

    p0=[2.0, 0.3]

)

print(“Estimated parameters:”, parameters)

  • In this example, curve_fit() estimates the initial value and exponential growth rate that best fit the observed measurements under its least-squares framework. The covariance estimate provides information about parameter uncertainty under the method’s assumptions, but it should not automatically be interpreted as a complete description of uncertainty. Model choice, measurement errors, parameter identifiability, sample size, and residual patterns must also be considered.
  • Numerical integration is useful when biological processes are described by differential equations. Models of population dynamics, gene regulation, metabolic reactions, infection progression, and drug concentration can involve quantities that change continuously over time. SciPy provides numerical integration routines such as scipy.integrate.solve_ivp() for solving initial-value problems. Researchers can use these methods to simulate how biological systems evolve under specified assumptions and parameter values.
  • For example, a simple exponential-growth model can be represented by the differential equation (dN/dt=rN), where (N) is population size and (r) is the growth rate. Numerical solvers can approximate the solution when the governing equations are more complicated and cannot be solved analytically. Numerical results should be checked for appropriate solver settings, tolerances, time intervals, and biological plausibility. A mathematically accurate simulation does not guarantee that the underlying biological model represents the real system correctly.
  • Interpolation is used to estimate values between known measurements. In bioinformatics, interpolation may be helpful when processing time-series data, analyzing sampled signals, or comparing measurements collected at different intervals. SciPy provides interpolation tools for one-dimensional and multidimensional data. However, interpolation should not be confused with experimental observation, and extrapolating beyond the observed range can be particularly unreliable when biological processes are nonlinear.
  • Signal processing is another area supported by SciPy. The scipy.signal module includes filtering, peak detection, spectral analysis, and related operations. These tools can be applied to biological time-series data, physiological signals, electrophysiological measurements, and other numerical observations. For example, researchers may detect peaks in a measured signal or remove certain types of high-frequency noise. Filtering parameters should be selected carefully because preprocessing can alter important biological features, including the amplitude, timing, or shape of a signal.
  • Sparse matrices are useful when a dataset contains many zeros or when most entries are absent. Large gene-expression matrices, single-cell RNA-sequencing data, and biological interaction networks can be sparse. Storing such matrices in ordinary dense arrays may waste substantial memory because every element occupies storage, including zeros. SciPy provides sparse matrix and sparse array structures that store nonzero values and the information needed to locate them. The appropriate sparse representation depends on the operations being performed and the structure of the data.
  • Scientific computing also requires attention to numerical precision and stability. Computers represent many real numbers using floating-point approximations, which can introduce small rounding errors. Repeated operations, subtraction of nearly equal values, and calculations involving very large or very small magnitudes can amplify numerical problems. Researchers should avoid relying on exact equality comparisons for arbitrary floating-point results and instead use tolerances when appropriate. For example, np.isclose() can determine whether two numerical values are sufficiently close under specified absolute and relative tolerances.
  • Memory management and computational efficiency are particularly important in modern bioinformatics because genomic and transcriptomic datasets may contain millions of observations. NumPy arrays often provide much better performance than equivalent loops over ordinary Python lists, but large arrays can still consume substantial memory. Researchers should select suitable numerical data types, avoid unnecessary copies, process data in chunks when practical, and use sparse representations when the data structure supports them. For specialized large-scale tasks, tools such as Dask, GPU-enabled array libraries, and distributed computing frameworks may complement NumPy and SciPy.
  • NumPy and SciPy are not substitutes for every bioinformatics tool. They are particularly well suited to numerical computation, statistical methods, optimization, and mathematical modeling, but biological sequence formats and domain-specific operations often require specialized libraries. Biopython provides sequence objects, sequence parsing, alignment-related utilities, and access to biological annotations. pandas is useful for labeled tabular datasets, while scikit-learn supports machine learning. These tools can work together: Biopython can parse biological records, pandas can organize associated metadata, and NumPy or SciPy can perform numerical calculations and statistical analysis.
  • A reliable scientific computing workflow should include data validation, documented preprocessing, appropriate statistical assumptions, numerical checks, and reproducible software environments. Researchers should test functions on small examples with known results, inspect intermediate values, handle missing and invalid data explicitly, and compare important findings with independent calculations or trusted implementations. When analyzing clinical, experimental, or other sensitive biological data, data access controls and privacy requirements must also be respected.
  • In conclusion, NumPy and SciPy form a core part of Python’s scientific computing ecosystem. NumPy enables efficient array processing, vectorized calculations, statistical summaries, random simulations, and linear algebra, while SciPy provides advanced methods for statistical inference, optimization, numerical integration, interpolation, signal processing, and sparse computation. Together with specialized biological libraries, they provide powerful tools for analyzing high-dimensional datasets, testing hypotheses, fitting models, and investigating complex biological systems. Their effective use requires not only programming knowledge but also a sound understanding of statistics, numerical methods, and the biological questions being addressed.
Author:

Leave a Reply

Your email address will not be published. Required fields are marked *