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 globaldata = GlobalData::getInstance();
17 groupmap = globaldata->gGroupmap;
20 filehandle >> numSeqs >> name;
22 //make the matrix filled with zeros
23 matrix.resize(numSeqs);
24 for(int i = 0; i < numSeqs; i++) {
25 matrix[i].resize(numSeqs, 0.0);
27 group = groupmap->getGroup(name);
28 if(group == "not found") { mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); mothurOutEndLine(); exit(1); }
29 index.resize(numSeqs);
30 index[0].seqName = name;
31 index[0].groupName = group;
33 //determine if matrix is square or lower triangle
34 //if it is square read the distances for the first sequence
37 while((d=filehandle.get()) != EOF){
39 //is d a number meaning its square
42 filehandle.putback(d);
44 for(int i=0;i<numSeqs;i++){
45 filehandle >> matrix[0][i];
50 //is d a line return meaning its lower triangle
58 if (square == true) { readSquareMatrix(filehandle); }
59 else { readLTMatrix(filehandle); }
61 sortGroups(0, numSeqs-1);
65 errorOut(e, "FullMatrix", "FullMatrix");
69 /**************************************************************************/
70 void FullMatrix::readSquareMatrix(ifstream& filehandle) {
74 reading = new Progress("Reading matrix: ", numSeqs * numSeqs);
80 for(int i=1;i<numSeqs;i++){
83 group = groupmap->getGroup(name);
84 index[i].seqName = name;
85 index[i].groupName = group;
87 if(group == "not found") { mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); mothurOutEndLine(); exit(1); }
89 for(int j=0;j<numSeqs;j++){
90 filehandle >> matrix[i][j];
93 reading->update(count);
100 errorOut(e, "FullMatrix", "readSquareMatrix");
104 /**************************************************************************/
105 void FullMatrix::readLTMatrix(ifstream& filehandle) {
108 reading = new Progress("Reading matrix: ", numSeqs * (numSeqs - 1) / 2);
115 for(int i=1;i<numSeqs;i++){
118 group = groupmap->getGroup(name);
119 index[i].seqName = name;
120 index[i].groupName = group;
122 if(group == "not found") { mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); mothurOutEndLine(); exit(1); }
124 for(int j=0;j<i;j++){
125 filehandle >> distance;
126 matrix[i][j] = distance; matrix[j][i] = distance;
128 reading->update(count);
135 catch(exception& e) {
136 errorOut(e, "FullMatrix", "readLTMatrix");
141 /**************************************************************************/
143 void FullMatrix::sortGroups(int low, int high){
149 int pivot = (low+high) / 2;
151 swapRows(low, pivot); //puts pivot in final spot
154 //what group does this row belong to
155 string key = index[low].groupName;
159 /* find member above ... */
160 while((i <= high) && (index[i].groupName <= key)) { i++; }
162 /* find element below ... */
163 while((j >= low) && (index[j].groupName > key)) { j--; }
173 sortGroups(low, j-1);
174 sortGroups(j+1, high);
178 catch(exception& e) {
179 errorOut(e, "FullMatrix", "sortGroups");
184 /**************************************************************************/
185 void FullMatrix::swapRows(int i, int j) {
192 for (int h = 0; h < numSeqs; h++) {
194 matrix[i][h] = matrix[j][h];
199 for (int b = 0; b < numSeqs; b++) {
201 matrix[b][i] = matrix[b][j];
206 z = index[i].groupName;
207 index[i].groupName = index[j].groupName;
208 index[j].groupName = z;
210 name = index[i].seqName;
211 index[i].seqName = index[j].seqName;
212 index[j].seqName = name;
216 catch(exception& e) {
217 errorOut(e, "FullMatrix", "swapRows");
221 /**************************************************************************/
223 float FullMatrix::get(int i, int j){ return matrix[i][j]; }
225 /**************************************************************************/
227 vector<string> FullMatrix::getGroups(){ return groups; }
229 /**************************************************************************/
231 vector<int> FullMatrix::getSizes(){ return sizes; }
233 /**************************************************************************/
235 int FullMatrix::getNumGroups(){ return groups.size(); }
237 /**************************************************************************/
239 int FullMatrix::getNumSeqs(){ return numSeqs; }
241 /**************************************************************************/
243 void FullMatrix::printMatrix(ostream& out) {
245 for (int i = 0; i < numSeqs; i++) {
246 out << "row " << i << " group = " << index[i].groupName << " name = " << index[i].seqName << endl;
247 for (int j = 0; j < numSeqs; j++) {
248 out << i << '\t' << j << '\t' << matrix[i][j] << endl;
253 for (int i = 0; i < numSeqs; i++) { out << i << '\t' << index[i].seqName << endl; }
255 catch(exception& e) {
256 errorOut(e, "FullMatrix", "printMatrix");
261 /**************************************************************************/