Skip to content

Commit

Permalink
fix: fix haplotype name sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
markwoon committed Jul 25, 2024
1 parent ed4d03f commit f9888b0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 26 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ dependencies {
[group: 'org.apache.logging.log4j', name: 'log4j-to-slf4j', version: '2.23.1'],
[group: 'org.apache.poi', name: 'poi-ooxml', version: '5.2.5'],
[group: 'org.checkerframework', name: 'checker-qual', version: '3.39.0'],
[group: 'org.pharmgkb', name: 'pgkb-common', version: '0.7.2'],
[group: 'org.pharmgkb', name: 'pgkb-common', version: '0.7.3'],
[group: 'org.pharmgkb', name: 'vcf-parser', version: '0.3.1'],
[group: 'org.slf4j', name: 'slf4j-api', version: '2.0.13'],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,19 @@

import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.pharmgkb.pharmcat.haplotype.model.CombinationMatch;

import static org.pharmgkb.pharmcat.haplotype.model.CombinationMatch.COMBINATION_NAME_SPLITTER;
import static org.pharmgkb.pharmcat.haplotype.model.CombinationMatch.extractCombinationName;


/**
* Comparator for haplotype names that take combination names and HGVS names into account.
* <p>
* <b>WARNING! DANGER!</b> This comparator does NOT comply with the comparator contract.
* It DOES NOT GUARANTEE TRANSITIVITY (i.e. if A > B and B > C, then A > C) if all haplotype names being compared are
* not of like kind (i.e. all HGVS, all strict star pattern, all loose star pattern, or all non-star pattern).
* See {@link org.pharmgkb.common.comparator.HaplotypeNameComparator} for details.
* <p>
* Support for HGVS names is just a band-aid on this problem to support DPYD.
* Comparator for haplotype names that take combination names into account.
*
* @author Mark Woon
*/
public class HaplotypeNameComparator implements Comparator<String> {
private static final Comparator<String> sf_comparator = new HaplotypeNameComparator();
private static final Pattern sf_hgvsPattern = Pattern.compile("(?:.*:)?[cgnmopr]\\.(\\d+).*");

/**
* Gets an instance of this comparator.
Expand All @@ -44,9 +34,13 @@ public int compare(String name1, String name2) {
}
if (name1 == null) {
return -1;
} else if (name2 == null) {
}
if (name2 == null) {
return 1;
}
if (name1.equals(name2)) {
return 0;
}

boolean c1 = CombinationMatch.isCombinationName(name1);
boolean c2 = CombinationMatch.isCombinationName(name2);
Expand All @@ -66,20 +60,15 @@ public int compare(String name1, String name2) {
return rez;
}

name1 = h1.get(0);
name2 = h2.get(0);
}

// sort HGVS based names nicely
Matcher h1 = sf_hgvsPattern.matcher(name1);
Matcher h2 = sf_hgvsPattern.matcher(name2);
if (h1.matches() && h2.matches()) {
int p1 = Integer.parseInt(h1.group(1));
int p2 = Integer.parseInt(h2.group(1));
if (p1 == p2) {
return name1.compareTo(name2);
for (int x = 0; x < h1.size(); x += 1) {
String n1 = h1.get(x);
String n2 = h2.get(x);
rez = org.pharmgkb.common.comparator.HaplotypeNameComparator.getComparator().compare(n1, n2);
if (rez != 0) {
return rez;
}
}
return Integer.compare(p1, p2);
return 0;
}

return org.pharmgkb.common.comparator.HaplotypeNameComparator.getComparator().compare(name1, name2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ void testComboSortName() {
names.add("[*3 + *4]");
names.add("[*2 + *3]");
assertEquals("[*2 + *3]", names.first());


names = new TreeSet<>(new HaplotypeNameComparator());
names.add("[*2 + *4]");
names.add("[*2 + *3]");
assertEquals("[*2 + *3]", names.first());
}

@Test
Expand Down

0 comments on commit f9888b0

Please sign in to comment.