Skip to content

Inspecting Files

You do not always need R or Python to look at a bioinformatics file. A few command line tools check contents, verify integrity and count records. This page collects the useful commands per format, then ends with the checklist to run before any analysis.

The commands use example filenames. Replace them with your input paths, and run each format’s commands in an environment that provides its tools. Record the file source, sample identifier, reference release, and supplied checksum before you start. A readable file can still belong to the wrong sample or reference.

These work on almost any file.

Command What it does
head -n 20 file.txt Show the first 20 lines
tail -n 20 file.txt Show the last 20 lines
wc -l file.txt Count lines
zcat file.txt.gz | head Peek at a gzipped file
file mydata.bam Check the actual file type
ls -lh file.bam Check the file size

The file command earns its place when a file has the wrong extension or arrives with no documentation.

Terminal window
# View two reads (four lines each) from a compressed FASTQ
zcat reads.fastq.gz | head -8
# Count reads: divide the line count by 4
zcat reads.fastq.gz | wc -l
zcat reads.fastq.gz | awk 'END{print NR/4}'
# Check paired-end files agree
zcat R1.fastq.gz | wc -l
zcat R2.fastq.gz | wc -l

The division by four assumes one sequence line and one quality line per read. Equal line counts do not prove that mate identifiers or read order match. If paired files differ, check whether a transfer failed or a filter removed mates independently. Confirm read identifiers and pairing with the source pipeline before you use the files as paired input.

BAM is binary, so you need samtools.

Terminal window
# Header and first alignments
samtools view -h file.bam | head -20
# Alignment summary: totals, mapped, paired, duplicates
samtools flagstat file.bam
# Reads per chromosome (needs an index)
samtools idxstats file.bam
# Create the index if it is missing
samtools index file.bam
# Depth at specific positions
samtools depth -r chr1:1000000-1001000 file.bam

flagstat reads the whole file and summarizes alignment flags. It does not validate the biological sample or reference assembly. For a separate truncation check, samtools quickcheck checks the header and the end of a BAM or CRAM file. It does not read the middle of the file and cannot exclude internal corruption.

The index command writes an index beside the BAM. It requires coordinate-sorted input. See the index documentation for format limits and requirements.

VCF is text but usually large, compressed with bgzip and indexed with tabix.

Terminal window
# The header: filters, INFO fields, sample names
bcftools view -h file.vcf.gz
# Summary: variant counts, Ts/Tv ratio, quality distributions
bcftools stats file.vcf.gz | head -30
# Extract chromosome, position and alleles per variant
bcftools query -f '%CHROM\t%POS\t%REF\t%ALT\n' file.vcf.gz | head
# Count variants, excluding header lines
bcftools view -H file.vcf.gz | wc -l

These are tab-separated text, so standard tools work.

Terminal window
head file.bed
# Regions per chromosome
awk '{print $1}' file.bed | sort | uniq -c | sort -rn
# Gene count in a GTF: the tabs match the feature column, not a gene name
grep -c " gene " file.gtf
# Every feature type in a GFF/GTF
awk -F'\t' '$1 !~ /^#/ {print $3}' file.gtf | sort | uniq -c | sort -rn

The last command reports how many gene, transcript, exon and CDS entries the file carries.

H5AD is binary HDF5. A one-line Python command prints the summary.

Terminal window
python -c "import anndata; adata = anndata.read_h5ad('file.h5ad'); print(adata)"
AnnData object with n_obs × n_vars = 5000 × 20000
obs: 'cell_type', 'batch', 'n_counts'
var: 'gene_name', 'highly_variable'
obsm: 'X_pca', 'X_umap'

The filtered_feature_bc_matrix/ directory holds three files. The dimensions sit in the third line of the matrix header: rows (genes), columns (cells) and non-zero entries.

Terminal window
zcat matrix.mtx.gz | head -3
zcat barcodes.tsv.gz | wc -l
zcat features.tsv.gz | wc -l

Run these checks before analysis. Keep the command, result, and input filename in your analysis record so another reader can repeat the check.

Check What the result establishes When to stop
Compare the supplied checksum and test gzip compression with gzip -t. A matching checksum confirms agreement with the supplied file. A gzip test checks the compressed stream. Stop if the checksum differs or decompression fails.
Inspect the BAM header’s sort order and the downstream tool’s requirements. SO:coordinate declares coordinate order. The header alone does not verify every record. Stop if the required order is absent or the tool reports unsorted records.
Compare sequence names and lengths across the BAM, VCF, and reference. This detects incompatible sequence dictionaries. Matching one chromosome does not establish the complete reference. Stop if names or lengths disagree. Resolve the reference choice before analysis.
Compare the recorded assembly, annotation release, and reference checksums. These identify the intended reference files. A label such as GRCh38 alone does not identify every contig or annotation. Stop if the reference provenance is missing or inconsistent.
Check mate identifiers and order, and confirm each index belongs to its data file. Equal FASTQ counts are only an initial check. Index files must describe the current data file. Stop if mates differ or an index comes from an earlier file version.
  • head, zcat and wc -l cover text files.
  • samtools covers BAM: flagstat for quality, idxstats for per-chromosome counts, view -h for the header.
  • bcftools covers VCF: stats for summaries, query for field extraction.
  • awk and grep cover BED, GFF and GTF.
  • A one-line Python command summarises an H5AD file.
  • Validate truncation, sorting, chromosome naming, genome build and paired-file consistency before you analyse anything.

Danecek and colleagues, 2021 describe the development and the current features of SAMtools and BCFtools.

The Samtools quickcheck manual states which header and end-of-file checks it performs and why it cannot detect corruption inside a file.

The Samtools index manual explains the coordinate-order requirement and the BAI and CSI limits.

The BCFtools command reference documents the view, stats, and query commands used to inspect VCF records.

The AnnData format specification maps the printed object summary to stored arrays and metadata.