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;
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.0);
28 group = groupmap->getGroup(name);
29 if(group == "not found") { m->mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); }
30 index.resize(numSeqs);
31 index[0].seqName = name;
32 index[0].groupName = group;
34 //determine if matrix is square or lower triangle
35 //if it is square read the distances for the first sequence
38 while((d=filehandle.get()) != EOF){
40 //is d a number meaning its square
43 filehandle.putback(d);
45 for(int i=0;i<numSeqs;i++){
46 filehandle >> matrix[0][i];
51 //is d a line return meaning its lower triangle
59 if (square == true) { readSquareMatrix(filehandle); }
60 else { readLTMatrix(filehandle); }
62 sortGroups(0, numSeqs-1);
66 m->errorOut(e, "FullMatrix", "FullMatrix");
70 /**************************************************************************/
71 void FullMatrix::readSquareMatrix(ifstream& filehandle) {
75 reading = new Progress("Reading matrix: ", numSeqs * numSeqs);
81 for(int i=1;i<numSeqs;i++){
84 group = groupmap->getGroup(name);
85 index[i].seqName = name;
86 index[i].groupName = group;
88 if(group == "not found") { m->mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); }
90 for(int j=0;j<numSeqs;j++){
91 filehandle >> matrix[i][j];
94 reading->update(count);
100 catch(exception& e) {
101 m->errorOut(e, "FullMatrix", "readSquareMatrix");
105 /**************************************************************************/
106 void FullMatrix::readLTMatrix(ifstream& filehandle) {
109 reading = new Progress("Reading matrix: ", numSeqs * (numSeqs - 1) / 2);
116 for(int i=1;i<numSeqs;i++){
119 group = groupmap->getGroup(name);
120 index[i].seqName = name;
121 index[i].groupName = group;
123 if(group == "not found") { m->mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); }
125 for(int j=0;j<i;j++){
126 filehandle >> distance;
127 matrix[i][j] = distance; matrix[j][i] = distance;
129 reading->update(count);
136 catch(exception& e) {
137 m->errorOut(e, "FullMatrix", "readLTMatrix");
142 /**************************************************************************/
144 void FullMatrix::sortGroups(int low, int high){
150 int pivot = (low+high) / 2;
152 swapRows(low, pivot); //puts pivot in final spot
155 //what group does this row belong to
156 string key = index[low].groupName;
160 /* find member above ... */
161 while((i <= high) && (index[i].groupName <= key)) { i++; }
163 /* find element below ... */
164 while((j >= low) && (index[j].groupName > key)) { j--; }
174 sortGroups(low, j-1);
175 sortGroups(j+1, high);
179 catch(exception& e) {
180 m->errorOut(e, "FullMatrix", "sortGroups");
185 /**************************************************************************/
186 void FullMatrix::swapRows(int i, int j) {
193 for (int h = 0; h < numSeqs; h++) {
195 matrix[i][h] = matrix[j][h];
200 for (int b = 0; b < numSeqs; b++) {
202 matrix[b][i] = matrix[b][j];
207 z = index[i].groupName;
208 index[i].groupName = index[j].groupName;
209 index[j].groupName = z;
211 name = index[i].seqName;
212 index[i].seqName = index[j].seqName;
213 index[j].seqName = name;
217 catch(exception& e) {
218 m->errorOut(e, "FullMatrix", "swapRows");
222 /**************************************************************************/
224 float FullMatrix::get(int i, int j){ return matrix[i][j]; }
226 /**************************************************************************/
228 vector<string> FullMatrix::getGroups(){ return groups; }
230 /**************************************************************************/
232 vector<int> FullMatrix::getSizes(){ return sizes; }
234 /**************************************************************************/
236 int FullMatrix::getNumGroups(){ return groups.size(); }
238 /**************************************************************************/
240 int FullMatrix::getNumSeqs(){ return numSeqs; }
242 /**************************************************************************/
244 void FullMatrix::printMatrix(ostream& out) {
246 for (int i = 0; i < numSeqs; i++) {
247 out << "row " << i << " group = " << index[i].groupName << " name = " << index[i].seqName << endl;
248 for (int j = 0; j < numSeqs; j++) {
249 out << i << '\t' << j << '\t' << matrix[i][j] << endl;
254 for (int i = 0; i < numSeqs; i++) { out << i << '\t' << index[i].seqName << endl; }
256 catch(exception& e) {
257 m->errorOut(e, "FullMatrix", "printMatrix");
262 /**************************************************************************/