File: Block.java

package info (click to toggle)
bbmap 38.90%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,520 kB
  • sloc: java: 265,882; sh: 14,954; python: 5,247; ansic: 2,074; perl: 96; xml: 38; makefile: 37
file content (171 lines) | stat: -rwxr-xr-x 4,237 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package align2;

import java.io.File;
import java.io.Serializable;

import fileIO.LoadThread;
import fileIO.ReadWrite;
import shared.KillSwitch;

/**
 * @author Brian Bushnell
 * @date Dec 23, 2012
 *
 */
public class Block implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -1638122096023589384L;
	
	public Block(int numSites_, int numStarts_){
		numSites=numSites_;
		numStarts=numStarts_;
		sites=new int[numSites];
		starts=new int[numStarts+1];
		assert(Integer.bitCount(numStarts)==1 && Integer.bitCount(starts.length)==2) : numStarts;
	}
	
	public Block(int[] sites_, int[] starts_){
		sites=sites_;
		starts=starts_;
		numSites=sites.length;
		numStarts=starts.length-1;
		assert(Integer.bitCount(numStarts)==1 && Integer.bitCount(starts.length)==2) : numStarts;
	}
	
	/** For legacy support */
	public int[] getHitList(int key){
		int len=length(key);
		if(len==0){return null;}
		int start=starts[key];
		int[] r=KillSwitch.copyOfRange(sites, start, start+len);
		return r;
	}
	
	/** For legacy support */
	public int[] getHitList(int start, int stop){
		int len=length(start, stop);
		if(len==0){return null;}
		assert(len>0) : len+", "+start+", "+stop;
		int[] r=KillSwitch.copyOfRange(sites, start, start+len);
		return r;
	}
	
	/** For legacy support */
	public int[][] getHitLists(int[] start, int[] stop){
		int[][] r=new int[start.length][];
		for(int i=0; i<start.length; i++){r[i]=getHitList(start[i], stop[i]);}
		return r;
	}
	
	public int length(int key){
		int x=starts[key+1]-starts[key];
		if(x==0){return 0;}
		return sites[starts[key]]!=-1 ? x : 0; //Lists can be removed by making the first site -1.
	}
	
	public int length(int start, int stop){
		if(start==stop || sites[start]==-1){return 0;}
		return stop-start;
	}
	
	public boolean write(String fname, boolean overwrite){
		String fname2=fname+"2.gz";
		{
			File f=new File(fname);
			if(f.exists()){
				if(!overwrite){
					assert(false) : "Tried to overwrite file "+f.getAbsolutePath();
					return false;
				}
			}
			f=new File(fname2);
			if(f.exists()){
				if(!overwrite){
					assert(false) : "Tried to overwrite file "+f.getAbsolutePath();
					return false;
				}
			}
		}
		ReadWrite.writeObjectInThread(sites, fname, allowSubprocess);
		if(!compress){
			ReadWrite.writeObjectInThread(starts, fname+"2.gz", allowSubprocess);
		}else{
			if(copyOnWrite){
				final int[] x;
				x=new int[starts.length];
				for(int i=1; i<x.length; i++){
					x[i]=starts[i]-starts[i-1];
				}
				ReadWrite.writeObjectInThread(starts, fname+"2.gz", allowSubprocess);
			}else{
				compress(starts);
				ReadWrite.writeAsync(starts, fname+"2.gz", allowSubprocess);
				decompress(starts);
			}
		}
		return true;
	}
	
	private static void compress(int[] x){
		for(int i=x.length-1; i>0; i--){
			x[i]=x[i]-x[i-1];
		}
	}
	
	private static void decompress(int[] x){
		int sum=x[0];
		for(int i=1; i<x.length; i++){
			sum+=x[i];
			x[i]=sum;
		}
	}
	
	public static Block read(String fname){
		String fname2=fname+"2.gz";
		
		final int[] a, b;
		{
			LoadThread<int[]> lta=LoadThread.load(fname, int[].class);
			b=ReadWrite.read(int[].class, fname2, false);
			lta.waitForThisToFinish();
			a=lta.output;
		}
//		{
//			LoadThread<int[]> lta=LoadThread.load(fname, int[].class);
//			LoadThread<int[]> ltb=LoadThread.load(fname2, int[].class);
//			lta.waitForThisToFinish();
//			ltb.waitForThisToFinish();
//			a=lta.output;
//			b=ltb.output;
//		}
		
//		int[] a=ReadWrite.read(int[].class, fname);
//		int[] b=ReadWrite.read(int[].class, fname2);
		
		assert(a!=null && b!=null) : a+", "+b;
		if(compress){
			int sum=b[0];
			for(int i=1; i<b.length; i++){
				sum+=b[i];
				b[i]=sum;
			}
		}
		Block r=new Block(a, b);
		assert(r.sites==a);
		assert(r.starts==b);
		return r;
	}

	public final int numSites;
	public final int numStarts;
	public final int[] sites;
	public final int[] starts;

	private static boolean allowSubprocess=false;
	private static final boolean compress=true;
	private static final boolean copyOnWrite=false;
	
}