Unconfigured Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • pirates.genome
    Member
    • Mar 2012
    • 11

    Extract aligned sequence coordinates from SAM or BAM file

    Hi,
    I have a Illumina sequence reads which I mapped to HG19 using BWA. Resulting BAM file is indexed and sorted. Now what I want is to identify the region of alignment. Let me give an example, on chromosome 1 starting from base 1 to 10000, there are many reads aligned and then there is no any read aligned from 10000 to 20000 and then again from 20000 to 30000 there are many reads aligned and so on...

    I want an output like
    chr1 1 10000
    chr1 20000 30000
    etc...etc...

    I searched for tools doing similar functions but didn't find anything.
    Any help will be really appreciated.

    Thanks.
  • colindaven
    Senior Member
    • Oct 2008
    • 417

    #2
    Have a look at a variation of:

    samtools view -L bedfile.bed x.bam

    Comment

    • pirates.genome
      Member
      • Mar 2012
      • 11

      #3
      Thnx colindaven,

      I looked into samtools view -L option but that is not what I am looking for.

      Let me explain the problem,
      I have targeted DNA sequencing data from Illumina. After aligning to reference using BWA I got BAM files. From BAM files I want to extract a continuous stretch of reference region to which multiple reads are aligned. Assume that reads are aligned in following way:
      read1 aligned to chr1:1-50
      read2 chr1:10-60
      read3 chr1:30-80
      read4 chr1:150-200
      read5 chr1:200-250
      read6 chr1:250-300
      read7 chr1:350-400
      and so on....

      Now I want continuous stretch of reference to which any read is aligned. Output should be:
      chr1:1-80
      chr1:150-300
      chr1:350-400
      and so on...

      This output tells us that there are several number of reads aligned to chr1 position 1 to 80 in continuation of each other and then no any read aligned to position 80 to 150 and so on...

      Is there an easy way to do so..??..

      Thanks.

      Comment

      • Richard Finney
        Senior Member
        • Feb 2009
        • 701

        #4
        SIMPLE VERSION ... using samtools depth and awk ...
        #ploc = previous location, pchr=previous chromsome
        #Set SAMT and BAMF to samtools and your bamfile.
        export SAMT=/h1/finneyr/samtools-0.1.18/samtools
        export BAMF=98023.bam
        $SAMT depth $BAMF | awk '{ if ($2!=(ploc+1)){if (ploc!=0){printf("%s %d-%d\n",$1,s,ploc);}s=$2} ploc=$2; }


        More complicated version that handles chr1->chr2 transition and flushes at the end. (I think, this is not completely debugged and and is just a one-off. Not all corner conditions maybe addressed.)

        $SAMT depth $BAMF | \
        awk '
        BEGIN{firsttime=1;}
        {
        if (pchr!=$1) { if (firsttime==1) { firsttime = 0;} else { printf("%s %d-%d\n",pchr,s,ploc);}s=$2}
        else { if ($2!=(ploc+1)){if (ploc!=0){printf("%s %d-%d\n",$1,s,ploc);}s=$2} }
        ploc=$2; pchr=$1
        }
        END{ printf("%s %d-%d\n",pchr,s,ploc);}
        '

        CAVEAT: samtools depth doesn't do cigar 'N' quite right so this won't work for RNA in the best way.
        Last edited by Richard Finney; 08-16-2012, 06:58 AM.

        Comment

        • pirates.genome
          Member
          • Mar 2012
          • 11

          #5
          Originally posted by Richard Finney View Post
          SIMPLE VERSION ... using samtools depth and awk ...
          #ploc = previous location, pchr=previous chromsome
          #Set SAMT and BAMF to samtools and your bamfile.
          export SAMT=/h1/finneyr/samtools-0.1.18/samtools
          export BAMF=98023.bam
          $SAMT depth $BAMF | awk '{ if ($2!=(ploc+1)){if (ploc!=0){printf("%s %d-%d\n",$1,s,ploc);}s=$2} ploc=$2; }


          More complicated version that handles chr1->chr2 transition and flushes at the end. (I think, this is not completely debugged and and is just a one-off. Not all corner conditions maybe addressed.)

          $SAMT depth $BAMF | \
          awk '
          BEGIN{firsttime=1;}
          {
          if (pchr!=$1) { if (firsttime==1) { firsttime = 0;} else { printf("%s %d-%d\n",pchr,s,ploc);}s=$2}
          else { if ($2!=(ploc+1)){if (ploc!=0){printf("%s %d-%d\n",$1,s,ploc);}s=$2} }
          ploc=$2; pchr=$1
          }
          END{ printf("%s %d-%d\n",pchr,s,ploc);}
          '

          CAVEAT: samtools depth doesn't do cigar 'N' quite right so this won't work for RNA in the best way.
          Hi Richard Finney,
          Thank you for very helpful code. First code worked good for me.

          Comment

          • ishmael
            Member
            • Jul 2008
            • 17

            #6
            I think BEDtools should also fit your demand.
            1. convert the bam into bed by bam2bed
            2. mergeBed.

            Comment

            Latest Articles

            Collapse

            • SEQadmin2
              From Collection to Sequencing: Why Sample Preparation and Preservation Define Sequencing Data
              by SEQadmin2


              Data variability is still an issue in sequencing technologies despite the advances in reproducibility and accuracy of these platforms. But the problem does not originate in the sequencing itself, but in the previous steps, before the sample reaches the sequencer.


              The first step is collection, followed by preservation and sample preparation for analysis. Most scientists overlook those steps, but not being careful might just be skewing the experiment’s results.
              ...
              06-02-2026, 10:05 AM
            • SEQadmin2
              Single-Cell Sequencing at an Inflection Point: Early Impacts of New Platforms and Emerging Trends
              by SEQadmin2


              With the launch of new single-cell sequencing platforms in 2026, the field stands at an exciting inflection point. This article surveys the most impactful advances in the field and discusses how they’re reshaping research in cancer, immunology, and beyond.


              Introduction

              Single-cell sequencing technologies have undergone remarkable advances over the past decade, transitioning from low-throughput experimental approaches to highly scalable platforms capable of...
              05-22-2026, 06:42 AM
            • SEQadmin2
              Environmental Genomics in the Age of NGS: From Microbes to Conservation Strategies
              by SEQadmin2

              Studying ecosystems means dealing with complex, multi-species communities that are hard to observe at scale. This complexity, however, hides many important questions to be answered, from how biogeochemical cycles work and how climate change can affect species distribution to how conservation strategies can work best.


              Genomics, particularly since the expansion of NGS, has transformed ecosystem ecology. By sequencing environmental DNA, we can now assess biodiversity without direct...
              05-06-2026, 09:04 AM

            ad_right_rmr

            Collapse

            News

            Collapse

            Topics Statistics Last Post
            Started by SEQadmin2, 06-02-2026, 12:03 PM
            0 responses
            19 views
            0 reactions
            Last Post SEQadmin2  
            Started by SEQadmin2, 06-02-2026, 11:40 AM
            0 responses
            14 views
            0 reactions
            Last Post SEQadmin2  
            Started by SEQadmin2, 05-28-2026, 11:40 AM
            0 responses
            29 views
            0 reactions
            Last Post SEQadmin2  
            Started by SEQadmin2, 05-26-2026, 10:12 AM
            0 responses
            31 views
            0 reactions
            Last Post SEQadmin2  
            Working...