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
                    Advanced Tools Transforming the Field of Cytogenomics
                    by seqadmin


                    At the intersection of cytogenetics and genomics lies the exciting field of cytogenomics. It focuses on studying chromosomes at a molecular scale, involving techniques that analyze either the whole genome or particular DNA sequences to examine variations in structure and behavior at the chromosomal or subchromosomal level. By integrating cytogenetic techniques with genomic analysis, researchers can effectively investigate chromosomal abnormalities related to diseases, particularly...
                    09-26-2023, 06:26 AM
                  • seqadmin
                    How RNA-Seq is Transforming Cancer Studies
                    by seqadmin



                    Cancer research has been transformed through numerous molecular techniques, with RNA sequencing (RNA-seq) playing a crucial role in understanding the complexity of the disease. Maša Ivin, Ph.D., Scientific Writer at Lexogen, and Yvonne Goepel Ph.D., Product Manager at Lexogen, remarked that “The high-throughput nature of RNA-seq allows for rapid profiling and deep exploration of the transcriptome.” They emphasized its indispensable role in cancer research, aiding in biomarker...
                    09-07-2023, 11:15 PM

                  ad_right_rmr

                  Collapse

                  News

                  Collapse

                  Topics Statistics Last Post
                  Started by seqadmin, Yesterday, 09:38 AM
                  0 responses
                  9 views
                  0 likes
                  Last Post seqadmin  
                  Started by seqadmin, 09-27-2023, 06:57 AM
                  0 responses
                  11 views
                  0 likes
                  Last Post seqadmin  
                  Started by seqadmin, 09-26-2023, 07:53 AM
                  1 response
                  23 views
                  0 likes
                  Last Post seed_phrase_metal_storage  
                  Started by seqadmin, 09-25-2023, 07:42 AM
                  0 responses
                  17 views
                  0 likes
                  Last Post seqadmin  
                  Working...
                  X