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()){
59 cerr << "AAError: Sequence '" << firstName << "' was not found in the names file, please correct\n"; exit(1);
61 if(itB == nameMap->end()){
62 cerr << "ABError: Sequence '" << secondName << "' was not found in the names file, please correct\n"; exit(1);
64 //if (((itA->second == 8) && (itB->second == 1588)) || ((itA->second == 1588) && (itB->second == 8))) { cout << "found it" << endl; }
66 if (distance == -1) { distance = 1000000; }
67 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
69 if(distance < cutoff && itA != itB){
70 if(itA->second > itB->second){
71 PCell value(itA->second, itB->second, distance);
73 if(refRow == refCol){ // in other words, if we haven't loaded refRow and refCol...
78 else if(refRow == itA->second && refCol == itB->second){
85 else if(itA->second < itB->second){
86 PCell value(itB->second, itA->second, distance);
88 if(refRow == refCol){ // in other words, if we haven't loaded refRow and refCol...
93 else if(refRow == itB->second && refCol == itA->second){
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 D->clear(); //let's start over
110 m->openInputFile(distFile, fileHandle); //let's start over
113 fileHandle >> firstName >> secondName >> distance;
115 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
117 map<string,int>::iterator itA = nameMap->find(firstName);
118 map<string,int>::iterator itB = nameMap->find(secondName);
120 if(itA == nameMap->end()){
121 cerr << "BError: Sequence '" << firstName << "' was not found in the names file, please correct\n";
123 if(itB == nameMap->end()){
124 cerr << "BError: Sequence '" << secondName << "' was not found in the names file, please correct\n";
127 if (distance == -1) { distance = 1000000; }
128 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
130 if(distance < cutoff && itA->second > itB->second){
131 PCell value(itA->second, itB->second, distance);
133 reading->update(itA->second * nseqs);
136 m->gobble(fileHandle);
140 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
150 catch(exception& e) {
151 m->errorOut(e, "ReadColumnMatrix", "read");
156 /***********************************************************************/
158 ReadColumnMatrix::~ReadColumnMatrix(){