/** * Given an Interval and a band multiple, this method will return a list of Intervals such that all of the intervals * do not straddle integer multiples of that band. * * ex: if the interval is (7200-9300) and the bandMultiple is 1000, the interval will be split into: * (7200-7999, 8000-8999, 9000-9300) * * @param interval an Interval * @param bandMultiple integer value (> 0) to break up intervals in the list at integer multiples of * @return list of intervals that are broken up */ private static List<Interval> breakIntervalAtBandMultiples(final Interval interval, final int bandMultiple) { final List<Interval> brokenUpIntervals = new ArrayList<>(); int startPos = interval.getStart(); final int startOfIntervalIndex = startPos / bandMultiple; int startIndex = startOfIntervalIndex; final int endIndex = interval.getEnd() / bandMultiple; while (startIndex <= endIndex) { int endPos = (startIndex + 1) * bandMultiple - 1; if (endPos > interval.getEnd()) { endPos = interval.getEnd(); } // add start/end to list of broken up intervals to return (and uniquely name it). brokenUpIntervals.add(new Interval(interval.getContig(), startPos, endPos, interval.isNegativeStrand(), interval.getName() + "." + (startIndex - startOfIntervalIndex + 1))); startIndex++; startPos = startIndex * bandMultiple; } return brokenUpIntervals; }
public String toString() { if (toInterval == null) { // Matched a chain, but entirely within a gap. return fromInterval.toString() + " (len " + fromInterval.length() + ")=>null using chain " + chainId; } final String strand = toInterval.isNegativeStrand()? "-": "+"; return fromInterval.toString() + " (len " + fromInterval.length() + ")=>" + toInterval + "(" + strand + ") using chain " + chainId + " ; pct matched " + percentLiftedOver; } }
public int compare(final Interval lhs, final Interval rhs) { final int lhsIndex = this.header.getSequenceIndex(lhs.getSequence()); final int rhsIndex = this.header.getSequenceIndex(rhs.getSequence()); int retval = lhsIndex - rhsIndex; if (retval == 0) retval = lhs.getStart() - rhs.getStart(); if (retval == 0) retval = lhs.getEnd() - rhs.getEnd(); if (retval == 0) { if (lhs.isPositiveStrand() && rhs.isNegativeStrand()) retval = -1; else if (lhs.isNegativeStrand() && rhs.isPositiveStrand()) retval = 1; } if (retval == 0) { if (lhs.getName() == null) { if (rhs.getName() == null) return 0; else return -1; } else if (rhs.getName() == null) { return 1; } else { return lhs.getName().compareTo(rhs.getName()); } } return retval; } }
/** Merges a sorted collection of intervals and optionally concatenates unique names or takes the first name. */ static Interval merge(final SortedSet<Interval> intervals, final boolean concatenateNames) { final String chrom = intervals.first().getSequence(); int start = intervals.first().getStart(); int end = intervals.last().getEnd(); final boolean neg = intervals.first().isNegativeStrand(); final LinkedHashSet<String> names = new LinkedHashSet<String>(); final String name; for (final Interval i : intervals) { if (i.getName() != null) names.add(i.getName()); start = Math.min(start, i.getStart()); end = Math.max(end, i.getEnd()); } if (concatenateNames) { name = StringUtil.join("|", names); } else { name = names.iterator().next(); } return new Interval(chrom, start, end, neg, name); }
private Set<Interval> parseIntervalLists(final Set<String> intervalLists){ // Parse the PARs final Set<Interval> intervals = new HashSet<>(intervalLists.size()); for (final String par : intervalLists) { final String[] splits1 = par.split(":"); final String[] splits2 = splits1[1].split("-"); intervals.add(new Interval(splits1[0], Integer.parseInt(splits2[0]), Integer.parseInt(splits2[1]))); } return intervals; } /**
protected static VariantContextBuilder liftSimpleVariantContext(VariantContext source, Interval target) { if (target == null || source.getReference().length() != target.length()) { return null; } // Build the new variant context. Note: this will copy genotypes, annotations and filters final VariantContextBuilder builder = new VariantContextBuilder(source); builder.chr(target.getContig()); builder.start(target.getStart()); builder.stop(target.getEnd()); return builder; }
public int getIntersectionLength(final Interval other) { if (this.intersects(other)) { return (int) CoordMath.getOverlap(this.getStart(), this.getEnd(), other.getStart(), other.getEnd()); } return 0; }
@Test(dataProvider = "indelFlipData") public void testFlipIndel(final VariantContext source, final ReferenceSequence reference, final VariantContext result) { final LiftOver liftOver = new LiftOver(CHAIN_FILE); final Interval originalLocus = new Interval(source.getContig(), source.getStart(), source.getEnd()); final Interval target = liftOver.liftOver(originalLocus); if (target != null && !target.isNegativeStrand()) { throw new RuntimeException("not reversed"); } final VariantContext flipped = LiftoverUtils.liftVariant(source, target, reference, false, false); VcfTestUtils.assertEquals(flipped, result); }
public T remove(final Interval key) { final IntervalTree<T> tree = mSequenceMap.get(key.getSequence()); if (tree == null) { return null; } return tree.remove(key.getStart(), key.getEnd()); }
public List<PartialLiftover> diagnosticLiftover(final Interval interval) { final List<PartialLiftover> ret = new ArrayList<PartialLiftover>(); if (interval.length() == 0) { throw new IllegalArgumentException("Zero-length interval cannot be lifted over. Interval: " + interval.getName()); } for (final Chain chain : chains.getOverlaps(interval)) { Interval intersectingChain = interval.intersect(chain.interval); final TargetIntersection targetIntersection = targetIntersection(chain, intersectingChain); if (targetIntersection == null) { ret.add(new PartialLiftover(intersectingChain, chain.id)); } else { Interval toInterval = createToInterval(interval.getName(), interval.isNegativeStrand(), targetIntersection); float percentLiftedOver = targetIntersection.intersectionLength/(float)interval.length(); ret.add(new PartialLiftover(intersectingChain, toInterval, targetIntersection.chain.id, percentLiftedOver)); } } return ret; }
private static IntervalList composeIntervalList(final IntervalList source, final String chromosome, final int... segmentsByPair) { final IntervalList intervals = new IntervalList(source.getHeader()); for (int i = 0; i < segmentsByPair.length; i += 2) { final Interval parentInterval = lookupIntervalContainingLocus(source, chromosome, segmentsByPair[i]); intervals.add(new Interval(chromosome, segmentsByPair[i], segmentsByPair[i + 1], parentInterval.isNegativeStrand(), parentInterval.getName())); } return intervals; }
/** Returns a new Interval that represents the intersection between the two intervals. */ public Interval intersect(final Interval that) { if (!intersects(that)) throw new IllegalArgumentException(that + " does not intersect " + this); return new Interval(this.sequence, Math.max(this.start, that.start), Math.min(this.end, that.end), this.negativeStrand, this.name + " intersection " + that.name); }
if (interval.length() == 0) { throw new IllegalArgumentException("Zero-length interval cannot be lifted over. Interval: " + interval.getName()); double minMatchSize = liftOverMinMatch * interval.length(); hasOverlapBelowThreshold = true; if (logFailedIntervals) { LOG.info("Interval " + interval.getName() + " failed to match chain " + chain.id + " because intersection length " + candidateIntersection.intersectionLength + " < minMatchSize " + minMatchSize + " (" + (candidateIntersection.intersectionLength/(float)interval.length()) + " < " + liftOverMinMatch + ")"); return createToInterval(interval.getName(), interval.isNegativeStrand(), targetIntersection);
@Test public void testUnionSamePosition() { final IntervalList iList= new IntervalList(fileHeader); final List<Interval> intervals = Arrays.asList( new Interval("1", 2, 100, true, "test1"), new Interval("1", 2, 100, true, "test2") ); iList.addall(intervals); final List<Interval> uniqued = iList.uniqued().getIntervals(); Assert.assertEquals(uniqued.size(),1); Assert.assertEquals(uniqued.get(0).getName(),"test1|test2"); }
@Test public void testCoverageGetTotalOverflow() { final Interval interval = new Interval("chr1", 1, 2); final TargetMetricsCollector.Coverage coverage = new TargetMetricsCollector.Coverage(interval, 0); for (int offset = 0; offset <= interval.length(); offset++) { coverage.addBase(offset, Integer.MAX_VALUE - 1); } Assert.assertEquals((long)coverage.getTotal(), 2 * (long)(Integer.MAX_VALUE - 1)); } }
public List<PartialLiftover> diagnosticLiftover(final Interval interval) { final List<PartialLiftover> ret = new ArrayList<PartialLiftover>(); if (interval.length() == 0) { throw new IllegalArgumentException("Zero-length interval cannot be lifted over. Interval: " + interval.getName()); } for (final Chain chain : chains.getOverlaps(interval)) { Interval intersectingChain = interval.intersect(chain.interval); final TargetIntersection targetIntersection = targetIntersection(chain, intersectingChain); if (targetIntersection == null) { ret.add(new PartialLiftover(intersectingChain, chain.id)); } else { Interval toInterval = createToInterval(interval.getName(), targetIntersection); float percentLiftedOver = targetIntersection.intersectionLength/(float)interval.length(); ret.add(new PartialLiftover(intersectingChain, toInterval, targetIntersection.chain.id, percentLiftedOver)); } } return ret; }
/** Constructs a new coverage object for the provided mapping with the desired padding either side. */ public Coverage(final Interval i, final int padding) { this.interval = i; this.depths = new int[interval.length() + 2*padding]; }
/** * Adds an interval to the list of intervals. */ public void add(final Interval interval) { if (header.getSequence(interval.getContig()) == null) { throw new IllegalArgumentException(String.format("Cannot add interval %s, contig not in header", interval.toString())); } this.intervals.add(interval); }