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.
General tools
Section titled “General tools”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.
# View two reads (four lines each) from a compressed FASTQzcat reads.fastq.gz | head -8
# Count reads: divide the line count by 4zcat reads.fastq.gz | wc -lzcat reads.fastq.gz | awk 'END{print NR/4}'
# Check paired-end files agreezcat R1.fastq.gz | wc -lzcat R2.fastq.gz | wc -lThe 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.
# Header and first alignmentssamtools view -h file.bam | head -20
# Alignment summary: totals, mapped, paired, duplicatessamtools flagstat file.bam
# Reads per chromosome (needs an index)samtools idxstats file.bam
# Create the index if it is missingsamtools index file.bam
# Depth at specific positionssamtools depth -r chr1:1000000-1001000 file.bamflagstat 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.
# The header: filters, INFO fields, sample namesbcftools view -h file.vcf.gz
# Summary: variant counts, Ts/Tv ratio, quality distributionsbcftools stats file.vcf.gz | head -30
# Extract chromosome, position and alleles per variantbcftools query -f '%CHROM\t%POS\t%REF\t%ALT\n' file.vcf.gz | head
# Count variants, excluding header linesbcftools view -H file.vcf.gz | wc -lBED, GFF and GTF
Section titled “BED, GFF and GTF”These are tab-separated text, so standard tools work.
head file.bed
# Regions per chromosomeawk '{print $1}' file.bed | sort | uniq -c | sort -rn
# Gene count in a GTF: the tabs match the feature column, not a gene namegrep -c " gene " file.gtf
# Every feature type in a GFF/GTFawk -F'\t' '$1 !~ /^#/ {print $3}' file.gtf | sort | uniq -c | sort -rnThe 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.
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'10x Matrix Market files
Section titled “10x Matrix Market files”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.
zcat matrix.mtx.gz | head -3zcat barcodes.tsv.gz | wc -lzcat features.tsv.gz | wc -lThe validation checklist
Section titled “The validation checklist”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. |
Summary
Section titled “Summary”head,zcatandwc -lcover text files.samtoolscovers BAM:flagstatfor quality,idxstatsfor per-chromosome counts,view -hfor the header.bcftoolscovers VCF:statsfor summaries,queryfor field extraction.awkandgrepcover 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.
Scientific references
Section titled “Scientific references”Danecek and colleagues, 2021 describe the development and the current features of SAMtools and BCFtools.
Further reading
Section titled “Further reading”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.