Search in sources :

Example 1 with AggregateFilter

use of htsjdk.samtools.filter.AggregateFilter in project ASCIIGenome by dariober.

the class SamLocusIterator method iterator.

public Iterator<LocusInfo> iterator() {
    if (samIterator != null) {
        throw new IllegalStateException("Cannot call iterator() more than once on SamLocusIterator");
    }
    CloseableIterator<SAMRecord> tempIterator;
    if (intervals != null) {
        tempIterator = new SamRecordIntervalIteratorFactory().makeSamRecordIntervalIterator(samReader, intervals, useIndex);
    } else {
        tempIterator = samReader.iterator();
    }
    if (samFilters != null) {
        tempIterator = new FilteringIterator(tempIterator, new AggregateFilter(samFilters));
    }
    samIterator = new PeekableIterator<SAMRecord>(tempIterator);
    return this;
}
Also used : AggregateFilter(htsjdk.samtools.filter.AggregateFilter) SAMRecord(htsjdk.samtools.SAMRecord) FilteringIterator(htsjdk.samtools.filter.FilteringIterator) SamRecordIntervalIteratorFactory(htsjdk.samtools.util.SamRecordIntervalIteratorFactory)

Example 2 with AggregateFilter

use of htsjdk.samtools.filter.AggregateFilter in project ASCIIGenome by dariober.

the class Utils method countReadsInWindow.

/**
 * Count reads in interval using the given filters.
 * @param bam
 * @param gc
 * @param filters List of filters to apply
 * @return
 * @throws MalformedURLException
 */
public static long countReadsInWindow(String bam, GenomicCoords gc, List<SamRecordFilter> filters) throws MalformedURLException {
    /*  ------------------------------------------------------ */
    /* This chunk prepares SamReader from local bam or URL bam */
    UrlValidator urlValidator = new UrlValidator();
    SamReaderFactory srf = SamReaderFactory.make();
    srf.validationStringency(ValidationStringency.SILENT);
    SamReader samReader;
    if (urlValidator.isValid(bam)) {
        samReader = srf.open(SamInputResource.of(new URL(bam)).index(new URL(bam + ".bai")));
    } else {
        samReader = srf.open(new File(bam));
    }
    /*  ------------------------------------------------------ */
    long cnt = 0;
    Iterator<SAMRecord> sam = samReader.query(gc.getChrom(), gc.getFrom(), gc.getTo(), false);
    AggregateFilter aggregateFilter = new AggregateFilter(filters);
    while (sam.hasNext()) {
        SAMRecord rec = sam.next();
        if (!aggregateFilter.filterOut(rec)) {
            cnt++;
        }
    }
    try {
        samReader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return cnt;
}
Also used : SamReader(htsjdk.samtools.SamReader) AggregateFilter(htsjdk.samtools.filter.AggregateFilter) SamReaderFactory(htsjdk.samtools.SamReaderFactory) SAMRecord(htsjdk.samtools.SAMRecord) UrlValidator(org.apache.commons.validator.routines.UrlValidator) IOException(java.io.IOException) File(java.io.File) IndexedFastaSequenceFile(htsjdk.samtools.reference.IndexedFastaSequenceFile) URL(java.net.URL)

Example 3 with AggregateFilter

use of htsjdk.samtools.filter.AggregateFilter in project ASCIIGenome by dariober.

the class Track method filterReads.

public List<Boolean> filterReads(SamReader samReader, String chrom, int from, int to) throws IOException {
    Iterator<SAMRecord> filterSam = samReader.query(chrom, from, to, false);
    AggregateFilter aggregateFilter = new AggregateFilter(this.getFeatureFilter().getSamRecordFilter());
    // This array will contain true/false to indicate whether a record passes the
    // sam filters AND the awk filter (if given).
    // boolean[] results= new boolean[(int) this.nRecsInWindow];
    List<Boolean> results = new ArrayList<Boolean>();
    List<String> awkDataInput = new ArrayList<String>();
    while (filterSam.hasNext()) {
        // Record whether a read passes the sam filters. If necessary, we also
        // store the raw reads for awk.
        SAMRecord rec = filterSam.next();
        boolean passed;
        if (!rec.getReadUnmappedFlag() && !aggregateFilter.filterOut(rec) && rec.getAlignmentEnd() >= rec.getAlignmentStart()) {
            passed = true;
        } else {
            passed = false;
        }
        // Filter for variant reads: Do it only if there is an intersection between variant interval and current genomic window
        if (this.getFeatureFilter().getVariantChrom().equals(Filter.DEFAULT_VARIANT_CHROM.getValue())) {
        // Variant read filter is not set. Nothing to do.
        } else if (passed) {
            // Memo: Test filter(s) only if a read is passed==true.
            passed = this.isSNVRead(rec, this.getFeatureFilter().isVariantOnly());
        }
        String raw = null;
        if (passed && (!this.getFeatureFilter().getShowRegex().equals(Filter.DEFAULT_SHOW_REGEX.getValue()) || !this.getFeatureFilter().getHideRegex().equals(Filter.DEFAULT_HIDE_REGEX.getValue()))) {
            // grep
            raw = rec.getSAMString().trim();
            boolean showIt = true;
            if (!this.getFeatureFilter().getShowRegex().equals(Filter.DEFAULT_SHOW_REGEX.getValue())) {
                showIt = this.getFeatureFilter().getShowRegex().matcher(raw).find();
            }
            boolean hideIt = false;
            if (!this.getFeatureFilter().getHideRegex().equals(Filter.DEFAULT_HIDE_REGEX.getValue())) {
                hideIt = this.getFeatureFilter().getHideRegex().matcher(raw).find();
            }
            if (!showIt || hideIt) {
                passed = false;
            }
        }
        results.add(passed);
        if (passed && this.getAwk() != null && !this.getAwk().isEmpty()) {
            // We pass to awk only records that have been kept so far.
            if (raw == null) {
                raw = rec.getSAMString().trim();
            }
            awkDataInput.add(raw);
        }
    }
    // Apply the awk filter, if given
    if (this.getAwk() != null && !this.getAwk().equals(Filter.DEFAULT_AWK.getValue())) {
        String[] rawLines = new String[awkDataInput.size()];
        rawLines = awkDataInput.toArray(rawLines);
        boolean[] awkResults = Utils.passAwkFilter(rawLines, this.getAwk());
        // Compare the results array with awk filtered. Flip as appropriate the results array
        int awkIdx = 0;
        int i = 0;
        for (boolean isPassed : results) {
            if (isPassed) {
                if (!awkResults[awkIdx]) {
                    results.set(i, false);
                }
                awkIdx++;
            }
            i++;
        }
    }
    return results;
}
Also used : AggregateFilter(htsjdk.samtools.filter.AggregateFilter) SAMRecord(htsjdk.samtools.SAMRecord) ArrayList(java.util.ArrayList)

Aggregations

SAMRecord (htsjdk.samtools.SAMRecord)3 AggregateFilter (htsjdk.samtools.filter.AggregateFilter)3 SamReader (htsjdk.samtools.SamReader)1 SamReaderFactory (htsjdk.samtools.SamReaderFactory)1 FilteringIterator (htsjdk.samtools.filter.FilteringIterator)1 IndexedFastaSequenceFile (htsjdk.samtools.reference.IndexedFastaSequenceFile)1 SamRecordIntervalIteratorFactory (htsjdk.samtools.util.SamRecordIntervalIteratorFactory)1 File (java.io.File)1 IOException (java.io.IOException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 UrlValidator (org.apache.commons.validator.routines.UrlValidator)1