Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • meher
    Member
    • Jun 2011
    • 54

    #1

    Extract reads from multiple regions from bam file

    HI,

    samtools view aln.sorted.bam chr2:20,100,000-20,200,000 is used to extract reads from specific regions.

    Is there a way to extract if we have multiple regions specified in a bed file format?


    Regards
    Mehar
  • krobison
    Senior Member
    • Nov 2007
    • 734

    #2
    You can continue specifying regions after the first one

    samtools view aln.sorted.bam chr2:20,100,000-20,200,000 chr2:30,100,000-30,200,000

    (BTW, will it really tolerate the commas?)

    It's pretty easy to convert BED format to this using your favorite text mangling language; mine is Perl:

    Code:
    samtools view aln.sorted.bam `perl -p -e 'chomp; @a=split(/\t/); print "$a[0]:$a[1]-$a[2]"' sleepy.bed`

    Comment

    • meher
      Member
      • Jun 2011
      • 54

      #3
      Originally posted by krobison View Post
      You can continue specifying regions after the first one

      samtools view aln.sorted.bam chr2:20,100,000-20,200,000 chr2:30,100,000-30,200,000

      (BTW, will it really tolerate the commas?)

      It's pretty easy to convert BED format to this using your favorite text mangling language; mine is Perl:

      Code:
      samtools view aln.sorted.bam `perl -p -e 'chomp; @a=split(/\t/); print "$a[0]:$a[1]-$a[2]"' sleepy.bed`
      Thanks for the answer.

      I have tried it but, the samtools is throwing an error "Argument list too long".
      Any other way of doing this?

      Comment

      • dariober
        Senior Member
        • May 2010
        • 311

        #4
        Originally posted by krobison View Post
        It's pretty easy to convert BED format to this using your favorite text mangling language; mine is Perl:

        Code:
        samtools view aln.sorted.bam `perl -p -e 'chomp; @a=split(/\t/); print "$a[0]:$a[1]-$a[2]"' sleepy.bed`
        Hi- You don't need to convert BED format to string. samtools view accepts a bed file directly (see -L option "output alignments overlapping the input BED FILE")

        Dario

        Comment

        • meher
          Member
          • Jun 2011
          • 54

          #5
          Extract reads from multiple regions from bam file

          Originally posted by dariober View Post
          Hi- You don't need to convert BED format to string. samtools view accepts a bed file directly (see -L option "output alignments overlapping the input BED FILE")

          Dario
          Hi,

          I tried to do this using the following command

          samtools view -L sample.bed test.bam > test1.bam

          and surprisingly the output files is thrice in size than the original and i tried to count the number of reads on the output bam using samtools view -c test1.bam but, it throws the error

          [bam_header_read] EOF marker is absent. The input is probably truncated.
          [bam_header_read] invalid BAM binary header (this is not a BAM file).
          [main_samview] fail to read the header from "test1.bam"


          Any help??

          Comment

          • dariober
            Senior Member
            • May 2010
            • 311

            #6
            Originally posted by meher View Post
            Hi,

            I tried to do this using the following command

            samtools view -L sample.bed test.bam > test1.bam

            and surprisingly the output files is thrice in size than the original and i tried to count the number of reads on the output bam using samtools view -c test1.bam but, it throws the error

            [bam_header_read] EOF marker is absent. The input is probably truncated.
            [bam_header_read] invalid BAM binary header (this is not a BAM file).
            [main_samview] fail to read the header from "test1.bam"


            Any help??
            Hello,
            By default, samtools view expect bam as input and produces sam as output. This should explain why you get a very large output (uncompressed sam) and a complain about BAM binary header.
            To fix it use the -b option. This should work:
            Code:
            samtools view -b -L sample.bed test.bam > test1.bam
            samtools view -c test1.bam
            Hope this helps
            Dario

            Comment

            • wanfahmi
              Member
              • Apr 2008
              • 34

              #7
              Hi,

              How could we extract reads for specific locus / gene from accepted_hits.bam file? Is that possible? Could we get the fastq / fasta from it? Thank you

              Comment

              • ersgupta
                Member
                • Jun 2011
                • 26

                #8
                try out "intersectBed" in bedtools...


                Comment

                Latest Articles

                Collapse

                • SEQadmin2
                  Beyond CRISPR/Cas9: Understand, Choose, and Use the Right Genome Editing Tool
                  by SEQadmin2






                  CRISPR/Cas9 sparked the gene editing revolution for both research and therapeutics.1 But this system still showed severe issues that limited its applications. The most prominent were the heavy reliance on PAM sequences, delivery limitations, double-stranded breaks that prompt unintended edits and cell death, and editing inefficiency (both in targeting and in knock-in reliability).

                  Despite this, “CRISPR helped turn genome editing from a specialized
                  ...
                  07-31-2026, 11:01 AM
                • 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
                  ...
                  07-20-2026, 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

                ad_right_rmr

                Collapse

                News

                Collapse

                Topics Statistics Last Post
                Started by SEQadmin2, 07-31-2026, 02:55 AM
                0 responses
                20 views
                0 reactions
                Last Post SEQadmin2  
                Started by SEQadmin2, 07-24-2026, 12:17 PM
                0 responses
                16 views
                0 reactions
                Last Post SEQadmin2  
                Started by SEQadmin2, 07-23-2026, 11:41 AM
                0 responses
                16 views
                0 reactions
                Last Post SEQadmin2  
                Started by SEQadmin2, 07-20-2026, 11:10 AM
                0 responses
                26 views
                0 reactions
                Last Post SEQadmin2  
                Working...