I have a BAM file (containing reads mapped to only one chromosome), that has a very long header. I need to replace the long header of the chromosome by "chrM" and did the following:
samtools view -H my.bam > header.sam
sed "s/longheader/chrM/" header.sam > header_corrected.sam # (longheader stands for the long header..)
samtools reheader header_corrected.sam my.bam
Both header.sam and header_corrected.sam have the appropriate header. This query doesn't work and only displays a bunch of unreadable characters just as is I didn't use samtools to read the bam file.. what's wrong with my query? Is there another way to substitute "longheader" with "chrM"?
Your guess is correct. Samtools will never modify a file in place, since that would almost certainly lead to a corrupted file.
It worked well, thanks :)