5 * Created by Sarah Westcott on 4/21/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "readphylip.h"
11 #include "progress.hpp"
13 /***********************************************************************/
15 ReadPhylipMatrix::ReadPhylipMatrix(string distFile){
17 successOpen = m->openInputFile(distFile, fileHandle);
21 /***********************************************************************/
23 ReadPhylipMatrix::ReadPhylipMatrix(string distFile, bool s){
25 successOpen = m->openInputFile(distFile, fileHandle);
30 /***********************************************************************/
32 int ReadPhylipMatrix::read(NameAssignment* nameMap){
38 vector<string> matrixNames;
41 fileHandle >> numTest >> name;
43 if (!m->isContainingOnlyDigits(numTest)) { m->mothurOut("[ERROR]: expected a number and got " + numTest + ", quitting."); m->mothurOutEndLine(); exit(1); }
44 else { convert(numTest, nseqs); }
46 matrixNames.push_back(name);
49 list = new ListVector(nseqs);
53 list = new ListVector(nameMap->getListVector());
54 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
58 while((d=fileHandle.get()) != EOF){
62 fileHandle.putback(d);
63 for(int i=0;i<nseqs;i++){
64 fileHandle >> distance;
75 DMatrix->resize(nseqs);
79 reading = new Progress("Reading matrix: ", nseqs * (nseqs - 1) / 2);
83 for(int i=1;i<nseqs;i++){
84 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
87 matrixNames.push_back(name);
90 //there's A LOT of repeated code throughout this method...
96 if (m->control_pressed) { delete reading; fileHandle.close(); return 0; }
98 fileHandle >> distance;
100 if (distance == -1) { distance = 1000000; }
101 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
103 if(distance < cutoff){
104 PDistCell value(i, distance);
105 DMatrix->addCell(j, value);
108 reading->update(index);
113 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
115 for(int j=0;j<i;j++){
116 fileHandle >> distance;
118 if (m->control_pressed) { delete reading; fileHandle.close(); return 0; }
120 if (distance == -1) { distance = 1000000; }
121 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
123 if(distance < cutoff){
124 PDistCell value(nameMap->get(matrixNames[i]), distance);
125 DMatrix->addCell(nameMap->get(matrixNames[j]), value);
128 reading->update(index);
135 reading = new Progress("Reading matrix: ", nseqs * nseqs);
139 for(int i=1;i<nseqs;i++){
141 matrixNames.push_back(name);
147 for(int j=0;j<nseqs;j++){
148 fileHandle >> distance;
150 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
152 if (distance == -1) { distance = 1000000; }
153 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
155 if(distance < cutoff && j < i){
156 PDistCell value(i, distance);
157 DMatrix->addCell(j, value);
160 reading->update(index);
165 if(nameMap->count(name)==0){ m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
167 for(int j=0;j<nseqs;j++){
168 fileHandle >> distance;
170 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
172 if (distance == -1) { distance = 1000000; }
173 else if (sim) { distance = 1.0 - distance; } //user has entered a sim matrix that we need to convert.
175 if(distance < cutoff && j < i){
176 PDistCell value(nameMap->get(matrixNames[i]), distance);
177 DMatrix->addCell(nameMap->get(matrixNames[j]), value);
180 reading->update(index);
186 if (m->control_pressed) { fileHandle.close(); delete reading; return 0; }
198 catch(exception& e) {
199 m->errorOut(e, "ReadPhylipMatrix", "read");
204 /***********************************************************************/
205 ReadPhylipMatrix::~ReadPhylipMatrix(){}
206 /***********************************************************************/