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();
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 PCell value(itA->second, itB->second, distance);
68 if(refRow == refCol){ // in other words, if we haven't loaded refRow and refCol...
73 else if(refRow == itA->second && refCol == itB->second){
80 else if(itA->second < itB->second){
81 PCell value(itB->second, itA->second, distance);
83 if(refRow == refCol){ // in other words, if we haven't loaded refRow and refCol...
88 else if(refRow == itB->second && refCol == itA->second){
95 reading->update(itA->second * nseqs);
97 m->gobble(fileHandle);
100 if(lt == 0){ // oops, it was square
102 fileHandle.close(); //let's start over
103 D->clear(); //let's start over
105 m->openInputFile(distFile, fileHandle); //let's start over
108 fileHandle >> firstName >> secondName >> distance;
110 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
112 map<string,int>::iterator itA = nameMap->find(firstName);
113 map<string,int>::iterator itB = nameMap->find(secondName);
115 if(itA == nameMap->end()){ m->mothurOut("AAError: Sequence '" + firstName + "' was not found in the names file, please correct\n"); exit(1); }
116 if(itB == nameMap->end()){ m->mothurOut("ABError: Sequence '" + secondName + "' was not found in the names file, please correct\n"); exit(1); }
118 if (distance == -1) { distance = 1000000; }
119 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
121 if(distance < cutoff && itA->second > itB->second){
122 PCell value(itA->second, itB->second, distance);
124 reading->update(itA->second * nseqs);
127 m->gobble(fileHandle);
131 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
141 catch(exception& e) {
142 m->errorOut(e, "ReadColumnMatrix", "read");
147 /***********************************************************************/
149 ReadColumnMatrix::~ReadColumnMatrix(){