Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tahamasoodi
    Success
    • May 2012
    • 130

    #1

    Remove header from BAM file

    Hi,

    How can we remove header from a BAM file?
    Thanks,
  • kmcarr
    Senior Member
    • May 2008
    • 1181

    #2
    Originally posted by tahamasoodi View Post
    Hi,

    How can we remove header from a BAM file?
    Do you mean to strip the header off entirely? You can't. Without a minimal header it is no longer a valid BAM file.

    Comment

    • swbarnes2
      Senior Member
      • May 2008
      • 910

      #3
      If you really want to do that...

      Code:
      samtools view -b header.bam > noheader.bam

      Comment

      • kmcarr
        Senior Member
        • May 2008
        • 1181

        #4
        Originally posted by swbarnes2 View Post
        If you really want to do that...

        Code:
        samtools view -b header.bam > noheader.bam
        That won't do it. I assume you are thinking that without the -h option samtools view will not output a header, but the -h option only applies to SAM (text) output. Your command will output exactly the same file as the input.

        I think you were giong for

        Code:
        samtools view header.bam | samtools view -Sb - > noheader.bam
        This command will give you a warning that no @SQ lines are present but will generate an output BAM. The resulting BAM will be worthless though. Without the @SQ information all of your alignment entries will have no chromosome name (just '*') and all of the positions will be '0'. You have essentially just stripped out the most important bits of data from the file.

        Comment

        • maubp
          Peter (Biopython etc)
          • Jul 2009
          • 1544

          #5
          If you want to edit or remove the embedded SAM plain text header in a BAM file, use the 'samtools reheader' command.

          Comment

          • tahamasoodi
            Success
            • May 2012
            • 130

            #6
            Thanks, if I replace the header will it have any effect on the chromosome name and positions?
            Thanks,

            Comment

            • dpryan
              Devon Ryan
              • Jul 2011
              • 3478

              #7
              Originally posted by tahamasoodi View Post
              Thanks, if I replace the header will it have any effect on the chromosome name and positions?
              It can. In BAM files, chromosome (or contig) names are only stored in the header. In the actual reads, what is stored is a numeric index saying where in the header the chromosome/contig name is located. So, if you shuffled around the chromosome ordering in the header without changing all of the reads, the reads would then be mapped to different places (same position, just different chromosomes). For that reason, one should reheader BAM files with care.

              Comment

              • tahamasoodi
                Success
                • May 2012
                • 130

                #8
                But the picard's ReplaceSamHeader will replace it in all the reads, then how come it affects the bam file?
                Thanks,

                Comment

                • dpryan
                  Devon Ryan
                  • Jul 2011
                  • 3478

                  #9
                  Originally posted by tahamasoodi View Post
                  But the picard's ReplaceSamHeader will replace it in all the reads, then how come it affects the bam file?
                  As its name implies, the picard ReplaceSamHeader command is intended for SAM files, which are just regular human readable text files. In that case, there's no need to monkey with the reads. My comment is more in regards to the samtools reheader command, which quite literally just produces a BAM file with a different header.

                  Comment

                  • tahamasoodi
                    Success
                    • May 2012
                    • 130

                    #10
                    By the way read ReplaceSamHeader thoroughly, it is not for SAM files only but it can be used for BAM files also. The original post was not related to samtools reheader but something different.
                    Thanks,

                    Comment

                    • swbarnes2
                      Senior Member
                      • May 2008
                      • 910

                      #11
                      Originally posted by kmcarr View Post
                      That won't do it. I assume you are thinking that without the -h option samtools view will not output a header, but the -h option only applies to SAM (text) output. Your command will output exactly the same file as the input.

                      I think you were giong for

                      Code:
                      samtools view header.bam | samtools view -Sb - > noheader.bam
                      Ah, thanks. I was not at a computer where I could check it, and it's not anything I've actually wanted to do.

                      Comment

                      Latest Articles

                      Collapse

                      • SEQadmin2
                        Multiomic Liquid Biopsy: What DNA/RNA Panels Reveal That DNA Alone Can’t
                        by SEQadmin2



                        Tumors contain complex mixtures of cell populations that evolve in response to treatment and host-specific factors. Tissue biopsies provide only static snapshots of a tumor’s state. But what if we could follow that evolution over time using a minimally invasive technique?

                        Liquid biopsies can detect tumor-derived material circulating in the bloodstream, providing a way to study treatment response, detect emerging resistance, and discover new biomarkers. Circulating
                        ...
                        10-14-2026, 09:55 AM
                      • SEQadmin2
                        New Genomics Technologies Take Aim at Long-Standing Limits
                        by SEQadmin2


                        Researchers using sequencing and genomics tools often have to make trade-offs. They can choose between speed or scale, short reads or long-range information, or targeted panels or a view of the whole transcriptome. New technologies that have been released this year are built to address those tough choices.

                        We asked six companies the same four questions to learn about their latest products. The new technologies bring a lot to the table, including rethinking sequencing
                        ...
                        09-28-2026, 10:25 AM

                      ad_right_rmr

                      Collapse

                      News

                      Collapse

                      Topics Statistics Last Post
                      Started by SEQadmin2, Yesterday, 11:13 AM
                      0 responses
                      14 views
                      0 reactions
                      Last Post SEQadmin2  
                      Started by SEQadmin2, Yesterday, 10:48 AM
                      0 responses
                      12 views
                      0 reactions
                      Last Post SEQadmin2  
                      Started by SEQadmin2, 10-15-2026, 10:52 AM
                      0 responses
                      32 views
                      0 reactions
                      Last Post SEQadmin2  
                      Started by SEQadmin2, 10-14-2026, 05:39 AM
                      0 responses
                      40 views
                      0 reactions
                      Last Post SEQadmin2  
                      Working...