Zubair Khalid

Virologist/Molecular Biologist | Veterinarian | Bioinformatician

Conventional & Molecular Virology • Vaccine Development • Computational Biology

Dr. Zubair Khalid is a veterinarian and virologist specializing in conventional and molecular virology, vaccine development, and computational biology. Dedicated to advancing animal health through innovative research and multi-omics approaches.

Dr. Zubair Khalid - Veterinarian, Virologist, and Vaccine Development Researcher specializing in Computational Biology, Multi-omics, Animal Health, and Infectious Disease Research

Category: Guides

Rna Sequencing Analysis Python

RNA sequencing analysis in Python provides a flexible, reproducible framework for processing and interpreting transcriptome data using open source libraries and command line tools orchestrated through Python scripts. This guide is designed for bioinformaticians, life science researchers with intermediate programming experience, and graduate students who need a practical, source bounded approach to RNA seq analysis. The core workflow involves quality control, read alignment, quantification, differential expression testing, and functional interpretation all of which can be implemented or wrapped in Python. Training resources from EMBL-EBI Training offer structured tutorials on these steps.

RNA seq raw data is commonly deposited in public repositories such as the NCBI Sequence Read Archive. Using Python you can retrieve, process, and analyze these files. The recount3 Python package [9] provides a streamlined API to access uniformly processed RNA seq counts, which can save substantial computation time for re analysis of public data. This guide emphasizes practical decisions and execution steps supported by authoritative references.

At a Glance

Step Key Python Tools Purpose
Quality control FastQC (via subprocess), pyfastqc Assess base quality, GC content, adapter contamination
Trimming cutadapt (via Python), Trimmomatic wrapper Remove low quality bases and adapters
Alignment STAR (via subprocess), HISAT2 wrapper Map reads to reference transcriptome / genome
Quantification featureCounts (via subprocess), salmon (quant), recount3 API Obtain gene or transcript level counts
Differential expression pydeseq2, edgeR (via rpy2) Identify genes with significant expression changes
Functional analysis GSEApy, GO enrichment with goatools Interpret biological pathways and processes

Refer to Galaxy Training Network for interactive workflows that mirror these steps.

Decision Criteria

Before starting an RNA seq analysis pipeline you must decide on several parameters that influence tool selection and downstream interpretation.

First, choose between alignment based methods (STAR, HISAT2) and alignment free methods (salmon, kallisto). Alignment based approaches produce BAM files that support discovery of novel splice junctions and are required for variant calling. Alignment free methods run faster and are suitable for gene level quantification on well annotated genomes. The Bioconductor project provides extensive documentation on these trade offs, though its primary interface is R.

Second, determine whether your data is unstranded, stranded, or reversely stranded. Strandedness affects the direction of read mapping and the quantification of antisense transcripts. Many RNA seq protocols produce stranded libraries. Mis specifying this parameter leads to incorrect count tables.

Third, decide on the reference source. Use Ensembl or RefSeq gene annotations. Consistent annotation is critical when comparing samples from different experiments. The recount3 package [9] harmonizes annotations across datasets using a unified reference.

Fourth, consider experimental design factors: number of replicates, batch effects, and covariates. Differential expression methods such as pydeseq2 require a design matrix that accounts for these variables. The machine learning framework described in [8] demonstrates how modeling glycosphingolipid expression with ensemble methods can improve survival prediction, highlighting the importance of proper covariate handling.

Practical Workflow or Implementation Steps

The following implementation sequence assumes you have a Python environment with packages such as pandas, numpy, and matplotlib installed. Each step includes commands that can be executed in a Jupyter notebook or script.

Step 1: Data Retrieval

Download sample metadata and raw reads using the SRA Toolkit, which can be called via Python's subprocess module. Alternatively, use the recount3 Python package [9] to directly obtain gene level count matrices from uniformly processed samples.

import subprocess
## download FASTQ files for accession SRR123456
subprocess.run(['prefetch', 'SRR123456'])
subprocess.run(['fastq-dump', '--split-files', 'SRR123456'])

Step 2: Quality Control

Run FastQC on each FASTQ file and parse the HTML or text output for quality metrics. Use pyfastqc to generate summary tables.

import pyfastqc
fastqc_report = pyfastqc.Fastqc('sample_1.fastq')
fastqc_report.run()

Check per base sequence quality, GC content, adapter contamination, and duplication levels. Trim reads if necessary using cutadapt with a Python wrapper.

Step 3: Trimming

import cutadapt
from cutadapt import trim
adapters = ['AGATCGGAAGAGCACACGTCTGAACTCCAGTCA', 'AGATCGGAAGAGCGTCGTGTAGGGAAAGAGTGT']
trimmed = cutadapt.trim('sample_1.fastq', adapters=adapters)
trimmed.write('sample_1_trimmed.fastq')

Step 4: Alignment

Build a STAR genome index for your reference. Use subprocess to execute STAR alignment.

subprocess.run(['STAR', '--genomeDir', '/path/to/index', '--readFilesIn', 'sample_1_trimmed.fastq', '--outSAMtype', 'BAM', 'SortedByCoordinate', '--outFileNamePrefix', 'sample_1_'])

Alignments produce a BAM file. Verify alignment rates and read distribution using samtools flagstat.

Step 5: Quantification

Use featureCounts from the subread package to count reads mapping to genes.

subprocess.run(['featureCounts', '-a', 'annotation.gtf', '-o', 'counts.txt', 'sample_1_Aligned.sortedByCoord.out.bam'])

For alignment free quantification, call salmon.

subprocess.run(['salmon', 'quant', '-i', '/path/to/salmon_index', '-l', 'A', '-r', 'sample_1_trimmed.fastq', '-o', 'sample_1_quant'])

The recount3 Python package [9] can directly provide quantified counts from thousands of public datasets, bypassing alignment entirely for re analysis.

Step 6: Differential Expression

Load the count matrix into pandas and run pydeseq2.

import pydeseq2
from pydeseq2.dds import DeseqDataSet
dds = DeseqDataSet(counts='counts.csv', metadata='metadata.csv', design='condition')
dds.deseq2()
results = dds.results()

Examine the results by filtering on adjusted p value and log2 fold change.

Step 7: Functional Analysis

Perform over representation analysis or gene set enrichment using GSEApy.

import gseapy
enrich_res = gseapy.enrichr(gene_list='deg_genes.txt', gene_sets='KEGG_2021_Human')

The computational models in [8] demonstrate how downstream analyses can integrate expression data with metabolic modeling for disease classification.

Quality Checks

Throughout the workflow verify key metrics:

  • Base quality scores: median above 30 across most positions.
  • Alignment rate: at least 70% uniquely mapped reads for human RNA seq.
  • Gene body coverage: uniform across transcript lengths.
  • Duplication level: high duplication may indicate PCR bias.
  • Strandedness consistency: check using tools like infer_experiment.py from the RSeQC package.

The NCBI Bookshelf provides detailed background on sequencing quality metrics and their biological interpretation. For metagenomic RNA seq, quality filtering must remove rRNA contamination as shown in [11] using 16S and 23S rRNA targeted workflows.

Common Mistakes

  • Using default parameters without understanding strandedness or read length. This can lead to incorrect quantification and spurious differential expression.
  • Ignoring batch effects. Include batch as a covariate in the design formula for pydeseq2. The survival prediction work in [8] highlights how confounding factors undermine model performance.
  • Overlooking removal of lowly expressed genes. Prefiltering genes with fewer than 10 total counts across samples improves statistical power.
  • Running alignment free quantifiers on unadapter trimmed reads. Adapter contamination leads to false positives.
  • Misinterpreting p values without adjusting for multiple testing. Apply the Benjamini Hochberg correction.

Limits of Interpretation

RNA seq analysis does not directly measure protein abundance due to post transcriptional regulation and technical biases such as GC content and fragment length. Differential expression results are relative, not absolute. Lowly expressed genes have high variance and require deep sequencing for reliable detection. Isoform quantification remains less accurate than gene level quantification because of ambiguous read mapping. The MiRformer deep learning framework [6] addresses some limitations in predicting microRNA mRNA interactions but still depends on high quality paired sequence data.

Annotated genomes are essential, poorly annotated non model organisms make isoform and novel transcript detection unreliable. Public repos like NCBI Sequence Read Archive may contain metadata errors. Always verify sample labels and replicate structure. The recount3 package [9] applies uniform processing but cannot fix experimental design flaws.

Frequently Asked Questions

1. What Python packages are essential for a full RNA seq workflow? You need pydeseq2 or pyDESeq2 for differential expression, HTSeq or featureCounts (via subprocess) for quantification, cutadapt for trimming, and subprocess to run STAR or salmon. The recount3 package [9] offers an alternative that bypasses alignment.

2. Can I run the entire RNA seq analysis without leaving Python? Yes, by calling command line tools using subprocess and leveraging native Python packages for statistics and visualization. Alignment free methods like salmon can be launched from Python scripts.

3. How do I handle very large FASTQ files in Python? Process files iteratively or use streaming. Tools like pysam work with BAM files efficiently. Alternatively, use recount3 [9] to retrieve pre processed counts from uniformly processed data, which avoids large file handling.

4. Which differential expression method is most robust for small sample sizes? pydeseq2 uses a negative binomial model that performs well with as few as three replicates per group. Limma from Bioconductor (accessed via rpy2) is also robust. The ensemble approach in [8] shows that combining models can improve power but increases complexity.

References and Further Reading

Related Articles