Seqanswers Leaderboard Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • eilosei
    Member
    • Nov 2011
    • 19

    How to put header back to .bam file?

    I've filtered my .bam file for uniquely mapped reads with this command:

    samtools view file.bam | grep "XT:A:U" > file.unique.bam

    Then I wish to convert the .unique.bam file to .bed file with Bedtools but failed. The command I used is:

    bamToBed -i file.unique.bam > file.bed

    And I got error message like:

    BgzfStream ERROR: read block failed - invalid block header
    BamHeader ERROR: could not read magic number
    BamReader ERROR: Could not load header data for file.sorted.unique.bam


    It seems with the grep command I left out the header of the bam file. Can anyone tell me how to fix this problem? Thank you!
  • swbarnes2
    Senior Member
    • May 2008
    • 910

    #2
    throw a -h into "samtools view", but I won't vouch for how that will work with the grep command embedded in there.

    Comment

    • krobison
      Senior Member
      • Nov 2007
      • 734

      #3
      Originally posted by swbarnes2 View Post
      throw a -h into "samtools view", but I won't vouch for how that will work with the grep command embedded in there.
      It most certainly won't behave; the grep will strip out the header. A bit challenging (at least for me) to write a grep expression to save both, but with perl/awk/etc it is straightforward. The appropriate perl to replace the grep would be

      Code:
      perl -n -e 'print $_ if (/^\@/ || /XT:A:U/) '

      There is also the reheader command in samtools

      Code:
      samtools view -H file.bam > header.sam  # extract header only
      samtools reheader header.sam file.unique.bam
      It might even be possible to pipe the first command to the second, but I haven't tested that

      Comment

      • volks
        Member
        • Jun 2010
        • 80

        #4
        Originally posted by krobison View Post
        Code:
        samtools view -H file.bam > header.sam  # extract header only
        samtools reheader header.sam file.unique.bam
        It might even be possible to pipe the first command to the second, but I haven't tested that
        try:
        Code:
        samtools view -H file.bam > header.sam
        samtools view file.bam | grep "XT:A:U" | cat header.sam - | samtools view -Sb - > file.unique.bam

        Comment

        • eilosei
          Member
          • Nov 2011
          • 19

          #5
          Originally posted by volks View Post
          try:
          Code:
          samtools view -H file.bam > header.sam
          samtools view file.bam | grep "XT:A:U" | cat header.sam - | samtools view -Sb - > file.unique.bam
          This code works! Thank you!

          Comment

          • eilosei
            Member
            • Nov 2011
            • 19

            #6
            Thank you for the suggestion. But the reheader command came out gibberish. What's the problem?


            There is also the reheader command in samtools

            Code:
            samtools view -H file.bam > header.sam  # extract header only
            samtools reheader header.sam file.unique.bam
            It might even be possible to pipe the first command to the second, but I haven't tested that[/QUOTE]

            Comment

            • sophiespo
              Member
              • Apr 2013
              • 15

              #7
              Originally posted by eilosei View Post
              Thank you for the suggestion. But the reheader command came out gibberish. What's the problem?


              There is also the reheader command in samtools

              Code:
              samtools view -H file.bam > header.sam  # extract header only
              samtools reheader header.sam file.unique.bam
              It might even be possible to pipe the first command to the second, but I haven't tested that
              [/QUOTE]


              Sorry, this is an old question and it might be solved by now but for anyone who is searching, samtools will print to STDOUT unless you direct it somewhere.

              Code:
              samtools view -H file.bam > header.sam  # extract header only
              samtools reheader header.sam file.unique.bam > file.new.bam

              Comment

              Latest Articles

              Collapse

              • seqadmin
                Pathogen Surveillance with Advanced Genomic Tools
                by seqadmin




                The COVID-19 pandemic highlighted the need for proactive pathogen surveillance systems. As ongoing threats like avian influenza and newly emerging infections continue to pose risks, researchers are working to improve how quickly and accurately pathogens can be identified and tracked. In a recent SEQanswers webinar, two experts discussed how next-generation sequencing (NGS) and machine learning are shaping efforts to monitor viral variation and trace the origins of infectious...
                Yesterday, 11:48 AM
              • seqadmin
                New Genomics Tools and Methods Shared at AGBT 2025
                by seqadmin


                This year’s Advances in Genome Biology and Technology (AGBT) General Meeting commemorated the 25th anniversary of the event at its original venue on Marco Island, Florida. While this year’s event didn’t include high-profile musical performances, the industry announcements and cutting-edge research still drew the attention of leading scientists.

                The Headliner
                The biggest announcement was Roche stepping back into the sequencing platform market. In the years since...
                03-03-2025, 01:39 PM
              • seqadmin
                Investigating the Gut Microbiome Through Diet and Spatial Biology
                by seqadmin




                The human gut contains trillions of microorganisms that impact digestion, immune functions, and overall health1. Despite major breakthroughs, we’re only beginning to understand the full extent of the microbiome’s influence on health and disease. Advances in next-generation sequencing and spatial biology have opened new windows into this complex environment, yet many questions remain. This article highlights two recent studies exploring how diet influences microbial...
                02-24-2025, 06:31 AM

              ad_right_rmr

              Collapse

              News

              Collapse

              Topics Statistics Last Post
              Started by seqadmin, 03-20-2025, 05:03 AM
              0 responses
              26 views
              0 reactions
              Last Post seqadmin  
              Started by seqadmin, 03-19-2025, 07:27 AM
              0 responses
              33 views
              0 reactions
              Last Post seqadmin  
              Started by seqadmin, 03-18-2025, 12:50 PM
              0 responses
              25 views
              0 reactions
              Last Post seqadmin  
              Started by seqadmin, 03-03-2025, 01:15 PM
              0 responses
              190 views
              0 reactions
              Last Post seqadmin  
              Working...