5 * Created by Sarah Westcott on 4/21/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "readcolumn.h"
11 #include "progress.hpp"
13 /***********************************************************************/
15 ReadColumnMatrix::ReadColumnMatrix(string df) : distFile(df){
17 successOpen = m->openInputFile(distFile, fileHandle);
21 /***********************************************************************/
23 ReadColumnMatrix::ReadColumnMatrix(string df, bool s) : distFile(df){
25 successOpen = m->openInputFile(distFile, fileHandle);
29 /***********************************************************************/
31 int ReadColumnMatrix::read(NameAssignment* nameMap){
34 string firstName, secondName;
36 int nseqs = nameMap->size();
37 DMatrix->resize(nseqs);
38 list = new ListVector(nameMap->getListVector());
40 Progress* reading = new Progress("Reading matrix: ", nseqs * nseqs);
43 int refRow = 0; //we'll keep track of one cell - Cell(refRow,refCol) - and see if it's transpose
44 int refCol = 0; //shows up later - Cell(refCol,refRow). If it does, then its a square matrix
46 //need to see if this is a square or a triangular matrix...
48 while(fileHandle && lt == 1){ //let's assume it's a triangular matrix...
51 fileHandle >> firstName; m->gobble(fileHandle);
52 fileHandle >> secondName; m->gobble(fileHandle);
53 fileHandle >> distance; // get the row and column names and distance
55 if (m->debug) { cout << firstName << '\t' << secondName << '\t' << distance << endl; }
57 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
59 map<string,int>::iterator itA = nameMap->find(firstName);
60 map<string,int>::iterator itB = nameMap->find(secondName);
62 if(itA == nameMap->end()){ m->mothurOut("AAError: Sequence '" + firstName + "' was not found in the names file, please correct\n"); exit(1); }
63 if(itB == nameMap->end()){ m->mothurOut("ABError: Sequence '" + secondName + "' was not found in the names file, please correct\n"); exit(1); }
65 if (distance == -1) { distance = 1000000; }
66 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
68 if(distance < cutoff && itA != itB){
69 if(itA->second > itB->second){
70 PDistCell value(itA->second, distance);
73 if(refRow == refCol){ // in other words, if we haven't loaded refRow and refCol...
76 DMatrix->addCell(itB->second, value);
78 else if(refRow == itA->second && refCol == itB->second){
82 DMatrix->addCell(itB->second, value);
85 else if(itA->second < itB->second){
86 PDistCell value(itB->second, distance);
88 if(refRow == refCol){ // in other words, if we haven't loaded refRow and refCol...
91 DMatrix->addCell(itA->second, value);
93 else if(refRow == itB->second && refCol == itA->second){
97 DMatrix->addCell(itA->second, value);
100 reading->update(itA->second * nseqs);
102 m->gobble(fileHandle);
105 if(lt == 0){ // oops, it was square
107 fileHandle.close(); //let's start over
108 DMatrix->clear(); //let's start over
110 m->openInputFile(distFile, fileHandle); //let's start over
113 fileHandle >> firstName; m->gobble(fileHandle);
114 fileHandle >> secondName; m->gobble(fileHandle);
115 fileHandle >> distance; // get the row and column names and distance
117 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
119 map<string,int>::iterator itA = nameMap->find(firstName);
120 map<string,int>::iterator itB = nameMap->find(secondName);
122 if(itA == nameMap->end()){ m->mothurOut("AAError: Sequence '" + firstName + "' was not found in the names file, please correct\n"); exit(1); }
123 if(itB == nameMap->end()){ m->mothurOut("ABError: Sequence '" + secondName + "' was not found in the names file, please correct\n"); exit(1); }
125 if (distance == -1) { distance = 1000000; }
126 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
128 if(distance < cutoff && itA->second > itB->second){
129 PDistCell value(itA->second, distance);
130 DMatrix->addCell(itB->second, value);
131 reading->update(itA->second * nseqs);
134 m->gobble(fileHandle);
138 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
148 catch(exception& e) {
149 m->errorOut(e, "ReadColumnMatrix", "read");
153 /***********************************************************************/
155 int ReadColumnMatrix::read(CountTable* countTable){
158 string firstName, secondName;
160 int nseqs = countTable->size();
162 DMatrix->resize(nseqs);
163 list = new ListVector(countTable->getListVector());
165 Progress* reading = new Progress("Reading matrix: ", nseqs * nseqs);
168 int refRow = 0; //we'll keep track of one cell - Cell(refRow,refCol) - and see if it's transpose
169 int refCol = 0; //shows up later - Cell(refCol,refRow). If it does, then its a square matrix
171 //need to see if this is a square or a triangular matrix...
173 while(fileHandle && lt == 1){ //let's assume it's a triangular matrix...
176 fileHandle >> firstName; m->gobble(fileHandle);
177 fileHandle >> secondName; m->gobble(fileHandle);
178 fileHandle >> distance; // get the row and column names and distance
180 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
182 int itA = countTable->get(firstName);
183 int itB = countTable->get(secondName);
185 if (m->control_pressed) { exit(1); }
187 if (distance == -1) { distance = 1000000; }
188 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
190 if(distance < cutoff && itA != itB){
192 PDistCell value(itA, distance);
195 if(refRow == refCol){ // in other words, if we haven't loaded refRow and refCol...
198 DMatrix->addCell(itB, value);
200 else if(refRow == itA && refCol == itB){
204 DMatrix->addCell(itB, value);
208 PDistCell value(itB, distance);
210 if(refRow == refCol){ // in other words, if we haven't loaded refRow and refCol...
213 DMatrix->addCell(itA, value);
215 else if(refRow == itB && refCol == itA){
219 DMatrix->addCell(itA, value);
222 reading->update(itA * nseqs);
224 m->gobble(fileHandle);
227 if(lt == 0){ // oops, it was square
229 fileHandle.close(); //let's start over
230 DMatrix->clear(); //let's start over
232 m->openInputFile(distFile, fileHandle); //let's start over
235 fileHandle >> firstName; m->gobble(fileHandle);
236 fileHandle >> secondName; m->gobble(fileHandle);
237 fileHandle >> distance; // get the row and column names and distance
239 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
241 int itA = countTable->get(firstName);
242 int itB = countTable->get(secondName);
245 if (m->control_pressed) { exit(1); }
247 if (distance == -1) { distance = 1000000; }
248 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
250 if(distance < cutoff && itA > itB){
251 PDistCell value(itA, distance);
252 DMatrix->addCell(itB, value);
253 reading->update(itA * nseqs);
256 m->gobble(fileHandle);
260 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
270 catch(exception& e) {
271 m->errorOut(e, "ReadColumnMatrix", "read");
276 /***********************************************************************/
277 ReadColumnMatrix::~ReadColumnMatrix(){}
278 /***********************************************************************/