5 * Created by Sarah Westcott on 3/6/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "fullmatrix.h"
12 /**************************************************************************/
14 //This constructor reads a distance matrix file and stores the data in the matrix.
15 FullMatrix::FullMatrix(ifstream& filehandle) {
17 globaldata = GlobalData::getInstance();
18 groupmap = globaldata->gGroupmap;
21 filehandle >> numSeqs >> name;
23 //make the matrix filled with zeros
24 matrix.resize(numSeqs);
25 for(int i = 0; i < numSeqs; i++) {
26 matrix[i].resize(numSeqs, 0);
29 group = groupmap->getGroup(name);
30 if(group == "not found") { mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); mothurOutEndLine(); exit(1); }
31 index.resize(numSeqs);
32 index[0].groupName = group;
33 index[0].seqName = name;
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];
52 //is d a line return meaning its lower triangle
60 if (square == true) { readSquareMatrix(filehandle); }
61 else { readLTMatrix(filehandle); }
63 //sort sequences so they are gathered in groups for processing
64 sortGroups(0, numSeqs-1);
66 groups.push_back(index[0].groupName);
70 for(int i=1;i<numSeqs;i++){
71 if(index[i].groupName == index[i-1].groupName){ sizes[groupCount]++; }
74 groups.push_back(index[i].groupName);
81 errorOut(e, "FullMatrix", "FullMatrix");
85 /**************************************************************************/
86 void FullMatrix::readSquareMatrix(ifstream& filehandle) {
90 reading = new Progress("Reading matrix: ", numSeqs * numSeqs);
96 for(int i=1;i<numSeqs;i++){
99 group = groupmap->getGroup(name);
100 index[i].groupName = group;
101 index[i].seqName = name;
103 if(group == "not found") { mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); mothurOutEndLine(); exit(1); }
105 for(int j=0;j<numSeqs;j++){
106 filehandle >> matrix[i][j];
109 reading->update(count);
115 catch(exception& e) {
116 errorOut(e, "FullMatrix", "readSquareMatrix");
120 /**************************************************************************/
121 void FullMatrix::readLTMatrix(ifstream& filehandle) {
124 reading = new Progress("Reading matrix: ", numSeqs * (numSeqs - 1) / 2);
131 for(int i=1;i<numSeqs;i++){
134 group = groupmap->getGroup(name);
135 index[i].groupName = group;
136 index[i].seqName = name;
138 if(group == "not found") { mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); mothurOutEndLine(); exit(1); }
140 for(int j=0;j<i;j++){
141 filehandle >> distance;
143 matrix[i][j] = distance; matrix[j][i] = distance;
145 reading->update(count);
152 catch(exception& e) {
153 errorOut(e, "FullMatrix", "readLTMatrix");
158 /**************************************************************************/
160 void FullMatrix::sortGroups(int low, int high){
169 //what group does this row belong to
170 string z = index[(low + high) / 2].groupName;
174 /* find member above ... */
175 while(index[i].groupName < z) i++;
177 /* find element below ... */
178 while(index[j].groupName > z) j--;
182 for (int h = 0; h < numSeqs; h++) {
184 matrix[i][h] = matrix[j][h];
189 for (int b = 0; b < numSeqs; b++) {
191 matrix[b][i] = matrix[b][j];
196 z = index[i].groupName;
197 index[i].groupName = index[j].groupName;
198 index[j].groupName = z;
200 name = index[i].seqName;
201 index[i].seqName = index[j].seqName;
202 index[j].seqName = name;
219 catch(exception& e) {
220 errorOut(e, "FullMatrix", "sortGroups");
225 /**************************************************************************/
227 float FullMatrix::get(int i, int j){ return matrix[i][j]; }
229 /**************************************************************************/
231 vector<string> FullMatrix::getGroups(){ return groups; }
233 /**************************************************************************/
235 vector<int> FullMatrix::getSizes(){ return sizes; }
237 /**************************************************************************/
239 int FullMatrix::getNumGroups(){ return groups.size(); }
241 /**************************************************************************/
243 int FullMatrix::getNumSeqs(){ return numSeqs; }
245 /**************************************************************************/
247 void FullMatrix::printMatrix(ostream& out) {
249 for (int i = 0; i < numSeqs; i++) {
250 out << "row " << i << " group = " << index[i].groupName << " name = " << index[i].seqName << endl;
251 for (int j = 0; j < numSeqs; j++) {
252 out << matrix[i][j] << " ";
257 catch(exception& e) {
258 errorOut(e, "FullMatrix", "printMatrix");
263 /**************************************************************************/