]> git.donarmstrong.com Git - mothur.git/blob - libshuff.cpp
*** empty log message ***
[mothur.git] / libshuff.cpp
1 /*
2  *  libshuffform.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 "libshuff.h"
11
12 /***********************************************************************/
13
14 void swap(int& i,int& j){       int t = i;  i = j;  j = t;      }
15
16 /***********************************************************************/
17
18 Libshuff::Libshuff(FullMatrix* D, int it, float step, float co) : matrix(D), iters(it), stepSize(step), cutOff(co){
19         try{
20                 groupNames = matrix->getGroups();
21                 groupSizes = matrix->getSizes();
22                 numGroups = matrix->getNumGroups();
23
24                 initializeGroups(matrix);
25         }
26         catch(exception& e) {
27                 cout << "Standard Error: " << e.what() << " has occurred in the Libshuff class Function Libshuff. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
28                 exit(1);
29         }
30         catch(...) {
31                 cout << "An unknown error has occurred in the Libshuff class function Libshuff. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
32                 exit(1);
33         }       
34         
35 }
36
37 /***********************************************************************/
38
39 void Libshuff::initializeGroups(FullMatrix* matrix){
40         try{
41                 groups.resize(numGroups);
42                 savedGroups.resize(numGroups);
43                 
44                 savedGroups.resize(numGroups);
45                 for(int i=0;i<numGroups;i++) {
46                         groups[i].resize(groupSizes[i]);
47                         savedGroups[i].resize(groupSizes[i]);
48                 }
49                 int index=0;
50                 for(int i=0;i<numGroups;i++){
51                         for(int j=0;j<groupSizes[i];j++){
52                                 savedGroups[i][j] = groups[i][j] = index++;
53                         }
54                 }
55         }
56         catch(exception& e) {
57                 cout << "Standard Error: " << e.what() << " has occurred in the Libshuff class Function initializeGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
58                 exit(1);
59         }
60         catch(...) {
61                 cout << "An unknown error has occurred in the Libshuff class function initializeGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
62                 exit(1);
63         }       
64         
65 }
66
67 /***********************************************************************/
68
69 vector<vector<vector<double> > > Libshuff::getSavedMins(){
70         return savedMins;
71 }
72
73 /***********************************************************************/
74
75 vector<double> Libshuff::getMinX(int x){
76         try{
77                 vector<double> minX(groupSizes[x], 0);
78                 for(int i=0;i<groupSizes[x];i++){
79                         minX[i] = (groupSizes[x] > 1 ? (i==0 ? matrix->get(groups[x][0], groups[x][1]) : matrix->get(groups[x][i], groups[x][0])) : 0.0);
80                         for(int j=0;j<groupSizes[x];j++){
81                                 if(i != j)      {
82                                         double dx = matrix->get(groups[x][i], groups[x][j]);
83                                         if(dx < minX[i]){       minX[i] = dx;   }
84                                 }
85                         }
86                 }
87                 return minX;
88         }
89         catch(exception& e) {
90                 cout << "Standard Error: " << e.what() << " has occurred in the Libshuff class Function getMinX. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
91                 exit(1);
92         }
93         catch(...) {
94                 cout << "An unknown error has occurred in the Libshuff class function getMinX. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
95                 exit(1);
96         }       
97         
98 }
99
100 /***********************************************************************/
101
102 vector<double> Libshuff::getMinXY(int x, int y){
103         try{
104                 vector<double> minXY(groupSizes[x], 0);
105
106                 for(int i=0;i<groupSizes[x];i++){
107                         minXY[i] = matrix->get(groups[x][i], groups[y][0]);
108                         for(int j=0;j<groupSizes[y];j++){
109                                 double dxy = matrix->get(groups[x][i], groups[y][j]);
110                                 if(dxy<minXY[i]){       minXY[i] = dxy; }
111                         }
112                 }
113                 return minXY;
114         }
115         catch(exception& e) {
116                 cout << "Standard Error: " << e.what() << " has occurred in the Libshuff class Function getMinXY. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
117                 exit(1);
118         }
119         catch(...) {
120                 cout << "An unknown error has occurred in the Libshuff class function getMinXY. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
121                 exit(1);
122         }       
123 }
124
125 /***********************************************************************/
126
127 void Libshuff::randomizeGroups(int x, int y){
128         try{
129                 int nv = groupSizes[x]+groupSizes[y];
130                 vector<int> v(nv);
131                 
132                 int index=0;
133                 for(int k=0;k<groupSizes[x];k++)        {       v[index++] = groups[x][k];      }
134                 for(int k=0;k<groupSizes[y];k++)        {       v[index++] = groups[y][k];      }
135                 
136                 for(int k=nv-1;k>0;k--){
137                         int z = (int)(rand() % k);
138                         swap(v[z],v[k]);
139                 }
140                 
141                 index=0;
142                 for(int k=0;k<groupSizes[x];k++)        {       groups[x][k]=v[index++];        }
143                 for(int k=0;k<groupSizes[y];k++)        {       groups[y][k]=v[index++];        }
144         }
145         catch(exception& e) {
146                 cout << "Standard Error: " << e.what() << " has occurred in the Libshuff class Function randomizeGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
147                 exit(1);
148         }
149         catch(...) {
150                 cout << "An unknown error has occurred in the Libshuff class function randomizeGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
151                 exit(1);
152         }       
153 }
154
155 /***********************************************************************/
156
157 void Libshuff::resetGroup(int x){
158         
159         for(int k=0;k<groupSizes[x];k++)        {       groups[x][k] = savedGroups[x][k];       }
160         
161 }
162
163 /***********************************************************************/