-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
src/main/java/org/pharmgkb/common/comparator/ChromosomePositionComparator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
----- BEGIN LICENSE BLOCK ----- | ||
This Source Code Form is subject to the terms of the Mozilla Public License, v.2.0. | ||
If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
----- END LICENSE BLOCK ----- | ||
*/ | ||
package org.pharmgkb.common.comparator; | ||
|
||
import java.util.Comparator; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import org.apache.commons.lang3.ObjectUtils; | ||
|
||
|
||
/** | ||
* Comparator for chromosomal positions (in the format chrX:1234). | ||
* | ||
* @author Mark Woon | ||
*/ | ||
public class ChromosomePositionComparator implements Comparator<String> { | ||
public static final Comparator<String> sf_comparator = new ChromosomePositionComparator(); | ||
private static final Pattern sf_pattern = Pattern.compile("(?:chr)?(\\w{1,2}):(\\d+)"); | ||
|
||
/** | ||
* Gets an instance of this comparator. | ||
* | ||
* @return an instance of this comparator | ||
*/ | ||
public static Comparator<String> getComparator() { | ||
return sf_comparator; | ||
} | ||
|
||
|
||
@Override | ||
public int compare(String o1, String o2) { | ||
|
||
//noinspection StringEquality | ||
if (o1 == o2) { | ||
return 0; | ||
} | ||
if (o1 == null) { | ||
return -1; | ||
} else if (o2 == null) { | ||
return 1; | ||
} | ||
|
||
Matcher m1 = sf_pattern.matcher(o1); | ||
if (!m1.matches()) { | ||
throw new IllegalArgumentException("'" + o1 + "' is not in the expected chromosomal position format"); | ||
} | ||
Matcher m2 = sf_pattern.matcher(o2); | ||
if (!m2.matches()) { | ||
throw new IllegalArgumentException("'" + o2 + "' is not in the expected chromosomal position format"); | ||
} | ||
|
||
int rez = ChromosomeNameComparator.getComparator().compare(m1.group(1), m2.group(1)); | ||
if (rez != 0) { | ||
return rez; | ||
} | ||
Integer n1 = Integer.parseInt(m1.group(2)); | ||
Integer n2 = Integer.valueOf(m2.group(2)); | ||
return ObjectUtils.compare(n1, n2); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/test/java/org/pharmgkb/common/comparator/ChromosomePositionComparatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
----- BEGIN LICENSE BLOCK ----- | ||
This Source Code Form is subject to the terms of the Mozilla Public License, v.2.0. | ||
If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
----- END LICENSE BLOCK ----- | ||
*/ | ||
package org.pharmgkb.common.comparator; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
|
||
/** | ||
* Junit test for {@link ChromosomePositionComparator}. | ||
* | ||
* @author Mark Woon | ||
*/ | ||
public class ChromosomePositionComparatorTest { | ||
|
||
|
||
@Test | ||
public void testComparator() { | ||
|
||
assertEquals(0, ChromosomePositionComparator.getComparator().compare(null, null)); | ||
assertEquals(-1, ChromosomePositionComparator.getComparator().compare(null, "chr1:1")); | ||
assertEquals(1, ChromosomePositionComparator.getComparator().compare("chr1:1", null)); | ||
|
||
assertEquals(-1, ChromosomePositionComparator.getComparator().compare("chr1:4", "chr1:100")); | ||
assertEquals(0, ChromosomePositionComparator.getComparator().compare("chr1:4", "chr1:4")); | ||
assertEquals(1, ChromosomePositionComparator.getComparator().compare("chr3:4", "chr1:4")); | ||
assertEquals(1, ChromosomePositionComparator.getComparator().compare("chr4:100", "chr1:400")); | ||
} | ||
|
||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testBadArg1() throws IllegalArgumentException { | ||
ChromosomePositionComparator.getComparator().compare("chr1", "chr1:100"); | ||
} | ||
|
||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testBadArg2() throws IllegalArgumentException { | ||
ChromosomePositionComparator.getComparator().compare("chr1:1", ":100"); | ||
} | ||
} |