omics.co.in logo omics.co.in

5: Biological File I/O & Bioconductor Formats

Tested Stacks: GATK 4.6.1 Samtools 1.21 Nextflow 24.10 Seurat v5.1 Python 3.12
Prerequisites: Basic Linux CLI & Sequencing Formats
📚 Chapter 5 of 13 0% Course Completed
📊 Downloadable Lesson R Script 4.6 KB

r05_genomic_file_io.R

Fast tabular reading, custom base R FASTA parser, BED interval coordinate calculations, and serialized RDS object storage (saveRDS/readRDS).

$ Rscript r05_genomic_file_io.R

Chapter 5: Biological File I/O & Bioconductor Formats

Biological discovery relies on standardized file formats: FASTA (sequences), FASTQ (reads + quality), BED (intervals), GTF/GFF3 (gene models), and VCF (genomic variants). Reading and writing these efficiently prevents memory bottlenecks.

1. Parsing Multi-Record FASTA Files

Reading FASTA sequences in base R with header-sequence mapping:

read_fasta <- function(filepath) {
    lines <- readLines(filepath)
    headers <- c()
    sequences <- c()
    cur_seq <- ""
    for (line in lines) {
        line <- trimws(line)
        if (startsWith(line, ">")) {
            if (cur_seq != "") { sequences <- c(sequences, cur_seq); cur_seq <- "" }
            headers <- c(headers, substring(line, 2))
        } else {
            cur_seq <- paste0(cur_seq, line)
        }
    }
    if (cur_seq != "") sequences <- c(sequences, cur_seq)
    data.frame(Header = headers, Sequence = sequences, Length = nchar(sequences), stringsAsFactors = FALSE)
}

2. Parsing BED Interval Coordinates

Standard BED files use 0-based start and 1-based end coordinates:

bed_df <- read.table("exons.bed", sep = "\t", header = FALSE,
                     col.names = c("chrom", "chromStart", "chromEnd", "name", "score", "strand"))

# Calculate feature widths in bp
bed_df$width <- bed_df$chromEnd - bed_df$chromStart
print(head(bed_df))

3. Saving & Restoring Serialized R Objects (saveRDS)

Saving preprocessed genomic objects in compressed binary format preserves factors, S4 classes, and metadata exactly:

# Save compressed RDS
saveRDS(bundle, "genomic_bundle.rds", compress = "xz")

# Restore instantly in any future session
bundle <- readRDS("genomic_bundle.rds")
⚠️

Common Pitfalls & Troubleshooting Checklist

  • Java Heap Exhaustion (GATK / Picard): If processes terminate with java.lang.OutOfMemoryError, set your environment variable explicitly: export _JAVA_OPTIONS="-Xmx16g -Djava.io.tmpdir=/tmp".
  • Index Timestamp Mismatch: If tools complain that a BAM index is missing or out of date, regenerate with samtools index -@ 4 sample.bam.
  • Conda Channel Conflicts: Avoid ABI incompatibilities between bioconda and conda-forge by setting strict priority: conda config --set channel_priority strict.
  • Chromosome Naming Contamination: Always verify whether reference FASTA headers use chr1 or 1; divergent naming causes silent alignment drops.
Was this guide helpful to your research?
Help us maintain 100% accurate, peer-reviewed bioinformatics standards.
📝 Propose Edit on GitHub ↗