File: Shaver.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 (228 lines) | stat: -rwxr-xr-x 7,362 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package assemble;

import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;

import kmer.AbstractKmerTableSet;
import kmer.KmerTableSet;
import shared.Timer;
import ukmer.KmerTableSetU;

/**
 * Designed for removal of dead ends (aka hairs).
 * @author Brian Bushnell
 * @date Jun 26, 2015
 *
 */
public abstract class Shaver extends ShaveObject {
	
	
	/*--------------------------------------------------------------*/
	/*----------------         Factory          ----------------*/
	/*--------------------------------------------------------------*/

	public static final Shaver makeShaver(AbstractKmerTableSet tables, int threads){
		return makeShaver(tables, threads, 1, 1, 1, 1, 3, 100, 100, true, true);
	}
	
	public static final Shaver makeShaver(AbstractKmerTableSet tables, int threads,
			int minCount, int maxCount, int minSeed, int minCountExtend, float branchMult2, int maxLengthToDiscard, int maxDistanceToExplore,
			boolean removeHair, boolean removeBubbles){
		final Class<?> c=tables.getClass();
		if(c==KmerTableSet.class){
			return new Shaver1((KmerTableSet)tables, threads, minCount, maxCount, minSeed, minCountExtend, branchMult2,
					maxLengthToDiscard, maxDistanceToExplore, removeHair, removeBubbles);
		}else if(c==KmerTableSetU.class){
			return new Shaver2((KmerTableSetU)tables, threads, minCount, maxCount, minSeed, minCountExtend, branchMult2,
					maxLengthToDiscard, maxDistanceToExplore, removeHair, removeBubbles);
		}
		throw new RuntimeException("Unhandled class "+c);
	}
	
	
	/*--------------------------------------------------------------*/
	/*----------------         Constructor          ----------------*/
	/*--------------------------------------------------------------*/
	
	public Shaver(AbstractKmerTableSet tables_, int threads_,
			int minCount_, int maxCount_, int minSeed_, int minCountExtend_, float branchMult2_, int maxLengthToDiscard_, int maxDistanceToExplore_,
			boolean removeHair_, boolean removeBubbles_){
		threads=threads_;
		minCount=minCount_;
		maxCount=maxCount_;
		minSeed=minSeed_;
		minCountExtend=minCountExtend_;
		branchMult2=branchMult2_;
		maxLengthToDiscard=maxLengthToDiscard_;
		maxDistanceToExplore=maxDistanceToExplore_;
		removeHair=removeHair_;
		removeBubbles=removeBubbles_;
		
		kbig=tables_.kbig();
	}
	
	/*--------------------------------------------------------------*/
	/*----------------         Outer Methods        ----------------*/
	/*--------------------------------------------------------------*/

	abstract AbstractExploreThread makeExploreThread(int id_);
	abstract AbstractShaveThread makeShaveThread(int id_);
	
	public final long shave(int minCount_, int maxCount_){
		minCount=minCount_;
		maxCount=maxCount_;
		return shave();
	}
	
	public final long shave(){
		assert(minSeed>=minCount) : "Required: mincount >= minSeed >= maxCount. "+minCount+", "+minSeed+", "+maxCount;
		assert(minSeed<=maxCount) : "Required: mincount >= minSeed >= maxCount. "+minCount+", "+minSeed+", "+maxCount;
		assert(removeHair || removeBubbles);
		
		Timer t=new Timer();
		
		long kmersTestedTemp=0;
		long deadEndsFoundTemp=0;
		long kmersRemovedTemp=0;
		long bubblesFoundTemp=0;
		
		tables().initializeOwnership();
		

		countMatrix=new long[8][8];
		removeMatrix=new long[8][8];
		
		{
			nextTable.set(0);
			nextVictims.set(0);
			
			/* Create Explorethreads */
			ArrayList<AbstractExploreThread> alpt=new ArrayList<AbstractExploreThread>(threads);
			for(int i=0; i<threads; i++){alpt.add(makeExploreThread(i));}
			for(AbstractExploreThread pt : alpt){pt.start();}

			/* Wait for threads to die, and gather statistics */
			for(AbstractExploreThread pt : alpt){
				while(pt.getState()!=Thread.State.TERMINATED){
					try {
						pt.join();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

				kmersTestedTemp+=pt.kmersTestedT;
				deadEndsFoundTemp+=pt.deadEndsFoundT;
				bubblesFoundTemp+=pt.bubblesFoundT;

				for(int i=0; i<countMatrix.length; i++){
					for(int j=0; j<countMatrix[i].length; j++){
						countMatrix[i][j]+=pt.countMatrixT[i][j];
						removeMatrix[i][j]+=pt.removeMatrixT[i][j];
					}
				}
			}
			kmersTested+=kmersTestedTemp;
			deadEndsFound+=deadEndsFoundTemp;
			bubblesFound+=bubblesFoundTemp;

			t.stop();

			outstream.println("Tested "+kmersTestedTemp+" kmers.");
			outstream.println("Found "+deadEndsFoundTemp+" dead ends.");
			outstream.println("Found "+bubblesFoundTemp+" bubbles.");
			
			outstream.println("Search time: "+t);
		}

		{
			t.start();
			
			nextTable.set(0);
			nextVictims.set(0);
			
			/* Create Shavethreads */
			ArrayList<AbstractShaveThread> alpt=new ArrayList<AbstractShaveThread>(threads);
			for(int i=0; i<threads; i++){alpt.add(makeShaveThread(i));}
			for(AbstractShaveThread pt : alpt){pt.start();}
			
			for(AbstractShaveThread pt : alpt){
				while(pt.getState()!=Thread.State.TERMINATED){
					try {
						pt.join();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				kmersRemovedTemp+=pt.kmersRemovedT;
			}
			
			kmersRemoved+=kmersRemovedTemp;
			
			outstream.println("Removed "+kmersRemovedTemp+" kmers.");
			t.stop();
			outstream.println("Shave time: "+t);
		}
		
		if(printEventCounts){
			outstream.println("\nEvent counts:");
			for(int i=0; i<countMatrix.length; i++){
				for(int j=0; j<countMatrix[i].length; j++){
					outstream.print(countMatrix[i][j]+" ");
				}
				outstream.println();
			}
			outstream.println("\nRemoval counts:");
			for(int i=0; i<removeMatrix.length; i++){
				for(int j=0; j<removeMatrix[i].length; j++){
					outstream.print(removeMatrix[i][j]+" ");
				}
				outstream.println();
			}
		}
		
		return kmersRemovedTemp;
	}
	
	/*--------------------------------------------------------------*/
	/*----------------            Fields            ----------------*/
	/*--------------------------------------------------------------*/
	
	public long kmersTested=0;
	public long deadEndsFound=0;
	public long bubblesFound=0;
	public long kmersRemoved=0;
	
	/*--------------------------------------------------------------*/
	/*----------------         Final Fields         ----------------*/
	/*--------------------------------------------------------------*/
	
	abstract AbstractKmerTableSet tables();
	final int kbig;
	final int threads;
	int minCount;
	int maxCount;
	final int minSeed;
	final int minCountExtend;
	final float branchMult2;
	final int maxLengthToDiscard;
	final int maxDistanceToExplore;
	final boolean removeHair;
	final boolean removeBubbles;
	static boolean startFromHighCounts=true; //True is much faster, but decreases contiguity.
	static boolean shaveFast=true;
	static final boolean shaveVFast=false; //True is faster, but slightly decreases contiguity.

	private long[][] countMatrix;
	private long[][] removeMatrix;
	
	/** For controlling access to tables */
	final AtomicInteger nextTable=new AtomicInteger(0);
	
	/** For controlling access to victim buffers */
	final AtomicInteger nextVictims=new AtomicInteger(0);
	
}