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: BootstrappedMatrixByGenes.java,v $ |
2 |
26 Feb 07 |
jari |
* $Revision: 1.2 $ |
2 |
26 Feb 07 |
jari |
* $Date: 2006/02/23 20:59:59 $ |
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.util; |
2 |
26 Feb 07 |
jari |
13 |
|
2 |
26 Feb 07 |
jari |
14 |
import java.util.Random; |
2 |
26 Feb 07 |
jari |
15 |
import java.util.Vector; |
2 |
26 Feb 07 |
jari |
16 |
|
2 |
26 Feb 07 |
jari |
17 |
import org.tigr.util.FloatMatrix; |
2 |
26 Feb 07 |
jari |
18 |
|
2 |
26 Feb 07 |
jari |
19 |
public class BootstrappedMatrixByGenes { |
2 |
26 Feb 07 |
jari |
20 |
|
2 |
26 Feb 07 |
jari |
21 |
|
2 |
26 Feb 07 |
jari |
22 |
public Vector resampledIndices = new Vector(); |
2 |
26 Feb 07 |
jari |
23 |
|
2 |
26 Feb 07 |
jari |
//the following method "extracts" the 2D array from an ExpressionMatrix object to be used by the subsampleMatrix method in creating a resampled matrix. |
2 |
26 Feb 07 |
jari |
25 |
float[][] get2DArrFromExpMatrix(FloatMatrix expMatrix) { |
2 |
26 Feb 07 |
jari |
26 |
int geneDim = expMatrix.getRowDimension(); |
2 |
26 Feb 07 |
jari |
27 |
int expDim = expMatrix.getColumnDimension(); |
2 |
26 Feb 07 |
jari |
28 |
float[][] expArray = new float[geneDim][expDim]; |
2 |
26 Feb 07 |
jari |
29 |
int x, y; |
2 |
26 Feb 07 |
jari |
30 |
|
2 |
26 Feb 07 |
jari |
31 |
for (x = 0; x < geneDim; x++) { |
2 |
26 Feb 07 |
jari |
32 |
for (y = 0; y < expDim; y++) { |
2 |
26 Feb 07 |
jari |
33 |
expArray[x][y] = expMatrix.get(x,y); |
2 |
26 Feb 07 |
jari |
34 |
} |
2 |
26 Feb 07 |
jari |
35 |
} |
2 |
26 Feb 07 |
jari |
36 |
return expArray; |
2 |
26 Feb 07 |
jari |
37 |
} |
2 |
26 Feb 07 |
jari |
38 |
|
2 |
26 Feb 07 |
jari |
39 |
|
2 |
26 Feb 07 |
jari |
40 |
float[][] resampleMatrix(FloatMatrix expMatrix) {//This creates a resampled array (resampling done on genes) of the same dimensions as the original array |
2 |
26 Feb 07 |
jari |
41 |
|
2 |
26 Feb 07 |
jari |
42 |
float[][] aMatrix = get2DArrFromExpMatrix(expMatrix); |
2 |
26 Feb 07 |
jari |
43 |
float[][] newMatrix = new float[aMatrix.length][aMatrix[0].length]; |
2 |
26 Feb 07 |
jari |
44 |
int s, t, u; |
2 |
26 Feb 07 |
jari |
45 |
int numGenes = aMatrix.length; |
2 |
26 Feb 07 |
jari |
46 |
int randGene; |
2 |
26 Feb 07 |
jari |
47 |
Random generator = new Random(); |
2 |
26 Feb 07 |
jari |
48 |
|
2 |
26 Feb 07 |
jari |
49 |
final int STYLE = 2; //0 = Reshuffled, 1 = Reversed, 2 = Resampled |
2 |
26 Feb 07 |
jari |
50 |
|
2 |
26 Feb 07 |
jari |
51 |
if (STYLE == 0) { |
2 |
26 Feb 07 |
jari |
52 |
/* |
2 |
26 Feb 07 |
jari |
Vector reverseIDs = new Vector(); |
2 |
26 Feb 07 |
jari |
float[] temp = new float[aMatrix.length]; |
2 |
26 Feb 07 |
jari |
String tempID = null; |
2 |
26 Feb 07 |
jari |
56 |
|
2 |
26 Feb 07 |
jari |
Vector shuffledUniqueIDs = (Vector) ExpMatrix.UniqueIDs.clone(); |
2 |
26 Feb 07 |
jari |
58 |
|
2 |
26 Feb 07 |
jari |
for (s = numGenes; s > 0; s--) { |
2 |
26 Feb 07 |
jari |
60 |
|
2 |
26 Feb 07 |
jari |
randGene = generator.nextInt(s); |
2 |
26 Feb 07 |
jari |
for (int j = 0; j < aMatrix.length; j++) { |
2 |
26 Feb 07 |
jari |
temp[j] = aMatrix[j][randGene]; |
2 |
26 Feb 07 |
jari |
tempID = (String) shuffledUniqueIDs.elementAt(randGene); |
2 |
26 Feb 07 |
jari |
65 |
} |
2 |
26 Feb 07 |
jari |
for (int j = 0; j < aMatrix.length; j++) { |
2 |
26 Feb 07 |
jari |
aMatrix[j][randGene] = aMatrix[j][s - 1]; |
2 |
26 Feb 07 |
jari |
shuffledUniqueIDs.setElementAt(shuffledUniqueIDs.elementAt(s - 1), randGene); |
2 |
26 Feb 07 |
jari |
69 |
} |
2 |
26 Feb 07 |
jari |
for (int j = 0; j < aMatrix.length; j++) { |
2 |
26 Feb 07 |
jari |
aMatrix[j][s - 1] = temp[j]; |
2 |
26 Feb 07 |
jari |
shuffledUniqueIDs.setElementAt(tempID, (s - 1)); |
2 |
26 Feb 07 |
jari |
73 |
} |
2 |
26 Feb 07 |
jari |
74 |
|
2 |
26 Feb 07 |
jari |
//reverseIDs.add((String) ExpMatrix.GetUniqueID(randGene)); |
2 |
26 Feb 07 |
jari |
76 |
} |
2 |
26 Feb 07 |
jari |
77 |
|
2 |
26 Feb 07 |
jari |
//for (int i = reverseIDs.size(); i > 0; i--) { |
2 |
26 Feb 07 |
jari |
// resampledUniqueIDNames.add(reverseIDs.elementAt(i - 1)); |
2 |
26 Feb 07 |
jari |
80 |
//} |
2 |
26 Feb 07 |
jari |
81 |
|
2 |
26 Feb 07 |
jari |
resampledUniqueIDNames = shuffledUniqueIDs; |
2 |
26 Feb 07 |
jari |
83 |
*/ |
2 |
26 Feb 07 |
jari |
84 |
|
2 |
26 Feb 07 |
jari |
85 |
} else if (STYLE == 1) { |
2 |
26 Feb 07 |
jari |
86 |
|
2 |
26 Feb 07 |
jari |
87 |
/* |
2 |
26 Feb 07 |
jari |
float[] temp = new float[aMatrix.length]; |
2 |
26 Feb 07 |
jari |
for (int i = 0; i < numGenes; i++) { |
2 |
26 Feb 07 |
jari |
90 |
|
2 |
26 Feb 07 |
jari |
for (int j = 0; j < aMatrix.length; j++) { |
2 |
26 Feb 07 |
jari |
temp[j] = aMatrix[j][randGene]; |
2 |
26 Feb 07 |
jari |
tempID = (String) shuffledUniqueIDs.elementAt(randGene); |
2 |
26 Feb 07 |
jari |
94 |
} |
2 |
26 Feb 07 |
jari |
for (int j = 0; j < aMatrix.length; j++) { |
2 |
26 Feb 07 |
jari |
aMatrix[j][randGene] = aMatrix[j][s - 1]; |
2 |
26 Feb 07 |
jari |
shuffledUniqueIDs.setElementAt(shuffledUniqueIDs.elementAt(s - 1), randGene); |
2 |
26 Feb 07 |
jari |
98 |
} |
2 |
26 Feb 07 |
jari |
for (int j = 0; j < aMatrix.length; j++) { |
2 |
26 Feb 07 |
jari |
aMatrix[j][s - 1] = temp[j]; |
2 |
26 Feb 07 |
jari |
shuffledUniqueIDs.setElementAt(tempID, (s - 1)); |
2 |
26 Feb 07 |
jari |
102 |
} |
2 |
26 Feb 07 |
jari |
103 |
} |
2 |
26 Feb 07 |
jari |
Vector reverseIDs = new Vector(); |
2 |
26 Feb 07 |
jari |
for (int i = 0; i < numGenes; i++) { |
2 |
26 Feb 07 |
jari |
reverseIDs.add((String) ExpMatrix.GetUniqueID(i)); |
2 |
26 Feb 07 |
jari |
107 |
} |
2 |
26 Feb 07 |
jari |
for (int i = reverseIDs.size(); i > 0; i--) { |
2 |
26 Feb 07 |
jari |
resampledUniqueIDNames.add(reverseIDs.elementAt(i - 1)); |
2 |
26 Feb 07 |
jari |
110 |
} |
2 |
26 Feb 07 |
jari |
111 |
|
2 |
26 Feb 07 |
jari |
System.out.println("Reversed"); |
2 |
26 Feb 07 |
jari |
printMatrix(aMatrix); |
2 |
26 Feb 07 |
jari |
114 |
*/ |
2 |
26 Feb 07 |
jari |
115 |
|
2 |
26 Feb 07 |
jari |
116 |
} else if (STYLE == 2) { |
2 |
26 Feb 07 |
jari |
117 |
|
2 |
26 Feb 07 |
jari |
118 |
for (s = 0; s < numGenes; s++) { |
2 |
26 Feb 07 |
jari |
119 |
randGene = generator.nextInt(numGenes); |
2 |
26 Feb 07 |
jari |
120 |
for (u = 0; u < newMatrix[0].length; u++) { |
2 |
26 Feb 07 |
jari |
121 |
newMatrix[s][u] = aMatrix[randGene][u]; |
2 |
26 Feb 07 |
jari |
122 |
} |
2 |
26 Feb 07 |
jari |
123 |
resampledIndices.add(new Integer(randGene)); |
2 |
26 Feb 07 |
jari |
124 |
} |
2 |
26 Feb 07 |
jari |
125 |
|
2 |
26 Feb 07 |
jari |
126 |
aMatrix = newMatrix; |
2 |
26 Feb 07 |
jari |
127 |
} |
2 |
26 Feb 07 |
jari |
128 |
|
2 |
26 Feb 07 |
jari |
129 |
return aMatrix; |
2 |
26 Feb 07 |
jari |
130 |
} |
2 |
26 Feb 07 |
jari |
131 |
|
2 |
26 Feb 07 |
jari |
132 |
|
2 |
26 Feb 07 |
jari |
133 |
|
2 |
26 Feb 07 |
jari |
134 |
public FloatMatrix createResampExpMatrixObject(FloatMatrix expMatrix) { |
2 |
26 Feb 07 |
jari |
135 |
float[][] resampledArray = resampleMatrix(expMatrix); |
2 |
26 Feb 07 |
jari |
136 |
FloatMatrix resampMatrixObject = new FloatMatrix(resampledArray); |
2 |
26 Feb 07 |
jari |
137 |
|
2 |
26 Feb 07 |
jari |
138 |
return resampMatrixObject; |
2 |
26 Feb 07 |
jari |
139 |
} |
2 |
26 Feb 07 |
jari |
140 |
|
2 |
26 Feb 07 |
jari |
141 |
|
2 |
26 Feb 07 |
jari |
142 |
|
2 |
26 Feb 07 |
jari |
143 |
void printMatrix(float[][] matrix) { |
2 |
26 Feb 07 |
jari |
144 |
int i, j = 0; |
2 |
26 Feb 07 |
jari |
145 |
for (i = 0; i < matrix[0].length; i++) { |
2 |
26 Feb 07 |
jari |
146 |
for (j = 0; j < matrix.length; j++) { |
2 |
26 Feb 07 |
jari |
147 |
System.out.print(((int) (matrix[j][i] * 100)) / 100 + " "); |
2 |
26 Feb 07 |
jari |
148 |
} |
2 |
26 Feb 07 |
jari |
149 |
System.out.println(); |
2 |
26 Feb 07 |
jari |
150 |
} |
2 |
26 Feb 07 |
jari |
151 |
} |
2 |
26 Feb 07 |
jari |
152 |
|
2 |
26 Feb 07 |
jari |
153 |
} |