#!/usr/bin/env Rscript
# Script: fasta_stats.R
# Purpose: Calculate basic statistics for a FASTA file
args <- commandArgs(trailingOnly = TRUE) # like $1 $2 ... in bash
if (length(args) != 1) {
cat("Usage: Rscript fasta_stats.R sequences.fasta\n")
quit(status = 1)
}
input <- args[1]
if (!file.exists(input)) {
cat("Error: File", input, "not found!\n")
quit(status = 1)
}
lines <- readLines(input)
headers <- grepl("^>", lines) # TRUE for header lines
seq <- paste(lines[!headers], collapse = "")
cat("=== FASTA Statistics for", input, "===\n")
cat("Number of sequences:", sum(headers), "\n")
cat("Total length:", nchar(seq), "bp\n")
cat("Average length:", nchar(seq) / sum(headers), "bp\n")
n_count <- sum(strsplit(seq, "")[[1]] %in% c("N", "n"))
cat("N bases:", n_count, "\n")
gc <- sum(strsplit(toupper(seq), "")[[1]] %in% c("G", "C"))
cat("GC content:", round(100 * gc / nchar(seq), 1), "%\n")
orfs <- regmatches(seq, gregexpr("ATG([ACGT]{3}){20,}(TAA|TAG|TGA)", seq))[[1]]
cat("ORF-like stretches:", length(orfs), "\n")
Comments, names and style
#and run to the end of the line - in bash, R and PythonWeight,weightandWEIGHTare three different names_(and.in R); must not start with a digitsnake_case, short but descriptive -mean_gc,n_fish,total_reads