Unconfigured Ad

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

    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
                  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...
                  Yesterday, 05:17 AM
                • GATTACAT
                  Reply to Nine Things a Sample Prep Scientist Thinks About Before Sequencing
                  by GATTACAT
                  Love this - good data definitely starts from good input, and poor input can only give relatively poor data. I particularly like the mention of Nanodrop/absorbance based methods for quantification. It's such a toss up if you'll get an accurate reading or what amounts to a randomly generated number, and a lot of library/sequencing related issues can be traced back to poor quant.
                  07-01-2026, 11:43 AM
                • SEQadmin2
                  Nine Things a Sample Prep Scientist Thinks About Before Sequencing
                  by SEQadmin2


                  I’m not a sequencing expert. I’m a purification scientist who uses NGS to evaluate workflows my group develops. With this perspective, we think about the sample first and the NGS workflow second. The sequencer is an exceptionally honest reporter, but it can only report on what you give it, so whether you get clean, interpretable data from an NGS workflow is largely determined before you begin.

                  Here are nine questions we think about, in roughly the order they matter, before...
                  06-18-2026, 07:11 AM

                ad_right_rmr

                Collapse

                News

                Collapse

                Topics Statistics Last Post
                Started by SEQadmin2, Yesterday, 10:08 AM
                0 responses
                6 views
                0 reactions
                Last Post SEQadmin2  
                Started by SEQadmin2, 07-07-2026, 11:05 AM
                0 responses
                8 views
                0 reactions
                Last Post SEQadmin2  
                Started by SEQadmin2, 07-02-2026, 11:08 AM
                0 responses
                31 views
                0 reactions
                Last Post SEQadmin2  
                Started by SEQadmin2, 06-30-2026, 05:37 AM
                0 responses
                29 views
                0 reactions
                Last Post SEQadmin2  
                Working...