Seqanswers Leaderboard Ad

Collapse

Announcement

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

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

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

    Comment


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

      Comment


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


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


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


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


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


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

                  Comment

                  Latest Articles

                  Collapse

                  • seqadmin
                    Strategies for Sequencing Challenging Samples
                    by seqadmin


                    Despite advancements in sequencing platforms and related sample preparation technologies, certain sample types continue to present significant challenges that can compromise sequencing results. Pedro Echave, Senior Manager of the Global Business Segment at Revvity, explained that the success of a sequencing experiment ultimately depends on the amount and integrity of the nucleic acid template (RNA or DNA) obtained from a sample. “The better the quality of the nucleic acid isolated...
                    03-22-2024, 06:39 AM
                  • seqadmin
                    Techniques and Challenges in Conservation Genomics
                    by seqadmin



                    The field of conservation genomics centers on applying genomics technologies in support of conservation efforts and the preservation of biodiversity. This article features interviews with two researchers who showcase their innovative work and highlight the current state and future of conservation genomics.

                    Avian Conservation
                    Matthew DeSaix, a recent doctoral graduate from Kristen Ruegg’s lab at The University of Colorado, shared that most of his research...
                    03-08-2024, 10:41 AM

                  ad_right_rmr

                  Collapse

                  News

                  Collapse

                  Topics Statistics Last Post
                  Started by seqadmin, Yesterday, 06:37 PM
                  0 responses
                  8 views
                  0 likes
                  Last Post seqadmin  
                  Started by seqadmin, Yesterday, 06:07 PM
                  0 responses
                  8 views
                  0 likes
                  Last Post seqadmin  
                  Started by seqadmin, 03-22-2024, 10:03 AM
                  0 responses
                  49 views
                  0 likes
                  Last Post seqadmin  
                  Started by seqadmin, 03-21-2024, 07:32 AM
                  0 responses
                  66 views
                  0 likes
                  Last Post seqadmin  
                  Working...
                  X