Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kmac
    Junior Member
    • Oct 2013
    • 3

    #1

    Downsampling a bam file for a specific number of reads

    Is there an easy way to downsample a bam file for a specific number of reads? I know that picard and samtools can downsample to a proportion of reads (like 10% of your total reads) but I have three replicates for which I would like to extract an equal number of random reads.
    Thanks in advance for any help!
  • Brian Bushnell
    Super Moderator
    • Jan 2014
    • 2709

    #2
    I have a tool, ReformatReads, that will extract an exact number of randomly distributed reads from a file. It's designed for fasta, fastq, and scarf, but it will process sam files (and bam files if samtools is installed) if the cigar strings are in 1.4 format (with = and X instead of M) and the reads are not paired. If the reads paired or in an old format, you would need to sample the fastq files first and remap them. It can convert sam/bam to fastq regardless of the cigar string version, but sam/bam have no guarantees about read order, so pairing information will be lost (so don't do that!)

    reformat.sh in=reads.fq out=sampled.fq samplereads=1000

    ...would sample 1000 reads from a fastq file, or 1000 pairs if the file was interleaved.

    reformat.sh in1=r1.fq in2=r2.fq out1=s1.fq out2=s2.fq samplereads=1000

    ...would sample 1000 pairs from paired read files.

    reformat.sh in=mapped.bam out=sampled.bam samplereads=1000

    ...should sample 1000 reads from a bam file if all of the conditions are met (samtools installed, single-ended, sam 1.4 format reads).

    You alternatively sample a specific number of bases worth of reads, if you have variable-length reads, with the "samplebases" flag.
    Last edited by Brian Bushnell; 03-27-2014, 05:08 PM.

    Comment

    • mgogol
      Senior Member
      • Mar 2008
      • 197

      #3
      Here's a few ideas:


      Also look into bamtools random

      Comment

      • shenkers
        Junior Member
        • Mar 2014
        • 1

        #4
        StreamSampler

        I uploaded a command line utility I created to uniformly sample lines from a stream of input. Using samtools in a bash comand shell, you can use this command to uniformly downsample a bam file to a specific number of reads:

        (
        samtools view -H [bamfile];
        samtools view -F 0x004 [bamfile] |
        java -jar StreamSampler.jar [# of reads to sample] [total # reads]
        ) |
        samtools -bS - > [sampled bam file]

        It's important to keep in mind that this just does the downsampling, which as Brian mentions above, would result in a bam file with inconsistent flags if the data is paired. If this is important for your application, you would need another step to fix the flags on reads whose mates were discarded... Alternatively, you could avoid fixing flags by sampling the read names, and then filter out all reads that match one of the sampled names. If you want to try this out, you can download the jar from

        command line utility for uniformly sampilng from a stream with known number of elements - Releases · shenkers/sampling

        Comment

        • Chipper
          Senior Member
          • Mar 2008
          • 323

          #5
          samtools sort -n bam namesorted
          samtools view namesorted | head -n xxxxxx
          or just calculate the percentage and use picard?

          Comment

          Latest Articles

          Collapse

          • SEQadmin2
            The Factors Reshaping Genome Assembly
            by SEQadmin2



            When the Human Genome Project declared the human genome finished in 2003, portions of the sequence were still missing, including many of its most repetitive and structurally complex regions.1 Still, for the next two decades, researchers analyzed their sequencing data against the single linear reference that came out of this effort. Released in 2013 as GRCh38, that reference has powered countless discoveries, but it has gaps and cannot capture the genetic diversity of the world’s...
            11-24-2026, 07:04 AM
          • SEQadmin2
            16S/ITS Profiling vs. Shotgun Metagenomics: What Changes in Your Workflow?
            by SEQadmin2





            NGS has revolutionized clinical microbiology by offering new ways to identify and surveil pathogens without prior knowledge of the target. One popular approach is 16S rRNA and ITS amplicon sequencing, which targets specific microbial markers. But studies are increasingly incorporating shotgun metagenomics, a technique that sequences all DNA from a sample without targeting specific microbial markers. This approach allows more detailed taxonomic classification,...
            10-30-2026, 06:46 AM

          ad_right_rmr

          Collapse

          News

          Collapse

          Topics Statistics Last Post
          Started by SEQadmin2, 11-23-2026, 11:11 AM
          0 responses
          39 views
          0 reactions
          Last Post SEQadmin2  
          Started by SEQadmin2, 11-17-2026, 06:27 AM
          0 responses
          47 views
          0 reactions
          Last Post SEQadmin2  
          Started by SEQadmin2, 11-12-2026, 07:59 AM
          0 responses
          68 views
          0 reactions
          Last Post SEQadmin2  
          Started by SEQadmin2, 11-05-2026, 09:25 AM
          0 responses
          68 views
          0 reactions
          Last Post SEQadmin2  
          Working...