Unconfigured Ad

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

    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 - 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
            Proteomic Platforms: How to Choose the Right Analytical Strategy to Improve Detection and Clinical Applications
            by SEQadmin2


            Proteomics platforms are evolving rapidly, with advances in mass spectrometry and affinity-based approaches expanding what researchers can detect and at what scale. As the field moves toward deeper proteome coverage and clinical applications, scientists face an increasingly complex landscape of tools. This article will explore how researchers are navigating these choices to find the right platform for their work.

            The systematic characterization of the human proteome has
            ...
            Today, 11:48 AM
          • SEQadmin2
            Advanced Sequencing Platforms Tackle Neuroscience’s Toughest Genomics Problems
            by SEQadmin2



            Genomics studies in neuroscience face a special challenge due to the brain’s complexity and scarcity of samples. Mapping changes in cell type and state using conventional next-generation sequencing methods remains challenging. Advances in technologies like single-cell sequencing, spatial transcriptomics, and long-read sequencing have opened the door to deeper studies of the brain and diseases like Alzheimer’s, amyotrophic lateral sclerosis (ALS), and schizophrenia.
            ...
            07-09-2026, 11:10 AM
          • SEQadmin2
            Cancer Drug Resistance: The Lingering Barrier to Rising Survival
            by SEQadmin2



            Cancer survival rates have significantly increased in the last few decades in the United States, reaching a combined 70% 5-year survival rate by 2021. Behind this number, there are years of research to find new therapies, drug targets, and early detection methods. But there is one core challenge that keeps slowing down these advances, and it’s about drug resistance.

            There is no single reason why many patients don’t respond to treatment as expected. Cancer is...
            07-08-2026, 05:17 AM

          ad_right_rmr

          Collapse

          News

          Collapse

          Topics Statistics Last Post
          Started by SEQadmin2, Today, 11:10 AM
          0 responses
          8 views
          0 reactions
          Last Post SEQadmin2  
          Started by SEQadmin2, 07-13-2026, 10:26 AM
          0 responses
          30 views
          0 reactions
          Last Post SEQadmin2  
          Started by SEQadmin2, 07-09-2026, 10:04 AM
          0 responses
          39 views
          0 reactions
          Last Post SEQadmin2  
          Started by SEQadmin2, 07-08-2026, 10:08 AM
          0 responses
          25 views
          0 reactions
          Last Post SEQadmin2  
          Working...