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
                        Beyond CRISPR/Cas9: Understand, Choose, and Use the Right Genome Editing Tool
                        by SEQadmin2



                        CRISPR/Cas9 sparked the gene editing revolution for both research and therapeutics.1 But this system still showed severe issues that limited its applications. The most prominent were the heavy reliance on PAM sequences, delivery limitations, double-stranded breaks that prompt unintended edits and cell death, and editing inefficiency (both in targeting and in knock-in reliability).

                        Despite this, “CRISPR helped turn genome editing from a specialized technique into
                        ...
                        07-31-2026, 11:01 AM
                      • SEQadmin2
                        Proteomic Platforms: How to Choose the Right Analytical Strategy to Improve Detection and Clinical Applications
                        by SEQadmin2


                        Proteomics platforms are evolving rapidly, with advances in mass spectrometry and affinity-based approaches expanding what researchers can detect and at what scale. As the field moves toward deeper proteome coverage and clinical applications, scientists face an increasingly complex landscape of tools. This article will explore how researchers are navigating these choices to find the right platform for their work.

                        The systematic characterization of the human proteome has
                        ...
                        07-20-2026, 11:48 AM
                      • SEQadmin2
                        Advanced Sequencing Platforms Tackle Neuroscience’s Toughest Genomics Problems
                        by SEQadmin2



                        Genomics studies in neuroscience face a special challenge due to the brain’s complexity and scarcity of samples. Mapping changes in cell type and state using conventional next-generation sequencing methods remains challenging. Advances in technologies like single-cell sequencing, spatial transcriptomics, and long-read sequencing have opened the door to deeper studies of the brain and diseases like Alzheimer’s, amyotrophic lateral sclerosis (ALS), and schizophrenia.
                        ...
                        07-09-2026, 11:10 AM

                      ad_right_rmr

                      Collapse

                      News

                      Collapse

                      Topics Statistics Last Post
                      Started by SEQadmin2, Today, 07:41 AM
                      0 responses
                      9 views
                      0 reactions
                      Last Post SEQadmin2  
                      Started by SEQadmin2, 08-03-2026, 10:13 AM
                      0 responses
                      24 views
                      0 reactions
                      Last Post SEQadmin2  
                      Started by SEQadmin2, 07-31-2026, 02:55 AM
                      0 responses
                      38 views
                      0 reactions
                      Last Post SEQadmin2  
                      Started by SEQadmin2, 07-24-2026, 12:17 PM
                      0 responses
                      25 views
                      0 reactions
                      Last Post SEQadmin2  
                      Working...