| 1 |
package clump; |
| 2 |
|
| 3 |
import java.util.Comparator; |
| 4 |
|
| 5 |
import shared.Tools; |
| 6 |
import stream.Read; |
| 7 |
|
| 8 |
|
| 9 |
public abstract class KmerComparator2 implements Comparator<Read>{ |
| 10 |
|
| 11 |
@Override |
| 12 |
public final int compare(Read a, Read b) { |
| 13 |
final ReadKey keyA=(ReadKey)a.obj; |
| 14 |
final ReadKey keyB=(ReadKey)b.obj; |
| 15 |
|
| 16 |
int x=compare(keyA, keyB); |
| 17 |
if(x==0){ |
| 18 |
x=compareSequence(a, b, 0); |
| 19 |
} |
| 20 |
return x==0 ? a.id.compareTo(b.id) : x; |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
public abstract int compare(ReadKey a, ReadKey b); |
| 25 |
|
| 26 |
public static final int compareSequence(Read a, Read b, int depth){ |
| 27 |
int x=compareSequence(a.bases, b.bases); |
| 28 |
if(x!=0){return x;} |
| 29 |
if(a.mate!=null){x=compareSequence(a.mate.bases, b.mate.bases);} |
| 30 |
if(x!=0){return x;} |
| 31 |
return compareQuality(a, b); |
| 32 |
} |
| 33 |
|
| 34 |
public static final int compareSequence(final byte[] a, final byte[] b){ |
| 35 |
if(a==null || b==null){ |
| 36 |
if(a==null && b!=null){return 1;} |
| 37 |
if(a!=null && b==null){return -1;} |
| 38 |
return 0; |
| 39 |
} |
| 40 |
if(a.length!=b.length){ |
| 41 |
return b.length-a.length; |
| 42 |
} |
| 43 |
for(int i=0, lim=a.length; i<lim; i++){ |
| 44 |
int x=a[i]-b[i]; |
| 45 |
if(x!=0){return x;} |
| 46 |
} |
| 47 |
return 0; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
public static final int compareQuality(Read a, Read b){ |
| 52 |
if(a.quality==null){return 0;} |
| 53 |
int qa=Tools.sumInt(a.quality); |
| 54 |
int qb=Tools.sumInt(b.quality); |
| 55 |
return qb-qa; |
| 56 |
} |
| 57 |
|
| 58 |
} |