6263 |
28 May 21 |
nicklas |
1 |
package net.sf.basedb.varsearch.query; |
6263 |
28 May 21 |
nicklas |
2 |
|
6263 |
28 May 21 |
nicklas |
3 |
import java.util.Comparator; |
6263 |
28 May 21 |
nicklas |
4 |
|
6263 |
28 May 21 |
nicklas |
5 |
import org.apache.lucene.document.Document; |
6263 |
28 May 21 |
nicklas |
6 |
|
6263 |
28 May 21 |
nicklas |
7 |
import net.sf.basedb.util.Values; |
6263 |
28 May 21 |
nicklas |
8 |
|
6263 |
28 May 21 |
nicklas |
9 |
/** |
6377 |
08 Sep 21 |
nicklas |
Sort documents in chromosome and position order. If they are the same, |
6377 |
08 Sep 21 |
nicklas |
the ref and alt values are also considered. |
6263 |
28 May 21 |
nicklas |
@since 1.0 |
6263 |
28 May 21 |
nicklas |
13 |
*/ |
6263 |
28 May 21 |
nicklas |
14 |
public class SortByChromAndPos |
6263 |
28 May 21 |
nicklas |
15 |
implements Comparator<Document> |
6263 |
28 May 21 |
nicklas |
16 |
{ |
6263 |
28 May 21 |
nicklas |
17 |
|
6263 |
28 May 21 |
nicklas |
18 |
public SortByChromAndPos() |
6263 |
28 May 21 |
nicklas |
19 |
{} |
6263 |
28 May 21 |
nicklas |
20 |
|
6263 |
28 May 21 |
nicklas |
21 |
@Override |
6263 |
28 May 21 |
nicklas |
22 |
public int compare(Document o1, Document o2) |
6263 |
28 May 21 |
nicklas |
23 |
{ |
6263 |
28 May 21 |
nicklas |
24 |
String chr1 = makeComparable(o1.get("chrom")); |
6263 |
28 May 21 |
nicklas |
25 |
String chr2 = makeComparable(o2.get("chrom")); |
6263 |
28 May 21 |
nicklas |
26 |
int c = chr1.compareTo(chr2); |
6263 |
28 May 21 |
nicklas |
27 |
if (c != 0) return c; |
6263 |
28 May 21 |
nicklas |
28 |
|
6263 |
28 May 21 |
nicklas |
29 |
long pos1 = Values.getLong(o1.get("pos")); |
6263 |
28 May 21 |
nicklas |
30 |
long pos2 = Values.getLong(o2.get("pos")); |
6377 |
08 Sep 21 |
nicklas |
31 |
if (pos1 != pos2) return pos1 < pos2 ? -1 : 1; |
6377 |
08 Sep 21 |
nicklas |
32 |
|
6377 |
08 Sep 21 |
nicklas |
33 |
String refAlt1 = o1.get("ref")+">"+o1.get("alt"); |
6377 |
08 Sep 21 |
nicklas |
34 |
String refAlt2 = o2.get("ref")+">"+o2.get("alt"); |
6377 |
08 Sep 21 |
nicklas |
35 |
return refAlt1.compareTo(refAlt2); |
6263 |
28 May 21 |
nicklas |
36 |
} |
6263 |
28 May 21 |
nicklas |
37 |
|
6263 |
28 May 21 |
nicklas |
38 |
/** |
6263 |
28 May 21 |
nicklas |
To solve the problem with chr1, chr11, ... , chr2 we |
6263 |
28 May 21 |
nicklas |
insert a 0 before the number. Other values such as 'chrX' |
6263 |
28 May 21 |
nicklas |
etc. should automatically be correct. |
6263 |
28 May 21 |
nicklas |
42 |
*/ |
6263 |
28 May 21 |
nicklas |
43 |
private String makeComparable(String chr) |
6263 |
28 May 21 |
nicklas |
44 |
{ |
6263 |
28 May 21 |
nicklas |
45 |
if (chr.length() == 4) |
6263 |
28 May 21 |
nicklas |
46 |
{ |
6263 |
28 May 21 |
nicklas |
47 |
char last = chr.charAt(3); |
6263 |
28 May 21 |
nicklas |
48 |
if (last >= '0' && last <= '9') chr = "chr0"+chr.substring(3); |
6263 |
28 May 21 |
nicklas |
49 |
} |
6263 |
28 May 21 |
nicklas |
50 |
return chr; |
6263 |
28 May 21 |
nicklas |
51 |
} |
6263 |
28 May 21 |
nicklas |
52 |
|
6263 |
28 May 21 |
nicklas |
53 |
} |