SEQanswers

Go Back   SEQanswers > Bioinformatics > Bioinformatics



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it possible to convert a SNP.txt to a bed file or get a SNP.bed from samtools? Ling Bioinformatics 7 04-02-2015 06:17 AM
Updated How to convert .txt file to .bed .GFF or .BAR file format, forevermark4 Bioinformatics 2 06-30-2014 05:02 AM
How to convert .txt file to .bed or .gff, How can we use chip seq data in R software forevermark4 Bioinformatics 57 06-30-2014 05:01 AM
To convert BAM file to BED file Anjali Bioinformatics 10 04-28-2014 04:43 AM
How to convert a bam file to sam file badhikari Bioinformatics 2 04-01-2011 08:56 AM

Reply
 
Thread Tools
Old 01-23-2012, 12:58 PM   #1
alextree
Junior Member
 
Location: NJ

Join Date: Jan 2012
Posts: 3
Post 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!
alextree is offline   Reply With Quote
Old 01-23-2012, 01:04 PM   #2
tboothby
Member
 
Location: DC

Join Date: May 2011
Posts: 56
Default

Are you familiar with unix? Try a google search for the awk command. Very powerful tool.
tboothby is offline   Reply With Quote
Old 01-23-2012, 01:06 PM   #3
swbarnes2
Senior Member
 
Location: San Diego

Join Date: May 2008
Posts: 912
Default

Will awk work on a .bam file? Or do you have to conert the whole thing to .sam?
swbarnes2 is offline   Reply With Quote
Old 01-23-2012, 01:54 PM   #4
kmcarr
Senior Member
 
Location: USA, Midwest

Join Date: May 2008
Posts: 1,150
Default

Quote:
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 at 02:00 PM. Reason: Realized the question was about the Java API
kmcarr is offline   Reply With Quote
Old 01-23-2012, 02:28 PM   #5
gringer
David Eccles (gringer)
 
Location: Wellington, New Zealand

Join Date: May 2011
Posts: 821
Default

Quote:
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.
gringer is offline   Reply With Quote
Old 01-23-2012, 02:33 PM   #6
kmcarr
Senior Member
 
Location: USA, Midwest

Join Date: May 2008
Posts: 1,150
Default

Quote:
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.
kmcarr is offline   Reply With Quote
Old 01-24-2012, 07:15 AM   #7
alextree
Junior Member
 
Location: NJ

Join Date: Jan 2012
Posts: 3
Default

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 at 09:22 AM.
alextree is offline   Reply With Quote
Old 01-24-2012, 07:35 AM   #8
gringer
David Eccles (gringer)
 
Location: Wellington, New Zealand

Join Date: May 2011
Posts: 821
Default

Picard has quite good JavaDocs:

http://picard.sourceforge.net/javadoc

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.
gringer is offline   Reply With Quote
Old 01-24-2012, 09:20 AM   #9
alextree
Junior Member
 
Location: NJ

Join Date: Jan 2012
Posts: 3
Default

thx gringer!!! thats exactly what i was looking for!
alextree is offline   Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off




All times are GMT -8. The time now is 08:52 PM.


Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2018, vBulletin Solutions, Inc.
Single Sign On provided by vBSSO