Seqanswers Leaderboard Ad

Collapse

Announcement

Collapse
No announcement yet.
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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!

  • #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


    • #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


      • #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


        • #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


          • #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


            • #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
                Essential Discoveries and Tools in Epitranscriptomics
                by seqadmin


                The field of epigenetics has traditionally concentrated more on DNA and how changes like methylation and phosphorylation of histones impact gene expression and regulation. However, our increased understanding of RNA modifications and their importance in cellular processes has led to a rise in epitranscriptomics research. “Epitranscriptomics brings together the concepts of epigenetics and gene expression,” explained Adrien Leger, PhD, Principal Research Scientist on Modified Bases...
                Yesterday, 07:01 AM
              • seqadmin
                Current Approaches to Protein Sequencing
                by seqadmin


                Proteins are often described as the workhorses of the cell, and identifying their sequences is key to understanding their role in biological processes and disease. Currently, the most common technique used to determine protein sequences is mass spectrometry. While still a valuable tool, mass spectrometry faces several limitations and requires a highly experienced scientist familiar with the equipment to operate it. Additionally, other proteomic methods, like affinity assays, are constrained...
                04-04-2024, 04:25 PM

              ad_right_rmr

              Collapse

              News

              Collapse

              Topics Statistics Last Post
              Started by seqadmin, 04-11-2024, 12:08 PM
              0 responses
              39 views
              0 likes
              Last Post seqadmin  
              Started by seqadmin, 04-10-2024, 10:19 PM
              0 responses
              41 views
              0 likes
              Last Post seqadmin  
              Started by seqadmin, 04-10-2024, 09:21 AM
              0 responses
              35 views
              0 likes
              Last Post seqadmin  
              Started by seqadmin, 04-04-2024, 09:00 AM
              0 responses
              55 views
              0 likes
              Last Post seqadmin  
              Working...
              X