| 1 |
|
| /* |
| 2 |
|
| * MaxClassPol.cpp |
| 3 |
|
| * |
| 4 |
|
| * Created on: 26 feb 2012 |
| 5 |
|
| * Author: Wolfcastle |
| 6 |
|
| * |
| 7 |
|
| * Description |
| 8 |
|
| * This library implements the independent t-test |
| 9 |
|
| */ |
| 10 |
|
| |
| 11 |
|
| #include "feature_selection.h" |
| 12 |
|
| |
| 13 |
|
| /* |
| 14 |
|
| Description: Feature selection method that uses the independent t-test to select a feature |
| 15 |
|
| Parameters: (1) 2D expression data matrix, (2) number of matrix rows, (3) number of matrix columns (4) list of column names, |
| 16 |
|
| (5) first group symbol and (6) second group symbol |
| 17 |
|
| Return value: Index of the top t-value feature (row in data matrix) |
| 18 |
|
| */ |
| 19 |
0 |
| int indTTest(double** expData, int numFeatures, int numSamples, char** sampleNames, char* NORMAL, char* MALIGN){ |
| 20 |
|
| |
| 21 |
|
| //Determine class sizes |
| 22 |
|
| int sample; |
| 23 |
0 |
| int numNormal = 0; |
| 24 |
0 |
| int numMalign = 0; |
| 25 |
0 |
▶ | for(sample = 0; sample < numSamples; sample++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(sample = 0; sample < numSamples; sample++){ |
0 |
0 |
0 |
|
| 26 |
0 |
▶ | if(strcmp(sampleNames[sample], NORMAL) == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(strcmp(sampleNames[sample], NORMAL) == 0) |
0 |
0 |
0 |
|
| 27 |
0 |
| numNormal++; |
| 28 |
0 |
▶ | else if(strcmp(sampleNames[sample], MALIGN) == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(strcmp(sampleNames[sample], MALIGN) == 0) |
0 |
0 |
0 |
|
| 29 |
0 |
| numMalign++; |
| 30 |
|
| } |
| 31 |
|
| |
| 32 |
|
| //Establish class index arrays |
| 33 |
0 |
| int normalIndexes[numNormal]; |
| 34 |
0 |
| int malignIndexes[numMalign]; |
| 35 |
0 |
| int normalCounter = 0; |
| 36 |
0 |
| int malignCounter = 0; |
| 37 |
0 |
▶ | for(sample = 0; sample < numSamples; sample++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(sample = 0; sample < numSamples; sample++){ |
0 |
0 |
0 |
|
| 38 |
0 |
▶ | if(strcmp(sampleNames[sample], NORMAL) == 0){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(strcmp(sampleNames[sample], NORMAL) == 0){ |
0 |
0 |
0 |
|
| 39 |
0 |
| normalIndexes[normalCounter] = sample; |
| 40 |
0 |
| normalCounter++; |
| 41 |
|
| } |
| 42 |
0 |
▶ | else if(strcmp(sampleNames[sample], MALIGN) == 0){ |
Branch: 1 branch
| Branch | True | False | Total |
| else if(strcmp(sampleNames[sample], MALIGN) == 0){ |
0 |
0 |
0 |
|
| 43 |
0 |
| malignIndexes[malignCounter] = sample; |
| 44 |
0 |
| malignCounter++; |
| 45 |
|
| } |
| 46 |
|
| } |
| 47 |
|
| |
| 48 |
|
| //Perform t-test |
| 49 |
|
| int feature; |
| 50 |
0 |
| double currentT = 0; |
| 51 |
0 |
| double topT = 0; |
| 52 |
|
| int bestFeature; |
| 53 |
0 |
▶ | for(feature = 0; feature < numFeatures; feature++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(feature = 0; feature < numFeatures; feature++){ |
0 |
0 |
0 |
|
| 54 |
|
| |
| 55 |
|
| //Calculate sum of normal samples |
| 56 |
0 |
| double sumNormal = 0; |
| 57 |
|
| int normalBuffer; |
| 58 |
0 |
▶ | for(sample = 0; sample < numNormal; sample++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(sample = 0; sample < numNormal; sample++){ |
0 |
0 |
0 |
|
| 59 |
0 |
| normalBuffer = normalIndexes[sample]; |
| 60 |
0 |
| sumNormal += expData[feature][normalBuffer]; |
| 61 |
|
| } |
| 62 |
|
| //Calculate sum of malign samples |
| 63 |
0 |
| double sumMalign = 0; |
| 64 |
|
| int malignBuffer; |
| 65 |
0 |
▶ | for(sample = 0; sample < numMalign; sample++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(sample = 0; sample < numMalign; sample++){ |
0 |
0 |
0 |
|
| 66 |
0 |
| malignBuffer = malignIndexes[sample]; |
| 67 |
0 |
| sumMalign += expData[feature][malignBuffer]; |
| 68 |
|
| } |
| 69 |
|
| |
| 70 |
|
| //Calculate mean |
| 71 |
0 |
| double normalMean = sumNormal / numNormal; |
| 72 |
0 |
| double malignMean = sumMalign / numMalign; |
| 73 |
|
| |
| 74 |
|
| //Calculate variance of normal samples |
| 75 |
0 |
| double normalVar = 0; |
| 76 |
0 |
▶ | for(sample = 0; sample < numNormal; sample++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(sample = 0; sample < numNormal; sample++){ |
0 |
0 |
0 |
|
| 77 |
0 |
| normalBuffer = normalIndexes[sample]; |
| 78 |
0 |
| normalVar += pow(expData[feature][normalBuffer] - normalMean, 2); |
| 79 |
|
| } |
| 80 |
|
| |
| 81 |
|
| //Calculate variance of malign samples |
| 82 |
0 |
| double malignVar = 0; |
| 83 |
0 |
▶ | for(sample = 0; sample < numMalign; sample++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(sample = 0; sample < numMalign; sample++){ |
0 |
0 |
0 |
|
| 84 |
0 |
| malignBuffer = malignIndexes[sample]; |
| 85 |
0 |
| malignVar += pow(expData[feature][malignBuffer] - malignMean, 2); |
| 86 |
|
| } |
| 87 |
|
| |
| 88 |
|
| //Calculate t and compare to previous best |
| 89 |
0 |
▶ | if(normalVar == 0 && malignVar == 0){ |
Branch: 2 branches
| Branch | True | False | Total |
| if(normalVar == 0 && malignVar == 0){ |
0 |
0 |
0 |
| if(normalVar == 0 && malignVar == 0){ |
0 |
0 |
0 |
|
| 90 |
0 |
| normalVar = 0.000001; |
| 91 |
|
| } |
| 92 |
0 |
| currentT = fabs(normalMean - malignMean) / sqrt ((normalVar / numNormal) + (malignVar / numMalign)); |
| 93 |
0 |
▶ | if(currentT > topT){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(currentT > topT){ |
0 |
0 |
0 |
|
| 94 |
0 |
| topT = currentT; |
| 95 |
0 |
| bestFeature = feature; |
| 96 |
|
| } |
| 97 |
|
| } |
| 98 |
|
| |
| 99 |
|
| //Return index of best feature |
| 100 |
0 |
| return bestFeature; |
| 101 |
|
| } |