Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -286,34 +286,41 @@ public Location union( Location other )
}

/**
* Return the intersection, or null if no overlap.
*
* @param other The location to intersect.
* @return The maximal location that is contained by both. Returns null if no overlap!
* @throws IllegalArgumentException Locations are on opposite strands.
*/
public Location intersection( Location other )
{
if( isSameStrand( other ) )
{
int start= ( mStart > other.mStart)? mStart: other.mStart; //pick larger
int end= ( mEnd < other.mEnd)? mEnd: other.mEnd; //pick smaller

if( start <= end )
{
return new Location( start, end );
}
else
{
return null;
}
}
else
{
throw new IllegalArgumentException( "Locations are on opposite strands." );
}
* Return the intersection, or null if no overlap.
*
* @param other
* The location to intersect.
* @return The maximal location that is contained by both. Returns null if
* no overlap!
* @throws IllegalArgumentException
* Locations are on opposite strands.
*/
public Location intersection(Location other) {
if (isSameStrand(other)) {
return intersect(mStart, mEnd, other.mStart, other.mEnd);
} else {
throw new IllegalArgumentException("Locations are on opposite strands.");
}
}

private Location intersect(int a1, int a2, int b1, int b2) {
if (a1 > b1) {
return intersect(b1, b2, a1, a2);
}
// Safe to assume a1 <= b1
if (b1 >= a2) {
// b starts after a ends
return null;
} else if (b1 < a2 && b2 <= a2) {
// b starts after a starts and ends before or at where a ends
return new Location(b1, b2);
} else if (b1 >= a1 && a2 <= b2) {
// b starts after a but extends after the end of a
return new Location(b1, a2);
}
return null;
}

}

/**
* Get starting index (origin 0).
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.biojava.nbio.genome;

import static org.junit.Assert.*;

import org.biojava.nbio.genome.parsers.gff.Location;
import org.junit.Test;

public class TestIssue355 {

@Test
public void testIssue1() {
Location l1 = Location.fromBio(51227320, 51227381, '+');
Location l2 = Location.fromBio(51227323, 51227382, '+');

Location union = l1.union(l2);
assertEquals(51227320,union.bioStart());
assertEquals(51227382,union.bioEnd());
}

@Test
public void testIssue2() {
Location l1 = Location.fromBio(100, 200, '+');
Location l2 = Location.fromBio(1, 99, '+');
Location intersection = l1.intersection(l2);
assertNull(intersection);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,29 @@ public void testLocation() {
assertEquals(L(12,20), L(2,20).suffix( L(10,12)));

}

@Test
public void testLocationIntersections() {
// One inside another
Location r21_25 = new Location( 21, 25 );
Location r1_100 = new Location(1, 100 );

assertEquals(r21_25, r21_25.intersection( r1_100));
assertEquals(r21_25, r1_100.intersection( r21_25));

// Non overlapping
Location r10_100 = new Location(10, 100 );
Location r1_9 = new Location( 1, 9 );

assertNull(r10_100.intersection( r1_9));
assertNull(r1_9.intersection( new Location( 9, 10 )));

// Partially overlappping
Location r1_25 = new Location( 1, 25 );
Location r21_100 = new Location(21, 100 );
assertEquals(r21_25, r1_25.intersection( r21_100));
assertEquals(r21_25, r21_100.intersection( r1_25));
}

//shorthand for testing
private static Location L( int s, int e ) {
Expand Down