]> git.donarmstrong.com Git - mothur.git/blob - libshuff.cpp
fixed some bugs and added mgcluster command
[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                 initializeGroups(matrix);
24         }
25         catch(exception& e) {
26                 errorOut(e, "Libshuff", "Libshuff");
27                 exit(1);
28         }
29 }
30
31 /***********************************************************************/
32
33 void Libshuff::initializeGroups(FullMatrix* matrix){
34         try{
35                 groups.resize(numGroups);
36                 savedGroups.resize(numGroups);
37                 
38                 savedGroups.resize(numGroups);
39                 for(int i=0;i<numGroups;i++) {
40                         groups[i].resize(groupSizes[i]);
41                         savedGroups[i].resize(groupSizes[i]);
42                 }
43                 int index=0;
44                 for(int i=0;i<numGroups;i++){
45
46                         for(int j=0;j<groupSizes[i];j++){
47                                 savedGroups[i][j] = groups[i][j] = index++;
48                         }
49                 }
50         }
51         catch(exception& e) {
52                 errorOut(e, "Libshuff", "initializeGroups");
53                 exit(1);
54         }
55 }
56
57 /***********************************************************************/
58
59 vector<vector<vector<double> > > Libshuff::getSavedMins(){
60         return savedMins;
61 }
62
63 /***********************************************************************/
64
65 vector<double> Libshuff::getMinX(int x){
66         try{
67                 vector<double> minX(groupSizes[x], 0);
68                 for(int i=0;i<groupSizes[x];i++){
69                         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); //get the first value in row i of this block
70                         //minX[i] = matrix->get(groups[x][i], groups[x][0]);
71                         for(int j=0;j<groupSizes[x];j++){
72                                 if(i != j)      {
73                                         double dx = matrix->get(groups[x][i], groups[x][j]);
74                                         if(dx < minX[i]){       minX[i] = dx;   }
75                                 }
76                         }
77                 }
78                 return minX;
79         }
80         catch(exception& e) {
81                 errorOut(e, "Libshuff", "getMinX");
82                 exit(1);
83         }
84 }
85
86 /***********************************************************************/
87
88 vector<double> Libshuff::getMinXY(int x, int y){
89         try{
90                 vector<double> minXY(groupSizes[x], 0);
91
92                 for(int i=0;i<groupSizes[x];i++){
93                         minXY[i] = matrix->get(groups[x][i], groups[y][0]);
94                         for(int j=0;j<groupSizes[y];j++){
95                                 double dxy = matrix->get(groups[x][i], groups[y][j]);
96                                 if(dxy<minXY[i]){       minXY[i] = dxy; }
97                         }
98                 }
99                 return minXY;
100         }
101         catch(exception& e) {
102                 errorOut(e, "Libshuff", "getMinXY");
103                 exit(1);
104         }
105 }
106
107 /***********************************************************************/
108
109 void Libshuff::randomizeGroups(int x, int y){
110         try{
111                 int nv = groupSizes[x]+groupSizes[y];
112                 vector<int> v(nv);
113                 
114                 int index=0;
115                 for(int k=0;k<groupSizes[x];k++)        {       v[index++] = groups[x][k];      }
116                 for(int k=0;k<groupSizes[y];k++)        {       v[index++] = groups[y][k];      }
117                 
118                 for(int k=nv-1;k>0;k--){
119                         int z = (int)(rand() % k);
120                         swap(v[z],v[k]);
121                 }
122                 
123                 index=0;
124                 for(int k=0;k<groupSizes[x];k++)        {       groups[x][k]=v[index++];        }
125                 for(int k=0;k<groupSizes[y];k++)        {       groups[y][k]=v[index++];        }
126         }
127         catch(exception& e) {
128                 errorOut(e, "Libshuff", "randomizeGroups");
129                 exit(1);
130         }
131 }
132
133 /***********************************************************************/
134
135 void Libshuff::resetGroup(int x){
136         
137         for(int k=0;k<groupSizes[x];k++)        {       groups[x][k] = savedGroups[x][k];       }
138         
139 }
140
141 /***********************************************************************/