<p>The start position is the first correctly mapped base. One way to do it, is to use GenomicAlignments in R to read in the alignments.</p>

<p>For example the sam file looks like this:</p>

<pre><code>cat test.sam 
@HD VN:1.3  SO:coordinate
@SQ SN:chrI LN:228861
D00689:415:CD4LUANXX:3:1316:7447:98083#CGGCTATGCAGGACGT 0   chrI    769 0   42M8S   *   0   0   CCATAGCCCGGTGGGGCCTGTGCTGCCACAGAGGGTGTGTCTAGATCGGA  B/B//FFFFFFBFFFF//7FF/&lt;F/FFBFFFF//&lt;F///&lt;F/BFB//&lt;F/  NM:i:1  MD:Z:21A20  AS:i:37 XS:i:37
D00689:415:CD4LUANXX:3:2308:9597:30445#CGGCTATGCAGGACGT 0   chrI    3965    0   3S47M   *   0   0   CCACACACCCACACACCACACCCACACCACACCCACACCACACCCACACC  BBBBBFFFFFFFFFFFFFFFFFFFFFFFFFFFFBFFFFFBF&lt;FFFFF&lt;FF  NM:i:0  MD:Z:47 AS:i:47 XS:i:47
</code></pre>

<p>We convert to bam:</p>

<pre><code>samtools view -bS test.sam &gt; test.bam
</code></pre>

<p>And read using R:</p>

<pre><code>library(GenomicAlignments)
gr = readGAlignments("test.bam")
gr

GAlignments object with 2 alignments and 0 metadata columns:
      seqnames strand       cigar    qwidth     start       end     width
         &lt;Rle&gt;  &lt;Rle&gt; &lt;character&gt; &lt;integer&gt; &lt;integer&gt; &lt;integer&gt; &lt;integer&gt;
  [1]     chrI      +       42M8S        50       769       810        42
  [2]     chrI      +       3S47M        50      3965      4011        47
          njunc
      &lt;integer&gt;
  [1]         0
  [2]         0
  -------
  seqinfo: 1 sequence from an unspecified genome
</code></pre>

<p>The start and the end can be retrieved using:</p>

<pre><code>start(gr)
[1]  769 3965
end(gr)
[1]  810 4011
</code></pre>

CC BY-SA 4.0
2020-04-17T14:19:08.043Z
2020-04-17T14:19:08.043Z
StupidWolf
<p>The match is starting with the first mapped base ignoring any softclipped parts of the read.  </p>

<p>As an example this are two reads from a bam file of mine:</p>

<pre><code>start   cigar   sequence 
11710   17S83M  NNNNNNNNNNNNNNNNNGGCCTGGCCATGTGTATTTTTTTAAATTTCCACTGATGATTTTGCTGCATGGCCGGTGTTGAGAATGACTGCGCAAATTTGCC   
11713   100M    CTGGCCATGTGTATTTTTTTAAATTTCCACTGATGATTTTGCTGCATGGCCGGTGTTGAGAATGACTGCGCAAATTTGCCGGATTTCCTTTGCTGTTCCT
</code></pre>

CC BY-SA 4.0
2020-04-17T11:35:09.920Z
2020-04-17T11:42:56.453Z
2020-04-17T11:42:56.453Z
PPK
<p>I was wondering how could I obtain the end position of an alignment using its start position and its CIGAR String.</p>

<p>If an alignment present some soft clipping, for example: </p>

<pre><code>
    20S55M

</code></pre>

<p>Then, would the start position indicated in the SAM file be the first soft clipped base, or the first correctly mapped base?</p>

<p>Thanks</p>

<r><phylogenetics><sam><cigar>
Extract end position from CIGAR
# Extract end position from CIGAR

(Asked by: username_0 on 2020-04-17)

I was wondering how could I obtain the end position of an alignment using its start position and its CIGAR String.
If an alignment present some soft clipping, for example: 

    20S55M


Then, would the start position indicated in the SAM file be the first soft clipped base, or the first correctly mapped base?
Thanks

**Tags:** r, phylogenetics, sam, cigar

---

## 2 answers

### Answer by username_1 on 2020-04-17. Score: 3

The start position is the first correctly mapped base. One way to do it, is to use GenomicAlignments in R to read in the alignments.
For example the sam file looks like this:
cat test.sam 
@HD VN:1.3  SO:coordinate
@SQ SN:chrI LN:228861
D00689:415:CD4LUANXX:3:1316:7447:98083#CGGCTATGCAGGACGT 0   chrI    769 0   42M8S   *   0   0   CCATAGCCCGGTGGGGCCTGTGCTGCCACAGAGGGTGTGTCTAGATCGGA  B/B//FFFFFFBFFFF//7FF/
We convert to bam:
samtools view -bS test.sam > test.bam

And read using R:
library(GenomicAlignments)
gr = readGAlignments("test.bam")
gr

GAlignments object with 2 alignments and 0 metadata columns:
      seqnames strand       cigar    qwidth     start       end     width
                
  [1]     chrI      +       42M8S        50       769       810        42
  [2]     chrI      +       3S47M        50      3965      4011        47
          njunc
      
  [1]         0
  [2]         0
  -------
  seqinfo: 1 sequence from an unspecified genome

The start and the end can be retrieved using:
start(gr)
[1]  769 3965
end(gr)
[1]  810 4011


---

### Answer by username_2 on 2020-04-17. Score: 2

The match is starting with the first mapped base ignoring any softclipped parts of the read.  
As an example this are two reads from a bam file of mine:
start   cigar   sequence 
11710   17S83M  NNNNNNNNNNNNNNNNNGGCCTGGCCATGTGTATTTTTTTAAATTTCCACTGATGATTTTGCTGCATGGCCGGTGTTGAGAATGACTGCGCAAATTTGCC   
11713   100M    CTGGCCATGTGTATTTTTTTAAATTTCCACTGATGATTTTGCTGCATGGCCGGTGTTGAGAATGACTGCGCAAATTTGCCGGATTTCCTTTGCTGTTCCT


---

