Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HeyIamNuria
    Member
    • Dec 2012
    • 19

    #1

    How can I extract the reads that map in genomic region?

    Hello,
    I have a bam file with the alignment of Illumina reads to a genome, and a bed file with the coordinates of some genomic regions.

    I found out the coverage of these regions using Bedtools, and now I want to retrieve the reads that map in these regions, but I do not know how to get the names of the reads.

    Is there any available tool to extract the reads that map in a particular genomic region?

    I thought Bedtools or Samtools would have it but I cannot find it.
    My only other idea is to blast the reads against these regions, but I know that the results would be different.

    Thank you for your help
  • dpryan
    Devon Ryan
    • Jul 2011
    • 3478

    #2
    Assuming your BAM file is sorted and indexed:

    Code:
    samtools view -h -L Regions.bed alignments.bam > alignments_in_regions.sam

    Comment

    • dexterslab
      Junior Member
      • Jan 2013
      • 5

      #3
      Originally posted by dpryan View Post
      Assuming your BAM file is sorted and indexed:

      Code:
      samtools view -h -L Regions.bed alignments.bam > alignments_in_regions.sam
      Sorry for blatantly hijacking this thread with a follow up question:

      Assuming paired-end reads, would this suggested command also extract reads wherein only one of the mates falls into the region? If not, how could I archive that?

      Comment

      • dpryan
        Devon Ryan
        • Jul 2011
        • 3478

        #4
        Originally posted by dexterslab View Post
        Sorry for blatantly hijacking this thread with a follow up question:

        Assuming paired-end reads, would this suggested command also extract reads wherein only one of the mates falls into the region? If not, how could I archive that?
        Good question. Looking at the source code for samtools, it appears that it processes reads one at a time (i.e., not as pairs). So, if only one mate in a pair overlapped a region, I expect only that read would be output and not its mate. If you need both mates, it's possible that bedops implements that (I've never looked, but wouldn't be surprised).

        Comment

        • HeyIamNuria
          Member
          • Dec 2012
          • 19

          #5
          Devon,
          maybe I'm wrong but did you meant to type "-L"?
          Because:

          "view: invalid option -- 'L'

          Usage: samtools view [options] <in.bam>|<in.sam> [region1 [...]]

          Options: -b output BAM
          -h print header for the SAM output
          -H print header only (no alignments)
          -S input is SAM
          -u uncompressed BAM output (force -b)
          -x output FLAG in HEX (samtools-C specific)
          -X output FLAG in string (samtools-C specific)
          -t FILE list of reference names and lengths (force -S) [null]
          -T FILE reference sequence file (force -S) [null]
          -o FILE output file name [stdout]
          -f INT required flag, 0 for unset [0]
          -F INT filtering flag, 0 for unset [0]
          -q INT minimum mapping quality [0]
          -l STR only output reads in library STR [null]
          -r STR only output reads in read group STR [null]
          -? longer help"

          Comment

          • dpryan
            Devon Ryan
            • Jul 2011
            • 3478

            #6
            Yes, but it seems that you have an earlier version of samtools (0.1.17 maybe?). Try upgrading, as I know the -L option is available in 0.1.18 and 0.1.19 (the most recent).

            Comment

            • dpryan
              Devon Ryan
              • Jul 2011
              • 3478

              #7
              BTW, here are the options for the most recent version (0.1.19):

              Code:
              Usage:   samtools view [options] <in.bam>|<in.sam> [region1 [...]]
              
              Options: -b       output BAM
                       -h       print header for the SAM output
                       -H       print header only (no alignments)
                       -S       input is SAM
                       -u       uncompressed BAM output (force -b)
                       -1       fast compression (force -b)
                       -x       output FLAG in HEX (samtools-C specific)
                       -X       output FLAG in string (samtools-C specific)
                       -c       print only the count of matching records
                       -B       collapse the backward CIGAR operation
                       -@ INT   number of BAM compression threads [0]
                       -L FILE  output alignments overlapping the input BED FILE [null]
                       -t FILE  list of reference names and lengths (force -S) [null]
                       -T FILE  reference sequence file (force -S) [null]
                       -o FILE  output file name [stdout]
                       -R FILE  list of read groups to be outputted [null]
                       -f INT   required flag, 0 for unset [0]
                       -F INT   filtering flag, 0 for unset [0]
                       -q INT   minimum mapping quality [0]
                       -l STR   only output reads in library STR [null]
                       -r STR   only output reads in read group STR [null]
                       -s FLOAT fraction of templates to subsample; integer part as seed [-1]
                       -?       longer help
              You'll also often find the -@ option useful, at least if you ever need to make BAM files.

              Comment

              • martin2
                Member
                • Nov 2010
                • 42

                #8
                If somebody trips over this old thread ...Notably, samtools extract whole read overlapping the region of interest. If you want to extract the alignment just in the region you specified, you are out of luck. This is still a largely unsolved feature. But I found gofasta can do that while returning you modified reads back in dash-padded multiFASTA ALN file (without their INSertions relative to the reference, it just omits the inserted nucleotides). See https://bioinformatics.stackexchange...es/23003#23003 for details. Not ideal but somewhat works for me.

                Comment

                • Jorpena
                  Junior Member
                  • Sep 2025
                  • 1

                  #9
                  Following up on martin2's comment, I found that bedtools intersect is a better option if you want to extract only the reads that fully fall in the regions of interest, and it gives you way more flexibility on how you want to define the intersections. For example, to get only the reads that completely fall in the BED regions

                  HTML Code:
                  bedtools intersect -f 1 -wa -abam BAMFILE -b BEDFILE
                  And if you want the read fragments that fall in the BED regions you can use the default values

                  HTML Code:
                  bedtools intersect -abam BAMFILE -b BEDFILE
                  Also, the output will be a BAM file which can then be piped to samtools to do other things.

                  Comment

                  Latest Articles

                  Collapse

                  • SEQadmin2
                    Multiomic Liquid Biopsy: What DNA/RNA Panels Reveal That DNA Alone Can’t
                    by SEQadmin2



                    Tumors contain complex mixtures of cell populations that evolve in response to treatment and host-specific factors. Tissue biopsies provide only static snapshots of a tumor’s state. But what if we could follow that evolution over time using a minimally invasive technique?

                    Liquid biopsies can detect tumor-derived material circulating in the bloodstream, providing a way to study treatment response, detect emerging resistance, and discover new biomarkers. Circulating
                    ...
                    10-14-2026, 09:55 AM
                  • SEQadmin2
                    New Genomics Technologies Take Aim at Long-Standing Limits
                    by SEQadmin2


                    Researchers using sequencing and genomics tools often have to make trade-offs. They can choose between speed or scale, short reads or long-range information, or targeted panels or a view of the whole transcriptome. New technologies that have been released this year are built to address those tough choices.

                    We asked six companies the same four questions to learn about their latest products. The new technologies bring a lot to the table, including rethinking sequencing
                    ...
                    09-28-2026, 10:25 AM

                  ad_right_rmr

                  Collapse

                  News

                  Collapse

                  Topics Statistics Last Post
                  Started by SEQadmin2, 10-15-2026, 10:52 AM
                  0 responses
                  20 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 10-14-2026, 05:39 AM
                  0 responses
                  26 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 10-13-2026, 09:56 AM
                  0 responses
                  22 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 10-09-2026, 11:14 AM
                  0 responses
                  28 views
                  0 reactions
                  Last Post SEQadmin2  
                  Working...