I posted this on the samtools groups with no luck, so hopefully here i get better luck
I'm changing part of the header in a number of bam files. however, after I do that, the resulting bam file is truncated....
I used the following code borrowed/adapted from here
samtools view -H file.bam | grep 'SN:X\|^@RG\|^@PG\|^@HD' | samtools reheader - file.bam > bam_final/new_name.bam
and
samtools view -h bam_final/new_name.bam
gives me
[main_samview] truncated file.
right after the new header.... any suggestions?
You are only doing a
grep
for the pattern but are not changing anything? Is that what you meant to do? The thread you linked usessed
to make changes and then writing them out usingsamtools reheader
.yes, I wanted to
grep
out only the X chr and the three headers indicated. I did usesamtools reheader
indeedNot sure I understand what you want to do. Leave only the headers for X and remove others? If you only run the command up to the
grep
part, do you get what you expected to see?that's correct and I do get the header I expect to see.
I just tried your command on a file (selecting for just chrX) with samtools v. 1.3.1 and was able to re-header the file without an error. What version of samtools are you using?
Version: 1.3-27-g6994f30 (using htslib 1.3-51-g55ff37c)
hmmm...I need to see if we have an earlier version on the server... so your bam file was not truncated, correct?
I do not get any error with
samtools view -H
and the size looks about the same after re-header.have you tried to look at the first few lines? below the header like
i still get the truncated file error