| 1 |
package sort; |
| 2 |
|
| 3 |
import java.util.Comparator; |
| 4 |
|
| 5 |
import assemble.Contig; |
| 6 |
import shared.Tools; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
public final class ContigLengthComparator implements Comparator<Contig> { |
| 15 |
|
| 16 |
private ContigLengthComparator(){} |
| 17 |
|
| 18 |
@Override |
| 19 |
public int compare(Contig a, Contig b) { |
| 20 |
int x=compareInner(a, b); |
| 21 |
if(x==0){x=a.coverage>b.coverage ? 1 : a.coverage<b.coverage ? -1 : 0;} |
| 22 |
if(x==0){x=compareVectors(a.bases, b.bases);} |
| 23 |
if(x==0){x=a.id>b.id ? 1 : a.id<b.id ? -1 : 0;} |
| 24 |
return ascending*x; |
| 25 |
} |
| 26 |
|
| 27 |
private static int compareInner(Contig a, Contig b) { |
| 28 |
if(a==b){return 0;} |
| 29 |
if(a==null){return 1;} |
| 30 |
if(b==null){return -1;} |
| 31 |
int x=a.length()-b.length(); |
| 32 |
return x; |
| 33 |
} |
| 34 |
|
| 35 |
private static int compareVectors(final byte[] a, final byte[] b){ |
| 36 |
if(a==null || b==null){ |
| 37 |
if(a==null && b!=null){return 1;} |
| 38 |
if(a!=null && b==null){return -1;} |
| 39 |
return 0; |
| 40 |
} |
| 41 |
final int lim=Tools.min(a.length, b.length); |
| 42 |
for(int i=0; i<lim; i++){ |
| 43 |
if(a[i]<b[i]){return -1;} |
| 44 |
if(a[i]>b[i]){return 1;} |
| 45 |
} |
| 46 |
return 0; |
| 47 |
} |
| 48 |
|
| 49 |
public static final ContigLengthComparator comparator=new ContigLengthComparator(); |
| 50 |
|
| 51 |
private int ascending=-1; |
| 52 |
|
| 53 |
public void setAscending(boolean asc){ |
| 54 |
ascending=(asc ? 1 : -1); |
| 55 |
} |
| 56 |
|
| 57 |
} |