Debian Codegraph @

current/assemble/Shaver.java

raw symbols SCIP
1 package assemble;
2
3 import java.util.ArrayList;
4 import java.util.concurrent.atomic.AtomicInteger;
5
6 import kmer.AbstractKmerTableSet;
7 import kmer.KmerTableSet;
8 import shared.Timer;
9 import ukmer.KmerTableSetU;
10
11 /**
12 * Designed for removal of dead ends (aka hairs).
13 * @author Brian Bushnell
14 * @date Jun 26, 2015
15 *
16 */
17 public abstract class Shaver extends ShaveObject {
18
19
20 /*--------------------------------------------------------------*/
21 /*---------------- Factory ----------------*/
22 /*--------------------------------------------------------------*/
23
24 public static final Shaver makeShaver(AbstractKmerTableSet tables, int threads){
25 return makeShaver(tables, threads, 1, 1, 1, 1, 3, 100, 100, true, true);
26 }
27
28 public static final Shaver makeShaver(AbstractKmerTableSet tables, int threads,
29 int minCount, int maxCount, int minSeed, int minCountExtend, float branchMult2, int maxLengthToDiscard, int maxDistanceToExplore,
30 boolean removeHair, boolean removeBubbles){
31 final Class<?> c=tables.getClass();
32 if(c==KmerTableSet.class){
33 return new Shaver1((KmerTableSet)tables, threads, minCount, maxCount, minSeed, minCountExtend, branchMult2,
34 maxLengthToDiscard, maxDistanceToExplore, removeHair, removeBubbles);
35 }else if(c==KmerTableSetU.class){
36 return new Shaver2((KmerTableSetU)tables, threads, minCount, maxCount, minSeed, minCountExtend, branchMult2,
37 maxLengthToDiscard, maxDistanceToExplore, removeHair, removeBubbles);
38 }
39 throw new RuntimeException("Unhandled class "+c);
40 }
41
42
43 /*--------------------------------------------------------------*/
44 /*---------------- Constructor ----------------*/
45 /*--------------------------------------------------------------*/
46
47 public Shaver(AbstractKmerTableSet tables_, int threads_,
48 int minCount_, int maxCount_, int minSeed_, int minCountExtend_, float branchMult2_, int maxLengthToDiscard_, int maxDistanceToExplore_,
49 boolean removeHair_, boolean removeBubbles_){
50 threads=threads_;
51 minCount=minCount_;
52 maxCount=maxCount_;
53 minSeed=minSeed_;
54 minCountExtend=minCountExtend_;
55 branchMult2=branchMult2_;
56 maxLengthToDiscard=maxLengthToDiscard_;
57 maxDistanceToExplore=maxDistanceToExplore_;
58 removeHair=removeHair_;
59 removeBubbles=removeBubbles_;
60
61 kbig=tables_.kbig();
62 }
63
64 /*--------------------------------------------------------------*/
65 /*---------------- Outer Methods ----------------*/
66 /*--------------------------------------------------------------*/
67
68 abstract AbstractExploreThread makeExploreThread(int id_);
69 abstract AbstractShaveThread makeShaveThread(int id_);
70
71 public final long shave(int minCount_, int maxCount_){
72 minCount=minCount_;
73 maxCount=maxCount_;
74 return shave();
75 }
76
77 public final long shave(){
78 assert(minSeed>=minCount) : "Required: mincount >= minSeed >= maxCount. "+minCount+", "+minSeed+", "+maxCount;
79 assert(minSeed<=maxCount) : "Required: mincount >= minSeed >= maxCount. "+minCount+", "+minSeed+", "+maxCount;
80 assert(removeHair || removeBubbles);
81
82 Timer t=new Timer();
83
84 long kmersTestedTemp=0;
85 long deadEndsFoundTemp=0;
86 long kmersRemovedTemp=0;
87 long bubblesFoundTemp=0;
88
89 tables().initializeOwnership();
90
91
92 countMatrix=new long[8][8];
93 removeMatrix=new long[8][8];
94
95 {
96 nextTable.set(0);
97 nextVictims.set(0);
98
99 /* Create Explorethreads */
100 ArrayList<AbstractExploreThread> alpt=new ArrayList<AbstractExploreThread>(threads);
101 for(int i=0; i<threads; i++){alpt.add(makeExploreThread(i));}
102 for(AbstractExploreThread pt : alpt){pt.start();}
103
104 /* Wait for threads to die, and gather statistics */
105 for(AbstractExploreThread pt : alpt){
106 while(pt.getState()!=Thread.State.TERMINATED){
107 try {
108 pt.join();
109 } catch (InterruptedException e) {
110 // TODO Auto-generated catch block
111 e.printStackTrace();
112 }
113 }
114
115 kmersTestedTemp+=pt.kmersTestedT;
116 deadEndsFoundTemp+=pt.deadEndsFoundT;
117 bubblesFoundTemp+=pt.bubblesFoundT;
118
119 for(int i=0; i<countMatrix.length; i++){
120 for(int j=0; j<countMatrix[i].length; j++){
121 countMatrix[i][j]+=pt.countMatrixT[i][j];
122 removeMatrix[i][j]+=pt.removeMatrixT[i][j];
123 }
124 }
125 }
126 kmersTested+=kmersTestedTemp;
127 deadEndsFound+=deadEndsFoundTemp;
128 bubblesFound+=bubblesFoundTemp;
129
130 t.stop();
131
132 outstream.println("Tested "+kmersTestedTemp+" kmers.");
133 outstream.println("Found "+deadEndsFoundTemp+" dead ends.");
134 outstream.println("Found "+bubblesFoundTemp+" bubbles.");
135
136 outstream.println("Search time: "+t);
137 }
138
139 {
140 t.start();
141
142 nextTable.set(0);
143 nextVictims.set(0);
144
145 /* Create Shavethreads */
146 ArrayList<AbstractShaveThread> alpt=new ArrayList<AbstractShaveThread>(threads);
147 for(int i=0; i<threads; i++){alpt.add(makeShaveThread(i));}
148 for(AbstractShaveThread pt : alpt){pt.start();}
149
150 for(AbstractShaveThread pt : alpt){
151 while(pt.getState()!=Thread.State.TERMINATED){
152 try {
153 pt.join();
154 } catch (InterruptedException e) {
155 // TODO Auto-generated catch block
156 e.printStackTrace();
157 }
158 }
159
160 kmersRemovedTemp+=pt.kmersRemovedT;
161 }
162
163 kmersRemoved+=kmersRemovedTemp;
164
165 outstream.println("Removed "+kmersRemovedTemp+" kmers.");
166 t.stop();
167 outstream.println("Shave time: "+t);
168 }
169
170 if(printEventCounts){
171 outstream.println("\nEvent counts:");
172 for(int i=0; i<countMatrix.length; i++){
173 for(int j=0; j<countMatrix[i].length; j++){
174 outstream.print(countMatrix[i][j]+" ");
175 }
176 outstream.println();
177 }
178 outstream.println("\nRemoval counts:");
179 for(int i=0; i<removeMatrix.length; i++){
180 for(int j=0; j<removeMatrix[i].length; j++){
181 outstream.print(removeMatrix[i][j]+" ");
182 }
183 outstream.println();
184 }
185 }
186
187 return kmersRemovedTemp;
188 }
189
190 /*--------------------------------------------------------------*/
191 /*---------------- Fields ----------------*/
192 /*--------------------------------------------------------------*/
193
194 public long kmersTested=0;
195 public long deadEndsFound=0;
196 public long bubblesFound=0;
197 public long kmersRemoved=0;
198
199 /*--------------------------------------------------------------*/
200 /*---------------- Final Fields ----------------*/
201 /*--------------------------------------------------------------*/
202
203 abstract AbstractKmerTableSet tables();
204 final int kbig;
205 final int threads;
206 int minCount;
207 int maxCount;
208 final int minSeed;
209 final int minCountExtend;
210 final float branchMult2;
211 final int maxLengthToDiscard;
212 final int maxDistanceToExplore;
213 final boolean removeHair;
214 final boolean removeBubbles;
215 static boolean startFromHighCounts=true; //True is much faster, but decreases contiguity.
216 static boolean shaveFast=true;
217 static final boolean shaveVFast=false; //True is faster, but slightly decreases contiguity.
218
219 private long[][] countMatrix;
220 private long[][] removeMatrix;
221
222 /** For controlling access to tables */
223 final AtomicInteger nextTable=new AtomicInteger(0);
224
225 /** For controlling access to victim buffers */
226 final AtomicInteger nextVictims=new AtomicInteger(0);
227
228 }