Unconfigured Ad

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

    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
                    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, 06-17-2026, 06:09 AM
                  0 responses
                  25 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 06-09-2026, 11:58 AM
                  0 responses
                  42 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 06-05-2026, 10:09 AM
                  0 responses
                  48 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 06-04-2026, 08:59 AM
                  0 responses
                  49 views
                  0 reactions
                  Last Post SEQadmin2  
                  Working...