5 * Created by Sarah Westcott on 3/6/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "fullmatrix.h"
12 /**************************************************************************/
13 //This constructor reads a distance matrix file and stores the data in the matrix.
14 FullMatrix::FullMatrix(ifstream& filehandle) {
16 m = MothurOut::getInstance();
17 globaldata = GlobalData::getInstance();
18 groupmap = globaldata->gGroupmap;
22 filehandle >> numSeqs >> name;
24 //make the matrix filled with zeros
25 matrix.resize(numSeqs);
26 for(int i = 0; i < numSeqs; i++) {
27 matrix[i].resize(numSeqs, 0.0);
29 group = groupmap->getGroup(name);
30 if(group == "not found") { m->mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); }
31 index.resize(numSeqs);
32 index[0].seqName = name;
33 index[0].groupName = group;
35 //determine if matrix is square or lower triangle
36 //if it is square read the distances for the first sequence
39 while((d=filehandle.get()) != EOF){
41 //is d a number meaning its square
44 filehandle.putback(d);
46 for(int i=0;i<numSeqs;i++){
47 filehandle >> matrix[0][i];
48 if (globaldata->sim) { matrix[0][i] = 1.0 - matrix[0][i]; }
53 //is d a line return meaning its lower triangle
61 if (square == true) { readSquareMatrix(filehandle); }
62 else { readLTMatrix(filehandle); }
66 if (!m->control_pressed) { sortGroups(0, numSeqs-1); }
70 m->errorOut(e, "FullMatrix", "FullMatrix");
74 /**************************************************************************/
75 int FullMatrix::readSquareMatrix(ifstream& filehandle) {
79 reading = new Progress("Reading matrix: ", numSeqs * numSeqs);
85 for(int i=1;i<numSeqs;i++){
88 group = groupmap->getGroup(name);
89 index[i].seqName = name;
90 index[i].groupName = group;
92 if(group == "not found") { m->mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); }
94 for(int j=0;j<numSeqs;j++){
95 if (m->control_pressed) { delete reading; return 0; }
97 filehandle >> matrix[i][j];
98 if (globaldata->sim) { matrix[i][j] = 1.0 - matrix[i][j]; }
101 reading->update(count);
105 if (m->control_pressed) { delete reading; return 0; }
112 catch(exception& e) {
113 m->errorOut(e, "FullMatrix", "readSquareMatrix");
117 /**************************************************************************/
118 int FullMatrix::readLTMatrix(ifstream& filehandle) {
122 reading = new Progress("Reading matrix: ", numSeqs * (numSeqs - 1) / 2);
129 for(int i=1;i<numSeqs;i++){
132 group = groupmap->getGroup(name);
133 index[i].seqName = name;
134 index[i].groupName = group;
136 if(group == "not found") { m->mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); }
138 for(int j=0;j<i;j++){
139 if (m->control_pressed) { delete reading; return 0; }
141 filehandle >> distance;
142 if (globaldata->sim) { distance = 1.0 - distance; }
144 matrix[i][j] = distance; matrix[j][i] = distance;
147 reading->update(count);
151 if (m->control_pressed) { delete reading; return 0; }
158 catch(exception& e) {
159 m->errorOut(e, "FullMatrix", "readLTMatrix");
164 /**************************************************************************/
166 void FullMatrix::sortGroups(int low, int high){
172 int pivot = (low+high) / 2;
174 swapRows(low, pivot); //puts pivot in final spot
177 //what group does this row belong to
178 string key = index[low].groupName;
182 /* find member above ... */
183 while((i <= high) && (index[i].groupName <= key)) { i++; }
185 /* find element below ... */
186 while((j >= low) && (index[j].groupName > key)) { j--; }
196 sortGroups(low, j-1);
197 sortGroups(j+1, high);
201 catch(exception& e) {
202 m->errorOut(e, "FullMatrix", "sortGroups");
207 /**************************************************************************/
208 void FullMatrix::swapRows(int i, int j) {
215 for (int h = 0; h < numSeqs; h++) {
217 matrix[i][h] = matrix[j][h];
222 for (int b = 0; b < numSeqs; b++) {
224 matrix[b][i] = matrix[b][j];
229 z = index[i].groupName;
230 index[i].groupName = index[j].groupName;
231 index[j].groupName = z;
233 name = index[i].seqName;
234 index[i].seqName = index[j].seqName;
235 index[j].seqName = name;
239 catch(exception& e) {
240 m->errorOut(e, "FullMatrix", "swapRows");
244 /**************************************************************************/
246 float FullMatrix::get(int i, int j){ return matrix[i][j]; }
248 /**************************************************************************/
250 vector<string> FullMatrix::getGroups(){ return groups; }
252 /**************************************************************************/
254 vector<int> FullMatrix::getSizes(){ return sizes; }
256 /**************************************************************************/
258 int FullMatrix::getNumGroups(){ return groups.size(); }
260 /**************************************************************************/
262 int FullMatrix::getNumSeqs(){ return numSeqs; }
264 /**************************************************************************/
266 void FullMatrix::printMatrix(ostream& out) {
268 for (int i = 0; i < numSeqs; i++) {
269 out << "row " << i << " group = " << index[i].groupName << " name = " << index[i].seqName << endl;
270 for (int j = 0; j < numSeqs; j++) {
271 out << i << '\t' << j << '\t' << matrix[i][j] << endl;
276 for (int i = 0; i < numSeqs; i++) { out << i << '\t' << index[i].seqName << endl; }
278 catch(exception& e) {
279 m->errorOut(e, "FullMatrix", "printMatrix");
284 /**************************************************************************/