Thanks, It is clear now.
Same topic but different issue, So If I want to calculate the insert size to be aligner independent; It will be something like:
read.next_reference_start - read.reference_end ? "assuming that the reads aligned in the right direction"
I was trying to identify the insert size between paired reads. When using pysam template_length will do the work, I was confused with reference_length
aligned length of the read on the reference genome. This is equal to aend - pos. Returns None if not available.
I thought that either they will be same value or close, but this is what I got:
template_length reference_length
6 128
6 148
920 148
920 147
151 148
151 148
74 74
925 148
why some reads have big difference?
Thanks,
1 answer
The reference_length is just the number of bases in a given read that are mapped. This completely ignores any mates and how whatever the aligner is deciding to call the length of the sequenced fragment (i.e., the TLEN field in the SAM/BAM/CRAM file). The template_length is then whatever the TLEN value is in the SAM/BAM/CRAM file. For your example where the values are similar you have cases where the mates are overlapping. For cases where the TLEN is much smaller, it's likely that the reads overlap in a way that the aligner didn't expect:
<---------------- R1
-----------------> R2
For the third case, there's just a larger fragment size.
Take for example the following fake reads:
@HD VN:1.4 SO:coordinate
@SQ SN:1 LN:195471971
r1 97 1 1 255 50M = 100 150 * *
r1 145 1 100 255 50M = 1 -150 * *
r2 145 1 200 255 50M = 245 10 * *
r2 97 1 245 255 50M = 200 -10 * *
r3 97 1 300 255 50M = 500 250 * *
r3 145 1 500 255 50M = 300 -250 * *
Then in python:
>>> import pysam
>>> bam = pysam.AlignmentFile("foo.bam")
>>> bam = pysam.AlignmentFile("foo.bam")
>>> for b in bam:
... print("{}\t{}".format(b.template_length, b.reference_length))
...
150 50
-150 50
10 50
-10 50
250 50
-250 50
If by insert size (it's a meaningless term in my opinion, since that's not really an insert) you mean the part of each fragment not sequenced then yes.
Thanks for the information, but that will lead to this question, What is a meaning insert size? shall I add query_alignment_length to the equation? what is the right way to calculate it?
It depends wholly on what you mean. I only think in terms of fragment lengths, which is the template_length in most cases.
Log in to answer this question.
I think the difference is in in softclips: your template is longer than what is aligned to the reference there. Could that explain what you see?
Actually I see both, some times template shorter sometimes the other way around.
Maybe this related discussion helps? Fragment Size: TLEN vs. isize
Both
tlenandisizedeprecated now usingtemplate_length. but this have no relation withreference_lengthwhich is equals toreference_end - reference_startTLEN is template length, no? Also are your aligned reads spliced or not?
Yes Tlen is template_length.
No reads are not spliced