How to get SAMRecord from bam. file with specified readGroupId, referenceId, sequence end and start?
2
0
Entering edit mode
9.7 years ago
timofeevbog ▴ 30

I've got .bam and .bai files and I need to get SAMRecord from bam with specified readGroupId, referenceId, end and start and with using .bai index. How can I make it using Picard java-API? Thanks.

index java picard • 3.1k views
ADD COMMENT
0
Entering edit mode
9.7 years ago

To extract sequences for particular read group id you can use Picard's SamToFastq and FilterSamReads. But you can't really specify specific coordinates to extract your reads. If you pretty adamant about using Picard java-API, you may have to use a combination of samtools view function and Picard's FilterSamReads. Otherwise you can do sth like:

samtools view chrA:X-Y Input.bam | grep "readGroupId" >  new.sam
ADD COMMENT
0
Entering edit mode
9.7 years ago

There is no more picard-api. The java api used by picard tools is htsjdk.

Create a SAMReaderFactory:

SamReaderFactory srf=SamReaderFactory.make();
srf.validationStringency(ValidationStringency.LENIENT);

Open the SAM:

SamReader samReader = srf.open(bamFile);

Loop:

SAMRecordIterator iter= srf.iterator();
while(iter.hasNext())
{
    SAMRecord rec= iter.next();

Get the read group, get the sampleName:

SAMReadGroupRecord sgr=rec.getReadGroup();
String sampleName = (sgr!=null ?sgr.getSample() : null );
ADD COMMENT
0
Entering edit mode

Thanks for your reply, but does this construction uses index in .bai-file? I thought there should be some kind of reference to this file, something like index in SAMFileReader.

public SAMFileReader(final File file, final File indexFile) {
        this(file, indexFile, false);
    }

SamFileReader

ADD REPLY
0
Entering edit mode

Don't use SAMFileReader, it's flagged @Deprecated see https://github.com/samtools/htsjdk/blob/master/src/java/htsjdk/samtools/SAMFileReader.java#L53

Your link points to the old picard API. picard now uses https://github.com/samtools/htsjdk

ADD REPLY

Login before adding your answer.

Traffic: 1626 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6