2 |
26 Feb 07 |
jari |
1 |
/* |
2 |
26 Feb 07 |
jari |
Copyright @ 1999-2003, The Institute for Genomic Research (TIGR). |
2 |
26 Feb 07 |
jari |
All rights reserved. |
2 |
26 Feb 07 |
jari |
4 |
*/ |
2 |
26 Feb 07 |
jari |
5 |
/* |
2 |
26 Feb 07 |
jari |
* $RCSfile: ClusterWorker.java,v $ |
2 |
26 Feb 07 |
jari |
* $Revision: 1.2 $ |
2 |
26 Feb 07 |
jari |
* $Date: 2006/02/23 20:59:46 $ |
2 |
26 Feb 07 |
jari |
* $Author: caliente $ |
2 |
26 Feb 07 |
jari |
* $State: Exp $ |
2 |
26 Feb 07 |
jari |
11 |
*/ |
2 |
26 Feb 07 |
jari |
12 |
package org.tigr.microarray.mev.cluster.clusterUtil; |
2 |
26 Feb 07 |
jari |
13 |
|
2 |
26 Feb 07 |
jari |
14 |
import java.awt.Color; |
2 |
26 Feb 07 |
jari |
15 |
import java.util.HashSet; |
2 |
26 Feb 07 |
jari |
16 |
import java.util.Iterator; |
2 |
26 Feb 07 |
jari |
17 |
|
2 |
26 Feb 07 |
jari |
18 |
import javax.swing.JOptionPane; |
2 |
26 Feb 07 |
jari |
19 |
|
2 |
26 Feb 07 |
jari |
20 |
import org.tigr.microarray.mev.cluster.gui.Experiment; |
2 |
26 Feb 07 |
jari |
21 |
|
2 |
26 Feb 07 |
jari |
22 |
public class ClusterWorker { |
2 |
26 Feb 07 |
jari |
23 |
|
2 |
26 Feb 07 |
jari |
/** The repository which contains the cluster operators |
2 |
26 Feb 07 |
jari |
25 |
* |
2 |
26 Feb 07 |
jari |
26 |
*/ |
2 |
26 Feb 07 |
jari |
27 |
ClusterRepository repository; |
2 |
26 Feb 07 |
jari |
/** Creates new ClusterWorker */ |
2 |
26 Feb 07 |
jari |
29 |
public ClusterWorker( ClusterRepository repository ) { |
2 |
26 Feb 07 |
jari |
30 |
this.repository = repository; |
2 |
26 Feb 07 |
jari |
31 |
} |
2 |
26 Feb 07 |
jari |
32 |
|
2 |
26 Feb 07 |
jari |
/** Returns a Cluster containing the intersection |
2 |
26 Feb 07 |
jari |
* of member indices from the passed Cluster array. |
2 |
26 Feb 07 |
jari |
35 |
*/ |
2 |
26 Feb 07 |
jari |
36 |
public Cluster intersection(Cluster [] clusters) { |
2 |
26 Feb 07 |
jari |
37 |
Integer index; |
2 |
26 Feb 07 |
jari |
38 |
HashSet [] sets = new HashSet[clusters.length]; |
2 |
26 Feb 07 |
jari |
39 |
|
2 |
26 Feb 07 |
jari |
40 |
int minSize = 0; |
2 |
26 Feb 07 |
jari |
41 |
|
2 |
26 Feb 07 |
jari |
42 |
for(int i = 0; i < sets.length; i++){ |
2 |
26 Feb 07 |
jari |
43 |
sets[i] = clusters[i].getHashSet(); |
2 |
26 Feb 07 |
jari |
44 |
minSize = Math.min(minSize, clusters[i].getSize()); |
2 |
26 Feb 07 |
jari |
45 |
} |
2 |
26 Feb 07 |
jari |
46 |
|
2 |
26 Feb 07 |
jari |
47 |
HashSet resultSet = new HashSet(); //new HashSet(minSize); |
2 |
26 Feb 07 |
jari |
48 |
|
2 |
26 Feb 07 |
jari |
49 |
resultSet.addAll(intersect(sets)); |
2 |
26 Feb 07 |
jari |
50 |
|
2 |
26 Feb 07 |
jari |
51 |
Iterator iterator = resultSet.iterator(); |
2 |
26 Feb 07 |
jari |
52 |
int size = resultSet.size(); |
2 |
26 Feb 07 |
jari |
53 |
int [] result = new int[size]; |
2 |
26 Feb 07 |
jari |
54 |
for(int i = 0; i < size; i++){ |
2 |
26 Feb 07 |
jari |
55 |
result[i] = ((Integer)(iterator.next())).intValue(); |
2 |
26 Feb 07 |
jari |
56 |
} |
2 |
26 Feb 07 |
jari |
57 |
return buildResultCluster("Intersection", clusters, result); |
2 |
26 Feb 07 |
jari |
58 |
} |
2 |
26 Feb 07 |
jari |
59 |
|
2 |
26 Feb 07 |
jari |
/** Accumulates cluster operation results and returns a cluster. |
2 |
26 Feb 07 |
jari |
61 |
*/ |
2 |
26 Feb 07 |
jari |
62 |
private Cluster buildResultCluster(String operation, Cluster [] clusters, int [] indices){ |
2 |
26 Feb 07 |
jari |
63 |
String clusterNumbers = ""; |
2 |
26 Feb 07 |
jari |
64 |
int i = 0; |
2 |
26 Feb 07 |
jari |
65 |
for(; i < clusters.length-1; i++) |
2 |
26 Feb 07 |
jari |
66 |
clusterNumbers += clusters[i].getSerialNumber()+":"; |
2 |
26 Feb 07 |
jari |
67 |
if(clusters.length-1 > -1) |
2 |
26 Feb 07 |
jari |
68 |
clusterNumbers += clusters[clusters.length-1].getSerialNumber(); |
2 |
26 Feb 07 |
jari |
69 |
|
2 |
26 Feb 07 |
jari |
//in the case that experiment clusters are being intersected with possibly |
2 |
26 Feb 07 |
jari |
//different gene members, the most restrictive gene set is preserved. |
2 |
26 Feb 07 |
jari |
72 |
Experiment experiment = getMinExperiment(clusters); |
2 |
26 Feb 07 |
jari |
73 |
|
2 |
26 Feb 07 |
jari |
74 |
ClusterAttributesDialog dialog = new ClusterAttributesDialog("Cluster Operation: "+operation+"( "+clusterNumbers+" )", "Cluster Op", operation+"( "+clusterNumbers+" )", null, null, Color.lightGray); |
2 |
26 Feb 07 |
jari |
75 |
if(dialog.showModal() == JOptionPane.OK_OPTION) |
2 |
26 Feb 07 |
jari |
76 |
return new Cluster(indices, "Cluster Op.", dialog.getLabel(), operation+"( "+clusterNumbers+" )", "", dialog.getDescription(), -1, repository.takeNextClusterSerialNumber(), dialog.getColor(), experiment); |
2 |
26 Feb 07 |
jari |
77 |
else |
2 |
26 Feb 07 |
jari |
78 |
return null; |
2 |
26 Feb 07 |
jari |
79 |
} |
2 |
26 Feb 07 |
jari |
80 |
|
2 |
26 Feb 07 |
jari |
/** Accumulates cluster operation results and returns a cluster. |
2 |
26 Feb 07 |
jari |
82 |
* |
2 |
26 Feb 07 |
jari |
83 |
*/ |
2 |
26 Feb 07 |
jari |
84 |
private Cluster buildBasicCluster(String operation, Cluster [] clusters, int [] indices){ |
2 |
26 Feb 07 |
jari |
85 |
String clusterNumbers = ""; |
2 |
26 Feb 07 |
jari |
86 |
int i = 0; |
2 |
26 Feb 07 |
jari |
87 |
for(; i < clusters.length-1; i++) |
2 |
26 Feb 07 |
jari |
88 |
clusterNumbers += clusters[i].getSerialNumber()+":"; |
2 |
26 Feb 07 |
jari |
89 |
if(clusters.length-1 > -1) |
2 |
26 Feb 07 |
jari |
90 |
clusterNumbers += clusters[clusters.length-1].getSerialNumber(); |
2 |
26 Feb 07 |
jari |
91 |
|
2 |
26 Feb 07 |
jari |
//in the case that experiment clusters are being intersected with possibly |
2 |
26 Feb 07 |
jari |
//different gene members, the most restrictive gene set is preserved. |
2 |
26 Feb 07 |
jari |
94 |
Experiment experiment = getMinExperiment(clusters); |
2 |
26 Feb 07 |
jari |
95 |
|
2 |
26 Feb 07 |
jari |
96 |
return new Cluster(indices, "Cluster Op.", "", operation+"( "+clusterNumbers+" )", "", "", -1, -1, Color.lightGray, experiment); |
2 |
26 Feb 07 |
jari |
97 |
} |
2 |
26 Feb 07 |
jari |
98 |
|
2 |
26 Feb 07 |
jari |
99 |
public Experiment getMinExperiment(Cluster [] clusters){ |
2 |
26 Feb 07 |
jari |
100 |
Experiment smallestExp = clusters[0].getExperiment(); |
2 |
26 Feb 07 |
jari |
101 |
Experiment currentExp = smallestExp; |
2 |
26 Feb 07 |
jari |
102 |
for(int i = 0; i < clusters.length; i++){ |
2 |
26 Feb 07 |
jari |
103 |
currentExp = clusters[i].getExperiment(); |
2 |
26 Feb 07 |
jari |
104 |
if(currentExp.getNumberOfGenes() < smallestExp.getNumberOfGenes()) |
2 |
26 Feb 07 |
jari |
105 |
smallestExp = currentExp; |
2 |
26 Feb 07 |
jari |
106 |
} |
2 |
26 Feb 07 |
jari |
107 |
return smallestExp; |
2 |
26 Feb 07 |
jari |
108 |
} |
2 |
26 Feb 07 |
jari |
109 |
|
2 |
26 Feb 07 |
jari |
110 |
|
2 |
26 Feb 07 |
jari |
/** Helper method to perform cluster intersections |
2 |
26 Feb 07 |
jari |
112 |
*/ |
2 |
26 Feb 07 |
jari |
113 |
private HashSet intersect(HashSet [] sets){ |
2 |
26 Feb 07 |
jari |
114 |
HashSet result = intersect(sets[0], sets[1]); |
2 |
26 Feb 07 |
jari |
115 |
|
2 |
26 Feb 07 |
jari |
116 |
if(sets.length == 2) |
2 |
26 Feb 07 |
jari |
117 |
return result; |
2 |
26 Feb 07 |
jari |
118 |
else{ |
2 |
26 Feb 07 |
jari |
119 |
for(int i = 2; i < sets.length; i++){ |
2 |
26 Feb 07 |
jari |
120 |
result = intersect(result, sets[i]); |
2 |
26 Feb 07 |
jari |
121 |
} |
2 |
26 Feb 07 |
jari |
122 |
} |
2 |
26 Feb 07 |
jari |
123 |
return result; |
2 |
26 Feb 07 |
jari |
124 |
} |
2 |
26 Feb 07 |
jari |
125 |
|
2 |
26 Feb 07 |
jari |
126 |
private HashSet intersect(HashSet setOne, HashSet setTwo){ |
2 |
26 Feb 07 |
jari |
127 |
HashSet result = new HashSet(); |
2 |
26 Feb 07 |
jari |
128 |
Iterator iterator = setOne.iterator(); |
2 |
26 Feb 07 |
jari |
129 |
Integer currVal; |
2 |
26 Feb 07 |
jari |
130 |
while(iterator.hasNext()){ |
2 |
26 Feb 07 |
jari |
131 |
currVal = (Integer)(iterator.next()); |
2 |
26 Feb 07 |
jari |
132 |
if(setTwo.contains(currVal)) |
2 |
26 Feb 07 |
jari |
133 |
result.add(currVal); |
2 |
26 Feb 07 |
jari |
134 |
} |
2 |
26 Feb 07 |
jari |
135 |
return result; |
2 |
26 Feb 07 |
jari |
136 |
} |
2 |
26 Feb 07 |
jari |
137 |
|
2 |
26 Feb 07 |
jari |
/** Returns a Cluster representing the union of the members of the |
2 |
26 Feb 07 |
jari |
* clusters argument |
2 |
26 Feb 07 |
jari |
140 |
*/ |
2 |
26 Feb 07 |
jari |
141 |
public Cluster union(Cluster [] clusters){ |
2 |
26 Feb 07 |
jari |
142 |
int [] result = getUniqueIndices(clusters); |
2 |
26 Feb 07 |
jari |
143 |
return buildResultCluster("Union", clusters, result); |
2 |
26 Feb 07 |
jari |
144 |
} |
2 |
26 Feb 07 |
jari |
145 |
|
2 |
26 Feb 07 |
jari |
146 |
|
2 |
26 Feb 07 |
jari |
/** returns the unique indices among the indices of a group of clustes. |
2 |
26 Feb 07 |
jari |
148 |
*/ |
2 |
26 Feb 07 |
jari |
149 |
public int [] getUniqueIndices(Cluster [] clusters){ |
2 |
26 Feb 07 |
jari |
150 |
|
2 |
26 Feb 07 |
jari |
151 |
int [][] indices = new int[clusters.length][]; |
2 |
26 Feb 07 |
jari |
152 |
|
2 |
26 Feb 07 |
jari |
153 |
int count = 0; |
2 |
26 Feb 07 |
jari |
154 |
Cluster cluster; |
2 |
26 Feb 07 |
jari |
155 |
for(int i = 0; i < clusters.length; i++){ |
2 |
26 Feb 07 |
jari |
156 |
cluster = clusters[i]; |
2 |
26 Feb 07 |
jari |
157 |
indices[i] = cluster.getIndices(); |
2 |
26 Feb 07 |
jari |
158 |
count += indices[i].length; |
2 |
26 Feb 07 |
jari |
159 |
} |
2 |
26 Feb 07 |
jari |
160 |
int [] resultIndices = new int[count]; |
2 |
26 Feb 07 |
jari |
161 |
|
2 |
26 Feb 07 |
jari |
162 |
int cnt = 0; |
2 |
26 Feb 07 |
jari |
163 |
for(int i = 0; i < indices.length;i++){ |
2 |
26 Feb 07 |
jari |
164 |
for(int j = 0; j < indices[i].length; j++){ |
2 |
26 Feb 07 |
jari |
165 |
resultIndices[cnt] = indices[i][j]; |
2 |
26 Feb 07 |
jari |
166 |
cnt++; |
2 |
26 Feb 07 |
jari |
167 |
} |
2 |
26 Feb 07 |
jari |
168 |
} |
2 |
26 Feb 07 |
jari |
169 |
resultIndices = makeIndicesUnique(resultIndices); |
2 |
26 Feb 07 |
jari |
170 |
return resultIndices; |
2 |
26 Feb 07 |
jari |
171 |
|
2 |
26 Feb 07 |
jari |
172 |
} |
2 |
26 Feb 07 |
jari |
173 |
|
2 |
26 Feb 07 |
jari |
/** Helper method to eliminate redundant indices. |
2 |
26 Feb 07 |
jari |
175 |
*/ |
2 |
26 Feb 07 |
jari |
176 |
private int [] makeIndicesUnique(int [] indices){ |
2 |
26 Feb 07 |
jari |
177 |
|
2 |
26 Feb 07 |
jari |
178 |
int numberOfElements = repository.getDataElementCount(); |
2 |
26 Feb 07 |
jari |
179 |
|
2 |
26 Feb 07 |
jari |
180 |
boolean [] indexCheck = new boolean[numberOfElements]; |
2 |
26 Feb 07 |
jari |
181 |
|
2 |
26 Feb 07 |
jari |
182 |
int uniqueCount = 0; |
2 |
26 Feb 07 |
jari |
183 |
for(int i = 0; i < indices.length; i++){ |
2 |
26 Feb 07 |
jari |
184 |
if(!indexCheck[indices[i]]){ |
2 |
26 Feb 07 |
jari |
185 |
indexCheck[indices[i]] = true; |
2 |
26 Feb 07 |
jari |
186 |
uniqueCount++; |
2 |
26 Feb 07 |
jari |
187 |
} |
2 |
26 Feb 07 |
jari |
188 |
} |
2 |
26 Feb 07 |
jari |
189 |
int [] newIndices = new int[uniqueCount]; |
2 |
26 Feb 07 |
jari |
190 |
int cnt = 0; |
2 |
26 Feb 07 |
jari |
191 |
for(int i = 0; i < indexCheck.length; i++){ |
2 |
26 Feb 07 |
jari |
192 |
if(indexCheck[i]){ |
2 |
26 Feb 07 |
jari |
193 |
newIndices[cnt] = i; |
2 |
26 Feb 07 |
jari |
194 |
cnt++; |
2 |
26 Feb 07 |
jari |
195 |
} |
2 |
26 Feb 07 |
jari |
196 |
} |
2 |
26 Feb 07 |
jari |
197 |
return newIndices; |
2 |
26 Feb 07 |
jari |
198 |
} |
2 |
26 Feb 07 |
jari |
199 |
|
2 |
26 Feb 07 |
jari |
/** Returns an array of cluster labels. |
2 |
26 Feb 07 |
jari |
201 |
*/ |
2 |
26 Feb 07 |
jari |
202 |
public String [] getClusterLabels(Cluster [] clusters){ |
2 |
26 Feb 07 |
jari |
203 |
String [] labels = new String[clusters.length]; |
2 |
26 Feb 07 |
jari |
204 |
for(int i = 0; i < labels.length; i++){ |
2 |
26 Feb 07 |
jari |
205 |
labels[i] = clusters[i].getClusterLabel(); |
2 |
26 Feb 07 |
jari |
206 |
} |
2 |
26 Feb 07 |
jari |
207 |
return labels; |
2 |
26 Feb 07 |
jari |
208 |
} |
2 |
26 Feb 07 |
jari |
209 |
|
2 |
26 Feb 07 |
jari |
210 |
public Cluster xor(Cluster [] clusters){ |
2 |
26 Feb 07 |
jari |
// int serialNumber = this.repository.getMaxClusterSerialNumber(); |
2 |
26 Feb 07 |
jari |
212 |
Cluster union = this.utilityUnion(clusters); |
2 |
26 Feb 07 |
jari |
213 |
Cluster inter = this.utilityIntersection(clusters); |
2 |
26 Feb 07 |
jari |
// this.repository.setClusterSerialCounter(serialNumber); //roll back cluster serial numbers |
2 |
26 Feb 07 |
jari |
//following cluster creation for union and intersections |
2 |
26 Feb 07 |
jari |
216 |
HashSet unionSet = union.getHashSet(); |
2 |
26 Feb 07 |
jari |
217 |
HashSet interSet = inter.getHashSet(); |
2 |
26 Feb 07 |
jari |
218 |
|
2 |
26 Feb 07 |
jari |
219 |
Iterator iterator = interSet.iterator(); |
2 |
26 Feb 07 |
jari |
220 |
while(iterator.hasNext()){ |
2 |
26 Feb 07 |
jari |
221 |
unionSet.remove(iterator.next()); |
2 |
26 Feb 07 |
jari |
222 |
} |
2 |
26 Feb 07 |
jari |
223 |
return this.buildResultCluster("XOR", clusters, getIndices(unionSet)); |
2 |
26 Feb 07 |
jari |
224 |
} |
2 |
26 Feb 07 |
jari |
225 |
|
2 |
26 Feb 07 |
jari |
226 |
|
2 |
26 Feb 07 |
jari |
/** Returns a Cluster representing the union of the members of the |
2 |
26 Feb 07 |
jari |
* clusters argument |
2 |
26 Feb 07 |
jari |
229 |
*/ |
2 |
26 Feb 07 |
jari |
230 |
public Cluster utilityUnion(Cluster [] clusters){ |
2 |
26 Feb 07 |
jari |
231 |
int [] result = getUniqueIndices(clusters); |
2 |
26 Feb 07 |
jari |
232 |
return buildBasicCluster("Union", clusters, result); |
2 |
26 Feb 07 |
jari |
233 |
} |
2 |
26 Feb 07 |
jari |
234 |
|
2 |
26 Feb 07 |
jari |
/** Returns a Cluster containing the intersection |
2 |
26 Feb 07 |
jari |
* of member indices from the passed Cluster array. |
2 |
26 Feb 07 |
jari |
237 |
*/ |
2 |
26 Feb 07 |
jari |
238 |
public Cluster utilityIntersection(Cluster [] clusters) { |
2 |
26 Feb 07 |
jari |
239 |
Integer index; |
2 |
26 Feb 07 |
jari |
240 |
HashSet [] sets = new HashSet[clusters.length]; |
2 |
26 Feb 07 |
jari |
241 |
|
2 |
26 Feb 07 |
jari |
242 |
int minSize = 0; |
2 |
26 Feb 07 |
jari |
243 |
|
2 |
26 Feb 07 |
jari |
244 |
for(int i = 0; i < sets.length; i++){ |
2 |
26 Feb 07 |
jari |
245 |
sets[i] = clusters[i].getHashSet(); |
2 |
26 Feb 07 |
jari |
246 |
minSize = Math.min(minSize, clusters[i].getSize()); |
2 |
26 Feb 07 |
jari |
247 |
} |
2 |
26 Feb 07 |
jari |
248 |
|
2 |
26 Feb 07 |
jari |
249 |
HashSet resultSet = new HashSet(); //new HashSet(minSize); |
2 |
26 Feb 07 |
jari |
250 |
|
2 |
26 Feb 07 |
jari |
251 |
resultSet.addAll(intersect(sets)); |
2 |
26 Feb 07 |
jari |
252 |
|
2 |
26 Feb 07 |
jari |
253 |
Iterator iterator = resultSet.iterator(); |
2 |
26 Feb 07 |
jari |
254 |
int size = resultSet.size(); |
2 |
26 Feb 07 |
jari |
255 |
int [] result = new int[size]; |
2 |
26 Feb 07 |
jari |
256 |
for(int i = 0; i < size; i++){ |
2 |
26 Feb 07 |
jari |
257 |
result[i] = ((Integer)(iterator.next())).intValue(); |
2 |
26 Feb 07 |
jari |
258 |
} |
2 |
26 Feb 07 |
jari |
259 |
return buildBasicCluster("Intersection", clusters, result); |
2 |
26 Feb 07 |
jari |
260 |
} |
2 |
26 Feb 07 |
jari |
261 |
|
2 |
26 Feb 07 |
jari |
262 |
private int [] getIndices(HashSet set){ |
2 |
26 Feb 07 |
jari |
263 |
Object [] a = set.toArray(); |
2 |
26 Feb 07 |
jari |
264 |
int [] indices = new int[a.length]; |
2 |
26 Feb 07 |
jari |
265 |
for(int i = 0; i < a.length; i++) |
2 |
26 Feb 07 |
jari |
266 |
indices[i] = ((Integer)a[i]).intValue(); |
2 |
26 Feb 07 |
jari |
267 |
return indices; |
2 |
26 Feb 07 |
jari |
268 |
} |
2 |
26 Feb 07 |
jari |
269 |
} |
2 |
26 Feb 07 |
jari |
270 |
|