]> git.donarmstrong.com Git - mothur.git/blob - slibshuff.cpp
a9710ded9303b2f93373216bf2d22ba3c81c527c
[mothur.git] / slibshuff.cpp
1 /*
2  *  slibshuff.cpp
3  *  Mothur
4  *
5  *  Created by Pat Schloss on 4/8/09.
6  *  Copyright 2009 Patrick D. Schloss. All rights reserved.
7  *
8  */
9
10 #include "slibshuff.h"
11
12 /***********************************************************************/
13
14 SLibshuff::SLibshuff(FullMatrix* D, int it, float co) : Libshuff(D, it, 0, co){}
15
16 /***********************************************************************/
17
18 float SLibshuff::evaluatePair(int i, int j){
19         return sCalculate(i,j);
20 }
21
22 /***********************************************************************/
23
24 vector<vector<double> > SLibshuff::evaluateAll(){
25         try{
26                 savedMins.resize(numGroups);
27                 
28                 vector<vector<double> > dCXYValues(numGroups);
29
30                 for(int i=0;i<numGroups;i++){
31                         dCXYValues[i].resize(numGroups);
32                         savedMins[i].resize(numGroups);
33                         for(int j=0;j<numGroups;j++){
34                                 if(i!=j){
35                                         dCXYValues[i][j] = sCalculate(i,j);     
36                                         savedMins[i][j] = minXY;
37                                 }
38
39                                 if(savedMins[i][i].size() == 0){
40                                         savedMins[i][i] = minX;
41                                 }
42
43                         }
44                 }               
45                 return dCXYValues;
46         }
47         catch(exception& e) {
48                 cout << "Standard Error: " << e.what() << " has occurred in the SLibshuff class Function evaluateAll. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
49                 exit(1);
50         }
51         catch(...) {
52                 cout << "An unknown error has occurred in the SLibshuff class function evaluateAll. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
53                 exit(1);
54         }       
55         
56 }
57
58 /***********************************************************************/
59
60 double SLibshuff::sCalculate(int x, int y){
61         try{
62                 minX = getMinX(x);
63                 minXY = getMinXY(x,y);
64
65                 sort(minX.begin(), minX.end());
66                 sort(minXY.begin(), minXY.end());
67
68                 double sum = 0.0,t=0.0;
69                 int ix=0,iy=0;
70                 while( (ix < groupSizes[x]) && (iy < groupSizes[x]) ) {
71                         double h = (ix-iy)/double(groupSizes[x]);
72                         
73                         if(minX[ix] < minXY[iy]) {
74                                 sum += (minX[ix] - t)*h*h;
75                                 t = minX[ix++];
76                         }
77                         else {
78                                 sum += (minXY[iy] - t)*h*h;
79                                 t = minXY[iy++];
80                         }
81                         
82                 }
83                 
84                 if(ix < groupSizes[x]) {
85                         
86                         while(ix < groupSizes[x]) {
87                                 double h = (ix-iy)/double(groupSizes[x]);
88                                 sum += (minX[ix] - t)*h*h;
89                                 t = minX[ix++];
90                         }
91                         
92                 }
93                 else {
94                         
95                         while(iy < groupSizes[x]) {
96                                 double h = (ix-iy)/double(groupSizes[x]);
97                                 sum += (minXY[iy] - t)*h*h;
98                                 t = minXY[iy++];
99                         }
100                         
101                 }
102                 
103                 return sum;
104         }
105         catch(exception& e) {
106                 cout << "Standard Error: " << e.what() << " has occurred in the SLibshuff class Function sCalculate. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
107                 exit(1);
108         }
109         catch(...) {
110                 cout << "An unknown error has occurred in the SLibshuff class function sCalculate. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
111                 exit(1);
112         }       
113         
114 }
115
116 /***********************************************************************/