File: ContigLengthComparator.java

package info (click to toggle)
bbmap 39.20%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,024 kB
  • sloc: java: 312,743; sh: 18,099; python: 5,247; ansic: 2,074; perl: 96; makefile: 39; xml: 38
file content (57 lines) | stat: -rwxr-xr-x 1,337 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package sort;

import java.util.Comparator;

import assemble.Contig;
import shared.Tools;

/**
 * Sorts longest contig first
 * @author Brian Bushnell
 * @date Jul 12, 2018
 *
 */
public final class ContigLengthComparator implements Comparator<Contig> {
	
	private ContigLengthComparator(){}
	
	@Override
	public int compare(Contig a, Contig b) {
		int x=compareInner(a, b);
		if(x==0){x=a.coverage>b.coverage ? 1 : a.coverage<b.coverage ? -1 : 0;}
		if(x==0){x=compareVectors(a.bases, b.bases);}
		if(x==0){x=a.id>b.id ? 1 : a.id<b.id ? -1 : 0;}
		return ascending*x;
	}
	
	private static int compareInner(Contig a, Contig b) {
		if(a==b){return 0;}
		if(a==null){return 1;}
		if(b==null){return -1;}
		int x=a.length()-b.length();
		return x;
	}
	
	private static int compareVectors(final byte[] a, final byte[] b){
		if(a==null || b==null){
			if(a==null && b!=null){return 1;}
			if(a!=null && b==null){return -1;}
			return 0;
		}
		final int lim=Tools.min(a.length, b.length);
		for(int i=0; i<lim; i++){
			if(a[i]<b[i]){return -1;}
			if(a[i]>b[i]){return 1;}
		}
		return 0;
	}
	
	public static final ContigLengthComparator comparator=new ContigLengthComparator();
	
	private int ascending=-1;
	
	public void setAscending(boolean asc){
		ascending=(asc ? 1 : -1);
	}
	
}