Usage Guide
This guide provides detailed information on how to use PyEvoMotion for various evolutionary analyses, including preparation of input data, command-line options, and interpretation of results.
Prerequisites
Before you begin, ensure you have:
PyEvoMotioninstalled (see Installation).A
FASTAfile containing DNA sequences.A metadata file with corresponding information for each sequence, of which the first column must match the sequence IDs in the
FASTAfile and must contain the collection date inYYYY-MM-DDformat in adatecolumn for each sequence.
Input Data Requirements
PyEvoMotion requires two primary input files:
FASTAfile: Contains nucleotide sequences to be analyzed.Metadata file: Contains information about each sequence, including:
A unique identifier matching the sequence ID in the
FASTAfile.Collection date for each sequence (in
YYYY-MM-DDformat).Additional metadata fields that can be used for filtering (optional).
Metadata File Format
The metadata file can be in CSV or TSV format with headers. Example structure:
sequence_id,date,country,variant,clade
seq1,2020-01-15,USA,Alpha,G
seq2,2020-02-20,Canada,Alpha,G
seq3,2020-03-10,UK,Alpha,GH
...
Command-Line Interface
Main Arguments
PyEvoMotion requires three mandatory arguments:
seqs: Path to the inputFASTAfile.meta: Path to the metadata file.out: Prefix for output files.
Example basic command:
PyEvoMotion sequences.fasta metadata.csv results/analysis1
Time Interval Options
The -dt or --delta_t parameter controls the time interval for analysis (please see pandas’ offset aliases for more details):
# Analyze with 30-day intervals
PyEvoMotion sequences.fasta metadata.csv results/analysis1 -dt 30D
# Analyze with 2-month intervals
PyEvoMotion sequences.fasta metadata.csv results/analysis1 -dt 2M
# Analyze with 1-year intervals
PyEvoMotion sequences.fasta metadata.csv results/analysis1 -dt 1Y
Filtering Options
PyEvoMotion provides several filtering options:
1. Length filtering
Filter out sequences shorter than a specified length (useful when analyzing low-coverage genomes):
PyEvoMotion sequences.fasta metadata.csv results/analysis1 -l 29000
2. Date range filtering
Restrict analysis to a specific time period:
PyEvoMotion sequences.fasta metadata.csv results/analysis1 -dr 2020-03-01..2020-12-31
3. Metadata-based filtering
Filter sequences based on metadata attributes (the filters to be applied will be specific to the particular metadata file used):
# Single value filter
PyEvoMotion sequences.fasta metadata.csv results/analysis1 -f country "United States"
# Multiple value filter
PyEvoMotion sequences.fasta metadata.csv results/analysis1 -f variant [Alpha Beta Gamma]
# Multiple filters
PyEvoMotion sequences.fasta metadata.csv results/analysis1 -f country "United States" clade [GH GRY]
4. Genome position filtering
Restrict analysis to specific regions of the genome (useful when analyzing the evolution of specific genes or genomic regions):
# Analyze positions 1000 to 2000
PyEvoMotion sequences.fasta metadata.csv results/analysis1 -gp 1000..2000
# Analyze from beginning to position 1000
PyEvoMotion sequences.fasta metadata.csv results/analysis1 -gp ..1000
# Analyze from position 28000 to the end
PyEvoMotion sequences.fasta metadata.csv results/analysis1 -gp 28000..
Visualization Options
Control the visualization and export of results:
# Show plots interactively
PyEvoMotion sequences.fasta metadata.csv results/analysis1 -sh
# Export plots as image files
PyEvoMotion sequences.fasta metadata.csv results/analysis1 -ep
# Both show and export plots
PyEvoMotion sequences.fasta metadata.csv results/analysis1 -sh -ep
Mutation Type Analysis
Specify which types of mutations to analyze:
# Analyze all mutation types separately
PyEvoMotion sequences.fasta metadata.csv results/analysis1 -k all
# Analyze only substitutions
PyEvoMotion sequences.fasta metadata.csv results/analysis1 -k substitutions
# Analyze only insertions and deletions combined
PyEvoMotion sequences.fasta metadata.csv results/analysis1 -k indels
JSON Configuration
For reproducibility, you can export and import run configurations:
# Export run configuration to JSON
PyEvoMotion sequences.fasta metadata.csv results/analysis1 -xj
# Import run configuration from JSON
PyEvoMotion -ij config.json
Reference Sequence
By default the reference is the sequence with the earliest collection date.
-ref/--refseq uses the first record of a FASTA file instead, which
keeps runs on different datasets comparable:
PyEvoMotion sequences.fasta metadata.csv results/analysis1 -ref wuhan-hu-1.fasta
Progress Output
-v/--verbose logs each stage of the pipeline and shows an alignment
progress counter on stderr; -vv adds debug output with timestamps. When
using the library, configure the PyEvoMotion logger from Python’s
logging module instead.
Re-using a Previous Run
Aligning every sequence against the reference is by far the slowest step.
-load/--load_mutation_instructions rebuilds the analysis from the
{prefix}.tsv written by an earlier run, so the alignment is skipped.
Filters (-f, -gp, -dr), the time interval and the mutation kind
can all be changed:
# First run: aligns everything and writes results/full.tsv
PyEvoMotion sequences.fasta metadata.csv results/full
# Re-analyse the same data for spring 2020 only, in seconds
PyEvoMotion sequences.fasta metadata.csv results/spring \
-load results/full.tsv -dr 2020-03-01..2020-05-31
In this mode the metadata file is ignored and the FASTA file is only read to
fetch the reference sequence (unless -ref is given). The data TSV is not
rewritten. Add -recount/--recount_mutation_types to recompute the
per-sequence mutation counts from the loaded instructions rather than
reusing the stored columns.
Interpreting Results
Output Files
PyEvoMotion generates several output files:
Data File:
{prefix}.tsv: One row per sequence with its metadata,N count, mutation counts andmutation instructions: a list ofs_P_B(baseBat positionP),i_P_BASES(BASESinserted so that the first inserted base sits at positionP) andd_P_BASES(BASESdeleted starting at positionP). Positions are 1-based reference coordinates for all three kinds (since 0.2.0; earlier versions wrote insertions and deletions 0-based). This file can be fed back in with-load.
Statistics Files:
{prefix}_stats.csv: Contains raw statistics for each time interval.{prefix}_regression_results.json: The model parameters and the goodness of fit.
Plot Files (when using
-ep):{prefix}_plots.pdf: The time series of mutation statistics.
Understanding Results
The main output of PyEvoMotion includes:
Statistical parameters (along with the best-fit model and the corresponding \(R^2\) value indicating the goodness of fit) describing the evolutionary process:
Anomalous molecular clock model:
expression: d*x^alphaparameters:\(\alpha\) (alpha): The diffusion exponent, indicating whether evolution follows standard diffusion (\(\alpha = 1\)) or anomalous diffusion (\(\alpha \neq 1\))
\(D\): The diffusion coefficient, representing the overall rate of evolution
r2: The \(R^2\) value of the model
Standard molecular clock model:
expression: mx,mx + bif the intercept is fit to be non-zeroparameters:m: The rate of mutationb: The intercept of the model (initial mutation rate; only present if the intercept is fit to be non-zero)
r2: The \(R^2\) value of the model
Time series data showing the mean and variance of the number of mutations over time along with the fit model.
A value of \(\alpha \approx 1\) suggests that mutations accumulate according to standard molecular clock models, while deviations indicate more complex evolutionary dynamics. In the case of \(\alpha \approx 1\), then the anomalous molecular clock model is equivalent to the standard molecular clock model with \(m = D\), \(b = 0\) and \(\alpha = 1\).
Example 1: Simple Analysis
Let’s start with a simple analysis using the default parameters:
PyEvoMotion path/to/sequences.fasta path/to/metadata.tsv output_prefix
- This command will:
Load sequences from the
FASTAfile.Load corresponding metadata.
Align every sequence against the reference with the bundled
MAFFTport.Calculate mutation statistics using a 7-day interval.
Save the results with the specified output prefix.
- The output will include:
Summary statistics for mutations over time.
Distribution parameters for the evolution model.
Alignment files for further analysis.
Example 2: Filtering Data by Date Range
To restrict your analysis to a specific time period:
PyEvoMotion path/to/sequences.fasta path/to/metadata.tsv output_prefix -dr 2020-01-01..2020-06-30
This will analyze only sequences from the first half of 2020.
Example 3: Analyzing Specific Mutation Types
If you’re interested in analyzing only certain types of mutations:
PyEvoMotion path/to/sequences.fasta path/to/metadata.csv output_prefix -k substitutions
Options for -k include: all, total, substitutions and indels.
Example 4: Visualizing Results
To generate and display plots of your analysis:
PyEvoMotion path/to/sequences.fasta path/to/metadata.csv output_prefix -sh -ep
The -sh flag shows the plots interactively, while -ep exports them as .pdf files.
Example 5: Advanced Filtering
PyEvoMotion allows for complex filtering based on metadata:
PyEvoMotion path/to/sequences.fasta path/to/metadata.csv output_prefix -f country "United States" variant [Alpha Delta]
This example filters sequences from the United States that belong to either the Alpha or Delta variants.
Advanced Topics
Using Docker
PyEvoMotion is available as a Docker image for easy deployment:
# Pull the Docker image
docker pull ghcr.io/luksgrin/pyevomotion:latest
# Run the image interactively. It contains ``PyEvoMotion`` fully installed.
docker run -it ghcr.io/luksgrin/pyevomotion:latest
Programmatic Usage
PyEvoMotion can also be used as a Python library:
from PyEvoMotion import PyEvoMotion
# Initialize PyEvoMotion with input files
# Please see the module documentation for more details on constructor arguments
pem = PyEvoMotion(
"sequences.fasta",
"metadata.csv",
dt="10D", # 10 days time interval
)
# Runs the analysis
# Please see the module documentation for more details on the analysis method
stats, reg = instance.analysis(
length=29000, # Set a length filter to 29000 (for example; to remove short sequences)
mutation_kind="substitutions" # Set the type of mutations to analyze
)
# Pandas DataFrames with the statistics
print(stats)
# Dictionary with the model parameters and the goodness of fit
print(reg)
Troubleshooting
Problem: Too few sequences per time interval.
Solution: Increase the time interval with -dt to attempt to have more sequences per time interval.