Ah thank you so much! getInferredInsertSize is what I was looking for, much appreciated!
Might you know how to start iterating from the start of a particular chromosome?
Hello Biostars Community,
I am a newbie to the bioinformatics world and need some help with a project I am working on!
I need to use htsjdk to extract every tlen value from a BAM file and have a few questions.
Sorry for all of the questions, I have been googling a lot and haven't found answers!
Any help is appreciated, thanks in advance!
This may get you in the right direction:
// Open bam file
SamReader samReader= SamReaderFactory.
makeDefault().
validationStringency(ValidationStringency.SILENT).
open(new File("aln.bam"));
// Start iterating from start to end of chr7.
SAMRecordIterator iter = samReader.query("chr7", 0, 0, false);
while(iter.hasNext()){
// Iterate thorough each record and extract fragment size
SAMRecord rec= iter.next();
int tlen= rec.getInferredInsertSize();
System.out.println(tlen);
}
Get list of chromosomes/contig names from SamReader object:
List<SAMSequenceRecord> refSeqs= samReader.getFileHeader().getSequenceDictionary().getSequences();
for(SAMSequenceRecord ref : refSeqs){
System.out.println(ref.getSequenceName());
}
Ah thank you so much! getInferredInsertSize is what I was looking for, much appreciated!
Might you know how to start iterating from the start of a particular chromosome?
Hi- See edit. Have a look at the htsjdk docs for the meaning of the various method. Also make sure ValidationStringency.SILENT is ok for you.
Much appreciated. One more question, is there a function that returns the list of chromosomes/contigs so I can dynamically build that list?
Hi again - See edit, I haven't tested it but it should be about right.
The contig names do indeed work thank you. However, I am running into an issue with reading from a specific one using the query method.
I get this error: htsjdk.samtools.SAMException: No index is available for this BAM file. How do I provide the index file?
l'm using SamReader query method, but it doesn't work. Nothing print. l was wondering if you have this problem. My code:
SamReader reader=SamReaderFactory.makeDefault().validationStringency(ValidationStringency.SILENT).open(new File(in_bam_file));
SAMRecordIterator recordQuery=reader.query("chr3",179198825, 179199177,false);
while(recordQuery.hasNext()){
SAMRecord res=recordQuery.next();
System.out.println("query size is:\t"+res.getInferredInsertSize());
}
hi, l use SamReader query method, it doesn't work, l don't know why. My htsjdk version is 2.8.1.
SamReader reader=SamReaderFactory.makeDefault().validationStringency(ValidationStringency.SILENT).open(new File(in_bam_file));
SAMRecordIterator recordQuery=reader.query("chr3",179198825, 179199177,false);
while(recordQuery.hasNext()){
SAMRecord res=recordQuery.next();
System.out.println("query size is:\t"+res.getInferredInsertSize());
}
Are you sure the interval chr3:179198825-179199177 contains some reads? Make sure also that "chr3" is correct, maybe your chromosome is named "3". What is the output of samtools view in.bam chr3:179198825-179199177?
l am pretty sure, 'chr3' is right and the interval is also right. 'samtools view in.bam chr3:179198825-179199177' could print the results.
Log in to answer this question.