Answers.Body: <p>The TLEN for single-end reads is defined as 0 in the SAM specification, which is why this happens. You can still get the end position of the alignment by parsing the CIGAR string. An example using the pysam package in python is below:</p>
  
  <pre class="lang-py prettyprint-override"><code>import pysam
  
  bam = pysam.AlignmentFile("myfile.bam")
  for b in bam.fetch():
      print("This alignment spans {}:{}-{}".format(b.reference_name, b.reference_start+1, b.reference_end))
  </code></pre>
  
  <p>The <code>reference_end</code> value is automatically calculated from the CIGAR string. </p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-16T07:06:30.117Z
Answers.LastActivityDate: 2018-08-16T07:06:30.117Z
Answers.UserDisplayName: Devon Ryan
Body: <p>I have used minimap2 to map some pacbio reads to a reference genome. I would like to know the "insert size" (true length of sequence) relative to the reference. More specifically, I want to know the first base position in the reference where the read maps and the last base position in the reference where the mapping ends.</p>
  
  <p>As I understand it I need the TLEN field from bam files (<a href="https://samtools.github.io/hts-specs/SAMv1.pdf" rel="nofollow noreferrer">sam format field 9</a>). However, all the alignment files from minimap2 have <code>0</code> in the TLEN field.</p>
  
  <p>Does anyone know how to get pacbio alignments with the TLEN field? I am also happy using <code>bwa</code> or another aligner.</p>
  
  <p>The command I used was <code>minimap2 -ax map-pb {fasta} {reads} | samtools view -h -b - | samtools sort - &gt; myfile.bam</code>.</p>
  
Tags: <phylogenetics><bwa><minimap2>
Title: Minimap2 -ax map-pb doesn't output tlen field
ThreadText: # Minimap2 -ax map-pb doesn't output tlen field
  
  (Asked by: username_0 on 2018-08-15)
  
  I have used minimap2 to map some pacbio reads to a reference genome. I would like to know the "insert size" (true length of sequence) relative to the reference. More specifically, I want to know the first base position in the reference where the read maps and the last base position in the reference where the mapping ends.
  
  As I understand it I need the TLEN field from bam files ([sam format field 9](https://samtools.github.io/hts-specs/SAMv1.pdf)). However, all the alignment files from minimap2 have `0` in the TLEN field.
  
  Does anyone know how to get pacbio alignments with the TLEN field? I am also happy using `bwa` or another aligner.
  
  The command I used was `minimap2 -ax map-pb {fasta} {reads} | samtools view -h -b - | samtools sort - > myfile.bam`.
  
  **Tags:** phylogenetics, bwa, minimap2
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-08-16. Score: 2 | Accepted answer
  
  The TLEN for single-end reads is defined as 0 in the SAM specification, which is why this happens. You can still get the end position of the alignment by parsing the CIGAR string. An example using the pysam package in python is below:
  
  ```py
  import pysam
  
  bam = pysam.AlignmentFile("myfile.bam")
  for b in bam.fetch():
      print("This alignment spans {}:{}-{}".format(b.reference_name, b.reference_start+1, b.reference_end))
  
  ```
  
  The `reference_end` value is automatically calculated from the CIGAR string.
  
  
  ---
  
  