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