Answers.Body: <p>I think that you are dealing with two different phenomena:</p>
  
  <p><strong>1) TLEN == 0 reads</strong></p>
  
  <p>From the BAM spec:</p>
  
  <blockquote>
    <p>TLEN: signed observed Template LENgth. If all segments are mapped to the same reference sequence, the absolute value of TLEN equals the distance between the mapped end of the template and the mapped start of the template, inclusively (i.e., end − start + 1).14 Note that mapped base is defined to be one that aligns to the reference as described by CIGAR, hence excludes soft-clipped bases. The TLEN field is positive for the leftmost segment of the template, negative for the rightmost, and the sign for any middle segment is undefined. If segments cover the same coordinates then the choice of which is leftmost and rightmost is arbitrary, but the two ends must still have differing signs. <strong>It is set as 0 for a single-segment template or when the information is unavailable (e.g., when the first or last segment of a multi-segment template is unmapped or when the two are mapped to different reference sequences).</strong></p>
  </blockquote>
  
  <p>Note that in each case the mate read appears to be mapped to a different chromosome/contig. So, the mapper can't estimate insert size in these cases, so it just says zero.</p>
  
  <p><strong>2. Multiple reads with same qname</strong></p>
  
  <p>There are various reasons why the same qname can appear multiple times. Again from the BAM spec, one such explanation is that it is a chimeric alignment:</p>
  
  <blockquote>
    <p><strong>Chimeric alignment:</strong> An alignment of a read that cannot be represented as a linear alignment. A chimeric alignment is represented as a set of linear alignments that do not have large overlaps. Typically, one of the linear alignments in a chimeric alignment is considered the “representative” alignment, and the others are called “supplementary” and are distinguished by the supplementary alignment flag. All the SAM records in a chimeric alignment have the same QNAME and the same values for 0x40 and 0x80 flags (see Section 1.4). The decision regarding which linear alignment is representative is arbitrary.</p>
  </blockquote>
  
  <p>You may have to do some digging and looking at the different flags to decide what exactly is going on, but my bet is that they are supplementary alignments.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-20T17:36:25.230Z
Answers.LastActivityDate: 2020-04-20T17:36:25.230Z
Answers.UserDisplayName: Maximilian Press
Body: <p>I have removed duplicates from my bam file (not secondary, nor suplementary alignments), and then I have looked for some repeated <code>qnames</code> (to assess the failure of a program) and I've found a lot of them. All of them has <code>TLEN</code> equal to 0. </p>
  
  <p>As an example: this is part of a bed file that a generate from the bam.</p>
  
  <pre><code>CHR  Start   End       QNAME                                 PNEXT  TLEN
  12 86945793 86945944 A05325:240:SS348DSZX:2:5105:12246:3790 90044835 0
  4 168461445 168461485 A05325:240:SS348DSZX:2:5105:12246:3790 90044835 0
  </code></pre>
  
  <p>And those are these reads in the original bam:</p>
  
  <pre><code>A05325:240:SS348DSZX:2:5105:12246:3790  2177    4       168461445       0       111H40M 11      90044835        0       ATAAAATTAAAAAAATAAAAAAATTAAAAAAAAAAAAAAA        :,F,F,,FF:FF,F,,F,FFFF,,F,F,:,:,FFFFFFFF     NM:i:2  MD:Z:28T8G2     MC:Z:151M       AS:i:32 XS:i:31 RG:Z:HM327DSXX.3        SA:Z:12,86945793,+,20S37M94S,1,1;
  A05325:240:SS348DSZX:2:5105:12246:3790  129     12      86945793        1       20S37M94S       11      90044835        0       ATGGTGAAAAAACAAATTATTTAAAAAAATCAAAAAAAAAAAAAAAACCAAAAAACATAAAAAAAAGGGCAGTAATTTTATTCATGGGGTCAAACAAAAAAAAAGCATAATATAAAATTAAAAAAATAAAAAAATTAAAAAAAAAAAAAAA      FF,,,:,F,F,,,,F,,,,,,,F,FFFF:,:FFFF:FFF,FF::F,:,,FFFF,,,F,FF::,,F,::,,,,F,,F:FF,,,,,,,F::F,F,,,,F,FFFFF:,F,,F,,:,F,F,,FF:FF,F,,F,FFFF,,F,F,:,:,FFFFFFFF      NM:i:1  MD:Z:9G27       MC:Z:151M       AS:i:32 XS:i:29 RG:Z:HM327DSXX.3        SA:Z:4,168461445,+,111S40M,0,2;
  </code></pre>
  
  <p>This are the commands used to remove duplicates:</p>
  
  <pre><code>samtools view -F 1024 -bo filtered.bam original.bam
  
  </code></pre>
  
  <p>The commands used to generate this bed were:</p>
  
  <pre><code>samtools view sample.bam | awk '{ print $3, $4, $4+length($10), $1, $8, $9}' &gt; sample.bed
  </code></pre>
  
  <p>Why do I present those alignments?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-04-18T18:37:51.913Z
Comments.Text: Can you tell us more about how you generated the BED? I'm not sure that I know what `TSLEN` is off the top of my head. Googling around I found https://www.biostars.org/p/78878/ , maybe you are talking about TLEN (e.g. "TLEN: signed observed Template LENgth")? Have you already filtered alternate mappings from the BAM?
Comments.UserDisplayName: Maximilian Press
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-04-18T19:12:06.830Z
Comments.Text: Those reads are from different tiles (3105 and 5105).
Comments.UserDisplayName: Devon Ryan
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-04-18T23:06:45.523Z
Comments.Text: You're right! I've just edited to clarify these issues.
Comments.UserDisplayName: Jeni
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-04-19T08:44:31.587Z
Comments.Text: Please post the original SAM entries (before any filtering). Also describe exactly how deplicate removal was performed (with the commands).
Comments.UserDisplayName: Devon Ryan
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-04-19T11:33:02.383Z
Comments.Text: Okay, i've added this information
Comments.UserDisplayName: Jeni
Tags: <phylogenetics>
Title: What's the meaning of having different alignments with the same qname in a bam with no duplicates?
ThreadText: # What's the meaning of having different alignments with the same qname in a bam with no duplicates?
  
  (Asked by: username_0 on 2020-04-18)
  
  I have removed duplicates from my bam file (not secondary, nor suplementary alignments), and then I have looked for some repeated `qnames` (to assess the failure of a program) and I've found a lot of them. All of them has `TLEN` equal to 0.
  
  As an example: this is part of a bed file that a generate from the bam.
  
  ```
  CHR  Start   End       QNAME                                 PNEXT  TLEN
  12 86945793 86945944 A05325:240:SS348DSZX:2:5105:12246:3790 90044835 0
  4 168461445 168461485 A05325:240:SS348DSZX:2:5105:12246:3790 90044835 0
  
  ```
  
  And those are these reads in the original bam:
  
  ```
  A05325:240:SS348DSZX:2:5105:12246:3790  2177    4       168461445       0       111H40M 11      90044835        0       ATAAAATTAAAAAAATAAAAAAATTAAAAAAAAAAAAAAA        :,F,F,,FF:FF,F,,F,FFFF,,F,F,:,:,FFFFFFFF     NM:i:2  MD:Z:28T8G2     MC:Z:151M       AS:i:32 XS:i:31 RG:Z:HM327DSXX.3        SA:Z:12,86945793,+,20S37M94S,1,1;
  A05325:240:SS348DSZX:2:5105:12246:3790  129     12      86945793        1       20S37M94S       11      90044835        0       ATGGTGAAAAAACAAATTATTTAAAAAAATCAAAAAAAAAAAAAAAACCAAAAAACATAAAAAAAAGGGCAGTAATTTTATTCATGGGGTCAAACAAAAAAAAAGCATAATATAAAATTAAAAAAATAAAAAAATTAAAAAAAAAAAAAAA      FF,,,:,F,F,,,,F,,,,,,,F,FFFF:,:FFFF:FFF,FF::F,:,,FFFF,,,F,FF::,,F,::,,,,F,,F:FF,,,,,,,F::F,F,,,,F,FFFFF:,F,,F,,:,F,F,,FF:FF,F,,F,FFFF,,F,F,:,:,FFFFFFFF      NM:i:1  MD:Z:9G27       MC:Z:151M       AS:i:32 XS:i:29 RG:Z:HM327DSXX.3        SA:Z:4,168461445,+,111S40M,0,2;
  
  ```
  
  This are the commands used to remove duplicates:
  
  ```
  samtools view -F 1024 -bo filtered.bam original.bam
  
  ```
  
  The commands used to generate this bed were:
  
  ```
  samtools view sample.bam | awk '{ print $3, $4, $4+length($10), $1, $8, $9}' > sample.bed
  
  ```
  
  Why do I present those alignments?
  
  **Tags:** phylogenetics
  
  > **Comments:**
  > **username_1**: Can you tell us more about how you generated the BED? I'm not sure that I know what `TSLEN` is off the top of my head. Googling around I found https://www.biostars.org/p/78878/ , maybe you are talking about TLEN (e.g. "TLEN: signed observed Template LENgth")? Have you already filtered alternate mappings from the BAM?  
  > **username_2**: Those reads are from different tiles (3105 and 5105).  
  > **username_0**: You're right! I've just edited to clarify these issues.  
  > **username_2**: Please post the original SAM entries (before any filtering). Also describe exactly how deplicate removal was performed (with the commands).  
  > **username_0**: Okay, i've added this information  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-04-20. Score: 2
  
  I think that you are dealing with two different phenomena:
  
  **1) TLEN == 0 reads**
  
  From the BAM spec:
  
  > TLEN: signed observed Template LENgth. If all segments are mapped to the same reference sequence, the absolute value of TLEN equals the distance between the mapped end of the template and the mapped start of the template, inclusively (i.e., end − start + 1).14 Note that mapped base is defined to be one that aligns to the reference as described by CIGAR, hence excludes soft-clipped bases. The TLEN field is positive for the leftmost segment of the template, negative for the rightmost, and the sign for any middle segment is undefined. If segments cover the same coordinates then the choice of which is leftmost and rightmost is arbitrary, but the two ends must still have differing signs. **It is set as 0 for a single-segment template or when the information is unavailable (e.g., when the first or last segment of a multi-segment template is unmapped or when the two are mapped to different reference sequences).**
  
  Note that in each case the mate read appears to be mapped to a different chromosome/contig. So, the mapper can't estimate insert size in these cases, so it just says zero.
  
  **2. Multiple reads with same qname**
  
  There are various reasons why the same qname can appear multiple times. Again from the BAM spec, one such explanation is that it is a chimeric alignment:
  
  > **Chimeric alignment:** An alignment of a read that cannot be represented as a linear alignment. A chimeric alignment is represented as a set of linear alignments that do not have large overlaps. Typically, one of the linear alignments in a chimeric alignment is considered the “representative” alignment, and the others are called “supplementary” and are distinguished by the supplementary alignment flag. All the SAM records in a chimeric alignment have the same QNAME and the same values for 0x40 and 0x80 flags (see Section 1.4). The decision regarding which linear alignment is representative is arbitrary.
  
  You may have to do some digging and looking at the different flags to decide what exactly is going on, but my bet is that they are supplementary alignments.
  
  
  ---
  
  