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, GroupMap* g, bool s) : groupmap(g), sim(s) {
16 m = MothurOut::getInstance();
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") { m->mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); m->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];
46 if (sim) { matrix[0][i] = 1.0 - 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];
96 if (sim) { matrix[i][j] = 1.0 - matrix[i][j]; }
99 reading->update(count);
103 if (m->control_pressed) { delete reading; return 0; }
110 catch(exception& e) {
111 m->errorOut(e, "FullMatrix", "readSquareMatrix");
115 /**************************************************************************/
116 int FullMatrix::readLTMatrix(ifstream& filehandle) {
120 reading = new Progress("Reading matrix: ", numSeqs * (numSeqs - 1) / 2);
127 for(int i=1;i<numSeqs;i++){
130 group = groupmap->getGroup(name);
131 index[i].seqName = name;
132 index[i].groupName = group;
134 if(group == "not found") { m->mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); }
136 for(int j=0;j<i;j++){
137 if (m->control_pressed) { delete reading; return 0; }
139 filehandle >> distance;
140 if (sim) { distance = 1.0 - distance; }
142 matrix[i][j] = distance; matrix[j][i] = distance;
145 reading->update(count);
149 if (m->control_pressed) { delete reading; return 0; }
156 catch(exception& e) {
157 m->errorOut(e, "FullMatrix", "readLTMatrix");
162 /**************************************************************************/
164 void FullMatrix::sortGroups(int low, int high){
170 int pivot = (low+high) / 2;
172 swapRows(low, pivot); //puts pivot in final spot
175 //what group does this row belong to
176 string key = index[low].groupName;
180 /* find member above ... */
181 while((i <= high) && (index[i].groupName <= key)) { i++; }
183 /* find element below ... */
184 while((j >= low) && (index[j].groupName > key)) { j--; }
194 sortGroups(low, j-1);
195 sortGroups(j+1, high);
199 catch(exception& e) {
200 m->errorOut(e, "FullMatrix", "sortGroups");
205 /**************************************************************************/
206 void FullMatrix::swapRows(int i, int j) {
213 for (int h = 0; h < numSeqs; h++) {
215 matrix[i][h] = matrix[j][h];
220 for (int b = 0; b < numSeqs; b++) {
222 matrix[b][i] = matrix[b][j];
227 z = index[i].groupName;
228 index[i].groupName = index[j].groupName;
229 index[j].groupName = z;
231 name = index[i].seqName;
232 index[i].seqName = index[j].seqName;
233 index[j].seqName = name;
237 catch(exception& e) {
238 m->errorOut(e, "FullMatrix", "swapRows");
242 /**************************************************************************/
244 float FullMatrix::get(int i, int j){ return matrix[i][j]; }
246 /**************************************************************************/
248 vector<string> FullMatrix::getGroups(){ return groups; }
250 /**************************************************************************/
252 vector<int> FullMatrix::getSizes(){ return sizes; }
254 /**************************************************************************/
256 int FullMatrix::getNumGroups(){ return groups.size(); }
258 /**************************************************************************/
260 int FullMatrix::getNumSeqs(){ return numSeqs; }
262 /**************************************************************************/
264 void FullMatrix::printMatrix(ostream& out) {
266 for (int i = 0; i < numSeqs; i++) {
267 out << "row " << i << " group = " << index[i].groupName << " name = " << index[i].seqName << endl;
268 for (int j = 0; j < numSeqs; j++) {
269 out << i << '\t' << j << '\t' << matrix[i][j] << endl;
274 for (int i = 0; i < numSeqs; i++) { out << i << '\t' << index[i].seqName << endl; }
276 catch(exception& e) {
277 m->errorOut(e, "FullMatrix", "printMatrix");
282 /**************************************************************************/