File: libshuff.cpp

package info (click to toggle)
mothur 1.48.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,692 kB
  • sloc: cpp: 161,866; makefile: 122; sh: 31
file content (142 lines) | stat: -rwxr-xr-x 3,626 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 *  libshuffform.cpp
 *  Mothur
 *
 *  Created by Pat Schloss on 4/8/09.
 *  Copyright 2009 Patrick D. Schloss. All rights reserved.
 *
 */

#include "libshuff.h"

/***********************************************************************/

void swap(int& i,int& j){	int t = i;  i = j;  j = t;	}

/***********************************************************************/

Libshuff::Libshuff(FullMatrix* D, int it, float step, float co) : matrix(D), iters(it), stepSize(step), cutOff(co){
	try{
		m = MothurOut::getInstance();
		groupNames = matrix->getGroups();
		groupSizes = matrix->getSizes();
		numGroups = matrix->getNumGroups();
		initializeGroups(matrix);
	}
	catch(exception& e) {
		m->errorOut(e, "Libshuff", "Libshuff");
		exit(1);
	}
}

/***********************************************************************/

void Libshuff::initializeGroups(FullMatrix* matrix){
	try{
		groups.resize(numGroups);
		savedGroups.resize(numGroups);
		
		savedGroups.resize(numGroups);
		for(int i=0;i<numGroups;i++) {
			groups[i].resize(groupSizes[i]);
			savedGroups[i].resize(groupSizes[i]);
		}
		int index=0;
		for(int i=0;i<numGroups;i++){

			for(int j=0;j<groupSizes[i];j++){
				savedGroups[i][j] = groups[i][j] = index++;
			}
		}
	}
	catch(exception& e) {
		m->errorOut(e, "Libshuff", "initializeGroups");
		exit(1);
	}
}

/***********************************************************************/

vector<vector<vector<double> > > Libshuff::getSavedMins(){
	return savedMins;
}

/***********************************************************************/

vector<double> Libshuff::getMinX(int x){
	try{
		vector<double> minX(groupSizes[x], 0);
		for(int i=0;i<groupSizes[x];i++){
			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
			//minX[i] = matrix->get(groups[x][i], groups[x][0]);
			for(int j=0;j<groupSizes[x];j++){
				if(i != j)	{
					double dx = matrix->get(groups[x][i], groups[x][j]);
					if(dx < minX[i]){	minX[i] = dx;	}
				}
			}
		}
		return minX;
	}
	catch(exception& e) {
		m->errorOut(e, "Libshuff", "getMinX");
		exit(1);
	}
}

/***********************************************************************/

vector<double> Libshuff::getMinXY(int x, int y){
	try{
		vector<double> minXY(groupSizes[x], 0);

		for(int i=0;i<groupSizes[x];i++){
			minXY[i] = matrix->get(groups[x][i], groups[y][0]);
			for(int j=0;j<groupSizes[y];j++){
				double dxy = matrix->get(groups[x][i], groups[y][j]);
				if(dxy<minXY[i]){	minXY[i] = dxy;	}
			}
		}
		return minXY;
	}
	catch(exception& e) {
		m->errorOut(e, "Libshuff", "getMinXY");
		exit(1);
	}
}

/***********************************************************************/

void Libshuff::randomizeGroups(int x, int y){
	try{
		int nv = groupSizes[x]+groupSizes[y];
		vector<int> v(nv);
		
		int index=0;
		for(int k=0;k<groupSizes[x];k++)	{	v[index++] = groups[x][k];	}
		for(int k=0;k<groupSizes[y];k++)	{	v[index++] = groups[y][k];	}
		Utils util;
		for(int k=nv-1;k>0;k--){
			int z = util.getRandomIndex(k);
			swap(v[z],v[k]);
		}
		
		index=0;
		for(int k=0;k<groupSizes[x];k++)	{	groups[x][k]=v[index++];	}
		for(int k=0;k<groupSizes[y];k++)	{	groups[y][k]=v[index++];	}
	}
	catch(exception& e) {
		m->errorOut(e, "Libshuff", "randomizeGroups");
		exit(1);
	}
}

/***********************************************************************/

void Libshuff::resetGroup(int x){
	
	for(int k=0;k<groupSizes[x];k++)	{	groups[x][k] = savedGroups[x][k];	}
	
}

/***********************************************************************/