Skip to content

Commit

Permalink
Add ChromosomePositionComparator.
Browse files Browse the repository at this point in the history
  • Loading branch information
markwoon committed Apr 21, 2016
1 parent 64135ad commit ea35cd7
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
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);
}
}
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");
}
}

0 comments on commit ea35cd7

Please sign in to comment.