Debian Codegraph @

current/bin/Cluster.java

raw symbols SCIP
1 package bin;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5
6 import shared.Tools;
7 import sketch.Sketch;
8 import sketch.SketchMakerMini;
9 import stream.Read;
10 import structures.IntHashMap;
11 import structures.IntHashSet;
12 import tracker.EntropyTracker;
13
14 public class Cluster extends Bin {
15
16 public Cluster(int id_) {id=id_;}
17
18 public Cluster(Contig c) {
19 id=c.id();
20 add(c);
21 }
22
23 @Override
24 public String name() {return contigs.get(0).name;}
25
26 @Override
27 public boolean isCluster() {return true;}
28
29 @Override
30 public Cluster toCluster() {return this;}
31
32 @Override
33 public Cluster cluster() {return this;}
34
35 @Override
36 public final int id() {return id;}
37
38 public boolean pure(float fraction) {return nonDominantSize<=size*fraction;}
39
40 public float minContigDepth() {
41 float d=contigs.size()==0 ? 0 : contigs.get(0).depth();
42 for(Contig c : contigs) {d=Math.min(c.depth(), d);}
43 return d;
44 }
45
46 public float maxContigDepth() {
47 float d=0;
48 for(Contig c : contigs) {d=Math.max(c.depth(), d);}
49 return d;
50 }
51
52 @Override
53 @Deprecated
54 public void setID(int id_) {
55 throw new RuntimeException("Not supported.");
56 }
57
58 public Cluster add(Bin b) {
59 if(b.getClass()==Cluster.class) {return add((Cluster)b);}
60 else {return add((Contig)b);}
61 }
62
63 public Cluster add(Cluster clust) {
64 assert(clust!=this) : id();
65 for(Contig c : clust.contigs) {add(c);}
66 clust.contigs=null;
67 // assert(isValid());
68 return this;
69 }
70
71 boolean sameCluster(Bin b) {
72 if(contigSet.contains(b.id())) {return true;}
73 return b.isCluster() && ((Cluster)b).contigSet.contains(id());
74 }
75
76 public synchronized Cluster add(Contig c) {
77 timestamp=globalTime;
78 // verbose=(id()==52 || id()==3 || id()==3 || c.id()==52 || c.id()==3 || c.id()==3);
79 // if(verbose) {
80 // System.err.println("Adding "+c.id()+" to "+this.id()+(c.cluster==null ? "" : " from "+c.cluster.id));
81 // assert(c.cluster==null || c.cluster.id()!=3);
82 // }
83 // assert(c.isValid());
84 assert(c.cluster!=this);
85 assert(!contigSet.contains(c.id()));
86 c.cluster=this;
87 final long size2=size()+c.size();
88 final float invSize2=1f/size2;
89 if(contigs.size()==0) {
90 topHit=c.topHit;
91 secondHit=c.secondHit;
92 taxid=c.taxid;
93 genusTaxid=c.genusTaxid;
94 labelTaxid=c.labelTaxid;
95 sketchedSize=c.sketchedSize;
96 entropy=c.entropy;
97 // acRatio=c.acRatio;
98 }else {
99 if(labelTaxid<1 || (c.labelTaxid!=labelTaxid && c.size()>=0.05f*size())) {nonDominantSize+=c.size();}
100 // assert(isValid());
101 entropy=(entropy*size()+c.entropy*c.size())*invSize2;
102 // acRatio=(acRatio*size()+c.acRatio*c.size())*invSize2;
103 }
104 contigs.add(c);
105 contigSet.add(c.id());
106 numTetramers+=c.numTetramers;
107 numPentamers+=c.numPentamers;
108 // invKmers=1f/numTetramers;
109 if(tetramers==null) {tetramers=c.tetramers.clone();}
110 else {Tools.add(tetramers, c.tetramers);}
111 if(c.dimers!=null) {
112 if(dimers==null) {dimers=c.dimers.clone();}
113 else {Tools.add(dimers, c.dimers);}
114 strandedness=EntropyTracker.strandedness(dimers, 2);
115 }
116 if(c.trimers!=null) {
117 if(trimers==null) {trimers=c.trimers.clone();}
118 else {Tools.add(trimers, c.trimers);}
119 }
120 if(c.pentamers!=null) {
121 if(pentamers==null) {pentamers=c.pentamers.clone();}
122 else {Tools.add(pentamers, c.pentamers);}
123 }
124
125 if(c.pairMap!=null) {
126 if(pairMap==null) {pairMap=new IntHashMap(5);}
127 // pairMap.incrementAll(c.pairMap);//Remember, the targets are contig IDs.
128 pairMap.setToMax(c.pairMap);
129 }
130
131 if(numDepths()==0) {//Empty cluster
132 assert(size==0 || grading);
133 for(int i=0, max=c.numDepths(); i<max; i++) {appendDepth(c.depth(i));}
134 }else {//Nonempty cluster
135 assert(c.numDepths()==numDepths()) : c.numDepths()+", "+numDepths();
136 float cmult=invSize2*c.size();
137 float bmult=invSize2*size;
138 for(int i=0, max=c.numDepths(); i<max; i++) {
139 float cdepth=c.depth(i)*cmult;
140 float bdepth=depth(i)*bmult;
141 setDepth(cdepth+bdepth, i);
142 }
143 }
144 if(numDepths()>1) {
145 fillNormDepth();
146 }
147 avgDepthValid=false;
148 size=size2;
149 gcSum+=c.gcSum;
150 // assert(gcSum>0 || grading) : gcSum+", "+c.gcSum+", "+c.size()+", "+c.numContigs();
151 // assert(isValid());
152 // assert(c.isValid());
153 return this;
154 }
155
156 @Override
157 public long size() {return size;}
158
159 @Override
160 public Sketch toSketch(SketchMakerMini smm, Read r) {
161 String name=Long.toString(id());
162 if(r==null) {r=new Read(null, null, name, id());}
163 r.id=name;
164 r.numericID=id();
165 for(Contig c : contigs) {
166 r.bases=c.bases;
167 smm.processReadNucleotide(r);
168 }
169 return smm.toSketch(0);
170 }
171
172 @Override
173 public boolean isValid() {
174 if(id()<0) {
175 assert(false) : id()+", "+contigSet;
176 return false;
177 }
178 if(numDepths()<1) {
179 assert(false) : id()+", "+contigSet;
180 return false;
181 }
182 if(tetramers==null) {
183 assert(false) : id()+", "+contigSet;
184 return false;
185 }
186 if(contigs.isEmpty()) {
187 assert(false) : id()+", "+contigSet;
188 return false;
189 }
190 for(Contig c : contigs) {
191 if(c.cluster!=this) {
192 assert(c.cluster!=null);
193 assert(false) : "Cluster "+id()+" contains a contig "+c.id()+" that points to "+c.cluster().id()+"\n"
194 + " set="+contigSet+", c.set="+c.cluster.contigSet+"\n"+contigs+"\n"+c.cluster.contigs+"\n";
195 return false;
196 }
197 if(!contigSet.contains(c.id())) {
198 assert(false) : id()+", "+contigSet;
199 return false;
200 }
201 // if(c.pairMap!=null && !c.pairMap.isEmpty()) {
202 // for(KeyValue kv : KeyValue.toList(c.pairMap)) {
203 // int value2=pairMap.get(kv.key);
204 // assert(value2>=kv.value) : c.pairMap+"\n"+pairMap;
205 // }
206 // }
207 }
208 return true;
209 }
210
211 public void clear() {
212 size=0;
213 contigs.clear();
214 contigSet.clear();
215 pairMap=null;
216
217 numTetramers=0;
218 numPentamers=0;
219 // invKmers=0;
220 dimers=null;
221 trimers=null;
222 tetramers=null;
223 pentamers=null;
224 gcSum=0;
225 sketchedSize=0;
226 clearDepth();
227 completeness=contam=entropy=strandedness=score=0;
228 dest=0;
229 taxid=genusTaxid=labelTaxid=0;
230 topHit=secondHit=null;
231 nonDominantSize=0;
232 }
233
234 @Override
235 public int numContigs() {return contigs==null ? 0 : contigs.size();}
236
237 @Override
238 public Iterator<Contig> iterator() {
239 return contigs.iterator();
240 }
241
242 final int id;
243 public long size;
244 public ArrayList<Contig> contigs=new ArrayList<Contig>(8);
245 public IntHashSet contigSet=new IntHashSet(5);
246 private int timestamp=-1;
247 long nonDominantSize=0;
248
249 }