Unconfigured Ad

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

    #1

    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

              • 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
              19 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, Yesterday, 10:48 AM
              0 responses
              18 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 10-15-2026, 10:52 AM
              0 responses
              36 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 10-14-2026, 05:39 AM
              0 responses
              42 views
              0 reactions
              Last Post SEQadmin2  
              Working...