Rustynetics
Rustynetics is a high-performance Rust library designed for bioinformatics applications, offering efficient and scalable handling of common genomic file formats. It supports reading and writing of widely used formats such as BAM, FASTQ, FASTA, bigWig, bedGraph, BED, and GFF, making it an essential tool for genomic data processing pipelines.
The library excels in computing coverage tracks, summarizing sequence alignments or read counts across the genome, allowing users to generate coverage profiles over specified the genome. In addition, it offers advanced statistical features, such as the calculation of cross-correlations, which can be used to assess relationships between different genomic datasets, for example, in ChIP-seq or RNA-seq analysis.
One of the library's core strengths is its efficient handling of genomic ranges. It offers a highly optimized data structure for manipulating large genomic intervals, ensuring that operations like querying, merging, or intersecting genomic regions are performed with minimal overhead. Moreover, the library provides sequence containers for FASTA data, motif/PWM utilities, k-mer counting, segmentation import/export, and pretty printing for displaying genomic ranges in human-readable formats.
Designed with performance and usability in mind, this library is ideal for large-scale genomics projects requiring both speed and precision, whether for research in genomics, epigenetics, or other related fields.
Documentation
Please find the API documentation here.
Tools
The package contains the following command line tools:
| Tool |
Description |
| bam-check-fastq |
check whether all BAM read names are present in one or more FASTQ files |
| bam-check-bin |
check bin records of a bam file |
| bam-genome |
print the genome (sequence table) of a bam file |
| bam-to-fastq |
reconstruct FASTQ records from a BAM file |
| bam-to-bigwig |
convert bam to bigWig (estimate fragment length if required) |
| bam-view |
print contents of a bam file |
| bed-remove-overlaps |
remove BED or table rows that overlap inadmissible regions |
| bigwig-counts-to-quantiles |
convert bigWig counts to empirical quantiles |
| bigwig-edit-chrom-names |
rewrite a bigWig with chromosome names transformed by a regex |
| bigwig-extract |
extract bigWig data for BED regions as a table or bigWig |
| bigwig-extract-chroms |
write a new bigWig containing only selected chromosomes |
| bigwig-genome |
print the genome (sequence table) of a bigWig file |
| bigwig-histogram |
compute a histogram or cumulative histogram over track values |
| bigwig-info |
print information about a bigWig file |
| bigwig-map |
apply a shared-library mapping function across one or more bigWig tracks |
| bigwig-nil |
re-encode a bigWig track through the Rust implementation |
| bigwig-positive |
call joint positive regions across one or more bigWig tracks |
| bigwig-quantile-normalize |
quantile-normalize one bigWig track against a reference |
| bigwig-query |
retrieve data from a bigWig file |
| bigwig-query-sequence |
retrieve sequences from a bigWig file |
| bigwig-statistics |
print summary statistics for a bigWig track |
| chromhmm-tables-to-bigwig |
convert ChromHMM per-chromosome tables to bigWig |
| count-kmers |
count or identify k-mers in FASTA sequences or BED regions |
| draw-genomic-regions |
draw random genomic regions from a genome |
| fasta-extract |
extract FASTA subsequences for BED regions |
| fasta-unresolved-regions |
report unresolved (N) FASTA intervals as BED |
| gtf-to-bed |
convert GTF records to BED6 |
| meme-extract |
extract PWM or PPM motif matrices from MEME or DREME XML |
| observed-over-expected-cpg |
compute observed/expected CpG scores for regions or whole sequences |
| pwm-scan-regions |
score genomic regions with one or more PWMs |
| pwm-scan-sequences |
scan FASTA sequences with a PWM and export a bigWig track |
| segmentation-differential |
merge and score differential chromatin states across segmentations |
| sequence-similarity |
compute sliding-window k-mer similarity to a reference sequence |
Examples
Import genes from UCSC
use crate::genes::Genes;
// Import from local file
if let Ok(genes) = Genes::import_genes("data/hg19.knownGene.txt.gz") {
println!("{}", genes);
}
// Retrieve from USCS server
if let Ok(genes) = Genes::import_genes_from_ucsc("hg19", "knownGene") {
println!("{}", genes);
}
The result is:
seqnames ranges strand | names cds
1 chr1 [ 11868, 14409) + | ENST00000456328.2_1 [11868, 11868)
2 chr1 [ 29553, 31097) + | ENST00000473358.1_5 [29553, 29553)
3 chr1 [ 30266, 31109) + | ENST00000469289.1_1 [30266, 30266)
4 chr1 [ 34553, 36081) - | ENST00000417324.1_4 [34553, 34553)
5 chr1 [ 35244, 36073) - | ENST00000461467.1_3 [35244, 35244)
... ... | ... ...
254531 chrY [ 59161253, 59162245) - | ENST00000711258.1_1 [59161253, 59161253)
254532 chrY [ 59208304, 59208554) + | ENST00000711259.1_1 [59208304, 59208304)
254533 chrY [ 59311662, 59311996) - | ENST00000711266.1_1 [59311662, 59311662)
254534 chrY [ 59318040, 59318920) - | ENST00000711267.1_1 [59318040, 59318040)
254535 chrY [ 59358334, 59360548) - | ENST00000711270.1_1 [59358334, 59358334)
Read GTF files
use crate::granges::GRanges;
let granges = GRanges::import_gtf("src/granges_gtf.gtf",
vec!["gene_id", "gene_num"], // Names of optional fields
vec!["str" , "int" ], // Types of optional fields
vec![None , Some("0") ], // Default values, can be an empty vector if omitted
).unwrap();
The result is:
seqnames ranges strand | source feature gene_num gene_id
1 1 [11869, 14409) + | transcribed_unprocessed_pseudogene gene 1 ENSG00000223972
2 1 [11870, 14410) + | processed_transcript transcript 0 ENSG00000223972
Read a BAM file into a GRanges object
use crate::bam::BamReaderOptions;
use crate::granges::GRanges;
let mut options = BamReaderOptions::default();
options.read_cigar = true;
options.read_qual = true;
if let Ok(granges) = GRanges::import_bam_single_end("tests/test_bam_2.bam", Some(options)) {
println!("{}", granges);
}
The result is:
seqnames ranges strand | flag mapq cigar qual
1 chr15 [ 25969791, 25969842) + | 99 60 51M @@@DFFFFHHHHGBHIBHHHGGGIHIEEHEIIIIIIGCHGHIGIGIIIIHH
2 chr15 [ 25969837, 25969888) - | 147 60 51M GJJIIIIHIHDIHIIHHEGEEGJIIHFHIHCIHHGEIDHHDDHFDFFD@C@
3 chr1 [175925088, 175925139) - | 153 0 51M IIIIIJJJIJJJJJJJJIJGIJIJHJJJJJJJIIJJJJHHHHHFFFFFBCC
4 chrX [ 71582197, 71582248) - | 83 60 51M GGDDIIIGJIJJJJJJJJJHGEHGJJJJIHDEIIGIJJGHHFHFFFFFCC@
5 chrX [ 71581965, 71582016) + | 163 60 51M @CCFFDFFHHDHHJJJIGCHGIGIGIGJJJIGCGCHBFGDBFGFGIJIJGC
... ... | ... ... ... ...
4887 chr11 [ 9074777, 9074828) + | 163 29 51M <@:B;DDDFH:CC>CFEAADFFFCDFHIEHIHJEGGEHIJJIIDGGIGHII
4888 chr7 [ 3303179, 3303230) - | 83 60 51M HIHH@GIIHGHGHCJHGJIIIIIJJJJIJJIIIIIIJJHHGHHFFFFFCCC
4889 chr7 [ 3303050, 3303101) + | 163 60 51M <@<DADADAAFFFC@>DGEHIICEGH@HCCEGHCCEBGGGFG:BFCGGGBB
4890 chr11 [ 4737838, 4737889) - | 83 60 51M DB9;HCD?D??:?:):)CCA<C2:@HFAHEEHF@<?<?:ACADB;:BB1@?
4891 chr11 [ 4737786, 4737837) + | 163 60 51M @@<DDBDDFD+C?A:1CFDHBFHC<?F9+CGGI:49CCGFACE99?DC990
Read and write FASTQ records
use std::io::{BufReader, BufWriter};
use std::fs::File;
use rustynetics::fastq::{FastqReader, FastqRecord, FastqWriter};
let input = File::open("reads.fastq").unwrap();
let mut reader = FastqReader::new(BufReader::new(input));
let output = File::create("copy.fastq").unwrap();
let mut writer = FastqWriter::new(BufWriter::new(output));
while let Some(record) = reader.read_record().unwrap() {
writer.write_record(&record).unwrap();
}
let record = FastqRecord::new(
"read1/1".to_string(),
"ACGT".to_string(),
"IIII".to_string(),
).unwrap();
writer.write_record(&record).unwrap();
writer.flush().unwrap();
Reconstruct FASTQ from BAM
bam-to-fastq reconstructs FASTQ records from BAM alignments. Reverse-strand reads are emitted in original FASTQ orientation by reverse-complementing the sequence and reversing the qualities.
# Write a single FASTQ stream
bam-to-fastq reads.bam > reads.fastq
# Split paired-end output into separate files and keep singles
bam-to-fastq reads.bam \
--output1 reads_R1.fastq \
--output2 reads_R2.fastq \
--output-single reads_single.fastq \
--pair-suffixes
If a BAM file does not contain quality scores, use --fill-missing-quality to emit synthetic qualities:
bam-to-fastq reads.bam --fill-missing-quality I > reads.fastq
Check BAM Read Names Against FASTQ
bam-check-fastq verifies that every BAM read name is present in one or more FASTQ inputs. This is useful when a BAM file was built from several FASTQ files. The report now answers the main question first: whether all BAM reads were found, how many BAM records and unique read names are missing, and whether truncated FASTQ files may have affected the comparison. Additional sections still include BAM and FASTQ overlap, duplicate-name statistics, BAM record-class summaries, and a per-FASTQ-file breakdown. Truncated FASTQ files are reported as warnings and processing continues with the remaining files.