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); }
64 if (!m->control_pressed) { sortGroups(0, numSeqs-1); }
68 m->errorOut(e, "FullMatrix", "FullMatrix");
72 /**************************************************************************/
73 int FullMatrix::readSquareMatrix(ifstream& filehandle) {
77 reading = new Progress("Reading matrix: ", numSeqs * numSeqs);
83 for(int i=1;i<numSeqs;i++){
86 group = groupmap->getGroup(name);
87 index[i].seqName = name;
88 index[i].groupName = group;
90 if(group == "not found") { m->mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); }
92 for(int j=0;j<numSeqs;j++){
93 if (m->control_pressed) { delete reading; return 0; }
95 filehandle >> matrix[i][j];
98 reading->update(count);
102 if (m->control_pressed) { delete reading; return 0; }
109 catch(exception& e) {
110 m->errorOut(e, "FullMatrix", "readSquareMatrix");
114 /**************************************************************************/
115 int FullMatrix::readLTMatrix(ifstream& filehandle) {
118 reading = new Progress("Reading matrix: ", numSeqs * (numSeqs - 1) / 2);
125 for(int i=1;i<numSeqs;i++){
128 group = groupmap->getGroup(name);
129 index[i].seqName = name;
130 index[i].groupName = group;
132 if(group == "not found") { m->mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); }
134 for(int j=0;j<i;j++){
135 if (m->control_pressed) { delete reading; return 0; }
137 filehandle >> distance;
139 matrix[i][j] = distance; matrix[j][i] = distance;
141 reading->update(count);
145 if (m->control_pressed) { delete reading; return 0; }
152 catch(exception& e) {
153 m->errorOut(e, "FullMatrix", "readLTMatrix");
158 /**************************************************************************/
160 void FullMatrix::sortGroups(int low, int high){
166 int pivot = (low+high) / 2;
168 swapRows(low, pivot); //puts pivot in final spot
171 //what group does this row belong to
172 string key = index[low].groupName;
176 /* find member above ... */
177 while((i <= high) && (index[i].groupName <= key)) { i++; }
179 /* find element below ... */
180 while((j >= low) && (index[j].groupName > key)) { j--; }
190 sortGroups(low, j-1);
191 sortGroups(j+1, high);
195 catch(exception& e) {
196 m->errorOut(e, "FullMatrix", "sortGroups");
201 /**************************************************************************/
202 void FullMatrix::swapRows(int i, int j) {
209 for (int h = 0; h < numSeqs; h++) {
211 matrix[i][h] = matrix[j][h];
216 for (int b = 0; b < numSeqs; b++) {
218 matrix[b][i] = matrix[b][j];
223 z = index[i].groupName;
224 index[i].groupName = index[j].groupName;
225 index[j].groupName = z;
227 name = index[i].seqName;
228 index[i].seqName = index[j].seqName;
229 index[j].seqName = name;
233 catch(exception& e) {
234 m->errorOut(e, "FullMatrix", "swapRows");
238 /**************************************************************************/
240 float FullMatrix::get(int i, int j){ return matrix[i][j]; }
242 /**************************************************************************/
244 vector<string> FullMatrix::getGroups(){ return groups; }
246 /**************************************************************************/
248 vector<int> FullMatrix::getSizes(){ return sizes; }
250 /**************************************************************************/
252 int FullMatrix::getNumGroups(){ return groups.size(); }
254 /**************************************************************************/
256 int FullMatrix::getNumSeqs(){ return numSeqs; }
258 /**************************************************************************/
260 void FullMatrix::printMatrix(ostream& out) {
262 for (int i = 0; i < numSeqs; i++) {
263 out << "row " << i << " group = " << index[i].groupName << " name = " << index[i].seqName << endl;
264 for (int j = 0; j < numSeqs; j++) {
265 out << i << '\t' << j << '\t' << matrix[i][j] << endl;
270 for (int i = 0; i < numSeqs; i++) { out << i << '\t' << index[i].seqName << endl; }
272 catch(exception& e) {
273 m->errorOut(e, "FullMatrix", "printMatrix");
278 /**************************************************************************/