Unconfigured Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • shuoguo
    Member
    • Sep 2012
    • 23

    SAM to BAM --- samtools fail to read error

    I am trying to transfer a SAM file to BAM file using samtools but get error as shown in the attached figure.

    I have the SAM files ready in the right format, this SAM file is prepared by join another sam file with one bed file together. The columns are correct.

    I used -S to indicate input is sam, and -b to indicate output is bam

    i use -t to indicate that the sam file contains tab delimiter, and no header.

    what could be the issue?

    Thanks a lot
    Attached Files
  • shuoguo
    Member
    • Sep 2012
    • 23

    #2
    $ /usr/local/samtools-0.1.18/samtools view -bS all-hg18.bam -o test.bam
    [samopen] no @SQ lines in the header.
    [sam_read1] missing header? Abort!

    $ /usr/local/samtools-0.1.18/samtools view -tbS all-hg18.bam -o test.bam
    [main_samview] fail to open "all-hg18.bam" for reading.

    Comment

    • shuoguo
      Member
      • Sep 2012
      • 23

      #3
      find online manual said:
      If your SAM file has header @SQ lines, you may get BAM by ...
      If not, you need to have your reference file ref.fa and then do this:
      looks like i need a ref.fa file, not sure what is that? how does it looks like and how to get it?

      thanks

      Shuoguo

      Comment

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

        #4
        That was shorthand for your reference genome in FASTA format.

        Comment

        • swbarnes2
          Senior Member
          • May 2008
          • 910

          #5
          Try

          samtools view -bSh mysam.sam > mybam.bam
          I don't think you need the -t, and I'm not sure that the software likes you putting the -o option after the input file name.

          -h will make sure the header goes on there.

          Comment

          • shuoguo
            Member
            • Sep 2012
            • 23

            #6
            It seems that no matter how I run it it will just spit out errors

            $ /usr/local/samtools-0.1.18/samtools view -bS all-hg18-lifted.sam -o test.bam
            [samopen] no @SQ lines in the header.
            [sam_read1] missing header? Abort!

            ]$ /usr/local/samtools-0.1.18/samtools view -bS all-hg18-lifted.sam test.bam
            [samopen] no @SQ lines in the header.
            [main_samview] random alignment retrieval only works for indexed BAM files.

            Comment

            • shuoguo
              Member
              • Sep 2012
              • 23

              #7
              in my case i do not have teh original fasta files

              Originally posted by maubp View Post
              That was shorthand for your reference genome in FASTA format.

              Comment

              • swbarnes2
                Senior Member
                • May 2008
                • 910

                #8
                Originally posted by shuoguo View Post
                It seems that no matter how I run it it will just spit out errors
                Use the caret to write to the .bam

                /usr/local/samtools-0.1.18/samtools view -bS all-hg18-lifted.sam > test.bam

                Comment

                • shuoguo
                  Member
                  • Sep 2012
                  • 23

                  #9
                  tried that with no luck. thanks for the reply.

                  Originally posted by swbarnes2 View Post
                  Use the caret to write to the .bam

                  Comment

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

                    #10
                    Which version of samtools do you have?

                    Comment

                    • shuoguo
                      Member
                      • Sep 2012
                      • 23

                      #11
                      samtools-0.1.18

                      Comment

                      • dpryan
                        Devon Ryan
                        • Jul 2011
                        • 3478

                        #12
                        Without knowing the chromosome names and sizes, you won't be able to make a BAM file (there'd be no way for it to know how to create the header). You can probably half-ass it by just getting a sorted list of chromosomes from your SAM file:
                        Code:
                        cat all-hg18-lifted.sam | cut -f 3 | sort | uniq > ref.fa.fai
                        For each line in ref.fa.fai, you then need to add that chromosome's length (separated by a tab from the chromosome name, so "chr1 500000000"). Obviously you don't know that, but you could just use a sufficiently large number. That would at least allow things to be converted to a BAM file. I don't know if that would screw things up with any downstream applications, but I can't currently think of a situation where it would.

                        Comment

                        • masterpiece
                          Member
                          • Mar 2009
                          • 40

                          #13
                          As what dpryan and other said, its the header issue. May I know how you generate the sam file at the first place?

                          Comment

                          • shuoguo
                            Member
                            • Sep 2012
                            • 23

                            #14
                            Originally posted by masterpiece View Post
                            As what dpryan and other said, its the header issue. May I know how you generate the sam file at the first place?
                            Step 1: i have the original bam and bam.bai files downloaded from web. These are aligned with hg18.

                            Step 2: i transfer the same bam file to a bed file and a sam file.

                            Step 3: i liftover the bed file with the hg19 chain file (so the position is updated)

                            Step 4: I replace the position (3rd column) in the sam file with the liftover file.

                            Step 5: I want to transfer this sam file back to bam file, getting the error

                            Thanks for the help!!

                            Comment

                            • dpryan
                              Devon Ryan
                              • Jul 2011
                              • 3478

                              #15
                              Ah, then since this is just hg18, you have a two options:

                              1) Do what I suggested above, substituting the length of the various chromosomes in hg18 for the made up values I originally suggested. You would have to look up these values.

                              2) Download the hg18 fasta file and run "samtools faidx" on it. You can then use the "-t" option that you tried before with the resulting .fai file. This is effectively the same as option 1, but requires less googling.

                              BTW, I hope you replaced all of the coordinate information rather than just the chromosome (position 3). Otherwise whatever you do downstream will be messed up.

                              Comment

                              Latest Articles

                              Collapse

                              • 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
                              • SEQadmin2
                                Cancer Drug Resistance: The Lingering Barrier to Rising Survival
                                by SEQadmin2



                                Cancer survival rates have significantly increased in the last few decades in the United States, reaching a combined 70% 5-year survival rate by 2021. Behind this number, there are years of research to find new therapies, drug targets, and early detection methods. But there is one core challenge that keeps slowing down these advances, and it’s about drug resistance.

                                There is no single reason why many patients don’t respond to treatment as expected. Cancer is...
                                07-08-2026, 05:17 AM

                              ad_right_rmr

                              Collapse

                              News

                              Collapse

                              Topics Statistics Last Post
                              Started by SEQadmin2, 07-24-2026, 12:17 PM
                              0 responses
                              31 views
                              0 reactions
                              Last Post SEQadmin2  
                              Started by SEQadmin2, 07-23-2026, 11:41 AM
                              0 responses
                              23 views
                              0 reactions
                              Last Post SEQadmin2  
                              Started by SEQadmin2, 07-20-2026, 11:10 AM
                              0 responses
                              215 views
                              0 reactions
                              Last Post SEQadmin2  
                              Started by SEQadmin2, 07-13-2026, 10:26 AM
                              0 responses
                              79 views
                              0 reactions
                              Last Post SEQadmin2  
                              Working...