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.
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.
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
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 );
Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
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.
SamFileReader
Don't use
SAMFileReader
, it's flagged@Deprecated
see https://github.com/samtools/htsjdk/blob/master/src/java/htsjdk/samtools/SAMFileReader.java#L53Your link points to the old picard API. picard now uses https://github.com/samtools/htsjdk