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 >> secondName >> distance; // get the row and column names and distance
53 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
55 map<string,int>::iterator itA = nameMap->find(firstName);
56 map<string,int>::iterator itB = nameMap->find(secondName);
58 if(itA == nameMap->end()){ m->mothurOut("AAError: Sequence '" + firstName + "' was not found in the names file, please correct\n"); exit(1); }
59 if(itB == nameMap->end()){ m->mothurOut("ABError: Sequence '" + secondName + "' was not found in the names file, please correct\n"); exit(1); }
61 if (distance == -1) { distance = 1000000; }
62 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
64 if(distance < cutoff && itA != itB){
65 if(itA->second > itB->second){
66 PDistCell value(itA->second, distance);
69 if(refRow == refCol){ // in other words, if we haven't loaded refRow and refCol...
72 DMatrix->addCell(itB->second, value);
74 else if(refRow == itA->second && refCol == itB->second){
78 DMatrix->addCell(itB->second, value);
81 else if(itA->second < itB->second){
82 PDistCell value(itB->second, distance);
84 if(refRow == refCol){ // in other words, if we haven't loaded refRow and refCol...
87 DMatrix->addCell(itA->second, value);
89 else if(refRow == itB->second && refCol == itA->second){
93 DMatrix->addCell(itA->second, value);
96 reading->update(itA->second * nseqs);
98 m->gobble(fileHandle);
101 if(lt == 0){ // oops, it was square
103 fileHandle.close(); //let's start over
104 DMatrix->clear(); //let's start over
106 m->openInputFile(distFile, fileHandle); //let's start over
109 fileHandle >> firstName >> secondName >> distance;
111 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
113 map<string,int>::iterator itA = nameMap->find(firstName);
114 map<string,int>::iterator itB = nameMap->find(secondName);
116 if(itA == nameMap->end()){ m->mothurOut("AAError: Sequence '" + firstName + "' was not found in the names file, please correct\n"); exit(1); }
117 if(itB == nameMap->end()){ m->mothurOut("ABError: Sequence '" + secondName + "' was not found in the names file, please correct\n"); exit(1); }
119 if (distance == -1) { distance = 1000000; }
120 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
122 if(distance < cutoff && itA->second > itB->second){
123 PDistCell value(itA->second, distance);
124 DMatrix->addCell(itB->second, value);
125 reading->update(itA->second * nseqs);
128 m->gobble(fileHandle);
132 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
142 catch(exception& e) {
143 m->errorOut(e, "ReadColumnMatrix", "read");
147 /***********************************************************************/
149 int ReadColumnMatrix::read(CountTable* countTable){
152 string firstName, secondName;
154 int nseqs = countTable->size();
156 DMatrix->resize(nseqs);
157 list = new ListVector(countTable->getListVector());
159 Progress* reading = new Progress("Reading matrix: ", nseqs * nseqs);
162 int refRow = 0; //we'll keep track of one cell - Cell(refRow,refCol) - and see if it's transpose
163 int refCol = 0; //shows up later - Cell(refCol,refRow). If it does, then its a square matrix
165 //need to see if this is a square or a triangular matrix...
167 while(fileHandle && lt == 1){ //let's assume it's a triangular matrix...
170 fileHandle >> firstName >> secondName >> distance; // get the row and column names and distance
172 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
174 int itA = countTable->get(firstName);
175 int itB = countTable->get(secondName);
177 if (m->control_pressed) { exit(1); }
179 if (distance == -1) { distance = 1000000; }
180 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
182 if(distance < cutoff && itA != itB){
184 PDistCell value(itA, distance);
187 if(refRow == refCol){ // in other words, if we haven't loaded refRow and refCol...
190 DMatrix->addCell(itB, value);
192 else if(refRow == itA && refCol == itB){
196 DMatrix->addCell(itB, value);
200 PDistCell value(itB, distance);
202 if(refRow == refCol){ // in other words, if we haven't loaded refRow and refCol...
205 DMatrix->addCell(itA, value);
207 else if(refRow == itB && refCol == itA){
211 DMatrix->addCell(itA, value);
214 reading->update(itA * nseqs);
216 m->gobble(fileHandle);
219 if(lt == 0){ // oops, it was square
221 fileHandle.close(); //let's start over
222 DMatrix->clear(); //let's start over
224 m->openInputFile(distFile, fileHandle); //let's start over
227 fileHandle >> firstName >> secondName >> distance;
229 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
231 int itA = countTable->get(firstName);
232 int itB = countTable->get(secondName);
235 if (m->control_pressed) { exit(1); }
237 if (distance == -1) { distance = 1000000; }
238 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
240 if(distance < cutoff && itA > itB){
241 PDistCell value(itA, distance);
242 DMatrix->addCell(itB, value);
243 reading->update(itA * nseqs);
246 m->gobble(fileHandle);
250 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
260 catch(exception& e) {
261 m->errorOut(e, "ReadColumnMatrix", "read");
266 /***********************************************************************/
267 ReadColumnMatrix::~ReadColumnMatrix(){}
268 /***********************************************************************/