Debian Codegraph @

current/bin/BinMap.java

raw symbols SCIP
1 package bin;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Iterator;
6 import java.util.Map.Entry;
7 import java.util.concurrent.ConcurrentHashMap;
8 import java.util.concurrent.atomic.AtomicLong;
9
10 import shared.Tools;
11
12 public class BinMap extends BinObject implements Iterable<Cluster> {
13
14 public BinMap(ArrayList<Contig> contigs) {contigList=contigs;}
15
16 /** Higher is less stringent; 1.0 is neutral, 0 is exact match */
17 public Cluster addOrMerge(Bin a, int minSizeToCompare, int minSizeToMerge,
18 int minSizeToAdd, Oracle oracle, Key key, int matrixRange) {
19 if(minSizeToMerge>=0 && a.size()>=minSizeToMerge) {
20 Cluster best=findBestCluster(a, minSizeToCompare, key, matrixRange, oracle);
21 if(best!=null) {
22 best.add(a);
23 return best;
24 }
25 }
26 if(minSizeToAdd>=0 && a.size()>=minSizeToAdd) {return add(a, key.set(a));}
27 residual.add(a);
28 return null;
29 }
30
31 void addAll(Collection<? extends Bin> bins, int minSize) {
32 Key key=new Key();
33 for(Bin b : bins) {
34 if(b.size()<minSize) {
35 residual.add(b);
36 }else {
37 add(b, key);
38 }
39 }
40 }
41
42 Cluster add(Bin a, Key key) {
43 Cluster c=a.toCluster();
44 if(key==null) {key=new Key();}
45 key.set(a);
46 ArrayList<Cluster> list=getOrMakeList(key);
47 synchronized(list) {list.add(c);}
48 return c;
49 }
50
51 public ArrayList<Cluster> getOrMakeList(Key key){
52 ArrayList<Cluster> list=map.get(key);
53 if(list==null) {
54 map.putIfAbsent((Key)(key.clone()), new ArrayList<Cluster>(8));
55 list=map.get(key);
56 minGridGC=Tools.min(minGridGC, key.gcLevel);
57 maxGridGC=Tools.max(maxGridGC, key.gcLevel);
58 minGridDepth=Tools.min(minGridDepth, key.covLevel, key.covLevel2);
59 maxGridDepth=Tools.max(maxGridDepth, key.covLevel, key.covLevel2);
60 assert(minGridGC<=maxGridGC) : minGridGC+", "+maxGridGC+", "+key;
61 assert(minGridDepth<=maxGridDepth);
62 }
63 return list;
64 }
65
66 public Cluster findBestCluster(Bin a, int minSizeToCompare, Key key, int matrixRange, Oracle oracle) {
67 if(key==null) {key=new Key();}
68 oracle.clear();
69 final float gc=a.gc();
70 final float depth=a.depth(0);
71 final float depth2=a.depth(1);
72 key.set(a);
73
74 float mult=Binner.sizeAdjustMult(a.size());
75 final float maxDepthRatio=1+(oracle.maxDepthRatio0-1)*mult;
76 final float maxGCDif=oracle.maxGCDif0*mult;
77 assert(maxDepthRatio>=1) : maxDepthRatio;
78 final int minDepthLevel=Tools.max(minGridDepth,
79 key.covLevel-matrixRange, Key.quantizeDepth(depth/maxDepthRatio));
80 final int maxDepthLevel=Tools.min(maxGridDepth,
81 key.covLevel+matrixRange, Key.quantizeDepth(depth*maxDepthRatio));
82 final int minGCLevel=Tools.max(minGridGC, key.gcLevel-matrixRange, Key.quantizeGC(gc-maxGCDif));
83 final int maxGCLevel=Tools.min(maxGridGC, key.gcLevel+matrixRange, Key.quantizeGC(gc+maxGCDif));
84
85 int minDepthLevel2=0;
86 int maxDepthLevel2=0;
87 if(a.numDepths()>1) {
88 minDepthLevel2=Tools.max(minGridDepth, key.covLevel2-matrixRange, Key.quantizeDepth(depth2/maxDepthRatio));
89 maxDepthLevel2=Tools.min(maxGridDepth, key.covLevel2+matrixRange, Key.quantizeDepth(depth2*maxDepthRatio));
90 }
91
92 if(verbose) {
93 System.err.println("Using params minSizeToCompare="+minSizeToCompare+
94 ", maxKmerDif="+oracle.maxKmerDif0+", maxDepthRatio="+maxDepthRatio+
95 ", maxProduct="+oracle.maxProduct0+", maxGCDif="+maxGCDif+
96 ", maxCovariance="+oracle.maxCovariance0+", matrixRange="+matrixRange);
97 }
98
99 for(int depthLevel2=minDepthLevel2; depthLevel2<=maxDepthLevel2; depthLevel2++) {
100 for(int depthLevel=minDepthLevel; depthLevel<=maxDepthLevel; depthLevel++) {
101 for(int gcLevel=minGCLevel; gcLevel<=maxGCLevel; gcLevel++) {
102 if(verbose) {System.err.println("Looking at depth "+depthLevel+", gc "+gcLevel);}
103 key.setLevel(gcLevel, depthLevel, depthLevel2);
104 ArrayList<Cluster> list=map.get(key);
105 if(list!=null) {
106 int idx=findBestBinIndex(a, list, minSizeToCompare, oracle);
107 if(verbose && idx>=0) {System.err.println("***Set best to "+oracle.best.id());}
108 }
109 }
110 }
111 }
112 return (Cluster)oracle.best;
113 }
114
115 private int findBestBinIndex(Bin a, ArrayList<? extends Bin> clusters,
116 int minSizeToCompare, Oracle oracle) {
117
118 int bestIdx=-1;
119 for(int i=0; i<clusters.size(); i++) {
120 Bin b=clusters.get(i);
121 if(b==null || a==b) {continue;}
122 if(b.size()<minSizeToCompare) {break;}
123
124 float f=oracle.similarity(a, b, 1f);
125 assert(f!=0) : b.id();
126 if(verbose) {
127 System.err.println("Comparing to "+b.id()+"; score="+oracle.score+"/"+oracle.topScore);
128 }
129 if(f>oracle.topScore) {
130 assert(f>0);//Actually, could be -1; or clear should set to -1
131 oracle.best=b;
132 oracle.bestIdx=bestIdx=i;
133 oracle.topScore=f;
134 }
135 }
136 return bestIdx;
137 }
138
139 ArrayList<Cluster> toList(boolean addResidue){
140 ArrayList<Cluster> list=new ArrayList<Cluster>();
141 for(Entry<Key, ArrayList<Cluster>> e : map.entrySet()) {
142 list.addAll(e.getValue());
143 }
144 if(addResidue) {
145 for(Bin b : residual) {
146 list.add(b.cluster()==null ? b.toCluster() : b.cluster());
147 }
148 }
149 return list;
150 }
151
152 @Override
153 public Iterator<Cluster> iterator() {
154 return toList(false).iterator();
155 }
156
157 public void clear(boolean clearResidual) {
158 map.clear();
159 if(clearResidual) {residual.clear();}
160 minGridGC=999999;
161 maxGridGC=0;
162 minGridDepth=999999;
163 maxGridDepth=0;
164 }
165
166 public boolean isValid() {
167 assert(isValid(contigList, true));
168 assert(isValid(residual, false));
169 for(ArrayList<Cluster> list : map.values()) {
170 assert(isValid(list, false));
171 }
172 return true;
173 }
174
175 public int countClusters() {
176 int sum=0;
177 for(ArrayList<Cluster> list : map.values()) {sum+=list.size();}
178 return sum;
179 }
180
181 public ConcurrentHashMap<Key, ArrayList<Cluster>> map=
182 new ConcurrentHashMap<Key, ArrayList<Cluster>>(2000, 0.6f, 32);
183 public ArrayList<Bin> residual=new ArrayList<Bin>();//Should really be contigs
184 public ArrayList<Contig> contigList;
185 private int minGridGC=999999;
186 private int maxGridGC=0;
187 private int minGridDepth=999999;
188 private int maxGridDepth=0;
189
190 // public AtomicLong fastComparisons=new AtomicLong(0);
191 // public AtomicLong slowComparisons=new AtomicLong(0);
192
193 }