Skip to content

Instantly share code, notes, and snippets.

@lindenb
Created March 11, 2019 16:48
Show Gist options
  • Select an option

  • Save lindenb/f85c92336ed3400beb565c222f9c2733 to your computer and use it in GitHub Desktop.

Select an option

Save lindenb/f85c92336ed3400beb565c222f9c2733 to your computer and use it in GitHub Desktop.
https://www.biostars.org/p/368754/ jvarkit java bed bam

usage:

$ ls input.bed
$ java -jar dist/samjdk.jar --body -f biostar368754.code input.bam
private IntervalTreeMap<Interval> treemap = null;
@Override
public Object apply(final SAMRecord record) {
/** first time, fill the interval map */
if(this.treemap==null) {
try {
/** first call , read the bed */
this.treemap = new IntervalTreeMap<>();
/** read all the lines */
IOUtil.slurpLines(new java.io.File("input.bed")).
stream().
filter(L->!L.isEmpty()). /* remove empty lines */
map(L->L.split("[\t]")). /* split tab */
map(T->new Interval(T[0],1+Integer.parseInt(T[1]),Integer.parseInt(T[2]))). /* convert to Interval */
forEach(I->treemap.put(I,I));/* fill the treemap */
} catch(java.io.IOException err) {
throw new RuntimeIOException(err);
}
}
if(!record.getReadUnmappedFlag()) {
/** the read interval */
final Interval readInterval = new Interval(record.getContig(),record.getStart(),record.getEnd());
if(treemap.containsOverlapping(readInterval) ) return false;
}
/* mate interval */
if(record.getReadPairedFlag() && !record.getMateUnmappedFlag()) {
final int mateEnd= SAMUtils.getMateCigar(record)!=null?
SAMUtils.getMateAlignmentEnd(record):
record.getMateAlignmentStart()
;
final Interval mateInterval = new Interval(record.getMateReferenceName(),record.getMateAlignmentStart(),mateEnd);
if(treemap.containsOverlapping(mateInterval) ) return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment