Hot answers tagged

23 votes
Accepted

Convert a BAM file from one reference to another?

You're the second person I have ever seen using NCBI "chromosome names" (they're more like supercontig IDs). Normally I would point you to a resource providing mappings between chromosome names, but ...
user avatar
  • 19.2k
19 votes
Accepted

Converting a BAM file into VCF

It's not really possible to convert bam to vcf. bam is a mapping file, it does not contain ...
user avatar
  • 5,287
17 votes
Accepted

Why would someone use a CRAM instead of a BAM?

Whenever you want to save space (this can be a substantial savings). Until quite recently (samtools/htslib 1.7), only CRAM supported long CIGAR strings. If you need to guarantee that any random ...
user avatar
  • 19.2k
16 votes
Accepted

How can I downsample a BAM file while keeping both reads in pairs?

samtools has a subsampling option: -s FLOAT: Integer part is used to seed the random number generator [0]. Part after the decimal point sets the fraction of templates/pairs to subsample [no ...
user avatar
16 votes
Accepted

Why does this human bam file only have one copy of each chromosome?

The maternal and paternal copies of a chromosome are called haplotypes. Many metazoans (animals) are diploid and have maternal and paternal chromosome contribution during sexual reproduction, not just ...
user avatar
  • 3,071
15 votes

What is the difference between samtools, bamtools, picard, sambamba and biobambam?

The obvious answer is that different people wrote them. It's fairly common in bioinformatics for people with a computer science background to get frustrated with existing tools and create their own ...
user avatar
  • 11.7k
15 votes
Accepted

Are soft-clipped bases used for variant calling in samtools + bcftools?

No, samtools (and therefore bcftools) does not use soft-clipped bases. You can quickly confirm this by using either samtools depth or ...
user avatar
  • 19.2k
14 votes
Accepted

Merge hundreds of small BAM files into a single BAM file

samtools merge merged.bam *.bam is efficient enough since the input files are sorted. You can get a bit faster with sambamba and/or biobambam, but they're not ...
user avatar
  • 19.2k
13 votes
Accepted

Obtaining uniquely mapped reads from BWA mem alignment

To exclude all possible multi-mapped reads from a BWA-mapped BAM file, it looks like you need to use grep on the uncompressed SAM fields: ...
user avatar
  • 11.7k
12 votes

Convert bam file to highly compressible bam

You can just set the fields you don't need to *: ...
user avatar
  • 19.2k
12 votes
Accepted

Why do SAM and BAM use different coordinate systems?

Arithmetic on a zero-based coordinate system is less complicated than that on a one-based system, so it appears zero-based is often (not exclusively) used for binary data formats, like BAM or bigBed, ...
user avatar
11 votes
Accepted

BAM to BigWig without intermediary BedGraph

This can be done in R very easily from an indexed .bam file. Given single-end file for sample1. ...
user avatar
  • 1,498
11 votes

Random access on a FASTQ file

Arbitrary record access in constant time To get a random record in constant time, it is sufficient to get an arbitrary record in constant time. I have two solutions here: One with ...
user avatar
  • 2,051
11 votes

I have 23andme text files and would like to convert to SAM/BAM format

Having done 23andme myself I can tell you that your variant file, which contains SNP genotypes, cannot be converted to a bam file. It does not contain the same information as a bam file. It may be ...
user avatar
9 votes
Accepted

Quantifying reads mapping to multiple loci

You almost had the correct python code already, you just need to filter out secondary alignments: ...
user avatar
  • 19.2k
9 votes
Accepted

How to count reads in bam per bed interval with bedtools

The order of -a and -b switched at some point. You want: ...
user avatar
  • 19.2k
9 votes

How to subset a BAM by a list of QNAMEs?

use picard FilterSamReads http://broadinstitute.github.io/picard/command-line-overview.html#FilterSamReads READ_LIST_FILE (File) Read List File containing reads that will be included or excluded ...
user avatar
  • 1,473
9 votes
Accepted

How to validate that BAMs have been downloaded correctly?

samtools quickcheck is all you need. From the manual: Quickly check that input files appear to be intact. Checks that beginning of the file contains a valid header ...
user avatar
  • 19.2k
9 votes
Accepted

Extracting all reads from bam file which match read IDs in another file

It is still slow but grep has a -f option to take in a file samtools view inbam.bam | grep -f read_names.txt > read_locs.txt
user avatar
  • 2,556
9 votes
Accepted

How to count the number of mapped read in 100-bp window from a BAM/SAM file

one-liner Here's a gritty one-liner to count the number of reads in a region if you have just one region that you want to investigate. Change the 1 in ...
user avatar
  • 3,071
9 votes
Accepted

Index a BAM file using pysam

Oh you silly sausage, pysam.index takes a bam file name, not a python object. ...
user avatar
  • 5,287
8 votes

Merge hundreds of small BAM files into a single BAM file

Merging sorted files is a linear operation, so any well-implemented tools that do it will do it with approximately the same efficiency. So samtools merge (use the ...
user avatar
8 votes
Accepted

True bam file encoding and viewing as text

Why not work only with sam files and we need to convert them to bam? SAM is for human only, a binary format like BAM is smaller and parsed much faster by a program (and we want speed and optimize ...
user avatar
  • 1,473
8 votes

ethnicity check either from bam or vcf files

The main difficulty here is the use of GRCh38. Unfortunately, despite the fact that it's more than four years old, the major ethnicity-labeled public datasets (1000 Genomes, gnomAD when allele ...
user avatar
8 votes
Accepted

How can I count the number of reads that support a variant in a bam file?

If you don't mind a bit of manual counting, then samtools mpileup -f reference.fa -r chr22:425236-425236 alignments.bam will produce output where you can count the ...
user avatar
  • 19.2k
8 votes
Accepted

Total reads aligning to each reference within a bam file

The quick way to get the number of alignments on each reference is samtools idxstats my_bam.bam Number of reads on each reference is column 3. Although, as has ...
user avatar
  • 3,211
7 votes

Why do BAM files created by different tools have different file sizes?

It's worth bearing in mind that when outputting compressed BAM, as most tools do by default, they may well be using different levels of compression and/or different libraries, or versions of said, ...
user avatar
  • 1,029
7 votes
Accepted

Random access on a FASTQ file

As wkretzsch suggested this was worthy of an actual answer, I feel the obvious solution is missing here; index the FASTQ. Index it As much as I typically hesitate ...
user avatar
7 votes

How to manipulate a reference FASTA or bam to include variants from a VCF?

GATK has a solution that might work for you: FastaAlternateReferenceMaker, which : "Given a variant callset, this tool replaces the reference bases at variation sites with the bases supplied in the ...
user avatar
  • 841
7 votes
Accepted

Read counts from BAM file

Your data is not aligned to hg19, but to a bunch of RNA ref sequences. If you would align to hg19 you'll get each chromosome instead of the NR_* or NM_* accession codes with your ...
user avatar
  • 3,541

Only top scored, non community-wiki answers of a minimum length are eligible