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
                                Nine Things a Sample Prep Scientist Thinks About Before Sequencing
                                by SEQadmin2


                                I’m not a sequencing expert. I’m a purification scientist who uses NGS to evaluate workflows my group develops. With this perspective, we think about the sample first and the NGS workflow second. The sequencer is an exceptionally honest reporter, but it can only report on what you give it, so whether you get clean, interpretable data from an NGS workflow is largely determined before you begin.

                                Here are nine questions we think about, in roughly the order they matter, before...
                                06-18-2026, 07:11 AM
                              • SEQadmin2
                                From Collection to Sequencing: Why Sample Preparation and Preservation Define Sequencing Data
                                by SEQadmin2


                                Data variability is still an issue in sequencing technologies despite the advances in reproducibility and accuracy of these platforms. But the problem does not originate in the sequencing itself, but in the previous steps, before the sample reaches the sequencer.


                                The first step is collection, followed by preservation and sample preparation for analysis. Most scientists overlook those steps, but not being careful might just be skewing the experiment’s results.
                                ...
                                06-02-2026, 10:05 AM

                              ad_right_rmr

                              Collapse

                              News

                              Collapse

                              Topics Statistics Last Post
                              Started by SEQadmin2, Today, 11:10 AM
                              0 responses
                              5 views
                              0 reactions
                              Last Post SEQadmin2  
                              Started by SEQadmin2, 06-17-2026, 06:09 AM
                              0 responses
                              41 views
                              0 reactions
                              Last Post SEQadmin2  
                              Started by SEQadmin2, 06-09-2026, 11:58 AM
                              0 responses
                              102 views
                              0 reactions
                              Last Post SEQadmin2  
                              Started by SEQadmin2, 06-05-2026, 10:09 AM
                              0 responses
                              123 views
                              0 reactions
                              Last Post SEQadmin2  
                              Working...