Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alextree
    Junior Member
    • Jan 2012
    • 3

    #1

    Are there any good ways to use SAMtools java API to convert .bam file into .txt file?

    Hi guys im new in SAMtools.

    I want to extract some info from the .bam file and put them into a new .txt file.

    e.g. a portion of the .bam file is like:

    read1 0 chr1 1 37 39M * 0 0 GAACTGGATACAGACATGGCCTTAAGGTTGGGAGGTAAT
    read9 0 chr1 20 37 19M1D5M * 0 0 GAACTGGATACAGACATGGCTTGA

    so what if I only want to extract certain fields as the following format:

    chr1 1 GAACTGGATACAGACATGGCCTTAAGGTTGGGAGGTAAT
    chr1 20 GAACTGGATACAGACATGGCTTGA

    (fields 3, 4, 10)

    are there any methods can do such thing?

    any suggestions? thx!
  • tboothby
    Member
    • May 2011
    • 56

    #2
    Are you familiar with unix? Try a google search for the awk command. Very powerful tool.

    Comment

    • swbarnes2
      Senior Member
      • May 2008
      • 910

      #3
      Will awk work on a .bam file? Or do you have to conert the whole thing to .sam?

      Comment

      • kmcarr
        Senior Member
        • May 2008
        • 1181

        #4
        Originally posted by swbarnes2 View Post
        Will awk work on a .bam file? Or do you have to conert the whole thing to .sam?
        You are correct that awk will only work on text so you must convert from bam to sam, but doing things "the unix way" you connect the commands up with pipes so that you just stream the output of one command directly to the input of the next, thus you do not have to save the intermediate form of the data. For example:

        Code:
        # samtools view myFile.bam | awk [arguments to process text as desired]
        Since the OP's text processing needs are very simple I would suggest using cut instead of awk. cut does exactly what [s]he want (selects fields in tabular data) and is much easier to learn and use than awk.


        Code:
        # samtools view myFile.bam | cut -f 3,4,10 > myFile.txt
        O.K. I looked at the title of alextree's post so I guess my answer isn't useful in that context but I'll leave anyway incase it may be useful for someone wanting to do the same thing on the command line.
        Last edited by kmcarr; 01-23-2012, 03:00 PM. Reason: Realized the question was about the Java API

        Comment

        • gringer
          David Eccles (gringer)
          • May 2011
          • 845

          #5
          I looked at the title of alextree's post so I guess my answer isn't useful in that context
          I think using the SAMTools Java API to convert a BAM file to text is a bit like using a wrecking ball to break up a wood veneer. A cut using a little pipe is much more lightweight and easier to manage.

          Comment

          • kmcarr
            Senior Member
            • May 2008
            • 1181

            #6
            Originally posted by gringer View Post
            I think using the SAMTools Java API to convert a BAM file to text is a bit like using a wrecking ball to break up a wood veneer. A cut using a little pipe is much more lightweight and easier to manage.
            Definitely agree with that way of thinking. But to be fair I can't tell from alextree's original post if that was the full extent of the exercise.

            Comment

            • alextree
              Junior Member
              • Jan 2012
              • 3

              #7
              Thx tboothby, swbarnes2, kmcarr and gringer for the advises! I'd rather using command line instead of java API, Cut command can certainly do the work, but I'm writing a JAVA program and I need to convert a certain .bam or .sam file into desired format for further use. So I'm just wondering is there any method in picard java API can do the same work as Cut command?
              Last edited by alextree; 01-24-2012, 10:22 AM.

              Comment

              • gringer
                David Eccles (gringer)
                • May 2011
                • 845

                #8
                Picard has quite good JavaDocs:



                The procedure is fairly simple for doing what you've asked, and appears to follow the usual conventions.

                Having a look through a few things, there's a SAMFileReader class for reading SAM/BAM files. This class has an iterator method if you want all records, and a query method if you want a particular region / contig. Both the iterator and query methods return a SAMRecordIterator, which is an Iterator over SAMRecords. Once you've got that deep into the class heirarchy, you can then access the things you desire, like record.getReferenceName(), record.getAlignmentStart(), and record.getReadBases() (which I think match the fields you asked for).

                Then, you just write that out to your text file via PrintWriter, or whatever your favourite file writing method is. Or, more likely, you don't actually want to write to a text file (because that would be much easier done using cut et al.), and want to process these fields in some other way using other Java code.

                Comment

                • alextree
                  Junior Member
                  • Jan 2012
                  • 3

                  #9
                  thx gringer!!! thats exactly what i was looking for!

                  Comment

                  Latest Articles

                  Collapse

                  • SEQadmin2
                    Beyond CRISPR/Cas9: Understand, Choose, and Use the Right Genome Editing Tool
                    by SEQadmin2



                    CRISPR/Cas9 sparked the gene editing revolution for both research and therapeutics.1 But this system still showed severe issues that limited its applications. The most prominent were the heavy reliance on PAM sequences, delivery limitations, double-stranded breaks that prompt unintended edits and cell death, and editing inefficiency (both in targeting and in knock-in reliability).

                    Despite this, “CRISPR helped turn genome editing from a specialized technique into
                    ...
                    07-31-2026, 11:01 AM
                  • 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

                  ad_right_rmr

                  Collapse

                  News

                  Collapse

                  Topics Statistics Last Post
                  Started by SEQadmin2, 08-06-2026, 07:41 AM
                  0 responses
                  13 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 08-03-2026, 10:13 AM
                  0 responses
                  30 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 07-31-2026, 02:55 AM
                  0 responses
                  40 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 07-24-2026, 12:17 PM
                  0 responses
                  26 views
                  0 reactions
                  Last Post SEQadmin2  
                  Working...